I want to build a modified version of the occur
command that obeys to isearch-filter-predicate
. I made some tests and I found that I can do it using a modified version of re-search-forward
that I built:
(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)
This is my modified version of occur
that obeys isearch-filter-predicate
:
(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)))
I copied the interactive
stuff from the original occur
command in replace.el
but I'd prefer to have a cleaner definition. I mean something like:
;; PSEUDOCODE (closure?):
(let ((re-search-forward re-search-forward-ifp))
(defalias 'occur-ifp
(symbol-function 'occur)))
Is it possible?