2

I am trying to come up with a Elisp function that narrows a buffer to a certain LaTeX enviroment (table, figures, ...), does some query-replace-regexp inside the narrowing, then searches for the next occurrence of the environment and does the same, until the last occurrence of the environment.

I have come up with this:

(defun narrowtable ()
  "narrowing on table environment"
  (interactive)
  (beginning-of-buffer)
  (save-excursion  
    (search-forward "begin{table}")
    (LaTeX-narrow-to-environment) 
    (goto-char (point-min))
    (query-replace "X" "B"); Just some query-replace
    (widen)))

which works for fine the first occurence of {table}, but only for that.

I guess I misplaced save-excursion for this to work properly, or there is something other to improve?

1 Answer 1

1

I suggest you try something like this:

(defun narrowtable ()
  "Narrowing on table or figure environments."
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (while (search-forward-regexp
            "\\\\begin{\\(?:table\\|figure\\)}\\(?:\\[[^]]*\\]\\)?" nil t)
      (LaTeX-narrow-to-environment)
      (query-replace "X" "B"); Just some query-replace
      (widen))))

Some comments:

  1. Do not use beginning-of-buffer in Lisp code, use (goto-char (point-min)) instead
  2. You were missing the loop for continuous forward search
  3. I used search-forward-regexp in order to match the environments table and figure incl. an optional argument

Your Answer

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

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