3

So, you would be able to setup it like this

(setq
  one-line-comment "//"
  multi-line-comment-start "/*"
  multi-line-comment-end "*/")

And use one command to comment anything. If nothing is selected it would comment one line otherwise it would use multi line comment. And most important if you inside a comment it would uncomment both types of comments. I know about newcomment.el but I cannot figure out if it's possible to configure it in such way, especially for uncommenting multi line comments.

2
  • I would suggest broadening your question, so that someone will write up a solution if it doesn't exist in a current package. Consider specifying the major-mode that you intend to use this with. – lawlist Jan 20 '16 at 7:37
  • 1
    It should work in any major mode. For example JavaScript has two types of comments – ais Jan 20 '16 at 7:59
3

You don't need a special package for this. Emacs offers such a feature out of the box. And as @ais indicated in a comment, it is available to use for any mode.

See the Elisp manual, node Syntax Class Table:

Comment starters: ‘<’
Comment enders: ‘>’
 Characters used in various languages to delimit comments.  Human
 text has no comment characters.  In Lisp, the semicolon (‘;’)
 starts a comment and a newline or formfeed ends one.

Generic comment delimiters: ‘!’
 Characters that start or end a special kind of comment.  _Any_
 generic comment delimiter matches _any_ generic comment delimiter,
 but they cannot match a comment starter or comment ender; generic
 comment delimiters can only match each other.

 This syntax class is primarily meant for use with the
 ‘syntax-table’ text property (*note Syntax Properties::).  You can
 mark any range of characters as forming a comment, by giving the
 first and last characters of the range ‘syntax-table’ properties
 identifying them as generic comment delimiters.

and more particularly node Syntax Flags.

(See how it is done for C code, for example, by checking the Lisp sources.)

0

Here is a solution that I came up with to comment the way I like:

(defun comment-or-uncomment-line-or-region ()
  "Comments or uncomments the current line or region."
  (interactive)
  (if (region-active-p)
      (comment-or-uncomment-region (region-beginning) (region-end))
    (if (save-excursion (beginning-of-line) (looking-at "[[:space:]]*$"))
        (comment-dwim nil)
      (comment-or-uncomment-region (line-beginning-position) (line-end-position)))))

(global-set-key (kbd "M-;") 'comment-or-uncomment-line-or-region)

This should do everything the way you want, except that multi line comments are formatted as multiple single line comments. If want a package instead of a home-grown function, you can try the comment-dwim-2 package, but it seems like this does more or less the same thing my function does. Also, see the various answers and links in this question which discusses multi line comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.