]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/derived.el
8e1f79f95ee39a8386b8215d5785bedcffc8fb16
[gnu-emacs] / lisp / emacs-lisp / derived.el
1 ;;; derived.el --- allow inheritance of major modes
2 ;; (formerly mode-clone.el)
3
4 ;; Copyright (C) 1993, 1994, 1999, 2002, 2003, 2004,
5 ;; 2005 Free Software Foundation, Inc.
6
7 ;; Author: David Megginson (dmeggins@aix1.uottawa.ca)
8 ;; Maintainer: FSF
9 ;; Keywords: extensions
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
27 \f
28 ;;; Commentary:
29
30 ;; GNU Emacs is already, in a sense, object oriented -- each object
31 ;; (buffer) belongs to a class (major mode), and that class defines
32 ;; the relationship between messages (input events) and methods
33 ;; (commands) by means of a keymap.
34 ;;
35 ;; The only thing missing is a good scheme of inheritance. It is
36 ;; possible to simulate a single level of inheritance with generous
37 ;; use of hooks and a bit of work -- sgml-mode, for example, also runs
38 ;; the hooks for text-mode, and keymaps can inherit from other keymaps
39 ;; -- but generally, each major mode ends up reinventing the wheel.
40 ;; Ideally, someone should redesign all of Emacs's major modes to
41 ;; follow a more conventional object-oriented system: when defining a
42 ;; new major mode, the user should need only to name the existing mode
43 ;; it is most similar to, then list the (few) differences.
44 ;;
45 ;; In the mean time, this package offers most of the advantages of
46 ;; full inheritance with the existing major modes. The macro
47 ;; `define-derived-mode' allows the user to make a variant of an existing
48 ;; major mode, with its own keymap. The new mode will inherit the key
49 ;; bindings of its parent, and will, in fact, run its parent first
50 ;; every time it is called. For example, the commands
51 ;;
52 ;; (define-derived-mode hypertext-mode text-mode "Hypertext"
53 ;; "Major mode for hypertext.\n\n\\{hypertext-mode-map}"
54 ;; (setq case-fold-search nil))
55 ;;
56 ;; (define-key hypertext-mode-map [down-mouse-3] 'do-hyper-link)
57 ;;
58 ;; will create a function `hypertext-mode' with its own (sparse)
59 ;; keymap `hypertext-mode-map.' The command M-x hypertext-mode will
60 ;; perform the following actions:
61 ;;
62 ;; - run the command (text-mode) to get its default setup
63 ;; - replace the current keymap with 'hypertext-mode-map,' which will
64 ;; inherit from 'text-mode-map'.
65 ;; - replace the current syntax table with
66 ;; 'hypertext-mode-syntax-table', which will borrow its defaults
67 ;; from the current text-mode-syntax-table.
68 ;; - replace the current abbrev table with
69 ;; 'hypertext-mode-abbrev-table', which will borrow its defaults
70 ;; from the current text-mode-abbrev table
71 ;; - change the mode line to read "Hypertext"
72 ;; - assign the value 'hypertext-mode' to the 'major-mode' variable
73 ;; - run the body of commands provided in the macro -- in this case,
74 ;; set the local variable `case-fold-search' to nil.
75 ;;
76 ;; The advantages of this system are threefold. First, text mode is
77 ;; untouched -- if you had added the new keystroke to `text-mode-map,'
78 ;; possibly using hooks, you would have added it to all text buffers
79 ;; -- here, it appears only in hypertext buffers, where it makes
80 ;; sense. Second, it is possible to build even further, and make
81 ;; a derived mode from a derived mode. The commands
82 ;;
83 ;; (define-derived-mode html-mode hypertext-mode "HTML")
84 ;; [various key definitions]
85 ;;
86 ;; will add a new major mode for HTML with very little fuss.
87 ;;
88 ;; Note also the function `derived-mode-p' which can tell if the current
89 ;; mode derives from another. In a hypertext-mode, buffer, for example,
90 ;; (derived-mode-p 'text-mode) would return non-nil. This should always
91 ;; be used in place of (eq major-mode 'text-mode).
92 \f
93 ;;; Code:
94
95 (eval-when-compile (require 'cl))
96
97 ;;; PRIVATE: defsubst must be defined before they are first used
98
99 (defsubst derived-mode-hook-name (mode)
100 "Construct a mode-hook name based on a MODE name."
101 (intern (concat (symbol-name mode) "-hook")))
102
103 (defsubst derived-mode-map-name (mode)
104 "Construct a map name based on a MODE name."
105 (intern (concat (symbol-name mode) "-map")))
106
107 (defsubst derived-mode-syntax-table-name (mode)
108 "Construct a syntax-table name based on a MODE name."
109 (intern (concat (symbol-name mode) "-syntax-table")))
110
111 (defsubst derived-mode-abbrev-table-name (mode)
112 "Construct an abbrev-table name based on a MODE name."
113 (intern (concat (symbol-name mode) "-abbrev-table")))
114
115 ;; PUBLIC: define a new major mode which inherits from an existing one.
116
117 ;;;###autoload
118 (defmacro define-derived-mode (child parent name &optional docstring &rest body)
119 "Create a new mode as a variant of an existing mode.
120
121 The arguments to this command are as follow:
122
123 CHILD: the name of the command for the derived mode.
124 PARENT: the name of the command for the parent mode (e.g. `text-mode')
125 or nil if there is no parent.
126 NAME: a string which will appear in the status line (e.g. \"Hypertext\")
127 DOCSTRING: an optional documentation string--if you do not supply one,
128 the function will attempt to invent something useful.
129 BODY: forms to execute just before running the
130 hooks for the new mode. Do not use `interactive' here.
131
132 BODY can start with a bunch of keyword arguments. The following keyword
133 arguments are currently understood:
134 :group GROUP
135 Declare the customization group that corresponds to this mode.
136 The command `customize-mode' uses this.
137 :syntax-table TABLE
138 Use TABLE instead of the default.
139 A nil value means to simply use the same syntax-table as the parent.
140 :abbrev-table TABLE
141 Use TABLE instead of the default.
142 A nil value means to simply use the same abbrev-table as the parent.
143
144 Here is how you could define LaTeX-Thesis mode as a variant of LaTeX mode:
145
146 (define-derived-mode LaTeX-thesis-mode LaTeX-mode \"LaTeX-Thesis\")
147
148 You could then make new key bindings for `LaTeX-thesis-mode-map'
149 without changing regular LaTeX mode. In this example, BODY is empty,
150 and DOCSTRING is generated by default.
151
152 On a more complicated level, the following command uses `sgml-mode' as
153 the parent, and then sets the variable `case-fold-search' to nil:
154
155 (define-derived-mode article-mode sgml-mode \"Article\"
156 \"Major mode for editing technical articles.\"
157 (setq case-fold-search nil))
158
159 Note that if the documentation string had been left out, it would have
160 been generated automatically, with a reference to the keymap.
161
162 The new mode runs the hook constructed by the function
163 `derived-mode-hook-name'.
164
165 See Info node `(elisp)Derived Modes' for more details."
166 (declare (debug (&define name symbolp sexp [&optional stringp]
167 [&rest keywordp sexp] def-body)))
168
169 (when (and docstring (not (stringp docstring)))
170 ;; Some trickiness, since what appears to be the docstring may really be
171 ;; the first element of the body.
172 (push docstring body)
173 (setq docstring nil))
174
175 (when (eq parent 'fundamental-mode) (setq parent nil))
176
177 (let ((map (derived-mode-map-name child))
178 (syntax (derived-mode-syntax-table-name child))
179 (abbrev (derived-mode-abbrev-table-name child))
180 (declare-abbrev t)
181 (declare-syntax t)
182 (hook (derived-mode-hook-name child))
183 (group nil))
184
185 ;; Process the keyword args.
186 (while (keywordp (car body))
187 (case (pop body)
188 (:group (setq group (pop body)))
189 (:abbrev-table (setq abbrev (pop body)) (setq declare-abbrev nil))
190 (:syntax-table (setq syntax (pop body)) (setq declare-syntax nil))
191 (t (pop body))))
192
193 (setq docstring (derived-mode-make-docstring
194 parent child docstring syntax abbrev))
195
196 `(progn
197 (defvar ,hook nil ,(format "Hook run when entering %s mode." name))
198 (defvar ,map (make-sparse-keymap))
199 ,(if declare-syntax
200 `(defvar ,syntax (make-syntax-table)))
201 ,(if declare-abbrev
202 `(defvar ,abbrev
203 (progn (define-abbrev-table ',abbrev nil) ,abbrev)))
204 (put ',child 'derived-mode-parent ',parent)
205 ,(if group `(put ',child 'custom-mode-group ,group))
206
207 (defun ,child ()
208 ,docstring
209 (interactive)
210 ; Run the parent.
211 (delay-mode-hooks
212
213 (,(or parent 'kill-all-local-variables))
214 ; Identify the child mode.
215 (setq major-mode (quote ,child))
216 (setq mode-name ,name)
217 ; Identify special modes.
218 ,(when parent
219 `(progn
220 (if (get (quote ,parent) 'mode-class)
221 (put (quote ,child) 'mode-class
222 (get (quote ,parent) 'mode-class)))
223 ; Set up maps and tables.
224 (unless (keymap-parent ,map)
225 ;; It would probably be better to set the keymap's parent
226 ;; at the toplevel rather than inside the mode function,
227 ;; but this is not easy for at least the following reasons:
228 ;; - the parent (and its keymap) may not yet be loaded.
229 ;; - the parent's keymap name may be called something else
230 ;; than <parent>-mode-map.
231 (set-keymap-parent ,map (current-local-map)))
232 ,(when declare-syntax
233 `(let ((parent (char-table-parent ,syntax)))
234 (unless (and parent
235 (not (eq parent (standard-syntax-table))))
236 (set-char-table-parent ,syntax (syntax-table)))))))
237
238 (use-local-map ,map)
239 ,(when syntax `(set-syntax-table ,syntax))
240 ,(when abbrev `(setq local-abbrev-table ,abbrev))
241 ; Splice in the body (if any).
242 ,@body
243 )
244 ;; Run the hooks, if any.
245 ;; Make the generated code work in older Emacs versions
246 ;; that do not yet have run-mode-hooks.
247 (if (fboundp 'run-mode-hooks)
248 (run-mode-hooks ',hook)
249 (run-hooks ',hook))))))
250
251 ;; PUBLIC: find the ultimate class of a derived mode.
252
253 (defun derived-mode-class (mode)
254 "Find the class of a major MODE.
255 A mode's class is the first ancestor which is NOT a derived mode.
256 Use the `derived-mode-parent' property of the symbol to trace backwards.
257 Since major-modes might all derive from `fundamental-mode', this function
258 is not very useful."
259 (while (get mode 'derived-mode-parent)
260 (setq mode (get mode 'derived-mode-parent)))
261 mode)
262 (make-obsolete 'derived-mode-class 'derived-mode-p "22.1")
263
264 \f
265 ;;; PRIVATE
266
267 (defun derived-mode-make-docstring (parent child &optional
268 docstring syntax abbrev)
269 "Construct a docstring for a new mode if none is provided."
270
271 (let ((map (derived-mode-map-name child))
272 (hook (derived-mode-hook-name child)))
273
274 (unless (stringp docstring)
275 ;; Use a default docstring.
276 (setq docstring
277 (if (null parent)
278 (format "Major-mode.
279 Uses keymap `%s', abbrev table `%s' and syntax-table `%s'." map abbrev syntax)
280 (format "Major mode derived from `%s' by `define-derived-mode'.
281 It inherits all of the parent's attributes, but has its own keymap,
282 abbrev table and syntax table:
283
284 `%s', `%s' and `%s'
285
286 which more-or-less shadow %s's corresponding tables."
287 parent map abbrev syntax parent))))
288
289 (unless (string-match (regexp-quote (symbol-name hook)) docstring)
290 ;; Make sure the docstring mentions the mode's hook.
291 (setq docstring
292 (concat docstring
293 (if (null parent)
294 "\n\nThis mode "
295 (concat
296 "\n\nIn addition to any hooks its parent mode "
297 (if (string-match (regexp-quote (format "`%s'" parent))
298 docstring) nil
299 (format "`%s' " parent))
300 "might have run,\nthis mode "))
301 (format "runs the hook `%s'" hook)
302 ", as the final step\nduring initialization.")))
303
304 (unless (string-match "\\\\[{[]" docstring)
305 ;; And don't forget to put the mode's keymap.
306 (setq docstring (concat docstring "\n\n\\{" (symbol-name map) "}")))
307
308 docstring))
309
310 \f
311 ;;; OBSOLETE
312 ;; The functions below are only provided for backward compatibility with
313 ;; code byte-compiled with versions of derived.el prior to Emacs-21.
314
315 (defsubst derived-mode-setup-function-name (mode)
316 "Construct a setup-function name based on a MODE name."
317 (intern (concat (symbol-name mode) "-setup")))
318
319 \f
320 ;; Utility functions for defining a derived mode.
321
322 ;;;###autoload
323 (defun derived-mode-init-mode-variables (mode)
324 "Initialize variables for a new MODE.
325 Right now, if they don't already exist, set up a blank keymap, an
326 empty syntax table, and an empty abbrev table -- these will be merged
327 the first time the mode is used."
328
329 (if (boundp (derived-mode-map-name mode))
330 t
331 (eval `(defvar ,(derived-mode-map-name mode)
332 (make-sparse-keymap)
333 ,(format "Keymap for %s." mode)))
334 (put (derived-mode-map-name mode) 'derived-mode-unmerged t))
335
336 (if (boundp (derived-mode-syntax-table-name mode))
337 t
338 (eval `(defvar ,(derived-mode-syntax-table-name mode)
339 ;; Make a syntax table which doesn't specify anything
340 ;; for any char. Valid data will be merged in by
341 ;; derived-mode-merge-syntax-tables.
342 (make-char-table 'syntax-table nil)
343 ,(format "Syntax table for %s." mode)))
344 (put (derived-mode-syntax-table-name mode) 'derived-mode-unmerged t))
345
346 (if (boundp (derived-mode-abbrev-table-name mode))
347 t
348 (eval `(defvar ,(derived-mode-abbrev-table-name mode)
349 (progn
350 (define-abbrev-table (derived-mode-abbrev-table-name mode) nil)
351 (make-abbrev-table))
352 ,(format "Abbrev table for %s." mode)))))
353 \f
354 ;; Utility functions for running a derived mode.
355
356 (defun derived-mode-set-keymap (mode)
357 "Set the keymap of the new MODE, maybe merging with the parent."
358 (let* ((map-name (derived-mode-map-name mode))
359 (new-map (eval map-name))
360 (old-map (current-local-map)))
361 (and old-map
362 (get map-name 'derived-mode-unmerged)
363 (derived-mode-merge-keymaps old-map new-map))
364 (put map-name 'derived-mode-unmerged nil)
365 (use-local-map new-map)))
366
367 (defun derived-mode-set-syntax-table (mode)
368 "Set the syntax table of the new MODE, maybe merging with the parent."
369 (let* ((table-name (derived-mode-syntax-table-name mode))
370 (old-table (syntax-table))
371 (new-table (eval table-name)))
372 (if (get table-name 'derived-mode-unmerged)
373 (derived-mode-merge-syntax-tables old-table new-table))
374 (put table-name 'derived-mode-unmerged nil)
375 (set-syntax-table new-table)))
376
377 (defun derived-mode-set-abbrev-table (mode)
378 "Set the abbrev table for MODE if it exists.
379 Always merge its parent into it, since the merge is non-destructive."
380 (let* ((table-name (derived-mode-abbrev-table-name mode))
381 (old-table local-abbrev-table)
382 (new-table (eval table-name)))
383 (derived-mode-merge-abbrev-tables old-table new-table)
384 (setq local-abbrev-table new-table)))
385
386 (defun derived-mode-run-hooks (mode)
387 "Run the mode hook for MODE."
388 (let ((hooks-name (derived-mode-hook-name mode)))
389 (if (boundp hooks-name)
390 (run-hooks hooks-name))))
391
392 ;; Functions to merge maps and tables.
393
394 (defun derived-mode-merge-keymaps (old new)
395 "Merge an OLD keymap into a NEW one.
396 The old keymap is set to be the last cdr of the new one, so that there will
397 be automatic inheritance."
398 ;; ?? Can this just use `set-keymap-parent'?
399 (let ((tail new))
400 ;; Scan the NEW map for prefix keys.
401 (while (consp tail)
402 (and (consp (car tail))
403 (let* ((key (vector (car (car tail))))
404 (subnew (lookup-key new key))
405 (subold (lookup-key old key)))
406 ;; If KEY is a prefix key in both OLD and NEW, merge them.
407 (and (keymapp subnew) (keymapp subold)
408 (derived-mode-merge-keymaps subold subnew))))
409 (and (vectorp (car tail))
410 ;; Search a vector of ASCII char bindings for prefix keys.
411 (let ((i (1- (length (car tail)))))
412 (while (>= i 0)
413 (let* ((key (vector i))
414 (subnew (lookup-key new key))
415 (subold (lookup-key old key)))
416 ;; If KEY is a prefix key in both OLD and NEW, merge them.
417 (and (keymapp subnew) (keymapp subold)
418 (derived-mode-merge-keymaps subold subnew)))
419 (setq i (1- i)))))
420 (setq tail (cdr tail))))
421 (setcdr (nthcdr (1- (length new)) new) old))
422
423 (defun derived-mode-merge-syntax-tables (old new)
424 "Merge an OLD syntax table into a NEW one.
425 Where the new table already has an entry, nothing is copied from the old one."
426 (set-char-table-parent new old))
427
428 ;; Merge an old abbrev table into a new one.
429 ;; This function requires internal knowledge of how abbrev tables work,
430 ;; presuming that they are obarrays with the abbrev as the symbol, the expansion
431 ;; as the value of the symbol, and the hook as the function definition.
432 (defun derived-mode-merge-abbrev-tables (old new)
433 (if old
434 (mapatoms
435 (lambda (symbol)
436 (or (intern-soft (symbol-name symbol) new)
437 (define-abbrev new (symbol-name symbol)
438 (symbol-value symbol) (symbol-function symbol))))
439 old)))
440
441 (provide 'derived)
442
443 ;; arch-tag: 630be248-47d1-4f02-afa0-8207de0ebea0
444 ;;; derived.el ends here