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