C模式下的Emacs注释区域
在GNU Emacs中,有没有一种好方法可以将C模式下的comment region命令从C模式下的Emacs注释区域,c,emacs,elisp,c-mode,C,Emacs,Elisp,C Mode,在GNU Emacs中,有没有一种好方法可以将C模式下的comment region命令从 /* This is a comment which extends */ /* over more than one line in C. */ 到 ??我试过了 (setq comment-multi-line t) 但这没有帮助。有一个,但它没有提到任何东西。我能找到的最接近内置评论支持的是,如果您将评论样式设置为多行,这将产生以下结果: /* This is a comment which
/* This is a comment which extends */
/* over more than one line in C. */
到
??我试过了
(setq comment-multi-line t)
但这没有帮助。有一个,但它没有提到任何东西。我能找到的最接近内置评论支持的是,如果您将
评论样式设置为多行
,这将产生以下结果:
/* This is a comment which extends
* over more than one line in C. */
如果距离不够近,请查看newcomment.el
,并根据需要定义自己的注释函数 自Emacs 21以来,出现了一个名为的模块,该模块具有不同的注释样式(请参见变量“comment-style
)。此设置接近您想要的:
(setq comment-style 'multi-line)
(注意:您可能应该在'c-mode-hook
中进行设置)
但是,这些设置都不会使注释看起来像您想要的
要获得您想要的内容,我看到的最简单的方法是添加以下hack:
(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) ?\ ))))))
注释样式的当前设置总是在注释行前面加上“*”(如果不是整个“/*”)
如果您没有Emacs 21,我想您可以直接从存储库下载。我不知道它是否能像早期版本的Emacs那样工作,但它可能值得一试,尽管升级Emacs会是一个更好的解决方案
我的黑客攻击破坏了'uncomment-region
。一个合适的修复方法是更改'comment-padright
。这需要更多的研究,以免破坏其他东西。上面的黑客攻击只会改变'c-mode
中的行为(根据您的喜好调整条件)。这是谷歌给我的唯一“评论风格”Emacs C-h是注释样式吗?它是在Emacs 23.1中定义的,你使用的是旧版本吗?我使用的是Ubuntu/FreeBSD附带的Emacs。在Windows上,我使用的是Emacs 23.1,但我现在使用的是FreeBSD,上面写着22.3.1。不管怎样,你的示例很有效,谢谢。
(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) ?\ ))))))