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