home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / a2.0bemacs-src.lha / Emacs-19.25 / lisp / lmenu.el < prev    next >
Encoding:
Text File  |  1994-05-29  |  16.2 KB  |  448 lines

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