Provide `simple'.
[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
SM
90 It will be executed after any toggling but before running the hooks.
91 BODY can start with a list of CL-style keys specifying additional arguments.
bff53411
SM
92 The following keyword arguments are supported:
93:group Followed by the group name to use for any generated `defcustom'.
94:global If non-nil specifies that the minor mode is not meant to be
95 buffer-local. By default, the variable is made buffer-local.
bff53411
SM
96:init-value Same as the INIT-VALUE argument.
97:lighter Same as the LIGHTER argument."
98 ;; Allow skipping the first three args.
99 (cond
100 ((keywordp init-value)
101 (setq body (list* init-value lighter keymap body)
102 init-value nil lighter nil keymap nil))
103 ((keywordp lighter)
104 (setq body (list* lighter keymap body) lighter nil keymap nil))
105 ((keywordp keymap) (push keymap body) (setq keymap nil)))
106
6b279740 107 (let* ((mode-name (symbol-name mode))
b5bbbb76 108 (pretty-name (easy-mmode-pretty-mode-name mode lighter))
c8c21615 109 (globalp nil)
0a74e3bf
SM
110 (group nil)
111 (extra-args nil)
1328a6df
SM
112 (keymap-sym (if (and keymap (symbolp keymap)) keymap
113 (intern (concat mode-name "-map"))))
b5bbbb76
SM
114 (hook (intern (concat mode-name "-hook")))
115 (hook-on (intern (concat mode-name "-on-hook")))
116 (hook-off (intern (concat mode-name "-off-hook"))))
117
b5bbbb76 118 ;; Check keys.
be22f4cc
SM
119 (while (keywordp (car body))
120 (case (pop body)
bff53411
SM
121 (:init-value (setq init-value (pop body)))
122 (:lighter (setq lighter (pop body)))
be22f4cc 123 (:global (setq globalp (pop body)))
0a74e3bf
SM
124 (:extra-args (setq extra-args (pop body)))
125 (:group (setq group (nconc group (list :group (pop body)))))
bff53411 126 (t (pop body))))
eab6e8b9 127
0a74e3bf
SM
128 (unless group
129 ;; We might as well provide a best-guess default group.
130 (setq group
131 `(:group ',(intern (replace-regexp-in-string "-mode\\'" ""
132 mode-name)))))
b5bbbb76
SM
133 ;; Add default properties to LIGHTER.
134 (unless (or (not (stringp lighter)) (get-text-property 0 'local-map lighter)
135 (get-text-property 0 'keymap lighter))
136 (setq lighter
ed771c89
GM
137 (propertize lighter
138 'local-map mode-line-minor-mode-keymap
139 'help-echo "mouse-3: minor mode menu")))
b5bbbb76 140
6b279740 141 `(progn
5e21ef7a 142 ;; Define the variable to enable or disable the mode.
d5b037c5
SM
143 ,(if (not globalp)
144 `(progn
145 (defvar ,mode ,init-value ,(format "Non-nil if %s is enabled.
a2ed9670 146Use the command `%s' to change this variable." pretty-name mode))
d5b037c5 147 (make-variable-buffer-local ',mode))
6b279740 148
1328a6df
SM
149 (let ((curfile (or (and (boundp 'byte-compile-current-file)
150 byte-compile-current-file)
151 load-file-name)))
152 `(defcustom ,mode ,init-value
a2ed9670 153 ,(format "Non-nil if %s is enabled.
bff53411 154See the command `%s' for a description of this minor-mode.
d5b037c5
SM
155Setting this variable directly does not take effect;
156use either \\[customize] or the function `%s'."
bff53411 157 pretty-name mode mode)
1328a6df
SM
158 :set (lambda (symbol value) (funcall symbol (or value 0)))
159 :initialize 'custom-initialize-default
0a74e3bf 160 ,@group
1328a6df
SM
161 :type 'boolean
162 ,@(when curfile
163 (list
164 :require
165 (list 'quote
166 (intern (file-name-nondirectory
167 (file-name-sans-extension curfile)))))))))
168
b5bbbb76 169 ;; The actual function.
0a74e3bf 170 (defun ,mode (&optional arg ,@extra-args)
b5bbbb76 171 ,(or doc
bff53411
SM
172 (format (concat "Toggle %s on or off.
173Interactively, with no prefix argument, toggle the mode.
5ddfa187 174With universal prefix ARG turn mode on.
b5bbbb76 175With zero or negative ARG turn mode off.
bff53411 176\\{%s}") pretty-name keymap-sym))
5ddfa187
SM
177 ;; Use `toggle' rather than (if ,mode 0 1) so that using
178 ;; repeat-command still does the toggling correctly.
179 (interactive (list (or current-prefix-arg 'toggle)))
b5bbbb76 180 (setq ,mode
5ddfa187
SM
181 (cond
182 ((eq arg 'toggle) (not ,mode))
183 (arg (> (prefix-numeric-value arg) 0))
184 (t
185 (if (null ,mode) t
186 (message
187 "Toggling %s off; better pass an explicit argument."
188 ',mode)
189 nil))))
b5bbbb76
SM
190 ,@body
191 ;; The on/off hooks are here for backward compatibility only.
192 (run-hooks ',hook (if ,mode ',hook-on ',hook-off))
b5bbbb76 193 (if (interactive-p)
d99d3266
SM
194 (progn
195 ,(if globalp `(customize-mark-as-set ',mode))
196 (message ,(format "%s %%sabled" pretty-name)
197 (if ,mode "en" "dis"))))
bff53411 198 (force-mode-line-update)
d99d3266 199 ;; Return the new setting.
b5bbbb76 200 ,mode)
6b279740 201
1328a6df
SM
202 ;; Autoloading an easy-mmode-define-minor-mode autoloads
203 ;; everything up-to-here.
204 :autoload-end
205
0a74e3bf
SM
206 ;; The toggle's hook.
207 (defcustom ,hook nil
208 ,(format "Hook run at the end of function `%s'." mode-name)
209 :group ,(cadr group)
210 :type 'hook)
211
d5b037c5 212 ;; Define the minor-mode keymap.
1328a6df 213 ,(unless (symbolp keymap) ;nil is also a symbol.
d5b037c5 214 `(defvar ,keymap-sym
1328a6df
SM
215 (let ((m ,keymap))
216 (cond ((keymapp m) m)
217 ((listp m) (easy-mmode-define-keymap m))
218 (t (error "Invalid keymap %S" ,keymap))))
d5b037c5
SM
219 ,(format "Keymap for `%s'." mode-name)))
220
b5bbbb76 221 (add-minor-mode ',mode ',lighter
1328a6df 222 ,(if keymap keymap-sym
cb5da1a3
SM
223 `(if (boundp ',keymap-sym)
224 (symbol-value ',keymap-sym))))
bff53411 225
c8c21615 226 ;; If the mode is global, call the function according to the default.
851040a5
SM
227 ,(if globalp
228 `(if (and load-file-name ,mode)
229 (eval-after-load load-file-name '(,mode 1)))))))
5a7a545c 230\f
be22f4cc
SM
231;;;
232;;; make global minor mode
233;;;
234
d5b037c5 235;;;###autoload
be22f4cc
SM
236(defmacro easy-mmode-define-global-mode (global-mode mode turn-on
237 &rest keys)
bff53411 238 "Make GLOBAL-MODE out of the buffer-local minor MODE.
be22f4cc
SM
239TURN-ON is a function that will be called with no args in every buffer
240 and that should try to turn MODE on if applicable for that buffer.
241KEYS is a list of CL-style keyword arguments:
242:group to specify the custom group."
0a74e3bf 243 (let* ((global-mode-name (symbol-name global-mode))
be22f4cc
SM
244 (pretty-name (easy-mmode-pretty-mode-name mode))
245 (pretty-global-name (easy-mmode-pretty-mode-name global-mode))
0a74e3bf
SM
246 (group nil)
247 (extra-args nil)
be22f4cc
SM
248 (buffers (intern (concat global-mode-name "-buffers")))
249 (cmmh (intern (concat global-mode-name "-cmmh"))))
250
251 ;; Check keys.
252 (while (keywordp (car keys))
253 (case (pop keys)
0a74e3bf
SM
254 (:extra-args (setq extra-args (pop keys)))
255 (:group (setq group (nconc group (list :group (pop keys)))))
be22f4cc
SM
256 (t (setq keys (cdr keys)))))
257
0a74e3bf
SM
258 (unless group
259 ;; We might as well provide a best-guess default group.
260 (setq group
261 `(:group ',(intern (replace-regexp-in-string "-mode\\'" ""
262 (symbol-name mode))))))
be22f4cc 263 `(progn
be22f4cc
SM
264 ;; The actual global minor-mode
265 (define-minor-mode ,global-mode
266 ,(format "Toggle %s in every buffer.
267With prefix ARG, turn %s on if and only if ARG is positive.
268%s is actually not turned on in every buffer but only in those
269in which `%s' turns it on."
270 pretty-name pretty-global-name pretty-name turn-on)
0a74e3bf 271 :global t :extra-args ,extra-args ,@group
be22f4cc
SM
272
273 ;; Setup hook to handle future mode changes and new buffers.
274 (if ,global-mode
d5b037c5 275 (progn
5ddfa187 276 (add-hook 'find-file-hook ',buffers)
d5b037c5 277 (add-hook 'change-major-mode-hook ',cmmh))
5ddfa187 278 (remove-hook 'find-file-hook ',buffers)
be22f4cc
SM
279 (remove-hook 'change-major-mode-hook ',cmmh))
280
281 ;; Go through existing buffers.
282 (dolist (buf (buffer-list))
283 (with-current-buffer buf
34befa9a 284 (if ,global-mode (,turn-on) (when ,mode (,mode -1))))))
be22f4cc 285
1328a6df
SM
286 ;; Autoloading easy-mmode-define-global-mode
287 ;; autoloads everything up-to-here.
288 :autoload-end
289
be22f4cc
SM
290 ;; List of buffers left to process.
291 (defvar ,buffers nil)
292
293 ;; The function that calls TURN-ON in each buffer.
294 (defun ,buffers ()
be22f4cc 295 (remove-hook 'post-command-hook ',buffers)
d5b037c5
SM
296 (while ,buffers
297 (let ((buf (pop ,buffers)))
298 (when (buffer-live-p buf)
299 (with-current-buffer buf (,turn-on))))))
02565504 300 (put ',buffers 'definition-name ',global-mode)
be22f4cc
SM
301
302 ;; The function that catches kill-all-local-variables.
303 (defun ,cmmh ()
304 (add-to-list ',buffers (current-buffer))
02565504
RS
305 (add-hook 'post-command-hook ',buffers))
306 (put ',cmmh 'definition-name ',global-mode))))
be22f4cc 307
5a7a545c
SM
308;;;
309;;; easy-mmode-defmap
310;;;
311
312(if (fboundp 'set-keymap-parents)
313 (defalias 'easy-mmode-set-keymap-parents 'set-keymap-parents)
314 (defun easy-mmode-set-keymap-parents (m parents)
315 (set-keymap-parent
316 m
317 (cond
318 ((not (consp parents)) parents)
319 ((not (cdr parents)) (car parents))
320 (t (let ((m (copy-keymap (pop parents))))
321 (easy-mmode-set-keymap-parents m parents)
322 m))))))
323
5d78d57d 324;;;###autoload
5a7a545c
SM
325(defun easy-mmode-define-keymap (bs &optional name m args)
326 "Return a keymap built from bindings BS.
327BS must be a list of (KEY . BINDING) where
3837de12
SM
328KEY and BINDINGS are suitable for `define-key'.
329Optional NAME is passed to `make-sparse-keymap'.
330Optional map M can be used to modify an existing map.
165958d2 331ARGS is a list of additional keyword arguments."
5a7a545c
SM
332 (let (inherit dense suppress)
333 (while args
334 (let ((key (pop args))
335 (val (pop args)))
be22f4cc 336 (case key
165958d2 337 (:name (setq name val))
be22f4cc
SM
338 (:dense (setq dense val))
339 (:inherit (setq inherit val))
340 (:group)
5a7a545c
SM
341 ;;((eq key :suppress) (setq suppress val))
342 (t (message "Unknown argument %s in defmap" key)))))
343 (unless (keymapp m)
344 (setq bs (append m bs))
345 (setq m (if dense (make-keymap name) (make-sparse-keymap name))))
346 (dolist (b bs)
347 (let ((keys (car b))
348 (binding (cdr b)))
349 (dolist (key (if (consp keys) keys (list keys)))
350 (cond
351 ((symbolp key)
352 (substitute-key-definition key binding m global-map))
353 ((null binding)
354 (unless (keymapp (lookup-key m key)) (define-key m key binding)))
355 ((let ((o (lookup-key m key)))
356 (or (null o) (numberp o) (eq o 'undefined)))
357 (define-key m key binding))))))
358 (cond
359 ((keymapp inherit) (set-keymap-parent m inherit))
360 ((consp inherit) (easy-mmode-set-keymap-parents m inherit)))
361 m))
362
363;;;###autoload
364(defmacro easy-mmode-defmap (m bs doc &rest args)
5d78d57d
SM
365 `(defconst ,m
366 (easy-mmode-define-keymap ,bs nil (if (boundp ',m) ,m) ,(cons 'list args))
367 ,doc))
5a7a545c
SM
368
369\f
370;;;
371;;; easy-mmode-defsyntax
372;;;
373
374(defun easy-mmode-define-syntax (css args)
e4fe3460
SM
375 (let ((st (make-syntax-table (plist-get args :copy)))
376 (parent (plist-get args :inherit)))
5a7a545c
SM
377 (dolist (cs css)
378 (let ((char (car cs))
379 (syntax (cdr cs)))
380 (if (sequencep char)
e4ad5f9e 381 (mapcar (lambda (c) (modify-syntax-entry c syntax st)) char)
5a7a545c 382 (modify-syntax-entry char syntax st))))
e4fe3460
SM
383 (if parent (set-char-table-parent
384 st (if (symbolp parent) (symbol-value parent) parent)))
5a7a545c
SM
385 st))
386
387;;;###autoload
388(defmacro easy-mmode-defsyntax (st css doc &rest args)
e4fe3460 389 "Define variable ST as a syntax-table.
2a83a11d 390CSS contains a list of syntax specifications of the form (CHAR . SYNTAX)."
e4ad5f9e
SM
391 `(progn
392 (autoload 'easy-mmode-define-syntax "easy-mmode")
2a83a11d 393 (defconst ,st (easy-mmode-define-syntax ,css ,(cons 'list args)) ,doc)))
5a7a545c
SM
394
395
396\f
c7ea3acc
SM
397;;;
398;;; easy-mmode-define-navigation
399;;;
400
401(defmacro easy-mmode-define-navigation (base re &optional name endfun)
402 "Define BASE-next and BASE-prev to navigate in the buffer.
403RE determines the places the commands should move point to.
eed083e6 404NAME should describe the entities matched by RE. It is used to build
c7ea3acc
SM
405 the docstrings of the two functions.
406BASE-next also tries to make sure that the whole entry is visible by
407 searching for its end (by calling ENDFUN if provided or by looking for
408 the next entry) and recentering if necessary.
409ENDFUN should return the end position (with or without moving point)."
410 (let* ((base-name (symbol-name base))
411 (prev-sym (intern (concat base-name "-prev")))
412 (next-sym (intern (concat base-name "-next"))))
36a5b60e 413 (unless name (setq name (symbol-name base-name)))
c7ea3acc 414 `(progn
b5bbbb76
SM
415 (add-to-list 'debug-ignored-errors
416 ,(concat "^No \\(previous\\|next\\) " (regexp-quote name)))
c7ea3acc 417 (defun ,next-sym (&optional count)
36a5b60e 418 ,(format "Go to the next COUNT'th %s." name)
c7ea3acc
SM
419 (interactive)
420 (unless count (setq count 1))
421 (if (< count 0) (,prev-sym (- count))
422 (if (looking-at ,re) (incf count))
eed083e6
SM
423 (if (not (re-search-forward ,re nil t count))
424 (if (looking-at ,re)
425 (goto-char (or ,(if endfun `(,endfun)) (point-max)))
426 (error ,(format "No next %s" name)))
427 (goto-char (match-beginning 0))
851040a5
SM
428 (when (and (eq (current-buffer) (window-buffer (selected-window)))
429 (interactive-p))
eed083e6
SM
430 (let ((endpt (or (save-excursion
431 ,(if endfun `(,endfun)
432 `(re-search-forward ,re nil t 2)))
433 (point-max))))
434 (unless (pos-visible-in-window-p endpt nil t)
435 (recenter '(0))))))))
c7ea3acc
SM
436 (defun ,prev-sym (&optional count)
437 ,(format "Go to the previous COUNT'th %s" (or name base-name))
438 (interactive)
439 (unless count (setq count 1))
440 (if (< count 0) (,next-sym (- count))
36a5b60e 441 (unless (re-search-backward ,re nil t count)
c8c21615 442 (error ,(format "No previous %s" name))))))))
5a7a545c 443
6b279740
RS
444(provide 'easy-mmode)
445
446;;; easy-mmode.el ends here