1 ;;; derived.el --- allow inheritance of major modes
2 ;; (formerly mode-clone.el)
4 ;; Copyright (C) 1993, 1994, 1999, 2001, 2002, 2003, 2004,
5 ;; 2005, 2006 Free Software Foundation, Inc.
7 ;; Author: David Megginson (dmeggins@aix1.uottawa.ca)
9 ;; Keywords: extensions
11 ;; This file is part of GNU Emacs.
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)
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.
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.
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.
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.
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
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))
56 ;; (define-key hypertext-mode-map [down-mouse-3] 'do-hyper-link)
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:
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.
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
83 ;; (define-derived-mode html-mode hypertext-mode "HTML")
84 ;; [various key definitions]
86 ;; will add a new major mode for HTML with very little fuss.
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).
95 (eval-when-compile (require 'cl
))
97 ;;; PRIVATE: defsubst must be defined before they are first used
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")))
103 (defsubst derived-mode-map-name
(mode)
104 "Construct a map name based on a MODE name."
105 (intern (concat (symbol-name mode
) "-map")))
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")))
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")))
115 ;; PUBLIC: define a new major mode which inherits from an existing one.
118 (defmacro define-derived-mode
(child parent name
&optional docstring
&rest body
)
119 "Create a new mode as a variant of an existing mode.
121 The arguments to this command are as follow:
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.
132 BODY can start with a bunch of keyword arguments. The following keyword
133 arguments are currently understood:
135 Declare the customization group that corresponds to this mode.
136 The command `customize-mode' uses this.
138 Use TABLE instead of the default.
139 A nil value means to simply use the same syntax-table as the parent.
141 Use TABLE instead of the default.
142 A nil value means to simply use the same abbrev-table as the parent.
144 Here is how you could define LaTeX-Thesis mode as a variant of LaTeX mode:
146 (define-derived-mode LaTeX-thesis-mode LaTeX-mode \"LaTeX-Thesis\")
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.
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:
155 (define-derived-mode article-mode sgml-mode \"Article\"
156 \"Major mode for editing technical articles.\"
157 (setq case-fold-search nil))
159 Note that if the documentation string had been left out, it would have
160 been generated automatically, with a reference to the keymap.
162 The new mode runs the hook constructed by the function
163 `derived-mode-hook-name'.
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
)))
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
))
175 (when (eq parent
'fundamental-mode
) (setq parent nil
))
177 (let ((map (derived-mode-map-name child
))
178 (syntax (derived-mode-syntax-table-name child
))
179 (abbrev (derived-mode-abbrev-table-name child
))
182 (hook (derived-mode-hook-name child
))
185 ;; Process the keyword args.
186 (while (keywordp (car 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
))
193 (setq docstring
(derived-mode-make-docstring
194 parent child docstring syntax abbrev
))
197 (unless (get ',hook
'variable-documentation
)
198 (put ',hook
'variable-documentation
199 ,(format "Hook run when entering %s mode.
200 No problems result if this variable is not bound.
201 `add-hook' automatically binds it. (This is true for all hook variables.)"
203 (unless (boundp ',map
)
204 (put ',map
'definition-name
',child
))
205 (defvar ,map
(make-sparse-keymap))
208 (unless (boundp ',syntax
)
209 (put ',syntax
'definition-name
',child
))
210 (defvar ,syntax
(make-syntax-table))))
213 (put ',abbrev
'definition-name
',child
)
215 (progn (define-abbrev-table ',abbrev nil
) ,abbrev
))))
216 (put ',child
'derived-mode-parent
',parent
)
217 ,(if group
`(put ',child
'custom-mode-group
,group
))
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.
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
)
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.
243 (set-keymap-parent ,map
(current-local-map)))
244 ,(when declare-syntax
245 `(let ((parent (char-table-parent ,syntax
)))
247 (not (eq parent
(standard-syntax-table))))
248 (set-char-table-parent ,syntax
(syntax-table)))))))
251 ,(when syntax
`(set-syntax-table ,syntax
))
252 ,(when abbrev
`(setq local-abbrev-table
,abbrev
))
253 ; Splice in the body (if any).
256 ;; Run the hooks, if any.
257 ;; Make the generated code work in older Emacs versions
258 ;; that do not yet have run-mode-hooks.
259 (if (fboundp 'run-mode-hooks
)
260 (run-mode-hooks ',hook
)
261 (run-hooks ',hook
))))))
263 ;; PUBLIC: find the ultimate class of a derived mode.
265 (defun derived-mode-class (mode)
266 "Find the class of a major MODE.
267 A mode's class is the first ancestor which is NOT a derived mode.
268 Use the `derived-mode-parent' property of the symbol to trace backwards.
269 Since major-modes might all derive from `fundamental-mode', this function
271 (while (get mode
'derived-mode-parent
)
272 (setq mode
(get mode
'derived-mode-parent
)))
274 (make-obsolete 'derived-mode-class
'derived-mode-p
"22.1")
279 (defun derived-mode-make-docstring (parent child
&optional
280 docstring syntax abbrev
)
281 "Construct a docstring for a new mode if none is provided."
283 (let ((map (derived-mode-map-name child
))
284 (hook (derived-mode-hook-name child
)))
286 (unless (stringp docstring
)
287 ;; Use a default docstring.
291 Uses keymap `%s', abbrev table `%s' and syntax-table `%s'." map abbrev syntax
)
292 (format "Major mode derived from `%s' by `define-derived-mode'.
293 It inherits all of the parent's attributes, but has its own keymap,
294 abbrev table and syntax table:
298 which more-or-less shadow %s's corresponding tables."
299 parent map abbrev syntax parent
))))
301 (unless (string-match (regexp-quote (symbol-name hook
)) docstring
)
302 ;; Make sure the docstring mentions the mode's hook.
308 "\n\nIn addition to any hooks its parent mode "
309 (if (string-match (regexp-quote (format "`%s'" parent
))
311 (format "`%s' " parent
))
312 "might have run,\nthis mode "))
313 (format "runs the hook `%s'" hook
)
314 ", as the final step\nduring initialization.")))
316 (unless (string-match "\\\\[{[]" docstring
)
317 ;; And don't forget to put the mode's keymap.
318 (setq docstring
(concat docstring
"\n\n\\{" (symbol-name map
) "}")))
324 ;; The functions below are only provided for backward compatibility with
325 ;; code byte-compiled with versions of derived.el prior to Emacs-21.
327 (defsubst derived-mode-setup-function-name
(mode)
328 "Construct a setup-function name based on a MODE name."
329 (intern (concat (symbol-name mode
) "-setup")))
332 ;; Utility functions for defining a derived mode.
335 (defun derived-mode-init-mode-variables (mode)
336 "Initialize variables for a new MODE.
337 Right now, if they don't already exist, set up a blank keymap, an
338 empty syntax table, and an empty abbrev table -- these will be merged
339 the first time the mode is used."
341 (if (boundp (derived-mode-map-name mode
))
343 (eval `(defvar ,(derived-mode-map-name mode
)
345 ,(format "Keymap for %s." mode
)))
346 (put (derived-mode-map-name mode
) 'derived-mode-unmerged t
))
348 (if (boundp (derived-mode-syntax-table-name mode
))
350 (eval `(defvar ,(derived-mode-syntax-table-name mode
)
351 ;; Make a syntax table which doesn't specify anything
352 ;; for any char. Valid data will be merged in by
353 ;; derived-mode-merge-syntax-tables.
354 (make-char-table 'syntax-table nil
)
355 ,(format "Syntax table for %s." mode
)))
356 (put (derived-mode-syntax-table-name mode
) 'derived-mode-unmerged t
))
358 (if (boundp (derived-mode-abbrev-table-name mode
))
360 (eval `(defvar ,(derived-mode-abbrev-table-name mode
)
362 (define-abbrev-table (derived-mode-abbrev-table-name mode
) nil
)
364 ,(format "Abbrev table for %s." mode
)))))
366 ;; Utility functions for running a derived mode.
368 (defun derived-mode-set-keymap (mode)
369 "Set the keymap of the new MODE, maybe merging with the parent."
370 (let* ((map-name (derived-mode-map-name mode
))
371 (new-map (eval map-name
))
372 (old-map (current-local-map)))
374 (get map-name
'derived-mode-unmerged
)
375 (derived-mode-merge-keymaps old-map new-map
))
376 (put map-name
'derived-mode-unmerged nil
)
377 (use-local-map new-map
)))
379 (defun derived-mode-set-syntax-table (mode)
380 "Set the syntax table of the new MODE, maybe merging with the parent."
381 (let* ((table-name (derived-mode-syntax-table-name mode
))
382 (old-table (syntax-table))
383 (new-table (eval table-name
)))
384 (if (get table-name
'derived-mode-unmerged
)
385 (derived-mode-merge-syntax-tables old-table new-table
))
386 (put table-name
'derived-mode-unmerged nil
)
387 (set-syntax-table new-table
)))
389 (defun derived-mode-set-abbrev-table (mode)
390 "Set the abbrev table for MODE if it exists.
391 Always merge its parent into it, since the merge is non-destructive."
392 (let* ((table-name (derived-mode-abbrev-table-name mode
))
393 (old-table local-abbrev-table
)
394 (new-table (eval table-name
)))
395 (derived-mode-merge-abbrev-tables old-table new-table
)
396 (setq local-abbrev-table new-table
)))
398 (defun derived-mode-run-hooks (mode)
399 "Run the mode hook for MODE."
400 (let ((hooks-name (derived-mode-hook-name mode
)))
401 (if (boundp hooks-name
)
402 (run-hooks hooks-name
))))
404 ;; Functions to merge maps and tables.
406 (defun derived-mode-merge-keymaps (old new
)
407 "Merge an OLD keymap into a NEW one.
408 The old keymap is set to be the last cdr of the new one, so that there will
409 be automatic inheritance."
410 ;; ?? Can this just use `set-keymap-parent'?
412 ;; Scan the NEW map for prefix keys.
414 (and (consp (car tail
))
415 (let* ((key (vector (car (car tail
))))
416 (subnew (lookup-key new key
))
417 (subold (lookup-key old key
)))
418 ;; If KEY is a prefix key in both OLD and NEW, merge them.
419 (and (keymapp subnew
) (keymapp subold
)
420 (derived-mode-merge-keymaps subold subnew
))))
421 (and (vectorp (car tail
))
422 ;; Search a vector of ASCII char bindings for prefix keys.
423 (let ((i (1- (length (car tail
)))))
425 (let* ((key (vector i
))
426 (subnew (lookup-key new key
))
427 (subold (lookup-key old key
)))
428 ;; If KEY is a prefix key in both OLD and NEW, merge them.
429 (and (keymapp subnew
) (keymapp subold
)
430 (derived-mode-merge-keymaps subold subnew
)))
432 (setq tail
(cdr tail
))))
433 (setcdr (nthcdr (1- (length new
)) new
) old
))
435 (defun derived-mode-merge-syntax-tables (old new
)
436 "Merge an OLD syntax table into a NEW one.
437 Where the new table already has an entry, nothing is copied from the old one."
438 (set-char-table-parent new old
))
440 ;; Merge an old abbrev table into a new one.
441 ;; This function requires internal knowledge of how abbrev tables work,
442 ;; presuming that they are obarrays with the abbrev as the symbol, the expansion
443 ;; as the value of the symbol, and the hook as the function definition.
444 (defun derived-mode-merge-abbrev-tables (old new
)
448 (or (intern-soft (symbol-name symbol
) new
)
449 (define-abbrev new
(symbol-name symbol
)
450 (symbol-value symbol
) (symbol-function symbol
))))
455 ;; arch-tag: 630be248-47d1-4f02-afa0-8207de0ebea0
456 ;;; derived.el ends here