修复ESS / Stata中不需要的EMACS标签行为

时间:2011-11-09 17:55:38

标签: emacs indentation ess

emacs中的ESS / Stata模式错误地缩进以运算符结尾的行后面的行。它似乎错误地将这些行解释为多行命令。

例如:

gen foo = 1

/* generate another variable */
  gen bar = 1

“gen bar = 1”这一行不应缩进。看起来EMACS将注释中的尾部斜杠解释为运算符,并认为这行代码跨越两行。

实际上,stata中的多行命令有3个尾部斜杠,没有3个尾部斜杠的换行符表示语句结束。例如以下缩进是正确的:

gen bar = 1
gen ///
  foo = 1

我可以在我的.emacs中添加一些内容来纠正这种行为吗?我不想完全放弃自动标签 - 除了/ *看起来像* /的评论之外,它的效果非常好。

谢谢,

PNJ

1 个答案:

答案 0 :(得分:5)

你是对的,ESS将尾随/解释为行继续的指示。这被硬编码到函数ess-continued-statement-p中,因此要修改您必须重写代码的行为。以下代码(在您的.emacs中)适用于您的示例。

(eval-after-load 'ess-mode
  '(defun ess-continued-statement-p ()
   "this is modified code"
     (let ((eol (point)))
       (save-excursion
         (cond ((memq (preceding-char) '(nil ?\, ?\; ?\} ?\{ ?\]))
                nil)
               ;; ((bolp))
               ((= (preceding-char) ?\))
                (forward-sexp -2)
                (looking-at "if\\b[ \t]*(\\|function\\b[ \t]*(\\|for\\b[ \t]*(\\|while\\b[ \t]*("))
               ((progn (forward-sexp -1)
                       (and (looking-at "else\\b\\|repeat\\b")
                            (not (looking-at "else\\s_\\|repeat\\s_"))))
                (skip-chars-backward " \t")
                (or (bolp)
                    (= (preceding-char) ?\;)))
               (t
                (progn (goto-char eol)
                       (skip-chars-backward " \t")
                       (or (and (> (current-column) 1)
                                (save-excursion (backward-char 1)

        ;;;; Modified code starts here: ;;;;
                                                (or (looking-at "[-:+*><=]")
                                                    (and (looking-at "/")
                                                         (save-excursion (backward-char 1)
                                                                         (not (looking-at "*")))))))
        ;;;; End of modified code ;;;;

                           (and (> (current-column) 3)
                                (progn (backward-char 3)
                                       (looking-at "%[^ \t]%")))))))))))