GNU Emacs Manual. Node: Options for Comments

PREVMulti-Line Comments UPComments next

20.7.3: Options Controlling Comments

The comment column is stored in the variable comment-column. You can set it to a number explicitly. Alternatively, the command C-x ; (set-comment-column) sets the comment column to the column point is at. C-u C-x ; sets the comment column to match the last comment before point in the buffer, and then does a M-; to align the current line's comment under the previous one. Note that C-u - C-x ; runs the function kill-comment as described above.

The variable comment-column is per-buffer: setting the variable in the normal fashion affects only the current buffer, but there is a default value which you can change with setq-default. See Locals. Many major modes initialize this variable for the current buffer.

The comment commands recognize comments based on the regular expression that is the value of the variable comment-start-skip. Make sure this regexp does not match the null string. It may match more than the comment starting delimiter in the strictest sense of the word; for example, in C mode the value of the variable is "/\\*+ *", which matches extra stars and spaces after the `/*' itself. (Note that `\\' is needed in Lisp syntax to include a `\' in the string, which is needed to deny the first star its special meaning in regexp syntax. See Regexps.)

When a comment command makes a new comment, it inserts the value of comment-start to begin it. The value of comment-end is inserted after point, so that it will follow the text that you will insert into the comment. In C mode, comment-start has the value "/* " and comment-end has the value " */".

The variable comment-multi-line controls how C-M-j (indent-new-comment-line) behaves when used inside a comment. If comment-multi-line is nil, as it normally is, then the comment on the starting line is terminated and a new comment is started on the new following line. If comment-multi-line is not nil, then the new following line is set up as part of the same comment that was found on the starting line. This is done by not inserting a terminator on the old line, and not inserting a starter on the new line. In languages where multi-line comments work, the choice of value for this variable is a matter of taste.

The variable comment-indent-function should contain a function that will be called to compute the indentation for a newly inserted comment or for aligning an existing comment. It is set differently by various major modes. The function is called with no arguments, but with point at the beginning of the comment, or at the end of a line if a new comment is to be inserted. It should return the column in which the comment ought to start. For example, in Lisp mode, the indent hook function bases its decision on how many semicolons begin an existing comment, and on the code in the preceding lines.

PREVMulti-Line Comments UPComments next