aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChong Yidong2006-08-07 13:39:53 +0000
committerChong Yidong2006-08-07 13:39:53 +0000
commit34e5974a22ec15f2baa2364b8fea18609fdfb690 (patch)
tree58eb7b62eb2be284b1a68d5c0b2e9461170015a1
parentf2c056984949eb4f9be66ad05a9ad865752ffa06 (diff)
downloademacs-34e5974a22ec15f2baa2364b8fea18609fdfb690.tar.gz
emacs-34e5974a22ec15f2baa2364b8fea18609fdfb690.zip
* modes.texi (Hooks): Clarify.
(Major Mode Basics): Mention define-derived-mode explicitly. (Major Mode Conventions): Rebinding RET is OK for some modes. Mention change-major-mode-hook and after-change-major-mode-hook. (Example Major Modes): Moved to end of Modes section. (Mode Line Basics): Clarify. (Mode Line Data): Mention help-echo and local-map in strings. Explain reason for treatment of non-risky variables. (Properties in Mode): Clarify. (Faces for Font Lock): Add font-lock-negation-char-face.
-rw-r--r--lispref/ChangeLog13
-rw-r--r--lispref/modes.texi703
2 files changed, 368 insertions, 348 deletions
diff --git a/lispref/ChangeLog b/lispref/ChangeLog
index eb1fd9797c2..8c963512d2f 100644
--- a/lispref/ChangeLog
+++ b/lispref/ChangeLog
@@ -1,3 +1,16 @@
12006-08-07 Chong Yidong <cyd@stupidchicken.com>
2
3 * modes.texi (Hooks): Clarify.
4 (Major Mode Basics): Mention define-derived-mode explicitly.
5 (Major Mode Conventions): Rebinding RET is OK for some modes.
6 Mention change-major-mode-hook and after-change-major-mode-hook.
7 (Example Major Modes): Moved to end of Modes section.
8 (Mode Line Basics): Clarify.
9 (Mode Line Data): Mention help-echo and local-map in strings.
10 Explain reason for treatment of non-risky variables.
11 (Properties in Mode): Clarify.
12 (Faces for Font Lock): Add font-lock-negation-char-face.
13
12006-08-04 Eli Zaretskii <eliz@gnu.org> 142006-08-04 Eli Zaretskii <eliz@gnu.org>
2 15
3 * strings.texi (Formatting Strings): Warn against arbitrary 16 * strings.texi (Formatting Strings): Warn against arbitrary
diff --git a/lispref/modes.texi b/lispref/modes.texi
index 8b24db35621..e0953c403b2 100644
--- a/lispref/modes.texi
+++ b/lispref/modes.texi
@@ -43,18 +43,19 @@ up in the init file (@pxref{Init File}), but Lisp programs can set them also.
43 43
44@cindex normal hook 44@cindex normal hook
45 Most of the hooks in Emacs are @dfn{normal hooks}. These variables 45 Most of the hooks in Emacs are @dfn{normal hooks}. These variables
46contain lists of functions to be called with no arguments. When the 46contain lists of functions to be called with no arguments. By
47hook name ends in @samp{-hook}, that tells you it is normal. We try to 47convention, whenever the hook name ends in @samp{-hook}, that tells
48make all hooks normal, as much as possible, so that you can use them in 48you it is normal. We try to make all hooks normal, as much as
49a uniform way. 49possible, so that you can use them in a uniform way.
50 50
51 Every major mode function is supposed to run a normal hook called the 51 Every major mode function is supposed to run a normal hook called
52@dfn{mode hook} as the last step of initialization. This makes it easy 52the @dfn{mode hook} as the one of the last steps of initialization.
53for a user to customize the behavior of the mode, by overriding the 53This makes it easy for a user to customize the behavior of the mode,
54buffer-local variable assignments already made by the mode. Most 54by overriding the buffer-local variable assignments already made by
55minor modes also run a mode hook at their end. But hooks are used in 55the mode. Most minor modes also run a mode hook at their end. But
56other contexts too. For example, the hook @code{suspend-hook} runs 56hooks are used in other contexts too. For example, the hook
57just before Emacs suspends itself (@pxref{Suspending Emacs}). 57@code{suspend-hook} runs just before Emacs suspends itself
58(@pxref{Suspending Emacs}).
58 59
59 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
60calling @code{add-hook} (see below). The hook functions may be any of 61calling @code{add-hook} (see below). The hook functions may be any of
@@ -68,14 +69,11 @@ globally or buffer-locally with @code{add-hook}.
68indicates it is probably an @dfn{abnormal hook}. Then you should look at its 69indicates it is probably an @dfn{abnormal hook}. Then you should look at its
69documentation to see how to use the hook properly. 70documentation to see how to use the hook properly.
70 71
71 If the variable's name ends in @samp{-functions} or @samp{-hooks}, 72 @dfn{Abnormal hooks} are hooks in which the functions are called
72then the value is a list of functions, but it is abnormal in that either 73with arguments, or the return values are used in some way. By
73these functions are called with arguments or their values are used in 74convention, abnormal hook names end in @samp{-functions} or
74some way. You can use @code{add-hook} to add a function to the list, 75@samp{-hooks}. You can use @code{add-hook} to add a function to the
75but you must take care in writing the function. (A few of these 76list, but you must take care in writing the function.
76variables, notably those ending in @samp{-hooks}, are actually
77normal hooks which were named before we established the convention of
78using @samp{-hook} for them.)
79 77
80 If the variable's name ends in @samp{-function}, then its value 78 If the variable's name ends in @samp{-function}, then its value
81is just a single function, not a list of functions. 79is just a single function, not a list of functions.
@@ -96,12 +94,13 @@ arguments, and runs each hook in turn. Each argument should be a
96symbol that is a normal hook variable. These arguments are processed 94symbol that is a normal hook variable. These arguments are processed
97in the order specified. 95in the order specified.
98 96
99If a hook variable has a non-@code{nil} value, that value may be a 97If a hook variable has a non-@code{nil} value, that value should be a
100function or a list of functions. (The former option is considered 98list of functions. Each function in this list is called,
101obsolete.) If the value is a function (either a lambda expression or 99consecutively, with no arguments.
102a symbol with a function definition), it is called. If it is a list 100
103that isn't a function, its elements are called, consecutively. All 101A hook variable can also be a single function (either a lambda
104the hook functions are called with no arguments. 102expression or a symbol with a function definition) to be called. This
103use is considered obsolete.
105@end defun 104@end defun
106 105
107@defun run-hook-with-args hook &rest args 106@defun run-hook-with-args hook &rest args
@@ -187,7 +186,6 @@ to another major mode in the same buffer.
187@menu 186@menu
188* Major Mode Basics:: 187* Major Mode Basics::
189* Major Mode Conventions:: Coding conventions for keymaps, etc. 188* Major Mode Conventions:: Coding conventions for keymaps, etc.
190* Example Major Modes:: Text mode and Lisp modes.
191* Auto Major Mode:: How Emacs chooses the major mode automatically. 189* Auto Major Mode:: How Emacs chooses the major mode automatically.
192* Mode Help:: Finding out how to use a mode. 190* Mode Help:: Finding out how to use a mode.
193* Derived Modes:: Defining a new major mode based on another major 191* Derived Modes:: Defining a new major mode based on another major
@@ -195,6 +193,7 @@ to another major mode in the same buffer.
195* Generic Modes:: Defining a simple major mode that supports 193* Generic Modes:: Defining a simple major mode that supports
196 comment syntax and Font Lock mode. 194 comment syntax and Font Lock mode.
197* Mode Hooks:: Hooks run at the end of major mode functions. 195* Mode Hooks:: Hooks run at the end of major mode functions.
196* Example Major Modes:: Text mode and Lisp modes.
198@end menu 197@end menu
199 198
200@node Major Mode Basics 199@node Major Mode Basics
@@ -214,14 +213,14 @@ specialized editing task, creating a new major mode is usually a good
214idea. In practice, writing a major mode is easy (in contrast to 213idea. In practice, writing a major mode is easy (in contrast to
215writing a minor mode, which is often difficult). 214writing a minor mode, which is often difficult).
216 215
217 If the new mode is similar to an old one, it is often unwise to modify 216 If the new mode is similar to an old one, it is often unwise to
218the old one to serve two purposes, since it may become harder to use and 217modify the old one to serve two purposes, since it may become harder
219maintain. Instead, copy and rename an existing major mode definition 218to use and maintain. Instead, copy and rename an existing major mode
220and alter the copy---or define a @dfn{derived mode} (@pxref{Derived 219definition and alter the copy---or use @code{define-derived-mode} to
221Modes}). For example, Rmail Edit mode, which is in 220define a @dfn{derived mode} (@pxref{Derived Modes}). For example,
222@file{emacs/lisp/mail/rmailedit.el}, is a major mode that is very similar to 221Rmail Edit mode is a major mode that is very similar to Text mode
223Text mode except that it provides two additional commands. Its 222except that it provides two additional commands. Its definition is
224definition is distinct from that of Text mode, but uses that of Text mode. 223distinct from that of Text mode, but uses that of Text mode.
225 224
226 Even if the new mode is not an obvious derivative of any other mode, 225 Even if the new mode is not an obvious derivative of any other mode,
227it is convenient to use @code{define-derived-mode} with a @code{nil} 226it is convenient to use @code{define-derived-mode} with a @code{nil}
@@ -287,8 +286,10 @@ Documentation}.
287 286
288@item 287@item
289The major mode command should start by calling 288The major mode command should start by calling
290@code{kill-all-local-variables}. This is what gets rid of the 289@code{kill-all-local-variables}. This runs the normal hook
291buffer-local variables of the major mode previously in effect. 290@code{change-major-mode-hook}, then gets rid of the buffer-local
291variables of the major mode previously in effect. @xref{Creating
292Buffer-Local}.
292 293
293@item 294@item
294The major mode command should set the variable @code{major-mode} to the 295The major mode command should set the variable @code{major-mode} to the
@@ -355,9 +356,10 @@ Rmail that do not allow self-insertion of text can reasonably redefine
355letters and other printing characters as special commands. 356letters and other printing characters as special commands.
356 357
357@item 358@item
358Major modes must not define @key{RET} to do anything other than insert 359Major modes modes for editing text should not define @key{RET} to do
359a newline. The command to insert a newline and then indent is 360anything other than insert a newline. The command to insert a newline
360@kbd{C-j}. Please keep this distinction uniform for all major modes. 361and then indent is @kbd{C-j}. It is ok for more specialized modes,
362such as Info mode, to redefine @key{RET} to something else.
361 363
362@item 364@item
363Major modes should not alter options that are primarily a matter of user 365Major modes should not alter options that are primarily a matter of user
@@ -427,10 +429,11 @@ other packages would interfere with them.
427@item 429@item
428@cindex mode hook 430@cindex mode hook
429@cindex major mode hook 431@cindex major mode hook
430Each major mode should have a @dfn{mode hook} named 432Each major mode should have a normal @dfn{mode hook} named
431@code{@var{modename}-mode-hook}. The major mode command should run that 433@code{@var{modename}-mode-hook}. The very last thing the major mode command
432hook, with @code{run-mode-hooks}, as the very last thing it 434should do is to call @code{run-mode-hooks}. This runs the mode hook,
433does. @xref{Mode Hooks}. 435and then runs the normal hook @code{after-change-major-mode-hook}.
436@xref{Mode Hooks}.
434 437
435@item 438@item
436The major mode command may start by calling some other major mode 439The major mode command may start by calling some other major mode
@@ -488,281 +491,6 @@ that they may be evaluated more than once without adverse consequences.
488Even if you never load the file more than once, someone else will. 491Even if you never load the file more than once, someone else will.
489@end itemize 492@end itemize
490 493
491@node Example Major Modes
492@subsection Major Mode Examples
493
494 Text mode is perhaps the simplest mode besides Fundamental mode.
495Here are excerpts from @file{text-mode.el} that illustrate many of
496the conventions listed above:
497
498@smallexample
499@group
500;; @r{Create the syntax table for this mode.}
501(defvar text-mode-syntax-table
502 (let ((st (make-syntax-table)))
503 (modify-syntax-entry ?\" ". " st)
504 (modify-syntax-entry ?\\ ". " st)
505 ;; Add `p' so M-c on `hello' leads to `Hello', not `hello'.
506 (modify-syntax-entry ?' "w p" st)
507 st)
508 "Syntax table used while in `text-mode'.")
509@end group
510
511;; @r{Create the keymap for this mode.}
512@group
513(defvar text-mode-map
514 (let ((map (make-sparse-keymap)))
515 (define-key map "\e\t" 'ispell-complete-word)
516 (define-key map "\es" 'center-line)
517 (define-key map "\eS" 'center-paragraph)
518 map)
519 "Keymap for `text-mode'.
520Many other modes, such as Mail mode, Outline mode
521and Indented Text mode, inherit all the commands
522defined in this map.")
523@end group
524@end smallexample
525
526 Here is how the actual mode command is defined now:
527
528@smallexample
529@group
530(define-derived-mode text-mode nil "Text"
531 "Major mode for editing text written for humans to read.
532In this mode, paragraphs are delimited only by blank or white lines.
533You can thus get the full benefit of adaptive filling
534 (see the variable `adaptive-fill-mode').
535\\@{text-mode-map@}
536Turning on Text mode runs the normal hook `text-mode-hook'."
537@end group
538@group
539 (make-local-variable 'text-mode-variant)
540 (setq text-mode-variant t)
541 ;; @r{These two lines are a feature added recently.}
542 (set (make-local-variable 'require-final-newline)
543 mode-require-final-newline)
544 (set (make-local-variable 'indent-line-function) 'indent-relative))
545@end group
546@end smallexample
547
548 But here is how it was defined formerly, before
549@code{define-derived-mode} existed:
550
551@smallexample
552@group
553;; @r{This isn't needed nowadays, since @code{define-derived-mode} does it.}
554(defvar text-mode-abbrev-table nil
555 "Abbrev table used while in text mode.")
556(define-abbrev-table 'text-mode-abbrev-table ())
557@end group
558
559@group
560(defun text-mode ()
561 "Major mode for editing text intended for humans to read...
562 Special commands: \\@{text-mode-map@}
563@end group
564@group
565Turning on text-mode runs the hook `text-mode-hook'."
566 (interactive)
567 (kill-all-local-variables)
568 (use-local-map text-mode-map)
569@end group
570@group
571 (setq local-abbrev-table text-mode-abbrev-table)
572 (set-syntax-table text-mode-syntax-table)
573@end group
574@group
575 ;; @r{These four lines are absent from the current version}
576 ;; @r{not because this is done some other way, but rather}
577 ;; @r{because nowadays Text mode uses the normal definition of paragraphs.}
578 (make-local-variable 'paragraph-start)
579 (setq paragraph-start (concat "[ \t]*$\\|" page-delimiter))
580 (make-local-variable 'paragraph-separate)
581 (setq paragraph-separate paragraph-start)
582 (make-local-variable 'indent-line-function)
583 (setq indent-line-function 'indent-relative-maybe)
584@end group
585@group
586 (setq mode-name "Text")
587 (setq major-mode 'text-mode)
588 (run-mode-hooks 'text-mode-hook)) ; @r{Finally, this permits the user to}
589 ; @r{customize the mode with a hook.}
590@end group
591@end smallexample
592
593@cindex @file{lisp-mode.el}
594 The three Lisp modes (Lisp mode, Emacs Lisp mode, and Lisp
595Interaction mode) have more features than Text mode and the code is
596correspondingly more complicated. Here are excerpts from
597@file{lisp-mode.el} that illustrate how these modes are written.
598
599@cindex syntax table example
600@smallexample
601@group
602;; @r{Create mode-specific table variables.}
603(defvar lisp-mode-syntax-table nil "")
604(defvar lisp-mode-abbrev-table nil "")
605@end group
606
607@group
608(defvar emacs-lisp-mode-syntax-table
609 (let ((table (make-syntax-table)))
610 (let ((i 0))
611@end group
612
613@group
614 ;; @r{Set syntax of chars up to @samp{0} to say they are}
615 ;; @r{part of symbol names but not words.}
616 ;; @r{(The digit @samp{0} is @code{48} in the @acronym{ASCII} character set.)}
617 (while (< i ?0)
618 (modify-syntax-entry i "_ " table)
619 (setq i (1+ i)))
620 ;; @r{@dots{} similar code follows for other character ranges.}
621@end group
622@group
623 ;; @r{Then set the syntax codes for characters that are special in Lisp.}
624 (modify-syntax-entry ? " " table)
625 (modify-syntax-entry ?\t " " table)
626 (modify-syntax-entry ?\f " " table)
627 (modify-syntax-entry ?\n "> " table)
628@end group
629@group
630 ;; @r{Give CR the same syntax as newline, for selective-display.}
631 (modify-syntax-entry ?\^m "> " table)
632 (modify-syntax-entry ?\; "< " table)
633 (modify-syntax-entry ?` "' " table)
634 (modify-syntax-entry ?' "' " table)
635 (modify-syntax-entry ?, "' " table)
636@end group
637@group
638 ;; @r{@dots{}likewise for many other characters@dots{}}
639 (modify-syntax-entry ?\( "() " table)
640 (modify-syntax-entry ?\) ")( " table)
641 (modify-syntax-entry ?\[ "(] " table)
642 (modify-syntax-entry ?\] ")[ " table))
643 table))
644@end group
645@group
646;; @r{Create an abbrev table for lisp-mode.}
647(define-abbrev-table 'lisp-mode-abbrev-table ())
648@end group
649@end smallexample
650
651 Much code is shared among the three Lisp modes. The following
652function sets various variables; it is called by each of the major Lisp
653mode functions:
654
655@smallexample
656@group
657(defun lisp-mode-variables (lisp-syntax)
658 (when lisp-syntax
659 (set-syntax-table lisp-mode-syntax-table))
660 (setq local-abbrev-table lisp-mode-abbrev-table)
661 @dots{}
662@end group
663@end smallexample
664
665 Functions such as @code{forward-paragraph} use the value of the
666@code{paragraph-start} variable. Since Lisp code is different from
667ordinary text, the @code{paragraph-start} variable needs to be set
668specially to handle Lisp. Also, comments are indented in a special
669fashion in Lisp and the Lisp modes need their own mode-specific
670@code{comment-indent-function}. The code to set these variables is the
671rest of @code{lisp-mode-variables}.
672
673@smallexample
674@group
675 (make-local-variable 'paragraph-start)
676 (setq paragraph-start (concat page-delimiter "\\|$" ))
677 (make-local-variable 'paragraph-separate)
678 (setq paragraph-separate paragraph-start)
679 @dots{}
680@end group
681@group
682 (make-local-variable 'comment-indent-function)
683 (setq comment-indent-function 'lisp-comment-indent))
684 @dots{}
685@end group
686@end smallexample
687
688 Each of the different Lisp modes has a slightly different keymap. For
689example, Lisp mode binds @kbd{C-c C-z} to @code{run-lisp}, but the other
690Lisp modes do not. However, all Lisp modes have some commands in
691common. The following code sets up the common commands:
692
693@smallexample
694@group
695(defvar shared-lisp-mode-map ()
696 "Keymap for commands shared by all sorts of Lisp modes.")
697
698;; @r{Putting this @code{if} after the @code{defvar} is an older style.}
699(if shared-lisp-mode-map
700 ()
701 (setq shared-lisp-mode-map (make-sparse-keymap))
702 (define-key shared-lisp-mode-map "\e\C-q" 'indent-sexp)
703 (define-key shared-lisp-mode-map "\177"
704 'backward-delete-char-untabify))
705@end group
706@end smallexample
707
708@noindent
709And here is the code to set up the keymap for Lisp mode:
710
711@smallexample
712@group
713(defvar lisp-mode-map ()
714 "Keymap for ordinary Lisp mode...")
715
716(if lisp-mode-map
717 ()
718 (setq lisp-mode-map (make-sparse-keymap))
719 (set-keymap-parent lisp-mode-map shared-lisp-mode-map)
720 (define-key lisp-mode-map "\e\C-x" 'lisp-eval-defun)
721 (define-key lisp-mode-map "\C-c\C-z" 'run-lisp))
722@end group
723@end smallexample
724
725 Finally, here is the complete major mode function definition for
726Lisp mode.
727
728@smallexample
729@group
730(defun lisp-mode ()
731 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
732Commands:
733Delete converts tabs to spaces as it moves back.
734Blank lines separate paragraphs. Semicolons start comments.
735\\@{lisp-mode-map@}
736Note that `run-lisp' may be used either to start an inferior Lisp job
737or to switch back to an existing one.
738@end group
739
740@group
741Entry to this mode calls the value of `lisp-mode-hook'
742if that value is non-nil."
743 (interactive)
744 (kill-all-local-variables)
745@end group
746@group
747 (use-local-map lisp-mode-map) ; @r{Select the mode's keymap.}
748 (setq major-mode 'lisp-mode) ; @r{This is how @code{describe-mode}}
749 ; @r{finds out what to describe.}
750 (setq mode-name "Lisp") ; @r{This goes into the mode line.}
751 (lisp-mode-variables t) ; @r{This defines various variables.}
752 (make-local-variable 'comment-start-skip)
753 (setq comment-start-skip
754 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
755 (make-local-variable 'font-lock-keywords-case-fold-search)
756 (setq font-lock-keywords-case-fold-search t)
757@end group
758@group
759 (setq imenu-case-fold-search t)
760 (set-syntax-table lisp-mode-syntax-table)
761 (run-mode-hooks 'lisp-mode-hook)) ; @r{This permits the user to use a}
762 ; @r{hook to customize the mode.}
763@end group
764@end smallexample
765
766@node Auto Major Mode 494@node Auto Major Mode
767@subsection How Emacs Chooses a Major Mode 495@subsection How Emacs Chooses a Major Mode
768 496
@@ -1074,10 +802,9 @@ Do not write an @code{interactive} spec in the definition;
1074@cindex generic mode 802@cindex generic mode
1075 803
1076@dfn{Generic modes} are simple major modes with basic support for 804@dfn{Generic modes} are simple major modes with basic support for
1077comment syntax and Font Lock mode. They are primarily useful for 805comment syntax and Font Lock mode. To define a generic mode, use the
1078configuration files. To define a generic mode, use the macro 806macro @code{define-generic-mode}. See the file @file{generic-x.el}
1079@code{define-generic-mode}. See the file @file{generic-x.el} for some 807for some examples of the use of @code{define-generic-mode}.
1080examples of the use of @code{define-generic-mode}.
1081 808
1082@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
1083This macro creates a new generic mode. The argument @var{mode} (an 810This macro creates a new generic mode. The argument @var{mode} (an
@@ -1171,6 +898,281 @@ as the very last thing it does, and the last thing
1171@code{run-mode-hooks} does is run @code{after-change-major-mode-hook}. 898@code{run-mode-hooks} does is run @code{after-change-major-mode-hook}.
1172@end defvar 899@end defvar
1173 900
901@node Example Major Modes
902@subsection Major Mode Examples
903
904 Text mode is perhaps the simplest mode besides Fundamental mode.
905Here are excerpts from @file{text-mode.el} that illustrate many of
906the conventions listed above:
907
908@smallexample
909@group
910;; @r{Create the syntax table for this mode.}
911(defvar text-mode-syntax-table
912 (let ((st (make-syntax-table)))
913 (modify-syntax-entry ?\" ". " st)
914 (modify-syntax-entry ?\\ ". " st)
915 ;; Add `p' so M-c on `hello' leads to `Hello', not `hello'.
916 (modify-syntax-entry ?' "w p" st)
917 st)
918 "Syntax table used while in `text-mode'.")
919@end group
920
921;; @r{Create the keymap for this mode.}
922@group
923(defvar text-mode-map
924 (let ((map (make-sparse-keymap)))
925 (define-key map "\e\t" 'ispell-complete-word)
926 (define-key map "\es" 'center-line)
927 (define-key map "\eS" 'center-paragraph)
928 map)
929 "Keymap for `text-mode'.
930Many other modes, such as Mail mode, Outline mode
931and Indented Text mode, inherit all the commands
932defined in this map.")
933@end group
934@end smallexample
935
936 Here is how the actual mode command is defined now:
937
938@smallexample
939@group
940(define-derived-mode text-mode nil "Text"
941 "Major mode for editing text written for humans to read.
942In this mode, paragraphs are delimited only by blank or white lines.
943You can thus get the full benefit of adaptive filling
944 (see the variable `adaptive-fill-mode').
945\\@{text-mode-map@}
946Turning on Text mode runs the normal hook `text-mode-hook'."
947@end group
948@group
949 (make-local-variable 'text-mode-variant)
950 (setq text-mode-variant t)
951 ;; @r{These two lines are a feature added recently.}
952 (set (make-local-variable 'require-final-newline)
953 mode-require-final-newline)
954 (set (make-local-variable 'indent-line-function) 'indent-relative))
955@end group
956@end smallexample
957
958 But here is how it was defined formerly, before
959@code{define-derived-mode} existed:
960
961@smallexample
962@group
963;; @r{This isn't needed nowadays, since @code{define-derived-mode} does it.}
964(defvar text-mode-abbrev-table nil
965 "Abbrev table used while in text mode.")
966(define-abbrev-table 'text-mode-abbrev-table ())
967@end group
968
969@group
970(defun text-mode ()
971 "Major mode for editing text intended for humans to read...
972 Special commands: \\@{text-mode-map@}
973@end group
974@group
975Turning on text-mode runs the hook `text-mode-hook'."
976 (interactive)
977 (kill-all-local-variables)
978 (use-local-map text-mode-map)
979@end group
980@group
981 (setq local-abbrev-table text-mode-abbrev-table)
982 (set-syntax-table text-mode-syntax-table)
983@end group
984@group
985 ;; @r{These four lines are absent from the current version}
986 ;; @r{not because this is done some other way, but rather}
987 ;; @r{because nowadays Text mode uses the normal definition of paragraphs.}
988 (make-local-variable 'paragraph-start)
989 (setq paragraph-start (concat "[ \t]*$\\|" page-delimiter))
990 (make-local-variable 'paragraph-separate)
991 (setq paragraph-separate paragraph-start)
992 (make-local-variable 'indent-line-function)
993 (setq indent-line-function 'indent-relative-maybe)
994@end group
995@group
996 (setq mode-name "Text")
997 (setq major-mode 'text-mode)
998 (run-mode-hooks 'text-mode-hook)) ; @r{Finally, this permits the user to}
999 ; @r{customize the mode with a hook.}
1000@end group
1001@end smallexample
1002
1003@cindex @file{lisp-mode.el}
1004 The three Lisp modes (Lisp mode, Emacs Lisp mode, and Lisp
1005Interaction mode) have more features than Text mode and the code is
1006correspondingly more complicated. Here are excerpts from
1007@file{lisp-mode.el} that illustrate how these modes are written.
1008
1009@cindex syntax table example
1010@smallexample
1011@group
1012;; @r{Create mode-specific table variables.}
1013(defvar lisp-mode-syntax-table nil "")
1014(defvar lisp-mode-abbrev-table nil "")
1015@end group
1016
1017@group
1018(defvar emacs-lisp-mode-syntax-table
1019 (let ((table (make-syntax-table)))
1020 (let ((i 0))
1021@end group
1022
1023@group
1024 ;; @r{Set syntax of chars up to @samp{0} to say they are}
1025 ;; @r{part of symbol names but not words.}
1026 ;; @r{(The digit @samp{0} is @code{48} in the @acronym{ASCII} character set.)}
1027 (while (< i ?0)
1028 (modify-syntax-entry i "_ " table)
1029 (setq i (1+ i)))
1030 ;; @r{@dots{} similar code follows for other character ranges.}
1031@end group
1032@group
1033 ;; @r{Then set the syntax codes for characters that are special in Lisp.}
1034 (modify-syntax-entry ? " " table)
1035 (modify-syntax-entry ?\t " " table)
1036 (modify-syntax-entry ?\f " " table)
1037 (modify-syntax-entry ?\n "> " table)
1038@end group
1039@group
1040 ;; @r{Give CR the same syntax as newline, for selective-display.}
1041 (modify-syntax-entry ?\^m "> " table)
1042 (modify-syntax-entry ?\; "< " table)
1043 (modify-syntax-entry ?` "' " table)
1044 (modify-syntax-entry ?' "' " table)
1045 (modify-syntax-entry ?, "' " table)
1046@end group
1047@group
1048 ;; @r{@dots{}likewise for many other characters@dots{}}
1049 (modify-syntax-entry ?\( "() " table)
1050 (modify-syntax-entry ?\) ")( " table)
1051 (modify-syntax-entry ?\[ "(] " table)
1052 (modify-syntax-entry ?\] ")[ " table))
1053 table))
1054@end group
1055@group
1056;; @r{Create an abbrev table for lisp-mode.}
1057(define-abbrev-table 'lisp-mode-abbrev-table ())
1058@end group
1059@end smallexample
1060
1061 Much code is shared among the three Lisp modes. The following
1062function sets various variables; it is called by each of the major Lisp
1063mode functions:
1064
1065@smallexample
1066@group
1067(defun lisp-mode-variables (lisp-syntax)
1068 (when lisp-syntax
1069 (set-syntax-table lisp-mode-syntax-table))
1070 (setq local-abbrev-table lisp-mode-abbrev-table)
1071 @dots{}
1072@end group
1073@end smallexample
1074
1075 Functions such as @code{forward-paragraph} use the value of the
1076@code{paragraph-start} variable. Since Lisp code is different from
1077ordinary text, the @code{paragraph-start} variable needs to be set
1078specially to handle Lisp. Also, comments are indented in a special
1079fashion in Lisp and the Lisp modes need their own mode-specific
1080@code{comment-indent-function}. The code to set these variables is the
1081rest of @code{lisp-mode-variables}.
1082
1083@smallexample
1084@group
1085 (make-local-variable 'paragraph-start)
1086 (setq paragraph-start (concat page-delimiter "\\|$" ))
1087 (make-local-variable 'paragraph-separate)
1088 (setq paragraph-separate paragraph-start)
1089 @dots{}
1090@end group
1091@group
1092 (make-local-variable 'comment-indent-function)
1093 (setq comment-indent-function 'lisp-comment-indent))
1094 @dots{}
1095@end group
1096@end smallexample
1097
1098 Each of the different Lisp modes has a slightly different keymap. For
1099example, Lisp mode binds @kbd{C-c C-z} to @code{run-lisp}, but the other
1100Lisp modes do not. However, all Lisp modes have some commands in
1101common. The following code sets up the common commands:
1102
1103@smallexample
1104@group
1105(defvar shared-lisp-mode-map ()
1106 "Keymap for commands shared by all sorts of Lisp modes.")
1107
1108;; @r{Putting this @code{if} after the @code{defvar} is an older style.}
1109(if shared-lisp-mode-map
1110 ()
1111 (setq shared-lisp-mode-map (make-sparse-keymap))
1112 (define-key shared-lisp-mode-map "\e\C-q" 'indent-sexp)
1113 (define-key shared-lisp-mode-map "\177"
1114 'backward-delete-char-untabify))
1115@end group
1116@end smallexample
1117
1118@noindent
1119And here is the code to set up the keymap for Lisp mode:
1120
1121@smallexample
1122@group
1123(defvar lisp-mode-map ()
1124 "Keymap for ordinary Lisp mode...")
1125
1126(if lisp-mode-map
1127 ()
1128 (setq lisp-mode-map (make-sparse-keymap))
1129 (set-keymap-parent lisp-mode-map shared-lisp-mode-map)
1130 (define-key lisp-mode-map "\e\C-x" 'lisp-eval-defun)
1131 (define-key lisp-mode-map "\C-c\C-z" 'run-lisp))
1132@end group
1133@end smallexample
1134
1135 Finally, here is the complete major mode function definition for
1136Lisp mode.
1137
1138@smallexample
1139@group
1140(defun lisp-mode ()
1141 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
1142Commands:
1143Delete converts tabs to spaces as it moves back.
1144Blank lines separate paragraphs. Semicolons start comments.
1145\\@{lisp-mode-map@}
1146Note that `run-lisp' may be used either to start an inferior Lisp job
1147or to switch back to an existing one.
1148@end group
1149
1150@group
1151Entry to this mode calls the value of `lisp-mode-hook'
1152if that value is non-nil."
1153 (interactive)
1154 (kill-all-local-variables)
1155@end group
1156@group
1157 (use-local-map lisp-mode-map) ; @r{Select the mode's keymap.}
1158 (setq major-mode 'lisp-mode) ; @r{This is how @code{describe-mode}}
1159 ; @r{finds out what to describe.}
1160 (setq mode-name "Lisp") ; @r{This goes into the mode line.}
1161 (lisp-mode-variables t) ; @r{This defines various variables.}
1162 (make-local-variable 'comment-start-skip)
1163 (setq comment-start-skip
1164 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
1165 (make-local-variable 'font-lock-keywords-case-fold-search)
1166 (setq font-lock-keywords-case-fold-search t)
1167@end group
1168@group
1169 (setq imenu-case-fold-search t)
1170 (set-syntax-table lisp-mode-syntax-table)
1171 (run-mode-hooks 'lisp-mode-hook)) ; @r{This permits the user to use a}
1172 ; @r{hook to customize the mode.}
1173@end group
1174@end smallexample
1175
1174@node Minor Modes 1176@node Minor Modes
1175@section Minor Modes 1177@section Minor Modes
1176@cindex minor mode 1178@cindex minor mode
@@ -1533,16 +1535,14 @@ minor modes.
1533@subsection Mode Line Basics 1535@subsection Mode Line Basics
1534 1536
1535 @code{mode-line-format} is a buffer-local variable that holds a 1537 @code{mode-line-format} is a buffer-local variable that holds a
1536@dfn{mode line construct}, a kind of template, which controls the 1538@dfn{mode line construct}, a kind of template, which controls what is
1537display the mode line of the current buffer. All windows for the same 1539displayed on the mode line of the current buffer. The value of
1538buffer use the same @code{mode-line-format}, so their mode lines 1540@code{header-line-format} specifies the buffer's header line in the
1539appear the same---except for scrolling percentages, and line and 1541same way. All windows for the same buffer use the same
1540column numbers, since those depend on point and on how the window is 1542@code{mode-line-format} and @code{header-line-format}.
1541scrolled. The value of @code{header-line-format} specifies the 1543
1542buffer's header line in the same way, with a mode line construct. 1544 For efficiency, Emacs does not continuously recompute the mode
1543 1545line and header line of a window. It does so when circumstances
1544 For efficiency, Emacs does not recompute the mode line and header
1545line of a window in every redisplay. It does so when circumstances
1546appear to call for it---for instance, if you change the window 1546appear to call for it---for instance, if you change the window
1547configuration, switch buffers, narrow or widen the buffer, scroll, or 1547configuration, switch buffers, narrow or widen the buffer, scroll, or
1548change the buffer's modification status. If you modify any of the 1548change the buffer's modification status. If you modify any of the
@@ -1552,7 +1552,6 @@ how text is displayed (@pxref{Display}), you may want to force an
1552update of the mode line so as to display the new information or 1552update of the mode line so as to display the new information or
1553display it in the new way. 1553display it in the new way.
1554 1554
1555@c Emacs 19 feature
1556@defun force-mode-line-update &optional all 1555@defun force-mode-line-update &optional all
1557Force redisplay of the current buffer's mode line and header line. 1556Force redisplay of the current buffer's mode line and header line.
1558The next redisplay will update the mode line and header line based on 1557The next redisplay will update the mode line and header line based on
@@ -1589,15 +1588,17 @@ defined to have mode-line constructs as their values.
1589@table @code 1588@table @code
1590@cindex percent symbol in mode line 1589@cindex percent symbol in mode line
1591@item @var{string} 1590@item @var{string}
1592A string as a mode-line construct appears verbatim in the mode line 1591A string as a mode-line construct appears verbatim except for
1593except for @dfn{@code{%}-constructs} in it. These stand for 1592@dfn{@code{%}-constructs} in it. These stand for substitution of
1594substitution of other data; see @ref{%-Constructs}. 1593other data; see @ref{%-Constructs}.
1595 1594
1596If the string has @code{face} properties, they are copied into the 1595If parts of the string have @code{face} properties, they control
1597mode line contents too (@pxref{Properties in Mode}). Any characters 1596display of the text just as they would text in the buffer. Any
1598in the mode line which have no @code{face} properties are displayed, 1597characters which have no @code{face} properties are displayed, by
1599by default, in the face @code{mode-line} or @code{mode-line-inactive} 1598default, in the face @code{mode-line} or @code{mode-line-inactive}
1600(@pxref{Standard Faces,,, emacs, The GNU Emacs Manual}). 1599(@pxref{Standard Faces,,, emacs, The GNU Emacs Manual}). The
1600@code{help-echo} and @code{local-map} properties in @var{string} have
1601special meanings. @xref{Properties in Mode}.
1601 1602
1602@item @var{symbol} 1603@item @var{symbol}
1603A symbol as a mode-line construct stands for its value. The value of 1604A symbol as a mode-line construct stands for its value. The value of
@@ -1612,7 +1613,9 @@ Unless @var{symbol} is marked as ``risky'' (i.e., it has a
1612non-@code{nil} @code{risky-local-variable} property), all text 1613non-@code{nil} @code{risky-local-variable} property), all text
1613properties specified in @var{symbol}'s value are ignored. This 1614properties specified in @var{symbol}'s value are ignored. This
1614includes the text properties of strings in @var{symbol}'s value, as 1615includes the text properties of strings in @var{symbol}'s value, as
1615well as all @code{:eval} and @code{:propertize} forms in it. 1616well as all @code{:eval} and @code{:propertize} forms in it. (The
1617reason for this is security: non-risky variables could be set
1618automatically from file variables without prompting the user.)
1616 1619
1617@item (@var{string} @var{rest}@dots{}) 1620@item (@var{string} @var{rest}@dots{})
1618@itemx (@var{list} @var{rest}@dots{}) 1621@itemx (@var{list} @var{rest}@dots{})
@@ -2055,10 +2058,10 @@ structure, and make @var{form} evaluate to a string that has a text
2055property. 2058property.
2056@end enumerate 2059@end enumerate
2057 2060
2058 You use the @code{local-map} property to specify a keymap. Like any 2061 You can use the @code{local-map} property to specify a keymap. This
2059keymap, it can bind character keys and function keys; but that has no 2062keymap only takes real effect for mouse clicks; binding character keys
2060effect, since it is impossible to move point into the mode line. This 2063and function keys to it has no effect, since it is impossible to move
2061keymap can only take real effect for mouse clicks. 2064point into the mode line.
2062 2065
2063 When the mode line refers to a variable which does not have a 2066 When the mode line refers to a variable which does not have a
2064non-@code{nil} @code{risky-local-variable} property, any text 2067non-@code{nil} @code{risky-local-variable} property, any text
@@ -2889,6 +2892,10 @@ Used (typically) for constant names.
2889@vindex font-lock-preprocessor-face 2892@vindex font-lock-preprocessor-face
2890Used (typically) for preprocessor commands. 2893Used (typically) for preprocessor commands.
2891 2894
2895@item font-lock-negation-char-face
2896@vindex font-lock-negation-char-face
2897Used (typically) for easily-overlooked negation characters.
2898
2892@item font-lock-warning-face 2899@item font-lock-warning-face
2893@vindex font-lock-warning-face 2900@vindex font-lock-warning-face
2894Used (typically) for constructs that are peculiar, or that greatly 2901Used (typically) for constructs that are peculiar, or that greatly