home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / prim / menubar.el < prev    next >
Encoding:
Text File  |  1993-03-14  |  21.7 KB  |  574 lines

  1. ;; Menubar support.
  2. ;; Copyright (C) 1991-1993 Free Software Foundation, Inc.
  3.  
  4. ;; This file is part of GNU Emacs.
  5.  
  6. ;; GNU Emacs is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation; either version 2, or (at your option)
  9. ;; any later version.
  10.  
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ;; GNU General Public License for more details.
  15.  
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  18. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. (defconst default-menubar
  21.   '(("File"    ["New Screen"        x-new-screen        t]
  22.         ["Open File..."        find-file        t]
  23.         ["Save Buffer"        save-buffer        t  nil]
  24.         ["Save Buffer As..."    write-file        t]
  25.         ["Revert Buffer"    revert-buffer        t  nil]
  26.         "-----"
  27.         ["Print Buffer"        lpr-buffer        t  nil]
  28.         "-----"
  29.         ["Delete Screen"    delete-screen        t]
  30. ;;        ["Kill Buffer..."    kill-buffer        t]
  31.         ["Kill Buffer"        kill-this-buffer    t  nil]
  32.         ["Exit Emacs"        save-buffers-kill-emacs    t]
  33.         )
  34.     ("Edit"    ["Undo"            advertised-undo           t]
  35.         ["Cut"            x-kill-primary-selection   t]
  36.         ["Copy"            x-copy-primary-selection   t]
  37.         ["Paste"        x-yank-clipboard-selection t]
  38.         ["Clear"        x-delete-primary-selection t]
  39.         )
  40.     ("Buffers"    "")
  41.  
  42.     nil        ; the partition: menus after this are flushright
  43.  
  44.     ("Help"    ["Info"            info            t]
  45.         ["Describe Mode"    describe-mode        t]
  46.         ["Command Apropos..."    command-apropos        t]
  47.         ["List Keybindings"    describe-bindings    t]
  48.         ["Describe Key..."    describe-key        t]
  49.         ["Describe Function..."    describe-function    t]
  50.         ["Describe Variable..."    describe-variable    t]
  51.         "-----"
  52.         ["Unix Manual..."    manual-entry        t]
  53.         ["Emacs Tutorial"    help-with-tutorial    t]
  54.         ["Emacs News"        view-emacs-news        t]
  55.         )
  56.     ))
  57.  
  58.  
  59. (defun kill-this-buffer ()    ; for the menubar
  60.   "Kills the current buffer."
  61.   (interactive)
  62.   (kill-buffer (current-buffer)))
  63.  
  64. (defun x-new-screen (&optional screen-name)
  65.   "Creates a new emacs screen (that is, a new X window.)"
  66.   (interactive)
  67.   (select-screen (x-create-screen
  68.           (append (if screen-name
  69.                   (list (cons 'name screen-name))
  70.                 nil)
  71.               screen-default-alist)))
  72.   (switch-to-buffer (get-buffer-create "*scratch*"))
  73.   ;; hack: if evi mode is loaded and in use, put the new screen in evi mode.
  74.   (if (and (boundp 'evi-install-undo-list) evi-install-undo-list)
  75.       (evi-mode))
  76.   )
  77.  
  78.  
  79. (defun set-menubar (menubar)
  80.   "Set the default menubar to be menubar."
  81.   (setq-default current-menubar (copy-sequence menubar))
  82.   (set-menubar-dirty-flag))
  83.  
  84. (defun set-buffer-menubar (menubar)
  85.   "Set the buffer-local menubar to be menubar."
  86.   (make-local-variable 'current-menubar)
  87.   (setq current-menubar (copy-sequence menubar))
  88.   (set-menubar-dirty-flag))
  89.  
  90.  
  91. ;;; menu manipulation functions
  92.  
  93. (defun find-menu-item (menubar item-path-list &optional parent)
  94.   "Searches MENUBAR for item given by ITEM-PATH-LIST.
  95. Returns (ITEM . PARENT), where PARENT is the immediate parent of
  96.  the item found.
  97. Signals an error if the item is not found."
  98.   (or parent (setq item-path-list (mapcar 'downcase item-path-list)))
  99.   (if (not (consp menubar))
  100.       nil
  101.     (let ((rest menubar)
  102.       result)
  103.       (while rest
  104.     (if (and (car rest)
  105.          (equal (car item-path-list)
  106.             (downcase (if (vectorp (car rest))
  107.                       (aref (car rest) 0)
  108.                     (if (stringp (car rest))
  109.                     (car rest)
  110.                       (car (car rest)))))))
  111.         (setq result (car rest) rest nil)
  112.       (setq rest (cdr rest))))
  113.       (if (cdr item-path-list)
  114.       (if (consp result)
  115.           (find-menu-item (cdr result) (cdr item-path-list) result)
  116.         (if result
  117.         (signal 'error (list "not a submenu" result))
  118.           (signal 'error (list "no such submenu" (car item-path-list)))))
  119.     (cons result parent)))))
  120.  
  121.  
  122. (defun disable-menu-item (path)
  123.   "Make the named menu item be unselectable.
  124. PATH is a list of strings which identify the position of the menu item in 
  125. the menu hierarchy.  (\"File\" \"Save\") means the menu item called \"Save\"
  126. under the toplevel \"File\" menu.  (\"Menu\" \"Foo\" \"Item\") means the 
  127. menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
  128.   (let* ((menubar current-menubar)
  129.      (pair (find-menu-item menubar path))
  130.      (item (car pair))
  131.      (menu (cdr pair)))
  132.     (or item
  133.     (signal 'error (list (if menu "No such menu item" "No such menu")
  134.                  path)))
  135.     (if (consp item) (error "can't disable menus, only menu items"))
  136.     (aset item 2 nil)
  137.     (set-menubar-dirty-flag)
  138.     item))
  139.  
  140.  
  141. (defun enable-menu-item (path)
  142.   "Make the named menu item be selectable.
  143. PATH is a list of strings which identify the position of the menu item in 
  144. the menu hierarchy.  (\"File\" \"Save\") means the menu item called \"Save\"
  145. under the toplevel \"File\" menu.  (\"Menu\" \"Foo\" \"Item\") means the 
  146. menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
  147.   (let* ((menubar current-menubar)
  148.      (pair (find-menu-item menubar path))
  149.      (item (car pair))
  150.      (menu (cdr pair)))
  151.     (or item
  152.     (signal 'error (list (if menu "No such menu item" "No such menu")
  153.                  path)))
  154.     (if (consp item) (error "%S is a menu, not a menu item" path))
  155.     (aset item 2 t)
  156.     (set-menubar-dirty-flag)
  157.     item))
  158.  
  159.  
  160. (defun add-menu-item-1 (item-p menu-path item-name item-data enabled-p before)
  161.   (if before (setq before (downcase before)))
  162.   (let* ((menubar current-menubar)
  163.      (menu (condition-case ()
  164.            (car (find-menu-item menubar menu-path))
  165.          (error nil)))
  166.      (item (if (listp menu)
  167.            (car (find-menu-item (cdr menu) (list item-name)))
  168.          (signal 'error (list "not a submenu" menu-path)))))
  169.     (or menu
  170.     (let ((rest menu-path)
  171.           (so-far menubar))
  172.       (while rest
  173. ;;;        (setq menu (car (find-menu-item (cdr so-far) (list (car rest)))))
  174.         (setq menu
  175.           (if (eq so-far menubar)
  176.               (car (find-menu-item so-far (list (car rest))))
  177.             (car (find-menu-item (cdr so-far) (list (car rest))))))
  178.         (or menu
  179.         (let ((rest2 so-far))
  180.           (while (and (cdr rest2) (car (cdr rest2)))
  181.             (setq rest2 (cdr rest2)))
  182.           (setcdr rest2
  183.           (nconc (list (setq menu (list (car rest))))
  184.              (cdr rest2)))))
  185.         (setq so-far menu)
  186.         (setq rest (cdr rest)))))
  187.     (or menu (setq menu menubar))
  188.     (if item
  189.     nil    ; it's already there
  190.       (if item-p
  191.       (setq item (vector item-name item-data enabled-p))
  192.     (setq item (cons item-name item-data)))
  193.       ;; if BEFORE is specified, try to add it there.
  194.       (if before
  195.       (setq before (car (find-menu-item menu (list before)))))
  196.       (let ((rest menu)
  197.         (added-before nil))
  198.     (while rest
  199.       (if (eq before (car (cdr rest)))
  200.           (progn
  201.         (setcdr rest (cons item (cdr rest)))
  202.         (setq rest nil added-before t))
  203.         (setq rest (cdr rest))))
  204.     (if (not added-before)
  205.         ;; adding before the first item on the menubar itself is harder
  206.         (if (and (eq menu menubar) (eq before (car menu)))
  207.         (setq menu (cons item menu)
  208.               current-menubar menu)
  209.           ;; otherwise, add the item to the end.
  210.           (nconc menu (list item))))))
  211.     (if item-p
  212.     (progn
  213.       (aset item 1 item-data)
  214.       (aset item 2 (not (null enabled-p))))
  215.       (setcar item item-name)
  216.       (setcdr item item-data))
  217.     (set-menubar-dirty-flag)
  218.     item))
  219.  
  220. (defun add-menu-item (menu-path item-name function enabled-p &optional before)
  221.   "Add a menu item to some menu, creating the menu first if necessary.
  222. If the named item exists already, it is changed.
  223. MENU-PATH identifies the menu under which the new menu item should be inserted.
  224.  It is a list of strings; for example, (\"File\") names the top-level \"File\"
  225.  menu.  (\"File\" \"Foo\") names a hypothetical submenu of \"File\".
  226. ITEM-NAME is the string naming the menu item to be added.
  227. FUNCTION is the command to invoke when this menu item is selected.
  228.  If it is a symbol, then it is invoked with `call-interactively', in the same
  229.  way that functions bound to keys are invoked.  If it is a list, then the 
  230.  list is simply evaluated.
  231. ENABLED-P controls whether the item is selectable or not.
  232. BEFORE, if provided, is the name of a menu item before which this item should
  233.  be added, if this item is not on the menu already.  If the item is already
  234.  present, it will not be moved."
  235.   (or menu-path (error "must specify a menu path"))
  236.   (or item-name (error "must specify an item name"))
  237.   (add-menu-item-1 t menu-path item-name function enabled-p before))
  238.  
  239.  
  240. (defun delete-menu-item (path)
  241.   "Remove the named menu item from the menu hierarchy.
  242. PATH is a list of strings which identify the position of the menu item in 
  243. the menu hierarchy.  (\"File\" \"Save\") means the menu item called \"Save\"
  244. under the toplevel \"File\" menu.  (\"Menu\" \"Foo\" \"Item\") means the 
  245. menu item called \"Item\" under the \"Foo\" submenu of \"Menu\"."
  246.   (let* ((menubar current-menubar)
  247.      (pair (find-menu-item menubar path))
  248.      (item (car pair))
  249.      (menu (or (cdr pair) menubar)))
  250.     (if (not item)
  251.     nil
  252.       ;; the menubar is the only special case, because other menus begin
  253.       ;; with their name.
  254.       (if (eq menu current-menubar)
  255.       (setq current-menubar (delq item menu))
  256.     (delq item menu))
  257.       (set-menubar-dirty-flag)
  258.       item)))
  259.  
  260.  
  261. (defun relabel-menu-item (path new-name)
  262.   "Change the string of the specified menu item.
  263. PATH is a list of strings which identify the position of the menu item in 
  264. the menu hierarchy.  (\"File\" \"Save\") means the menu item called \"Save\"
  265. under the toplevel \"File\" menu.  (\"Menu\" \"Foo\" \"Item\") means the 
  266. menu item called \"Item\" under the \"Foo\" submenu of \"Menu\".
  267. NEW-NAME is the string that the menu item will be printed as from now on."
  268.   (or (stringp new-name)
  269.       (setq new-name (signal 'wrong-type-argument (list 'stringp new-name))))
  270.   (let* ((menubar current-menubar)
  271.      (pair (find-menu-item menubar path))
  272.      (item (car pair))
  273.      (menu (cdr pair)))
  274.     (or item
  275.     (signal 'error (list (if menu "No such menu item" "No such menu")
  276.                  path)))
  277.     (if (and (consp item)
  278.          (stringp (car item)))
  279.     (setcar item new-name)
  280.       (aset item 0 new-name))
  281.     (set-menubar-dirty-flag)
  282.     item))
  283.  
  284. (defun add-menu (menu-path menu-name menu-items &optional before)
  285.   "Add a menu to the menubar or one of its submenus.
  286. If the named menu exists already, it is changed.
  287. MENU-PATH identifies the menu under which the new menu should be inserted.
  288.  It is a list of strings; for example, (\"File\") names the top-level \"File\"
  289.  menu.  (\"File\" \"Foo\") names a hypothetical submenu of \"File\".
  290.  If MENU-PATH is nil, then the menu will be added to the menubar itself.
  291. MENU-NAME is the string naming the menu to be added.
  292. MENU-ITEMS is a list of menu item descriptions.
  293.  Each menu item should be a vector of three elements:
  294.    - a string, the name of the menu item;
  295.    - a symbol naming a command, or a form to evaluate;
  296.    - and t or nil, whether this item is selectable.
  297. BEFORE, if provided, is the name of a menu before which this menu should
  298.  be added, if this menu is not on its parent already.  If the menu is already
  299.  present, it will not be moved."
  300.   (or menu-name (error "must specify a menu name"))
  301.   (or menu-items (error "must specify some menu items"))
  302.   (add-menu-item-1 nil menu-path menu-name menu-items t before))
  303.  
  304.  
  305.  
  306. (defvar put-buffer-names-in-file-menu t)
  307.  
  308. (defun sensitize-file-and-edit-menus-hook ()
  309.   "For use as a value of activate-menubar-hook.
  310. This function changes the sensitivity of these File and Edit menu items:
  311.  
  312.   Cut    sensitive only when emacs owns the primary X Selection.
  313.   Copy   sensitive only when emacs owns the primary X Selection.
  314.   Clear  sensitive only when emacs owns the primary X Selection.
  315.   Paste  sensitive only when there is an owner for the X Clipboard Selection.
  316.   Undo   sensitive only when there is undo information.
  317.          While in the midst of an undo, this is changed to \"Undo More\".
  318.  
  319.   Kill Buffer    has the name of the current buffer appended to it.
  320.   Print Buffer   has the name of the current buffer appended to it.
  321.   Save Buffer    has the name of the current buffer appended to it, and is
  322.                  sensitive only when the current buffer is modified.
  323.   Revert Buffer  has the name of the current buffer appended to it, and is
  324.                  sensitive only when the current buffer has a file.
  325.   Delete Screen  sensitive only when there is more than one visible screen."
  326.   ;;
  327.   ;; the hair in here to not update the menubar unless something has changed
  328.   ;; isn't really necessary (the menubar code is fast enough) but it makes
  329.   ;; me feel better (and creates marginally less list garbage.)
  330.   (let* ((file-menu (cdr (car (find-menu-item current-menubar '("File")))))
  331.      (edit-menu (cdr (car (find-menu-item current-menubar '("Edit")))))
  332.      (save    (car (find-menu-item file-menu '("Save Buffer"))))
  333.      (rvt   (car (find-menu-item file-menu '("Revert Buffer"))))
  334.      (del   (car (find-menu-item file-menu '("Delete Screen"))))
  335.      (print (car (find-menu-item file-menu '("Print Buffer"))))
  336.      (kill  (car (find-menu-item file-menu '("Kill Buffer"))))
  337.      (cut   (car (find-menu-item edit-menu '("Cut"))))
  338.      (copy  (car (find-menu-item edit-menu '("Copy"))))
  339.      (paste (car (find-menu-item edit-menu '("Paste"))))
  340.      (clear (car (find-menu-item edit-menu '("Clear"))))
  341.      (undo  (or (car (find-menu-item edit-menu '("Undo")))
  342.             (car (find-menu-item edit-menu '("Undo More")))))
  343.      (name (buffer-name))
  344.      (emacs-owns-selection-p (x-selection-owner-p))
  345.      (clipboard-exists-p (x-selection-exists-p 'CLIPBOARD))
  346.      undo-available undoing-more
  347.      (undo-info-available (not (null (and (not (eq t buffer-undo-list))
  348.                    (if (eq last-command 'undo)
  349.                        (setq undoing-more
  350.                          (and (boundp 'pending-undo-list)
  351.                         pending-undo-list)
  352.                      buffer-undo-list))))))
  353.      undo-name undo-state
  354.      (change-p
  355.       (or (and cut   (not (eq emacs-owns-selection-p (aref cut 2))))
  356.           (and copy  (not (eq emacs-owns-selection-p (aref copy 2))))
  357.           (and clear (not (eq emacs-owns-selection-p (aref clear 2))))
  358.           (and paste (not (eq clipboard-exists-p (aref paste 2))))
  359.           (and save  (not (eq (buffer-modified-p) (aref save 2))))
  360.           (and rvt   (not (eq (not (not buffer-file-name)) (aref rvt 2))))
  361.           (and del   (eq (eq (next-screen nil nil t) (selected-screen))
  362.                  (aref del 2)))
  363.           )))
  364.     (if (not put-buffer-names-in-file-menu)
  365.     nil
  366.       (if (= (length save)  4) (progn (aset save  3 name) (setq change-p t)))
  367.       (if (= (length rvt)   4) (progn (aset rvt   3 name) (setq change-p t)))
  368.       (if (= (length print) 4) (progn (aset print 3 name) (setq change-p t)))
  369.       (if (= (length kill)  4) (progn (aset kill  3 name) (setq change-p t))))
  370.     (if save  (aset save  2 (buffer-modified-p)))
  371.     (if rvt   (aset rvt   2 (not (not buffer-file-name))))
  372.     (if del   (aset del   2 (not (eq (next-screen () () t) (selected-screen)))))
  373.     (if cut   (aset cut   2 emacs-owns-selection-p))
  374.     (if copy  (aset copy  2 emacs-owns-selection-p))
  375.     (if clear (aset clear 2 emacs-owns-selection-p))
  376.     (if paste (aset paste 2 clipboard-exists-p))
  377.  
  378.     ;; we could also do this with the third field of the item.
  379.     (if (eq last-command 'undo)
  380.     (setq undo-name "Undo More"
  381.           undo-state (not (null (and (boundp 'pending-undo-list)
  382.                      pending-undo-list))))
  383.       (setq undo-name "Undo"
  384.         undo-state (and (not (eq buffer-undo-list t))
  385.                 (not (null
  386.                   (or buffer-undo-list
  387.                       (and (boundp 'pending-undo-list)
  388.                        pending-undo-list)))))))
  389.     (if buffer-read-only (setq undo-state nil))
  390.     (if (and undo
  391.          (or (not (equal undo-name (aref undo 0)))
  392.          (not (eq undo-state (aref undo 2)))))
  393.     (progn (aset undo 0 undo-name)
  394.            (aset undo 2 undo-state)
  395.            (setq change-p t)))
  396.     ;; if we made any changes, return nil
  397.     ;; otherwise return t to indicate that we haven't done anything.
  398.     (not change-p)))
  399.  
  400. ;; this version is too slow
  401. (defun format-buffers-menu-line (buffer)
  402.   "Returns a string to represent the given buffer in the Buffer menu.
  403. nil means the buffer shouldn't be listed.  You can redefine this."
  404.   (if (string-match "\\` " (buffer-name buffer))
  405.       nil
  406.     (save-excursion
  407.      (set-buffer buffer)
  408.      (let ((size (buffer-size)))
  409.        (format "%s%s %-19s %6s %-15s %s"
  410.            (if (buffer-modified-p) "*" " ")
  411.            (if buffer-read-only "%" " ")
  412.            (buffer-name)
  413.            size
  414.            mode-name
  415.            (or (buffer-file-name) ""))))))
  416.        
  417. (defun format-buffers-menu-line (buffer)
  418.   (if (string-match "\\` " (setq buffer (buffer-name buffer)))
  419.       nil
  420.     buffer))
  421.  
  422. (defvar buffers-menu-max-size 10
  423.   "*Maximum number of entries which may appear on the \"Buffers\" menu.
  424. If this is 10, then only the ten most-recently-selected buffers will be
  425. shown.  If this is nil, then all buffers will be shown.  Setting this to
  426. a large number or nil will slow down menu responsiveness.")
  427.  
  428. (defvar complex-buffers-menu-p nil
  429.   "*If true, the buffers menu will contain several commands, as submenus
  430. of each buffer line.  If this is false, then there will be only one command:
  431. select that buffer.")
  432.  
  433. (defvar buffers-menu-switch-to-buffer-function 'switch-to-buffer
  434.   "*The function to call to select a buffer from the buffers menu.
  435. `switch-to-buffer' is a good choice, as is `pop-to-buffer'.")
  436.  
  437.  
  438. (defun buffer-menu-save-buffer (buffer)
  439.   (save-excursion
  440.     (set-buffer buffer)
  441.     (save-buffer)))
  442.  
  443. (defun buffer-menu-write-file (buffer)
  444.   (save-excursion
  445.     (set-buffer buffer)
  446.     (write-file (read-file-name
  447.          (concat "Write " (buffer-name (current-buffer))
  448.              " to file: ")))))
  449.  
  450.  
  451. (defsubst build-buffers-menu-internal (buffers)
  452.   (let (name line)
  453.     (mapcar
  454.      (if complex-buffers-menu-p
  455.      (function
  456.       (lambda (buffer)
  457.         (if (setq line (format-buffers-menu-line buffer))
  458.         (list line
  459.               (vector "Switch to Buffer"
  460.                   (list buffers-menu-switch-to-buffer-function
  461.                     (setq name (buffer-name buffer)))
  462.                   t)
  463.               (if (and (buffer-modified-p buffer)
  464.                    (buffer-file-name buffer))
  465.               (vector "Save Buffer"
  466.                   (list 'buffer-menu-save-buffer name) t)
  467.             ["Save Buffer" nil nil])
  468.               (vector "Save Buffer As..."
  469.                   (list 'buffer-menu-write-file name) t)
  470.               (vector "Kill Buffer" (list 'kill-buffer name) t)))))
  471.        (function (lambda (buffer)
  472.            (if (setq line (format-buffers-menu-line buffer))
  473.                (vector line
  474.                    (list buffers-menu-switch-to-buffer-function
  475.                      (buffer-name buffer))
  476.                    t)))))
  477.      buffers)))
  478.  
  479. (defun build-buffers-menu-hook ()
  480.   "For use as a value of activate-menubar-hook.
  481. This function changes the contents of the \"Buffers\" menu to correspond
  482. to the current set of buffers.  Only the most-recently-used few buffers
  483. will be listed on the menu, for efficiency reasons.  You can control how
  484. many buffers will be shown by setting `buffers-menu-max-size'.
  485. You can control the text of the menu items by redefining the function
  486. `format-buffers-menu-line'."
  487.   (let ((buffer-menu (car (find-menu-item current-menubar '("Buffers"))))
  488.     name
  489.     buffers)
  490.     (if (not buffer-menu)
  491.     nil
  492.       (setq buffers (buffer-list))
  493.  
  494.       (if (and (integerp buffers-menu-max-size)
  495.            (> buffers-menu-max-size 1))
  496.       (if (> (length buffers) buffers-menu-max-size)
  497.           (setcdr (nthcdr buffers-menu-max-size buffers) nil)))
  498.  
  499.       (setq buffers (build-buffers-menu-internal buffers))
  500.       (setq buffers (nconc (delq nil buffers)
  501.                '("----" ["List All Buffers" list-buffers t])))
  502.       ;; slightly (only slightly) more efficient to not install the menubar
  503.       ;; if it hasn't visibly changed.
  504.       (if (equal buffers (cdr buffer-menu))
  505.       t  ; return t meaning "no change"
  506.     (setcdr buffer-menu buffers)
  507.     nil))))
  508.  
  509. (add-hook 'activate-menubar-hook 'build-buffers-menu-hook)
  510. (add-hook 'activate-menubar-hook 'sensitize-file-and-edit-menus-hook)
  511.  
  512. (set-menubar default-menubar)
  513.  
  514.  
  515.  
  516. (defun yes-or-no-p-dialog-box (prompt)
  517.   "Ask user a \"y or n\" question with a popup dialog box.
  518. Returns t if answer is \"yes\".
  519. Takes one argument, which is the string to display to ask the question."
  520.   (let ((event (allocate-event))
  521.     (echo-keystrokes 0))     
  522.     (popup-dialog-box
  523.      (cons prompt '(["Yes" yes t] ["No" no t] nil ["Abort" abort t])))
  524.     (catch 'ynp-done
  525.       (while t
  526.     (next-command-event event)
  527.     (cond ((and (menu-event-p event) (eq (event-object event) 'yes))
  528.            (throw 'ynp-done t))
  529.           ((and (menu-event-p event) (eq (event-object event) 'no))
  530.            (throw 'ynp-done nil))
  531.           ((and (menu-event-p event)
  532.             (or (eq (event-object event) 'abort)
  533.             (eq (event-object event) 'menu-no-selection-hook)))
  534.            (signal 'quit nil))
  535.           ((button-release-event-p event) ;; don't beep twice
  536.            nil)
  537.           (t
  538.            (beep)
  539.            (message "please answer the dialog box")))))))
  540.  
  541. (defun yes-or-no-p-maybe-dialog-box (prompt)
  542.   "Ask user a yes-or-no question.  Return t if answer is yes.
  543. The question is asked with a dialog box or the minibuffer, as appropriate.
  544. Takes one argument, which is the string to display to ask the question.
  545. It should end in a space; `yes-or-no-p' adds `(yes or no) ' to it.
  546. The user must confirm the answer with RET,
  547. and can edit it until it as been confirmed."
  548.   (if (or (button-press-event-p last-command-event)
  549.       (button-release-event-p last-command-event)
  550.       (menu-event-p last-command-event))
  551.       (yes-or-no-p-dialog-box prompt)
  552.     (yes-or-no-p-minibuf prompt)))
  553.  
  554. (defun y-or-n-p-maybe-dialog-box (prompt)
  555.   "Ask user a \"y or n\" question.  Return t if answer is \"y\".
  556. Takes one argument, which is the string to display to ask the question.
  557. The question is asked with a dialog box or the minibuffer, as appropriate.
  558. It should end in a space; `y-or-n-p' adds `(y or n) ' to it.
  559. No confirmation of the answer is requested; a single character is enough.
  560. Also accepts Space to mean yes, or Delete to mean no."
  561.   (if (or (button-press-event-p last-command-event)
  562.       (button-release-event-p last-command-event)
  563.       (menu-event-p last-command-event))
  564.       (yes-or-no-p-dialog-box prompt)
  565.     (y-or-n-p-minibuf prompt)))
  566.  
  567. (if (fboundp 'popup-dialog-box)
  568.     (progn
  569.       (fset 'yes-or-no-p 'yes-or-no-p-maybe-dialog-box)
  570.       (fset 'y-or-n-p 'y-or-n-p-maybe-dialog-box)))
  571.  
  572.  
  573. (provide 'menubar)
  574.