(cal-tex-hook, cal-tex-insert-preamble, cal-tex-month-name): Doc fix.
[emacs.git] / lisp / emacs-lisp / easymenu.el
blobca5151fa9846bdea33a3484e44ba3217fdf184d3
1 ;;; easymenu.el --- support the easymenu interface for defining a menu
3 ;; Copyright (C) 1994, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Keywords: emulations
7 ;; Author: Richard Stallman <rms@gnu.org>
9 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Commentary:
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.
32 ;;; Code:
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")
45 (defsubst easy-menu-intern (s)
46 (if (stringp s) (intern s) s))
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.
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.
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
61 :filter FUNCTION
63 FUNCTION is a function with one argument, the rest of menu items.
64 It returns the remaining items of the displayed menu.
66 :visible INCLUDE
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'.
71 :active ENABLE
73 ENABLE is an expression; the menu is enabled for selection
74 whenever this expression's value is non-nil.
76 The rest of the elements in MENU, are menu items.
78 A menu item is usually a vector of three elements: [NAME CALLBACK ENABLE]
80 NAME is a string--the menu item name.
82 CALLBACK is a command to run when the item is chosen,
83 or a list to evaluate when the item is chosen.
85 ENABLE is an expression; the item is enabled for selection
86 whenever this expression's value is non-nil.
88 Alternatively, a menu item may have the form:
90 [ NAME CALLBACK [ KEYWORD ARG ] ... ]
92 Where KEYWORD is one of the symbols defined below.
94 :keys KEYS
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.
101 :key-sequence KEYS
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.
109 :active ENABLE
111 ENABLE is an expression; the item is enabled for selection
112 whenever this expression's value is non-nil.
114 :visible INCLUDE
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'.
119 :label FORM
121 FORM is an expression that will be dynamically evaluated and whose
122 value will be used for the menu entry's text label (the default is NAME).
124 :suffix FORM
126 FORM is an expression that will be dynamically evaluated and whose
127 value will be concatenated to the menu entry's label.
129 :style STYLE
131 STYLE is a symbol describing the type of menu item. The following are
132 defined:
134 toggle: A checkbox.
135 Prepend the name with `(*) ' or `( ) ' depending on if selected or not.
136 radio: A radio button.
137 Prepend the name with `[X] ' or `[ ] ' depending on if selected or not.
138 button: Surround the name with `[' and `]'. Use this for an item in the
139 menu bar itself.
140 anything else means an ordinary menu item.
142 :selected SELECTED
144 SELECTED is an expression; the checkbox or radio button is selected
145 whenever this expression's value is non-nil.
147 :help HELP
149 HELP is a string, the help to display for the menu item.
151 A menu item can be a string. Then that string appears in the menu as
152 unselectable text. A string consisting solely of hyphens is displayed
153 as a solid horizontal line.
155 A menu item can be a list with the same format as MENU. This is a submenu."
156 `(progn
157 ,(if symbol `(defvar ,symbol nil ,doc))
158 (easy-menu-do-define (quote ,symbol) ,maps ,doc ,menu)))
160 (defun easy-menu-binding (menu &optional item-name)
161 "Return a binding suitable to pass to `define-key'.
162 This is expected to be bound to a mouse event."
163 ;; Under Emacs this is almost trivial, whereas under XEmacs this may
164 ;; involve defining a function that calls popup-menu.
165 (let ((props (if (symbolp menu)
166 (prog1 (get menu 'menu-prop)
167 (setq menu (symbol-function menu))))))
168 (cons 'menu-item
169 (cons (or item-name
170 (if (keymapp menu)
171 (keymap-prompt menu))
173 (cons menu props)))))
175 ;;;###autoload
176 (defun easy-menu-do-define (symbol maps doc menu)
177 ;; We can't do anything that might differ between Emacs dialects in
178 ;; `easy-menu-define' in order to make byte compiled files
179 ;; compatible. Therefore everything interesting is done in this
180 ;; function.
181 (let ((keymap (easy-menu-create-menu (car menu) (cdr menu))))
182 (when symbol
183 (set symbol keymap)
184 (defalias symbol
185 `(lambda (event) ,doc (interactive "@e")
186 ;; FIXME: XEmacs uses popup-menu which calls the binding
187 ;; while x-popup-menu only returns the selection.
188 (x-popup-menu event
189 (or (and (symbolp ,symbol)
190 (funcall
191 (or (plist-get (get ,symbol 'menu-prop)
192 :filter)
193 'identity)
194 (symbol-function ,symbol)))
195 ,symbol)))))
196 (dolist (map (if (keymapp maps) (list maps) maps))
197 (define-key map
198 (vector 'menu-bar (easy-menu-intern (car menu)))
199 (easy-menu-binding keymap (car menu))))))
201 (defun easy-menu-filter-return (menu &optional name)
202 "Convert MENU to the right thing to return from a menu filter.
203 MENU is a menu as computed by `easy-menu-define' or `easy-menu-create-menu' or
204 a symbol whose value is such a menu.
205 In Emacs a menu filter must return a menu (a keymap), in XEmacs a filter must
206 return a menu items list (without menu name and keywords).
207 This function returns the right thing in the two cases.
208 If NAME is provided, it is used for the keymap."
209 (cond
210 ((and (not (keymapp menu)) (consp menu))
211 ;; If it's a cons but not a keymap, then it can't be right
212 ;; unless it's an XEmacs menu.
213 (setq menu (easy-menu-create-menu (or name "") menu)))
214 ((vectorp menu)
215 ;; It's just a menu entry.
216 (setq menu (cdr (easy-menu-convert-item menu)))))
217 menu)
219 (defvar easy-menu-avoid-duplicate-keys t
220 "Dynamically scoped var to register already used keys in a menu.
221 If it holds a list, this is expected to be a list of keys already seen in the
222 menu we're processing. Else it means we're not processing a menu.")
224 ;;;###autoload
225 (defun easy-menu-create-menu (menu-name menu-items)
226 "Create a menu called MENU-NAME with items described in MENU-ITEMS.
227 MENU-NAME is a string, the name of the menu. MENU-ITEMS is a list of items
228 possibly preceded by keyword pairs as described in `easy-menu-define'."
229 (let ((menu (make-sparse-keymap menu-name))
230 (easy-menu-avoid-duplicate-keys nil)
231 prop keyword arg label enable filter visible help)
232 ;; Look for keywords.
233 (while (and menu-items
234 (cdr menu-items)
235 (keywordp (setq keyword (car menu-items))))
236 (setq arg (cadr menu-items))
237 (setq menu-items (cddr menu-items))
238 (cond
239 ((eq keyword :filter)
240 (setq filter `(lambda (menu)
241 (easy-menu-filter-return (,arg menu) ,menu-name))))
242 ((eq keyword :active) (setq enable (or arg ''nil)))
243 ((eq keyword :label) (setq label arg))
244 ((eq keyword :help) (setq help arg))
245 ((or (eq keyword :included) (eq keyword :visible))
246 (setq visible (or arg ''nil)))))
247 (if (equal visible ''nil)
248 nil ; Invisible menu entry, return nil.
249 (if (and visible (not (easy-menu-always-true-p visible)))
250 (setq prop (cons :visible (cons visible prop))))
251 (if (and enable (not (easy-menu-always-true-p enable)))
252 (setq prop (cons :enable (cons enable prop))))
253 (if filter (setq prop (cons :filter (cons filter prop))))
254 (if help (setq prop (cons :help (cons help prop))))
255 (if label (setq prop (cons nil (cons label prop))))
256 (if filter
257 ;; The filter expects the menu in its XEmacs form and the pre-filter
258 ;; form will only be passed to the filter anyway, so we'd better
259 ;; not convert it at all (it will be converted on the fly by
260 ;; easy-menu-filter-return).
261 (setq menu menu-items)
262 (setq menu (append menu (mapcar 'easy-menu-convert-item menu-items))))
263 (when prop
264 (setq menu (easy-menu-make-symbol menu 'noexp))
265 (put menu 'menu-prop prop))
266 menu)))
269 ;; Known button types.
270 (defvar easy-menu-button-prefix
271 '((radio . :radio) (toggle . :toggle)))
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)
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-p 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-p 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 (let ((key (easy-menu-intern name)))
381 (when (listp easy-menu-avoid-duplicate-keys)
382 ;; Merging multiple entries with the same name is sometimes what we
383 ;; want, but not when the entries are actually different (e.g. same
384 ;; name but different :suffix as seen in cal-menu.el) and appear in
385 ;; the same menu. So we try to detect and resolve conflicts.
386 (while (and (stringp name)
387 (memq key easy-menu-avoid-duplicate-keys))
388 ;; We need to use some distinct object, ideally a symbol, ideally
389 ;; related to the `name'. Uninterned symbols do not work (they
390 ;; are apparently turned into strings and re-interned later on).
391 (setq key (intern (format "%s (%d)" (symbol-name key)
392 (length easy-menu-avoid-duplicate-keys)))))
393 (push key easy-menu-avoid-duplicate-keys))
395 (cons key
396 (and (not remove)
397 (cons 'menu-item
398 (cons label
399 (and name
400 (cons command prop)))))))))
402 (defun easy-menu-define-key (menu key item &optional before)
403 "Add binding in MENU for KEY => ITEM. Similar to `define-key-after'.
404 If KEY is not nil then delete any duplications.
405 If ITEM is nil, then delete the definition of KEY.
407 Optional argument BEFORE is nil or a key in MENU. If BEFORE is not nil,
408 put binding before the item in MENU named BEFORE; otherwise,
409 if a binding for KEY is already present in MENU, just change it;
410 otherwise put the new binding last in MENU.
411 BEFORE can be either a string (menu item name) or a symbol
412 \(the fake function key for the menu item).
413 KEY does not have to be a symbol, and comparison is done with equal."
414 (if (symbolp menu) (setq menu (indirect-function menu)))
415 (let ((inserted (null item)) ; Fake already inserted.
416 tail done)
417 (while (not done)
418 (cond
419 ((or (setq done (or (null (cdr menu)) (keymapp (cdr menu))))
420 (and before (easy-menu-name-match before (cadr menu))))
421 ;; If key is nil, stop here, otherwise keep going past the
422 ;; inserted element so we can delete any duplications that come
423 ;; later.
424 (if (null key) (setq done t))
425 (unless inserted ; Don't insert more than once.
426 (setcdr menu (cons (cons key item) (cdr menu)))
427 (setq inserted t)
428 (setq menu (cdr menu)))
429 (setq menu (cdr menu)))
430 ((and key (equal (car-safe (cadr menu)) key))
431 (if (or inserted ; Already inserted or
432 (and before ; wanted elsewhere and
433 (setq tail (cddr menu)) ; not last item and not
434 (not (keymapp tail))
435 (not (easy-menu-name-match
436 before (car tail))))) ; in position
437 (setcdr menu (cddr menu)) ; Remove item.
438 (setcdr (cadr menu) item) ; Change item.
439 (setq inserted t)
440 (setq menu (cdr menu))))
441 (t (setq menu (cdr menu)))))))
443 (defun easy-menu-name-match (name item)
444 "Return t if NAME is the name of menu item ITEM.
445 NAME can be either a string, or a symbol.
446 ITEM should be a keymap binding of the form (KEY . MENU-ITEM)."
447 (if (consp item)
448 (if (symbolp name)
449 (eq (car-safe item) name)
450 (if (stringp name)
451 ;; Match against the text that is displayed to the user.
452 (or (condition-case nil (member-ignore-case name item)
453 (error nil)) ;`item' might not be a proper list.
454 ;; Also check the string version of the symbol name,
455 ;; for backwards compatibility.
456 (eq (car-safe item) (intern name)))))))
458 (defun easy-menu-always-true-p (x)
459 "Return true if form X never evaluates to nil."
460 (if (consp x) (and (eq (car x) 'quote) (cadr x))
461 (or (eq x t) (not (symbolp x)))))
463 (defvar easy-menu-item-count 0)
465 (defun easy-menu-make-symbol (callback &optional noexp)
466 "Return a unique symbol with CALLBACK as function value.
467 When non-nil, NOEXP indicates that CALLBACK cannot be an expression
468 \(i.e. does not need to be turned into a function)."
469 (let ((command
470 (make-symbol (format "menu-function-%d" easy-menu-item-count))))
471 (setq easy-menu-item-count (1+ easy-menu-item-count))
472 (fset command
473 (if (or (keymapp callback) (functionp callback) noexp) callback
474 `(lambda () (interactive) ,callback)))
475 command))
477 ;;;###autoload
478 (defun easy-menu-change (path name items &optional before map)
479 "Change menu found at PATH as item NAME to contain ITEMS.
480 PATH is a list of strings for locating the menu that
481 should contain a submenu named NAME.
482 ITEMS is a list of menu items, as in `easy-menu-define'.
483 These items entirely replace the previous items in that submenu.
485 If MAP is specified, it should normally be a keymap; nil stands for the local
486 menu-bar keymap. It can also be a symbol, which has earlier been used as the
487 first argument in a call to `easy-menu-define', or the value of such a symbol.
489 If the menu located by PATH has no submenu named NAME, add one.
490 If the optional argument BEFORE is present, add it just before
491 the submenu named BEFORE, otherwise add it at the end of the menu.
493 To implement dynamic menus, either call this from
494 `menu-bar-update-hook' or use a menu filter."
495 (easy-menu-add-item map path (easy-menu-create-menu name items) before))
497 ;; XEmacs needs the following two functions to add and remove menus.
498 ;; In Emacs this is done automatically when switching keymaps, so
499 ;; here easy-menu-remove is a noop and easy-menu-add only precalculates
500 ;; equivalent keybindings (if easy-menu-precalculate-equivalent-keybindings
501 ;; is on).
502 (defalias 'easy-menu-remove 'ignore
503 "Remove MENU from the current menu bar.
504 Contrary to XEmacs, this is a nop on Emacs since menus are automatically
505 \(de)activated when the corresponding keymap is (de)activated.
507 \(fn MENU)")
509 (defun easy-menu-add (menu &optional map)
510 "Add the menu to the menubar.
511 On Emacs, menus are already automatically activated when the
512 corresponding keymap is activated. On XEmacs this is needed to
513 actually add the menu to the current menubar.
515 This also precalculates equivalent key bindings when
516 `easy-menu-precalculate-equivalent-keybindings' is on.
518 You should call this once the menu and keybindings are set up
519 completely and menu filter functions can be expected to work."
520 (when easy-menu-precalculate-equivalent-keybindings
521 (if (and (symbolp menu) (not (keymapp menu)) (boundp menu))
522 (setq menu (symbol-value menu)))
523 (and (keymapp menu) (fboundp 'x-popup-menu)
524 (x-popup-menu nil menu))
527 (defun add-submenu (menu-path submenu &optional before in-menu)
528 "Add submenu SUBMENU in the menu at MENU-PATH.
529 If BEFORE is non-nil, add before the item named BEFORE.
530 If IN-MENU is non-nil, follow MENU-PATH in IN-MENU.
531 This is a compatibility function; use `easy-menu-add-item'."
532 (easy-menu-add-item (or in-menu (current-global-map))
533 (cons "menu-bar" menu-path)
534 submenu before))
536 (defun easy-menu-add-item (map path item &optional before)
537 "To the submenu of MAP with path PATH, add ITEM.
539 If an item with the same name is already present in this submenu,
540 then ITEM replaces it. Otherwise, ITEM is added to this submenu.
541 In the latter case, ITEM is normally added at the end of the submenu.
542 However, if BEFORE is a string and there is an item in the submenu
543 with that name, then ITEM is added before that item.
545 MAP should normally be a keymap; nil stands for the local menu-bar keymap.
546 It can also be a symbol, which has earlier been used as the first
547 argument in a call to `easy-menu-define', or the value of such a symbol.
549 PATH is a list of strings for locating the submenu where ITEM is to be
550 added. If PATH is nil, MAP itself is used. Otherwise, the first
551 element should be the name of a submenu directly under MAP. This
552 submenu is then traversed recursively with the remaining elements of PATH.
554 ITEM is either defined as in `easy-menu-define' or a non-nil value returned
555 by `easy-menu-item-present-p' or `easy-menu-remove-item' or a menu defined
556 earlier by `easy-menu-define' or `easy-menu-create-menu'."
557 (setq map (easy-menu-get-map map path
558 (and (null map) (null path)
559 (stringp (car-safe item))
560 (car item))))
561 (if (and (consp item) (consp (cdr item)) (eq (cadr item) 'menu-item))
562 ;; This is a value returned by `easy-menu-item-present-p' or
563 ;; `easy-menu-remove-item'.
564 (easy-menu-define-key map (easy-menu-intern (car item))
565 (cdr item) before)
566 (if (or (keymapp item)
567 (and (symbolp item) (keymapp (symbol-value item))
568 (setq item (symbol-value item))))
569 ;; Item is a keymap, find the prompt string and use as item name.
570 (setq item (cons (keymap-prompt item) item)))
571 (setq item (easy-menu-convert-item item))
572 (easy-menu-define-key map (easy-menu-intern (car item)) (cdr item) before)))
574 (defun easy-menu-item-present-p (map path name)
575 "In submenu of MAP with path PATH, return non-nil if item NAME is present.
576 MAP and PATH are defined as in `easy-menu-add-item'.
577 NAME should be a string, the name of the element to be looked for."
578 (easy-menu-return-item (easy-menu-get-map map path) name))
580 (defun easy-menu-remove-item (map path name)
581 "From submenu of MAP with path PATH remove item NAME.
582 MAP and PATH are defined as in `easy-menu-add-item'.
583 NAME should be a string, the name of the element to be removed."
584 (setq map (easy-menu-get-map map path))
585 (let ((ret (easy-menu-return-item map name)))
586 (if ret (easy-menu-define-key map (easy-menu-intern name) nil))
587 ret))
589 (defun easy-menu-return-item (menu name)
590 "In menu MENU try to look for menu item with name NAME.
591 If a menu item is found, return (NAME . item), otherwise return nil.
592 If item is an old format item, a new format item is returned."
593 ;; The call to `lookup-key' also calls the C function `get_keyelt' which
594 ;; looks inside a menu-item to only return the actual command. This is
595 ;; not what we want here. We should either add an arg to lookup-key to be
596 ;; able to turn off this "feature", or else we could use map-keymap here.
597 ;; In the mean time, I just use `assq' which is an OK approximation since
598 ;; menus are rarely built from vectors or char-tables.
599 (let ((item (or (cdr (assq name menu))
600 (lookup-key menu (vector (easy-menu-intern name)))))
601 ret enable cache label)
602 (cond
603 ((stringp (car-safe item))
604 ;; This is the old menu format. Convert it to new format.
605 (setq label (car item))
606 (when (stringp (car (setq item (cdr item)))) ; Got help string
607 (setq ret (list :help (car item)))
608 (setq item (cdr item)))
609 (when (and (consp item) (consp (car item))
610 (or (null (caar item)) (numberp (caar item))))
611 (setq cache (car item)) ; Got cache
612 (setq item (cdr item)))
613 (and (symbolp item) (setq enable (get item 'menu-enable)) ; Got enable
614 (setq ret (cons :enable (cons enable ret))))
615 (if cache (setq ret (cons cache ret)))
616 (cons name (cons 'menu-enable (cons label (cons item ret)))))
617 (item ; (or (symbolp item) (keymapp item) (eq (car-safe item) 'menu-item))
618 (cons name item)) ; Keymap or new menu format
621 (defun easy-menu-lookup-name (map name)
622 "Lookup menu item NAME in keymap MAP.
623 Like `lookup-key' except that NAME is not an array but just a single key
624 and that NAME can be a string representing the menu item's name."
625 (or (lookup-key map (vector (easy-menu-intern name)))
626 (when (stringp name)
627 ;; `lookup-key' failed and we have a menu item name: look at the
628 ;; actual menu entries's names.
629 (catch 'found
630 (map-keymap (lambda (key item)
631 (if (condition-case nil (member name item)
632 (error nil))
633 ;; Found it!! Look for it again with
634 ;; `lookup-key' so as to handle inheritance and
635 ;; to extract the actual command/keymap bound to
636 ;; `name' from the item (via get_keyelt).
637 (throw 'found (lookup-key map (vector key)))))
638 map)))))
640 (defun easy-menu-get-map (map path &optional to-modify)
641 "Return a sparse keymap in which to add or remove an item.
642 MAP and PATH are as defined in `easy-menu-add-item'.
644 TO-MODIFY, if non-nil, is the name of the item the caller
645 wants to modify in the map that we return.
646 In some cases we use that to select between the local and global maps."
647 (setq map
648 (catch 'found
649 (if (and map (symbolp map) (not (keymapp map)))
650 (setq map (symbol-value map)))
651 (let ((maps (if map (if (keymapp map) (list map) map)
652 (current-active-maps))))
653 ;; Look for PATH in each map.
654 (unless map (push 'menu-bar path))
655 (dolist (name path)
656 (setq maps
657 (delq nil (mapcar (lambda (map)
658 (setq map (easy-menu-lookup-name
659 map name))
660 (and (keymapp map) map))
661 maps))))
663 ;; Prefer a map that already contains the to-be-modified entry.
664 (when to-modify
665 (dolist (map maps)
666 (when (easy-menu-lookup-name map to-modify)
667 (throw 'found map))))
668 ;; Use the first valid map.
669 (when maps (throw 'found (car maps)))
671 ;; Otherwise, make one up.
672 ;; Hardcoding current-local-map is lame, but it's difficult
673 ;; to know what the caller intended for us to do ;-(
674 (let* ((name (if path (format "%s" (car (reverse path)))))
675 (newmap (make-sparse-keymap name)))
676 (define-key (or map (current-local-map))
677 (apply 'vector (mapcar 'easy-menu-intern path))
678 (if name (cons name newmap) newmap))
679 newmap))))
680 (or (keymapp map) (error "Malformed menu in easy-menu: (%s)" map))
681 map)
683 (provide 'easymenu)
685 ;; arch-tag: 2a04020d-90d2-476d-a7c6-71e072007a4a
686 ;;; easymenu.el ends here