unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Gabriele Nicolardi <gabriele@medialab.sissa.it>
To: Juri Linkov <juri@linkov.net>
Cc: 69542@debbugs.gnu.org
Subject: bug#69542: Feature request: making occur obey isearch-filter-predicate
Date: Tue, 5 Mar 2024 18:11:37 +0100	[thread overview]
Message-ID: <ae3e8b4d-2413-4d11-b6bb-3233bbb734d7@medialab.sissa.it> (raw)
In-Reply-To: <86msrdnjdn.fsf@mail.linkov.net>

[-- Attachment #1: Type: text/plain, Size: 2809 bytes --]

> I think duplicating the whole body of functions is not better
> than using advice.

Until yesterday, I had no idea how to advise a function.

Now I'm experimenting with this code to obtain modified versions of 
|occur| and |how-many| that are sensitive to |isearch-filter-predicate|.

I'm not a programmer, so my code may not be very rigorous:

;; I need a copy of `re-search-forward' because it will be temporary 
adviced in
;; `how-many-ifp' and `occur-ifp' functions:
(defalias 're-search-forward-copy
   (symbol-function 're-search-forward)
   "Copy of `re-search-forward'")

(defun re-search-forward-ifp (REGEXP &optional BOUND NOERROR COUNT)
   "Modified version of `search-forward-regexp' that filters (skips) matches
according to `isearch-filter-predicate'."
   (let ((POINT (point)))
     (catch 'filtered
       (while (re-search-forward-copy REGEXP BOUND NOERROR COUNT)
         (let ((B (match-beginning 0))
               (E (match-end 0)))
           (when (funcall isearch-filter-predicate B E)
             (throw 'filtered (point)))))
       (goto-char POINT)
       nil)))
(defalias 'search-forward-regexp-ifp 're-search-forward-ifp)

(defun override-re-search-forward (orig-fun &rest args)
   "Temporary advice the `re-search-forward' function, overriding it with
`re-search-forward-ifp' to make it sensitive to `isearch-filter-predicate'"
   (unwind-protect
       (progn
         (advice-add 're-search-forward :override #'re-search-forward-ifp)
         (apply orig-fun args))
     (advice-remove 're-search-forward #'re-search-forward-ifp)))

(defalias 'how-many-ifp
   (symbol-function 'how-many)
   "Copy of the `how-many' function (to be) adviced to obey to
`isearch-filter-predicate'")
(defalias 'count-matches-ifp 'how-many-ifp)

(advice-add 'how-many-ifp :around #'override-re-search-forward)

(defalias 'occur-ifp
   (symbol-function 'occur)
   "Copy of the `occur' function (to be) adviced to obey to
  `isearch-filter-predicate'")

(advice-add 'occur-ifp :around #'override-re-search-forward)

The key is the `re-search-forward` function used in `how-many` and 
`occur` definition. If you make it sensible to 
`isearch-filter-predicate` also `how-many` and `occur` will be.

I don't know if advicing a function to advice a function inside it is a 
good idea but it seem to work (I need more tests).

Now I discovered how to build these functions I could be fine with my 
modified ones, but making this feature avaible (maybe not default) in 
the original functions could be a better idea.

>   An alternative would be to add a new variable
> 're-search-forward-function' and then to use it like
> '(funcall re-search-forward-function)'.

The idea of a new function seems good to me.


[-- Attachment #2: Type: text/html, Size: 3955 bytes --]

  reply	other threads:[~2024-03-05 17:11 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-04 11:44 bug#69542: Feature request: making occur obey isearch-filter-predicate Gabriele Nicolardi
2024-03-05 16:30 ` Juri Linkov
2024-03-05 17:11   ` Gabriele Nicolardi [this message]
2024-03-06 17:47     ` Juri Linkov
2024-03-08 23:09     ` Gabriele Nicolardi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ae3e8b4d-2413-4d11-b6bb-3233bbb734d7@medialab.sissa.it \
    --to=gabriele@medialab.sissa.it \
    --cc=69542@debbugs.gnu.org \
    --cc=juri@linkov.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).