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