diff options
| author | Luc Teirlinck | 2005-05-14 16:16:44 +0000 |
|---|---|---|
| committer | Luc Teirlinck | 2005-05-14 16:16:44 +0000 |
| commit | 302691abbceed17e0bfbcf38b3ce1c87917c6f61 (patch) | |
| tree | 697592594c7ebc13b50e9005a313d44842828541 | |
| parent | 7177e2a315ac8610e397037c7eaddf78630c4279 (diff) | |
| download | emacs-302691abbceed17e0bfbcf38b3ce1c87917c6f61.tar.gz emacs-302691abbceed17e0bfbcf38b3ce1c87917c6f61.zip | |
(Modes): Update Menu.
(Hooks): Move to beginning of chapter.
(Major Modes): Update Menu.
(Major Mode Basics): New node, split off from `Major Modes'.
(Major Mode Conventions): Correct xref. Explain how to handle
auto-mode-alist if the major mode command has an autoload cookie.
(Auto Major Mode): Major update. Add magic-mode-alist.
(Derived Modes): Major update.
(Mode Line Format): Update Menu.
(Mode Line Basics): New node, split off from `Mode Line Format'.
| -rw-r--r-- | lispref/ChangeLog | 19 | ||||
| -rw-r--r-- | lispref/modes.texi | 526 |
2 files changed, 316 insertions, 229 deletions
diff --git a/lispref/ChangeLog b/lispref/ChangeLog index 94a67a6e037..6b0d147c586 100644 --- a/lispref/ChangeLog +++ b/lispref/ChangeLog | |||
| @@ -1,3 +1,22 @@ | |||
| 1 | 2005-05-14 Luc Teirlinck <teirllm@auburn.edu> | ||
| 2 | |||
| 3 | * modes.texi (Modes): Update Menu. | ||
| 4 | (Hooks): Move to beginning of chapter. | ||
| 5 | Most minor modes run mode hooks too. | ||
| 6 | `add-hook' can handle void hooks or hooks whose value is a single | ||
| 7 | function. | ||
| 8 | (Major Modes): Update Menu. | ||
| 9 | (Major Mode Basics): New node, split off from `Major Modes'. | ||
| 10 | (Major Mode Conventions): Correct xref. Explain how to handle | ||
| 11 | auto-mode-alist if the major mode command has an autoload cookie. | ||
| 12 | (Auto Major Mode): Major update. Add magic-mode-alist. | ||
| 13 | (Derived Modes): Major update. | ||
| 14 | (Mode Line Format): Update Menu. | ||
| 15 | (Mode Line Basics): New node, split off from `Mode Line Format'. | ||
| 16 | |||
| 17 | * loading.texi (Autoload): Mention `autoload cookie' as synonym | ||
| 18 | for `magic autoload comment'. Add index entries and anchor. | ||
| 19 | |||
| 1 | 2005-05-14 Richard M. Stallman <rms@gnu.org> | 20 | 2005-05-14 Richard M. Stallman <rms@gnu.org> |
| 2 | 21 | ||
| 3 | * tips.texi (Coding Conventions): Explain how important it is | 22 | * tips.texi (Coding Conventions): Explain how important it is |
diff --git a/lispref/modes.texi b/lispref/modes.texi index 47d9ded1346..6766f357adf 100644 --- a/lispref/modes.texi +++ b/lispref/modes.texi | |||
| @@ -20,6 +20,7 @@ user. For related topics such as keymaps and syntax tables, see | |||
| 20 | @ref{Keymaps}, and @ref{Syntax Tables}. | 20 | @ref{Keymaps}, and @ref{Syntax Tables}. |
| 21 | 21 | ||
| 22 | @menu | 22 | @menu |
| 23 | * Hooks:: How to use hooks; how to write code that provides hooks. | ||
| 23 | * Major Modes:: Defining major modes. | 24 | * Major Modes:: Defining major modes. |
| 24 | * Minor Modes:: Defining minor modes. | 25 | * Minor Modes:: Defining minor modes. |
| 25 | * Mode Line Format:: Customizing the text that appears in the mode line. | 26 | * Mode Line Format:: Customizing the text that appears in the mode line. |
| @@ -28,13 +29,153 @@ user. For related topics such as keymaps and syntax tables, see | |||
| 28 | * Font Lock Mode:: How modes can highlight text according to syntax. | 29 | * Font Lock Mode:: How modes can highlight text according to syntax. |
| 29 | * Desktop Save Mode:: How modes can have buffer state saved between | 30 | * Desktop Save Mode:: How modes can have buffer state saved between |
| 30 | Emacs sessions. | 31 | Emacs sessions. |
| 31 | * Hooks:: How to use hooks; how to write code that provides hooks. | ||
| 32 | @end menu | 32 | @end menu |
| 33 | 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. When the | ||
| 47 | hook name ends in @samp{-hook}, that tells you it is normal. We try to | ||
| 48 | make all hooks normal, as much as possible, so that you can use them in | ||
| 49 | a uniform way. | ||
| 50 | |||
| 51 | Every major mode function is supposed to run a normal hook called the | ||
| 52 | @dfn{mode hook} as the last step of initialization. This makes it easy | ||
| 53 | for a user to customize the behavior of the mode, by overriding the | ||
| 54 | buffer-local variable assignments already made by the mode. Most | ||
| 55 | minor modes also run a mode hook at their end. But hooks are used in | ||
| 56 | other contexts too. For example, the hook @code{suspend-hook} runs | ||
| 57 | just before Emacs suspends itself (@pxref{Suspending Emacs}). | ||
| 58 | |||
| 59 | The recommended way to add a hook function to a normal hook is by | ||
| 60 | calling @code{add-hook} (see below). The hook functions may be any of | ||
| 61 | the valid kinds of functions that @code{funcall} accepts (@pxref{What | ||
| 62 | Is a Function}). Most normal hook variables are initially void; | ||
| 63 | @code{add-hook} knows how to deal with this. You can add hooks either | ||
| 64 | globally or buffer-locally with @code{add-hook}. | ||
| 65 | |||
| 66 | @cindex abnormal hook | ||
| 67 | If the hook variable's name does not end with @samp{-hook}, that | ||
| 68 | indicates it is probably an @dfn{abnormal hook}. Then you should look at its | ||
| 69 | documentation to see how to use the hook properly. | ||
| 70 | |||
| 71 | If the variable's name ends in @samp{-functions} or @samp{-hooks}, | ||
| 72 | then the value is a list of functions, but it is abnormal in that either | ||
| 73 | these functions are called with arguments or their values are used in | ||
| 74 | some way. You can use @code{add-hook} to add a function to the list, | ||
| 75 | but you must take care in writing the function. (A few of these | ||
| 76 | variables, notably those ending in @samp{-hooks}, are actually | ||
| 77 | normal hooks which were named before we established the convention of | ||
| 78 | using @samp{-hook} for them.) | ||
| 79 | |||
| 80 | If the variable's name ends in @samp{-function}, then its value | ||
| 81 | is just a single function, not a list of functions. | ||
| 82 | |||
| 83 | Here's an example that uses a mode hook to turn on Auto Fill mode when | ||
| 84 | in Lisp Interaction mode: | ||
| 85 | |||
| 86 | @example | ||
| 87 | (add-hook 'lisp-interaction-mode-hook 'turn-on-auto-fill) | ||
| 88 | @end example | ||
| 89 | |||
| 90 | At the appropriate time, Emacs uses the @code{run-hooks} function to | ||
| 91 | run particular hooks. This function calls the hook functions that have | ||
| 92 | been added with @code{add-hook}. | ||
| 93 | |||
| 94 | @defun run-hooks &rest hookvars | ||
| 95 | This function takes one or more normal hook variable names as | ||
| 96 | arguments, and runs each hook in turn. Each argument should be a | ||
| 97 | symbol that is a normal hook variable. These arguments are processed | ||
| 98 | in the order specified. | ||
| 99 | |||
| 100 | If a hook variable has a non-@code{nil} value, that value may be a | ||
| 101 | function or a list of functions. (The former option is considered | ||
| 102 | obsolete.) If the value is a function (either a lambda expression or | ||
| 103 | a symbol with a function definition), it is called. If it is a list | ||
| 104 | that isn't a function, its elements are called, consecutively. All | ||
| 105 | the hook functions are called with no arguments. | ||
| 106 | @end defun | ||
| 107 | |||
| 108 | @defun run-hook-with-args hook &rest args | ||
| 109 | This function is the way to run an abnormal hook and always call all | ||
| 110 | of the hook functions. It calls each of the hook functions one by | ||
| 111 | one, passing each of them the arguments @var{args}. | ||
| 112 | @end defun | ||
| 113 | |||
| 114 | @defun run-hook-with-args-until-failure hook &rest args | ||
| 115 | This function is the way to run an abnormal hook until one of the hook | ||
| 116 | functions fails. It calls each of the hook functions, passing each of | ||
| 117 | them the arguments @var{args}, until some hook function returns | ||
| 118 | @code{nil}. It then stops and returns @code{nil}. If none of the | ||
| 119 | hook functions return @code{nil}, it returns a non-@code{nil} value. | ||
| 120 | @end defun | ||
| 121 | |||
| 122 | @defun run-hook-with-args-until-success hook &rest args | ||
| 123 | This function is the way to run an abnormal hook until a hook function | ||
| 124 | succeeds. It calls each of the hook functions, passing each of them | ||
| 125 | the arguments @var{args}, until some hook function returns | ||
| 126 | non-@code{nil}. Then it stops, and returns whatever was returned by | ||
| 127 | the last hook function that was called. If all hook functions return | ||
| 128 | @code{nil}, it returns @code{nil} as well. | ||
| 129 | @end defun | ||
| 130 | |||
| 131 | @defun add-hook hook function &optional append local | ||
| 132 | This function is the handy way to add function @var{function} to hook | ||
| 133 | variable @var{hook}. You can use it for abnormal hooks as well as for | ||
| 134 | normal hooks. @var{function} can be any Lisp function that can accept | ||
| 135 | the proper number of arguments for @var{hook}. For example, | ||
| 136 | |||
| 137 | @example | ||
| 138 | (add-hook 'text-mode-hook 'my-text-hook-function) | ||
| 139 | @end example | ||
| 140 | |||
| 141 | @noindent | ||
| 142 | adds @code{my-text-hook-function} to the hook called @code{text-mode-hook}. | ||
| 143 | |||
| 144 | If @var{function} is already present in @var{hook} (comparing using | ||
| 145 | @code{equal}), then @code{add-hook} does not add it a second time. | ||
| 146 | |||
| 147 | It is best to design your hook functions so that the order in which they | ||
| 148 | are executed does not matter. Any dependence on the order is ``asking | ||
| 149 | for trouble''. However, the order is predictable: normally, | ||
| 150 | @var{function} goes at the front of the hook list, so it will be | ||
| 151 | executed first (barring another @code{add-hook} call). If the optional | ||
| 152 | argument @var{append} is non-@code{nil}, the new hook function goes at | ||
| 153 | the end of the hook list and will be executed last. | ||
| 154 | |||
| 155 | @code{add-hook} can handle the cases where @var{hook} is void or its | ||
| 156 | value is a single function; it sets or changes the value to a list of | ||
| 157 | functions. | ||
| 158 | |||
| 159 | If @var{local} is non-@code{nil}, that says to add @var{function} to | ||
| 160 | the buffer-local hook list instead of to the global hook list. If | ||
| 161 | needed, this makes the hook buffer-local and adds @code{t} to the | ||
| 162 | buffer-local value. The latter acts as a flag to run the hook | ||
| 163 | functions in the default value as well as in the local value. | ||
| 164 | @end defun | ||
| 165 | |||
| 166 | @defun remove-hook hook function &optional local | ||
| 167 | This function removes @var{function} from the hook variable | ||
| 168 | @var{hook}. It compares @var{function} with elements of @var{hook} | ||
| 169 | using @code{equal}, so it works for both symbols and lambda | ||
| 170 | expressions. | ||
| 171 | |||
| 172 | If @var{local} is non-@code{nil}, that says to remove @var{function} | ||
| 173 | from the buffer-local hook list instead of from the global hook list. | ||
| 174 | @end defun | ||
| 175 | |||
| 34 | @node Major Modes | 176 | @node Major Modes |
| 35 | @section Major Modes | 177 | @section Major Modes |
| 36 | @cindex major mode | 178 | @cindex major mode |
| 37 | @cindex Fundamental mode | ||
| 38 | 179 | ||
| 39 | Major modes specialize Emacs for editing particular kinds of text. | 180 | Major modes specialize Emacs for editing particular kinds of text. |
| 40 | Each buffer has only one major mode at a time. For each major mode | 181 | Each buffer has only one major mode at a time. For each major mode |
| @@ -44,6 +185,23 @@ buffer-local variable bindings and other data associated with the | |||
| 44 | buffer, such as a local keymap. The effect lasts until you switch | 185 | buffer, such as a local keymap. The effect lasts until you switch |
| 45 | to another major mode in the same buffer. | 186 | to another major mode in the same buffer. |
| 46 | 187 | ||
| 188 | @menu | ||
| 189 | * Major Mode Basics:: | ||
| 190 | * Major Mode Conventions:: Coding conventions for keymaps, etc. | ||
| 191 | * Example Major Modes:: Text mode and Lisp modes. | ||
| 192 | * Auto Major Mode:: How Emacs chooses the major mode automatically. | ||
| 193 | * Mode Help:: Finding out how to use a mode. | ||
| 194 | * Derived Modes:: Defining a new major mode based on another major | ||
| 195 | mode. | ||
| 196 | * Generic Modes:: Defining a simple major mode that supports | ||
| 197 | comment syntax and Font Lock mode. | ||
| 198 | * Mode Hooks:: Hooks run at the end of major mode functions. | ||
| 199 | @end menu | ||
| 200 | |||
| 201 | @node Major Mode Basics | ||
| 202 | @subsection Major Mode Basics | ||
| 203 | @cindex Fundamental mode | ||
| 204 | |||
| 47 | The least specialized major mode is called @dfn{Fundamental mode}. | 205 | The least specialized major mode is called @dfn{Fundamental mode}. |
| 48 | This mode has no mode-specific definitions or variable settings, so each | 206 | This mode has no mode-specific definitions or variable settings, so each |
| 49 | Emacs command behaves in its default manner, and each option is in its | 207 | Emacs command behaves in its default manner, and each option is in its |
| @@ -95,18 +253,6 @@ for several major modes, in files such as @file{text-mode.el}, | |||
| 95 | are written. Text mode is perhaps the simplest major mode aside from | 253 | are written. Text mode is perhaps the simplest major mode aside from |
| 96 | Fundamental mode. Rmail mode is a complicated and specialized mode. | 254 | Fundamental mode. Rmail mode is a complicated and specialized mode. |
| 97 | 255 | ||
| 98 | @menu | ||
| 99 | * Major Mode Conventions:: Coding conventions for keymaps, etc. | ||
| 100 | * Example Major Modes:: Text mode and Lisp modes. | ||
| 101 | * Auto Major Mode:: How Emacs chooses the major mode automatically. | ||
| 102 | * Mode Help:: Finding out how to use a mode. | ||
| 103 | * Derived Modes:: Defining a new major mode based on another major | ||
| 104 | mode. | ||
| 105 | * Generic Modes:: Defining a simple major mode that supports | ||
| 106 | comment syntax and Font Lock mode. | ||
| 107 | * Mode Hooks:: Hooks run at the end of major mode functions. | ||
| 108 | @end menu | ||
| 109 | |||
| 110 | @node Major Mode Conventions | 256 | @node Major Mode Conventions |
| 111 | @subsection Major Mode Conventions | 257 | @subsection Major Mode Conventions |
| 112 | 258 | ||
| @@ -240,7 +386,7 @@ related modes. If it has its own abbrev table, it should store this | |||
| 240 | in a variable named @code{@var{modename}-mode-abbrev-table}. If the | 386 | in a variable named @code{@var{modename}-mode-abbrev-table}. If the |
| 241 | major mode command defines any abbrevs itself, it should pass @code{t} | 387 | major mode command defines any abbrevs itself, it should pass @code{t} |
| 242 | for the @var{system-flag} argument to @code{define-abbrev}. | 388 | for the @var{system-flag} argument to @code{define-abbrev}. |
| 243 | @xref{Abbrev Tables}. | 389 | @xref{Defining Abbrevs}. |
| 244 | 390 | ||
| 245 | @item | 391 | @item |
| 246 | The mode should specify how to do highlighting for Font Lock mode, by | 392 | The mode should specify how to do highlighting for Font Lock mode, by |
| @@ -326,8 +472,11 @@ If you want to make the new mode the default for files with certain | |||
| 326 | recognizable names, add an element to @code{auto-mode-alist} to select | 472 | recognizable names, add an element to @code{auto-mode-alist} to select |
| 327 | the mode for those file names. If you define the mode command to | 473 | the mode for those file names. If you define the mode command to |
| 328 | autoload, you should add this element in the same file that calls | 474 | autoload, you should add this element in the same file that calls |
| 329 | @code{autoload}. Otherwise, it is sufficient to add the element in the | 475 | @code{autoload}. If you use an autoload cookie for the mode command, |
| 330 | file that contains the mode definition. @xref{Auto Major Mode}. | 476 | you can also use an autoload cookie for the form that adds the element |
| 477 | (@pxref{autoload cookie}). If you do not autoload the mode command, | ||
| 478 | it is sufficient to add the element in the file that contains the mode | ||
| 479 | definition. @xref{Auto Major Mode}. | ||
| 331 | 480 | ||
| 332 | @item | 481 | @item |
| 333 | In the comments that document the file, you should provide a sample | 482 | In the comments that document the file, you should provide a sample |
| @@ -635,21 +784,28 @@ state of Emacs.) | |||
| 635 | 784 | ||
| 636 | @deffn Command normal-mode &optional find-file | 785 | @deffn Command normal-mode &optional find-file |
| 637 | This function establishes the proper major mode and buffer-local variable | 786 | This function establishes the proper major mode and buffer-local variable |
| 638 | bindings for the current buffer. First it calls @code{set-auto-mode}, | 787 | bindings for the current buffer. First it calls @code{set-auto-mode} |
| 639 | then it runs @code{hack-local-variables} to parse, and bind or | 788 | (see below), then it runs @code{hack-local-variables} to parse, and |
| 640 | evaluate as appropriate, the file's local variables. | 789 | bind or evaluate as appropriate, the file's local variables |
| 790 | (@pxref{File Local Variables}). | ||
| 641 | 791 | ||
| 642 | If the @var{find-file} argument to @code{normal-mode} is non-@code{nil}, | 792 | If the @var{find-file} argument to @code{normal-mode} is non-@code{nil}, |
| 643 | @code{normal-mode} assumes that the @code{find-file} function is calling | 793 | @code{normal-mode} assumes that the @code{find-file} function is calling |
| 644 | it. In this case, it may process a local variables list at the end of | 794 | it. In this case, it may process local variables in the @samp{-*-} |
| 645 | the file and in the @samp{-*-} line. The variable | 795 | line or at the end of the file. The variable |
| 646 | @code{enable-local-variables} controls whether to do so. @xref{File | 796 | @code{enable-local-variables} controls whether to do so. @xref{File |
| 647 | variables, , Local Variables in Files, emacs, The GNU Emacs Manual}, for | 797 | Variables, , Local Variables in Files, emacs, The GNU Emacs Manual}, |
| 648 | the syntax of the local variables section of a file. | 798 | for the syntax of the local variables section of a file. |
| 649 | 799 | ||
| 650 | If you run @code{normal-mode} interactively, the argument | 800 | If you run @code{normal-mode} interactively, the argument |
| 651 | @var{find-file} is normally @code{nil}. In this case, | 801 | @var{find-file} is normally @code{nil}. In this case, |
| 652 | @code{normal-mode} unconditionally processes any local variables list. | 802 | @code{normal-mode} unconditionally processes any file local variables. |
| 803 | |||
| 804 | If @code{normal-mode} processes the local variables list and this list | ||
| 805 | specifies a major mode, that mode overrides any mode chosen by | ||
| 806 | @code{set-auto-mode}. If neither @code{set-auto-mode} nor | ||
| 807 | @code{hack-local-variables} specify a major mode, the buffer stays in | ||
| 808 | the major mode determined by @code{default-major-mode} (see below). | ||
| 653 | 809 | ||
| 654 | @cindex file mode specification error | 810 | @cindex file mode specification error |
| 655 | @code{normal-mode} uses @code{condition-case} around the call to the | 811 | @code{normal-mode} uses @code{condition-case} around the call to the |
| @@ -657,16 +813,25 @@ major mode function, so errors are caught and reported as a @samp{File | |||
| 657 | mode specification error}, followed by the original error message. | 813 | mode specification error}, followed by the original error message. |
| 658 | @end deffn | 814 | @end deffn |
| 659 | 815 | ||
| 660 | @defun set-auto-mode | 816 | @defun set-auto-mode &optional keep-mode-if-same |
| 661 | @cindex visited file mode | 817 | @cindex visited file mode |
| 662 | This function selects the major mode that is appropriate for the | 818 | This function selects the major mode that is appropriate for the |
| 663 | current buffer. It may base its decision on the value of the @w{@samp{-*-}} | 819 | current buffer. It bases its decision (in order of precedence) on |
| 664 | line, on the visited file name (using @code{auto-mode-alist}), on the | 820 | the @w{@samp{-*-}} line, on the @w{@samp{#!}} line (using |
| 665 | @w{@samp{#!}} line (using @code{interpreter-mode-alist}), or on the | 821 | @code{interpreter-mode-alist}), on the text at the beginning of the |
| 666 | file's local variables list. However, this function does not look for | 822 | buffer (using @code{magic-mode-alist}), and finally on the visited |
| 667 | the @samp{mode:} local variable near the end of a file; the | 823 | file name (using @code{auto-mode-alist}). @xref{Choosing Modes, , How |
| 668 | @code{hack-local-variables} function does that. @xref{Choosing Modes, , | 824 | Major Modes are Chosen, emacs, The GNU Emacs Manual}. However, this |
| 669 | How Major Modes are Chosen, emacs, The GNU Emacs Manual}. | 825 | function does not look for the @samp{mode:} local variable near the |
| 826 | end of a file; the @code{hack-local-variables} function does that. | ||
| 827 | If @code{enable-local-variables} is @code{nil}, @code{set-auto-mode} | ||
| 828 | does not check the @w{@samp{-*-}} line for a mode tag either. | ||
| 829 | |||
| 830 | If @var{keep-mode-if-same} is non-@code{nil}, this function does not | ||
| 831 | call the mode command if the buffer is already in the proper major | ||
| 832 | mode. For instance, @code{set-visited-file-name} sets this to | ||
| 833 | @code{t} to avoid killing buffer local variables that the user may | ||
| 834 | have set. | ||
| 670 | @end defun | 835 | @end defun |
| 671 | 836 | ||
| 672 | @defopt default-major-mode | 837 | @defopt default-major-mode |
| @@ -674,8 +839,8 @@ This variable holds the default major mode for new buffers. The | |||
| 674 | standard value is @code{fundamental-mode}. | 839 | standard value is @code{fundamental-mode}. |
| 675 | 840 | ||
| 676 | If the value of @code{default-major-mode} is @code{nil}, Emacs uses | 841 | If the value of @code{default-major-mode} is @code{nil}, Emacs uses |
| 677 | the (previously) current buffer's major mode for the major mode of a new | 842 | the (previously) current buffer's major mode as the default major mode |
| 678 | buffer. However, if that major mode symbol has a @code{mode-class} | 843 | of a new buffer. However, if that major mode symbol has a @code{mode-class} |
| 679 | property with value @code{special}, then it is not used for new buffers; | 844 | property with value @code{special}, then it is not used for new buffers; |
| 680 | Fundamental mode is used instead. The modes that have this property are | 845 | Fundamental mode is used instead. The modes that have this property are |
| 681 | those such as Dired and Rmail that are useful only with text that has | 846 | those such as Dired and Rmail that are useful only with text that has |
| @@ -684,28 +849,50 @@ been specially prepared. | |||
| 684 | 849 | ||
| 685 | @defun set-buffer-major-mode buffer | 850 | @defun set-buffer-major-mode buffer |
| 686 | This function sets the major mode of @var{buffer} to the value of | 851 | This function sets the major mode of @var{buffer} to the value of |
| 687 | @code{default-major-mode}. If that variable is @code{nil}, it uses | 852 | @code{default-major-mode}; if that variable is @code{nil}, it uses the |
| 688 | the current buffer's major mode (if that is suitable). | 853 | current buffer's major mode (if that is suitable). As an exception, |
| 854 | if @var{buffer}'s name is @samp{*scratch*}, it sets the mode to | ||
| 855 | @code{initial-major-mode}. | ||
| 689 | 856 | ||
| 690 | The low-level primitives for creating buffers do not use this function, | 857 | The low-level primitives for creating buffers do not use this function, |
| 691 | but medium-level commands such as @code{switch-to-buffer} and | 858 | but medium-level commands such as @code{switch-to-buffer} and |
| 692 | @code{find-file-noselect} use it whenever they create buffers. | 859 | @code{find-file-noselect} use it whenever they create buffers. |
| 693 | @end defun | 860 | @end defun |
| 694 | 861 | ||
| 695 | @defvar initial-major-mode | 862 | @defopt initial-major-mode |
| 696 | @cindex @samp{*scratch*} | 863 | @cindex @samp{*scratch*} |
| 697 | The value of this variable determines the major mode of the initial | 864 | The value of this variable determines the major mode of the initial |
| 698 | @samp{*scratch*} buffer. The value should be a symbol that is a major | 865 | @samp{*scratch*} buffer. The value should be a symbol that is a major |
| 699 | mode command. The default value is @code{lisp-interaction-mode}. | 866 | mode command. The default value is @code{lisp-interaction-mode}. |
| 867 | @end defopt | ||
| 868 | |||
| 869 | @defvar interpreter-mode-alist | ||
| 870 | This variable specifies major modes to use for scripts that specify a | ||
| 871 | command interpreter in a @samp{#!} line. Its value is an alist with | ||
| 872 | elements of the form @code{(@var{interpreter} . @var{mode})}; for | ||
| 873 | example, @code{("perl" . perl-mode)} is one element present by | ||
| 874 | default. The element says to use mode @var{mode} if the file | ||
| 875 | specifies an interpreter which matches @var{interpreter}. The value | ||
| 876 | of @var{interpreter} is actually a regular expression. @xref{Regular | ||
| 877 | Expressions}. | ||
| 878 | @end defvar | ||
| 879 | |||
| 880 | @defvar magic-mode-alist | ||
| 881 | This variable's value is an alist with elements of the form | ||
| 882 | @code{(@var{regexp} . @var{function})}, where @var{regexp} is a | ||
| 883 | regular expression and @var{function} is a function or @code{nil}. | ||
| 884 | After visiting a file, @code{set-auto-mode} calls @var{function} if | ||
| 885 | the text at the beginning of the buffer matches @var{regexp} and | ||
| 886 | @var{function} is non-@code{nil}; if @var{function} is @code{nil}, | ||
| 887 | @code{auto-mode-alist} gets to decide the mode. | ||
| 700 | @end defvar | 888 | @end defvar |
| 701 | 889 | ||
| 702 | @defvar auto-mode-alist | 890 | @defvar auto-mode-alist |
| 703 | This variable contains an association list of file name patterns | 891 | This variable contains an association list of file name patterns |
| 704 | (regular expressions; @pxref{Regular Expressions}) and corresponding | 892 | (regular expressions) and corresponding major mode commands. Usually, |
| 705 | major mode commands. Usually, the file name patterns test for suffixes, | 893 | the file name patterns test for suffixes, such as @samp{.el} and |
| 706 | such as @samp{.el} and @samp{.c}, but this need not be the case. An | 894 | @samp{.c}, but this need not be the case. An ordinary element of the |
| 707 | ordinary element of the alist looks like @code{(@var{regexp} . | 895 | alist looks like @code{(@var{regexp} . @var{mode-function})}. |
| 708 | @var{mode-function})}. | ||
| 709 | 896 | ||
| 710 | For example, | 897 | For example, |
| 711 | 898 | ||
| @@ -724,9 +911,11 @@ For example, | |||
| 724 | @end smallexample | 911 | @end smallexample |
| 725 | 912 | ||
| 726 | When you visit a file whose expanded file name (@pxref{File Name | 913 | When you visit a file whose expanded file name (@pxref{File Name |
| 727 | Expansion}) matches a @var{regexp}, @code{set-auto-mode} calls the | 914 | Expansion}), with version numbers and backup suffixes removed using |
| 728 | corresponding @var{mode-function}. This feature enables Emacs to select | 915 | @code{file-name-sans-versions} (@pxref{File Name Components}), matches |
| 729 | the proper major mode for most files. | 916 | a @var{regexp}, @code{set-auto-mode} calls the corresponding |
| 917 | @var{mode-function}. This feature enables Emacs to select the proper | ||
| 918 | major mode for most files. | ||
| 730 | 919 | ||
| 731 | If an element of @code{auto-mode-alist} has the form @code{(@var{regexp} | 920 | If an element of @code{auto-mode-alist} has the form @code{(@var{regexp} |
| 732 | @var{function} t)}, then after calling @var{function}, Emacs searches | 921 | @var{function} t)}, then after calling @var{function}, Emacs searches |
| @@ -755,19 +944,6 @@ init file.) | |||
| 755 | @end smallexample | 944 | @end smallexample |
| 756 | @end defvar | 945 | @end defvar |
| 757 | 946 | ||
| 758 | @defvar interpreter-mode-alist | ||
| 759 | This variable specifies major modes to use for scripts that specify a | ||
| 760 | command interpreter in a @samp{#!} line. Its value is a list of | ||
| 761 | elements of the form @code{(@var{interpreter} . @var{mode})}; for | ||
| 762 | example, @code{("perl" . perl-mode)} is one element present by default. | ||
| 763 | The element says to use mode @var{mode} if the file specifies | ||
| 764 | an interpreter which matches @var{interpreter}. The value of | ||
| 765 | @var{interpreter} is actually a regular expression. | ||
| 766 | |||
| 767 | This variable is applicable only when the @code{auto-mode-alist} does | ||
| 768 | not indicate which major mode to use. | ||
| 769 | @end defvar | ||
| 770 | |||
| 771 | @node Mode Help | 947 | @node Mode Help |
| 772 | @subsection Getting Help about a Major Mode | 948 | @subsection Getting Help about a Major Mode |
| 773 | @cindex mode help | 949 | @cindex mode help |
| @@ -804,36 +980,38 @@ mode. | |||
| 804 | It's often useful to define a new major mode in terms of an existing | 980 | It's often useful to define a new major mode in terms of an existing |
| 805 | one. An easy way to do this is to use @code{define-derived-mode}. | 981 | one. An easy way to do this is to use @code{define-derived-mode}. |
| 806 | 982 | ||
| 807 | @defmac define-derived-mode variant parent name docstring body@dots{} | 983 | @defmac define-derived-mode variant parent name docstring keyword-args@dots{} body@dots{} |
| 808 | This construct defines @var{variant} as a major mode command, using | 984 | This construct defines @var{variant} as a major mode command, using |
| 809 | @var{name} as the string form of the mode name. | 985 | @var{name} as the string form of the mode name. @var{variant} and |
| 986 | @var{parent} should be unquoted symbols. | ||
| 810 | 987 | ||
| 811 | The new command @var{variant} is defined to call the function | 988 | The new command @var{variant} is defined to call the function |
| 812 | @var{parent}, then override certain aspects of that parent mode: | 989 | @var{parent}, then override certain aspects of that parent mode: |
| 813 | 990 | ||
| 814 | @itemize @bullet | 991 | @itemize @bullet |
| 815 | @item | 992 | @item |
| 816 | The new mode has its own keymap, named @code{@var{variant}-map}. | 993 | The new mode has its own sparse keymap, named |
| 817 | @code{define-derived-mode} initializes this map to inherit from | 994 | @code{@var{variant}-map}. @code{define-derived-mode} |
| 818 | @code{@var{parent}-map}, if it is not already set. | 995 | makes the parent mode's keymap the parent of the new map, unless |
| 996 | @code{@var{variant}-map} is already set and already has a parent. | ||
| 819 | 997 | ||
| 820 | @item | 998 | @item |
| 821 | The new mode has its own syntax table, kept in the variable | 999 | The new mode has its own syntax table, kept in the variable |
| 822 | @code{@var{variant}-syntax-table}. | 1000 | @code{@var{variant}-syntax-table}, unless you override this using the |
| 823 | @code{define-derived-mode} initializes this variable by copying | 1001 | @code{:syntax-table} keyword (see below). @code{define-derived-mode} |
| 824 | @code{@var{parent}-syntax-table}, if it is not already set. | 1002 | makes the parent mode's syntax-table the parent of |
| 1003 | @code{@var{variant}-syntax-table}, unless the latter is already set | ||
| 1004 | and already has a parent different from @code{standard-syntax-table}. | ||
| 825 | 1005 | ||
| 826 | @item | 1006 | @item |
| 827 | The new mode has its own abbrev table, kept in the variable | 1007 | The new mode has its own abbrev table, kept in the variable |
| 828 | @code{@var{variant}-abbrev-table}. | 1008 | @code{@var{variant}-abbrev-table}, unless you override this using the |
| 829 | @code{define-derived-mode} initializes this variable by copying | 1009 | @code{:abbrev-table} keyword (see below). |
| 830 | @code{@var{parent}-abbrev-table}, if it is not already set. | ||
| 831 | 1010 | ||
| 832 | @item | 1011 | @item |
| 833 | The new mode has its own mode hook, @code{@var{variant}-hook}, | 1012 | The new mode has its own mode hook, @code{@var{variant}-hook}. It |
| 834 | which it runs in standard fashion as the very last thing that it does. | 1013 | runs this hook, after running the hooks of its ancestor modes, with |
| 835 | (The new mode also runs the mode hook of @var{parent} as part | 1014 | @code{run-mode-hooks}. |
| 836 | of calling @var{parent}.) | ||
| 837 | @end itemize | 1015 | @end itemize |
| 838 | 1016 | ||
| 839 | In addition, you can specify how to override other aspects of | 1017 | In addition, you can specify how to override other aspects of |
| @@ -841,9 +1019,38 @@ In addition, you can specify how to override other aspects of | |||
| 841 | evaluates the forms in @var{body} after setting up all its usual | 1019 | evaluates the forms in @var{body} after setting up all its usual |
| 842 | overrides, just before running @code{@var{variant}-hook}. | 1020 | overrides, just before running @code{@var{variant}-hook}. |
| 843 | 1021 | ||
| 844 | The argument @var{docstring} specifies the documentation string for the | 1022 | You can also specify @code{nil} for @var{parent}. This gives the new |
| 845 | new mode. If you omit @var{docstring}, @code{define-derived-mode} | 1023 | mode no parent. Then @code{define-derived-mode} behaves as described |
| 846 | generates a documentation string. | 1024 | above, but, of course, omits all actions connected with @var{parent}. |
| 1025 | |||
| 1026 | The argument @var{docstring} specifies the documentation string for | ||
| 1027 | the new mode. @code{define-derived-mode} adds some general | ||
| 1028 | information about the mode's hook, followed by the mode's keymap, at | ||
| 1029 | the end of this docstring. If you omit @var{docstring}, | ||
| 1030 | @code{define-derived-mode} generates a documentation string. | ||
| 1031 | |||
| 1032 | The @var{keyword-args} are pairs of keywords and values. The values | ||
| 1033 | are evaluated. The following keywords are currently supported: | ||
| 1034 | |||
| 1035 | @table @code | ||
| 1036 | @item :group | ||
| 1037 | If this is specified, it is the customization group for this mode. | ||
| 1038 | |||
| 1039 | @item :syntax-table | ||
| 1040 | You can use this to explicitly specify a syntax table for the new | ||
| 1041 | mode. If you specify a @code{nil} value, the new mode uses the same | ||
| 1042 | syntax table as @var{parent}, or @code{standard-syntax-table} if | ||
| 1043 | @var{parent} is @code{nil}. (Note that this does @emph{not} follow | ||
| 1044 | the convention used for non-keyword arguments that a @code{nil} value | ||
| 1045 | is equivalent with not specifying the argument.) | ||
| 1046 | |||
| 1047 | @item :abbrev-table | ||
| 1048 | You can use this to explicitly specify an abbrev table for the new | ||
| 1049 | mode. If you specify a @code{nil} value, the new mode uses the same | ||
| 1050 | abbrev-table as @var{parent}, or @code{fundamental-mode-abbrev-table} | ||
| 1051 | if @var{parent} is @code{nil}. (Again,a @code{nil} value is | ||
| 1052 | @emph{not} equivalent to not specifying this keyword.) | ||
| 1053 | @end table | ||
| 847 | 1054 | ||
| 848 | Here is a hypothetical example: | 1055 | Here is a hypothetical example: |
| 849 | 1056 | ||
| @@ -1295,6 +1502,19 @@ and header line. We include it in this chapter because much of the | |||
| 1295 | information displayed in the mode line relates to the enabled major and | 1502 | information displayed in the mode line relates to the enabled major and |
| 1296 | minor modes. | 1503 | minor modes. |
| 1297 | 1504 | ||
| 1505 | @menu | ||
| 1506 | * Mode Line Basics:: | ||
| 1507 | * Mode Line Data:: The data structure that controls the mode line. | ||
| 1508 | * Mode Line Variables:: Variables used in that data structure. | ||
| 1509 | * %-Constructs:: Putting information into a mode line. | ||
| 1510 | * Properties in Mode:: Using text properties in the mode line. | ||
| 1511 | * Header Lines:: Like a mode line, but at the top. | ||
| 1512 | * Emulating Mode Line:: Formatting text as the mode line would. | ||
| 1513 | @end menu | ||
| 1514 | |||
| 1515 | @node Mode Line Basics | ||
| 1516 | @subsection Mode Line Basics | ||
| 1517 | |||
| 1298 | @code{mode-line-format} is a buffer-local variable that holds a | 1518 | @code{mode-line-format} is a buffer-local variable that holds a |
| 1299 | template used to display the mode line of the current buffer. All | 1519 | template used to display the mode line of the current buffer. All |
| 1300 | windows for the same buffer use the same @code{mode-line-format}, so | 1520 | windows for the same buffer use the same @code{mode-line-format}, so |
| @@ -1336,15 +1556,6 @@ that is two lines tall cannot display both a mode line and a header | |||
| 1336 | line at once; if the variables call for both, only the mode line | 1556 | line at once; if the variables call for both, only the mode line |
| 1337 | actually appears. | 1557 | actually appears. |
| 1338 | 1558 | ||
| 1339 | @menu | ||
| 1340 | * Mode Line Data:: The data structure that controls the mode line. | ||
| 1341 | * Mode Line Variables:: Variables used in that data structure. | ||
| 1342 | * %-Constructs:: Putting information into a mode line. | ||
| 1343 | * Properties in Mode:: Using text properties in the mode line. | ||
| 1344 | * Header Lines:: Like a mode line, but at the top. | ||
| 1345 | * Emulating Mode Line:: Formatting text as the mode line would. | ||
| 1346 | @end menu | ||
| 1347 | |||
| 1348 | @node Mode Line Data | 1559 | @node Mode Line Data |
| 1349 | @subsection The Data Structure of the Mode Line | 1560 | @subsection The Data Structure of the Mode Line |
| 1350 | @cindex mode-line construct | 1561 | @cindex mode-line construct |
| @@ -2813,151 +3024,8 @@ argument list | |||
| 2813 | and it should return the restored buffer. | 3024 | and it should return the restored buffer. |
| 2814 | Here @var{desktop-buffer-misc} is the value returned by the function | 3025 | Here @var{desktop-buffer-misc} is the value returned by the function |
| 2815 | optionally bound to @code{desktop-save-buffer}. | 3026 | optionally bound to @code{desktop-save-buffer}. |
| 2816 | |||
| 2817 | @end defvar | 3027 | @end defvar |
| 2818 | 3028 | ||
| 2819 | @node Hooks | ||
| 2820 | @section Hooks | ||
| 2821 | @cindex hooks | ||
| 2822 | |||
| 2823 | A @dfn{hook} is a variable where you can store a function or functions | ||
| 2824 | to be called on a particular occasion by an existing program. Emacs | ||
| 2825 | provides hooks for the sake of customization. Most often, hooks are set | ||
| 2826 | up in the init file (@pxref{Init File}), but Lisp programs can set them also. | ||
| 2827 | @xref{Standard Hooks}, for a list of standard hook variables. | ||
| 2828 | |||
| 2829 | @cindex normal hook | ||
| 2830 | Most of the hooks in Emacs are @dfn{normal hooks}. These variables | ||
| 2831 | contain lists of functions to be called with no arguments. When the | ||
| 2832 | hook name ends in @samp{-hook}, that tells you it is normal. We try to | ||
| 2833 | make all hooks normal, as much as possible, so that you can use them in | ||
| 2834 | a uniform way. | ||
| 2835 | |||
| 2836 | Every major mode function is supposed to run a normal hook called the | ||
| 2837 | @dfn{mode hook} as the last step of initialization. This makes it easy | ||
| 2838 | for a user to customize the behavior of the mode, by overriding the | ||
| 2839 | buffer-local variable assignments already made by the mode. Most | ||
| 2840 | minor modes also run a mode hook at their end. But hooks are used in | ||
| 2841 | other contexts too. For example, the hook @code{suspend-hook} runs | ||
| 2842 | just before Emacs suspends itself (@pxref{Suspending Emacs}). | ||
| 2843 | |||
| 2844 | The recommended way to add a hook function to a normal hook is by | ||
| 2845 | calling @code{add-hook} (see below). The hook functions may be any of | ||
| 2846 | the valid kinds of functions that @code{funcall} accepts (@pxref{What | ||
| 2847 | Is a Function}). Most normal hook variables are initially void; | ||
| 2848 | @code{add-hook} knows how to deal with this. You can add hooks either | ||
| 2849 | globally or buffer-locally with @code{add-hook}. | ||
| 2850 | |||
| 2851 | @cindex abnormal hook | ||
| 2852 | If the hook variable's name does not end with @samp{-hook}, that | ||
| 2853 | indicates it is probably an @dfn{abnormal hook}. Then you should look at its | ||
| 2854 | documentation to see how to use the hook properly. | ||
| 2855 | |||
| 2856 | If the variable's name ends in @samp{-functions} or @samp{-hooks}, | ||
| 2857 | then the value is a list of functions, but it is abnormal in that either | ||
| 2858 | these functions are called with arguments or their values are used in | ||
| 2859 | some way. You can use @code{add-hook} to add a function to the list, | ||
| 2860 | but you must take care in writing the function. (A few of these | ||
| 2861 | variables, notably those ending in @samp{-hooks}, are actually | ||
| 2862 | normal hooks which were named before we established the convention of | ||
| 2863 | using @samp{-hook} for them.) | ||
| 2864 | |||
| 2865 | If the variable's name ends in @samp{-function}, then its value | ||
| 2866 | is just a single function, not a list of functions. | ||
| 2867 | |||
| 2868 | Here's an example that uses a mode hook to turn on Auto Fill mode when | ||
| 2869 | in Lisp Interaction mode: | ||
| 2870 | |||
| 2871 | @example | ||
| 2872 | (add-hook 'lisp-interaction-mode-hook 'turn-on-auto-fill) | ||
| 2873 | @end example | ||
| 2874 | |||
| 2875 | At the appropriate time, Emacs uses the @code{run-hooks} function to | ||
| 2876 | run particular hooks. This function calls the hook functions that have | ||
| 2877 | been added with @code{add-hook}. | ||
| 2878 | |||
| 2879 | @defun run-hooks &rest hookvars | ||
| 2880 | This function takes one or more normal hook variable names as | ||
| 2881 | arguments, and runs each hook in turn. Each argument should be a | ||
| 2882 | symbol that is a normal hook variable. These arguments are processed | ||
| 2883 | in the order specified. | ||
| 2884 | |||
| 2885 | If a hook variable has a non-@code{nil} value, that value may be a | ||
| 2886 | function or a list of functions. (The former option is considered | ||
| 2887 | obsolete.) If the value is a function (either a lambda expression or | ||
| 2888 | a symbol with a function definition), it is called. If it is a list | ||
| 2889 | that isn't a function, its elements are called, consecutively. All | ||
| 2890 | the hook functions are called with no arguments. | ||
| 2891 | @end defun | ||
| 2892 | |||
| 2893 | @defun run-hook-with-args hook &rest args | ||
| 2894 | This function is the way to run an abnormal hook and always call all | ||
| 2895 | of the hook functions. It calls each of the hook functions one by | ||
| 2896 | one, passing each of them the arguments @var{args}. | ||
| 2897 | @end defun | ||
| 2898 | |||
| 2899 | @defun run-hook-with-args-until-failure hook &rest args | ||
| 2900 | This function is the way to run an abnormal hook until one of the hook | ||
| 2901 | functions fails. It calls each of the hook functions, passing each of | ||
| 2902 | them the arguments @var{args}, until some hook function returns | ||
| 2903 | @code{nil}. It then stops and returns @code{nil}. If none of the | ||
| 2904 | hook functions return @code{nil}, it returns a non-@code{nil} value. | ||
| 2905 | @end defun | ||
| 2906 | |||
| 2907 | @defun run-hook-with-args-until-success hook &rest args | ||
| 2908 | This function is the way to run an abnormal hook until a hook function | ||
| 2909 | succeeds. It calls each of the hook functions, passing each of them | ||
| 2910 | the arguments @var{args}, until some hook function returns | ||
| 2911 | non-@code{nil}. Then it stops, and returns whatever was returned by | ||
| 2912 | the last hook function that was called. If all hook functions return | ||
| 2913 | @code{nil}, it returns @code{nil} as well. | ||
| 2914 | @end defun | ||
| 2915 | |||
| 2916 | @defun add-hook hook function &optional append local | ||
| 2917 | This function is the handy way to add function @var{function} to hook | ||
| 2918 | variable @var{hook}. You can use it for abnormal hooks as well as for | ||
| 2919 | normal hooks. @var{function} can be any Lisp function that can accept | ||
| 2920 | the proper number of arguments for @var{hook}. For example, | ||
| 2921 | |||
| 2922 | @example | ||
| 2923 | (add-hook 'text-mode-hook 'my-text-hook-function) | ||
| 2924 | @end example | ||
| 2925 | |||
| 2926 | @noindent | ||
| 2927 | adds @code{my-text-hook-function} to the hook called @code{text-mode-hook}. | ||
| 2928 | |||
| 2929 | If @var{function} is already present in @var{hook} (comparing using | ||
| 2930 | @code{equal}), then @code{add-hook} does not add it a second time. | ||
| 2931 | |||
| 2932 | It is best to design your hook functions so that the order in which they | ||
| 2933 | are executed does not matter. Any dependence on the order is ``asking | ||
| 2934 | for trouble''. However, the order is predictable: normally, | ||
| 2935 | @var{function} goes at the front of the hook list, so it will be | ||
| 2936 | executed first (barring another @code{add-hook} call). If the optional | ||
| 2937 | argument @var{append} is non-@code{nil}, the new hook function goes at | ||
| 2938 | the end of the hook list and will be executed last. | ||
| 2939 | |||
| 2940 | @code{add-hook} can handle the cases where @var{hook} is void or its | ||
| 2941 | value is a single function; it sets or changes the value to a list of | ||
| 2942 | functions. | ||
| 2943 | |||
| 2944 | If @var{local} is non-@code{nil}, that says to add @var{function} to | ||
| 2945 | the buffer-local hook list instead of to the global hook list. If | ||
| 2946 | needed, this makes the hook buffer-local and adds @code{t} to the | ||
| 2947 | buffer-local value. The latter acts as a flag to run the hook | ||
| 2948 | functions in the default value as well as in the local value. | ||
| 2949 | @end defun | ||
| 2950 | |||
| 2951 | @defun remove-hook hook function &optional local | ||
| 2952 | This function removes @var{function} from the hook variable | ||
| 2953 | @var{hook}. It compares @var{function} with elements of @var{hook} | ||
| 2954 | using @code{equal}, so it works for both symbols and lambda | ||
| 2955 | expressions. | ||
| 2956 | |||
| 2957 | If @var{local} is non-@code{nil}, that says to remove @var{function} | ||
| 2958 | from the buffer-local hook list instead of from the global hook list. | ||
| 2959 | @end defun | ||
| 2960 | |||
| 2961 | @ignore | 3029 | @ignore |
| 2962 | arch-tag: 4c7bff41-36e6-4da6-9e7f-9b9289e27c8e | 3030 | arch-tag: 4c7bff41-36e6-4da6-9e7f-9b9289e27c8e |
| 2963 | @end ignore | 3031 | @end ignore |