unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Gabriele Nicolardi <gabriele@medialab.sissa.it>
To: Juri Linkov <juri@linkov.net>,
	"Basil L. Contovounesios" <basil@contovou.net>
Cc: emacs-devel@gnu.org
Subject: Re: Why don't `occur`, `search-forward*` and `string-match*` respect `isearch-filter-predicate`?
Date: Mon, 4 Mar 2024 15:44:40 +0100	[thread overview]
Message-ID: <c70e28cb-d7a9-4805-990e-1ccaa3a3a462@medialab.sissa.it> (raw)
In-Reply-To: <86cysfky3l.fsf@mail.linkov.net>

This code I wrote seems to work:

;; see https://emacs.stackexchange.com/a/80612/15606

(let ((re-search-forward-copy (symbol-function '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 (funcall 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 how-many-ifp (regexp &optional rstart rend interactive)
   "Modified version of `how-many' that filters (skips) matches according to
`isearch-filter-predicate'."
   (interactive
    (keep-lines-read-args "How many matches for regexp"))
   (unwind-protect
       (progn
         (advice-add 're-search-forward :override #'re-search-forward-ifp)
         (how-many regexp rstart rend interactive))
     (advice-remove 're-search-forward #'re-search-forward-ifp)))
(defalias 'count-matches-ifp 'how-many-ifp)

(defun occur-ifp (regexp &optional nlines region)
   "Modified version of `occur' that filters (skips) matches according to
`isearch-filter-predicate'."
   (interactive
    (nconc (occur-read-primary-args)
           (and (use-region-p) (list (region-bounds)))))
   (unwind-protect
       (progn
         (advice-add 're-search-forward :override #'re-search-forward-ifp)
         (occur regexp nlines region))
     (advice-remove 're-search-forward #'re-search-forward-ifp)))

These are modified versions of re-search-forward, how-many and occur 
functions that obey to isearch-filter-pradicate but I am not entirely 
certain if I have written the code rigorously.

Perhaps they could be a starting point.




      reply	other threads:[~2024-03-04 14:44 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-26 18:02 Why don't `occur`, `search-forward*` and `string-match*` respect `isearch-filter-predicate`? Gabriele Nicolardi
2024-02-28 14:06 ` Basil L. Contovounesios
2024-02-28 14:18   ` Alfred M. Szmidt
2024-02-28 14:24   ` Gabriele Nicolardi
2024-02-28 16:53     ` [External] : " Drew Adams
2024-02-28 17:19       ` Gabriele Nicolardi
2024-02-29 17:47   ` Juri Linkov
2024-03-04 14:44     ` Gabriele Nicolardi [this message]

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=c70e28cb-d7a9-4805-990e-1ccaa3a3a462@medialab.sissa.it \
    --to=gabriele@medialab.sissa.it \
    --cc=basil@contovou.net \
    --cc=emacs-devel@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).