Next: , Previous: , Up: Comments   [Index]


D.2.2 Some Comment Functions

Because comments are such an important part of programming, Emacs provides special commands for editing and inserting comments. It can also do spell checking on comments with Flyspell Prog mode (*note Spelling::).

comment-dwim (‘M-;’)

Insert or realign comment on current line. If the region is active, comment or uncomment the region instead. In this case, supplying a positive ARG adds that many comment characters, or if negative, removes that many.

Function: comment-dwim arg

Call the comment command you want (“Do What I Mean”).

If the region is active and ‘transient-mark-mode’ is on, call comment-region (unless it only consists of comments, in which case it calls uncomment-region).

Else, if the current line is empty, call comment-insert-comment-function if it is defined, otherwise insert a comment and indent it. If the line is blank, the comment is indented to the same position where <TAB> would indent to; if the line is non-blank, the comment is placed after the last non-whitespace character on the line, somewhere between comment-column and comment-fill-column if possible. Point is placed after the comment character.

ARG (‘C-u M-;’)

Else if a prefix ARG is specified, call comment-kill. The comment is saved to the kill ring, and you can reinsert it on another line by moving the end of that line, doing ‘C-y’, and then ‘M-;’ to realin the comment.

Else, call comment-indent. If a line already contains the comment-start string, this command realigns it to the conventional alignment and moves point after. Comments starting in column 0 are not moved.

If you are typing a comment and wish to continue it to another line, type ‘M-j’ or ‘C-M-j’ (comment-indent-new-line). This breaks the current line, and inserts the necessary comment delimiters and indentation to continue the comment.

comment-line (‘C-x C-;’)

Comments or uncomments complete lines.

Function: comment-line N

Comment or uncomment current line and leave point after it. If region is active, comment lines in active region instead. Unlike ‘comment-dwim’, this always comments whole lines.

N

With positive prefix, apply to N lines including current one. With negative prefix, apply to -N lines above. Also, further consecutive invocations of this command will inherit the negative argument.

comment-region (‘C-c C-c’)

Add comment delimiters to all the lines in the region, even if the region is inactive.

Use ‘C-c C-c’ in C-like languages.

Function: comment-region begin end &optional arg

Comment or uncomment each line in the region.

With just ‘C-u prefix arg’, uncomment each line in region BEG .. END.

ARG

Numeric prefix ARG means use ARG comment characters. If ARG is negative, delete that many comment characters instead.

The strings used as comment starts are built from comment-start and comment-padding; the strings used as comment ends are built from comment-end and comment-padding.

For C-like modes, you can configure the exact effect of ‘M-;’ by setting the variables c-indent-comment-alist and c-indent-comments-syntactically-p.

uncomment-region
Function: uncomment-region begin end &optional arg

Uncomment each line in the BEGIN .. END region.

ARG

The numeric prefix ARG can specify a number of chars to remove from the comment delimiters.

comment-indent-new-line (‘C-M-j’, ‘M-j’)

If you are typing a comment and wish to continue it to another line, type ‘M-j’ or ‘C-M-j’ (‘comment-indent-new-line’). This breaks the current line, and inserts the necessary comment delimiters and indentation to continue the comment.

Like ‘<RET>’ followed by inserting and aligning a comment.

This command is intended for styles where you write a comment per line, starting a new comment (and terminating it if necessary) on each line. If you want to continue one comment across several lines, use M-x newline-and-indent.

Function: comment-indent-new-line &optional soft

Break line at point and indent, continuing comment if within one. This indents the body of the continued comment under the previous comment line.

SOFT

The inserted newline is marked hard if variable ‘use-hard-newlines’ is true, unless optional argument SOFT is non-nil.

comment-insert-comment-function (a variable)
Variable: comment-insert-comment-function

Function to insert a comment when a line doesn’t contain one.

comment-kill
Function: comment-kill arg

Kill the first comment on this line, if any.

ARG

With prefix ARG, kill comments on that many lines starting with this one.

comment-indent
Function: comment-indent &optional continue

Indent this line’s comment to ‘comment-column’, or insert an empty comment.

CONTINUE

if non-nil, use the ‘comment-continue’ markers if any.

comment-set-column (‘C-x ;’)
Function: comment-set-column arg

Set the comment column based on point. With no ARG, set the comment column to the current column.

ARG

With just minus as arg, kill any comment on this line.

With any other ARG, set comment column to indentation of the previous comment and then align or create a comment on this line at that column.

comment-column (a variable)
Variable: comment-column

Column to indent right-margin comments to. Each mode may establish a different default value for this variable; you can set the value for a particular mode using that mode’s hook.

comment-continue (a variable)
Variable: comment-continue

Continuation string to insert for multiline comments. This string will be added at the beginning of each line except the very first one when commenting a region with a commenting style that allows comments to span several lines. It should generally have the same length as comment-start in order to preserve indentation.

comment-start (a variable)
Variable: comment-start

String to insert to start a new comment, or nil if no comment syntax.

comment-fill-column (a variable)
Variable: comment-fill-column

Column to use for ‘comment-indent’. If nil, use ‘fill-column’ instead.

comment-style (a variable)

Its value is ‘indent’.

Variable: comment-style

Style to be used for ‘comment-region’.

comment-styles (a variable)
Variable: comment-styles

Comment region style definitions.

  • plain
  • indent-or-triple
  • indent
  • aligned
  • box
  • extra-line
  • multi-line
  • box-multi
c-indent-comment-alist (a variable)
c-indent-comments-syntactically-p (a variable)

Next: Multi-Line Comments, Previous: Comment Conventions, Up: Comments   [Index]