Next: , Up: Defuns


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