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