]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/easymenu.el
Fix make-obsolete for internal-get-face.
[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 (defcustom easy-menu-precalculate-equivalent-keybindings t
34 "Determine when equivalent key bindings are computed for easy-menu menus.
35 It can take some time to calculate the equivalent key bindings that are shown
36 in a menu. If the variable is on, then this calculation gives a (maybe
37 noticeable) delay when a mode is first entered. If the variable is off, then
38 this delay will come when a menu is displayed the first time. If you never use
39 menus, turn this variable off, otherwise it is probably better to keep it on."
40 :type 'boolean
41 :group 'menu
42 :version "20.3")
43
44 ;;;###autoload
45 (defmacro easy-menu-define (symbol maps doc menu)
46 "Define a menu bar submenu in maps MAPS, according to MENU.
47 The menu keymap is stored in symbol SYMBOL, both as its value
48 and as its function definition. DOC is used as the doc string for SYMBOL.
49
50 The first element of MENU must be a string. It is the menu bar item name.
51 It may be followed by the following keyword argument pairs
52
53 :filter FUNCTION
54
55 FUNCTION is a function with one argument, the menu. It returns the actual
56 menu displayed.
57
58 :visible INCLUDE
59
60 INCLUDE is an expression; this menu is only visible if this
61 expression has a non-nil value. `:include' is an alias for `:visible'.
62
63 :active ENABLE
64
65 ENABLE is an expression; the menu is enabled for selection
66 whenever this expression's value is non-nil.
67
68 The rest of the elements in MENU, are menu items.
69
70 A menu item is usually a vector of three elements: [NAME CALLBACK ENABLE]
71
72 NAME is a string--the menu item name.
73
74 CALLBACK is a command to run when the item is chosen,
75 or a list to evaluate when the item is chosen.
76
77 ENABLE is an expression; the item is enabled for selection
78 whenever this expression's value is non-nil.
79
80 Alternatively, a menu item may have the form:
81
82 [ NAME CALLBACK [ KEYWORD ARG ] ... ]
83
84 Where KEYWORD is one of the symbols defined below.
85
86 :keys KEYS
87
88 KEYS is a string; a complex keyboard equivalent to this menu item.
89 This is normally not needed because keyboard equivalents are usually
90 computed automatically.
91 KEYS is expanded with `substitute-command-keys' before it is used.
92
93 :key-sequence KEYS
94
95 KEYS is nil a string or a vector; nil or a keyboard equivalent to this
96 menu item.
97 This is a hint that will considerably speed up Emacs first display of
98 a menu. Use `:key-sequence nil' when you know that this menu item has no
99 keyboard equivalent.
100
101 :active ENABLE
102
103 ENABLE is an expression; the item is enabled for selection
104 whenever this expression's value is non-nil.
105
106 :included INCLUDE
107
108 INCLUDE is an expression; this item is only visible if this
109 expression has a non-nil value.
110
111 :suffix NAME
112
113 NAME is a string; the name of an argument to CALLBACK.
114
115 :style STYLE
116
117 STYLE is a symbol describing the type of menu item. The following are
118 defined:
119
120 toggle: A checkbox.
121 Prepend the name with `(*) ' or `( ) ' depending on if selected or not.
122 radio: A radio button.
123 Prepend the name with `[X] ' or `[ ] ' depending on if selected or not.
124 button: Surround the name with `[' and `]'. Use this for an item in the
125 menu bar itself.
126 anything else means an ordinary menu item.
127
128 :selected SELECTED
129
130 SELECTED is an expression; the checkbox or radio button is selected
131 whenever this expression's value is non-nil.
132
133 :help HELP
134
135 HELP is a string, the help to display for the menu item.
136
137 A menu item can be a string. Then that string appears in the menu as
138 unselectable text. A string consisting solely of hyphens is displayed
139 as a solid horizontal line.
140
141 A menu item can be a list with the same format as MENU. This is a submenu."
142 `(progn
143 (defvar ,symbol nil ,doc)
144 (easy-menu-do-define (quote ,symbol) ,maps ,doc ,menu)))
145
146 ;;;###autoload
147 (defun easy-menu-do-define (symbol maps doc menu)
148 ;; We can't do anything that might differ between Emacs dialects in
149 ;; `easy-menu-define' in order to make byte compiled files
150 ;; compatible. Therefore everything interesting is done in this
151 ;; function.
152 (set symbol (easy-menu-create-menu (car menu) (cdr menu)))
153 (fset symbol `(lambda (event) ,doc (interactive "@e")
154 (x-popup-menu event ,symbol)))
155 (mapcar (function (lambda (map)
156 (define-key map (vector 'menu-bar (intern (car menu)))
157 (cons (car menu) (symbol-value symbol)))))
158 (if (keymapp maps) (list maps) maps)))
159
160 (defun easy-menu-filter-return (menu)
161 "Convert MENU to the right thing to return from a menu filter.
162 MENU is a menu as computed by `easy-menu-define' or `easy-menu-create-menu' or
163 a symbol whose value is such a menu.
164 In Emacs a menu filter must return a menu (a keymap), in XEmacs a filter must
165 return a menu items list (without menu name and keywords).
166 This function returns the right thing in the two cases."
167 (easy-menu-get-map menu nil)) ; Get past indirections.
168
169 ;;;###autoload
170 (defun easy-menu-create-menu (menu-name menu-items)
171 "Create a menu called MENU-NAME with items described in MENU-ITEMS.
172 MENU-NAME is a string, the name of the menu. MENU-ITEMS is a list of items
173 possibly preceded by keyword pairs as described in `easy-menu-define'."
174 (let ((menu (make-sparse-keymap menu-name))
175 prop keyword arg label enable filter visible help)
176 ;; Look for keywords.
177 (while (and menu-items (cdr menu-items)
178 (symbolp (setq keyword (car menu-items)))
179 (= ?: (aref (symbol-name keyword) 0)))
180 (setq arg (cadr menu-items))
181 (setq menu-items (cddr menu-items))
182 (cond
183 ((eq keyword :filter) (setq filter arg))
184 ((eq keyword :active) (setq enable (or arg ''nil)))
185 ((eq keyword :label) (setq label arg))
186 ((eq keyword :help) (setq help arg))
187 ((or (eq keyword :included) (eq keyword :visible))
188 (setq visible (or arg ''nil)))))
189 (if (equal visible ''nil) nil ; Invisible menu entry, return nil.
190 (if (and visible (not (easy-menu-always-true visible)))
191 (setq prop (cons :visible (cons visible prop))))
192 (if (and enable (not (easy-menu-always-true enable)))
193 (setq prop (cons :enable (cons enable prop))))
194 (if filter (setq prop (cons :filter (cons filter prop))))
195 (if help (setq prop (cons :help (cons help prop))))
196 (if label (setq prop (cons nil (cons label prop))))
197 (while menu-items
198 (easy-menu-do-add-item menu (car menu-items))
199 (setq menu-items (cdr menu-items)))
200 (when prop
201 (setq menu (easy-menu-make-symbol menu))
202 (put menu 'menu-prop prop))
203 menu)))
204
205
206 ;; Known button types.
207 (defvar easy-menu-button-prefix
208 '((radio . :radio) (toggle . :toggle)))
209
210 (defun easy-menu-do-add-item (menu item &optional before)
211 ;; Parse an item description and add the item to a keymap. This is
212 ;; the function that is used for item definition by the other easy-menu
213 ;; functions.
214 ;; MENU is a sparse keymap i.e. a list starting with the symbol `keymap'.
215 ;; ITEM defines an item as in `easy-menu-define'.
216 ;; Optional argument BEFORE is nil or a key in MENU. If BEFORE is not nil
217 ;; put item before BEFORE in MENU, otherwise if item is already present in
218 ;; MENU, just change it, otherwise put it last in MENU.
219 (let (name command label prop remove help)
220 (cond
221 ((stringp item) ; An item or separator.
222 (setq label item))
223 ((consp item) ; A sub-menu
224 (setq label (setq name (car item)))
225 (setq command (cdr item))
226 (if (not (keymapp command))
227 (setq command (easy-menu-create-menu name command)))
228 (if (null command)
229 ;; Invisible menu item. Don't insert into keymap.
230 (setq remove t)
231 (when (and (symbolp command) (setq prop (get command 'menu-prop)))
232 (when (null (car prop))
233 (setq label (cadr prop))
234 (setq prop (cddr prop)))
235 (setq command (symbol-function command)))))
236 ((vectorp item) ; An item.
237 (let* ((ilen (length item))
238 (active (if (> ilen 2) (or (aref item 2) ''nil) t))
239 (no-name (not (symbolp (setq command (aref item 1)))))
240 cache cache-specified)
241 (setq label (setq name (aref item 0)))
242 (if no-name (setq command (easy-menu-make-symbol command)))
243 (if (and (symbolp active) (= ?: (aref (symbol-name active) 0)))
244 (let ((count 2)
245 keyword arg suffix visible style selected keys)
246 (setq active nil)
247 (while (> ilen count)
248 (setq keyword (aref item count))
249 (setq arg (aref item (1+ count)))
250 (setq count (+ 2 count))
251 (cond
252 ((or (eq keyword :included) (eq keyword :visible))
253 (setq visible (or arg ''nil)))
254 ((eq keyword :key-sequence)
255 (setq cache arg cache-specified t))
256 ((eq keyword :keys) (setq keys arg no-name nil))
257 ((eq keyword :label) (setq label arg))
258 ((eq keyword :active) (setq active (or arg ''nil)))
259 ((eq keyword :help) (setq prop (cons :help (cons arg prop))))
260 ((eq keyword :suffix) (setq suffix arg))
261 ((eq keyword :style) (setq style arg))
262 ((eq keyword :selected) (setq selected (or arg ''nil)))))
263 (if suffix
264 (setq label
265 (if (stringp suffix)
266 (if (stringp label) (concat label " " suffix)
267 (list 'concat label (concat " " suffix)))
268 (if (stringp label)
269 (list 'concat (concat label " ") suffix)
270 (list 'concat label " " suffix)))))
271 (cond
272 ((eq style 'button)
273 (setq label (if (stringp label) (concat "[" label "]")
274 (list 'concat "[" label "]"))))
275 ((and selected
276 (setq style (assq style easy-menu-button-prefix)))
277 (setq prop (cons :button
278 (cons (cons (cdr style) selected) prop)))))
279 (when (stringp keys)
280 (if (string-match "^[^\\]*\\(\\\\\\[\\([^]]+\\)]\\)[^\\]*$"
281 keys)
282 (let ((prefix
283 (if (< (match-beginning 0) (match-beginning 1))
284 (substring keys 0 (match-beginning 1))))
285 (postfix
286 (if (< (match-end 1) (match-end 0))
287 (substring keys (match-end 1))))
288 (cmd (intern (substring keys (match-beginning 2)
289 (match-end 2)))))
290 (setq keys (and (or prefix postfix)
291 (cons prefix postfix)))
292 (setq keys
293 (and (or keys (not (eq command cmd)))
294 (cons cmd keys))))
295 (setq cache-specified nil))
296 (if keys (setq prop (cons :keys (cons keys prop)))))
297 (if (and visible (not (easy-menu-always-true visible)))
298 (if (equal visible ''nil)
299 ;; Invisible menu item. Don't insert into keymap.
300 (setq remove t)
301 (setq prop (cons :visible (cons visible prop)))))))
302 (if (and active (not (easy-menu-always-true active)))
303 (setq prop (cons :enable (cons active prop))))
304 (if (and (or no-name cache-specified)
305 (or (null cache) (stringp cache) (vectorp cache)))
306 (setq prop (cons :key-sequence (cons cache prop))))))
307 (t (error "Invalid menu item in easymenu")))
308 (easy-menu-define-key-intern menu name
309 (and (not remove)
310 (cons 'menu-item
311 (cons label
312 (and name
313 (cons command prop)))))
314 before)))
315
316 (defun easy-menu-define-key-intern (menu key item &optional before)
317 ;; This is the same as easy-menu-define-key, but it interns KEY and
318 ;; BEFORE if they are strings.
319 (easy-menu-define-key menu (if (stringp key) (intern key) key) item
320 (if (stringp before) (intern before) before)))
321
322 (defun easy-menu-define-key (menu key item &optional before)
323 ;; Add binding in MENU for KEY => ITEM. Similar to `define-key-after'.
324 ;; If KEY is not nil then delete any duplications. If ITEM is nil, then
325 ;; don't insert, only delete.
326 ;; Optional argument BEFORE is nil or a key in MENU. If BEFORE is not nil
327 ;; put binding before BEFORE in MENU, otherwise if binding is already
328 ;; present in MENU, just change it, otherwise put it last in MENU.
329 ;; KEY and BEFORE don't have to be symbols, comparison is done with equal
330 ;; not with eq.
331 (let ((inserted (null item)) ; Fake already inserted.
332 tail done)
333 (while (not done)
334 (cond
335 ((or (setq done (or (null (cdr menu)) (keymapp (cdr menu))))
336 (and before (equal (car-safe (cadr menu)) before)))
337 ;; If key is nil, stop here, otherwise keep going past the
338 ;; inserted element so we can delete any duplications that come
339 ;; later.
340 (if (null key) (setq done t))
341 (unless inserted ; Don't insert more than once.
342 (setcdr menu (cons (cons key item) (cdr menu)))
343 (setq inserted t)
344 (setq menu (cdr menu)))
345 (setq menu (cdr menu)))
346 ((and key (equal (car-safe (cadr menu)) key))
347 (if (or inserted ; Already inserted or
348 (and before ; wanted elsewhere and
349 (setq tail (cddr menu)) ; not last item and not
350 (not (keymapp tail))
351 (not (equal (car-safe (car tail)) before)))) ; in position
352 (setcdr menu (cddr menu)) ; Remove item.
353 (setcdr (cadr menu) item) ; Change item.
354 (setq inserted t)
355 (setq menu (cdr menu))))
356 (t (setq menu (cdr menu)))))))
357
358 (defun easy-menu-always-true (x)
359 ;; Return true if X never evaluates to nil.
360 (if (consp x) (and (eq (car x) 'quote) (cadr x))
361 (or (eq x t) (not (symbolp x)))))
362
363 (defvar easy-menu-item-count 0)
364
365 (defun easy-menu-make-symbol (callback)
366 ;; Return a unique symbol with CALLBACK as function value.
367 (let ((command
368 (make-symbol (format "menu-function-%d" easy-menu-item-count))))
369 (setq easy-menu-item-count (1+ easy-menu-item-count))
370 (fset command
371 (if (keymapp callback) callback
372 `(lambda () (interactive) ,callback)))
373 command))
374
375 ;;;###autoload
376 (defun easy-menu-change (path name items &optional before)
377 "Change menu found at PATH as item NAME to contain ITEMS.
378 PATH is a list of strings for locating the menu that
379 should contain a submenu named NAME.
380 ITEMS is a list of menu items, as in `easy-menu-define'.
381 These items entirely replace the previous items in that submenu.
382
383 If the menu located by PATH has no submenu named NAME, add one.
384 If the optional argument BEFORE is present, add it just before
385 the submenu named BEFORE, otherwise add it at the end of the menu.
386
387 Either call this from `menu-bar-update-hook' or use a menu filter,
388 to implement dynamic menus."
389 (easy-menu-add-item nil path (cons name items) before))
390
391 ;; XEmacs needs the following two functions to add and remove menus.
392 ;; In Emacs this is done automatically when switching keymaps, so
393 ;; here easy-menu-remove is a noop and easy-menu-add only precalculates
394 ;; equivalent keybindings (if easy-menu-precalculate-equivalent-keybindings
395 ;; is on).
396 (defun easy-menu-remove (menu))
397
398 (defun easy-menu-add (menu &optional map)
399 "Maybe precalculate equivalent key bindings.
400 Do it if `easy-menu-precalculate-equivalent-keybindings' is on,"
401 (when easy-menu-precalculate-equivalent-keybindings
402 (if (and (symbolp menu) (not (keymapp menu)) (boundp menu))
403 (setq menu (symbol-value menu)))
404 (if (keymapp menu) (x-popup-menu nil menu))))
405
406 (defun easy-menu-add-item (map path item &optional before)
407 "To the submenu of MAP with path PATH, add ITEM.
408
409 If an item with the same name is already present in this submenu,
410 then ITEM replaces it. Otherwise, ITEM is added to this submenu.
411 In the latter case, ITEM is normally added at the end of the submenu.
412 However, if BEFORE is a string and there is an item in the submenu
413 with that name, then ITEM is added before that item.
414
415 MAP should normally be a keymap; nil stands for the global menu-bar keymap.
416 It can also be a symbol, which has earlier been used as the first
417 argument in a call to `easy-menu-define', or the value of such a symbol.
418
419 PATH is a list of strings for locating the submenu where ITEM is to be
420 added. If PATH is nil, MAP itself is used. Otherwise, the first
421 element should be the name of a submenu directly under MAP. This
422 submenu is then traversed recursively with the remaining elements of PATH.
423
424 ITEM is either defined as in `easy-menu-define' or a non-nil value returned
425 by `easy-menu-item-present-p' or `easy-menu-remove-item' or a menu defined
426 earlier by `easy-menu-define' or `easy-menu-create-menu'."
427 (setq map (easy-menu-get-map map path
428 (and (null map) (null path)
429 (stringp (car-safe item))
430 (car item))))
431 (if (and (consp item) (consp (cdr item)) (eq (cadr item) 'menu-item))
432 ;; This is a value returned by `easy-menu-item-present-p' or
433 ;; `easy-menu-remove-item'.
434 (easy-menu-define-key-intern map (car item) (cdr item) before)
435 (if (or (keymapp item)
436 (and (symbolp item) (keymapp (symbol-value item))))
437 ;; Item is a keymap, find the prompt string and use as item name.
438 (let ((tail (easy-menu-get-map item nil)) name)
439 (if (not (keymapp item)) (setq item tail))
440 (while (and (null name) (consp (setq tail (cdr tail)))
441 (not (keymapp tail)))
442 (if (stringp (car tail)) (setq name (car tail)) ; Got a name.
443 (setq tail (cdr tail))))
444 (setq item (cons name item))))
445 (easy-menu-do-add-item map item before)))
446
447 (defun easy-menu-item-present-p (map path name)
448 "In submenu of MAP with path PATH, return true iff item NAME is present.
449 MAP and PATH are defined as in `easy-menu-add-item'.
450 NAME should be a string, the name of the element to be looked for."
451 (easy-menu-return-item (easy-menu-get-map map path) name))
452
453 (defun easy-menu-remove-item (map path name)
454 "From submenu of MAP with path PATH remove item NAME.
455 MAP and PATH are defined as in `easy-menu-add-item'.
456 NAME should be a string, the name of the element to be removed."
457 (setq map (easy-menu-get-map map path))
458 (let ((ret (easy-menu-return-item map name)))
459 (if ret (easy-menu-define-key-intern map name nil))
460 ret))
461
462 (defun easy-menu-return-item (menu name)
463 ;; In menu MENU try to look for menu item with name NAME.
464 ;; If a menu item is found, return (NAME . item), otherwise return nil.
465 ;; If item is an old format item, a new format item is returned.
466 (let ((item (lookup-key menu (vector (intern name))))
467 ret enable cache label)
468 (cond
469 ((or (keymapp item) (eq (car-safe item) 'menu-item))
470 (cons name item)) ; Keymap or new menu format
471 ((stringp (car-safe item))
472 ;; This is the old menu format. Convert it to new format.
473 (setq label (car item))
474 (when (stringp (car (setq item (cdr item)))) ; Got help string
475 (setq ret (list :help (car item)))
476 (setq item (cdr item)))
477 (when (and (consp item) (consp (car item))
478 (or (null (caar item)) (numberp (caar item))))
479 (setq cache (car item)) ; Got cache
480 (setq item (cdr item)))
481 (and (symbolp item) (setq enable (get item 'menu-enable)) ; Got enable
482 (setq ret (cons :enable (cons enable ret))))
483 (if cache (setq ret (cons cache ret)))
484 (cons name (cons 'menu-enable (cons label (cons item ret))))))))
485
486 (defun easy-menu-get-map-look-for-name (name submap)
487 (while (and submap (not (or (equal (car-safe (cdr-safe (car submap))) name)
488 (equal (car-safe (cdr-safe (cdr-safe (car submap)))) name))))
489 (setq submap (cdr submap)))
490 submap)
491
492 (defun easy-menu-get-map (map path &optional to-modify)
493 ;; Return a sparse keymap in which to add or remove an item.
494 ;; MAP and PATH are as defined in `easy-menu-add-item'.
495
496 ;; TO-MODIFY, if non-nil, is the name of the item the caller
497 ;; wants to modify in the map that we return.
498 ;; In some cases we use that to select between the local and global maps.
499 (if (null map)
500 (let ((local (and (current-local-map)
501 (lookup-key (current-local-map)
502 (vconcat '(menu-bar) (mapcar 'intern path)))))
503 (global (lookup-key global-map
504 (vconcat '(menu-bar) (mapcar 'intern path)))))
505 (cond ((and to-modify local (not (integerp local))
506 (easy-menu-get-map-look-for-name to-modify local))
507 (setq map local))
508 ((and to-modify global (not (integerp global))
509 (easy-menu-get-map-look-for-name to-modify global))
510 (setq map global))
511 ((and local local (not (integerp local)))
512 (setq map local))
513 ((and global (not (integerp global)))
514 (setq map global))
515 (t
516 (setq map (make-sparse-keymap))
517 (define-key (current-local-map)
518 (vconcat '(menu-bar) (mapcar 'intern path)) map))))
519 (if (and (symbolp map) (not (keymapp map)))
520 (setq map (symbol-value map)))
521 (if path (setq map (lookup-key map (vconcat (mapcar 'intern path))))))
522 (while (and (symbolp map) (keymapp map))
523 (setq map (symbol-function map)))
524 (unless map
525 (error "Menu specified in easy-menu is not defined"))
526 (or (keymapp map) (error "Malformed menu in easy-menu: (%s)" map))
527 map)
528
529 (provide 'easymenu)
530
531 ;;; easymenu.el ends here