]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/easy-mmode.el
7f7198c36c9111424b38697819e06a0ac5ef70a1
[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 or an expression that returns either a keymap or a list of
98 arguments for `easy-mmode-define-keymap'. If KEYMAP is not a symbol,
99 this also defines the variable MODE-map.
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 Interactively, with no prefix argument, toggle the mode.
237 With universal prefix ARG turn mode on.
238 With zero or negative ARG turn mode off.
239 \\{%s}") 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 every possible buffer.
355 With prefix ARG, turn %s on if and only if
356 ARG is positive.
357 %s is enabled in all buffers where
358 \`%s' would do it.
359 See `%s' for more information on %s."
360 pretty-name pretty-global-name pretty-name turn-on
361 mode pretty-name)
362 :global t ,@group ,@(nreverse extra-keywords)
363
364 ;; Setup hook to handle future mode changes and new buffers.
365 (if ,global-mode
366 (progn
367 (add-hook 'after-change-major-mode-hook
368 ',MODE-enable-in-buffers)
369 (add-hook 'fundamental-mode-hook ',MODE-enable-in-buffers)
370 (add-hook 'find-file-hook ',MODE-check-buffers)
371 (add-hook 'change-major-mode-hook ',MODE-cmhh))
372 (remove-hook 'after-change-major-mode-hook ',MODE-enable-in-buffers)
373 (remove-hook 'fundamental-mode-hook ',MODE-enable-in-buffers)
374 (remove-hook 'find-file-hook ',MODE-check-buffers)
375 (remove-hook 'change-major-mode-hook ',MODE-cmhh))
376
377 ;; Go through existing buffers.
378 (dolist (buf (buffer-list))
379 (with-current-buffer buf
380 (if ,global-mode (,turn-on) (when ,mode (,mode -1))))))
381
382 ;; Autoloading define-globalized-minor-mode autoloads everything
383 ;; up-to-here.
384 :autoload-end
385
386 ;; List of buffers left to process.
387 (defvar ,MODE-buffers nil)
388
389 ;; The function that calls TURN-ON in each buffer.
390 (defun ,MODE-enable-in-buffers ()
391 (dolist (buf ,MODE-buffers)
392 (when (buffer-live-p buf)
393 (with-current-buffer buf
394 (unless (eq ,MODE-major-mode major-mode)
395 (if ,mode
396 (progn
397 (,mode -1)
398 (,turn-on)
399 (setq ,MODE-major-mode major-mode))
400 (,turn-on)
401 (setq ,MODE-major-mode major-mode)))))))
402 (put ',MODE-enable-in-buffers 'definition-name ',global-mode)
403
404 (defun ,MODE-check-buffers ()
405 (,MODE-enable-in-buffers)
406 (setq ,MODE-buffers nil)
407 (remove-hook 'post-command-hook ',MODE-check-buffers))
408 (put ',MODE-check-buffers 'definition-name ',global-mode)
409
410 ;; The function that catches kill-all-local-variables.
411 (defun ,MODE-cmhh ()
412 (add-to-list ',MODE-buffers (current-buffer))
413 (add-hook 'post-command-hook ',MODE-check-buffers))
414 (put ',MODE-cmhh 'definition-name ',global-mode))))
415
416 ;;;
417 ;;; easy-mmode-defmap
418 ;;;
419
420 (eval-and-compile
421 (if (fboundp 'set-keymap-parents)
422 (defalias 'easy-mmode-set-keymap-parents 'set-keymap-parents)
423 (defun easy-mmode-set-keymap-parents (m parents)
424 (set-keymap-parent
425 m
426 (cond
427 ((not (consp parents)) parents)
428 ((not (cdr parents)) (car parents))
429 (t (let ((m (copy-keymap (pop parents))))
430 (easy-mmode-set-keymap-parents m parents)
431 m)))))))
432
433 ;;;###autoload
434 (defun easy-mmode-define-keymap (bs &optional name m args)
435 "Return a keymap built from bindings BS.
436 BS must be a list of (KEY . BINDING) where
437 KEY and BINDINGS are suitable for `define-key'.
438 Optional NAME is passed to `make-sparse-keymap'.
439 Optional map M can be used to modify an existing map.
440 ARGS is a list of additional keyword arguments.
441
442 Valid keywords and arguments are:
443
444 :name Name of the keymap; overrides NAME argument.
445 :dense Non-nil for a dense keymap.
446 :inherit Parent keymap.
447 :group Ignored.
448 :suppress Non-nil to call `suppress-keymap' on keymap,
449 'nodigits to suppress digits as prefix arguments."
450 (let (inherit dense suppress)
451 (while args
452 (let ((key (pop args))
453 (val (pop args)))
454 (case key
455 (:name (setq name val))
456 (:dense (setq dense val))
457 (:inherit (setq inherit val))
458 (:suppress (setq suppress val))
459 (:group)
460 (t (message "Unknown argument %s in defmap" key)))))
461 (unless (keymapp m)
462 (setq bs (append m bs))
463 (setq m (if dense (make-keymap name) (make-sparse-keymap name))))
464 (when suppress
465 (suppress-keymap m (eq suppress 'nodigits)))
466 (dolist (b bs)
467 (let ((keys (car b))
468 (binding (cdr b)))
469 (dolist (key (if (consp keys) keys (list keys)))
470 (cond
471 ((symbolp key)
472 (substitute-key-definition key binding m global-map))
473 ((null binding)
474 (unless (keymapp (lookup-key m key)) (define-key m key binding)))
475 ((let ((o (lookup-key m key)))
476 (or (null o) (numberp o) (eq o 'undefined)))
477 (define-key m key binding))))))
478 (cond
479 ((keymapp inherit) (set-keymap-parent m inherit))
480 ((consp inherit) (easy-mmode-set-keymap-parents m inherit)))
481 m))
482
483 ;;;###autoload
484 (defmacro easy-mmode-defmap (m bs doc &rest args)
485 "Define a constant M whose value is the result of `easy-mmode-define-keymap'.
486 The M, BS, and ARGS arguments are as per that function. DOC is
487 the constant's documentation."
488 `(defconst ,m
489 (easy-mmode-define-keymap ,bs nil (if (boundp ',m) ,m) ,(cons 'list args))
490 ,doc))
491
492 \f
493 ;;;
494 ;;; easy-mmode-defsyntax
495 ;;;
496
497 (defun easy-mmode-define-syntax (css args)
498 (let ((st (make-syntax-table (plist-get args :copy)))
499 (parent (plist-get args :inherit)))
500 (dolist (cs css)
501 (let ((char (car cs))
502 (syntax (cdr cs)))
503 (if (sequencep char)
504 (mapc (lambda (c) (modify-syntax-entry c syntax st)) char)
505 (modify-syntax-entry char syntax st))))
506 (if parent (set-char-table-parent
507 st (if (symbolp parent) (symbol-value parent) parent)))
508 st))
509
510 ;;;###autoload
511 (defmacro easy-mmode-defsyntax (st css doc &rest args)
512 "Define variable ST as a syntax-table.
513 CSS contains a list of syntax specifications of the form (CHAR . SYNTAX)."
514 `(progn
515 (autoload 'easy-mmode-define-syntax "easy-mmode")
516 (defconst ,st (easy-mmode-define-syntax ,css ,(cons 'list args)) ,doc)))
517
518
519 \f
520 ;;;
521 ;;; easy-mmode-define-navigation
522 ;;;
523
524 (defmacro easy-mmode-define-navigation (base re &optional name endfun narrowfun
525 &rest body)
526 "Define BASE-next and BASE-prev to navigate in the buffer.
527 RE determines the places the commands should move point to.
528 NAME should describe the entities matched by RE. It is used to build
529 the docstrings of the two functions.
530 BASE-next also tries to make sure that the whole entry is visible by
531 searching for its end (by calling ENDFUN if provided or by looking for
532 the next entry) and recentering if necessary.
533 ENDFUN should return the end position (with or without moving point).
534 NARROWFUN non-nil means to check for narrowing before moving, and if
535 found, do `widen' first and then call NARROWFUN with no args after moving.
536 BODY is executed after moving to the destination location."
537 (declare (indent 5) (debug (exp exp exp def-form def-form &rest def-body)))
538 (let* ((base-name (symbol-name base))
539 (prev-sym (intern (concat base-name "-prev")))
540 (next-sym (intern (concat base-name "-next")))
541 (when-narrowed
542 (lambda (body)
543 (if (null narrowfun) body
544 `(let ((was-narrowed
545 (prog1 (or (< (- (point-max) (point-min)) (buffer-size)))
546 (widen))))
547 ,body
548 (when was-narrowed (,narrowfun)))))))
549 (unless name (setq name base-name))
550 `(progn
551 (add-to-list 'debug-ignored-errors
552 ,(concat "^No \\(previous\\|next\\) " (regexp-quote name)))
553 (defun ,next-sym (&optional count)
554 ,(format "Go to the next COUNT'th %s." name)
555 (interactive "p")
556 (unless count (setq count 1))
557 (if (< count 0) (,prev-sym (- count))
558 (if (looking-at ,re) (setq count (1+ count)))
559 ,(funcall when-narrowed
560 `(if (not (re-search-forward ,re nil t count))
561 (if (looking-at ,re)
562 (goto-char (or ,(if endfun `(,endfun)) (point-max)))
563 (error "No next %s" ,name))
564 (goto-char (match-beginning 0))
565 (when (and (eq (current-buffer) (window-buffer (selected-window)))
566 (called-interactively-p 'interactive))
567 (let ((endpt (or (save-excursion
568 ,(if endfun `(,endfun)
569 `(re-search-forward ,re nil t 2)))
570 (point-max))))
571 (unless (pos-visible-in-window-p endpt nil t)
572 (recenter '(0)))))))
573 ,@body))
574 (put ',next-sym 'definition-name ',base)
575 (defun ,prev-sym (&optional count)
576 ,(format "Go to the previous COUNT'th %s" (or name base-name))
577 (interactive "p")
578 (unless count (setq count 1))
579 (if (< count 0) (,next-sym (- count))
580 ,(funcall when-narrowed
581 `(unless (re-search-backward ,re nil t count)
582 (error "No previous %s" ,name)))
583 ,@body))
584 (put ',prev-sym 'definition-name ',base))))
585
586
587 (provide 'easy-mmode)
588
589 ;;; easy-mmode.el ends here