Mercurial > emacs
comparison lispref/modes.texi @ 25875:6a17c48b52ef
*** empty log message ***
author | Phillip Rulon <pjr@gnu.org> |
---|---|
date | Tue, 05 Oct 1999 23:26:05 +0000 |
parents | 467b88fab665 |
children | 7996385fc601 |
comparison
equal
deleted
inserted
replaced
25874:906a123add31 | 25875:6a17c48b52ef |
---|---|
52 If the new mode is similar to an old one, it is often unwise to modify | 52 If the new mode is similar to an old one, it is often unwise to modify |
53 the old one to serve two purposes, since it may become harder to use and | 53 the old one to serve two purposes, since it may become harder to use and |
54 maintain. Instead, copy and rename an existing major mode definition | 54 maintain. Instead, copy and rename an existing major mode definition |
55 and alter the copy---or define a @dfn{derived mode} (@pxref{Derived | 55 and alter the copy---or define a @dfn{derived mode} (@pxref{Derived |
56 Modes}). For example, Rmail Edit mode, which is in | 56 Modes}). For example, Rmail Edit mode, which is in |
57 @file{emacs/lisp/rmailedit.el}, is a major mode that is very similar to | 57 @file{emacs/lisp/mail/rmailedit.el}, is a major mode that is very similar to |
58 Text mode except that it provides three additional commands. Its | 58 Text mode except that it provides two additional commands. Its |
59 definition is distinct from that of Text mode, but was derived from it. | 59 definition is distinct from that of Text mode, but uses that of Text mode. |
60 | 60 |
61 Rmail Edit mode offers an example of changing the major mode | 61 Rmail Edit mode offers an example of changing the major mode |
62 temporarily for a buffer, so it can be edited in a different way (with | 62 temporarily for a buffer, so it can be edited in a different way (with |
63 ordinary Emacs commands rather than Rmail commands). In such cases, the | 63 ordinary Emacs commands rather than Rmail commands). In such cases, the |
64 temporary major mode usually provides a command to switch back to the | 64 temporary major mode usually provides a command to switch back to the |
71 Editing}. | 71 Editing}. |
72 | 72 |
73 The standard GNU Emacs Lisp library directory tree contains the code | 73 The standard GNU Emacs Lisp library directory tree contains the code |
74 for several major modes, in files such as @file{text-mode.el}, | 74 for several major modes, in files such as @file{text-mode.el}, |
75 @file{texinfo.el}, @file{lisp-mode.el}, @file{c-mode.el}, and | 75 @file{texinfo.el}, @file{lisp-mode.el}, @file{c-mode.el}, and |
76 @file{rmail.el}. You can study these libraries to see how modes are | 76 @file{rmail.el}. They are found in various subdirectories of the |
77 written. Text mode is perhaps the simplest major mode aside from | 77 @file{lisp} directory. You can study these libraries to see how modes |
78 are written. Text mode is perhaps the simplest major mode aside from | |
78 Fundamental mode. Rmail mode is a complicated and specialized mode. | 79 Fundamental mode. Rmail mode is a complicated and specialized mode. |
79 | 80 |
80 @menu | 81 @menu |
81 * Major Mode Conventions:: Coding conventions for keymaps, etc. | 82 * Major Mode Conventions:: Coding conventions for keymaps, etc. |
82 * Example Major Modes:: Text mode and Lisp modes. | 83 * Example Major Modes:: Text mode and Lisp modes. |
208 @code{make-variable-buffer-local}. The latter function would make the | 209 @code{make-variable-buffer-local}. The latter function would make the |
209 variable local to every buffer in which it is subsequently set, which | 210 variable local to every buffer in which it is subsequently set, which |
210 would affect buffers that do not use this mode. It is undesirable for a | 211 would affect buffers that do not use this mode. It is undesirable for a |
211 mode to have such global effects. @xref{Buffer-Local Variables}. | 212 mode to have such global effects. @xref{Buffer-Local Variables}. |
212 | 213 |
213 It's OK to use @code{make-variable-buffer-local}, if you wish, for a | 214 With rare exceptions, the only reasonable way to use use |
214 variable used only within a single Lisp package. | 215 @code{make-variable-buffer-local} in a Lisp package is for a variable |
216 which is used only within that package. Using it on a variable used by | |
217 other packages would interfere with them. | |
215 | 218 |
216 @item | 219 @item |
217 @cindex mode hook | 220 @cindex mode hook |
218 @cindex major mode hook | 221 @cindex major mode hook |
219 Each major mode should have a @dfn{mode hook} named | 222 Each major mode should have a @dfn{mode hook} named |
256 autoload, you should add this element in the same file that calls | 259 autoload, you should add this element in the same file that calls |
257 @code{autoload}. Otherwise, it is sufficient to add the element in the | 260 @code{autoload}. Otherwise, it is sufficient to add the element in the |
258 file that contains the mode definition. @xref{Auto Major Mode}. | 261 file that contains the mode definition. @xref{Auto Major Mode}. |
259 | 262 |
260 @item | 263 @item |
261 @cindex @file{.emacs} customization | |
262 In the documentation, you should provide a sample @code{autoload} form | 264 In the documentation, you should provide a sample @code{autoload} form |
263 and an example of how to add to @code{auto-mode-alist}, that users can | 265 and an example of how to add to @code{auto-mode-alist}, that users can |
264 include in their @file{.emacs} files. | 266 include in their init files (@pxref{Init File}). |
265 | 267 |
266 @item | 268 @item |
267 @cindex mode loading | 269 @cindex mode loading |
268 The top-level forms in the file defining the mode should be written so | 270 The top-level forms in the file defining the mode should be written so |
269 that they may be evaluated more than once without adverse consequences. | 271 that they may be evaluated more than once without adverse consequences. |
298 "Abbrev table used while in text mode.") | 300 "Abbrev table used while in text mode.") |
299 (define-abbrev-table 'text-mode-abbrev-table ()) | 301 (define-abbrev-table 'text-mode-abbrev-table ()) |
300 @end group | 302 @end group |
301 | 303 |
302 @group | 304 @group |
303 (defvar text-mode-map nil) ; @r{Create a mode-specific keymap.} | 305 (defvar text-mode-map nil ; @r{Create a mode-specific keymap.} |
306 "Keymap for Text mode. | |
307 Many other modes, such as Mail mode, Outline mode and Indented Text mode, | |
308 inherit all the commands defined in this map.") | |
304 | 309 |
305 (if text-mode-map | 310 (if text-mode-map |
306 () ; @r{Do not change the keymap if it is already set up.} | 311 () ; @r{Do not change the keymap if it is already set up.} |
307 (setq text-mode-map (make-sparse-keymap)) | 312 (setq text-mode-map (make-sparse-keymap)) |
313 (define-key text-mode-map "\e\t" 'ispell-complete-word) | |
308 (define-key text-mode-map "\t" 'indent-relative) | 314 (define-key text-mode-map "\t" 'indent-relative) |
309 (define-key text-mode-map "\es" 'center-line) | 315 (define-key text-mode-map "\es" 'center-line) |
310 (define-key text-mode-map "\eS" 'center-paragraph)) | 316 (define-key text-mode-map "\eS" 'center-paragraph)) |
311 @end group | 317 @end group |
312 @end smallexample | 318 @end smallexample |
332 @group | 338 @group |
333 (make-local-variable 'paragraph-start) | 339 (make-local-variable 'paragraph-start) |
334 (setq paragraph-start (concat "[ \t]*$\\|" page-delimiter)) | 340 (setq paragraph-start (concat "[ \t]*$\\|" page-delimiter)) |
335 (make-local-variable 'paragraph-separate) | 341 (make-local-variable 'paragraph-separate) |
336 (setq paragraph-separate paragraph-start) | 342 (setq paragraph-separate paragraph-start) |
343 (make-local-variable 'indent-line-function) | |
344 (setq indent-line-function 'indent-relative-maybe) | |
337 @end group | 345 @end group |
338 @group | 346 @group |
339 (setq mode-name "Text") | 347 (setq mode-name "Text") |
340 (setq major-mode 'text-mode) | 348 (setq major-mode 'text-mode) |
341 (run-hooks 'text-mode-hook)) ; @r{Finally, this permits the user to} | 349 (run-hooks 'text-mode-hook)) ; @r{Finally, this permits the user to} |
420 @dots{} | 428 @dots{} |
421 @end group | 429 @end group |
422 @group | 430 @group |
423 (make-local-variable 'comment-indent-function) | 431 (make-local-variable 'comment-indent-function) |
424 (setq comment-indent-function 'lisp-comment-indent)) | 432 (setq comment-indent-function 'lisp-comment-indent)) |
433 @dots{} | |
425 @end group | 434 @end group |
426 @end smallexample | 435 @end smallexample |
427 | 436 |
428 Each of the different Lisp modes has a slightly different keymap. For | 437 Each of the different Lisp modes has a slightly different keymap. For |
429 example, Lisp mode binds @kbd{C-c C-z} to @code{run-lisp}, but the other | 438 example, Lisp mode binds @kbd{C-c C-z} to @code{run-lisp}, but the other |
640 @var{function} t)} can uncompress the file and then put the uncompressed | 649 @var{function} t)} can uncompress the file and then put the uncompressed |
641 file in the proper mode according to the name sans @samp{.gz}. | 650 file in the proper mode according to the name sans @samp{.gz}. |
642 | 651 |
643 Here is an example of how to prepend several pattern pairs to | 652 Here is an example of how to prepend several pattern pairs to |
644 @code{auto-mode-alist}. (You might use this sort of expression in your | 653 @code{auto-mode-alist}. (You might use this sort of expression in your |
645 @file{.emacs} file.) | 654 init file.) |
646 | 655 |
647 @smallexample | 656 @smallexample |
648 @group | 657 @group |
649 (setq auto-mode-alist | 658 (setq auto-mode-alist |
650 (append | 659 (append |
879 When you add an element to @code{minor-mode-alist}, use @code{assq} to | 888 When you add an element to @code{minor-mode-alist}, use @code{assq} to |
880 check for an existing element, to avoid duplication. For example: | 889 check for an existing element, to avoid duplication. For example: |
881 | 890 |
882 @smallexample | 891 @smallexample |
883 @group | 892 @group |
884 (or (assq 'leif-mode minor-mode-alist) | 893 (unless (assq 'leif-mode minor-mode-alist) |
885 (setq minor-mode-alist | 894 (setq minor-mode-alist |
886 (cons '(leif-mode " Leif") minor-mode-alist))) | 895 (cons '(leif-mode " Leif") minor-mode-alist))) |
896 @end group | |
897 @end smallexample | |
898 | |
899 @noindent | |
900 or like this, using @code{add-to-list} (@pxref{Setting Variables}): | |
901 | |
902 @smallexample | |
903 @group | |
904 (add-to-list 'minor-mode-alist '(leif-mode " Leif")) | |
887 @end group | 905 @end group |
888 @end smallexample | 906 @end smallexample |
889 @end itemize | 907 @end itemize |
890 | |
891 You can also use @code{add-to-list} to add an element to this list | |
892 just once (@pxref{Setting Variables}). | |
893 | 908 |
894 Global minor modes distributed with Emacs should if possible support | 909 Global minor modes distributed with Emacs should if possible support |
895 enabling and disabling via Custom (@pxref{Customization}). To do this, | 910 enabling and disabling via Custom (@pxref{Customization}). To do this, |
896 the first step is to define the mode variable with @code{defcustom}, and | 911 the first step is to define the mode variable with @code{defcustom}, and |
897 specify @code{:type boolean}. | 912 specify @code{:type boolean}. |
1062 @node Mode Line Data | 1077 @node Mode Line Data |
1063 @subsection The Data Structure of the Mode Line | 1078 @subsection The Data Structure of the Mode Line |
1064 @cindex mode line construct | 1079 @cindex mode line construct |
1065 | 1080 |
1066 The mode line contents are controlled by a data structure of lists, | 1081 The mode line contents are controlled by a data structure of lists, |
1067 strings, symbols, and numbers kept in the buffer-local variable | 1082 strings, symbols, and numbers kept in buffer-local variables. The data |
1068 @code{mode-line-format}. The data structure is called a @dfn{mode line | 1083 structure is called a @dfn{mode line construct}, and it is built in |
1069 construct}, and it is built in recursive fashion out of simpler mode line | 1084 recursive fashion out of simpler mode line constructs. The same data |
1070 constructs. The same data structure is used for constructing | 1085 structure is used for constructing frame titles (@pxref{Frame Titles}) |
1071 frame titles (@pxref{Frame Titles}). | 1086 and header lines (@pxref{Header Lines}). |
1072 | 1087 |
1073 @defvar mode-line-format | 1088 @defvar mode-line-format |
1074 The value of this variable is a mode line construct with overall | 1089 The value of this variable is a mode line construct with overall |
1075 responsibility for the mode line format. The value of this variable | 1090 responsibility for the mode line format. The value of this variable |
1076 controls which other variables are used to form the mode line text, and | 1091 controls which other variables are used to form the mode line text, and |
1297 This buffer-local variable contains the mode line information on process | 1312 This buffer-local variable contains the mode line information on process |
1298 status in modes used for communicating with subprocesses. It is | 1313 status in modes used for communicating with subprocesses. It is |
1299 displayed immediately following the major mode name, with no intervening | 1314 displayed immediately following the major mode name, with no intervening |
1300 space. For example, its value in the @samp{*shell*} buffer is | 1315 space. For example, its value in the @samp{*shell*} buffer is |
1301 @code{(":%s")}, which allows the shell to display its status along | 1316 @code{(":%s")}, which allows the shell to display its status along |
1302 with the major mode as: @samp{(Shell:@: run)}. Normally this variable | 1317 with the major mode as: @samp{(Shell:run)}. Normally this variable |
1303 is @code{nil}. | 1318 is @code{nil}. |
1304 @end defvar | 1319 @end defvar |
1320 | |
1321 Some variables are used by @code{minor-mode-alist} to display | |
1322 a string for various minor modes when enabled. This is a typical | |
1323 example: | |
1324 | |
1325 @defvar vc-mode | |
1326 The variable @code{vc-mode}, buffer-local in each buffer, records | |
1327 whether the buffer's visited file is maintained with version control, | |
1328 and, if so, which kind. Its value is a string that appears in the mode | |
1329 line, or @code{nil} for no version control. | |
1330 @end defvar | |
1331 | |
1332 The variable @code{default-mode-line-format} is where | |
1333 @code{mode-line-format} usually gets its value: | |
1305 | 1334 |
1306 @defvar default-mode-line-format | 1335 @defvar default-mode-line-format |
1307 This variable holds the default @code{mode-line-format} for buffers | 1336 This variable holds the default @code{mode-line-format} for buffers |
1308 that do not override it. This is the same as @code{(default-value | 1337 that do not override it. This is the same as @code{(default-value |
1309 'mode-line-format)}. | 1338 'mode-line-format)}. |
1320 @end group | 1349 @end group |
1321 " " | 1350 " " |
1322 global-mode-string | 1351 global-mode-string |
1323 @group | 1352 @group |
1324 " %[(" | 1353 " %[(" |
1354 ;; @r{@code{mode-line-mode-name} is a function} | |
1355 ;; @r{that copies the mode name and adds text | |
1356 ;; @r{properties to make it mouse-sensitive.} | |
1325 (:eval (mode-line-mode-name)) | 1357 (:eval (mode-line-mode-name)) |
1326 mode-line-process | 1358 mode-line-process |
1327 minor-mode-alist | 1359 minor-mode-alist |
1328 "%n" | 1360 "%n" |
1329 ")%]--" | 1361 ")%]--" |
1336 "-%-") | 1368 "-%-") |
1337 @end group | 1369 @end group |
1338 @end example | 1370 @end example |
1339 @end defvar | 1371 @end defvar |
1340 | 1372 |
1341 @defvar vc-mode | |
1342 The variable @code{vc-mode}, buffer-local in each buffer, records | |
1343 whether the buffer's visited file is maintained with version control, | |
1344 and, if so, which kind. Its value is @code{nil} for no version control, | |
1345 or a string that appears in the mode line. | |
1346 @end defvar | |
1347 | |
1348 @node %-Constructs | 1373 @node %-Constructs |
1349 @subsection @code{%}-Constructs in the Mode Line | 1374 @subsection @code{%}-Constructs in the Mode Line |
1350 | 1375 |
1351 The following table lists the recognized @code{%}-constructs and what | 1376 The following table lists the recognized @code{%}-constructs and what |
1352 they mean. In any construct except @samp{%%}, you can add a decimal | 1377 they mean. In any construct except @samp{%%}, you can add a decimal |
1355 @table @code | 1380 @table @code |
1356 @item %b | 1381 @item %b |
1357 The current buffer name, obtained with the @code{buffer-name} function. | 1382 The current buffer name, obtained with the @code{buffer-name} function. |
1358 @xref{Buffer Names}. | 1383 @xref{Buffer Names}. |
1359 | 1384 |
1385 @item %c | |
1386 The current column number of point. | |
1387 | |
1360 @item %f | 1388 @item %f |
1361 The visited file name, obtained with the @code{buffer-file-name} | 1389 The visited file name, obtained with the @code{buffer-file-name} |
1362 function. @xref{Buffer File Name}. | 1390 function. @xref{Buffer File Name}. |
1363 | 1391 |
1364 @item %F | 1392 @item %F |
1365 The title (only on a window system) or the name of the selected frame. | 1393 The title (only on a window system) or the name of the selected frame. |
1366 @xref{Window Frame Parameters}. | 1394 @xref{Window Frame Parameters}. |
1367 | 1395 |
1368 @item %c | |
1369 The current column number of point. | |
1370 | |
1371 @item %l | 1396 @item %l |
1372 The current line number of point, counting within the accessible portion | 1397 The current line number of point, counting within the accessible portion |
1373 of the buffer. | 1398 of the buffer. |
1399 | |
1400 @item %n | |
1401 @samp{Narrow} when narrowing is in effect; nothing otherwise (see | |
1402 @code{narrow-to-region} in @ref{Narrowing}). | |
1403 | |
1404 @item %p | |
1405 The percentage of the buffer text above the @strong{top} of window, or | |
1406 @samp{Top}, @samp{Bottom} or @samp{All}. Note that the default | |
1407 mode-line specification truncates this to three characters. | |
1408 | |
1409 @item %P | |
1410 The percentage of the buffer text that is above the @strong{bottom} of | |
1411 the window (which includes the text visible in the window, as well as | |
1412 the text above the top), plus @samp{Top} if the top of the buffer is | |
1413 visible on screen; or @samp{Bottom} or @samp{All}. | |
1414 | |
1415 @item %s | |
1416 The status of the subprocess belonging to the current buffer, obtained with | |
1417 @code{process-status}. @xref{Process Information}. | |
1418 | |
1419 @item %t | |
1420 Whether the visited file is a text file or a binary file. This is a | |
1421 meaningful distinction only on certain operating systems (@pxref{MS-DOS | |
1422 File Types}). | |
1374 | 1423 |
1375 @item %* | 1424 @item %* |
1376 @samp{%} if the buffer is read only (see @code{buffer-read-only}); @* | 1425 @samp{%} if the buffer is read only (see @code{buffer-read-only}); @* |
1377 @samp{*} if the buffer is modified (see @code{buffer-modified-p}); @* | 1426 @samp{*} if the buffer is modified (see @code{buffer-modified-p}); @* |
1378 @samp{-} otherwise. @xref{Buffer Modification}. | 1427 @samp{-} otherwise. @xref{Buffer Modification}. |
1384 read-only buffer. @xref{Buffer Modification}. | 1433 read-only buffer. @xref{Buffer Modification}. |
1385 | 1434 |
1386 @item %& | 1435 @item %& |
1387 @samp{*} if the buffer is modified, and @samp{-} otherwise. | 1436 @samp{*} if the buffer is modified, and @samp{-} otherwise. |
1388 | 1437 |
1389 @item %s | |
1390 The status of the subprocess belonging to the current buffer, obtained with | |
1391 @code{process-status}. @xref{Process Information}. | |
1392 | |
1393 @item %t | |
1394 Whether the visited file is a text file or a binary file. (This is a | |
1395 meaningful distinction only on certain operating systems.) | |
1396 | |
1397 @item %p | |
1398 The percentage of the buffer text above the @strong{top} of window, or | |
1399 @samp{Top}, @samp{Bottom} or @samp{All}. | |
1400 | |
1401 @item %P | |
1402 The percentage of the buffer text that is above the @strong{bottom} of | |
1403 the window (which includes the text visible in the window, as well as | |
1404 the text above the top), plus @samp{Top} if the top of the buffer is | |
1405 visible on screen; or @samp{Bottom} or @samp{All}. | |
1406 | |
1407 @item %n | |
1408 @samp{Narrow} when narrowing is in effect; nothing otherwise (see | |
1409 @code{narrow-to-region} in @ref{Narrowing}). | |
1410 | |
1411 @item %[ | 1438 @item %[ |
1412 An indication of the depth of recursive editing levels (not counting | 1439 An indication of the depth of recursive editing levels (not counting |
1413 minibuffer levels): one @samp{[} for each editing level. | 1440 minibuffer levels): one @samp{[} for each editing level. |
1414 @xref{Recursive Editing}. | 1441 @xref{Recursive Editing}. |
1415 | 1442 |
1416 @item %] | 1443 @item %] |
1417 One @samp{]} for each recursive editing level (not counting minibuffer | 1444 One @samp{]} for each recursive editing level (not counting minibuffer |
1418 levels). | 1445 levels). |
1419 | 1446 |
1447 @item %- | |
1448 Dashes sufficient to fill the remainder of the mode line. | |
1449 | |
1420 @item %% | 1450 @item %% |
1421 The character @samp{%}---this is how to include a literal @samp{%} in a | 1451 The character @samp{%}---this is how to include a literal @samp{%} in a |
1422 string in which @code{%}-constructs are allowed. | 1452 string in which @code{%}-constructs are allowed. |
1423 | |
1424 @item %- | |
1425 Dashes sufficient to fill the remainder of the mode line. | |
1426 @end table | 1453 @end table |
1427 | 1454 |
1428 The following two @code{%}-constructs are still supported, but they are | 1455 The following two @code{%}-constructs are still supported, but they are |
1429 obsolete, since you can get the same results with the variables | 1456 obsolete, since you can get the same results with the variables |
1430 @code{mode-name} and @code{global-mode-string}. | 1457 @code{mode-name} and @code{global-mode-string}. |
1465 @code{local-map} property. | 1492 @code{local-map} property. |
1466 @end enumerate | 1493 @end enumerate |
1467 | 1494 |
1468 You use the @code{local-map} property to specify a keymap. Like any | 1495 You use the @code{local-map} property to specify a keymap. Like any |
1469 keymap, it can bind character keys and function keys; but that has no | 1496 keymap, it can bind character keys and function keys; but that has no |
1470 effect, since is impossible to move point into the mode line, This | 1497 effect, since it is impossible to move point into the mode line. This |
1471 keymap can only take real effect for mouse clicks. | 1498 keymap can only take real effect for mouse clicks. |
1472 | 1499 |
1473 @node Header Lines | 1500 @node Header Lines |
1474 @subsection Window Header Lines | 1501 @subsection Window Header Lines |
1475 @cindex header line (of a window) | 1502 @cindex header line (of a window) |
1502 @cindex Imenu | 1529 @cindex Imenu |
1503 @dfn{Imenu} is a feature that lets users select a definition or | 1530 @dfn{Imenu} is a feature that lets users select a definition or |
1504 section in the buffer, from a menu which lists all of them, to go | 1531 section in the buffer, from a menu which lists all of them, to go |
1505 directly to that location in the buffer. Imenu works by constructing a | 1532 directly to that location in the buffer. Imenu works by constructing a |
1506 buffer index which lists the names and buffer positions of the | 1533 buffer index which lists the names and buffer positions of the |
1507 definitions, or portions of the buffer, so the user can pick one of them | 1534 definitions, or other named portions of the buffer; then the user can |
1508 to move to. This section explains how to customize Imenu for a major | 1535 choose one of them and move point to it. This section explains how to |
1509 mode. | 1536 customize how Imenu finds the definitions or buffer portions for a |
1537 particular major mode. | |
1510 | 1538 |
1511 The usual and simplest way is to set the variable | 1539 The usual and simplest way is to set the variable |
1512 @code{imenu-generic-expression}: | 1540 @code{imenu-generic-expression}: |
1513 | 1541 |
1514 @defvar imenu-generic-expression | 1542 @defvar imenu-generic-expression |
1526 @var{menu-title} is @code{nil}, the matches for this element go directly | 1554 @var{menu-title} is @code{nil}, the matches for this element go directly |
1527 in the top level of the buffer index. | 1555 in the top level of the buffer index. |
1528 | 1556 |
1529 The second item in the list, @var{regexp}, is a regular expression | 1557 The second item in the list, @var{regexp}, is a regular expression |
1530 (@pxref{Regular Expressions}); anything in the buffer that it matches is | 1558 (@pxref{Regular Expressions}); anything in the buffer that it matches is |
1531 considered a definition, to mention in the buffer index. The third | 1559 considered a definition, something to mention in the buffer index. The |
1532 item, @var{subexp}, indicates which subexpression in @var{regexp} | 1560 third item, @var{subexp}, indicates which subexpression in @var{regexp} |
1533 matches the definition's name. | 1561 matches the definition's name. |
1534 | 1562 |
1535 An element can also look like this: | 1563 An element can also look like this: |
1536 | 1564 |
1537 @example | 1565 @example |
1538 (@var{menu-title} @var{regexp} @var{index} @var{function} @var{arguments}@dots{}) | 1566 (@var{menu-title} @var{regexp} @var{index} @var{function} @var{arguments}@dots{}) |
1539 @end example | 1567 @end example |
1540 | 1568 |
1541 Each match for this element creates a special index item which, if | 1569 Each match for this element creates a special index item which, if |
1542 selected by the user, calls @var{function} with arguments | 1570 selected by the user, calls @var{function} with arguments consisting of |
1543 @var{item-name}, the buffer position, and @var{arguments}. | 1571 the item name, the buffer position, and @var{arguments}. |
1544 | 1572 |
1545 For Emacs Lisp mode, @var{pattern} could look like this: | 1573 For Emacs Lisp mode, @var{pattern} could look like this: |
1546 | 1574 |
1547 @c should probably use imenu-syntax-alist and \\sw rather than [-A-Za-z0-9+] | 1575 @c should probably use imenu-syntax-alist and \\sw rather than [-A-Za-z0-9+] |
1548 @example | 1576 @example |
1608 Another way to customize Imenu for a major mode is to set the | 1636 Another way to customize Imenu for a major mode is to set the |
1609 variables @code{imenu-prev-index-position-function} and | 1637 variables @code{imenu-prev-index-position-function} and |
1610 @code{imenu-extract-index-name-function}: | 1638 @code{imenu-extract-index-name-function}: |
1611 | 1639 |
1612 @defvar imenu-prev-index-position-function | 1640 @defvar imenu-prev-index-position-function |
1613 If this variable is non-@code{nil}, its value should be a function for | 1641 If this variable is non-@code{nil}, its value should be a funtion that |
1614 finding the next definition to mention in the buffer index, moving | 1642 finds the next ``definition'' to put in the buffer index, scanning |
1615 backwards in the file. | 1643 backward in the buffer from point. It should return @code{nil} if it |
1616 | 1644 doesn't find another ``definition'' before point. Otherwise it shuould |
1617 The function should leave point at the place to be connected to the | 1645 leave point at the place it finds a ``definition,'' and return any |
1618 index item; it should return @code{nil} if it doesn't find another item. | 1646 non-@code{nil} value. |
1619 | 1647 |
1620 Setting this variable makes it buffer-local in the current buffer. | 1648 Setting this variable makes it buffer-local in the current buffer. |
1621 @end defvar | 1649 @end defvar |
1622 | 1650 |
1623 @defvar imenu-extract-index-name-function | 1651 @defvar imenu-extract-index-name-function |
1628 | 1656 |
1629 Setting this variable makes it buffer-local in the current buffer. | 1657 Setting this variable makes it buffer-local in the current buffer. |
1630 @end defvar | 1658 @end defvar |
1631 | 1659 |
1632 The last way to customize Imenu for a major mode is to set the | 1660 The last way to customize Imenu for a major mode is to set the |
1633 variables @code{imenu-create-index-function}: | 1661 variable @code{imenu-create-index-function}: |
1634 | 1662 |
1635 @defvar imenu-create-index-function | 1663 @defvar imenu-create-index-function |
1636 This variable specifies the function to use for creating a buffer index. | 1664 This variable specifies the function to use for creating a buffer index. |
1637 The function should take no arguments, and return an index for the | 1665 The function should take no arguments, and return an index for the |
1638 current buffer. It is called within @code{save-excursion}, so where it | 1666 current buffer. It is called within @code{save-excursion}, so where it |
1671 @cindex Font Lock Mode | 1699 @cindex Font Lock Mode |
1672 | 1700 |
1673 @dfn{Font Lock mode} is a feature that automatically attaches | 1701 @dfn{Font Lock mode} is a feature that automatically attaches |
1674 @code{face} properties to certain parts of the buffer based on their | 1702 @code{face} properties to certain parts of the buffer based on their |
1675 syntactic role. How it parses the buffer depends on the major mode; | 1703 syntactic role. How it parses the buffer depends on the major mode; |
1676 most major modes define syntactic criteria for which faces to use, in | 1704 most major modes define syntactic criteria for which faces to use in |
1677 which contexts. This section explains how to customize Font Lock for a | 1705 which contexts. This section explains how to customize Font Lock for a |
1678 particular language---in other words, for a particular major mode. | 1706 particular major mode. |
1679 | 1707 |
1680 Font Lock mode finds text to highlight in two ways: through syntactic | 1708 Font Lock mode finds text to highlight in two ways: through syntactic |
1681 parsing based on the syntax table, and through searching (usually for | 1709 parsing based on the syntax table, and through searching (usually for |
1682 regular expressions). Syntactic fontification happens first; it finds | 1710 regular expressions). Syntactic fontification happens first; it finds |
1683 comments and string constants, and highlights them using | 1711 comments and string constants, and highlights them using |
1684 @code{font-lock-comment-face} and @code{font-lock-string-face} | 1712 @code{font-lock-comment-face} and @code{font-lock-string-face} |
1685 (@pxref{Faces for Font Lock}); search-based fontification follows. | 1713 (@pxref{Faces for Font Lock}). Search-based fontification follows. |
1686 | 1714 |
1687 @menu | 1715 @menu |
1688 * Font Lock Basics:: | 1716 * Font Lock Basics:: |
1689 * Search-based Fontification:: | 1717 * Search-based Fontification:: |
1690 * Other Font Lock Variables:: | 1718 * Other Font Lock Variables:: |
1712 @var{syntax-alist} @var{syntax-begin} @var{other-vars}@dots{}) | 1740 @var{syntax-alist} @var{syntax-begin} @var{other-vars}@dots{}) |
1713 @end example | 1741 @end example |
1714 | 1742 |
1715 The first element, @var{keywords}, indirectly specifies the value of | 1743 The first element, @var{keywords}, indirectly specifies the value of |
1716 @code{font-lock-keywords}. It can be a symbol, a variable whose value | 1744 @code{font-lock-keywords}. It can be a symbol, a variable whose value |
1717 is list to use for @code{font-lock-keywords}. It can also be a list of | 1745 is the list to use for @code{font-lock-keywords}. It can also be a list of |
1718 several such symbols, one for each possible level of fontification. The | 1746 several such symbols, one for each possible level of fontification. The |
1719 first symbol specifies how to do level 1 fontification, the second | 1747 first symbol specifies how to do level 1 fontification, the second |
1720 symbol how to do level 2, and so on. | 1748 symbol how to do level 2, and so on. |
1721 | 1749 |
1722 The second element, @var{keywords-only}, specifies the value of the | 1750 The second element, @var{keywords-only}, specifies the value of the |
1735 table is stored in @code{font-lock-syntax-table}. | 1763 table is stored in @code{font-lock-syntax-table}. |
1736 | 1764 |
1737 The fifth element, @var{syntax-begin}, specifies the value of | 1765 The fifth element, @var{syntax-begin}, specifies the value of |
1738 @code{font-lock-beginning-of-syntax-function} (see below). | 1766 @code{font-lock-beginning-of-syntax-function} (see below). |
1739 | 1767 |
1740 Any further elements @var{other-vars} are of the form | 1768 All the remaining elements (if any) are collectively called |
1741 @code{(@var{variable} . @var{value})}. This kind of element means to | 1769 @var{other-vars}. Each of these elements should have the form |
1742 make @var{variable} buffer-local and then set it to @var{value}. This | 1770 @code{(@var{variable} . @var{value})}---which means, make @var{variable} |
1743 is used to set other variables that affect fontification. | 1771 buffer-local and then set it to @var{value}. You can use these |
1772 @var{other-vars} to set other variables that affect fontification, | |
1773 aside from those you can control with the first five elements. | |
1744 @end defvar | 1774 @end defvar |
1745 | 1775 |
1746 @node Search-based Fontification | 1776 @node Search-based Fontification |
1747 @subsection Search-based Fontification | 1777 @subsection Search-based Fontification |
1748 | 1778 |
2108 @cindex hooks | 2138 @cindex hooks |
2109 | 2139 |
2110 A @dfn{hook} is a variable where you can store a function or functions | 2140 A @dfn{hook} is a variable where you can store a function or functions |
2111 to be called on a particular occasion by an existing program. Emacs | 2141 to be called on a particular occasion by an existing program. Emacs |
2112 provides hooks for the sake of customization. Most often, hooks are set | 2142 provides hooks for the sake of customization. Most often, hooks are set |
2113 up in the @file{.emacs} file, but Lisp programs can set them also. | 2143 up in the init file (@pxref{Init File}), but Lisp programs can set them also. |
2114 @xref{Standard Hooks}, for a list of standard hook variables. | 2144 @xref{Standard Hooks}, for a list of standard hook variables. |
2115 | 2145 |
2116 @cindex normal hook | 2146 @cindex normal hook |
2117 Most of the hooks in Emacs are @dfn{normal hooks}. These variables | 2147 Most of the hooks in Emacs are @dfn{normal hooks}. These variables |
2118 contain lists of functions to be called with no arguments. When the | 2148 contain lists of functions to be called with no arguments. When the |
2134 a Function}). Most normal hook variables are initially void; | 2164 a Function}). Most normal hook variables are initially void; |
2135 @code{add-hook} knows how to deal with this. | 2165 @code{add-hook} knows how to deal with this. |
2136 | 2166 |
2137 @cindex abnormal hook | 2167 @cindex abnormal hook |
2138 If the hook variable's name does not end with @samp{-hook}, that | 2168 If the hook variable's name does not end with @samp{-hook}, that |
2139 indicates it is probably an @dfn{abnormal hook}; you should look at its | 2169 indicates it is probably an @dfn{abnormal hook}. Then you should look at its |
2140 documentation to see how to use the hook properly. | 2170 documentation to see how to use the hook properly. |
2141 | 2171 |
2142 If the variable's name ends in @samp{-functions} or @samp{-hooks}, | 2172 If the variable's name ends in @samp{-functions} or @samp{-hooks}, |
2143 then the value is a list of functions, but it is abnormal in that either | 2173 then the value is a list of functions, but it is abnormal in that either |
2144 these functions are called with arguments or their values are used in | 2174 these functions are called with arguments or their values are used in |
2159 | 2189 |
2160 At the appropriate time, Emacs uses the @code{run-hooks} function to | 2190 At the appropriate time, Emacs uses the @code{run-hooks} function to |
2161 run particular hooks. This function calls the hook functions that have | 2191 run particular hooks. This function calls the hook functions that have |
2162 been added with @code{add-hook}. | 2192 been added with @code{add-hook}. |
2163 | 2193 |
2164 @defun run-hooks &rest hookvar | 2194 @defun run-hooks &rest hookvars |
2165 This function takes one or more hook variable names as arguments, and | 2195 This function takes one or more hook variable names as arguments, and |
2166 runs each hook in turn. Each @var{hookvar} argument should be a symbol | 2196 runs each hook in turn. Each argument should be a symbol that is a hook |
2167 that is a hook variable. These arguments are processed in the order | 2197 variable. These arguments are processed in the order specified. |
2168 specified. | |
2169 | 2198 |
2170 If a hook variable has a non-@code{nil} value, that value may be a | 2199 If a hook variable has a non-@code{nil} value, that value may be a |
2171 function or a list of functions. If the value is a function (either a | 2200 function or a list of functions. If the value is a function (either a |
2172 lambda expression or a symbol with a function definition), it is called. | 2201 lambda expression or a symbol with a function definition), it is called. |
2173 If it is a list, the elements are called, in order. The hook functions | 2202 If it is a list, the elements are called, in order. The hook functions |
2191 @defun run-hook-with-args-until-failure hook &rest args | 2220 @defun run-hook-with-args-until-failure hook &rest args |
2192 This function is the way to run an abnormal hook which passes arguments | 2221 This function is the way to run an abnormal hook which passes arguments |
2193 to the hook functions, and stops as soon as any hook function fails. It | 2222 to the hook functions, and stops as soon as any hook function fails. It |
2194 calls each of the hook functions, passing each of them the arguments | 2223 calls each of the hook functions, passing each of them the arguments |
2195 @var{args}, until some hook function returns @code{nil}. Then it stops, | 2224 @var{args}, until some hook function returns @code{nil}. Then it stops, |
2196 and returns @code{nil} if some hook function did, and otherwise | 2225 and returns @code{nil} if some hook function returned @code{nil}. |
2197 returns a non-@code{nil} value. | 2226 Otherwise it returns a non-@code{nil} value. |
2198 @end defun | 2227 @end defun |
2199 | 2228 |
2200 @defun run-hook-with-args-until-success hook &rest args | 2229 @defun run-hook-with-args-until-success hook &rest args |
2201 This function is the way to run an abnormal hook which passes arguments | 2230 This function is the way to run an abnormal hook which passes arguments |
2202 to the hook functions, and stops as soon as any hook function succeeds. | 2231 to the hook functions, and stops as soon as any hook function succeeds. |
2250 This function makes the hook variable @code{hook} buffer-local in the | 2279 This function makes the hook variable @code{hook} buffer-local in the |
2251 current buffer. When a hook variable is buffer-local, it can have | 2280 current buffer. When a hook variable is buffer-local, it can have |
2252 buffer-local and global hook functions, and @code{run-hooks} runs all of | 2281 buffer-local and global hook functions, and @code{run-hooks} runs all of |
2253 them. | 2282 them. |
2254 | 2283 |
2255 This function works by making @code{t} an element of the buffer-local | 2284 This function works by adding @code{t} as an element of the buffer-local |
2256 value. That serves as a flag to use the hook functions in the default | 2285 value. That serves as a flag to use the hook functions listed in the default |
2257 value of the hook variable as well as those in the buffer-local value. | 2286 value of the hook variable, as well as those listed in the buffer-local value. |
2258 Since @code{run-hooks} understands this flag, @code{make-local-hook} | 2287 Since @code{run-hooks} understands this flag, @code{make-local-hook} |
2259 works with all normal hooks. It works for only some non-normal | 2288 works with all normal hooks. It works for only some non-normal |
2260 hooks---those whose callers have been updated to understand this meaning | 2289 hooks---those whose callers have been updated to understand this meaning |
2261 of @code{t}. | 2290 of @code{t}. |
2262 | 2291 |