C模式下的Emacs注释区域

在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进行设置)。

但是,没有任何设置使评论看起来像您想要的。

我看到得到你想要的最简单的方法是添加这个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) ?\ )))))) 

comment-style的当前设置始终在注释行前面加上“*”(如果不是整个“/ *”)。

如果你没有Emacs 21,我想你可以直接从存储库下载newcomment.el 。 我不知道它是否在早期版本的Emacs中按原样运行,但它可能值得一试,尽管升级Emacs将是一个更好的解决方案。

我的黑客打破了'uncomment-region 。 一个正确的解决方法是改变'comment-padright 。 这需要更多的研究,以免打破其他事情。 上述hack只会改变'c-mode行为(根据自己的喜好调整条件)。

我可以找到内置的评论支持,如果你将comment-style设置为multi-line ,这将产生以下结果:

 /* This is a comment which extends * over more than one line in C. */ 

如果距离不够近,请查看newcomment.el并根据需要定义自己的注释函数。