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