1 ;;; lmenu.el --- emulate Lucid's menubar support
3 ;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
5 ;; Keywords: emulations
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
27 ;; First, emulate the Lucid menubar support in GNU Emacs 19.
29 ;; Arrange to use current-menubar to set up part of the menu bar.
31 (defvar current-menubar
)
33 (setq recompute-lucid-menubar
'recompute-lucid-menubar
)
34 (defun recompute-lucid-menubar ()
35 (define-key lucid-menubar-map
[menu-bar
]
37 (make-lucid-menu-keymap "menu-bar" current-menubar
)
38 (error (message "Invalid data in current-menubar moved to lucid-failing-menubar")
40 (setq lucid-failing-menubar current-menubar
41 current-menubar nil
))))
42 (setq lucid-menu-bar-dirty-flag nil
))
44 (defvar lucid-menubar-map
(make-sparse-keymap))
45 (or (assq 'current-menubar minor-mode-map-alist
)
46 (setq minor-mode-map-alist
47 (cons (cons 'current-menubar lucid-menubar-map
)
48 minor-mode-map-alist
)))
50 (defun set-menubar-dirty-flag ()
51 (force-mode-line-update)
52 (setq lucid-menu-bar-dirty-flag t
))
54 (defvar add-menu-item-count
0)
56 ;; This is a variable whose value is always nil.
57 (defvar make-lucid-menu-keymap-disable nil
)
59 ;; Return a menu keymap corresponding to a Lucid-style menu list
60 ;; MENU-ITEMS, and with name MENU-NAME.
61 (defun make-lucid-menu-keymap (menu-name menu-items
)
62 (let ((menu (make-sparse-keymap menu-name
)))
63 ;; Process items in reverse order,
64 ;; since the define-key loop reverses them again.
65 (setq menu-items
(reverse menu-items
))
67 (let ((item (car menu-items
))
68 command name callback
)
71 (setq name
(if (string-match "^-+$" item
) "" item
)))
73 (setq command
(make-lucid-menu-keymap (car item
) (cdr item
)))
74 (setq name
(car item
)))
76 (setq command
(make-symbol (format "menu-function-%d"
78 add-menu-item-count
(1+ add-menu-item-count
)
80 callback
(aref item
1))
81 (if (symbolp callback
)
82 (fset command callback
)
83 (fset command
(list 'lambda
() '(interactive) callback
)))
84 (put command
'menu-alias t
)
86 (while (< i
(length item
))
88 ((eq (aref item i
) ':active
)
89 (put command
'menu-enable
90 (or (aref item
(1+ i
))
91 'make-lucid-menu-keymap-disable
))
93 ((eq (aref item i
) ':suffix
)
96 ((eq (aref item i
) ':keys
)
99 ((eq (aref item i
) ':style
)
102 ((eq (aref item i
) ':selected
)
105 ((and (symbolp (aref item i
))
106 (= ?
: (string-to-char (symbol-name (aref item i
)))))
107 (error "Unrecognized menu item keyword: %S"
110 ;; old-style format: active-p &optional suffix
111 (put command
'menu-enable
112 (or (aref item i
) 'make-lucid-menu-keymap-disable
))
113 ;; suffix is unimplemented
114 (setq i
(length item
)))
116 (error "Unexpected menu item value: %S"
119 ;; Handle inactive strings specially--allow any number
120 ;; of identical ones.
121 (setcdr menu
(cons (list nil name
) (cdr menu
)))
123 (define-key menu
(vector (intern name
)) (cons name command
)))))
124 (setq menu-items
(cdr menu-items
)))
127 (defun popup-menu (menu-desc)
128 "Pop up the given menu.
129 A menu is a list of menu items, strings, and submenus.
131 The first element of a menu must be a string, which is the name of the
132 menu. This is the string that will be displayed in the parent menu, if
133 any. For toplevel menus, it is ignored. This string is not displayed
136 A menu item is a vector containing:
138 - the name of the menu item (a string);
139 - the `callback' of that item;
140 - a list of keywords with associated values:
141 - :active active-p a form specifying whether this item is selectable;
142 - :suffix suffix a string to be appended to the name as an `argument'
143 to the command, like `Kill Buffer NAME';
144 - :keys command-keys a string, suitable for `substitute-command-keys',
145 to specify the keyboard equivalent of a command
146 when the callback is a form (this is not necessary
147 when the callback is a symbol, as the keyboard
148 equivalent is computed automatically in that case);
149 - :style style a symbol: nil for a normal menu item, `toggle' for
150 a toggle button (a single option that can be turned
151 on or off), or `radio' for a radio button (one of a
152 group of mutually exclusive options);
153 - :selected form for `toggle' or `radio' style, a form that specifies
154 whether the button will be in the selected state.
156 Alternately, the vector may contain exactly 3 or 4 elements, with the third
157 element specifying `active-p' and the fourth specifying `suffix'.
159 If the `callback' of a menu item is a symbol, then it must name a command.
160 It will be invoked with `call-interactively'. If it is a list, then it is
161 evaluated with `eval'.
163 If an element of a menu is a string, then that string will be presented in
164 the menu as unselectable text.
166 If an element of a menu is a string consisting solely of hyphens, then that
167 item will be presented as a solid horizontal line.
169 If an element of a menu is a list, it is treated as a submenu. The name of
170 that submenu (the first element in the list) will be used as the name of the
171 item representing this menu on the parent.
173 The syntax, more precisely:
175 form := <something to pass to `eval'>
176 command := <a symbol or string, to pass to `call-interactively'>
177 callback := command | form
178 active-p := <t or nil, whether this thing is selectable>
179 text := <string, non selectable>
182 command-keys := <string>
183 object-style := 'nil' | 'toggle' | 'radio'
184 keyword := ':active' active-p
186 | ':keys' command-keys
187 | ':style' object-style
189 menu-item := '[' name callback active-p [ suffix ] ']'
190 | '[' name callback [ keyword ]+ ']'
191 menu := '(' name [ menu-item | menu | text ]+ ')'"
192 (let ((menu (make-lucid-menu-keymap (car menu-desc
) (cdr menu-desc
)))
193 (pos (mouse-pixel-position))
196 (setq answer
(x-popup-menu (list (list (nth 1 pos
)
200 (setq cmd
(lookup-key menu
(apply 'vector answer
)))
205 (call-interactively cmd
))))))
207 (defun popup-dialog-box (data)
208 "Pop up a dialog box.
209 A dialog box description is a list.
211 - The first element of the list is a string to display in the dialog box.
212 - The rest of the elements are descriptions of the dialog box's buttons.
213 Each one is a vector of three elements:
214 - The first element is the text of the button.
215 - The second element is the `callback'.
216 - The third element is t or nil, whether this button is selectable.
218 If the `callback' of a button is a symbol, then it must name a command.
219 It will be invoked with `call-interactively'. If it is a list, then it is
220 evaluated with `eval'.
222 One (and only one) of the buttons may be `nil'. This marker means that all
223 following buttons should be flushright instead of flushleft.
225 The syntax, more precisely:
227 form := <something to pass to `eval'>
228 command := <a symbol or string, to pass to `call-interactively'>
229 callback := command | form
230 active-p := <t, nil, or a form to evaluate to decide whether this
231 button should be selectable>
234 button := '[' name callback active-p ']'
235 dialog := '(' name [ button ]+ [ partition [ button ]+ ] ')'"
236 (let ((name (car data
))
241 (if (null (car tail
))
242 (setq converted
(cons nil converted
))
243 (let ((item (aref (car tail
) 0))
244 (callback (aref (car tail
) 1))
245 (enable (aref (car tail
) 2)))
247 (cons (if enable
(cons item callback
) item
)
249 (setq tail
(cdr tail
)))
250 (setq choice
(x-popup-dialog t
(cons name
(nreverse converted
))))
253 (call-interactively choice
)
256 ;; This is empty because the usual elements of the menu bar
257 ;; are provided by menu-bar.el instead.
258 ;; It would not make sense to duplicate them here.
259 (defconst default-menubar nil
)
261 (defun set-menubar (menubar)
262 "Set the default menubar to be menubar."
263 (setq-default current-menubar
(copy-sequence menubar
))
264 (set-menubar-dirty-flag))
266 (defun set-buffer-menubar (menubar)
267 "Set the buffer-local menubar to be menubar."
268 (make-local-variable 'current-menubar
)
269 (setq current-menubar
(copy-sequence menubar
))
270 (set-menubar-dirty-flag))
273 ;;; menu manipulation functions
275 (defun find-menu-item (menubar item-path-list
&optional parent
)
276 "Searches MENUBAR for item given by ITEM-PATH-LIST.
277 Returns (ITEM . PARENT), where PARENT is the immediate parent of
279 Signals an error if the item is not found."
280 (or parent
(setq item-path-list
(mapcar 'downcase item-path-list
)))
281 (if (not (consp menubar
))
287 (equal (car item-path-list
)
288 (downcase (if (vectorp (car rest
))
290 (if (stringp (car rest
))
292 (car (car rest
)))))))
293 (setq result
(car rest
) rest nil
)
294 (setq rest
(cdr rest
))))
295 (if (cdr item-path-list
)
297 (find-menu-item (cdr result
) (cdr item-path-list
) result
)
299 (signal 'error
(list "not a submenu" result
))
300 (signal 'error
(list "no such submenu" (car item-path-list
)))))
301 (cons result parent
)))))
304 (defun disable-menu-item (path)
305 "Make the named menu item be unselectable.
306 PATH is a list of strings which identify the position of the menu item in
307 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
308 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
309 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
310 (let* ((menubar current-menubar
)
311 (pair (find-menu-item menubar path
))
315 (signal 'error
(list (if menu
"No such menu item" "No such menu")
317 (if (consp item
) (error "can't disable menus, only menu items"))
319 (set-menubar-dirty-flag)
323 (defun enable-menu-item (path)
324 "Make the named menu item be selectable.
325 PATH is a list of strings which identify the position of the menu item in
326 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
327 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
328 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
329 (let* ((menubar current-menubar
)
330 (pair (find-menu-item menubar path
))
334 (signal 'error
(list (if menu
"No such menu item" "No such menu")
336 (if (consp item
) (error "%S is a menu, not a menu item" path
))
338 (set-menubar-dirty-flag)
342 (defun add-menu-item-1 (item-p menu-path item-name item-data enabled-p before
)
343 (if before
(setq before
(downcase before
)))
344 (let* ((menubar current-menubar
)
345 (menu (condition-case ()
346 (car (find-menu-item menubar menu-path
))
348 (item (if (listp menu
)
349 (car (find-menu-item (cdr menu
) (list item-name
)))
350 (signal 'error
(list "not a submenu" menu-path
)))))
352 (let ((rest menu-path
)
355 ;;; (setq menu (car (find-menu-item (cdr so-far) (list (car rest)))))
357 (if (eq so-far menubar
)
358 (car (find-menu-item so-far
(list (car rest
))))
359 (car (find-menu-item (cdr so-far
) (list (car rest
))))))
361 (let ((rest2 so-far
))
363 (error "Trying to modify a menu that doesn't exist"))
364 (while (and (cdr rest2
) (car (cdr rest2
)))
365 (setq rest2
(cdr rest2
)))
367 (nconc (list (setq menu
(list (car rest
))))
370 (setq rest
(cdr rest
)))))
371 (or menu
(setq menu menubar
))
373 nil
; it's already there
375 (setq item
(vector item-name item-data enabled-p
))
376 (setq item
(cons item-name item-data
)))
377 ;; if BEFORE is specified, try to add it there.
379 (setq before
(car (find-menu-item menu
(list before
)))))
383 (if (eq before
(car (cdr rest
)))
385 (setcdr rest
(cons item
(cdr rest
)))
386 (setq rest nil added-before t
))
387 (setq rest
(cdr rest
))))
388 (if (not added-before
)
389 ;; adding before the first item on the menubar itself is harder
390 (if (and (eq menu menubar
) (eq before
(car menu
)))
391 (setq menu
(cons item menu
)
392 current-menubar menu
)
393 ;; otherwise, add the item to the end.
394 (nconc menu
(list item
))))))
397 (aset item
1 item-data
)
398 (aset item
2 (not (null enabled-p
))))
399 (setcar item item-name
)
400 (setcdr item item-data
))
401 (set-menubar-dirty-flag)
404 (defun add-menu-item (menu-path item-name function enabled-p
&optional before
)
405 "Add a menu item to some menu, creating the menu first if necessary.
406 If the named item exists already, it is changed.
407 MENU-PATH identifies the menu under which the new menu item should be inserted.
408 It is a list of strings; for example, (\"File\") names the top-level \"File\"
409 menu. (\"File\" \"Foo\") names a hypothetical submenu of \"File\".
410 ITEM-NAME is the string naming the menu item to be added.
411 FUNCTION is the command to invoke when this menu item is selected.
412 If it is a symbol, then it is invoked with `call-interactively', in the same
413 way that functions bound to keys are invoked. If it is a list, then the
414 list is simply evaluated.
415 ENABLED-P controls whether the item is selectable or not.
416 BEFORE, if provided, is the name of a menu item before which this item should
417 be added, if this item is not on the menu already. If the item is already
418 present, it will not be moved."
419 (or menu-path
(error "must specify a menu path"))
420 (or item-name
(error "must specify an item name"))
421 (add-menu-item-1 t menu-path item-name function enabled-p before
))
424 (defun delete-menu-item (path)
425 "Remove the named menu item from the menu hierarchy.
426 PATH is a list of strings which identify the position of the menu item in
427 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
428 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
429 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
430 (let* ((menubar current-menubar
)
431 (pair (find-menu-item menubar path
))
433 (menu (or (cdr pair
) menubar
)))
436 ;; the menubar is the only special case, because other menus begin
438 (if (eq menu current-menubar
)
439 (setq current-menubar
(delq item menu
))
441 (set-menubar-dirty-flag)
445 (defun relabel-menu-item (path new-name
)
446 "Change the string of the specified menu item.
447 PATH is a list of strings which identify the position of the menu item in
448 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
449 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
450 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\".
451 NEW-NAME is the string that the menu item will be printed as from now on."
452 (or (stringp new-name
)
453 (setq new-name
(signal 'wrong-type-argument
(list 'stringp new-name
))))
454 (let* ((menubar current-menubar
)
455 (pair (find-menu-item menubar path
))
459 (signal 'error
(list (if menu
"No such menu item" "No such menu")
461 (if (and (consp item
)
462 (stringp (car item
)))
463 (setcar item new-name
)
464 (aset item
0 new-name
))
465 (set-menubar-dirty-flag)
468 (defun add-menu (menu-path menu-name menu-items
&optional before
)
469 "Add a menu to the menubar or one of its submenus.
470 If the named menu exists already, it is changed.
471 MENU-PATH identifies the menu under which the new menu should be inserted.
472 It is a list of strings; for example, (\"File\") names the top-level \"File\"
473 menu. (\"File\" \"Foo\") names a hypothetical submenu of \"File\".
474 If MENU-PATH is nil, then the menu will be added to the menubar itself.
475 MENU-NAME is the string naming the menu to be added.
476 MENU-ITEMS is a list of menu item descriptions.
477 Each menu item should be a vector of three elements:
478 - a string, the name of the menu item;
479 - a symbol naming a command, or a form to evaluate;
480 - and a form whose value determines whether this item is selectable.
481 BEFORE, if provided, is the name of a menu before which this menu should
482 be added, if this menu is not on its parent already. If the menu is already
483 present, it will not be moved."
484 (or menu-name
(error "must specify a menu name"))
485 (or menu-items
(error "must specify some menu items"))
486 (add-menu-item-1 nil menu-path menu-name menu-items t before
))
490 (defvar put-buffer-names-in-file-menu t
)
493 ;; Don't unconditionally enable menu bars; leave that up to the user.
494 ;;(let ((frames (frame-list)))
496 ;; (modify-frame-parameters (car frames) '((menu-bar-lines . 1)))
497 ;; (setq frames (cdr frames))))
498 ;;(or (assq 'menu-bar-lines default-frame-alist)
499 ;; (setq default-frame-alist
500 ;; (cons '(menu-bar-lines . 1) default-frame-alist)))
502 (set-menubar default-menubar
)
506 ;;; lmenu.el ends here