comparison lispref/modes.texi @ 72318:6e29ae378991

Clean up wording in previous change.
author Richard M. Stallman <rms@gnu.org>
date Tue, 08 Aug 2006 17:39:01 +0000
parents ffa74c9e9a48
children 9ef3c2ead027 7f3f771c85fa
comparison
equal deleted inserted replaced
72317:5e59478af974 72318:6e29ae378991
50 50
51 Every major mode function is supposed to run a normal hook called 51 Every major mode function is supposed to run a normal hook called
52 the @dfn{mode hook} as the one of the last steps of initialization. 52 the @dfn{mode hook} as the one of the last steps of initialization.
53 This makes it easy for a user to customize the behavior of the mode, 53 This makes it easy for a user to customize the behavior of the mode,
54 by overriding the buffer-local variable assignments already made by 54 by overriding the buffer-local variable assignments already made by
55 the mode. Most minor modes also run a mode hook at their end. But 55 the mode. Most minor mode functions also run a mode hook at the end.
56 hooks are used in other contexts too. For example, the hook 56 But hooks are used in other contexts too. For example, the hook
57 @code{suspend-hook} runs just before Emacs suspends itself 57 @code{suspend-hook} runs just before Emacs suspends itself
58 (@pxref{Suspending Emacs}). 58 (@pxref{Suspending Emacs}).
59 59
60 The recommended way to add a hook function to a normal hook is by 60 The recommended way to add a hook function to a normal hook is by
61 calling @code{add-hook} (see below). The hook functions may be any of 61 calling @code{add-hook} (see below). The hook functions may be any of
64 @code{add-hook} knows how to deal with this. You can add hooks either 64 @code{add-hook} knows how to deal with this. You can add hooks either
65 globally or buffer-locally with @code{add-hook}. 65 globally or buffer-locally with @code{add-hook}.
66 66
67 @cindex abnormal hook 67 @cindex abnormal hook
68 If the hook variable's name does not end with @samp{-hook}, that 68 If the hook variable's name does not end with @samp{-hook}, that
69 indicates it is probably an @dfn{abnormal hook}. Then you should look at its 69 indicates it is probably an @dfn{abnormal hook}. That means the hook
70 documentation to see how to use the hook properly. 70 functions are called with arguments, or their return values are used
71 71 in some way. The hook's documentation says how the functions are
72 @dfn{Abnormal hooks} are hooks in which the functions are called 72 called. You can use @code{add-hook} to add a function to an abnormal
73 with arguments, or the return values are used in some way. By 73 hook, but you must write the function to follow the hook's calling
74 convention, abnormal hook names end in @samp{-functions} or 74 convention.
75 @samp{-hooks}. You can use @code{add-hook} to add a function to the 75
76 list, but you must take care in writing the function. 76 By convention, abnormal hook names end in @samp{-functions} or
77 77 @samp{-hooks}. If the variable's name ends in @samp{-function}, then
78 If the variable's name ends in @samp{-function}, then its value 78 its value is just a single function, not a list of functions.
79 is just a single function, not a list of functions.
80 79
81 Here's an example that uses a mode hook to turn on Auto Fill mode when 80 Here's an example that uses a mode hook to turn on Auto Fill mode when
82 in Lisp Interaction mode: 81 in Lisp Interaction mode:
83 82
84 @example 83 @example
93 arguments, and runs each hook in turn. Each argument should be a 92 arguments, and runs each hook in turn. Each argument should be a
94 symbol that is a normal hook variable. These arguments are processed 93 symbol that is a normal hook variable. These arguments are processed
95 in the order specified. 94 in the order specified.
96 95
97 If a hook variable has a non-@code{nil} value, that value should be a 96 If a hook variable has a non-@code{nil} value, that value should be a
98 list of functions. Each function in this list is called, 97 list of functions. @code{run-hooks} calls all the functions, one by
99 consecutively, with no arguments. 98 one, with no arguments.
100 99
101 A hook variable can also be a single function (either a lambda 100 The hook variable's value can also be a single function---either a
102 expression or a symbol with a function definition) to be called. This 101 lambda expression or a symbol with a function definition---which
103 use is considered obsolete. 102 @code{run-hooks} calls. But this usage is obsolete.
104 @end defun 103 @end defun
105 104
106 @defun run-hook-with-args hook &rest args 105 @defun run-hook-with-args hook &rest args
107 This function is the way to run an abnormal hook and always call all 106 This function is the way to run an abnormal hook and always call all
108 of the hook functions. It calls each of the hook functions one by 107 of the hook functions. It calls each of the hook functions one by
355 Rmail that do not allow self-insertion of text can reasonably redefine 354 Rmail that do not allow self-insertion of text can reasonably redefine
356 letters and other printing characters as special commands. 355 letters and other printing characters as special commands.
357 356
358 @item 357 @item
359 Major modes modes for editing text should not define @key{RET} to do 358 Major modes modes for editing text should not define @key{RET} to do
360 anything other than insert a newline. The command to insert a newline 359 anything other than insert a newline. However, it is ok for
361 and then indent is @kbd{C-j}. It is ok for more specialized modes, 360 specialized modes for text that users don't directly edit, such as
362 such as Info mode, to redefine @key{RET} to something else. 361 Dired and Info modes, to redefine @key{RET} to do something entirely
362 different.
363 363
364 @item 364 @item
365 Major modes should not alter options that are primarily a matter of user 365 Major modes should not alter options that are primarily a matter of user
366 preference, such as whether Auto-Fill mode is enabled. Leave this to 366 preference, such as whether Auto-Fill mode is enabled. Leave this to
367 each user to decide. However, a major mode should customize other 367 each user to decide. However, a major mode should customize other
799 799
800 @node Generic Modes 800 @node Generic Modes
801 @subsection Generic Modes 801 @subsection Generic Modes
802 @cindex generic mode 802 @cindex generic mode
803 803
804 @dfn{Generic modes} are simple major modes with basic support for 804 @dfn{Generic modes} are simple major modes with basic support for
805 comment syntax and Font Lock mode. To define a generic mode, use the 805 comment syntax and Font Lock mode. To define a generic mode, use the
806 macro @code{define-generic-mode}. See the file @file{generic-x.el} 806 macro @code{define-generic-mode}. See the file @file{generic-x.el}
807 for some examples of the use of @code{define-generic-mode}. 807 for some examples of the use of @code{define-generic-mode}.
808 808
809 @defmac define-generic-mode mode comment-list keyword-list font-lock-list auto-mode-list function-list &optional docstring 809 @defmac define-generic-mode mode comment-list keyword-list font-lock-list auto-mode-list function-list &optional docstring
810 This macro creates a new generic mode. The argument @var{mode} (an 810 This macro defines a generic mode command named @var{mode} (a symbol,
811 unquoted symbol) is the major mode command. The optional argument 811 not quoted). The optional argument @var{docstring} is the
812 @var{docstring} is the documentation for the mode command. If you do 812 documentation for the mode command. If you do not supply it,
813 not supply it, @code{define-generic-mode} uses a default documentation 813 @code{define-generic-mode} generates one by default.
814 string instead. 814
815 815 The argument @var{comment-list} is a list in which each element is
816 @var{comment-list} is a list in which each element is either a 816 either a character, a string of one or two characters, or a cons cell.
817 character, a string of one or two characters, or a cons cell. A 817 A character or a string is set up in the mode's syntax table as a
818 character or a string is set up in the mode's syntax table as a
819 ``comment starter.'' If the entry is a cons cell, the @sc{car} is set 818 ``comment starter.'' If the entry is a cons cell, the @sc{car} is set
820 up as a ``comment starter'' and the @sc{cdr} as a ``comment ender.'' 819 up as a ``comment starter'' and the @sc{cdr} as a ``comment ender.''
821 (Use @code{nil} for the latter if you want comments to end at the end 820 (Use @code{nil} for the latter if you want comments to end at the end
822 of the line.) Note that the syntax table has limitations about what 821 of the line.) Note that the syntax table mechanism has limitations
823 comment starters and enders are actually possible. @xref{Syntax 822 about what comment starters and enders are actually possible.
824 Tables}. 823 @xref{Syntax Tables}.
825 824
826 @var{keyword-list} is a list of keywords to highlight with 825 The argument @var{keyword-list} is a list of keywords to highlight
827 @code{font-lock-keyword-face}. Each keyword should be a string. 826 with @code{font-lock-keyword-face}. Each keyword should be a string.
828 @var{font-lock-list} is a list of additional expressions to highlight. 827 Meanwhile, @var{font-lock-list} is a list of additional expressions to
829 Each element of this list should have the same form as an element of 828 highlight. Each element of this list should have the same form as an
830 @code{font-lock-keywords}. @xref{Search-based Fontification}. 829 element of @code{font-lock-keywords}. @xref{Search-based
831 830 Fontification}.
832 @var{auto-mode-list} is a list of regular expressions to add to the 831
833 variable @code{auto-mode-alist}. These regular expressions are added 832 The argument @var{auto-mode-list} is a list of regular expressions to
834 when Emacs runs the macro expansion. 833 add to the variable @code{auto-mode-alist}. They are added by the execution
835 834 of the @code{define-generic-mode} form, not by expanding the macro call.
836 @var{function-list} is a list of functions to call to do some 835
837 additional setup. The mode command calls these functions just before 836 Finally, @var{function-list} is a list of functions for the mode
838 it runs the mode hook variable @code{@var{mode}-hook}. 837 command to call for additional setup. It calls these functions just
838 before it runs the mode hook variable @code{@var{mode}-hook}.
839 @end defmac 839 @end defmac
840 840
841 @node Mode Hooks 841 @node Mode Hooks
842 @subsection Mode Hooks 842 @subsection Mode Hooks
843 843
844 The two last things a major mode function should do is run its mode 844 Every major mode function should finish by running its mode hook and
845 hook and finally the mode independent normal hook 845 the mode-independent normal hook @code{after-change-major-mode-hook}.
846 @code{after-change-major-mode-hook}. If the major mode is a derived 846 It does this by calling @code{run-mode-hooks}. If the major mode is a
847 mode, that is if it calls another major mode (the parent mode) in its 847 derived mode, that is if it calls another major mode (the parent mode)
848 body, then the parent's mode hook is run just before the derived 848 in its body, it should do this inside @code{delay-mode-hooks} so that
849 mode's hook. Neither the parent's mode hook nor 849 the parent won't run these hooks itself. Instead, the derived mode's
850 @code{after-change-major-mode-hook} are run at the end of the actual 850 call to @code{run-mode-hooks} runs the parent's mode hook too.
851 call to the parent mode. This applies recursively if the parent mode 851 @xref{Major Mode Conventions}.
852 has itself a parent. That is, the mode hooks of all major modes 852
853 called directly or indirectly by the major mode function are all run 853 Emacs versions before Emacs 22 did not have @code{delay-mode-hooks}.
854 in sequence at the end, just before 854 When user-implemented major modes have not been updated to use it,
855 @code{after-change-major-mode-hook}. 855 they won't entirely follow these conventions: they may run the
856 856 parent's mode hook too early, or fail to run
857 These conventions are new in Emacs 22, and some major modes 857 @code{after-change-major-mode-hook}. If you encounter such a major
858 implemented by users do not follow them yet. So if you put a function 858 mode, please correct it to follow these conventions.
859 onto @code{after-change-major-mode-hook}, keep in mind that some modes
860 will fail to run it. If a user complains about that, you can respond,
861 ``That major mode fails to follow Emacs conventions, and that's why it
862 fails to work. Please fix the major mode.'' In most cases, that is
863 good enough, so go ahead and use @code{after-change-major-mode-hook}.
864 However, if a certain feature needs to be completely reliable,
865 it should not use @code{after-change-major-mode-hook} as of yet.
866 859
867 When you defined a major mode using @code{define-derived-mode}, it 860 When you defined a major mode using @code{define-derived-mode}, it
868 automatically makes sure these conventions are followed. If you 861 automatically makes sure these conventions are followed. If you
869 define a major mode ``from scratch,'' not using 862 define a major mode ``by hand,'' not using @code{define-derived-mode},
870 @code{define-derived-mode}, make sure the major mode command follows 863 use the following functions to handle these conventions automatically.
871 these and other conventions. @xref{Major Mode Conventions}. You use
872 these functions to do it properly.
873 864
874 @defun run-mode-hooks &rest hookvars 865 @defun run-mode-hooks &rest hookvars
875 Major modes should run their mode hook using this function. It is 866 Major modes should run their mode hook using this function. It is
876 similar to @code{run-hooks} (@pxref{Hooks}), but it also runs 867 similar to @code{run-hooks} (@pxref{Hooks}), but it also runs
877 @code{after-change-major-mode-hook}. 868 @code{after-change-major-mode-hook}.
878 869
879 When the call to this function is dynamically inside a 870 When this function is called during the execution of a
880 @code{delay-mode-hooks} form, this function does not run any hooks. 871 @code{delay-mode-hooks} form, it does not run the hooks immediately.
881 Instead, it arranges for the next call to @code{run-mode-hooks} to run 872 Instead, it arranges for the next call to @code{run-mode-hooks} to run
882 @var{hookvars}. 873 them.
883 @end defun 874 @end defun
884 875
885 @defmac delay-mode-hooks body@dots{} 876 @defmac delay-mode-hooks body@dots{}
886 This macro executes @var{body} like @code{progn}, but all calls to 877 When one major mode command calls another, it should do so inside of
887 @code{run-mode-hooks} inside @var{body} delay running their hooks. 878 @code{delay-mode-hooks}.
888 They will be run by the first call to @code{run-mode-hooks} after exit 879
889 from @code{delay-mode-hooks}. This is the proper way for a major mode 880 This macro executes @var{body}, but tells all @code{run-mode-hooks}
890 command to invoke its parent mode. 881 calls during the execution of @var{body} to delay running their hooks.
882 The hooks will actually run during the next call to
883 @code{run-mode-hooks} after the end of the @code{delay-mode-hooks}
884 construct.
891 @end defmac 885 @end defmac
892 886
893 @defvar after-change-major-mode-hook 887 @defvar after-change-major-mode-hook
894 Every major mode function should run this normal hook at its very end. 888 This is a normal hook run by @code{run-mode-hooks}. It is run at the
895 It normally does not need to do so explicitly. Indeed, a major mode 889 very end of every properly-written major mode function.
896 function should normally run its mode hook with @code{run-mode-hooks}
897 as the very last thing it does, and the last thing
898 @code{run-mode-hooks} does is run @code{after-change-major-mode-hook}.
899 @end defvar 890 @end defvar
900 891
901 @node Example Major Modes 892 @node Example Major Modes
902 @subsection Major Mode Examples 893 @subsection Major Mode Examples
903 894
1056 ;; @r{Create an abbrev table for lisp-mode.} 1047 ;; @r{Create an abbrev table for lisp-mode.}
1057 (define-abbrev-table 'lisp-mode-abbrev-table ()) 1048 (define-abbrev-table 'lisp-mode-abbrev-table ())
1058 @end group 1049 @end group
1059 @end smallexample 1050 @end smallexample
1060 1051
1061 Much code is shared among the three Lisp modes. The following 1052 The three modes for Lisp share much of their code. For instance,
1062 function sets various variables; it is called by each of the major Lisp 1053 each calls the following function to set various variables:
1063 mode functions:
1064 1054
1065 @smallexample 1055 @smallexample
1066 @group 1056 @group
1067 (defun lisp-mode-variables (lisp-syntax) 1057 (defun lisp-mode-variables (lisp-syntax)
1068 (when lisp-syntax 1058 (when lisp-syntax
1070 (setq local-abbrev-table lisp-mode-abbrev-table) 1060 (setq local-abbrev-table lisp-mode-abbrev-table)
1071 @dots{} 1061 @dots{}
1072 @end group 1062 @end group
1073 @end smallexample 1063 @end smallexample
1074 1064
1075 Functions such as @code{forward-paragraph} use the value of the 1065 In Lisp and most programming languages, we want the paragraph
1076 @code{paragraph-start} variable. Since Lisp code is different from 1066 commands to treat only blank lines as paragraph separators. And the
1077 ordinary text, the @code{paragraph-start} variable needs to be set 1067 modes should undestand the Lisp conventions for comments. The rest of
1078 specially to handle Lisp. Also, comments are indented in a special 1068 @code{lisp-mode-variables} sets this up:
1079 fashion in Lisp and the Lisp modes need their own mode-specific
1080 @code{comment-indent-function}. The code to set these variables is the
1081 rest of @code{lisp-mode-variables}.
1082 1069
1083 @smallexample 1070 @smallexample
1084 @group 1071 @group
1085 (make-local-variable 'paragraph-start) 1072 (make-local-variable 'paragraph-start)
1086 (setq paragraph-start (concat page-delimiter "\\|$" )) 1073 (setq paragraph-start (concat page-delimiter "\\|$" ))