5

In my custom style support file, I would like to add custom code to parse an environment. That is, my LaTeX code looks like this

\begin{foobar}
  some complex stuff
\end{foobar}

and I don't want to capture some complex stuff in a regexp, it's too complex for that, but I do want to analyze it. I can't use generic entries to parse some complex stuff because they would have a ridiculous amount of false positives outside the foobar environment. I can capture the beginning of the environment in an entry in TeX-auto-regexp-list, but what am I supposed to put in my function?

(defun TeX-parse-foobar-environment (match)
  (let ((name (TeX-match-buffer match))
        (start ???) (end ???))
    (parse-foobar-region start end)))
(TeX-auto-add-regexp '("\\\\begin{\\(foobar\\)}" 1 TeX-parse-foobar-environment))

I don't see anything about this in the manual. Should I call LaTeX-find-matching-begin then LaTeX-find-matching-end to determine where the environment starts and ends? How does this generalize to examples other than environments? I could find out empirically where the point is, but can I count on that being the same in all versions of AUCTeX?

1 Answers1

1

AUCTeX parser works with regexps, and the position of the matches isn't recorded. I think you have 2 choices:

  • Leave the parser alone and write a function which does the job. Maybe something like this:
    (defun gilles/TeX-parse-foobar-environment ()
      (save-excursion
        (goto-char (point-min))
        (let (beg end result)
          (while (re-search-forward "^[ \t]*\\\\begin{foobar}" nil t)
            (setq beg (line-beginning-position 2))
            (LaTeX-find-matching-end)
            (setq end (line-end-position 0))
            (push (buffer-substring-no-properties beg end) result))
          (dolist (x (nreverse result))
            (parse-foobar-region x)))))   ```
    

  • Or really plug into AUCTeX parser and do something like this in your custom style file:
    (TeX-auto-add-type "gilles-env-foobar" "LaTeX")
    

    (defvar LaTeX-gilles-foobar-regexp '("\\begin{foobar}\(.?\(?:\n.?\)*?\)\\end{foobar}" 1 LaTeX-auto-gilles-env-foobar))

    (defun LaTeX-gilles-foobar-auto-prepare () (setq LaTeX-auto-gilles-env-foobar nil))

    (add-hook 'TeX-auto-prepare-hook #'LaTeX-gilles-foobar-auto-prepare t)

    (TeX-add-style-hook "gilles-style" (lambda () (TeX-auto-add-regexp LaTeX-gilles-foobar-regexp)) :latex)

    (defun gilles/TeX-parse-foobar-environment () (dolist (x (LaTeX-gilles-env-foobar-list)) (gilles-parser-fun-taking-one-string-as-arg x)))

    TeX-auto-add-type is a macro which defined some variables and a functionen needed for parsing. LaTeX-gilles-foobar-regexp tells AUCTeX what to parse and where to put the match. The middle part adds all of this to parser. The final part is to write gilles-parser-fun-taking-one-string-as-arg which takes a string as argument which is the content of the environment, say some complex stuff you want to parse.

All this is of course 100% untested.

Arash Esbati
  • 1,975
  • 1
  • 8
  • 14