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