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