【问题标题】:How to automatically align comments in different pieces of code?如何自动对齐不同代码段中的注释?
【发布时间】:2013-11-28 20:33:10
【问题描述】:

我必须做一项非常具体的任务来一遍又一遍地重做,并希望将其永久保存在我的.emacs 文件中。但是我对 emacs-lisp 不够精通来管理它:

  • Keystroke-1,比如[F8]
    • 记住当前光标的列位置,例如xtab
  • Keystroke-2,说[F9],而光标在其他行:
    • 在当前行中找到最左边的字符串//,如果没有,哔并停止
    • 插入尽可能多的空格,以便// 到达先前记住的列xtab,或者如果光标已经超出xtab,则不执行任何操作
    • 向前搜索下一个// 并将光标放在上面

我设法将它分配给一个临时键盘宏,但必须为每次更改 xtab 值重新记录它。

最终目标是我想在不同的代码段中轻松对齐 cmets,来自

int main() {     // the enty function
    int x = 100; // my new variable
    for(int i=1; i<2012; ++i) { // loop a lot
        x -= i;
    } 
} // end of all things

int main() {                    // the entry function
    int x = 100;                // my new variable
    for(int i=1; i<2012; ++i) { // loop a lot
        x -= i;
    } 
}                               // end of all things

知道如何实现自动化吗?我必须在我的 .emacs-file 中放入什么来存档这个 - 或类似的?

【问题讨论】:

    标签: emacs comments alignment text-formatting


    【解决方案1】:

    正如 tungd 所说,align-regexp 适合这种事情。

    (defun my-align-comments (beginning end)
      "Align instances of // within marked region."
      (interactive "*r")
      (let (indent-tabs-mode align-to-tab-stop)
        (align-regexp beginning end "\\(\\s-*\\)//")))
    

    类似于交互式调用:
    M-x align-regexp RET // RET

    或者对于更多语言不可知的版本:

    (defun my-align-comments (beginning end)
      "Align comments within marked region."
      (interactive "*r")
      (let (indent-tabs-mode align-to-tab-stop)
        (align-regexp beginning end (concat "\\(\\s-*\\)"
                                            (regexp-quote comment-start)))))
    

    【讨论】:

    • 我从没想过 emacs 已经有一个命令。工作就像一种享受!
    【解决方案2】:

    不完全是您问题的答案,但要实现预期目标,您只需标记区域并使用align-regexp

    【讨论】:

      【解决方案3】:

      代码如下:

      (defvar c-current-comment-col 30)
      (defun c-set-comment-col ()
        (interactive)
        (setq c-current-comment-col (current-column)))
      (defun c-comment-to-col ()
        (interactive)
        (beginning-of-line)
          (when (re-search-forward "//" (line-end-position) t)
            (backward-char 2)
            (let ((delta (- c-current-comment-col
                            (current-column))))
              (if (plusp delta)
                  (insert (make-string delta ? ))
                (if (looking-back
                     (format "\\( \\{%d\\}\\)" (- delta)))
                    (delete-region
                     (match-beginning 1)
                     (match-end 1))
                  (message
                   "I'm sorry Dave, I afraid can't do that.")))))
          (next-line 1))
      (global-set-key [C-f6] 'c-set-comment-col)
      (global-set-key [f6] 'c-comment-to-col)
      

      我在末尾添加了一个next-line 调用。现在你可以做 C-f6 f3 f6 M-0 f4 对齐直到缓冲区结束。

      【讨论】:

      • 太棒了。实际上,我不是在 c++ 模式下,它是带有源代码的文本(基础,flyspell),所以我改用(global-set-key [S-f6] 'c-set-comment-col)。另外,为方便起见,我在设置c-current-comment-col 之前添加了在当前行中搜索//。但我不知道如何在c-comment-to-col 的末尾搜索下一个//...所以我可以重复按宏键而无需手动搜索。
      【解决方案4】:

      M-x align 非常强大,会自动处理给定的特定示例。

      但是,它也会对齐变量声明,这可能超出您的预期。在这种情况下,您必须自定义 align-region-separate 或使用 align-regexp 答案。

      【讨论】:

        猜你喜欢
        • 2023-03-24
        • 2017-03-20
        • 2010-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-23
        • 1970-01-01
        相关资源
        最近更新 更多