1 ;;; lmenu.el --- emulate Lucid's menubar support
3 ;; Keywords: emulations
5 ;; Copyright (C) 1992, 1993 Free Software Foundation, Inc.
7 ;; This file is part of GNU Emacs.
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)
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.
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.
26 ;; First, emulate the Lucid menubar support in GNU Emacs 19.
28 ;; Arrange to use current-menubar to set up part of the menu bar.
30 (setq recompute-lucid-menubar 'recompute-lucid-menubar)
31 (defun recompute-lucid-menubar ()
32 (define-key lucid-menubar-map [menu-bar]
34 (make-lucid-menu-keymap "menu-bar" current-menubar)
35 (error (message "Invalid data in current-menubar moved to lucid-failing-menubar")
37 (setq lucid-failing-menubar current-menubar
38 current-menubar nil))))
39 (setq lucid-menu-bar-dirty-flag nil))
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)))
47 (defun set-menubar-dirty-flag ()
48 (force-mode-line-update)
49 (setq lucid-menu-bar-dirty-flag t))
51 (defvar add-menu-item-count 0)
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))
61 (let* ((item (car menu-items))
62 (callback (if (vectorp item) (aref item 1)))
68 (setq command (make-lucid-menu-keymap (car item) (cdr item)))
69 (setq name (car 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)))))
83 (define-key menu (vector (intern name)) (cons name command))))
84 (setq menu-items (cdr menu-items)))
87 (defun popup-menu (menu-desc)
88 "Pop up the given menu.
89 A menu is a list of menu items, strings, and submenus.
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
96 A menu item is a vector of three or four elements:
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.
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'.
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''.
110 If an element of a menu is a string, then that string will be presented in
111 the menu as unselectable text.
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.
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.
120 The syntax, more precisely:
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>
129 menu-item := '[' name callback active-p [ argument ] ']'
130 menu := '(' name [ menu-item | menu | text ]+ ')'
132 (let ((menu (make-lucid-menu-keymap (car menu-desc) (cdr menu-desc)))
133 (pos (mouse-position))
135 (setq answer (x-popup-menu (list (list (nth 1 pos) (nthcdr 2 pos))
138 (setq cmd (lookup-key menu (vector answer)))
139 (if cmd (call-interactively cmd))))
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)
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))
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))
158 ;;; menu manipulation functions
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
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))
172 (equal (car item-path-list)
173 (downcase (if (vectorp (car rest))
175 (if (stringp (car rest))
177 (car (car rest)))))))
178 (setq result (car rest) rest nil)
179 (setq rest (cdr rest))))
180 (if (cdr item-path-list)
182 (find-menu-item (cdr result) (cdr item-path-list) result)
184 (signal 'error (list "not a submenu" result))
185 (signal 'error (list "no such submenu" (car item-path-list)))))
186 (cons result parent)))))
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))
200 (signal 'error (list (if menu "No such menu item" "No such menu")
202 (if (consp item) (error "can't disable menus, only menu items"))
204 (set-menubar-dirty-flag)
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))
219 (signal 'error (list (if menu "No such menu item" "No such menu")
221 (if (consp item) (error "%S is a menu, not a menu item" path))
223 (set-menubar-dirty-flag)
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))
233 (item (if (listp menu)
234 (car (find-menu-item (cdr menu) (list item-name)))
235 (signal 'error (list "not a submenu" menu-path)))))
237 (let ((rest menu-path)
240 ;;; (setq menu (car (find-menu-item (cdr so-far) (list (car rest)))))
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))))))
246 (let ((rest2 so-far))
247 (while (and (cdr rest2) (car (cdr rest2)))
248 (setq rest2 (cdr rest2)))
250 (nconc (list (setq menu (list (car rest))))
253 (setq rest (cdr rest)))))
254 (or menu (setq menu menubar))
256 nil ; it's already there
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.
262 (setq before (car (find-menu-item menu (list before)))))
266 (if (eq before (car (cdr rest)))
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))))))
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)
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))
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))
316 (menu (or (cdr pair) menubar)))
319 ;; the menubar is the only special case, because other menus begin
321 (if (eq menu current-menubar)
322 (setq current-menubar (delq item menu))
324 (set-menubar-dirty-flag)
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))
342 (signal 'error (list (if menu "No such menu item" "No such menu")
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)
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))
373 (defvar put-buffer-names-in-file-menu t)
376 (let ((frames (frame-list)))
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)))
384 (set-menubar default-menubar)
388 ;;; lmenu.el ends here