Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top