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 (defconst default-menubar
142 '(("File" ["New Frame" x-new-frame t]
143 ["Open File..." find-file t]
144 ["Save Buffer" save-buffer t nil]
145 ["Save Buffer As..." write-file t]
146 ["Revert Buffer" revert-buffer t nil]
148 ["Print Buffer" lpr-buffer t nil]
150 ["Delete Frame" delete-frame t]
151 ;; ["Kill Buffer..." kill-buffer t]
152 ["Kill Buffer" kill-this-buffer t nil]
153 ["Exit Emacs" save-buffers-kill-emacs t]
155 ("Edit" ["Undo" advertised-undo t]
156 ["Cut" x-kill-primary-selection t]
157 ["Copy" x-copy-primary-selection t]
158 ["Paste" x-yank-clipboard-selection t]
159 ["Clear" x-delete-primary-selection t]
163 nil ; the partition: menus after this are flushright
165 ("Help" ["Info" info t]
166 ["Describe Mode" describe-mode t]
167 ["Command Apropos..." command-apropos t]
168 ["List Keybindings" describe-bindings t]
169 ["Describe Key..." describe-key t]
170 ["Describe Function..." describe-function t]
171 ["Describe Variable..." describe-variable t]
173 ["Man..." manual-entry t]
174 ["Emacs Tutorial" help-with-tutorial t]
175 ["Emacs News" view-emacs-news t]
180 (defun kill-this-buffer () ; for the menubar
181 "Kills the current buffer."
183 (kill-buffer (current-buffer)))
185 (defun x-new-frame (&optional frame-name)
186 "Creates a new Emacs frame (that is, a new X window.)"
188 (select-frame (x-create-frame
189 (append (if frame-name
190 (list (cons 'name frame-name))
192 frame-default-alist)))
193 (switch-to-buffer (get-buffer-create "*scratch*"))
196 (defun set-menubar (menubar)
197 "Set the default menubar to be menubar."
198 (setq-default current-menubar (copy-sequence menubar))
199 (set-menubar-dirty-flag))
201 (defun set-buffer-menubar (menubar)
202 "Set the buffer-local menubar to be menubar."
203 (make-local-variable 'current-menubar)
204 (setq current-menubar (copy-sequence menubar))
205 (set-menubar-dirty-flag))
208 ;;; menu manipulation functions
210 (defun find-menu-item (menubar item-path-list &optional parent)
211 "Searches MENUBAR for item given by ITEM-PATH-LIST.
212 Returns (ITEM . PARENT), where PARENT is the immediate parent of
214 Signals an error if the item is not found."
215 (or parent (setq item-path-list (mapcar 'downcase item-path-list)))
216 (if (not (consp menubar))
222 (equal (car item-path-list)
223 (downcase (if (vectorp (car rest))
225 (if (stringp (car rest))
227 (car (car rest)))))))
228 (setq result (car rest) rest nil)
229 (setq rest (cdr rest))))
230 (if (cdr item-path-list)
232 (find-menu-item (cdr result) (cdr item-path-list) result)
234 (signal 'error (list "not a submenu" result))
235 (signal 'error (list "no such submenu" (car item-path-list)))))
236 (cons result parent)))))
239 (defun disable-menu-item (path)
240 "Make the named menu item be unselectable.
241 PATH is a list of strings which identify the position of the menu item in
242 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
243 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
244 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
245 (let* ((menubar current-menubar)
246 (pair (find-menu-item menubar path))
250 (signal 'error (list (if menu "No such menu item" "No such menu")
252 (if (consp item) (error "can't disable menus, only menu items"))
254 (set-menubar-dirty-flag)
258 (defun enable-menu-item (path)
259 "Make the named menu item be selectable.
260 PATH is a list of strings which identify the position of the menu item in
261 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
262 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
263 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
264 (let* ((menubar current-menubar)
265 (pair (find-menu-item menubar path))
269 (signal 'error (list (if menu "No such menu item" "No such menu")
271 (if (consp item) (error "%S is a menu, not a menu item" path))
273 (set-menubar-dirty-flag)
277 (defun add-menu-item-1 (item-p menu-path item-name item-data enabled-p before)
278 (if before (setq before (downcase before)))
279 (let* ((menubar current-menubar)
280 (menu (condition-case ()
281 (car (find-menu-item menubar menu-path))
283 (item (if (listp menu)
284 (car (find-menu-item (cdr menu) (list item-name)))
285 (signal 'error (list "not a submenu" menu-path)))))
287 (let ((rest menu-path)
290 ;;; (setq menu (car (find-menu-item (cdr so-far) (list (car rest)))))
292 (if (eq so-far menubar)
293 (car (find-menu-item so-far (list (car rest))))
294 (car (find-menu-item (cdr so-far) (list (car rest))))))
296 (let ((rest2 so-far))
297 (while (and (cdr rest2) (car (cdr rest2)))
298 (setq rest2 (cdr rest2)))
300 (nconc (list (setq menu (list (car rest))))
303 (setq rest (cdr rest)))))
304 (or menu (setq menu menubar))
306 nil ; it's already there
308 (setq item (vector item-name item-data enabled-p))
309 (setq item (cons item-name item-data)))
310 ;; if BEFORE is specified, try to add it there.
312 (setq before (car (find-menu-item menu (list before)))))
316 (if (eq before (car (cdr rest)))
318 (setcdr rest (cons item (cdr rest)))
319 (setq rest nil added-before t))
320 (setq rest (cdr rest))))
321 (if (not added-before)
322 ;; adding before the first item on the menubar itself is harder
323 (if (and (eq menu menubar) (eq before (car menu)))
324 (setq menu (cons item menu)
325 current-menubar menu)
326 ;; otherwise, add the item to the end.
327 (nconc menu (list item))))))
330 (aset item 1 item-data)
331 (aset item 2 (not (null enabled-p))))
332 (setcar item item-name)
333 (setcdr item item-data))
334 (set-menubar-dirty-flag)
337 (defun add-menu-item (menu-path item-name function enabled-p &optional before)
338 "Add a menu item to some menu, creating the menu first if necessary.
339 If the named item exists already, it is changed.
340 MENU-PATH identifies the menu under which the new menu item should be inserted.
341 It is a list of strings; for example, (\"File\") names the top-level \"File\"
342 menu. (\"File\" \"Foo\") names a hypothetical submenu of \"File\".
343 ITEM-NAME is the string naming the menu item to be added.
344 FUNCTION is the command to invoke when this menu item is selected.
345 If it is a symbol, then it is invoked with `call-interactively', in the same
346 way that functions bound to keys are invoked. If it is a list, then the
347 list is simply evaluated.
348 ENABLED-P controls whether the item is selectable or not.
349 BEFORE, if provided, is the name of a menu item before which this item should
350 be added, if this item is not on the menu already. If the item is already
351 present, it will not be moved."
352 (or menu-path (error "must specify a menu path"))
353 (or item-name (error "must specify an item name"))
354 (add-menu-item-1 t menu-path item-name function enabled-p before))
357 (defun delete-menu-item (path)
358 "Remove the named menu item from the menu hierarchy.
359 PATH is a list of strings which identify the position of the menu item in
360 the menu hierarchy. (\"File\" \"Save\") means the menu item called \"Save\"
361 under the toplevel \"File\" menu. (\"Menu\" \"Foo\" \"Item\") means the
362 menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
363 (let* ((menubar current-menubar)
364 (pair (find-menu-item menubar path))
366 (menu (or (cdr pair) menubar)))
369 ;; the menubar is the only special case, because other menus begin
371 (if (eq menu current-menubar)
372 (setq current-menubar (delq item menu))
374 (set-menubar-dirty-flag)
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))
392 (signal 'error (list (if menu "No such menu item" "No such menu")
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)
401 (defun add-menu (menu-path menu-name menu-items &optional before)
402 "Add a menu to the menubar or one of its submenus.
403 If the named menu exists already, it is changed.
404 MENU-PATH identifies the menu under which the new menu should be inserted.
405 It is a list of strings; for example, (\"File\") names the top-level \"File\"
406 menu. (\"File\" \"Foo\") names a hypothetical submenu of \"File\".
407 If MENU-PATH is nil, then the menu will be added to the menubar itself.
408 MENU-NAME is the string naming the menu to be added.
409 MENU-ITEMS is a list of menu item descriptions.
410 Each menu item should be a vector of three elements:
411 - a string, the name of the menu item;
412 - a symbol naming a command, or a form to evaluate;
413 - and t or nil, whether this item is selectable.
414 BEFORE, if provided, is the name of a menu before which this menu should
415 be added, if this menu is not on its parent already. If the menu is already
416 present, it will not be moved."
417 (or menu-name (error "must specify a menu name"))
418 (or menu-items (error "must specify some menu items"))
419 (add-menu-item-1 nil menu-path menu-name menu-items t before))
423 (defvar put-buffer-names-in-file-menu t)
425 (defun sensitize-file-and-edit-menus-hook ()
426 "For use as a value of activate-menubar-hook.
427 This function changes the sensitivity of these File and Edit menu items:
429 Cut sensitive only when emacs owns the primary X Selection.
430 Copy sensitive only when emacs owns the primary X Selection.
431 Clear sensitive only when emacs owns the primary X Selection.
432 Paste sensitive only when there is an owner for the X Clipboard Selection.
433 Undo sensitive only when there is undo information.
434 While in the midst of an undo, this is changed to \"Undo More\".
436 Kill Buffer has the name of the current buffer appended to it.
437 Print Buffer has the name of the current buffer appended to it.
438 Save Buffer has the name of the current buffer appended to it, and is
439 sensitive only when the current buffer is modified.
440 Revert Buffer has the name of the current buffer appended to it, and is
441 sensitive only when the current buffer has a file.
442 Delete Frame sensitive only when there is more than one visible frame."
444 ;; the hair in here to not update the menubar unless something has changed
445 ;; isn't really necessary (the menubar code is fast enough) but it makes
446 ;; me feel better (and creates marginally less list garbage.)
447 (let* ((file-menu (cdr (car (find-menu-item current-menubar '("File")))))
448 (edit-menu (cdr (car (find-menu-item current-menubar '("Edit")))))
449 (save (car (find-menu-item file-menu '("Save Buffer"))))
450 (rvt (car (find-menu-item file-menu '("Revert Buffer"))))
451 (del (car (find-menu-item file-menu '("Delete Frame"))))
452 (print (car (find-menu-item file-menu '("Print Buffer"))))
453 (kill (car (find-menu-item file-menu '("Kill Buffer"))))
454 (cut (car (find-menu-item edit-menu '("Cut"))))
455 (copy (car (find-menu-item edit-menu '("Copy"))))
456 (paste (car (find-menu-item edit-menu '("Paste"))))
457 (clear (car (find-menu-item edit-menu '("Clear"))))
458 (undo (or (car (find-menu-item edit-menu '("Undo")))
459 (car (find-menu-item edit-menu '("Undo More")))))
461 (emacs-owns-selection-p (x-selection-owner-p))
462 (clipboard-exists-p (x-selection-exists-p 'CLIPBOARD))
463 undo-available undoing-more
464 (undo-info-available (not (null (and (not (eq t buffer-undo-list))
465 (if (eq last-command 'undo)
467 (and (boundp 'pending-undo-list)
469 buffer-undo-list))))))
472 (or (and cut (not (eq emacs-owns-selection-p (aref cut 2))))
473 (and copy (not (eq emacs-owns-selection-p (aref copy 2))))
474 (and clear (not (eq emacs-owns-selection-p (aref clear 2))))
475 (and paste (not (eq clipboard-exists-p (aref paste 2))))
476 (and save (not (eq (buffer-modified-p) (aref save 2))))
477 (and rvt (not (eq (not (not buffer-file-name)) (aref rvt 2))))
478 (and del (not (eq (null (cdr (visible-frame-list))) (aref del 2))))
480 (if (not put-buffer-names-in-file-menu)
482 (if (= (length save) 4) (progn (aset save 3 name) (setq change-p t)))
483 (if (= (length rvt) 4) (progn (aset rvt 3 name) (setq change-p t)))
484 (if (= (length print) 4) (progn (aset print 3 name) (setq change-p t)))
485 (if (= (length kill) 4) (progn (aset kill 3 name) (setq change-p t))))
486 (if save (aset save 2 (buffer-modified-p)))
487 (if rvt (aset rvt 2 (not (not buffer-file-name))))
488 (if del (aset del 2 (null (cdr (visible-frame-list)))))
489 (if cut (aset cut 2 emacs-owns-selection-p))
490 (if copy (aset copy 2 emacs-owns-selection-p))
491 (if clear (aset clear 2 emacs-owns-selection-p))
492 (if paste (aset paste 2 clipboard-exists-p))
494 ;; we could also do this with the third field of the item.
495 (if (eq last-command 'undo)
496 (setq undo-name "Undo More"
497 undo-state (not (null (and (boundp 'pending-undo-list)
498 pending-undo-list))))
499 (setq undo-name "Undo"
500 undo-state (and (not (eq buffer-undo-list t))
503 (and (boundp 'pending-undo-list)
504 pending-undo-list)))))))
505 (if buffer-read-only (setq undo-state nil))
507 (or (not (equal undo-name (aref undo 0)))
508 (not (eq undo-state (aref undo 2)))))
509 (progn (aset undo 0 undo-name)
510 (aset undo 2 undo-state)
512 ;; if we made any changes, return nil
513 ;; otherwise return t to indicate that we haven't done anything.
516 ;; this version is too slow
517 (defun format-buffers-menu-line (buffer)
518 "Returns a string to represent the given buffer in the Buffer menu.
519 nil means the buffer shouldn't be listed. You can redefine this."
520 (if (string-match "\\` " (buffer-name buffer))
524 (let ((size (buffer-size)))
525 (format "%s%s %-19s %6s %-15s %s"
526 (if (buffer-modified-p) "*" " ")
527 (if buffer-read-only "%" " ")
531 (or (buffer-file-name) ""))))))
533 (defun format-buffers-menu-line (buffer)
534 (if (string-match "\\` " (setq buffer (buffer-name buffer)))
538 (defvar buffers-menu-max-size 10
539 "*Maximum number of entries which may appear on the \"Buffers\" menu.
540 If this is 10, then only the ten most-recently-selected buffers will be
541 shown. If this is nil, then all buffers will be shown. Setting this to
542 a large number or nil will slow down menu responsiveness.")
544 (defvar complex-buffers-menu-p nil
545 "*If true, the buffers menu will contain several commands, as submenus
546 of each buffer line. If this is false, then there will be only one command:
547 select that buffer.")
549 (defvar buffers-menu-switch-to-buffer-function 'switch-to-buffer
550 "*The function to call to select a buffer from the buffers menu.
551 `switch-to-buffer' is a good choice, as is `pop-to-buffer'.")
554 (defun buffer-menu-save-buffer (buffer)
559 (defun buffer-menu-write-file (buffer)
562 (write-file (read-file-name
563 (concat "Write " (buffer-name (current-buffer))
567 (defsubst build-buffers-menu-internal (buffers)
570 (if complex-buffers-menu-p
573 (if (setq line (format-buffers-menu-line buffer))
575 (vector "Switch to Buffer"
576 (list buffers-menu-switch-to-buffer-function
577 (setq name (buffer-name buffer)))
579 (if (and (buffer-modified-p buffer)
580 (buffer-file-name buffer))
581 (vector "Save Buffer"
582 (list 'buffer-menu-save-buffer name) t)
583 ["Save Buffer" nil nil])
584 (vector "Save Buffer As..."
585 (list 'buffer-menu-write-file name) t)
586 (vector "Kill Buffer" (list 'kill-buffer name) t)))))
587 (function (lambda (buffer)
588 (if (setq line (format-buffers-menu-line buffer))
590 (list buffers-menu-switch-to-buffer-function
591 (buffer-name buffer))
595 (defun build-buffers-menu-hook ()
596 "For use as a value of activate-menubar-hook.
597 This function changes the contents of the \"Buffers\" menu to correspond
598 to the current set of buffers. Only the most-recently-used few buffers
599 will be listed on the menu, for efficiency reasons. You can control how
600 many buffers will be shown by setting `buffers-menu-max-size'.
601 You can control the text of the menu items by redefining the function
602 `format-buffers-menu-line'."
603 (let ((buffer-menu (car (find-menu-item current-menubar '("Buffers"))))
606 (if (not buffer-menu)
608 (setq buffers (buffer-list))
610 (if (and (integerp buffers-menu-max-size)
611 (> buffers-menu-max-size 1))
612 (if (> (length buffers) buffers-menu-max-size)
613 (setcdr (nthcdr buffers-menu-max-size buffers) nil)))
615 (setq buffers (build-buffers-menu-internal buffers))
616 (setq buffers (nconc (delq nil buffers)
617 '("----" ["List All Buffers" list-buffers t])))
618 ;; slightly (only slightly) more efficient to not install the menubar
619 ;; if it hasn't visibly changed.
620 (if (equal buffers (cdr buffer-menu))
621 t ; return t meaning "no change"
622 (setcdr buffer-menu buffers)
623 (set-menubar-dirty-flag)
626 (add-hook 'activate-menubar-hook 'build-buffers-menu-hook)
627 (add-hook 'activate-menubar-hook 'sensitize-file-and-edit-menus-hook)
629 (let ((frames (frame-list)))
631 (modify-frame-parameters (car frames) '((menu-bar-lines . 1)))
632 (setq frames (cdr frames))))
633 (or (assq 'menu-bar-lines default-frame-alist)
634 (setq default-frame-alist
635 (cons '(menu-bar-lines . 1) default-frame-alist)))
637 (set-menubar default-menubar)
641 ;;; lmenu.el ends here