easymenu.el 27 KB

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