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