* lisp/mail/rmailsum.el: Commas no longer separate regexps. (Bug#19026)
[emacs.git] / lisp / emacs-lisp / easy-mmode.el
1 ;;; easy-mmode.el --- easy definition for major and minor modes
3 ;; Copyright (C) 1997, 2000-2015 Free Software Foundation, Inc.
5 ;; Author: Georges Brun-Cottan <Georges.Brun-Cottan@inria.fr>
6 ;; Maintainer: Stefan Monnier <monnier@gnu.org>
7 ;; Package: emacs
9 ;; Keywords: extensions lisp
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
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.
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>.
37 ;; see `define-minor-mode' documentation
38 ;;
39 ;; eval
40 ;; (pp (macroexpand '(define-minor-mode <your-mode> <doc>)))
41 ;; to check the result before using it.
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.
49 ;; Additionally to `define-minor-mode', the package provides convenient
50 ;; ways to define keymaps, and other helper functions for major and minor modes.
52 ;;; Code:
54 (defun easy-mmode-pretty-mode-name (mode &optional lighter)
55 "Turn the symbol MODE into a string intended for the user.
56 If provided, LIGHTER will be used to help choose capitalization by,
57 replacing its case-insensitive matches with the literal string in LIGHTER."
58 (let* ((case-fold-search t)
59 ;; Produce "Foo-Bar minor mode" from foo-bar-minor-mode.
60 (name (concat (replace-regexp-in-string
61 ;; If the original mode name included "-minor" (some
62 ;; of them don't, e.g. auto-revert-mode), then
63 ;; replace it with " minor".
64 "-Minor" " minor"
65 ;; "foo-bar-minor" -> "Foo-Bar-Minor"
66 (capitalize (replace-regexp-in-string
67 ;; "foo-bar-minor-mode" -> "foo-bar-minor"
68 "toggle-\\|-mode\\'" ""
69 (symbol-name mode))))
70 " mode")))
71 (if (not (stringp lighter)) name
72 ;; Strip leading and trailing whitespace from LIGHTER.
73 (setq lighter (replace-regexp-in-string "\\`\\s-+\\|\\s-+\\'" ""
74 lighter))
75 ;; Replace any (case-insensitive) matches for LIGHTER in NAME
76 ;; with a literal LIGHTER. E.g., if NAME is "Iimage mode" and
77 ;; LIGHTER is " iImag", then this will produce "iImage mode".
78 ;; (LIGHTER normally comes from the mode-line string passed to
79 ;; define-minor-mode, and normally includes at least one leading
80 ;; space.)
81 (replace-regexp-in-string (regexp-quote lighter) lighter name t t))))
83 ;;;###autoload
84 (defalias 'easy-mmode-define-minor-mode 'define-minor-mode)
85 ;;;###autoload
86 (defmacro define-minor-mode (mode doc &optional init-value lighter keymap &rest body)
87 "Define a new minor mode MODE.
88 This defines the toggle command MODE and (by default) a control variable
89 MODE (you can override this with the :variable keyword, see below).
90 DOC is the documentation for the mode toggle command.
92 The defined mode command takes one optional (prefix) argument.
93 Interactively with no prefix argument, it toggles the mode.
94 A prefix argument enables the mode if the argument is positive,
95 and disables it otherwise.
97 When called from Lisp, the mode command toggles the mode if the
98 argument is `toggle', disables the mode if the argument is a
99 non-positive integer, and enables the mode otherwise (including
100 if the argument is omitted or nil or a positive integer).
102 If DOC is nil, give the mode command a basic doc-string
103 documenting what its argument does.
105 Optional INIT-VALUE is the initial value of the mode's variable.
106 Optional LIGHTER is displayed in the mode line when the mode is on.
107 Optional KEYMAP is the default keymap bound to the mode keymap.
108 If non-nil, it should be a variable name (whose value is a keymap),
109 or an expression that returns either a keymap or a list of
110 arguments for `easy-mmode-define-keymap'. If you supply a KEYMAP
111 argument that is not a symbol, this macro defines the variable
112 MODE-map and gives it the value that KEYMAP specifies.
114 BODY contains code to execute each time the mode is enabled or disabled.
115 It is executed after toggling the mode, and before running MODE-hook.
116 Before the actual body code, you can write keyword arguments, i.e.
117 alternating keywords and values. If you provide BODY, then you must
118 provide (even if just nil) INIT-VALUE, LIGHTER, and KEYMAP, or provide
119 at least one keyword argument, or both; otherwise, BODY would be
120 misinterpreted as the first omitted argument. The following special
121 keywords are supported (other keywords are passed to `defcustom' if
122 the minor mode is global):
124 :group GROUP Custom group name to use in all generated `defcustom' forms.
125 Defaults to MODE without the possible trailing \"-mode\".
126 Don't use this default group name unless you have written a
127 `defgroup' to define that group properly.
128 :global GLOBAL If non-nil specifies that the minor mode is not meant to be
129 buffer-local, so don't make the variable MODE buffer-local.
130 By default, the mode is buffer-local.
131 :init-value VAL Same as the INIT-VALUE argument.
132 Not used if you also specify :variable.
133 :lighter SPEC Same as the LIGHTER argument.
134 :keymap MAP Same as the KEYMAP argument.
135 :require SYM Same as in `defcustom'.
136 :variable PLACE The location to use instead of the variable MODE to store
137 the state of the mode. This can be simply a different
138 named variable, or a generalized variable.
139 PLACE can also be of the form \(GET . SET), where GET is
140 an expression that returns the current state, and SET is
141 a function that takes one argument, the new state, and
142 sets it. If you specify a :variable, this function does
143 not define a MODE variable (nor any of the terms used
144 in :variable).
146 :after-hook A single lisp form which is evaluated after the mode hooks
147 have been run. It should not be quoted.
149 For example, you could write
150 (define-minor-mode foo-mode \"If enabled, foo on you!\"
151 :lighter \" Foo\" :require 'foo :global t :group 'hassle :version \"27.5\"
152 ...BODY CODE...)"
153 (declare (doc-string 2)
154 (debug (&define name string-or-null-p
155 [&optional [&not keywordp] sexp
156 &optional [&not keywordp] sexp
157 &optional [&not keywordp] sexp]
158 [&rest [keywordp sexp]]
159 def-body))
160 (indent 1))
162 ;; Allow skipping the first three args.
163 (cond
164 ((keywordp init-value)
165 (setq body (if keymap `(,init-value ,lighter ,keymap ,@body)
166 `(,init-value ,lighter))
167 init-value nil lighter nil keymap nil))
168 ((keywordp lighter)
169 (setq body `(,lighter ,keymap ,@body) lighter nil keymap nil))
170 ((keywordp keymap) (push keymap body) (setq keymap nil)))
172 (let* ((last-message (make-symbol "last-message"))
173 (mode-name (symbol-name mode))
174 (pretty-name (easy-mmode-pretty-mode-name mode lighter))
175 (globalp nil)
176 (set nil)
177 (initialize nil)
178 (group nil)
179 (type nil)
180 (extra-args nil)
181 (extra-keywords nil)
182 (variable nil) ;The PLACE where the state is stored.
183 (setter nil) ;The function (if any) to set the mode var.
184 (modefun mode) ;The minor mode function name we're defining.
185 (require t)
186 (after-hook nil)
187 (hook (intern (concat mode-name "-hook")))
188 (hook-on (intern (concat mode-name "-on-hook")))
189 (hook-off (intern (concat mode-name "-off-hook")))
190 keyw keymap-sym tmp)
192 ;; Check keys.
193 (while (keywordp (setq keyw (car body)))
194 (setq body (cdr body))
195 (pcase keyw
196 (`:init-value (setq init-value (pop body)))
197 (`:lighter (setq lighter (purecopy (pop body))))
198 (`:global (setq globalp (pop body)))
199 (`:extra-args (setq extra-args (pop body)))
200 (`:set (setq set (list :set (pop body))))
201 (`:initialize (setq initialize (list :initialize (pop body))))
202 (`:group (setq group (nconc group (list :group (pop body)))))
203 (`:type (setq type (list :type (pop body))))
204 (`:require (setq require (pop body)))
205 (`:keymap (setq keymap (pop body)))
206 (`:variable (setq variable (pop body))
207 (if (not (and (setq tmp (cdr-safe variable))
208 (or (symbolp tmp)
209 (functionp tmp))))
210 ;; PLACE is not of the form (GET . SET).
211 (setq mode variable)
212 (setq mode (car variable))
213 (setq setter (cdr variable))))
214 (`:after-hook (setq after-hook (pop body)))
215 (_ (push keyw extra-keywords) (push (pop body) extra-keywords))))
217 (setq keymap-sym (if (and keymap (symbolp keymap)) keymap
218 (intern (concat mode-name "-map"))))
220 (unless set (setq set '(:set 'custom-set-minor-mode)))
222 (unless initialize
223 (setq initialize '(:initialize 'custom-initialize-default)))
225 (unless group
226 ;; We might as well provide a best-guess default group.
227 (setq group
228 `(:group ',(intern (replace-regexp-in-string
229 "-mode\\'" "" mode-name)))))
231 ;; TODO? Mark booleans as safe if booleanp? Eg abbrev-mode.
232 (unless type (setq type '(:type 'boolean)))
234 `(progn
235 ;; Define the variable to enable or disable the mode.
236 ,(cond
237 ;; If :variable is specified, then the var will be
238 ;; declared elsewhere.
239 (variable nil)
240 ((not globalp)
241 `(progn
242 :autoload-end
243 (defvar ,mode ,init-value ,(format "Non-nil if %s is enabled.
244 Use the command `%s' to change this variable." pretty-name mode))
245 (make-variable-buffer-local ',mode)))
246 (t
247 (let ((base-doc-string
248 (concat "Non-nil if %s is enabled.
249 See the command `%s' for a description of this minor mode."
250 (if body "
251 Setting this variable directly does not take effect;
252 either customize it (see the info node `Easy Customization')
253 or call the function `%s'."))))
254 `(defcustom ,mode ,init-value
255 ,(format base-doc-string pretty-name mode mode)
256 ,@set
257 ,@initialize
258 ,@group
259 ,@type
260 ,@(unless (eq require t) `(:require ,require))
261 ,@(nreverse extra-keywords)))))
263 ;; The actual function.
264 (defun ,modefun (&optional arg ,@extra-args)
265 ,(or doc
266 (format (concat "Toggle %s on or off.
267 With a prefix argument ARG, enable %s if ARG is
268 positive, and disable it otherwise. If called from Lisp, enable
269 the mode if ARG is omitted or nil, and toggle it if ARG is `toggle'.
270 \\{%s}") pretty-name pretty-name keymap-sym))
271 ;; Use `toggle' rather than (if ,mode 0 1) so that using
272 ;; repeat-command still does the toggling correctly.
273 (interactive (list (or current-prefix-arg 'toggle)))
274 (let ((,last-message (current-message)))
275 (,@(if setter `(funcall #',setter)
276 (list (if (symbolp mode) 'setq 'setf) mode))
277 (if (eq arg 'toggle)
278 (not ,mode)
279 ;; A nil argument also means ON now.
280 (> (prefix-numeric-value arg) 0)))
281 ,@body
282 ;; The on/off hooks are here for backward compatibility only.
283 (run-hooks ',hook (if ,mode ',hook-on ',hook-off))
284 (if (called-interactively-p 'any)
285 (progn
286 ,(if (and globalp (symbolp mode))
287 ;; Unnecessary but harmless if mode set buffer-locally
288 `(customize-mark-as-set ',mode))
289 ;; Avoid overwriting a message shown by the body,
290 ;; but do overwrite previous messages.
291 (unless (and (current-message)
292 (not (equal ,last-message
293 (current-message))))
294 (let ((local
295 ,(if globalp
296 (if (symbolp mode)
297 `(if (local-variable-p ',mode)
298 " in current buffer"
299 "")
300 "")
301 " in current buffer")))
302 (message ,(format "%s %%sabled%%s" pretty-name)
303 (if ,mode "en" "dis") local)))))
304 ,@(when after-hook `(,after-hook)))
305 (force-mode-line-update)
306 ;; Return the new setting.
307 ,mode)
309 ;; Autoloading a define-minor-mode autoloads everything
310 ;; up-to-here.
311 :autoload-end
313 (defvar ,hook nil
314 ,(format "Hook run after entering or leaving `%s'.
315 No problems result if this variable is not bound.
316 `add-hook' automatically binds it. (This is true for all hook variables.)"
317 modefun))
319 ;; Define the minor-mode keymap.
320 ,(unless (symbolp keymap) ;nil is also a symbol.
321 `(defvar ,keymap-sym
322 (let ((m ,keymap))
323 (cond ((keymapp m) m)
324 ((listp m) (easy-mmode-define-keymap m))
325 (t (error "Invalid keymap %S" m))))
326 ,(format "Keymap for `%s'." mode-name)))
328 ,(if (not (symbolp mode))
329 (if (or lighter keymap)
330 (error ":lighter and :keymap unsupported with mode expression %s" mode))
331 `(with-no-warnings
332 (add-minor-mode ',mode ',lighter
333 ,(if keymap keymap-sym
334 `(if (boundp ',keymap-sym) ,keymap-sym))
335 nil
336 ,(unless (eq mode modefun) `',modefun)))))))
337 \f
338 ;;;
339 ;;; make global minor mode
340 ;;;
342 ;;;###autoload
343 (defalias 'easy-mmode-define-global-mode 'define-globalized-minor-mode)
344 ;;;###autoload
345 (defalias 'define-global-minor-mode 'define-globalized-minor-mode)
346 ;;;###autoload
347 (defmacro define-globalized-minor-mode (global-mode mode turn-on &rest keys)
348 "Make a global mode GLOBAL-MODE corresponding to buffer-local minor MODE.
349 TURN-ON is a function that will be called with no args in every buffer
350 and that should try to turn MODE on if applicable for that buffer.
351 KEYS is a list of CL-style keyword arguments. As the minor mode
352 defined by this function is always global, any :global keyword is
353 ignored. Other keywords have the same meaning as in `define-minor-mode',
354 which see. In particular, :group specifies the custom group.
355 The most useful keywords are those that are passed on to the
356 `defcustom'. It normally makes no sense to pass the :lighter
357 or :keymap keywords to `define-globalized-minor-mode', since these
358 are usually passed to the buffer-local version of the minor mode.
360 If MODE's set-up depends on the major mode in effect when it was
361 enabled, then disabling and reenabling MODE should make MODE work
362 correctly with the current major mode. This is important to
363 prevent problems with derived modes, that is, major modes that
364 call another major mode in their body.
366 When a major mode is initialized, MODE is actually turned on just
367 after running the major mode's hook. However, MODE is not turned
368 on if the hook has explicitly disabled it."
369 (declare (doc-string 2))
370 (let* ((global-mode-name (symbol-name global-mode))
371 (mode-name (symbol-name mode))
372 (pretty-name (easy-mmode-pretty-mode-name mode))
373 (pretty-global-name (easy-mmode-pretty-mode-name global-mode))
374 (group nil)
375 (extra-keywords nil)
376 (MODE-buffers (intern (concat global-mode-name "-buffers")))
377 (MODE-enable-in-buffers
378 (intern (concat global-mode-name "-enable-in-buffers")))
379 (MODE-check-buffers
380 (intern (concat global-mode-name "-check-buffers")))
381 (MODE-cmhh (intern (concat global-mode-name "-cmhh")))
382 (minor-MODE-hook (intern (concat mode-name "-hook")))
383 (MODE-set-explicitly (intern (concat mode-name "-set-explicitly")))
384 (MODE-major-mode (intern (concat (symbol-name mode) "-major-mode")))
385 keyw)
387 ;; Check keys.
388 (while (keywordp (setq keyw (car keys)))
389 (setq keys (cdr keys))
390 (pcase keyw
391 (`:group (setq group (nconc group (list :group (pop keys)))))
392 (`:global (setq keys (cdr keys)))
393 (_ (push keyw extra-keywords) (push (pop keys) extra-keywords))))
395 (unless group
396 ;; We might as well provide a best-guess default group.
397 (setq group
398 `(:group ',(intern (replace-regexp-in-string
399 "-mode\\'" "" (symbol-name mode))))))
401 `(progn
402 (progn
403 :autoload-end
404 (defvar ,MODE-major-mode nil)
405 (make-variable-buffer-local ',MODE-major-mode))
406 ;; The actual global minor-mode
407 (define-minor-mode ,global-mode
408 ;; Very short lines to avoid too long lines in the generated
409 ;; doc string.
410 ,(format "Toggle %s in all buffers.
411 With prefix ARG, enable %s if ARG is positive;
412 otherwise, disable it. If called from Lisp, enable the mode if
413 ARG is omitted or nil.
415 %s is enabled in all buffers where
416 \`%s' would do it.
417 See `%s' for more information on %s."
418 pretty-name pretty-global-name
419 pretty-name turn-on mode pretty-name)
420 :global t ,@group ,@(nreverse extra-keywords)
422 ;; Setup hook to handle future mode changes and new buffers.
423 (if ,global-mode
424 (progn
425 (add-hook 'after-change-major-mode-hook
426 ',MODE-enable-in-buffers)
427 (add-hook 'find-file-hook ',MODE-check-buffers)
428 (add-hook 'change-major-mode-hook ',MODE-cmhh))
429 (remove-hook 'after-change-major-mode-hook ',MODE-enable-in-buffers)
430 (remove-hook 'find-file-hook ',MODE-check-buffers)
431 (remove-hook 'change-major-mode-hook ',MODE-cmhh))
433 ;; Go through existing buffers.
434 (dolist (buf (buffer-list))
435 (with-current-buffer buf
436 (if ,global-mode (funcall #',turn-on) (when ,mode (,mode -1))))))
438 ;; Autoloading define-globalized-minor-mode autoloads everything
439 ;; up-to-here.
440 :autoload-end
442 ;; MODE-set-explicitly is set in MODE-set-explicitly and cleared by
443 ;; kill-all-local-variables.
444 (defvar-local ,MODE-set-explicitly nil)
445 (defun ,MODE-set-explicitly ()
446 (setq ,MODE-set-explicitly t))
447 (put ',MODE-set-explicitly 'definition-name ',global-mode)
449 ;; A function which checks whether MODE has been disabled in the major
450 ;; mode hook which has just been run.
451 (add-hook ',minor-MODE-hook ',MODE-set-explicitly)
453 ;; List of buffers left to process.
454 (defvar ,MODE-buffers nil)
456 ;; The function that calls TURN-ON in each buffer.
457 (defun ,MODE-enable-in-buffers ()
458 (dolist (buf ,MODE-buffers)
459 (when (buffer-live-p buf)
460 (with-current-buffer buf
461 (unless ,MODE-set-explicitly
462 (unless (eq ,MODE-major-mode major-mode)
463 (if ,mode
464 (progn
465 (,mode -1)
466 (funcall #',turn-on))
467 (funcall #',turn-on))))
468 (setq ,MODE-major-mode major-mode)))))
469 (put ',MODE-enable-in-buffers 'definition-name ',global-mode)
471 (defun ,MODE-check-buffers ()
472 (,MODE-enable-in-buffers)
473 (setq ,MODE-buffers nil)
474 (remove-hook 'post-command-hook ',MODE-check-buffers))
475 (put ',MODE-check-buffers 'definition-name ',global-mode)
477 ;; The function that catches kill-all-local-variables.
478 (defun ,MODE-cmhh ()
479 (add-to-list ',MODE-buffers (current-buffer))
480 (add-hook 'post-command-hook ',MODE-check-buffers))
481 (put ',MODE-cmhh 'definition-name ',global-mode))))
483 ;;;
484 ;;; easy-mmode-defmap
485 ;;;
487 (defun easy-mmode-set-keymap-parents (m parents)
488 (set-keymap-parent
489 m (if (cdr parents) (make-composed-keymap parents) (car parents))))
491 ;;;###autoload
492 (defun easy-mmode-define-keymap (bs &optional name m args)
493 "Return a keymap built from bindings BS.
494 BS must be a list of (KEY . BINDING) where
495 KEY and BINDINGS are suitable for `define-key'.
496 Optional NAME is passed to `make-sparse-keymap'.
497 Optional map M can be used to modify an existing map.
498 ARGS is a list of additional keyword arguments.
500 Valid keywords and arguments are:
502 :name Name of the keymap; overrides NAME argument.
503 :dense Non-nil for a dense keymap.
504 :inherit Parent keymap.
505 :group Ignored.
506 :suppress Non-nil to call `suppress-keymap' on keymap,
507 'nodigits to suppress digits as prefix arguments."
508 (let (inherit dense suppress)
509 (while args
510 (let ((key (pop args))
511 (val (pop args)))
512 (pcase key
513 (`:name (setq name val))
514 (`:dense (setq dense val))
515 (`:inherit (setq inherit val))
516 (`:suppress (setq suppress val))
517 (`:group)
518 (_ (message "Unknown argument %s in defmap" key)))))
519 (unless (keymapp m)
520 (setq bs (append m bs))
521 (setq m (if dense (make-keymap name) (make-sparse-keymap name))))
522 (when suppress
523 (suppress-keymap m (eq suppress 'nodigits)))
524 (dolist (b bs)
525 (let ((keys (car b))
526 (binding (cdr b)))
527 (dolist (key (if (consp keys) keys (list keys)))
528 (cond
529 ((symbolp key)
530 (substitute-key-definition key binding m global-map))
531 ((null binding)
532 (unless (keymapp (lookup-key m key)) (define-key m key binding)))
533 ((let ((o (lookup-key m key)))
534 (or (null o) (numberp o) (eq o 'undefined)))
535 (define-key m key binding))))))
536 (cond
537 ((keymapp inherit) (set-keymap-parent m inherit))
538 ((consp inherit) (easy-mmode-set-keymap-parents m inherit)))
539 m))
541 ;;;###autoload
542 (defmacro easy-mmode-defmap (m bs doc &rest args)
543 "Define a constant M whose value is the result of `easy-mmode-define-keymap'.
544 The M, BS, and ARGS arguments are as per that function. DOC is
545 the constant's documentation."
546 `(defconst ,m
547 (easy-mmode-define-keymap ,bs nil (if (boundp ',m) ,m) ,(cons 'list args))
548 ,doc))
550 \f
551 ;;;
552 ;;; easy-mmode-defsyntax
553 ;;;
555 (defun easy-mmode-define-syntax (css args)
556 (let ((st (make-syntax-table (plist-get args :copy)))
557 (parent (plist-get args :inherit)))
558 (dolist (cs css)
559 (let ((char (car cs))
560 (syntax (cdr cs)))
561 (if (sequencep char)
562 (mapc (lambda (c) (modify-syntax-entry c syntax st)) char)
563 (modify-syntax-entry char syntax st))))
564 (if parent (set-char-table-parent
565 st (if (symbolp parent) (symbol-value parent) parent)))
566 st))
568 ;;;###autoload
569 (defmacro easy-mmode-defsyntax (st css doc &rest args)
570 "Define variable ST as a syntax-table.
571 CSS contains a list of syntax specifications of the form (CHAR . SYNTAX)."
572 `(progn
573 (autoload 'easy-mmode-define-syntax "easy-mmode")
574 (defconst ,st (easy-mmode-define-syntax ,css ,(cons 'list args)) ,doc)))
577 \f
578 ;;;
579 ;;; easy-mmode-define-navigation
580 ;;;
582 (defmacro easy-mmode-define-navigation (base re &optional name endfun narrowfun
583 &rest body)
584 "Define BASE-next and BASE-prev to navigate in the buffer.
585 RE determines the places the commands should move point to.
586 NAME should describe the entities matched by RE. It is used to build
587 the docstrings of the two functions.
588 BASE-next also tries to make sure that the whole entry is visible by
589 searching for its end (by calling ENDFUN if provided or by looking for
590 the next entry) and recentering if necessary.
591 ENDFUN should return the end position (with or without moving point).
592 NARROWFUN non-nil means to check for narrowing before moving, and if
593 found, do `widen' first and then call NARROWFUN with no args after moving.
594 BODY is executed after moving to the destination location."
595 (declare (indent 5) (debug (exp exp exp def-form def-form &rest def-body)))
596 (let* ((base-name (symbol-name base))
597 (prev-sym (intern (concat base-name "-prev")))
598 (next-sym (intern (concat base-name "-next")))
599 (when-narrowed
600 (lambda (body)
601 (if (null narrowfun) body
602 `(let ((was-narrowed
603 (prog1 (or (< (- (point-max) (point-min)) (buffer-size)))
604 (widen))))
605 ,body
606 (when was-narrowed (funcall #',narrowfun)))))))
607 (unless name (setq name base-name))
608 `(progn
609 (defun ,next-sym (&optional count)
610 ,(format "Go to the next COUNT'th %s." name)
611 (interactive "p")
612 (unless count (setq count 1))
613 (if (< count 0) (,prev-sym (- count))
614 (if (looking-at ,re) (setq count (1+ count)))
615 ,(funcall when-narrowed
616 `(if (not (re-search-forward ,re nil t count))
617 (if (looking-at ,re)
618 (goto-char (or ,(if endfun `(funcall #',endfun)) (point-max)))
619 (user-error "No next %s" ,name))
620 (goto-char (match-beginning 0))
621 (when (and (eq (current-buffer) (window-buffer))
622 (called-interactively-p 'interactive))
623 (let ((endpt (or (save-excursion
624 ,(if endfun `(funcall #',endfun)
625 `(re-search-forward ,re nil t 2)))
626 (point-max))))
627 (unless (pos-visible-in-window-p endpt nil t)
628 (recenter '(0)))))))
629 ,@body))
630 (put ',next-sym 'definition-name ',base)
631 (defun ,prev-sym (&optional count)
632 ,(format "Go to the previous COUNT'th %s" (or name base-name))
633 (interactive "p")
634 (unless count (setq count 1))
635 (if (< count 0) (,next-sym (- count))
636 ,(funcall when-narrowed
637 `(unless (re-search-backward ,re nil t count)
638 (user-error "No previous %s" ,name)))
639 ,@body))
640 (put ',prev-sym 'definition-name ',base))))
643 (provide 'easy-mmode)
645 ;;; easy-mmode.el ends here