Mercurial > emacs
annotate lisp/emacs-lisp/easy-mmode.el @ 24271:bab606b16c3c
Comment changes.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sun, 07 Feb 1999 04:07:14 +0000 |
parents | 80bfa3fcb7fb |
children | 26cd0ba03116 |
rev | line source |
---|---|
18388 | 1 ;;; easy-mmode.el --- easy definition of minor modes. |
2 | |
3 ;; Copyright (C) 1997 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Georges Brun-Cottan <Georges.Brun-Cottan@inria.fr> | |
6 | |
7 ;; This file is part of GNU Emacs. | |
8 | |
9 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
10 ;; it under the terms of the GNU General Public License as published by | |
11 ;; the Free Software Foundation; either version 2, or (at your option) | |
12 ;; any later version. | |
13 | |
14 ;; GNU Emacs is distributed in the hope that it will be useful, | |
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 ;; GNU General Public License for more details. | |
18 | |
19 ;; You should have received a copy of the GNU General Public License | |
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
22 ;; Boston, MA 02111-1307, USA. | |
23 | |
24 ;;; Commentary: | |
25 | |
26 ;; Minor modes are useful and common. This package makes defining a | |
27 ;; minor mode easy, by focusing on the writing of the minor mode | |
28 ;; functionalities themselves. Moreover, this package enforces a | |
29 ;; conventional naming of user interface primitives, making things | |
30 ;; natural for the minor-mode end-users. | |
31 | |
32 ;; For each mode, easy-mmode defines the following: | |
33 ;; <mode> : The minor mode predicate. A buffer-local variable. | |
34 ;; <mode>-map : The keymap possibly associated to <mode>. | |
35 ;; <mode>-hook,<mode>-on-hook,<mode>-off-hook and <mode>-mode: | |
36 ;; see `easy-mmode-define-minor-mode' documentation | |
37 ;; | |
38 ;; eval | |
39 ;; (pp (macroexpand '(easy-mmode-define-minor-mode <your-mode> <doc>))) | |
40 ;; to check the result before using it. | |
41 | |
42 ;; The order in which minor modes are installed is important. Keymap | |
43 ;; lookup proceeds down minor-mode-map-alist, and the order there | |
44 ;; tends to be the reverse of the order in which the modes were | |
45 ;; installed. Perhaps there should be a feature to let you specify | |
46 ;; orderings. | |
47 | |
48 ;;; Code: | |
49 | |
50 (defun easy-mmode-define-keymap (keymap-alist &optional menu-name) | |
51 "Return a keymap builded from KEYMAP-ALIST. | |
52 KEYMAP-ALIST must be a list of (KEYBINDING . BINDING) where | |
53 KEYBINDING and BINDINGS are suited as for define-key. | |
54 optional MENU-NAME is passed to `make-sparse-keymap'." | |
55 (let ((keymap (make-sparse-keymap menu-name))) | |
56 (mapcar | |
57 (function (lambda (bind) | |
58 (define-key keymap | |
59 (car bind) (cdr bind)))) | |
60 keymap-alist) | |
61 keymap)) | |
62 | |
63 (defmacro easy-mmode-define-toggle (mode &optional doc) | |
64 "Define a one arg toggle mode MODE function and associated hooks. | |
65 MODE-mode is the so defined function that toggle the mode. | |
66 optional DOC is its associated documentation. | |
67 | |
68 Hooks are checked for run, each time MODE-mode is called. | |
69 They run under the followings conditions: | |
70 MODE-hook: if the mode is toggled. | |
71 MODE-on-hook: if the mode is on. | |
72 MODE-off-hook: if the mode is off. | |
73 | |
74 When the mode is effectively toggled, two hooks may run. | |
75 If so MODE-hook is guaranteed to be the first. | |
76 | |
77 \(defmacro easy-mmode-define-toggle (MODE &optional DOC)" | |
78 (let* ((mode-name | |
79 (if (string-match "-mode\\'" (symbol-name mode)) | |
80 (symbol-name mode) | |
81 (concat (symbol-name mode) "-mode"))) | |
82 (hook (intern (concat mode-name "-hook"))) | |
83 (hook-on (intern (concat mode-name "-on-hook"))) | |
84 (hook-off (intern (concat mode-name "-off-hook"))) | |
85 (toggle (intern mode-name)) | |
86 (mode toggle) | |
87 (toggle-doc (or doc | |
88 (format "With no argument, toggle %s mode. | |
89 With arg turn mode on. | |
90 With zero or negative arg turn mode off" | |
91 mode-name)))) | |
92 `(progn | |
93 (defvar ,hook nil | |
94 ,(format "Hook called when %s mode is toggled" mode-name)) | |
95 | |
96 (defvar ,hook-on nil | |
97 ,(format "Hook called when %s mode is turned on" mode-name)) | |
98 | |
99 (defvar ,hook-off nil | |
100 ,(format "Hook called when %s mode is turned off" mode-name)) | |
101 | |
102 (defun ,toggle (&optional arg) | |
103 ,toggle-doc | |
104 (interactive "P") | |
105 (let ((old-mode ,mode)) | |
106 (setq ,mode | |
107 (if arg | |
108 (or (listp arg);; C-u alone | |
109 (> (prefix-numeric-value arg) 0)) | |
110 (not ,mode))) | |
111 (and ,hook | |
112 (not (equal old-mode ,mode)) | |
113 (run-hooks ',hook)) | |
114 (and ,hook-on | |
115 ,mode | |
116 (run-hooks ',hook-on)) | |
117 (and ,hook-off | |
118 (not ,mode) | |
119 (run-hooks ',hook-off))))))) | |
120 | |
121 ;;;###autoload | |
20852
51fc8872fa32
(easy-mmode-define-minor-mode): Fix
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
122 (defmacro easy-mmode-define-minor-mode (mode doc &optional init-value lighter keymap) |
18388 | 123 "Define a new minor mode MODE. |
124 This function defines the associated control variable, keymap, | |
125 toggle command, and hooks (see `easy-mmode-define-toggle'). | |
126 | |
127 DOC is the documentation for the mode toggle command. | |
128 Optional LIGHTER is displayed in the mode-bar when the mode is on. | |
129 Optional KEYMAP is the default (defvar) keymap bound to the mode keymap. | |
130 If it is a list, it is passed to `easy-mmode-define-keymap' | |
131 in order to build a valid keymap. | |
132 | |
133 \(defmacro easy-mmode-define-minor-mode | |
20852
51fc8872fa32
(easy-mmode-define-minor-mode): Fix
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
134 (MODE DOC &optional INIT-VALUE LIGHTER KEYMAP)...\)" |
18388 | 135 (let* ((mode-name (symbol-name mode)) |
22301
80bfa3fcb7fb
(easy-mmode-define-minor-mode): Add missing format arg.
Karl Heuer <kwzh@gnu.org>
parents:
20852
diff
changeset
|
136 (mode-doc (format "Non-nil if %s mode is enabled." mode-name)) |
18388 | 137 (keymap-name (concat mode-name "-map")) |
20852
51fc8872fa32
(easy-mmode-define-minor-mode): Fix
Richard M. Stallman <rms@gnu.org>
parents:
18388
diff
changeset
|
138 (keymap-doc (format "Keymap for %s mode." mode-name))) |
18388 | 139 `(progn |
140 ;; define the switch | |
141 (defvar ,mode ,init-value ,mode-doc) | |
142 (make-variable-buffer-local ',mode) | |
143 | |
144 ;; define the minor-mode keymap | |
145 (defvar ,(intern keymap-name) | |
146 (cond ((and ,keymap (keymapp ,keymap)) | |
147 ,keymap) | |
148 ((listp ,keymap) | |
149 (easy-mmode-define-keymap ,keymap)) | |
150 (t (error "Invalid keymap %S" ,keymap))) | |
151 ,keymap-doc) | |
152 | |
153 ;; define the toggle and the hooks | |
154 ,(macroexpand `(easy-mmode-define-toggle ,mode ,doc)) ; toggle and hooks | |
155 | |
156 ;; update the mode-bar | |
157 (or (assq ',mode minor-mode-alist) | |
158 (setq minor-mode-alist | |
159 (cons (list ',mode ,lighter) minor-mode-alist))) | |
160 | |
161 ;; update the minor-mode-map | |
162 (or (assq ',mode minor-mode-map-alist) | |
163 (setq minor-mode-map-alist | |
164 (cons (cons ',mode ,(intern keymap-name)) minor-mode-map-alist)))) )) | |
165 | |
166 (provide 'easy-mmode) | |
167 | |
168 ;;; easy-mmode.el ends here |