(define-derived-mode): Add a proper edebug declaration.
[bpt/emacs.git] / lisp / emacs-lisp / easy-mmode.el
CommitLineData
e8af40ee 1;;; easy-mmode.el --- easy definition for major and minor modes
6b279740 2
ed771c89 3;; Copyright (C) 1997, 2000, 2001 Free Software Foundation, Inc.
6b279740 4
9781053a
PJ
5;; Author: Georges Brun-Cottan <Georges.Brun-Cottan@inria.fr>
6;; Maintainer: Stefan Monnier <monnier@gnu.org>
7
8;; Keywords: extensions lisp
6b279740
RS
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 2, or (at your option)
15;; any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25;; Boston, MA 02111-1307, USA.
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>.
c8c21615
SM
38;; <mode>-hook : The hook run at the end of the toggle function.
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.
60If provided LIGHTER will be used to help choose capitalization."
61 (let* ((case-fold-search t)
b643ec53
SM
62 (name (concat (replace-regexp-in-string
63 "-Minor" " minor"
64 (capitalize (replace-regexp-in-string
65 "-mode\\'" "" (symbol-name mode))))
b5bbbb76
SM
66 " mode")))
67 (if (not (stringp lighter)) name
68 (setq lighter (replace-regexp-in-string "\\`\\s-+\\|\\-s+\\'" "" lighter))
69 (replace-regexp-in-string lighter lighter name t t))))
3837de12 70
6b279740 71;;;###autoload
29cc3b84
SM
72(defalias 'easy-mmode-define-minor-mode 'define-minor-mode)
73;;;###autoload
74(defmacro define-minor-mode (mode doc &optional init-value lighter keymap &rest body)
6b279740 75 "Define a new minor mode MODE.
b5bbbb76 76This function defines the associated control variable MODE, keymap MODE-map,
bff53411 77toggle command MODE, and hook MODE-hook.
6b279740
RS
78
79DOC is the documentation for the mode toggle command.
29cc3b84 80Optional INIT-VALUE is the initial value of the mode's variable.
c8c21615 81Optional LIGHTER is displayed in the modeline when the mode is on.
6b279740 82Optional KEYMAP is the default (defvar) keymap bound to the mode keymap.
b5bbbb76 83 If it is a list, it is passed to `easy-mmode-define-keymap'
bff53411
SM
84 in order to build a valid keymap. It's generally better to use
85 a separate MODE-map variable than to use this argument.
86The above three arguments can be skipped if keyword arguments are
87used (see below).
88
29cc3b84 89BODY contains code that will be executed each time the mode is (dis)activated.
b5bbbb76 90 It will be executed after any toggling but before running the hooks.
a6ce6869
RS
91 Before the actual body code, you can write
92 keyword arguments (alternating keywords and values).
73ceba9f
SM
93 These following keyword arguments are supported (other keywords
94 will be passed to `defcustom' if the minor mode is global):
a6ce6869 95:group GROUP Custom group name to use in all generated `defcustom' forms.
c8fb3bf9 96:global GLOBAL If non-nil specifies that the minor mode is not meant to be
a6ce6869
RS
97 buffer-local, so don't make the variable MODE buffer-local.
98 By default, the mode is buffer-local.
c8fb3bf9
SM
99:init-value VAL Same as the INIT-VALUE argument.
100:lighter SPEC Same as the LIGHTER argument.
a6ce6869
RS
101:require SYM Same as in `defcustom'.
102
103For example, you could write
104 (define-minor-mode foo-mode \"If enabled, foo on you!\"
73ceba9f 105 :lighter \" Foo\" :require 'foo :global t :group 'hassle :version \"27.5\"
a6ce6869
RS
106 ...BODY CODE...)"
107
bff53411
SM
108 ;; Allow skipping the first three args.
109 (cond
110 ((keywordp init-value)
111 (setq body (list* init-value lighter keymap body)
112 init-value nil lighter nil keymap nil))
113 ((keywordp lighter)
114 (setq body (list* lighter keymap body) lighter nil keymap nil))
115 ((keywordp keymap) (push keymap body) (setq keymap nil)))
116
6b279740 117 (let* ((mode-name (symbol-name mode))
b5bbbb76 118 (pretty-name (easy-mmode-pretty-mode-name mode lighter))
c8c21615 119 (globalp nil)
0a74e3bf
SM
120 (group nil)
121 (extra-args nil)
73ceba9f 122 (extra-keywords nil)
c8fb3bf9 123 (require t)
1328a6df
SM
124 (keymap-sym (if (and keymap (symbolp keymap)) keymap
125 (intern (concat mode-name "-map"))))
b5bbbb76
SM
126 (hook (intern (concat mode-name "-hook")))
127 (hook-on (intern (concat mode-name "-on-hook")))
73ceba9f
SM
128 (hook-off (intern (concat mode-name "-off-hook")))
129 keyw)
b5bbbb76 130
b5bbbb76 131 ;; Check keys.
73ceba9f
SM
132 (while (keywordp (setq keyw (car body)))
133 (setq body (cdr body))
134 (case keyw
bff53411
SM
135 (:init-value (setq init-value (pop body)))
136 (:lighter (setq lighter (pop body)))
be22f4cc 137 (:global (setq globalp (pop body)))
0a74e3bf
SM
138 (:extra-args (setq extra-args (pop body)))
139 (:group (setq group (nconc group (list :group (pop body)))))
c8fb3bf9 140 (:require (setq require (pop body)))
73ceba9f 141 (t (push keyw extra-keywords) (push (pop body) extra-keywords))))
eab6e8b9 142
0a74e3bf
SM
143 (unless group
144 ;; We might as well provide a best-guess default group.
145 (setq group
dce88ea6
MR
146 `(:group ',(or (custom-current-group)
147 (intern (replace-regexp-in-string
148 "-mode\\'" "" mode-name))))))
b5bbbb76 149
6b279740 150 `(progn
5e21ef7a 151 ;; Define the variable to enable or disable the mode.
d5b037c5
SM
152 ,(if (not globalp)
153 `(progn
154 (defvar ,mode ,init-value ,(format "Non-nil if %s is enabled.
a2ed9670 155Use the command `%s' to change this variable." pretty-name mode))
d5b037c5 156 (make-variable-buffer-local ',mode))
6b279740 157
1328a6df
SM
158 (let ((curfile (or (and (boundp 'byte-compile-current-file)
159 byte-compile-current-file)
160 load-file-name)))
161 `(defcustom ,mode ,init-value
a2ed9670 162 ,(format "Non-nil if %s is enabled.
bff53411 163See the command `%s' for a description of this minor-mode.
d5b037c5
SM
164Setting this variable directly does not take effect;
165use either \\[customize] or the function `%s'."
bff53411 166 pretty-name mode mode)
73ceba9f 167 :set 'custom-set-minor-mode
1328a6df 168 :initialize 'custom-initialize-default
0a74e3bf 169 ,@group
1328a6df 170 :type 'boolean
c8fb3bf9
SM
171 ,@(cond
172 ((not (and curfile require)) nil)
173 ((not (eq require t)) `(:require ,require))
174 (t `(:require
175 ',(intern (file-name-nondirectory
73ceba9f
SM
176 (file-name-sans-extension curfile))))))
177 ,@(nreverse extra-keywords))))
1328a6df 178
b5bbbb76 179 ;; The actual function.
0a74e3bf 180 (defun ,mode (&optional arg ,@extra-args)
b5bbbb76 181 ,(or doc
bff53411
SM
182 (format (concat "Toggle %s on or off.
183Interactively, with no prefix argument, toggle the mode.
5ddfa187 184With universal prefix ARG turn mode on.
b5bbbb76 185With zero or negative ARG turn mode off.
bff53411 186\\{%s}") pretty-name keymap-sym))
5ddfa187
SM
187 ;; Use `toggle' rather than (if ,mode 0 1) so that using
188 ;; repeat-command still does the toggling correctly.
189 (interactive (list (or current-prefix-arg 'toggle)))
b5bbbb76 190 (setq ,mode
5ddfa187
SM
191 (cond
192 ((eq arg 'toggle) (not ,mode))
193 (arg (> (prefix-numeric-value arg) 0))
194 (t
195 (if (null ,mode) t
196 (message
197 "Toggling %s off; better pass an explicit argument."
198 ',mode)
199 nil))))
b5bbbb76
SM
200 ,@body
201 ;; The on/off hooks are here for backward compatibility only.
202 (run-hooks ',hook (if ,mode ',hook-on ',hook-off))
b5bbbb76 203 (if (interactive-p)
d99d3266
SM
204 (progn
205 ,(if globalp `(customize-mark-as-set ',mode))
eb81f275 206 (unless (current-message)
d99d3266 207 (message ,(format "%s %%sabled" pretty-name)
eb81f275 208 (if ,mode "en" "dis")))))
bff53411 209 (force-mode-line-update)
d99d3266 210 ;; Return the new setting.
b5bbbb76 211 ,mode)
1328a6df
SM
212 ;; Autoloading an easy-mmode-define-minor-mode autoloads
213 ;; everything up-to-here.
214 :autoload-end
215
0a74e3bf
SM
216 ;; The toggle's hook.
217 (defcustom ,hook nil
218 ,(format "Hook run at the end of function `%s'." mode-name)
dce88ea6 219 ,@group
0a74e3bf
SM
220 :type 'hook)
221
d5b037c5 222 ;; Define the minor-mode keymap.
1328a6df 223 ,(unless (symbolp keymap) ;nil is also a symbol.
d5b037c5 224 `(defvar ,keymap-sym
1328a6df
SM
225 (let ((m ,keymap))
226 (cond ((keymapp m) m)
227 ((listp m) (easy-mmode-define-keymap m))
228 (t (error "Invalid keymap %S" ,keymap))))
d5b037c5
SM
229 ,(format "Keymap for `%s'." mode-name)))
230
b5bbbb76 231 (add-minor-mode ',mode ',lighter
1328a6df 232 ,(if keymap keymap-sym
cb5da1a3
SM
233 `(if (boundp ',keymap-sym)
234 (symbol-value ',keymap-sym))))
a1506d29 235
c8c21615 236 ;; If the mode is global, call the function according to the default.
bb76239b
SM
237 ,(if globalp
238 `(if (and load-file-name (not (equal ,init-value ,mode)))
239 (eval-after-load load-file-name '(,mode (if ,mode 1 -1))))))))
5a7a545c 240\f
be22f4cc
SM
241;;;
242;;; make global minor mode
243;;;
244
d5b037c5 245;;;###autoload
be22f4cc
SM
246(defmacro easy-mmode-define-global-mode (global-mode mode turn-on
247 &rest keys)
bff53411 248 "Make GLOBAL-MODE out of the buffer-local minor MODE.
be22f4cc
SM
249TURN-ON is a function that will be called with no args in every buffer
250 and that should try to turn MODE on if applicable for that buffer.
251KEYS is a list of CL-style keyword arguments:
252:group to specify the custom group."
0a74e3bf 253 (let* ((global-mode-name (symbol-name global-mode))
be22f4cc
SM
254 (pretty-name (easy-mmode-pretty-mode-name mode))
255 (pretty-global-name (easy-mmode-pretty-mode-name global-mode))
0a74e3bf
SM
256 (group nil)
257 (extra-args nil)
be22f4cc
SM
258 (buffers (intern (concat global-mode-name "-buffers")))
259 (cmmh (intern (concat global-mode-name "-cmmh"))))
260
261 ;; Check keys.
262 (while (keywordp (car keys))
263 (case (pop keys)
0a74e3bf
SM
264 (:extra-args (setq extra-args (pop keys)))
265 (:group (setq group (nconc group (list :group (pop keys)))))
be22f4cc
SM
266 (t (setq keys (cdr keys)))))
267
0a74e3bf
SM
268 (unless group
269 ;; We might as well provide a best-guess default group.
270 (setq group
dce88ea6
MR
271 `(:group ',(or (custom-current-group)
272 (intern (replace-regexp-in-string
273 "-mode\\'" "" (symbol-name mode)))))))
274
be22f4cc 275 `(progn
be22f4cc
SM
276 ;; The actual global minor-mode
277 (define-minor-mode ,global-mode
278 ,(format "Toggle %s in every buffer.
279With prefix ARG, turn %s on if and only if ARG is positive.
280%s is actually not turned on in every buffer but only in those
281in which `%s' turns it on."
282 pretty-name pretty-global-name pretty-name turn-on)
0a74e3bf 283 :global t :extra-args ,extra-args ,@group
be22f4cc
SM
284
285 ;; Setup hook to handle future mode changes and new buffers.
286 (if ,global-mode
d5b037c5 287 (progn
5ddfa187 288 (add-hook 'find-file-hook ',buffers)
d5b037c5 289 (add-hook 'change-major-mode-hook ',cmmh))
5ddfa187 290 (remove-hook 'find-file-hook ',buffers)
be22f4cc
SM
291 (remove-hook 'change-major-mode-hook ',cmmh))
292
293 ;; Go through existing buffers.
294 (dolist (buf (buffer-list))
295 (with-current-buffer buf
34befa9a 296 (if ,global-mode (,turn-on) (when ,mode (,mode -1))))))
be22f4cc 297
1328a6df
SM
298 ;; Autoloading easy-mmode-define-global-mode
299 ;; autoloads everything up-to-here.
300 :autoload-end
301
be22f4cc
SM
302 ;; List of buffers left to process.
303 (defvar ,buffers nil)
304
305 ;; The function that calls TURN-ON in each buffer.
306 (defun ,buffers ()
be22f4cc 307 (remove-hook 'post-command-hook ',buffers)
d5b037c5
SM
308 (while ,buffers
309 (let ((buf (pop ,buffers)))
310 (when (buffer-live-p buf)
311 (with-current-buffer buf (,turn-on))))))
02565504 312 (put ',buffers 'definition-name ',global-mode)
be22f4cc
SM
313
314 ;; The function that catches kill-all-local-variables.
315 (defun ,cmmh ()
316 (add-to-list ',buffers (current-buffer))
02565504
RS
317 (add-hook 'post-command-hook ',buffers))
318 (put ',cmmh 'definition-name ',global-mode))))
be22f4cc 319
5a7a545c
SM
320;;;
321;;; easy-mmode-defmap
322;;;
323
324(if (fboundp 'set-keymap-parents)
325 (defalias 'easy-mmode-set-keymap-parents 'set-keymap-parents)
326 (defun easy-mmode-set-keymap-parents (m parents)
327 (set-keymap-parent
328 m
329 (cond
330 ((not (consp parents)) parents)
331 ((not (cdr parents)) (car parents))
332 (t (let ((m (copy-keymap (pop parents))))
333 (easy-mmode-set-keymap-parents m parents)
334 m))))))
335
5d78d57d 336;;;###autoload
5a7a545c
SM
337(defun easy-mmode-define-keymap (bs &optional name m args)
338 "Return a keymap built from bindings BS.
339BS must be a list of (KEY . BINDING) where
3837de12
SM
340KEY and BINDINGS are suitable for `define-key'.
341Optional NAME is passed to `make-sparse-keymap'.
342Optional map M can be used to modify an existing map.
165958d2 343ARGS is a list of additional keyword arguments."
eb81f275 344 (let (inherit dense)
5a7a545c
SM
345 (while args
346 (let ((key (pop args))
347 (val (pop args)))
be22f4cc 348 (case key
165958d2 349 (:name (setq name val))
be22f4cc
SM
350 (:dense (setq dense val))
351 (:inherit (setq inherit val))
352 (:group)
5a7a545c
SM
353 (t (message "Unknown argument %s in defmap" key)))))
354 (unless (keymapp m)
355 (setq bs (append m bs))
356 (setq m (if dense (make-keymap name) (make-sparse-keymap name))))
357 (dolist (b bs)
358 (let ((keys (car b))
359 (binding (cdr b)))
360 (dolist (key (if (consp keys) keys (list keys)))
361 (cond
362 ((symbolp key)
363 (substitute-key-definition key binding m global-map))
364 ((null binding)
365 (unless (keymapp (lookup-key m key)) (define-key m key binding)))
366 ((let ((o (lookup-key m key)))
367 (or (null o) (numberp o) (eq o 'undefined)))
368 (define-key m key binding))))))
369 (cond
370 ((keymapp inherit) (set-keymap-parent m inherit))
371 ((consp inherit) (easy-mmode-set-keymap-parents m inherit)))
372 m))
373
374;;;###autoload
375(defmacro easy-mmode-defmap (m bs doc &rest args)
5d78d57d
SM
376 `(defconst ,m
377 (easy-mmode-define-keymap ,bs nil (if (boundp ',m) ,m) ,(cons 'list args))
378 ,doc))
5a7a545c
SM
379
380\f
381;;;
382;;; easy-mmode-defsyntax
383;;;
384
385(defun easy-mmode-define-syntax (css args)
e4fe3460
SM
386 (let ((st (make-syntax-table (plist-get args :copy)))
387 (parent (plist-get args :inherit)))
5a7a545c
SM
388 (dolist (cs css)
389 (let ((char (car cs))
390 (syntax (cdr cs)))
391 (if (sequencep char)
e4ad5f9e 392 (mapcar (lambda (c) (modify-syntax-entry c syntax st)) char)
5a7a545c 393 (modify-syntax-entry char syntax st))))
e4fe3460
SM
394 (if parent (set-char-table-parent
395 st (if (symbolp parent) (symbol-value parent) parent)))
5a7a545c
SM
396 st))
397
398;;;###autoload
399(defmacro easy-mmode-defsyntax (st css doc &rest args)
e4fe3460 400 "Define variable ST as a syntax-table.
2a83a11d 401CSS contains a list of syntax specifications of the form (CHAR . SYNTAX)."
e4ad5f9e
SM
402 `(progn
403 (autoload 'easy-mmode-define-syntax "easy-mmode")
2a83a11d 404 (defconst ,st (easy-mmode-define-syntax ,css ,(cons 'list args)) ,doc)))
5a7a545c
SM
405
406
407\f
c7ea3acc
SM
408;;;
409;;; easy-mmode-define-navigation
410;;;
411
412(defmacro easy-mmode-define-navigation (base re &optional name endfun)
413 "Define BASE-next and BASE-prev to navigate in the buffer.
414RE determines the places the commands should move point to.
eed083e6 415NAME should describe the entities matched by RE. It is used to build
c7ea3acc
SM
416 the docstrings of the two functions.
417BASE-next also tries to make sure that the whole entry is visible by
418 searching for its end (by calling ENDFUN if provided or by looking for
419 the next entry) and recentering if necessary.
420ENDFUN should return the end position (with or without moving point)."
421 (let* ((base-name (symbol-name base))
422 (prev-sym (intern (concat base-name "-prev")))
423 (next-sym (intern (concat base-name "-next"))))
36a5b60e 424 (unless name (setq name (symbol-name base-name)))
c7ea3acc 425 `(progn
b5bbbb76
SM
426 (add-to-list 'debug-ignored-errors
427 ,(concat "^No \\(previous\\|next\\) " (regexp-quote name)))
c7ea3acc 428 (defun ,next-sym (&optional count)
36a5b60e 429 ,(format "Go to the next COUNT'th %s." name)
c7ea3acc
SM
430 (interactive)
431 (unless count (setq count 1))
432 (if (< count 0) (,prev-sym (- count))
433 (if (looking-at ,re) (incf count))
eed083e6
SM
434 (if (not (re-search-forward ,re nil t count))
435 (if (looking-at ,re)
436 (goto-char (or ,(if endfun `(,endfun)) (point-max)))
437 (error ,(format "No next %s" name)))
438 (goto-char (match-beginning 0))
851040a5
SM
439 (when (and (eq (current-buffer) (window-buffer (selected-window)))
440 (interactive-p))
eed083e6
SM
441 (let ((endpt (or (save-excursion
442 ,(if endfun `(,endfun)
443 `(re-search-forward ,re nil t 2)))
444 (point-max))))
445 (unless (pos-visible-in-window-p endpt nil t)
446 (recenter '(0))))))))
c7ea3acc
SM
447 (defun ,prev-sym (&optional count)
448 ,(format "Go to the previous COUNT'th %s" (or name base-name))
449 (interactive)
450 (unless count (setq count 1))
451 (if (< count 0) (,next-sym (- count))
36a5b60e 452 (unless (re-search-backward ,re nil t count)
c8c21615 453 (error ,(format "No previous %s" name))))))))
5a7a545c 454
6b279740
RS
455(provide 'easy-mmode)
456
457;;; easy-mmode.el ends here