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