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