用于清除LaTeX代码的Elisp宏

时间:2012-07-18 09:23:05

标签: emacs latex elisp auctex

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

1 个答案:

答案 0 :(得分:1)

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

(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的内置函数来移动性别,这比使用自己的正则表达式更容易,它可以处理大括号内的多个可能嵌套的表达式。