*** empty log message ***
[emacs.git] / lisp / emacs-lisp / easy-mmode.el
blobc331bd744e34bd27f4e30769e9d0b6e5112b6fc6
1 ;;; easy-mmode.el --- easy definition for major and minor modes
3 ;; Copyright (C) 1997, 2000, 2001 Free Software Foundation, Inc.
5 ;; Author: Georges Brun-Cottan <Georges.Brun-Cottan@inria.fr>
6 ;; Maintainer: Stefan Monnier <monnier@gnu.org>
8 ;; Keywords: extensions lisp
10 ;; This file is part of GNU Emacs.
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.
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.
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.
27 ;;; Commentary:
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.
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>.
38 ;; <mode>-hook : The hook run at the end of the toggle function.
39 ;; see `define-minor-mode' documentation
41 ;; eval
42 ;; (pp (macroexpand '(define-minor-mode <your-mode> <doc>)))
43 ;; to check the result before using it.
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.
51 ;; Additionally to `define-minor-mode', the package provides convenient
52 ;; ways to define keymaps, and other helper functions for major and minor modes.
54 ;;; Code:
56 (eval-when-compile (require 'cl))
58 (defun easy-mmode-pretty-mode-name (mode &optional lighter)
59 "Turn the symbol MODE into a string intended for the user.
60 If provided LIGHTER will be used to help choose capitalization."
61 (let* ((case-fold-search t)
62 (name (concat (replace-regexp-in-string
63 "-Minor" " minor"
64 (capitalize (replace-regexp-in-string
65 "-mode\\'" "" (symbol-name mode))))
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))))
71 ;;;###autoload
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)
75 "Define a new minor mode MODE.
76 This function defines the associated control variable MODE, keymap MODE-map,
77 toggle command MODE, and hook MODE-hook.
79 DOC is the documentation for the mode toggle command.
80 Optional INIT-VALUE is the initial value of the mode's variable.
81 Optional LIGHTER is displayed in the modeline when the mode is on.
82 Optional KEYMAP is the default (defvar) keymap bound to the mode keymap.
83 If it is a list, it is passed to `easy-mmode-define-keymap'
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.
86 The above three arguments can be skipped if keyword arguments are
87 used (see below).
89 BODY contains code that will be executed each time the mode is (dis)activated.
90 It will be executed after any toggling but before running the hooks.
91 Before the actual body code, you can write
92 keyword arguments (alternating keywords and values).
93 These following keyword arguments are supported (other keywords
94 will be passed to `defcustom' if the minor mode is global):
95 :group GROUP Custom group name to use in all generated `defcustom' forms.
96 :global GLOBAL If non-nil specifies that the minor mode is not meant to be
97 buffer-local, so don't make the variable MODE buffer-local.
98 By default, the mode is buffer-local.
99 :init-value VAL Same as the INIT-VALUE argument.
100 :lighter SPEC Same as the LIGHTER argument.
101 :require SYM Same as in `defcustom'.
103 For example, you could write
104 (define-minor-mode foo-mode \"If enabled, foo on you!\"
105 :lighter \" Foo\" :require 'foo :global t :group 'hassle :version \"27.5\"
106 ...BODY CODE...)"
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)))
117 (let* ((mode-name (symbol-name mode))
118 (pretty-name (easy-mmode-pretty-mode-name mode lighter))
119 (globalp nil)
120 (group nil)
121 (extra-args nil)
122 (extra-keywords nil)
123 (require t)
124 (keymap-sym (if (and keymap (symbolp keymap)) keymap
125 (intern (concat mode-name "-map"))))
126 (hook (intern (concat mode-name "-hook")))
127 (hook-on (intern (concat mode-name "-on-hook")))
128 (hook-off (intern (concat mode-name "-off-hook")))
129 keyw)
131 ;; Check keys.
132 (while (keywordp (setq keyw (car body)))
133 (setq body (cdr body))
134 (case keyw
135 (:init-value (setq init-value (pop body)))
136 (:lighter (setq lighter (pop body)))
137 (:global (setq globalp (pop body)))
138 (:extra-args (setq extra-args (pop body)))
139 (:group (setq group (nconc group (list :group (pop body)))))
140 (:require (setq require (pop body)))
141 (t (push keyw extra-keywords) (push (pop body) extra-keywords))))
143 (unless group
144 ;; We might as well provide a best-guess default group.
145 (setq group
146 `(:group ',(or (custom-current-group)
147 (intern (replace-regexp-in-string
148 "-mode\\'" "" mode-name))))))
150 `(progn
151 ;; Define the variable to enable or disable the mode.
152 ,(if (not globalp)
153 `(progn
154 (defvar ,mode ,init-value ,(format "Non-nil if %s is enabled.
155 Use the command `%s' to change this variable." pretty-name mode))
156 (make-variable-buffer-local ',mode))
158 (let ((curfile (or (and (boundp 'byte-compile-current-file)
159 byte-compile-current-file)
160 load-file-name)))
161 `(defcustom ,mode ,init-value
162 ,(format "Non-nil if %s is enabled.
163 See the command `%s' for a description of this minor-mode.
164 Setting this variable directly does not take effect;
165 use either \\[customize] or the function `%s'."
166 pretty-name mode mode)
167 :set 'custom-set-minor-mode
168 :initialize 'custom-initialize-default
169 ,@group
170 :type 'boolean
171 ,@(cond
172 ((not (and curfile require)) nil)
173 ((not (eq require t)) `(:require ,require))
174 (t `(:require
175 ',(intern (file-name-nondirectory
176 (file-name-sans-extension curfile))))))
177 ,@(nreverse extra-keywords))))
179 ;; The actual function.
180 (defun ,mode (&optional arg ,@extra-args)
181 ,(or doc
182 (format (concat "Toggle %s on or off.
183 Interactively, with no prefix argument, toggle the mode.
184 With universal prefix ARG turn mode on.
185 With zero or negative ARG turn mode off.
186 \\{%s}") pretty-name keymap-sym))
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)))
190 (setq ,mode
191 (cond
192 ((eq arg 'toggle) (not ,mode))
193 (arg (> (prefix-numeric-value arg) 0))
195 (if (null ,mode) t
196 (message
197 "Toggling %s off; better pass an explicit argument."
198 ',mode)
199 nil))))
200 ,@body
201 ;; The on/off hooks are here for backward compatibility only.
202 (run-hooks ',hook (if ,mode ',hook-on ',hook-off))
203 (if (interactive-p)
204 (progn
205 ,(if globalp `(customize-mark-as-set ',mode))
206 (message ,(format "%s %%sabled" pretty-name)
207 (if ,mode "en" "dis"))))
208 (force-mode-line-update)
209 ;; Return the new setting.
210 ,mode)
212 ;; Autoloading an easy-mmode-define-minor-mode autoloads
213 ;; everything up-to-here.
214 :autoload-end
216 ;; The toggle's hook.
217 (defcustom ,hook nil
218 ,(format "Hook run at the end of function `%s'." mode-name)
219 ,@group
220 :type 'hook)
222 ;; Define the minor-mode keymap.
223 ,(unless (symbolp keymap) ;nil is also a symbol.
224 `(defvar ,keymap-sym
225 (let ((m ,keymap))
226 (cond ((keymapp m) m)
227 ((listp m) (easy-mmode-define-keymap m))
228 (t (error "Invalid keymap %S" ,keymap))))
229 ,(format "Keymap for `%s'." mode-name)))
231 (add-minor-mode ',mode ',lighter
232 ,(if keymap keymap-sym
233 `(if (boundp ',keymap-sym)
234 (symbol-value ',keymap-sym))))
236 ;; If the mode is global, call the function according to the default.
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))))))))
242 ;;; make global minor mode
245 ;;;###autoload
246 (defmacro easy-mmode-define-global-mode (global-mode mode turn-on
247 &rest keys)
248 "Make GLOBAL-MODE out of the buffer-local minor MODE.
249 TURN-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.
251 KEYS is a list of CL-style keyword arguments:
252 :group to specify the custom group."
253 (let* ((global-mode-name (symbol-name global-mode))
254 (pretty-name (easy-mmode-pretty-mode-name mode))
255 (pretty-global-name (easy-mmode-pretty-mode-name global-mode))
256 (group nil)
257 (extra-args nil)
258 (buffers (intern (concat global-mode-name "-buffers")))
259 (cmmh (intern (concat global-mode-name "-cmmh"))))
261 ;; Check keys.
262 (while (keywordp (car keys))
263 (case (pop keys)
264 (:extra-args (setq extra-args (pop keys)))
265 (:group (setq group (nconc group (list :group (pop keys)))))
266 (t (setq keys (cdr keys)))))
268 (unless group
269 ;; We might as well provide a best-guess default group.
270 (setq group
271 `(:group ',(or (custom-current-group)
272 (intern (replace-regexp-in-string
273 "-mode\\'" "" (symbol-name mode)))))))
275 `(progn
276 ;; The actual global minor-mode
277 (define-minor-mode ,global-mode
278 ,(format "Toggle %s in every buffer.
279 With 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
281 in which `%s' turns it on."
282 pretty-name pretty-global-name pretty-name turn-on)
283 :global t :extra-args ,extra-args ,@group
285 ;; Setup hook to handle future mode changes and new buffers.
286 (if ,global-mode
287 (progn
288 (add-hook 'find-file-hook ',buffers)
289 (add-hook 'change-major-mode-hook ',cmmh))
290 (remove-hook 'find-file-hook ',buffers)
291 (remove-hook 'change-major-mode-hook ',cmmh))
293 ;; Go through existing buffers.
294 (dolist (buf (buffer-list))
295 (with-current-buffer buf
296 (if ,global-mode (,turn-on) (when ,mode (,mode -1))))))
298 ;; Autoloading easy-mmode-define-global-mode
299 ;; autoloads everything up-to-here.
300 :autoload-end
302 ;; List of buffers left to process.
303 (defvar ,buffers nil)
305 ;; The function that calls TURN-ON in each buffer.
306 (defun ,buffers ()
307 (remove-hook 'post-command-hook ',buffers)
308 (while ,buffers
309 (let ((buf (pop ,buffers)))
310 (when (buffer-live-p buf)
311 (with-current-buffer buf (,turn-on))))))
312 (put ',buffers 'definition-name ',global-mode)
314 ;; The function that catches kill-all-local-variables.
315 (defun ,cmmh ()
316 (add-to-list ',buffers (current-buffer))
317 (add-hook 'post-command-hook ',buffers))
318 (put ',cmmh 'definition-name ',global-mode))))
321 ;;; easy-mmode-defmap
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
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))))))
336 ;;;###autoload
337 (defun easy-mmode-define-keymap (bs &optional name m args)
338 "Return a keymap built from bindings BS.
339 BS must be a list of (KEY . BINDING) where
340 KEY and BINDINGS are suitable for `define-key'.
341 Optional NAME is passed to `make-sparse-keymap'.
342 Optional map M can be used to modify an existing map.
343 ARGS is a list of additional keyword arguments."
344 (let (inherit dense suppress)
345 (while args
346 (let ((key (pop args))
347 (val (pop args)))
348 (case key
349 (:name (setq name val))
350 (:dense (setq dense val))
351 (:inherit (setq inherit val))
352 (:group)
353 ;;((eq key :suppress) (setq suppress val))
354 (t (message "Unknown argument %s in defmap" key)))))
355 (unless (keymapp m)
356 (setq bs (append m bs))
357 (setq m (if dense (make-keymap name) (make-sparse-keymap name))))
358 (dolist (b bs)
359 (let ((keys (car b))
360 (binding (cdr b)))
361 (dolist (key (if (consp keys) keys (list keys)))
362 (cond
363 ((symbolp key)
364 (substitute-key-definition key binding m global-map))
365 ((null binding)
366 (unless (keymapp (lookup-key m key)) (define-key m key binding)))
367 ((let ((o (lookup-key m key)))
368 (or (null o) (numberp o) (eq o 'undefined)))
369 (define-key m key binding))))))
370 (cond
371 ((keymapp inherit) (set-keymap-parent m inherit))
372 ((consp inherit) (easy-mmode-set-keymap-parents m inherit)))
375 ;;;###autoload
376 (defmacro easy-mmode-defmap (m bs doc &rest args)
377 `(defconst ,m
378 (easy-mmode-define-keymap ,bs nil (if (boundp ',m) ,m) ,(cons 'list args))
379 ,doc))
383 ;;; easy-mmode-defsyntax
386 (defun easy-mmode-define-syntax (css args)
387 (let ((st (make-syntax-table (plist-get args :copy)))
388 (parent (plist-get args :inherit)))
389 (dolist (cs css)
390 (let ((char (car cs))
391 (syntax (cdr cs)))
392 (if (sequencep char)
393 (mapcar (lambda (c) (modify-syntax-entry c syntax st)) char)
394 (modify-syntax-entry char syntax st))))
395 (if parent (set-char-table-parent
396 st (if (symbolp parent) (symbol-value parent) parent)))
397 st))
399 ;;;###autoload
400 (defmacro easy-mmode-defsyntax (st css doc &rest args)
401 "Define variable ST as a syntax-table.
402 CSS contains a list of syntax specifications of the form (CHAR . SYNTAX)."
403 `(progn
404 (autoload 'easy-mmode-define-syntax "easy-mmode")
405 (defconst ,st (easy-mmode-define-syntax ,css ,(cons 'list args)) ,doc)))
410 ;;; easy-mmode-define-navigation
413 (defmacro easy-mmode-define-navigation (base re &optional name endfun)
414 "Define BASE-next and BASE-prev to navigate in the buffer.
415 RE determines the places the commands should move point to.
416 NAME should describe the entities matched by RE. It is used to build
417 the docstrings of the two functions.
418 BASE-next also tries to make sure that the whole entry is visible by
419 searching for its end (by calling ENDFUN if provided or by looking for
420 the next entry) and recentering if necessary.
421 ENDFUN should return the end position (with or without moving point)."
422 (let* ((base-name (symbol-name base))
423 (prev-sym (intern (concat base-name "-prev")))
424 (next-sym (intern (concat base-name "-next"))))
425 (unless name (setq name (symbol-name base-name)))
426 `(progn
427 (add-to-list 'debug-ignored-errors
428 ,(concat "^No \\(previous\\|next\\) " (regexp-quote name)))
429 (defun ,next-sym (&optional count)
430 ,(format "Go to the next COUNT'th %s." name)
431 (interactive)
432 (unless count (setq count 1))
433 (if (< count 0) (,prev-sym (- count))
434 (if (looking-at ,re) (incf count))
435 (if (not (re-search-forward ,re nil t count))
436 (if (looking-at ,re)
437 (goto-char (or ,(if endfun `(,endfun)) (point-max)))
438 (error ,(format "No next %s" name)))
439 (goto-char (match-beginning 0))
440 (when (and (eq (current-buffer) (window-buffer (selected-window)))
441 (interactive-p))
442 (let ((endpt (or (save-excursion
443 ,(if endfun `(,endfun)
444 `(re-search-forward ,re nil t 2)))
445 (point-max))))
446 (unless (pos-visible-in-window-p endpt nil t)
447 (recenter '(0))))))))
448 (defun ,prev-sym (&optional count)
449 ,(format "Go to the previous COUNT'th %s" (or name base-name))
450 (interactive)
451 (unless count (setq count 1))
452 (if (< count 0) (,next-sym (- count))
453 (unless (re-search-backward ,re nil t count)
454 (error ,(format "No previous %s" name))))))))
456 (provide 'easy-mmode)
458 ;;; easy-mmode.el ends here