Fix minor mode docstrings for the new meaning of a nil ARG.
[bpt/emacs.git] / lisp / emacs-lisp / easy-mmode.el
CommitLineData
e8af40ee 1;;; easy-mmode.el --- easy definition for major and minor modes
6b279740 2
73b0cd50 3;; Copyright (C) 1997, 2000-2011 Free Software Foundation, Inc.
6b279740 4
9781053a
PJ
5;; Author: Georges Brun-Cottan <Georges.Brun-Cottan@inria.fr>
6;; Maintainer: Stefan Monnier <monnier@gnu.org>
bd78fa1d 7;; Package: emacs
9781053a
PJ
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.
bc7d7ea6 89This defines the control variable MODE and the toggle command MODE.
6b279740 90DOC is the documentation for the mode toggle command.
bc7d7ea6 91
29cc3b84 92Optional INIT-VALUE is the initial value of the mode's variable.
c8c21615 93Optional LIGHTER is displayed in the modeline when the mode is on.
bc7d7ea6
CY
94Optional KEYMAP is the default keymap bound to the mode keymap.
95 If non-nil, it should be a variable name (whose value is a keymap),
1a1fcbe1
SM
96 or an expression that returns either a keymap or a list of
97 arguments for `easy-mmode-define-keymap'. If KEYMAP is not a symbol,
98 this also defines the variable MODE-map.
bc7d7ea6
CY
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
9d794026
GM
122 a new state and sets it. If you specify a :variable, this
123 function assumes it is defined elsewhere.
a6ce6869
RS
124
125For example, you could write
126 (define-minor-mode foo-mode \"If enabled, foo on you!\"
73ceba9f 127 :lighter \" Foo\" :require 'foo :global t :group 'hassle :version \"27.5\"
a6ce6869 128 ...BODY CODE...)"
2e2a0075
SM
129 (declare (debug (&define name stringp
130 [&optional [&not keywordp] sexp
131 &optional [&not keywordp] sexp
132 &optional [&not keywordp] sexp]
133 [&rest [keywordp sexp]]
134 def-body)))
a6ce6869 135
bff53411
SM
136 ;; Allow skipping the first three args.
137 (cond
138 ((keywordp init-value)
139 (setq body (list* init-value lighter keymap body)
140 init-value nil lighter nil keymap nil))
141 ((keywordp lighter)
142 (setq body (list* lighter keymap body) lighter nil keymap nil))
143 ((keywordp keymap) (push keymap body) (setq keymap nil)))
144
dae157b7
SM
145 (let* ((last-message (make-symbol "last-message"))
146 (mode-name (symbol-name mode))
b5bbbb76 147 (pretty-name (easy-mmode-pretty-mode-name mode lighter))
c8c21615 148 (globalp nil)
fceb44d2 149 (set nil)
c736d6cf 150 (initialize nil)
0a74e3bf 151 (group nil)
fceb44d2 152 (type nil)
0a74e3bf 153 (extra-args nil)
73ceba9f 154 (extra-keywords nil)
0c495c21
SM
155 (variable nil) ;The PLACE where the state is stored.
156 (setter nil) ;The function (if any) to set the mode var.
157 (modefun mode) ;The minor mode function name we're defining.
c8fb3bf9 158 (require t)
b5bbbb76
SM
159 (hook (intern (concat mode-name "-hook")))
160 (hook-on (intern (concat mode-name "-on-hook")))
73ceba9f 161 (hook-off (intern (concat mode-name "-off-hook")))
2e2a0075 162 keyw keymap-sym)
b5bbbb76 163
b5bbbb76 164 ;; Check keys.
73ceba9f
SM
165 (while (keywordp (setq keyw (car body)))
166 (setq body (cdr body))
167 (case keyw
bff53411 168 (:init-value (setq init-value (pop body)))
8b908da6 169 (:lighter (setq lighter (purecopy (pop body))))
be22f4cc 170 (:global (setq globalp (pop body)))
0a74e3bf 171 (:extra-args (setq extra-args (pop body)))
fceb44d2 172 (:set (setq set (list :set (pop body))))
c736d6cf 173 (:initialize (setq initialize (list :initialize (pop body))))
0a74e3bf 174 (:group (setq group (nconc group (list :group (pop body)))))
fceb44d2 175 (:type (setq type (list :type (pop body))))
c8fb3bf9 176 (:require (setq require (pop body)))
2e2a0075 177 (:keymap (setq keymap (pop body)))
0c495c21
SM
178 (:variable (setq variable (pop body))
179 (if (not (functionp (cdr-safe variable)))
180 ;; PLACE is not of the form (GET . SET).
181 (setq mode variable)
182 (setq mode (car variable))
183 (setq setter (cdr variable))))
73ceba9f 184 (t (push keyw extra-keywords) (push (pop body) extra-keywords))))
eab6e8b9 185
2e2a0075
SM
186 (setq keymap-sym (if (and keymap (symbolp keymap)) keymap
187 (intern (concat mode-name "-map"))))
188
fceb44d2
LT
189 (unless set (setq set '(:set 'custom-set-minor-mode)))
190
c736d6cf 191 (unless initialize
fceb44d2 192 (setq initialize '(:initialize 'custom-initialize-default)))
c736d6cf 193
0a74e3bf
SM
194 (unless group
195 ;; We might as well provide a best-guess default group.
196 (setq group
ab7bc290
LK
197 `(:group ',(intern (replace-regexp-in-string
198 "-mode\\'" "" mode-name)))))
7fb80935 199
9d794026 200 ;; TODO? Mark booleans as safe if booleanp? Eg abbrev-mode.
fceb44d2
LT
201 (unless type (setq type '(:type 'boolean)))
202
6b279740 203 `(progn
5e21ef7a 204 ;; Define the variable to enable or disable the mode.
f44379e7
SM
205 ,(cond
206 ;; If :variable is specified, then the var will be
207 ;; declared elsewhere.
208 (variable nil)
209 ((not globalp)
210 `(progn
211 (defvar ,mode ,init-value ,(format "Non-nil if %s is enabled.
a2ed9670 212Use the command `%s' to change this variable." pretty-name mode))
f44379e7
SM
213 (make-variable-buffer-local ',mode)))
214 (t
94dfee0b
SM
215 (let ((base-doc-string
216 (concat "Non-nil if %s is enabled.
7d5e5e70 217See the command `%s' for a description of this minor mode."
94dfee0b 218 (if body "
d5b037c5 219Setting this variable directly does not take effect;
da506c0e
RS
220either customize it (see the info node `Easy Customization')
221or call the function `%s'."))))
a566ce8e
RS
222 `(defcustom ,mode ,init-value
223 ,(format base-doc-string pretty-name mode mode)
fceb44d2 224 ,@set
c736d6cf 225 ,@initialize
0a74e3bf 226 ,@group
fceb44d2 227 ,@type
94dfee0b 228 ,@(unless (eq require t) `(:require ,require))
f44379e7 229 ,@(nreverse extra-keywords)))))
1328a6df 230
b5bbbb76 231 ;; The actual function.
f44379e7 232 (defun ,modefun (&optional arg ,@extra-args)
b5bbbb76 233 ,(or doc
bff53411
SM
234 (format (concat "Toggle %s on or off.
235Interactively, with no prefix argument, toggle the mode.
5ddfa187 236With universal prefix ARG turn mode on.
b5bbbb76 237With zero or negative ARG turn mode off.
bff53411 238\\{%s}") pretty-name keymap-sym))
5ddfa187
SM
239 ;; Use `toggle' rather than (if ,mode 0 1) so that using
240 ;; repeat-command still does the toggling correctly.
241 (interactive (list (or current-prefix-arg 'toggle)))
dae157b7 242 (let ((,last-message (current-message)))
0c495c21
SM
243 (,@(if setter (list setter)
244 (list (if (symbolp mode) 'setq 'setf) mode))
f44379e7
SM
245 (if (eq arg 'toggle)
246 (not ,mode)
247 ;; A nil argument also means ON now.
248 (> (prefix-numeric-value arg) 0)))
dae157b7
SM
249 ,@body
250 ;; The on/off hooks are here for backward compatibility only.
251 (run-hooks ',hook (if ,mode ',hook-on ',hook-off))
12a3c28c 252 (if (called-interactively-p 'any)
dae157b7 253 (progn
0c495c21
SM
254 ,(if (and globalp (symbolp mode))
255 `(customize-mark-as-set ',mode))
dae157b7
SM
256 ;; Avoid overwriting a message shown by the body,
257 ;; but do overwrite previous messages.
258 (unless (and (current-message)
259 (not (equal ,last-message
260 (current-message))))
261 (message ,(format "%s %%sabled" pretty-name)
262 (if ,mode "en" "dis"))))))
bff53411 263 (force-mode-line-update)
d99d3266 264 ;; Return the new setting.
b5bbbb76 265 ,mode)
2e2a0075 266
ab7bc290
LK
267 ;; Autoloading a define-minor-mode autoloads everything
268 ;; up-to-here.
1328a6df
SM
269 :autoload-end
270
d5b037c5 271 ;; Define the minor-mode keymap.
1328a6df 272 ,(unless (symbolp keymap) ;nil is also a symbol.
d5b037c5 273 `(defvar ,keymap-sym
1328a6df
SM
274 (let ((m ,keymap))
275 (cond ((keymapp m) m)
276 ((listp m) (easy-mmode-define-keymap m))
1a1fcbe1 277 (t (error "Invalid keymap %S" m))))
d5b037c5
SM
278 ,(format "Keymap for `%s'." mode-name)))
279
0c495c21
SM
280 ,(if (not (symbolp mode))
281 (if (or lighter keymap)
282 (error ":lighter and :keymap unsupported with mode expression %s" mode))
283 `(with-no-warnings
284 (add-minor-mode ',mode ',lighter
f44379e7 285 ,(if keymap keymap-sym
0c495c21
SM
286 `(if (boundp ',keymap-sym) ,keymap-sym))
287 nil
288 ,(unless (eq mode modefun) 'modefun)))))))
5a7a545c 289\f
be22f4cc
SM
290;;;
291;;; make global minor mode
292;;;
293
d5b037c5 294;;;###autoload
275e4f4c 295(defalias 'easy-mmode-define-global-mode 'define-globalized-minor-mode)
39a27f95 296;;;###autoload
275e4f4c
CY
297(defalias 'define-global-minor-mode 'define-globalized-minor-mode)
298;;;###autoload
299(defmacro define-globalized-minor-mode (global-mode mode turn-on &rest keys)
9f729dab 300 "Make a global mode GLOBAL-MODE corresponding to buffer-local minor MODE.
be22f4cc
SM
301TURN-ON is a function that will be called with no args in every buffer
302 and that should try to turn MODE on if applicable for that buffer.
0ceed14b
LT
303KEYS is a list of CL-style keyword arguments. As the minor mode
304 defined by this function is always global, any :global keyword is
305 ignored. Other keywords have the same meaning as in `define-minor-mode',
306 which see. In particular, :group specifies the custom group.
307 The most useful keywords are those that are passed on to the
308 `defcustom'. It normally makes no sense to pass the :lighter
275e4f4c 309 or :keymap keywords to `define-globalized-minor-mode', since these
0ceed14b 310 are usually passed to the buffer-local version of the minor mode.
876daebc
LT
311
312If MODE's set-up depends on the major mode in effect when it was
313enabled, then disabling and reenabling MODE should make MODE work
314correctly with the current major mode. This is important to
315prevent problems with derived modes, that is, major modes that
316call another major mode in their body."
317
0a74e3bf 318 (let* ((global-mode-name (symbol-name global-mode))
be22f4cc
SM
319 (pretty-name (easy-mmode-pretty-mode-name mode))
320 (pretty-global-name (easy-mmode-pretty-mode-name global-mode))
0a74e3bf 321 (group nil)
0ceed14b 322 (extra-keywords nil)
876daebc
LT
323 (MODE-buffers (intern (concat global-mode-name "-buffers")))
324 (MODE-enable-in-buffers
325 (intern (concat global-mode-name "-enable-in-buffers")))
326 (MODE-check-buffers
327 (intern (concat global-mode-name "-check-buffers")))
328 (MODE-cmhh (intern (concat global-mode-name "-cmhh")))
0ceed14b
LT
329 (MODE-major-mode (intern (concat (symbol-name mode) "-major-mode")))
330 keyw)
be22f4cc
SM
331
332 ;; Check keys.
0ceed14b
LT
333 (while (keywordp (setq keyw (car keys)))
334 (setq keys (cdr keys))
335 (case keyw
0a74e3bf 336 (:group (setq group (nconc group (list :group (pop keys)))))
0ceed14b
LT
337 (:global (setq keys (cdr keys)))
338 (t (push keyw extra-keywords) (push (pop keys) extra-keywords))))
be22f4cc 339
0a74e3bf
SM
340 (unless group
341 ;; We might as well provide a best-guess default group.
342 (setq group
ab7bc290
LK
343 `(:group ',(intern (replace-regexp-in-string
344 "-mode\\'" "" (symbol-name mode))))))
dce88ea6 345
be22f4cc 346 `(progn
876daebc
LT
347 (defvar ,MODE-major-mode nil)
348 (make-variable-buffer-local ',MODE-major-mode)
be22f4cc
SM
349 ;; The actual global minor-mode
350 (define-minor-mode ,global-mode
af414f10
EZ
351 ;; Very short lines to avoid too long lines in the generated
352 ;; doc string.
06e21633
CY
353 ,(format "Toggle %s in all buffers.
354With prefix ARG, enable %s if ARG is positive;
355otherwise, disable it. If called from Lisp, enable the mode if
356ARG is omitted or nil.
357
af414f10
EZ
358%s is enabled in all buffers where
359\`%s' would do it.
44395dee 360See `%s' for more information on %s."
06e21633
CY
361 pretty-name pretty-global-name
362 pretty-name turn-on mode pretty-name)
0ceed14b 363 :global t ,@group ,@(nreverse extra-keywords)
be22f4cc
SM
364
365 ;; Setup hook to handle future mode changes and new buffers.
366 (if ,global-mode
d5b037c5 367 (progn
876daebc
LT
368 (add-hook 'after-change-major-mode-hook
369 ',MODE-enable-in-buffers)
56924d99 370 (add-hook 'fundamental-mode-hook ',MODE-enable-in-buffers)
876daebc
LT
371 (add-hook 'find-file-hook ',MODE-check-buffers)
372 (add-hook 'change-major-mode-hook ',MODE-cmhh))
373 (remove-hook 'after-change-major-mode-hook ',MODE-enable-in-buffers)
56924d99 374 (remove-hook 'fundamental-mode-hook ',MODE-enable-in-buffers)
876daebc
LT
375 (remove-hook 'find-file-hook ',MODE-check-buffers)
376 (remove-hook 'change-major-mode-hook ',MODE-cmhh))
be22f4cc
SM
377
378 ;; Go through existing buffers.
379 (dolist (buf (buffer-list))
380 (with-current-buffer buf
34befa9a 381 (if ,global-mode (,turn-on) (when ,mode (,mode -1))))))
be22f4cc 382
275e4f4c 383 ;; Autoloading define-globalized-minor-mode autoloads everything
f5678943 384 ;; up-to-here.
1328a6df
SM
385 :autoload-end
386
be22f4cc 387 ;; List of buffers left to process.
876daebc 388 (defvar ,MODE-buffers nil)
be22f4cc
SM
389
390 ;; The function that calls TURN-ON in each buffer.
876daebc
LT
391 (defun ,MODE-enable-in-buffers ()
392 (dolist (buf ,MODE-buffers)
393 (when (buffer-live-p buf)
394 (with-current-buffer buf
17818d71
SM
395 (unless (eq ,MODE-major-mode major-mode)
396 (if ,mode
397 (progn
398 (,mode -1)
399 (,turn-on)
400 (setq ,MODE-major-mode major-mode))
401 (,turn-on)
402 (setq ,MODE-major-mode major-mode)))))))
876daebc
LT
403 (put ',MODE-enable-in-buffers 'definition-name ',global-mode)
404
405 (defun ,MODE-check-buffers ()
406 (,MODE-enable-in-buffers)
407 (setq ,MODE-buffers nil)
408 (remove-hook 'post-command-hook ',MODE-check-buffers))
409 (put ',MODE-check-buffers 'definition-name ',global-mode)
be22f4cc
SM
410
411 ;; The function that catches kill-all-local-variables.
876daebc
LT
412 (defun ,MODE-cmhh ()
413 (add-to-list ',MODE-buffers (current-buffer))
414 (add-hook 'post-command-hook ',MODE-check-buffers))
415 (put ',MODE-cmhh 'definition-name ',global-mode))))
be22f4cc 416
5a7a545c
SM
417;;;
418;;; easy-mmode-defmap
419;;;
420
210c6549
GM
421(eval-and-compile
422 (if (fboundp 'set-keymap-parents)
423 (defalias 'easy-mmode-set-keymap-parents 'set-keymap-parents)
424 (defun easy-mmode-set-keymap-parents (m parents)
425 (set-keymap-parent
426 m
427 (cond
428 ((not (consp parents)) parents)
429 ((not (cdr parents)) (car parents))
430 (t (let ((m (copy-keymap (pop parents))))
431 (easy-mmode-set-keymap-parents m parents)
432 m)))))))
5a7a545c 433
5d78d57d 434;;;###autoload
5a7a545c
SM
435(defun easy-mmode-define-keymap (bs &optional name m args)
436 "Return a keymap built from bindings BS.
437BS must be a list of (KEY . BINDING) where
3837de12
SM
438KEY and BINDINGS are suitable for `define-key'.
439Optional NAME is passed to `make-sparse-keymap'.
440Optional map M can be used to modify an existing map.
38a48ab7
GM
441ARGS is a list of additional keyword arguments.
442
443Valid keywords and arguments are:
444
445 :name Name of the keymap; overrides NAME argument.
446 :dense Non-nil for a dense keymap.
447 :inherit Parent keymap.
448 :group Ignored.
449 :suppress Non-nil to call `suppress-keymap' on keymap,
450 'nodigits to suppress digits as prefix arguments."
451 (let (inherit dense suppress)
5a7a545c
SM
452 (while args
453 (let ((key (pop args))
454 (val (pop args)))
be22f4cc 455 (case key
165958d2 456 (:name (setq name val))
be22f4cc
SM
457 (:dense (setq dense val))
458 (:inherit (setq inherit val))
38a48ab7 459 (:suppress (setq suppress val))
be22f4cc 460 (:group)
5a7a545c
SM
461 (t (message "Unknown argument %s in defmap" key)))))
462 (unless (keymapp m)
463 (setq bs (append m bs))
464 (setq m (if dense (make-keymap name) (make-sparse-keymap name))))
38a48ab7
GM
465 (when suppress
466 (suppress-keymap m (eq suppress 'nodigits)))
5a7a545c
SM
467 (dolist (b bs)
468 (let ((keys (car b))
469 (binding (cdr b)))
470 (dolist (key (if (consp keys) keys (list keys)))
471 (cond
472 ((symbolp key)
473 (substitute-key-definition key binding m global-map))
474 ((null binding)
475 (unless (keymapp (lookup-key m key)) (define-key m key binding)))
476 ((let ((o (lookup-key m key)))
477 (or (null o) (numberp o) (eq o 'undefined)))
478 (define-key m key binding))))))
479 (cond
480 ((keymapp inherit) (set-keymap-parent m inherit))
481 ((consp inherit) (easy-mmode-set-keymap-parents m inherit)))
482 m))
483
484;;;###autoload
485(defmacro easy-mmode-defmap (m bs doc &rest args)
117fdd32
GM
486 "Define a constant M whose value is the result of `easy-mmode-define-keymap'.
487The M, BS, and ARGS arguments are as per that function. DOC is
488the constant's documentation."
5d78d57d
SM
489 `(defconst ,m
490 (easy-mmode-define-keymap ,bs nil (if (boundp ',m) ,m) ,(cons 'list args))
491 ,doc))
5a7a545c
SM
492
493\f
494;;;
495;;; easy-mmode-defsyntax
496;;;
497
498(defun easy-mmode-define-syntax (css args)
e4fe3460
SM
499 (let ((st (make-syntax-table (plist-get args :copy)))
500 (parent (plist-get args :inherit)))
5a7a545c
SM
501 (dolist (cs css)
502 (let ((char (car cs))
503 (syntax (cdr cs)))
504 (if (sequencep char)
9b97ee2e 505 (mapc (lambda (c) (modify-syntax-entry c syntax st)) char)
5a7a545c 506 (modify-syntax-entry char syntax st))))
e4fe3460
SM
507 (if parent (set-char-table-parent
508 st (if (symbolp parent) (symbol-value parent) parent)))
5a7a545c
SM
509 st))
510
511;;;###autoload
512(defmacro easy-mmode-defsyntax (st css doc &rest args)
e4fe3460 513 "Define variable ST as a syntax-table.
2a83a11d 514CSS contains a list of syntax specifications of the form (CHAR . SYNTAX)."
e4ad5f9e
SM
515 `(progn
516 (autoload 'easy-mmode-define-syntax "easy-mmode")
2a83a11d 517 (defconst ,st (easy-mmode-define-syntax ,css ,(cons 'list args)) ,doc)))
5a7a545c
SM
518
519
520\f
c7ea3acc
SM
521;;;
522;;; easy-mmode-define-navigation
523;;;
524
cc349341
SM
525(defmacro easy-mmode-define-navigation (base re &optional name endfun narrowfun
526 &rest body)
c7ea3acc
SM
527 "Define BASE-next and BASE-prev to navigate in the buffer.
528RE determines the places the commands should move point to.
eed083e6 529NAME should describe the entities matched by RE. It is used to build
c7ea3acc
SM
530 the docstrings of the two functions.
531BASE-next also tries to make sure that the whole entry is visible by
532 searching for its end (by calling ENDFUN if provided or by looking for
533 the next entry) and recentering if necessary.
877f9b05
TTN
534ENDFUN should return the end position (with or without moving point).
535NARROWFUN non-nil means to check for narrowing before moving, and if
cc349341
SM
536found, do `widen' first and then call NARROWFUN with no args after moving.
537BODY is executed after moving to the destination location."
538 (declare (indent 5) (debug (exp exp exp def-form def-form &rest def-body)))
c7ea3acc
SM
539 (let* ((base-name (symbol-name base))
540 (prev-sym (intern (concat base-name "-prev")))
877f9b05 541 (next-sym (intern (concat base-name "-next")))
cc349341
SM
542 (when-narrowed
543 (lambda (body)
544 (if (null narrowfun) body
545 `(let ((was-narrowed
546 (prog1 (or (< (- (point-max) (point-min)) (buffer-size)))
547 (widen))))
548 ,body
549 (when was-narrowed (,narrowfun)))))))
8fd9bef2 550 (unless name (setq name base-name))
c7ea3acc 551 `(progn
b5bbbb76
SM
552 (add-to-list 'debug-ignored-errors
553 ,(concat "^No \\(previous\\|next\\) " (regexp-quote name)))
c7ea3acc 554 (defun ,next-sym (&optional count)
36a5b60e 555 ,(format "Go to the next COUNT'th %s." name)
9e288f75 556 (interactive "p")
c7ea3acc
SM
557 (unless count (setq count 1))
558 (if (< count 0) (,prev-sym (- count))
6c119ac0 559 (if (looking-at ,re) (setq count (1+ count)))
cc349341
SM
560 ,(funcall when-narrowed
561 `(if (not (re-search-forward ,re nil t count))
562 (if (looking-at ,re)
563 (goto-char (or ,(if endfun `(,endfun)) (point-max)))
564 (error "No next %s" ,name))
565 (goto-char (match-beginning 0))
566 (when (and (eq (current-buffer) (window-buffer (selected-window)))
32226619 567 (called-interactively-p 'interactive))
cc349341
SM
568 (let ((endpt (or (save-excursion
569 ,(if endfun `(,endfun)
570 `(re-search-forward ,re nil t 2)))
571 (point-max))))
572 (unless (pos-visible-in-window-p endpt nil t)
573 (recenter '(0)))))))
574 ,@body))
d5ba8197 575 (put ',next-sym 'definition-name ',base)
c7ea3acc
SM
576 (defun ,prev-sym (&optional count)
577 ,(format "Go to the previous COUNT'th %s" (or name base-name))
9e288f75 578 (interactive "p")
c7ea3acc
SM
579 (unless count (setq count 1))
580 (if (< count 0) (,next-sym (- count))
cc349341
SM
581 ,(funcall when-narrowed
582 `(unless (re-search-backward ,re nil t count)
583 (error "No previous %s" ,name)))
584 ,@body))
d5ba8197 585 (put ',prev-sym 'definition-name ',base))))
877f9b05 586
5a7a545c 587
6b279740
RS
588(provide 'easy-mmode)
589
590;;; easy-mmode.el ends here