I have written a function to temporarily set the value of isearch-filter-predicate (it works with query-replace*
functions too):
;; From AUCTeX, but it works as a standalone too:
(require 'texmathp)
(defvar-local ifp-predicates-list '(skip-maths keep-maths)
"List of isearch filter predicates")
(defun skip-maths (beg end)
"Return nil if some text BEG to END is in a LaTeX maths
environment or in in-line maths."
(catch 'skip-region
(let ((pos beg))
(save-excursion
(save-match-data
(goto-char pos)
(while (< pos end)
(when (texmathp) (throw 'skip-region nil))
(goto-char (setq pos (1+ pos)))))
t))))
(defun keep-maths (beg end)
"Return t if some text BEG to END is NOT in a LaTeX maths
environment or in in-line maths."
(catch 'skip-region
(let ((pos beg))
(save-excursion
(save-match-data
(goto-char pos)
(while (< pos end)
(unless (texmathp) (throw 'skip-region nil))
(goto-char (setq pos (1+ pos)))))
t))))
(defun with-temp-isearch-filter-predicate-test ()
"Temporarily assigns a predicate to the variable `isearch-filter-predicate`
in order to make portions of the buffer visible/invisible to search and replacement functions
based on specific criteria."
(interactive)
(save-excursion
(let* ((IFP_DEFAULT isearch-filter-predicate)
;; https://emacs.stackexchange.com/q/80307/15606 :
(set-message-function nil)
(IFP (completing-read
"Specify which predicate you want to activate [TAB]: "
ifp-predicates-list
nil
t
nil
nil
nil))
(isearch-filter-predicate (intern-soft IFP))
;; Non-nil means to allow minibuffer commands while in the minibuffer:
(enable-recursive-minibuffers t))
(read-string
(format
"The current value assigned to the variable `isearch-filter-predicate' is `%s'.
Press ENTER to restore the default value (`%s') at the end of the operations: "
IFP
IFP_DEFAULT)))))
I'm working on a function to combine "multiple" predicates at the same time.