(no-break-space): Test `class' before `min-colors'
[bpt/emacs.git] / lisp / emacs-lisp / easy-mmode.el
CommitLineData
e8af40ee 1;;; easy-mmode.el --- easy definition for major and minor modes
6b279740 2
2a3a044c
LK
3;; Copyright (C) 1997, 2000, 2001, 2002, 2003, 2004, 2005
4;; Free Software Foundation, Inc.
6b279740 5
9781053a
PJ
6;; Author: Georges Brun-Cottan <Georges.Brun-Cottan@inria.fr>
7;; Maintainer: Stefan Monnier <monnier@gnu.org>
8
9;; Keywords: extensions lisp
6b279740
RS
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 2, or (at your option)
16;; 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; see the file COPYING. If not, write to the
25;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26;; Boston, MA 02111-1307, USA.
27
28;;; Commentary:
29
30;; Minor modes are useful and common. This package makes defining a
31;; minor mode easy, by focusing on the writing of the minor mode
32;; functionalities themselves. Moreover, this package enforces a
33;; conventional naming of user interface primitives, making things
34;; natural for the minor-mode end-users.
35
36;; For each mode, easy-mmode defines the following:
37;; <mode> : The minor mode predicate. A buffer-local variable.
38;; <mode>-map : The keymap possibly associated to <mode>.
c8c21615 39;; see `define-minor-mode' documentation
6b279740
RS
40;;
41;; eval
c8c21615 42;; (pp (macroexpand '(define-minor-mode <your-mode> <doc>)))
6b279740
RS
43;; to check the result before using it.
44
45;; The order in which minor modes are installed is important. Keymap
46;; lookup proceeds down minor-mode-map-alist, and the order there
47;; tends to be the reverse of the order in which the modes were
48;; installed. Perhaps there should be a feature to let you specify
49;; orderings.
50
5a7a545c
SM
51;; Additionally to `define-minor-mode', the package provides convenient
52;; ways to define keymaps, and other helper functions for major and minor modes.
6b279740 53
5a7a545c 54;;; Code:
6b279740 55
be22f4cc
SM
56(eval-when-compile (require 'cl))
57
b5bbbb76
SM
58(defun easy-mmode-pretty-mode-name (mode &optional lighter)
59 "Turn the symbol MODE into a string intended for the user.
e6469973
EZ
60If provided, LIGHTER will be used to help choose capitalization by,
61replacing its case-insensitive matches with the literal string in LIGHTER."
b5bbbb76 62 (let* ((case-fold-search t)
906aee93 63 ;; Produce "Foo-Bar minor mode" from foo-bar-minor-mode.
b643ec53 64 (name (concat (replace-regexp-in-string
906aee93
EZ
65 ;; If the original mode name included "-minor" (some
66 ;; of them don't, e.g. auto-revert-mode), then
67 ;; replace it with " minor".
b643ec53 68 "-Minor" " minor"
e6469973 69 ;; "foo-bar-minor" -> "Foo-Bar-Minor"
b643ec53 70 (capitalize (replace-regexp-in-string
e6469973 71 ;; "foo-bar-minor-mode" -> "foo-bar-minor"
b643ec53 72 "-mode\\'" "" (symbol-name mode))))
b5bbbb76
SM
73 " mode")))
74 (if (not (stringp lighter)) name
e6469973
EZ
75 ;; Strip leading and trailing whitespace from LIGHTER.
76 (setq lighter (replace-regexp-in-string "\\`\\s-+\\|\\s-+\\'" ""
77 lighter))
78 ;; Replace any (case-insensitive) matches for LIGHTER in NAME
79 ;; with a literal LIGHTER. E.g., if NAME is "Iimage mode" and
80 ;; LIGHTER is " iImag", then this will produce "iImage mode".
81 ;; (LIGHTER normally comes from the mode-line string passed to
82 ;; define-minor-mode, and normally includes at least one leading
83 ;; space.)
84 (replace-regexp-in-string (regexp-quote lighter) lighter name t t))))
3837de12 85
6b279740 86;;;###autoload
29cc3b84
SM
87(defalias 'easy-mmode-define-minor-mode 'define-minor-mode)
88;;;###autoload
89(defmacro define-minor-mode (mode doc &optional init-value lighter keymap &rest body)
6b279740 90 "Define a new minor mode MODE.
b5bbbb76 91This function defines the associated control variable MODE, keymap MODE-map,
f5678943 92and toggle command MODE.
6b279740
RS
93
94DOC is the documentation for the mode toggle command.
29cc3b84 95Optional INIT-VALUE is the initial value of the mode's variable.
c8c21615 96Optional LIGHTER is displayed in the modeline when the mode is on.
6b279740 97Optional KEYMAP is the default (defvar) keymap bound to the mode keymap.
b5bbbb76 98 If it is a list, it is passed to `easy-mmode-define-keymap'
bff53411
SM
99 in order to build a valid keymap. It's generally better to use
100 a separate MODE-map variable than to use this argument.
101The above three arguments can be skipped if keyword arguments are
102used (see below).
103
29cc3b84 104BODY contains code that will be executed each time the mode is (dis)activated.
f5678943
LK
105 It will be executed after any toggling but before running the hook variable
106 `mode-HOOK'.
107 Before the actual body code, you can write keyword arguments (alternating
108 keywords and values). These following keyword arguments are supported (other
109 keywords will be passed to `defcustom' if the minor mode is global):
a6ce6869 110:group GROUP Custom group name to use in all generated `defcustom' forms.
ab7bc290 111 Defaults to MODE without the possible trailing \"-mode\".
c25eec81
LK
112 Don't use this default group name unless you have written a
113 `defgroup' to define that group properly.
c8fb3bf9 114:global GLOBAL If non-nil specifies that the minor mode is not meant to be
ab7bc290 115 buffer-local, so don't make the variable MODE buffer-local.
a6ce6869 116 By default, the mode is buffer-local.
c8fb3bf9
SM
117:init-value VAL Same as the INIT-VALUE argument.
118:lighter SPEC Same as the LIGHTER argument.
2e2a0075 119:keymap MAP Same as the KEYMAP argument.
a6ce6869
RS
120:require SYM Same as in `defcustom'.
121
122For example, you could write
123 (define-minor-mode foo-mode \"If enabled, foo on you!\"
73ceba9f 124 :lighter \" Foo\" :require 'foo :global t :group 'hassle :version \"27.5\"
a6ce6869 125 ...BODY CODE...)"
2e2a0075
SM
126 (declare (debug (&define name stringp
127 [&optional [&not keywordp] sexp
128 &optional [&not keywordp] sexp
129 &optional [&not keywordp] sexp]
130 [&rest [keywordp sexp]]
131 def-body)))
a6ce6869 132
bff53411
SM
133 ;; Allow skipping the first three args.
134 (cond
135 ((keywordp init-value)
136 (setq body (list* init-value lighter keymap body)
137 init-value nil lighter nil keymap nil))
138 ((keywordp lighter)
139 (setq body (list* lighter keymap body) lighter nil keymap nil))
140 ((keywordp keymap) (push keymap body) (setq keymap nil)))
141
6b279740 142 (let* ((mode-name (symbol-name mode))
b5bbbb76 143 (pretty-name (easy-mmode-pretty-mode-name mode lighter))
c8c21615 144 (globalp nil)
0a74e3bf
SM
145 (group nil)
146 (extra-args nil)
73ceba9f 147 (extra-keywords nil)
c8fb3bf9 148 (require t)
b5bbbb76
SM
149 (hook (intern (concat mode-name "-hook")))
150 (hook-on (intern (concat mode-name "-on-hook")))
73ceba9f 151 (hook-off (intern (concat mode-name "-off-hook")))
2e2a0075 152 keyw keymap-sym)
b5bbbb76 153
b5bbbb76 154 ;; Check keys.
73ceba9f
SM
155 (while (keywordp (setq keyw (car body)))
156 (setq body (cdr body))
157 (case keyw
bff53411
SM
158 (:init-value (setq init-value (pop body)))
159 (:lighter (setq lighter (pop body)))
be22f4cc 160 (:global (setq globalp (pop body)))
0a74e3bf
SM
161 (:extra-args (setq extra-args (pop body)))
162 (:group (setq group (nconc group (list :group (pop body)))))
c8fb3bf9 163 (:require (setq require (pop body)))
2e2a0075 164 (:keymap (setq keymap (pop body)))
73ceba9f 165 (t (push keyw extra-keywords) (push (pop body) extra-keywords))))
eab6e8b9 166
2e2a0075
SM
167 (setq keymap-sym (if (and keymap (symbolp keymap)) keymap
168 (intern (concat mode-name "-map"))))
169
0a74e3bf
SM
170 (unless group
171 ;; We might as well provide a best-guess default group.
172 (setq group
ab7bc290
LK
173 `(:group ',(intern (replace-regexp-in-string
174 "-mode\\'" "" mode-name)))))
7fb80935 175
6b279740 176 `(progn
5e21ef7a 177 ;; Define the variable to enable or disable the mode.
d5b037c5
SM
178 ,(if (not globalp)
179 `(progn
180 (defvar ,mode ,init-value ,(format "Non-nil if %s is enabled.
a2ed9670 181Use the command `%s' to change this variable." pretty-name mode))
d5b037c5 182 (make-variable-buffer-local ',mode))
6b279740 183
1328a6df
SM
184 (let ((curfile (or (and (boundp 'byte-compile-current-file)
185 byte-compile-current-file)
186 load-file-name)))
187 `(defcustom ,mode ,init-value
a2ed9670 188 ,(format "Non-nil if %s is enabled.
bff53411 189See the command `%s' for a description of this minor-mode.
d5b037c5
SM
190Setting this variable directly does not take effect;
191use either \\[customize] or the function `%s'."
bff53411 192 pretty-name mode mode)
73ceba9f 193 :set 'custom-set-minor-mode
1328a6df 194 :initialize 'custom-initialize-default
0a74e3bf 195 ,@group
1328a6df 196 :type 'boolean
c8fb3bf9
SM
197 ,@(cond
198 ((not (and curfile require)) nil)
199 ((not (eq require t)) `(:require ,require))
200 (t `(:require
201 ',(intern (file-name-nondirectory
73ceba9f
SM
202 (file-name-sans-extension curfile))))))
203 ,@(nreverse extra-keywords))))
1328a6df 204
b5bbbb76 205 ;; The actual function.
0a74e3bf 206 (defun ,mode (&optional arg ,@extra-args)
b5bbbb76 207 ,(or doc
bff53411
SM
208 (format (concat "Toggle %s on or off.
209Interactively, with no prefix argument, toggle the mode.
5ddfa187 210With universal prefix ARG turn mode on.
b5bbbb76 211With zero or negative ARG turn mode off.
bff53411 212\\{%s}") pretty-name keymap-sym))
5ddfa187
SM
213 ;; Use `toggle' rather than (if ,mode 0 1) so that using
214 ;; repeat-command still does the toggling correctly.
215 (interactive (list (or current-prefix-arg 'toggle)))
b5bbbb76 216 (setq ,mode
5ddfa187
SM
217 (cond
218 ((eq arg 'toggle) (not ,mode))
219 (arg (> (prefix-numeric-value arg) 0))
220 (t
221 (if (null ,mode) t
222 (message
223 "Toggling %s off; better pass an explicit argument."
224 ',mode)
225 nil))))
b5bbbb76
SM
226 ,@body
227 ;; The on/off hooks are here for backward compatibility only.
228 (run-hooks ',hook (if ,mode ',hook-on ',hook-off))
a27235b3 229 (if (called-interactively-p)
d99d3266
SM
230 (progn
231 ,(if globalp `(customize-mark-as-set ',mode))
eb81f275 232 (unless (current-message)
2e2a0075 233 (message ,(format "%s %%sabled" pretty-name)
eb81f275 234 (if ,mode "en" "dis")))))
bff53411 235 (force-mode-line-update)
d99d3266 236 ;; Return the new setting.
b5bbbb76 237 ,mode)
2e2a0075 238
ab7bc290
LK
239 ;; Autoloading a define-minor-mode autoloads everything
240 ;; up-to-here.
1328a6df
SM
241 :autoload-end
242
d5b037c5 243 ;; Define the minor-mode keymap.
1328a6df 244 ,(unless (symbolp keymap) ;nil is also a symbol.
d5b037c5 245 `(defvar ,keymap-sym
1328a6df
SM
246 (let ((m ,keymap))
247 (cond ((keymapp m) m)
248 ((listp m) (easy-mmode-define-keymap m))
249 (t (error "Invalid keymap %S" ,keymap))))
d5b037c5
SM
250 ,(format "Keymap for `%s'." mode-name)))
251
b5bbbb76 252 (add-minor-mode ',mode ',lighter
1328a6df 253 ,(if keymap keymap-sym
cb5da1a3
SM
254 `(if (boundp ',keymap-sym)
255 (symbol-value ',keymap-sym))))
a1506d29 256
c8c21615 257 ;; If the mode is global, call the function according to the default.
bb76239b
SM
258 ,(if globalp
259 `(if (and load-file-name (not (equal ,init-value ,mode)))
260 (eval-after-load load-file-name '(,mode (if ,mode 1 -1))))))))
5a7a545c 261\f
be22f4cc
SM
262;;;
263;;; make global minor mode
264;;;
265
d5b037c5 266;;;###autoload
39a27f95
RS
267(defalias 'easy-mmode-define-global-mode 'define-global-minor-mode)
268;;;###autoload
269(defmacro define-global-minor-mode (global-mode mode turn-on &rest keys)
bff53411 270 "Make GLOBAL-MODE out of the buffer-local minor MODE.
be22f4cc
SM
271TURN-ON is a function that will be called with no args in every buffer
272 and that should try to turn MODE on if applicable for that buffer.
273KEYS is a list of CL-style keyword arguments:
876daebc
LT
274:group to specify the custom group.
275
276If MODE's set-up depends on the major mode in effect when it was
277enabled, then disabling and reenabling MODE should make MODE work
278correctly with the current major mode. This is important to
279prevent problems with derived modes, that is, major modes that
280call another major mode in their body."
281
0a74e3bf 282 (let* ((global-mode-name (symbol-name global-mode))
be22f4cc
SM
283 (pretty-name (easy-mmode-pretty-mode-name mode))
284 (pretty-global-name (easy-mmode-pretty-mode-name global-mode))
0a74e3bf
SM
285 (group nil)
286 (extra-args nil)
876daebc
LT
287 (MODE-buffers (intern (concat global-mode-name "-buffers")))
288 (MODE-enable-in-buffers
289 (intern (concat global-mode-name "-enable-in-buffers")))
290 (MODE-check-buffers
291 (intern (concat global-mode-name "-check-buffers")))
292 (MODE-cmhh (intern (concat global-mode-name "-cmhh")))
293 (MODE-major-mode (intern (concat (symbol-name mode) "-major-mode"))))
be22f4cc
SM
294
295 ;; Check keys.
296 (while (keywordp (car keys))
297 (case (pop keys)
0a74e3bf
SM
298 (:extra-args (setq extra-args (pop keys)))
299 (:group (setq group (nconc group (list :group (pop keys)))))
be22f4cc
SM
300 (t (setq keys (cdr keys)))))
301
0a74e3bf
SM
302 (unless group
303 ;; We might as well provide a best-guess default group.
304 (setq group
ab7bc290
LK
305 `(:group ',(intern (replace-regexp-in-string
306 "-mode\\'" "" (symbol-name mode))))))
dce88ea6 307
be22f4cc 308 `(progn
876daebc
LT
309 (defvar ,MODE-major-mode nil)
310 (make-variable-buffer-local ',MODE-major-mode)
be22f4cc
SM
311 ;; The actual global minor-mode
312 (define-minor-mode ,global-mode
313 ,(format "Toggle %s in every buffer.
314With prefix ARG, turn %s on if and only if ARG is positive.
315%s is actually not turned on in every buffer but only in those
316in which `%s' turns it on."
317 pretty-name pretty-global-name pretty-name turn-on)
0a74e3bf 318 :global t :extra-args ,extra-args ,@group
be22f4cc
SM
319
320 ;; Setup hook to handle future mode changes and new buffers.
321 (if ,global-mode
d5b037c5 322 (progn
876daebc
LT
323 (add-hook 'after-change-major-mode-hook
324 ',MODE-enable-in-buffers)
325 (add-hook 'find-file-hook ',MODE-check-buffers)
326 (add-hook 'change-major-mode-hook ',MODE-cmhh))
327 (remove-hook 'after-change-major-mode-hook ',MODE-enable-in-buffers)
328 (remove-hook 'find-file-hook ',MODE-check-buffers)
329 (remove-hook 'change-major-mode-hook ',MODE-cmhh))
be22f4cc
SM
330
331 ;; Go through existing buffers.
332 (dolist (buf (buffer-list))
333 (with-current-buffer buf
34befa9a 334 (if ,global-mode (,turn-on) (when ,mode (,mode -1))))))
be22f4cc 335
f5678943
LK
336 ;; Autoloading define-global-minor-mode autoloads everything
337 ;; up-to-here.
1328a6df
SM
338 :autoload-end
339
be22f4cc 340 ;; List of buffers left to process.
876daebc 341 (defvar ,MODE-buffers nil)
be22f4cc
SM
342
343 ;; The function that calls TURN-ON in each buffer.
876daebc
LT
344 (defun ,MODE-enable-in-buffers ()
345 (dolist (buf ,MODE-buffers)
346 (when (buffer-live-p buf)
347 (with-current-buffer buf
348 (if ,mode
349 (unless (eq ,MODE-major-mode major-mode)
350 (,mode -1)
351 (,turn-on)
352 (setq ,MODE-major-mode major-mode))
353 (,turn-on)
354 (setq ,MODE-major-mode major-mode))))))
355 (put ',MODE-enable-in-buffers 'definition-name ',global-mode)
356
357 (defun ,MODE-check-buffers ()
358 (,MODE-enable-in-buffers)
359 (setq ,MODE-buffers nil)
360 (remove-hook 'post-command-hook ',MODE-check-buffers))
361 (put ',MODE-check-buffers 'definition-name ',global-mode)
be22f4cc
SM
362
363 ;; The function that catches kill-all-local-variables.
876daebc
LT
364 (defun ,MODE-cmhh ()
365 (add-to-list ',MODE-buffers (current-buffer))
366 (add-hook 'post-command-hook ',MODE-check-buffers))
367 (put ',MODE-cmhh 'definition-name ',global-mode))))
be22f4cc 368
5a7a545c
SM
369;;;
370;;; easy-mmode-defmap
371;;;
372
373(if (fboundp 'set-keymap-parents)
374 (defalias 'easy-mmode-set-keymap-parents 'set-keymap-parents)
375 (defun easy-mmode-set-keymap-parents (m parents)
376 (set-keymap-parent
377 m
378 (cond
379 ((not (consp parents)) parents)
380 ((not (cdr parents)) (car parents))
381 (t (let ((m (copy-keymap (pop parents))))
382 (easy-mmode-set-keymap-parents m parents)
383 m))))))
384
5d78d57d 385;;;###autoload
5a7a545c
SM
386(defun easy-mmode-define-keymap (bs &optional name m args)
387 "Return a keymap built from bindings BS.
388BS must be a list of (KEY . BINDING) where
3837de12
SM
389KEY and BINDINGS are suitable for `define-key'.
390Optional NAME is passed to `make-sparse-keymap'.
391Optional map M can be used to modify an existing map.
165958d2 392ARGS is a list of additional keyword arguments."
eb81f275 393 (let (inherit dense)
5a7a545c
SM
394 (while args
395 (let ((key (pop args))
396 (val (pop args)))
be22f4cc 397 (case key
165958d2 398 (:name (setq name val))
be22f4cc
SM
399 (:dense (setq dense val))
400 (:inherit (setq inherit val))
401 (:group)
5a7a545c
SM
402 (t (message "Unknown argument %s in defmap" key)))))
403 (unless (keymapp m)
404 (setq bs (append m bs))
405 (setq m (if dense (make-keymap name) (make-sparse-keymap name))))
406 (dolist (b bs)
407 (let ((keys (car b))
408 (binding (cdr b)))
409 (dolist (key (if (consp keys) keys (list keys)))
410 (cond
411 ((symbolp key)
412 (substitute-key-definition key binding m global-map))
413 ((null binding)
414 (unless (keymapp (lookup-key m key)) (define-key m key binding)))
415 ((let ((o (lookup-key m key)))
416 (or (null o) (numberp o) (eq o 'undefined)))
417 (define-key m key binding))))))
418 (cond
419 ((keymapp inherit) (set-keymap-parent m inherit))
420 ((consp inherit) (easy-mmode-set-keymap-parents m inherit)))
421 m))
422
423;;;###autoload
424(defmacro easy-mmode-defmap (m bs doc &rest args)
5d78d57d
SM
425 `(defconst ,m
426 (easy-mmode-define-keymap ,bs nil (if (boundp ',m) ,m) ,(cons 'list args))
427 ,doc))
5a7a545c
SM
428
429\f
430;;;
431;;; easy-mmode-defsyntax
432;;;
433
434(defun easy-mmode-define-syntax (css args)
e4fe3460
SM
435 (let ((st (make-syntax-table (plist-get args :copy)))
436 (parent (plist-get args :inherit)))
5a7a545c
SM
437 (dolist (cs css)
438 (let ((char (car cs))
439 (syntax (cdr cs)))
440 (if (sequencep char)
e4ad5f9e 441 (mapcar (lambda (c) (modify-syntax-entry c syntax st)) char)
5a7a545c 442 (modify-syntax-entry char syntax st))))
e4fe3460
SM
443 (if parent (set-char-table-parent
444 st (if (symbolp parent) (symbol-value parent) parent)))
5a7a545c
SM
445 st))
446
447;;;###autoload
448(defmacro easy-mmode-defsyntax (st css doc &rest args)
e4fe3460 449 "Define variable ST as a syntax-table.
2a83a11d 450CSS contains a list of syntax specifications of the form (CHAR . SYNTAX)."
e4ad5f9e
SM
451 `(progn
452 (autoload 'easy-mmode-define-syntax "easy-mmode")
2a83a11d 453 (defconst ,st (easy-mmode-define-syntax ,css ,(cons 'list args)) ,doc)))
5a7a545c
SM
454
455
456\f
c7ea3acc
SM
457;;;
458;;; easy-mmode-define-navigation
459;;;
460
877f9b05 461(defmacro easy-mmode-define-navigation (base re &optional name endfun narrowfun)
c7ea3acc
SM
462 "Define BASE-next and BASE-prev to navigate in the buffer.
463RE determines the places the commands should move point to.
eed083e6 464NAME should describe the entities matched by RE. It is used to build
c7ea3acc
SM
465 the docstrings of the two functions.
466BASE-next also tries to make sure that the whole entry is visible by
467 searching for its end (by calling ENDFUN if provided or by looking for
468 the next entry) and recentering if necessary.
877f9b05
TTN
469ENDFUN should return the end position (with or without moving point).
470NARROWFUN non-nil means to check for narrowing before moving, and if
471found, do widen first and then call NARROWFUN with no args after moving."
c7ea3acc
SM
472 (let* ((base-name (symbol-name base))
473 (prev-sym (intern (concat base-name "-prev")))
877f9b05 474 (next-sym (intern (concat base-name "-next")))
e8a12926
SM
475 (check-narrow-maybe
476 (when narrowfun
477 '(setq was-narrowed
478 (prog1 (or (< (- (point-max) (point-min)) (buffer-size)))
479 (widen)))))
877f9b05 480 (re-narrow-maybe (when narrowfun
e8a12926 481 `(when was-narrowed (,narrowfun)))))
8fd9bef2 482 (unless name (setq name base-name))
c7ea3acc 483 `(progn
b5bbbb76
SM
484 (add-to-list 'debug-ignored-errors
485 ,(concat "^No \\(previous\\|next\\) " (regexp-quote name)))
c7ea3acc 486 (defun ,next-sym (&optional count)
36a5b60e 487 ,(format "Go to the next COUNT'th %s." name)
c7ea3acc
SM
488 (interactive)
489 (unless count (setq count 1))
490 (if (< count 0) (,prev-sym (- count))
6c119ac0 491 (if (looking-at ,re) (setq count (1+ count)))
e8a12926 492 (let (was-narrowed)
877f9b05
TTN
493 ,check-narrow-maybe
494 (if (not (re-search-forward ,re nil t count))
495 (if (looking-at ,re)
496 (goto-char (or ,(if endfun `(,endfun)) (point-max)))
497 (error "No next %s" ,name))
498 (goto-char (match-beginning 0))
499 (when (and (eq (current-buffer) (window-buffer (selected-window)))
500 (interactive-p))
501 (let ((endpt (or (save-excursion
502 ,(if endfun `(,endfun)
503 `(re-search-forward ,re nil t 2)))
504 (point-max))))
505 (unless (pos-visible-in-window-p endpt nil t)
506 (recenter '(0))))))
507 ,re-narrow-maybe)))
c7ea3acc
SM
508 (defun ,prev-sym (&optional count)
509 ,(format "Go to the previous COUNT'th %s" (or name base-name))
510 (interactive)
511 (unless count (setq count 1))
512 (if (< count 0) (,next-sym (- count))
e8a12926 513 (let (was-narrowed)
877f9b05
TTN
514 ,check-narrow-maybe
515 (unless (re-search-backward ,re nil t count)
516 (error "No previous %s" ,name))
517 ,re-narrow-maybe))))))
518
5a7a545c 519
6b279740
RS
520(provide 'easy-mmode)
521
ab5796a9 522;;; arch-tag: d48a5250-6961-4528-9cb0-3c9ea042a66a
6b279740 523;;; easy-mmode.el ends here