简体   繁体   English

C模式下的Emacs评论区域

[英]Emacs comment-region in C mode

In GNU Emacs, is there a good way to change the comment-region command in C mode from 在GNU Emacs中,有一种很好的方法可以在C模式下更改comment-region命令

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

to

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

? I have tried 我试过了

(setq comment-multi-line t)

but this does not help. 但这没有用。 There is a section on multi-line comments in the Emacs manual , but it does not mention anything. 在Emacs手册中有一个关于多行注释部分 ,但它没有提到任何内容。

Since Emacs 21, there's been a module named 'newcomment , which has different comment styles (see the variable 'comment-styles . This setting gets close to what you want: 从Emacs 21开始,有一个名为'newcomment的模块,它有不同的注释样式(参见变量'comment-styles 。这个设置接近你想要的:

(setq comment-style 'multi-line)

(Note: you should probably make that setting in 'c-mode-hook ). (注意:您应该在'c-mode-hook进行设置)。

However, none of the settings make the comments look like what you want. 但是,没有任何设置可以使评论看起来像您想要的。

The easiest way I saw to get what you want is to add this hack: 我看到得到你想要的最简单的方法是添加这个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) ?\ ))))))

The current settings for comment-style always prefix the comment lines with " * " (if not the whole " /* "). comment-style的当前设置始终在注释行前面加上“*”(如果不是整个“/ *”)。

If you don't have Emacs 21, I suppose you could simply download newcomment.el from the repository. 如果您没有Emacs 21,我想您只需从存储库下载newcomment.el即可。 I don't know if it works as-is in earlier versions of Emacs, but it might be worth a shot, though upgrading Emacs would be a better solution. 我不知道它是否在早期版本的Emacs中按原样运行,但它可能值得一试,尽管升级Emacs将是一个更好的解决方案。

My hack breaks the 'uncomment-region . 我的黑客打破了'uncomment-region A proper fix would be to change 'comment-padright . 一个正确的解决方法是改变'comment-padright That would take a little more research so as not to break other things. 这需要更多的研究,以免破坏其他事情。 The above hack only changes behavior in 'c-mode (adjust the condition to your liking). 上述hack只会改变'c-mode行为(根据自己的喜好调整条件)。

Closest I could find with the built-in commenting support is if you set comment-style to multi-line , which will produce this: 我可以找到内置的评论支持,如果你将comment-style设置为multi-line ,这将产生这样的结果:

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

If that isn't close enough, take a look at newcomment.el and define your own commenting functions as appropriate. 如果距离不够近,请查看newcomment.el并根据需要定义自己的注释函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM