(custom-face-value-create): If face name doesn't end with "face", add
[bpt/emacs.git] / lisp / emacs-lisp / easymenu.el
CommitLineData
029b623a
RS
1;;; easymenu.el --- support the easymenu interface for defining a menu.
2
024bda02 3;; Copyright (C) 1994, 1996, 1998 Free Software Foundation, Inc.
029b623a 4
8df69fb0
RS
5;; Keywords: emulations
6;; Author: rms
7
029b623a
RS
8;; This file is part of GNU Emacs.
9
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.
14
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.
19
20;; You should have received a copy of the GNU General Public License
b578f267
EN
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.
029b623a 24
b578f267
EN
25;;; Commentary:
26
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.
029b623a
RS
30
31;;; Code:
32
545128a8
RS
33(defcustom easy-menu-precalculate-equivalent-keybindings t
34 "Determine when equivalent key bindings are computed for easy-menu menus.
35It can take some time to calculate the equivalent key bindings that are shown
36in a menu. If the variable is on, then this calculation gives a (maybe
37noticeable) delay when a mode is first entered. If the variable is off, then
38this delay will come when a menu is displayed the first time. If you never use
39menus, turn this variable off, otherwise it is probably better to keep it on."
40 :type 'boolean
41 :group 'menu
42 :version "20.3")
43
a8226f67 44;;;###autoload
8df69fb0 45(defmacro easy-menu-define (symbol maps doc menu)
029b623a 46 "Define a menu bar submenu in maps MAPS, according to MENU.
819fefed
RS
47The menu keymap is stored in symbol SYMBOL, both as its value
48and as its function definition. DOC is used as the doc string for SYMBOL.
029b623a
RS
49
50The first element of MENU must be a string. It is the menu bar item name.
3de63fb6
RS
51It may be followed by the following keyword argument pairs
52
024bda02 53 :filter FUNCTION
3de63fb6 54
024bda02
RS
55FUNCTION is a function with one argument, the menu. It returns the actual
56menu displayed.
57
3de63fb6
RS
58 :visible INCLUDE
59
60INCLUDE is an expression; this menu is only visible if this
61expression has a non-nil value. `:include' is an alias for `:visible'.
62
63 :active ENABLE
64
65ENABLE is an expression; the menu is enabled for selection
66whenever this expression's value is non-nil.
67
68The rest of the elements in MENU, are menu items.
029b623a 69
8df69fb0 70A menu item is usually a vector of three elements: [NAME CALLBACK ENABLE]
029b623a 71
a8226f67 72NAME is a string--the menu item name.
029b623a 73
a8226f67
RS
74CALLBACK is a command to run when the item is chosen,
75or a list to evaluate when the item is chosen.
029b623a 76
5a51e0a6
RS
77ENABLE is an expression; the item is enabled for selection
78whenever this expression's value is non-nil.
8df69fb0 79
6c2599ed 80Alternatively, a menu item may have the form:
1ba15fe6
RS
81
82 [ NAME CALLBACK [ KEYWORD ARG ] ... ]
83
024bda02 84Where KEYWORD is one of the symbols defined below.
1ba15fe6
RS
85
86 :keys KEYS
87
88KEYS is a string; a complex keyboard equivalent to this menu item.
89This is normally not needed because keyboard equivalents are usually
90computed automatically.
3de63fb6
RS
91KEYS is expanded with `substitute-command-keys' before it is used.
92
93 :key-sequence KEYS
94
d0acce1e 95KEYS is nil, a string or a vector; nil or a keyboard equivalent to this
3de63fb6 96menu item.
d0acce1e 97This is a hint that will considerably speed up Emacs' first display of
3de63fb6
RS
98a menu. Use `:key-sequence nil' when you know that this menu item has no
99keyboard equivalent.
1ba15fe6
RS
100
101 :active ENABLE
102
103ENABLE is an expression; the item is enabled for selection
104whenever this expression's value is non-nil.
105
3de63fb6
RS
106 :included INCLUDE
107
108INCLUDE is an expression; this item is only visible if this
109expression has a non-nil value.
110
d0acce1e 111 :suffix FORM
1ba15fe6 112
d0acce1e
SM
113FORM is an expression that will be dynamically evaluated and whose
114value will be concatenated to the menu entry's NAME.
1ba15fe6 115
41e6ca7a 116 :style STYLE
6c2599ed 117
1ba15fe6 118STYLE is a symbol describing the type of menu item. The following are
6c2599ed 119defined:
1ba15fe6 120
e6a6d697 121toggle: A checkbox.
3de63fb6 122 Prepend the name with `(*) ' or `( ) ' depending on if selected or not.
e6a6d697 123radio: A radio button.
3de63fb6 124 Prepend the name with `[X] ' or `[ ] ' depending on if selected or not.
4d6d04b5 125button: Surround the name with `[' and `]'. Use this for an item in the
3de63fb6
RS
126 menu bar itself.
127anything else means an ordinary menu item.
1ba15fe6
RS
128
129 :selected SELECTED
130
131SELECTED is an expression; the checkbox or radio button is selected
132whenever this expression's value is non-nil.
1ba15fe6 133
25112054
GM
134 :help HELP
135
136HELP is a string, the help to display for the menu item.
137
a8226f67
RS
138A menu item can be a string. Then that string appears in the menu as
139unselectable text. A string consisting solely of hyphens is displayed
140as a solid horizontal line.
029b623a 141
3de63fb6 142A menu item can be a list with the same format as MENU. This is a submenu."
024bda02
RS
143 `(progn
144 (defvar ,symbol nil ,doc)
145 (easy-menu-do-define (quote ,symbol) ,maps ,doc ,menu)))
1ba15fe6 146
f39223a6 147;;;###autoload
1ba15fe6
RS
148(defun easy-menu-do-define (symbol maps doc menu)
149 ;; We can't do anything that might differ between Emacs dialects in
150 ;; `easy-menu-define' in order to make byte compiled files
151 ;; compatible. Therefore everything interesting is done in this
6c2599ed 152 ;; function.
d0acce1e
SM
153 (let ((keymap (easy-menu-create-menu (car menu) (cdr menu))))
154 (set symbol keymap)
155 (fset symbol
156 `(lambda (event) ,doc (interactive "@e")
157 ;; FIXME: XEmacs uses popup-menu which calls the binding
158 ;; while x-popup-menu only returns the selection.
159 (x-popup-menu event
160 (or (and (symbolp ,symbol)
161 (funcall
162 (or (plist-get (get ,symbol 'menu-prop)
163 :filter)
164 'identity)
165 (symbol-function ,symbol)))
166 ,symbol))))
167 (mapcar (lambda (map)
168 (define-key map (vector 'menu-bar (intern (car menu)))
169 (cons 'menu-item
170 (cons (car menu)
171 (if (not (symbolp keymap))
172 (list keymap)
173 (cons (symbol-function keymap)
174 (get keymap 'menu-prop)))))))
175 (if (keymapp maps) (list maps) maps))))
176
177(defun easy-menu-filter-return (menu &optional name)
024bda02
RS
178 "Convert MENU to the right thing to return from a menu filter.
179MENU is a menu as computed by `easy-menu-define' or `easy-menu-create-menu' or
180a symbol whose value is such a menu.
181In Emacs a menu filter must return a menu (a keymap), in XEmacs a filter must
3de63fb6 182return a menu items list (without menu name and keywords).
d0acce1e
SM
183This function returns the right thing in the two cases.
184If NAME is provided, it is used for the keymap."
185 (when (and (not (keymapp menu)) (consp menu))
186 ;; If it's a cons but not a keymap, then it can't be right
187 ;; unless it's an XEmacs menu.
188 (setq menu (easy-menu-create-menu (or name "") menu)))
024bda02 189 (easy-menu-get-map menu nil)) ; Get past indirections.
029b623a 190
815d2127 191;;;###autoload
024bda02
RS
192(defun easy-menu-create-menu (menu-name menu-items)
193 "Create a menu called MENU-NAME with items described in MENU-ITEMS.
194MENU-NAME is a string, the name of the menu. MENU-ITEMS is a list of items
195possibly preceded by keyword pairs as described in `easy-menu-define'."
196 (let ((menu (make-sparse-keymap menu-name))
25112054 197 prop keyword arg label enable filter visible help)
024bda02 198 ;; Look for keywords.
b9f5db90
DL
199 (while (and menu-items
200 (cdr menu-items)
201 (keywordp (setq keyword (car menu-items))))
c78fb6a6
RS
202 (setq arg (cadr menu-items))
203 (setq menu-items (cddr menu-items))
204 (cond
d0acce1e
SM
205 ((eq keyword :filter)
206 (setq filter `(lambda (menu)
207 (easy-menu-filter-return (,arg menu) ,menu-name))))
3de63fb6
RS
208 ((eq keyword :active) (setq enable (or arg ''nil)))
209 ((eq keyword :label) (setq label arg))
25112054 210 ((eq keyword :help) (setq help arg))
3de63fb6
RS
211 ((or (eq keyword :included) (eq keyword :visible))
212 (setq visible (or arg ''nil)))))
4d6d04b5
DL
213 (if (equal visible ''nil)
214 nil ; Invisible menu entry, return nil.
c78fb6a6
RS
215 (if (and visible (not (easy-menu-always-true visible)))
216 (setq prop (cons :visible (cons visible prop))))
217 (if (and enable (not (easy-menu-always-true enable)))
218 (setq prop (cons :enable (cons enable prop))))
219 (if filter (setq prop (cons :filter (cons filter prop))))
25112054 220 (if help (setq prop (cons :help (cons help prop))))
c78fb6a6 221 (if label (setq prop (cons nil (cons label prop))))
d0acce1e
SM
222 (if filter
223 ;; The filter expects the menu in its XEmacs form and the pre-filter
224 ;; form will only be passed to the filter anyway, so we'd better
225 ;; not convert it at all (it will be converted on the fly by
226 ;; easy-menu-filter-return).
227 (setq menu menu-items)
228 (setq menu (append menu (mapcar 'easy-menu-convert-item menu-items))))
c78fb6a6 229 (when prop
d0acce1e 230 (setq menu (easy-menu-make-symbol menu 'noexp))
c78fb6a6
RS
231 (put menu 'menu-prop prop))
232 menu)))
029b623a 233
024bda02 234
3de63fb6 235;; Known button types.
024bda02 236(defvar easy-menu-button-prefix
c78fb6a6 237 '((radio . :radio) (toggle . :toggle)))
024bda02 238
c78fb6a6 239(defun easy-menu-do-add-item (menu item &optional before)
d0acce1e
SM
240 (setq item (easy-menu-convert-item item))
241 (easy-menu-define-key-intern menu (car item) (cdr item) before))
242
243(defvar easy-menu-converted-items-table (make-hash-table :test 'equal))
244
245(defun easy-menu-convert-item (item)
4d6d04b5
DL
246 "Memoize the value returned by `easy-menu-convert-item-1' called on ITEM.
247This makes key-shortcut-caching work a *lot* better when this
248conversion is done from within a filter.
249This also helps when the NAME of the entry is recreated each time:
250since the menu is built and traversed separately, the lookup
251would always fail because the key is `equal' but not `eq'."
d0acce1e
SM
252 (or (gethash item easy-menu-converted-items-table)
253 (puthash item (easy-menu-convert-item-1 item)
254 easy-menu-converted-items-table)))
255
256(defun easy-menu-convert-item-1 (item)
4d6d04b5
DL
257 "Parse an item description and add the item to a keymap.
258This is the function that is used for item definition by the other easy-menu
259functions.
260MENU is a sparse keymap i.e. a list starting with the symbol `keymap'.
261ITEM defines an item as in `easy-menu-define'.
262Optional argument BEFORE is nil or a key in MENU. If BEFORE is not nil
263put item before BEFORE in MENU, otherwise if item is already present in
264MENU, just change it, otherwise put it last in MENU."
25112054 265 (let (name command label prop remove help)
024bda02 266 (cond
2c5c9cb9
GM
267 ((stringp item) ; An item or separator.
268 (setq label item))
269 ((consp item) ; A sub-menu
c78fb6a6
RS
270 (setq label (setq name (car item)))
271 (setq command (cdr item))
272 (if (not (keymapp command))
273 (setq command (easy-menu-create-menu name command)))
274 (if (null command)
275 ;; Invisible menu item. Don't insert into keymap.
276 (setq remove t)
277 (when (and (symbolp command) (setq prop (get command 'menu-prop)))
278 (when (null (car prop))
279 (setq label (cadr prop))
280 (setq prop (cddr prop)))
281 (setq command (symbol-function command)))))
3de63fb6 282 ((vectorp item) ; An item.
545128a8
RS
283 (let* ((ilen (length item))
284 (active (if (> ilen 2) (or (aref item 2) ''nil) t))
285 (no-name (not (symbolp (setq command (aref item 1)))))
286 cache cache-specified)
c78fb6a6
RS
287 (setq label (setq name (aref item 0)))
288 (if no-name (setq command (easy-menu-make-symbol command)))
b9f5db90 289 (if (keywordp active)
c78fb6a6
RS
290 (let ((count 2)
291 keyword arg suffix visible style selected keys)
292 (setq active nil)
545128a8 293 (while (> ilen count)
024bda02
RS
294 (setq keyword (aref item count))
295 (setq arg (aref item (1+ count)))
296 (setq count (+ 2 count))
297 (cond
3de63fb6
RS
298 ((or (eq keyword :included) (eq keyword :visible))
299 (setq visible (or arg ''nil)))
c78fb6a6
RS
300 ((eq keyword :key-sequence)
301 (setq cache arg cache-specified t))
302 ((eq keyword :keys) (setq keys arg no-name nil))
303 ((eq keyword :label) (setq label arg))
304 ((eq keyword :active) (setq active (or arg ''nil)))
25112054 305 ((eq keyword :help) (setq prop (cons :help (cons arg prop))))
c78fb6a6
RS
306 ((eq keyword :suffix) (setq suffix arg))
307 ((eq keyword :style) (setq style arg))
308 ((eq keyword :selected) (setq selected (or arg ''nil)))))
3de63fb6
RS
309 (if suffix
310 (setq label
311 (if (stringp suffix)
312 (if (stringp label) (concat label " " suffix)
313 (list 'concat label (concat " " suffix)))
314 (if (stringp label)
315 (list 'concat (concat label " ") suffix)
316 (list 'concat label " " suffix)))))
317 (cond
318 ((eq style 'button)
319 (setq label (if (stringp label) (concat "[" label "]")
320 (list 'concat "[" label "]"))))
321 ((and selected
322 (setq style (assq style easy-menu-button-prefix)))
323 (setq prop (cons :button
324 (cons (cons (cdr style) selected) prop)))))
c78fb6a6
RS
325 (when (stringp keys)
326 (if (string-match "^[^\\]*\\(\\\\\\[\\([^]]+\\)]\\)[^\\]*$"
327 keys)
328 (let ((prefix
329 (if (< (match-beginning 0) (match-beginning 1))
330 (substring keys 0 (match-beginning 1))))
331 (postfix
332 (if (< (match-end 1) (match-end 0))
333 (substring keys (match-end 1))))
334 (cmd (intern (substring keys (match-beginning 2)
335 (match-end 2)))))
545128a8
RS
336 (setq keys (and (or prefix postfix)
337 (cons prefix postfix)))
c78fb6a6 338 (setq keys
545128a8
RS
339 (and (or keys (not (eq command cmd)))
340 (cons cmd keys))))
c78fb6a6
RS
341 (setq cache-specified nil))
342 (if keys (setq prop (cons :keys (cons keys prop)))))
343 (if (and visible (not (easy-menu-always-true visible)))
344 (if (equal visible ''nil)
345 ;; Invisible menu item. Don't insert into keymap.
346 (setq remove t)
347 (setq prop (cons :visible (cons visible prop)))))))
348 (if (and active (not (easy-menu-always-true active)))
349 (setq prop (cons :enable (cons active prop))))
350 (if (and (or no-name cache-specified)
351 (or (null cache) (stringp cache) (vectorp cache)))
352 (setq prop (cons :key-sequence (cons cache prop))))))
a2c896c3 353 (t (error "Invalid menu item in easymenu")))
3a4f3f86
SM
354 ;; `intern' the name so as to merge multiple entries with the same name.
355 ;; It also makes it easier/possible to lookup/change menu bindings
356 ;; via keymap functions.
115f38ae 357 (cons (if (stringp name) (intern name) name)
3a4f3f86
SM
358 (and (not remove)
359 (cons 'menu-item
360 (cons label
361 (and name
362 (cons command prop))))))))
3de63fb6
RS
363
364(defun easy-menu-define-key-intern (menu key item &optional before)
4d6d04b5 365 "Like easy-menu-define-key, but interns KEY and BEFORE if they are strings."
3de63fb6
RS
366 (easy-menu-define-key menu (if (stringp key) (intern key) key) item
367 (if (stringp before) (intern before) before)))
c78fb6a6
RS
368
369(defun easy-menu-define-key (menu key item &optional before)
4d6d04b5
DL
370 "Add binding in MENU for KEY => ITEM. Similar to `define-key-after'.
371If KEY is not nil then delete any duplications. If ITEM is nil, then
372don't insert, only delete.
373Optional argument BEFORE is nil or a key in MENU. If BEFORE is not nil
374put binding before BEFORE in MENU, otherwise if binding is already
375present in MENU, just change it, otherwise put it last in MENU.
376KEY and BEFORE don't have to be symbols, comparison is done with equal
377not with eq."
c78fb6a6 378 (let ((inserted (null item)) ; Fake already inserted.
d8868253 379 tail done)
d5660985
RS
380 (while (not done)
381 (cond
382 ((or (setq done (or (null (cdr menu)) (keymapp (cdr menu))))
c78fb6a6
RS
383 (and before (equal (car-safe (cadr menu)) before)))
384 ;; If key is nil, stop here, otherwise keep going past the
d5660985
RS
385 ;; inserted element so we can delete any duplications that come
386 ;; later.
c78fb6a6 387 (if (null key) (setq done t))
d5660985 388 (unless inserted ; Don't insert more than once.
c78fb6a6 389 (setcdr menu (cons (cons key item) (cdr menu)))
d5660985 390 (setq inserted t)
d8868253
RS
391 (setq menu (cdr menu)))
392 (setq menu (cdr menu)))
c78fb6a6 393 ((and key (equal (car-safe (cadr menu)) key))
d8868253
RS
394 (if (or inserted ; Already inserted or
395 (and before ; wanted elsewhere and
396 (setq tail (cddr menu)) ; not last item and not
397 (not (keymapp tail))
398 (not (equal (car-safe (car tail)) before)))) ; in position
c78fb6a6
RS
399 (setcdr menu (cddr menu)) ; Remove item.
400 (setcdr (cadr menu) item) ; Change item.
d8868253
RS
401 (setq inserted t)
402 (setq menu (cdr menu))))
403 (t (setq menu (cdr menu)))))))
6c2599ed 404
c78fb6a6 405(defun easy-menu-always-true (x)
4d6d04b5 406 "Return true if X never evaluates to nil."
c78fb6a6
RS
407 (if (consp x) (and (eq (car x) 'quote) (cadr x))
408 (or (eq x t) (not (symbolp x)))))
024bda02
RS
409
410(defvar easy-menu-item-count 0)
411
d0acce1e
SM
412(defun easy-menu-make-symbol (callback &optional noexp)
413 "Return a unique symbol with CALLBACK as function value.
414When non-nil, NOEXP indicates that CALLBACK cannot be an expression
415\(i.e. does not need to be turned into a function)."
024bda02
RS
416 (let ((command
417 (make-symbol (format "menu-function-%d" easy-menu-item-count))))
418 (setq easy-menu-item-count (1+ easy-menu-item-count))
419 (fset command
d0acce1e 420 (if (or (keymapp callback) noexp) callback
c78fb6a6 421 `(lambda () (interactive) ,callback)))
024bda02
RS
422 command))
423
168b2d0d 424;;;###autoload
024bda02 425(defun easy-menu-change (path name items &optional before)
88153c47 426 "Change menu found at PATH as item NAME to contain ITEMS.
fd43ede0
KH
427PATH is a list of strings for locating the menu that
428should contain a submenu named NAME.
429ITEMS is a list of menu items, as in `easy-menu-define'.
430These items entirely replace the previous items in that submenu.
431
432If the menu located by PATH has no submenu named NAME, add one.
433If the optional argument BEFORE is present, add it just before
434the submenu named BEFORE, otherwise add it at the end of the menu.
88153c47 435
024bda02
RS
436Either call this from `menu-bar-update-hook' or use a menu filter,
437to implement dynamic menus."
438 (easy-menu-add-item nil path (cons name items) before))
88153c47 439
024bda02
RS
440;; XEmacs needs the following two functions to add and remove menus.
441;; In Emacs this is done automatically when switching keymaps, so
545128a8
RS
442;; here easy-menu-remove is a noop and easy-menu-add only precalculates
443;; equivalent keybindings (if easy-menu-precalculate-equivalent-keybindings
444;; is on).
4d6d04b5 445(defalias 'easy-menu-remove 'ignore)
8df69fb0 446
545128a8
RS
447(defun easy-menu-add (menu &optional map)
448 "Maybe precalculate equivalent key bindings.
449Do it if `easy-menu-precalculate-equivalent-keybindings' is on,"
450 (when easy-menu-precalculate-equivalent-keybindings
451 (if (and (symbolp menu) (not (keymapp menu)) (boundp menu))
452 (setq menu (symbol-value menu)))
453 (if (keymapp menu) (x-popup-menu nil menu))))
8df69fb0 454
5e6656e0 455(defun easy-menu-add-item (map path item &optional before)
3de63fb6 456 "To the submenu of MAP with path PATH, add ITEM.
fd43ede0
KH
457
458If an item with the same name is already present in this submenu,
459then ITEM replaces it. Otherwise, ITEM is added to this submenu.
460In the latter case, ITEM is normally added at the end of the submenu.
461However, if BEFORE is a string and there is an item in the submenu
462with that name, then ITEM is added before that item.
5e6656e0
RS
463
464MAP should normally be a keymap; nil stands for the global menu-bar keymap.
465It can also be a symbol, which has earlier been used as the first
466argument in a call to `easy-menu-define', or the value of such a symbol.
467
024bda02 468PATH is a list of strings for locating the submenu where ITEM is to be
5e6656e0
RS
469added. If PATH is nil, MAP itself is used. Otherwise, the first
470element should be the name of a submenu directly under MAP. This
024bda02 471submenu is then traversed recursively with the remaining elements of PATH.
3de63fb6
RS
472
473ITEM is either defined as in `easy-menu-define' or a non-nil value returned
474by `easy-menu-item-present-p' or `easy-menu-remove-item' or a menu defined
475earlier by `easy-menu-define' or `easy-menu-create-menu'."
fd43ede0
KH
476 (setq map (easy-menu-get-map map path
477 (and (null map) (null path)
478 (stringp (car-safe item))
479 (car item))))
3de63fb6
RS
480 (if (and (consp item) (consp (cdr item)) (eq (cadr item) 'menu-item))
481 ;; This is a value returned by `easy-menu-item-present-p' or
482 ;; `easy-menu-remove-item'.
483 (easy-menu-define-key-intern map (car item) (cdr item) before)
484 (if (or (keymapp item)
485 (and (symbolp item) (keymapp (symbol-value item))))
486 ;; Item is a keymap, find the prompt string and use as item name.
487 (let ((tail (easy-menu-get-map item nil)) name)
488 (if (not (keymapp item)) (setq item tail))
489 (while (and (null name) (consp (setq tail (cdr tail)))
490 (not (keymapp tail)))
491 (if (stringp (car tail)) (setq name (car tail)) ; Got a name.
492 (setq tail (cdr tail))))
493 (setq item (cons name item))))
494 (easy-menu-do-add-item map item before)))
024bda02 495
5e6656e0
RS
496(defun easy-menu-item-present-p (map path name)
497 "In submenu of MAP with path PATH, return true iff item NAME is present.
498MAP and PATH are defined as in `easy-menu-add-item'.
024bda02 499NAME should be a string, the name of the element to be looked for."
3de63fb6 500 (easy-menu-return-item (easy-menu-get-map map path) name))
024bda02 501
5e6656e0
RS
502(defun easy-menu-remove-item (map path name)
503 "From submenu of MAP with path PATH remove item NAME.
504MAP and PATH are defined as in `easy-menu-add-item'.
024bda02 505NAME should be a string, the name of the element to be removed."
3de63fb6
RS
506 (setq map (easy-menu-get-map map path))
507 (let ((ret (easy-menu-return-item map name)))
508 (if ret (easy-menu-define-key-intern map name nil))
509 ret))
510
511(defun easy-menu-return-item (menu name)
4d6d04b5
DL
512 "In menu MENU try to look for menu item with name NAME.
513If a menu item is found, return (NAME . item), otherwise return nil.
514If item is an old format item, a new format item is returned."
3de63fb6
RS
515 (let ((item (lookup-key menu (vector (intern name))))
516 ret enable cache label)
517 (cond
518 ((or (keymapp item) (eq (car-safe item) 'menu-item))
519 (cons name item)) ; Keymap or new menu format
520 ((stringp (car-safe item))
521 ;; This is the old menu format. Convert it to new format.
522 (setq label (car item))
523 (when (stringp (car (setq item (cdr item)))) ; Got help string
524 (setq ret (list :help (car item)))
525 (setq item (cdr item)))
526 (when (and (consp item) (consp (car item))
527 (or (null (caar item)) (numberp (caar item))))
528 (setq cache (car item)) ; Got cache
529 (setq item (cdr item)))
530 (and (symbolp item) (setq enable (get item 'menu-enable)) ; Got enable
531 (setq ret (cons :enable (cons enable ret))))
532 (if cache (setq ret (cons cache ret)))
533 (cons name (cons 'menu-enable (cons label (cons item ret))))))))
024bda02 534
fd43ede0
KH
535(defun easy-menu-get-map-look-for-name (name submap)
536 (while (and submap (not (or (equal (car-safe (cdr-safe (car submap))) name)
537 (equal (car-safe (cdr-safe (cdr-safe (car submap)))) name))))
538 (setq submap (cdr submap)))
539 submap)
540
541(defun easy-menu-get-map (map path &optional to-modify)
4d6d04b5
DL
542 "Return a sparse keymap in which to add or remove an item.
543MAP and PATH are as defined in `easy-menu-add-item'.
fd43ede0 544
4d6d04b5
DL
545TO-MODIFY, if non-nil, is the name of the item the caller
546wants to modify in the map that we return.
547In some cases we use that to select between the local and global maps."
5e6656e0 548 (if (null map)
3373a131
RS
549 (let ((local (and (current-local-map)
550 (lookup-key (current-local-map)
551 (vconcat '(menu-bar) (mapcar 'intern path)))))
bec64ef6
RS
552 (global (lookup-key global-map
553 (vconcat '(menu-bar) (mapcar 'intern path)))))
fd43ede0
KH
554 (cond ((and to-modify local (not (integerp local))
555 (easy-menu-get-map-look-for-name to-modify local))
556 (setq map local))
557 ((and to-modify global (not (integerp global))
558 (easy-menu-get-map-look-for-name to-modify global))
559 (setq map global))
560 ((and local local (not (integerp local)))
561 (setq map local))
562 ((and global (not (integerp global)))
563 (setq map global))
564 (t
565 (setq map (make-sparse-keymap))
566 (define-key (current-local-map)
567 (vconcat '(menu-bar) (mapcar 'intern path)) map))))
5e6656e0
RS
568 (if (and (symbolp map) (not (keymapp map)))
569 (setq map (symbol-value map)))
570 (if path (setq map (lookup-key map (vconcat (mapcar 'intern path))))))
571 (while (and (symbolp map) (keymapp map))
572 (setq map (symbol-function map)))
a2c896c3
RS
573 (unless map
574 (error "Menu specified in easy-menu is not defined"))
5e6656e0
RS
575 (or (keymapp map) (error "Malformed menu in easy-menu: (%s)" map))
576 map)
024bda02 577
029b623a
RS
578(provide 'easymenu)
579
580;;; easymenu.el ends here