Commit | Line | Data |
---|---|---|
5a7a545c | 1 | ;;; easy-mmode.el --- easy definition for major and minor modes. |
6b279740 | 2 | |
c8c21615 | 3 | ;; Copyright (C) 1997,2000 Free Software Foundation, Inc. |
6b279740 RS |
4 | |
5 | ;; Author: Georges Brun-Cottan <Georges.Brun-Cottan@inria.fr> | |
29cc3b84 | 6 | ;; Maintainer: Stefan Monnier <monnier@gnu.org> |
6b279740 RS |
7 | |
8 | ;; This file is part of GNU Emacs. | |
9 | ||
10 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 | ;; it under the terms of the GNU General Public License as published by | |
12 | ;; the Free Software Foundation; either version 2, or (at your option) | |
13 | ;; any later version. | |
14 | ||
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | ;; Boston, MA 02111-1307, USA. | |
24 | ||
25 | ;;; Commentary: | |
26 | ||
27 | ;; Minor modes are useful and common. This package makes defining a | |
28 | ;; minor mode easy, by focusing on the writing of the minor mode | |
29 | ;; functionalities themselves. Moreover, this package enforces a | |
30 | ;; conventional naming of user interface primitives, making things | |
31 | ;; natural for the minor-mode end-users. | |
32 | ||
33 | ;; For each mode, easy-mmode defines the following: | |
34 | ;; <mode> : The minor mode predicate. A buffer-local variable. | |
35 | ;; <mode>-map : The keymap possibly associated to <mode>. | |
c8c21615 SM |
36 | ;; <mode>-hook : The hook run at the end of the toggle function. |
37 | ;; see `define-minor-mode' documentation | |
6b279740 RS |
38 | ;; |
39 | ;; eval | |
c8c21615 | 40 | ;; (pp (macroexpand '(define-minor-mode <your-mode> <doc>))) |
6b279740 RS |
41 | ;; to check the result before using it. |
42 | ||
43 | ;; The order in which minor modes are installed is important. Keymap | |
44 | ;; lookup proceeds down minor-mode-map-alist, and the order there | |
45 | ;; tends to be the reverse of the order in which the modes were | |
46 | ;; installed. Perhaps there should be a feature to let you specify | |
47 | ;; orderings. | |
48 | ||
5a7a545c SM |
49 | ;; Additionally to `define-minor-mode', the package provides convenient |
50 | ;; ways to define keymaps, and other helper functions for major and minor modes. | |
6b279740 | 51 | |
5a7a545c | 52 | ;;; Code: |
6b279740 | 53 | |
be22f4cc SM |
54 | (eval-when-compile (require 'cl)) |
55 | ||
b5bbbb76 SM |
56 | (defun easy-mmode-pretty-mode-name (mode &optional lighter) |
57 | "Turn the symbol MODE into a string intended for the user. | |
58 | If provided LIGHTER will be used to help choose capitalization." | |
59 | (let* ((case-fold-search t) | |
60 | (name (concat (capitalize (replace-regexp-in-string | |
61 | "-mode\\'" "" (symbol-name mode))) | |
62 | " mode"))) | |
63 | (if (not (stringp lighter)) name | |
64 | (setq lighter (replace-regexp-in-string "\\`\\s-+\\|\\-s+\\'" "" lighter)) | |
65 | (replace-regexp-in-string lighter lighter name t t)))) | |
3837de12 | 66 | |
6b279740 | 67 | ;;;###autoload |
29cc3b84 SM |
68 | (defalias 'easy-mmode-define-minor-mode 'define-minor-mode) |
69 | ;;;###autoload | |
70 | (defmacro define-minor-mode (mode doc &optional init-value lighter keymap &rest body) | |
6b279740 | 71 | "Define a new minor mode MODE. |
b5bbbb76 SM |
72 | This function defines the associated control variable MODE, keymap MODE-map, |
73 | toggle command MODE, and hook MODE-hook. | |
6b279740 RS |
74 | |
75 | DOC is the documentation for the mode toggle command. | |
29cc3b84 | 76 | Optional INIT-VALUE is the initial value of the mode's variable. |
c8c21615 | 77 | Optional LIGHTER is displayed in the modeline when the mode is on. |
6b279740 | 78 | Optional KEYMAP is the default (defvar) keymap bound to the mode keymap. |
b5bbbb76 SM |
79 | If it is a list, it is passed to `easy-mmode-define-keymap' |
80 | in order to build a valid keymap. | |
29cc3b84 | 81 | BODY contains code that will be executed each time the mode is (dis)activated. |
b5bbbb76 SM |
82 | It will be executed after any toggling but before running the hooks. |
83 | BODY can start with a list of CL-style keys specifying additional arguments. | |
84 | Currently two such keyword arguments are supported: | |
85 | :group followed by the group name to use for any generated `defcustom'. | |
86 | :global if non-nil specifies that the minor mode is not meant to be | |
87 | buffer-local. By default, the variable is made buffer-local." | |
6b279740 | 88 | (let* ((mode-name (symbol-name mode)) |
b5bbbb76 | 89 | (pretty-name (easy-mmode-pretty-mode-name mode lighter)) |
c8c21615 | 90 | (globalp nil) |
b5bbbb76 | 91 | ;; We might as well provide a best-guess default group. |
be22f4cc SM |
92 | (group |
93 | (list 'quote | |
94 | (intern (replace-regexp-in-string "-mode\\'" "" mode-name)))) | |
1328a6df SM |
95 | (keymap-sym (if (and keymap (symbolp keymap)) keymap |
96 | (intern (concat mode-name "-map")))) | |
b5bbbb76 SM |
97 | (hook (intern (concat mode-name "-hook"))) |
98 | (hook-on (intern (concat mode-name "-on-hook"))) | |
99 | (hook-off (intern (concat mode-name "-off-hook")))) | |
100 | ||
101 | ;; FIXME: compatibility that should be removed. | |
c8c21615 SM |
102 | (when (and (consp init-value) (eq (car init-value) 'global)) |
103 | (setq init-value (cdr init-value) globalp t)) | |
104 | ||
b5bbbb76 | 105 | ;; Check keys. |
be22f4cc SM |
106 | (while (keywordp (car body)) |
107 | (case (pop body) | |
108 | (:global (setq globalp (pop body))) | |
109 | (:group (setq group (pop body))) | |
110 | (t (setq body (cdr body))))) | |
b5bbbb76 SM |
111 | |
112 | ;; Add default properties to LIGHTER. | |
113 | (unless (or (not (stringp lighter)) (get-text-property 0 'local-map lighter) | |
114 | (get-text-property 0 'keymap lighter)) | |
115 | (setq lighter | |
116 | (apply 'propertize lighter | |
117 | 'local-map (make-mode-line-mouse2-map mode) | |
118 | (unless (get-text-property 0 'help-echo lighter) | |
119 | (list 'help-echo | |
120 | (format "mouse-2: turn off %s" pretty-name)))))) | |
121 | ||
6b279740 | 122 | `(progn |
5e21ef7a | 123 | ;; Define the variable to enable or disable the mode. |
d5b037c5 SM |
124 | ,(if (not globalp) |
125 | `(progn | |
126 | (defvar ,mode ,init-value ,(format "Non-nil if %s is enabled. | |
b5bbbb76 | 127 | Use the function `%s' to change this variable." pretty-name mode)) |
d5b037c5 | 128 | (make-variable-buffer-local ',mode)) |
6b279740 | 129 | |
1328a6df SM |
130 | (let ((curfile (or (and (boundp 'byte-compile-current-file) |
131 | byte-compile-current-file) | |
132 | load-file-name))) | |
133 | `(defcustom ,mode ,init-value | |
134 | ,(format "Toggle %s. | |
d5b037c5 SM |
135 | Setting this variable directly does not take effect; |
136 | use either \\[customize] or the function `%s'." | |
1328a6df SM |
137 | pretty-name mode) |
138 | :set (lambda (symbol value) (funcall symbol (or value 0))) | |
139 | :initialize 'custom-initialize-default | |
140 | :group ,group | |
141 | :type 'boolean | |
142 | ,@(when curfile | |
143 | (list | |
144 | :require | |
145 | (list 'quote | |
146 | (intern (file-name-nondirectory | |
147 | (file-name-sans-extension curfile))))))))) | |
148 | ||
149 | ;; The toggle's hook. Wrapped in `progn' to prevent autoloading. | |
150 | (progn | |
151 | (defcustom ,hook nil | |
152 | ,(format "Hook run at the end of function `%s'." mode-name) | |
153 | :group ,group | |
154 | :type 'hook)) | |
b5bbbb76 SM |
155 | |
156 | ;; The actual function. | |
157 | (defun ,mode (&optional arg) | |
158 | ,(or doc | |
159 | (format "With no argument, toggle %s. | |
160 | With universal prefix ARG turn mode on. | |
161 | With zero or negative ARG turn mode off. | |
162 | \\{%s}" pretty-name keymap-sym)) | |
163 | (interactive "P") | |
164 | (setq ,mode | |
165 | (if arg | |
166 | (> (prefix-numeric-value arg) 0) | |
167 | (not ,mode))) | |
168 | ,@body | |
169 | ;; The on/off hooks are here for backward compatibility only. | |
170 | (run-hooks ',hook (if ,mode ',hook-on ',hook-off)) | |
171 | ;; Return the new setting. | |
172 | (if (interactive-p) | |
173 | (message ,(format "%s %%sabled" pretty-name) | |
174 | (if ,mode "en" "dis"))) | |
175 | ,mode) | |
6b279740 | 176 | |
1328a6df SM |
177 | ;; Autoloading an easy-mmode-define-minor-mode autoloads |
178 | ;; everything up-to-here. | |
179 | :autoload-end | |
180 | ||
d5b037c5 | 181 | ;; Define the minor-mode keymap. |
1328a6df | 182 | ,(unless (symbolp keymap) ;nil is also a symbol. |
d5b037c5 | 183 | `(defvar ,keymap-sym |
1328a6df SM |
184 | (let ((m ,keymap)) |
185 | (cond ((keymapp m) m) | |
186 | ((listp m) (easy-mmode-define-keymap m)) | |
187 | (t (error "Invalid keymap %S" ,keymap)))) | |
d5b037c5 SM |
188 | ,(format "Keymap for `%s'." mode-name))) |
189 | ||
b5bbbb76 | 190 | (add-minor-mode ',mode ',lighter |
1328a6df SM |
191 | ,(if keymap keymap-sym |
192 | `(if (boundp ',keymap-sym) ,keymap-sym))) | |
c8c21615 SM |
193 | |
194 | ;; If the mode is global, call the function according to the default. | |
195 | ,(if globalp `(if ,mode (,mode 1)))))) | |
5a7a545c | 196 | \f |
be22f4cc SM |
197 | ;;; |
198 | ;;; make global minor mode | |
199 | ;;; | |
200 | ||
d5b037c5 | 201 | ;;;###autoload |
be22f4cc SM |
202 | (defmacro easy-mmode-define-global-mode (global-mode mode turn-on |
203 | &rest keys) | |
204 | "Make GLOBAL-MODE out of the MODE buffer-local minor mode. | |
205 | TURN-ON is a function that will be called with no args in every buffer | |
206 | and that should try to turn MODE on if applicable for that buffer. | |
207 | KEYS is a list of CL-style keyword arguments: | |
208 | :group to specify the custom group." | |
209 | (let* ((mode-name (symbol-name mode)) | |
210 | (global-mode-name (symbol-name global-mode)) | |
211 | (pretty-name (easy-mmode-pretty-mode-name mode)) | |
212 | (pretty-global-name (easy-mmode-pretty-mode-name global-mode)) | |
213 | ;; We might as well provide a best-guess default group. | |
214 | (group | |
215 | (list 'quote | |
216 | (intern (replace-regexp-in-string "-mode\\'" "" mode-name)))) | |
217 | (buffers (intern (concat global-mode-name "-buffers"))) | |
218 | (cmmh (intern (concat global-mode-name "-cmmh")))) | |
219 | ||
220 | ;; Check keys. | |
221 | (while (keywordp (car keys)) | |
222 | (case (pop keys) | |
223 | (:group (setq group (pop keys))) | |
224 | (t (setq keys (cdr keys))))) | |
225 | ||
226 | `(progn | |
be22f4cc SM |
227 | ;; The actual global minor-mode |
228 | (define-minor-mode ,global-mode | |
229 | ,(format "Toggle %s in every buffer. | |
230 | With prefix ARG, turn %s on if and only if ARG is positive. | |
231 | %s is actually not turned on in every buffer but only in those | |
232 | in which `%s' turns it on." | |
233 | pretty-name pretty-global-name pretty-name turn-on) | |
234 | nil nil nil :global t :group ,group | |
235 | ||
236 | ;; Setup hook to handle future mode changes and new buffers. | |
237 | (if ,global-mode | |
d5b037c5 SM |
238 | (progn |
239 | (add-hook 'find-file-hooks ',buffers) | |
240 | (add-hook 'change-major-mode-hook ',cmmh)) | |
241 | (remove-hook 'find-file-hooks ',buffers) | |
be22f4cc SM |
242 | (remove-hook 'change-major-mode-hook ',cmmh)) |
243 | ||
244 | ;; Go through existing buffers. | |
245 | (dolist (buf (buffer-list)) | |
246 | (with-current-buffer buf | |
247 | (if ,global-mode (,turn-on) (,mode -1))))) | |
248 | ||
1328a6df SM |
249 | ;; Autoloading easy-mmode-define-global-mode |
250 | ;; autoloads everything up-to-here. | |
251 | :autoload-end | |
252 | ||
be22f4cc SM |
253 | ;; List of buffers left to process. |
254 | (defvar ,buffers nil) | |
255 | ||
256 | ;; The function that calls TURN-ON in each buffer. | |
257 | (defun ,buffers () | |
be22f4cc | 258 | (remove-hook 'post-command-hook ',buffers) |
d5b037c5 SM |
259 | (while ,buffers |
260 | (let ((buf (pop ,buffers))) | |
261 | (when (buffer-live-p buf) | |
262 | (with-current-buffer buf (,turn-on)))))) | |
be22f4cc SM |
263 | |
264 | ;; The function that catches kill-all-local-variables. | |
265 | (defun ,cmmh () | |
266 | (add-to-list ',buffers (current-buffer)) | |
d5b037c5 | 267 | (add-hook 'post-command-hook ',buffers))))) |
be22f4cc | 268 | |
5a7a545c SM |
269 | ;;; |
270 | ;;; easy-mmode-defmap | |
271 | ;;; | |
272 | ||
273 | (if (fboundp 'set-keymap-parents) | |
274 | (defalias 'easy-mmode-set-keymap-parents 'set-keymap-parents) | |
275 | (defun easy-mmode-set-keymap-parents (m parents) | |
276 | (set-keymap-parent | |
277 | m | |
278 | (cond | |
279 | ((not (consp parents)) parents) | |
280 | ((not (cdr parents)) (car parents)) | |
281 | (t (let ((m (copy-keymap (pop parents)))) | |
282 | (easy-mmode-set-keymap-parents m parents) | |
283 | m)))))) | |
284 | ||
285 | (defun easy-mmode-define-keymap (bs &optional name m args) | |
286 | "Return a keymap built from bindings BS. | |
287 | BS must be a list of (KEY . BINDING) where | |
3837de12 SM |
288 | KEY and BINDINGS are suitable for `define-key'. |
289 | Optional NAME is passed to `make-sparse-keymap'. | |
290 | Optional map M can be used to modify an existing map. | |
5a7a545c SM |
291 | ARGS is a list of additional arguments." |
292 | (let (inherit dense suppress) | |
293 | (while args | |
294 | (let ((key (pop args)) | |
295 | (val (pop args))) | |
be22f4cc SM |
296 | (case key |
297 | (:dense (setq dense val)) | |
298 | (:inherit (setq inherit val)) | |
299 | (:group) | |
5a7a545c SM |
300 | ;;((eq key :suppress) (setq suppress val)) |
301 | (t (message "Unknown argument %s in defmap" key))))) | |
302 | (unless (keymapp m) | |
303 | (setq bs (append m bs)) | |
304 | (setq m (if dense (make-keymap name) (make-sparse-keymap name)))) | |
305 | (dolist (b bs) | |
306 | (let ((keys (car b)) | |
307 | (binding (cdr b))) | |
308 | (dolist (key (if (consp keys) keys (list keys))) | |
309 | (cond | |
310 | ((symbolp key) | |
311 | (substitute-key-definition key binding m global-map)) | |
312 | ((null binding) | |
313 | (unless (keymapp (lookup-key m key)) (define-key m key binding))) | |
314 | ((let ((o (lookup-key m key))) | |
315 | (or (null o) (numberp o) (eq o 'undefined))) | |
316 | (define-key m key binding)))))) | |
317 | (cond | |
318 | ((keymapp inherit) (set-keymap-parent m inherit)) | |
319 | ((consp inherit) (easy-mmode-set-keymap-parents m inherit))) | |
320 | m)) | |
321 | ||
322 | ;;;###autoload | |
323 | (defmacro easy-mmode-defmap (m bs doc &rest args) | |
e4ad5f9e SM |
324 | `(progn |
325 | (autoload 'easy-mmode-define-keymap "easy-mmode") | |
326 | (defconst ,m | |
327 | (easy-mmode-define-keymap ,bs nil (if (boundp ',m) ,m) ,(cons 'list args)) | |
328 | ,doc))) | |
5a7a545c SM |
329 | |
330 | \f | |
331 | ;;; | |
332 | ;;; easy-mmode-defsyntax | |
333 | ;;; | |
334 | ||
335 | (defun easy-mmode-define-syntax (css args) | |
336 | (let ((st (make-syntax-table (cadr (memq :copy args))))) | |
337 | (dolist (cs css) | |
338 | (let ((char (car cs)) | |
339 | (syntax (cdr cs))) | |
340 | (if (sequencep char) | |
e4ad5f9e | 341 | (mapcar (lambda (c) (modify-syntax-entry c syntax st)) char) |
5a7a545c SM |
342 | (modify-syntax-entry char syntax st)))) |
343 | st)) | |
344 | ||
345 | ;;;###autoload | |
346 | (defmacro easy-mmode-defsyntax (st css doc &rest args) | |
e4ad5f9e SM |
347 | `(progn |
348 | (autoload 'easy-mmode-define-syntax "easy-mmode") | |
349 | (defconst ,st (easy-mmode-define-syntax ,css ,(cons 'list args)) doc))) | |
5a7a545c SM |
350 | |
351 | ||
352 | \f | |
c7ea3acc | 353 | ;;; |
5a7a545c | 354 | ;;; A "macro-only" reimplementation of define-derived-mode. |
c7ea3acc | 355 | ;;; |
5a7a545c | 356 | |
c7ea3acc SM |
357 | ;;;###autoload |
358 | (defmacro define-derived-mode (child parent name &optional docstring &rest body) | |
5a7a545c SM |
359 | "Create a new mode as a variant of an existing mode. |
360 | ||
361 | The arguments to this command are as follow: | |
362 | ||
363 | CHILD: the name of the command for the derived mode. | |
364 | PARENT: the name of the command for the parent mode (e.g. `text-mode'). | |
365 | NAME: a string which will appear in the status line (e.g. \"Hypertext\") | |
366 | DOCSTRING: an optional documentation string--if you do not supply one, | |
367 | the function will attempt to invent something useful. | |
368 | BODY: forms to execute just before running the | |
369 | hooks for the new mode. | |
370 | ||
371 | Here is how you could define LaTeX-Thesis mode as a variant of LaTeX mode: | |
372 | ||
373 | (define-derived-mode LaTeX-thesis-mode LaTeX-mode \"LaTeX-Thesis\") | |
374 | ||
375 | You could then make new key bindings for `LaTeX-thesis-mode-map' | |
376 | without changing regular LaTeX mode. In this example, BODY is empty, | |
377 | and DOCSTRING is generated by default. | |
378 | ||
379 | On a more complicated level, the following command uses `sgml-mode' as | |
380 | the parent, and then sets the variable `case-fold-search' to nil: | |
381 | ||
382 | (define-derived-mode article-mode sgml-mode \"Article\" | |
383 | \"Major mode for editing technical articles.\" | |
384 | (setq case-fold-search nil)) | |
385 | ||
386 | Note that if the documentation string had been left out, it would have | |
387 | been generated automatically, with a reference to the keymap." | |
388 | ||
5a7a545c SM |
389 | (let* ((child-name (symbol-name child)) |
390 | (map (intern (concat child-name "-map"))) | |
391 | (syntax (intern (concat child-name "-syntax-table"))) | |
392 | (abbrev (intern (concat child-name "-abbrev-table"))) | |
393 | (hook (intern (concat child-name "-hook")))) | |
394 | ||
3837de12 SM |
395 | (unless parent (setq parent 'fundamental-mode)) |
396 | ||
e4ad5f9e SM |
397 | (when (and docstring (not (stringp docstring))) |
398 | ;; DOCSTRING is really the first command and there's no docstring | |
399 | (push docstring body) | |
400 | (setq docstring nil)) | |
401 | ||
402 | (unless (stringp docstring) | |
403 | ;; Use a default docstring. | |
404 | (setq docstring | |
5a7a545c SM |
405 | (format "Major mode derived from `%s' by `define-derived-mode'. |
406 | Inherits all of the parent's attributes, but has its own keymap, | |
407 | abbrev table and syntax table: | |
408 | ||
409 | `%s', `%s' and `%s' | |
410 | ||
e4ad5f9e SM |
411 | which more-or-less shadow %s's corresponding tables." |
412 | parent map syntax abbrev parent))) | |
413 | ||
414 | (unless (string-match (regexp-quote (symbol-name hook)) docstring) | |
415 | ;; Make sure the docstring mentions the mode's hook | |
c8c21615 SM |
416 | (setq docstring |
417 | (concat docstring | |
418 | (unless (eq parent 'fundamental-mode) | |
419 | (concat | |
420 | "\nAdditionally to any hooks its parent mode " | |
421 | (if (string-match (regexp-quote (format "`%s'" parent)) | |
422 | docstring) nil | |
423 | (format "`%s' " parent)) | |
424 | "might have run),")) | |
425 | (format "\nThis mode runs `%s' just before exiting." hook)))) | |
5a7a545c | 426 | |
e4ad5f9e SM |
427 | (unless (string-match "\\\\[{[]" docstring) |
428 | ;; And don't forget to put the mode's keymap | |
429 | (setq docstring (concat docstring "\n\\{" (symbol-name map) "}"))) | |
430 | ||
431 | `(progn | |
432 | (defvar ,map (make-sparse-keymap)) | |
433 | (defvar ,syntax (make-char-table 'syntax-table nil)) | |
1328a6df SM |
434 | (defvar ,abbrev) |
435 | (define-abbrev-table ',abbrev nil) | |
3837de12 | 436 | (put ',child 'derived-mode-parent ',parent) |
e4ad5f9e SM |
437 | |
438 | (defun ,child () | |
439 | ,docstring | |
440 | (interactive) | |
5a7a545c | 441 | ; Run the parent. |
c7ea3acc SM |
442 | (combine-run-hooks |
443 | ||
444 | (,parent) | |
5a7a545c | 445 | ; Identify special modes. |
c7ea3acc | 446 | (put ',child 'special (get ',parent 'special)) |
5a7a545c | 447 | ; Identify the child mode. |
c7ea3acc SM |
448 | (setq major-mode ',child) |
449 | (setq mode-name ,name) | |
5a7a545c | 450 | ; Set up maps and tables. |
c7ea3acc SM |
451 | (unless (keymap-parent ,map) |
452 | (set-keymap-parent ,map (current-local-map))) | |
453 | (let ((parent (char-table-parent ,syntax))) | |
454 | (unless (and parent (not (eq parent (standard-syntax-table)))) | |
455 | (set-char-table-parent ,syntax (syntax-table)))) | |
456 | (when local-abbrev-table | |
457 | (mapatoms | |
458 | (lambda (symbol) | |
459 | (or (intern-soft (symbol-name symbol) ,abbrev) | |
460 | (define-abbrev ,abbrev (symbol-name symbol) | |
461 | (symbol-value symbol) (symbol-function symbol)))) | |
462 | local-abbrev-table)) | |
5a7a545c | 463 | |
c7ea3acc SM |
464 | (use-local-map ,map) |
465 | (set-syntax-table ,syntax) | |
466 | (setq local-abbrev-table ,abbrev) | |
5a7a545c | 467 | ; Splice in the body (if any). |
c7ea3acc | 468 | ,@body) |
5a7a545c | 469 | ; Run the hooks, if any. |
e4ad5f9e SM |
470 | (run-hooks ',hook))))) |
471 | ||
3837de12 SM |
472 | ;; Inspired from derived-mode-class in derived.el |
473 | (defun easy-mmode-derived-mode-p (mode) | |
474 | "Non-nil if the current major mode is derived from MODE. | |
475 | Uses the `derived-mode-parent' property of the symbol to trace backwards." | |
476 | (let ((parent major-mode)) | |
477 | (while (and (not (eq parent mode)) | |
478 | (setq parent (get parent 'derived-mode-parent)))) | |
479 | parent)) | |
480 | ||
c7ea3acc SM |
481 | \f |
482 | ;;; | |
483 | ;;; easy-mmode-define-navigation | |
484 | ;;; | |
485 | ||
486 | (defmacro easy-mmode-define-navigation (base re &optional name endfun) | |
487 | "Define BASE-next and BASE-prev to navigate in the buffer. | |
488 | RE determines the places the commands should move point to. | |
489 | NAME should describe the entities matched by RE and is used to build | |
490 | the docstrings of the two functions. | |
491 | BASE-next also tries to make sure that the whole entry is visible by | |
492 | searching for its end (by calling ENDFUN if provided or by looking for | |
493 | the next entry) and recentering if necessary. | |
494 | ENDFUN should return the end position (with or without moving point)." | |
495 | (let* ((base-name (symbol-name base)) | |
496 | (prev-sym (intern (concat base-name "-prev"))) | |
497 | (next-sym (intern (concat base-name "-next")))) | |
36a5b60e | 498 | (unless name (setq name (symbol-name base-name))) |
c7ea3acc | 499 | `(progn |
b5bbbb76 SM |
500 | (add-to-list 'debug-ignored-errors |
501 | ,(concat "^No \\(previous\\|next\\) " (regexp-quote name))) | |
c7ea3acc | 502 | (defun ,next-sym (&optional count) |
36a5b60e | 503 | ,(format "Go to the next COUNT'th %s." name) |
c7ea3acc SM |
504 | (interactive) |
505 | (unless count (setq count 1)) | |
506 | (if (< count 0) (,prev-sym (- count)) | |
507 | (if (looking-at ,re) (incf count)) | |
36a5b60e | 508 | (unless (re-search-forward ,re nil t count) |
c8c21615 | 509 | (error ,(format "No next %s" name))) |
c7ea3acc SM |
510 | (goto-char (match-beginning 0)) |
511 | (when (eq (current-buffer) (window-buffer (selected-window))) | |
512 | (let ((endpt (or (save-excursion | |
513 | ,(if endfun `(,endfun) | |
514 | `(re-search-forward ,re nil t 2))) | |
515 | (point-max)))) | |
516 | (unless (<= endpt (window-end)) (recenter)))))) | |
517 | (defun ,prev-sym (&optional count) | |
518 | ,(format "Go to the previous COUNT'th %s" (or name base-name)) | |
519 | (interactive) | |
520 | (unless count (setq count 1)) | |
521 | (if (< count 0) (,next-sym (- count)) | |
36a5b60e | 522 | (unless (re-search-backward ,re nil t count) |
c8c21615 | 523 | (error ,(format "No previous %s" name)))))))) |
5a7a545c | 524 | |
6b279740 RS |
525 | (provide 'easy-mmode) |
526 | ||
527 | ;;; easy-mmode.el ends here |