Skip to main content
I added the tag 'isearch-filter-predicate' + improved formatting
Source Link
foo

\begin{equation}
  foo
\end{equation}

foo

$foo$

foo
foo

\begin{equation}
  foo
\end{equation}

foo

$foo$

foo
(defun test-a ()
  (interactive)
  (let ((isearch-filter-predicate 'skip-equation-env))

    (query-replace-regexp "foo"
                          "bar" nil (point-min) (point-max))))

(defun skip-equation-env (beg end)
  (catch 'skip-region
    (save-excursion
      (save-match-data
        (when (and (looking-back "\\\\begin{equation}\\([[:graph:] \n]+\\)")
                   (looking-at "\\([[:graph:] \n]+\\)\\\\end{equation}"))
          (throw 'skip-region nil)))
      t)))

(defun test-b ()
  (interactive)
  (let ((isearch-filter-predicate 'skip-inline-math))

    (query-replace-regexp "foo"
                          "bar" nil (point-min) (point-max))))

(defun skip-inline-math (beg end)
  (catch 'skip-region
    (save-excursion
      (save-match-data
        (when (face-at-point beg 'tex-math)
          (throw 'skip-region nil)))
      t)))
(defun test-a ()
  (interactive)
  (let ((isearch-filter-predicate 'skip-equation-env))

    (query-replace-regexp "foo"
                          "bar" nil (point-min) (point-max))))

(defun skip-equation-env (beg end)
  (catch 'skip-region
    (save-excursion
      (save-match-data
        (when (and (looking-back "\\\\begin{equation}\\([[:graph:] \n]+\\)")
                   (looking-at "\\([[:graph:] \n]+\\)\\\\end{equation}"))
          (throw 'skip-region nil)))
      t)))

(defun test-b ()
  (interactive)
  (let ((isearch-filter-predicate 'skip-inline-math))

    (query-replace-regexp "foo"
                          "bar" nil (point-min) (point-max))))

(defun skip-inline-math (beg end)
  (catch 'skip-region
    (save-excursion
      (save-match-data
        (when (face-at-point beg 'tex-math)
          (throw 'skip-region nil)))
      t)))
foo

\begin{equation}
  foo
\end{equation}

foo

$foo$

foo
(defun test-a ()
  (interactive)
  (let ((isearch-filter-predicate 'skip-equation-env))

    (query-replace-regexp "foo"
                          "bar" nil (point-min) (point-max))))

(defun skip-equation-env (beg end)
  (catch 'skip-region
    (save-excursion
      (save-match-data
        (when (and (looking-back "\\\\begin{equation}\\([[:graph:] \n]+\\)")
                   (looking-at "\\([[:graph:] \n]+\\)\\\\end{equation}"))
          (throw 'skip-region nil)))
      t)))

(defun test-b ()
  (interactive)
  (let ((isearch-filter-predicate 'skip-inline-math))

    (query-replace-regexp "foo"
                          "bar" nil (point-min) (point-max))))

(defun skip-inline-math (beg end)
  (catch 'skip-region
    (save-excursion
      (save-match-data
        (when (face-at-point beg 'tex-math)
          (throw 'skip-region nil)))
      t)))
foo

\begin{equation}
  foo
\end{equation}

foo

$foo$

foo
(defun test-a ()
  (interactive)
  (let ((isearch-filter-predicate 'skip-equation-env))

    (query-replace-regexp "foo"
                          "bar" nil (point-min) (point-max))))

(defun skip-equation-env (beg end)
  (catch 'skip-region
    (save-excursion
      (save-match-data
        (when (and (looking-back "\\\\begin{equation}\\([[:graph:] \n]+\\)")
                   (looking-at "\\([[:graph:] \n]+\\)\\\\end{equation}"))
          (throw 'skip-region nil)))
      t)))

(defun test-b ()
  (interactive)
  (let ((isearch-filter-predicate 'skip-inline-math))

    (query-replace-regexp "foo"
                          "bar" nil (point-min) (point-max))))

(defun skip-inline-math (beg end)
  (catch 'skip-region
    (save-excursion
      (save-match-data
        (when (face-at-point beg 'tex-math)
          (throw 'skip-region nil)))
      t)))
deleted 1 character in body
Source Link

I have this test code:

foo

\begin{equation}
  foo
\end{equation}

foo

$foo$

foo

I want to replace "foo" with "bar" everywhere but LaTeX math, so I have written these functions:

(defun test-a ()
  (interactive)
  (let ((isearch-filter-predicate 'skip-equation-env))

    (query-replace-regexp "foo"
                          "bar" nil (point-min) (point-max))))

(defun skip-equation-env (beg end)
  (catch 'skip-region
    (save-excursion
      (save-match-data
        (when (and (looking-back "\\\\begin{equation}\\([[:graph:] \n]+\\)")
                   (looking-at "\\([[:graph:] \n]+\\)\\\\end{equation}"))
          (throw 'skip-region nil)))
      t)))

(defun test-b ()
  (interactive)
  (let ((isearch-filter-predicate 'skip-inline-math))

    (query-replace-regexp "foo"
                          "bar" nil (point-min) (point-max))))

(defun skip-inline-math (beg end)
  (catch 'skip-region
    (save-excursion
      (save-match-data
        (when (face-at-point beg 'text'tex-math)
          (throw 'skip-region nil)))
      t)))

I have adapted skip-equation-env and skip-inline-math from another code to be fit to my case, so I'm not sure of all their lines.

I have tested these functions successfully, but I would like to know if there is a more suitable way to write them using isearch-filter-predicate. I am new to isearch.

I am not allowed to use auctex.

I have this test code:

foo

\begin{equation}
  foo
\end{equation}

foo

$foo$

foo

I want to replace "foo" with "bar" everywhere but LaTeX math, so I have written these functions:

(defun test-a ()
  (interactive)
  (let ((isearch-filter-predicate 'skip-equation-env))

    (query-replace-regexp "foo"
                          "bar" nil (point-min) (point-max))))

(defun skip-equation-env (beg end)
  (catch 'skip-region
    (save-excursion
      (save-match-data
        (when (and (looking-back "\\\\begin{equation}\\([[:graph:] \n]+\\)")
                   (looking-at "\\([[:graph:] \n]+\\)\\\\end{equation}"))
          (throw 'skip-region nil)))
      t)))

(defun test-b ()
  (interactive)
  (let ((isearch-filter-predicate 'skip-inline-math))

    (query-replace-regexp "foo"
                          "bar" nil (point-min) (point-max))))

(defun skip-inline-math (beg end)
  (catch 'skip-region
    (save-excursion
      (save-match-data
        (when (face-at-point beg 'text-math)
          (throw 'skip-region nil)))
      t)))

I have adapted skip-equation-env and skip-inline-math from another code to be fit to my case, so I'm not sure of all their lines.

I have tested these functions successfully, but I would like to know if there is a more suitable way to write them using isearch-filter-predicate. I am new to isearch.

I am not allowed to use auctex.

I have this test code:

foo

\begin{equation}
  foo
\end{equation}

foo

$foo$

foo

I want to replace "foo" with "bar" everywhere but LaTeX math, so I have written these functions:

(defun test-a ()
  (interactive)
  (let ((isearch-filter-predicate 'skip-equation-env))

    (query-replace-regexp "foo"
                          "bar" nil (point-min) (point-max))))

(defun skip-equation-env (beg end)
  (catch 'skip-region
    (save-excursion
      (save-match-data
        (when (and (looking-back "\\\\begin{equation}\\([[:graph:] \n]+\\)")
                   (looking-at "\\([[:graph:] \n]+\\)\\\\end{equation}"))
          (throw 'skip-region nil)))
      t)))

(defun test-b ()
  (interactive)
  (let ((isearch-filter-predicate 'skip-inline-math))

    (query-replace-regexp "foo"
                          "bar" nil (point-min) (point-max))))

(defun skip-inline-math (beg end)
  (catch 'skip-region
    (save-excursion
      (save-match-data
        (when (face-at-point beg 'tex-math)
          (throw 'skip-region nil)))
      t)))

I have adapted skip-equation-env and skip-inline-math from another code to be fit to my case, so I'm not sure of all their lines.

I have tested these functions successfully, but I would like to know if there is a more suitable way to write them using isearch-filter-predicate. I am new to isearch.

I am not allowed to use auctex.

edited tags; edited title
Link
Drew
  • 79.1k
  • 10
  • 123
  • 257

How to perform incremental searchquery-replace excluding LaTeX math?

added 3 characters in body
Source Link
Loading
added 2 characters in body
Source Link
Loading
Source Link
Loading