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