1 ;;; lmenu.el --- emulate Lucid's menubar support
3 ;; Copyright (C) 1992, 1993, 1994, 1997, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Keywords: emulations obsolete
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; First, emulate the Lucid menubar support in GNU Emacs 19.
30 ;; Arrange to use current-menubar to set up part of the menu bar.
32 (defvar current-menubar)
33 (defvar lucid-menubar-map)
34 (defvar lucid-failing-menubar)
36 (defvar recompute-lucid-menubar 'recompute-lucid-menubar)
37 (defun recompute-lucid-menubar ()
38 (define-key lucid-menubar-map [menu-bar]
40 (make-lucid-menu-keymap "menu-bar" current-menubar)
41 (error (message "Invalid data in current-menubar moved to lucid-failing-menubar")
43 (setq lucid-failing-menubar current-menubar
44 current-menubar nil))))
45 (setq lucid-menu-bar-dirty-flag nil))
47 (defvar lucid-menubar-map (make-sparse-keymap))
48 (or (assq 'current-menubar minor-mode-map-alist)
49 (setq minor-mode-map-alist
50 (cons (cons 'current-menubar lucid-menubar-map)
51 minor-mode-map-alist)))
53 ;; XEmacs compatibility
54 (defun set-menubar-dirty-flag ()
55 (force-mode-line-update)
56 (setq lucid-menu-bar-dirty-flag t))
58 (defvar add-menu-item-count 0)
60 ;; This is a variable whose value is always nil.
61 (defvar make-lucid-menu-keymap-disable nil)
63 ;; Return a menu keymap corresponding to a Lucid-style menu list
64 ;; MENU-ITEMS, and with name MENU-NAME.
65 (defun make-lucid-menu-keymap (menu-name menu-items)
66 (let ((menu (make-sparse-keymap menu-name)))
67 ;; Process items in reverse order,
68 ;; since the define-key loop reverses them again.
69 (setq menu-items (reverse menu-items))
71 (let ((item (car menu-items))
72 command name callback)
75 (setq name (if (string-match "^-+$" item) "" item)))
77 (setq command (make-lucid-menu-keymap (car item) (cdr item)))
78 (setq name (car item)))
80 (setq command (make-symbol (format "menu-function-%d"
82 add-menu-item-count (1+ add-menu-item-count)
84 callback (aref item 1))
85 (if (symbolp callback)
86 (fset command callback)
87 (fset command (list 'lambda () '(interactive) callback)))
88 (put command 'menu-alias t)
90 (while (< i (length item))
92 ((eq (aref item i) ':active)
93 (put command 'menu-enable
94 (or (aref item (1+ i))
95 'make-lucid-menu-keymap-disable))
97 ((eq (aref item i) ':suffix)
100 ((eq (aref item i) ':keys)
103 ((eq (aref item i) ':style)
106 ((eq (aref item i) ':selected)
109 ((and (symbolp (aref item i))
110 (= ?: (string-to-char (symbol-name (aref item i)))))
111 (error "Unrecognized menu item keyword: %S"
114 ;; old-style format: active-p &optional suffix
115 (put command 'menu-enable
116 (or (aref item i) 'make-lucid-menu-keymap-disable))
117 ;; suffix is unimplemented
118 (setq i (length item)))
120 (error "Unexpected menu item value: %S"
123 ;; Handle inactive strings specially--allow any number
124 ;; of identical ones.
125 (setcdr menu (cons (list nil name) (cdr menu)))
127 (define-key menu (vector (intern name)) (cons name command)))))
128 (setq menu-items (cdr menu-items)))
131 ;; XEmacs compatibility function
132 (defun popup-dialog-box (data)
133 "Pop up a dialog box.
134 A dialog box description is a list.
136 - The first element of the list is a string to display in the dialog box.
137 - The rest of the elements are descriptions of the dialog box's buttons.
138 Each one is a vector of three elements:
139 - The first element is the text of the button.
140 - The second element is the `callback'.
141 - The third element is t or nil, whether this button is selectable.
143 If the `callback' of a button is a symbol, then it must name a command.
144 It will be invoked with `call-interactively'. If it is a list, then it is
145 evaluated with `eval'.
147 One (and only one) of the buttons may be nil. This marker means that all
148 following buttons should be flushright instead of flushleft.
150 The syntax, more precisely:
152 form := <something to pass to `eval'>
153 command := <a symbol or string, to pass to `call-interactively'>
154 callback := command | form
155 active-p := <t, nil, or a form to evaluate to decide whether this
156 button should be selectable>
159 button := '[' name callback active-p ']'
160 dialog := '(' name [ button ]+ [ partition [ button ]+ ] ')'"
161 (let ((name (car data))
166 (if (null (car tail))
167 (setq converted (cons nil converted))
168 (let ((item (aref (car tail) 0))
169 (callback (aref (car tail) 1))
170 (enable (aref (car tail) 2)))
172 (cons (if enable (cons item callback) item)
174 (setq tail (cdr tail)))
175 (setq choice (x-popup-dialog t (cons name (nreverse converted))))
178 (call-interactively choice)
181 ;; This is empty because the usual elements of the menu bar
182 ;; are provided by menu-bar.el instead.
183 ;; It would not make sense to duplicate them here.
184 (defconst default-menubar nil)
186 ;; XEmacs compatibility
187 (defun set-menubar (menubar)
188 "Set the default menubar to be menubar."
189 (setq-default current-menubar (copy-sequence menubar))
190 (set-menubar-dirty-flag))
192 ;; XEmacs compatibility
193 (defun set-buffer-menubar (menubar)
194 "Set the buffer-local menubar to be menubar."
195 (make-local-variable 'current-menubar)
196 (setq current-menubar (copy-sequence menubar))
197 (set-menubar-dirty-flag))
200 ;;; menu manipulation functions
202 ;; XEmacs compatibility
203 (defun find-menu-item (menubar item-path-list &optional parent)
204 "Searches MENUBAR for item given by ITEM-PATH-LIST.
205 Returns (ITEM . PARENT), where PARENT is the immediate parent of
207 Signals an error if the item is not found."
208 (or parent (setq item-path-list (mapcar 'downcase item-path-list)))
209 (if (not (consp menubar))
215 (equal (car item-path-list)
216 (downcase (if (vectorp (car rest))
218 (if (stringp (car rest))
220 (car (car rest)))))))
221 (setq result (car rest) rest nil)
222 (setq rest (cdr rest))))
223 (if (cdr item-path-list)
225 (find-menu-item (cdr result) (cdr item-path-list) result)
227 (signal 'error (list "not a submenu" result))
228 (signal 'error (list "no such submenu" (car item-path-list)))))
229 (cons result parent)))))
232 ;; XEmacs compatibility
233 (defun disable-menu-item (path)
234 "Make the named menu item be unselectable.
235 PATH is a list of strings which identify the position of the menu item in
236 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
237 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
238 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
239 (let* ((menubar current-menubar)
240 (pair (find-menu-item menubar path))
244 (signal 'error (list (if menu "No such menu item" "No such menu")
246 (if (consp item) (error "can't disable menus, only menu items"))
248 (set-menubar-dirty-flag)
252 ;; XEmacs compatibility
253 (defun enable-menu-item (path)
254 "Make the named menu item be selectable.
255 PATH is a list of strings which identify the position of the menu item in
256 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
257 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
258 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
259 (let* ((menubar current-menubar)
260 (pair (find-menu-item menubar path))
264 (signal 'error (list (if menu "No such menu item" "No such menu")
266 (if (consp item) (error "%S is a menu, not a menu item" path))
268 (set-menubar-dirty-flag)
272 (defun add-menu-item-1 (item-p menu-path item-name item-data enabled-p before)
273 (if before (setq before (downcase before)))
274 (let* ((menubar current-menubar)
275 (menu (condition-case ()
276 (car (find-menu-item menubar menu-path))
278 (item (if (listp menu)
279 (car (find-menu-item (cdr menu) (list item-name)))
280 (signal 'error (list "not a submenu" menu-path)))))
282 (let ((rest menu-path)
285 ;;; (setq menu (car (find-menu-item (cdr so-far) (list (car rest)))))
287 (if (eq so-far menubar)
288 (car (find-menu-item so-far (list (car rest))))
289 (car (find-menu-item (cdr so-far) (list (car rest))))))
291 (let ((rest2 so-far))
293 (error "Trying to modify a menu that doesn't exist"))
294 (while (and (cdr rest2) (car (cdr rest2)))
295 (setq rest2 (cdr rest2)))
297 (nconc (list (setq menu (list (car rest))))
300 (setq rest (cdr rest)))))
301 (or menu (setq menu menubar))
303 nil ; it's already there
305 (setq item (vector item-name item-data enabled-p))
306 (setq item (cons item-name item-data)))
307 ;; if BEFORE is specified, try to add it there.
309 (setq before (car (find-menu-item menu (list before)))))
313 (if (eq before (car (cdr rest)))
315 (setcdr rest (cons item (cdr rest)))
316 (setq rest nil added-before t))
317 (setq rest (cdr rest))))
318 (if (not added-before)
319 ;; adding before the first item on the menubar itself is harder
320 (if (and (eq menu menubar) (eq before (car menu)))
321 (setq menu (cons item menu)
322 current-menubar menu)
323 ;; otherwise, add the item to the end.
324 (nconc menu (list item))))))
327 (aset item 1 item-data)
328 (aset item 2 (not (null enabled-p))))
329 (setcar item item-name)
330 (setcdr item item-data))
331 (set-menubar-dirty-flag)
334 ;; XEmacs compatibility
335 (defun add-menu-item (menu-path item-name function enabled-p &optional before)
336 "Add a menu item to some menu, creating the menu first if necessary.
337 If the named item exists already, it is changed.
338 MENU-PATH identifies the menu under which the new menu item should be inserted.
339 It is a list of strings; for example, (\"File\") names the top-level \"File\"
340 menu. (\"File\" \"Foo\") names a hypothetical submenu of \"File\".
341 ITEM-NAME is the string naming the menu item to be added.
342 FUNCTION is the command to invoke when this menu item is selected.
343 If it is a symbol, then it is invoked with `call-interactively', in the same
344 way that functions bound to keys are invoked. If it is a list, then the
345 list is simply evaluated.
346 ENABLED-P controls whether the item is selectable or not.
347 BEFORE, if provided, is the name of a menu item before which this item should
348 be added, if this item is not on the menu already. If the item is already
349 present, it will not be moved."
350 (or menu-path (error "must specify a menu path"))
351 (or item-name (error "must specify an item name"))
352 (add-menu-item-1 t menu-path item-name function enabled-p before))
355 ;; XEmacs compatibility
356 (defun delete-menu-item (path)
357 "Remove the named menu item from the menu hierarchy.
358 PATH is a list of strings which identify the position of the menu item in
359 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
360 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
361 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
362 (let* ((menubar current-menubar)
363 (pair (find-menu-item menubar path))
365 (menu (or (cdr pair) menubar)))
368 ;; the menubar is the only special case, because other menus begin
370 (if (eq menu current-menubar)
371 (setq current-menubar (delq item menu))
373 (set-menubar-dirty-flag)
377 ;; XEmacs compatibility
378 (defun relabel-menu-item (path new-name)
379 "Change the string of the specified menu item.
380 PATH is a list of strings which identify the position of the menu item in
381 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
382 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
383 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\".
384 NEW-NAME is the string that the menu item will be printed as from now on."
385 (or (stringp new-name)
386 (setq new-name (signal 'wrong-type-argument (list 'stringp new-name))))
387 (let* ((menubar current-menubar)
388 (pair (find-menu-item menubar path))
392 (signal 'error (list (if menu "No such menu item" "No such menu")
394 (if (and (consp item)
395 (stringp (car item)))
396 (setcar item new-name)
397 (aset item 0 new-name))
398 (set-menubar-dirty-flag)
401 ;; XEmacs compatibility
402 (defun add-menu (menu-path menu-name menu-items &optional before)
403 "Add a menu to the menubar or one of its submenus.
404 If the named menu exists already, it is changed.
405 MENU-PATH identifies the menu under which the new menu should be inserted.
406 It is a list of strings; for example, (\"File\") names the top-level \"File\"
407 menu. (\"File\" \"Foo\") names a hypothetical submenu of \"File\".
408 If MENU-PATH is nil, then the menu will be added to the menubar itself.
409 MENU-NAME is the string naming the menu to be added.
410 MENU-ITEMS is a list of menu item descriptions.
411 Each menu item should be a vector of three elements:
412 - a string, the name of the menu item;
413 - a symbol naming a command, or a form to evaluate;
414 - and a form whose value determines whether this item is selectable.
415 BEFORE, if provided, is the name of a menu before which this menu should
416 be added, if this menu is not on its parent already. If the menu is already
417 present, it will not be moved."
418 (or menu-name (error "must specify a menu name"))
419 (or menu-items (error "must specify some menu items"))
420 (add-menu-item-1 nil menu-path menu-name menu-items t before))
424 (defvar put-buffer-names-in-file-menu t)
427 ;; Don't unconditionally enable menu bars; leave that up to the user.
428 ;;(let ((frames (frame-list)))
430 ;; (modify-frame-parameters (car frames) '((menu-bar-lines . 1)))
431 ;; (setq frames (cdr frames))))
432 ;;(or (assq 'menu-bar-lines default-frame-alist)
433 ;; (setq default-frame-alist
434 ;; (cons '(menu-bar-lines . 1) default-frame-alist)))
436 (set-menubar default-menubar)
440 ;; arch-tag: 7051c396-2837-435a-ae11-b2d2e2af8fc1
441 ;;; lmenu.el ends here