• Whitespace, such as spaces and tabs, which are to be ignored.
• Comments, which are strings of characters to be ignored and surrounded by delimiters that depend on the language (e.g., /* and */ for C, // and a newline for C++ and Java, or semicolon (;) and a newline for Lisp).
Emacs keeps this information internally in the form of syntax tables; like keymaps (as described in Chapter 10), Emacs has a global syntax table used for all buffers, as well a local table for each buffer, which varies according to the mode the buffer is in. You can view the syntax table for the current buffer by typing C-h s (for describe-syntax). In addition, language modes know about more advanced language-dependent syntactic concepts like statements, statement blocks, functions, subroutines, Lisp syntactic expressions, and so on.
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.
9.2.3 Indenting Code
In addition to syntactic knowledge, Emacs language modes contain various features to help you produce nicely formatted code. These features implement standards of indentation, commenting, and other aspects of programming style, thus ensuring consistency and readability, getting comments to line up, and so on. Perhaps more importantly, they relieve you of the tiresome burden of supplying correct indentation and even of remembering what the current indentation is. The nicest thing about these standards is that they are usually customizable.
We have already seen that, in text mode, you can type C-j instead of Enter, at the end of a line, and Emacs indents the next line properly for you. This indentation is controlled by the variable left-margin, whose value is the column to indent to. Much the same thing happens in programming language modes, but the process is more flexible and complex.
As in text mode, C-j indents the next line properly in language modes. You can also indent any line properly after it has been typed by pressing Tab with the cursor anywhere on the line.
Some language modes have extra functionality attached to characters that terminate statements—like semicolons or right curly braces—so that when you type them, Emacs automatically indents the current line. Emacs documentation calls this behavior electric. Most language modes also have sets of variables that control indentation style (and that you can customize).
Table 9-2 lists a few other commands relating to indentation that work according to the rules set up for the language in question.
Table 9-2. Basic indentation commands
| Keystrokes | Command name | Action |
|---|---|---|
| C-M-\ | indent-region | Indent each line between the cursor and mark. |
| M-m | back-to-indentation | Move to the first nonblank character on the line. |
| M-^ | delete-indentation | Join this line to the previous one. |
60
The key binding is mnemonic for Lisp programmers because comments in Lisp start with semicolons.