Êíèãà: Learning GNU Emacs, 3rd Edition
9.2.2 Comments
9.2.2 Comments
All programming languages have comment syntax, so Emacs provides a few features that deal with comments in general; these are made language-specific in each language mode. The universal comment command for all language modes is M-; (for indent-for-comment).[60] When you type M-;, Emacs moves to a column equal to the value of the variable comment-column; if the text on the line goes past that column, it moves to one space past the last text character. It then inserts a comment delimiter (or a pair of opening and closing delimiters, as in /* and */ for C) and puts the cursor after the opening delimiter.
For example, if you want to add a comment to a statement, put the cursor anywhere on the line containing that statement and type M-;. The result is
result += y; /*
_ */
You can then type your comment in between the delimiters. If you were to do the same thing on a longer line of code, say,
q_i = term_arr[i].num_docs / total_docs;
the result would be
q_i = term_arr[i].num_docs / total_docs; /*
_*/
You can customize the variable comment-column, of course, by putting the appropriate code in your .emacs file. This is the most useful way if you want to do it permanently. But if you want to reset comment-column temporarily within the current buffer, you can just move the cursor to where you want the comment column to be and type C-x ; (for set-comment-column). Note that this command affects only the value of comment-column in the current buffer; its value in other buffers—even other buffers in the same mode—is not changed.
When you are typing a comment and want to continue it on the next line, M-j (for indent-new-comment-line) does it. This command starts a new comment on the next line (though some language modes allow you to customize it so that it continues the same comment instead). Say you have typed in the text of the comment for this statement, and the cursor is at the end of the text:
result += y; /* add the multiplicand
_*/
You want to extend the comment to another line. If you type M-j, you get the following:
result += y; /* add the multiplicand*/
/* */
You can type the second line of your comment. You can also use M-j to split existing comment text into two lines. Assume your cursor is positioned like this:
result += y; /* add the
_multiplicand */
If you type M-j now, the result is:
result += y; /* add the */
/* multiplicand */
If you want to comment out a section of your code, you can use the comment-region command (not bound to keystrokes except in certain language modes). Assume you have code that looks like this:
this = is (a);
section (of, source, code);
that += (takes[up]->a * number);
of (lines);
If you define a region in the usual way and type M-x comment-region, the result is:
/* this = is (a); */
/* section (of, source, code); */
/* that += (takes[up]->a * number); */
/* of (lines); */
You can easily get rid of single-line comments by typing M-x kill-comment Enter, which deletes any comment on the current line. The cursor does not have to be within the comment. Each language mode has special features relating to comments in the particular language, usually including variables that let you customize commenting style.