views:

620

answers:

2

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.

+2  A: 

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.

Nicholas Riley
Thanks for your answer.
Kinopiko
+6  A: 

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).

Trey Jackson
The only thing Google gives me for "comment-styles" ishttp://emacsbugs.donarmstrong.com/cgi-bin/bugreport.cgi?bug=3371Emacs C-h a comment-styles doesn't give anything either.
Kinopiko
It's defined in Emacs 23.1, are you using an older version?
Trey Jackson
I am using the Emacs which came with Ubuntu/FreeBSD. On Windows I have Emacs 23.1 but I am using FreeBSD right now and it says 22.3.1. Anyway your example worked, so thanks.
Kinopiko