Does anyone know some good elisp macros for cleaning LaTeX code?
I do a lot of LaTeX editing from other sources, and I would like to expand my set of cleaning tools, since not everyone organizes their code the way I like; -)
In particular, it would be interesting to run the function X in the buffer and get all LaTeX environments (\ begin {...} and \ end {...} pairs) to sit on their own lines, this helps readability of the code.
I could try this myself, but would like to hear suggestions for best practices for programming such a function, for example. he should, of course, not enter blank lines.
offers?
Edit: for archives, here is my current version based on the answer (assuming the use of auctex). It more or less matches my needs at the moment. I added the y-or-n test to be able to detect corner cases that I did not think about.
(defun enviro-split () "Find begin and end macros, and put them on their own line." (interactive) (save-excursion (beginning-of-buffer) ;; loop over document looking for begin and end macros (while (re-search-forward "\\\\\\(begin\\|end\\)" nil t) (catch 'continue ; if the line is a pure comment, then goto next (if (TeX-in-commented-line) (throw 'continue nil) ) ;; when you find one, back up to the beginning of the macro (search-backward "\\") ;; If it not at the beginning of the line, add a newline (when (not (looking-back "^[ \t]*")) (if (y-or-np "newline?") (insert "\n") ) ) ;; move over the arguments, one or two pairs of matching braces (search-forward "{") ; start of the argument (forward-char -1) (forward-sexp) ; move over the argument (if (looking-at "[ \t]*{") ; is there a second argument? (forward-sexp) ) ; move over it if so (if (looking-at "[ \t]*\\[") ; is there a second argument? (forward-sexp) ) ; move over it if so (when (looking-at (concat "[ \t]*" (regexp-quote TeX-esc) "label")) (goto-char (match-end 0)) (forward-sexp) ) (if (looking-at (concat "[ \t]*%" )) (throw 'continue nil) ) ;; If there is anything other than whitespace following the macro, ;; insert a newline (if (not (looking-at "\\s *$")) ;;(insert "\n") (if (y-or-np "newline (a)?") (insert "\n") ) ) ) ; end catch 'continue ) (LaTeX-fill-buffer 'left) ) )
source share