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