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