【问题标题】:Set comments at the same indentation level as all forms将注释设置为与所有表单相同的缩进级别
【发布时间】:2013-01-07 05:38:19
【问题描述】:

默认情况下,cmets 的缩进级别对我来说似乎很陌生。

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

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

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

我可以发现默认机制通过将 cmets 与固定列对齐(可通过 M-x comment-set-column 查询)来工作,并且可以修改 comment-indent-function 变量(将其设置为 nil 部分解决了我的问题) .

【问题讨论】:

    标签: emacs elisp indentation


    【解决方案1】:

    Emacs 对 elisp 中的 cmets 进行不同的缩进,具体取决于使用的分号数量。如果你使用两个,你应该得到你想要的缩进:

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

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

    【讨论】:

    • 太棒了!我可以配置 Emacs 以使 ; 的行为与 ;; 完全相同吗?
    • 我认为您不会想要这样做。它会使您的代码缩进与所有其他 elisp 代码不同......无论如何,这是硬编码到 lisp-indent-line,所以你必须修改它(可能使用 defadvice)。
    【解决方案2】:

    您可以自定义注释缩进功能

    使用你自己的函数代替comment-indent-default。

    通过将最后一行 `comment-column' 替换为 (save-excursion (forward-line -1)(current-indentation))

    应该提供一个起点。

    【讨论】:

      【解决方案3】:

      如果您从 lisp-indent-line 中删除单个分号 cmets 的大小写,它将按照您的意愿行事。

      我已经在下面的代码中删除了它,你可以将它添加到你的 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))))))
      

      【讨论】:

        猜你喜欢
        • 2011-02-22
        • 2017-09-02
        • 1970-01-01
        • 1970-01-01
        • 2017-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-29
        相关资源
        最近更新 更多