Mercurial > emacs
comparison lisp/emacs-lisp/easy-mmode.el @ 18388:7e14277c51f3
Initial revision
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sun, 22 Jun 1997 20:08:32 +0000 |
parents | |
children | 51fc8872fa32 |
comparison
equal
deleted
inserted
replaced
18387:c9700d281f81 | 18388:7e14277c51f3 |
---|---|
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 | |
122 (defmacro easy-mmode-define-minor-mode | |
123 (mode doc &optional init-value &optional lighter &optional keymap) | |
124 "Define a new minor mode MODE. | |
125 This function defines the associated control variable, keymap, | |
126 toggle command, and hooks (see `easy-mmode-define-toggle'). | |
127 | |
128 DOC is the documentation for the mode toggle command. | |
129 Optional LIGHTER is displayed in the mode-bar when the mode is on. | |
130 Optional KEYMAP is the default (defvar) keymap bound to the mode keymap. | |
131 If it is a list, it is passed to `easy-mmode-define-keymap' | |
132 in order to build a valid keymap. | |
133 | |
134 \(defmacro easy-mmode-define-minor-mode | |
135 (MODE DOC &optional INIT-VALUE &optional LIGHTER &optional KEYMAP)...\)" | |
136 (let* ((mode-name (symbol-name mode)) | |
137 (mode-doc (format "%s mode control switch." mode-name)) | |
138 (keymap-name (concat mode-name "-map")) | |
139 (keymap-doc (format "Keymap activated when %s mode is on." mode-name))) | |
140 `(progn | |
141 ;; define the switch | |
142 (defvar ,mode ,init-value ,mode-doc) | |
143 (make-variable-buffer-local ',mode) | |
144 | |
145 ;; define the minor-mode keymap | |
146 (defvar ,(intern keymap-name) | |
147 (cond ((and ,keymap (keymapp ,keymap)) | |
148 ,keymap) | |
149 ((listp ,keymap) | |
150 (easy-mmode-define-keymap ,keymap)) | |
151 (t (error "Invalid keymap %S" ,keymap))) | |
152 ,keymap-doc) | |
153 | |
154 ;; define the toggle and the hooks | |
155 ,(macroexpand `(easy-mmode-define-toggle ,mode ,doc)) ; toggle and hooks | |
156 | |
157 ;; update the mode-bar | |
158 (or (assq ',mode minor-mode-alist) | |
159 (setq minor-mode-alist | |
160 (cons (list ',mode ,lighter) minor-mode-alist))) | |
161 | |
162 ;; update the minor-mode-map | |
163 (or (assq ',mode minor-mode-map-alist) | |
164 (setq minor-mode-map-alist | |
165 (cons (cons ',mode ,(intern keymap-name)) minor-mode-map-alist)))) )) | |
166 | |
167 (provide 'easy-mmode) | |
168 | |
169 ;;; easy-mmode.el ends here |