3

有人知道一些用于清理 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)
)
  )
4

1 回答 1

1

您可能可以编写一个正则表达式并为此进行正则表达式替换。但是,我发现这些操作的逻辑变得非常复杂,尤其是当您想要考虑各种边缘情况时。在您的示例中,您需要处理一些环境采用一个参数,而其他环境采用两个参数。我认为将一系列简单的正则表达式与基本的文本编辑命令结合起来更容易:

(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)

      ;; 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-at "^"))
        (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 "\\s *{")         ; is there a second argument?
          (forward-sexp))               ; move over it if so

      ;; If there is anything other than whitespace following the macro,
      ;; insert a newline
      (if (not (looking-at "\\s *$"))
          (insert "\n")))))

这种方法的优势在于使用 Emacs 的内置函数来移动 sexps,这比使用自己的正则表达式来处理大括号内的多个可能嵌套的表达式要容易得多。

于 2012-07-19T21:27:39.170 回答