有人知道一些用于清理 LaTeX 代码的好 elisp 宏吗?
我对其他人的资源进行了很多 LaTeX 编辑,并且我想扩展我的清理工具集,因为不是每个人都以我喜欢的方式组织他们的代码 ;-)
一个特别有趣的是,在缓冲区上运行函数 X 并让所有 LaTeX 环境(\begin{...} 和 \end{...} 对)位于它们自己的行上,这有助于代码。
我可以自己尝试一下,但想听听关于编写这样一个函数的最佳实践的建议,例如它当然不应该引入自己的空白行。
建议?
编辑:对于档案,这是我根据给出的答案的当前版本(假设使用 auctex)。它或多或少适合我目前的需求。我添加了 y-or-n 测试只是为了能够检测到我没有想到的极端情况。
(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's not at the beginning of the line, add a newline
(when (not (looking-back "^[ \t]*"))
(if (y-or-n-p "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-n-p "newline (a)?")
(insert "\n")
)
)
) ; end catch 'continue
)
(LaTeX-fill-buffer 'left)
)
)