Reduce use of cl in lisp/emacs-lisp/.
[emacs.git] / lisp / emacs-lisp / easy-mmode.el
blob4da48805278a1146010d799f9bb9fcdd49ee5b6d
1 ;;; easy-mmode.el --- easy definition for major and minor modes
3 ;; Copyright (C) 1997, 2000-2012 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
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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
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 "-mode\\'" "" (symbol-name mode))))
69 " mode")))
70 (if (not (stringp lighter)) name
71 ;; Strip leading and trailing whitespace from LIGHTER.
72 (setq lighter (replace-regexp-in-string "\\`\\s-+\\|\\s-+\\'" ""
73 lighter))
74 ;; Replace any (case-insensitive) matches for LIGHTER in NAME
75 ;; with a literal LIGHTER. E.g., if NAME is "Iimage mode" and
76 ;; LIGHTER is " iImag", then this will produce "iImage mode".
77 ;; (LIGHTER normally comes from the mode-line string passed to
78 ;; define-minor-mode, and normally includes at least one leading
79 ;; space.)
80 (replace-regexp-in-string (regexp-quote lighter) lighter name t t))))
82 ;;;###autoload
83 (defalias 'easy-mmode-define-minor-mode 'define-minor-mode)
84 ;;;###autoload
85 (defmacro define-minor-mode (mode doc &optional init-value lighter keymap &rest body)
86 "Define a new minor mode MODE.
87 This defines the toggle command MODE and (by default) a control variable
88 MODE (you can override this with the :variable keyword, see below).
89 DOC is the documentation for the mode toggle command.
91 The defined mode command takes one optional (prefix) argument.
92 Interactively with no prefix argument it toggles the mode.
93 With a prefix argument, it enables the mode if the argument is
94 positive and otherwise disables it. When called from Lisp, it
95 enables the mode if the argument is omitted or nil, and toggles
96 the mode if the argument is `toggle'. If DOC is nil this
97 function adds a basic doc-string stating these facts.
99 Optional INIT-VALUE is the initial value of the mode's variable.
100 Optional LIGHTER is displayed in the mode line when the mode is on.
101 Optional KEYMAP is the default keymap bound to the mode keymap.
102 If non-nil, it should be a variable name (whose value is a keymap),
103 or an expression that returns either a keymap or a list of
104 arguments for `easy-mmode-define-keymap'. If you supply a KEYMAP
105 argument that is not a symbol, this macro defines the variable
106 MODE-map and gives it the value that KEYMAP specifies.
108 BODY contains code to execute each time the mode is enabled or disabled.
109 It is executed after toggling the mode, and before running MODE-hook.
110 Before the actual body code, you can write keyword arguments, i.e.
111 alternating keywords and values. These following special keywords
112 are supported (other keywords are passed to `defcustom' if the minor
113 mode is global):
115 :group GROUP Custom group name to use in all generated `defcustom' forms.
116 Defaults to MODE without the possible trailing \"-mode\".
117 Don't use this default group name unless you have written a
118 `defgroup' to define that group properly.
119 :global GLOBAL If non-nil specifies that the minor mode is not meant to be
120 buffer-local, so don't make the variable MODE buffer-local.
121 By default, the mode is buffer-local.
122 :init-value VAL Same as the INIT-VALUE argument.
123 Not used if you also specify :variable.
124 :lighter SPEC Same as the LIGHTER argument.
125 :keymap MAP Same as the KEYMAP argument.
126 :require SYM Same as in `defcustom'.
127 :variable PLACE The location to use instead of the variable MODE to store
128 the state of the mode. This can be simply a different
129 named variable, or more generally anything that can be used
130 with the CL macro `setf'. PLACE can also be of the form
131 \(GET . SET), where GET is an expression that returns the
132 current state, and SET is a function that takes one argument,
133 the new state, and sets it. If you specify a :variable,
134 this function does not define a MODE variable (nor any of
135 the terms used in :variable).
136 :after-hook A single lisp form which is evaluated after the mode hooks
137 have been run. It should not be quoted.
139 For example, you could write
140 (define-minor-mode foo-mode \"If enabled, foo on you!\"
141 :lighter \" Foo\" :require 'foo :global t :group 'hassle :version \"27.5\"
142 ...BODY CODE...)"
143 (declare (doc-string 2)
144 (debug (&define name stringp
145 [&optional [&not keywordp] sexp
146 &optional [&not keywordp] sexp
147 &optional [&not keywordp] sexp]
148 [&rest [keywordp sexp]]
149 def-body)))
151 ;; Allow skipping the first three args.
152 (cond
153 ((keywordp init-value)
154 (setq body `(,init-value ,lighter ,keymap ,@body)
155 init-value nil lighter nil keymap nil))
156 ((keywordp lighter)
157 (setq body `(,lighter ,keymap ,@body) lighter nil keymap nil))
158 ((keywordp keymap) (push keymap body) (setq keymap nil)))
160 (let* ((last-message (make-symbol "last-message"))
161 (mode-name (symbol-name mode))
162 (pretty-name (easy-mmode-pretty-mode-name mode lighter))
163 (globalp nil)
164 (set nil)
165 (initialize nil)
166 (group nil)
167 (type nil)
168 (extra-args nil)
169 (extra-keywords nil)
170 (variable nil) ;The PLACE where the state is stored.
171 (setter nil) ;The function (if any) to set the mode var.
172 (modefun mode) ;The minor mode function name we're defining.
173 (require t)
174 (after-hook nil)
175 (hook (intern (concat mode-name "-hook")))
176 (hook-on (intern (concat mode-name "-on-hook")))
177 (hook-off (intern (concat mode-name "-off-hook")))
178 keyw keymap-sym tmp)
180 ;; Check keys.
181 (while (keywordp (setq keyw (car body)))
182 (setq body (cdr body))
183 (pcase keyw
184 (`:init-value (setq init-value (pop body)))
185 (`:lighter (setq lighter (purecopy (pop body))))
186 (`:global (setq globalp (pop body)))
187 (`:extra-args (setq extra-args (pop body)))
188 (`:set (setq set (list :set (pop body))))
189 (`:initialize (setq initialize (list :initialize (pop body))))
190 (`:group (setq group (nconc group (list :group (pop body)))))
191 (`:type (setq type (list :type (pop body))))
192 (`:require (setq require (pop body)))
193 (`:keymap (setq keymap (pop body)))
194 (`:variable (setq variable (pop body))
195 (if (not (and (setq tmp (cdr-safe variable))
196 (or (symbolp tmp)
197 (functionp tmp))))
198 ;; PLACE is not of the form (GET . SET).
199 (setq mode variable)
200 (setq mode (car variable))
201 (setq setter (cdr variable))))
202 (`:after-hook (setq after-hook (pop body)))
203 (_ (push keyw extra-keywords) (push (pop body) extra-keywords))))
205 (setq keymap-sym (if (and keymap (symbolp keymap)) keymap
206 (intern (concat mode-name "-map"))))
208 (unless set (setq set '(:set 'custom-set-minor-mode)))
210 (unless initialize
211 (setq initialize '(:initialize 'custom-initialize-default)))
213 (unless group
214 ;; We might as well provide a best-guess default group.
215 (setq group
216 `(:group ',(intern (replace-regexp-in-string
217 "-mode\\'" "" mode-name)))))
219 ;; TODO? Mark booleans as safe if booleanp? Eg abbrev-mode.
220 (unless type (setq type '(:type 'boolean)))
222 `(progn
223 ;; Define the variable to enable or disable the mode.
224 ,(cond
225 ;; If :variable is specified, then the var will be
226 ;; declared elsewhere.
227 (variable nil)
228 ((not globalp)
229 `(progn
230 :autoload-end
231 (defvar ,mode ,init-value ,(format "Non-nil if %s is enabled.
232 Use the command `%s' to change this variable." pretty-name mode))
233 (make-variable-buffer-local ',mode)))
235 (let ((base-doc-string
236 (concat "Non-nil if %s is enabled.
237 See the command `%s' for a description of this minor mode."
238 (if body "
239 Setting this variable directly does not take effect;
240 either customize it (see the info node `Easy Customization')
241 or call the function `%s'."))))
242 `(defcustom ,mode ,init-value
243 ,(format base-doc-string pretty-name mode mode)
244 ,@set
245 ,@initialize
246 ,@group
247 ,@type
248 ,@(unless (eq require t) `(:require ,require))
249 ,@(nreverse extra-keywords)))))
251 ;; The actual function.
252 (defun ,modefun (&optional arg ,@extra-args)
253 ,(or doc
254 (format (concat "Toggle %s on or off.
255 With a prefix argument ARG, enable %s if ARG is
256 positive, and disable it otherwise. If called from Lisp, enable
257 the mode if ARG is omitted or nil, and toggle it if ARG is `toggle'.
258 \\{%s}") pretty-name pretty-name keymap-sym))
259 ;; Use `toggle' rather than (if ,mode 0 1) so that using
260 ;; repeat-command still does the toggling correctly.
261 (interactive (list (or current-prefix-arg 'toggle)))
262 (let ((,last-message (current-message)))
263 (,@(if setter `(funcall #',setter)
264 (list (if (symbolp mode) 'setq 'setf) mode))
265 (if (eq arg 'toggle)
266 (not ,mode)
267 ;; A nil argument also means ON now.
268 (> (prefix-numeric-value arg) 0)))
269 ,@body
270 ;; The on/off hooks are here for backward compatibility only.
271 (run-hooks ',hook (if ,mode ',hook-on ',hook-off))
272 (if (called-interactively-p 'any)
273 (progn
274 ,(if (and globalp (symbolp mode))
275 `(customize-mark-as-set ',mode))
276 ;; Avoid overwriting a message shown by the body,
277 ;; but do overwrite previous messages.
278 (unless (and (current-message)
279 (not (equal ,last-message
280 (current-message))))
281 (message ,(format "%s %%sabled" pretty-name)
282 (if ,mode "en" "dis")))))
283 ,@(when after-hook `(,after-hook)))
284 (force-mode-line-update)
285 ;; Return the new setting.
286 ,mode)
288 ;; Autoloading a define-minor-mode autoloads everything
289 ;; up-to-here.
290 :autoload-end
292 ;; Define the minor-mode keymap.
293 ,(unless (symbolp keymap) ;nil is also a symbol.
294 `(defvar ,keymap-sym
295 (let ((m ,keymap))
296 (cond ((keymapp m) m)
297 ((listp m) (easy-mmode-define-keymap m))
298 (t (error "Invalid keymap %S" m))))
299 ,(format "Keymap for `%s'." mode-name)))
301 ,(if (not (symbolp mode))
302 (if (or lighter keymap)
303 (error ":lighter and :keymap unsupported with mode expression %s" mode))
304 `(with-no-warnings
305 (add-minor-mode ',mode ',lighter
306 ,(if keymap keymap-sym
307 `(if (boundp ',keymap-sym) ,keymap-sym))
309 ,(unless (eq mode modefun) `',modefun)))))))
312 ;;; make global minor mode
315 ;;;###autoload
316 (defalias 'easy-mmode-define-global-mode 'define-globalized-minor-mode)
317 ;;;###autoload
318 (defalias 'define-global-minor-mode 'define-globalized-minor-mode)
319 ;;;###autoload
320 (defmacro define-globalized-minor-mode (global-mode mode turn-on &rest keys)
321 "Make a global mode GLOBAL-MODE corresponding to buffer-local minor MODE.
322 TURN-ON is a function that will be called with no args in every buffer
323 and that should try to turn MODE on if applicable for that buffer.
324 KEYS is a list of CL-style keyword arguments. As the minor mode
325 defined by this function is always global, any :global keyword is
326 ignored. Other keywords have the same meaning as in `define-minor-mode',
327 which see. In particular, :group specifies the custom group.
328 The most useful keywords are those that are passed on to the
329 `defcustom'. It normally makes no sense to pass the :lighter
330 or :keymap keywords to `define-globalized-minor-mode', since these
331 are usually passed to the buffer-local version of the minor mode.
333 If MODE's set-up depends on the major mode in effect when it was
334 enabled, then disabling and reenabling MODE should make MODE work
335 correctly with the current major mode. This is important to
336 prevent problems with derived modes, that is, major modes that
337 call another major mode in their body."
338 (declare (doc-string 2))
339 (let* ((global-mode-name (symbol-name global-mode))
340 (pretty-name (easy-mmode-pretty-mode-name mode))
341 (pretty-global-name (easy-mmode-pretty-mode-name global-mode))
342 (group nil)
343 (extra-keywords nil)
344 (MODE-buffers (intern (concat global-mode-name "-buffers")))
345 (MODE-enable-in-buffers
346 (intern (concat global-mode-name "-enable-in-buffers")))
347 (MODE-check-buffers
348 (intern (concat global-mode-name "-check-buffers")))
349 (MODE-cmhh (intern (concat global-mode-name "-cmhh")))
350 (MODE-major-mode (intern (concat (symbol-name mode) "-major-mode")))
351 keyw)
353 ;; Check keys.
354 (while (keywordp (setq keyw (car keys)))
355 (setq keys (cdr keys))
356 (pcase keyw
357 (`:group (setq group (nconc group (list :group (pop keys)))))
358 (`:global (setq keys (cdr keys)))
359 (_ (push keyw extra-keywords) (push (pop keys) extra-keywords))))
361 (unless group
362 ;; We might as well provide a best-guess default group.
363 (setq group
364 `(:group ',(intern (replace-regexp-in-string
365 "-mode\\'" "" (symbol-name mode))))))
367 `(progn
368 (progn
369 :autoload-end
370 (defvar ,MODE-major-mode nil)
371 (make-variable-buffer-local ',MODE-major-mode))
372 ;; The actual global minor-mode
373 (define-minor-mode ,global-mode
374 ;; Very short lines to avoid too long lines in the generated
375 ;; doc string.
376 ,(format "Toggle %s in all buffers.
377 With prefix ARG, enable %s if ARG is positive;
378 otherwise, disable it. If called from Lisp, enable the mode if
379 ARG is omitted or nil.
381 %s is enabled in all buffers where
382 \`%s' would do it.
383 See `%s' for more information on %s."
384 pretty-name pretty-global-name
385 pretty-name turn-on mode pretty-name)
386 :global t ,@group ,@(nreverse extra-keywords)
388 ;; Setup hook to handle future mode changes and new buffers.
389 (if ,global-mode
390 (progn
391 (add-hook 'after-change-major-mode-hook
392 ',MODE-enable-in-buffers)
393 (add-hook 'change-major-mode-after-body-hook
394 ',MODE-enable-in-buffers)
395 (add-hook 'find-file-hook ',MODE-check-buffers)
396 (add-hook 'change-major-mode-hook ',MODE-cmhh))
397 (remove-hook 'after-change-major-mode-hook ',MODE-enable-in-buffers)
398 (remove-hook 'change-major-mode-after-body-hook
399 ',MODE-enable-in-buffers)
400 (remove-hook 'find-file-hook ',MODE-check-buffers)
401 (remove-hook 'change-major-mode-hook ',MODE-cmhh))
403 ;; Go through existing buffers.
404 (dolist (buf (buffer-list))
405 (with-current-buffer buf
406 (if ,global-mode (,turn-on) (when ,mode (,mode -1))))))
408 ;; Autoloading define-globalized-minor-mode autoloads everything
409 ;; up-to-here.
410 :autoload-end
412 ;; List of buffers left to process.
413 (defvar ,MODE-buffers nil)
415 ;; The function that calls TURN-ON in each buffer.
416 (defun ,MODE-enable-in-buffers ()
417 (dolist (buf ,MODE-buffers)
418 (when (buffer-live-p buf)
419 (with-current-buffer buf
420 (unless (eq ,MODE-major-mode major-mode)
421 (if ,mode
422 (progn
423 (,mode -1)
424 (,turn-on)
425 (setq ,MODE-major-mode major-mode))
426 (,turn-on)
427 (setq ,MODE-major-mode major-mode)))))))
428 (put ',MODE-enable-in-buffers 'definition-name ',global-mode)
430 (defun ,MODE-check-buffers ()
431 (,MODE-enable-in-buffers)
432 (setq ,MODE-buffers nil)
433 (remove-hook 'post-command-hook ',MODE-check-buffers))
434 (put ',MODE-check-buffers 'definition-name ',global-mode)
436 ;; The function that catches kill-all-local-variables.
437 (defun ,MODE-cmhh ()
438 (add-to-list ',MODE-buffers (current-buffer))
439 (add-hook 'post-command-hook ',MODE-check-buffers))
440 (put ',MODE-cmhh 'definition-name ',global-mode))))
443 ;;; easy-mmode-defmap
446 (eval-and-compile
447 (if (fboundp 'set-keymap-parents)
448 (defalias 'easy-mmode-set-keymap-parents 'set-keymap-parents)
449 (defun easy-mmode-set-keymap-parents (m parents)
450 (set-keymap-parent
452 (cond
453 ((not (consp parents)) parents)
454 ((not (cdr parents)) (car parents))
455 (t (let ((m (copy-keymap (pop parents))))
456 (easy-mmode-set-keymap-parents m parents)
457 m)))))))
459 ;;;###autoload
460 (defun easy-mmode-define-keymap (bs &optional name m args)
461 "Return a keymap built from bindings BS.
462 BS must be a list of (KEY . BINDING) where
463 KEY and BINDINGS are suitable for `define-key'.
464 Optional NAME is passed to `make-sparse-keymap'.
465 Optional map M can be used to modify an existing map.
466 ARGS is a list of additional keyword arguments.
468 Valid keywords and arguments are:
470 :name Name of the keymap; overrides NAME argument.
471 :dense Non-nil for a dense keymap.
472 :inherit Parent keymap.
473 :group Ignored.
474 :suppress Non-nil to call `suppress-keymap' on keymap,
475 'nodigits to suppress digits as prefix arguments."
476 (let (inherit dense suppress)
477 (while args
478 (let ((key (pop args))
479 (val (pop args)))
480 (pcase key
481 (`:name (setq name val))
482 (`:dense (setq dense val))
483 (`:inherit (setq inherit val))
484 (`:suppress (setq suppress val))
485 (`:group)
486 (_ (message "Unknown argument %s in defmap" key)))))
487 (unless (keymapp m)
488 (setq bs (append m bs))
489 (setq m (if dense (make-keymap name) (make-sparse-keymap name))))
490 (when suppress
491 (suppress-keymap m (eq suppress 'nodigits)))
492 (dolist (b bs)
493 (let ((keys (car b))
494 (binding (cdr b)))
495 (dolist (key (if (consp keys) keys (list keys)))
496 (cond
497 ((symbolp key)
498 (substitute-key-definition key binding m global-map))
499 ((null binding)
500 (unless (keymapp (lookup-key m key)) (define-key m key binding)))
501 ((let ((o (lookup-key m key)))
502 (or (null o) (numberp o) (eq o 'undefined)))
503 (define-key m key binding))))))
504 (cond
505 ((keymapp inherit) (set-keymap-parent m inherit))
506 ((consp inherit) (easy-mmode-set-keymap-parents m inherit)))
509 ;;;###autoload
510 (defmacro easy-mmode-defmap (m bs doc &rest args)
511 "Define a constant M whose value is the result of `easy-mmode-define-keymap'.
512 The M, BS, and ARGS arguments are as per that function. DOC is
513 the constant's documentation."
514 `(defconst ,m
515 (easy-mmode-define-keymap ,bs nil (if (boundp ',m) ,m) ,(cons 'list args))
516 ,doc))
520 ;;; easy-mmode-defsyntax
523 (defun easy-mmode-define-syntax (css args)
524 (let ((st (make-syntax-table (plist-get args :copy)))
525 (parent (plist-get args :inherit)))
526 (dolist (cs css)
527 (let ((char (car cs))
528 (syntax (cdr cs)))
529 (if (sequencep char)
530 (mapc (lambda (c) (modify-syntax-entry c syntax st)) char)
531 (modify-syntax-entry char syntax st))))
532 (if parent (set-char-table-parent
533 st (if (symbolp parent) (symbol-value parent) parent)))
534 st))
536 ;;;###autoload
537 (defmacro easy-mmode-defsyntax (st css doc &rest args)
538 "Define variable ST as a syntax-table.
539 CSS contains a list of syntax specifications of the form (CHAR . SYNTAX)."
540 `(progn
541 (autoload 'easy-mmode-define-syntax "easy-mmode")
542 (defconst ,st (easy-mmode-define-syntax ,css ,(cons 'list args)) ,doc)))
547 ;;; easy-mmode-define-navigation
550 (defmacro easy-mmode-define-navigation (base re &optional name endfun narrowfun
551 &rest body)
552 "Define BASE-next and BASE-prev to navigate in the buffer.
553 RE determines the places the commands should move point to.
554 NAME should describe the entities matched by RE. It is used to build
555 the docstrings of the two functions.
556 BASE-next also tries to make sure that the whole entry is visible by
557 searching for its end (by calling ENDFUN if provided or by looking for
558 the next entry) and recentering if necessary.
559 ENDFUN should return the end position (with or without moving point).
560 NARROWFUN non-nil means to check for narrowing before moving, and if
561 found, do `widen' first and then call NARROWFUN with no args after moving.
562 BODY is executed after moving to the destination location."
563 (declare (indent 5) (debug (exp exp exp def-form def-form &rest def-body)))
564 (let* ((base-name (symbol-name base))
565 (prev-sym (intern (concat base-name "-prev")))
566 (next-sym (intern (concat base-name "-next")))
567 (when-narrowed
568 (lambda (body)
569 (if (null narrowfun) body
570 `(let ((was-narrowed
571 (prog1 (or (< (- (point-max) (point-min)) (buffer-size)))
572 (widen))))
573 ,body
574 (when was-narrowed (,narrowfun)))))))
575 (unless name (setq name base-name))
576 `(progn
577 (defun ,next-sym (&optional count)
578 ,(format "Go to the next COUNT'th %s." name)
579 (interactive "p")
580 (unless count (setq count 1))
581 (if (< count 0) (,prev-sym (- count))
582 (if (looking-at ,re) (setq count (1+ count)))
583 ,(funcall when-narrowed
584 `(if (not (re-search-forward ,re nil t count))
585 (if (looking-at ,re)
586 (goto-char (or ,(if endfun `(,endfun)) (point-max)))
587 (user-error "No next %s" ,name))
588 (goto-char (match-beginning 0))
589 (when (and (eq (current-buffer) (window-buffer (selected-window)))
590 (called-interactively-p 'interactive))
591 (let ((endpt (or (save-excursion
592 ,(if endfun `(,endfun)
593 `(re-search-forward ,re nil t 2)))
594 (point-max))))
595 (unless (pos-visible-in-window-p endpt nil t)
596 (recenter '(0)))))))
597 ,@body))
598 (put ',next-sym 'definition-name ',base)
599 (defun ,prev-sym (&optional count)
600 ,(format "Go to the previous COUNT'th %s" (or name base-name))
601 (interactive "p")
602 (unless count (setq count 1))
603 (if (< count 0) (,next-sym (- count))
604 ,(funcall when-narrowed
605 `(unless (re-search-backward ,re nil t count)
606 (user-error "No previous %s" ,name)))
607 ,@body))
608 (put ',prev-sym 'definition-name ',base))))
611 (provide 'easy-mmode)
613 ;;; easy-mmode.el ends here