]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/lmenu.el
(struct group): Add gr_gid member.
[gnu-emacs] / lisp / emacs-lisp / lmenu.el
1 ;;; lmenu.el --- emulate Lucid's menubar support
2
3 ;; Copyright (C) 1992, 1993, 1994, 1997, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Keywords: emulations obsolete
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 ;;; Code:
26
27 \f
28 ;; First, emulate the Lucid menubar support in GNU Emacs 19.
29
30 ;; Arrange to use current-menubar to set up part of the menu bar.
31
32 (defvar current-menubar)
33 (defvar lucid-menubar-map)
34 (defvar lucid-failing-menubar)
35
36 (defvar recompute-lucid-menubar 'recompute-lucid-menubar)
37 (defun recompute-lucid-menubar ()
38 (define-key lucid-menubar-map [menu-bar]
39 (condition-case nil
40 (make-lucid-menu-keymap "menu-bar" current-menubar)
41 (error (message "Invalid data in current-menubar moved to lucid-failing-menubar")
42 (sit-for 1)
43 (setq lucid-failing-menubar current-menubar
44 current-menubar nil))))
45 (setq lucid-menu-bar-dirty-flag nil))
46
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)))
52
53 ;; XEmacs compatibility
54 (defun set-menubar-dirty-flag ()
55 (force-mode-line-update)
56 (setq lucid-menu-bar-dirty-flag t))
57
58 (defvar add-menu-item-count 0)
59
60 ;; This is a variable whose value is always nil.
61 (defvar make-lucid-menu-keymap-disable nil)
62
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))
70 (while menu-items
71 (let ((item (car menu-items))
72 command name callback)
73 (cond ((stringp item)
74 (setq command nil)
75 (setq name (if (string-match "^-+$" item) "" item)))
76 ((consp item)
77 (setq command (make-lucid-menu-keymap (car item) (cdr item)))
78 (setq name (car item)))
79 ((vectorp item)
80 (setq command (make-symbol (format "menu-function-%d"
81 add-menu-item-count))
82 add-menu-item-count (1+ add-menu-item-count)
83 name (aref item 0)
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)
89 (let ((i 2))
90 (while (< i (length item))
91 (cond
92 ((eq (aref item i) ':active)
93 (put command 'menu-enable
94 (or (aref item (1+ i))
95 'make-lucid-menu-keymap-disable))
96 (setq i (+ 2 i)))
97 ((eq (aref item i) ':suffix)
98 ;; unimplemented
99 (setq i (+ 2 i)))
100 ((eq (aref item i) ':keys)
101 ;; unimplemented
102 (setq i (+ 2 i)))
103 ((eq (aref item i) ':style)
104 ;; unimplemented
105 (setq i (+ 2 i)))
106 ((eq (aref item i) ':selected)
107 ;; unimplemented
108 (setq i (+ 2 i)))
109 ((and (symbolp (aref item i))
110 (= ?: (string-to-char (symbol-name (aref item i)))))
111 (error "Unrecognized menu item keyword: %S"
112 (aref item i)))
113 ((= i 2)
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)))
119 (t
120 (error "Unexpected menu item value: %S"
121 (aref item i))))))))
122 (if (null command)
123 ;; Handle inactive strings specially--allow any number
124 ;; of identical ones.
125 (setcdr menu (cons (list nil name) (cdr menu)))
126 (if name
127 (define-key menu (vector (intern name)) (cons name command)))))
128 (setq menu-items (cdr menu-items)))
129 menu))
130
131 ;; XEmacs compatibility function
132 (defun popup-dialog-box (data)
133 "Pop up a dialog box.
134 A dialog box description is a list.
135
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.
142
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'.
146
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.
149
150 The syntax, more precisely:
151
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>
157 name := <string>
158 partition := 'nil'
159 button := '[' name callback active-p ']'
160 dialog := '(' name [ button ]+ [ partition [ button ]+ ] ')'"
161 (let ((name (car data))
162 (tail (cdr data))
163 converted
164 choice meaning)
165 (while tail
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)))
171 (setq converted
172 (cons (if enable (cons item callback) item)
173 converted))))
174 (setq tail (cdr tail)))
175 (setq choice (x-popup-dialog t (cons name (nreverse converted))))
176 (if choice
177 (if (symbolp choice)
178 (call-interactively choice)
179 (eval choice)))))
180 \f
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)
185
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))
191
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))
198
199 \f
200 ;;; menu manipulation functions
201
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
206 the item found.
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))
210 nil
211 (let ((rest menubar)
212 result)
213 (while rest
214 (if (and (car rest)
215 (equal (car item-path-list)
216 (downcase (if (vectorp (car rest))
217 (aref (car rest) 0)
218 (if (stringp (car rest))
219 (car rest)
220 (car (car rest)))))))
221 (setq result (car rest) rest nil)
222 (setq rest (cdr rest))))
223 (if (cdr item-path-list)
224 (if (consp result)
225 (find-menu-item (cdr result) (cdr item-path-list) result)
226 (if result
227 (signal 'error (list "not a submenu" result))
228 (signal 'error (list "no such submenu" (car item-path-list)))))
229 (cons result parent)))))
230
231
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))
241 (item (car pair))
242 (menu (cdr pair)))
243 (or item
244 (signal 'error (list (if menu "No such menu item" "No such menu")
245 path)))
246 (if (consp item) (error "can't disable menus, only menu items"))
247 (aset item 2 nil)
248 (set-menubar-dirty-flag)
249 item))
250
251
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))
261 (item (car pair))
262 (menu (cdr pair)))
263 (or item
264 (signal 'error (list (if menu "No such menu item" "No such menu")
265 path)))
266 (if (consp item) (error "%S is a menu, not a menu item" path))
267 (aset item 2 t)
268 (set-menubar-dirty-flag)
269 item))
270
271
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))
277 (error nil)))
278 (item (if (listp menu)
279 (car (find-menu-item (cdr menu) (list item-name)))
280 (signal 'error (list "not a submenu" menu-path)))))
281 (or menu
282 (let ((rest menu-path)
283 (so-far menubar))
284 (while rest
285 ;;; (setq menu (car (find-menu-item (cdr so-far) (list (car rest)))))
286 (setq menu
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))))))
290 (or menu
291 (let ((rest2 so-far))
292 (or rest2
293 (error "Trying to modify a menu that doesn't exist"))
294 (while (and (cdr rest2) (car (cdr rest2)))
295 (setq rest2 (cdr rest2)))
296 (setcdr rest2
297 (nconc (list (setq menu (list (car rest))))
298 (cdr rest2)))))
299 (setq so-far menu)
300 (setq rest (cdr rest)))))
301 (or menu (setq menu menubar))
302 (if item
303 nil ; it's already there
304 (if item-p
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.
308 (if before
309 (setq before (car (find-menu-item menu (list before)))))
310 (let ((rest menu)
311 (added-before nil))
312 (while rest
313 (if (eq before (car (cdr rest)))
314 (progn
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))))))
325 (if item-p
326 (progn
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)
332 item))
333
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))
353
354
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))
364 (item (car pair))
365 (menu (or (cdr pair) menubar)))
366 (if (not item)
367 nil
368 ;; the menubar is the only special case, because other menus begin
369 ;; with their name.
370 (if (eq menu current-menubar)
371 (setq current-menubar (delq item menu))
372 (delq item menu))
373 (set-menubar-dirty-flag)
374 item)))
375
376
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))
389 (item (car pair))
390 (menu (cdr pair)))
391 (or item
392 (signal 'error (list (if menu "No such menu item" "No such menu")
393 path)))
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)
399 item))
400
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))
421
422 \f
423
424 (defvar put-buffer-names-in-file-menu t)
425
426
427 ;; Don't unconditionally enable menu bars; leave that up to the user.
428 ;;(let ((frames (frame-list)))
429 ;; (while frames
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)))
435
436 (set-menubar default-menubar)
437 \f
438 (provide 'lmenu)
439
440 ;; arch-tag: 7051c396-2837-435a-ae11-b2d2e2af8fc1
441 ;;; lmenu.el ends here