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