lmenu.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. ;;; lmenu.el --- emulate Lucid's menubar support
  2. ;; Copyright (C) 1992-1994, 1997, 2001-2012 Free Software Foundation, Inc.
  3. ;; Keywords: emulations obsolete
  4. ;; Obsolete-since: 23.3
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This file has been obsolete since Emacs 23.3.
  18. ;;; Code:
  19. ;; First, emulate the Lucid menubar support in GNU Emacs 19.
  20. ;; Arrange to use current-menubar to set up part of the menu bar.
  21. (defvar current-menubar)
  22. (defvar lucid-menubar-map)
  23. (defvar lucid-failing-menubar)
  24. (defvar recompute-lucid-menubar 'recompute-lucid-menubar)
  25. (defun recompute-lucid-menubar ()
  26. (define-key lucid-menubar-map [menu-bar]
  27. (condition-case nil
  28. (make-lucid-menu-keymap "menu-bar" current-menubar)
  29. (error (message "Invalid data in current-menubar moved to lucid-failing-menubar")
  30. (sit-for 1)
  31. (setq lucid-failing-menubar current-menubar
  32. current-menubar nil))))
  33. (setq lucid-menu-bar-dirty-flag nil))
  34. (defvar lucid-menubar-map (make-sparse-keymap))
  35. (or (assq 'current-menubar minor-mode-map-alist)
  36. (setq minor-mode-map-alist
  37. (cons (cons 'current-menubar lucid-menubar-map)
  38. minor-mode-map-alist)))
  39. ;; XEmacs compatibility
  40. (defun set-menubar-dirty-flag ()
  41. (force-mode-line-update)
  42. (setq lucid-menu-bar-dirty-flag t))
  43. (defvar add-menu-item-count 0)
  44. ;; This is a variable whose value is always nil.
  45. (defvar make-lucid-menu-keymap-disable nil)
  46. ;; Return a menu keymap corresponding to a Lucid-style menu list
  47. ;; MENU-ITEMS, and with name MENU-NAME.
  48. (defun make-lucid-menu-keymap (menu-name menu-items)
  49. (let ((menu (make-sparse-keymap menu-name)))
  50. ;; Process items in reverse order,
  51. ;; since the define-key loop reverses them again.
  52. (setq menu-items (reverse menu-items))
  53. (while menu-items
  54. (let ((item (car menu-items))
  55. command name callback)
  56. (cond ((stringp item)
  57. (setq command nil)
  58. (setq name (if (string-match "^-+$" item) "" item)))
  59. ((consp item)
  60. (setq command (make-lucid-menu-keymap (car item) (cdr item)))
  61. (setq name (car item)))
  62. ((vectorp item)
  63. (setq command (make-symbol (format "menu-function-%d"
  64. add-menu-item-count))
  65. add-menu-item-count (1+ add-menu-item-count)
  66. name (aref item 0)
  67. callback (aref item 1))
  68. (if (symbolp callback)
  69. (fset command callback)
  70. (fset command (list 'lambda () '(interactive) callback)))
  71. (put command 'menu-alias t)
  72. (let ((i 2))
  73. (while (< i (length item))
  74. (cond
  75. ((eq (aref item i) ':active)
  76. (put command 'menu-enable
  77. (or (aref item (1+ i))
  78. 'make-lucid-menu-keymap-disable))
  79. (setq i (+ 2 i)))
  80. ((eq (aref item i) ':suffix)
  81. ;; unimplemented
  82. (setq i (+ 2 i)))
  83. ((eq (aref item i) ':keys)
  84. ;; unimplemented
  85. (setq i (+ 2 i)))
  86. ((eq (aref item i) ':style)
  87. ;; unimplemented
  88. (setq i (+ 2 i)))
  89. ((eq (aref item i) ':selected)
  90. ;; unimplemented
  91. (setq i (+ 2 i)))
  92. ((and (symbolp (aref item i))
  93. (= ?: (string-to-char (symbol-name (aref item i)))))
  94. (error "Unrecognized menu item keyword: %S"
  95. (aref item i)))
  96. ((= i 2)
  97. ;; old-style format: active-p &optional suffix
  98. (put command 'menu-enable
  99. (or (aref item i) 'make-lucid-menu-keymap-disable))
  100. ;; suffix is unimplemented
  101. (setq i (length item)))
  102. (t
  103. (error "Unexpected menu item value: %S"
  104. (aref item i))))))))
  105. (if (null command)
  106. ;; Handle inactive strings specially--allow any number
  107. ;; of identical ones.
  108. (setcdr menu (cons (list nil name) (cdr menu)))
  109. (if name
  110. (define-key menu (vector (intern name)) (cons name command)))))
  111. (setq menu-items (cdr menu-items)))
  112. menu))
  113. (declare-function x-popup-dialog "xmenu.c" (position contents &optional header))
  114. ;; XEmacs compatibility function
  115. (defun popup-dialog-box (data)
  116. "Pop up a dialog box.
  117. A dialog box description is a list.
  118. - The first element of the list is a string to display in the dialog box.
  119. - The rest of the elements are descriptions of the dialog box's buttons.
  120. Each one is a vector of three elements:
  121. - The first element is the text of the button.
  122. - The second element is the `callback'.
  123. - The third element is t or nil, whether this button is selectable.
  124. If the `callback' of a button is a symbol, then it must name a command.
  125. It will be invoked with `call-interactively'. If it is a list, then it is
  126. evaluated with `eval'.
  127. One (and only one) of the buttons may be nil. This marker means that all
  128. following buttons should be flushright instead of flushleft.
  129. The syntax, more precisely:
  130. form := <something to pass to `eval'>
  131. command := <a symbol or string, to pass to `call-interactively'>
  132. callback := command | form
  133. active-p := <t, nil, or a form to evaluate to decide whether this
  134. button should be selectable>
  135. name := <string>
  136. partition := 'nil'
  137. button := '[' name callback active-p ']'
  138. dialog := '(' name [ button ]+ [ partition [ button ]+ ] ')'"
  139. (let ((name (car data))
  140. (tail (cdr data))
  141. converted
  142. choice meaning)
  143. (while tail
  144. (if (null (car tail))
  145. (setq converted (cons nil converted))
  146. (let ((item (aref (car tail) 0))
  147. (callback (aref (car tail) 1))
  148. (enable (aref (car tail) 2)))
  149. (setq converted
  150. (cons (if enable (cons item callback) item)
  151. converted))))
  152. (setq tail (cdr tail)))
  153. (setq choice (x-popup-dialog t (cons name (nreverse converted))))
  154. (if choice
  155. (if (symbolp choice)
  156. (call-interactively choice)
  157. (eval choice)))))
  158. ;; This is empty because the usual elements of the menu bar
  159. ;; are provided by menu-bar.el instead.
  160. ;; It would not make sense to duplicate them here.
  161. (defconst default-menubar nil)
  162. ;; XEmacs compatibility
  163. (defun set-menubar (menubar)
  164. "Set the default menubar to be menubar."
  165. (setq-default current-menubar (copy-sequence menubar))
  166. (set-menubar-dirty-flag))
  167. ;; XEmacs compatibility
  168. (defun set-buffer-menubar (menubar)
  169. "Set the buffer-local menubar to be menubar."
  170. (make-local-variable 'current-menubar)
  171. (setq current-menubar (copy-sequence menubar))
  172. (set-menubar-dirty-flag))
  173. ;;; menu manipulation functions
  174. ;; XEmacs compatibility
  175. (defun find-menu-item (menubar item-path-list &optional parent)
  176. "Searches MENUBAR for item given by ITEM-PATH-LIST.
  177. Returns (ITEM . PARENT), where PARENT is the immediate parent of
  178. the item found.
  179. Signals an error if the item is not found."
  180. (or parent (setq item-path-list (mapcar 'downcase item-path-list)))
  181. (if (not (consp menubar))
  182. nil
  183. (let ((rest menubar)
  184. result)
  185. (while rest
  186. (if (and (car rest)
  187. (equal (car item-path-list)
  188. (downcase (if (vectorp (car rest))
  189. (aref (car rest) 0)
  190. (if (stringp (car rest))
  191. (car rest)
  192. (car (car rest)))))))
  193. (setq result (car rest) rest nil)
  194. (setq rest (cdr rest))))
  195. (if (cdr item-path-list)
  196. (if (consp result)
  197. (find-menu-item (cdr result) (cdr item-path-list) result)
  198. (if result
  199. (signal 'error (list "not a submenu" result))
  200. (signal 'error (list "no such submenu" (car item-path-list)))))
  201. (cons result parent)))))
  202. ;; XEmacs compatibility
  203. (defun disable-menu-item (path)
  204. "Make the named menu item be unselectable.
  205. PATH is a list of strings which identify the position of the menu item in
  206. the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
  207. under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
  208. menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
  209. (let* ((menubar current-menubar)
  210. (pair (find-menu-item menubar path))
  211. (item (car pair))
  212. (menu (cdr pair)))
  213. (or item
  214. (signal 'error (list (if menu "No such menu item" "No such menu")
  215. path)))
  216. (if (consp item) (error "can't disable menus, only menu items"))
  217. (aset item 2 nil)
  218. (set-menubar-dirty-flag)
  219. item))
  220. ;; XEmacs compatibility
  221. (defun enable-menu-item (path)
  222. "Make the named menu item be selectable.
  223. PATH is a list of strings which identify the position of the menu item in
  224. the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
  225. under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
  226. menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
  227. (let* ((menubar current-menubar)
  228. (pair (find-menu-item menubar path))
  229. (item (car pair))
  230. (menu (cdr pair)))
  231. (or item
  232. (signal 'error (list (if menu "No such menu item" "No such menu")
  233. path)))
  234. (if (consp item) (error "%S is a menu, not a menu item" path))
  235. (aset item 2 t)
  236. (set-menubar-dirty-flag)
  237. item))
  238. (defun add-menu-item-1 (item-p menu-path item-name item-data enabled-p before)
  239. (if before (setq before (downcase before)))
  240. (let* ((menubar current-menubar)
  241. (menu (condition-case ()
  242. (car (find-menu-item menubar menu-path))
  243. (error nil)))
  244. (item (if (listp menu)
  245. (car (find-menu-item (cdr menu) (list item-name)))
  246. (signal 'error (list "not a submenu" menu-path)))))
  247. (or menu
  248. (let ((rest menu-path)
  249. (so-far menubar))
  250. (while rest
  251. ;;; (setq menu (car (find-menu-item (cdr so-far) (list (car rest)))))
  252. (setq menu
  253. (if (eq so-far menubar)
  254. (car (find-menu-item so-far (list (car rest))))
  255. (car (find-menu-item (cdr so-far) (list (car rest))))))
  256. (or menu
  257. (let ((rest2 so-far))
  258. (or rest2
  259. (error "Trying to modify a menu that doesn't exist"))
  260. (while (and (cdr rest2) (car (cdr rest2)))
  261. (setq rest2 (cdr rest2)))
  262. (setcdr rest2
  263. (nconc (list (setq menu (list (car rest))))
  264. (cdr rest2)))))
  265. (setq so-far menu)
  266. (setq rest (cdr rest)))))
  267. (or menu (setq menu menubar))
  268. (if item
  269. nil ; it's already there
  270. (if item-p
  271. (setq item (vector item-name item-data enabled-p))
  272. (setq item (cons item-name item-data)))
  273. ;; if BEFORE is specified, try to add it there.
  274. (if before
  275. (setq before (car (find-menu-item menu (list before)))))
  276. (let ((rest menu)
  277. (added-before nil))
  278. (while rest
  279. (if (eq before (car (cdr rest)))
  280. (progn
  281. (setcdr rest (cons item (cdr rest)))
  282. (setq rest nil added-before t))
  283. (setq rest (cdr rest))))
  284. (if (not added-before)
  285. ;; adding before the first item on the menubar itself is harder
  286. (if (and (eq menu menubar) (eq before (car menu)))
  287. (setq menu (cons item menu)
  288. current-menubar menu)
  289. ;; otherwise, add the item to the end.
  290. (nconc menu (list item))))))
  291. (if item-p
  292. (progn
  293. (aset item 1 item-data)
  294. (aset item 2 (not (null enabled-p))))
  295. (setcar item item-name)
  296. (setcdr item item-data))
  297. (set-menubar-dirty-flag)
  298. item))
  299. ;; XEmacs compatibility
  300. (defun add-menu-item (menu-path item-name function enabled-p &optional before)
  301. "Add a menu item to some menu, creating the menu first if necessary.
  302. If the named item exists already, it is changed.
  303. MENU-PATH identifies the menu under which the new menu item should be inserted.
  304. It is a list of strings; for example, (\"File\") names the top-level \"File\"
  305. menu. (\"File\" \"Foo\") names a hypothetical submenu of \"File\".
  306. ITEM-NAME is the string naming the menu item to be added.
  307. FUNCTION is the command to invoke when this menu item is selected.
  308. If it is a symbol, then it is invoked with `call-interactively', in the same
  309. way that functions bound to keys are invoked. If it is a list, then the
  310. list is simply evaluated.
  311. ENABLED-P controls whether the item is selectable or not.
  312. BEFORE, if provided, is the name of a menu item before which this item should
  313. be added, if this item is not on the menu already. If the item is already
  314. present, it will not be moved."
  315. (or menu-path (error "must specify a menu path"))
  316. (or item-name (error "must specify an item name"))
  317. (add-menu-item-1 t menu-path item-name function enabled-p before))
  318. ;; XEmacs compatibility
  319. (defun delete-menu-item (path)
  320. "Remove the named menu item from the menu hierarchy.
  321. PATH is a list of strings which identify the position of the menu item in
  322. the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
  323. under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
  324. menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
  325. (let* ((menubar current-menubar)
  326. (pair (find-menu-item menubar path))
  327. (item (car pair))
  328. (menu (or (cdr pair) menubar)))
  329. (if (not item)
  330. nil
  331. ;; the menubar is the only special case, because other menus begin
  332. ;; with their name.
  333. (if (eq menu current-menubar)
  334. (setq current-menubar (delq item menu))
  335. (delq item menu))
  336. (set-menubar-dirty-flag)
  337. item)))
  338. ;; XEmacs compatibility
  339. (defun relabel-menu-item (path new-name)
  340. "Change the string of the specified menu item.
  341. PATH is a list of strings which identify the position of the menu item in
  342. the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
  343. under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
  344. menu item called \"Item\" under the \"Foo\" submenu of \"Menu\".
  345. NEW-NAME is the string that the menu item will be printed as from now on."
  346. (or (stringp new-name)
  347. (setq new-name (signal 'wrong-type-argument (list 'stringp new-name))))
  348. (let* ((menubar current-menubar)
  349. (pair (find-menu-item menubar path))
  350. (item (car pair))
  351. (menu (cdr pair)))
  352. (or item
  353. (signal 'error (list (if menu "No such menu item" "No such menu")
  354. path)))
  355. (if (and (consp item)
  356. (stringp (car item)))
  357. (setcar item new-name)
  358. (aset item 0 new-name))
  359. (set-menubar-dirty-flag)
  360. item))
  361. ;; XEmacs compatibility
  362. (defun add-menu (menu-path menu-name menu-items &optional before)
  363. "Add a menu to the menubar or one of its submenus.
  364. If the named menu exists already, it is changed.
  365. MENU-PATH identifies the menu under which the new menu should be inserted.
  366. It is a list of strings; for example, (\"File\") names the top-level \"File\"
  367. menu. (\"File\" \"Foo\") names a hypothetical submenu of \"File\".
  368. If MENU-PATH is nil, then the menu will be added to the menubar itself.
  369. MENU-NAME is the string naming the menu to be added.
  370. MENU-ITEMS is a list of menu item descriptions.
  371. Each menu item should be a vector of three elements:
  372. - a string, the name of the menu item;
  373. - a symbol naming a command, or a form to evaluate;
  374. - and a form whose value determines whether this item is selectable.
  375. BEFORE, if provided, is the name of a menu before which this menu should
  376. be added, if this menu is not on its parent already. If the menu is already
  377. present, it will not be moved."
  378. (or menu-name (error "must specify a menu name"))
  379. (or menu-items (error "must specify some menu items"))
  380. (add-menu-item-1 nil menu-path menu-name menu-items t before))
  381. (defvar put-buffer-names-in-file-menu t)
  382. ;; Don't unconditionally enable menu bars; leave that up to the user.
  383. ;;(let ((frames (frame-list)))
  384. ;; (while frames
  385. ;; (modify-frame-parameters (car frames) '((menu-bar-lines . 1)))
  386. ;; (setq frames (cdr frames))))
  387. ;;(or (assq 'menu-bar-lines default-frame-alist)
  388. ;; (setq default-frame-alist
  389. ;; (cons '(menu-bar-lines . 1) default-frame-alist)))
  390. (set-menubar default-menubar)
  391. (provide 'lmenu)
  392. ;;; lmenu.el ends here