"Fossies" - the Fresh Open Source Software Archive

Member "emacs-26.1/doc/emacs/programs.texi" (25 May 2018, 79156 Bytes) of package /linux/misc/emacs-26.1.tar.xz:


Caution: As a special service "Fossies" has tried to format the requested Texinfo source page into HTML format but that may be not always succeeeded perfectly. Alternatively you can here view or download the uninterpreted Texinfo source code. A member file download can also be achieved by clicking within a package contents listing on the according byte size field. See also the latest Fossies "Diffs" side-by-side code changes report for "programs.texi": 25.3_vs_26.1.

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1 Editing Programs

This chapter describes Emacs features for facilitating editing programs. Some of the things these features can do are:


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.1 Major Modes for Programming Languages

Emacs has specialized major modes (@pxref{Major Modes}) for many programming languages. A programming language mode typically specifies the syntax of expressions, the customary rules for indentation, how to do syntax highlighting for the language, and how to find the beginning or end of a function definition. It often has features for compiling and debugging programs as well. The major mode for each language is named after the language; for instance, the major mode for the C programming language is c-mode.

Emacs has programming language modes for Lisp, Scheme, the Scheme-based DSSSL expression language, Ada, ASM, AWK, C, C++, Fortran, Icon, IDL (CORBA), IDLWAVE, Java, Javascript, M4, Makefiles, Metafont (TeX’s companion for font creation), Modula2, Object Pascal, Objective-C, Octave, Pascal, Perl, Pike, PostScript, Prolog, Python, Ruby, Simula, SQL, Tcl, Verilog, and VHDL. An alternative mode for Perl is called CPerl mode. Modes are also available for the scripting languages of the common GNU and Unix shells, and MS-DOS/MS-Windows ‘BAT’ files, and for makefiles, DNS master files, and various sorts of configuration files.

Ideally, Emacs should have a major mode for each programming language that you might want to edit. If it doesn’t have a mode for your favorite language, the mode might be implemented in a package not distributed with Emacs (@pxref{Packages}); or you can contribute one.

In most programming languages, indentation should vary from line to line to illustrate the structure of the program. Therefore, in most programming language modes, typing <TAB> updates the indentation of the current line (see section Indentation for Programs). Furthermore, <DEL> is usually bound to backward-delete-char-untabify, which deletes backward treating each tab as if it were the equivalent number of spaces, so that you can delete one column of indentation without worrying whether the whitespace consists of spaces or tabs.

Entering a programming language mode runs the custom Lisp functions specified in the hook variable prog-mode-hook, followed by those specified in the mode’s own mode hook (@pxref{Major Modes}). For instance, entering C mode runs the hooks prog-mode-hook and c-mode-hook. @xref{Hooks}, for information about hooks.

Separate manuals are available for the modes for Ada (see Ada Mode in Ada Mode), C/C++/Objective C/Java/Corba IDL/Pike/AWK (see CC Mode in CC Mode), Octave, VHDL, and IDLWAVE (see IDLWAVE in IDLWAVE User Manual).


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.2 Top-Level Definitions, or Defuns

In Emacs, a major definition at the top level in the buffer, such as a function, is called a defun. The name comes from Lisp, but in Emacs we use it for all languages.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.2.1 Left Margin Convention

Many programming-language modes assume by default that any opening delimiter found at the left margin is the start of a top-level definition, or defun. Therefore, don’t put an opening delimiter at the left margin unless it should have that significance. For instance, never put an open-parenthesis at the left margin in a Lisp file unless it is the start of a top-level list.

The convention speeds up many Emacs operations, which would otherwise have to scan back to the beginning of the buffer to analyze the syntax of the code.

If you don’t follow this convention, not only will you have trouble when you explicitly use the commands for motion by defuns; other features that use them will also give you trouble. This includes the indentation commands (see section Indentation for Programs) and Font Lock mode (@pxref{Font Lock}).

The most likely problem case is when you want an opening delimiter at the start of a line inside a string. To avoid trouble, put an escape character (‘\’, in C and Emacs Lisp, ‘/’ in some other Lisp dialects) before the opening delimiter. This will not affect the contents of the string, but will prevent that opening delimiter from starting a defun. Here’s an example:

  (insert "Foo:
\(bar)
")

To help you catch violations of this convention, Font Lock mode highlights confusing opening delimiters (those that ought to be quoted) in bold red.

If you need to override this convention, you can do so by setting the variable open-paren-in-column-0-is-defun-start. If this user option is set to t (the default), opening parentheses or braces at column zero always start defuns. When it is nil, defuns are found by searching for parens or braces at the outermost level.

Usually, you should leave this option at its default value of t. If your buffer contains parentheses or braces in column zero which don’t start defuns, and it is somehow impractical to remove these parentheses or braces, it might be helpful to set the option to nil. Be aware that this might make scrolling and display in large buffers quite sluggish. Furthermore, the parentheses and braces must be correctly matched throughout the buffer for it to work properly.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.2.2 Moving by Defuns

These commands move point or set up the region based on top-level major definitions, also called defuns.

C-M-a

Move to beginning of current or preceding defun (beginning-of-defun).

C-M-e

Move to end of current or following defun (end-of-defun).

C-M-h

Put region around whole current or following defun (mark-defun).

The commands to move to the beginning and end of the current defun are C-M-a (beginning-of-defun) and C-M-e (end-of-defun). If you repeat one of these commands, or use a positive numeric argument, each repetition moves to the next defun in the direction of motion.

C-M-a with a negative argument -n moves forward n times to the next beginning of a defun. This is not exactly the same place that C-M-e with argument n would move to; the end of this defun is not usually exactly the same place as the beginning of the following defun. (Whitespace, comments, and perhaps declarations can separate them.) Likewise, C-M-e with a negative argument moves back to an end of a defun, which is not quite the same as C-M-a with a positive argument.

To operate on the current defun, use C-M-h (mark-defun), which sets the mark at the end of the current defun and puts point at its beginning. @xref{Marking Objects}. This is the easiest way to get ready to kill the defun in order to move it to a different place in the file. If the defun is directly preceded by comments (with no intervening blank lines), they are marked, too. If you use the command while point is between defuns, it uses the following defun. If you use the command while the mark is already active, it extends the end of the region to include one more defun. With a prefix argument, it marks that many defuns or extends the region by the appropriate number of defuns. With negative prefix argument it marks defuns in the opposite direction and also changes the direction of selecting for subsequent uses of mark-defun.

In C mode, C-M-h runs the function c-mark-function, which is almost the same as mark-defun; the difference is that it backs up over the argument declarations, function name and returned data type so that the entire C function is inside the region. This is an example of how major modes adjust the standard key bindings so that they do their standard jobs in a way better fitting a particular language. Other major modes may replace any or all of these key bindings for that purpose.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.2.3 Imenu

The Imenu facility offers a way to find the major definitions in a file by name. It is also useful in text formatter major modes, where it treats each chapter, section, etc., as a definition. (@xref{Xref}, for a more powerful feature that handles multiple files together.)

If you type M-x imenu, it reads the name of a definition using the minibuffer, then moves point to that definition. You can use completion to specify the name; the command always displays the whole list of valid names.

Alternatively, you can bind the command imenu to a mouse click. Then it displays mouse menus for you to select a definition name. You can also add the buffer’s index to the menu bar by calling imenu-add-menubar-index. If you want to have this menu bar item available for all buffers in a certain major mode, you can do this by adding imenu-add-menubar-index to its mode hook. But if you have done that, you will have to wait a little while each time you visit a file in that mode, while Emacs finds all the definitions in that buffer.

When you change the contents of a buffer, if you add or delete definitions, you can update the buffer’s index based on the new contents by invoking the ‘*Rescan*’ item in the menu. Rescanning happens automatically if you set imenu-auto-rescan to a non-nil value. There is no need to rescan because of small changes in the text.

You can customize the way the menus are sorted by setting the variable imenu-sort-function. By default, names are ordered as they occur in the buffer; if you want alphabetic sorting, use the symbol imenu--sort-by-name as the value. You can also define your own comparison function by writing Lisp code.

Imenu provides the information to guide Which Function mode (see section Which Function Mode). The Speedbar can also use it (@pxref{Speedbar}).


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.2.4 Which Function Mode

Which Function mode is a global minor mode (@pxref{Minor Modes}) which displays the current function name in the mode line, updating it as you move around in a buffer.

To either enable or disable Which Function mode, use the command M-x which-function-mode. Which Function mode is a global minor mode. By default, it takes effect in all major modes that know how to support it (i.e., all the major modes that support Imenu). You can restrict it to a specific list of major modes by changing the value of the variable which-func-modes from t (which means to support all available major modes) to a list of major mode names.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.3 Indentation for Programs

The best way to keep a program properly indented is to use Emacs to reindent it as you change it. Emacs has commands to indent either a single line, a specified number of lines, or all of the lines inside a single parenthetical grouping.

@xref{Indentation}, for general information about indentation. This section describes indentation features specific to programming language modes.

Emacs also provides a Lisp pretty-printer in the pp package, which reformats Lisp objects with nice-looking indentation. See pp in The Emacs Lisp Reference Manual.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.3.1 Basic Program Indentation Commands

<TAB>

Adjust indentation of current line (indent-for-tab-command).

<RET>

Insert a newline, then adjust indentation of following line (newline).

The basic indentation command is <TAB> (indent-for-tab-command), which was documented in @ref{Indentation}. In programming language modes, <TAB> indents the current line, based on the indentation and syntactic content of the preceding lines; if the region is active, <TAB> indents each line within the region, not just the current line.

The command <RET> (newline), which was documented in @ref{Inserting Text}, does the same as C-j followed by <TAB>: it inserts a new line, then adjusts the line’s indentation.

When indenting a line that starts within a parenthetical grouping, Emacs usually places the start of the line under the preceding line within the group, or under the text after the parenthesis. If you manually give one of these lines a nonstandard indentation (e.g., for aesthetic purposes), the lines below will follow it.

The indentation commands for most programming language modes assume that an open-parenthesis, open-brace or other opening delimiter at the left margin is the start of a function. If the code you are editing violates this assumption—even if the delimiters occur in strings or comments—you must set open-paren-in-column-0-is-defun-start to nil for indentation to work properly. See section Left Margin Convention.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.3.2 Indenting Several Lines

Sometimes, you may want to reindent several lines of code at a time. One way to do this is to use the mark; when the mark is active and the region is non-empty, <TAB> indents every line in the region. Alternatively, the command C-M-\ (indent-region) indents every line in the region, whether or not the mark is active (@pxref{Indentation Commands}).

In addition, Emacs provides the following commands for indenting large chunks of code:

C-M-q

Reindent all the lines within one parenthetical grouping.

C-u <TAB>

Shift an entire parenthetical grouping rigidly sideways so that its first line is properly indented.

M-x indent-code-rigidly

Shift all the lines in the region rigidly sideways, but do not alter lines that start inside comments and strings.

To reindent the contents of a single parenthetical grouping, position point before the beginning of the grouping and type C-M-q. This changes the relative indentation within the grouping, without affecting its overall indentation (i.e., the indentation of the line where the grouping starts). The function that C-M-q runs depends on the major mode; it is indent-pp-sexp in Lisp mode, c-indent-exp in C mode, etc. To correct the overall indentation as well, type <TAB> first.

If you like the relative indentation within a grouping but not the indentation of its first line, move point to that first line and type C-u <TAB>. In Lisp, C, and some other major modes, <TAB> with a numeric argument reindents the current line as usual, then reindents by the same amount all the lines in the parenthetical grouping starting on the current line. It is clever, though, and does not alter lines that start inside strings. Neither does it alter C preprocessor lines when in C mode, but it does reindent any continuation lines that may be attached to them.

The command M-x indent-code-rigidly rigidly shifts all the lines in the region sideways, like indent-rigidly does (@pxref{Indentation Commands}). It doesn’t alter the indentation of lines that start inside a string, unless the region also starts inside that string. The prefix arg specifies the number of columns to indent.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.3.3 Customizing Lisp Indentation

The indentation pattern for a Lisp expression can depend on the function called by the expression. For each Lisp function, you can choose among several predefined patterns of indentation, or define an arbitrary one with a Lisp program.

The standard pattern of indentation is as follows: the second line of the expression is indented under the first argument, if that is on the same line as the beginning of the expression; otherwise, the second line is indented underneath the function name. Each following line is indented under the previous line whose nesting depth is the same.

If the variable lisp-indent-offset is non-nil, it overrides the usual indentation pattern for the second line of an expression, so that such lines are always indented lisp-indent-offset more columns than the containing list.

Certain functions override the standard pattern. Functions whose names start with def treat the second lines as the start of a body, by indenting the second line lisp-body-indent additional columns beyond the open-parenthesis that starts the expression.

You can override the standard pattern in various ways for individual functions, according to the lisp-indent-function property of the function name. This is normally done for macro definitions, using the declare construct. See Defining Macros in The Emacs Lisp Reference Manual.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.3.4 Commands for C Indentation

Here are special features for indentation in C mode and related modes:

C-c C-q

Reindent the current top-level function definition or aggregate type declaration (c-indent-defun).

C-M-q

Reindent each line in the balanced expression that follows point (c-indent-exp). A prefix argument inhibits warning messages about invalid syntax.

<TAB>

Reindent the current line, and/or in some cases insert a tab character (c-indent-command).

If c-tab-always-indent is t, this command always reindents the current line and does nothing else. This is the default.

If that variable is nil, this command reindents the current line only if point is at the left margin or in the line’s indentation; otherwise, it inserts a tab (or the equivalent number of spaces, if indent-tabs-mode is nil).

Any other value (not nil or t) means always reindent the line, and also insert a tab if within a comment or a string.

To reindent the whole current buffer, type C-x h C-M-\. This first selects the whole buffer as the region, then reindents that region.

To reindent the current block, use C-M-u C-M-q. This moves to the front of the block and then reindents it all.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.3.5 Customizing C Indentation

C mode and related modes use a flexible mechanism for customizing indentation. C mode indents a source line in two steps: first it classifies the line syntactically according to its contents and context; second, it determines the indentation offset associated by your selected style with the syntactic construct and adds this onto the indentation of the anchor statement.

C-c . style <RET>

Select a predefined style style (c-set-style).

A style is a named collection of customizations that can be used in C mode and the related modes. Styles in The CC Mode Manual, for a complete description. Emacs comes with several predefined styles, including gnu, k&r, bsd, stroustrup, linux, python, java, whitesmith, ellemtel, and awk. Some of these styles are primarily intended for one language, but any of them can be used with any of the languages supported by these modes. To find out what a style looks like, select it and reindent some code, e.g., by typing C-M-q at the start of a function definition.

To choose a style for the current buffer, use the command C-c .. Specify a style name as an argument (case is not significant). This command affects the current buffer only, and it affects only future invocations of the indentation commands; it does not reindent the code already in the buffer. To reindent the whole buffer in the new style, you can type C-x h C-M-\.

You can also set the variable c-default-style to specify the default style for various major modes. Its value should be either the style’s name (a string) or an alist, in which each element specifies one major mode and which indentation style to use for it. For example,

(setq c-default-style
      '((java-mode . "java")
        (awk-mode . "awk")
        (other . "gnu")))

specifies explicit choices for Java and AWK modes, and the default ‘gnu’ style for the other C-like modes. (These settings are actually the defaults.) This variable takes effect when you select one of the C-like major modes; thus, if you specify a new default style for Java mode, you can make it take effect in an existing Java mode buffer by typing M-x java-mode there.

The gnu style specifies the formatting recommended by the GNU Project for C; it is the default, so as to encourage use of our recommended style.

See Indentation Engine Basics in the CC Mode Manual, and Customizing Indentation in the CC Mode Manual, for more information on customizing indentation for C and related modes, including how to override parts of an existing style and how to define your own styles.

As an alternative to specifying a style, you can tell Emacs to guess a style by typing M-x c-guess in a sample code buffer. You can then apply the guessed style to other buffers with M-x c-guess-install. See Guessing the Style in the CC Mode Manual, for details.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.4 Commands for Editing with Parentheses

This section describes the commands and features that take advantage of the parenthesis structure in a program, or help you keep it balanced.

When talking about these facilities, the term “parenthesis” also includes braces, brackets, or whatever delimiters are defined to match in pairs. The major mode controls which delimiters are significant, through the syntax table (see Syntax Tables in The Emacs Lisp Reference Manual). In Lisp, only parentheses count; in C, these commands apply to braces and brackets too.

You can use M-x check-parens to find any unbalanced parentheses and unbalanced string quotes in the buffer.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.4.1 Expressions with Balanced Parentheses

Each programming language mode has its own definition of a balanced expression. Balanced expressions typically include individual symbols, numbers, and string constants, as well as pieces of code enclosed in a matching pair of delimiters. The following commands deal with balanced expressions (in Emacs, such expressions are referred to internally as sexps(1)).

C-M-f

Move forward over a balanced expression (forward-sexp).

C-M-b

Move backward over a balanced expression (backward-sexp).

C-M-k

Kill balanced expression forward (kill-sexp).

C-M-t

Transpose expressions (transpose-sexps).

C-M-@
C-M-<SPC>

Put mark after following expression (mark-sexp).

To move forward over a balanced expression, use C-M-f (forward-sexp). If the first significant character after point is an opening delimiter (e.g., ‘(’, ‘[’ or ‘{’ in C), this command moves past the matching closing delimiter. If the character begins a symbol, string, or number, the command moves over that.

The command C-M-b (backward-sexp) moves backward over a balanced expression—like C-M-f, but in the reverse direction. If the expression is preceded by any prefix characters (single-quote, backquote and comma, in Lisp), the command moves back over them as well.

C-M-f or C-M-b with an argument repeats that operation the specified number of times; with a negative argument means to move in the opposite direction. In most modes, these two commands move across comments as if they were whitespace. Note that their keys, C-M-f and C-M-b, are analogous to C-f and C-b, which move by characters (@pxref{Moving Point}), and M-f and M-b, which move by words (@pxref{Words}).

To kill a whole balanced expression, type C-M-k (kill-sexp). This kills the text that C-M-f would move over.

C-M-t (transpose-sexps) switches the positions of the previous balanced expression and the next one. It is analogous to the C-t command, which transposes characters (@pxref{Transpose}). An argument to C-M-t serves as a repeat count, moving the previous expression over that many following ones. A negative argument moves the previous balanced expression backwards across those before it. An argument of zero, rather than doing nothing, transposes the balanced expressions ending at or after point and the mark.

To operate on balanced expressions with a command which acts on the region, type C-M-<SPC> (mark-sexp). This sets the mark where C-M-f would move to. While the mark is active, each successive call to this command extends the region by shifting the mark by one expression. Positive or negative numeric arguments move the mark forward or backward by the specified number of expressions. The alias C-M-@ is equivalent to C-M-<SPC>. @xref{Marking Objects}, for more information about this and related commands.

In languages that use infix operators, such as C, it is not possible to recognize all balanced expressions because there can be multiple possibilities at a given position. For example, C mode does not treat ‘foo + bar’ as a single expression, even though it is one C expression; instead, it recognizes ‘foo’ as one expression and ‘bar’ as another, with the ‘+’ as punctuation between them. However, C mode recognizes ‘(foo + bar)’ as a single expression, because of the parentheses.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.4.2 Moving in the Parenthesis Structure

The following commands move over groupings delimited by parentheses (or whatever else serves as delimiters in the language you are working with). They ignore strings and comments, including any parentheses within them, and also ignore parentheses that are quoted with an escape character. These commands are mainly intended for editing programs, but can be useful for editing any text containing parentheses. They are referred to internally as “list commands” because in Lisp these groupings are lists.

These commands assume that the starting point is not inside a string or a comment. If you invoke them from inside a string or comment, the results are unreliable.

C-M-n

Move forward over a parenthetical group (forward-list).

C-M-p

Move backward over a parenthetical group (backward-list).

C-M-u

Move up in parenthesis structure (backward-up-list).

C-M-d

Move down in parenthesis structure (down-list).

The list commands C-M-n (forward-list) and C-M-p (backward-list) move forward or backward over one (or n) parenthetical groupings.

C-M-n and C-M-p try to stay at the same level in the parenthesis structure. To move up one (or n) levels, use C-M-u (backward-up-list). C-M-u moves backward up past one unmatched opening delimiter. A positive argument serves as a repeat count; a negative argument reverses the direction of motion, so that the command moves forward and up one or more levels.

To move down in the parenthesis structure, use C-M-d (down-list). In Lisp mode, where ‘(’ is the only opening delimiter, this is nearly the same as searching for a ‘(’. An argument specifies the number of levels to go down.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.4.3 Matching Parentheses

Emacs has a number of parenthesis matching features, which make it easy to see how and whether parentheses (or other delimiters) match up.

Whenever you type a self-inserting character that is a closing delimiter, Emacs briefly indicates the location of the matching opening delimiter, provided that is on the screen. If it is not on the screen, Emacs displays some of the text near it in the echo area. Either way, you can tell which grouping you are closing off. If the opening delimiter and closing delimiter are mismatched—such as in ‘[x)’—a warning message is displayed in the echo area.

Three variables control the display of matching parentheses:

Show Paren mode, a global minor mode, provides a more powerful kind of automatic matching. Whenever point is before an opening delimiter or after a closing delimiter, the delimiter, its matching delimiter, and optionally the text between them are highlighted. To toggle Show Paren mode, type M-x show-paren-mode. To customize it, type M-x customize-group <RET> paren-showing. The customizable options which control the operation of this mode include:

Electric Pair mode, a global minor mode, provides a way to easily insert matching delimiters: parentheses, braces, brackets, etc. Whenever you insert an opening delimiter, the matching closing delimiter is automatically inserted as well, leaving point between the two. Conversely, when you insert a closing delimiter over an existing one, no insertion takes places, and that position is simply skipped over. If the region is active (@pxref{Mark}), insertion of a delimiter operates on the region: the characters in the region are enclosed in a pair of matching delimiters, leaving point after the delimiter you typed.

These variables control additional features of Electric Pair mode:

To toggle Electric Pair mode, type M-x electric-pair-mode. To toggle the mode in a single buffer, use M-x electric-pair-local-mode.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.5 Manipulating Comments

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 (@pxref{Spelling}).

Some major modes have special rules for indenting different kinds of comments. For example, in Lisp code, comments starting with two semicolons are indented as if they were lines of code, while those starting with three semicolons are supposed to be aligned to the left margin and are often used for sectioning purposes. Emacs understands these conventions; for instance, typing <TAB> on a comment line will indent the comment to the appropriate position.

;; This function is just an example.
;;; Here either two or three semicolons are appropriate.
(defun foo (x)
;;;  And now, the first part of the function:
  ;; The following line adds one.
  (1+ x))           ; This line adds one.

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.5.1 Comment Commands

The following commands operate on comments:

M-;

Insert or realign comment on current line; if the region is active, comment or uncomment the region instead (comment-dwim).

C-x C-;

Comment or uncomment the current line (comment-line). If the region is active, comment or uncomment the lines in the region instead.

C-u M-;

Kill comment on current line (comment-kill).

C-x ;

Set comment column (comment-set-column).

C-M-j
M-j

Like <RET> followed by inserting and aligning a comment (comment-indent-new-line). See section Multiple Lines of Comments.

M-x comment-region
C-c C-c (in C-like modes)

Add comment delimiters to all the lines in the region.

The command to create or align a comment is M-; (comment-dwim). The word “dwim” is an acronym for “Do What I Mean”; it indicates that this command can be used for many different jobs relating to comments, depending on the situation where you use it.

When a region is active (@pxref{Mark}), M-; either adds comment delimiters to the region, or removes them. If every line in the region is already a comment, it uncomments each of those lines by removing their comment delimiters. Otherwise, it adds comment delimiters to enclose the text in the region.

If you supply a prefix argument to M-; when a region is active, that specifies the number of comment delimiters to add or delete. A positive argument n adds n delimiters, while a negative argument -n removes n delimiters.

If the region is not active, and there is no existing comment on the current line, M-; adds a new comment to the current line. If the line is blank (i.e., empty or containing only whitespace characters), the comment is indented to the same position where <TAB> would indent to (see section Basic Program Indentation Commands). If the line is non-blank, the comment is placed after the last non-whitespace character on the line. Emacs tries to fit the comment between the columns specified by the variables comment-column and comment-fill-column (see section Options Controlling Comments), if possible. Otherwise, it will choose some other suitable position, usually separated from the non-comment text by at least one space. In each case, Emacs places point after the comment’s starting delimiter, so that you can start typing the comment text right away.

You can also use M-; to align an existing comment. If a line already contains the comment-start string, M-; realigns it to the conventional alignment and moves point after the comment’s starting delimiter. As an exception, comments starting in column 0 are not moved. Even when an existing comment is properly aligned, M-; is still useful for moving directly to the start of the comment text.

C-x C-; (comment-line) comments or uncomments complete lines. When a region is active (@pxref{Mark}), C-x C-; either comments or uncomments the lines in the region. If the region is not active, this command comments or uncomments the line point is on. With a positive prefix argument n, it operates on n lines starting with the current one; with a negative n, it affects n preceding lines. After invoking this command with a negative argument, successive invocations with a positive argument will operate on preceding lines as if the argument were negated.

C-u M-; (comment-dwim with a prefix argument) kills any comment on the current line, along with the whitespace before it. Since the comment is saved to the kill ring, you can reinsert it on another line by moving to the end of that line, doing C-y, and then M-; to realign the comment. You can achieve the same effect as C-u M-; by typing M-x comment-kill (comment-dwim actually calls comment-kill as a subroutine when it is given a prefix argument).

The command M-x comment-region is equivalent to calling M-; on an active region, except that it always acts on the region, even if the mark is inactive. In C mode and related modes, this command is bound to C-c C-c. The command M-x uncomment-region uncomments each line in the region; a numeric prefix argument specifies the number of comment delimiters to remove (negative arguments specify the number of comment delimiters to add).

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. For example, on a line ending in a closing brace, M-; puts the comment one space after the brace rather than at comment-column. For full details see Comment Commands in The CC Mode Manual.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.5.2 Multiple Lines of Comments

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.

For languages with closing comment delimiters (e.g., ‘*/’ in C), the exact behavior of M-j depends on the value of the variable comment-multi-line. If the value is nil, the command closes the comment on the old line and starts a new comment on the new line. Otherwise, it opens a new line within the current comment delimiters.

When Auto Fill mode is on, going past the fill column while typing a comment also continues the comment, in the same way as an explicit invocation of M-j.

To turn existing lines into comment lines, use M-; with the region active, or use M-x comment-region as described in the preceding section.

You can configure C Mode such that when you type a ‘/’ at the start of a line in a multi-line block comment, this closes the comment. Enable the comment-close-slash clean-up for this. See Clean-ups in The CC Mode Manual.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.5.3 Options Controlling Comments

As mentioned in Comment Commands, when the M-j command adds a comment to a line, it tries to place the comment between the columns specified by the buffer-local variables comment-column and comment-fill-column (or if that is nil, then the value of fill-column, @pxref{Fill Commands}). You can set either the local value or the default value of these buffer-local variables in the usual way (@pxref{Locals}). Alternatively, you can type C-x ; (comment-set-column) to set the value of comment-column in the current buffer to the column where point is currently located. C-u C-x ; sets the comment column to match the last comment before point in the buffer, and then does a M-; to align the current line’s comment under the previous one.

The comment commands recognize comments based on the regular expression that is the value of the variable comment-start-skip. Make sure this regexp does not match the null string. It may match more than the comment starting delimiter in the strictest sense of the word; for example, in C mode the value of the variable is "\\(//+\\|/\\*+\\)\\s *", which matches extra stars and spaces after the ‘/*’ itself, and accepts C++ style comments also. (Note that ‘\\’ is needed in Lisp syntax to include a ‘\’ in the string, which is needed to deny the first star its special meaning in regexp syntax. @xref{Regexp Backslash}.)

When a comment command makes a new comment, it inserts the value of comment-start as an opening comment delimiter. It also inserts the value of comment-end after point, as a closing comment delimiter. For example, in Lisp mode, comment-start is ‘";"’ and comment-end is "" (the empty string). In C mode, comment-start is "/* " and comment-end is " */".

The variable comment-padding specifies a string that the commenting commands should insert between the comment delimiter(s) and the comment text. The default, ‘" "’, specifies a single space. Alternatively, the value can be a number, which specifies that number of spaces, or nil, which means no spaces at all.

The variable comment-multi-line controls how M-j and Auto Fill mode continue comments over multiple lines. See section Multiple Lines of Comments.

The variable comment-indent-function should contain a function that will be called to compute the alignment for a newly inserted comment or for aligning an existing comment. It is set differently by various major modes. The function is called with no arguments, but with point at the beginning of the comment, or at the end of a line if a new comment is to be inserted. It should return the column in which the comment ought to start. For example, the default function bases its decision on how many comment characters begin an existing comment.

Emacs also tries to align comments on adjacent lines. To override this, the function may return a cons of two (possibly equal) integers to indicate an acceptable range of indentation.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.6 Documentation Lookup

Emacs provides several features you can use to look up the documentation of functions, variables and commands that you plan to use in your program.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.6.1 Info Documentation Lookup

For major modes that apply to languages which have documentation in Info, you can use C-h S (info-lookup-symbol) to view the Info documentation for a symbol used in the program. You specify the symbol with the minibuffer; the default is the symbol appearing in the buffer at point. For example, in C mode this looks for the symbol in the C Library Manual. The command only works if the appropriate manual’s Info files are installed.

Emacs determines where to look for documentation for the symbol—which Info files to look in, and which indices to search—based on the major mode. You can also use M-x info-lookup-file to look for documentation for a file name.

If you use C-h S in a major mode that does not support it, it asks you to specify the symbol help mode. You should enter a command such as c-mode that would select a major mode which C-h S does support.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.6.2 Man Page Lookup

On Unix, the main form of on-line documentation was the manual page or man page. In the GNU operating system, we aim to replace man pages with better-organized manuals that you can browse with Info (@pxref{Misc Help}). This process is not finished, so it is still useful to read manual pages.

You can read the man page for an operating system command, library function, or system call, with the M-x man command. This prompts for a topic, with completion (@pxref{Completion}), and runs the man program to format the corresponding man page. If the system permits, it runs man asynchronously, so that you can keep on editing while the page is being formatted. The result goes in a buffer named ‘*Man topic*’. These buffers use a special major mode, Man mode, that facilitates scrolling and jumping to other manual pages. For details, type C-h m while in a Man mode buffer.

Each man page belongs to one of ten or more sections, each named by a digit or by a digit and a letter. Sometimes there are man pages with the same name in different sections. To read a man page from a specific section, type ‘topic(section)’ or ‘section topic’ when M-x man prompts for the topic. For example, the man page for the C library function chmod is in section 2, but there is a shell command of the same name, whose man page is in section 1; to view the former, type M-x man <RET> chmod(2) <RET>.

If you do not specify a section, M-x man normally displays only the first man page found. On some systems, the man program accepts a ‘-a’ command-line option, which tells it to display all the man pages for the specified topic. To make use of this, change the value of the variable Man-switches to ‘"-a"’. Then, in the Man mode buffer, you can type M-n and M-p to switch between man pages in different sections. The mode line shows how many manual pages are available.

An alternative way of reading manual pages is the M-x woman command. Unlike M-x man, it does not run any external programs to format and display the man pages; the formatting is done by Emacs, so it works on systems such as MS-Windows where the man program may be unavailable. It prompts for a man page, and displays it in a buffer named ‘*WoMan section topic’.

M-x woman computes the completion list for manpages the first time you invoke the command. With a numeric argument, it recomputes this list; this is useful if you add or delete manual pages.

If you type a name of a manual page and M-x woman finds that several manual pages by the same name exist in different sections, it pops up a window with possible candidates asking you to choose one of them.

Note that M-x woman doesn’t yet support the latest features of modern man pages, so we recommend using M-x man if that is available on your system.

For more information about setting up and using M-x woman, see the WoMan Info manual, which is distributed with Emacs.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.6.3 Emacs Lisp Documentation Lookup

When editing Emacs Lisp code, you can use the commands C-h f (describe-function) and C-h v (describe-variable) to view the built-in documentation for the Lisp functions and variables that you want to use. @xref{Name Help}.

Eldoc is a buffer-local minor mode that helps with looking up Lisp documentation. When it is enabled, the echo area displays some useful information whenever there is a Lisp function or variable at point; for a function, it shows the argument list, and for a variable it shows the first line of the variable’s documentation string. To toggle Eldoc mode, type M-x eldoc-mode. There’s also a Global Eldoc mode, which is turned on by default, and affects buffers, such as ‘*scratch*’, whose major mode is Emacs Lisp or Lisp Interaction (M-x global-eldoc-mode to turn it off globally).


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.7 Hideshow minor mode

Hideshow mode is a buffer-local minor mode that allows you to selectively display portions of a program, which are referred to as blocks. Type M-x hs-minor-mode to toggle this minor mode (@pxref{Minor Modes}).

When you use Hideshow mode to hide a block, the block disappears from the screen, to be replaced by an ellipsis (three periods in a row). Just what constitutes a block depends on the major mode. In C mode and related modes, blocks are delimited by braces, while in Lisp mode they are delimited by parentheses. Multi-line comments also count as blocks.

Hideshow mode provides the following commands:

C-c @ C-h
C-c @ C-d

Hide the current block (hs-hide-block).

C-c @ C-s

Show the current block (hs-show-block).

C-c @ C-c
C-x @ C-e

Either hide or show the current block (hs-toggle-hiding).

S-mouse-2

Toggle hiding for the block you click on (hs-mouse-toggle-hiding).

C-c @ C-M-h
C-c @ C-t

Hide all top-level blocks (hs-hide-all).

C-c @ C-M-s
C-c @ C-a

Show all blocks in the buffer (hs-show-all).

C-u n C-c @ C-l

Hide all blocks n levels below this block (hs-hide-level).

These variables can be used to customize Hideshow mode:

hs-hide-comments-when-hiding-all

If non-nil, C-c @ C-M-h (hs-hide-all) hides comments too.

hs-isearch-open

This variable specifies the conditions under which incremental search should unhide a hidden block when matching text occurs within the block. Its value should be either code (unhide only code blocks), comment (unhide only comments), t (unhide both code blocks and comments), or nil (unhide neither code blocks nor comments). The default value is code.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.8 Completion for Symbol Names

Completion is normally done in the minibuffer (@pxref{Completion}), but you can also complete symbol names in ordinary Emacs buffers.

In programming language modes, type C-M-i or M-<TAB> to complete the partial symbol before point. On graphical displays, the M-<TAB> key is usually reserved by the window manager for switching graphical windows, so you should type C-M-i or <ESC> <TAB> instead.

In most programming language modes, C-M-i (or M-<TAB>) invokes the command completion-at-point, which generates its completion list in a flexible way. If Semantic mode is enabled, it tries to use the Semantic parser data for completion (see section Semantic). If Semantic mode is not enabled or fails at performing completion, it tries to complete using the selected tags table (@pxref{Tags Tables}). If in Emacs Lisp mode, it performs completion using the function, variable, or property names defined in the current Emacs session.

In all other respects, in-buffer symbol completion behaves like minibuffer completion. For instance, if Emacs cannot complete to a unique symbol, it displays a list of completion alternatives in another window. @xref{Completion}.

In Text mode and related modes, M-<TAB> completes words based on the spell-checker’s dictionary. @xref{Spelling}.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.9 MixedCase Words

Some programming styles make use of mixed-case (or “CamelCase”) symbols like ‘unReadableSymbol’. (In the GNU project, we recommend using underscores to separate words within an identifier, rather than using case distinctions.) Emacs has various features to make it easier to deal with such symbols.

Glasses mode is a buffer-local minor mode that makes it easier to read such symbols, by altering how they are displayed. By default, it displays extra underscores between each lower-case letter and the following capital letter. This does not alter the buffer text, only how it is displayed.

To toggle Glasses mode, type M-x glasses-mode (@pxref{Minor Modes}). When Glasses mode is enabled, the minor mode indicator ‘o^o’ appears in the mode line. For more information about Glasses mode, type C-h P glasses <RET>.

Subword mode is another buffer-local minor mode. In subword mode, Emacs’s word commands recognize upper case letters in ‘StudlyCapsIdentifiers’ as word boundaries. When Subword mode is enabled, the minor mode indicator ‘,’ appears in the mode line. See also the similar superword-mode (see section Other Features Useful for Editing Programs).


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.10 Semantic

Semantic is a package that provides language-aware editing commands based on source code parsers. This section provides a brief description of Semantic; for full details, see Semantic in Semantic.

Most of the language-aware features in Emacs, such as Font Lock mode (@pxref{Font Lock}), rely on rules of thumb(2) that usually give good results but are never completely exact. In contrast, the parsers used by Semantic have an exact understanding of programming language syntax. This allows Semantic to provide search, navigation, and completion commands that are powerful and precise.

To begin using Semantic, type M-x semantic-mode or click on the menu item named ‘Source Code Parsers (Semantic)’ in the ‘Tools’ menu. This enables Semantic mode, a global minor mode.

When Semantic mode is enabled, Emacs automatically attempts to parse each file you visit. Currently, Semantic understands C, C++, HTML, Java, Javascript, Make, Python, Scheme, SRecode, and Texinfo. Within each parsed buffer, the following commands are available:

C-c , j

Prompt for the name of a function defined in the current file, and move point there (semantic-complete-jump-local).

C-c , J

Prompt for the name of a function defined in any file Emacs has parsed, and move point there (semantic-complete-jump).

C-c , <SPC>

Display a list of possible completions for the symbol at point (semantic-complete-analyze-inline). This also activates a set of special key bindings for choosing a completion: <RET> accepts the current completion, M-n and M-p cycle through possible completions, <TAB> completes as far as possible and then cycles, and C-g or any other key aborts completion.

C-c , l

Display a list of the possible completions of the symbol at point, in another window (semantic-analyze-possible-completions).

In addition to the above commands, the Semantic package provides a variety of other ways to make use of parser information. For instance, you can use it to display a list of completions when Emacs is idle. See Semantic in Semantic, for details.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.11 Other Features Useful for Editing Programs

Some Emacs commands that aren’t designed specifically for editing programs are useful for that nonetheless.

The Emacs commands that operate on words, sentences and paragraphs are useful for editing code. Most symbol names contain words (@pxref{Words}), while sentences can be found in strings and comments (@pxref{Sentences}). As for paragraphs, they are defined in most programming language modes to begin and end at blank lines (@pxref{Paragraphs}). Therefore, judicious use of blank lines to make the program clearer will also provide useful chunks of text for the paragraph commands to work on. Auto Fill mode, if enabled in a programming language major mode, indents the new lines which it creates.

Superword mode is a buffer-local minor mode that causes editing and motion commands to treat symbols (e.g., ‘this_is_a_symbol’) as words. When Superword mode is enabled, the minor mode indicator ‘²’ appears in the mode line. See also the similar subword-mode (see section MixedCase Words).

Electric Layout mode (M-x electric-layout-mode) is a global minor mode that automatically inserts newlines when you type certain characters; for example, ‘{’, ‘}’ and ‘;’ in Javascript mode.

Apart from Hideshow mode (see section Hideshow minor mode), another way to selectively display parts of a program is to use the selective display feature (@pxref{Selective Display}). Programming modes often also support Outline minor mode (@pxref{Outline Mode}), which can be used with the Foldout package (@pxref{Foldout}).

Prettify Symbols mode is a buffer-local minor mode that replaces certain strings with more attractive versions for display purposes. For example, in Emacs Lisp mode, it replaces the string ‘lambda’ with the Greek lambda character ‘λ’. In a TeX buffer, it will replace ‘\alpha’ … ‘\omega’ and other math macros with their Unicode characters. You may wish to use this in non-programming modes as well. You can customize the mode by adding more entries to prettify-symbols-alist. More elaborate customization is available via customizing prettify-symbols-compose-predicate if its default value prettify-symbols-default-compose-p is not appropriate. There is also a global version, global-prettify-symbols-mode, which enables the mode in all buffers that support it.

The symbol at point can be shown in its original form. This is controlled by the variable prettify-symbols-unprettify-at-point: if non-nil, the original form of symbol at point will be restored for as long as point is at it.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.12 C and Related Modes

This section gives a brief description of the special features available in C, C++, Objective-C, Java, CORBA IDL, Pike and AWK modes. (These are called “C mode and related modes”.) For more details, see the CC mode Info manual, which is distributed with Emacs.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.12.1 C Mode Motion Commands

This section describes commands for moving point, in C mode and related modes.

C-M-a
C-M-e

Move point to the beginning or end of the current function or top-level definition. In languages with enclosing scopes (such as C++’s classes) the current function is the immediate one, possibly inside a scope. Otherwise it is the one defined by the least enclosing braces. (By contrast, beginning-of-defun and end-of-defun search for braces in column zero.) See section Moving by Defuns.

C-c C-u

Move point back to the containing preprocessor conditional, leaving the mark behind. A prefix argument acts as a repeat count. With a negative argument, move point forward to the end of the containing preprocessor conditional.

#elif’ is equivalent to ‘#else’ followed by ‘#if’, so the function will stop at a ‘#elif’ when going backward, but not when going forward.

C-c C-p

Move point back over a preprocessor conditional, leaving the mark behind. A prefix argument acts as a repeat count. With a negative argument, move forward.

C-c C-n

Move point forward across a preprocessor conditional, leaving the mark behind. A prefix argument acts as a repeat count. With a negative argument, move backward.

M-a

Move point to the beginning of the innermost C statement (c-beginning-of-statement). If point is already at the beginning of a statement, move to the beginning of the preceding statement. With prefix argument n, move back n - 1 statements.

In comments or in strings which span more than one line, this command moves by sentences instead of statements.

M-e

Move point to the end of the innermost C statement or sentence; like M-a except that it moves in the other direction (c-end-of-statement).


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.12.2 Electric C Characters

In C mode and related modes, certain printing characters are electric—in addition to inserting themselves, they also reindent the current line, and optionally also insert newlines. The electric characters are {, }, :, #, ;, ,, <, >, /, *, (, and ).

You might find electric indentation inconvenient if you are editing chaotically indented code. If you are new to CC Mode, you might find it disconcerting. You can toggle electric action with the command C-c C-l; when it is enabled, ‘/cl’ appears in the mode line after the mode name (where c, if present, is ‘*’ or ‘/’, depending on whether the comment style is block or line). See Minor Modes in The CC Mode Manual, for more about mode-line indicators in CC Mode.

C-c C-l

Toggle electric action (c-toggle-electric-state). With a positive prefix argument, this command enables electric action, with a negative one it disables it.

Electric characters insert newlines only when, in addition to the electric state, the auto-newline feature is enabled (indicated by ‘/cla’ in the mode line after the mode name). You can turn this feature on or off with the command C-c C-a:

C-c C-a

Toggle the auto-newline feature (c-toggle-auto-newline). With a prefix argument, this command turns the auto-newline feature on if the argument is positive, and off if it is negative.

Usually the CC Mode style configures the exact circumstances in which Emacs inserts auto-newlines. You can also configure this directly. See Custom Auto-newlines in The CC Mode Manual.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.12.3 Hungry Delete Feature in C

If you want to delete an entire block of whitespace at point, you can use hungry deletion. This deletes all the contiguous whitespace either before point or after point in a single operation. Whitespace here includes tabs and newlines, but not comments or preprocessor commands.

C-c C-<DEL>
C-c <DEL>

Delete the entire block of whitespace preceding point (c-hungry-delete-backwards).

C-c C-d
C-c C-<Delete>
C-c <Delete>

Delete the entire block of whitespace after point (c-hungry-delete-forward).

As an alternative to the above commands, you can enable hungry delete mode. When this feature is enabled (indicated by ‘h’ after a ‘/’ in the mode line after the mode name), a single <DEL> deletes all preceding whitespace, not just one space, and a single C-d (but not plain <Delete>) deletes all following whitespace.

M-x c-toggle-hungry-state

Toggle the hungry-delete feature (c-toggle-hungry-state). With a prefix argument, this command turns the hungry-delete feature on if the argument is positive, and off if it is negative.

The variable c-hungry-delete-key controls whether the hungry-delete feature is enabled.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.12.4 Other Commands for C Mode

M-x c-context-line-break

This command inserts a line break and indents the new line in a manner appropriate to the context. In normal code, it does the work of <RET> (newline), in a C preprocessor line it additionally inserts a ‘\’ at the line break, and within comments it’s like M-j (c-indent-new-comment-line).

c-context-line-break isn’t bound to a key by default, but it needs a binding to be useful. The following code will bind it to <RET>. We use c-initialization-hook here to make sure the keymap is loaded before we try to change it.

(defun my-bind-clb ()
  (define-key c-mode-base-map "\C-m"
              'c-context-line-break))
(add-hook 'c-initialization-hook 'my-bind-clb)
C-M-h

Put mark at the end of a function definition, and put point at the beginning (c-mark-function).

M-q

Fill a paragraph, handling C and C++ comments (c-fill-paragraph). If any part of the current line is a comment or within a comment, this command fills the comment or the paragraph of it that point is in, preserving the comment indentation and comment delimiters.

C-c C-e

Run the C preprocessor on the text in the region, and show the result, which includes the expansion of all the macro calls (c-macro-expand). The buffer text before the region is also included in preprocessing, for the sake of macros defined there, but the output from this part isn’t shown.

When you are debugging C code that uses macros, sometimes it is hard to figure out precisely how the macros expand. With this command, you don’t have to figure it out; you can see the expansions.

C-c C-\

Insert or align ‘\’ characters at the ends of the lines of the region (c-backslash-region). This is useful after writing or editing a C macro definition.

If a line already ends in ‘\’, this command adjusts the amount of whitespace before it. Otherwise, it inserts a new ‘\’. However, the last line in the region is treated specially; no ‘\’ is inserted on that line, and any ‘\’ there is deleted.

M-x cpp-highlight-buffer

Highlight parts of the text according to its preprocessor conditionals. This command displays another buffer named ‘*CPP Edit*’, which serves as a graphic menu for selecting how to display particular kinds of conditionals and their contents. After changing various settings, click on ‘[A]pply these settings’ (or go to that buffer and type a) to rehighlight the C mode buffer accordingly.

C-c C-s

Display the syntactic information about the current source line (c-show-syntactic-information). This information directs how the line is indented.

M-x cwarn-mode
M-x global-cwarn-mode

CWarn minor mode highlights certain suspicious C and C++ constructions:

You can enable the mode for one buffer with the command M-x cwarn-mode, or for all suitable buffers with the command M-x global-cwarn-mode or by customizing the variable global-cwarn-mode. You must also enable Font Lock mode to make it work.

M-x hide-ifdef-mode

Hide-ifdef minor mode hides selected code within ‘#if’ and ‘#ifdef’ preprocessor blocks. If you change the variable hide-ifdef-shadow to t, Hide-ifdef minor mode shadows preprocessor blocks by displaying them with a less prominent face, instead of hiding them entirely. See the documentation string of hide-ifdef-mode for more information.

M-x ff-find-related-file

Find a file related in a special way to the file visited by the current buffer. Typically this will be the header file corresponding to a C/C++ source file, or vice versa. The variable ff-related-file-alist specifies how to compute related file names.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.13 Asm Mode

Asm mode is a major mode for editing files of assembler code. It defines these commands:

<TAB>

tab-to-tab-stop.

C-j

Insert a newline and then indent using tab-to-tab-stop.

:

Insert a colon and then remove the indentation from before the label preceding colon. Then do tab-to-tab-stop.

;

Insert or align a comment.

The variable asm-comment-char specifies which character starts comments in assembler syntax.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.14 Fortran Mode

Fortran mode is meant for editing fixed form (and also tab format) source code (normally Fortran 77). For editing more modern free-form source code (Fortran 90, 95, 2003, 2008), use F90 mode (f90-mode). Emacs normally uses Fortran mode for files with extension ‘.f’, ‘.F’ or ‘.for’, and F90 mode for the extensions ‘.f90’, ‘.f95’, ‘.f03’ and ‘.f08’. Customize auto-mode-alist to add more extensions. GNU Fortran supports both free and fixed form. This manual mainly documents Fortran mode, but the corresponding F90 mode features are mentioned when relevant.

Fortran mode provides special motion commands for Fortran statements and subprograms, and indentation commands that understand Fortran conventions of nesting, line numbers and continuation statements. Fortran mode has support for Auto Fill mode that breaks long lines into proper Fortran continuation lines. Fortran mode also supports Hideshow minor mode (see section Hideshow minor mode), and Imenu (see section Imenu).

Special commands for comments are provided because Fortran comments are unlike those of other languages. Built-in abbrevs optionally save typing when you insert Fortran keywords.

Use M-x fortran-mode to switch to this major mode. This command runs the hook fortran-mode-hook. @xref{Hooks}.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.14.1 Motion Commands

In addition to the normal commands for moving by and operating on defuns (Fortran subprograms—functions and subroutines, as well as modules for F90 mode, using the commands fortran-end-of-subprogram and fortran-beginning-of-subprogram), Fortran mode provides special commands to move by statements and other program units.

C-c C-n

Move to the beginning of the next statement (fortran-next-statement/f90-next-statement).

C-c C-p

Move to the beginning of the previous statement (fortran-previous-statement/f90-previous-statement). If there is no previous statement (i.e., if called from the first statement in the buffer), move to the start of the buffer.

C-c C-e

Move point forward to the start of the next code block, or the end of the current one, whichever comes first (f90-next-block). A code block is a subroutine, ifendif statement, and so forth. This command exists for F90 mode only, not Fortran mode. With a numeric argument, it moves forward that many blocks.

C-c C-a

Move point backward to the previous block (f90-previous-block). This is like f90-next-block, but moves backwards.

C-M-n

Move to the end of the current code block (fortran-end-of-block/f90-end-of-block). With a numeric argument, move forward that number of blocks. The mark is set before moving point. The F90 mode version of this command checks for consistency of block types and labels (if present), but it does not check the outermost block since that may be incomplete.

C-M-p

Move to the start of the current code block (fortran-beginning-of-block/f90-beginning-of-block). This is like fortran-end-of-block, but moves backwards.

The commands fortran-beginning-of-subprogram and fortran-end-of-subprogram move to the start or end of the current subprogram, respectively. The commands fortran-mark-do and fortran-mark-if mark the end of the current do or if block, and move point to the start.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.14.2 Fortran Indentation

Special commands and features are needed for indenting fixed (or tab) form Fortran code in order to make sure various syntactic entities (line numbers, comment line indicators and continuation line flags) appear in the required columns.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.14.2.1 Fortran Indentation and Filling Commands

C-M-j

Break the current line at point and set up a continuation line (fortran-split-line).

M-^

Join this line to the previous line (fortran-join-line).

C-M-q

Indent all the lines of the subprogram that point is in (fortran-indent-subprogram).

M-q

Fill a comment block or statement (using fortran-fill-paragraph or fortran-fill-statement).

The key C-M-q runs fortran-indent-subprogram, a command to reindent all the lines of the Fortran subprogram (function or subroutine) containing point.

The key C-M-j runs fortran-split-line, which splits a line in the appropriate fashion for Fortran. In a non-comment line, the second half becomes a continuation line and is indented accordingly. In a comment line, both halves become separate comment lines.

M-^ or C-c C-d run the command fortran-join-line, which joins a continuation line back to the previous line, roughly as the inverse of fortran-split-line. The point must be on a continuation line when this command is invoked.

M-q in Fortran mode fills the comment block or statement that point is in. This removes any excess statement continuations.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.14.2.2 Continuation Lines

Most Fortran 77 compilers allow two ways of writing continuation lines. If the first non-space character on a line is in column 5, then that line is a continuation of the previous line. We call this fixed form. (In GNU Emacs we always count columns from 0; but note that the Fortran standard counts from 1. You can customize the variable column-number-indicator-zero-based to make the column display Fortran-like; @pxref{Optional Mode Line}.) The variable fortran-continuation-string specifies what character to put in column 5. A line that starts with a tab character followed by any digit except ‘0’ is also a continuation line. We call this style of continuation tab format. (Fortran 90 introduced free-form continuation lines.)

Fortran mode can use either style of continuation line. When you enter Fortran mode, it tries to deduce the proper continuation style automatically from the buffer contents. It does this by scanning up to fortran-analyze-depth (default 100) lines from the start of the buffer. The first line that begins with either a tab character or six spaces determines the choice. If the scan fails (for example, if the buffer is new and therefore empty), the value of fortran-tab-mode-default (nil for fixed form, and non-nil for tab format) is used. ‘/t’ (fortran-tab-mode-string) in the mode line indicates tab format is selected. Fortran mode sets the value of indent-tabs-mode accordingly.

If the text on a line starts with the Fortran continuation marker ‘$’, or if it begins with any non-whitespace character in column 5, Fortran mode treats it as a continuation line. When you indent a continuation line with <TAB>, it converts the line to the current continuation style. When you split a Fortran statement with C-M-j, the continuation marker on the newline is created according to the continuation style.

The setting of continuation style affects several other aspects of editing in Fortran mode. In fixed form mode, the minimum column number for the body of a statement is 6. Lines inside of Fortran blocks that are indented to larger column numbers must use only the space character for whitespace. In tab format mode, the minimum column number for the statement body is 8, and the whitespace before column 8 must consist of one tab character.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.14.2.3 Line Numbers

If a number is the first non-whitespace in the line, Fortran indentation assumes it is a line number and moves it to columns 0 through 4. (Columns always count from 0 in Emacs, but setting column-number-indicator-zero-based to nil can change that, @pxref{Optional Mode Line}.)

Line numbers of four digits or less are normally indented one space. The variable fortran-line-number-indent controls this; it specifies the maximum indentation a line number can have. The default value of the variable is 1. Fortran mode tries to prevent line number digits passing column 4, reducing the indentation below the specified maximum if necessary. If fortran-line-number-indent has the value 5, line numbers are right-justified to end in column 4.

Simply inserting a line number is enough to indent it according to these rules. As each digit is inserted, the indentation is recomputed. To turn off this feature, set the variable fortran-electric-line-number to nil.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.14.2.4 Syntactic Conventions

Fortran mode assumes that you follow certain conventions that simplify the task of understanding a Fortran program well enough to indent it properly:

If you fail to follow these conventions, the indentation commands may indent some lines unaesthetically. However, a correct Fortran program retains its meaning when reindented even if the conventions are not followed.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.14.2.5 Variables for Fortran Indentation

Several additional variables control how Fortran indentation works:

fortran-do-indent

Extra indentation within each level of ‘do’ statement (default 3).

fortran-if-indent

Extra indentation within each level of ‘if’, ‘select case’, or ‘where’ statements (default 3).

fortran-structure-indent

Extra indentation within each level of ‘structure’, ‘union’, ‘map’, or ‘interface’ statements (default 3).

fortran-continuation-indent

Extra indentation for bodies of continuation lines (default 5).

fortran-check-all-num-for-matching-do

In Fortran 77, a numbered ‘do’ statement is terminated by any statement with a matching line number. It is common (but not compulsory) to use a ‘continue’ statement for this purpose. If this variable has a non-nil value, indenting any numbered statement must check for a ‘do’ that ends there. If you always end ‘do’ statements with a ‘continue’ line (or if you use the more modern ‘enddo’), then you can speed up indentation by setting this variable to nil (the default).

fortran-blink-matching-if

If this is t, indenting an ‘endif’ (or ‘enddo’) statement moves the cursor momentarily to the matching ‘if’ (or ‘do’) statement to show where it is. The default is nil.

fortran-minimum-statement-indent-fixed

Minimum indentation for Fortran statements when using fixed form continuation line style. Statement bodies are never indented by less than this. The default is 6.

fortran-minimum-statement-indent-tab

Minimum indentation for Fortran statements for tab format continuation line style. Statement bodies are never indented by less than this. The default is 8.

The following section describes the variables controlling the indentation of comments.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.14.3 Fortran Comments

The usual Emacs comment commands assume that a comment can follow a line of code. In Fortran 77, the standard comment syntax requires an entire line to be just a comment. Therefore, Fortran mode replaces the standard Emacs comment commands and defines some new variables.

Fortran mode can also handle the Fortran 90 comment syntax where comments start with ‘!’ and can follow other text. Because only some Fortran 77 compilers accept this syntax, Fortran mode will not insert such comments unless you have said in advance to do so. To do this, set the variable fortran-comment-line-start to ‘"!"’. If you use an unusual value, you may need to change fortran-comment-line-start-skip.

M-;

Align comment or insert new comment (comment-dwim).

C-x ;

Applies to nonstandard ‘!’ comments only (comment-set-column).

C-c ;

Turn all lines of the region into comments, or (with argument) turn them back into real code (fortran-comment-region).

M-; in Fortran mode runs the standard comment-dwim. This recognizes any kind of existing comment and aligns its text appropriately; if there is no existing comment, a comment is inserted and aligned. Inserting and aligning comments are not the same in Fortran mode as in other modes.

When a new comment must be inserted, if the current line is blank, a full-line comment is inserted. On a non-blank line, a nonstandard ‘!’ comment is inserted if you have said you want to use them. Otherwise, a full-line comment is inserted on a new line before the current line.

Nonstandard ‘!’ comments are aligned like comments in other languages, but full-line comments are different. In a standard full-line comment, the comment delimiter itself must always appear in column zero. What can be aligned is the text within the comment. You can choose from three styles of alignment by setting the variable fortran-comment-indent-style to one of these values:

fixed

Align the text at a fixed column, which is the sum of fortran-comment-line-extra-indent and the minimum statement indentation. This is the default.

The minimum indentation is fortran-minimum-statement-indent-tab for tab format continuation line style and fortran-minimum-statement-indent-fixed for fixed form style.

relative

Align the text as if it were a line of code, but with an additional fortran-comment-line-extra-indent columns of indentation.

nil

Don’t move text in full-line comments automatically.

In addition, you can specify the character to be used to indent within full-line comments by setting the variable fortran-comment-indent-char to the single-character string you want to use.

Compiler directive lines, or preprocessor lines, have much the same appearance as comment lines. It is important, though, that such lines never be indented at all, no matter what the value of fortran-comment-indent-style. The variable fortran-directive-re is a regular expression that specifies which lines are directives. Matching lines are never indented, and receive distinctive font-locking.

The normal Emacs comment command C-x ; (comment-set-column) has not been redefined. If you use ‘!’ comments, this command can be used with them. Otherwise, it is useless in Fortran mode.

The command C-c ; (fortran-comment-region) turns all the lines of the region into comments by inserting the string ‘c$$$’ at the front of each one. With a numeric argument, it turns the region back into live code by deleting ‘c$$$’ from the front of each line in it. The string used for these comments can be controlled by setting the variable fortran-comment-region. Note that here we have an example of a command and a variable with the same name; these two uses of the name never conflict because in Lisp and in Emacs it is always clear from the context which one is meant.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.14.4 Auto Fill in Fortran Mode

Fortran mode has specialized support for Auto Fill mode, which is a minor mode that automatically splits statements as you insert them when they become too wide. Splitting a statement involves making continuation lines using fortran-continuation-string (see section Continuation Lines). This splitting happens when you type <SPC>, <RET>, or <TAB>, and also in the Fortran indentation commands. You activate Auto Fill in Fortran mode in the normal way. @xref{Auto Fill}.

Auto Fill breaks lines at spaces or delimiters when the lines get longer than the desired width (the value of fill-column). The delimiters (besides whitespace) that Auto Fill can break at are ‘+’, ‘-’, ‘/’, ‘*’, ‘=’, ‘<’, ‘>’, and ‘,’. The line break comes after the delimiter if the variable fortran-break-before-delimiters is nil. Otherwise (and by default), the break comes before the delimiter.

To enable Auto Fill in all Fortran buffers, add auto-fill-mode to fortran-mode-hook. @xref{Hooks}.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.14.5 Checking Columns in Fortran

In standard Fortran 77, anything beyond column 72 is ignored. Most compilers provide an option to change this (for example, ‘-ffixed-line-length-N’ in gfortran). Customize the variable fortran-line-length to change the line length in Fortran mode. Anything beyond this point is font-locked as a comment. (Unless it is inside a string: strings that extend beyond fortran-line-length will confuse font-lock.)

C-c C-r

Display a column ruler momentarily above the current line (fortran-column-ruler).

C-c C-w

Split the current window horizontally temporarily so that it is fortran-line-length columns wide (fortran-window-create-momentarily). This may help you avoid making lines longer than the limit imposed by your Fortran compiler.

C-u C-c C-w

Split the current window horizontally so that it is fortran-line-length columns wide (fortran-window-create). You can then continue editing.

M-x fortran-strip-sequence-nos

Delete all text in column fortran-line-length and beyond.

The command C-c C-r (fortran-column-ruler) shows a column ruler momentarily above the current line. The comment ruler is two lines of text that show you the locations of columns with special significance in Fortran programs. Square brackets show the limits of the columns for line numbers, and curly brackets show the limits of the columns for the statement body. Column numbers appear above them.

Note that the column numbers count from zero, as always in GNU Emacs (but customizing column-number-indicator-zero-based can change column display to match that of Fortran; @pxref{Optional Mode Line}.) As a result, the numbers may be one less than those you are familiar with; but the positions they indicate in the line are standard for Fortran.

The text used to display the column ruler depends on the value of the variable indent-tabs-mode. If indent-tabs-mode is nil, then the value of the variable fortran-column-ruler-fixed is used as the column ruler. Otherwise, the value of the variable fortran-column-ruler-tab is displayed. By changing these variables, you can change the column ruler display.

C-c C-w (fortran-window-create-momentarily) temporarily splits the current window horizontally, making a window fortran-line-length columns wide, so you can see any lines that are too long. Type a space to restore the normal width.

You can also split the window horizontally and continue editing with the split in place. To do this, use C-u C-c C-w (M-x fortran-window-create). By editing in this window you can immediately see when you make a line too wide to be correct Fortran.

The command M-x fortran-strip-sequence-nos deletes all text in column fortran-line-length and beyond, on all lines in the current buffer. This is the easiest way to get rid of old sequence numbers.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.14.6 Fortran Keyword Abbrevs

Fortran mode provides many built-in abbrevs for common keywords and declarations. These are the same sort of abbrev that you can define yourself. To use them, you must turn on Abbrev mode. @xref{Abbrevs}.

The built-in abbrevs are unusual in one way: they all start with a semicolon. For example, one built-in Fortran abbrev is ‘;c’ for ‘continue’. If you insert ‘;c’ and then insert a punctuation character such as a space or a newline, the ‘;c’ expands automatically to ‘continue’, provided Abbrev mode is enabled.

Type ‘;?’ or ‘;C-h’ to display a list of all the built-in Fortran abbrevs and what they stand for.


[Top] [Contents] [Index] [ ? ]

Footnotes

(1)

The word “sexp” is used to refer to an expression in Lisp.

(2)

Regular expressions and syntax tables.


[Top] [Contents] [Index] [ ? ]

About This Document

This document was generated on May 29, 2018 using texi2html.

The buttons in the navigation panels have the following meaning:

Button Name Go to From 1.2.3 go to
[ << ] FastBack Beginning of this chapter or previous chapter 1
[ < ] Back Previous section in reading order 1.2.2
[ Up ] Up Up section 1.2
[ > ] Forward Next section in reading order 1.2.4
[ >> ] FastForward Next chapter 2
[Top] Top Cover (top) of document  
[Contents] Contents Table of contents  
[Index] Index Index  
[ ? ] About About (help)  

where the Example assumes that the current position is at Subsubsection One-Two-Three of a document of the following structure:


This document was generated on May 29, 2018 using texi2html.