comparison man/lispref/modes.texi @ 444:576fb035e263 r21-2-37

Import from CVS: tag r21-2-37
author cvs
date Mon, 13 Aug 2007 11:36:19 +0200
parents abe6d1db359e
children 1ccc32a20af4
comparison
equal deleted inserted replaced
443:a8296e22da4e 444:576fb035e263
1 @c -*-texinfo-*- 1 @c -*-texinfo-*-
2 @c This is part of the XEmacs Lisp Reference Manual. 2 @c This is part of the XEmacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. 3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4 @c See the file lispref.texi for copying conditions. 4 @c See the file lispref.texi for copying conditions.
5 @setfilename ../../info/modes.info 5 @setfilename ../../info/modes.info
6 @node Modes, Documentation, Drag and Drop, Top 6 @node Modes, Documentation, Drag and Drop, Top
7 @chapter Major and Minor Modes 7 @chapter Major and Minor Modes
8 @cindex mode 8 @cindex mode
77 @menu 77 @menu
78 * Major Mode Conventions:: Coding conventions for keymaps, etc. 78 * Major Mode Conventions:: Coding conventions for keymaps, etc.
79 * Example Major Modes:: Text mode and Lisp modes. 79 * Example Major Modes:: Text mode and Lisp modes.
80 * Auto Major Mode:: How XEmacs chooses the major mode automatically. 80 * Auto Major Mode:: How XEmacs chooses the major mode automatically.
81 * Mode Help:: Finding out how to use a mode. 81 * Mode Help:: Finding out how to use a mode.
82 * Derived Modes:: Defining a new major mode based on another major 82 * Derived Modes:: Defining a new major mode based on another major
83 mode. 83 mode.
84 @end menu 84 @end menu
85 85
86 @node Major Mode Conventions 86 @node Major Mode Conventions
87 @subsection Major Mode Conventions 87 @subsection Major Mode Conventions
247 the conventions listed above: 247 the conventions listed above:
248 248
249 @smallexample 249 @smallexample
250 @group 250 @group
251 ;; @r{Create mode-specific tables.} 251 ;; @r{Create mode-specific tables.}
252 (defvar text-mode-syntax-table nil 252 (defvar text-mode-syntax-table nil
253 "Syntax table used while in text mode.") 253 "Syntax table used while in text mode.")
254 @end group 254 @end group
255 255
256 @group 256 @group
257 (if text-mode-syntax-table 257 (if text-mode-syntax-table
283 Here is the complete major mode function definition for Text mode: 283 Here is the complete major mode function definition for Text mode:
284 284
285 @smallexample 285 @smallexample
286 @group 286 @group
287 (defun text-mode () 287 (defun text-mode ()
288 "Major mode for editing text intended for humans to read. 288 "Major mode for editing text intended for humans to read.
289 Special commands: \\@{text-mode-map@} 289 Special commands: \\@{text-mode-map@}
290 @end group 290 @end group
291 @group 291 @group
292 Turning on text-mode runs the hook `text-mode-hook'." 292 Turning on text-mode runs the hook `text-mode-hook'."
293 (interactive) 293 (interactive)
313 313
314 @cindex syntax table example 314 @cindex syntax table example
315 @smallexample 315 @smallexample
316 @group 316 @group
317 ;; @r{Create mode-specific table variables.} 317 ;; @r{Create mode-specific table variables.}
318 (defvar lisp-mode-syntax-table nil "") 318 (defvar lisp-mode-syntax-table nil "")
319 (defvar emacs-lisp-mode-syntax-table nil "") 319 (defvar emacs-lisp-mode-syntax-table nil "")
320 (defvar lisp-mode-abbrev-table nil "") 320 (defvar lisp-mode-abbrev-table nil "")
321 @end group 321 @end group
322 322
323 @group 323 @group
329 329
330 @group 330 @group
331 ;; @r{Set syntax of chars up to 0 to class of chars that are} 331 ;; @r{Set syntax of chars up to 0 to class of chars that are}
332 ;; @r{part of symbol names but not words.} 332 ;; @r{part of symbol names but not words.}
333 ;; @r{(The number 0 is @code{48} in the @sc{ascii} character set.)} 333 ;; @r{(The number 0 is @code{48} in the @sc{ascii} character set.)}
334 (while (< i ?0) 334 (while (< i ?0)
335 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) 335 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table)
336 (setq i (1+ i))) 336 (setq i (1+ i)))
337 @dots{} 337 @dots{}
338 @end group 338 @end group
339 @group 339 @group
430 This code avoids changing the keymap or the variable if it is already 430 This code avoids changing the keymap or the variable if it is already
431 set up. This lets the user customize the keymap. 431 set up. This lets the user customize the keymap.
432 432
433 @smallexample 433 @smallexample
434 @group 434 @group
435 (defvar emacs-lisp-mode-map () "") 435 (defvar emacs-lisp-mode-map () "")
436 (if emacs-lisp-mode-map 436 (if emacs-lisp-mode-map
437 () 437 ()
438 (setq emacs-lisp-mode-map (make-sparse-keymap)) 438 (setq emacs-lisp-mode-map (make-sparse-keymap))
439 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun) 439 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun)
440 (lisp-mode-commands emacs-lisp-mode-map)) 440 (lisp-mode-commands emacs-lisp-mode-map))
441 @end group 441 @end group
442 @end smallexample 442 @end smallexample
443 443
444 Finally, here is the complete major mode function definition for 444 Finally, here is the complete major mode function definition for
445 Emacs Lisp mode. 445 Emacs Lisp mode.
446 446
447 @smallexample 447 @smallexample
448 @group 448 @group
449 (defun emacs-lisp-mode () 449 (defun emacs-lisp-mode ()
450 "Major mode for editing Lisp code to run in XEmacs. 450 "Major mode for editing Lisp code to run in XEmacs.
543 the @samp{mode:} local variable near the end of a file; the 543 the @samp{mode:} local variable near the end of a file; the
544 @code{hack-local-variables} function does that. @xref{Choosing Modes, , 544 @code{hack-local-variables} function does that. @xref{Choosing Modes, ,
545 How Major Modes are Chosen, emacs, The XEmacs Reference Manual}. 545 How Major Modes are Chosen, emacs, The XEmacs Reference Manual}.
546 @end defun 546 @end defun
547 547
548 @defopt default-major-mode 548 @defopt default-major-mode
549 This variable holds the default major mode for new buffers. The 549 This variable holds the default major mode for new buffers. The
550 standard value is @code{fundamental-mode}. 550 standard value is @code{fundamental-mode}.
551 551
552 If the value of @code{default-major-mode} is @code{nil}, XEmacs uses 552 If the value of @code{default-major-mode} is @code{nil}, XEmacs uses
553 the (previously) current buffer's major mode for the major mode of a new 553 the (previously) current buffer's major mode for the major mode of a new
591 ("\\.texinfo\\'" . texinfo-mode) 591 ("\\.texinfo\\'" . texinfo-mode)
592 ("\\.texi\\'" . texinfo-mode) 592 ("\\.texi\\'" . texinfo-mode)
593 @end group 593 @end group
594 @group 594 @group
595 ("\\.el\\'" . emacs-lisp-mode) 595 ("\\.el\\'" . emacs-lisp-mode)
596 ("\\.c\\'" . c-mode) 596 ("\\.c\\'" . c-mode)
597 ("\\.h\\'" . c-mode) 597 ("\\.h\\'" . c-mode)
598 @dots{}) 598 @dots{})
599 @end group 599 @end group
600 @end smallexample 600 @end smallexample
601 601
619 @file{.emacs} file.) 619 @file{.emacs} file.)
620 620
621 @smallexample 621 @smallexample
622 @group 622 @group
623 (setq auto-mode-alist 623 (setq auto-mode-alist
624 (append 624 (append
625 ;; @r{File name starts with a dot.} 625 ;; @r{File name starts with a dot.}
626 '(("/\\.[^/]*\\'" . fundamental-mode) 626 '(("/\\.[^/]*\\'" . fundamental-mode)
627 ;; @r{File name has no dot.} 627 ;; @r{File name has no dot.}
628 ("[^\\./]*\\'" . fundamental-mode) 628 ("[^\\./]*\\'" . fundamental-mode)
629 ;; @r{File name ends in @samp{.C}.} 629 ;; @r{File name ends in @samp{.C}.}
630 ("\\.C\\'" . c++-mode)) 630 ("\\.C\\'" . c++-mode))
631 auto-mode-alist)) 631 auto-mode-alist))
632 @end group 632 @end group
633 @end smallexample 633 @end smallexample
695 @var{name} as the string form of the mode name. 695 @var{name} as the string form of the mode name.
696 696
697 The new command @var{variant} is defined to call the function 697 The new command @var{variant} is defined to call the function
698 @var{parent}, then override certain aspects of that parent mode: 698 @var{parent}, then override certain aspects of that parent mode:
699 699
700 @itemize @bullet 700 @itemize @bullet
701 @item 701 @item
702 The new mode has its own keymap, named @code{@var{variant}-map}. 702 The new mode has its own keymap, named @code{@var{variant}-map}.
703 @code{define-derived-mode} initializes this map to inherit from 703 @code{define-derived-mode} initializes this map to inherit from
704 @code{@var{parent}-map}, if it is not already set. 704 @code{@var{parent}-map}, if it is not already set.
705 705
706 @item 706 @item
707 The new mode has its own syntax table, kept in the variable 707 The new mode has its own syntax table, kept in the variable
708 @code{@var{variant}-syntax-table}. 708 @code{@var{variant}-syntax-table}.
709 @code{define-derived-mode} initializes this variable by copying 709 @code{define-derived-mode} initializes this variable by copying
710 @code{@var{parent}-syntax-table}, if it is not already set. 710 @code{@var{parent}-syntax-table}, if it is not already set.
711 711
712 @item 712 @item
713 The new mode has its own abbrev table, kept in the variable 713 The new mode has its own abbrev table, kept in the variable
714 @code{@var{variant}-abbrev-table}. 714 @code{@var{variant}-abbrev-table}.
715 @code{define-derived-mode} initializes this variable by copying 715 @code{define-derived-mode} initializes this variable by copying
716 @code{@var{parent}-abbrev-table}, if it is not already set. 716 @code{@var{parent}-abbrev-table}, if it is not already set.
717 717
718 @item 718 @item
719 The new mode has its own mode hook, @code{@var{variant}-hook}, 719 The new mode has its own mode hook, @code{@var{variant}-hook},
720 which it runs in standard fashion as the very last thing that it does. 720 which it runs in standard fashion as the very last thing that it does.
721 (The new mode also runs the mode hook of @var{parent} as part 721 (The new mode also runs the mode hook of @var{parent} as part
722 of calling @var{parent}.) 722 of calling @var{parent}.)
723 @end itemize 723 @end itemize
724 724
725 In addition, you can specify how to override other aspects of 725 In addition, you can specify how to override other aspects of
726 @var{parent} with @var{body}. The command @var{variant} 726 @var{parent} with @var{body}. The command @var{variant}
727 evaluates the forms in @var{body} after setting up all its usual 727 evaluates the forms in @var{body} after setting up all its usual
728 overrides, just before running @code{@var{variant}-hook}. 728 overrides, just before running @code{@var{variant}-hook}.
729 729
730 The argument @var{docstring} specifies the documentation string for the 730 The argument @var{docstring} specifies the documentation string for the
731 new mode. If you omit @var{docstring}, @code{define-derived-mode} 731 new mode. If you omit @var{docstring}, @code{define-derived-mode}
732 generates a documentation string. 732 generates a documentation string.
1026 @example 1026 @example
1027 @group 1027 @group
1028 (setq modeline-format 1028 (setq modeline-format
1029 (list "" 1029 (list ""
1030 'modeline-modified 1030 'modeline-modified
1031 "%b--" 1031 "%b--"
1032 @end group 1032 @end group
1033 (getenv "HOST") ; @r{One element is not constant.} 1033 (getenv "HOST") ; @r{One element is not constant.}
1034 ":" 1034 ":"
1035 'default-directory 1035 'default-directory
1036 " " 1036 " "
1037 'global-mode-string 1037 'global-mode-string
1038 " %[(" 1038 " %[("
1039 'mode-name 1039 'mode-name
1040 'modeline-process 1040 'modeline-process
1041 'minor-mode-alist 1041 'minor-mode-alist
1042 "%n" 1042 "%n"
1043 ")%]----" 1043 ")%]----"
1044 @group 1044 @group
1045 '(line-number-mode "L%l--") 1045 '(line-number-mode "L%l--")
1046 '(-3 . "%p") 1046 '(-3 . "%p")
1047 "-%-")) 1047 "-%-"))
1118 1118
1119 @example 1119 @example
1120 @group 1120 @group
1121 minor-mode-alist 1121 minor-mode-alist
1122 @result{} ((vc-mode vc-mode) 1122 @result{} ((vc-mode vc-mode)
1123 (abbrev-mode " Abbrev") 1123 (abbrev-mode " Abbrev")
1124 (overwrite-mode overwrite-mode) 1124 (overwrite-mode overwrite-mode)
1125 (auto-fill-function " Fill") 1125 (auto-fill-function " Fill")
1126 (defining-kbd-macro " Def") 1126 (defining-kbd-macro " Def")
1127 (isearch-mode isearch-mode)) 1127 (isearch-mode isearch-mode))
1128 @end group 1128 @end group
1129 @end example 1129 @end example
1130 1130
1156 modeline-modified 1156 modeline-modified
1157 modeline-buffer-identification 1157 modeline-buffer-identification
1158 " " 1158 " "
1159 global-mode-string 1159 global-mode-string
1160 " %[(" 1160 " %[("
1161 mode-name 1161 mode-name
1162 @end group 1162 @end group
1163 @group 1163 @group
1164 modeline-process 1164 modeline-process
1165 minor-mode-alist 1165 minor-mode-alist
1166 "%n" 1166 "%n"
1167 ")%]----" 1167 ")%]----"
1168 (line-number-mode "L%l--") 1168 (line-number-mode "L%l--")
1169 (-3 . "%p") 1169 (-3 . "%p")
1170 "-%-") 1170 "-%-")
1171 @end group 1171 @end group
1292 @xref{Standard Hooks}, for a list of standard hook variables. 1292 @xref{Standard Hooks}, for a list of standard hook variables.
1293 1293
1294 Most of the hooks in XEmacs are @dfn{normal hooks}. These variables 1294 Most of the hooks in XEmacs are @dfn{normal hooks}. These variables
1295 contain lists of functions to be called with no arguments. The reason 1295 contain lists of functions to be called with no arguments. The reason
1296 most hooks are normal hooks is so that you can use them in a uniform 1296 most hooks are normal hooks is so that you can use them in a uniform
1297 way. You can usually tell when a hook is a normal hook, because its 1297 way. You can usually tell when a hook is a normal hook, because its
1298 name ends in @samp{-hook}. 1298 name ends in @samp{-hook}.
1299 1299
1300 The recommended way to add a hook function to a normal hook is by 1300 The recommended way to add a hook function to a normal hook is by
1301 calling @code{add-hook} (see below). The hook functions may be any of 1301 calling @code{add-hook} (see below). The hook functions may be any of
1302 the valid kinds of functions that @code{funcall} accepts (@pxref{What Is 1302 the valid kinds of functions that @code{funcall} accepts (@pxref{What Is
1331 expression. 1331 expression.
1332 1332
1333 @cindex lambda expression in hook 1333 @cindex lambda expression in hook
1334 @example 1334 @example
1335 @group 1335 @group
1336 (add-hook 'c-mode-hook 1336 (add-hook 'c-mode-hook
1337 (function (lambda () 1337 (function (lambda ()
1338 (setq c-indent-level 4 1338 (setq c-indent-level 4
1339 c-argdecl-indent 0 1339 c-argdecl-indent 0
1340 c-label-offset -4 1340 c-label-offset -4
1341 @end group 1341 @end group
1356 (add-hook 'text-mode-hook 1356 (add-hook 'text-mode-hook
1357 (function (lambda () 1357 (function (lambda ()
1358 (setq modeline-format 1358 (setq modeline-format
1359 '(modeline-modified 1359 '(modeline-modified
1360 "Emacs: %14b" 1360 "Emacs: %14b"
1361 " " 1361 " "
1362 @end group 1362 @end group
1363 @group 1363 @group
1364 default-directory 1364 default-directory
1365 " " 1365 " "
1366 global-mode-string 1366 global-mode-string
1367 "%[(" 1367 "%[("
1368 mode-name 1368 mode-name
1369 minor-mode-alist 1369 minor-mode-alist
1370 "%n" 1370 "%n"
1371 modeline-process 1371 modeline-process
1372 ") %]---" 1372 ") %]---"
1373 (-3 . "%p") 1373 (-3 . "%p")
1374 "-%-"))))) 1374 "-%-")))))
1375 @end group 1375 @end group
1376 @end example 1376 @end example
1438 hook itself is not buffer-local, then the value of @var{local} makes no 1438 hook itself is not buffer-local, then the value of @var{local} makes no
1439 difference. 1439 difference.
1440 @end defun 1440 @end defun
1441 1441
1442 @defun make-local-hook hook 1442 @defun make-local-hook hook
1443 This function makes the hook variable @code{hook} local to the current 1443 This function makes the hook variable @var{hook} local to the current
1444 buffer. When a hook variable is local, it can have local and global 1444 buffer. When a hook variable is local, it can have local and global
1445 hook functions, and @code{run-hooks} runs all of them. 1445 hook functions, and @code{run-hooks} runs all of them.
1446 1446
1447 This function works by making @code{t} an element of the buffer-local 1447 This function works by making @code{t} an element of the buffer-local
1448 value. That serves as a flag to use the hook functions in the default 1448 value. That serves as a flag to use the hook functions in the default