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