Emacs comment-region in C mode
Asked Answered
S

2

9

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.

Sancha answered 11/10, 2009 at 21:37 Comment(0)
I
7

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

Iatrics answered 11/10, 2009 at 22:4 Comment(3)
The only thing Google gives me for "comment-styles" is emacsbugs.donarmstrong.com/cgi-bin/bugreport.cgi?bug=3371 Emacs C-h a comment-styles doesn't give anything either.Sancha
It's defined in Emacs 23.1, are you using an older version?Iatrics
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.Sancha
F
3

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.

Fabricant answered 11/10, 2009 at 22:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.