Add 2010 to copyright years.
[bpt/emacs.git] / lisp / emacs-lisp / easy-mmode.el
CommitLineData
e8af40ee 1;;; easy-mmode.el --- easy definition for major and minor modes
6b279740 2
117fdd32 3;; Copyright (C) 1997, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
114f9c96 4;; 2008, 2009, 2010 Free Software Foundation, Inc.
6b279740 5
9781053a
PJ
6;; Author: Georges Brun-Cottan <Georges.Brun-Cottan@inria.fr>
7;; Maintainer: Stefan Monnier <monnier@gnu.org>
8
9;; Keywords: extensions lisp
6b279740
RS
10
11;; This file is part of GNU Emacs.
12
d6cba7ae 13;; GNU Emacs is free software: you can redistribute it and/or modify
6b279740 14;; it under the terms of the GNU General Public License as published by
d6cba7ae
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
6b279740
RS
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
d6cba7ae 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
6b279740
RS
25
26;;; Commentary:
27
28;; Minor modes are useful and common. This package makes defining a
29;; minor mode easy, by focusing on the writing of the minor mode
30;; functionalities themselves. Moreover, this package enforces a
31;; conventional naming of user interface primitives, making things
32;; natural for the minor-mode end-users.
33
34;; For each mode, easy-mmode defines the following:
35;; <mode> : The minor mode predicate. A buffer-local variable.
36;; <mode>-map : The keymap possibly associated to <mode>.
c8c21615 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.
e6469973
EZ
58If provided, LIGHTER will be used to help choose capitalization by,
59replacing its case-insensitive matches with the literal string in LIGHTER."
b5bbbb76 60 (let* ((case-fold-search t)
906aee93 61 ;; Produce "Foo-Bar minor mode" from foo-bar-minor-mode.
b643ec53 62 (name (concat (replace-regexp-in-string
906aee93
EZ
63 ;; If the original mode name included "-minor" (some
64 ;; of them don't, e.g. auto-revert-mode), then
65 ;; replace it with " minor".
b643ec53 66 "-Minor" " minor"
e6469973 67 ;; "foo-bar-minor" -> "Foo-Bar-Minor"
b643ec53 68 (capitalize (replace-regexp-in-string
e6469973 69 ;; "foo-bar-minor-mode" -> "foo-bar-minor"
b643ec53 70 "-mode\\'" "" (symbol-name mode))))
b5bbbb76
SM
71 " mode")))
72 (if (not (stringp lighter)) name
e6469973
EZ
73 ;; Strip leading and trailing whitespace from LIGHTER.
74 (setq lighter (replace-regexp-in-string "\\`\\s-+\\|\\s-+\\'" ""
75 lighter))
76 ;; Replace any (case-insensitive) matches for LIGHTER in NAME
77 ;; with a literal LIGHTER. E.g., if NAME is "Iimage mode" and
78 ;; LIGHTER is " iImag", then this will produce "iImage mode".
79 ;; (LIGHTER normally comes from the mode-line string passed to
80 ;; define-minor-mode, and normally includes at least one leading
81 ;; space.)
82 (replace-regexp-in-string (regexp-quote lighter) lighter name t t))))
3837de12 83
6b279740 84;;;###autoload
29cc3b84
SM
85(defalias 'easy-mmode-define-minor-mode 'define-minor-mode)
86;;;###autoload
87(defmacro define-minor-mode (mode doc &optional init-value lighter keymap &rest body)
6b279740 88 "Define a new minor mode MODE.
b5bbbb76 89This function defines the associated control variable MODE, keymap MODE-map,
f5678943 90and toggle command MODE.
6b279740
RS
91
92DOC is the documentation for the mode toggle command.
29cc3b84 93Optional INIT-VALUE is the initial value of the mode's variable.
c8c21615 94Optional LIGHTER is displayed in the modeline when the mode is on.
6b279740 95Optional KEYMAP is the default (defvar) keymap bound to the mode keymap.
b5bbbb76 96 If it is a list, it is passed to `easy-mmode-define-keymap'
bff53411
SM
97 in order to build a valid keymap. It's generally better to use
98 a separate MODE-map variable than to use this argument.
99The above three arguments can be skipped if keyword arguments are
100used (see below).
101
10944042 102BODY contains code to execute each time the mode is activated or deactivated.
b50b95ce 103 It is executed after toggling the mode,
96053020 104 and before running the hook variable `MODE-hook'.
f5678943
LK
105 Before the actual body code, you can write keyword arguments (alternating
106 keywords and values). These following keyword arguments are supported (other
107 keywords will be passed to `defcustom' if the minor mode is global):
a6ce6869 108:group GROUP Custom group name to use in all generated `defcustom' forms.
ab7bc290 109 Defaults to MODE without the possible trailing \"-mode\".
c25eec81
LK
110 Don't use this default group name unless you have written a
111 `defgroup' to define that group properly.
c8fb3bf9 112:global GLOBAL If non-nil specifies that the minor mode is not meant to be
ab7bc290 113 buffer-local, so don't make the variable MODE buffer-local.
a6ce6869 114 By default, the mode is buffer-local.
c8fb3bf9
SM
115:init-value VAL Same as the INIT-VALUE argument.
116:lighter SPEC Same as the LIGHTER argument.
2e2a0075 117:keymap MAP Same as the KEYMAP argument.
a6ce6869
RS
118:require SYM Same as in `defcustom'.
119
120For example, you could write
121 (define-minor-mode foo-mode \"If enabled, foo on you!\"
73ceba9f 122 :lighter \" Foo\" :require 'foo :global t :group 'hassle :version \"27.5\"
a6ce6869 123 ...BODY CODE...)"
2e2a0075
SM
124 (declare (debug (&define name stringp
125 [&optional [&not keywordp] sexp
126 &optional [&not keywordp] sexp
127 &optional [&not keywordp] sexp]
128 [&rest [keywordp sexp]]
129 def-body)))
a6ce6869 130
bff53411
SM
131 ;; Allow skipping the first three args.
132 (cond
133 ((keywordp init-value)
134 (setq body (list* init-value lighter keymap body)
135 init-value nil lighter nil keymap nil))
136 ((keywordp lighter)
137 (setq body (list* lighter keymap body) lighter nil keymap nil))
138 ((keywordp keymap) (push keymap body) (setq keymap nil)))
139
dae157b7
SM
140 (let* ((last-message (make-symbol "last-message"))
141 (mode-name (symbol-name mode))
b5bbbb76 142 (pretty-name (easy-mmode-pretty-mode-name mode lighter))
c8c21615 143 (globalp nil)
fceb44d2 144 (set nil)
c736d6cf 145 (initialize nil)
0a74e3bf 146 (group nil)
fceb44d2 147 (type nil)
0a74e3bf 148 (extra-args nil)
73ceba9f 149 (extra-keywords nil)
c8fb3bf9 150 (require t)
b5bbbb76
SM
151 (hook (intern (concat mode-name "-hook")))
152 (hook-on (intern (concat mode-name "-on-hook")))
73ceba9f 153 (hook-off (intern (concat mode-name "-off-hook")))
2e2a0075 154 keyw keymap-sym)
b5bbbb76 155
b5bbbb76 156 ;; Check keys.
73ceba9f
SM
157 (while (keywordp (setq keyw (car body)))
158 (setq body (cdr body))
159 (case keyw
bff53411 160 (:init-value (setq init-value (pop body)))
8b908da6 161 (:lighter (setq lighter (purecopy (pop body))))
be22f4cc 162 (:global (setq globalp (pop body)))
0a74e3bf 163 (:extra-args (setq extra-args (pop body)))
fceb44d2 164 (:set (setq set (list :set (pop body))))
c736d6cf 165 (:initialize (setq initialize (list :initialize (pop body))))
0a74e3bf 166 (:group (setq group (nconc group (list :group (pop body)))))
fceb44d2 167 (:type (setq type (list :type (pop body))))
c8fb3bf9 168 (:require (setq require (pop body)))
2e2a0075 169 (:keymap (setq keymap (pop body)))
73ceba9f 170 (t (push keyw extra-keywords) (push (pop body) extra-keywords))))
eab6e8b9 171
2e2a0075
SM
172 (setq keymap-sym (if (and keymap (symbolp keymap)) keymap
173 (intern (concat mode-name "-map"))))
174
fceb44d2
LT
175 (unless set (setq set '(:set 'custom-set-minor-mode)))
176
c736d6cf 177 (unless initialize
fceb44d2 178 (setq initialize '(:initialize 'custom-initialize-default)))
c736d6cf 179
0a74e3bf
SM
180 (unless group
181 ;; We might as well provide a best-guess default group.
182 (setq group
ab7bc290
LK
183 `(:group ',(intern (replace-regexp-in-string
184 "-mode\\'" "" mode-name)))))
7fb80935 185
fceb44d2
LT
186 (unless type (setq type '(:type 'boolean)))
187
6b279740 188 `(progn
5e21ef7a 189 ;; Define the variable to enable or disable the mode.
d5b037c5
SM
190 ,(if (not globalp)
191 `(progn
192 (defvar ,mode ,init-value ,(format "Non-nil if %s is enabled.
a2ed9670 193Use the command `%s' to change this variable." pretty-name mode))
d5b037c5 194 (make-variable-buffer-local ',mode))
6b279740 195
94dfee0b
SM
196 (let ((base-doc-string
197 (concat "Non-nil if %s is enabled.
7d5e5e70 198See the command `%s' for a description of this minor mode."
94dfee0b 199 (if body "
d5b037c5 200Setting this variable directly does not take effect;
da506c0e
RS
201either customize it (see the info node `Easy Customization')
202or call the function `%s'."))))
a566ce8e
RS
203 `(defcustom ,mode ,init-value
204 ,(format base-doc-string pretty-name mode mode)
fceb44d2 205 ,@set
c736d6cf 206 ,@initialize
0a74e3bf 207 ,@group
fceb44d2 208 ,@type
94dfee0b
SM
209 ,@(unless (eq require t) `(:require ,require))
210 ,@(nreverse extra-keywords))))
1328a6df 211
b5bbbb76 212 ;; The actual function.
0a74e3bf 213 (defun ,mode (&optional arg ,@extra-args)
b5bbbb76 214 ,(or doc
bff53411
SM
215 (format (concat "Toggle %s on or off.
216Interactively, with no prefix argument, toggle the mode.
5ddfa187 217With universal prefix ARG turn mode on.
b5bbbb76 218With zero or negative ARG turn mode off.
bff53411 219\\{%s}") pretty-name keymap-sym))
5ddfa187
SM
220 ;; Use `toggle' rather than (if ,mode 0 1) so that using
221 ;; repeat-command still does the toggling correctly.
222 (interactive (list (or current-prefix-arg 'toggle)))
dae157b7
SM
223 (let ((,last-message (current-message)))
224 (setq ,mode
225 (cond
226 ((eq arg 'toggle) (not ,mode))
227 (arg (> (prefix-numeric-value arg) 0))
228 (t
229 (if (null ,mode) t
230 (message
231 "Toggling %s off; better pass an explicit argument."
232 ',mode)
233 nil))))
234 ,@body
235 ;; The on/off hooks are here for backward compatibility only.
236 (run-hooks ',hook (if ,mode ',hook-on ',hook-off))
12a3c28c 237 (if (called-interactively-p 'any)
dae157b7
SM
238 (progn
239 ,(if globalp `(customize-mark-as-set ',mode))
240 ;; Avoid overwriting a message shown by the body,
241 ;; but do overwrite previous messages.
242 (unless (and (current-message)
243 (not (equal ,last-message
244 (current-message))))
245 (message ,(format "%s %%sabled" pretty-name)
246 (if ,mode "en" "dis"))))))
bff53411 247 (force-mode-line-update)
d99d3266 248 ;; Return the new setting.
b5bbbb76 249 ,mode)
2e2a0075 250
ab7bc290
LK
251 ;; Autoloading a define-minor-mode autoloads everything
252 ;; up-to-here.
1328a6df
SM
253 :autoload-end
254
d5b037c5 255 ;; Define the minor-mode keymap.
1328a6df 256 ,(unless (symbolp keymap) ;nil is also a symbol.
d5b037c5 257 `(defvar ,keymap-sym
1328a6df
SM
258 (let ((m ,keymap))
259 (cond ((keymapp m) m)
260 ((listp m) (easy-mmode-define-keymap m))
261 (t (error "Invalid keymap %S" ,keymap))))
d5b037c5
SM
262 ,(format "Keymap for `%s'." mode-name)))
263
b5bbbb76 264 (add-minor-mode ',mode ',lighter
1328a6df 265 ,(if keymap keymap-sym
5ca4661e 266 `(if (boundp ',keymap-sym) ,keymap-sym))))))
5a7a545c 267\f
be22f4cc
SM
268;;;
269;;; make global minor mode
270;;;
271
d5b037c5 272;;;###autoload
275e4f4c 273(defalias 'easy-mmode-define-global-mode 'define-globalized-minor-mode)
39a27f95 274;;;###autoload
275e4f4c
CY
275(defalias 'define-global-minor-mode 'define-globalized-minor-mode)
276;;;###autoload
277(defmacro define-globalized-minor-mode (global-mode mode turn-on &rest keys)
9f729dab 278 "Make a global mode GLOBAL-MODE corresponding to buffer-local minor MODE.
be22f4cc
SM
279TURN-ON is a function that will be called with no args in every buffer
280 and that should try to turn MODE on if applicable for that buffer.
0ceed14b
LT
281KEYS is a list of CL-style keyword arguments. As the minor mode
282 defined by this function is always global, any :global keyword is
283 ignored. Other keywords have the same meaning as in `define-minor-mode',
284 which see. In particular, :group specifies the custom group.
285 The most useful keywords are those that are passed on to the
286 `defcustom'. It normally makes no sense to pass the :lighter
275e4f4c 287 or :keymap keywords to `define-globalized-minor-mode', since these
0ceed14b 288 are usually passed to the buffer-local version of the minor mode.
876daebc
LT
289
290If MODE's set-up depends on the major mode in effect when it was
291enabled, then disabling and reenabling MODE should make MODE work
292correctly with the current major mode. This is important to
293prevent problems with derived modes, that is, major modes that
294call another major mode in their body."
295
0a74e3bf 296 (let* ((global-mode-name (symbol-name global-mode))
be22f4cc
SM
297 (pretty-name (easy-mmode-pretty-mode-name mode))
298 (pretty-global-name (easy-mmode-pretty-mode-name global-mode))
0a74e3bf 299 (group nil)
0ceed14b 300 (extra-keywords nil)
876daebc
LT
301 (MODE-buffers (intern (concat global-mode-name "-buffers")))
302 (MODE-enable-in-buffers
303 (intern (concat global-mode-name "-enable-in-buffers")))
304 (MODE-check-buffers
305 (intern (concat global-mode-name "-check-buffers")))
306 (MODE-cmhh (intern (concat global-mode-name "-cmhh")))
0ceed14b
LT
307 (MODE-major-mode (intern (concat (symbol-name mode) "-major-mode")))
308 keyw)
be22f4cc
SM
309
310 ;; Check keys.
0ceed14b
LT
311 (while (keywordp (setq keyw (car keys)))
312 (setq keys (cdr keys))
313 (case keyw
0a74e3bf 314 (:group (setq group (nconc group (list :group (pop keys)))))
0ceed14b
LT
315 (:global (setq keys (cdr keys)))
316 (t (push keyw extra-keywords) (push (pop keys) extra-keywords))))
be22f4cc 317
0a74e3bf
SM
318 (unless group
319 ;; We might as well provide a best-guess default group.
320 (setq group
ab7bc290
LK
321 `(:group ',(intern (replace-regexp-in-string
322 "-mode\\'" "" (symbol-name mode))))))
dce88ea6 323
be22f4cc 324 `(progn
876daebc
LT
325 (defvar ,MODE-major-mode nil)
326 (make-variable-buffer-local ',MODE-major-mode)
be22f4cc
SM
327 ;; The actual global minor-mode
328 (define-minor-mode ,global-mode
af414f10
EZ
329 ;; Very short lines to avoid too long lines in the generated
330 ;; doc string.
44395dee 331 ,(format "Toggle %s in every possible buffer.
af414f10
EZ
332With prefix ARG, turn %s on if and only if
333ARG is positive.
334%s is enabled in all buffers where
335\`%s' would do it.
44395dee
RS
336See `%s' for more information on %s."
337 pretty-name pretty-global-name pretty-name turn-on
338 mode pretty-name)
0ceed14b 339 :global t ,@group ,@(nreverse extra-keywords)
be22f4cc
SM
340
341 ;; Setup hook to handle future mode changes and new buffers.
342 (if ,global-mode
d5b037c5 343 (progn
876daebc
LT
344 (add-hook 'after-change-major-mode-hook
345 ',MODE-enable-in-buffers)
346 (add-hook 'find-file-hook ',MODE-check-buffers)
347 (add-hook 'change-major-mode-hook ',MODE-cmhh))
348 (remove-hook 'after-change-major-mode-hook ',MODE-enable-in-buffers)
349 (remove-hook 'find-file-hook ',MODE-check-buffers)
350 (remove-hook 'change-major-mode-hook ',MODE-cmhh))
be22f4cc
SM
351
352 ;; Go through existing buffers.
353 (dolist (buf (buffer-list))
354 (with-current-buffer buf
34befa9a 355 (if ,global-mode (,turn-on) (when ,mode (,mode -1))))))
be22f4cc 356
275e4f4c 357 ;; Autoloading define-globalized-minor-mode autoloads everything
f5678943 358 ;; up-to-here.
1328a6df
SM
359 :autoload-end
360
be22f4cc 361 ;; List of buffers left to process.
876daebc 362 (defvar ,MODE-buffers nil)
be22f4cc
SM
363
364 ;; The function that calls TURN-ON in each buffer.
876daebc
LT
365 (defun ,MODE-enable-in-buffers ()
366 (dolist (buf ,MODE-buffers)
367 (when (buffer-live-p buf)
368 (with-current-buffer buf
369 (if ,mode
370 (unless (eq ,MODE-major-mode major-mode)
371 (,mode -1)
372 (,turn-on)
373 (setq ,MODE-major-mode major-mode))
374 (,turn-on)
375 (setq ,MODE-major-mode major-mode))))))
376 (put ',MODE-enable-in-buffers 'definition-name ',global-mode)
377
378 (defun ,MODE-check-buffers ()
379 (,MODE-enable-in-buffers)
380 (setq ,MODE-buffers nil)
381 (remove-hook 'post-command-hook ',MODE-check-buffers))
382 (put ',MODE-check-buffers 'definition-name ',global-mode)
be22f4cc
SM
383
384 ;; The function that catches kill-all-local-variables.
876daebc
LT
385 (defun ,MODE-cmhh ()
386 (add-to-list ',MODE-buffers (current-buffer))
387 (add-hook 'post-command-hook ',MODE-check-buffers))
388 (put ',MODE-cmhh 'definition-name ',global-mode))))
be22f4cc 389
5a7a545c
SM
390;;;
391;;; easy-mmode-defmap
392;;;
393
210c6549
GM
394(eval-and-compile
395 (if (fboundp 'set-keymap-parents)
396 (defalias 'easy-mmode-set-keymap-parents 'set-keymap-parents)
397 (defun easy-mmode-set-keymap-parents (m parents)
398 (set-keymap-parent
399 m
400 (cond
401 ((not (consp parents)) parents)
402 ((not (cdr parents)) (car parents))
403 (t (let ((m (copy-keymap (pop parents))))
404 (easy-mmode-set-keymap-parents m parents)
405 m)))))))
5a7a545c 406
5d78d57d 407;;;###autoload
5a7a545c
SM
408(defun easy-mmode-define-keymap (bs &optional name m args)
409 "Return a keymap built from bindings BS.
410BS must be a list of (KEY . BINDING) where
3837de12
SM
411KEY and BINDINGS are suitable for `define-key'.
412Optional NAME is passed to `make-sparse-keymap'.
413Optional map M can be used to modify an existing map.
38a48ab7
GM
414ARGS is a list of additional keyword arguments.
415
416Valid keywords and arguments are:
417
418 :name Name of the keymap; overrides NAME argument.
419 :dense Non-nil for a dense keymap.
420 :inherit Parent keymap.
421 :group Ignored.
422 :suppress Non-nil to call `suppress-keymap' on keymap,
423 'nodigits to suppress digits as prefix arguments."
424 (let (inherit dense suppress)
5a7a545c
SM
425 (while args
426 (let ((key (pop args))
427 (val (pop args)))
be22f4cc 428 (case key
165958d2 429 (:name (setq name val))
be22f4cc
SM
430 (:dense (setq dense val))
431 (:inherit (setq inherit val))
38a48ab7 432 (:suppress (setq suppress val))
be22f4cc 433 (:group)
5a7a545c
SM
434 (t (message "Unknown argument %s in defmap" key)))))
435 (unless (keymapp m)
436 (setq bs (append m bs))
437 (setq m (if dense (make-keymap name) (make-sparse-keymap name))))
38a48ab7
GM
438 (when suppress
439 (suppress-keymap m (eq suppress 'nodigits)))
5a7a545c
SM
440 (dolist (b bs)
441 (let ((keys (car b))
442 (binding (cdr b)))
443 (dolist (key (if (consp keys) keys (list keys)))
444 (cond
445 ((symbolp key)
446 (substitute-key-definition key binding m global-map))
447 ((null binding)
448 (unless (keymapp (lookup-key m key)) (define-key m key binding)))
449 ((let ((o (lookup-key m key)))
450 (or (null o) (numberp o) (eq o 'undefined)))
451 (define-key m key binding))))))
452 (cond
453 ((keymapp inherit) (set-keymap-parent m inherit))
454 ((consp inherit) (easy-mmode-set-keymap-parents m inherit)))
455 m))
456
457;;;###autoload
458(defmacro easy-mmode-defmap (m bs doc &rest args)
117fdd32
GM
459 "Define a constant M whose value is the result of `easy-mmode-define-keymap'.
460The M, BS, and ARGS arguments are as per that function. DOC is
461the constant's documentation."
5d78d57d
SM
462 `(defconst ,m
463 (easy-mmode-define-keymap ,bs nil (if (boundp ',m) ,m) ,(cons 'list args))
464 ,doc))
5a7a545c
SM
465
466\f
467;;;
468;;; easy-mmode-defsyntax
469;;;
470
471(defun easy-mmode-define-syntax (css args)
e4fe3460
SM
472 (let ((st (make-syntax-table (plist-get args :copy)))
473 (parent (plist-get args :inherit)))
5a7a545c
SM
474 (dolist (cs css)
475 (let ((char (car cs))
476 (syntax (cdr cs)))
477 (if (sequencep char)
9b97ee2e 478 (mapc (lambda (c) (modify-syntax-entry c syntax st)) char)
5a7a545c 479 (modify-syntax-entry char syntax st))))
e4fe3460
SM
480 (if parent (set-char-table-parent
481 st (if (symbolp parent) (symbol-value parent) parent)))
5a7a545c
SM
482 st))
483
484;;;###autoload
485(defmacro easy-mmode-defsyntax (st css doc &rest args)
e4fe3460 486 "Define variable ST as a syntax-table.
2a83a11d 487CSS contains a list of syntax specifications of the form (CHAR . SYNTAX)."
e4ad5f9e
SM
488 `(progn
489 (autoload 'easy-mmode-define-syntax "easy-mmode")
2a83a11d 490 (defconst ,st (easy-mmode-define-syntax ,css ,(cons 'list args)) ,doc)))
5a7a545c
SM
491
492
493\f
c7ea3acc
SM
494;;;
495;;; easy-mmode-define-navigation
496;;;
497
cc349341
SM
498(defmacro easy-mmode-define-navigation (base re &optional name endfun narrowfun
499 &rest body)
c7ea3acc
SM
500 "Define BASE-next and BASE-prev to navigate in the buffer.
501RE determines the places the commands should move point to.
eed083e6 502NAME should describe the entities matched by RE. It is used to build
c7ea3acc
SM
503 the docstrings of the two functions.
504BASE-next also tries to make sure that the whole entry is visible by
505 searching for its end (by calling ENDFUN if provided or by looking for
506 the next entry) and recentering if necessary.
877f9b05
TTN
507ENDFUN should return the end position (with or without moving point).
508NARROWFUN non-nil means to check for narrowing before moving, and if
cc349341
SM
509found, do `widen' first and then call NARROWFUN with no args after moving.
510BODY is executed after moving to the destination location."
511 (declare (indent 5) (debug (exp exp exp def-form def-form &rest def-body)))
c7ea3acc
SM
512 (let* ((base-name (symbol-name base))
513 (prev-sym (intern (concat base-name "-prev")))
877f9b05 514 (next-sym (intern (concat base-name "-next")))
cc349341
SM
515 (when-narrowed
516 (lambda (body)
517 (if (null narrowfun) body
518 `(let ((was-narrowed
519 (prog1 (or (< (- (point-max) (point-min)) (buffer-size)))
520 (widen))))
521 ,body
522 (when was-narrowed (,narrowfun)))))))
8fd9bef2 523 (unless name (setq name base-name))
c7ea3acc 524 `(progn
b5bbbb76
SM
525 (add-to-list 'debug-ignored-errors
526 ,(concat "^No \\(previous\\|next\\) " (regexp-quote name)))
c7ea3acc 527 (defun ,next-sym (&optional count)
36a5b60e 528 ,(format "Go to the next COUNT'th %s." name)
9e288f75 529 (interactive "p")
c7ea3acc
SM
530 (unless count (setq count 1))
531 (if (< count 0) (,prev-sym (- count))
6c119ac0 532 (if (looking-at ,re) (setq count (1+ count)))
cc349341
SM
533 ,(funcall when-narrowed
534 `(if (not (re-search-forward ,re nil t count))
535 (if (looking-at ,re)
536 (goto-char (or ,(if endfun `(,endfun)) (point-max)))
537 (error "No next %s" ,name))
538 (goto-char (match-beginning 0))
539 (when (and (eq (current-buffer) (window-buffer (selected-window)))
32226619 540 (called-interactively-p 'interactive))
cc349341
SM
541 (let ((endpt (or (save-excursion
542 ,(if endfun `(,endfun)
543 `(re-search-forward ,re nil t 2)))
544 (point-max))))
545 (unless (pos-visible-in-window-p endpt nil t)
546 (recenter '(0)))))))
547 ,@body))
d5ba8197 548 (put ',next-sym 'definition-name ',base)
c7ea3acc
SM
549 (defun ,prev-sym (&optional count)
550 ,(format "Go to the previous COUNT'th %s" (or name base-name))
9e288f75 551 (interactive "p")
c7ea3acc
SM
552 (unless count (setq count 1))
553 (if (< count 0) (,next-sym (- count))
cc349341
SM
554 ,(funcall when-narrowed
555 `(unless (re-search-backward ,re nil t count)
556 (error "No previous %s" ,name)))
557 ,@body))
d5ba8197 558 (put ',prev-sym 'definition-name ',base))))
877f9b05 559
5a7a545c 560
6b279740
RS
561(provide 'easy-mmode)
562
dae157b7 563;; arch-tag: d48a5250-6961-4528-9cb0-3c9ea042a66a
6b279740 564;;; easy-mmode.el ends here