Merge from trunk.
[bpt/emacs.git] / doc / lispref / modes.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-1999, 2001-2011 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions.
5 @setfilename ../../info/modes
6 @node Modes, Documentation, Keymaps, Top
7 @chapter Major and Minor Modes
8 @cindex mode
9
10 A @dfn{mode} is a set of definitions that customize Emacs and can be
11 turned on and off while you edit. There are two varieties of modes:
12 @dfn{major modes}, which are mutually exclusive and used for editing
13 particular kinds of text, and @dfn{minor modes}, which provide features
14 that users can enable individually.
15
16 This chapter describes how to write both major and minor modes, how to
17 indicate them in the mode line, and how they run hooks supplied by the
18 user. For related topics such as keymaps and syntax tables, see
19 @ref{Keymaps}, and @ref{Syntax Tables}.
20
21 @menu
22 * Hooks:: How to use hooks; how to write code that provides hooks.
23 * Major Modes:: Defining major modes.
24 * Minor Modes:: Defining minor modes.
25 * Mode Line Format:: Customizing the text that appears in the mode line.
26 * Imenu:: How a mode can provide a menu
27 of definitions in the buffer.
28 * Font Lock Mode:: How modes can highlight text according to syntax.
29 * Auto-Indentation:: How to teach Emacs to indent for a major mode.
30 * Desktop Save Mode:: How modes can have buffer state saved between
31 Emacs sessions.
32 @end menu
33
34 @node Hooks
35 @section Hooks
36 @cindex hooks
37
38 A @dfn{hook} is a variable where you can store a function or functions
39 to be called on a particular occasion by an existing program. Emacs
40 provides hooks for the sake of customization. Most often, hooks are set
41 up in the init file (@pxref{Init File}), but Lisp programs can set them also.
42 @xref{Standard Hooks}, for a list of standard hook variables.
43
44 @cindex normal hook
45 Most of the hooks in Emacs are @dfn{normal hooks}. These variables
46 contain lists of functions to be called with no arguments. By
47 convention, whenever the hook name ends in @samp{-hook}, that tells
48 you it is normal. We try to make all hooks normal, as much as
49 possible, so that you can use them in a uniform way.
50
51 Every major mode function is supposed to run a normal hook called
52 the @dfn{mode hook} as the one of the last steps of initialization.
53 This makes it easy for a user to customize the behavior of the mode,
54 by overriding the buffer-local variable assignments already made by
55 the mode. Most minor mode functions also run a mode hook at the end.
56 But hooks are used in other contexts too. For example, the hook
57 @code{suspend-hook} runs just before Emacs suspends itself
58 (@pxref{Suspending Emacs}).
59
60 The recommended way to add a hook function to a normal hook is by
61 calling @code{add-hook} (see below). The hook functions may be any of
62 the valid kinds of functions that @code{funcall} accepts (@pxref{What
63 Is a Function}). Most normal hook variables are initially void;
64 @code{add-hook} knows how to deal with this. You can add hooks either
65 globally or buffer-locally with @code{add-hook}.
66
67 @cindex abnormal hook
68 If the hook variable's name does not end with @samp{-hook}, that
69 indicates it is probably an @dfn{abnormal hook}. That means the hook
70 functions are called with arguments, or their return values are used
71 in some way. The hook's documentation says how the functions are
72 called. You can use @code{add-hook} to add a function to an abnormal
73 hook, but you must write the function to follow the hook's calling
74 convention.
75
76 By convention, abnormal hook names end in @samp{-functions} or
77 @samp{-hooks}. If the variable's name ends in @samp{-function}, then
78 its value is just a single function, not a list of functions.
79
80 @menu
81 * Running Hooks:: How to run a hook.
82 * Setting Hooks:: How to put functions on a hook, or remove them.
83 @end menu
84
85 @node Running Hooks
86 @subsection Running Hooks
87
88 At the appropriate times, Emacs uses the @code{run-hooks} function
89 and the other functions below to run particular hooks.
90
91 @defun run-hooks &rest hookvars
92 This function takes one or more normal hook variable names as
93 arguments, and runs each hook in turn. Each argument should be a
94 symbol that is a normal hook variable. These arguments are processed
95 in the order specified.
96
97 If a hook variable has a non-@code{nil} value, that value should be a
98 list of functions. @code{run-hooks} calls all the functions, one by
99 one, with no arguments.
100
101 The hook variable's value can also be a single function---either a
102 lambda expression or a symbol with a function definition---which
103 @code{run-hooks} calls. But this usage is obsolete.
104
105 If the hook variable is buffer-local, the buffer-local variable will
106 be used instead of the global variable. However, if the buffer-local
107 variable contains the element @code{t}, the global hook variable will
108 be run as well.
109 @end defun
110
111 @defun run-hook-with-args hook &rest args
112 This function is the way to run an abnormal hook and always call all
113 of the hook functions. It calls each of the hook functions one by
114 one, passing each of them the arguments @var{args}.
115 @end defun
116
117 @defun run-hook-with-args-until-failure hook &rest args
118 This function is the way to run an abnormal hook until one of the hook
119 functions fails. It calls each of the hook functions, passing each of
120 them the arguments @var{args}, until some hook function returns
121 @code{nil}. It then stops and returns @code{nil}. If none of the
122 hook functions return @code{nil}, it returns a non-@code{nil} value.
123 @end defun
124
125 @defun run-hook-with-args-until-success hook &rest args
126 This function is the way to run an abnormal hook until a hook function
127 succeeds. It calls each of the hook functions, passing each of them
128 the arguments @var{args}, until some hook function returns
129 non-@code{nil}. Then it stops, and returns whatever was returned by
130 the last hook function that was called. If all hook functions return
131 @code{nil}, it returns @code{nil} as well.
132 @end defun
133
134 @node Setting Hooks
135 @subsection Setting Hooks
136
137 Here's an example that uses a mode hook to turn on Auto Fill mode when
138 in Lisp Interaction mode:
139
140 @example
141 (add-hook 'lisp-interaction-mode-hook 'turn-on-auto-fill)
142 @end example
143
144 @defun add-hook hook function &optional append local
145 This function is the handy way to add function @var{function} to hook
146 variable @var{hook}. You can use it for abnormal hooks as well as for
147 normal hooks. @var{function} can be any Lisp function that can accept
148 the proper number of arguments for @var{hook}. For example,
149
150 @example
151 (add-hook 'text-mode-hook 'my-text-hook-function)
152 @end example
153
154 @noindent
155 adds @code{my-text-hook-function} to the hook called @code{text-mode-hook}.
156
157 If @var{function} is already present in @var{hook} (comparing using
158 @code{equal}), then @code{add-hook} does not add it a second time.
159
160 If @var{function} has a non-@code{nil} property
161 @code{permanent-local-hook}, then @code{kill-all-local-variables} (or
162 changing major modes) won't delete it from the hook variable's local
163 value.
164
165 It is best to design your hook functions so that the order in which
166 they are executed does not matter. Any dependence on the order is
167 asking for trouble. However, the order is predictable: normally,
168 @var{function} goes at the front of the hook list, so it will be
169 executed first (barring another @code{add-hook} call). If the
170 optional argument @var{append} is non-@code{nil}, the new hook
171 function goes at the end of the hook list and will be executed last.
172
173 @code{add-hook} can handle the cases where @var{hook} is void or its
174 value is a single function; it sets or changes the value to a list of
175 functions.
176
177 If @var{local} is non-@code{nil}, that says to add @var{function} to the
178 buffer-local hook list instead of to the global hook list. This makes
179 the hook buffer-local and adds @code{t} to the buffer-local value. The
180 latter acts as a flag to run the hook functions in the default value as
181 well as in the local value.
182 @end defun
183
184 @defun remove-hook hook function &optional local
185 This function removes @var{function} from the hook variable
186 @var{hook}. It compares @var{function} with elements of @var{hook}
187 using @code{equal}, so it works for both symbols and lambda
188 expressions.
189
190 If @var{local} is non-@code{nil}, that says to remove @var{function}
191 from the buffer-local hook list instead of from the global hook list.
192 @end defun
193
194 @node Major Modes
195 @section Major Modes
196 @cindex major mode
197
198 Major modes specialize Emacs for editing particular kinds of text.
199 Each buffer has only one major mode at a time. For each major mode
200 there is a function to switch to that mode in the current buffer; its
201 name should end in @samp{-mode}. These functions work by setting
202 buffer-local variable bindings and other data associated with the
203 buffer, such as a local keymap. The effect lasts until you switch
204 to another major mode in the same buffer.
205
206 @menu
207 * Major Mode Basics::
208 * Major Mode Conventions:: Coding conventions for keymaps, etc.
209 * Auto Major Mode:: How Emacs chooses the major mode automatically.
210 * Mode Help:: Finding out how to use a mode.
211 * Derived Modes:: Defining a new major mode based on another major
212 mode.
213 * Generic Modes:: Defining a simple major mode that supports
214 comment syntax and Font Lock mode.
215 * Mode Hooks:: Hooks run at the end of major mode functions.
216 * Example Major Modes:: Text mode and Lisp modes.
217 @end menu
218
219 @node Major Mode Basics
220 @subsection Major Mode Basics
221 @cindex Fundamental mode
222
223 The least specialized major mode is called @dfn{Fundamental mode}.
224 This mode has no mode-specific definitions or variable settings, so each
225 Emacs command behaves in its default manner, and each option is in its
226 default state. All other major modes redefine various keys and options.
227 For example, Lisp Interaction mode provides special key bindings for
228 @kbd{C-j} (@code{eval-print-last-sexp}), @key{TAB}
229 (@code{lisp-indent-line}), and other keys.
230
231 When you need to write several editing commands to help you perform a
232 specialized editing task, creating a new major mode is usually a good
233 idea. In practice, writing a major mode is easy (in contrast to
234 writing a minor mode, which is often difficult).
235
236 If the new mode is similar to an old one, it is often unwise to
237 modify the old one to serve two purposes, since it may become harder
238 to use and maintain. Instead, copy and rename an existing major mode
239 definition and alter the copy---or use the @code{define-derived-mode}
240 macro to define a @dfn{derived mode} (@pxref{Derived Modes}). For
241 example, Rmail Edit mode is a major mode that is very similar to Text
242 mode except that it provides two additional commands. Its definition
243 is distinct from that of Text mode, but uses that of Text mode.
244
245 Even if the new mode is not an obvious derivative of any other mode,
246 we recommend to use @code{define-derived-mode}, since it automatically
247 enforces the most important coding conventions for you.
248
249 For a very simple programming language major mode that handles
250 comments and fontification, you can use @code{define-generic-mode}.
251 @xref{Generic Modes}.
252
253 Rmail Edit mode offers an example of changing the major mode
254 temporarily for a buffer, so it can be edited in a different way (with
255 ordinary Emacs commands rather than Rmail commands). In such cases, the
256 temporary major mode usually provides a command to switch back to the
257 buffer's usual mode (Rmail mode, in this case). You might be tempted to
258 present the temporary redefinitions inside a recursive edit and restore
259 the usual ones when the user exits; but this is a bad idea because it
260 constrains the user's options when it is done in more than one buffer:
261 recursive edits must be exited most-recently-entered first. Using an
262 alternative major mode avoids this limitation. @xref{Recursive
263 Editing}.
264
265 The standard GNU Emacs Lisp library directory tree contains the code
266 for several major modes, in files such as @file{text-mode.el},
267 @file{texinfo.el}, @file{lisp-mode.el}, @file{c-mode.el}, and
268 @file{rmail.el}. They are found in various subdirectories of the
269 @file{lisp} directory. You can study these libraries to see how modes
270 are written. Text mode is perhaps the simplest major mode aside from
271 Fundamental mode. Rmail mode is a complicated and specialized mode.
272
273 @node Major Mode Conventions
274 @subsection Major Mode Conventions
275 @cindex major mode conventions
276 @cindex conventions for writing major modes
277
278 The code for existing major modes follows various coding conventions,
279 including conventions for local keymap and syntax table initialization,
280 global names, and hooks. Please follow these conventions when you
281 define a new major mode. (Fundamental mode is an exception to many
282 of these conventions, because its definition is to present the global
283 state of Emacs.)
284
285 This list of conventions is only partial, because each major mode
286 should aim for consistency in general with other Emacs major modes.
287 This makes Emacs as a whole more coherent. It is impossible to list
288 here all the possible points where this issue might come up; if the
289 Emacs developers point out an area where your major mode deviates from
290 the usual conventions, please make it compatible.
291
292 @itemize @bullet
293 @item
294 Define a command whose name ends in @samp{-mode}, with no arguments,
295 that switches to the new mode in the current buffer. This command
296 should set up the keymap, syntax table, and buffer-local variables in an
297 existing buffer, without changing the buffer's contents.
298
299 @item
300 Write a documentation string for this command that describes the
301 special commands available in this mode. @kbd{C-h m}
302 (@code{describe-mode}) in your mode will display this string.
303
304 The documentation string may include the special documentation
305 substrings, @samp{\[@var{command}]}, @samp{\@{@var{keymap}@}}, and
306 @samp{\<@var{keymap}>}, which enable the documentation to adapt
307 automatically to the user's own key bindings. @xref{Keys in
308 Documentation}.
309
310 @item
311 The major mode command should start by calling
312 @code{kill-all-local-variables}. This runs the normal hook
313 @code{change-major-mode-hook}, then gets rid of the buffer-local
314 variables of the major mode previously in effect. @xref{Creating
315 Buffer-Local}.
316
317 @item
318 The major mode command should set the variable @code{major-mode} to the
319 major mode command symbol. This is how @code{describe-mode} discovers
320 which documentation to print.
321
322 @item
323 The major mode command should set the variable @code{mode-name} to the
324 ``pretty'' name of the mode, usually a string (but see @ref{Mode Line
325 Data}, for other possible forms). The name of the mode appears
326 in the mode line.
327
328 @item
329 @cindex functions in modes
330 Since all global names are in the same name space, all the global
331 variables, constants, and functions that are part of the mode should
332 have names that start with the major mode name (or with an abbreviation
333 of it if the name is long). @xref{Coding Conventions}.
334
335 @item
336 In a major mode for editing some kind of structured text, such as a
337 programming language, indentation of text according to structure is
338 probably useful. So the mode should set @code{indent-line-function}
339 to a suitable function, and probably customize other variables
340 for indentation. @xref{Auto-Indentation}.
341
342 @item
343 @cindex keymaps in modes
344 The major mode should usually have its own keymap, which is used as the
345 local keymap in all buffers in that mode. The major mode command should
346 call @code{use-local-map} to install this local map. @xref{Active
347 Keymaps}, for more information.
348
349 This keymap should be stored permanently in a global variable named
350 @code{@var{modename}-mode-map}. Normally the library that defines the
351 mode sets this variable.
352
353 @xref{Tips for Defining}, for advice about how to write the code to set
354 up the mode's keymap variable.
355
356 @item
357 The key sequences bound in a major mode keymap should usually start with
358 @kbd{C-c}, followed by a control character, a digit, or @kbd{@{},
359 @kbd{@}}, @kbd{<}, @kbd{>}, @kbd{:} or @kbd{;}. The other punctuation
360 characters are reserved for minor modes, and ordinary letters are
361 reserved for users.
362
363 A major mode can also rebind the keys @kbd{M-n}, @kbd{M-p} and
364 @kbd{M-s}. The bindings for @kbd{M-n} and @kbd{M-p} should normally
365 be some kind of ``moving forward and backward,'' but this does not
366 necessarily mean cursor motion.
367
368 It is legitimate for a major mode to rebind a standard key sequence if
369 it provides a command that does ``the same job'' in a way better
370 suited to the text this mode is used for. For example, a major mode
371 for editing a programming language might redefine @kbd{C-M-a} to
372 ``move to the beginning of a function'' in a way that works better for
373 that language.
374
375 It is also legitimate for a major mode to rebind a standard key
376 sequence whose standard meaning is rarely useful in that mode. For
377 instance, minibuffer modes rebind @kbd{M-r}, whose standard meaning is
378 rarely of any use in the minibuffer. Major modes such as Dired or
379 Rmail that do not allow self-insertion of text can reasonably redefine
380 letters and other printing characters as special commands.
381
382 @item
383 Major modes for editing text should not define @key{RET} to do
384 anything other than insert a newline. However, it is ok for
385 specialized modes for text that users don't directly edit, such as
386 Dired and Info modes, to redefine @key{RET} to do something entirely
387 different.
388
389 @item
390 Major modes should not alter options that are primarily a matter of user
391 preference, such as whether Auto-Fill mode is enabled. Leave this to
392 each user to decide. However, a major mode should customize other
393 variables so that Auto-Fill mode will work usefully @emph{if} the user
394 decides to use it.
395
396 @item
397 @cindex syntax tables in modes
398 The mode may have its own syntax table or may share one with other
399 related modes. If it has its own syntax table, it should store this in
400 a variable named @code{@var{modename}-mode-syntax-table}. @xref{Syntax
401 Tables}.
402
403 @item
404 If the mode handles a language that has a syntax for comments, it should
405 set the variables that define the comment syntax. @xref{Options for
406 Comments,, Options Controlling Comments, emacs, The GNU Emacs Manual}.
407
408 @item
409 @cindex abbrev tables in modes
410 The mode may have its own abbrev table or may share one with other
411 related modes. If it has its own abbrev table, it should store this
412 in a variable named @code{@var{modename}-mode-abbrev-table}. If the
413 major mode command defines any abbrevs itself, it should pass @code{t}
414 for the @var{system-flag} argument to @code{define-abbrev}.
415 @xref{Defining Abbrevs}.
416
417 @item
418 The mode should specify how to do highlighting for Font Lock mode, by
419 setting up a buffer-local value for the variable
420 @code{font-lock-defaults} (@pxref{Font Lock Mode}).
421
422 @item
423 Each face that the mode defines should, if possible, inherit from an
424 existing Emacs face. This reduces the chance of conflicting with a
425 user's face customizations. Useful faces include:
426
427 @table @asis
428 @item @code{highlight}
429 for stretches of text that should temporarily stand out.
430
431 @item @code{match}
432 for text matching a search command.
433
434 @item @code{link} and @code{link-visited}
435 for clickable text buttons that send the user to a different buffer or
436 ``location''.
437
438 @item @code{button}
439 for clickable text buttons that perform other actions.
440
441 @item @asis{Font Lock faces}
442 for other kinds of syntactic highlighting, if highlighting is not
443 handled by Font Lock mode or some Font Lock faces are not in use.
444 @xref{Faces for Font Lock}, for how to assign Font Lock faces.
445 @end table
446
447 @item
448 The mode should specify how Imenu should find the definitions or
449 sections of a buffer, by setting up a buffer-local value for the
450 variable @code{imenu-generic-expression}, for the two variables
451 @code{imenu-prev-index-position-function} and
452 @code{imenu-extract-index-name-function}, or for the variable
453 @code{imenu-create-index-function} (@pxref{Imenu}).
454
455 @item
456 The mode can specify a local value for
457 @code{eldoc-documentation-function} to tell ElDoc mode how to handle
458 this mode.
459
460 @item
461 The mode can specify how to complete various keywords by adding
462 to the special hook @code{completion-at-point-functions}.
463
464 @item
465 Use @code{defvar} or @code{defcustom} to set mode-related variables, so
466 that they are not reinitialized if they already have a value. (Such
467 reinitialization could discard customizations made by the user.)
468
469 @item
470 @cindex buffer-local variables in modes
471 To make a buffer-local binding for an Emacs customization variable, use
472 @code{make-local-variable} in the major mode command, not
473 @code{make-variable-buffer-local}. The latter function would make the
474 variable local to every buffer in which it is subsequently set, which
475 would affect buffers that do not use this mode. It is undesirable for a
476 mode to have such global effects. @xref{Buffer-Local Variables}.
477
478 With rare exceptions, the only reasonable way to use
479 @code{make-variable-buffer-local} in a Lisp package is for a variable
480 which is used only within that package. Using it on a variable used by
481 other packages would interfere with them.
482
483 @item
484 @cindex mode hook
485 @cindex major mode hook
486 Each major mode should have a normal @dfn{mode hook} named
487 @code{@var{modename}-mode-hook}. The very last thing the major mode command
488 should do is to call @code{run-mode-hooks}. This runs the mode hook,
489 and then runs the normal hook @code{after-change-major-mode-hook}.
490 @xref{Mode Hooks}.
491
492 @item
493 The major mode command may start by calling some other major mode
494 command (called the @dfn{parent mode}) and then alter some of its
495 settings. A mode that does this is called a @dfn{derived mode}. The
496 recommended way to define one is to use the @code{define-derived-mode}
497 macro, but this is not required. Such a mode should call the parent
498 mode command inside a @code{delay-mode-hooks} form. (Using
499 @code{define-derived-mode} does this automatically.) @xref{Derived
500 Modes}, and @ref{Mode Hooks}.
501
502 @item
503 If something special should be done if the user switches a buffer from
504 this mode to any other major mode, this mode can set up a buffer-local
505 value for @code{change-major-mode-hook} (@pxref{Creating Buffer-Local}).
506
507 @item
508 If this mode is appropriate only for specially-prepared text produced by
509 the mode itself (rather than by the user typing at the keyboard or by an
510 external file), then the major mode command symbol should have a
511 property named @code{mode-class} with value @code{special}, put on as
512 follows:
513
514 @kindex mode-class @r{(property)}
515 @cindex @code{special} modes
516 @example
517 (put 'funny-mode 'mode-class 'special)
518 @end example
519
520 @noindent
521 This tells Emacs that new buffers created while the current buffer is in
522 Funny mode should not be put in Funny mode, even though the default
523 value of @code{major-mode} is @code{nil}. By default, the value of
524 @code{nil} for @code{major-mode} means to use the current buffer's major
525 mode when creating new buffers (@pxref{Auto Major Mode}), but with such
526 @code{special} modes, Fundamental mode is used instead. Modes such as
527 Dired, Rmail, and Buffer List use this feature.
528
529 The @code{define-derived-mode} macro automatically marks the derived
530 mode as special if the parent mode is special. The special mode
531 @code{special-mode} provides a convenient parent for other special
532 modes to inherit from; it sets @code{buffer-read-only} to @code{t},
533 and does little else.
534
535 @item
536 If you want to make the new mode the default for files with certain
537 recognizable names, add an element to @code{auto-mode-alist} to select
538 the mode for those file names (@pxref{Auto Major Mode}). If you
539 define the mode command to autoload, you should add this element in
540 the same file that calls @code{autoload}. If you use an autoload
541 cookie for the mode command, you can also use an autoload cookie for
542 the form that adds the element (@pxref{autoload cookie}). If you do
543 not autoload the mode command, it is sufficient to add the element in
544 the file that contains the mode definition.
545
546 @item
547 In the comments that document the file, you should provide a sample
548 @code{autoload} form and an example of how to add to
549 @code{auto-mode-alist}, that users can include in their init files
550 (@pxref{Init File}).
551
552 @item
553 @cindex mode loading
554 The top-level forms in the file defining the mode should be written so
555 that they may be evaluated more than once without adverse consequences.
556 Even if you never load the file more than once, someone else will.
557 @end itemize
558
559 @node Auto Major Mode
560 @subsection How Emacs Chooses a Major Mode
561 @cindex major mode, automatic selection
562
563 Based on information in the file name or in the file itself, Emacs
564 automatically selects a major mode for the new buffer when a file is
565 visited. It also processes local variables specified in the file text.
566
567 @deffn Command fundamental-mode
568 Fundamental mode is a major mode that is not specialized for anything
569 in particular. Other major modes are defined in effect by comparison
570 with this one---their definitions say what to change, starting from
571 Fundamental mode. The @code{fundamental-mode} function does @emph{not}
572 run any mode hooks; you're not supposed to customize it. (If you want Emacs
573 to behave differently in Fundamental mode, change the @emph{global}
574 state of Emacs.)
575 @end deffn
576
577 @deffn Command normal-mode &optional find-file
578 This function establishes the proper major mode and buffer-local variable
579 bindings for the current buffer. First it calls @code{set-auto-mode}
580 (see below), then it runs @code{hack-local-variables} to parse, and
581 bind or evaluate as appropriate, the file's local variables
582 (@pxref{File Local Variables}).
583
584 If the @var{find-file} argument to @code{normal-mode} is non-@code{nil},
585 @code{normal-mode} assumes that the @code{find-file} function is calling
586 it. In this case, it may process local variables in the @samp{-*-}
587 line or at the end of the file. The variable
588 @code{enable-local-variables} controls whether to do so. @xref{File
589 Variables, , Local Variables in Files, emacs, The GNU Emacs Manual},
590 for the syntax of the local variables section of a file.
591
592 If you run @code{normal-mode} interactively, the argument
593 @var{find-file} is normally @code{nil}. In this case,
594 @code{normal-mode} unconditionally processes any file local variables.
595
596 The function calls @code{set-auto-mode} to choose a major mode. If this
597 does not specify a mode, the buffer stays in the major mode determined
598 by the default value of @code{major-mode} (see below).
599
600 @cindex file mode specification error
601 @code{normal-mode} uses @code{condition-case} around the call to the
602 major mode function, so errors are caught and reported as a @samp{File
603 mode specification error}, followed by the original error message.
604 @end deffn
605
606 @defun set-auto-mode &optional keep-mode-if-same
607 @cindex visited file mode
608 This function selects the major mode that is appropriate for the
609 current buffer. It bases its decision (in order of precedence) on
610 the @w{@samp{-*-}} line, on any @samp{mode:} local variable near the
611 end of a file, on the @w{@samp{#!}} line (using
612 @code{interpreter-mode-alist}), on the text at the beginning of the
613 buffer (using @code{magic-mode-alist}), and finally on the visited
614 file name (using @code{auto-mode-alist}). @xref{Choosing Modes, , How
615 Major Modes are Chosen, emacs, The GNU Emacs Manual}.
616 If @code{enable-local-variables} is @code{nil}, @code{set-auto-mode}
617 does not check the @w{@samp{-*-}} line, or near the end of the file,
618 for any mode tag.
619
620 If @var{keep-mode-if-same} is non-@code{nil}, this function does not
621 call the mode command if the buffer is already in the proper major
622 mode. For instance, @code{set-visited-file-name} sets this to
623 @code{t} to avoid killing buffer local variables that the user may
624 have set.
625 @end defun
626
627 @defopt major-mode
628 The buffer-local value of this variable holds the major mode
629 currently active. The default value of this variable holds the
630 default major mode for new buffers. The standard default value is
631 @code{fundamental-mode}.
632
633 If the default value of @code{major-mode} is @code{nil}, Emacs uses
634 the (previously) current buffer's major mode as the default major mode
635 of a new buffer. However, if that major mode symbol has a @code{mode-class}
636 property with value @code{special}, then it is not used for new buffers;
637 Fundamental mode is used instead. The modes that have this property are
638 those such as Dired and Rmail that are useful only with text that has
639 been specially prepared.
640 @end defopt
641
642 @defun set-buffer-major-mode buffer
643 This function sets the major mode of @var{buffer} to the default value of
644 @code{major-mode}; if that is @code{nil}, it uses the
645 current buffer's major mode (if that is suitable). As an exception,
646 if @var{buffer}'s name is @samp{*scratch*}, it sets the mode to
647 @code{initial-major-mode}.
648
649 The low-level primitives for creating buffers do not use this function,
650 but medium-level commands such as @code{switch-to-buffer} and
651 @code{find-file-noselect} use it whenever they create buffers.
652 @end defun
653
654 @defopt initial-major-mode
655 @cindex @samp{*scratch*}
656 The value of this variable determines the major mode of the initial
657 @samp{*scratch*} buffer. The value should be a symbol that is a major
658 mode command. The default value is @code{lisp-interaction-mode}.
659 @end defopt
660
661 @defvar interpreter-mode-alist
662 This variable specifies major modes to use for scripts that specify a
663 command interpreter in a @samp{#!} line. Its value is an alist with
664 elements of the form @code{(@var{interpreter} . @var{mode})}; for
665 example, @code{("perl" . perl-mode)} is one element present by
666 default. The element says to use mode @var{mode} if the file
667 specifies an interpreter which matches @var{interpreter}.
668 @end defvar
669
670 @defvar magic-mode-alist
671 This variable's value is an alist with elements of the form
672 @code{(@var{regexp} . @var{function})}, where @var{regexp} is a
673 regular expression and @var{function} is a function or @code{nil}.
674 After visiting a file, @code{set-auto-mode} calls @var{function} if
675 the text at the beginning of the buffer matches @var{regexp} and
676 @var{function} is non-@code{nil}; if @var{function} is @code{nil},
677 @code{auto-mode-alist} gets to decide the mode.
678 @end defvar
679
680 @defvar magic-fallback-mode-alist
681 This works like @code{magic-mode-alist}, except that it is handled
682 only if @code{auto-mode-alist} does not specify a mode for this file.
683 @end defvar
684
685 @defvar auto-mode-alist
686 This variable contains an association list of file name patterns
687 (regular expressions) and corresponding major mode commands. Usually,
688 the file name patterns test for suffixes, such as @samp{.el} and
689 @samp{.c}, but this need not be the case. An ordinary element of the
690 alist looks like @code{(@var{regexp} . @var{mode-function})}.
691
692 For example,
693
694 @smallexample
695 @group
696 (("\\`/tmp/fol/" . text-mode)
697 ("\\.texinfo\\'" . texinfo-mode)
698 ("\\.texi\\'" . texinfo-mode)
699 @end group
700 @group
701 ("\\.el\\'" . emacs-lisp-mode)
702 ("\\.c\\'" . c-mode)
703 ("\\.h\\'" . c-mode)
704 @dots{})
705 @end group
706 @end smallexample
707
708 When you visit a file whose expanded file name (@pxref{File Name
709 Expansion}), with version numbers and backup suffixes removed using
710 @code{file-name-sans-versions} (@pxref{File Name Components}), matches
711 a @var{regexp}, @code{set-auto-mode} calls the corresponding
712 @var{mode-function}. This feature enables Emacs to select the proper
713 major mode for most files.
714
715 If an element of @code{auto-mode-alist} has the form @code{(@var{regexp}
716 @var{function} t)}, then after calling @var{function}, Emacs searches
717 @code{auto-mode-alist} again for a match against the portion of the file
718 name that did not match before. This feature is useful for
719 uncompression packages: an entry of the form @code{("\\.gz\\'"
720 @var{function} t)} can uncompress the file and then put the uncompressed
721 file in the proper mode according to the name sans @samp{.gz}.
722
723 Here is an example of how to prepend several pattern pairs to
724 @code{auto-mode-alist}. (You might use this sort of expression in your
725 init file.)
726
727 @smallexample
728 @group
729 (setq auto-mode-alist
730 (append
731 ;; @r{File name (within directory) starts with a dot.}
732 '(("/\\.[^/]*\\'" . fundamental-mode)
733 ;; @r{File name has no dot.}
734 ("/[^\\./]*\\'" . fundamental-mode)
735 ;; @r{File name ends in @samp{.C}.}
736 ("\\.C\\'" . c++-mode))
737 auto-mode-alist))
738 @end group
739 @end smallexample
740 @end defvar
741
742 @node Mode Help
743 @subsection Getting Help about a Major Mode
744 @cindex mode help
745 @cindex help for major mode
746 @cindex documentation for major mode
747
748 The @code{describe-mode} function is used to provide information
749 about major modes. It is normally called with @kbd{C-h m}. The
750 @code{describe-mode} function uses the value of @code{major-mode},
751 which is why every major mode function needs to set the
752 @code{major-mode} variable.
753
754 @deffn Command describe-mode
755 This function displays the documentation of the current major mode.
756
757 The @code{describe-mode} function calls the @code{documentation}
758 function using the value of @code{major-mode} as an argument. Thus, it
759 displays the documentation string of the major mode function.
760 (@xref{Accessing Documentation}.)
761 @end deffn
762
763 @defvar major-mode
764 This buffer-local variable holds the symbol for the current buffer's
765 major mode. This symbol should have a function definition that is the
766 command to switch to that major mode. The @code{describe-mode}
767 function uses the documentation string of the function as the
768 documentation of the major mode.
769 @end defvar
770
771 @node Derived Modes
772 @subsection Defining Derived Modes
773 @cindex derived mode
774
775 The recommended way to define a new major mode is to derive it
776 from an existing one using @code{define-derived-mode}. If there is no
777 closely related mode, you can inherit from @code{text-mode},
778 @code{special-mode}, @code{prog-mode}, or in the worst case
779 @code{fundamental-mode}.
780
781 @defmac define-derived-mode variant parent name docstring keyword-args@dots{} body@dots{}
782 This macro defines @var{variant} as a major mode command, using
783 @var{name} as the string form of the mode name. @var{variant} and
784 @var{parent} should be unquoted symbols.
785
786 The new command @var{variant} is defined to call the function
787 @var{parent}, then override certain aspects of that parent mode:
788
789 @itemize @bullet
790 @item
791 The new mode has its own sparse keymap, named
792 @code{@var{variant}-map}. @code{define-derived-mode}
793 makes the parent mode's keymap the parent of the new map, unless
794 @code{@var{variant}-map} is already set and already has a parent.
795
796 @item
797 The new mode has its own syntax table, kept in the variable
798 @code{@var{variant}-syntax-table}, unless you override this using the
799 @code{:syntax-table} keyword (see below). @code{define-derived-mode}
800 makes the parent mode's syntax-table the parent of
801 @code{@var{variant}-syntax-table}, unless the latter is already set
802 and already has a parent different from the standard syntax table.
803
804 @item
805 The new mode has its own abbrev table, kept in the variable
806 @code{@var{variant}-abbrev-table}, unless you override this using the
807 @code{:abbrev-table} keyword (see below).
808
809 @item
810 The new mode has its own mode hook, @code{@var{variant}-hook}. It
811 runs this hook, after running the hooks of its ancestor modes, with
812 @code{run-mode-hooks}, as the last thing it does. @xref{Mode Hooks}.
813 @end itemize
814
815 In addition, you can specify how to override other aspects of
816 @var{parent} with @var{body}. The command @var{variant}
817 evaluates the forms in @var{body} after setting up all its usual
818 overrides, just before running the mode hooks.
819
820 If @var{parent} has a non-@code{nil} @code{mode-class} symbol
821 property, then @code{define-derived-mode} sets the @code{mode-class}
822 property of @var{variant} to the same value. This ensures, for
823 example, that if @var{parent} is a special mode, then @var{variant} is
824 also a special mode (@pxref{Major Mode Conventions}).
825
826 You can also specify @code{nil} for @var{parent}. This gives the new
827 mode no parent. Then @code{define-derived-mode} behaves as described
828 above, but, of course, omits all actions connected with @var{parent}.
829
830 The argument @var{docstring} specifies the documentation string for
831 the new mode. @code{define-derived-mode} adds some general
832 information about the mode's hook, followed by the mode's keymap, at
833 the end of this docstring. If you omit @var{docstring},
834 @code{define-derived-mode} generates a documentation string.
835
836 The @var{keyword-args} are pairs of keywords and values. The values
837 are evaluated. The following keywords are currently supported:
838
839 @table @code
840 @item :syntax-table
841 You can use this to explicitly specify a syntax table for the new
842 mode. If you specify a @code{nil} value, the new mode uses the same
843 syntax table as @var{parent}, or the standard syntax table if
844 @var{parent} is @code{nil}. (Note that this does @emph{not} follow
845 the convention used for non-keyword arguments that a @code{nil} value
846 is equivalent with not specifying the argument.)
847
848 @item :abbrev-table
849 You can use this to explicitly specify an abbrev table for the new
850 mode. If you specify a @code{nil} value, the new mode uses the same
851 abbrev table as @var{parent}, or @code{fundamental-mode-abbrev-table}
852 if @var{parent} is @code{nil}. (Again, a @code{nil} value is
853 @emph{not} equivalent to not specifying this keyword.)
854
855 @item :group
856 If this is specified, the value should be the customization group for
857 this mode. (Not all major modes have one.) Only the (still
858 experimental and unadvertised) command @code{customize-mode} currently
859 uses this. @code{define-derived-mode} does @emph{not} automatically
860 define the specified customization group.
861 @end table
862
863 Here is a hypothetical example:
864
865 @example
866 (define-derived-mode hypertext-mode
867 text-mode "Hypertext"
868 "Major mode for hypertext.
869 \\@{hypertext-mode-map@}"
870 (setq case-fold-search nil))
871
872 (define-key hypertext-mode-map
873 [down-mouse-3] 'do-hyper-link)
874 @end example
875
876 Do not write an @code{interactive} spec in the definition;
877 @code{define-derived-mode} does that automatically.
878 @end defmac
879
880 @node Generic Modes
881 @subsection Generic Modes
882 @cindex generic mode
883
884 @dfn{Generic modes} are simple major modes with basic support for
885 comment syntax and Font Lock mode. To define a generic mode, use the
886 macro @code{define-generic-mode}. See the file @file{generic-x.el}
887 for some examples of the use of @code{define-generic-mode}.
888
889 @defmac define-generic-mode mode comment-list keyword-list font-lock-list auto-mode-list function-list &optional docstring
890 This macro defines a generic mode command named @var{mode} (a symbol,
891 not quoted). The optional argument @var{docstring} is the
892 documentation for the mode command. If you do not supply it,
893 @code{define-generic-mode} generates one by default.
894
895 The argument @var{comment-list} is a list in which each element is
896 either a character, a string of one or two characters, or a cons cell.
897 A character or a string is set up in the mode's syntax table as a
898 ``comment starter.'' If the entry is a cons cell, the @sc{car} is set
899 up as a ``comment starter'' and the @sc{cdr} as a ``comment ender.''
900 (Use @code{nil} for the latter if you want comments to end at the end
901 of the line.) Note that the syntax table mechanism has limitations
902 about what comment starters and enders are actually possible.
903 @xref{Syntax Tables}.
904
905 The argument @var{keyword-list} is a list of keywords to highlight
906 with @code{font-lock-keyword-face}. Each keyword should be a string.
907 Meanwhile, @var{font-lock-list} is a list of additional expressions to
908 highlight. Each element of this list should have the same form as an
909 element of @code{font-lock-keywords}. @xref{Search-based
910 Fontification}.
911
912 The argument @var{auto-mode-list} is a list of regular expressions to
913 add to the variable @code{auto-mode-alist}. They are added by the execution
914 of the @code{define-generic-mode} form, not by expanding the macro call.
915
916 Finally, @var{function-list} is a list of functions for the mode
917 command to call for additional setup. It calls these functions just
918 before it runs the mode hook variable @code{@var{mode}-hook}.
919 @end defmac
920
921 @node Mode Hooks
922 @subsection Mode Hooks
923
924 Every major mode function should finish by running its mode hook and
925 the mode-independent normal hook @code{after-change-major-mode-hook}.
926 It does this by calling @code{run-mode-hooks}. If the major mode is a
927 derived mode, that is if it calls another major mode (the parent mode)
928 in its body, it should do this inside @code{delay-mode-hooks} so that
929 the parent won't run these hooks itself. Instead, the derived mode's
930 call to @code{run-mode-hooks} runs the parent's mode hook too.
931 @xref{Major Mode Conventions}.
932
933 Emacs versions before Emacs 22 did not have @code{delay-mode-hooks}.
934 When user-implemented major modes have not been updated to use it,
935 they won't entirely follow these conventions: they may run the
936 parent's mode hook too early, or fail to run
937 @code{after-change-major-mode-hook}. If you encounter such a major
938 mode, please correct it to follow these conventions.
939
940 When you defined a major mode using @code{define-derived-mode}, it
941 automatically makes sure these conventions are followed. If you
942 define a major mode ``by hand,'' not using @code{define-derived-mode},
943 use the following functions to handle these conventions automatically.
944
945 @defun run-mode-hooks &rest hookvars
946 Major modes should run their mode hook using this function. It is
947 similar to @code{run-hooks} (@pxref{Hooks}), but it also runs
948 @code{after-change-major-mode-hook}.
949
950 When this function is called during the execution of a
951 @code{delay-mode-hooks} form, it does not run the hooks immediately.
952 Instead, it arranges for the next call to @code{run-mode-hooks} to run
953 them.
954 @end defun
955
956 @defmac delay-mode-hooks body@dots{}
957 When one major mode command calls another, it should do so inside of
958 @code{delay-mode-hooks}.
959
960 This macro executes @var{body}, but tells all @code{run-mode-hooks}
961 calls during the execution of @var{body} to delay running their hooks.
962 The hooks will actually run during the next call to
963 @code{run-mode-hooks} after the end of the @code{delay-mode-hooks}
964 construct.
965 @end defmac
966
967 @defvar after-change-major-mode-hook
968 This is a normal hook run by @code{run-mode-hooks}. It is run at the
969 very end of every properly-written major mode function.
970 @end defvar
971
972 @node Example Major Modes
973 @subsection Major Mode Examples
974
975 Text mode is perhaps the simplest mode besides Fundamental mode.
976 Here are excerpts from @file{text-mode.el} that illustrate many of
977 the conventions listed above:
978
979 @smallexample
980 @group
981 ;; @r{Create the syntax table for this mode.}
982 (defvar text-mode-syntax-table
983 (let ((st (make-syntax-table)))
984 (modify-syntax-entry ?\" ". " st)
985 (modify-syntax-entry ?\\ ". " st)
986 ;; Add `p' so M-c on `hello' leads to `Hello', not `hello'.
987 (modify-syntax-entry ?' "w p" st)
988 st)
989 "Syntax table used while in `text-mode'.")
990 @end group
991
992 ;; @r{Create the keymap for this mode.}
993 @group
994 (defvar text-mode-map
995 (let ((map (make-sparse-keymap)))
996 (define-key map "\e\t" 'ispell-complete-word)
997 (define-key map "\es" 'center-line)
998 (define-key map "\eS" 'center-paragraph)
999 map)
1000 "Keymap for `text-mode'.
1001 Many other modes, such as Mail mode, Outline mode
1002 and Indented Text mode, inherit all the commands
1003 defined in this map.")
1004 @end group
1005 @end smallexample
1006
1007 Here is how the actual mode command is defined now:
1008
1009 @smallexample
1010 @group
1011 (define-derived-mode text-mode nil "Text"
1012 "Major mode for editing text written for humans to read.
1013 In this mode, paragraphs are delimited only by blank or white lines.
1014 You can thus get the full benefit of adaptive filling
1015 (see the variable `adaptive-fill-mode').
1016 \\@{text-mode-map@}
1017 Turning on Text mode runs the normal hook `text-mode-hook'."
1018 @end group
1019 @group
1020 (set (make-local-variable 'text-mode-variant) t)
1021 ;; @r{These two lines are a feature added recently.}
1022 (set (make-local-variable 'require-final-newline)
1023 mode-require-final-newline)
1024 (set (make-local-variable 'indent-line-function) 'indent-relative))
1025 @end group
1026 @end smallexample
1027
1028 @noindent
1029 (The last line is redundant nowadays, since @code{indent-relative} is
1030 the default value, and we'll delete it in a future version.)
1031
1032 Here is how it was defined formerly, before
1033 @code{define-derived-mode} existed:
1034
1035 @smallexample
1036 @group
1037 ;; @r{This isn't needed nowadays, since @code{define-derived-mode} does it.}
1038 (define-abbrev-table 'text-mode-abbrev-table ()
1039 "Abbrev table used while in text mode.")
1040 @end group
1041
1042 @group
1043 (defun text-mode ()
1044 "Major mode for editing text intended for humans to read...
1045 Special commands: \\@{text-mode-map@}
1046 @end group
1047 @group
1048 Turning on text-mode runs the hook `text-mode-hook'."
1049 (interactive)
1050 (kill-all-local-variables)
1051 (use-local-map text-mode-map)
1052 @end group
1053 @group
1054 (setq local-abbrev-table text-mode-abbrev-table)
1055 (set-syntax-table text-mode-syntax-table)
1056 @end group
1057 @group
1058 ;; @r{These four lines are absent from the current version}
1059 ;; @r{not because this is done some other way, but because}
1060 ;; @r{nowadays Text mode uses the normal definition of paragraphs.}
1061 (set (make-local-variable 'paragraph-start)
1062 (concat "[ \t]*$\\|" page-delimiter))
1063 (set (make-local-variable 'paragraph-separate) paragraph-start)
1064 (set (make-local-variable 'indent-line-function) 'indent-relative-maybe)
1065 @end group
1066 @group
1067 (setq mode-name "Text")
1068 (setq major-mode 'text-mode)
1069 (run-mode-hooks 'text-mode-hook)) ; @r{Finally, this permits the user to}
1070 ; @r{customize the mode with a hook.}
1071 @end group
1072 @end smallexample
1073
1074 @cindex @file{lisp-mode.el}
1075 The three Lisp modes (Lisp mode, Emacs Lisp mode, and Lisp
1076 Interaction mode) have more features than Text mode and the code is
1077 correspondingly more complicated. Here are excerpts from
1078 @file{lisp-mode.el} that illustrate how these modes are written.
1079
1080 @cindex syntax table example
1081 @smallexample
1082 @group
1083 ;; @r{Create mode-specific table variables.}
1084 (defvar lisp-mode-syntax-table nil "")
1085 (defvar lisp-mode-abbrev-table nil "")
1086 @end group
1087
1088 @group
1089 (defvar emacs-lisp-mode-syntax-table
1090 (let ((table (make-syntax-table)))
1091 (let ((i 0))
1092 @end group
1093
1094 @group
1095 ;; @r{Set syntax of chars up to @samp{0} to say they are}
1096 ;; @r{part of symbol names but not words.}
1097 ;; @r{(The digit @samp{0} is @code{48} in the @acronym{ASCII} character set.)}
1098 (while (< i ?0)
1099 (modify-syntax-entry i "_ " table)
1100 (setq i (1+ i)))
1101 ;; @r{@dots{} similar code follows for other character ranges.}
1102 @end group
1103 @group
1104 ;; @r{Then set the syntax codes for characters that are special in Lisp.}
1105 (modify-syntax-entry ? " " table)
1106 (modify-syntax-entry ?\t " " table)
1107 (modify-syntax-entry ?\f " " table)
1108 (modify-syntax-entry ?\n "> " table)
1109 @end group
1110 @group
1111 ;; @r{Give CR the same syntax as newline, for selective-display.}
1112 (modify-syntax-entry ?\^m "> " table)
1113 (modify-syntax-entry ?\; "< " table)
1114 (modify-syntax-entry ?` "' " table)
1115 (modify-syntax-entry ?' "' " table)
1116 (modify-syntax-entry ?, "' " table)
1117 @end group
1118 @group
1119 ;; @r{@dots{}likewise for many other characters@dots{}}
1120 (modify-syntax-entry ?\( "() " table)
1121 (modify-syntax-entry ?\) ")( " table)
1122 (modify-syntax-entry ?\[ "(] " table)
1123 (modify-syntax-entry ?\] ")[ " table))
1124 table))
1125 @end group
1126 @group
1127 ;; @r{Create an abbrev table for lisp-mode.}
1128 (define-abbrev-table 'lisp-mode-abbrev-table ())
1129 @end group
1130 @end smallexample
1131
1132 The three modes for Lisp share much of their code. For instance,
1133 each calls the following function to set various variables:
1134
1135 @smallexample
1136 @group
1137 (defun lisp-mode-variables (lisp-syntax)
1138 (when lisp-syntax
1139 (set-syntax-table lisp-mode-syntax-table))
1140 (setq local-abbrev-table lisp-mode-abbrev-table)
1141 @dots{}
1142 @end group
1143 @end smallexample
1144
1145 In Lisp and most programming languages, we want the paragraph
1146 commands to treat only blank lines as paragraph separators. And the
1147 modes should understand the Lisp conventions for comments. The rest of
1148 @code{lisp-mode-variables} sets this up:
1149
1150 @smallexample
1151 @group
1152 (set (make-local-variable 'paragraph-start)
1153 (concat page-delimiter "\\|$" ))
1154 (set (make-local-variable 'paragraph-separate)
1155 paragraph-start)
1156 @dots{}
1157 @end group
1158 @group
1159 (set (make-local-variable 'comment-indent-function)
1160 'lisp-comment-indent))
1161 @dots{}
1162 @end group
1163 @end smallexample
1164
1165 Each of the different Lisp modes has a slightly different keymap. For
1166 example, Lisp mode binds @kbd{C-c C-z} to @code{run-lisp}, but the other
1167 Lisp modes do not. However, all Lisp modes have some commands in
1168 common. The following code sets up the common commands:
1169
1170 @smallexample
1171 @group
1172 (defvar shared-lisp-mode-map
1173 (let ((map (make-sparse-keymap)))
1174 (define-key shared-lisp-mode-map "\e\C-q" 'indent-sexp)
1175 (define-key shared-lisp-mode-map "\177"
1176 'backward-delete-char-untabify)
1177 map)
1178 "Keymap for commands shared by all sorts of Lisp modes.")
1179 @end group
1180 @end smallexample
1181
1182 @noindent
1183 And here is the code to set up the keymap for Lisp mode:
1184
1185 @smallexample
1186 @group
1187 (defvar lisp-mode-map
1188 (let ((map (make-sparse-keymap)))
1189 (set-keymap-parent map shared-lisp-mode-map)
1190 (define-key map "\e\C-x" 'lisp-eval-defun)
1191 (define-key map "\C-c\C-z" 'run-lisp)
1192 map)
1193 "Keymap for ordinary Lisp mode...")
1194 @end group
1195 @end smallexample
1196
1197 Finally, here is the complete major mode function definition for
1198 Lisp mode.
1199
1200 @smallexample
1201 @group
1202 (defun lisp-mode ()
1203 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
1204 Commands:
1205 Delete converts tabs to spaces as it moves back.
1206 Blank lines separate paragraphs. Semicolons start comments.
1207 \\@{lisp-mode-map@}
1208 Note that `run-lisp' may be used either to start an inferior Lisp job
1209 or to switch back to an existing one.
1210 @end group
1211
1212 @group
1213 Entry to this mode calls the value of `lisp-mode-hook'
1214 if that value is non-nil."
1215 (interactive)
1216 (kill-all-local-variables)
1217 @end group
1218 @group
1219 (use-local-map lisp-mode-map) ; @r{Select the mode's keymap.}
1220 (setq major-mode 'lisp-mode) ; @r{This is how @code{describe-mode}}
1221 ; @r{finds out what to describe.}
1222 (setq mode-name "Lisp") ; @r{This goes into the mode line.}
1223 (lisp-mode-variables t) ; @r{This defines various variables.}
1224 (set (make-local-variable 'comment-start-skip)
1225 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
1226 (set (make-local-variable 'font-lock-keywords-case-fold-search) t)
1227 @end group
1228 @group
1229 (setq imenu-case-fold-search t)
1230 (set-syntax-table lisp-mode-syntax-table)
1231 (run-mode-hooks 'lisp-mode-hook)) ; @r{This permits the user to use a}
1232 ; @r{hook to customize the mode.}
1233 @end group
1234 @end smallexample
1235
1236 @node Minor Modes
1237 @section Minor Modes
1238 @cindex minor mode
1239
1240 A @dfn{minor mode} provides features that users may enable or disable
1241 independently of the choice of major mode. Minor modes can be enabled
1242 individually or in combination. Minor modes would be better named
1243 ``generally available, optional feature modes,'' except that such a name
1244 would be unwieldy.
1245
1246 A minor mode is not usually meant as a variation of a single major mode.
1247 Usually they are general and can apply to many major modes. For
1248 example, Auto Fill mode works with any major mode that permits text
1249 insertion. To be general, a minor mode must be effectively independent
1250 of the things major modes do.
1251
1252 A minor mode is often much more difficult to implement than a major
1253 mode. One reason is that you should be able to activate and deactivate
1254 minor modes in any order. A minor mode should be able to have its
1255 desired effect regardless of the major mode and regardless of the other
1256 minor modes in effect.
1257
1258 Often the biggest problem in implementing a minor mode is finding a
1259 way to insert the necessary hook into the rest of Emacs. Minor mode
1260 keymaps make this easier than it used to be.
1261
1262 @defvar minor-mode-list
1263 The value of this variable is a list of all minor mode commands.
1264 @end defvar
1265
1266 @menu
1267 * Minor Mode Conventions:: Tips for writing a minor mode.
1268 * Keymaps and Minor Modes:: How a minor mode can have its own keymap.
1269 * Defining Minor Modes:: A convenient facility for defining minor modes.
1270 @end menu
1271
1272 @node Minor Mode Conventions
1273 @subsection Conventions for Writing Minor Modes
1274 @cindex minor mode conventions
1275 @cindex conventions for writing minor modes
1276
1277 There are conventions for writing minor modes just as there are for
1278 major modes. Several of the major mode conventions apply to minor
1279 modes as well: those regarding the name of the mode initialization
1280 function, the names of global symbols, the use of a hook at the end of
1281 the initialization function, and the use of keymaps and other tables.
1282
1283 In addition, there are several conventions that are specific to
1284 minor modes. (The easiest way to follow all the conventions is to use
1285 the macro @code{define-minor-mode}; @ref{Defining Minor Modes}.)
1286
1287 @itemize @bullet
1288 @item
1289 @cindex mode variable
1290 Make a variable whose name ends in @samp{-mode} to control the minor
1291 mode. We call this the @dfn{mode variable}. The minor mode command
1292 should set this variable (@code{nil} to disable; anything else to
1293 enable).
1294
1295 If possible, implement the mode so that setting the variable
1296 automatically enables or disables the mode. Then the minor mode command
1297 does not need to do anything except set the variable.
1298
1299 This variable is used in conjunction with the @code{minor-mode-alist} to
1300 display the minor mode name in the mode line. It can also enable
1301 or disable a minor mode keymap. Individual commands or hooks can also
1302 check the variable's value.
1303
1304 If you want the minor mode to be enabled separately in each buffer,
1305 make the variable buffer-local.
1306
1307 @item
1308 Define a command whose name is the same as the mode variable.
1309 Its job is to enable and disable the mode by setting the variable.
1310
1311 The command should accept one optional argument. If the argument is
1312 @code{nil}, it should toggle the mode (turn it on if it is off, and
1313 off if it is on). It should turn the mode on if the argument is a
1314 positive integer, the symbol @code{t}, or a list whose @sc{car} is one
1315 of those. It should turn the mode off if the argument is a negative
1316 integer or zero, the symbol @code{-}, or a list whose @sc{car} is a
1317 negative integer or zero. The meaning of other arguments is not
1318 specified.
1319
1320 Here is an example taken from the definition of @code{transient-mark-mode}.
1321 It shows the use of @code{transient-mark-mode} as a variable that enables or
1322 disables the mode's behavior, and also shows the proper way to toggle,
1323 enable or disable the minor mode based on the raw prefix argument value.
1324
1325 @smallexample
1326 @group
1327 (setq transient-mark-mode
1328 (if (null arg) (not transient-mark-mode)
1329 (> (prefix-numeric-value arg) 0)))
1330 @end group
1331 @end smallexample
1332
1333 @item
1334 Add an element to @code{minor-mode-alist} for each minor mode
1335 (@pxref{Definition of minor-mode-alist}), if you want to indicate the
1336 minor mode in the mode line. This element should be a list of the
1337 following form:
1338
1339 @smallexample
1340 (@var{mode-variable} @var{string})
1341 @end smallexample
1342
1343 Here @var{mode-variable} is the variable that controls enabling of the
1344 minor mode, and @var{string} is a short string, starting with a space,
1345 to represent the mode in the mode line. These strings must be short so
1346 that there is room for several of them at once.
1347
1348 When you add an element to @code{minor-mode-alist}, use @code{assq} to
1349 check for an existing element, to avoid duplication. For example:
1350
1351 @smallexample
1352 @group
1353 (unless (assq 'leif-mode minor-mode-alist)
1354 (setq minor-mode-alist
1355 (cons '(leif-mode " Leif") minor-mode-alist)))
1356 @end group
1357 @end smallexample
1358
1359 @noindent
1360 or like this, using @code{add-to-list} (@pxref{List Variables}):
1361
1362 @smallexample
1363 @group
1364 (add-to-list 'minor-mode-alist '(leif-mode " Leif"))
1365 @end group
1366 @end smallexample
1367 @end itemize
1368
1369 Global minor modes distributed with Emacs should if possible support
1370 enabling and disabling via Custom (@pxref{Customization}). To do this,
1371 the first step is to define the mode variable with @code{defcustom}, and
1372 specify @code{:type 'boolean}.
1373
1374 If just setting the variable is not sufficient to enable the mode, you
1375 should also specify a @code{:set} method which enables the mode by
1376 invoking the mode command. Note in the variable's documentation string that
1377 setting the variable other than via Custom may not take effect.
1378
1379 Also mark the definition with an autoload cookie (@pxref{autoload cookie}),
1380 and specify a @code{:require} so that customizing the variable will load
1381 the library that defines the mode. This will copy suitable definitions
1382 into @file{loaddefs.el} so that users can use @code{customize-option} to
1383 enable the mode. For example:
1384
1385 @smallexample
1386 @group
1387
1388 ;;;###autoload
1389 (defcustom msb-mode nil
1390 "Toggle msb-mode.
1391 Setting this variable directly does not take effect;
1392 use either \\[customize] or the function `msb-mode'."
1393 :set 'custom-set-minor-mode
1394 :initialize 'custom-initialize-default
1395 :version "20.4"
1396 :type 'boolean
1397 :group 'msb
1398 :require 'msb)
1399 @end group
1400 @end smallexample
1401
1402 @node Keymaps and Minor Modes
1403 @subsection Keymaps and Minor Modes
1404
1405 Each minor mode can have its own keymap, which is active when the mode
1406 is enabled. To set up a keymap for a minor mode, add an element to the
1407 alist @code{minor-mode-map-alist}. @xref{Definition of minor-mode-map-alist}.
1408
1409 @cindex @code{self-insert-command}, minor modes
1410 One use of minor mode keymaps is to modify the behavior of certain
1411 self-inserting characters so that they do something else as well as
1412 self-insert. In general, this is the only way to do that, since the
1413 facilities for customizing @code{self-insert-command} are limited to
1414 special cases (designed for abbrevs and Auto Fill mode). (Do not try
1415 substituting your own definition of @code{self-insert-command} for the
1416 standard one. The editor command loop handles this function specially.)
1417
1418 The key sequences bound in a minor mode should consist of @kbd{C-c}
1419 followed by one of @kbd{.,/?`'"[]\|~!#$%^&*()-_+=}. (The other
1420 punctuation characters are reserved for major modes.)
1421
1422 @node Defining Minor Modes
1423 @subsection Defining Minor Modes
1424
1425 The macro @code{define-minor-mode} offers a convenient way of
1426 implementing a mode in one self-contained definition.
1427
1428 @defmac define-minor-mode mode doc [init-value [lighter [keymap]]] keyword-args@dots{} body@dots{}
1429 This macro defines a new minor mode whose name is @var{mode} (a
1430 symbol). It defines a command named @var{mode} to toggle the minor
1431 mode, with @var{doc} as its documentation string. It also defines a
1432 variable named @var{mode}, which is set to @code{t} or @code{nil} by
1433 enabling or disabling the mode. The variable is initialized to
1434 @var{init-value}. Except in unusual circumstances (see below), this
1435 value must be @code{nil}.
1436
1437 The string @var{lighter} says what to display in the mode line
1438 when the mode is enabled; if it is @code{nil}, the mode is not displayed
1439 in the mode line.
1440
1441 The optional argument @var{keymap} specifies the keymap for the minor
1442 mode. If non-@code{nil}, it should be a variable name (whose value is
1443 a keymap), a keymap, or an alist of the form
1444
1445 @example
1446 (@var{key-sequence} . @var{definition})
1447 @end example
1448
1449 @noindent
1450 where each @var{key-sequence} and @var{definition} are arguments
1451 suitable for passing to @code{define-key} (@pxref{Changing Key
1452 Bindings}). If @var{keymap} is a keymap or an alist, this also
1453 defines the variable @code{@var{mode}-map}.
1454
1455 The above three arguments @var{init-value}, @var{lighter}, and
1456 @var{keymap} can be (partially) omitted when @var{keyword-args} are
1457 used. The @var{keyword-args} consist of keywords followed by
1458 corresponding values. A few keywords have special meanings:
1459
1460 @table @code
1461 @item :group @var{group}
1462 Custom group name to use in all generated @code{defcustom} forms.
1463 Defaults to @var{mode} without the possible trailing @samp{-mode}.
1464 @strong{Warning:} don't use this default group name unless you have
1465 written a @code{defgroup} to define that group properly. @xref{Group
1466 Definitions}.
1467
1468 @item :global @var{global}
1469 If non-@code{nil}, this specifies that the minor mode should be global
1470 rather than buffer-local. It defaults to @code{nil}.
1471
1472 One of the effects of making a minor mode global is that the
1473 @var{mode} variable becomes a customization variable. Toggling it
1474 through the Custom interface turns the mode on and off, and its value
1475 can be saved for future Emacs sessions (@pxref{Saving
1476 Customizations,,, emacs, The GNU Emacs Manual}. For the saved
1477 variable to work, you should ensure that the @code{define-minor-mode}
1478 form is evaluated each time Emacs starts; for packages that are not
1479 part of Emacs, the easiest way to do this is to specify a
1480 @code{:require} keyword.
1481
1482 @item :init-value @var{init-value}
1483 This is equivalent to specifying @var{init-value} positionally.
1484
1485 @item :lighter @var{lighter}
1486 This is equivalent to specifying @var{lighter} positionally.
1487
1488 @item :keymap @var{keymap}
1489 This is equivalent to specifying @var{keymap} positionally.
1490 @end table
1491
1492 Any other keyword arguments are passed directly to the
1493 @code{defcustom} generated for the variable @var{mode}.
1494
1495 The command named @var{mode} first performs the standard actions such
1496 as setting the variable named @var{mode} and then executes the
1497 @var{body} forms, if any. It finishes by running the mode hook
1498 variable @code{@var{mode}-hook}.
1499 @end defmac
1500
1501 The initial value must be @code{nil} except in cases where (1) the
1502 mode is preloaded in Emacs, or (2) it is painless for loading to
1503 enable the mode even though the user did not request it. For
1504 instance, if the mode has no effect unless something else is enabled,
1505 and will always be loaded by that time, enabling it by default is
1506 harmless. But these are unusual circumstances. Normally, the
1507 initial value must be @code{nil}.
1508
1509 @findex easy-mmode-define-minor-mode
1510 The name @code{easy-mmode-define-minor-mode} is an alias
1511 for this macro.
1512
1513 Here is an example of using @code{define-minor-mode}:
1514
1515 @smallexample
1516 (define-minor-mode hungry-mode
1517 "Toggle Hungry mode.
1518 With no argument, this command toggles the mode.
1519 Non-null prefix argument turns on the mode.
1520 Null prefix argument turns off the mode.
1521
1522 When Hungry mode is enabled, the control delete key
1523 gobbles all preceding whitespace except the last.
1524 See the command \\[hungry-electric-delete]."
1525 ;; The initial value.
1526 nil
1527 ;; The indicator for the mode line.
1528 " Hungry"
1529 ;; The minor mode bindings.
1530 '(([C-backspace] . hungry-electric-delete))
1531 :group 'hunger)
1532 @end smallexample
1533
1534 @noindent
1535 This defines a minor mode named ``Hungry mode,'' a command named
1536 @code{hungry-mode} to toggle it, a variable named @code{hungry-mode}
1537 which indicates whether the mode is enabled, and a variable named
1538 @code{hungry-mode-map} which holds the keymap that is active when the
1539 mode is enabled. It initializes the keymap with a key binding for
1540 @kbd{C-@key{DEL}}. It puts the variable @code{hungry-mode} into
1541 custom group @code{hunger}. There are no @var{body} forms---many
1542 minor modes don't need any.
1543
1544 Here's an equivalent way to write it:
1545
1546 @smallexample
1547 (define-minor-mode hungry-mode
1548 "Toggle Hungry mode.
1549 With no argument, this command toggles the mode.
1550 Non-null prefix argument turns on the mode.
1551 Null prefix argument turns off the mode.
1552
1553 When Hungry mode is enabled, the control delete key
1554 gobbles all preceding whitespace except the last.
1555 See the command \\[hungry-electric-delete]."
1556 ;; The initial value.
1557 :init-value nil
1558 ;; The indicator for the mode line.
1559 :lighter " Hungry"
1560 ;; The minor mode bindings.
1561 :keymap
1562 '(([C-backspace] . hungry-electric-delete)
1563 ([C-M-backspace]
1564 . (lambda ()
1565 (interactive)
1566 (hungry-electric-delete t))))
1567 :group 'hunger)
1568 @end smallexample
1569
1570 @defmac define-globalized-minor-mode global-mode mode turn-on keyword-args@dots{}
1571 This defines a global toggle named @var{global-mode} whose meaning is
1572 to enable or disable the buffer-local minor mode @var{mode} in all
1573 buffers. To turn on the minor mode in a buffer, it uses the function
1574 @var{turn-on}; to turn off the minor mode, it calls @code{mode} with
1575 @minus{}1 as argument.
1576
1577 Globally enabling the mode also affects buffers subsequently created
1578 by visiting files, and buffers that use a major mode other than
1579 Fundamental mode; but it does not detect the creation of a new buffer
1580 in Fundamental mode.
1581
1582 This defines the customization option @var{global-mode} (@pxref{Customization}),
1583 which can be toggled in the Custom interface to turn the minor mode on
1584 and off. As with @code{define-minor-mode}, you should ensure that the
1585 @code{define-globalized-minor-mode} form is evaluated each time Emacs
1586 starts, for example by providing a @code{:require} keyword.
1587
1588 Use @code{:group @var{group}} in @var{keyword-args} to specify the
1589 custom group for the mode variable of the global minor mode.
1590 @end defmac
1591
1592 @node Mode Line Format
1593 @section Mode-Line Format
1594 @cindex mode line
1595
1596 Each Emacs window (aside from minibuffer windows) typically has a mode
1597 line at the bottom, which displays status information about the buffer
1598 displayed in the window. The mode line contains information about the
1599 buffer, such as its name, associated file, depth of recursive editing,
1600 and major and minor modes. A window can also have a @dfn{header
1601 line}, which is much like the mode line but appears at the top of the
1602 window.
1603
1604 This section describes how to control the contents of the mode line
1605 and header line. We include it in this chapter because much of the
1606 information displayed in the mode line relates to the enabled major and
1607 minor modes.
1608
1609 @menu
1610 * Base: Mode Line Basics. Basic ideas of mode line control.
1611 * Data: Mode Line Data. The data structure that controls the mode line.
1612 * Top: Mode Line Top. The top level variable, mode-line-format.
1613 * Mode Line Variables:: Variables used in that data structure.
1614 * %-Constructs:: Putting information into a mode line.
1615 * Properties in Mode:: Using text properties in the mode line.
1616 * Header Lines:: Like a mode line, but at the top.
1617 * Emulating Mode Line:: Formatting text as the mode line would.
1618 @end menu
1619
1620 @node Mode Line Basics
1621 @subsection Mode Line Basics
1622
1623 @code{mode-line-format} is a buffer-local variable that holds a
1624 @dfn{mode line construct}, a kind of template, which controls what is
1625 displayed on the mode line of the current buffer. The value of
1626 @code{header-line-format} specifies the buffer's header line in the
1627 same way. All windows for the same buffer use the same
1628 @code{mode-line-format} and @code{header-line-format}.
1629
1630 For efficiency, Emacs does not continuously recompute the mode
1631 line and header line of a window. It does so when circumstances
1632 appear to call for it---for instance, if you change the window
1633 configuration, switch buffers, narrow or widen the buffer, scroll, or
1634 change the buffer's modification status. If you modify any of the
1635 variables referenced by @code{mode-line-format} (@pxref{Mode Line
1636 Variables}), or any other variables and data structures that affect
1637 how text is displayed (@pxref{Display}), you may want to force an
1638 update of the mode line so as to display the new information or
1639 display it in the new way.
1640
1641 @defun force-mode-line-update &optional all
1642 Force redisplay of the current buffer's mode line and header line.
1643 The next redisplay will update the mode line and header line based on
1644 the latest values of all relevant variables. With optional
1645 non-@code{nil} @var{all}, force redisplay of all mode lines and header
1646 lines.
1647
1648 This function also forces recomputation of the menu bar menus
1649 and the frame title.
1650 @end defun
1651
1652 The selected window's mode line is usually displayed in a different
1653 color using the face @code{mode-line}. Other windows' mode lines
1654 appear in the face @code{mode-line-inactive} instead. @xref{Faces}.
1655
1656 @node Mode Line Data
1657 @subsection The Data Structure of the Mode Line
1658 @cindex mode-line construct
1659
1660 The mode-line contents are controlled by a data structure called a
1661 @dfn{mode-line construct}, made up of lists, strings, symbols, and
1662 numbers kept in buffer-local variables. Each data type has a specific
1663 meaning for the mode-line appearance, as described below. The same
1664 data structure is used for constructing frame titles (@pxref{Frame
1665 Titles}) and header lines (@pxref{Header Lines}).
1666
1667 A mode-line construct may be as simple as a fixed string of text,
1668 but it usually specifies how to combine fixed strings with variables'
1669 values to construct the text. Many of these variables are themselves
1670 defined to have mode-line constructs as their values.
1671
1672 Here are the meanings of various data types as mode-line constructs:
1673
1674 @table @code
1675 @cindex percent symbol in mode line
1676 @item @var{string}
1677 A string as a mode-line construct appears verbatim except for
1678 @dfn{@code{%}-constructs} in it. These stand for substitution of
1679 other data; see @ref{%-Constructs}.
1680
1681 If parts of the string have @code{face} properties, they control
1682 display of the text just as they would text in the buffer. Any
1683 characters which have no @code{face} properties are displayed, by
1684 default, in the face @code{mode-line} or @code{mode-line-inactive}
1685 (@pxref{Standard Faces,,, emacs, The GNU Emacs Manual}). The
1686 @code{help-echo} and @code{local-map} properties in @var{string} have
1687 special meanings. @xref{Properties in Mode}.
1688
1689 @item @var{symbol}
1690 A symbol as a mode-line construct stands for its value. The value of
1691 @var{symbol} is used as a mode-line construct, in place of @var{symbol}.
1692 However, the symbols @code{t} and @code{nil} are ignored, as is any
1693 symbol whose value is void.
1694
1695 There is one exception: if the value of @var{symbol} is a string, it is
1696 displayed verbatim: the @code{%}-constructs are not recognized.
1697
1698 Unless @var{symbol} is marked as ``risky'' (i.e., it has a
1699 non-@code{nil} @code{risky-local-variable} property), all text
1700 properties specified in @var{symbol}'s value are ignored. This
1701 includes the text properties of strings in @var{symbol}'s value, as
1702 well as all @code{:eval} and @code{:propertize} forms in it. (The
1703 reason for this is security: non-risky variables could be set
1704 automatically from file variables without prompting the user.)
1705
1706 @item (@var{string} @var{rest}@dots{})
1707 @itemx (@var{list} @var{rest}@dots{})
1708 A list whose first element is a string or list means to process all the
1709 elements recursively and concatenate the results. This is the most
1710 common form of mode-line construct.
1711
1712 @item (:eval @var{form})
1713 A list whose first element is the symbol @code{:eval} says to evaluate
1714 @var{form}, and use the result as a string to display. Make sure this
1715 evaluation cannot load any files, as doing so could cause infinite
1716 recursion.
1717
1718 @item (:propertize @var{elt} @var{props}@dots{})
1719 A list whose first element is the symbol @code{:propertize} says to
1720 process the mode-line construct @var{elt} recursively, then add the text
1721 properties specified by @var{props} to the result. The argument
1722 @var{props} should consist of zero or more pairs @var{text-property}
1723 @var{value}. (This feature is new as of Emacs 22.1.)
1724
1725 @item (@var{symbol} @var{then} @var{else})
1726 A list whose first element is a symbol that is not a keyword specifies
1727 a conditional. Its meaning depends on the value of @var{symbol}. If
1728 @var{symbol} has a non-@code{nil} value, the second element,
1729 @var{then}, is processed recursively as a mode-line element.
1730 Otherwise, the third element, @var{else}, is processed recursively.
1731 You may omit @var{else}; then the mode-line element displays nothing
1732 if the value of @var{symbol} is @code{nil} or void.
1733
1734 @item (@var{width} @var{rest}@dots{})
1735 A list whose first element is an integer specifies truncation or
1736 padding of the results of @var{rest}. The remaining elements
1737 @var{rest} are processed recursively as mode-line constructs and
1738 concatenated together. When @var{width} is positive, the result is
1739 space filled on the right if its width is less than @var{width}. When
1740 @var{width} is negative, the result is truncated on the right to
1741 @minus{}@var{width} columns if its width exceeds @minus{}@var{width}.
1742
1743 For example, the usual way to show what percentage of a buffer is above
1744 the top of the window is to use a list like this: @code{(-3 "%p")}.
1745 @end table
1746
1747 @node Mode Line Top
1748 @subsection The Top Level of Mode Line Control
1749
1750 The variable in overall control of the mode line is
1751 @code{mode-line-format}.
1752
1753 @defopt mode-line-format
1754 The value of this variable is a mode-line construct that controls the
1755 contents of the mode-line. It is always buffer-local in all buffers.
1756
1757 If you set this variable to @code{nil} in a buffer, that buffer does
1758 not have a mode line. (A window that is just one line tall never
1759 displays a mode line.)
1760 @end defopt
1761
1762 The default value of @code{mode-line-format} is designed to use the
1763 values of other variables such as @code{mode-line-position} and
1764 @code{mode-line-modes} (which in turn incorporates the values of the
1765 variables @code{mode-name} and @code{minor-mode-alist}). Very few
1766 modes need to alter @code{mode-line-format} itself. For most
1767 purposes, it is sufficient to alter some of the variables that
1768 @code{mode-line-format} either directly or indirectly refers to.
1769
1770 If you do alter @code{mode-line-format} itself, the new value should
1771 use the same variables that appear in the default value (@pxref{Mode
1772 Line Variables}), rather than duplicating their contents or displaying
1773 the information in another fashion. This way, customizations made by
1774 the user or by Lisp programs (such as @code{display-time} and major
1775 modes) via changes to those variables remain effective.
1776
1777 Here is an example of a @code{mode-line-format} that might be
1778 useful for @code{shell-mode}, since it contains the host name and default
1779 directory.
1780
1781 @example
1782 @group
1783 (setq mode-line-format
1784 (list "-"
1785 'mode-line-mule-info
1786 'mode-line-modified
1787 'mode-line-frame-identification
1788 "%b--"
1789 @end group
1790 @group
1791 ;; @r{Note that this is evaluated while making the list.}
1792 ;; @r{It makes a mode-line construct which is just a string.}
1793 (getenv "HOST")
1794 @end group
1795 ":"
1796 'default-directory
1797 " "
1798 'global-mode-string
1799 " %[("
1800 '(:eval (mode-line-mode-name))
1801 'mode-line-process
1802 'minor-mode-alist
1803 "%n"
1804 ")%]--"
1805 @group
1806 '(which-func-mode ("" which-func-format "--"))
1807 '(line-number-mode "L%l--")
1808 '(column-number-mode "C%c--")
1809 '(-3 "%p")
1810 "-%-"))
1811 @end group
1812 @end example
1813
1814 @noindent
1815 (The variables @code{line-number-mode}, @code{column-number-mode}
1816 and @code{which-func-mode} enable particular minor modes; as usual,
1817 these variable names are also the minor mode command names.)
1818
1819 @node Mode Line Variables
1820 @subsection Variables Used in the Mode Line
1821
1822 This section describes variables incorporated by the standard value
1823 of @code{mode-line-format} into the text of the mode line. There is
1824 nothing inherently special about these variables; any other variables
1825 could have the same effects on the mode line if
1826 @code{mode-line-format}'s value were changed to use them. However,
1827 various parts of Emacs set these variables on the understanding that
1828 they will control parts of the mode line; therefore, practically
1829 speaking, it is essential for the mode line to use them.
1830
1831 @defvar mode-line-mule-info
1832 This variable holds the value of the mode-line construct that displays
1833 information about the language environment, buffer coding system, and
1834 current input method. @xref{Non-ASCII Characters}.
1835 @end defvar
1836
1837 @defvar mode-line-modified
1838 This variable holds the value of the mode-line construct that displays
1839 whether the current buffer is modified. Its default value displays
1840 @samp{**} if the buffer is modified, @samp{--} if the buffer is not
1841 modified, @samp{%%} if the buffer is read only, and @samp{%*} if the
1842 buffer is read only and modified.
1843
1844 Changing this variable does not force an update of the mode line.
1845 @end defvar
1846
1847 @defvar mode-line-frame-identification
1848 This variable identifies the current frame. Its default value
1849 displays @code{" "} if you are using a window system which can show
1850 multiple frames, or @code{"-%F "} on an ordinary terminal which shows
1851 only one frame at a time.
1852 @end defvar
1853
1854 @defvar mode-line-buffer-identification
1855 This variable identifies the buffer being displayed in the window.
1856 Its default value displays the buffer name, padded with spaces to at
1857 least 12 columns.
1858 @end defvar
1859
1860 @defopt mode-line-position
1861 This variable indicates the position in the buffer. Its default value
1862 displays the buffer percentage and, optionally, the buffer size, the
1863 line number and the column number.
1864 @end defopt
1865
1866 @defvar vc-mode
1867 The variable @code{vc-mode}, buffer-local in each buffer, records
1868 whether the buffer's visited file is maintained with version control,
1869 and, if so, which kind. Its value is a string that appears in the mode
1870 line, or @code{nil} for no version control.
1871 @end defvar
1872
1873 @defopt mode-line-modes
1874 This variable displays the buffer's major and minor modes. Its
1875 default value also displays the recursive editing level, information
1876 on the process status, and whether narrowing is in effect.
1877 @end defopt
1878
1879 @defopt mode-line-remote
1880 This variable is used to show whether @code{default-directory} for the
1881 current buffer is remote.
1882 @end defopt
1883
1884 @defopt mode-line-client
1885 This variable is used to identify @code{emacsclient} frames.
1886 @end defopt
1887
1888 The following three variables are used in @code{mode-line-modes}:
1889
1890 @defvar mode-name
1891 This buffer-local variable holds the ``pretty'' name of the current
1892 buffer's major mode. Each major mode should set this variable so that
1893 the mode name will appear in the mode line. The value does not have
1894 to be a string, but can use any of the data types valid in a mode-line
1895 construct (@pxref{Mode Line Data}). To compute the string that will
1896 identify the mode name in the mode line, use @code{format-mode-line}
1897 (@pxref{Emulating Mode Line}).
1898 @end defvar
1899
1900 @defvar mode-line-process
1901 This buffer-local variable contains the mode-line information on process
1902 status in modes used for communicating with subprocesses. It is
1903 displayed immediately following the major mode name, with no intervening
1904 space. For example, its value in the @samp{*shell*} buffer is
1905 @code{(":%s")}, which allows the shell to display its status along
1906 with the major mode as: @samp{(Shell:run)}. Normally this variable
1907 is @code{nil}.
1908 @end defvar
1909
1910 @defvar minor-mode-alist
1911 @anchor{Definition of minor-mode-alist}
1912 This variable holds an association list whose elements specify how the
1913 mode line should indicate that a minor mode is active. Each element of
1914 the @code{minor-mode-alist} should be a two-element list:
1915
1916 @example
1917 (@var{minor-mode-variable} @var{mode-line-string})
1918 @end example
1919
1920 More generally, @var{mode-line-string} can be any mode-line spec. It
1921 appears in the mode line when the value of @var{minor-mode-variable}
1922 is non-@code{nil}, and not otherwise. These strings should begin with
1923 spaces so that they don't run together. Conventionally, the
1924 @var{minor-mode-variable} for a specific mode is set to a
1925 non-@code{nil} value when that minor mode is activated.
1926
1927 @code{minor-mode-alist} itself is not buffer-local. Each variable
1928 mentioned in the alist should be buffer-local if its minor mode can be
1929 enabled separately in each buffer.
1930 @end defvar
1931
1932 @defvar global-mode-string
1933 This variable holds a mode-line spec that, by default, appears in the
1934 mode line just after the @code{which-func-mode} minor mode if set,
1935 else after @code{mode-line-modes}. The command @code{display-time}
1936 sets @code{global-mode-string} to refer to the variable
1937 @code{display-time-string}, which holds a string containing the time
1938 and load information.
1939
1940 The @samp{%M} construct substitutes the value of
1941 @code{global-mode-string}, but that is obsolete, since the variable is
1942 included in the mode line from @code{mode-line-format}.
1943 @end defvar
1944
1945 Here is a simplified version of the default value of
1946 @code{mode-line-format}. The real default value also
1947 specifies addition of text properties.
1948
1949 @example
1950 @group
1951 ("-"
1952 mode-line-mule-info
1953 mode-line-modified
1954 mode-line-frame-identification
1955 mode-line-buffer-identification
1956 @end group
1957 " "
1958 mode-line-position
1959 (vc-mode vc-mode)
1960 " "
1961 @group
1962 mode-line-modes
1963 (which-func-mode ("" which-func-format "--"))
1964 (global-mode-string ("--" global-mode-string))
1965 "-%-")
1966 @end group
1967 @end example
1968
1969 @node %-Constructs
1970 @subsection @code{%}-Constructs in the Mode Line
1971
1972 Strings used as mode-line constructs can use certain
1973 @code{%}-constructs to substitute various kinds of data. Here is a
1974 list of the defined @code{%}-constructs, and what they mean. In any
1975 construct except @samp{%%}, you can add a decimal integer after the
1976 @samp{%} to specify a minimum field width. If the width is less, the
1977 field is padded with spaces to the right.
1978
1979 @table @code
1980 @item %b
1981 The current buffer name, obtained with the @code{buffer-name} function.
1982 @xref{Buffer Names}.
1983
1984 @item %c
1985 The current column number of point.
1986
1987 @item %e
1988 When Emacs is nearly out of memory for Lisp objects, a brief message
1989 saying so. Otherwise, this is empty.
1990
1991 @item %f
1992 The visited file name, obtained with the @code{buffer-file-name}
1993 function. @xref{Buffer File Name}.
1994
1995 @item %F
1996 The title (only on a window system) or the name of the selected frame.
1997 @xref{Basic Parameters}.
1998
1999 @item %i
2000 The size of the accessible part of the current buffer; basically
2001 @code{(- (point-max) (point-min))}.
2002
2003 @item %I
2004 Like @samp{%i}, but the size is printed in a more readable way by using
2005 @samp{k} for 10^3, @samp{M} for 10^6, @samp{G} for 10^9, etc., to
2006 abbreviate.
2007
2008 @item %l
2009 The current line number of point, counting within the accessible portion
2010 of the buffer.
2011
2012 @item %n
2013 @samp{Narrow} when narrowing is in effect; nothing otherwise (see
2014 @code{narrow-to-region} in @ref{Narrowing}).
2015
2016 @item %p
2017 The percentage of the buffer text above the @strong{top} of window, or
2018 @samp{Top}, @samp{Bottom} or @samp{All}. Note that the default
2019 mode-line specification truncates this to three characters.
2020
2021 @item %P
2022 The percentage of the buffer text that is above the @strong{bottom} of
2023 the window (which includes the text visible in the window, as well as
2024 the text above the top), plus @samp{Top} if the top of the buffer is
2025 visible on screen; or @samp{Bottom} or @samp{All}.
2026
2027 @item %s
2028 The status of the subprocess belonging to the current buffer, obtained with
2029 @code{process-status}. @xref{Process Information}.
2030
2031 @item %t
2032 Whether the visited file is a text file or a binary file. This is a
2033 meaningful distinction only on certain operating systems (@pxref{MS-DOS
2034 File Types}).
2035
2036 @item %z
2037 The mnemonics of keyboard, terminal, and buffer coding systems.
2038
2039 @item %Z
2040 Like @samp{%z}, but including the end-of-line format.
2041
2042 @item %*
2043 @samp{%} if the buffer is read only (see @code{buffer-read-only}); @*
2044 @samp{*} if the buffer is modified (see @code{buffer-modified-p}); @*
2045 @samp{-} otherwise. @xref{Buffer Modification}.
2046
2047 @item %+
2048 @samp{*} if the buffer is modified (see @code{buffer-modified-p}); @*
2049 @samp{%} if the buffer is read only (see @code{buffer-read-only}); @*
2050 @samp{-} otherwise. This differs from @samp{%*} only for a modified
2051 read-only buffer. @xref{Buffer Modification}.
2052
2053 @item %&
2054 @samp{*} if the buffer is modified, and @samp{-} otherwise.
2055
2056 @item %[
2057 An indication of the depth of recursive editing levels (not counting
2058 minibuffer levels): one @samp{[} for each editing level.
2059 @xref{Recursive Editing}.
2060
2061 @item %]
2062 One @samp{]} for each recursive editing level (not counting minibuffer
2063 levels).
2064
2065 @item %-
2066 Dashes sufficient to fill the remainder of the mode line.
2067
2068 @item %%
2069 The character @samp{%}---this is how to include a literal @samp{%} in a
2070 string in which @code{%}-constructs are allowed.
2071 @end table
2072
2073 The following two @code{%}-constructs are still supported, but they are
2074 obsolete, since you can get the same results with the variables
2075 @code{mode-name} and @code{global-mode-string}.
2076
2077 @table @code
2078 @item %m
2079 The value of @code{mode-name}.
2080
2081 @item %M
2082 The value of @code{global-mode-string}.
2083 @end table
2084
2085 @node Properties in Mode
2086 @subsection Properties in the Mode Line
2087 @cindex text properties in the mode line
2088
2089 Certain text properties are meaningful in the
2090 mode line. The @code{face} property affects the appearance of text; the
2091 @code{help-echo} property associates help strings with the text, and
2092 @code{local-map} can make the text mouse-sensitive.
2093
2094 There are four ways to specify text properties for text in the mode
2095 line:
2096
2097 @enumerate
2098 @item
2099 Put a string with a text property directly into the mode-line data
2100 structure.
2101
2102 @item
2103 Put a text property on a mode-line %-construct such as @samp{%12b}; then
2104 the expansion of the %-construct will have that same text property.
2105
2106 @item
2107 Use a @code{(:propertize @var{elt} @var{props}@dots{})} construct to
2108 give @var{elt} a text property specified by @var{props}.
2109
2110 @item
2111 Use a list containing @code{:eval @var{form}} in the mode-line data
2112 structure, and make @var{form} evaluate to a string that has a text
2113 property.
2114 @end enumerate
2115
2116 You can use the @code{local-map} property to specify a keymap. This
2117 keymap only takes real effect for mouse clicks; binding character keys
2118 and function keys to it has no effect, since it is impossible to move
2119 point into the mode line.
2120
2121 When the mode line refers to a variable which does not have a
2122 non-@code{nil} @code{risky-local-variable} property, any text
2123 properties given or specified within that variable's values are
2124 ignored. This is because such properties could otherwise specify
2125 functions to be called, and those functions could come from file
2126 local variables.
2127
2128 @node Header Lines
2129 @subsection Window Header Lines
2130 @cindex header line (of a window)
2131 @cindex window header line
2132
2133 A window can have a @dfn{header line} at the
2134 top, just as it can have a mode line at the bottom. The header line
2135 feature works just like the mode-line feature, except that it's
2136 controlled by different variables.
2137
2138 @defvar header-line-format
2139 This variable, local in every buffer, specifies how to display the
2140 header line, for windows displaying the buffer. The format of the value
2141 is the same as for @code{mode-line-format} (@pxref{Mode Line Data}).
2142 It is normally @code{nil}, so that ordinary buffers have no header line.
2143 @end defvar
2144
2145 A window that is just one line tall never displays a header line. A
2146 window that is two lines tall cannot display both a mode line and a
2147 header line at once; if it has a mode line, then it does not display a
2148 header line.
2149
2150 @node Emulating Mode Line
2151 @subsection Emulating Mode-Line Formatting
2152
2153 You can use the function @code{format-mode-line} to compute
2154 the text that would appear in a mode line or header line
2155 based on a certain mode-line specification.
2156
2157 @defun format-mode-line format &optional face window buffer
2158 This function formats a line of text according to @var{format} as if it
2159 were generating the mode line for @var{window}, but it also returns the
2160 text as a string. The argument @var{window} defaults to the selected
2161 window. If @var{buffer} is non-@code{nil}, all the information used is
2162 taken from @var{buffer}; by default, it comes from @var{window}'s
2163 buffer.
2164
2165 The value string normally has text properties that correspond to the
2166 faces, keymaps, etc., that the mode line would have. Any character for
2167 which no @code{face} property is specified by @var{format} gets a
2168 default value determined by @var{face}. If @var{face} is @code{t}, that
2169 stands for either @code{mode-line} if @var{window} is selected,
2170 otherwise @code{mode-line-inactive}. If @var{face} is @code{nil} or
2171 omitted, that stands for the default face. If @var{face} is an integer,
2172 the value returned by this function will have no text properties.
2173
2174 You can also specify other valid faces as the value of @var{face}.
2175 If specified, that face provides the @code{face} property for characters
2176 whose face is not specified by @var{format}.
2177
2178 Note that using @code{mode-line}, @code{mode-line-inactive}, or
2179 @code{header-line} as @var{face} will actually redisplay the mode line
2180 or the header line, respectively, using the current definitions of the
2181 corresponding face, in addition to returning the formatted string.
2182 (Other faces do not cause redisplay.)
2183
2184 For example, @code{(format-mode-line header-line-format)} returns the
2185 text that would appear in the selected window's header line (@code{""}
2186 if it has no header line). @code{(format-mode-line header-line-format
2187 'header-line)} returns the same text, with each character
2188 carrying the face that it will have in the header line itself, and also
2189 redraws the header line.
2190 @end defun
2191
2192 @node Imenu
2193 @section Imenu
2194
2195 @cindex Imenu
2196 @dfn{Imenu} is a feature that lets users select a definition or
2197 section in the buffer, from a menu which lists all of them, to go
2198 directly to that location in the buffer. Imenu works by constructing
2199 a buffer index which lists the names and buffer positions of the
2200 definitions, or other named portions of the buffer; then the user can
2201 choose one of them and move point to it. Major modes can add a menu
2202 bar item to use Imenu using @code{imenu-add-to-menubar}.
2203
2204 @defun imenu-add-to-menubar name
2205 This function defines a local menu bar item named @var{name}
2206 to run Imenu.
2207 @end defun
2208
2209 The user-level commands for using Imenu are described in the Emacs
2210 Manual (@pxref{Imenu,, Imenu, emacs, the Emacs Manual}). This section
2211 explains how to customize Imenu's method of finding definitions or
2212 buffer portions for a particular major mode.
2213
2214 The usual and simplest way is to set the variable
2215 @code{imenu-generic-expression}:
2216
2217 @defvar imenu-generic-expression
2218 This variable, if non-@code{nil}, is a list that specifies regular
2219 expressions for finding definitions for Imenu. Simple elements of
2220 @code{imenu-generic-expression} look like this:
2221
2222 @example
2223 (@var{menu-title} @var{regexp} @var{index})
2224 @end example
2225
2226 Here, if @var{menu-title} is non-@code{nil}, it says that the matches
2227 for this element should go in a submenu of the buffer index;
2228 @var{menu-title} itself specifies the name for the submenu. If
2229 @var{menu-title} is @code{nil}, the matches for this element go directly
2230 in the top level of the buffer index.
2231
2232 The second item in the list, @var{regexp}, is a regular expression
2233 (@pxref{Regular Expressions}); anything in the buffer that it matches
2234 is considered a definition, something to mention in the buffer index.
2235 The third item, @var{index}, is a non-negative integer that indicates
2236 which subexpression in @var{regexp} matches the definition's name.
2237
2238 An element can also look like this:
2239
2240 @example
2241 (@var{menu-title} @var{regexp} @var{index} @var{function} @var{arguments}@dots{})
2242 @end example
2243
2244 Each match for this element creates an index item, and when the index
2245 item is selected by the user, it calls @var{function} with arguments
2246 consisting of the item name, the buffer position, and @var{arguments}.
2247
2248 For Emacs Lisp mode, @code{imenu-generic-expression} could look like
2249 this:
2250
2251 @c should probably use imenu-syntax-alist and \\sw rather than [-A-Za-z0-9+]
2252 @example
2253 @group
2254 ((nil "^\\s-*(def\\(un\\|subst\\|macro\\|advice\\)\
2255 \\s-+\\([-A-Za-z0-9+]+\\)" 2)
2256 @end group
2257 @group
2258 ("*Vars*" "^\\s-*(def\\(var\\|const\\)\
2259 \\s-+\\([-A-Za-z0-9+]+\\)" 2)
2260 @end group
2261 @group
2262 ("*Types*"
2263 "^\\s-*\
2264 (def\\(type\\|struct\\|class\\|ine-condition\\)\
2265 \\s-+\\([-A-Za-z0-9+]+\\)" 2))
2266 @end group
2267 @end example
2268
2269 Setting this variable makes it buffer-local in the current buffer.
2270 @end defvar
2271
2272 @defvar imenu-case-fold-search
2273 This variable controls whether matching against the regular
2274 expressions in the value of @code{imenu-generic-expression} is
2275 case-sensitive: @code{t}, the default, means matching should ignore
2276 case.
2277
2278 Setting this variable makes it buffer-local in the current buffer.
2279 @end defvar
2280
2281 @defvar imenu-syntax-alist
2282 This variable is an alist of syntax table modifiers to use while
2283 processing @code{imenu-generic-expression}, to override the syntax table
2284 of the current buffer. Each element should have this form:
2285
2286 @example
2287 (@var{characters} . @var{syntax-description})
2288 @end example
2289
2290 The @sc{car}, @var{characters}, can be either a character or a string.
2291 The element says to give that character or characters the syntax
2292 specified by @var{syntax-description}, which is passed to
2293 @code{modify-syntax-entry} (@pxref{Syntax Table Functions}).
2294
2295 This feature is typically used to give word syntax to characters which
2296 normally have symbol syntax, and thus to simplify
2297 @code{imenu-generic-expression} and speed up matching.
2298 For example, Fortran mode uses it this way:
2299
2300 @example
2301 (setq imenu-syntax-alist '(("_$" . "w")))
2302 @end example
2303
2304 The @code{imenu-generic-expression} regular expressions can then use
2305 @samp{\\sw+} instead of @samp{\\(\\sw\\|\\s_\\)+}. Note that this
2306 technique may be inconvenient when the mode needs to limit the initial
2307 character of a name to a smaller set of characters than are allowed in
2308 the rest of a name.
2309
2310 Setting this variable makes it buffer-local in the current buffer.
2311 @end defvar
2312
2313 Another way to customize Imenu for a major mode is to set the
2314 variables @code{imenu-prev-index-position-function} and
2315 @code{imenu-extract-index-name-function}:
2316
2317 @defvar imenu-prev-index-position-function
2318 If this variable is non-@code{nil}, its value should be a function that
2319 finds the next ``definition'' to put in the buffer index, scanning
2320 backward in the buffer from point. It should return @code{nil} if it
2321 doesn't find another ``definition'' before point. Otherwise it should
2322 leave point at the place it finds a ``definition'' and return any
2323 non-@code{nil} value.
2324
2325 Setting this variable makes it buffer-local in the current buffer.
2326 @end defvar
2327
2328 @defvar imenu-extract-index-name-function
2329 If this variable is non-@code{nil}, its value should be a function to
2330 return the name for a definition, assuming point is in that definition
2331 as the @code{imenu-prev-index-position-function} function would leave
2332 it.
2333
2334 Setting this variable makes it buffer-local in the current buffer.
2335 @end defvar
2336
2337 The last way to customize Imenu for a major mode is to set the
2338 variable @code{imenu-create-index-function}:
2339
2340 @defvar imenu-create-index-function
2341 This variable specifies the function to use for creating a buffer
2342 index. The function should take no arguments, and return an index
2343 alist for the current buffer. It is called within
2344 @code{save-excursion}, so where it leaves point makes no difference.
2345
2346 The index alist can have three types of elements. Simple elements
2347 look like this:
2348
2349 @example
2350 (@var{index-name} . @var{index-position})
2351 @end example
2352
2353 Selecting a simple element has the effect of moving to position
2354 @var{index-position} in the buffer. Special elements look like this:
2355
2356 @example
2357 (@var{index-name} @var{index-position} @var{function} @var{arguments}@dots{})
2358 @end example
2359
2360 Selecting a special element performs:
2361
2362 @example
2363 (funcall @var{function}
2364 @var{index-name} @var{index-position} @var{arguments}@dots{})
2365 @end example
2366
2367 A nested sub-alist element looks like this:
2368
2369 @example
2370 (@var{menu-title} @var{sub-alist})
2371 @end example
2372
2373 It creates the submenu @var{menu-title} specified by @var{sub-alist}.
2374
2375 The default value of @code{imenu-create-index-function} is
2376 @code{imenu-default-create-index-function}. This function calls the
2377 value of @code{imenu-prev-index-position-function} and the value of
2378 @code{imenu-extract-index-name-function} to produce the index alist.
2379 However, if either of these two variables is @code{nil}, the default
2380 function uses @code{imenu-generic-expression} instead.
2381
2382 Setting this variable makes it buffer-local in the current buffer.
2383 @end defvar
2384
2385 @node Font Lock Mode
2386 @section Font Lock Mode
2387 @cindex Font Lock mode
2388
2389 @dfn{Font Lock mode} is a feature that automatically attaches
2390 @code{face} properties to certain parts of the buffer based on their
2391 syntactic role. How it parses the buffer depends on the major mode;
2392 most major modes define syntactic criteria for which faces to use in
2393 which contexts. This section explains how to customize Font Lock for a
2394 particular major mode.
2395
2396 Font Lock mode finds text to highlight in two ways: through
2397 syntactic parsing based on the syntax table, and through searching
2398 (usually for regular expressions). Syntactic fontification happens
2399 first; it finds comments and string constants and highlights them.
2400 Search-based fontification happens second.
2401
2402 @menu
2403 * Font Lock Basics:: Overview of customizing Font Lock.
2404 * Search-based Fontification:: Fontification based on regexps.
2405 * Customizing Keywords:: Customizing search-based fontification.
2406 * Other Font Lock Variables:: Additional customization facilities.
2407 * Levels of Font Lock:: Each mode can define alternative levels
2408 so that the user can select more or less.
2409 * Precalculated Fontification:: How Lisp programs that produce the buffer
2410 contents can also specify how to fontify it.
2411 * Faces for Font Lock:: Special faces specifically for Font Lock.
2412 * Syntactic Font Lock:: Fontification based on syntax tables.
2413 * Setting Syntax Properties:: Defining character syntax based on context
2414 using the Font Lock mechanism.
2415 * Multiline Font Lock:: How to coerce Font Lock into properly
2416 highlighting multiline constructs.
2417 @end menu
2418
2419 @node Font Lock Basics
2420 @subsection Font Lock Basics
2421
2422 There are several variables that control how Font Lock mode highlights
2423 text. But major modes should not set any of these variables directly.
2424 Instead, they should set @code{font-lock-defaults} as a buffer-local
2425 variable. The value assigned to this variable is used, if and when Font
2426 Lock mode is enabled, to set all the other variables.
2427
2428 @defvar font-lock-defaults
2429 This variable is set by major modes, as a buffer-local variable, to
2430 specify how to fontify text in that mode. It automatically becomes
2431 buffer-local when you set it. If its value is @code{nil}, Font-Lock
2432 mode does no highlighting, and you can use the @samp{Faces} menu
2433 (under @samp{Edit} and then @samp{Text Properties} in the menu bar) to
2434 assign faces explicitly to text in the buffer.
2435
2436 If non-@code{nil}, the value should look like this:
2437
2438 @example
2439 (@var{keywords} [@var{keywords-only} [@var{case-fold}
2440 [@var{syntax-alist} [@var{syntax-begin} @var{other-vars}@dots{}]]]])
2441 @end example
2442
2443 The first element, @var{keywords}, indirectly specifies the value of
2444 @code{font-lock-keywords} which directs search-based fontification.
2445 It can be a symbol, a variable or a function whose value is the list
2446 to use for @code{font-lock-keywords}. It can also be a list of
2447 several such symbols, one for each possible level of fontification.
2448 The first symbol specifies the @samp{mode default} level of
2449 fontification, the next symbol level 1 fontification, the next level 2,
2450 and so on. The @samp{mode default} level is normally the same as level
2451 1. It is used when @code{font-lock-maximum-decoration} has a @code{nil}
2452 value. @xref{Levels of Font Lock}.
2453
2454 The second element, @var{keywords-only}, specifies the value of the
2455 variable @code{font-lock-keywords-only}. If this is omitted or
2456 @code{nil}, syntactic fontification (of strings and comments) is also
2457 performed. If this is non-@code{nil}, such fontification is not
2458 performed. @xref{Syntactic Font Lock}.
2459
2460 The third element, @var{case-fold}, specifies the value of
2461 @code{font-lock-keywords-case-fold-search}. If it is non-@code{nil},
2462 Font Lock mode ignores case when searching as directed by
2463 @code{font-lock-keywords}.
2464
2465 If the fourth element, @var{syntax-alist}, is non-@code{nil}, it
2466 should be a list of cons cells of the form @code{(@var{char-or-string}
2467 . @var{string})}. These are used to set up a syntax table for
2468 syntactic fontification (@pxref{Syntax Table Functions}). The
2469 resulting syntax table is stored in @code{font-lock-syntax-table}.
2470
2471 The fifth element, @var{syntax-begin}, specifies the value of
2472 @code{font-lock-beginning-of-syntax-function}. We recommend setting
2473 this variable to @code{nil} and using @code{syntax-begin-function}
2474 instead.
2475
2476 All the remaining elements (if any) are collectively called
2477 @var{other-vars}. Each of these elements should have the form
2478 @code{(@var{variable} . @var{value})}---which means, make
2479 @var{variable} buffer-local and then set it to @var{value}. You can
2480 use these @var{other-vars} to set other variables that affect
2481 fontification, aside from those you can control with the first five
2482 elements. @xref{Other Font Lock Variables}.
2483 @end defvar
2484
2485 If your mode fontifies text explicitly by adding
2486 @code{font-lock-face} properties, it can specify @code{(nil t)} for
2487 @code{font-lock-defaults} to turn off all automatic fontification.
2488 However, this is not required; it is possible to fontify some things
2489 using @code{font-lock-face} properties and set up automatic
2490 fontification for other parts of the text.
2491
2492 @node Search-based Fontification
2493 @subsection Search-based Fontification
2494
2495 The most important variable for customizing Font Lock mode is
2496 @code{font-lock-keywords}. It specifies the search criteria for
2497 search-based fontification. You should specify the value of this
2498 variable with @var{keywords} in @code{font-lock-defaults}.
2499
2500 @defvar font-lock-keywords
2501 This variable's value is a list of the keywords to highlight. Be
2502 careful when composing regular expressions for this list; a poorly
2503 written pattern can dramatically slow things down!
2504 @end defvar
2505
2506 Each element of @code{font-lock-keywords} specifies how to find
2507 certain cases of text, and how to highlight those cases. Font Lock mode
2508 processes the elements of @code{font-lock-keywords} one by one, and for
2509 each element, it finds and handles all matches. Ordinarily, once
2510 part of the text has been fontified already, this cannot be overridden
2511 by a subsequent match in the same text; but you can specify different
2512 behavior using the @var{override} element of a @var{subexp-highlighter}.
2513
2514 Each element of @code{font-lock-keywords} should have one of these
2515 forms:
2516
2517 @table @code
2518 @item @var{regexp}
2519 Highlight all matches for @var{regexp} using
2520 @code{font-lock-keyword-face}. For example,
2521
2522 @example
2523 ;; @r{Highlight occurrences of the word @samp{foo}}
2524 ;; @r{using @code{font-lock-keyword-face}.}
2525 "\\<foo\\>"
2526 @end example
2527
2528 The function @code{regexp-opt} (@pxref{Regexp Functions}) is useful
2529 for calculating optimal regular expressions to match a number of
2530 different keywords.
2531
2532 @item @var{function}
2533 Find text by calling @var{function}, and highlight the matches
2534 it finds using @code{font-lock-keyword-face}.
2535
2536 When @var{function} is called, it receives one argument, the limit of
2537 the search; it should begin searching at point, and not search beyond the
2538 limit. It should return non-@code{nil} if it succeeds, and set the
2539 match data to describe the match that was found. Returning @code{nil}
2540 indicates failure of the search.
2541
2542 Fontification will call @var{function} repeatedly with the same limit,
2543 and with point where the previous invocation left it, until
2544 @var{function} fails. On failure, @var{function} need not reset point
2545 in any particular way.
2546
2547 @item (@var{matcher} . @var{subexp})
2548 In this kind of element, @var{matcher} is either a regular
2549 expression or a function, as described above. The @sc{cdr},
2550 @var{subexp}, specifies which subexpression of @var{matcher} should be
2551 highlighted (instead of the entire text that @var{matcher} matched).
2552
2553 @example
2554 ;; @r{Highlight the @samp{bar} in each occurrence of @samp{fubar},}
2555 ;; @r{using @code{font-lock-keyword-face}.}
2556 ("fu\\(bar\\)" . 1)
2557 @end example
2558
2559 If you use @code{regexp-opt} to produce the regular expression
2560 @var{matcher}, you can use @code{regexp-opt-depth} (@pxref{Regexp
2561 Functions}) to calculate the value for @var{subexp}.
2562
2563 @item (@var{matcher} . @var{facespec})
2564 In this kind of element, @var{facespec} is an expression whose value
2565 specifies the face to use for highlighting. In the simplest case,
2566 @var{facespec} is a Lisp variable (a symbol) whose value is a face
2567 name.
2568
2569 @example
2570 ;; @r{Highlight occurrences of @samp{fubar},}
2571 ;; @r{using the face which is the value of @code{fubar-face}.}
2572 ("fubar" . fubar-face)
2573 @end example
2574
2575 However, @var{facespec} can also evaluate to a list of this form:
2576
2577 @example
2578 (face @var{face} @var{prop1} @var{val1} @var{prop2} @var{val2}@dots{})
2579 @end example
2580
2581 @noindent
2582 to specify the face @var{face} and various additional text properties
2583 to put on the text that matches. If you do this, be sure to add the
2584 other text property names that you set in this way to the value of
2585 @code{font-lock-extra-managed-props} so that the properties will also
2586 be cleared out when they are no longer appropriate. Alternatively,
2587 you can set the variable @code{font-lock-unfontify-region-function} to
2588 a function that clears these properties. @xref{Other Font Lock
2589 Variables}.
2590
2591 @item (@var{matcher} . @var{subexp-highlighter})
2592 In this kind of element, @var{subexp-highlighter} is a list
2593 which specifies how to highlight matches found by @var{matcher}.
2594 It has the form:
2595
2596 @example
2597 (@var{subexp} @var{facespec} [@var{override} [@var{laxmatch}]])
2598 @end example
2599
2600 The @sc{car}, @var{subexp}, is an integer specifying which subexpression
2601 of the match to fontify (0 means the entire matching text). The second
2602 subelement, @var{facespec}, is an expression whose value specifies the
2603 face, as described above.
2604
2605 The last two values in @var{subexp-highlighter}, @var{override} and
2606 @var{laxmatch}, are optional flags. If @var{override} is @code{t},
2607 this element can override existing fontification made by previous
2608 elements of @code{font-lock-keywords}. If it is @code{keep}, then
2609 each character is fontified if it has not been fontified already by
2610 some other element. If it is @code{prepend}, the face specified by
2611 @var{facespec} is added to the beginning of the @code{font-lock-face}
2612 property. If it is @code{append}, the face is added to the end of the
2613 @code{font-lock-face} property.
2614
2615 If @var{laxmatch} is non-@code{nil}, it means there should be no error
2616 if there is no subexpression numbered @var{subexp} in @var{matcher}.
2617 Obviously, fontification of the subexpression numbered @var{subexp} will
2618 not occur. However, fontification of other subexpressions (and other
2619 regexps) will continue. If @var{laxmatch} is @code{nil}, and the
2620 specified subexpression is missing, then an error is signaled which
2621 terminates search-based fontification.
2622
2623 Here are some examples of elements of this kind, and what they do:
2624
2625 @smallexample
2626 ;; @r{Highlight occurrences of either @samp{foo} or @samp{bar}, using}
2627 ;; @r{@code{foo-bar-face}, even if they have already been highlighted.}
2628 ;; @r{@code{foo-bar-face} should be a variable whose value is a face.}
2629 ("foo\\|bar" 0 foo-bar-face t)
2630
2631 ;; @r{Highlight the first subexpression within each occurrence}
2632 ;; @r{that the function @code{fubar-match} finds,}
2633 ;; @r{using the face which is the value of @code{fubar-face}.}
2634 (fubar-match 1 fubar-face)
2635 @end smallexample
2636
2637 @item (@var{matcher} . @var{anchored-highlighter})
2638 In this kind of element, @var{anchored-highlighter} specifies how to
2639 highlight text that follows a match found by @var{matcher}. So a
2640 match found by @var{matcher} acts as the anchor for further searches
2641 specified by @var{anchored-highlighter}. @var{anchored-highlighter}
2642 is a list of the following form:
2643
2644 @example
2645 (@var{anchored-matcher} @var{pre-form} @var{post-form}
2646 @var{subexp-highlighters}@dots{})
2647 @end example
2648
2649 Here, @var{anchored-matcher}, like @var{matcher}, is either a regular
2650 expression or a function. After a match of @var{matcher} is found,
2651 point is at the end of the match. Now, Font Lock evaluates the form
2652 @var{pre-form}. Then it searches for matches of
2653 @var{anchored-matcher} and uses @var{subexp-highlighters} to highlight
2654 these. A @var{subexp-highlighter} is as described above. Finally,
2655 Font Lock evaluates @var{post-form}.
2656
2657 The forms @var{pre-form} and @var{post-form} can be used to initialize
2658 before, and cleanup after, @var{anchored-matcher} is used. Typically,
2659 @var{pre-form} is used to move point to some position relative to the
2660 match of @var{matcher}, before starting with @var{anchored-matcher}.
2661 @var{post-form} might be used to move back, before resuming with
2662 @var{matcher}.
2663
2664 After Font Lock evaluates @var{pre-form}, it does not search for
2665 @var{anchored-matcher} beyond the end of the line. However, if
2666 @var{pre-form} returns a buffer position that is greater than the
2667 position of point after @var{pre-form} is evaluated, then the position
2668 returned by @var{pre-form} is used as the limit of the search instead.
2669 It is generally a bad idea to return a position greater than the end
2670 of the line; in other words, the @var{anchored-matcher} search should
2671 not span lines.
2672
2673 For example,
2674
2675 @smallexample
2676 ;; @r{Highlight occurrences of the word @samp{item} following}
2677 ;; @r{an occurrence of the word @samp{anchor} (on the same line)}
2678 ;; @r{in the value of @code{item-face}.}
2679 ("\\<anchor\\>" "\\<item\\>" nil nil (0 item-face))
2680 @end smallexample
2681
2682 Here, @var{pre-form} and @var{post-form} are @code{nil}. Therefore
2683 searching for @samp{item} starts at the end of the match of
2684 @samp{anchor}, and searching for subsequent instances of @samp{anchor}
2685 resumes from where searching for @samp{item} concluded.
2686
2687 @item (@var{matcher} @var{highlighters}@dots{})
2688 This sort of element specifies several @var{highlighter} lists for a
2689 single @var{matcher}. A @var{highlighter} list can be of the type
2690 @var{subexp-highlighter} or @var{anchored-highlighter} as described
2691 above.
2692
2693 For example,
2694
2695 @smallexample
2696 ;; @r{Highlight occurrences of the word @samp{anchor} in the value}
2697 ;; @r{of @code{anchor-face}, and subsequent occurrences of the word}
2698 ;; @r{@samp{item} (on the same line) in the value of @code{item-face}.}
2699 ("\\<anchor\\>" (0 anchor-face)
2700 ("\\<item\\>" nil nil (0 item-face)))
2701 @end smallexample
2702
2703 @item (eval . @var{form})
2704 Here @var{form} is an expression to be evaluated the first time
2705 this value of @code{font-lock-keywords} is used in a buffer.
2706 Its value should have one of the forms described in this table.
2707 @end table
2708
2709 @strong{Warning:} Do not design an element of @code{font-lock-keywords}
2710 to match text which spans lines; this does not work reliably.
2711 For details, see @xref{Multiline Font Lock}.
2712
2713 You can use @var{case-fold} in @code{font-lock-defaults} to specify
2714 the value of @code{font-lock-keywords-case-fold-search} which says
2715 whether search-based fontification should be case-insensitive.
2716
2717 @defvar font-lock-keywords-case-fold-search
2718 Non-@code{nil} means that regular expression matching for the sake of
2719 @code{font-lock-keywords} should be case-insensitive.
2720 @end defvar
2721
2722 @node Customizing Keywords
2723 @subsection Customizing Search-Based Fontification
2724
2725 You can use @code{font-lock-add-keywords} to add additional
2726 search-based fontification rules to a major mode, and
2727 @code{font-lock-remove-keywords} to remove rules.
2728
2729 @defun font-lock-add-keywords mode keywords &optional how
2730 This function adds highlighting @var{keywords}, for the current buffer
2731 or for major mode @var{mode}. The argument @var{keywords} should be a
2732 list with the same format as the variable @code{font-lock-keywords}.
2733
2734 If @var{mode} is a symbol which is a major mode command name, such as
2735 @code{c-mode}, the effect is that enabling Font Lock mode in
2736 @var{mode} will add @var{keywords} to @code{font-lock-keywords}.
2737 Calling with a non-@code{nil} value of @var{mode} is correct only in
2738 your @file{~/.emacs} file.
2739
2740 If @var{mode} is @code{nil}, this function adds @var{keywords} to
2741 @code{font-lock-keywords} in the current buffer. This way of calling
2742 @code{font-lock-add-keywords} is usually used in mode hook functions.
2743
2744 By default, @var{keywords} are added at the beginning of
2745 @code{font-lock-keywords}. If the optional argument @var{how} is
2746 @code{set}, they are used to replace the value of
2747 @code{font-lock-keywords}. If @var{how} is any other non-@code{nil}
2748 value, they are added at the end of @code{font-lock-keywords}.
2749
2750 Some modes provide specialized support you can use in additional
2751 highlighting patterns. See the variables
2752 @code{c-font-lock-extra-types}, @code{c++-font-lock-extra-types},
2753 and @code{java-font-lock-extra-types}, for example.
2754
2755 @strong{Warning:} major mode functions must not call
2756 @code{font-lock-add-keywords} under any circumstances, either directly
2757 or indirectly, except through their mode hooks. (Doing so would lead
2758 to incorrect behavior for some minor modes.) They should set up their
2759 rules for search-based fontification by setting
2760 @code{font-lock-keywords}.
2761 @end defun
2762
2763 @defun font-lock-remove-keywords mode keywords
2764 This function removes @var{keywords} from @code{font-lock-keywords}
2765 for the current buffer or for major mode @var{mode}. As in
2766 @code{font-lock-add-keywords}, @var{mode} should be a major mode
2767 command name or @code{nil}. All the caveats and requirements for
2768 @code{font-lock-add-keywords} apply here too.
2769 @end defun
2770
2771 For example, this code
2772
2773 @smallexample
2774 (font-lock-add-keywords 'c-mode
2775 '(("\\<\\(FIXME\\):" 1 font-lock-warning-face prepend)
2776 ("\\<\\(and\\|or\\|not\\)\\>" . font-lock-keyword-face)))
2777 @end smallexample
2778
2779 @noindent
2780 adds two fontification patterns for C mode: one to fontify the word
2781 @samp{FIXME}, even in comments, and another to fontify the words
2782 @samp{and}, @samp{or} and @samp{not} as keywords.
2783
2784 @noindent
2785 That example affects only C mode proper. To add the same patterns to
2786 C mode @emph{and} all modes derived from it, do this instead:
2787
2788 @smallexample
2789 (add-hook 'c-mode-hook
2790 (lambda ()
2791 (font-lock-add-keywords nil
2792 '(("\\<\\(FIXME\\):" 1 font-lock-warning-face prepend)
2793 ("\\<\\(and\\|or\\|not\\)\\>" .
2794 font-lock-keyword-face)))))
2795 @end smallexample
2796
2797 @node Other Font Lock Variables
2798 @subsection Other Font Lock Variables
2799
2800 This section describes additional variables that a major mode can
2801 set by means of @var{other-vars} in @code{font-lock-defaults}
2802 (@pxref{Font Lock Basics}).
2803
2804 @defvar font-lock-mark-block-function
2805 If this variable is non-@code{nil}, it should be a function that is
2806 called with no arguments, to choose an enclosing range of text for
2807 refontification for the command @kbd{M-o M-o}
2808 (@code{font-lock-fontify-block}).
2809
2810 The function should report its choice by placing the region around it.
2811 A good choice is a range of text large enough to give proper results,
2812 but not too large so that refontification becomes slow. Typical values
2813 are @code{mark-defun} for programming modes or @code{mark-paragraph} for
2814 textual modes.
2815 @end defvar
2816
2817 @defvar font-lock-extra-managed-props
2818 This variable specifies additional properties (other than
2819 @code{font-lock-face}) that are being managed by Font Lock mode. It
2820 is used by @code{font-lock-default-unfontify-region}, which normally
2821 only manages the @code{font-lock-face} property. If you want Font
2822 Lock to manage other properties as well, you must specify them in a
2823 @var{facespec} in @code{font-lock-keywords} as well as add them to
2824 this list. @xref{Search-based Fontification}.
2825 @end defvar
2826
2827 @defvar font-lock-fontify-buffer-function
2828 Function to use for fontifying the buffer. The default value is
2829 @code{font-lock-default-fontify-buffer}.
2830 @end defvar
2831
2832 @defvar font-lock-unfontify-buffer-function
2833 Function to use for unfontifying the buffer. This is used when
2834 turning off Font Lock mode. The default value is
2835 @code{font-lock-default-unfontify-buffer}.
2836 @end defvar
2837
2838 @defvar font-lock-fontify-region-function
2839 Function to use for fontifying a region. It should take two
2840 arguments, the beginning and end of the region, and an optional third
2841 argument @var{verbose}. If @var{verbose} is non-@code{nil}, the
2842 function should print status messages. The default value is
2843 @code{font-lock-default-fontify-region}.
2844 @end defvar
2845
2846 @defvar font-lock-unfontify-region-function
2847 Function to use for unfontifying a region. It should take two
2848 arguments, the beginning and end of the region. The default value is
2849 @code{font-lock-default-unfontify-region}.
2850 @end defvar
2851
2852 @defun jit-lock-register function &optional contextual
2853 This function tells Font Lock mode to run the Lisp function
2854 @var{function} any time it has to fontify or refontify part of the
2855 current buffer. It calls @var{function} before calling the default
2856 fontification functions, and gives it two arguments, @var{start} and
2857 @var{end}, which specify the region to be fontified or refontified.
2858
2859 The optional argument @var{contextual}, if non-@code{nil}, forces Font
2860 Lock mode to always refontify a syntactically relevant part of the
2861 buffer, and not just the modified lines. This argument can usually be
2862 omitted.
2863 @end defun
2864
2865 @defun jit-lock-unregister function
2866 If @var{function} was previously registered as a fontification
2867 function using @code{jit-lock-register}, this function unregisters it.
2868 @end defun
2869
2870 @node Levels of Font Lock
2871 @subsection Levels of Font Lock
2872
2873 Many major modes offer three different levels of fontification. You
2874 can define multiple levels by using a list of symbols for @var{keywords}
2875 in @code{font-lock-defaults}. Each symbol specifies one level of
2876 fontification; it is up to the user to choose one of these levels,
2877 normally by setting @code{font-lock-maximum-decoration} (@pxref{Font
2878 Lock,,, emacs, the GNU Emacs Manual}). The chosen level's symbol
2879 value is used to initialize @code{font-lock-keywords}.
2880
2881 Here are the conventions for how to define the levels of
2882 fontification:
2883
2884 @itemize @bullet
2885 @item
2886 Level 1: highlight function declarations, file directives (such as include or
2887 import directives), strings and comments. The idea is speed, so only
2888 the most important and top-level components are fontified.
2889
2890 @item
2891 Level 2: in addition to level 1, highlight all language keywords,
2892 including type names that act like keywords, as well as named constant
2893 values. The idea is that all keywords (either syntactic or semantic)
2894 should be fontified appropriately.
2895
2896 @item
2897 Level 3: in addition to level 2, highlight the symbols being defined in
2898 function and variable declarations, and all builtin function names,
2899 wherever they appear.
2900 @end itemize
2901
2902 @node Precalculated Fontification
2903 @subsection Precalculated Fontification
2904
2905 Some major modes such as @code{list-buffers} and @code{occur}
2906 construct the buffer text programmatically. The easiest way for them
2907 to support Font Lock mode is to specify the faces of text when they
2908 insert the text in the buffer.
2909
2910 The way to do this is to specify the faces in the text with the
2911 special text property @code{font-lock-face} (@pxref{Special
2912 Properties}). When Font Lock mode is enabled, this property controls
2913 the display, just like the @code{face} property. When Font Lock mode
2914 is disabled, @code{font-lock-face} has no effect on the display.
2915
2916 It is ok for a mode to use @code{font-lock-face} for some text and
2917 also use the normal Font Lock machinery. But if the mode does not use
2918 the normal Font Lock machinery, it should not set the variable
2919 @code{font-lock-defaults}.
2920
2921 @node Faces for Font Lock
2922 @subsection Faces for Font Lock
2923 @cindex faces for font lock
2924 @cindex font lock faces
2925
2926 Font Lock mode can highlight using any face, but Emacs defines several
2927 faces specifically for syntactic highlighting. These @dfn{Font Lock
2928 faces} are listed below. They can also be used by major modes for
2929 syntactic highlighting outside of Font Lock mode (@pxref{Major Mode
2930 Conventions}).
2931
2932 Each of these symbols is both a face name, and a variable whose
2933 default value is the symbol itself. Thus, the default value of
2934 @code{font-lock-comment-face} is @code{font-lock-comment-face}.
2935
2936 The faces are listed with descriptions of their typical usage, and in
2937 order of greater to lesser ``prominence''. If a mode's syntactic
2938 categories do not fit well with the usage descriptions, the faces can be
2939 assigned using the ordering as a guide.
2940
2941 @table @code
2942 @item font-lock-warning-face
2943 @vindex font-lock-warning-face
2944 for a construct that is peculiar, or that greatly changes the meaning of
2945 other text, like @samp{;;;###autoload} in Emacs Lisp and @samp{#error}
2946 in C.
2947
2948 @item font-lock-function-name-face
2949 @vindex font-lock-function-name-face
2950 for the name of a function being defined or declared.
2951
2952 @item font-lock-variable-name-face
2953 @vindex font-lock-variable-name-face
2954 for the name of a variable being defined or declared.
2955
2956 @item font-lock-keyword-face
2957 @vindex font-lock-keyword-face
2958 for a keyword with special syntactic significance, like @samp{for} and
2959 @samp{if} in C.
2960
2961 @item font-lock-comment-face
2962 @vindex font-lock-comment-face
2963 for comments.
2964
2965 @item font-lock-comment-delimiter-face
2966 @vindex font-lock-comment-delimiter-face
2967 for comments delimiters, like @samp{/*} and @samp{*/} in C. On most
2968 terminals, this inherits from @code{font-lock-comment-face}.
2969
2970 @item font-lock-type-face
2971 @vindex font-lock-type-face
2972 for the names of user-defined data types.
2973
2974 @item font-lock-constant-face
2975 @vindex font-lock-constant-face
2976 for the names of constants, like @samp{NULL} in C.
2977
2978 @item font-lock-builtin-face
2979 @vindex font-lock-builtin-face
2980 for the names of built-in functions.
2981
2982 @item font-lock-preprocessor-face
2983 @vindex font-lock-preprocessor-face
2984 for preprocessor commands. This inherits, by default, from
2985 @code{font-lock-builtin-face}.
2986
2987 @item font-lock-string-face
2988 @vindex font-lock-string-face
2989 for string constants.
2990
2991 @item font-lock-doc-face
2992 @vindex font-lock-doc-face
2993 for documentation strings in the code. This inherits, by default, from
2994 @code{font-lock-string-face}.
2995
2996 @item font-lock-negation-char-face
2997 @vindex font-lock-negation-char-face
2998 for easily-overlooked negation characters.
2999 @end table
3000
3001 @node Syntactic Font Lock
3002 @subsection Syntactic Font Lock
3003 @cindex syntactic font lock
3004
3005 Syntactic fontification uses the syntax table to find comments and
3006 string constants (@pxref{Syntax Tables}). It highlights them using
3007 @code{font-lock-comment-face} and @code{font-lock-string-face}
3008 (@pxref{Faces for Font Lock}), or whatever
3009 @code{font-lock-syntactic-face-function} chooses. There are several
3010 variables that affect syntactic fontification; you should set them by
3011 means of @code{font-lock-defaults} (@pxref{Font Lock Basics}).
3012
3013 @defvar font-lock-keywords-only
3014 Non-@code{nil} means Font Lock should not do syntactic fontification;
3015 it should only fontify based on @code{font-lock-keywords}. The normal
3016 way for a mode to set this variable to @code{t} is with
3017 @var{keywords-only} in @code{font-lock-defaults}.
3018 @end defvar
3019
3020 @defvar font-lock-syntax-table
3021 This variable holds the syntax table to use for fontification of
3022 comments and strings. Specify it using @var{syntax-alist} in
3023 @code{font-lock-defaults}. If this is @code{nil}, fontification uses
3024 the buffer's syntax table.
3025 @end defvar
3026
3027 @defvar font-lock-beginning-of-syntax-function
3028 If this variable is non-@code{nil}, it should be a function to move
3029 point back to a position that is syntactically at ``top level'' and
3030 outside of strings or comments. Font Lock uses this when necessary
3031 to get the right results for syntactic fontification.
3032
3033 This function is called with no arguments. It should leave point at
3034 the beginning of any enclosing syntactic block. Typical values are
3035 @code{beginning-of-line} (used when the start of the line is known to
3036 be outside a syntactic block), or @code{beginning-of-defun} for
3037 programming modes, or @code{backward-paragraph} for textual modes.
3038
3039 If the value is @code{nil}, Font Lock uses
3040 @code{syntax-begin-function} to move back outside of any comment,
3041 string, or sexp. This variable is semi-obsolete; we recommend setting
3042 @code{syntax-begin-function} instead.
3043
3044 Specify this variable using @var{syntax-begin} in
3045 @code{font-lock-defaults}.
3046 @end defvar
3047
3048 @defvar font-lock-syntactic-face-function
3049 A function to determine which face to use for a given syntactic
3050 element (a string or a comment). The function is called with one
3051 argument, the parse state at point returned by
3052 @code{parse-partial-sexp}, and should return a face. The default
3053 value returns @code{font-lock-comment-face} for comments and
3054 @code{font-lock-string-face} for strings.
3055
3056 This can be used to highlighting different kinds of strings or
3057 comments differently. It is also sometimes abused together with
3058 @code{font-lock-syntactic-keywords} to highlight constructs that span
3059 multiple lines, but this is too esoteric to document here.
3060
3061 Specify this variable using @var{other-vars} in
3062 @code{font-lock-defaults}.
3063 @end defvar
3064
3065 @node Setting Syntax Properties
3066 @subsection Setting Syntax Properties
3067
3068 Font Lock mode can be used to update @code{syntax-table} properties
3069 automatically (@pxref{Syntax Properties}). This is useful in
3070 languages for which a single syntax table by itself is not sufficient.
3071
3072 @defvar font-lock-syntactic-keywords
3073 This variable enables and controls updating @code{syntax-table}
3074 properties by Font Lock. Its value should be a list of elements of
3075 this form:
3076
3077 @example
3078 (@var{matcher} @var{subexp} @var{syntax} @var{override} @var{laxmatch})
3079 @end example
3080
3081 The parts of this element have the same meanings as in the corresponding
3082 sort of element of @code{font-lock-keywords},
3083
3084 @example
3085 (@var{matcher} @var{subexp} @var{facespec} @var{override} @var{laxmatch})
3086 @end example
3087
3088 However, instead of specifying the value @var{facespec} to use for the
3089 @code{face} property, it specifies the value @var{syntax} to use for
3090 the @code{syntax-table} property. Here, @var{syntax} can be a string
3091 (as taken by @code{modify-syntax-entry}), a syntax table, a cons cell
3092 (as returned by @code{string-to-syntax}), or an expression whose value
3093 is one of those two types. @var{override} cannot be @code{prepend} or
3094 @code{append}.
3095
3096 For example, an element of the form:
3097
3098 @example
3099 ("\\$\\(#\\)" 1 ".")
3100 @end example
3101
3102 highlights syntactically a hash character when following a dollar
3103 character, with a SYNTAX of @code{"."} (meaning punctuation syntax).
3104 Assuming that the buffer syntax table specifies hash characters to
3105 have comment start syntax, the element will only highlight hash
3106 characters that do not follow dollar characters as comments
3107 syntactically.
3108
3109 An element of the form:
3110
3111 @example
3112 ("\\('\\).\\('\\)"
3113 (1 "\"")
3114 (2 "\""))
3115 @end example
3116
3117 highlights syntactically both single quotes which surround a single
3118 character, with a SYNTAX of @code{"\""} (meaning string quote syntax).
3119 Assuming that the buffer syntax table does not specify single quotes
3120 to have quote syntax, the element will only highlight single quotes of
3121 the form @samp{'@var{c}'} as strings syntactically. Other forms, such
3122 as @samp{foo'bar} or @samp{'fubar'}, will not be highlighted as
3123 strings.
3124
3125 Major modes normally set this variable with @var{other-vars} in
3126 @code{font-lock-defaults}.
3127 @end defvar
3128
3129 @node Multiline Font Lock
3130 @subsection Multiline Font Lock Constructs
3131 @cindex multiline font lock
3132
3133 Normally, elements of @code{font-lock-keywords} should not match
3134 across multiple lines; that doesn't work reliably, because Font Lock
3135 usually scans just part of the buffer, and it can miss a multi-line
3136 construct that crosses the line boundary where the scan starts. (The
3137 scan normally starts at the beginning of a line.)
3138
3139 Making elements that match multiline constructs work properly has
3140 two aspects: correct @emph{identification} and correct
3141 @emph{rehighlighting}. The first means that Font Lock finds all
3142 multiline constructs. The second means that Font Lock will correctly
3143 rehighlight all the relevant text when a multiline construct is
3144 changed---for example, if some of the text that was previously part of
3145 a multiline construct ceases to be part of it. The two aspects are
3146 closely related, and often getting one of them to work will appear to
3147 make the other also work. However, for reliable results you must
3148 attend explicitly to both aspects.
3149
3150 There are three ways to ensure correct identification of multiline
3151 constructs:
3152
3153 @itemize
3154 @item
3155 Add a function to @code{font-lock-extend-region-functions} that does
3156 the @emph{identification} and extends the scan so that the scanned
3157 text never starts or ends in the middle of a multiline construct.
3158 @item
3159 Use the @code{font-lock-fontify-region-function} hook similarly to
3160 extend the scan so that the scanned text never starts or ends in the
3161 middle of a multiline construct.
3162 @item
3163 Somehow identify the multiline construct right when it gets inserted
3164 into the buffer (or at any point after that but before font-lock
3165 tries to highlight it), and mark it with a @code{font-lock-multiline}
3166 which will instruct font-lock not to start or end the scan in the
3167 middle of the construct.
3168 @end itemize
3169
3170 There are three ways to do rehighlighting of multiline constructs:
3171
3172 @itemize
3173 @item
3174 Place a @code{font-lock-multiline} property on the construct. This
3175 will rehighlight the whole construct if any part of it is changed. In
3176 some cases you can do this automatically by setting the
3177 @code{font-lock-multiline} variable, which see.
3178 @item
3179 Make sure @code{jit-lock-contextually} is set and rely on it doing its
3180 job. This will only rehighlight the part of the construct that
3181 follows the actual change, and will do it after a short delay.
3182 This only works if the highlighting of the various parts of your
3183 multiline construct never depends on text in subsequent lines.
3184 Since @code{jit-lock-contextually} is activated by default, this can
3185 be an attractive solution.
3186 @item
3187 Place a @code{jit-lock-defer-multiline} property on the construct.
3188 This works only if @code{jit-lock-contextually} is used, and with the
3189 same delay before rehighlighting, but like @code{font-lock-multiline},
3190 it also handles the case where highlighting depends on
3191 subsequent lines.
3192 @end itemize
3193
3194 @menu
3195 * Font Lock Multiline:: Marking multiline chunks with a text property.
3196 * Region to Refontify:: Controlling which region gets refontified
3197 after a buffer change.
3198 @end menu
3199
3200 @node Font Lock Multiline
3201 @subsubsection Font Lock Multiline
3202
3203 One way to ensure reliable rehighlighting of multiline Font Lock
3204 constructs is to put on them the text property @code{font-lock-multiline}.
3205 It should be present and non-@code{nil} for text that is part of a
3206 multiline construct.
3207
3208 When Font Lock is about to highlight a range of text, it first
3209 extends the boundaries of the range as necessary so that they do not
3210 fall within text marked with the @code{font-lock-multiline} property.
3211 Then it removes any @code{font-lock-multiline} properties from the
3212 range, and highlights it. The highlighting specification (mostly
3213 @code{font-lock-keywords}) must reinstall this property each time,
3214 whenever it is appropriate.
3215
3216 @strong{Warning:} don't use the @code{font-lock-multiline} property
3217 on large ranges of text, because that will make rehighlighting slow.
3218
3219 @defvar font-lock-multiline
3220 If the @code{font-lock-multiline} variable is set to @code{t}, Font
3221 Lock will try to add the @code{font-lock-multiline} property
3222 automatically on multiline constructs. This is not a universal
3223 solution, however, since it slows down Font Lock somewhat. It can
3224 miss some multiline constructs, or make the property larger or smaller
3225 than necessary.
3226
3227 For elements whose @var{matcher} is a function, the function should
3228 ensure that submatch 0 covers the whole relevant multiline construct,
3229 even if only a small subpart will be highlighted. It is often just as
3230 easy to add the @code{font-lock-multiline} property by hand.
3231 @end defvar
3232
3233 The @code{font-lock-multiline} property is meant to ensure proper
3234 refontification; it does not automatically identify new multiline
3235 constructs. Identifying the requires that Font-Lock operate on large
3236 enough chunks at a time. This will happen by accident on many cases,
3237 which may give the impression that multiline constructs magically work.
3238 If you set the @code{font-lock-multiline} variable non-@code{nil},
3239 this impression will be even stronger, since the highlighting of those
3240 constructs which are found will be properly updated from then on.
3241 But that does not work reliably.
3242
3243 To find multiline constructs reliably, you must either manually
3244 place the @code{font-lock-multiline} property on the text before
3245 Font-Lock looks at it, or use
3246 @code{font-lock-fontify-region-function}.
3247
3248 @node Region to Refontify
3249 @subsubsection Region to Fontify after a Buffer Change
3250
3251 When a buffer is changed, the region that Font Lock refontifies is
3252 by default the smallest sequence of whole lines that spans the change.
3253 While this works well most of the time, sometimes it doesn't---for
3254 example, when a change alters the syntactic meaning of text on an
3255 earlier line.
3256
3257 You can enlarge (or even reduce) the region to refontify by setting
3258 the following variable:
3259
3260 @defvar font-lock-extend-after-change-region-function
3261 This buffer-local variable is either @code{nil} or a function for
3262 Font-Lock to call to determine the region to scan and fontify.
3263
3264 The function is given three parameters, the standard @var{beg},
3265 @var{end}, and @var{old-len} from @code{after-change-functions}
3266 (@pxref{Change Hooks}). It should return either a cons of the
3267 beginning and end buffer positions (in that order) of the region to
3268 fontify, or @code{nil} (which means choose the region in the standard
3269 way). This function needs to preserve point, the match-data, and the
3270 current restriction. The region it returns may start or end in the
3271 middle of a line.
3272
3273 Since this function is called after every buffer change, it should be
3274 reasonably fast.
3275 @end defvar
3276
3277 @node Auto-Indentation
3278 @section Auto-indentation of code
3279
3280 For programming languages, an important feature of a major mode is to
3281 provide automatic indentation. This is controlled in Emacs by
3282 @code{indent-line-function} (@pxref{Mode-Specific Indent}).
3283 Writing a good indentation function can be difficult and to a large
3284 extent it is still a black art.
3285
3286 Many major mode authors will start by writing a simple indentation
3287 function that works for simple cases, for example by comparing with the
3288 indentation of the previous text line. For most programming languages
3289 that are not really line-based, this tends to scale very poorly:
3290 improving such a function to let it handle more diverse situations tends
3291 to become more and more difficult, resulting in the end with a large,
3292 complex, unmaintainable indentation function which nobody dares to touch.
3293
3294 A good indentation function will usually need to actually parse the
3295 text, according to the syntax of the language. Luckily, it is not
3296 necessary to parse the text in as much detail as would be needed
3297 for a compiler, but on the other hand, the parser embedded in the
3298 indentation code will want to be somewhat friendly to syntactically
3299 incorrect code.
3300
3301 Good maintainable indentation functions usually fall into 2 categories:
3302 either parsing forward from some ``safe'' starting point until the
3303 position of interest, or parsing backward from the position of interest.
3304 Neither of the two is a clearly better choice than the other: parsing
3305 backward is often more difficult than parsing forward because
3306 programming languages are designed to be parsed forward, but for the
3307 purpose of indentation it has the advantage of not needing to
3308 guess a ``safe'' starting point, and it generally enjoys the property
3309 that only a minimum of text will be analyzed to decide the indentation
3310 of a line, so indentation will tend to be unaffected by syntax errors in
3311 some earlier unrelated piece of code. Parsing forward on the other hand
3312 is usually easier and has the advantage of making it possible to
3313 reindent efficiently a whole region at a time, with a single parse.
3314
3315 Rather than write your own indentation function from scratch, it is
3316 often preferable to try and reuse some existing ones or to rely
3317 on a generic indentation engine. There are sadly few such
3318 engines. The CC-mode indentation code (used with C, C++, Java, Awk
3319 and a few other such modes) has been made more generic over the years,
3320 so if your language seems somewhat similar to one of those languages,
3321 you might try to use that engine. @c FIXME: documentation?
3322 Another one is SMIE which takes an approach in the spirit
3323 of Lisp sexps and adapts it to non-Lisp languages.
3324
3325 @menu
3326 * SMIE:: A simple minded indentation engine
3327 @end menu
3328
3329 @node SMIE
3330 @subsection Simple Minded Indentation Engine
3331
3332 SMIE is a package that provides a generic navigation and indentation
3333 engine. Based on a very simple parser using an ``operator precedence
3334 grammar'', it lets major modes extend the sexp-based navigation of Lisp
3335 to non-Lisp languages as well as provide a simple to use but reliable
3336 auto-indentation.
3337
3338 Operator precedence grammar is a very primitive technology for parsing
3339 compared to some of the more common techniques used in compilers.
3340 It has the following characteristics: its parsing power is very limited,
3341 and it is largely unable to detect syntax errors, but it has the
3342 advantage of being algorithmically efficient and able to parse forward
3343 just as well as backward. In practice that means that SMIE can use it
3344 for indentation based on backward parsing, that it can provide both
3345 @code{forward-sexp} and @code{backward-sexp} functionality, and that it
3346 will naturally work on syntactically incorrect code without any extra
3347 effort. The downside is that it also means that most programming
3348 languages cannot be parsed correctly using SMIE, at least not without
3349 resorting to some special tricks (@pxref{SMIE Tricks}).
3350
3351 @menu
3352 * SMIE setup:: SMIE setup and features
3353 * Operator Precedence Grammars:: A very simple parsing technique
3354 * SMIE Grammar:: Defining the grammar of a language
3355 * SMIE Lexer:: Defining tokens
3356 * SMIE Tricks:: Working around the parser's limitations
3357 * SMIE Indentation:: Specifying indentation rules
3358 * SMIE Indentation Helpers:: Helper functions for indentation rules
3359 * SMIE Indentation Example:: Sample indentation rules
3360 @end menu
3361
3362 @node SMIE setup
3363 @subsubsection SMIE Setup and Features
3364
3365 SMIE is meant to be a one-stop shop for structural navigation and
3366 various other features which rely on the syntactic structure of code, in
3367 particular automatic indentation. The main entry point is
3368 @code{smie-setup} which is a function typically called while setting
3369 up a major mode.
3370
3371 @defun smie-setup grammar rules-function &rest keywords
3372 Setup SMIE navigation and indentation.
3373 @var{grammar} is a grammar table generated by @code{smie-prec2->grammar}.
3374 @var{rules-function} is a set of indentation rules for use on
3375 @code{smie-rules-function}.
3376 @var{keywords} are additional arguments, which can include the following
3377 keywords:
3378 @itemize
3379 @item
3380 @code{:forward-token} @var{fun}: Specify the forward lexer to use.
3381 @item
3382 @code{:backward-token} @var{fun}: Specify the backward lexer to use.
3383 @end itemize
3384 @end defun
3385
3386 Calling this function is sufficient to make commands such as
3387 @code{forward-sexp}, @code{backward-sexp}, and @code{transpose-sexps} be
3388 able to properly handle structural elements other than just the paired
3389 parentheses already handled by syntax tables. For example, if the
3390 provided grammar is precise enough, @code{transpose-sexps} can correctly
3391 transpose the two arguments of a @code{+} operator, taking into account
3392 the precedence rules of the language.
3393
3394 Calling `smie-setup' is also sufficient to make TAB indentation work in
3395 the expected way, extends @code{blink-matching-paren} to apply to
3396 elements like @code{begin...end}, and provides some commands that you
3397 can bind in the major mode keymap.
3398
3399 @deffn Command smie-close-block
3400 This command closes the most recently opened (and not yet closed) block.
3401 @end deffn
3402
3403 @deffn Command smie-down-list &optional arg
3404 This command is like @code{down-list} but it also pays attention to
3405 nesting of tokens other than parentheses, such as @code{begin...end}.
3406 @end deffn
3407
3408 @node Operator Precedence Grammars
3409 @subsubsection Operator Precedence Grammars
3410
3411 SMIE's precedence grammars simply give to each token a pair of
3412 precedences: the left-precedence and the right-precedence. We say
3413 @code{T1 < T2} if the right-precedence of token @code{T1} is less than
3414 the left-precedence of token @code{T2}. A good way to read this
3415 @code{<} is as a kind of parenthesis: if we find @code{... T1 something
3416 T2 ...} then that should be parsed as @code{... T1 (something T2 ...}
3417 rather than as @code{... T1 something) T2 ...}. The latter
3418 interpretation would be the case if we had @code{T1 > T2}. If we have
3419 @code{T1 = T2}, it means that token T2 follows token T1 in the same
3420 syntactic construction, so typically we have @code{"begin" = "end"}.
3421 Such pairs of precedences are sufficient to express left-associativity
3422 or right-associativity of infix operators, nesting of tokens like
3423 parentheses and many other cases.
3424
3425 @c Let's leave this undocumented to leave it more open for change!
3426 @c @defvar smie-grammar
3427 @c The value of this variable is an alist specifying the left and right
3428 @c precedence of each token. It is meant to be initialized by using one of
3429 @c the functions below.
3430 @c @end defvar
3431
3432 @defun smie-prec2->grammar table
3433 This function takes a @emph{prec2} grammar @var{table} and returns an
3434 alist suitable for use in @code{smie-setup}. The @emph{prec2}
3435 @var{table} is itself meant to be built by one of the functions below.
3436 @end defun
3437
3438 @defun smie-merge-prec2s &rest tables
3439 This function takes several @emph{prec2} @var{tables} and merges them
3440 into a new @emph{prec2} table.
3441 @end defun
3442
3443 @defun smie-precs->prec2 precs
3444 This function builds a @emph{prec2} table from a table of precedences
3445 @var{precs}. @var{precs} should be a list, sorted by precedence (for
3446 example @code{"+"} will come before @code{"*"}), of elements of the form
3447 @code{(@var{assoc} @var{op} ...)}, where each @var{op} is a token that
3448 acts as an operator; @var{assoc} is their associativity, which can be
3449 either @code{left}, @code{right}, @code{assoc}, or @code{nonassoc}.
3450 All operators in a given element share the same precedence level
3451 and associativity.
3452 @end defun
3453
3454 @defun smie-bnf->prec2 bnf &rest resolvers
3455 This function lets you specify the grammar using a BNF notation.
3456 It accepts a @var{bnf} description of the grammar along with a set of
3457 conflict resolution rules @var{resolvers}, and
3458 returns a @emph{prec2} table.
3459
3460 @var{bnf} is a list of nonterminal definitions of the form
3461 @code{(@var{nonterm} @var{rhs1} @var{rhs2} ...)} where each @var{rhs}
3462 is a (non-empty) list of terminals (aka tokens) or non-terminals.
3463
3464 Not all grammars are accepted:
3465 @itemize
3466 @item
3467 An @var{rhs} cannot be an empty list (an empty list is never needed,
3468 since SMIE allows all non-terminals to match the empty string anyway).
3469 @item
3470 An @var{rhs} cannot have 2 consecutive non-terminals: each pair of
3471 non-terminals needs to be separated by a terminal (aka token).
3472 This is a fundamental limitation of operator precedence grammars.
3473 @end itemize
3474
3475 Additionally, conflicts can occur:
3476 @itemize
3477 @item
3478 The returned @emph{prec2} table holds constraints between pairs of tokens, and
3479 for any given pair only one constraint can be present: T1 < T2,
3480 T1 = T2, or T1 > T2.
3481 @item
3482 A token can be an @code{opener} (something similar to an open-paren),
3483 a @code{closer} (like a close-paren), or @code{neither} of the two
3484 (e.g. an infix operator, or an inner token like @code{"else"}).
3485 @end itemize
3486
3487 Precedence conflicts can be resolved via @var{resolvers}, which
3488 is a list of @emph{precs} tables (see @code{smie-precs->prec2}): for
3489 each precedence conflict, if those @code{precs} tables
3490 specify a particular constraint, then the conflict is resolved by using
3491 this constraint instead, else a conflict is reported and one of the
3492 conflicting constraints is picked arbitrarily and the others are
3493 simply ignored.
3494 @end defun
3495
3496 @node SMIE Grammar
3497 @subsubsection Defining the Grammar of a Language
3498
3499 The usual way to define the SMIE grammar of a language is by
3500 defining a new global variable that holds the precedence table by
3501 giving a set of BNF rules.
3502 For example, the grammar definition for a small Pascal-like language
3503 could look like:
3504 @example
3505 @group
3506 (require 'smie)
3507 (defvar sample-smie-grammar
3508 (smie-prec2->grammar
3509 (smie-bnf->prec2
3510 @end group
3511 @group
3512 '((id)
3513 (inst ("begin" insts "end")
3514 ("if" exp "then" inst "else" inst)
3515 (id ":=" exp)
3516 (exp))
3517 (insts (insts ";" insts) (inst))
3518 (exp (exp "+" exp)
3519 (exp "*" exp)
3520 ("(" exps ")"))
3521 (exps (exps "," exps) (exp)))
3522 @end group
3523 @group
3524 '((assoc ";"))
3525 '((assoc ","))
3526 '((assoc "+") (assoc "*")))))
3527 @end group
3528 @end example
3529
3530 @noindent
3531 A few things to note:
3532
3533 @itemize
3534 @item
3535 The above grammar does not explicitly mention the syntax of function
3536 calls: SMIE will automatically allow any sequence of sexps, such as
3537 identifiers, balanced parentheses, or @code{begin ... end} blocks
3538 to appear anywhere anyway.
3539 @item
3540 The grammar category @code{id} has no right hand side: this does not
3541 mean that it can match only the empty string, since as mentioned any
3542 sequence of sexps can appear anywhere anyway.
3543 @item
3544 Because non terminals cannot appear consecutively in the BNF grammar, it
3545 is difficult to correctly handle tokens that act as terminators, so the
3546 above grammar treats @code{";"} as a statement @emph{separator} instead,
3547 which SMIE can handle very well.
3548 @item
3549 Separators used in sequences (such as @code{","} and @code{";"} above)
3550 are best defined with BNF rules such as @code{(foo (foo "separator" foo) ...)}
3551 which generate precedence conflicts which are then resolved by giving
3552 them an explicit @code{(assoc "separator")}.
3553 @item
3554 The @code{("(" exps ")")} rule was not needed to pair up parens, since
3555 SMIE will pair up any characters that are marked as having paren syntax
3556 in the syntax table. What this rule does instead (together with the
3557 definition of @code{exps}) is to make it clear that @code{","} should
3558 not appear outside of parentheses.
3559 @item
3560 Rather than have a single @emph{precs} table to resolve conflicts, it is
3561 preferable to have several tables, so as to let the BNF part of the
3562 grammar specify relative precedences where possible.
3563 @item
3564 Unless there is a very good reason to prefer @code{left} or
3565 @code{right}, it is usually preferable to mark operators as associative,
3566 using @code{assoc}. For that reason @code{"+"} and @code{"*"} are
3567 defined above as @code{assoc}, although the language defines them
3568 formally as left associative.
3569 @end itemize
3570
3571 @node SMIE Lexer
3572 @subsubsection Defining Tokens
3573
3574 SMIE comes with a predefined lexical analyzer which uses syntax tables
3575 in the following way: any sequence of characters that have word or
3576 symbol syntax is considered a token, and so is any sequence of
3577 characters that have punctuation syntax. This default lexer is
3578 often a good starting point but is rarely actually correct for any given
3579 language. For example, it will consider @code{"2,+3"} to be composed
3580 of 3 tokens: @code{"2"}, @code{",+"}, and @code{"3"}.
3581
3582 To describe the lexing rules of your language to SMIE, you need
3583 2 functions, one to fetch the next token, and another to fetch the
3584 previous token. Those functions will usually first skip whitespace and
3585 comments and then look at the next chunk of text to see if it
3586 is a special token. If so it should skip the token and
3587 return a description of this token. Usually this is simply the string
3588 extracted from the buffer, but it can be anything you want.
3589 For example:
3590 @example
3591 @group
3592 (defvar sample-keywords-regexp
3593 (regexp-opt '("+" "*" "," ";" ">" ">=" "<" "<=" ":=" "=")))
3594 @end group
3595 @group
3596 (defun sample-smie-forward-token ()
3597 (forward-comment (point-max))
3598 (cond
3599 ((looking-at sample-keywords-regexp)
3600 (goto-char (match-end 0))
3601 (match-string-no-properties 0))
3602 (t (buffer-substring-no-properties
3603 (point)
3604 (progn (skip-syntax-forward "w_")
3605 (point))))))
3606 @end group
3607 @group
3608 (defun sample-smie-backward-token ()
3609 (forward-comment (- (point)))
3610 (cond
3611 ((looking-back sample-keywords-regexp (- (point) 2) t)
3612 (goto-char (match-beginning 0))
3613 (match-string-no-properties 0))
3614 (t (buffer-substring-no-properties
3615 (point)
3616 (progn (skip-syntax-backward "w_")
3617 (point))))))
3618 @end group
3619 @end example
3620
3621 Notice how those lexers return the empty string when in front of
3622 parentheses. This is because SMIE automatically takes care of the
3623 parentheses defined in the syntax table. More specifically if the lexer
3624 returns nil or an empty string, SMIE tries to handle the corresponding
3625 text as a sexp according to syntax tables.
3626
3627 @node SMIE Tricks
3628 @subsubsection Living With a Weak Parser
3629
3630 The parsing technique used by SMIE does not allow tokens to behave
3631 differently in different contexts. For most programming languages, this
3632 manifests itself by precedence conflicts when converting the
3633 BNF grammar.
3634
3635 Sometimes, those conflicts can be worked around by expressing the
3636 grammar slightly differently. For example, for Modula-2 it might seem
3637 natural to have a BNF grammar that looks like this:
3638
3639 @example
3640 ...
3641 (inst ("IF" exp "THEN" insts "ELSE" insts "END")
3642 ("CASE" exp "OF" cases "END")
3643 ...)
3644 (cases (cases "|" cases)
3645 (caselabel ":" insts)
3646 ("ELSE" insts))
3647 ...
3648 @end example
3649
3650 But this will create conflicts for @code{"ELSE"}: on the one hand, the
3651 IF rule implies (among many other things) that @code{"ELSE" = "END"};
3652 but on the other hand, since @code{"ELSE"} appears within @code{cases},
3653 which appears left of @code{"END"}, we also have @code{"ELSE" > "END"}.
3654 We can solve the conflict either by using:
3655 @example
3656 ...
3657 (inst ("IF" exp "THEN" insts "ELSE" insts "END")
3658 ("CASE" exp "OF" cases "END")
3659 ("CASE" exp "OF" cases "ELSE" insts "END")
3660 ...)
3661 (cases (cases "|" cases) (caselabel ":" insts))
3662 ...
3663 @end example
3664 or
3665 @example
3666 ...
3667 (inst ("IF" exp "THEN" else "END")
3668 ("CASE" exp "OF" cases "END")
3669 ...)
3670 (else (insts "ELSE" insts))
3671 (cases (cases "|" cases) (caselabel ":" insts) (else))
3672 ...
3673 @end example
3674
3675 Reworking the grammar to try and solve conflicts has its downsides, tho,
3676 because SMIE assumes that the grammar reflects the logical structure of
3677 the code, so it is preferable to keep the BNF closer to the intended
3678 abstract syntax tree.
3679
3680 Other times, after careful consideration you may conclude that those
3681 conflicts are not serious and simply resolve them via the
3682 @var{resolvers} argument of @code{smie-bnf->prec2}. Usually this is
3683 because the grammar is simply ambiguous: the conflict does not affect
3684 the set of programs described by the grammar, but only the way those
3685 programs are parsed. This is typically the case for separators and
3686 associative infix operators, where you want to add a resolver like
3687 @code{'((assoc "|"))}. Another case where this can happen is for the
3688 classic @emph{dangling else} problem, where you will use @code{'((assoc
3689 "else" "then"))}. It can also happen for cases where the conflict is
3690 real and cannot really be resolved, but it is unlikely to pose a problem
3691 in practice.
3692
3693 Finally, in many cases some conflicts will remain despite all efforts to
3694 restructure the grammar. Do not despair: while the parser cannot be
3695 made more clever, you can make the lexer as smart as you want. So, the
3696 solution is then to look at the tokens involved in the conflict and to
3697 split one of those tokens into 2 (or more) different tokens. E.g. if
3698 the grammar needs to distinguish between two incompatible uses of the
3699 token @code{"begin"}, make the lexer return different tokens (say
3700 @code{"begin-fun"} and @code{"begin-plain"}) depending on which kind of
3701 @code{"begin"} it finds. This pushes the work of distinguishing the
3702 different cases to the lexer, which will thus have to look at the
3703 surrounding text to find ad-hoc clues.
3704
3705 @node SMIE Indentation
3706 @subsubsection Specifying Indentation Rules
3707
3708 Based on the provided grammar, SMIE will be able to provide automatic
3709 indentation without any extra effort. But in practice, this default
3710 indentation style will probably not be good enough. You will want to
3711 tweak it in many different cases.
3712
3713 SMIE indentation is based on the idea that indentation rules should be
3714 as local as possible. To this end, it relies on the idea of
3715 @emph{virtual} indentation, which is the indentation that a particular
3716 program point would have if it were at the beginning of a line.
3717 Of course, if that program point is indeed at the beginning of a line,
3718 its virtual indentation is its current indentation. But if not, then
3719 SMIE uses the indentation algorithm to compute the virtual indentation
3720 of that point. Now in practice, the virtual indentation of a program
3721 point does not have to be identical to the indentation it would have if
3722 we inserted a newline before it. To see how this works, the SMIE rule
3723 for indentation after a @code{@{} in C does not care whether the
3724 @code{@{} is standing on a line of its own or is at the end of the
3725 preceding line. Instead, these different cases are handled in the
3726 indentation rule that decides how to indent before a @code{@{}.
3727
3728 Another important concept is the notion of @emph{parent}: The
3729 @emph{parent} of a token, is the head token of the nearest enclosing
3730 syntactic construct. For example, the parent of an @code{else} is the
3731 @code{if} to which it belongs, and the parent of an @code{if}, in turn,
3732 is the lead token of the surrounding construct. The command
3733 @code{backward-sexp} jumps from a token to its parent, but there are
3734 some caveats: for @emph{openers} (tokens which start a construct, like
3735 @code{if}), you need to start with point before the token, while for
3736 others you need to start with point after the token.
3737 @code{backward-sexp} stops with point before the parent token if that is
3738 the @emph{opener} of the token of interest, and otherwise it stops with
3739 point after the parent token.
3740
3741 SMIE indentation rules are specified using a function that takes two
3742 arguments @var{method} and @var{arg} where the meaning of @var{arg} and the
3743 expected return value depend on @var{method}.
3744
3745 @var{method} can be:
3746 @itemize
3747 @item
3748 @code{:after}, in which case @var{arg} is a token and the function
3749 should return the @var{offset} to use for indentation after @var{arg}.
3750 @item
3751 @code{:before}, in which case @var{arg} is a token and the function
3752 should return the @var{offset} to use to indent @var{arg} itself.
3753 @item
3754 @code{:elem}, in which case the function should return either the offset
3755 to use to indent function arguments (if @var{arg} is the symbol
3756 @code{arg}) or the basic indentation step (if @var{arg} is the symbol
3757 @code{basic}).
3758 @item
3759 @code{:list-intro}, in which case @var{arg} is a token and the function
3760 should return non-@code{nil} if the token is followed by a list of
3761 expressions (not separated by any token) rather than an expression.
3762 @end itemize
3763
3764 When @var{arg} is a token, the function is called with point just before
3765 that token. A return value of nil always means to fallback on the
3766 default behavior, so the function should return nil for arguments it
3767 does not expect.
3768
3769 @var{offset} can be:
3770 @itemize
3771 @item
3772 @code{nil}: use the default indentation rule.
3773 @item
3774 @code{(column . @var{column})}: indent to column @var{column}.
3775 @item
3776 @var{number}: offset by @var{number}, relative to a base token which is
3777 the current token for @code{:after} and its parent for @code{:before}.
3778 @end itemize
3779
3780 @node SMIE Indentation Helpers
3781 @subsubsection Helper Functions for Indentation Rules
3782
3783 SMIE provides various functions designed specifically for use in the
3784 indentation rules function (several of those functions break if used in
3785 another context). These functions all start with the prefix
3786 @code{smie-rule-}.
3787
3788 @defun smie-rule-bolp
3789 Return non-@code{nil} if the current token is the first on the line.
3790 @end defun
3791
3792 @defun smie-rule-hanging-p
3793 Return non-@code{nil} if the current token is @emph{hanging}.
3794 A token is @emph{hanging} if it is the last token on the line
3795 and if it is preceded by other tokens: a lone token on a line is not
3796 hanging.
3797 @end defun
3798
3799 @defun smie-rule-next-p &rest tokens
3800 Return non-@code{nil} if the next token is among @var{tokens}.
3801 @end defun
3802
3803 @defun smie-rule-prev-p &rest tokens
3804 Return non-@code{nil} if the previous token is among @var{tokens}.
3805 @end defun
3806
3807 @defun smie-rule-parent-p &rest parents
3808 Return non-@code{nil} if the current token's parent is among @var{parents}.
3809 @end defun
3810
3811 @defun smie-rule-sibling-p
3812 Return non-nil if the current token's parent is actually a sibling.
3813 This is the case for example when the parent of a @code{","} is just the
3814 previous @code{","}.
3815 @end defun
3816
3817 @defun smie-rule-parent &optional offset
3818 Return the proper offset to align the current token with the parent.
3819 If non-@code{nil}, @var{offset} should be an integer giving an
3820 additional offset to apply.
3821 @end defun
3822
3823 @defun smie-rule-separator method
3824 Indent current token as a @emph{separator}.
3825
3826 By @emph{separator}, we mean here a token whose sole purpose is to
3827 separate various elements within some enclosing syntactic construct, and
3828 which does not have any semantic significance in itself (i.e. it would
3829 typically not exist as a node in an abstract syntax tree).
3830
3831 Such a token is expected to have an associative syntax and be closely
3832 tied to its syntactic parent. Typical examples are @code{","} in lists
3833 of arguments (enclosed inside parentheses), or @code{";"} in sequences
3834 of instructions (enclosed in a @code{@{...@}} or @code{begin...end}
3835 block).
3836
3837 @var{method} should be the method name that was passed to
3838 `smie-rules-function'.
3839 @end defun
3840
3841 @node SMIE Indentation Example
3842 @subsubsection Sample Indentation Rules
3843
3844 Here is an example of an indentation function:
3845
3846 @example
3847 (defun sample-smie-rules (kind token)
3848 (pcase (cons kind token)
3849 (`(:elem . basic) sample-indent-basic)
3850 (`(,_ . ",") (smie-rule-separator kind))
3851 (`(:after . ":=") sample-indent-basic)
3852 (`(:before . ,(or `"begin" `"(" `"@{")))
3853 (if (smie-rule-hanging-p) (smie-rule-parent)))
3854 (`(:before . "if")
3855 (and (not (smie-rule-bolp)) (smie-rule-prev-p "else")
3856 (smie-rule-parent)))))
3857 @end example
3858
3859 @noindent
3860 A few things to note:
3861
3862 @itemize
3863 @item
3864 The first case indicates the basic indentation increment to use.
3865 If @code{sample-indent-basic} is nil, then SMIE uses the global
3866 setting @code{smie-indent-basic}. The major mode could have set
3867 @code{smie-indent-basic} buffer-locally instead, but that
3868 is discouraged.
3869
3870 @item
3871 The rule for the token @code{","} make SMIE try to be more clever when
3872 the comma separator is placed at the beginning of lines. It tries to
3873 outdent the separator so as to align the code after the comma; for
3874 example:
3875
3876 @example
3877 x = longfunctionname (
3878 arg1
3879 , arg2
3880 );
3881 @end example
3882
3883 @item
3884 The rule for indentation after @code{":="} exists because otherwise
3885 SMIE would treat @code{":="} as an infix operator and would align the
3886 right argument with the left one.
3887
3888 @item
3889 The rule for indentation before @code{"begin"} is an example of the use
3890 of virtual indentation: This rule is used only when @code{"begin"} is
3891 hanging, which can happen only when @code{"begin"} is not at the
3892 beginning of a line. So this is not used when indenting
3893 @code{"begin"} itself but only when indenting something relative to this
3894 @code{"begin"}. Concretely, this rule changes the indentation from:
3895
3896 @example
3897 if x > 0 then begin
3898 dosomething(x);
3899 end
3900 @end example
3901 to
3902 @example
3903 if x > 0 then begin
3904 dosomething(x);
3905 end
3906 @end example
3907
3908 @item
3909 The rule for indentation before @code{"if"} is similar to the one for
3910 @code{"begin"}, but where the purpose is to treat @code{"else if"}
3911 as a single unit, so as to align a sequence of tests rather than indent
3912 each test further to the right. This function does this only in the
3913 case where the @code{"if"} is not placed on a separate line, hence the
3914 @code{smie-rule-bolp} test.
3915
3916 If we know that the @code{"else"} is always aligned with its @code{"if"}
3917 and is always at the beginning of a line, we can use a more efficient
3918 rule:
3919 @example
3920 ((equal token "if")
3921 (and (not (smie-rule-bolp))
3922 (smie-rule-prev-p "else")
3923 (save-excursion
3924 (sample-smie-backward-token)
3925 (cons 'column (current-column)))))
3926 @end example
3927
3928 The advantage of this formulation is that it reuses the indentation of
3929 the previous @code{"else"}, rather than going all the way back to the
3930 first @code{"if"} of the sequence.
3931 @end itemize
3932
3933 @node Desktop Save Mode
3934 @section Desktop Save Mode
3935 @cindex desktop save mode
3936
3937 @dfn{Desktop Save Mode} is a feature to save the state of Emacs from
3938 one session to another. The user-level commands for using Desktop
3939 Save Mode are described in the GNU Emacs Manual (@pxref{Saving Emacs
3940 Sessions,,, emacs, the GNU Emacs Manual}). Modes whose buffers visit
3941 a file, don't have to do anything to use this feature.
3942
3943 For buffers not visiting a file to have their state saved, the major
3944 mode must bind the buffer local variable @code{desktop-save-buffer} to
3945 a non-@code{nil} value.
3946
3947 @defvar desktop-save-buffer
3948 If this buffer-local variable is non-@code{nil}, the buffer will have
3949 its state saved in the desktop file at desktop save. If the value is
3950 a function, it is called at desktop save with argument
3951 @var{desktop-dirname}, and its value is saved in the desktop file along
3952 with the state of the buffer for which it was called. When file names
3953 are returned as part of the auxiliary information, they should be
3954 formatted using the call
3955
3956 @example
3957 (desktop-file-name @var{file-name} @var{desktop-dirname})
3958 @end example
3959
3960 @end defvar
3961
3962 For buffers not visiting a file to be restored, the major mode must
3963 define a function to do the job, and that function must be listed in
3964 the alist @code{desktop-buffer-mode-handlers}.
3965
3966 @defvar desktop-buffer-mode-handlers
3967 Alist with elements
3968
3969 @example
3970 (@var{major-mode} . @var{restore-buffer-function})
3971 @end example
3972
3973 The function @var{restore-buffer-function} will be called with
3974 argument list
3975
3976 @example
3977 (@var{buffer-file-name} @var{buffer-name} @var{desktop-buffer-misc})
3978 @end example
3979
3980 and it should return the restored buffer.
3981 Here @var{desktop-buffer-misc} is the value returned by the function
3982 optionally bound to @code{desktop-save-buffer}.
3983 @end defvar
3984
3985 @ignore
3986 Local Variables:
3987 fill-column: 72
3988 End:
3989 @end ignore