comparison lispref/modes.texi @ 51917:3039afbb0746

(Major Mode Conventions): Explain about run-mode-hooks and about derived modes. (Minor Modes): Add minor-mode-list. (Defining Minor Modes): Keyword args for define-minor-mode. (Search-based Fontification): Explain managing other properties. (Other Font Lock Variables): Add font-lock-extra-managed-props. (Faces for Font Lock): Add font-locl-preprocessor-face. (Hooks): Add run-mode-hooks and delay-mode-hooks.
author Richard M. Stallman <rms@gnu.org>
date Mon, 14 Jul 2003 15:59:12 +0000
parents 8a7816e7f5bd
children b96d92c96bd1
comparison
equal deleted inserted replaced
51916:f7b97d07a39c 51917:3039afbb0746
261 @item 261 @item
262 @cindex mode hook 262 @cindex mode hook
263 @cindex major mode hook 263 @cindex major mode hook
264 Each major mode should have a @dfn{mode hook} named 264 Each major mode should have a @dfn{mode hook} named
265 @code{@var{modename}-mode-hook}. The major mode command should run that 265 @code{@var{modename}-mode-hook}. The major mode command should run that
266 hook, with @code{run-hooks}, as the very last thing it 266 hook, with @code{run-mode-hooks}, as the very last thing it
267 does. @xref{Hooks}. 267 does. @xref{Hooks}.
268 268
269 @item 269 @item
270 The major mode command may also run the hooks of some more basic modes. 270 The major mode command may start by calling some other major mode
271 For example, @code{indented-text-mode} runs @code{text-mode-hook} as 271 command (called the @dfn{parent mode}) and then alter some of its
272 well as @code{indented-text-mode-hook}. It may run these other hooks 272 settings. A mode that does this is called a @dfn{derived mode}. The
273 immediately before the mode's own hook (that is, after everything else), 273 recommended way to define one is to use @code{define-derived-mode},
274 or it may run them earlier. 274 but this is not required. Such a mode should use
275 @code{delay-mode-hooks} around its entire body, including the call to
276 the parent mode command and the final call to @code{run-mode-hooks}.
277 (Using @code{define-derived-mode} does this automatically.)
275 278
276 @item 279 @item
277 If something special should be done if the user switches a buffer from 280 If something special should be done if the user switches a buffer from
278 this mode to any other major mode, this mode can set up a buffer-local 281 this mode to any other major mode, this mode can set up a buffer-local
279 value for @code{change-major-mode-hook} (@pxref{Creating Buffer-Local}). 282 value for @code{change-major-mode-hook} (@pxref{Creating Buffer-Local}).
357 (define-key text-mode-map "\es" 'center-line) 360 (define-key text-mode-map "\es" 'center-line)
358 (define-key text-mode-map "\eS" 'center-paragraph)) 361 (define-key text-mode-map "\eS" 'center-paragraph))
359 @end group 362 @end group
360 @end smallexample 363 @end smallexample
361 364
362 Here is the complete major mode function definition for Text mode: 365 This was formerly the complete major mode function definition for Text mode:
363 366
364 @smallexample 367 @smallexample
365 @group 368 @group
366 (defun text-mode () 369 (defun text-mode ()
367 "Major mode for editing text intended for humans to read... 370 "Major mode for editing text intended for humans to read...
386 (setq indent-line-function 'indent-relative-maybe) 389 (setq indent-line-function 'indent-relative-maybe)
387 @end group 390 @end group
388 @group 391 @group
389 (setq mode-name "Text") 392 (setq mode-name "Text")
390 (setq major-mode 'text-mode) 393 (setq major-mode 'text-mode)
391 (run-hooks 'text-mode-hook)) ; @r{Finally, this permits the user to} 394 (run-mode-hooks 'text-mode-hook)) ; @r{Finally, this permits the user to}
392 ; @r{customize the mode with a hook.} 395 ; @r{customize the mode with a hook.}
393 @end group 396 @end group
394 @end smallexample 397 @end smallexample
395 398
396 @cindex @file{lisp-mode.el} 399 @cindex @file{lisp-mode.el}
541 (lisp-mode-variables t) ; @r{This defines various variables.} 544 (lisp-mode-variables t) ; @r{This defines various variables.}
542 @end group 545 @end group
543 @group 546 @group
544 (setq imenu-case-fold-search t) 547 (setq imenu-case-fold-search t)
545 (set-syntax-table lisp-mode-syntax-table) 548 (set-syntax-table lisp-mode-syntax-table)
546 (run-hooks 'lisp-mode-hook)) ; @r{This permits the user to use a} 549 (run-mode-hooks 'lisp-mode-hook)) ; @r{This permits the user to use a}
547 ; @r{hook to customize the mode.} 550 ; @r{hook to customize the mode.}
548 @end group 551 @end group
549 @end smallexample 552 @end smallexample
550 553
551 @node Auto Major Mode 554 @node Auto Major Mode
816 minor modes in effect. 819 minor modes in effect.
817 820
818 Often the biggest problem in implementing a minor mode is finding a 821 Often the biggest problem in implementing a minor mode is finding a
819 way to insert the necessary hook into the rest of Emacs. Minor mode 822 way to insert the necessary hook into the rest of Emacs. Minor mode
820 keymaps make this easier than it used to be. 823 keymaps make this easier than it used to be.
824
825 @defvar minor-mode-list
826 The value of this variable is a list of all minor mode commands.
827 @end defvar
821 828
822 @menu 829 @menu
823 * Minor Mode Conventions:: Tips for writing a minor mode. 830 * Minor Mode Conventions:: Tips for writing a minor mode.
824 * Keymaps and Minor Modes:: How a minor mode can have its own keymap. 831 * Keymaps and Minor Modes:: How a minor mode can have its own keymap.
825 * Defining Minor Modes:: A convenient facility for defining minor modes. 832 * Defining Minor Modes:: A convenient facility for defining minor modes.
979 986
980 The macro @code{define-minor-mode} offers a convenient way of 987 The macro @code{define-minor-mode} offers a convenient way of
981 implementing a mode in one self-contained definition. It supports only 988 implementing a mode in one self-contained definition. It supports only
982 buffer-local minor modes, not global ones. 989 buffer-local minor modes, not global ones.
983 990
984 @defmac define-minor-mode mode doc &optional init-value mode-indicator keymap body... 991 @defmac define-minor-mode mode doc [init-value [lighter [keymap keyword-args... body...]]]
985 @tindex define-minor-mode 992 @tindex define-minor-mode
986 This macro defines a new minor mode whose name is @var{mode} (a symbol). 993 This macro defines a new minor mode whose name is @var{mode} (a
987 It defines a command named @var{mode} to toggle the minor 994 symbol). It defines a command named @var{mode} to toggle the minor
988 mode, with @var{doc} as its documentation string. It also defines a 995 mode, with @var{doc} as its documentation string. It also defines a
989 variable named @var{mode}, which is set to @code{t} or @code{nil} by 996 variable named @var{mode}, which is set to @code{t} or @code{nil} by
990 enabling or disabling the mode. The variable is initialized to 997 enabling or disabling the mode. The variable is initialized to
991 @var{init-value}. 998 @var{init-value}.
992 999
1000 The string @var{lighter} says what to display in the mode line
1001 when the mode is enabled; if it is @code{nil}, the mode is not displayed
1002 in the mode line.
1003
1004 The optional argument @var{keymap} specifies the keymap for the minor mode.
1005 It can be a variable name, whose value is the keymap, or it can be an alist
1006 specifying bindings in this form:
1007
1008 @example
1009 (@var{key-sequence} . @var{definition})
1010 @end example
1011
1012 The @var{keyword-args} consist of keywords followed by corresponding
1013 values. A few keywords have special meanings:
1014
1015 @table @code
1016 @item :global @var{global}
1017 If non-@code{nil} specifies that the minor mode should be global.
1018 By default, minor modes are buffer-local.
1019
1020 @item :init-value @var{init-value}
1021 This is equivalent to specifying @var{init-value} positionally.
1022
1023 @item :lighter @var{lighter}
1024 This is equivalent to specifying @var{lighter} positionally.
1025
1026 @item :keymap @var{keymap}
1027 This is equivalent to specifying @var{keymap} positionally.
1028 @end table
1029
1030 Any other keyword arguments are passed passed directly to the
1031 @code{defcustom} generated for the variable @var{mode}.
1032
993 The command named @var{mode} finishes by executing the @var{body} forms, 1033 The command named @var{mode} finishes by executing the @var{body} forms,
994 if any, after it has performed the standard actions such as setting 1034 if any, after it has performed the standard actions such as setting
995 the variable named @var{mode}. 1035 the variable named @var{mode}.
996
997 The string @var{mode-indicator} says what to display in the mode line
998 when the mode is enabled; if it is @code{nil}, the mode is not displayed
999 in the mode line.
1000
1001 The optional argument @var{keymap} specifies the keymap for the minor mode.
1002 It can be a variable name, whose value is the keymap, or it can be an alist
1003 specifying bindings in this form:
1004
1005 @example
1006 (@var{key-sequence} . @var{definition})
1007 @end example
1008 @end defmac 1036 @end defmac
1037
1038 @findex easy-mmode-define-minor-mode
1039 The name @code{easy-mmode-define-minor-mode} is an alias
1040 for this macro.
1009 1041
1010 Here is an example of using @code{define-minor-mode}: 1042 Here is an example of using @code{define-minor-mode}:
1011 1043
1012 @smallexample 1044 @smallexample
1013 (define-minor-mode hungry-mode 1045 (define-minor-mode hungry-mode
1026 ;; The minor mode bindings. 1058 ;; The minor mode bindings.
1027 '(("\C-\^?" . hungry-electric-delete) 1059 '(("\C-\^?" . hungry-electric-delete)
1028 ("\C-\M-\^?" 1060 ("\C-\M-\^?"
1029 . (lambda () 1061 . (lambda ()
1030 (interactive) 1062 (interactive)
1031 (hungry-electric-delete t))))) 1063 (hungry-electric-delete t))))
1064 :group 'hunger)
1032 @end smallexample 1065 @end smallexample
1033 1066
1034 @noindent 1067 @noindent
1035 This defines a minor mode named ``Hungry mode'', a command named 1068 This defines a minor mode named ``Hungry mode'', a command named
1036 @code{hungry-mode} to toggle it, a variable named @code{hungry-mode} 1069 @code{hungry-mode} to toggle it, a variable named @code{hungry-mode}
1037 which indicates whether the mode is enabled, and a variable named 1070 which indicates whether the mode is enabled, and a variable named
1038 @code{hungry-mode-map} which holds the keymap that is active when the 1071 @code{hungry-mode-map} which holds the keymap that is active when the
1039 mode is enabled. It initializes the keymap with key bindings for 1072 mode is enabled. It initializes the keymap with key bindings for
1040 @kbd{C-@key{DEL}} and @kbd{C-M-@key{DEL}}. 1073 @kbd{C-@key{DEL}} and @kbd{C-M-@key{DEL}}. It puts the variable
1041 1074 @code{hungry-mode} into custom group @code{hunger}. There are no
1042 1075 @var{body} forms---many minor modes don't need any.
1043 @findex easy-mmode-define-minor-mode 1076
1044 The name @code{easy-mmode-define-minor-mode} is an alias 1077 Here's an equivalent way to write it:
1045 for this macro. 1078
1079 @smallexample
1080 (define-minor-mode hungry-mode
1081 "Toggle Hungry mode.
1082 With no argument, this command toggles the mode.
1083 Non-null prefix argument turns on the mode.
1084 Null prefix argument turns off the mode.
1085
1086 When Hungry mode is enabled, the control delete key
1087 gobbles all preceding whitespace except the last.
1088 See the command \\[hungry-electric-delete]."
1089 ;; The initial value.
1090 :initial-value nil
1091 ;; The indicator for the mode line.
1092 :lighter " Hungry"
1093 ;; The minor mode bindings.
1094 :keymap
1095 '(("\C-\^?" . hungry-electric-delete)
1096 ("\C-\M-\^?"
1097 . (lambda ()
1098 (interactive)
1099 (hungry-electric-delete t))))
1100 :group 'hunger)
1101 @end smallexample
1046 1102
1047 @node Mode Line Format 1103 @node Mode Line Format
1048 @section Mode Line Format 1104 @section Mode Line Format
1049 @cindex mode line 1105 @cindex mode line
1050 1106
1883 ;; @r{Highlight occurrences of @samp{fubar},} 1939 ;; @r{Highlight occurrences of @samp{fubar},}
1884 ;; @r{using the face which is the value of @code{fubar-face}.} 1940 ;; @r{using the face which is the value of @code{fubar-face}.}
1885 ("fubar" . fubar-face) 1941 ("fubar" . fubar-face)
1886 @end example 1942 @end example
1887 1943
1944 The value of @var{facename} is usually a face name (a symbol), but it
1945 can also be a list of the form
1946
1947 @example
1948 (face @var{face} @var{prop1} @var{val1} @var{prop2} @var{val2}@dots{})
1949 @end example
1950
1951 to specify various text properties to put on the text that matches.
1952 If you do this, be sure to add the other text property names that you
1953 set in this way to the value of @code{font-lock-extra-managed-props}
1954 so that the properties will also be cleared out when they are no longer
1955 appropriate.
1956
1888 @item (@var{matcher} . @var{highlighter}) 1957 @item (@var{matcher} . @var{highlighter})
1889 In this kind of element, @var{highlighter} is a list 1958 In this kind of element, @var{highlighter} is a list
1890 which specifies how to highlight matches found by @var{matcher}. 1959 which specifies how to highlight matches found by @var{matcher}.
1891 It has the form 1960 It has the form
1892 1961
2058 but not too large so that refontification becomes slow. Typical values 2127 but not too large so that refontification becomes slow. Typical values
2059 are @code{mark-defun} for programming modes or @code{mark-paragraph} for 2128 are @code{mark-defun} for programming modes or @code{mark-paragraph} for
2060 textual modes. 2129 textual modes.
2061 @end defvar 2130 @end defvar
2062 2131
2132 @defvar font-lock-extra-managed-props
2133 Additional properties (other than @code{face}) that are being managed
2134 by Font Lock mode. Font Lock mode normally manages only the @code{face}
2135 property; if you want it to manage others as well, you must specify
2136 them in a @var{facename} in @code{font-lock-keywords} as well as adding
2137 them to this list.
2138 @end defvar
2139
2063 @node Levels of Font Lock 2140 @node Levels of Font Lock
2064 @subsection Levels of Font Lock 2141 @subsection Levels of Font Lock
2065 2142
2066 Many major modes offer three different levels of fontification. You 2143 Many major modes offer three different levels of fontification. You
2067 can define multiple levels by using a list of symbols for @var{keywords} 2144 can define multiple levels by using a list of symbols for @var{keywords}
2161 2238
2162 @item font-lock-constant-face 2239 @item font-lock-constant-face
2163 @vindex font-lock-constant-face 2240 @vindex font-lock-constant-face
2164 Used (typically) for constant names. 2241 Used (typically) for constant names.
2165 2242
2243 @item font-locl-preprocessor-face
2244 @vindex font-locl-preprocessor-face
2245 Used (typically) for preprocessor commands.
2246
2166 @item font-lock-warning-face 2247 @item font-lock-warning-face
2167 @vindex font-lock-warning-face 2248 @vindex font-lock-warning-face
2168 Used (typically) for constructs that are peculiar, or that greatly 2249 Used (typically) for constructs that are peculiar, or that greatly
2169 change the meaning of other text. For example, this is used for 2250 change the meaning of other text. For example, this is used for
2170 @samp{;;;###autoload} cookies in Emacs Lisp, and for @code{#error} 2251 @samp{;;;###autoload} cookies in Emacs Lisp, and for @code{#error}
2310 @example 2391 @example
2311 (run-hooks 'emacs-lisp-mode-hook) 2392 (run-hooks 'emacs-lisp-mode-hook)
2312 @end example 2393 @end example
2313 @end defun 2394 @end defun
2314 2395
2396 @defun run-mode-hooks &rest hookvars
2397 Like @code{run-hooks}, but is affected by the @code{delay-mode-hooks}
2398 macro.
2399 @end defun
2400
2401 @defmac delay-mode-hooks body...
2402 This macro executes the @var{body} forms but defers all calls to
2403 @code{run-mode-hooks} within them until the end of @var{body}.
2404 This macro enables a derived mode to arrange not to run
2405 its parent modes' mode hooks until the end.
2406 @end defmac
2407
2315 @defun run-hook-with-args hook &rest args 2408 @defun run-hook-with-args hook &rest args
2316 This function is the way to run an abnormal hook which passes arguments 2409 This function is the way to run an abnormal hook which passes arguments
2317 to the hook functions. It calls each of the hook functions, passing 2410 to the hook functions. It calls each of the hook functions, passing
2318 each of them the arguments @var{args}. 2411 each of them the arguments @var{args}.
2319 @end defun 2412 @end defun