题
在 GNU Emacs 中,有没有一种好方法可以将 C 模式下的 comment-region 命令从
/* This is a comment which extends */
/* over more than one line in C. */
到
/* This is a comment which extends
over more than one line in C. */
?我努力了
(setq comment-multi-line t)
但这没有帮助。有一个 Emacs 手册中有关多行注释的部分, ,但它没有提及任何内容。
解决方案
从 Emacs 21 开始,出现了一个名为 'newcomment
, ,它有不同的注释风格(参见变量 'comment-styles
. 。此设置接近您想要的:
(setq comment-style 'multi-line)
(笔记:你可能应该进行该设置 'c-mode-hook
).
但是,所有设置都无法使注释看起来像您想要的那样。
我看到得到你想要的最简单的方法是添加这个黑客:
(defadvice comment-region-internal (before comment-region-internal-hack-ccs activate)
"override 4th argument to be just spaces"
(when (eq major-mode 'c-mode) ; some condition here
(let ((arg (ad-get-arg 4)))
(when arg
(ad-set-arg 4 (make-string (length arg) ?\ ))))))
当前设置为 comment-style
始终在注释行前面加上“ * ”(如果不是整个“ /* ”)。
如果你没有 Emacs 21,我想你可以简单地下载 newcomment.el
从存储库中。我不知道它在早期版本的 Emacs 中是否能正常工作,但可能值得一试,尽管升级 Emacs 会是一个更好的解决方案。
我的黑客打破了 'uncomment-region
. 。正确的解决办法是改变 'comment-padright
. 。这需要更多的研究,以免破坏其他东西。上述黑客仅改变行为 'c-mode
(根据您的喜好调整条件)。
其他提示
最近我可以用内置的注释支持,如果你设置comment-style
到multi-line
,这将产生这种发现:
/* This is a comment which extends
* over more than one line in C. */
如果这还不够紧密,看看newcomment.el
和定义自己的评论功能为宜。
不隶属于 StackOverflow