annotate lisp/derived.el @ 272:c5d627a313b1 r21-0b34

Import from CVS: tag r21-0b34
author cvs
date Mon, 13 Aug 2007 10:28:48 +0200
parents 41ff10fd062f
children 8626e4521993
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
1 ;;; derived.el --- allow inheritance of major modes.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
2
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
3 ;; Copyright (C) 1993, 1994, 1997 Free Software Foundation, Inc.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
4
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
5 ;; Author: David Megginson (dmeggins@aix1.uottawa.ca)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
6 ;; Maintainer: XEmacs Development Team
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
7 ;; Keywords: extensions, dumped
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
8
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
9 ;; This file is part of XEmacs.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
10
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
11 ;; XEmacs is free software; you can redistribute it and/or modify it
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
12 ;; under the terms of the GNU General Public License as published by
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
13 ;; the Free Software Foundation; either version 2, or (at your option)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
14 ;; any later version.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
15
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
16 ;; XEmacs is distributed in the hope that it will be useful, but
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
19 ;; General Public License for more details.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
20
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
21 ;; You should have received a copy of the GNU General Public License
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
22 ;; along with XEmacs; see the file COPYING. If not, write to the Free
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
23 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
24 ;; 02111-1307, USA.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
25
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
26 ;;; Synched up with: FSF 19.34.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
27
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
28 ;;; Commentary:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
29
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
30 ;; This file is dumped with XEmacs.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
31
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
32 ;; GNU Emacs is already, in a sense, object oriented -- each object
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
33 ;; (buffer) belongs to a class (major mode), and that class defines
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
34 ;; the relationship between messages (input events) and methods
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
35 ;; (commands) by means of a keymap.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
36 ;;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
37 ;; The only thing missing is a good scheme of inheritance. It is
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
38 ;; possible to simulate a single level of inheritance with generous
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
39 ;; use of hooks and a bit of work -- sgml-mode, for example, also runs
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
40 ;; the hooks for text-mode, and keymaps can inherit from other keymaps
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
41 ;; -- but generally, each major mode ends up reinventing the wheel.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
42 ;; Ideally, someone should redesign all of Emacs's major modes to
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
43 ;; follow a more conventional object-oriented system: when defining a
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
44 ;; new major mode, the user should need only to name the existing mode
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
45 ;; it is most similar to, then list the (few) differences.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
46 ;;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
47 ;; In the mean time, this package offers most of the advantages of
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
48 ;; full inheritance with the existing major modes. The macro
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
49 ;; `define-derived-mode' allows the user to make a variant of an existing
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
50 ;; major mode, with its own keymap. The new mode will inherit the key
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
51 ;; bindings of its parent, and will, in fact, run its parent first
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
52 ;; every time it is called. For example, the commands
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
53 ;;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
54 ;; (define-derived-mode hypertext-mode text-mode "Hypertext"
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
55 ;; "Major mode for hypertext.\n\n\\{hypertext-mode-map}"
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
56 ;; (setq case-fold-search nil))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
57 ;;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
58 ;; (define-key hypertext-mode-map [down-mouse-3] 'do-hyper-link)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
59 ;;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
60 ;; will create a function `hypertext-mode' with its own (sparse)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
61 ;; keymap `hypertext-mode-map.' The command M-x hypertext-mode will
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
62 ;; perform the following actions:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
63 ;;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
64 ;; - run the command (text-mode) to get its default setup
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
65 ;; - replace the current keymap with 'hypertext-mode-map,' which will
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
66 ;; inherit from 'text-mode-map'.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
67 ;; - replace the current syntax table with
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
68 ;; 'hypertext-mode-syntax-table', which will borrow its defaults
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
69 ;; from the current text-mode-syntax-table.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
70 ;; - replace the current abbrev table with
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
71 ;; 'hypertext-mode-abbrev-table', which will borrow its defaults
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
72 ;; from the current text-mode-abbrev table
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
73 ;; - change the mode line to read "Hypertext"
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
74 ;; - assign the value 'hypertext-mode' to the 'major-mode' variable
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
75 ;; - run the body of commands provided in the macro -- in this case,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
76 ;; set the local variable `case-fold-search' to nil.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
77 ;; - **run the command (hypertext-mode-setup), which is empty by
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
78 ;; default, but may be redefined by the user to contain special
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
79 ;; commands (ie. setting local variables like 'outline-regexp')
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
80 ;; **NOTE: do not use this option -- it will soon be obsolete.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
81 ;; - run anything assigned to 'hypertext-mode-hooks' (obsolete, but
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
82 ;; supported for the sake of compatibility).
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
83 ;;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
84 ;; The advantages of this system are threefold. First, text mode is
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
85 ;; untouched -- if you had added the new keystroke to `text-mode-map,'
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
86 ;; possibly using hooks, you would have added it to all text buffers
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
87 ;; -- here, it appears only in hypertext buffers, where it makes
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
88 ;; sense. Second, it is possible to build even further, and make
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
89 ;; a derived mode from a derived mode. The commands
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
90 ;;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
91 ;; (define-derived-mode html-mode hypertext-mode "HTML")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
92 ;; [various key definitions]
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
93 ;;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
94 ;; will add a new major mode for HTML with very little fuss.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
95 ;;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
96 ;; Note also the function `derived-mode-class,' which returns the non-derived
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
97 ;; major mode which a derived mode is based on (ie. NOT necessarily the
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
98 ;; immediate parent).
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
99 ;;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
100 ;; (derived-mode-class 'text-mode) ==> text-mode
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
101 ;; (derived-mode-class 'hypertext-mode) ==> text-mode
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
102 ;; (derived-mode-class 'html-mode) ==> text-mode
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
103
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
104 ;;; Code:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
105
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
106 ;; PUBLIC: define a new major mode which inherits from an existing one.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
107
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
108 ;; XEmacs -- no autoload
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
109 (defmacro define-derived-mode (child parent name &optional docstring &rest body)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
110 "Create a new mode as a variant of an existing mode.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
111
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
112 The arguments to this command are as follow:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
113
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
114 CHILD: the name of the command for the derived mode.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
115 PARENT: the name of the command for the parent mode (ie. text-mode).
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
116 NAME: a string which will appear in the status line (ie. \"Hypertext\")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
117 DOCSTRING: an optional documentation string--if you do not supply one,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
118 the function will attempt to invent something useful.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
119 BODY: forms to execute just before running the
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
120 hooks for the new mode.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
121
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
122 Here is how you could define LaTeX-Thesis mode as a variant of LaTeX mode:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
123
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
124 (define-derived-mode LaTeX-thesis-mode LaTeX-mode \"LaTeX-Thesis\")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
125
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
126 You could then make new key bindings for `LaTeX-thesis-mode-map'
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
127 without changing regular LaTeX mode. In this example, BODY is empty,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
128 and DOCSTRING is generated by default.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
129
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
130 On a more complicated level, the following command uses sgml-mode as
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
131 the parent, and then sets the variable `case-fold-search' to nil:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
132
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
133 (define-derived-mode article-mode sgml-mode \"Article\"
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
134 \"Major mode for editing technical articles.\"
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
135 (setq case-fold-search nil))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
136
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
137 Note that if the documentation string had been left out, it would have
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
138 been generated automatically, with a reference to the keymap."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
139
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
140 ; Some trickiness, since what
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
141 ; appears to be the docstring
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
142 ; may really be the first
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
143 ; element of the body.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
144 (if (and docstring (not (stringp docstring)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
145 (progn (setq body (cons docstring body))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
146 (setq docstring nil)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
147 (setq docstring (or docstring (derived-mode-make-docstring parent child)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
148
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
149 (` (progn
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
150 (derived-mode-init-mode-variables (quote (, child)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
151 (defun (, child) ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
152 (, docstring)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
153 (interactive)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
154 ; Run the parent.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
155 ((, parent))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
156 ; Identify special modes.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
157 (if (get (quote (, parent)) 'special)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
158 (put (quote (, child)) 'special t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
159 ;; XEmacs addition
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
160 (let ((mode-class (get (quote (, parent)) 'mode-class)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
161 (if mode-class
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
162 (put (quote (, child)) 'mode-class mode-class)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
163 ; Identify the child mode.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
164 (setq major-mode (quote (, child)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
165 (setq mode-name (, name))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
166 ; Set up maps and tables.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
167 (derived-mode-set-keymap (quote (, child)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
168 (derived-mode-set-syntax-table (quote (, child)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
169 (derived-mode-set-abbrev-table (quote (, child)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
170 ; Splice in the body (if any).
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
171 (,@ body)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
172 ;;; ; Run the setup function, if
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
173 ;;; ; any -- this will soon be
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
174 ;;; ; obsolete.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
175 ;;; (derived-mode-run-setup-function (quote (, child)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
176 ; Run the hooks, if any.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
177 (derived-mode-run-hooks (quote (, child)))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
178
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
179
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
180 ;; PUBLIC: find the ultimate class of a derived mode.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
181
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
182 (defun derived-mode-class (mode)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
183 "Find the class of a major mode.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
184 A mode's class is the first ancestor which is NOT a derived mode.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
185 Use the `derived-mode-parent' property of the symbol to trace backwards."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
186 (while (get mode 'derived-mode-parent)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
187 (setq mode (get mode 'derived-mode-parent)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
188 mode)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
189
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
190
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
191 ;; Inline functions to construct various names from a mode name.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
192
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
193 (defsubst derived-mode-setup-function-name (mode)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
194 "Construct a setup-function name based on a mode name."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
195 (intern (concat (symbol-name mode) "-setup")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
196
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
197 (defsubst derived-mode-hooks-name (mode)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
198 "Construct a hooks name based on a mode name."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
199 ;; XEmacs change from -hooks
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
200 (intern (concat (symbol-name mode) "-hook")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
201
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
202 (defsubst derived-mode-map-name (mode)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
203 "Construct a map name based on a mode name."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
204 (intern (concat (symbol-name mode) "-map")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
205
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
206 (defsubst derived-mode-syntax-table-name (mode)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
207 "Construct a syntax-table name based on a mode name."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
208 (intern (concat (symbol-name mode) "-syntax-table")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
210 (defsubst derived-mode-abbrev-table-name (mode)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
211 "Construct an abbrev-table name based on a mode name."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
212 (intern (concat (symbol-name mode) "-abbrev-table")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
213
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
214
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
215 ;; Utility functions for defining a derived mode.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
216
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
217 ;; XEmacs -- don't autoload
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
218 (defun derived-mode-init-mode-variables (mode)
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 209
diff changeset
219 "Initialize variables for a new mode.
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
220 Right now, if they don't already exist, set up a blank keymap, an
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
221 empty syntax table, and an empty abbrev table -- these will be merged
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
222 the first time the mode is used."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
223
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
224 (if (boundp (derived-mode-map-name mode))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
225 t
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
226 (eval (` (defvar (, (derived-mode-map-name mode))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
227 ;; XEmacs change
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
228 (make-sparse-keymap (derived-mode-map-name mode))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
229 (, (format "Keymap for %s." mode)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
230 (put (derived-mode-map-name mode) 'derived-mode-unmerged t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
231
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
232 (if (boundp (derived-mode-syntax-table-name mode))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
233 t
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
234 (eval (` (defvar (, (derived-mode-syntax-table-name mode))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
235 ;; XEmacs change
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
236 ;; Make a syntax table which doesn't specify anything
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
237 ;; for any char. Valid data will be merged in by
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
238 ;; derived-mode-merge-syntax-tables.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
239 ;; (make-char-table 'syntax-table nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
240 (make-syntax-table)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
241 (, (format "Syntax table for %s." mode)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
242 (put (derived-mode-syntax-table-name mode) 'derived-mode-unmerged t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
243
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
244 (if (boundp (derived-mode-abbrev-table-name mode))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
245 t
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
246 (eval (` (defvar (, (derived-mode-abbrev-table-name mode))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
247 (progn (define-abbrev-table (derived-mode-abbrev-table-name mode) nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
248 (make-abbrev-table))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
249 (, (format "Abbrev table for %s." mode)))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
250
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
251 (defun derived-mode-make-docstring (parent child)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
252 "Construct a docstring for a new mode if none is provided."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
253
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
254 (format "This major mode is a variant of `%s', created by `define-derived-mode'.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
255 It inherits all of the parent's attributes, but has its own keymap,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
256 abbrev table and syntax table:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
257
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
258 `%s-map' and `%s-syntax-table'
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
259
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
260 which more-or-less shadow
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
261
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
262 `%s-map' and `%s-syntax-table'
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
263
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
264 \\{%s-map}" parent child child parent parent child))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
265
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
266
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
267 ;; Utility functions for running a derived mode.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
268
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
269 (defun derived-mode-set-keymap (mode)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
270 "Set the keymap of the new mode, maybe merging with the parent."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
271 (let* ((map-name (derived-mode-map-name mode))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
272 (new-map (eval map-name))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
273 (old-map (current-local-map)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
274 (and old-map
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
275 (get map-name 'derived-mode-unmerged)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
276 (derived-mode-merge-keymaps old-map new-map))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
277 (put map-name 'derived-mode-unmerged nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
278 (use-local-map new-map)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
279
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
280 (defun derived-mode-set-syntax-table (mode)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
281 "Set the syntax table of the new mode, maybe merging with the parent."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
282 (let* ((table-name (derived-mode-syntax-table-name mode))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
283 (old-table (syntax-table))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
284 (new-table (eval table-name)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
285 (if (get table-name 'derived-mode-unmerged)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
286 (derived-mode-merge-syntax-tables old-table new-table))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
287 (put table-name 'derived-mode-unmerged nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
288 (set-syntax-table new-table)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
289
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
290 (defun derived-mode-set-abbrev-table (mode)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
291 "Set the abbrev table if it exists.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
292 Always merge its parent into it, since the merge is non-destructive."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
293 (let* ((table-name (derived-mode-abbrev-table-name mode))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
294 (old-table local-abbrev-table)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
295 (new-table (eval table-name)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
296 (derived-mode-merge-abbrev-tables old-table new-table)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
297 (setq local-abbrev-table new-table)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
298
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
299 ;;;(defun derived-mode-run-setup-function (mode)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
300 ;;; "Run the setup function if it exists."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
301
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
302 ;;; (let ((fname (derived-mode-setup-function-name mode)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
303 ;;; (if (fboundp fname)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
304 ;;; (funcall fname))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
305
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
306 (defun derived-mode-run-hooks (mode)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
307 "Run the hooks if they exist."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
308
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
309 (let ((hooks-name (derived-mode-hooks-name mode)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
310 (if (boundp hooks-name)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
311 (run-hooks hooks-name))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
312
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
313 ;; Functions to merge maps and tables.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
314
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
315 (defun derived-mode-merge-keymaps (old new)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
316 "Merge an old keymap into a new one.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
317 The old keymap is set to be the parent of the new one, so that there will
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
318 be automatic inheritance."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
319 ;; XEmacs change. FSF 19.30 & 19.34 has a whole bunch of weird crap here
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
320 ;; for merging prefix keys and such. Hopefully none of this is
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
321 ;; necessary in XEmacs.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
322 (set-keymap-parents new (list old)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
323
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
324 (defun derived-mode-merge-syntax-tables (old new)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
325 "Merge an old syntax table into a new one.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
326 Where the new table already has an entry, nothing is copied from the old one."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
327 ;; 20.x
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
328 (if (fboundp 'map-char-table)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
329 ;; we use map-char-table not map-syntax-table so we can explicitly
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
330 ;; check for inheritance.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
331 (map-char-table
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
332 #'(lambda (key value)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
333 (if (eq ?@ (char-syntax-from-code value))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
334 (map-char-table #'(lambda (key1 value1)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
335 (put-char-table key1 value1 new))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
336 old
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
337 key)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
338 new)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
339 ;; pre-20.0
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
340 (let ((idx 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
341 (end (min (length new) (length old))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
342 (while (< idx end)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
343 (if (not (aref new idx))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
344 (aset new idx (aref old idx)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
345 (setq idx (1+ idx))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
346
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
347 ;; Merge an old abbrev table into a new one.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
348 ;; This function requires internal knowledge of how abbrev tables work,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
349 ;; presuming that they are obarrays with the abbrev as the symbol, the expansion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
350 ;; as the value of the symbol, and the hook as the function definition.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
351 (defun derived-mode-merge-abbrev-tables (old new)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
352 (if old
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
353 (mapatoms
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
354 (function
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
355 (lambda (symbol)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
356 (or (intern-soft (symbol-name symbol) new)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
357 (define-abbrev new (symbol-name symbol)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
358 (symbol-value symbol) (symbol-function symbol)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
359 old)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
360
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
361 (provide 'derived)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
362
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
363 ;;; derived.el ends here