Editing Programs
****************

Emacs provides many features to facilitate editing programs.  Some of these features can

  * Find or move over top-level definitions (see Defuns).

  * Apply the usual indentation conventions of the language (see Program Indent).

  * Balance parentheses (see Parentheses).

  * Insert, kill or align comments (see Comments).

  * Highlight program syntax (see Font Lock).

This chapter describes these features and many more.

Major Modes for Programming Languages
=====================================

Emacs has specialized major modes for various programming languages. See Major Modes.  A programming language major 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 of a function definition.  It often customizes or provides facilities for compiling and debugging programs as well.

Ideally, Emacs should provide a major mode for each programming language that you might want to edit; if it doesn't have a mode for your favorite language, you can contribute one.  But often the mode for one language can serve for other syntactically similar languages. The major mode for language L is called L-mode, and you can select it by typing M-x L-mode RET. See Choosing Modes.

The existing programming language major modes include Lisp, Scheme (a variant of Lisp) and the Scheme-based DSSSL expression language, Ada, ASM, AWK, C, C++, Delphi (Object Pascal), Fortran (free format and fixed format), Icon, IDL (CORBA), IDLWAVE, Java, Metafont (TeX's companion for font creation), Modula2, Objective-C, Octave, Pascal, Perl, Pike, PostScript, Prolog, Python, Simula, Tcl, and VHDL.  An alternative mode for Perl is called CPerl mode.  Modes are available for the scripting languages of the common GNU and Unix shells, VMS DCL, and MS-DOS/MS-Windows BAT files.  There are also major modes for editing makefiles, DNS master files, and various sorts of configuration files.

In most programming languages, indentation should vary from line to line to illustrate the structure of the program.  So the major modes for programming languages arrange for TAB to update the indentation of the current line.  They also rebind DEL to treat a tab as if it were the equivalent number of spaces; this lets you delete one column of indentation without worrying whether the whitespace consists of spaces or tabs.  Use C-b C-d to delete a tab character before point, in these modes.

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

Turning on a major mode runs a normal hook called the mode hook, which is the value of a Lisp variable.  Each major mode has a mode hook, and the hook's name is always made from the mode command's name by adding -hook.  For example, turning on C mode runs the hook c-mode-hook, while turning on Lisp mode runs the hook lisp-mode-hook.  The purpose of the mode hook is to give you a place to set up customizations for that major mode.  See Hooks.

Top-Level Definitions, or Defuns
================================

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

In many programming language modes, Emacs assumes that a defun is any pair of parentheses (or braces, if the language uses braces this way) that starts at the left margin.  For example, in C, the body of a function definition is a defun, usually recognized as an open-brace that begins at the left margin (Alternatively, you can set up C Mode to recognize a defun at an opening brace at the outermost level.  See Movement Commands (the CC Mode Manual).).  A variable's initializer can also count as a defun, if the open-brace that begins the initializer is at the left margin.

However, some language modes provide their own code for recognizing defuns in a way that suits the language syntax and conventions better.

Left Margin Convention
----------------------

Emacs assumes by default that any opening delimiter found at the left margin is the start of a top-level definition, or defun.  You can override this default by setting this user option:

 -- 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's nil, defuns are found by searching for parens or braces at the outermost level.  Some major modes, including C and related modes, set open-paren-in-column-0-is-defun-start buffer-locally to nil

In modes where open-paren-in-column-0-is-defun-start is t, don't put an opening delimiter at the left margin unless it is a defun start.  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.

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 Program Indent) and Font Lock mode (see 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 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.

In the earliest days, the original Emacs found defuns by moving upward a level of parentheses or braces until there were no more levels to go up.  This always required scanning all the way back to the beginning of the buffer, even for a small function.  To speed up the operation, we changed Emacs to assume that any opening delimiter at the left margin is the start of a defun.  This heuristic is nearly always right, and avoids the need to scan back to the beginning of the buffer.  However, now that modern computers are so powerful, this scanning is rarely slow enough to annoy, so we've provided a way to disable the heuristic.

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 puts point at the beginning and mark at the end of the current defun.  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 you use the command while point is between defuns, it uses the following defun. Successive uses of C-M-h, or using it in Transient Mark mode when the mark is active, includes an additional defun in the region each time.

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.

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. (see Tags, 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 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 Which Function). The Speedbar can also use it (see Speedbar).

Which Function Mode
-------------------

Which Function mode is a minor mode that displays the current function name in the mode line, updating it as you move around in a buffer.

To enable (or disable) Which Function mode, use the command M-x which-function-mode.  This command is global; it applies to all buffers, both existing ones and those yet to be created.  However, it takes effect only in certain major modes, those listed in the value of which-func-modes.  If the value is t, then Which Function mode applies to all major modes that know how to support it---in other words, all the major modes that support Imenu.

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 properly either a single line, a specified number of lines, or all of the lines inside a single parenthetical grouping.

Emacs also provides a Lisp pretty-printer in the library pp. This program reformats a Lisp object with indentation chosen to look nice.

Basic Program Indentation Commands
----------------------------------

The basic indentation commands indent a single line according to the usual conventions of the language you are editing.

TAB
     Adjust indentation of current line.

C-j
     Equivalent to RET followed by TAB (newline-and-indent).

LINEFEED
     This key, if the keyboard has it, is another way to enter C-j.

The basic indentation command is TAB, which gives the current line the correct indentation as determined from the previous lines.  The function that TAB runs depends on the major mode; it is lisp-indent-line in Lisp mode, c-indent-command in C mode, etc.  These functions understand the syntax and conventions of different languages, but they all do conceptually the same job: TAB in any programming-language major mode inserts or deletes whitespace at the beginning of the current line, independent of where point is in the line.  If point was inside the whitespace at the beginning of the line, TAB puts it at the end of that whitespace; otherwise, TAB keeps point fixed with respect to the characters around it.

Use C-q TAB to insert a tab character at point.

When entering lines of new code, use C-j (newline-and-indent), which is equivalent to a RET followed by a TAB.  C-j at the end of a line creates a blank line and then gives it the appropriate indentation.

TAB indents a line that starts within a parenthetical grouping under the preceding line within the grouping, or the text after the parenthesis.  Therefore, if you manually give one of these lines a nonstandard indentation, the lines below will tend to follow it.  This behavior is convenient in cases where you have overridden the standard result of TAB because you find it unaesthetic for a particular line.

By default, an open-parenthesis, open-brace or other opening delimiter at the left margin is assumed by Emacs (including the indentation routines) to be the start of a function.  This speeds up indentation commands.  If you will be editing text which contains opening delimiters in column zero that aren't the beginning of a functions, even inside strings or comments, you must set open-paren-in-column-0-is-defun-start.  See Left Margin Paren, for more information on this.

Normally, lines are indented with tabs and spaces.  If you want Emacs to use spaces only, see Just Spaces.

Indenting Several Lines
-----------------------

When you wish to reindent several lines of code which have been altered or moved to a different level in the parenthesis structure, you have several commands available.

C-M-q
     Reindent all the lines within one parenthetical grouping (indent-pp-sexp).

C-M-\
     Reindent all lines in the region (indent-region).

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.

You can reindent the contents of a single parenthetical grouping by positioning point before the beginning of it and typing C-M-q (indent-pp-sexp in Lisp mode, c-indent-exp in C mode; also bound to other suitable commands in other modes).  The indentation of the line where the grouping starts is not changed; therefore this changes only the relative indentation within the grouping, not its overall indentation.  To correct that as well, type TAB first.

Another way to specify the range to be reindented is with the region.  The command C-M-\ (indent-region) applies TAB to every line whose first character is between point and mark.

If you like the relative indentation within a grouping, but not the indentation of its first line, you can type C-u TAB to reindent the whole grouping as a rigid unit.  (This works in Lisp modes and C and related 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.

You can also perform this operation on the region, using the command M-x indent-code-rigidly.  It rigidly shifts all the lines in the region sideways, like indent-rigidly does (see 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.

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.  Normally you would use this for macro definitions and specify it using the declare construct (see Defining Macros (the Emacs Lisp Reference Manual)).

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.

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 . RET 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 (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 (the CC Mode Manual), and Customizing Indentation (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.

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).  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.

Expressions with Balanced Parentheses
-------------------------------------

These commands deal with balanced expressions, also called sexps (The word ``sexp'' is used to refer to an expression in Lisp.).

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).

Each programming language major mode customizes the definition of balanced expressions to suit that language.  Balanced expressions typically include symbols, numbers, and string constants, as well as any pair of matching delimiters and their contents.  Some languages have obscure forms of expression syntax that nobody has bothered to implement in Emacs.

By convention, the keys for these commands are all Control-Meta characters.  They usually act on expressions just as the corresponding Meta characters act on words.  For instance, the command C-M-b moves backward over a balanced expression, just as M-b moves back over a word.

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

The command C-M-b (backward-sexp) moves backward over a balanced expression.  The detailed rules are like those above for C-M-f, but with directions reversed.  If there are prefix characters (single-quote, backquote and comma, in Lisp) preceding the expression, C-M-b moves back over them as well.  The balanced expression commands move across comments as if they were whitespace, in most modes.

C-M-f or C-M-b with an argument repeats that operation the specified number of times; with a negative argument, it moves in the opposite direction.

Killing a whole balanced expression can be done with C-M-k (kill-sexp).  C-M-k kills the characters that C-M-f would move over.

A somewhat random-sounding command which is nevertheless handy is C-M-t (transpose-sexps), which drags the previous balanced expression across the next one.  An argument serves as a repeat count, moving the previous expression over that many following ones.  A negative argument drags the previous balanced expression backwards across those before it (thus canceling out the effect of C-M-t with a positive argument).  An argument of zero, rather than doing nothing, transposes the balanced expressions ending at or after point and the mark.

To set the region around the next balanced expression in the buffer, use C-M-@ (mark-sexp), which sets mark at the same place that C-M-f would move to.  C-M-@ takes arguments like C-M-f.  In particular, a negative argument is useful for putting the mark at the beginning of the previous balanced expression.  The alias C-M-SPC is equivalent to C-M-@.  When you repeat this command, or use it in Transient Mark mode when the mark is active, it extends the region by one sexp each time.

In languages that use infix operators, such as C, it is not possible to recognize all balanced expressions as such 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.  Both foo + bar and foo are legitimate choices for ``the expression following point'' when point is at the f, so the expression commands must perforce choose one or the other to operate on.  Note that (foo + bar) is recognized as a single expression in C mode, because of the parentheses.

Moving in the Parenthesis Structure
-----------------------------------

The Emacs commands for handling parenthetical groupings see nothing except parentheses (or whatever characters must balance in the language you are working with), and the escape characters that might be used to quote those.  They are mainly intended for editing programs, but can be useful for editing any text that has parentheses. They are sometimes called ``list'' commands because in Lisp these groupings are lists.

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 over one (or N) parenthetical groupings, skipping blithely over any amount of text that doesn't include meaningful parentheses (symbols, strings, etc.).

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.

Automatic Display Of Matching Parentheses
-----------------------------------------

The Emacs parenthesis-matching feature is designed to show automatically how parentheses (and other matching delimiters) match in the text.  Whenever you type a self-inserting character that is a closing delimiter, the cursor moves momentarily to 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 parenthesis match display:

blink-matching-paren turns the feature on or off: nil disables it, but the default is t to enable match display.

blink-matching-delay says how many seconds to leave the cursor on the matching opening delimiter, before bringing it back to the real location of point; the default is 1, but on some systems it is useful to specify a fraction of a second.

blink-matching-paren-distance specifies how many characters back to search to find the matching opening delimiter.  If the match is not found in that distance, scanning stops, and nothing is displayed. This is to prevent the scan for the matching delimiter from wasting lots of time when there is no match.  The default is 25600.

Show Paren mode provides a more powerful kind of automatic matching. Whenever point is after a closing delimiter, that delimiter and its matching opening delimiter are both highlighted; otherwise, if point is before an opening delimiter, the matching closing delimiter is highlighted.  (There is no need to highlight the opening delimiter in that case, because the cursor appears on top of that character.)  Use the command M-x show-paren-mode to enable or disable this mode.

By default, show-paren-mode uses colors to highlight the parentheses.  However, if your display doesn't support colors, you can customize the faces show-paren-match-face and show-paren-mismatch-face to use other attributes, such as bold or underline.  See Face Customization.

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 (see Spelling).

Comment Commands
----------------

The comment commands in this table insert, kill and align comments. They are described in this section and following sections.

M-;
     Insert or realign comment on current line; alternatively, comment or uncomment the region (comment-dwim).

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 Multi-Line Comments.

M-x comment-region
C-c C-c (in C-like modes)
     Add or remove comment delimiters on 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.

If there is no comment already on the line, M-; inserts a new comment, aligned at a specific column called the comment column. The new comment begins with the string Emacs thinks comments should start with (the value of comment-start; see below).  Point is after that string, so you can insert the text of the comment right away.  If the major mode has specified a string to terminate comments, M-; inserts that too, to keep the syntax valid.

If the text of the line extends past the comment column, then the comment start string is indented to a suitable boundary (usually, at least one space is inserted).

You can also use M-; to align an existing comment.  If a line already contains the comment-start string, M-; reindents it to the conventional alignment and moves point after it.  (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 text inside the comment.

C-u M-; kills any comment on the current line, along with the whitespace before it.  To reinsert the comment on another line, move to the end of that line, do C-y, and then do M-; to realign it.

Note that C-u M-; is not a distinct key; it is M-; (comment-dwim) with a prefix argument.  That command is programmed so that when it receives a prefix argument it calls comment-kill.  However, comment-kill is a valid command in its own right, and you can bind it directly to a key if you wish.

M-; does two other jobs when used with an active region in Transient Mark mode (see Transient Mark).  Then it either adds or removes comment delimiters on each line of the region.  (If every line is a comment, it removes comment delimiters from each; otherwise, it adds comment delimiters to each.)  If you are not using Transient Mark mode, then you should use the commands comment-region and uncomment-region to do these jobs (see Multi-Line Comments). A prefix argument used in these circumstances specifies how many comment delimiters to add or how many to delete.

Some major modes have special rules for indenting certain kinds of comments in certain contexts.  For example, in Lisp code, comments which start with two semicolons are indented as if they were lines of code, instead of at the comment column.  Comments which start with three semicolons are supposed to start at the left margin.  Emacs understands these conventions by indenting a double-semicolon comment using TAB, and by not changing the indentation of a triple-semicolon comment at all.

     ;; 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.

For C-like buffers, you can configure the exact effect of M-; more flexibly than for most buffers by setting the user options 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 (The CC Mode Manual).

Multiple Lines of Comments
--------------------------

If you are typing a comment and wish to continue it on another line, you can use the command C-M-j or M-j (comment-indent-new-line).  If comment-multi-line (see Options for Comments) is non-nil, it moves to a new line within the comment.  Otherwise it closes the comment and starts a new comment on a new line.  When Auto Fill mode is on, going past the fill column while typing a comment causes the comment to be continued in just this fashion.

To turn existing lines into comment lines, use the M-x comment-region command (or type C-c C-c in C-like buffers).  It adds comment delimiters to the lines that start in the region, thus commenting them out.  With a negative argument, it does the opposite---it deletes comment delimiters from the lines in the region.

With a positive argument, comment-region duplicates the last character of the comment start sequence it adds; the argument specifies how many copies of the character to insert.  Thus, in Lisp mode, C-u 2 M-x comment-region adds ;; to each line.  Duplicating the comment delimiter is a way of calling attention to the comment.  It can also affect how the comment is indented.  In Lisp, for proper indentation, you should use an argument of two or three, if between defuns; if within a defun, it must be three.

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 (The CC Mode Manual).

Options Controlling Comments
----------------------------

The comment column, the column at which Emacs tries to place comments, is stored in the variable comment-column.  You can set it to a number explicitly.  Alternatively, the command C-x ; (comment-set-column) sets the comment column to the column point is at.  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 variable comment-column is per-buffer: setting the variable in the normal fashion affects only the current buffer, but there is a default value which you can change with setq-default. See Locals.  Many major modes initialize this variable for the current buffer.

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 "/\\*+ *\\|//+ *", 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.  See Regexp Backslash.)

When a comment command makes a new comment, it inserts the value of comment-start to begin it.  The value of comment-end is inserted after point, so that it will follow the text that you will insert into the comment.  When comment-end is non-empty, it should start with a space.  For example, in C mode, comment-start has the value "/* " and comment-end has the value " */".

The variable comment-padding specifies how many spaces comment-region should insert on each line between the comment delimiter and the line's original text.  The default is 1, to insert one space.  nil means 0.  Alternatively, comment-padding can hold the actual string to insert.

The variable comment-multi-line controls how C-M-j (indent-new-comment-line) behaves when used inside a comment. Specifically, when comment-multi-line is nil, the command inserts a comment terminator, begins a new line, and finally inserts a comment starter.  Otherwise it does not insert the terminator and starter, so it effectively continues the current comment across multiple lines.  In languages that allow multi-line comments, the choice of value for this variable is a matter of taste. The default for this variable depends on the major mode.

The variable comment-indent-function should contain a function that will be called to compute the indentation 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, in Lisp mode, the indent hook function bases its decision on how many semicolons begin an existing comment, and on the code in the preceding lines.

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.

Info Documentation Lookup
-------------------------

For C, Lisp, and other languages that 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 major mode determines where to look for documentation for the symbol---which Info files to look in, and which indices to search. You can also use M-x info-lookup-file to look for documentation for a file name.

This feature currently supports the modes AWK, Autoconf, Bison, C, Emacs Lisp, LaTeX, M4, Makefile, Octave, Perl, Scheme, and Texinfo, provided you have installed the relevant Info files, which are typically available with the appropriate GNU package.

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 hope to replace man pages with better-organized manuals that you can browse with Info (see 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.  It runs the man program to format the man page; if the system permits, it runs man asynchronously, so that you can keep on editing while the page is being formatted.  (On MS-DOS and MS-Windows 3, you cannot edit while Emacs waits for man to finish.)  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 page 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 multiple 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 manual-entry prompts for the topic.  For example, to read the man page for the C library function chmod (as opposed to a command of the same name), type M-x manual-entry RET chmod(2) RET (chmod is a system call, so it is in section 2).

If you do not specify a section, the results depend on how the man program works on your system.  Some of them display only the first man page they find.  Others display all man pages that have the specified name, so you can move between them with the M-n and M-p keys (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.  If you want this behavior, you can add this option to the value of the variable Man-switches.). The mode line shows how many manual pages are present in the Man buffer.

By default, Emacs highlights the text in man pages.  For a long man page, highlighting can take substantial time.  You can turn off highlighting of man pages by setting the variable Man-fontify-manpage-flag to nil.

If you insert the text of a man page into an Emacs buffer in some other fashion, you can use the command M-x Man-fontify-manpage to perform the same conversions that M-x manual-entry does.

An alternative way of reading manual pages is the M-x woman command (The name of the command, woman, is an acronym for ``w/o (without) man,'' since it doesn't use the man program.).  Unlike M-x man, it does not run any external programs to format and display the man pages; instead it does the job in Emacs Lisp, so it works on systems such as MS-Windows, where the man program (and other programs it uses) are not generally available.

M-x woman prompts for a name of a manual page, and provides completion based on the list of manual pages that are installed on your machine; the list of available manual pages is computed automatically the first time you invoke woman.  The word at point in the current buffer is used to suggest the default for the name the manual page.

With a numeric argument, M-x woman recomputes the list of the manual pages used for completion.  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.

By default, M-x woman looks for manual pages in the directories specified in the MANPATH environment variable.  (If MANPATH is not set, woman uses a suitable default value, which can be customized.)  More precisely, woman looks for subdirectories that match the shell wildcard pattern man* in each one of these directories, and tries to find the manual pages in those subdirectories.  When first invoked, M-x woman converts the value of MANPATH to a list of directory names and stores that list in the woman-manpath variable.  Changing the value of this variable is another way to control the list of directories used.

You can also augment the list of directories searched by woman by setting the value of the woman-path variable. This variable should hold a list of specific directories which woman should search, in addition to those in woman-manpath.  Unlike woman-manpath, the directories in woman-path are searched for the manual pages, not for man* subdirectories.

Occasionally, you might need to display manual pages that are not in any of the directories listed by woman-manpath and woman-path.  The M-x woman-find-file command prompts for a name of a manual page file, with completion, and then formats and displays that file like M-x woman does.

The first time you invoke M-x woman, it defines the Dired W key to run the woman-find-file command on the current line's file.  You can disable this by setting the variable woman-dired-keys to nil.  See Dired.  In addition, the Tar-mode w key is define to invoke woman-find-file on the current line's archive member.

For more information about setting up and using M-x woman, see Browse UN*X Manual Pages WithOut Man (The WoMan Manual).

Emacs Lisp Documentation Lookup
-------------------------------

As you edit Lisp code to be run in Emacs, you can use the commands C-h f (describe-function) and C-h v (describe-variable) to view documentation of functions and variables that you want to use.  These commands use the minibuffer to read the name of a function or variable to document, and display the documentation in a window.  Their default arguments are based on the code in the neighborhood of point.  For C-h f, the default is the function called in the innermost list containing point.  C-h v uses the symbol name around or adjacent to point as its default.

A more automatic but less powerful method is Eldoc mode.  This minor mode constantly displays in the echo area the argument list for the function being called at point.  (In other words, it finds the function call that point is contained in, and displays the argument list of that function.)  If point is over a documented variable, it shows the first line of the variable's docstring.  Eldoc mode applies in Emacs Lisp and Lisp Interaction modes, and perhaps a few others that provide special support for looking up doc strings.  Use the command M-x eldoc-mode to enable or disable this feature.

Hideshow minor mode
===================

Hideshow minor mode provides selective display of portions of a program, known as blocks.  You can use M-x hs-minor-mode to enable or disable this mode, or add hs-minor-mode to the mode hook for certain major modes in order to enable it automatically for those modes.

Just what constitutes a block depends on the major mode.  In C mode or C++ mode, they are delimited by braces, while in Lisp mode and similar modes they are delimited by parentheses.  Multi-line comments also count as blocks.

C-c @ C-h
     Hide the current block (hs-hide-block).

C-c @ C-s
     Show the current block (hs-show-block).

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

S-Mouse-2
     Either hide or show the block you click on (hs-mouse-toggle-hiding).

C-c @ C-M-h
     Hide all top-level blocks (hs-hide-all).

C-c @ C-M-s
     Show everything in the buffer (hs-show-all).

C-c @ C-l
     Hide all blocks N levels below this block (hs-hide-level).

These variables exist for customizing Hideshow mode.

hs-hide-comments-when-hiding-all
     Non-nil says that hs-hide-all should hide comments too.

hs-isearch-open
     Specifies what kind of hidden blocks to open in Isearch mode. The value should be one of these four symbols:

     code
          Open only code blocks.

     comment
          Open only comments.

     t
          Open both code blocks and comments.

     nil
          Open neither code blocks nor comments.

hs-special-modes-alist
     A list of elements, each specifying how to initialize Hideshow variables for one major mode.  See the variable's documentation string for more information.

Completion for Symbol Names
===========================

In Emacs, completion is something you normally do in the minibuffer. But one kind of completion is available in all buffers: completion for symbol names.

The character M-TAB runs a command to complete the partial symbol before point against the set of meaningful symbol names.  This command inserts at point any additional characters that it can determine from the partial name.  (If your window manager defines M-TAB to switch windows, you can type ESC TAB or C-M-i.)

If the partial name in the buffer has multiple possible completions that differ in the very next character, so that it is impossible to complete even one more character, M-TAB displays a list of all possible completions in another window.

In most programming language major modes, M-TAB runs the command complete-symbol, which provides two kinds of completion. Normally it does completion based on a tags table (see Tags); with a numeric argument (regardless of the value), it does completion based on the names listed in the Info file indexes for your language.  Thus, to complete the name of a symbol defined in your own program, use M-TAB with no argument; to complete the name of a standard library function, use C-u M-TAB.  Of course, Info-based completion works only if there is an Info file for the standard library functions of your language, and only if it is installed at your site.

In Emacs-Lisp mode, the name space for completion normally consists of nontrivial symbols present in Emacs---those that have function definitions, values or properties.  However, if there is an open-parenthesis immediately before the beginning of the partial symbol, only symbols with function definitions are considered as completions. The command which implements this is lisp-complete-symbol.

In Text mode and related modes, M-TAB completes words based on the spell-checker's dictionary.  See Spelling.

Glasses minor mode
==================

Glasses minor mode makes unreadableIdentifiersLikeThis readable by altering the way they display.  It knows two different ways to do this: by displaying underscores between a lower-case letter and the following capital letter, and by emboldening the capital letters.  It does not alter the buffer text, only the way they display, so you can use it even on read-only buffers.  You can use the command M-x glasses-mode to enable or disable the mode in the current buffer; you can also add glasses-mode to the mode hook of the programming language major modes in which you normally want to use Glasses mode.

Other Features Useful for Editing Programs
==========================================

A number of 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 symbols names contain words (see Words); sentences can be found in strings and comments (see Sentences).  Paragraphs in the strict sense can be found in program code (in long comments), but the paragraph commands are useful in other places too, because programming language major modes define paragraphs to begin and end at blank lines (see Paragraphs). 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.

The selective display feature is useful for looking at the overall structure of a function (see Selective Display).  This feature hides the lines that are indented more than a specified amount. Programming modes often support Outline minor mode (see Outline Mode).  The Foldout package provides folding-editor features (see Foldout).

The ``automatic typing'' features may be useful for writing programs. See Autotyping.

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.'')  See CC Mode, for a more extensive description of these modes and their special features.

C Mode Motion Commands
----------------------

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

M-x c-beginning-of-defun
M-x c-end-of-defun
     Move point to the beginning or end of the current function or top-level definition.  These are found by searching for the least enclosing braces.  (By contrast, beginning-of-defun and end-of-defun search for braces in column zero.)  If you are editing code where the opening brace of a function isn't placed in column zero, you may wish to bind C-M-a and C-M-e to these commands.  See 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).

M-x c-backward-into-nomenclature
     Move point backward to beginning of a C++ nomenclature section or word.  With prefix argument N, move N times.  If N is negative, move forward.  C++ nomenclature means a symbol name in the style of NamingSymbolsWithMixedCaseAndNoUnderlines; each capital letter begins a section or word.  Rather than this command, you might well prefer the newer ``Subword Mode'', which does the same thing better.  See Other C Commands.

     In the GNU project, we recommend using underscores to separate words within an identifier in C or C++, rather than using case distinctions.

M-x c-forward-into-nomenclature
     Move point forward to end of a C++ nomenclature section or word. With prefix argument N, move N times.

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, /l appears in the mode line after the mode name:

C-c C-l
     Toggle electric action (c-toggle-electric-state).  With a prefix argument, this command enables electric action if the argument is positive, disables it if it is negative.

Electric characters insert newlines only when, in addition to the electric state, the auto-newline feature is enabled (indicated by /la 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 (The CC Mode Manual).

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-BS
C-c BS
     c-hungry-backspace---Delete the entire block of whitespace preceding point.

C-c C-d
C-c C-DEL
C-c DEL
     c-hungry-delete-forward---Delete the entire block of whitespace following point.

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

M-x c-toggle-hungry-state
     Toggle the hungry-delete feature (c-toggle-hungry-state) (This command had the binding C-c C-d in earlier versions of Emacs.  C-c C-d is now bound to c-hungry-delete-forward.).  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.

Other Commands for C Mode
-------------------------

C-c C-w
M-x c-subword-mode
     Enable (or disable) subword mode.  In subword mode, Emacs's word commands then recognize upper case letters in StudlyCapsIdentifiers as word boundaries.  This is indicated by the flag /w on the mode line after the mode name (e.g. C/law).  You can even use M-x c-subword-mode in non-CC Mode buffers.

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 C-j (newline-and-indent), 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 C-j.  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-j" '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:

       * Assignments inside expressions.

       * Semicolon following immediately after if, for, and while (except after a do ... while statement);

       * C++ functions with reference parameters.

     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.  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.

Fortran Mode
============

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.

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 (see Hooks).

Fortran mode is meant for editing Fortran77 ``fixed format'' (and also ``tab format'') source code.  For editing the modern Fortran90 or Fortran95 ``free format'' source code, use F90 mode (f90-mode). Emacs normally uses Fortran mode for files with extension .f, .F or .for, and F90 mode for the extension .f90 and .f95.  GNU Fortran supports both kinds of format.

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), 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 (f90-next-block).  A code block is a subroutine, if--endif statement, and so forth.  This command exists for F90 mode only, not Fortran mode.  With a numeric argument, this moves forward that many blocks.

C-c C-a
     Move point backward to the previous code 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 agument, 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.

Fortran Indentation
-------------------

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

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 point is in (fortran-indent-subprogram).

M-q
     Fill a comment block or 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 runs 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.

Continuation Lines
..................

Most Fortran77 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 format.  (In GNU Emacs we always count columns from 0; but note that the Fortran standard counts from 1.)  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.  (Fortran90 introduced ``free format'', with another style of 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 format, and non-nil for tab format) is used.  /t in the mode line indicates tab format is selected.  Fortran mode sets the value of indent-tabs-mode accordingly (see Just Spaces).

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 format 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 always 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 always consist of one tab character.

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 GNU Emacs.)

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.

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:

  * Two nested do loops never share a continue statement.

  * Fortran keywords such as if, else, then, do and others are written without embedded whitespace or line breaks.

     Fortran compilers generally ignore whitespace outside of string constants, but Fortran mode does not recognize these keywords if they are not contiguous.  Constructs such as else if or end do are acceptable, but the second word should be on the same line as the first and not on a continuation line.

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.

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 Fortran77, a numbered do statement is ended 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 is nil.

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 format continuation line style.  Statement bodies are never indented less than this much.  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 less than this much.  The default is 8.

The variables controlling the indentation of comments are described in the following section.

Fortran Comments
----------------

The usual Emacs comment commands assume that a comment can follow a line of code.  In Fortran77, 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 Fortran90 comment syntax where comments start with ! and can follow other text.  Because only some Fortran77 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 "!".

M-;
     Align comment or insert new comment (fortran-indent-comment).

C-x ;
     Applies to nonstandard ! comments only.

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 is redefined as the command fortran-indent-comment.  Like the usual M-; command, this recognizes any kind of existing comment and aligns its text appropriately; if there is no existing comment, a comment is inserted and aligned.  But 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 statement indentation is fortran-minimum-statement-indent-fixed for fixed format continuation line style and fortran-minimum-statement-indent-tab for tab format 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 ; 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.

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 ForIndent Cont).  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 (see 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 turn-on-auto-fill to fortran-mode-hook.  See Hooks.

Checking Columns in Fortran
---------------------------

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 72 columns wide (fortran-window-create-momentarily).  This may help you avoid making lines longer than the 72-character limit that some Fortran compilers impose.

C-u C-c C-w
     Split the current window horizontally so that it is 72 columns wide (fortran-window-create).  You can then continue editing.

M-x fortran-strip-sequence-nos
     Delete all text in column 72 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. 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 72 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 72 and beyond, on all lines in the current buffer.  This is the easiest way to get rid of old sequence numbers.

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.  See Abbrevs.

The built-in abbrevs are unusual in one way: they all start with a semicolon.  You cannot normally use semicolon in an abbrev, but Fortran mode makes this possible by changing the syntax of semicolon to ``word constituent.''

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.

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.