Merge from emacs--rel--22
[bpt/emacs.git] / lisp / emacs-lisp / derived.el
CommitLineData
5e046f6d 1;;; derived.el --- allow inheritance of major modes
84ae4908 2;; (formerly mode-clone.el)
5e046f6d 3
d59c3137 4;; Copyright (C) 1993, 1994, 1999, 2001, 2002, 2003, 2004,
f0fa15c5 5;; 2005, 2006, 2007 Free Software Foundation, Inc.
5e046f6d
JB
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
3a35cf56
LK
25;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26;; Boston, MA 02110-1301, USA.
5e046f6d
JB
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)
fea34e9f 100 "Construct a mode-hook name based on a MODE name."
5e046f6d
JB
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
121The arguments to this command are as follow:
122
123CHILD: the name of the command for the derived mode.
124PARENT: the name of the command for the parent mode (e.g. `text-mode')
125 or nil if there is no parent.
126NAME: a string which will appear in the status line (e.g. \"Hypertext\")
127DOCSTRING: an optional documentation string--if you do not supply one,
128 the function will attempt to invent something useful.
129BODY: forms to execute just before running the
130 hooks for the new mode. Do not use `interactive' here.
131
132BODY 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.
3e2c3ab0 136 The command `customize-mode' uses this.
5e046f6d
JB
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
144Here 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
148You could then make new key bindings for `LaTeX-thesis-mode-map'
149without changing regular LaTeX mode. In this example, BODY is empty,
150and DOCSTRING is generated by default.
151
152On a more complicated level, the following command uses `sgml-mode' as
153the 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
159Note that if the documentation string had been left out, it would have
8d11b450
GM
160been generated automatically, with a reference to the keymap.
161
162The new mode runs the hook constructed by the function
a5079d40
LT
163`derived-mode-hook-name'.
164
165See Info node `(elisp)Derived Modes' for more details."
5e046f6d
JB
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
156680e2
LT
197 (unless (get ',hook 'variable-documentation)
198 (put ',hook 'variable-documentation
199 ,(format "Hook run when entering %s mode.
200No problems result if this variable is not bound.
201`add-hook' automatically binds it. (This is true for all hook variables.)"
202 name)))
cc793355
JL
203 (unless (boundp ',map)
204 (put ',map 'definition-name ',child))
5e046f6d
JB
205 (defvar ,map (make-sparse-keymap))
206 ,(if declare-syntax
cc793355
JL
207 `(progn
208 (unless (boundp ',syntax)
209 (put ',syntax 'definition-name ',child))
210 (defvar ,syntax (make-syntax-table))))
5e046f6d 211 ,(if declare-abbrev
cc793355
JL
212 `(progn
213 (put ',abbrev 'definition-name ',child)
214 (defvar ,abbrev
215 (progn (define-abbrev-table ',abbrev nil) ,abbrev))))
5e046f6d
JB
216 (put ',child 'derived-mode-parent ',parent)
217 ,(if group `(put ',child 'custom-mode-group ,group))
218
219 (defun ,child ()
220 ,docstring
221 (interactive)
222 ; Run the parent.
223 (delay-mode-hooks
224
225 (,(or parent 'kill-all-local-variables))
226 ; Identify the child mode.
227 (setq major-mode (quote ,child))
228 (setq mode-name ,name)
229 ; Identify special modes.
230 ,(when parent
231 `(progn
232 (if (get (quote ,parent) 'mode-class)
233 (put (quote ,child) 'mode-class
234 (get (quote ,parent) 'mode-class)))
235 ; Set up maps and tables.
236 (unless (keymap-parent ,map)
84ae4908
SM
237 ;; It would probably be better to set the keymap's parent
238 ;; at the toplevel rather than inside the mode function,
239 ;; but this is not easy for at least the following reasons:
240 ;; - the parent (and its keymap) may not yet be loaded.
241 ;; - the parent's keymap name may be called something else
242 ;; than <parent>-mode-map.
5e046f6d
JB
243 (set-keymap-parent ,map (current-local-map)))
244 ,(when declare-syntax
245 `(let ((parent (char-table-parent ,syntax)))
246 (unless (and parent
247 (not (eq parent (standard-syntax-table))))
248 (set-char-table-parent ,syntax (syntax-table)))))))
249
250 (use-local-map ,map)
251 ,(when syntax `(set-syntax-table ,syntax))
252 ,(when abbrev `(setq local-abbrev-table ,abbrev))
253 ; Splice in the body (if any).
254 ,@body
255 )
256 ;; Run the hooks, if any.
cdcfbcb2 257 (run-mode-hooks ',hook)))))
5e046f6d
JB
258
259;; PUBLIC: find the ultimate class of a derived mode.
260
261(defun derived-mode-class (mode)
262 "Find the class of a major MODE.
263A mode's class is the first ancestor which is NOT a derived mode.
264Use the `derived-mode-parent' property of the symbol to trace backwards.
265Since major-modes might all derive from `fundamental-mode', this function
266is not very useful."
267 (while (get mode 'derived-mode-parent)
268 (setq mode (get mode 'derived-mode-parent)))
269 mode)
bf247b6e 270(make-obsolete 'derived-mode-class 'derived-mode-p "22.1")
5e046f6d
JB
271
272\f
273;;; PRIVATE
274
275(defun derived-mode-make-docstring (parent child &optional
276 docstring syntax abbrev)
277 "Construct a docstring for a new mode if none is provided."
278
279 (let ((map (derived-mode-map-name child))
280 (hook (derived-mode-hook-name child)))
281
282 (unless (stringp docstring)
283 ;; Use a default docstring.
284 (setq docstring
285 (if (null parent)
286 (format "Major-mode.
287Uses keymap `%s', abbrev table `%s' and syntax-table `%s'." map abbrev syntax)
288 (format "Major mode derived from `%s' by `define-derived-mode'.
289It inherits all of the parent's attributes, but has its own keymap,
290abbrev table and syntax table:
291
292 `%s', `%s' and `%s'
293
294which more-or-less shadow %s's corresponding tables."
295 parent map abbrev syntax parent))))
296
297 (unless (string-match (regexp-quote (symbol-name hook)) docstring)
298 ;; Make sure the docstring mentions the mode's hook.
299 (setq docstring
300 (concat docstring
301 (if (null parent)
302 "\n\nThis mode "
303 (concat
304 "\n\nIn addition to any hooks its parent mode "
305 (if (string-match (regexp-quote (format "`%s'" parent))
306 docstring) nil
307 (format "`%s' " parent))
308 "might have run,\nthis mode "))
309 (format "runs the hook `%s'" hook)
310 ", as the final step\nduring initialization.")))
311
312 (unless (string-match "\\\\[{[]" docstring)
313 ;; And don't forget to put the mode's keymap.
314 (setq docstring (concat docstring "\n\n\\{" (symbol-name map) "}")))
315
316 docstring))
317
318\f
319;;; OBSOLETE
320;; The functions below are only provided for backward compatibility with
321;; code byte-compiled with versions of derived.el prior to Emacs-21.
322
323(defsubst derived-mode-setup-function-name (mode)
324 "Construct a setup-function name based on a MODE name."
325 (intern (concat (symbol-name mode) "-setup")))
326
327\f
328;; Utility functions for defining a derived mode.
329
330;;;###autoload
331(defun derived-mode-init-mode-variables (mode)
a9873dbb 332 "Initialize variables for a new MODE.
5e046f6d
JB
333Right now, if they don't already exist, set up a blank keymap, an
334empty syntax table, and an empty abbrev table -- these will be merged
335the first time the mode is used."
336
337 (if (boundp (derived-mode-map-name mode))
338 t
339 (eval `(defvar ,(derived-mode-map-name mode)
340 (make-sparse-keymap)
341 ,(format "Keymap for %s." mode)))
342 (put (derived-mode-map-name mode) 'derived-mode-unmerged t))
343
344 (if (boundp (derived-mode-syntax-table-name mode))
345 t
346 (eval `(defvar ,(derived-mode-syntax-table-name mode)
347 ;; Make a syntax table which doesn't specify anything
348 ;; for any char. Valid data will be merged in by
349 ;; derived-mode-merge-syntax-tables.
350 (make-char-table 'syntax-table nil)
351 ,(format "Syntax table for %s." mode)))
352 (put (derived-mode-syntax-table-name mode) 'derived-mode-unmerged t))
353
354 (if (boundp (derived-mode-abbrev-table-name mode))
355 t
356 (eval `(defvar ,(derived-mode-abbrev-table-name mode)
357 (progn
358 (define-abbrev-table (derived-mode-abbrev-table-name mode) nil)
359 (make-abbrev-table))
360 ,(format "Abbrev table for %s." mode)))))
361\f
362;; Utility functions for running a derived mode.
363
364(defun derived-mode-set-keymap (mode)
365 "Set the keymap of the new MODE, maybe merging with the parent."
366 (let* ((map-name (derived-mode-map-name mode))
367 (new-map (eval map-name))
368 (old-map (current-local-map)))
369 (and old-map
370 (get map-name 'derived-mode-unmerged)
371 (derived-mode-merge-keymaps old-map new-map))
372 (put map-name 'derived-mode-unmerged nil)
373 (use-local-map new-map)))
374
375(defun derived-mode-set-syntax-table (mode)
376 "Set the syntax table of the new MODE, maybe merging with the parent."
377 (let* ((table-name (derived-mode-syntax-table-name mode))
378 (old-table (syntax-table))
379 (new-table (eval table-name)))
380 (if (get table-name 'derived-mode-unmerged)
381 (derived-mode-merge-syntax-tables old-table new-table))
382 (put table-name 'derived-mode-unmerged nil)
383 (set-syntax-table new-table)))
384
385(defun derived-mode-set-abbrev-table (mode)
386 "Set the abbrev table for MODE if it exists.
387Always merge its parent into it, since the merge is non-destructive."
388 (let* ((table-name (derived-mode-abbrev-table-name mode))
389 (old-table local-abbrev-table)
390 (new-table (eval table-name)))
391 (derived-mode-merge-abbrev-tables old-table new-table)
392 (setq local-abbrev-table new-table)))
393
3c8dd9b9
JB
394(defun derived-mode-run-hooks (mode)
395 "Run the mode hook for MODE."
396 (let ((hooks-name (derived-mode-hook-name mode)))
397 (if (boundp hooks-name)
398 (run-hooks hooks-name))))
399
5e046f6d
JB
400;; Functions to merge maps and tables.
401
402(defun derived-mode-merge-keymaps (old new)
403 "Merge an OLD keymap into a NEW one.
404The old keymap is set to be the last cdr of the new one, so that there will
405be automatic inheritance."
406 ;; ?? Can this just use `set-keymap-parent'?
407 (let ((tail new))
408 ;; Scan the NEW map for prefix keys.
409 (while (consp tail)
410 (and (consp (car tail))
411 (let* ((key (vector (car (car tail))))
412 (subnew (lookup-key new key))
413 (subold (lookup-key old key)))
414 ;; If KEY is a prefix key in both OLD and NEW, merge them.
415 (and (keymapp subnew) (keymapp subold)
416 (derived-mode-merge-keymaps subold subnew))))
417 (and (vectorp (car tail))
418 ;; Search a vector of ASCII char bindings for prefix keys.
419 (let ((i (1- (length (car tail)))))
420 (while (>= i 0)
421 (let* ((key (vector i))
422 (subnew (lookup-key new key))
423 (subold (lookup-key old key)))
424 ;; If KEY is a prefix key in both OLD and NEW, merge them.
425 (and (keymapp subnew) (keymapp subold)
426 (derived-mode-merge-keymaps subold subnew)))
427 (setq i (1- i)))))
428 (setq tail (cdr tail))))
429 (setcdr (nthcdr (1- (length new)) new) old))
430
431(defun derived-mode-merge-syntax-tables (old new)
432 "Merge an OLD syntax table into a NEW one.
433Where the new table already has an entry, nothing is copied from the old one."
434 (set-char-table-parent new old))
435
436;; Merge an old abbrev table into a new one.
437;; This function requires internal knowledge of how abbrev tables work,
438;; presuming that they are obarrays with the abbrev as the symbol, the expansion
439;; as the value of the symbol, and the hook as the function definition.
440(defun derived-mode-merge-abbrev-tables (old new)
441 (if old
442 (mapatoms
443 (lambda (symbol)
444 (or (intern-soft (symbol-name symbol) new)
445 (define-abbrev new (symbol-name symbol)
446 (symbol-value symbol) (symbol-function symbol))))
447 old)))
448
449(provide 'derived)
450
84ae4908 451;; arch-tag: 630be248-47d1-4f02-afa0-8207de0ebea0
5e046f6d 452;;; derived.el ends here