1 This is ../info/lispref.info, produced by makeinfo version 4.0 from
4 INFO-DIR-SECTION XEmacs Editor
6 * Lispref: (lispref). XEmacs Lisp Reference Manual.
11 GNU Emacs Lisp Reference Manual Second Edition (v2.01), May 1993 GNU
12 Emacs Lisp Reference Manual Further Revised (v2.02), August 1993 Lucid
13 Emacs Lisp Reference Manual (for 19.10) First Edition, March 1994
14 XEmacs Lisp Programmer's Manual (for 19.12) Second Edition, April 1995
15 GNU Emacs Lisp Reference Manual v2.4, June 1995 XEmacs Lisp
16 Programmer's Manual (for 19.13) Third Edition, July 1995 XEmacs Lisp
17 Reference Manual (for 19.14 and 20.0) v3.1, March 1996 XEmacs Lisp
18 Reference Manual (for 19.15 and 20.1, 20.2, 20.3) v3.2, April, May,
19 November 1997 XEmacs Lisp Reference Manual (for 21.0) v3.3, April 1998
21 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995 Free Software
22 Foundation, Inc. Copyright (C) 1994, 1995 Sun Microsystems, Inc.
23 Copyright (C) 1995, 1996 Ben Wing.
25 Permission is granted to make and distribute verbatim copies of this
26 manual provided the copyright notice and this permission notice are
27 preserved on all copies.
29 Permission is granted to copy and distribute modified versions of
30 this manual under the conditions for verbatim copying, provided that the
31 entire resulting derived work is distributed under the terms of a
32 permission notice identical to this one.
34 Permission is granted to copy and distribute translations of this
35 manual into another language, under the above conditions for modified
36 versions, except that this permission notice may be stated in a
37 translation approved by the Foundation.
39 Permission is granted to copy and distribute modified versions of
40 this manual under the conditions for verbatim copying, provided also
41 that the section entitled "GNU General Public License" is included
42 exactly as in the original, and provided that the entire resulting
43 derived work is distributed under the terms of a permission notice
44 identical to this one.
46 Permission is granted to copy and distribute translations of this
47 manual into another language, under the above conditions for modified
48 versions, except that the section entitled "GNU General Public License"
49 may be included in a translation approved by the Free Software
50 Foundation instead of in the original English.
53 File: lispref.info, Node: Major Modes, Next: Minor Modes, Up: Modes
58 Major modes specialize XEmacs for editing particular kinds of text.
59 Each buffer has only one major mode at a time.
61 The least specialized major mode is called "Fundamental mode". This
62 mode has no mode-specific definitions or variable settings, so each
63 XEmacs command behaves in its default manner, and each option is in its
64 default state. All other major modes redefine various keys and options.
65 For example, Lisp Interaction mode provides special key bindings for
66 <LFD> (`eval-print-last-sexp'), <TAB> (`lisp-indent-line'), and other
69 When you need to write several editing commands to help you perform a
70 specialized editing task, creating a new major mode is usually a good
71 idea. In practice, writing a major mode is easy (in contrast to
72 writing a minor mode, which is often difficult).
74 If the new mode is similar to an old one, it is often unwise to
75 modify the old one to serve two purposes, since it may become harder to
76 use and maintain. Instead, copy and rename an existing major mode
77 definition and alter the copy--or define a "derived mode" (*note
78 Derived Modes::). For example, Rmail Edit mode, which is in
79 `emacs/lisp/rmailedit.el', is a major mode that is very similar to Text
80 mode except that it provides three additional commands. Its definition
81 is distinct from that of Text mode, but was derived from it.
83 Rmail Edit mode is an example of a case where one piece of text is
84 put temporarily into a different major mode so it can be edited in a
85 different way (with ordinary XEmacs commands rather than Rmail). In
86 such cases, the temporary major mode usually has a command to switch
87 back to the buffer's usual mode (Rmail mode, in this case). You might
88 be tempted to present the temporary redefinitions inside a recursive
89 edit and restore the usual ones when the user exits; but this is a bad
90 idea because it constrains the user's options when it is done in more
91 than one buffer: recursive edits must be exited most-recently-entered
92 first. Using alternative major modes avoids this limitation. *Note
95 The standard XEmacs Lisp library directory contains the code for
96 several major modes, in files including `text-mode.el', `texinfo.el',
97 `lisp-mode.el', `c-mode.el', and `rmail.el'. You can look at these
98 libraries to see how modes are written. Text mode is perhaps the
99 simplest major mode aside from Fundamental mode. Rmail mode is a
100 complicated and specialized mode.
104 * Major Mode Conventions:: Coding conventions for keymaps, etc.
105 * Example Major Modes:: Text mode and Lisp modes.
106 * Auto Major Mode:: How XEmacs chooses the major mode automatically.
107 * Mode Help:: Finding out how to use a mode.
108 * Derived Modes:: Defining a new major mode based on another major
112 File: lispref.info, Node: Major Mode Conventions, Next: Example Major Modes, Up: Major Modes
114 Major Mode Conventions
115 ----------------------
117 The code for existing major modes follows various coding conventions,
118 including conventions for local keymap and syntax table initialization,
119 global names, and hooks. Please follow these conventions when you
120 define a new major mode:
122 * Define a command whose name ends in `-mode', with no arguments,
123 that switches to the new mode in the current buffer. This command
124 should set up the keymap, syntax table, and local variables in an
125 existing buffer without changing the buffer's text.
127 * Write a documentation string for this command that describes the
128 special commands available in this mode. `C-h m'
129 (`describe-mode') in your mode will display this string.
131 The documentation string may include the special documentation
132 substrings, `\[COMMAND]', `\{KEYMAP}', and `\<KEYMAP>', that
133 enable the documentation to adapt automatically to the user's own
134 key bindings. *Note Keys in Documentation::.
136 * The major mode command should start by calling
137 `kill-all-local-variables'. This is what gets rid of the local
138 variables of the major mode previously in effect.
140 * The major mode command should set the variable `major-mode' to the
141 major mode command symbol. This is how `describe-mode' discovers
142 which documentation to print.
144 * The major mode command should set the variable `mode-name' to the
145 "pretty" name of the mode, as a string. This appears in the mode
148 * Since all global names are in the same name space, all the global
149 variables, constants, and functions that are part of the mode
150 should have names that start with the major mode name (or with an
151 abbreviation of it if the name is long). *Note Style Tips::.
153 * The major mode should usually have its own keymap, which is used
154 as the local keymap in all buffers in that mode. The major mode
155 function should call `use-local-map' to install this local map.
156 *Note Active Keymaps::, for more information.
158 This keymap should be kept in a global variable named
159 `MODENAME-mode-map'. Normally the library that defines the mode
162 * The mode may have its own syntax table or may share one with other
163 related modes. If it has its own syntax table, it should store
164 this in a variable named `MODENAME-mode-syntax-table'. *Note
167 * The mode may have its own abbrev table or may share one with other
168 related modes. If it has its own abbrev table, it should store
169 this in a variable named `MODENAME-mode-abbrev-table'. *Note
172 * Use `defvar' to set mode-related variables, so that they are not
173 reinitialized if they already have a value. (Such reinitialization
174 could discard customizations made by the user.)
176 * To make a buffer-local binding for an Emacs customization
177 variable, use `make-local-variable' in the major mode command, not
178 `make-variable-buffer-local'. The latter function would make the
179 variable local to every buffer in which it is subsequently set,
180 which would affect buffers that do not use this mode. It is
181 undesirable for a mode to have such global effects. *Note
182 Buffer-Local Variables::.
184 It's ok to use `make-variable-buffer-local', if you wish, for a
185 variable used only within a single Lisp package.
187 * Each major mode should have a "mode hook" named
188 `MODENAME-mode-hook'. The major mode command should run that
189 hook, with `run-hooks', as the very last thing it does. *Note
192 * The major mode command may also run the hooks of some more basic
193 modes. For example, `indented-text-mode' runs `text-mode-hook' as
194 well as `indented-text-mode-hook'. It may run these other hooks
195 immediately before the mode's own hook (that is, after everything
196 else), or it may run them earlier.
198 * If something special should be done if the user switches a buffer
199 from this mode to any other major mode, the mode can set a local
200 value for `change-major-mode-hook'.
202 * If this mode is appropriate only for specially-prepared text, then
203 the major mode command symbol should have a property named
204 `mode-class' with value `special', put on as follows:
206 (put 'funny-mode 'mode-class 'special)
208 This tells XEmacs that new buffers created while the current
209 buffer has Funny mode should not inherit Funny mode. Modes such
210 as Dired, Rmail, and Buffer List use this feature.
212 * If you want to make the new mode the default for files with certain
213 recognizable names, add an element to `auto-mode-alist' to select
214 the mode for those file names. If you define the mode command to
215 autoload, you should add this element in the same file that calls
216 `autoload'. Otherwise, it is sufficient to add the element in the
217 file that contains the mode definition. *Note Auto Major Mode::.
219 * In the documentation, you should provide a sample `autoload' form
220 and an example of how to add to `auto-mode-alist', that users can
221 include in their `.emacs' files.
223 * The top-level forms in the file defining the mode should be
224 written so that they may be evaluated more than once without
225 adverse consequences. Even if you never load the file more than
226 once, someone else will.
228 - Variable: change-major-mode-hook
229 This normal hook is run by `kill-all-local-variables' before it
230 does anything else. This gives major modes a way to arrange for
231 something special to be done if the user switches to a different
232 major mode. For best results, make this variable buffer-local, so
233 that it will disappear after doing its job and will not interfere
234 with the subsequent major mode. *Note Hooks::.
237 File: lispref.info, Node: Example Major Modes, Next: Auto Major Mode, Prev: Major Mode Conventions, Up: Major Modes
242 Text mode is perhaps the simplest mode besides Fundamental mode.
243 Here are excerpts from `text-mode.el' that illustrate many of the
244 conventions listed above:
246 ;; Create mode-specific tables.
247 (defvar text-mode-syntax-table nil
248 "Syntax table used while in text mode.")
250 (if text-mode-syntax-table
251 () ; Do not change the table if it is already set up.
252 (setq text-mode-syntax-table (make-syntax-table))
253 (modify-syntax-entry ?\" ". " text-mode-syntax-table)
254 (modify-syntax-entry ?\\ ". " text-mode-syntax-table)
255 (modify-syntax-entry ?' "w " text-mode-syntax-table))
257 (defvar text-mode-abbrev-table nil
258 "Abbrev table used while in text mode.")
259 (define-abbrev-table 'text-mode-abbrev-table ())
261 (defvar text-mode-map nil) ; Create a mode-specific keymap.
264 () ; Do not change the keymap if it is already set up.
265 (setq text-mode-map (make-sparse-keymap))
266 (define-key text-mode-map "\t" 'tab-to-tab-stop)
267 (define-key text-mode-map "\es" 'center-line)
268 (define-key text-mode-map "\eS" 'center-paragraph))
270 Here is the complete major mode function definition for Text mode:
273 "Major mode for editing text intended for humans to read.
274 Special commands: \\{text-mode-map}
275 Turning on text-mode runs the hook `text-mode-hook'."
277 (kill-all-local-variables)
278 (use-local-map text-mode-map) ; This provides the local keymap.
279 (setq mode-name "Text") ; This name goes into the modeline.
280 (setq major-mode 'text-mode) ; This is how `describe-mode'
281 ; finds the doc string to print.
282 (setq local-abbrev-table text-mode-abbrev-table)
283 (set-syntax-table text-mode-syntax-table)
284 (run-hooks 'text-mode-hook)) ; Finally, this permits the user to
285 ; customize the mode with a hook.
287 The three Lisp modes (Lisp mode, Emacs Lisp mode, and Lisp
288 Interaction mode) have more features than Text mode and the code is
289 correspondingly more complicated. Here are excerpts from
290 `lisp-mode.el' that illustrate how these modes are written.
292 ;; Create mode-specific table variables.
293 (defvar lisp-mode-syntax-table nil "")
294 (defvar emacs-lisp-mode-syntax-table nil "")
295 (defvar lisp-mode-abbrev-table nil "")
297 (if (not emacs-lisp-mode-syntax-table) ; Do not change the table
298 ; if it is already set.
300 (setq emacs-lisp-mode-syntax-table (make-syntax-table))
302 ;; Set syntax of chars up to 0 to class of chars that are
303 ;; part of symbol names but not words.
304 ;; (The number 0 is `48' in the ASCII character set.)
306 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
309 ;; Set the syntax for other characters.
310 (modify-syntax-entry ? " " emacs-lisp-mode-syntax-table)
311 (modify-syntax-entry ?\t " " emacs-lisp-mode-syntax-table)
313 (modify-syntax-entry ?\( "() " emacs-lisp-mode-syntax-table)
314 (modify-syntax-entry ?\) ")( " emacs-lisp-mode-syntax-table)
316 ;; Create an abbrev table for lisp-mode.
317 (define-abbrev-table 'lisp-mode-abbrev-table ())
319 Much code is shared among the three Lisp modes. The following
320 function sets various variables; it is called by each of the major Lisp
323 (defun lisp-mode-variables (lisp-syntax)
324 ;; The `lisp-syntax' argument is `nil' in Emacs Lisp mode,
325 ;; and `t' in the other two Lisp modes.
327 (if (not lisp-mode-syntax-table)
328 ;; The Emacs Lisp mode syntax table always exists, but
329 ;; the Lisp Mode syntax table is created the first time a
330 ;; mode that needs it is called. This is to save space.
331 (progn (setq lisp-mode-syntax-table
332 (copy-syntax-table emacs-lisp-mode-syntax-table))
333 ;; Change some entries for Lisp mode.
334 (modify-syntax-entry ?\| "\" "
335 lisp-mode-syntax-table)
336 (modify-syntax-entry ?\[ "_ "
337 lisp-mode-syntax-table)
338 (modify-syntax-entry ?\] "_ "
339 lisp-mode-syntax-table)))
340 (set-syntax-table lisp-mode-syntax-table)))
341 (setq local-abbrev-table lisp-mode-abbrev-table)
344 Functions such as `forward-paragraph' use the value of the
345 `paragraph-start' variable. Since Lisp code is different from ordinary
346 text, the `paragraph-start' variable needs to be set specially to
347 handle Lisp. Also, comments are indented in a special fashion in Lisp
348 and the Lisp modes need their own mode-specific
349 `comment-indent-function'. The code to set these variables is the rest
350 of `lisp-mode-variables'.
352 (make-local-variable 'paragraph-start)
353 ;; Having `^' is not clean, but `page-delimiter'
354 ;; has them too, and removing those is a pain.
355 (setq paragraph-start (concat "^$\\|" page-delimiter))
357 (make-local-variable 'comment-indent-function)
358 (setq comment-indent-function 'lisp-comment-indent))
360 Each of the different Lisp modes has a slightly different keymap.
361 For example, Lisp mode binds `C-c C-l' to `run-lisp', but the other
362 Lisp modes do not. However, all Lisp modes have some commands in
363 common. The following function adds these common commands to a given
366 (defun lisp-mode-commands (map)
367 (define-key map "\e\C-q" 'indent-sexp)
368 (define-key map "\177" 'backward-delete-char-untabify)
369 (define-key map "\t" 'lisp-indent-line))
371 Here is an example of using `lisp-mode-commands' to initialize a
372 keymap, as part of the code for Emacs Lisp mode. First we declare a
373 variable with `defvar' to hold the mode-specific keymap. When this
374 `defvar' executes, it sets the variable to `nil' if it was void. Then
375 we set up the keymap if the variable is `nil'.
377 This code avoids changing the keymap or the variable if it is already
378 set up. This lets the user customize the keymap.
380 (defvar emacs-lisp-mode-map () "")
381 (if emacs-lisp-mode-map
383 (setq emacs-lisp-mode-map (make-sparse-keymap))
384 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun)
385 (lisp-mode-commands emacs-lisp-mode-map))
387 Finally, here is the complete major mode function definition for
390 (defun emacs-lisp-mode ()
391 "Major mode for editing Lisp code to run in XEmacs.
393 Delete converts tabs to spaces as it moves back.
394 Blank lines separate paragraphs. Semicolons start comments.
395 \\{emacs-lisp-mode-map}
396 Entry to this mode runs the hook `emacs-lisp-mode-hook'."
398 (kill-all-local-variables)
399 (use-local-map emacs-lisp-mode-map) ; This provides the local keymap.
400 (set-syntax-table emacs-lisp-mode-syntax-table)
401 (setq major-mode 'emacs-lisp-mode) ; This is how `describe-mode'
402 ; finds out what to describe.
403 (setq mode-name "Emacs-Lisp") ; This goes into the modeline.
404 (lisp-mode-variables nil) ; This defines various variables.
405 (run-hooks 'emacs-lisp-mode-hook)) ; This permits the user to use a
406 ; hook to customize the mode.
409 File: lispref.info, Node: Auto Major Mode, Next: Mode Help, Prev: Example Major Modes, Up: Major Modes
411 How XEmacs Chooses a Major Mode
412 -------------------------------
414 Based on information in the file name or in the file itself, XEmacs
415 automatically selects a major mode for the new buffer when a file is
418 - Command: fundamental-mode
419 Fundamental mode is a major mode that is not specialized for
420 anything in particular. Other major modes are defined in effect
421 by comparison with this one--their definitions say what to change,
422 starting from Fundamental mode. The `fundamental-mode' function
423 does _not_ run any hooks; you're not supposed to customize it.
424 (If you want Emacs to behave differently in Fundamental mode,
425 change the _global_ state of Emacs.)
427 - Command: normal-mode &optional find-file
428 This function establishes the proper major mode and local variable
429 bindings for the current buffer. First it calls `set-auto-mode',
430 then it runs `hack-local-variables' to parse, and bind or evaluate
431 as appropriate, any local variables.
433 If the FIND-FILE argument to `normal-mode' is non-`nil',
434 `normal-mode' assumes that the `find-file' function is calling it.
435 In this case, it may process a local variables list at the end of
436 the file and in the `-*-' line. The variable
437 `enable-local-variables' controls whether to do so.
439 If you run `normal-mode' interactively, the argument FIND-FILE is
440 normally `nil'. In this case, `normal-mode' unconditionally
441 processes any local variables list. *Note Local Variables in
442 Files: (emacs)File variables, for the syntax of the local
443 variables section of a file.
445 `normal-mode' uses `condition-case' around the call to the major
446 mode function, so errors are caught and reported as a `File mode
447 specification error', followed by the original error message.
449 - User Option: enable-local-variables
450 This variable controls processing of local variables lists in files
451 being visited. A value of `t' means process the local variables
452 lists unconditionally; `nil' means ignore them; anything else means
453 ask the user what to do for each file. The default value is `t'.
455 - Variable: ignored-local-variables
456 This variable holds a list of variables that should not be set by
457 a local variables list. Any value specified for one of these
458 variables is ignored.
460 In addition to this list, any variable whose name has a non-`nil'
461 `risky-local-variable' property is also ignored.
463 - User Option: enable-local-eval
464 This variable controls processing of `Eval:' in local variables
465 lists in files being visited. A value of `t' means process them
466 unconditionally; `nil' means ignore them; anything else means ask
467 the user what to do for each file. The default value is `maybe'.
469 - Function: set-auto-mode
470 This function selects the major mode that is appropriate for the
471 current buffer. It may base its decision on the value of the `-*-'
472 line, on the visited file name (using `auto-mode-alist'), or on the
473 value of a local variable. However, this function does not look
474 for the `mode:' local variable near the end of a file; the
475 `hack-local-variables' function does that. *Note How Major Modes
476 are Chosen: (emacs)Choosing Modes.
478 - User Option: default-major-mode
479 This variable holds the default major mode for new buffers. The
480 standard value is `fundamental-mode'.
482 If the value of `default-major-mode' is `nil', XEmacs uses the
483 (previously) current buffer's major mode for the major mode of a
484 new buffer. However, if the major mode symbol has a `mode-class'
485 property with value `special', then it is not used for new buffers;
486 Fundamental mode is used instead. The modes that have this
487 property are those such as Dired and Rmail that are useful only
488 with text that has been specially prepared.
490 - Function: set-buffer-major-mode buffer
491 This function sets the major mode of BUFFER to the value of
492 `default-major-mode'. If that variable is `nil', it uses the
493 current buffer's major mode (if that is suitable).
495 The low-level primitives for creating buffers do not use this
496 function, but medium-level commands such as `switch-to-buffer' and
497 `find-file-noselect' use it whenever they create buffers.
499 - Variable: initial-major-mode
500 The value of this variable determines the major mode of the initial
501 `*scratch*' buffer. The value should be a symbol that is a major
502 mode command name. The default value is `lisp-interaction-mode'.
504 - Variable: auto-mode-alist
505 This variable contains an association list of file name patterns
506 (regular expressions; *note Regular Expressions::) and
507 corresponding major mode functions. Usually, the file name
508 patterns test for suffixes, such as `.el' and `.c', but this need
509 not be the case. An ordinary element of the alist looks like
510 `(REGEXP . MODE-FUNCTION)'.
514 (("^/tmp/fol/" . text-mode)
515 ("\\.texinfo\\'" . texinfo-mode)
516 ("\\.texi\\'" . texinfo-mode)
517 ("\\.el\\'" . emacs-lisp-mode)
522 When you visit a file whose expanded file name (*note File Name
523 Expansion::) matches a REGEXP, `set-auto-mode' calls the
524 corresponding MODE-FUNCTION. This feature enables XEmacs to select
525 the proper major mode for most files.
527 If an element of `auto-mode-alist' has the form `(REGEXP FUNCTION
528 t)', then after calling FUNCTION, XEmacs searches
529 `auto-mode-alist' again for a match against the portion of the file
530 name that did not match before.
532 This match-again feature is useful for uncompression packages: an
533 entry of the form `("\\.gz\\'" . FUNCTION)' can uncompress the file
534 and then put the uncompressed file in the proper mode according to
537 Here is an example of how to prepend several pattern pairs to
538 `auto-mode-alist'. (You might use this sort of expression in your
541 (setq auto-mode-alist
543 ;; File name starts with a dot.
544 '(("/\\.[^/]*\\'" . fundamental-mode)
545 ;; File name has no dot.
546 ("[^\\./]*\\'" . fundamental-mode)
547 ;; File name ends in `.C'.
548 ("\\.C\\'" . c++-mode))
551 - Variable: interpreter-mode-alist
552 This variable specifies major modes to use for scripts that
553 specify a command interpreter in an `#!' line. Its value is a
554 list of elements of the form `(INTERPRETER . MODE)'; for example,
555 `("perl" . perl-mode)' is one element present by default. The
556 element says to use mode MODE if the file specifies INTERPRETER.
558 This variable is applicable only when the `auto-mode-alist' does
559 not indicate which major mode to use.
561 - Function: hack-local-variables &optional force
562 This function parses, and binds or evaluates as appropriate, any
563 local variables for the current buffer.
565 The handling of `enable-local-variables' documented for
566 `normal-mode' actually takes place here. The argument FORCE
567 usually comes from the argument FIND-FILE given to `normal-mode'.
570 File: lispref.info, Node: Mode Help, Next: Derived Modes, Prev: Auto Major Mode, Up: Major Modes
572 Getting Help about a Major Mode
573 -------------------------------
575 The `describe-mode' function is used to provide information about
576 major modes. It is normally called with `C-h m'. The `describe-mode'
577 function uses the value of `major-mode', which is why every major mode
578 function needs to set the `major-mode' variable.
580 - Command: describe-mode
581 This function displays the documentation of the current major mode.
583 The `describe-mode' function calls the `documentation' function
584 using the value of `major-mode' as an argument. Thus, it displays
585 the documentation string of the major mode function. (*Note
586 Accessing Documentation::.)
588 - Variable: major-mode
589 This variable holds the symbol for the current buffer's major mode.
590 This symbol should have a function definition that is the command
591 to switch to that major mode. The `describe-mode' function uses
592 the documentation string of the function as the documentation of
596 File: lispref.info, Node: Derived Modes, Prev: Mode Help, Up: Major Modes
598 Defining Derived Modes
599 ----------------------
601 It's often useful to define a new major mode in terms of an existing
602 one. An easy way to do this is to use `define-derived-mode'.
604 - Macro: define-derived-mode variant parent name docstring body...
605 This construct defines VARIANT as a major mode command, using NAME
606 as the string form of the mode name.
608 The new command VARIANT is defined to call the function PARENT,
609 then override certain aspects of that parent mode:
611 * The new mode has its own keymap, named `VARIANT-map'.
612 `define-derived-mode' initializes this map to inherit from
613 `PARENT-map', if it is not already set.
615 * The new mode has its own syntax table, kept in the variable
616 `VARIANT-syntax-table'. `define-derived-mode' initializes
617 this variable by copying `PARENT-syntax-table', if it is not
620 * The new mode has its own abbrev table, kept in the variable
621 `VARIANT-abbrev-table'. `define-derived-mode' initializes
622 this variable by copying `PARENT-abbrev-table', if it is not
625 * The new mode has its own mode hook, `VARIANT-hook', which it
626 runs in standard fashion as the very last thing that it does.
627 (The new mode also runs the mode hook of PARENT as part of
630 In addition, you can specify how to override other aspects of
631 PARENT with BODY. The command VARIANT evaluates the forms in BODY
632 after setting up all its usual overrides, just before running
635 The argument DOCSTRING specifies the documentation string for the
636 new mode. If you omit DOCSTRING, `define-derived-mode' generates
637 a documentation string.
639 Here is a hypothetical example:
641 (define-derived-mode hypertext-mode
642 text-mode "Hypertext"
643 "Major mode for hypertext.
644 \\{hypertext-mode-map}"
645 (setq case-fold-search nil))
647 (define-key hypertext-mode-map
648 [down-mouse-3] 'do-hyper-link)
651 File: lispref.info, Node: Minor Modes, Next: Modeline Format, Prev: Major Modes, Up: Modes
656 A "minor mode" provides features that users may enable or disable
657 independently of the choice of major mode. Minor modes can be enabled
658 individually or in combination. Minor modes would be better named
659 "Generally available, optional feature modes" except that such a name is
662 A minor mode is not usually a modification of single major mode. For
663 example, Auto Fill mode may be used in any major mode that permits text
664 insertion. To be general, a minor mode must be effectively independent
665 of the things major modes do.
667 A minor mode is often much more difficult to implement than a major
668 mode. One reason is that you should be able to activate and deactivate
669 minor modes in any order. A minor mode should be able to have its
670 desired effect regardless of the major mode and regardless of the other
671 minor modes in effect.
673 Often the biggest problem in implementing a minor mode is finding a
674 way to insert the necessary hook into the rest of XEmacs. Minor mode
675 keymaps make this easier than it used to be.
679 * Minor Mode Conventions:: Tips for writing a minor mode.
680 * Keymaps and Minor Modes:: How a minor mode can have its own keymap.
683 File: lispref.info, Node: Minor Mode Conventions, Next: Keymaps and Minor Modes, Up: Minor Modes
685 Conventions for Writing Minor Modes
686 -----------------------------------
688 There are conventions for writing minor modes just as there are for
689 major modes. Several of the major mode conventions apply to minor
690 modes as well: those regarding the name of the mode initialization
691 function, the names of global symbols, and the use of keymaps and other
694 In addition, there are several conventions that are specific to
697 * Make a variable whose name ends in `-mode' to represent the minor
698 mode. Its value should enable or disable the mode (`nil' to
699 disable; anything else to enable.) We call this the "mode
702 This variable is used in conjunction with the `minor-mode-alist' to
703 display the minor mode name in the modeline. It can also enable
704 or disable a minor mode keymap. Individual commands or hooks can
705 also check the variable's value.
707 If you want the minor mode to be enabled separately in each buffer,
708 make the variable buffer-local.
710 * Define a command whose name is the same as the mode variable. Its
711 job is to enable and disable the mode by setting the variable.
713 The command should accept one optional argument. If the argument
714 is `nil', it should toggle the mode (turn it on if it is off, and
715 off if it is on). Otherwise, it should turn the mode on if the
716 argument is a positive integer, a symbol other than `nil' or `-',
717 or a list whose CAR is such an integer or symbol; it should turn
718 the mode off otherwise.
720 Here is an example taken from the definition of
721 `transient-mark-mode'. It shows the use of `transient-mark-mode'
722 as a variable that enables or disables the mode's behavior, and
723 also shows the proper way to toggle, enable or disable the minor
724 mode based on the raw prefix argument value.
726 (setq transient-mark-mode
727 (if (null arg) (not transient-mark-mode)
728 (> (prefix-numeric-value arg) 0)))
730 * Add an element to `minor-mode-alist' for each minor mode (*note
731 Modeline Variables::). This element should be a list of the
734 (MODE-VARIABLE STRING)
736 Here MODE-VARIABLE is the variable that controls enabling of the
737 minor mode, and STRING is a short string, starting with a space,
738 to represent the mode in the modeline. These strings must be
739 short so that there is room for several of them at once.
741 When you add an element to `minor-mode-alist', use `assq' to check
742 for an existing element, to avoid duplication. For example:
744 (or (assq 'leif-mode minor-mode-alist)
745 (setq minor-mode-alist
746 (cons '(leif-mode " Leif") minor-mode-alist)))
749 File: lispref.info, Node: Keymaps and Minor Modes, Prev: Minor Mode Conventions, Up: Minor Modes
751 Keymaps and Minor Modes
752 -----------------------
754 Each minor mode can have its own keymap, which is active when the
755 mode is enabled. To set up a keymap for a minor mode, add an element
756 to the alist `minor-mode-map-alist'. *Note Active Keymaps::.
758 One use of minor mode keymaps is to modify the behavior of certain
759 self-inserting characters so that they do something else as well as
760 self-insert. In general, this is the only way to do that, since the
761 facilities for customizing `self-insert-command' are limited to special
762 cases (designed for abbrevs and Auto Fill mode). (Do not try
763 substituting your own definition of `self-insert-command' for the
764 standard one. The editor command loop handles this function specially.)
767 File: lispref.info, Node: Modeline Format, Next: Hooks, Prev: Minor Modes, Up: Modes
772 Each Emacs window (aside from minibuffer windows) includes a
773 modeline, which displays status information about the buffer displayed
774 in the window. The modeline contains information about the buffer,
775 such as its name, associated file, depth of recursive editing, and the
776 major and minor modes.
778 This section describes how the contents of the modeline are
779 controlled. It is in the chapter on modes because much of the
780 information displayed in the modeline relates to the enabled major and
783 `modeline-format' is a buffer-local variable that holds a template
784 used to display the modeline of the current buffer. All windows for
785 the same buffer use the same `modeline-format' and their modelines
786 appear the same (except for scrolling percentages and line numbers).
788 The modeline of a window is normally updated whenever a different
789 buffer is shown in the window, or when the buffer's modified-status
790 changes from `nil' to `t' or vice-versa. If you modify any of the
791 variables referenced by `modeline-format' (*note Modeline Variables::),
792 you may want to force an update of the modeline so as to display the
795 - Function: redraw-modeline &optional all
796 Force redisplay of the current buffer's modeline. If ALL is
797 non-`nil', then force redisplay of all modelines.
799 The modeline is usually displayed in inverse video. This is
800 controlled using the `modeline' face. *Note Faces::.
804 * Modeline Data:: The data structure that controls the modeline.
805 * Modeline Variables:: Variables used in that data structure.
806 * %-Constructs:: Putting information into a modeline.
809 File: lispref.info, Node: Modeline Data, Next: Modeline Variables, Up: Modeline Format
811 The Data Structure of the Modeline
812 ----------------------------------
814 The modeline contents are controlled by a data structure of lists,
815 strings, symbols, and numbers kept in the buffer-local variable
816 `mode-line-format'. The data structure is called a "modeline
817 construct", and it is built in recursive fashion out of simpler modeline
818 constructs. The same data structure is used for constructing frame
819 titles (*note Frame Titles::).
821 - Variable: modeline-format
822 The value of this variable is a modeline construct with overall
823 responsibility for the modeline format. The value of this variable
824 controls which other variables are used to form the modeline text,
825 and where they appear.
827 A modeline construct may be as simple as a fixed string of text, but
828 it usually specifies how to use other variables to construct the text.
829 Many of these variables are themselves defined to have modeline
830 constructs as their values.
832 The default value of `modeline-format' incorporates the values of
833 variables such as `mode-name' and `minor-mode-alist'. Because of this,
834 very few modes need to alter `modeline-format'. For most purposes, it
835 is sufficient to alter the variables referenced by `modeline-format'.
837 A modeline construct may be a list, a symbol, or a string. If the
838 value is a list, each element may be a list, a symbol, or a string.
841 A string as a modeline construct is displayed verbatim in the mode
842 line except for "`%'-constructs". Decimal digits after the `%'
843 specify the field width for space filling on the right (i.e., the
844 data is left justified). *Note %-Constructs::.
847 A symbol as a modeline construct stands for its value. The value
848 of SYMBOL is used as a modeline construct, in place of SYMBOL.
849 However, the symbols `t' and `nil' are ignored; so is any symbol
852 There is one exception: if the value of SYMBOL is a string, it is
853 displayed verbatim: the `%'-constructs are not recognized.
855 `(STRING REST...) or (LIST REST...)'
856 A list whose first element is a string or list means to process
857 all the elements recursively and concatenate the results. This is
858 the most common form of mode line construct.
861 A list whose first element is a symbol is a conditional. Its
862 meaning depends on the value of SYMBOL. If the value is non-`nil',
863 the second element, THEN, is processed recursively as a modeline
864 element. But if the value of SYMBOL is `nil', the third element,
865 ELSE, is processed recursively. You may omit ELSE; then the mode
866 line element displays nothing if the value of SYMBOL is `nil'.
869 A list whose first element is an integer specifies truncation or
870 padding of the results of REST. The remaining elements REST are
871 processed recursively as modeline constructs and concatenated
872 together. Then the result is space filled (if WIDTH is positive)
873 or truncated (to -WIDTH columns, if WIDTH is negative) on the
876 For example, the usual way to show what percentage of a buffer is
877 above the top of the window is to use a list like this: `(-3
880 If you do alter `modeline-format' itself, the new value should use
881 the same variables that appear in the default value (*note Modeline
882 Variables::), rather than duplicating their contents or displaying the
883 information in another fashion. This way, customizations made by the
884 user or by Lisp programs (such as `display-time' and major modes) via
885 changes to those variables remain effective.
887 Here is an example of a `modeline-format' that might be useful for
888 `shell-mode', since it contains the hostname and default directory.
890 (setq modeline-format
894 (getenv "HOST") ; One element is not constant.
905 '(line-number-mode "L%l--")
910 File: lispref.info, Node: Modeline Variables, Next: %-Constructs, Prev: Modeline Data, Up: Modeline Format
912 Variables Used in the Modeline
913 ------------------------------
915 This section describes variables incorporated by the standard value
916 of `modeline-format' into the text of the mode line. There is nothing
917 inherently special about these variables; any other variables could
918 have the same effects on the modeline if `modeline-format' were changed
921 - Variable: modeline-modified
922 This variable holds the value of the modeline construct that
923 displays whether the current buffer is modified.
925 The default value of `modeline-modified' is `("--%1*%1+-")'. This
926 means that the modeline displays `--**-' if the buffer is
927 modified, `-----' if the buffer is not modified, `--%%-' if the
928 buffer is read only, and `--%*--' if the buffer is read only and
931 Changing this variable does not force an update of the modeline.
933 - Variable: modeline-buffer-identification
934 This variable identifies the buffer being displayed in the window.
935 Its default value is `("%F: %17b")', which means that it usually
936 displays `Emacs:' followed by seventeen characters of the buffer
937 name. (In a terminal frame, it displays the frame name instead of
938 `Emacs'; this has the effect of showing the frame number.) You may
939 want to change this in modes such as Rmail that do not behave like
942 - Variable: global-mode-string
943 This variable holds a modeline spec that appears in the mode line
944 by default, just after the buffer name. The command `display-time'
945 sets `global-mode-string' to refer to the variable
946 `display-time-string', which holds a string containing the time and
949 The `%M' construct substitutes the value of `global-mode-string',
950 but this is obsolete, since the variable is included directly in
953 - Variable: mode-name
954 This buffer-local variable holds the "pretty" name of the current
955 buffer's major mode. Each major mode should set this variable so
956 that the mode name will appear in the modeline.
958 - Variable: minor-mode-alist
959 This variable holds an association list whose elements specify how
960 the modeline should indicate that a minor mode is active. Each
961 element of the `minor-mode-alist' should be a two-element list:
963 (MINOR-MODE-VARIABLE MODELINE-STRING)
965 More generally, MODELINE-STRING can be any mode line spec. It
966 appears in the mode line when the value of MINOR-MODE-VARIABLE is
967 non-`nil', and not otherwise. These strings should begin with
968 spaces so that they don't run together. Conventionally, the
969 MINOR-MODE-VARIABLE for a specific mode is set to a non-`nil'
970 value when that minor mode is activated.
972 The default value of `minor-mode-alist' is:
975 => ((vc-mode vc-mode)
976 (abbrev-mode " Abbrev")
977 (overwrite-mode overwrite-mode)
978 (auto-fill-function " Fill")
979 (defining-kbd-macro " Def")
980 (isearch-mode isearch-mode))
982 `minor-mode-alist' is not buffer-local. The variables mentioned
983 in the alist should be buffer-local if the minor mode can be
984 enabled separately in each buffer.
986 - Variable: modeline-process
987 This buffer-local variable contains the modeline information on
988 process status in modes used for communicating with subprocesses.
989 It is displayed immediately following the major mode name, with no
990 intervening space. For example, its value in the `*shell*' buffer
991 is `(": %s")', which allows the shell to display its status along
992 with the major mode as: `(Shell: run)'. Normally this variable is
995 - Variable: default-modeline-format
996 This variable holds the default `modeline-format' for buffers that
997 do not override it. This is the same as `(default-value
1000 The default value of `default-modeline-format' is:
1004 modeline-buffer-identification
1013 (line-number-mode "L%l--")
1018 The variable `vc-mode', local in each buffer, records whether the
1019 buffer's visited file is maintained with version control, and, if
1020 so, which kind. Its value is `nil' for no version control, or a
1021 string that appears in the mode line.
1024 File: lispref.info, Node: %-Constructs, Prev: Modeline Variables, Up: Modeline Format
1026 `%'-Constructs in the ModeLine
1027 ------------------------------
1029 The following table lists the recognized `%'-constructs and what
1030 they mean. In any construct except `%%', you can add a decimal integer
1031 after the `%' to specify how many characters to display.
1034 The current buffer name, obtained with the `buffer-name' function.
1035 *Note Buffer Names::.
1038 The visited file name, obtained with the `buffer-file-name'
1039 function. *Note Buffer File Name::.
1042 The name of the selected frame.
1045 The current column number of point.
1048 The current line number of point.
1051 `%' if the buffer is read only (see `buffer-read-only');
1052 `*' if the buffer is modified (see `buffer-modified-p');
1053 `-' otherwise. *Note Buffer Modification::.
1056 `*' if the buffer is modified (see `buffer-modified-p');
1057 `%' if the buffer is read only (see `buffer-read-only');
1058 `-' otherwise. This differs from `%*' only for a modified
1059 read-only buffer. *Note Buffer Modification::.
1062 `*' if the buffer is modified, and `-' otherwise.
1065 The status of the subprocess belonging to the current buffer,
1066 obtained with `process-status'. *Note Process Information::.
1069 the current line number.
1072 the name of the selected frame; this is only meaningful under the
1073 X Window System. *Note Frame Name::.
1076 Whether the visited file is a text file or a binary file. (This
1077 is a meaningful distinction only on certain operating systems.)
1080 The percentage of the buffer text above the *top* of window, or
1081 `Top', `Bottom' or `All'.
1084 The percentage of the buffer text that is above the *bottom* of
1085 the window (which includes the text visible in the window, as well
1086 as the text above the top), plus `Top' if the top of the buffer is
1087 visible on screen; or `Bottom' or `All'.
1090 `Narrow' when narrowing is in effect; nothing otherwise (see
1091 `narrow-to-region' in *Note Narrowing::).
1094 An indication of the depth of recursive editing levels (not
1095 counting minibuffer levels): one `[' for each editing level.
1096 *Note Recursive Editing::.
1099 One `]' for each recursive editing level (not counting minibuffer
1103 The character `%'--this is how to include a literal `%' in a
1104 string in which `%'-constructs are allowed.
1107 Dashes sufficient to fill the remainder of the modeline.
1109 The following two `%'-constructs are still supported, but they are
1110 obsolete, since you can get the same results with the variables
1111 `mode-name' and `global-mode-string'.
1114 The value of `mode-name'.
1117 The value of `global-mode-string'. Currently, only `display-time'
1118 modifies the value of `global-mode-string'.