Книга: Learning GNU Emacs, 3rd Edition
9.2.3 Indenting Code
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. |
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-m is handy for moving to the beginning of the actual code on a line. For example, assume your cursor is positioned like this:
int result = 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 for statement 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: etags and font-lock mode. The etags facility helps programmers who work on large, multifile programs. All language modes can also take advantage of font-lock mode to make development more efficient.
- Formatting Lisp Code
- Indenting Printer
- 7.2.3.1 Indenting regions
- Системные переменные ROWS_AFFECTED, GDSCODE, SQLCODE, TRANSACTIONJD, CONNECTIONJD
- Appendix J. Example scripts code-base
- Test Driver Code
- Regulating Code to Regulate Better
- Part Two - Regulation By Code
- 6. Лекция: Обработка текстов. Регулярные выражения. Unicode.
- Переопределение System.Object.GetHashCode()
- Using the autoconf Utility to Configure Code
- 14.3.2. Debugging Optimized Kernel Code