]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/lmenu.el
(event-modifiers): Function deleted.
[gnu-emacs] / lisp / emacs-lisp / lmenu.el
1 ;;; lmenu.el --- emulate Lucid's menubar support
2
3 ;; Keywords: emulations
4
5 ;; Copyright (C) 1992, 1993 Free Software Foundation, Inc.
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
23 ;;; Code:
24
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
30 (setq recompute-lucid-menubar 'recompute-lucid-menubar)
31 (defun recompute-lucid-menubar ()
32 (define-key lucid-menubar-map [menu-bar]
33 (condition-case nil
34 (make-lucid-menu-keymap "menu-bar" current-menubar)
35 (error (message "Invalid data in current-menubar moved to lucid-failing-menubar")
36 (sit-for 1)
37 (setq lucid-failing-menubar current-menubar
38 current-menubar nil))))
39 (setq lucid-menu-bar-dirty-flag nil))
40
41 (defvar lucid-menubar-map (make-sparse-keymap))
42 (or (assq 'current-menubar minor-mode-map-alist)
43 (setq minor-mode-map-alist
44 (cons (cons 'current-menubar lucid-menubar-map)
45 minor-mode-map-alist)))
46
47 (defun set-menubar-dirty-flag ()
48 (force-mode-line-update)
49 (setq lucid-menu-bar-dirty-flag t))
50
51 (defvar add-menu-item-count 0)
52
53 ;; Return a menu keymap corresponding to a Lucid-style menu list
54 ;; MENU-ITEMS, and with name MENU-NAME.
55 (defun make-lucid-menu-keymap (menu-name menu-items)
56 (let ((menu (make-sparse-keymap menu-name)))
57 ;; Process items in reverse order,
58 ;; since the define-key loop reverses them again.
59 (setq menu-items (reverse menu-items))
60 (while menu-items
61 (let* ((item (car menu-items))
62 (callback (if (vectorp item) (aref item 1)))
63 command enabler name)
64 (cond ((stringp item)
65 (setq command nil)
66 (setq name item))
67 ((consp item)
68 (setq command (make-lucid-menu-keymap (car item) (cdr item)))
69 (setq name (car item)))
70 ((vectorp item)
71 (setq command (make-symbol (format "menu-function-%d"
72 add-menu-item-count)))
73 (setq enabler (make-symbol (format "menu-function-%d-enabler"
74 add-menu-item-count)))
75 (setq add-menu-item-count (1+ add-menu-item-count))
76 (put command 'menu-enable enabler)
77 (set enabler (aref item 2))
78 (setq name (aref item 0))
79 (if (symbolp callback)
80 (fset command callback)
81 (fset command (list 'lambda () '(interactive) callback)))))
82 (if name
83 (define-key menu (vector (intern name)) (cons name command))))
84 (setq menu-items (cdr menu-items)))
85 menu))
86
87 (defun popup-menu (menu-desc)
88 "Pop up the given menu.
89 A menu is a list of menu items, strings, and submenus.
90
91 The first element of a menu must be a string, which is the name of the
92 menu. This is the string that will be displayed in the parent menu, if
93 any. For toplevel menus, it is ignored. This string is not displayed
94 in the menu itself.
95
96 A menu item is a vector of three or four elements:
97
98 - the name of the menu item (a string);
99 - the `callback' of that item;
100 - whether this item is active (selectable);
101 - and an optional string to append to the name.
102
103 If the `callback' of a menu item is a symbol, then it must name a command.
104 It will be invoked with `call-interactively'. If it is a list, then it is
105 evaluated with `eval'.
106
107 The fourth element of a menu item is a convenient way of adding the name
108 of a command's ``argument'' to the menu, like ``Kill Buffer NAME''.
109
110 If an element of a menu is a string, then that string will be presented in
111 the menu as unselectable text.
112
113 If an element of a menu is a string consisting solely of hyphens, then that
114 item will be presented as a solid horizontal line.
115
116 If an element of a menu is a list, it is treated as a submenu. The name of
117 that submenu (the first element in the list) will be used as the name of the
118 item representing this menu on the parent.
119
120 The syntax, more precisely:
121
122 form := <something to pass to `eval'>
123 command := <a symbol or string, to pass to `call-interactively'>
124 callback := command | form
125 active-p := <t or nil, whether this thing is selectable>
126 text := <string, non selectable>
127 name := <string>
128 argument := <string>
129 menu-item := '[' name callback active-p [ argument ] ']'
130 menu := '(' name [ menu-item | menu | text ]+ ')'
131 "
132 (let ((menu (make-lucid-menu-keymap (car menu-desc) (cdr menu-desc)))
133 (pos (mouse-position))
134 answer)
135 (setq answer (x-popup-menu (list (list (nth 1 pos) (nthcdr 2 pos))
136 (car pos))
137 menu))
138 (setq cmd (lookup-key menu (vector answer)))
139 (if cmd (call-interactively cmd))))
140 \f
141 ;; This is empty because the usual elements of the menu bar
142 ;; are provided by menu-bar.el instead.
143 ;; It would not make sense to duplicate them here.
144 (defconst default-menubar nil)
145
146 (defun set-menubar (menubar)
147 "Set the default menubar to be menubar."
148 (setq-default current-menubar (copy-sequence menubar))
149 (set-menubar-dirty-flag))
150
151 (defun set-buffer-menubar (menubar)
152 "Set the buffer-local menubar to be menubar."
153 (make-local-variable 'current-menubar)
154 (setq current-menubar (copy-sequence menubar))
155 (set-menubar-dirty-flag))
156
157 \f
158 ;;; menu manipulation functions
159
160 (defun find-menu-item (menubar item-path-list &optional parent)
161 "Searches MENUBAR for item given by ITEM-PATH-LIST.
162 Returns (ITEM . PARENT), where PARENT is the immediate parent of
163 the item found.
164 Signals an error if the item is not found."
165 (or parent (setq item-path-list (mapcar 'downcase item-path-list)))
166 (if (not (consp menubar))
167 nil
168 (let ((rest menubar)
169 result)
170 (while rest
171 (if (and (car rest)
172 (equal (car item-path-list)
173 (downcase (if (vectorp (car rest))
174 (aref (car rest) 0)
175 (if (stringp (car rest))
176 (car rest)
177 (car (car rest)))))))
178 (setq result (car rest) rest nil)
179 (setq rest (cdr rest))))
180 (if (cdr item-path-list)
181 (if (consp result)
182 (find-menu-item (cdr result) (cdr item-path-list) result)
183 (if result
184 (signal 'error (list "not a submenu" result))
185 (signal 'error (list "no such submenu" (car item-path-list)))))
186 (cons result parent)))))
187
188
189 (defun disable-menu-item (path)
190 "Make the named menu item be unselectable.
191 PATH is a list of strings which identify the position of the menu item in
192 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
193 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
194 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
195 (let* ((menubar current-menubar)
196 (pair (find-menu-item menubar path))
197 (item (car pair))
198 (menu (cdr pair)))
199 (or item
200 (signal 'error (list (if menu "No such menu item" "No such menu")
201 path)))
202 (if (consp item) (error "can't disable menus, only menu items"))
203 (aset item 2 nil)
204 (set-menubar-dirty-flag)
205 item))
206
207
208 (defun enable-menu-item (path)
209 "Make the named menu item be selectable.
210 PATH is a list of strings which identify the position of the menu item in
211 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
212 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
213 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
214 (let* ((menubar current-menubar)
215 (pair (find-menu-item menubar path))
216 (item (car pair))
217 (menu (cdr pair)))
218 (or item
219 (signal 'error (list (if menu "No such menu item" "No such menu")
220 path)))
221 (if (consp item) (error "%S is a menu, not a menu item" path))
222 (aset item 2 t)
223 (set-menubar-dirty-flag)
224 item))
225
226
227 (defun add-menu-item-1 (item-p menu-path item-name item-data enabled-p before)
228 (if before (setq before (downcase before)))
229 (let* ((menubar current-menubar)
230 (menu (condition-case ()
231 (car (find-menu-item menubar menu-path))
232 (error nil)))
233 (item (if (listp menu)
234 (car (find-menu-item (cdr menu) (list item-name)))
235 (signal 'error (list "not a submenu" menu-path)))))
236 (or menu
237 (let ((rest menu-path)
238 (so-far menubar))
239 (while rest
240 ;;; (setq menu (car (find-menu-item (cdr so-far) (list (car rest)))))
241 (setq menu
242 (if (eq so-far menubar)
243 (car (find-menu-item so-far (list (car rest))))
244 (car (find-menu-item (cdr so-far) (list (car rest))))))
245 (or menu
246 (let ((rest2 so-far))
247 (while (and (cdr rest2) (car (cdr rest2)))
248 (setq rest2 (cdr rest2)))
249 (setcdr rest2
250 (nconc (list (setq menu (list (car rest))))
251 (cdr rest2)))))
252 (setq so-far menu)
253 (setq rest (cdr rest)))))
254 (or menu (setq menu menubar))
255 (if item
256 nil ; it's already there
257 (if item-p
258 (setq item (vector item-name item-data enabled-p))
259 (setq item (cons item-name item-data)))
260 ;; if BEFORE is specified, try to add it there.
261 (if before
262 (setq before (car (find-menu-item menu (list before)))))
263 (let ((rest menu)
264 (added-before nil))
265 (while rest
266 (if (eq before (car (cdr rest)))
267 (progn
268 (setcdr rest (cons item (cdr rest)))
269 (setq rest nil added-before t))
270 (setq rest (cdr rest))))
271 (if (not added-before)
272 ;; adding before the first item on the menubar itself is harder
273 (if (and (eq menu menubar) (eq before (car menu)))
274 (setq menu (cons item menu)
275 current-menubar menu)
276 ;; otherwise, add the item to the end.
277 (nconc menu (list item))))))
278 (if item-p
279 (progn
280 (aset item 1 item-data)
281 (aset item 2 (not (null enabled-p))))
282 (setcar item item-name)
283 (setcdr item item-data))
284 (set-menubar-dirty-flag)
285 item))
286
287 (defun add-menu-item (menu-path item-name function enabled-p &optional before)
288 "Add a menu item to some menu, creating the menu first if necessary.
289 If the named item exists already, it is changed.
290 MENU-PATH identifies the menu under which the new menu item should be inserted.
291 It is a list of strings; for example, (\"File\") names the top-level \"File\"
292 menu. (\"File\" \"Foo\") names a hypothetical submenu of \"File\".
293 ITEM-NAME is the string naming the menu item to be added.
294 FUNCTION is the command to invoke when this menu item is selected.
295 If it is a symbol, then it is invoked with `call-interactively', in the same
296 way that functions bound to keys are invoked. If it is a list, then the
297 list is simply evaluated.
298 ENABLED-P controls whether the item is selectable or not.
299 BEFORE, if provided, is the name of a menu item before which this item should
300 be added, if this item is not on the menu already. If the item is already
301 present, it will not be moved."
302 (or menu-path (error "must specify a menu path"))
303 (or item-name (error "must specify an item name"))
304 (add-menu-item-1 t menu-path item-name function enabled-p before))
305
306
307 (defun delete-menu-item (path)
308 "Remove the named menu item from the menu hierarchy.
309 PATH is a list of strings which identify the position of the menu item in
310 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
311 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
312 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
313 (let* ((menubar current-menubar)
314 (pair (find-menu-item menubar path))
315 (item (car pair))
316 (menu (or (cdr pair) menubar)))
317 (if (not item)
318 nil
319 ;; the menubar is the only special case, because other menus begin
320 ;; with their name.
321 (if (eq menu current-menubar)
322 (setq current-menubar (delq item menu))
323 (delq item menu))
324 (set-menubar-dirty-flag)
325 item)))
326
327
328 (defun relabel-menu-item (path new-name)
329 "Change the string of the specified menu item.
330 PATH is a list of strings which identify the position of the menu item in
331 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
332 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
333 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\".
334 NEW-NAME is the string that the menu item will be printed as from now on."
335 (or (stringp new-name)
336 (setq new-name (signal 'wrong-type-argument (list 'stringp new-name))))
337 (let* ((menubar current-menubar)
338 (pair (find-menu-item menubar path))
339 (item (car pair))
340 (menu (cdr pair)))
341 (or item
342 (signal 'error (list (if menu "No such menu item" "No such menu")
343 path)))
344 (if (and (consp item)
345 (stringp (car item)))
346 (setcar item new-name)
347 (aset item 0 new-name))
348 (set-menubar-dirty-flag)
349 item))
350
351 (defun add-menu (menu-path menu-name menu-items &optional before)
352 "Add a menu to the menubar or one of its submenus.
353 If the named menu exists already, it is changed.
354 MENU-PATH identifies the menu under which the new menu should be inserted.
355 It is a list of strings; for example, (\"File\") names the top-level \"File\"
356 menu. (\"File\" \"Foo\") names a hypothetical submenu of \"File\".
357 If MENU-PATH is nil, then the menu will be added to the menubar itself.
358 MENU-NAME is the string naming the menu to be added.
359 MENU-ITEMS is a list of menu item descriptions.
360 Each menu item should be a vector of three elements:
361 - a string, the name of the menu item;
362 - a symbol naming a command, or a form to evaluate;
363 - and t or nil, whether this item is selectable.
364 BEFORE, if provided, is the name of a menu before which this menu should
365 be added, if this menu is not on its parent already. If the menu is already
366 present, it will not be moved."
367 (or menu-name (error "must specify a menu name"))
368 (or menu-items (error "must specify some menu items"))
369 (add-menu-item-1 nil menu-path menu-name menu-items t before))
370
371 \f
372
373 (defvar put-buffer-names-in-file-menu t)
374
375
376 (let ((frames (frame-list)))
377 (while frames
378 (modify-frame-parameters (car frames) '((menu-bar-lines . 1)))
379 (setq frames (cdr frames))))
380 (or (assq 'menu-bar-lines default-frame-alist)
381 (setq default-frame-alist
382 (cons '(menu-bar-lines . 1) default-frame-alist)))
383
384 (set-menubar default-menubar)
385 \f
386 (provide 'menubar)
387
388 ;;; lmenu.el ends here