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