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