问题 在与所有表单相同的缩进级别设置注释


默认情况下,评论会得到缩进级别,这对我来说似乎很陌生。

(defun example ()
  just
  some
                ; a comment
  words)

如何调整它以使第一个分号与常规Lisp形式垂直对齐?

(defun example ()
  just
  some
  ; a comment
  words)

我能找到的是默认机制的工作原理是将注释与固定列对齐(可查询通过 M-x comment-set-column),那一个可以修改 comment-indent-function 变量(将其设置为nil部分修复了我的问题)。


4569
2018-01-07 05:38


起源



答案:


Emacs根据使用的分号数不同地缩进elisp中的注释。如果你使用两个,你应该得到你想要的缩进:

(defun test-single ()
                                        ; A single semicolon
  nil)

(defun test-double ()
  ;; Do two semicolons make a colon ;)
  nil)

另外,三个分号 ;;; 根本没有重新缩进。通常,它们用于标记源文件中的新主要部分。


11
2018-01-07 08:56



大!我可以这样配置Emacs吗? ; 表现完全如此 ;;? - vemv
我不认为你会这样做。它会使你的代码缩进与所有其他elisp代码不同......无论如何,这是硬编码的 lisp-indent-line,所以你必须修改它(也许使用 defadvice)。 - Lindydancer
好的,谢谢你的建议。 - vemv


答案:


Emacs根据使用的分号数不同地缩进elisp中的注释。如果你使用两个,你应该得到你想要的缩进:

(defun test-single ()
                                        ; A single semicolon
  nil)

(defun test-double ()
  ;; Do two semicolons make a colon ;)
  nil)

另外,三个分号 ;;; 根本没有重新缩进。通常,它们用于标记源文件中的新主要部分。


11
2018-01-07 08:56



大!我可以这样配置Emacs吗? ; 表现完全如此 ;;? - vemv
我不认为你会这样做。它会使你的代码缩进与所有其他elisp代码不同......无论如何,这是硬编码的 lisp-indent-line,所以你必须修改它(也许使用 defadvice)。 - Lindydancer
好的,谢谢你的建议。 - vemv


您可以自定义comment-indent-function

而不是comment-indent-default使用您自己的函数。

通过替换最后一行`comment-column'来编写新的 (save-excursion(forward-line -1)(current-indentation))

应该提供一个起点。


1
2018-01-07 13:19



嘿,这很棒! - vemv


如果你删除单个分号注释的大小写 lisp-indent-line 它会表现得像你想要的那样。

我已在下面的代码中删除它,您可以将其添加到您的emacs配置:

(defun lisp-indent-line (&optional _whole-exp)
  "Indent current line as Lisp code.
With argument, indent any additional lines of the same expression
rigidly along with this one.
Modified to indent single semicolon comments like double semicolon comments"
  (interactive "P")
  (let ((indent (calculate-lisp-indent)) shift-amt
    (pos (- (point-max) (point)))
    (beg (progn (beginning-of-line) (point))))
    (skip-chars-forward " \t")
    (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
    ;; Don't alter indentation of a ;;; comment line
    ;; or a line that starts in a string.
        ;; FIXME: inconsistency: comment-indent moves ;;; to column 0.
    (goto-char (- (point-max) pos))
      (if (listp indent) (setq indent (car indent)))
      (setq shift-amt (- indent (current-column)))
      (if (zerop shift-amt)
      nil
    (delete-region beg (point))
    (indent-to indent))
      ;; If initial point was within line's indentation,
      ;; position after the indentation.  Else stay at same point in text.
      (if (> (- (point-max) pos) (point))
      (goto-char (- (point-max) pos))))))

1
2017-11-01 19:10