问题描述
这里的标题注释是指:
/ *计划名称:Foo * /
/ *作者:Jane Doe * /
/ *日期:06/29/2014 * /
/ * Rev:1.0 * /
棘手的部分是:
- 难以对齐结尾 * / 如果可能的话,我想在:。
提前感谢。
您可以通过两次调用,但输入以下内容有点棘手:
/ *作者:Jane Doe * /
/ *日期:06 / 29/2014 * /
/ * Rev:1.0 * /
修改,然后用前缀参数做你的第一个 align-regexp :
Cu Mx align-regexp RET:\(\s- * \)RET RET RET n
此版本的 align-regexp 使用与冒号字符匹配的正则表达式,后跟任意数量的空格。然后我们
- 接受修改捕获组1(空格)的默认选项,
- 当我们调整时默认留出至少一个空格,
- 然后告诉Emacs不要为整行重复,因为我们不需要该特性。
这应该留给你
/ *程序名称:Foo * /
/ *作者:Jane Doe * /
/ *日期:06/29/2014 * /
/ * Rev:1.0 * /
现在, Cx Cx 重新选择您的文本,并做另一个 align-regexp 。这不需要前缀参数:
Mx align-regexp RET \ * RET
这个版本简单得多。我们提供与关闭注释指示符 * / 匹配的正则表达式,转义星号。
完成!最终结果如下所示:
/ *计划名称:Foo * /
/ *作者:Jane Doe * /
/ *日期:06/29/2014 * /
/ * Rev:1.0 * /
编辑:
您应该能够编写一个函数来自动完成此过程。我不是elisp专家,但这似乎做的诀窍:
(defun my- align-c-comment-block()
(交互式)
(when(use-region-p)
(align-regexp(region-beginning) \\\(\\s- * \\))
(交换点标记)
(align-regexp(region-beginning) \\(\\s- * \\)\\ * /)))
Here the "header comments" refers to:
/* Program Name: Foo */ /* Author: Jane Doe */ /* Date: 06/29/2014 */ /* Rev: 1.0 */
The tricky parts are:
- Hard to align the ending */ in each line if to use
- If possible, I'd like to align the content after : in each line.
Thanks in advance.
You can do this with two invocations of align-regexp, though it's a bit tricky to type:
/* Program Name: Foo */ /* Author: Jane Doe */ /* Date: 06/29/2014 */ /* Rev: 1.0 */
Select the region you wish to modify, then do your first align-regexp with a prefix argument:
C-u M-x align-regexp RET :\(\s-*\) RET RET RET n
This version of align-regexp uses a regular expression matching the colon character, followed by any amount of whitespace. Then we
- accept the default option of modifying capture group 1 (the whitespace),
- then the default of leaving at least one space when we adjust,
- then tell Emacs not to repeat for the whole line, as we don't need that feature.
This should leave you with
/* Program Name: Foo */ /* Author: Jane Doe */ /* Date: 06/29/2014 */ /* Rev: 1.0 */
Now, C-x C-x to swap point and mark, reselecting your text, and do another align-regexp. This one doesn't need a prefix argument:
M-x align-regexp RET \*/ RET
This version is much simpler. We provide a regular expression that matches the close comment indicator */, escaping the asterisk.
And you should be done! The final result looks like this:
/* Program Name: Foo */ /* Author: Jane Doe */ /* Date: 06/29/2014 */ /* Rev: 1.0 */
Edit:
You should be able to write a function to automate this process. I'm no elisp expert, but this seems to do the trick:
(defun my-align-c-comment-block () (interactive) (when (use-region-p) (align-regexp (region-beginning) (region-end) ":\\(\\s-*\\)") (exchange-point-and-mark) (align-regexp (region-beginning) (region-end) "\\(\\s-*\\)\\*/")))
这篇关于如何很好地格式化头的注释在C ++模式的emacs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!