Update copyright.
[bpt/emacs.git] / lisp / emacs-lisp / lmenu.el
CommitLineData
aae56ea7
ER
1;;; lmenu.el --- emulate Lucid's menubar support
2
76550a57
ER
3;; Keywords: emulations
4
d733c5ec 5;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
c7986c18
ER
6
7;; This file is part of GNU Emacs.
8
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)
12;; any later version.
13
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.
18
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
21;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
aae56ea7
ER
23;;; Code:
24
c7986c18
ER
25\f
26;; First, emulate the Lucid menubar support in GNU Emacs 19.
27
28;; Arrange to use current-menubar to set up part of the menu bar.
29
f1e20cc2
RS
30(defvar current-menubar)
31
c7986c18
ER
32(setq recompute-lucid-menubar 'recompute-lucid-menubar)
33(defun recompute-lucid-menubar ()
34 (define-key lucid-menubar-map [menu-bar]
35 (condition-case nil
36 (make-lucid-menu-keymap "menu-bar" current-menubar)
37 (error (message "Invalid data in current-menubar moved to lucid-failing-menubar")
38 (sit-for 1)
39 (setq lucid-failing-menubar current-menubar
40 current-menubar nil))))
41 (setq lucid-menu-bar-dirty-flag nil))
42
43(defvar lucid-menubar-map (make-sparse-keymap))
44(or (assq 'current-menubar minor-mode-map-alist)
45 (setq minor-mode-map-alist
46 (cons (cons 'current-menubar lucid-menubar-map)
47 minor-mode-map-alist)))
48
49(defun set-menubar-dirty-flag ()
50 (force-mode-line-update)
51 (setq lucid-menu-bar-dirty-flag t))
52
53(defvar add-menu-item-count 0)
54
1c45ffdf
RS
55;; This is a variable whose value is always nil.
56(defvar make-lucid-menu-keymap-disable nil)
57
c7986c18
ER
58;; Return a menu keymap corresponding to a Lucid-style menu list
59;; MENU-ITEMS, and with name MENU-NAME.
60(defun make-lucid-menu-keymap (menu-name menu-items)
61 (let ((menu (make-sparse-keymap menu-name)))
62 ;; Process items in reverse order,
63 ;; since the define-key loop reverses them again.
64 (setq menu-items (reverse menu-items))
65 (while menu-items
67893ba3
KH
66 (let ((item (car menu-items))
67 command name callback)
c7986c18
ER
68 (cond ((stringp item)
69 (setq command nil)
a7605222 70 (setq name (if (string-match "^-+$" item) "" item)))
c7986c18
ER
71 ((consp item)
72 (setq command (make-lucid-menu-keymap (car item) (cdr item)))
73 (setq name (car item)))
74 ((vectorp item)
75 (setq command (make-symbol (format "menu-function-%d"
67893ba3
KH
76 add-menu-item-count))
77 add-menu-item-count (1+ add-menu-item-count)
78 name (aref item 0)
79 callback (aref item 1))
c7986c18
ER
80 (if (symbolp callback)
81 (fset command callback)
67893ba3
KH
82 (fset command (list 'lambda () '(interactive) callback)))
83 (let ((i 2))
84 (while (< i (length item))
85 (cond
86 ((eq (aref item i) ':active)
87 (put command 'menu-enable
88 (or (aref item (1+ i))
89 'make-lucid-menu-keymap-disable))
90 (setq i (+ 2 i)))
91 ((eq (aref item i) ':suffix)
92 ;; unimplemented
93 (setq i (+ 2 i)))
94 ((eq (aref item i) ':keys)
95 ;; unimplemented
96 (setq i (+ 2 i)))
97 ((eq (aref item i) ':style)
98 ;; unimplemented
99 (setq i (+ 2 i)))
100 ((eq (aref item i) ':selected)
101 ;; unimplemented
102 (setq i (+ 2 i)))
103 ((and (symbolp (aref item i))
104 (= ?: (string-to-char (symbol-name (aref item i)))))
105 (error "Unrecognized menu item keyword: %S"
106 (aref item i)))
107 ((= i 2)
108 ;; old-style format: active-p &optional suffix
109 (put command 'menu-enable
110 (or (aref item i) 'make-lucid-menu-keymap-disable))
111 ;; suffix is unimplemented
112 (setq i (length item)))
113 (t
114 (error "Unexpected menu item value: %S"
115 (aref item i))))))))
b69f3ab1
RS
116 (if (null command)
117 ;; Handle inactive strings specially--allow any number
118 ;; of identical ones.
119 (setcdr menu (cons (list nil name) (cdr menu)))
67893ba3 120 (if name
b69f3ab1 121 (define-key menu (vector (intern name)) (cons name command)))))
c7986c18
ER
122 (setq menu-items (cdr menu-items)))
123 menu))
124
125(defun popup-menu (menu-desc)
126 "Pop up the given menu.
127A menu is a list of menu items, strings, and submenus.
128
129The first element of a menu must be a string, which is the name of the
130menu. This is the string that will be displayed in the parent menu, if
131any. For toplevel menus, it is ignored. This string is not displayed
132in the menu itself.
133
67893ba3 134A menu item is a vector containing:
c7986c18
ER
135
136 - the name of the menu item (a string);
137 - the `callback' of that item;
67893ba3
KH
138 - a list of keywords with associated values:
139 - :active active-p a form specifying whether this item is selectable;
140 - :suffix suffix a string to be appended to the name as an `argument'
141 to the command, like `Kill Buffer NAME';
142 - :keys command-keys a string, suitable for `substitute-command-keys',
143 to specify the keyboard equivalent of a command
144 when the callback is a form (this is not necessary
145 when the callback is a symbol, as the keyboard
146 equivalent is computed automatically in that case);
147 - :style style a symbol: nil for a normal menu item, `toggle' for
148 a toggle button (a single option that can be turned
149 on or off), or `radio' for a radio button (one of a
150 group of mutually exclusive options);
151 - :selected form for `toggle' or `radio' style, a form that specifies
152 whether the button will be in the selected state.
153
154Alternately, the vector may contain exactly 3 or 4 elements, with the third
155element specifying `active-p' and the fourth specifying `suffix'.
c7986c18
ER
156
157If the `callback' of a menu item is a symbol, then it must name a command.
158It will be invoked with `call-interactively'. If it is a list, then it is
159evaluated with `eval'.
160
c7986c18
ER
161If an element of a menu is a string, then that string will be presented in
162the menu as unselectable text.
163
164If an element of a menu is a string consisting solely of hyphens, then that
165item will be presented as a solid horizontal line.
166
167If an element of a menu is a list, it is treated as a submenu. The name of
168that submenu (the first element in the list) will be used as the name of the
169item representing this menu on the parent.
170
171The syntax, more precisely:
172
173 form := <something to pass to `eval'>
174 command := <a symbol or string, to pass to `call-interactively'>
175 callback := command | form
176 active-p := <t or nil, whether this thing is selectable>
177 text := <string, non selectable>
178 name := <string>
67893ba3
KH
179 suffix := <string>
180 command-keys := <string>
181 object-style := 'nil' | 'toggle' | 'radio'
182 keyword := ':active' active-p
183 | ':suffix' suffix
184 | ':keys' command-keys
185 | ':style' object-style
186 | ':selected' form
187 menu-item := '[' name callback active-p [ suffix ] ']'
188 | '[' name callback [ keyword ]+ ']'
189 menu := '(' name [ menu-item | menu | text ]+ ')'"
c7986c18 190 (let ((menu (make-lucid-menu-keymap (car menu-desc) (cdr menu-desc)))
c15d229a 191 (pos (mouse-pixel-position))
f97eee51 192 answer cmd)
9f04f023
KH
193 (while (and menu
194 (setq answer (x-popup-menu (list (list (nth 1 pos)
195 (nthcdr 2 pos))
196 (car pos))
197 menu)))
c6dd34cb 198 (setq cmd (lookup-key menu (apply 'vector answer)))
d06752db
RS
199 (setq menu nil)
200 (and cmd
201 (if (keymapp cmd)
202 (setq menu cmd)
203 (call-interactively cmd))))))
73a644b4
RS
204
205(defun popup-dialog-box (data)
206 "Pop up a dialog box.
207A dialog box description is a list.
208
209 - The first element of the list is a string to display in the dialog box.
210 - The rest of the elements are descriptions of the dialog box's buttons.
211 Each one is a vector of three elements:
212 - The first element is the text of the button.
213 - The second element is the `callback'.
214 - The third element is t or nil, whether this button is selectable.
215
216If the `callback' of a button is a symbol, then it must name a command.
217It will be invoked with `call-interactively'. If it is a list, then it is
218evaluated with `eval'.
219
220One (and only one) of the buttons may be `nil'. This marker means that all
221following buttons should be flushright instead of flushleft.
222
223The syntax, more precisely:
224
225 form := <something to pass to `eval'>
226 command := <a symbol or string, to pass to `call-interactively'>
227 callback := command | form
228 active-p := <t, nil, or a form to evaluate to decide whether this
229 button should be selectable>
230 name := <string>
231 partition := 'nil'
232 button := '[' name callback active-p ']'
233 dialog := '(' name [ button ]+ [ partition [ button ]+ ] ')'"
234 (let ((name (car data))
235 (tail (cdr data))
236 converted
f1e20cc2 237 choice meaning)
73a644b4
RS
238 (while tail
239 (if (null (car tail))
240 (setq converted (cons nil converted))
241 (let ((item (aref (car tail) 0))
242 (callback (aref (car tail) 1))
243 (enable (aref (car tail) 2)))
244 (setq converted
245 (cons (if enable (cons item callback) item)
246 converted))))
247 (setq tail (cdr tail)))
248 (setq choice (x-popup-dialog t (cons name (nreverse converted))))
249 (setq meaning (assq choice converted))
250 (if meaning
251 (if (symbolp (cdr meaning))
252 (call-interactively (cdr meaning))
253 (eval (cdr meaning))))))
c7986c18 254\f
67893ba3 255;; This is empty because the usual elements of the menu bar
8b86c9eb
RS
256;; are provided by menu-bar.el instead.
257;; It would not make sense to duplicate them here.
258(defconst default-menubar nil)
c7986c18
ER
259
260(defun set-menubar (menubar)
261 "Set the default menubar to be menubar."
262 (setq-default current-menubar (copy-sequence menubar))
263 (set-menubar-dirty-flag))
264
265(defun set-buffer-menubar (menubar)
266 "Set the buffer-local menubar to be menubar."
267 (make-local-variable 'current-menubar)
268 (setq current-menubar (copy-sequence menubar))
269 (set-menubar-dirty-flag))
270
271\f
272;;; menu manipulation functions
273
274(defun find-menu-item (menubar item-path-list &optional parent)
275 "Searches MENUBAR for item given by ITEM-PATH-LIST.
276Returns (ITEM . PARENT), where PARENT is the immediate parent of
277 the item found.
278Signals an error if the item is not found."
279 (or parent (setq item-path-list (mapcar 'downcase item-path-list)))
280 (if (not (consp menubar))
281 nil
282 (let ((rest menubar)
283 result)
284 (while rest
285 (if (and (car rest)
286 (equal (car item-path-list)
287 (downcase (if (vectorp (car rest))
288 (aref (car rest) 0)
289 (if (stringp (car rest))
290 (car rest)
291 (car (car rest)))))))
292 (setq result (car rest) rest nil)
293 (setq rest (cdr rest))))
294 (if (cdr item-path-list)
295 (if (consp result)
296 (find-menu-item (cdr result) (cdr item-path-list) result)
297 (if result
298 (signal 'error (list "not a submenu" result))
299 (signal 'error (list "no such submenu" (car item-path-list)))))
300 (cons result parent)))))
301
302
303(defun disable-menu-item (path)
304 "Make the named menu item be unselectable.
67893ba3 305PATH is a list of strings which identify the position of the menu item in
c7986c18 306the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
67893ba3 307under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
c7986c18
ER
308menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
309 (let* ((menubar current-menubar)
310 (pair (find-menu-item menubar path))
311 (item (car pair))
312 (menu (cdr pair)))
313 (or item
314 (signal 'error (list (if menu "No such menu item" "No such menu")
315 path)))
316 (if (consp item) (error "can't disable menus, only menu items"))
317 (aset item 2 nil)
318 (set-menubar-dirty-flag)
319 item))
320
321
322(defun enable-menu-item (path)
323 "Make the named menu item be selectable.
67893ba3 324PATH is a list of strings which identify the position of the menu item in
c7986c18 325the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
67893ba3 326under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
c7986c18
ER
327menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
328 (let* ((menubar current-menubar)
329 (pair (find-menu-item menubar path))
330 (item (car pair))
331 (menu (cdr pair)))
332 (or item
333 (signal 'error (list (if menu "No such menu item" "No such menu")
334 path)))
335 (if (consp item) (error "%S is a menu, not a menu item" path))
336 (aset item 2 t)
337 (set-menubar-dirty-flag)
338 item))
339
340
341(defun add-menu-item-1 (item-p menu-path item-name item-data enabled-p before)
342 (if before (setq before (downcase before)))
343 (let* ((menubar current-menubar)
344 (menu (condition-case ()
345 (car (find-menu-item menubar menu-path))
346 (error nil)))
347 (item (if (listp menu)
348 (car (find-menu-item (cdr menu) (list item-name)))
349 (signal 'error (list "not a submenu" menu-path)))))
350 (or menu
351 (let ((rest menu-path)
352 (so-far menubar))
353 (while rest
354;;; (setq menu (car (find-menu-item (cdr so-far) (list (car rest)))))
355 (setq menu
356 (if (eq so-far menubar)
357 (car (find-menu-item so-far (list (car rest))))
358 (car (find-menu-item (cdr so-far) (list (car rest))))))
359 (or menu
360 (let ((rest2 so-far))
6f57a741
RS
361 (or rest2
362 (error "Trying to modify a menu that doesn't exist"))
c7986c18
ER
363 (while (and (cdr rest2) (car (cdr rest2)))
364 (setq rest2 (cdr rest2)))
365 (setcdr rest2
f664f700
RS
366 (nconc (list (setq menu (list (car rest))))
367 (cdr rest2)))))
c7986c18
ER
368 (setq so-far menu)
369 (setq rest (cdr rest)))))
370 (or menu (setq menu menubar))
371 (if item
372 nil ; it's already there
373 (if item-p
374 (setq item (vector item-name item-data enabled-p))
375 (setq item (cons item-name item-data)))
376 ;; if BEFORE is specified, try to add it there.
377 (if before
378 (setq before (car (find-menu-item menu (list before)))))
379 (let ((rest menu)
380 (added-before nil))
381 (while rest
382 (if (eq before (car (cdr rest)))
383 (progn
384 (setcdr rest (cons item (cdr rest)))
385 (setq rest nil added-before t))
386 (setq rest (cdr rest))))
387 (if (not added-before)
388 ;; adding before the first item on the menubar itself is harder
389 (if (and (eq menu menubar) (eq before (car menu)))
390 (setq menu (cons item menu)
391 current-menubar menu)
392 ;; otherwise, add the item to the end.
393 (nconc menu (list item))))))
394 (if item-p
395 (progn
396 (aset item 1 item-data)
397 (aset item 2 (not (null enabled-p))))
398 (setcar item item-name)
399 (setcdr item item-data))
400 (set-menubar-dirty-flag)
401 item))
402
403(defun add-menu-item (menu-path item-name function enabled-p &optional before)
404 "Add a menu item to some menu, creating the menu first if necessary.
405If the named item exists already, it is changed.
406MENU-PATH identifies the menu under which the new menu item should be inserted.
407 It is a list of strings; for example, (\"File\") names the top-level \"File\"
408 menu. (\"File\" \"Foo\") names a hypothetical submenu of \"File\".
409ITEM-NAME is the string naming the menu item to be added.
410FUNCTION is the command to invoke when this menu item is selected.
411 If it is a symbol, then it is invoked with `call-interactively', in the same
67893ba3 412 way that functions bound to keys are invoked. If it is a list, then the
c7986c18
ER
413 list is simply evaluated.
414ENABLED-P controls whether the item is selectable or not.
415BEFORE, if provided, is the name of a menu item before which this item should
416 be added, if this item is not on the menu already. If the item is already
417 present, it will not be moved."
418 (or menu-path (error "must specify a menu path"))
419 (or item-name (error "must specify an item name"))
420 (add-menu-item-1 t menu-path item-name function enabled-p before))
421
422
423(defun delete-menu-item (path)
424 "Remove the named menu item from the menu hierarchy.
67893ba3 425PATH is a list of strings which identify the position of the menu item in
c7986c18 426the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
67893ba3 427under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
c7986c18
ER
428menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
429 (let* ((menubar current-menubar)
430 (pair (find-menu-item menubar path))
431 (item (car pair))
432 (menu (or (cdr pair) menubar)))
433 (if (not item)
434 nil
435 ;; the menubar is the only special case, because other menus begin
436 ;; with their name.
437 (if (eq menu current-menubar)
438 (setq current-menubar (delq item menu))
439 (delq item menu))
440 (set-menubar-dirty-flag)
441 item)))
442
443
444(defun relabel-menu-item (path new-name)
445 "Change the string of the specified menu item.
67893ba3 446PATH is a list of strings which identify the position of the menu item in
c7986c18 447the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
67893ba3 448under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
c7986c18
ER
449menu item called \"Item\" under the \"Foo\" submenu of \"Menu\".
450NEW-NAME is the string that the menu item will be printed as from now on."
451 (or (stringp new-name)
452 (setq new-name (signal 'wrong-type-argument (list 'stringp new-name))))
453 (let* ((menubar current-menubar)
454 (pair (find-menu-item menubar path))
455 (item (car pair))
456 (menu (cdr pair)))
457 (or item
458 (signal 'error (list (if menu "No such menu item" "No such menu")
459 path)))
460 (if (and (consp item)
461 (stringp (car item)))
462 (setcar item new-name)
463 (aset item 0 new-name))
464 (set-menubar-dirty-flag)
465 item))
466
467(defun add-menu (menu-path menu-name menu-items &optional before)
468 "Add a menu to the menubar or one of its submenus.
469If the named menu exists already, it is changed.
470MENU-PATH identifies the menu under which the new menu should be inserted.
471 It is a list of strings; for example, (\"File\") names the top-level \"File\"
472 menu. (\"File\" \"Foo\") names a hypothetical submenu of \"File\".
473 If MENU-PATH is nil, then the menu will be added to the menubar itself.
474MENU-NAME is the string naming the menu to be added.
475MENU-ITEMS is a list of menu item descriptions.
476 Each menu item should be a vector of three elements:
477 - a string, the name of the menu item;
478 - a symbol naming a command, or a form to evaluate;
e24cb6c5 479 - and a form whose value determines whether this item is selectable.
c7986c18
ER
480BEFORE, if provided, is the name of a menu before which this menu should
481 be added, if this menu is not on its parent already. If the menu is already
482 present, it will not be moved."
483 (or menu-name (error "must specify a menu name"))
484 (or menu-items (error "must specify some menu items"))
485 (add-menu-item-1 nil menu-path menu-name menu-items t before))
486
487\f
488
489(defvar put-buffer-names-in-file-menu t)
490
c7986c18 491
b0413013
RS
492;; Don't unconditionally enable menu bars; leave that up to the user.
493;;(let ((frames (frame-list)))
494;; (while frames
495;; (modify-frame-parameters (car frames) '((menu-bar-lines . 1)))
496;; (setq frames (cdr frames))))
497;;(or (assq 'menu-bar-lines default-frame-alist)
498;; (setq default-frame-alist
499;; (cons '(menu-bar-lines . 1) default-frame-alist)))
c7986c18
ER
500
501(set-menubar default-menubar)
502\f
289c4836 503(provide 'lmenu)
c7986c18 504
aae56ea7 505;;; lmenu.el ends here