使用AUCTeX自动缩进\ if语句


12

涉及if -like语句的AUCTeX原始tex代码的当前行为是在与周围条件相同的级别上缩进条件语句。即代码如

\if@sometoggle%
\dosomething%
\else%
\doanotherthing%
\fi%

显示为一大段文字。我想使AUCTeX代码段缩进如下:

\if@sometoggle%
  \dosomething%
\else%
  \doanotherthing%
\fi%

这可能吗?

Answers:


7

这是可能的:

(setq LaTeX-begin-regexp "\\(?:begin\\|if@\\)\\b")
(setq LaTeX-end-regexp "\\(?:end\\|else\\|fi\\)\\b")
(defun LaTeX-indent-level-count ()
  "Count indentation change caused by all \\left, \\right, \\begin, and
\\end commands in the current line."
  (save-excursion
    (save-restriction
      (let ((count 0))
        (narrow-to-region (point)
                          (save-excursion
                            (re-search-forward
                             (concat "[^" TeX-esc "]"
                                     "\\(" LaTeX-indent-comment-start-regexp
                                     "\\)\\|\n\\|\\'"))
                            (backward-char)
                            (point)))
        (while (search-forward TeX-esc nil t)
          (cond
            ((looking-at "left\\b")
             (setq count (+ count LaTeX-left-right-indent-level)))
            ((looking-at "right\\b")
             (setq count (- count LaTeX-left-right-indent-level)))
            ((looking-at LaTeX-begin-regexp)
             (setq count (+ count LaTeX-indent-level)))
            ((looking-at "else\\b"))
            ((looking-at LaTeX-end-regexp)
             (setq count (- count LaTeX-indent-level)))
            ((looking-at (regexp-quote TeX-esc))
             (forward-char 1))))
        count))))

请注意,我必须重新定义LaTeX-indent-level-count。diff只是一个cond分支:

((looking-at "else\\b"))

奇迹般有效!
elemakil 2015年

在遇到与OP相同的问题时,我复制了您的代码并成功了,但并不完全令人满意。它只缩进到下一个缩进\else。的位置\else是正确的,但是下面的代码(\doanotherthin,请参阅问题)仍在第一列而不是第3列。会有所帮助,但我失败了(至少在之后再次缩进\else)。这是我的部分工作代码:有(setq LaTeX-begin-regexp "\\(?:begin\\|if\\|ifx\\|else\\)\\b") 什么想法吗?
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.