annotate lisp/x-menu.el @ 32489:be6e5b11d5a7

*** empty log message ***
author Stefan Monnier <monnier@iro.umontreal.ca>
date Sun, 15 Oct 2000 05:18:48 +0000
parents 11218164bc54
children 67b464da13ec
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
656
d74e65773062 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 35
diff changeset
1 ;;; x-menu.el --- menu support for X
d74e65773062 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 35
diff changeset
2
35
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
3 ;; Copyright (C) 1986 Free Software Foundation, Inc.
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
4
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
5 ;; This file is part of GNU Emacs.
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
6
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
8 ;; it under the terms of the GNU General Public License as published by
806
d42e1151eed8 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 773
diff changeset
9 ;; the Free Software Foundation; either version 2, or (at your option)
35
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
10 ;; any later version.
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
11
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
12 ;; GNU Emacs is distributed in the hope that it will be useful,
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
15 ;; GNU General Public License for more details.
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
16
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
17 ;; You should have received a copy of the GNU General Public License
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 12950
diff changeset
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 12950
diff changeset
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 12950
diff changeset
20 ;; Boston, MA 02111-1307, USA.
35
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
21
773
9c89fd7ddd41 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 656
diff changeset
22 ;;; Code:
35
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
23
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
24 (defun x-menu-mode ()
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
25 "Major mode for creating permanent menus for use with X.
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
26 These menus are implemented entirely in Lisp; popup menus, implemented
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
27 with x-popup-menu, are implemented using XMenu primitives."
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
28 (make-local-variable 'x-menu-items-per-line)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
29 (make-local-variable 'x-menu-item-width)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
30 (make-local-variable 'x-menu-items-alist)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
31 (make-local-variable 'x-process-mouse-hook)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
32 (make-local-variable 'x-menu-assoc-buffer)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
33 (setq buffer-read-only t)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
34 (setq truncate-lines t)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
35 (setq x-process-mouse-hook 'x-menu-pick-entry)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
36 (setq mode-line-buffer-identification '("MENU: %32b")))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
37
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
38 (defvar x-menu-max-width 0)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
39 (defvar x-menu-items-per-line 0)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
40 (defvar x-menu-item-width 0)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
41 (defvar x-menu-items-alist nil)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
42 (defvar x-menu-assoc-buffer nil)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
43
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
44 (defvar x-menu-item-spacing 1
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
45 "*Minimum horizontal spacing between objects in a permanent X menu.")
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
46
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
47 (defun x-menu-create-menu (name)
12950
83fea4c633e8 Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 841
diff changeset
48 "Create a permanent X menu.
83fea4c633e8 Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 841
diff changeset
49 Returns an item which should be used as a
35
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
50 menu object whenever referring to the menu."
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
51 (let ((old (current-buffer))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
52 (buf (get-buffer-create name)))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
53 (set-buffer buf)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
54 (x-menu-mode)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
55 (setq x-menu-assoc-buffer old)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
56 (set-buffer old)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
57 buf))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
58
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
59 (defun x-menu-change-associated-buffer (menu buffer)
12950
83fea4c633e8 Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 841
diff changeset
60 "Change associated buffer of MENU to BUFFER.
83fea4c633e8 Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 841
diff changeset
61 BUFFER should be a buffer object."
35
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
62 (let ((old (current-buffer)))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
63 (set-buffer menu)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
64 (setq x-menu-assoc-buffer buffer)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
65 (set-buffer old)))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
66
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
67 (defun x-menu-add-item (menu item binding)
12950
83fea4c633e8 Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 841
diff changeset
68 "Add to MENU an item with name ITEM, associated with BINDING.
35
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
69 Following a sequence of calls to x-menu-add-item, a call to x-menu-compute
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
70 should be performed before the menu will be made available to the user.
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
71
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
72 BINDING should be a function of one argument, which is the numerical
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
73 button/key code as defined in x-menu.el."
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
74 (let ((old (current-buffer))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
75 elt)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
76 (set-buffer menu)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
77 (if (setq elt (assoc item x-menu-items-alist))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
78 (rplacd elt binding)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
79 (setq x-menu-items-alist (append x-menu-items-alist
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
80 (list (cons item binding)))))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
81 (set-buffer old)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
82 item))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
83
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
84 (defun x-menu-delete-item (menu item)
12950
83fea4c633e8 Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 841
diff changeset
85 "Delete from MENU the item named ITEM.
83fea4c633e8 Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 841
diff changeset
86 Call `x-menu-compute' before making the menu available to the user."
35
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
87 (let ((old (current-buffer))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
88 elt)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
89 (set-buffer menu)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
90 (if (setq elt (assoc item x-menu-items-alist))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
91 (rplaca elt nil))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
92 (set-buffer old)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
93 item))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
94
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
95 (defun x-menu-activate (menu)
12950
83fea4c633e8 Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 841
diff changeset
96 "Compute all necessary parameters for MENU.
83fea4c633e8 Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 841
diff changeset
97 This must be called whenever a menu is modified before it is made
83fea4c633e8 Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 841
diff changeset
98 available to the user. This also creates the menu itself."
35
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
99 (let ((buf (current-buffer)))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
100 (pop-to-buffer menu)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
101 (let (buffer-read-only)
806
d42e1151eed8 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 773
diff changeset
102 (setq x-menu-max-width (1- (frame-width)))
35
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
103 (setq x-menu-item-width 0)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
104 (let (items-head
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
105 (items-tail x-menu-items-alist))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
106 (while items-tail
12950
83fea4c633e8 Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 841
diff changeset
107 (if (car (car items-tail))
35
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
108 (progn (setq items-head (cons (car items-tail) items-head))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
109 (setq x-menu-item-width
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
110 (max x-menu-item-width
12950
83fea4c633e8 Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 841
diff changeset
111 (length (car (car items-tail)))))))
35
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
112 (setq items-tail (cdr items-tail)))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
113 (setq x-menu-items-alist (reverse items-head)))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
114 (setq x-menu-item-width (+ x-menu-item-spacing x-menu-item-width))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
115 (setq x-menu-items-per-line
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
116 (max 1 (/ x-menu-max-width x-menu-item-width)))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
117 (erase-buffer)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
118 (let ((items-head x-menu-items-alist))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
119 (while items-head
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
120 (let ((items 0))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
121 (while (and items-head
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
122 (<= (setq items (1+ items)) x-menu-items-per-line))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
123 (insert (format (concat "%"
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
124 (int-to-string x-menu-item-width) "s")
12950
83fea4c633e8 Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 841
diff changeset
125 (car (car items-head))))
35
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
126 (setq items-head (cdr items-head))))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
127 (insert ?\n)))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
128 (shrink-window (max 0
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
129 (- (window-height)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
130 (1+ (count-lines (point-min) (point-max))))))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
131 (goto-char (point-min)))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
132 (pop-to-buffer buf)))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
133
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
134 (defun x-menu-pick-entry (position event)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
135 "Internal function for dispatching on mouse/menu events"
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
136 (let* ((x (min (1- x-menu-items-per-line)
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
137 (/ (current-column) x-menu-item-width)))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
138 (y (- (count-lines (point-min) (point))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
139 (if (zerop (current-column)) 0 1)))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
140 (item (+ x (* y x-menu-items-per-line)))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
141 (litem (cdr (nth item x-menu-items-alist))))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
142 (and litem (funcall litem event)))
63b375f17a65 Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
143 (pop-to-buffer x-menu-assoc-buffer))
656
d74e65773062 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 35
diff changeset
144
18383
11218164bc54 Add provide call.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
145 (provide 'x-menu)
11218164bc54 Add provide call.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
146
656
d74e65773062 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 35
diff changeset
147 ;;; x-menu.el ends here