3

Is there a way to perform incremental search that filters out all non-math environments (i.e., only searches within $...$, $$...$$, and if possible other relevant \begin{..}..\end{..} environments)?

Note: a similar question provided an answer without the incremental part.

2 Answers 2

2

The following works.

(defun latex-isearch-in-math ()
  "Call `isearch-forward' with `isearch-filter-predicate' set to filter out matches outside LaTeX math environments."
  (interactive)
  (let ((isearch-filter-predicate
         (lambda (BEG END)
           (save-excursion (save-match-data (goto-char BEG) (texmathp)))))
        (case-fold-search nil))
    (funcall 'isearch-forward)))

Note: it is almost identical to this answer, except call-interactively must be replaced with funcall.

1

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)))))

enter image description here

I'm working on a function to combine "multiple" predicates at the same time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.