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