]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/easymenu.el
(easy-menu-define-key): Fixed bug with BEFORE
[gnu-emacs] / lisp / emacs-lisp / easymenu.el
1 ;;; easymenu.el --- support the easymenu interface for defining a menu.
2
3 ;; Copyright (C) 1994, 1996, 1998 Free Software Foundation, Inc.
4
5 ;; Keywords: emulations
6 ;; Author: rms
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 ;; This is compatible with easymenu.el by Per Abrahamsen
28 ;; but it is much simpler as it doesn't try to support other Emacs versions.
29 ;; The code was mostly derived from lmenu.el.
30
31 ;;; Code:
32
33 ;;;###autoload
34 (defmacro easy-menu-define (symbol maps doc menu)
35 "Define a menu bar submenu in maps MAPS, according to MENU.
36 The menu keymap is stored in symbol SYMBOL, both as its value
37 and as its function definition. DOC is used as the doc string for SYMBOL.
38
39 The first element of MENU must be a string. It is the menu bar item name.
40 It may be followed by the keyword argument pair
41 :filter FUNCTION
42 FUNCTION is a function with one argument, the menu. It returns the actual
43 menu displayed.
44
45 The rest of the elements are menu items.
46
47 A menu item is usually a vector of three elements: [NAME CALLBACK ENABLE]
48
49 NAME is a string--the menu item name.
50
51 CALLBACK is a command to run when the item is chosen,
52 or a list to evaluate when the item is chosen.
53
54 ENABLE is an expression; the item is enabled for selection
55 whenever this expression's value is non-nil.
56
57 Alternatively, a menu item may have the form:
58
59 [ NAME CALLBACK [ KEYWORD ARG ] ... ]
60
61 Where KEYWORD is one of the symbols defined below.
62
63 :keys KEYS
64
65 KEYS is a string; a complex keyboard equivalent to this menu item.
66 This is normally not needed because keyboard equivalents are usually
67 computed automatically.
68
69 :active ENABLE
70
71 ENABLE is an expression; the item is enabled for selection
72 whenever this expression's value is non-nil.
73
74 :suffix NAME
75
76 NAME is a string; the name of an argument to CALLBACK.
77
78 :style STYLE
79
80 STYLE is a symbol describing the type of menu item. The following are
81 defined:
82
83 toggle: A checkbox.
84 Prepend the name with '(*) ' or '( ) ' depending on if selected or not.
85 radio: A radio button.
86 Prepend the name with '[X] ' or '[ ] ' depending on if selected or not.
87 nil: An ordinary menu item.
88
89 :selected SELECTED
90
91 SELECTED is an expression; the checkbox or radio button is selected
92 whenever this expression's value is non-nil.
93
94 A menu item can be a string. Then that string appears in the menu as
95 unselectable text. A string consisting solely of hyphens is displayed
96 as a solid horizontal line.
97
98 A menu item can be a list. It is treated as a submenu.
99 The first element should be the submenu name. That's used as the
100 menu item name in the top-level menu. It may be followed by the :filter
101 FUNCTION keyword argument pair. The rest of the submenu list are menu items,
102 as above."
103 `(progn
104 (defvar ,symbol nil ,doc)
105 (easy-menu-do-define (quote ,symbol) ,maps ,doc ,menu)))
106
107 ;;;###autoload
108 (defun easy-menu-do-define (symbol maps doc menu)
109 ;; We can't do anything that might differ between Emacs dialects in
110 ;; `easy-menu-define' in order to make byte compiled files
111 ;; compatible. Therefore everything interesting is done in this
112 ;; function.
113 (set symbol (easy-menu-create-menu (car menu) (cdr menu)))
114 (fset symbol (` (lambda (event) (, doc) (interactive "@e")
115 (x-popup-menu event (, symbol)))))
116 (mapcar (function (lambda (map)
117 (define-key map (vector 'menu-bar (intern (car menu)))
118 (cons (car menu) (symbol-value symbol)))))
119 (if (keymapp maps) (list maps) maps)))
120
121 (defun easy-menu-filter-return (menu)
122 "Convert MENU to the right thing to return from a menu filter.
123 MENU is a menu as computed by `easy-menu-define' or `easy-menu-create-menu' or
124 a symbol whose value is such a menu.
125 In Emacs a menu filter must return a menu (a keymap), in XEmacs a filter must
126 return a menu items list (without menu name and keywords). This function
127 returns the right thing in the two cases."
128 (easy-menu-get-map menu nil)) ; Get past indirections.
129
130 ;;;###autoload
131 (defun easy-menu-create-menu (menu-name menu-items)
132 "Create a menu called MENU-NAME with items described in MENU-ITEMS.
133 MENU-NAME is a string, the name of the menu. MENU-ITEMS is a list of items
134 possibly preceded by keyword pairs as described in `easy-menu-define'."
135 (let ((menu (make-sparse-keymap menu-name))
136 prop keyword arg label enable filter visible)
137 ;; Look for keywords.
138 (while (and menu-items (cdr menu-items)
139 (symbolp (setq keyword (car menu-items)))
140 (= ?: (aref (symbol-name keyword) 0)))
141 (setq arg (cadr menu-items))
142 (setq menu-items (cddr menu-items))
143 (cond
144 ((eq keyword ':filter) (setq filter arg))
145 ((eq keyword ':active) (setq enable (or arg ''nil)))
146 ((eq keyword ':label) (setq label arg))
147 ((eq keyword ':visible) (setq visible (or arg ''nil)))))
148 (if (equal visible ''nil) nil ; Invisible menu entry, return nil.
149 (if (and visible (not (easy-menu-always-true visible)))
150 (setq prop (cons :visible (cons visible prop))))
151 (if (and enable (not (easy-menu-always-true enable)))
152 (setq prop (cons :enable (cons enable prop))))
153 (if filter (setq prop (cons :filter (cons filter prop))))
154 (if label (setq prop (cons nil (cons label prop))))
155 (while menu-items
156 (easy-menu-do-add-item menu (car menu-items))
157 (setq menu-items (cdr menu-items)))
158 (when prop
159 (setq menu (easy-menu-make-symbol menu))
160 (put menu 'menu-prop prop))
161 menu)))
162
163
164 ;; Button prefixes.
165 (defvar easy-menu-button-prefix
166 '((radio . :radio) (toggle . :toggle)))
167
168 (defun easy-menu-do-add-item (menu item &optional before)
169 ;; Parse an item description and add the item to a keymap. This is
170 ;; the function that is used for item definition by the other easy-menu
171 ;; functions.
172 ;; MENU is a sparse keymap i.e. a list starting with the symbol `keymap'.
173 ;; ITEM defines an item as in `easy-menu-define'.
174 ;; Optional argument BEFORE is nil or a key in MENU. If BEFORE is not nil
175 ;; put item before BEFORE in MENU, otherwise if item is already present in
176 ;; MENU, just change it, otherwise put it last in MENU.
177 (let (name command label prop remove)
178 (cond
179 ((stringp item)
180 (setq label
181 (if (string-match ; If an XEmacs separator
182 "^\\(-+\\|\
183 --:\\(\\(no\\|\\(sing\\|doub\\)le\\(Dashed\\)?\\)Line\\|\
184 shadow\\(Double\\)?Etched\\(In\\|Out\\)\\(Dash\\)?\\)\\)$"
185 item) "" ; use a single line separator.
186 item)))
187 ((consp item)
188 (setq label (setq name (car item)))
189 (setq command (cdr item))
190 (if (not (keymapp command))
191 (setq command (easy-menu-create-menu name command)))
192 (if (null command)
193 ;; Invisible menu item. Don't insert into keymap.
194 (setq remove t)
195 (when (and (symbolp command) (setq prop (get command 'menu-prop)))
196 (when (null (car prop))
197 (setq label (cadr prop))
198 (setq prop (cddr prop)))
199 (setq command (symbol-function command)))))
200 ((vectorp item)
201 (let ((active (if (> (length item) 2) (or (aref item 2) ''nil) t))
202 (no-name (not (symbolp (setq command (aref item 1)))))
203 cache cache-specified
204 (count 2))
205 (setq label (setq name (aref item 0)))
206 (if no-name (setq command (easy-menu-make-symbol command)))
207 (if (and (symbolp active) (= ?: (aref (symbol-name active) 0)))
208 (let ((count 2)
209 keyword arg suffix visible style selected keys)
210 (setq active nil)
211 (while (> (length item) count)
212 (setq keyword (aref item count))
213 (setq arg (aref item (1+ count)))
214 (setq count (+ 2 count))
215 (cond
216 ((eq keyword :visible) (setq visible (or arg ''nil)))
217 ((eq keyword :key-sequence)
218 (setq cache arg cache-specified t))
219 ((eq keyword :keys) (setq keys arg no-name nil))
220 ((eq keyword :label) (setq label arg))
221 ((eq keyword :active) (setq active (or arg ''nil)))
222 ((eq keyword :suffix) (setq suffix arg))
223 ((eq keyword :style) (setq style arg))
224 ((eq keyword :selected) (setq selected (or arg ''nil)))))
225 (if (stringp suffix)
226 (setq label (if (stringp label) (concat label " " suffix)
227 (list 'concat label (concat " " suffix)))))
228 (if (and selected
229 (setq style (assq style easy-menu-button-prefix)))
230 (setq prop (cons :button
231 (cons (cons (cdr style) (or selected ''nil))
232 prop))))
233 (when (stringp keys)
234 (if (string-match "^[^\\]*\\(\\\\\\[\\([^]]+\\)]\\)[^\\]*$"
235 keys)
236 (let ((prefix
237 (if (< (match-beginning 0) (match-beginning 1))
238 (substring keys 0 (match-beginning 1))))
239 (postfix
240 (if (< (match-end 1) (match-end 0))
241 (substring keys (match-end 1))))
242 (cmd (intern (substring keys (match-beginning 2)
243 (match-end 2)))))
244 (setq keys
245 (and (or prefix postfix (not (eq command cmd)))
246 (cons cmd
247 (and (or prefix postfix)
248 (cons prefix postfix))))))
249 (setq cache-specified nil))
250 (if keys (setq prop (cons :keys (cons keys prop)))))
251 (if (and visible (not (easy-menu-always-true visible)))
252 (if (equal visible ''nil)
253 ;; Invisible menu item. Don't insert into keymap.
254 (setq remove t)
255 (setq prop (cons :visible (cons visible prop)))))))
256 (if (and active (not (easy-menu-always-true active)))
257 (setq prop (cons :enable (cons active prop))))
258 (if (and (or no-name cache-specified)
259 (or (null cache) (stringp cache) (vectorp cache)))
260 (setq prop (cons :key-sequence (cons cache prop))))))
261 (t (error "Invalid menu item in easymenu.")))
262 (easy-menu-define-key menu (if (stringp name) (intern name) name)
263 (and (not remove)
264 (cons 'menu-item
265 (cons label
266 (and name (cons command prop)))))
267 (if (stringp before) (intern before) before))))
268
269 (defun easy-menu-define-key (menu key item &optional before)
270 ;; Add binding in MENU for KEY => ITEM. Similar to `define-key-after'.
271 ;; If KEY is not nil then delete any duplications. If ITEM is nil, then
272 ;; don't insert, only delete.
273 ;; Optional argument BEFORE is nil or a key in MENU. If BEFORE is not nil
274 ;; put binding before BEFORE in MENU, otherwise if binding is already
275 ;; present in MENU, just change it, otherwise put it last in MENU.
276 (let ((inserted (null item)) ; Fake already inserted.
277 tail done)
278 (while (not done)
279 (cond
280 ((or (setq done (or (null (cdr menu)) (keymapp (cdr menu))))
281 (and before (equal (car-safe (cadr menu)) before)))
282 ;; If key is nil, stop here, otherwise keep going past the
283 ;; inserted element so we can delete any duplications that come
284 ;; later.
285 (if (null key) (setq done t))
286 (unless inserted ; Don't insert more than once.
287 (setcdr menu (cons (cons key item) (cdr menu)))
288 (setq inserted t)
289 (setq menu (cdr menu)))
290 (setq menu (cdr menu)))
291 ((and key (equal (car-safe (cadr menu)) key))
292 (if (or inserted ; Already inserted or
293 (and before ; wanted elsewhere and
294 (setq tail (cddr menu)) ; not last item and not
295 (not (keymapp tail))
296 (not (equal (car-safe (car tail)) before)))) ; in position
297 (setcdr menu (cddr menu)) ; Remove item.
298 (setcdr (cadr menu) item) ; Change item.
299 (setq inserted t)
300 (setq menu (cdr menu))))
301 (t (setq menu (cdr menu)))))))
302
303 (defun easy-menu-always-true (x)
304 ;; Return true if X never evaluates to nil.
305 (if (consp x) (and (eq (car x) 'quote) (cadr x))
306 (or (eq x t) (not (symbolp x)))))
307
308 (defvar easy-menu-item-count 0)
309
310 (defun easy-menu-make-symbol (callback)
311 ;; Return a unique symbol with CALLBACK as function value.
312 (let ((command
313 (make-symbol (format "menu-function-%d" easy-menu-item-count))))
314 (setq easy-menu-item-count (1+ easy-menu-item-count))
315 (fset command
316 (if (keymapp callback) callback
317 `(lambda () (interactive) ,callback)))
318 command))
319
320 (defun easy-menu-change (path name items &optional before)
321 "Change menu found at PATH as item NAME to contain ITEMS.
322 PATH is a list of strings for locating the menu containing NAME in the
323 menu bar. ITEMS is a list of menu items, as in `easy-menu-define'.
324 These items entirely replace the previous items in that map.
325 If NAME is not present in the menu located by PATH, then add item NAME to
326 that menu. If the optional argument BEFORE is present add NAME in menu
327 just before BEFORE, otherwise add at end of menu.
328
329 Either call this from `menu-bar-update-hook' or use a menu filter,
330 to implement dynamic menus."
331 (easy-menu-add-item nil path (cons name items) before))
332
333 ;; XEmacs needs the following two functions to add and remove menus.
334 ;; In Emacs this is done automatically when switching keymaps, so
335 ;; here these functions are noops.
336 (defun easy-menu-remove (menu))
337
338 (defun easy-menu-add (menu &optional map))
339
340 (defun easy-menu-add-item (menu path item &optional before)
341 "At the end of the submenu of MENU with path PATH add ITEM.
342 If ITEM is already present in this submenu, then this item will be changed.
343 otherwise ITEM will be added at the end of the submenu, unless the optional
344 argument BEFORE is present, in which case ITEM will instead be added
345 before the item named BEFORE.
346 MENU is either a symbol, which have earlier been used as the first
347 argument in a call to `easy-menu-define', or the value of such a symbol
348 i.e. a menu, or nil which stands for the menu-bar itself.
349 PATH is a list of strings for locating the submenu where ITEM is to be
350 added. If PATH is nil, MENU itself is used. Otherwise, the first
351 element should be the name of a submenu directly under MENU. This
352 submenu is then traversed recursively with the remaining elements of PATH.
353 ITEM is either defined as in `easy-menu-define' or a menu defined earlier
354 by `easy-menu-define' or `easy-menu-create-menu'."
355 (setq menu (easy-menu-get-map menu path))
356 (if (or (keymapp item)
357 (and (symbolp item) (keymapp (symbol-value item))))
358 ;; Item is a keymap, find the prompt string and use as item name.
359 (let ((tail (easy-menu-get-map item nil)) name)
360 (if (not (keymapp item)) (setq item tail))
361 (while (and (null name) (consp (setq tail (cdr tail)))
362 (not (keymapp tail)))
363 (if (stringp (car tail)) (setq name (car tail)) ; Got a name.
364 (setq tail (cdr tail))))
365 (setq item (cons name item))))
366 (easy-menu-do-add-item menu item before))
367
368 (defun easy-menu-item-present-p (menu path name)
369 "In submenu of MENU with path PATH, return true iff item NAME is present.
370 MENU and PATH are defined as in `easy-menu-add-item'.
371 NAME should be a string, the name of the element to be looked for."
372 (lookup-key (easy-menu-get-map menu path) (vector (intern name))))
373
374 (defun easy-menu-remove-item (menu path name)
375 "From submenu of MENU with path PATH remove item NAME.
376 MENU and PATH are defined as in `easy-menu-add-item'.
377 NAME should be a string, the name of the element to be removed."
378 (easy-menu-define-key (easy-menu-get-map menu path) (intern name) nil))
379
380 (defun easy-menu-get-map (menu path)
381 ;; Return a sparse keymap in which to add or remove an item.
382 ;; MENU and PATH are as defined in `easy-menu-add-item'.
383 (if (null menu)
384 (setq menu (key-binding (vconcat '(menu-bar) (mapcar 'intern path))))
385 (if (and (symbolp menu) (not (keymapp menu)))
386 (setq menu (symbol-value menu)))
387 (if path (setq menu (lookup-key menu (vconcat (mapcar 'intern path))))))
388 (while (and (symbolp menu) (keymapp menu))
389 (setq menu (symbol-function menu)))
390 (or (keymapp menu) (error "Malformed menu in easy-menu: (%s)" menu))
391 menu)
392
393 (provide 'easymenu)
394
395 ;;; easymenu.el ends here