Mercurial > emacs
annotate lisp/emacs-lisp/derived.el @ 70166:1204092d4458
*** empty log message ***
author | Kim F. Storm <storm@cua.dk> |
---|---|
date | Fri, 21 Apr 2006 14:06:42 +0000 |
parents | 067115a6e738 |
children | 1d4b1a32fd66 c5406394f567 |
rev | line source |
---|---|
51349 | 1 ;;; derived.el --- allow inheritance of major modes |
63345 | 2 ;; (formerly mode-clone.el) |
51349 | 3 |
64751
5b1a238fcbb4
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64333
diff
changeset
|
4 ;; Copyright (C) 1993, 1994, 1999, 2002, 2003, 2004, |
68648
067115a6e738
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
67292
diff
changeset
|
5 ;; 2005, 2006 Free Software Foundation, Inc. |
51349 | 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 | |
64085 | 25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
26 ;; Boston, MA 02110-1301, USA. | |
51349 | 27 |
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 | |
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) | |
64328
5562cab21b1f
(derived-mode-run-hooks): Remove.
Juanma Barranquero <lekktu@gmail.com>
parents:
64085
diff
changeset
|
100 "Construct a mode-hook name based on a MODE name." |
51349 | 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. | |
62597
c51f100a7a23
(define-derived-mode): Doc fix.
Luc Teirlinck <teirllm@auburn.edu>
parents:
62349
diff
changeset
|
136 The command `customize-mode' uses this. |
51349 | 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 | |
52026
a1c58b19ec8a
(define-derived-mode): Mention hook in doc string. Defvar the derived
Glenn Morris <rgm@gnu.org>
parents:
51349
diff
changeset
|
160 been generated automatically, with a reference to the keymap. |
a1c58b19ec8a
(define-derived-mode): Mention hook in doc string. Defvar the derived
Glenn Morris <rgm@gnu.org>
parents:
51349
diff
changeset
|
161 |
a1c58b19ec8a
(define-derived-mode): Mention hook in doc string. Defvar the derived
Glenn Morris <rgm@gnu.org>
parents:
51349
diff
changeset
|
162 The new mode runs the hook constructed by the function |
62349
8d37ba4764ca
(define-derived-mode): Add link to Elisp manual to docstring.
Luc Teirlinck <teirllm@auburn.edu>
parents:
59996
diff
changeset
|
163 `derived-mode-hook-name'. |
8d37ba4764ca
(define-derived-mode): Add link to Elisp manual to docstring.
Luc Teirlinck <teirllm@auburn.edu>
parents:
59996
diff
changeset
|
164 |
8d37ba4764ca
(define-derived-mode): Add link to Elisp manual to docstring.
Luc Teirlinck <teirllm@auburn.edu>
parents:
59996
diff
changeset
|
165 See Info node `(elisp)Derived Modes' for more details." |
51349 | 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 | |
66872
4b798da1bc61
(define-derived-mode): Remove defvar for mode hook. (It conflicted
Luc Teirlinck <teirllm@auburn.edu>
parents:
64751
diff
changeset
|
197 (unless (get ',hook 'variable-documentation) |
4b798da1bc61
(define-derived-mode): Remove defvar for mode hook. (It conflicted
Luc Teirlinck <teirllm@auburn.edu>
parents:
64751
diff
changeset
|
198 (put ',hook 'variable-documentation |
4b798da1bc61
(define-derived-mode): Remove defvar for mode hook. (It conflicted
Luc Teirlinck <teirllm@auburn.edu>
parents:
64751
diff
changeset
|
199 ,(format "Hook run when entering %s mode. |
4b798da1bc61
(define-derived-mode): Remove defvar for mode hook. (It conflicted
Luc Teirlinck <teirllm@auburn.edu>
parents:
64751
diff
changeset
|
200 No problems result if this variable is not bound. |
4b798da1bc61
(define-derived-mode): Remove defvar for mode hook. (It conflicted
Luc Teirlinck <teirllm@auburn.edu>
parents:
64751
diff
changeset
|
201 `add-hook' automatically binds it. (This is true for all hook variables.)" |
4b798da1bc61
(define-derived-mode): Remove defvar for mode hook. (It conflicted
Luc Teirlinck <teirllm@auburn.edu>
parents:
64751
diff
changeset
|
202 name))) |
67292
ada17aff9ef8
(define-derived-mode): Put `definition-name'
Juri Linkov <juri@jurta.org>
parents:
66872
diff
changeset
|
203 (unless (boundp ',map) |
ada17aff9ef8
(define-derived-mode): Put `definition-name'
Juri Linkov <juri@jurta.org>
parents:
66872
diff
changeset
|
204 (put ',map 'definition-name ',child)) |
51349 | 205 (defvar ,map (make-sparse-keymap)) |
206 ,(if declare-syntax | |
67292
ada17aff9ef8
(define-derived-mode): Put `definition-name'
Juri Linkov <juri@jurta.org>
parents:
66872
diff
changeset
|
207 `(progn |
ada17aff9ef8
(define-derived-mode): Put `definition-name'
Juri Linkov <juri@jurta.org>
parents:
66872
diff
changeset
|
208 (unless (boundp ',syntax) |
ada17aff9ef8
(define-derived-mode): Put `definition-name'
Juri Linkov <juri@jurta.org>
parents:
66872
diff
changeset
|
209 (put ',syntax 'definition-name ',child)) |
ada17aff9ef8
(define-derived-mode): Put `definition-name'
Juri Linkov <juri@jurta.org>
parents:
66872
diff
changeset
|
210 (defvar ,syntax (make-syntax-table)))) |
51349 | 211 ,(if declare-abbrev |
67292
ada17aff9ef8
(define-derived-mode): Put `definition-name'
Juri Linkov <juri@jurta.org>
parents:
66872
diff
changeset
|
212 `(progn |
ada17aff9ef8
(define-derived-mode): Put `definition-name'
Juri Linkov <juri@jurta.org>
parents:
66872
diff
changeset
|
213 (put ',abbrev 'definition-name ',child) |
ada17aff9ef8
(define-derived-mode): Put `definition-name'
Juri Linkov <juri@jurta.org>
parents:
66872
diff
changeset
|
214 (defvar ,abbrev |
ada17aff9ef8
(define-derived-mode): Put `definition-name'
Juri Linkov <juri@jurta.org>
parents:
66872
diff
changeset
|
215 (progn (define-abbrev-table ',abbrev nil) ,abbrev)))) |
51349 | 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) | |
63345 | 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. | |
51349 | 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. | |
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)))))) | |
262 | |
263 ;; PUBLIC: find the ultimate class of a derived mode. | |
264 | |
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 | |
270 is not very useful." | |
271 (while (get mode 'derived-mode-parent) | |
272 (setq mode (get mode 'derived-mode-parent))) | |
273 mode) | |
59996
aac0a33f5772
Change release version from 21.4 to 22.1 throughout.
Kim F. Storm <storm@cua.dk>
parents:
52401
diff
changeset
|
274 (make-obsolete 'derived-mode-class 'derived-mode-p "22.1") |
51349 | 275 |
276 | |
277 ;;; PRIVATE | |
278 | |
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." | |
282 | |
283 (let ((map (derived-mode-map-name child)) | |
284 (hook (derived-mode-hook-name child))) | |
285 | |
286 (unless (stringp docstring) | |
287 ;; Use a default docstring. | |
288 (setq docstring | |
289 (if (null parent) | |
290 (format "Major-mode. | |
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: | |
295 | |
296 `%s', `%s' and `%s' | |
297 | |
298 which more-or-less shadow %s's corresponding tables." | |
299 parent map abbrev syntax parent)))) | |
300 | |
301 (unless (string-match (regexp-quote (symbol-name hook)) docstring) | |
302 ;; Make sure the docstring mentions the mode's hook. | |
303 (setq docstring | |
304 (concat docstring | |
305 (if (null parent) | |
306 "\n\nThis mode " | |
307 (concat | |
308 "\n\nIn addition to any hooks its parent mode " | |
309 (if (string-match (regexp-quote (format "`%s'" parent)) | |
310 docstring) nil | |
311 (format "`%s' " parent)) | |
312 "might have run,\nthis mode ")) | |
313 (format "runs the hook `%s'" hook) | |
314 ", as the final step\nduring initialization."))) | |
315 | |
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) "}"))) | |
319 | |
320 docstring)) | |
321 | |
322 | |
323 ;;; OBSOLETE | |
324 ;; The functions below are only provided for backward compatibility with | |
325 ;; code byte-compiled with versions of derived.el prior to Emacs-21. | |
326 | |
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"))) | |
330 | |
331 | |
332 ;; Utility functions for defining a derived mode. | |
333 | |
334 ;;;###autoload | |
335 (defun derived-mode-init-mode-variables (mode) | |
63510
9d09522aef6b
(derived-mode-init-mode-variables): Fix spelling in docstrings.
Juanma Barranquero <lekktu@gmail.com>
parents:
63345
diff
changeset
|
336 "Initialize variables for a new MODE. |
51349 | 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." | |
340 | |
341 (if (boundp (derived-mode-map-name mode)) | |
342 t | |
343 (eval `(defvar ,(derived-mode-map-name mode) | |
344 (make-sparse-keymap) | |
345 ,(format "Keymap for %s." mode))) | |
346 (put (derived-mode-map-name mode) 'derived-mode-unmerged t)) | |
347 | |
348 (if (boundp (derived-mode-syntax-table-name mode)) | |
349 t | |
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)) | |
357 | |
358 (if (boundp (derived-mode-abbrev-table-name mode)) | |
359 t | |
360 (eval `(defvar ,(derived-mode-abbrev-table-name mode) | |
361 (progn | |
362 (define-abbrev-table (derived-mode-abbrev-table-name mode) nil) | |
363 (make-abbrev-table)) | |
364 ,(format "Abbrev table for %s." mode))))) | |
365 | |
366 ;; Utility functions for running a derived mode. | |
367 | |
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))) | |
373 (and old-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))) | |
378 | |
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))) | |
388 | |
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))) | |
397 | |
64333
92e5d9e433d3
(derived-mode-run-hooks): Reinstalled, as it is needed for pre-21 compatibility.
Juanma Barranquero <lekktu@gmail.com>
parents:
64328
diff
changeset
|
398 (defun derived-mode-run-hooks (mode) |
92e5d9e433d3
(derived-mode-run-hooks): Reinstalled, as it is needed for pre-21 compatibility.
Juanma Barranquero <lekktu@gmail.com>
parents:
64328
diff
changeset
|
399 "Run the mode hook for MODE." |
92e5d9e433d3
(derived-mode-run-hooks): Reinstalled, as it is needed for pre-21 compatibility.
Juanma Barranquero <lekktu@gmail.com>
parents:
64328
diff
changeset
|
400 (let ((hooks-name (derived-mode-hook-name mode))) |
92e5d9e433d3
(derived-mode-run-hooks): Reinstalled, as it is needed for pre-21 compatibility.
Juanma Barranquero <lekktu@gmail.com>
parents:
64328
diff
changeset
|
401 (if (boundp hooks-name) |
92e5d9e433d3
(derived-mode-run-hooks): Reinstalled, as it is needed for pre-21 compatibility.
Juanma Barranquero <lekktu@gmail.com>
parents:
64328
diff
changeset
|
402 (run-hooks hooks-name)))) |
92e5d9e433d3
(derived-mode-run-hooks): Reinstalled, as it is needed for pre-21 compatibility.
Juanma Barranquero <lekktu@gmail.com>
parents:
64328
diff
changeset
|
403 |
51349 | 404 ;; Functions to merge maps and tables. |
405 | |
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'? | |
411 (let ((tail new)) | |
412 ;; Scan the NEW map for prefix keys. | |
413 (while (consp tail) | |
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))))) | |
424 (while (>= i 0) | |
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))) | |
431 (setq i (1- i))))) | |
432 (setq tail (cdr tail)))) | |
433 (setcdr (nthcdr (1- (length new)) new) old)) | |
434 | |
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)) | |
439 | |
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) | |
445 (if old | |
446 (mapatoms | |
447 (lambda (symbol) | |
448 (or (intern-soft (symbol-name symbol) new) | |
449 (define-abbrev new (symbol-name symbol) | |
450 (symbol-value symbol) (symbol-function symbol)))) | |
451 old))) | |
452 | |
453 (provide 'derived) | |
454 | |
63345 | 455 ;; arch-tag: 630be248-47d1-4f02-afa0-8207de0ebea0 |
51349 | 456 ;;; derived.el ends here |