[英]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的管理能力不足:
[F8]
[F8]
xtab
xtab
[F9]
while cursor is in some other line: [F9]
:
//
in current line, if none is there, beep and stop //
如果没有,则发出蜂鸣声并停止 //
gets to previously remembered column xtab
, or do nothing if cursor is already beyond xtab
//
获取以前记住的列xtab
,或者如果游标已经超出xtab
执行任何xtab
//
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.