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-jto split existing comment text into two lines. Assume your cursor is positioned like this:
result += y; /* add the
_ multiplicand */
If you type M-jnow, the result is:
result += y; /* add the */
/* multiplicand */
If you want to comment out a section of your code, you can use the comment-regioncommand (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.
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-jinstead 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-jindents the next line properly in language modes. You can also indent any line properly after it has been typed by pressing Tabwith 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-2lists 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. |
The following is an example of what C-M-\does. This example is in C, and subsequent examples refer to it. The concepts in all examples in this section are applicable to most other languages; we cover analogous Lisp and Java features in the sections on modes for those languages.
Suppose you have the following C code:
int times (x, y)
int x, y;
{
int i;
int result = 0;
for (i = 0; i < x; i++)
{
result += y;
}
}
If you set mark at the beginning of this code, put the cursor at the end, and type C-M-\, Emacs formats it like this:
int times (x, y)
int x, y;
{
int i;
int result = 0;
for (i = 0; i < x; i++)
{
result += y;
}
}
C-M-\is also handy for indenting an entire file according to your particular indentation style: you can just type C-x h(for mark-whole-buffer) followed by C-M-\.
M-mis handy for moving to the beginning of the actual code on a line. For example, assume your cursor is positioned like this:
int resul t= 0;
If you type M-m, it moves to the beginning of the int:
int result = 0;
As an example of M-^, let's say you want the opening curly brace for the forstatement to appear on the same line as the for. Put the cursor anywhere on the line with the opening curly brace, type M-^, and the code looks like this:
for (i = 0; i < x; i++) {
result += y;
}
Language modes usually provide additional indentation commands that relate to specific features of the language. Having covered the general language mode concepts, we want to show you a few other general utilities: etagsand font-lock mode. The etagsfacility helps programmers who work on large, multifile programs. All language modes can also take advantage of font-lock mode to make development more efficient.
Another general feature of Emacs that applies to programmers is the etagsfacility. [61] etagsworks with code in many other languages as well, including Fortran, Java, Perl, Pascal, LATEX,, Lisp, and many assembly languages. If you work on large, multifile projects, you will find etagsto be an enormous help.
etagsis basically a multifile search facility that knows about C and Perl function definitions as well as searching in general. With it, you can find a function anywhere in an entire directory without having to remember in which file the function is defined, and you can do searches and query-replaces that span multiple files. etagsuses tag tables , which contain lists of function names for each file in a directory along with information on where the functions' definitions are located within the files. Many of the commands associated with etagsinvolve regular expressions (see Chapter 11) in search strings.
Читать дальше