Is there a way to make some regions of the buffer invisible (I mean not evaluated by) to query-replace
and similar commands?
Let me better explain with an example. Let's take a piece of LaTeX
code
... in the following equation ,
\begin{equation}
x + y = z ,
\end{equation}
I need to replace the first " ," with "," (deleting the extra space) but the second " ," string, in the equation ambient it's not a problem and I'd like my query-replace
to ignore it.
I figure out I could use a loop to assign some text properties to the regions I need to ignore but I've got no idea if that kind of text properties exist.
I'm also open to any kind of suggestion.
Workaround. Following Drew's answer I wrote this code that seems to work:
(defun skip-if-my-ignore (beg end)
"Return nil iff some text BEG to END has non-`nil' property `my-ignore'."
(catch 'skip-if-my-ignore
(let ((pos beg))
(while (< pos end)
(when (get-text-property pos 'my-ignore) (throw 'skip-if-my-ignore nil))
(setq pos (1+ pos))))
t))
(defun query-replace-skipping-math-env ()
(interactive)
(save-excursion
(goto-char (point-min))
(while (search-forward-regexp "\\\\begin{equation}" nil t)
(save-excursion
(let ((b (copy-marker (match-beginning 0)))
(e (copy-marker (search-forward-regexp "\\\\end{equation}" nil t ))))
(put-text-property b e 'my-ignore t))))
(let ((isearch-filter-predicate 'skip-if-my-ignore))
(perform-replace " ," "," t nil nil 1 nil (point-min) (point-max)))
(remove-list-of-text-properties (point-min) (point-max) '(my-ignore))))
I tested it on:
... in the following equation ,
\begin{equation}
x + y = z ,
\end{equation}
... also in the following equation ,
\begin{equation}
2x + 2y = 2z ,
\end{equation}
query-replace
already lets you decide whether to replace each one, or do you think it's inconvenient?query-replace
to ignore some portions of the text. I need it to save time, avoiding to skip manually some queries.