]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/easymenu.el
(easy-menu-do-define): Use defalias, not fset.
[gnu-emacs] / lisp / emacs-lisp / easymenu.el
1 ;;; easymenu.el --- support the easymenu interface for defining a menu
2
3 ;; Copyright (C) 1994,96,98,1999,2000,2004 Free Software Foundation, Inc.
4
5 ;; Keywords: emulations
6 ;; Author: Richard Stallman <rms@gnu.org>
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 (defsubst easy-menu-intern (s)
45 (if (stringp s) (intern s) s))
46
47 ;;;###autoload
48 (put 'easy-menu-define 'lisp-indent-function 'defun)
49 ;;;###autoload
50 (defmacro easy-menu-define (symbol maps doc menu)
51 "Define a menu bar submenu in maps MAPS, according to MENU.
52
53 If SYMBOL is non-nil, store the menu keymap in the value of SYMBOL,
54 and define SYMBOL as a function to pop up the menu, with DOC as its doc string.
55 If SYMBOL is nil, just store the menu keymap into MAPS.
56
57 The first element of MENU must be a string. It is the menu bar item name.
58 It may be followed by the following keyword argument pairs
59
60 :filter FUNCTION
61
62 FUNCTION is a function with one argument, the rest of menu items.
63 It returns the remaining items of the displayed menu.
64
65 :visible INCLUDE
66
67 INCLUDE is an expression; this menu is only visible if this
68 expression has a non-nil value. `:include' is an alias for `:visible'.
69
70 :active ENABLE
71
72 ENABLE is an expression; the menu is enabled for selection
73 whenever this expression's value is non-nil.
74
75 The rest of the elements in MENU, are menu items.
76
77 A menu item is usually a vector of three elements: [NAME CALLBACK ENABLE]
78
79 NAME is a string--the menu item name.
80
81 CALLBACK is a command to run when the item is chosen,
82 or a list to evaluate when the item is chosen.
83
84 ENABLE is an expression; the item is enabled for selection
85 whenever this expression's value is non-nil.
86
87 Alternatively, a menu item may have the form:
88
89 [ NAME CALLBACK [ KEYWORD ARG ] ... ]
90
91 Where KEYWORD is one of the symbols defined below.
92
93 :keys KEYS
94
95 KEYS is a string; a complex keyboard equivalent to this menu item.
96 This is normally not needed because keyboard equivalents are usually
97 computed automatically.
98 KEYS is expanded with `substitute-command-keys' before it is used.
99
100 :key-sequence KEYS
101
102 KEYS is nil, a string or a vector; nil or a keyboard equivalent to this
103 menu item.
104 This is a hint that will considerably speed up Emacs' first display of
105 a menu. Use `:key-sequence nil' when you know that this menu item has no
106 keyboard equivalent.
107
108 :active ENABLE
109
110 ENABLE is an expression; the item is enabled for selection
111 whenever this expression's value is non-nil.
112
113 :included INCLUDE
114
115 INCLUDE is an expression; this item is only visible if this
116 expression has a non-nil value.
117
118 :suffix FORM
119
120 FORM is an expression that will be dynamically evaluated and whose
121 value will be concatenated to the menu entry's NAME.
122
123 :style STYLE
124
125 STYLE is a symbol describing the type of menu item. The following are
126 defined:
127
128 toggle: A checkbox.
129 Prepend the name with `(*) ' or `( ) ' depending on if selected or not.
130 radio: A radio button.
131 Prepend the name with `[X] ' or `[ ] ' depending on if selected or not.
132 button: Surround the name with `[' and `]'. Use this for an item in the
133 menu bar itself.
134 anything else means an ordinary menu item.
135
136 :selected SELECTED
137
138 SELECTED is an expression; the checkbox or radio button is selected
139 whenever this expression's value is non-nil.
140
141 :help HELP
142
143 HELP is a string, the help to display for the menu item.
144
145 A menu item can be a string. Then that string appears in the menu as
146 unselectable text. A string consisting solely of hyphens is displayed
147 as a solid horizontal line.
148
149 A menu item can be a list with the same format as MENU. This is a submenu."
150 `(progn
151 ,(if symbol `(defvar ,symbol nil ,doc))
152 (easy-menu-do-define (quote ,symbol) ,maps ,doc ,menu)))
153
154 ;;;###autoload
155 (defun easy-menu-do-define (symbol maps doc menu)
156 ;; We can't do anything that might differ between Emacs dialects in
157 ;; `easy-menu-define' in order to make byte compiled files
158 ;; compatible. Therefore everything interesting is done in this
159 ;; function.
160 (let ((keymap (easy-menu-create-menu (car menu) (cdr menu))))
161 (when symbol
162 (set symbol keymap)
163 (defalias symbol
164 `(lambda (event) ,doc (interactive "@e")
165 ;; FIXME: XEmacs uses popup-menu which calls the binding
166 ;; while x-popup-menu only returns the selection.
167 (x-popup-menu event
168 (or (and (symbolp ,symbol)
169 (funcall
170 (or (plist-get (get ,symbol 'menu-prop)
171 :filter)
172 'identity)
173 (symbol-function ,symbol)))
174 ,symbol)))))
175 (mapcar (lambda (map)
176 (define-key map (vector 'menu-bar (easy-menu-intern (car menu)))
177 (cons 'menu-item
178 (cons (car menu)
179 (if (not (symbolp keymap))
180 (list keymap)
181 (cons (symbol-function keymap)
182 (get keymap 'menu-prop)))))))
183 (if (keymapp maps) (list maps) maps))))
184
185 (defun easy-menu-filter-return (menu &optional name)
186 "Convert MENU to the right thing to return from a menu filter.
187 MENU is a menu as computed by `easy-menu-define' or `easy-menu-create-menu' or
188 a symbol whose value is such a menu.
189 In Emacs a menu filter must return a menu (a keymap), in XEmacs a filter must
190 return a menu items list (without menu name and keywords).
191 This function returns the right thing in the two cases.
192 If NAME is provided, it is used for the keymap."
193 (cond
194 ((and (not (keymapp menu)) (consp menu))
195 ;; If it's a cons but not a keymap, then it can't be right
196 ;; unless it's an XEmacs menu.
197 (setq menu (easy-menu-create-menu (or name "") menu)))
198 ((vectorp menu)
199 ;; It's just a menu entry.
200 (setq menu (cdr (easy-menu-convert-item menu)))))
201 menu)
202
203 ;;;###autoload
204 (defun easy-menu-create-menu (menu-name menu-items)
205 "Create a menu called MENU-NAME with items described in MENU-ITEMS.
206 MENU-NAME is a string, the name of the menu. MENU-ITEMS is a list of items
207 possibly preceded by keyword pairs as described in `easy-menu-define'."
208 (let ((menu (make-sparse-keymap menu-name))
209 prop keyword arg label enable filter visible help)
210 ;; Look for keywords.
211 (while (and menu-items
212 (cdr menu-items)
213 (keywordp (setq keyword (car menu-items))))
214 (setq arg (cadr menu-items))
215 (setq menu-items (cddr menu-items))
216 (cond
217 ((eq keyword :filter)
218 (setq filter `(lambda (menu)
219 (easy-menu-filter-return (,arg menu) ,menu-name))))
220 ((eq keyword :active) (setq enable (or arg ''nil)))
221 ((eq keyword :label) (setq label arg))
222 ((eq keyword :help) (setq help arg))
223 ((or (eq keyword :included) (eq keyword :visible))
224 (setq visible (or arg ''nil)))))
225 (if (equal visible ''nil)
226 nil ; Invisible menu entry, return nil.
227 (if (and visible (not (easy-menu-always-true-p visible)))
228 (setq prop (cons :visible (cons visible prop))))
229 (if (and enable (not (easy-menu-always-true-p enable)))
230 (setq prop (cons :enable (cons enable prop))))
231 (if filter (setq prop (cons :filter (cons filter prop))))
232 (if help (setq prop (cons :help (cons help prop))))
233 (if label (setq prop (cons nil (cons label prop))))
234 (if filter
235 ;; The filter expects the menu in its XEmacs form and the pre-filter
236 ;; form will only be passed to the filter anyway, so we'd better
237 ;; not convert it at all (it will be converted on the fly by
238 ;; easy-menu-filter-return).
239 (setq menu menu-items)
240 (setq menu (append menu (mapcar 'easy-menu-convert-item menu-items))))
241 (when prop
242 (setq menu (easy-menu-make-symbol menu 'noexp))
243 (put menu 'menu-prop prop))
244 menu)))
245
246
247 ;; Known button types.
248 (defvar easy-menu-button-prefix
249 '((radio . :radio) (toggle . :toggle)))
250
251 (defun easy-menu-do-add-item (menu item &optional before)
252 (setq item (easy-menu-convert-item item))
253 (easy-menu-define-key menu (easy-menu-intern (car item)) (cdr item) before))
254
255 (defvar easy-menu-converted-items-table (make-hash-table :test 'equal))
256
257 (defun easy-menu-convert-item (item)
258 "Memoize the value returned by `easy-menu-convert-item-1' called on ITEM.
259 This makes key-shortcut-caching work a *lot* better when this
260 conversion is done from within a filter.
261 This also helps when the NAME of the entry is recreated each time:
262 since the menu is built and traversed separately, the lookup
263 would always fail because the key is `equal' but not `eq'."
264 (or (gethash item easy-menu-converted-items-table)
265 (puthash item (easy-menu-convert-item-1 item)
266 easy-menu-converted-items-table)))
267
268 (defun easy-menu-convert-item-1 (item)
269 "Parse an item description and convert it to a menu keymap element.
270 ITEM defines an item as in `easy-menu-define'."
271 (let (name command label prop remove help)
272 (cond
273 ((stringp item) ; An item or separator.
274 (setq label item))
275 ((consp item) ; A sub-menu
276 (setq label (setq name (car item)))
277 (setq command (cdr item))
278 (if (not (keymapp command))
279 (setq command (easy-menu-create-menu name command)))
280 (if (null command)
281 ;; Invisible menu item. Don't insert into keymap.
282 (setq remove t)
283 (when (and (symbolp command) (setq prop (get command 'menu-prop)))
284 (when (null (car prop))
285 (setq label (cadr prop))
286 (setq prop (cddr prop)))
287 (setq command (symbol-function command)))))
288 ((vectorp item) ; An item.
289 (let* ((ilen (length item))
290 (active (if (> ilen 2) (or (aref item 2) ''nil) t))
291 (no-name (not (symbolp (setq command (aref item 1)))))
292 cache cache-specified)
293 (setq label (setq name (aref item 0)))
294 (if no-name (setq command (easy-menu-make-symbol command)))
295 (if (keywordp active)
296 (let ((count 2)
297 keyword arg suffix visible style selected keys)
298 (setq active nil)
299 (while (> ilen count)
300 (setq keyword (aref item count))
301 (setq arg (aref item (1+ count)))
302 (setq count (+ 2 count))
303 (cond
304 ((or (eq keyword :included) (eq keyword :visible))
305 (setq visible (or arg ''nil)))
306 ((eq keyword :key-sequence)
307 (setq cache arg cache-specified t))
308 ((eq keyword :keys) (setq keys arg no-name nil))
309 ((eq keyword :label) (setq label arg))
310 ((eq keyword :active) (setq active (or arg ''nil)))
311 ((eq keyword :help) (setq prop (cons :help (cons arg prop))))
312 ((eq keyword :suffix) (setq suffix arg))
313 ((eq keyword :style) (setq style arg))
314 ((eq keyword :selected) (setq selected (or arg ''nil)))))
315 (if suffix
316 (setq label
317 (if (stringp suffix)
318 (if (stringp label) (concat label " " suffix)
319 (list 'concat label (concat " " suffix)))
320 (if (stringp label)
321 (list 'concat (concat label " ") suffix)
322 (list 'concat label " " suffix)))))
323 (cond
324 ((eq style 'button)
325 (setq label (if (stringp label) (concat "[" label "]")
326 (list 'concat "[" label "]"))))
327 ((and selected
328 (setq style (assq style easy-menu-button-prefix)))
329 (setq prop (cons :button
330 (cons (cons (cdr style) selected) prop)))))
331 (when (stringp keys)
332 (if (string-match "^[^\\]*\\(\\\\\\[\\([^]]+\\)]\\)[^\\]*$"
333 keys)
334 (let ((prefix
335 (if (< (match-beginning 0) (match-beginning 1))
336 (substring keys 0 (match-beginning 1))))
337 (postfix
338 (if (< (match-end 1) (match-end 0))
339 (substring keys (match-end 1))))
340 (cmd (intern (match-string 2 keys))))
341 (setq keys (and (or prefix postfix)
342 (cons prefix postfix)))
343 (setq keys
344 (and (or keys (not (eq command cmd)))
345 (cons cmd keys))))
346 (setq cache-specified nil))
347 (if keys (setq prop (cons :keys (cons keys prop)))))
348 (if (and visible (not (easy-menu-always-true-p visible)))
349 (if (equal visible ''nil)
350 ;; Invisible menu item. Don't insert into keymap.
351 (setq remove t)
352 (setq prop (cons :visible (cons visible prop)))))))
353 (if (and active (not (easy-menu-always-true-p active)))
354 (setq prop (cons :enable (cons active prop))))
355 (if (and (or no-name cache-specified)
356 (or (null cache) (stringp cache) (vectorp cache)))
357 (setq prop (cons :key-sequence (cons cache prop))))))
358 (t (error "Invalid menu item in easymenu")))
359 ;; `intern' the name so as to merge multiple entries with the same name.
360 ;; It also makes it easier/possible to lookup/change menu bindings
361 ;; via keymap functions.
362 (cons (easy-menu-intern name)
363 (and (not remove)
364 (cons 'menu-item
365 (cons label
366 (and name
367 (cons command prop))))))))
368
369 (defun easy-menu-define-key (menu key item &optional before)
370 "Add binding in MENU for KEY => ITEM. Similar to `define-key-after'.
371 If KEY is not nil then delete any duplications.
372 If ITEM is nil, then delete the definition of KEY.
373
374 Optional argument BEFORE is nil or a key in MENU. If BEFORE is not nil,
375 put binding before the item in MENU named BEFORE; otherwise,
376 if a binding for KEY is already present in MENU, just change it;
377 otherwise put the new binding last in MENU.
378 BEFORE can be either a string (menu item name) or a symbol
379 \(the fake function key for the menu item).
380 KEY does not have to be a symbol, and comparison is done with equal."
381 (if (symbolp menu) (setq menu (indirect-function menu)))
382 (let ((inserted (null item)) ; Fake already inserted.
383 tail done)
384 (while (not done)
385 (cond
386 ((or (setq done (or (null (cdr menu)) (keymapp (cdr menu))))
387 (and before (easy-menu-name-match before (cadr menu))))
388 ;; If key is nil, stop here, otherwise keep going past the
389 ;; inserted element so we can delete any duplications that come
390 ;; later.
391 (if (null key) (setq done t))
392 (unless inserted ; Don't insert more than once.
393 (setcdr menu (cons (cons key item) (cdr menu)))
394 (setq inserted t)
395 (setq menu (cdr menu)))
396 (setq menu (cdr menu)))
397 ((and key (equal (car-safe (cadr menu)) key))
398 (if (or inserted ; Already inserted or
399 (and before ; wanted elsewhere and
400 (setq tail (cddr menu)) ; not last item and not
401 (not (keymapp tail))
402 (not (easy-menu-name-match
403 before (car tail))))) ; in position
404 (setcdr menu (cddr menu)) ; Remove item.
405 (setcdr (cadr menu) item) ; Change item.
406 (setq inserted t)
407 (setq menu (cdr menu))))
408 (t (setq menu (cdr menu)))))))
409
410 (defun easy-menu-name-match (name item)
411 "Return t if NAME is the name of menu item ITEM.
412 NAME can be either a string, or a symbol.
413 ITEM should be a keymap binding of the form (KEY . MENU-ITEM)."
414 (if (consp item)
415 (if (symbolp name)
416 (eq (car-safe item) name)
417 (if (stringp name)
418 ;; Match against the text that is displayed to the user.
419 (or (condition-case nil (member-ignore-case name item)
420 (error nil)) ;`item' might not be a proper list.
421 ;; Also check the string version of the symbol name,
422 ;; for backwards compatibility.
423 (eq (car-safe item) (intern name)))))))
424
425 (defun easy-menu-always-true-p (x)
426 "Return true if form X never evaluates to nil."
427 (if (consp x) (and (eq (car x) 'quote) (cadr x))
428 (or (eq x t) (not (symbolp x)))))
429
430 (defvar easy-menu-item-count 0)
431
432 (defun easy-menu-make-symbol (callback &optional noexp)
433 "Return a unique symbol with CALLBACK as function value.
434 When non-nil, NOEXP indicates that CALLBACK cannot be an expression
435 \(i.e. does not need to be turned into a function)."
436 (let ((command
437 (make-symbol (format "menu-function-%d" easy-menu-item-count))))
438 (setq easy-menu-item-count (1+ easy-menu-item-count))
439 (fset command
440 (if (or (keymapp callback) (functionp callback) noexp) callback
441 `(lambda () (interactive) ,callback)))
442 command))
443
444 ;;;###autoload
445 (defun easy-menu-change (path name items &optional before)
446 "Change menu found at PATH as item NAME to contain ITEMS.
447 PATH is a list of strings for locating the menu that
448 should contain a submenu named NAME.
449 ITEMS is a list of menu items, as in `easy-menu-define'.
450 These items entirely replace the previous items in that submenu.
451
452 If the menu located by PATH has no submenu named NAME, add one.
453 If the optional argument BEFORE is present, add it just before
454 the submenu named BEFORE, otherwise add it at the end of the menu.
455
456 Either call this from `menu-bar-update-hook' or use a menu filter,
457 to implement dynamic menus."
458 (easy-menu-add-item nil path (easy-menu-create-menu name items) before))
459
460 ;; XEmacs needs the following two functions to add and remove menus.
461 ;; In Emacs this is done automatically when switching keymaps, so
462 ;; here easy-menu-remove is a noop and easy-menu-add only precalculates
463 ;; equivalent keybindings (if easy-menu-precalculate-equivalent-keybindings
464 ;; is on).
465 (defalias 'easy-menu-remove 'ignore
466 "Remove MENU from the current menu bar.
467 Contrary to XEmacs, this is a nop on Emacs since menus are automatically
468 \(de)activated when the corresponding keymap is (de)activated.
469
470 \(fn MENU)")
471
472 (defun easy-menu-add (menu &optional map)
473 "Add the menu to the menubar.
474 This is a nop on Emacs since menus are automatically activated when the
475 corresponding keymap is activated. On XEmacs this is needed to actually
476 add the menu to the current menubar.
477 Maybe precalculate equivalent key bindings.
478 Do it only if `easy-menu-precalculate-equivalent-keybindings' is on."
479 (when easy-menu-precalculate-equivalent-keybindings
480 (if (and (symbolp menu) (not (keymapp menu)) (boundp menu))
481 (setq menu (symbol-value menu)))
482 (and (keymapp menu) (fboundp 'x-popup-menu)
483 (x-popup-menu nil menu))
484 ))
485
486 (defun add-submenu (menu-path submenu &optional before in-menu)
487 "Add submenu SUBMENU in the menu at MENU-PATH.
488 If BEFORE is non-nil, add before the item named BEFORE.
489 If IN-MENU is non-nil, follow MENU-PATH in IN-MENU.
490 This is a compatibility function; use `easy-menu-add-item'."
491 (easy-menu-add-item (or in-menu (current-global-map))
492 (cons "menu-bar" menu-path)
493 submenu before))
494
495 (defun easy-menu-add-item (map path item &optional before)
496 "To the submenu of MAP with path PATH, add ITEM.
497
498 If an item with the same name is already present in this submenu,
499 then ITEM replaces it. Otherwise, ITEM is added to this submenu.
500 In the latter case, ITEM is normally added at the end of the submenu.
501 However, if BEFORE is a string and there is an item in the submenu
502 with that name, then ITEM is added before that item.
503
504 MAP should normally be a keymap; nil stands for the local menu-bar keymap.
505 It can also be a symbol, which has earlier been used as the first
506 argument in a call to `easy-menu-define', or the value of such a symbol.
507
508 PATH is a list of strings for locating the submenu where ITEM is to be
509 added. If PATH is nil, MAP itself is used. Otherwise, the first
510 element should be the name of a submenu directly under MAP. This
511 submenu is then traversed recursively with the remaining elements of PATH.
512
513 ITEM is either defined as in `easy-menu-define' or a non-nil value returned
514 by `easy-menu-item-present-p' or `easy-menu-remove-item' or a menu defined
515 earlier by `easy-menu-define' or `easy-menu-create-menu'."
516 (setq map (easy-menu-get-map map path
517 (and (null map) (null path)
518 (stringp (car-safe item))
519 (car item))))
520 (if (and (consp item) (consp (cdr item)) (eq (cadr item) 'menu-item))
521 ;; This is a value returned by `easy-menu-item-present-p' or
522 ;; `easy-menu-remove-item'.
523 (easy-menu-define-key map (easy-menu-intern (car item))
524 (cdr item) before)
525 (if (or (keymapp item)
526 (and (symbolp item) (keymapp (symbol-value item))
527 (setq item (symbol-value item))))
528 ;; Item is a keymap, find the prompt string and use as item name.
529 (setq item (cons (keymap-prompt item) item)))
530 (easy-menu-do-add-item map item before)))
531
532 (defun easy-menu-item-present-p (map path name)
533 "In submenu of MAP with path PATH, return true iff item NAME is present.
534 MAP and PATH are defined as in `easy-menu-add-item'.
535 NAME should be a string, the name of the element to be looked for."
536 (easy-menu-return-item (easy-menu-get-map map path) name))
537
538 (defun easy-menu-remove-item (map path name)
539 "From submenu of MAP with path PATH remove item NAME.
540 MAP and PATH are defined as in `easy-menu-add-item'.
541 NAME should be a string, the name of the element to be removed."
542 (setq map (easy-menu-get-map map path))
543 (let ((ret (easy-menu-return-item map name)))
544 (if ret (easy-menu-define-key map (easy-menu-intern name) nil))
545 ret))
546
547 (defun easy-menu-return-item (menu name)
548 "In menu MENU try to look for menu item with name NAME.
549 If a menu item is found, return (NAME . item), otherwise return nil.
550 If item is an old format item, a new format item is returned."
551 (let ((item (lookup-key menu (vector (easy-menu-intern name))))
552 ret enable cache label)
553 (cond
554 ((stringp (car-safe item))
555 ;; This is the old menu format. Convert it to new format.
556 (setq label (car item))
557 (when (stringp (car (setq item (cdr item)))) ; Got help string
558 (setq ret (list :help (car item)))
559 (setq item (cdr item)))
560 (when (and (consp item) (consp (car item))
561 (or (null (caar item)) (numberp (caar item))))
562 (setq cache (car item)) ; Got cache
563 (setq item (cdr item)))
564 (and (symbolp item) (setq enable (get item 'menu-enable)) ; Got enable
565 (setq ret (cons :enable (cons enable ret))))
566 (if cache (setq ret (cons cache ret)))
567 (cons name (cons 'menu-enable (cons label (cons item ret)))))
568 (item ; (or (symbolp item) (keymapp item) (eq (car-safe item) 'menu-item))
569 (cons name item)) ; Keymap or new menu format
570 )))
571
572 (defun easy-menu-lookup-name (map name)
573 "Lookup menu item NAME in keymap MAP.
574 Like `lookup-key' except that NAME is not an array but just a single key
575 and that NAME can be a string representing the menu item's name."
576 (or (lookup-key map (vector (easy-menu-intern name)))
577 (when (stringp name)
578 ;; `lookup-key' failed and we have a menu item name: look at the
579 ;; actual menu entries's names.
580 (catch 'found
581 (map-keymap (lambda (key item)
582 (if (condition-case nil (member name item)
583 (error nil))
584 ;; Found it!! Look for it again with
585 ;; `lookup-key' so as to handle inheritance and
586 ;; to extract the actual command/keymap bound to
587 ;; `name' from the item (via get_keyelt).
588 (throw 'found (lookup-key map (vector key)))))
589 map)))))
590
591 (defun easy-menu-get-map (map path &optional to-modify)
592 "Return a sparse keymap in which to add or remove an item.
593 MAP and PATH are as defined in `easy-menu-add-item'.
594
595 TO-MODIFY, if non-nil, is the name of the item the caller
596 wants to modify in the map that we return.
597 In some cases we use that to select between the local and global maps."
598 (setq map
599 (catch 'found
600 (if (and map (symbolp map) (not (keymapp map)))
601 (setq map (symbol-value map)))
602 (let ((maps (if map (list map) (current-active-maps))))
603 ;; Look for PATH in each map.
604 (unless map (push 'menu-bar path))
605 (dolist (name path)
606 (setq maps
607 (delq nil (mapcar (lambda (map)
608 (setq map (easy-menu-lookup-name
609 map name))
610 (and (keymapp map) map))
611 maps))))
612
613 ;; Prefer a map that already contains the to-be-modified entry.
614 (when to-modify
615 (dolist (map maps)
616 (when (easy-menu-lookup-name map to-modify)
617 (throw 'found map))))
618 ;; Use the first valid map.
619 (when maps (throw 'found (car maps)))
620
621 ;; Otherwise, make one up.
622 ;; Hardcoding current-local-map is lame, but it's difficult
623 ;; to know what the caller intended for us to do ;-(
624 (let* ((name (if path (format "%s" (car (reverse path)))))
625 (newmap (make-sparse-keymap name)))
626 (define-key (or map (current-local-map))
627 (apply 'vector (mapcar 'easy-menu-intern path))
628 (if name (cons name newmap) newmap))
629 newmap))))
630 (or (keymapp map) (error "Malformed menu in easy-menu: (%s)" map))
631 map)
632
633 (provide 'easymenu)
634
635 ;; arch-tag: 2a04020d-90d2-476d-a7c6-71e072007a4a
636 ;;; easymenu.el ends here