简体   繁体   English

如何在不同的代码段中自动对齐注释?

[英]How to automatically align comments in different pieces of code?

I have to do a very specific task to re-do over and over again and would like to have it put permanently in my .emacs file. 我必须做一个非常具体的任务来重复做一遍又一遍,并希望将它永久地放在我的.emacs文件中。 But I am not versed enough in emacs-lisp to manage it: 但我对emacs-lisp的管理能力不足:

  • Keystroke-1, say [F8] 按键-1,说[F8]
    • remember the current cursors' column position in, say, xtab 记住当前游标的列位置,例如xtab
  • Keystroke-2, say [F9] while cursor is in some other line: 按键-2,当光标位于其他行时,说[F9]
    • find leftmost string // in current line, if none is there, beep and stop 在当前行中找到最左边的字符串//如果没有,则发出蜂鸣声并停止
    • insert as many spaces so the // gets to previously remembered column xtab , or do nothing if cursor is already beyond xtab 插入尽可能多的空格,以便//获取以前记住的列xtab ,或者如果游标已经超出xtab执行任何xtab
    • search-forward for next // and place the cursor on it 向前搜索//并将光标放在上面

I managed to assign it to a temporary keyboard macro, but have to re-record it for every changing xtab value. 我设法将它分配给临时键盘宏,但必须为每个更改的xtab值重新记录它。

The ultimate goal is that I want to align the comments in different pieces of code easily, from 最终目标是我希望轻松地将注释与不同的代码对齐

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

to

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

Any idea how I can automate this? 知道如何自动化这个吗? What do I have to put in my .emacs -file to archive this -- or similar? 我需要在我的.emacs文件中放置什么来存档 - 或类似的?

As tungd said, align-regexp is good for this sort of thing. 正如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-*\\)//")))

Which is like the interactive call: 这就像互动电话:
Mx align-regexp RET // RET Mx align-regexp RET // RET

Or for a more language-agnostic version: 或者更多与语言无关的版本:

(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)))))

Here's the code: 这是代码:

(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)

I've added a next-line call to the end. 我在最后添加了一个next-line电话。 Now you can do C-f6 f3 f6 M-0 f4 to align until end of buffer. 现在你可以做C-f6 f3 f6 M-0 f4对齐直到缓冲区结束。

不完全是你的问题的答案,但为了达到预期的目标,你可以只标记该区域并使用align-regexp

Mx align is very powerful and will automatically handle the particular example given. Mx align非常强大,将自动处理给定的特定示例。

However, it will also align variable declarations, which may be more than you want. 但是,它也会对齐变量声明,这可能比您想要的更多。 In that case, you would have to customize align-region-separate or use the align-regexp answer. 在这种情况下,您必须自定义align-region-separate或使用align-regexp答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM