home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2 / Openstep-4.2-Intel-User.iso / usr / lib / emacs / lisp / x-menu.el < prev    next >
Lisp/Scheme  |  1991-01-09  |  5KB  |  146 lines

  1. ;; Copyright (C) 1986 Free Software Foundation, Inc.
  2.  
  3. ;; This file is part of GNU Emacs.
  4.  
  5. ;; GNU Emacs is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation; either version 1, or (at your option)
  8. ;; any later version.
  9.  
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ;; GNU General Public License for more details.
  14.  
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  17. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.  
  20. (defmacro caar (conscell)
  21.   (list 'car (list 'car conscell)))
  22.  
  23. (defmacro cdar (conscell)
  24.   (list 'cdr (list 'car conscell)))
  25.  
  26. (defun x-menu-mode ()
  27.   "Major mode for creating permanent menus for use with X.
  28. These menus are implemented entirely in Lisp; popup menus, implemented
  29. with x-popup-menu, are implemented using XMenu primitives."
  30.   (make-local-variable 'x-menu-items-per-line)
  31.   (make-local-variable 'x-menu-item-width)
  32.   (make-local-variable 'x-menu-items-alist)
  33.   (make-local-variable 'x-process-mouse-hook)
  34.   (make-local-variable 'x-menu-assoc-buffer)
  35.   (setq buffer-read-only t)
  36.   (setq truncate-lines t)
  37.   (setq x-process-mouse-hook 'x-menu-pick-entry)
  38.   (setq mode-line-buffer-identification '("MENU: %32b")))
  39.  
  40. (defvar x-menu-max-width 0)
  41. (defvar x-menu-items-per-line 0)
  42. (defvar x-menu-item-width 0)
  43. (defvar x-menu-items-alist nil)
  44. (defvar x-menu-assoc-buffer nil)
  45.  
  46. (defvar x-menu-item-spacing 1
  47.   "*Minimum horizontal spacing between objects in a permanent X menu.")
  48.  
  49. (defun x-menu-create-menu (name)
  50.   "Create a permanent X menu.  Returns an item which should be used as a
  51. menu object whenever referring to the menu."
  52.   (let ((old (current-buffer))
  53.     (buf (get-buffer-create name)))
  54.     (set-buffer buf)
  55.     (x-menu-mode)
  56.     (setq x-menu-assoc-buffer old)
  57.     (set-buffer old)
  58.     buf))
  59.  
  60. (defun x-menu-change-associated-buffer (menu buffer)
  61.   "Change associated buffer of MENU to BUFFER.  BUFFER should be a buffer
  62. object."
  63.   (let ((old (current-buffer)))
  64.     (set-buffer menu)
  65.     (setq x-menu-assoc-buffer buffer)
  66.     (set-buffer old)))
  67.  
  68. (defun x-menu-add-item (menu item binding)
  69.   "Adds to MENU an item with name ITEM, associated with BINDING.
  70. Following a sequence of calls to x-menu-add-item, a call to x-menu-compute
  71. should be performed before the menu will be made available to the user.
  72.  
  73. BINDING should be a function of one argument, which is the numerical
  74. button/key code as defined in x-menu.el."
  75.   (let ((old (current-buffer))
  76.     elt)
  77.     (set-buffer menu)
  78.     (if (setq elt (assoc item x-menu-items-alist))
  79.     (rplacd elt binding)
  80.       (setq x-menu-items-alist (append x-menu-items-alist
  81.                        (list (cons item binding)))))
  82.     (set-buffer old)
  83.     item))
  84.  
  85. (defun x-menu-delete-item (menu item)
  86.   "Deletes from MENU the item named ITEM.  x-menu-compute should be called
  87. before the menu is made available to the user."
  88.   (let ((old (current-buffer))
  89.     elt)
  90.     (set-buffer menu)
  91.     (if (setq elt (assoc item x-menu-items-alist))
  92.     (rplaca elt nil))
  93.     (set-buffer old)
  94.     item))
  95.  
  96. (defun x-menu-activate (menu)
  97.   "Computes all necessary parameters for MENU.  This must be called whenever
  98. a menu is modified before it is made available to the user.
  99.  
  100. This also creates the menu itself."
  101.   (let ((buf (current-buffer)))
  102.     (pop-to-buffer menu)
  103.     (let (buffer-read-only)
  104.       (setq x-menu-max-width (1- (screen-width)))
  105.       (setq x-menu-item-width 0)
  106.       (let (items-head
  107.         (items-tail x-menu-items-alist))
  108.     (while items-tail
  109.       (if (caar items-tail)
  110.           (progn (setq items-head (cons (car items-tail) items-head))
  111.              (setq x-menu-item-width
  112.                (max x-menu-item-width
  113.                 (length (caar items-tail))))))
  114.       (setq items-tail (cdr items-tail)))
  115.     (setq x-menu-items-alist (reverse items-head)))
  116.       (setq x-menu-item-width (+ x-menu-item-spacing x-menu-item-width))
  117.       (setq x-menu-items-per-line
  118.         (max 1 (/ x-menu-max-width x-menu-item-width)))
  119.       (erase-buffer)
  120.       (let ((items-head x-menu-items-alist))
  121.     (while items-head
  122.       (let ((items 0))
  123.         (while (and items-head
  124.             (<= (setq items (1+ items)) x-menu-items-per-line))
  125.           (insert (format (concat "%"
  126.                       (int-to-string x-menu-item-width) "s")
  127.                   (caar items-head)))
  128.           (setq items-head (cdr items-head))))
  129.       (insert ?\n)))
  130.       (shrink-window (max 0
  131.               (- (window-height)
  132.                  (1+ (count-lines (point-min) (point-max))))))
  133.       (goto-char (point-min)))
  134.     (pop-to-buffer buf)))
  135.  
  136. (defun x-menu-pick-entry (position event)
  137.   "Internal function for dispatching on mouse/menu events"
  138.   (let*    ((x (min (1- x-menu-items-per-line)
  139.          (/ (current-column) x-menu-item-width)))
  140.      (y (- (count-lines (point-min) (point))
  141.            (if (zerop (current-column)) 0 1)))
  142.      (item (+ x (* y x-menu-items-per-line)))
  143.      (litem (cdr (nth item x-menu-items-alist))))
  144.     (and litem (funcall litem event)))
  145.   (pop-to-buffer x-menu-assoc-buffer))
  146.