Mercurial > hg > xemacs-beta
annotate lisp/derived.el @ 5753:dbd8305e13cb
Warn about non-string non-integer ARG to #'gensym, bytecomp.el.
lisp/ChangeLog addition:
2013-08-21 Aidan Kehoe <kehoea@parhasard.net>
* bytecomp.el:
* bytecomp.el (gensym):
* bytecomp.el (byte-compile-gensym): New.
Warn that gensym called in a for-effect context is unlikely to be
useful.
Warn about non-string non-integer ARGs, this is incorrect.
Am not changing the function to error with same, most code that
makes the mistake is has no problems, which is why it has survived
so long.
* window-xemacs.el (save-window-excursion/mapping):
* window.el (save-window-excursion):
Call #'gensym with a string, not a symbol.
author | Aidan Kehoe <kehoea@parhasard.net> |
---|---|
date | Wed, 21 Aug 2013 19:02:59 +0100 |
parents | 308d34e9f07d |
children |
rev | line source |
---|---|
2135 | 1 ;;; derived.el --- allow inheritance of major modes |
2 ;;; (formerly mode-clone.el) | |
428 | 3 |
2135 | 4 ;; Copyright (C) 1993, 1994, 1999, 2003 Free Software Foundation, Inc. |
428 | 5 |
6 ;; Author: David Megginson (dmeggins@aix1.uottawa.ca) | |
7 ;; Maintainer: XEmacs Development Team | |
8 ;; Keywords: extensions, dumped | |
9 | |
10 ;; This file is part of XEmacs. | |
11 | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
3163
diff
changeset
|
12 ;; XEmacs is free software: you can redistribute it and/or modify it |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
3163
diff
changeset
|
13 ;; under the terms of the GNU General Public License as published by the |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
3163
diff
changeset
|
14 ;; Free Software Foundation, either version 3 of the License, or (at your |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
3163
diff
changeset
|
15 ;; option) any later version. |
428 | 16 |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
3163
diff
changeset
|
17 ;; XEmacs is distributed in the hope that it will be useful, but WITHOUT |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
3163
diff
changeset
|
18 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
3163
diff
changeset
|
19 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
3163
diff
changeset
|
20 ;; for more details. |
428 | 21 |
22 ;; You should have received a copy of the GNU General Public License | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
3163
diff
changeset
|
23 ;; along with XEmacs. If not, see <http://www.gnu.org/licenses/>. |
428 | 24 |
2135 | 25 ;;; Synched up with: FSF 21.3. |
428 | 26 |
27 ;;; Commentary: | |
28 | |
29 ;; This file is dumped with XEmacs. | |
30 | |
613 | 31 ;; XEmacs is already, in a sense, object oriented -- each object |
428 | 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) | |
3061 | 60 ;; keymap `hypertext-mode-map'. The command M-x hypertext-mode will |
428 | 61 ;; perform the following actions: |
62 ;; | |
63 ;; - run the command (text-mode) to get its default setup | |
3061 | 64 ;; - replace the current keymap with 'hypertext-mode-map', which will |
428 | 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 | |
3061 | 78 ;; untouched -- if you had added the new keystroke to `text-mode-map', |
428 | 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] | |
2135 | 86 ;; |
428 | 87 ;; will add a new major mode for HTML with very little fuss. |
88 ;; | |
2135 | 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). | |
428 | 93 |
94 ;;; Code: | |
95 | |
2135 | 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 | |
428 | 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. | |
2135 | 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\") | |
428 | 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 | |
2135 | 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. | |
428 | 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 | |
2135 | 150 On a more complicated level, the following command uses `sgml-mode' as |
428 | 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 | |
2135 | 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)) | |
428 | 180 |
2135 | 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)) | |
428 | 191 |
2135 | 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 | |
428 | 203 (defun ,child () |
204 ,docstring | |
205 (interactive) | |
206 ; Run the parent. | |
2135 | 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) | |
428 | 213 ; Identify special modes. |
2135 | 214 ,(when parent |
215 `(progn | |
216 (if (get (quote ,parent) 'mode-class) | |
217 (put (quote ,child) 'mode-class | |
218 (get (quote ,parent) 'mode-class))) | |
428 | 219 ; Set up maps and tables. |
2135 | 220 (unless (keymap-parent ,map) |
2140 | 221 (set-keymap-parents ,map (list (current-local-map)))) |
2135 | 222 ,(when declare-syntax |
2140 | 223 ;; XEmacs change: we do not have char-table-parent |
224 `(derived-mode-merge-syntax-tables | |
2142 | 225 (syntax-table) ,syntax)))) |
2135 | 226 |
227 (use-local-map ,map) | |
228 ,(when syntax `(set-syntax-table ,syntax)) | |
229 ,(when abbrev `(setq local-abbrev-table ,abbrev)) | |
428 | 230 ; Splice in the body (if any). |
2135 | 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)))))) | |
428 | 239 |
240 ;; PUBLIC: find the ultimate class of a derived mode. | |
241 | |
242 (defun derived-mode-class (mode) | |
2135 | 243 "Find the class of a major MODE. |
428 | 244 A mode's class is the first ancestor which is NOT a derived mode. |
2135 | 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." | |
428 | 248 (while (get mode 'derived-mode-parent) |
249 (setq mode (get mode 'derived-mode-parent))) | |
250 mode) | |
2135 | 251 (make-obsolete 'derived-mode-class 'derived-mode-p) |
428 | 252 |
906 | 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 | |
428 | 264 |
2135 | 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 | |
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. | |
428 | 314 |
315 (defsubst derived-mode-setup-function-name (mode) | |
2135 | 316 "Construct a setup-function name based on a MODE name." |
428 | 317 (intern (concat (symbol-name mode) "-setup"))) |
318 | |
319 | |
320 ;; Utility functions for defining a derived mode. | |
321 | |
322 ;; XEmacs -- don't autoload | |
323 (defun derived-mode-init-mode-variables (mode) | |
2135 | 324 "Initialise variables for a new MODE. |
428 | 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 ;; XEmacs change | |
333 (make-sparse-keymap (derived-mode-map-name mode)) | |
334 ,(format "Keymap for %s." mode))) | |
335 (put (derived-mode-map-name mode) 'derived-mode-unmerged t)) | |
336 | |
337 (if (boundp (derived-mode-syntax-table-name mode)) | |
338 t | |
339 (eval `(defvar ,(derived-mode-syntax-table-name mode) | |
340 ;; XEmacs change | |
341 ;; Make a syntax table which doesn't specify anything | |
342 ;; for any char. Valid data will be merged in by | |
343 ;; derived-mode-merge-syntax-tables. | |
344 ;; (make-char-table 'syntax-table nil) | |
345 (make-syntax-table) | |
346 ,(format "Syntax table for %s." mode))) | |
347 (put (derived-mode-syntax-table-name mode) 'derived-mode-unmerged t)) | |
348 | |
349 (if (boundp (derived-mode-abbrev-table-name mode)) | |
350 t | |
351 (eval `(defvar ,(derived-mode-abbrev-table-name mode) | |
2135 | 352 (progn |
353 (define-abbrev-table (derived-mode-abbrev-table-name mode) nil) | |
354 (make-abbrev-table)) | |
428 | 355 ,(format "Abbrev table for %s." mode))))) |
356 | |
357 ;; Utility functions for running a derived mode. | |
358 | |
359 (defun derived-mode-set-keymap (mode) | |
2135 | 360 "Set the keymap of the new MODE, maybe merging with the parent." |
428 | 361 (let* ((map-name (derived-mode-map-name mode)) |
362 (new-map (eval map-name)) | |
363 (old-map (current-local-map))) | |
364 (and old-map | |
365 (get map-name 'derived-mode-unmerged) | |
366 (derived-mode-merge-keymaps old-map new-map)) | |
367 (put map-name 'derived-mode-unmerged nil) | |
368 (use-local-map new-map))) | |
369 | |
2135 | 370 (defun derived-mode-set-syntax-table (mode) |
371 "Set the syntax table of the new MODE, maybe merging with the parent." | |
428 | 372 (let* ((table-name (derived-mode-syntax-table-name mode)) |
373 (old-table (syntax-table)) | |
374 (new-table (eval table-name))) | |
375 (if (get table-name 'derived-mode-unmerged) | |
376 (derived-mode-merge-syntax-tables old-table new-table)) | |
377 (put table-name 'derived-mode-unmerged nil) | |
378 (set-syntax-table new-table))) | |
379 | |
380 (defun derived-mode-set-abbrev-table (mode) | |
2135 | 381 "Set the abbrev table for MODE if it exists. |
428 | 382 Always merge its parent into it, since the merge is non-destructive." |
383 (let* ((table-name (derived-mode-abbrev-table-name mode)) | |
384 (old-table local-abbrev-table) | |
385 (new-table (eval table-name))) | |
386 (derived-mode-merge-abbrev-tables old-table new-table) | |
387 (setq local-abbrev-table new-table))) | |
388 | |
389 ;;;(defun derived-mode-run-setup-function (mode) | |
390 ;;; "Run the setup function if it exists." | |
391 | |
392 ;;; (let ((fname (derived-mode-setup-function-name mode))) | |
393 ;;; (if (fboundp fname) | |
394 ;;; (funcall fname)))) | |
395 | |
396 (defun derived-mode-run-hooks (mode) | |
2135 | 397 "Run the mode hook for MODE." |
398 (let ((hooks-name (derived-mode-hook-name mode))) | |
428 | 399 (if (boundp hooks-name) |
400 (run-hooks hooks-name)))) | |
401 | |
402 ;; Functions to merge maps and tables. | |
403 | |
404 (defun derived-mode-merge-keymaps (old new) | |
2135 | 405 "Merge an OLD keymap into a NEW one. |
406 The old keymap is set to be the last cdr of the new one, so that there will | |
428 | 407 be automatic inheritance." |
2135 | 408 ;; XEmacs change. FSF 19.30 to 21.3 has a whole bunch of weird crap here |
428 | 409 ;; for merging prefix keys and such. Hopefully none of this is |
410 ;; necessary in XEmacs. | |
411 (set-keymap-parents new (list old))) | |
412 | |
413 (defun derived-mode-merge-syntax-tables (old new) | |
2135 | 414 "Merge an OLD syntax table into a NEW one. |
428 | 415 Where the new table already has an entry, nothing is copied from the old one." |
2135 | 416 ;; XEmacs change: on the other hand, Emacs 21.3 just has |
417 ;; (set-char-table-parent new old) here. | |
418 ;; We use map-char-table, not map-syntax-table, so we can explicitly | |
419 ;; check for inheritance. | |
3163 | 420 (map-char-table |
421 #'(lambda (key value) | |
422 (let ((newval (get-range-char-table key new 'multi))) | |
423 (cond ((eq newval 'multi) ; OK, dive into the class hierarchy | |
424 (map-char-table | |
425 #'(lambda (key1 value1) | |
426 (when (eq ?@ (char-syntax-from-code | |
427 (get-range-char-table key new ?@))) | |
428 (put-char-table key1 value new)) | |
429 nil) | |
430 new | |
431 key)) | |
432 ((eq ?@ (char-syntax-from-code newval)) ;; class at once | |
433 (put-char-table key value new)))) | |
434 nil) | |
435 old)) | |
428 | 436 |
437 ;; Merge an old abbrev table into a new one. | |
438 ;; This function requires internal knowledge of how abbrev tables work, | |
439 ;; presuming that they are obarrays with the abbrev as the symbol, the expansion | |
440 ;; as the value of the symbol, and the hook as the function definition. | |
441 (defun derived-mode-merge-abbrev-tables (old new) | |
442 (if old | |
2135 | 443 (mapatoms |
444 #'(lambda (symbol) | |
445 (or (intern-soft (symbol-name symbol) new) | |
446 (define-abbrev new (symbol-name symbol) | |
447 (symbol-value symbol) (symbol-function symbol)))) | |
428 | 448 old))) |
2135 | 449 |
428 | 450 (provide 'derived) |
451 | |
2135 | 452 ;;; arch-tag: 630be248-47d1-4f02-afa0-8207de0ebea0 |
428 | 453 ;;; derived.el ends here |