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