Emacs comment-region in C mode

China☆狼群 提交于 2019-12-12 10:36:10

问题


In GNU Emacs, is there a good way to change the comment-region command in C mode from

/* 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.


回答1:


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:

(setq comment-style 'multi-line)

(Note: you should probably make that setting in '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:

(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 " /* ").

If you don't have Emacs 21, I suppose you could simply download newcomment.el from the repository. 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.

My hack breaks the 'uncomment-region. A proper fix would be to change '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).




回答2:


Closest I could find with the built-in commenting support is if you set comment-style to multi-line, which will produce this:

/* 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.



来源:https://stackoverflow.com/questions/1551854/emacs-comment-region-in-c-mode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!