Commit | Line | Data |
---|---|---|
be010748 | 1 | ;;; tmm.el --- text mode access to menu-bar |
20062d6b | 2 | |
0d30b337 | 3 | ;; Copyright (C) 1994, 1995, 1996, 2000, 2001, 2002, 2003, |
409cc4a3 | 4 | ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
20062d6b RS |
5 | |
6 | ;; Author: Ilya Zakharevich <ilya@math.mps.ohio-state.edu> | |
fc225f66 | 7 | ;; Maintainer: FSF |
97895167 | 8 | ;; Keywords: convenience |
20062d6b | 9 | |
d440e474 | 10 | ;; This file is part of GNU Emacs. |
20062d6b RS |
11 | |
12 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 | ;; it under the terms of the GNU General Public License as published by | |
b4aa6026 | 14 | ;; the Free Software Foundation; either version 3, or (at your option) |
20062d6b RS |
15 | ;; any later version. |
16 | ||
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
b578f267 | 23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
086add15 LK |
24 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 | ;; Boston, MA 02110-1301, USA. | |
20062d6b | 26 | |
b578f267 | 27 | ;;; Commentary: |
20062d6b | 28 | |
97895167 | 29 | ;; This package provides text mode access to the menu bar. |
20062d6b | 30 | |
b578f267 | 31 | ;;; Code: |
20062d6b RS |
32 | |
33 | (require 'electric) | |
20062d6b | 34 | |
4bef9110 SE |
35 | (defgroup tmm nil |
36 | "Text mode access to menu-bar." | |
37 | :prefix "tmm-" | |
38 | :group 'menu) | |
39 | ||
20062d6b RS |
40 | ;;; The following will be localized, added only to pacify the compiler. |
41 | (defvar tmm-short-cuts) | |
fc225f66 | 42 | (defvar tmm-old-mb-map nil) |
20062d6b | 43 | (defvar tmm-old-comp-map) |
93253b0e | 44 | (defvar tmm-c-prompt nil) |
20062d6b | 45 | (defvar tmm-km-list) |
670ce6ea | 46 | (defvar tmm-next-shortcut-digit) |
20062d6b RS |
47 | (defvar tmm-table-undef) |
48 | ||
e6a5c7de | 49 | ;;;###autoload (define-key global-map "\M-`" 'tmm-menubar) |
77cc5db0 | 50 | ;;;###autoload (define-key global-map [menu-bar mouse-1] 'tmm-menubar-mouse) |
afb1835e | 51 | |
20062d6b | 52 | ;;;###autoload |
77cc5db0 | 53 | (defun tmm-menubar (&optional x-position) |
20062d6b | 54 | "Text-mode emulation of looking and choosing from a menubar. |
77cc5db0 RS |
55 | See the documentation for `tmm-prompt'. |
56 | X-POSITION, if non-nil, specifies a horizontal position within the menu bar; | |
57 | we make that menu bar item (the one at that position) the default choice." | |
20062d6b RS |
58 | (interactive) |
59 | (run-hooks 'menu-bar-update-hook) | |
fc225f66 | 60 | ;; Obey menu-bar-final-items; put those items last. |
77cc5db0 RS |
61 | (let ((menu-bar (tmm-get-keybind [menu-bar])) |
62 | menu-bar-item) | |
fc225f66 RS |
63 | (let ((list menu-bar-final-items)) |
64 | (while list | |
65 | (let ((item (car list))) | |
66 | ;; ITEM is the name of an item that we want to put last. | |
67 | ;; Find it in MENU-BAR and move it to the end. | |
68 | (let ((this-one (assq item menu-bar))) | |
69 | (setq menu-bar (append (delq this-one menu-bar) | |
70 | (list this-one))))) | |
71 | (setq list (cdr list)))) | |
77cc5db0 | 72 | (if x-position |
93253b0e | 73 | (let ((tail menu-bar) (column 0) |
a4e03d18 | 74 | this-one name visible) |
573e00ef | 75 | (while (and tail (<= column x-position)) |
77cc5db0 | 76 | (setq this-one (car tail)) |
573e00ef NR |
77 | (if (and (consp this-one) |
78 | (consp (cdr this-one)) | |
a4e03d18 | 79 | (setq name ;simple menu |
93253b0e NR |
80 | (cond ((stringp (nth 1 this-one)) |
81 | (nth 1 this-one)) | |
a4e03d18 NR |
82 | ;extended menu |
83 | ((stringp (nth 2 this-one)) | |
84 | (setq visible (plist-get | |
85 | (nthcdr 4 this-one) :visible)) | |
86 | (unless (and visible (not (eval visible))) | |
87 | (nth 2 this-one)))))) | |
93253b0e | 88 | (setq column (+ column (length name) 1))) |
77cc5db0 RS |
89 | (setq tail (cdr tail))) |
90 | (setq menu-bar-item (car this-one)))) | |
91 | (tmm-prompt menu-bar nil menu-bar-item))) | |
92 | ||
8e735883 | 93 | ;;;###autoload |
77cc5db0 RS |
94 | (defun tmm-menubar-mouse (event) |
95 | "Text-mode emulation of looking and choosing from a menubar. | |
96 | This command is used when you click the mouse in the menubar | |
97 | on a console which has no window system but does have a mouse. | |
98 | See the documentation for `tmm-prompt'." | |
99 | (interactive "e") | |
100 | (tmm-menubar (car (posn-x-y (event-start event))))) | |
20062d6b | 101 | |
4bef9110 | 102 | (defcustom tmm-mid-prompt "==>" |
c03aab72 | 103 | "String to insert between shortcut and menu item. |
670ce6ea | 104 | If nil, there will be no shortcuts. It should not consist only of spaces, |
4bef9110 SE |
105 | or else the correct item might not be found in the `*Completions*' buffer." |
106 | :type 'string | |
107 | :group 'tmm) | |
20062d6b RS |
108 | |
109 | (defvar tmm-mb-map nil | |
110 | "A place to store minibuffer map.") | |
111 | ||
97895167 | 112 | (defcustom tmm-completion-prompt |
e5ba1eb9 | 113 | "Press PageUp key to reach this buffer from the minibuffer. |
20062d6b | 114 | Alternatively, you can use Up/Down keys (or your History keys) to change |
97895167 | 115 | the item in the minibuffer, and press RET when you are done, or press the |
10fe2d38 | 116 | marked letters to pick up your choice. Type C-g or ESC ESC ESC to cancel. |
20062d6b | 117 | " |
c03aab72 | 118 | "Help text to insert on the top of the completion buffer. |
670ce6ea | 119 | To save space, you can set this to nil, |
4bef9110 SE |
120 | in which case the standard introduction text is deleted too." |
121 | :type '(choice string (const nil)) | |
122 | :group 'tmm) | |
670ce6ea | 123 | |
4bef9110 | 124 | (defcustom tmm-shortcut-style '(downcase upcase) |
c03aab72 | 125 | "What letters to use as menu shortcuts. |
97895167 | 126 | Must be either one of the symbols `downcase' or `upcase', |
4bef9110 SE |
127 | or else a list of the two in the order you prefer." |
128 | :type '(choice (const downcase) | |
129 | (const upcase) | |
130 | (repeat (choice (const downcase) (const upcase)))) | |
131 | :group 'tmm) | |
670ce6ea | 132 | |
4bef9110 | 133 | (defcustom tmm-shortcut-words 2 |
c03aab72 | 134 | "How many successive words to try for shortcuts, nil means all. |
97895167 | 135 | If you use only one of `downcase' or `upcase' for `tmm-shortcut-style', |
4bef9110 SE |
136 | specify nil for this variable." |
137 | :type '(choice integer (const nil)) | |
138 | :group 'tmm) | |
20062d6b | 139 | |
e43cbeae | 140 | (defface tmm-inactive |
1c8c1295 | 141 | '((t :inherit shadow)) |
04a5d30f NR |
142 | "Face used for inactive menu items." |
143 | :group 'tmm) | |
144 | ||
20062d6b | 145 | ;;;###autoload |
bdbc7685 | 146 | (defun tmm-prompt (menu &optional in-popup default-item) |
20062d6b | 147 | "Text-mode emulation of calling the bindings in keymap. |
77cc5db0 RS |
148 | Creates a text-mode menu of possible choices. You can access the elements |
149 | in the menu in two ways: | |
150 | *) via history mechanism from minibuffer; | |
20062d6b RS |
151 | *) Or via completion-buffer that is automatically shown. |
152 | The last alternative is currently a hack, you cannot use mouse reliably. | |
bdbc7685 RS |
153 | |
154 | MENU is like the MENU argument to `x-popup-menu': either a | |
155 | keymap or an alist of alists. | |
156 | DEFAULT-ITEM, if non-nil, specifies an initial default choice. | |
157 | Its value should be an event that has a binding in MENU." | |
158 | ;; If the optional argument IN-POPUP is t, | |
159 | ;; then MENU is an alist of elements of the form (STRING . VALUE). | |
160 | ;; That is used for recursive calls only. | |
161 | (let ((gl-str "Menu bar") ;; The menu bar itself is not a menu keymap | |
162 | ; so it doesn't have a name. | |
163 | tmm-km-list out history history-len tmm-table-undef tmm-c-prompt | |
164 | tmm-old-mb-map tmm-old-comp-map tmm-short-cuts | |
165 | chosen-string choice | |
166 | (not-menu (not (keymapp menu)))) | |
20062d6b | 167 | (run-hooks 'activate-menubar-hook) |
bdbc7685 RS |
168 | ;; Compute tmm-km-list from MENU. |
169 | ;; tmm-km-list is an alist of (STRING . MEANING). | |
170 | ;; It has no other elements. | |
171 | ;; The order of elements in tmm-km-list is the order of the menu bar. | |
e07436e1 DL |
172 | (mapc (lambda (elt) |
173 | (if (stringp elt) | |
174 | (setq gl-str elt) | |
175 | (and (listp elt) (tmm-get-keymap elt not-menu)))) | |
bdbc7685 RS |
176 | menu) |
177 | ;; Choose an element of tmm-km-list; put it in choice. | |
178 | (if (and not-menu (= 1 (length tmm-km-list))) | |
179 | ;; If this is the top-level of an x-popup-menu menu, | |
180 | ;; and there is just one pane, choose that one silently. | |
181 | ;; This way we only ask the user one question, | |
182 | ;; for which element of that pane. | |
183 | (setq choice (cdr (car tmm-km-list))) | |
ae3f2f3c RS |
184 | (unless tmm-km-list |
185 | (error "Empty menu reached")) | |
bdbc7685 RS |
186 | (and tmm-km-list |
187 | (let ((index-of-default 0)) | |
188 | (if tmm-mid-prompt | |
189 | (setq tmm-km-list (tmm-add-shortcuts tmm-km-list)) | |
190 | t) | |
191 | ;; Find the default item's index within the menu bar. | |
192 | ;; We use this to decide the initial minibuffer contents | |
193 | ;; and initial history position. | |
194 | (if default-item | |
a4e03d18 | 195 | (let ((tail menu) visible) |
bdbc7685 RS |
196 | (while (and tail |
197 | (not (eq (car-safe (car tail)) default-item))) | |
198 | ;; Be careful to count only the elements of MENU | |
199 | ;; that actually constitute menu bar items. | |
200 | (if (and (consp (car tail)) | |
d0bca3c9 | 201 | (or (stringp (car-safe (cdr (car tail)))) |
a4e03d18 NR |
202 | (and |
203 | (eq (car-safe (cdr (car tail))) 'menu-item) | |
204 | (progn | |
205 | (setq visible | |
206 | (plist-get | |
207 | (nthcdr 4 (car tail)) :visible)) | |
208 | (or (not visible) (eval visible)))))) | |
bdbc7685 RS |
209 | (setq index-of-default (1+ index-of-default))) |
210 | (setq tail (cdr tail))))) | |
04a5d30f NR |
211 | (let ((prompt (concat "^." (regexp-quote tmm-mid-prompt)))) |
212 | (setq history | |
213 | (reverse (delq nil | |
214 | (mapcar | |
215 | (lambda (elt) | |
216 | (if (string-match prompt (car elt)) | |
217 | (car elt))) | |
218 | tmm-km-list))))) | |
bdbc7685 RS |
219 | (setq history-len (length history)) |
220 | (setq history (append history history history history)) | |
221 | (setq tmm-c-prompt (nth (- history-len 1 index-of-default) history)) | |
222 | (add-hook 'minibuffer-setup-hook 'tmm-add-prompt) | |
573e00ef NR |
223 | (if default-item |
224 | (setq out (car (nth index-of-default tmm-km-list))) | |
225 | (save-excursion | |
226 | (unwind-protect | |
227 | (setq out | |
228 | (completing-read | |
229 | (concat gl-str | |
230 | " (up/down to change, PgUp to menu): ") | |
231 | tmm-km-list nil t nil | |
232 | (cons 'history | |
233 | (- (* 2 history-len) index-of-default)))) | |
c03aab72 SM |
234 | (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt) |
235 | (if (get-buffer "*Completions*") | |
236 | (with-current-buffer "*Completions*" | |
237 | (use-local-map tmm-old-comp-map) | |
238 | (bury-buffer (current-buffer))))))))) | |
bdbc7685 RS |
239 | (setq choice (cdr (assoc out tmm-km-list))) |
240 | (and (null choice) | |
241 | (> (length out) (length tmm-c-prompt)) | |
242 | (string= (substring out 0 (length tmm-c-prompt)) tmm-c-prompt) | |
243 | (setq out (substring out (length tmm-c-prompt)) | |
244 | choice (cdr (assoc out tmm-km-list)))) | |
ae3f2f3c | 245 | (and (null choice) out |
bdbc7685 RS |
246 | (setq out (try-completion out tmm-km-list) |
247 | choice (cdr (assoc out tmm-km-list))))) | |
248 | ;; CHOICE is now (STRING . MEANING). Separate the two parts. | |
249 | (setq chosen-string (car choice)) | |
250 | (setq choice (cdr choice)) | |
251 | (cond (in-popup | |
252 | ;; We just did the inner level of a -popup menu. | |
253 | choice) | |
254 | ;; We just did the outer level. Do the inner level now. | |
97895167 | 255 | (not-menu (tmm-prompt choice t)) |
bdbc7685 RS |
256 | ;; We just handled a menu keymap and found another keymap. |
257 | ((keymapp choice) | |
258 | (if (symbolp choice) | |
259 | (setq choice (indirect-function choice))) | |
260 | (condition-case nil | |
261 | (require 'mouse) | |
262 | (error nil)) | |
263 | (condition-case nil | |
264 | (x-popup-menu nil choice) ; Get the shortcuts | |
265 | (error nil)) | |
266 | (tmm-prompt choice)) | |
267 | ;; We just handled a menu keymap and found a command. | |
268 | (choice | |
269 | (if chosen-string | |
3132f319 KH |
270 | (progn |
271 | (setq last-command-event chosen-string) | |
272 | (call-interactively choice)) | |
bdbc7685 | 273 | choice))))) |
20062d6b | 274 | |
20062d6b RS |
275 | (defun tmm-add-shortcuts (list) |
276 | "Adds shortcuts to cars of elements of the list. | |
277 | Takes a list of lists with a string as car, returns list with | |
fc225f66 RS |
278 | shortcuts added to these cars. |
279 | Stores a list of all the shortcuts in the free variable `tmm-short-cuts'." | |
670ce6ea RS |
280 | (let ((tmm-next-shortcut-digit ?0)) |
281 | (mapcar 'tmm-add-one-shortcut (reverse list)))) | |
20062d6b | 282 | |
670ce6ea RS |
283 | (defsubst tmm-add-one-shortcut (elt) |
284 | ;; uses the free vars tmm-next-shortcut-digit and tmm-short-cuts | |
04a5d30f NR |
285 | (cond |
286 | ((eq (cddr elt) 'ignore) | |
287 | (cons (concat " " (make-string (length tmm-mid-prompt) ?\-) | |
288 | (car elt)) | |
289 | (cdr elt))) | |
290 | (t | |
291 | (let* ((str (car elt)) | |
292 | (paren (string-match "(" str)) | |
293 | (pos 0) (word 0) char) | |
294 | (catch 'done ; ??? is this slow? | |
295 | (while (and (or (not tmm-shortcut-words) ; no limit on words | |
296 | (< word tmm-shortcut-words)) ; try n words | |
297 | (setq pos (string-match "\\w+" str pos)) ; get next word | |
298 | (not (and paren (> pos paren)))) ; don't go past "(binding.." | |
299 | (if (or (= pos 0) | |
300 | (/= (aref str (1- pos)) ?.)) ; avoid file extensions | |
301 | (let ((shortcut-style | |
302 | (if (listp tmm-shortcut-style) ; convert to list | |
303 | tmm-shortcut-style | |
304 | (list tmm-shortcut-style)))) | |
305 | (while shortcut-style ; try upcase and downcase variants | |
306 | (setq char (funcall (car shortcut-style) (aref str pos))) | |
307 | (if (not (memq char tmm-short-cuts)) (throw 'done char)) | |
308 | (setq shortcut-style (cdr shortcut-style))))) | |
309 | (setq word (1+ word)) | |
310 | (setq pos (match-end 0))) | |
311 | (while (<= tmm-next-shortcut-digit ?9) ; no letter shortcut, pick a digit | |
312 | (setq char tmm-next-shortcut-digit) | |
313 | (setq tmm-next-shortcut-digit (1+ tmm-next-shortcut-digit)) | |
314 | (if (not (memq char tmm-short-cuts)) (throw 'done char))) | |
315 | (setq char nil)) | |
316 | (if char (setq tmm-short-cuts (cons char tmm-short-cuts))) | |
317 | (cons (concat (if char (concat (char-to-string char) tmm-mid-prompt) | |
318 | ;; keep them lined up in columns | |
a322861f | 319 | (make-string (1+ (length tmm-mid-prompt)) ?\s)) |
04a5d30f NR |
320 | str) |
321 | (cdr elt)))))) | |
670ce6ea RS |
322 | |
323 | ;; This returns the old map. | |
fe03654a | 324 | (defun tmm-define-keys (minibuffer) |
670ce6ea RS |
325 | (let ((map (make-sparse-keymap))) |
326 | (suppress-keymap map t) | |
e07436e1 DL |
327 | (mapc |
328 | (lambda (c) | |
329 | (if (listp tmm-shortcut-style) | |
330 | (define-key map (char-to-string c) 'tmm-shortcut) | |
331 | ;; only one kind of letters are shortcuts, so map both upcase and | |
332 | ;; downcase input to the same | |
333 | (define-key map (char-to-string (downcase c)) 'tmm-shortcut) | |
334 | (define-key map (char-to-string (upcase c)) 'tmm-shortcut))) | |
670ce6ea RS |
335 | tmm-short-cuts) |
336 | (if minibuffer | |
337 | (progn | |
338 | (define-key map [pageup] 'tmm-goto-completions) | |
339 | (define-key map [prior] 'tmm-goto-completions) | |
340 | (define-key map "\ev" 'tmm-goto-completions) | |
341 | (define-key map "\C-n" 'next-history-element) | |
342 | (define-key map "\C-p" 'previous-history-element))) | |
343 | (prog1 (current-local-map) | |
344 | (use-local-map (append map (current-local-map)))))) | |
345 | ||
346 | (defun tmm-completion-delete-prompt () | |
347 | (set-buffer standard-output) | |
348 | (goto-char 1) | |
349 | (delete-region 1 (search-forward "Possible completions are:\n"))) | |
b46324e6 | 350 | |
04a5d30f NR |
351 | (defun tmm-remove-inactive-mouse-face () |
352 | "Remove the mouse-face property from inactive menu items." | |
353 | (let ((inhibit-read-only t) | |
354 | (inactive-string | |
355 | (concat " " (make-string (length tmm-mid-prompt) ?\-))) | |
356 | next) | |
357 | (save-excursion | |
358 | (goto-char (point-min)) | |
359 | (while (not (eobp)) | |
360 | (setq next (next-single-char-property-change (point) 'mouse-face)) | |
361 | (when (looking-at inactive-string) | |
362 | (remove-text-properties (point) next '(mouse-face)) | |
e43cbeae | 363 | (add-text-properties (point) next '(face tmm-inactive))) |
04a5d30f NR |
364 | (goto-char next))) |
365 | (set-buffer-modified-p nil))) | |
366 | ||
20062d6b RS |
367 | (defun tmm-add-prompt () |
368 | (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt) | |
afb1835e | 369 | (add-hook 'minibuffer-exit-hook 'tmm-delete-map nil t) |
04a5d30f NR |
370 | (unless tmm-c-prompt |
371 | (error "No active menu entries")) | |
b46324e6 | 372 | (let ((win (selected-window))) |
670ce6ea | 373 | (setq tmm-old-mb-map (tmm-define-keys t)) |
20062d6b | 374 | ;; Get window and hide it for electric mode to get correct size |
97895167 | 375 | (save-window-excursion |
fc225f66 RS |
376 | (let ((completions |
377 | (mapcar 'car minibuffer-completion-table))) | |
670ce6ea RS |
378 | (or tmm-completion-prompt |
379 | (add-hook 'completion-setup-hook | |
380 | 'tmm-completion-delete-prompt 'append)) | |
fc225f66 | 381 | (with-output-to-temp-buffer "*Completions*" |
670ce6ea RS |
382 | (display-completion-list completions)) |
383 | (remove-hook 'completion-setup-hook 'tmm-completion-delete-prompt)) | |
04a5d30f NR |
384 | (set-buffer "*Completions*") |
385 | (tmm-remove-inactive-mouse-face) | |
0595722b | 386 | (when tmm-completion-prompt |
0595722b GM |
387 | (let ((buffer-read-only nil)) |
388 | (goto-char (point-min)) | |
389 | (insert tmm-completion-prompt)))) | |
ca85cf8a | 390 | (save-selected-window |
20062d6b RS |
391 | (other-window 1) ; Electric-pop-up-window does |
392 | ; not work in minibuffer | |
ca85cf8a RS |
393 | (Electric-pop-up-window "*Completions*") |
394 | (with-current-buffer "*Completions*" | |
395 | (setq tmm-old-comp-map (tmm-define-keys nil)))) | |
20062d6b RS |
396 | (insert tmm-c-prompt))) |
397 | ||
398 | (defun tmm-delete-map () | |
afb1835e | 399 | (remove-hook 'minibuffer-exit-hook 'tmm-delete-map t) |
fc225f66 RS |
400 | (if tmm-old-mb-map |
401 | (use-local-map tmm-old-mb-map))) | |
20062d6b RS |
402 | |
403 | (defun tmm-shortcut () | |
fc225f66 | 404 | "Choose the shortcut that the user typed." |
20062d6b | 405 | (interactive) |
670ce6ea RS |
406 | (let ((c last-command-char) s) |
407 | (if (symbolp tmm-shortcut-style) | |
408 | (setq c (funcall tmm-shortcut-style c))) | |
409 | (if (memq c tmm-short-cuts) | |
fc225f66 RS |
410 | (if (equal (buffer-name) "*Completions*") |
411 | (progn | |
b74f6606 | 412 | (goto-char (point-min)) |
fc225f66 | 413 | (re-search-forward |
670ce6ea | 414 | (concat "\\(^\\|[ \t]\\)" (char-to-string c) tmm-mid-prompt)) |
fc225f66 | 415 | (choose-completion)) |
c2a8d4a7 GM |
416 | ;; In minibuffer |
417 | (delete-region (minibuffer-prompt-end) (point-max)) | |
e07436e1 DL |
418 | (mapc (lambda (elt) |
419 | (if (string= | |
97895167 | 420 | (substring (car elt) 0 |
e07436e1 DL |
421 | (min (1+ (length tmm-mid-prompt)) |
422 | (length (car elt)))) | |
423 | (concat (char-to-string c) tmm-mid-prompt)) | |
424 | (setq s (car elt)))) | |
fc225f66 RS |
425 | tmm-km-list) |
426 | (insert s) | |
427 | (exit-minibuffer))))) | |
20062d6b RS |
428 | |
429 | (defun tmm-goto-completions () | |
430 | (interactive) | |
fbe91bbd GM |
431 | (let ((prompt-end (minibuffer-prompt-end))) |
432 | (setq tmm-c-prompt (buffer-substring prompt-end (point-max))) | |
433 | (delete-region prompt-end (point-max))) | |
fc225f66 | 434 | (switch-to-buffer-other-window "*Completions*") |
20062d6b RS |
435 | (search-forward tmm-c-prompt) |
436 | (search-backward tmm-c-prompt)) | |
437 | ||
97895167 | 438 | (defun tmm-get-keymap (elt &optional in-x-menu) |
20062d6b RS |
439 | "Prepends (DOCSTRING EVENT BINDING) to free variable `tmm-km-list'. |
440 | The values are deduced from the argument ELT, that should be an | |
fc225f66 | 441 | element of keymap, an `x-popup-menu' argument, or an element of |
20062d6b | 442 | `x-popup-menu' argument (when IN-X-MENU is not-nil). |
77cc5db0 RS |
443 | This function adds the element only if it is not already present. |
444 | It uses the free variable `tmm-table-undef' to keep undefined keys." | |
7ae862e2 | 445 | (let (km str cache plist filter visible enable (event (car elt))) |
20062d6b RS |
446 | (setq elt (cdr elt)) |
447 | (if (eq elt 'undefined) | |
448 | (setq tmm-table-undef (cons (cons event nil) tmm-table-undef)) | |
42d140b4 KH |
449 | (unless (assoc event tmm-table-undef) |
450 | (cond ((if (listp elt) | |
451 | (or (keymapp elt) (eq (car elt) 'lambda)) | |
452 | (fboundp elt)) | |
453 | (setq km elt)) | |
0595722b | 454 | |
42d140b4 KH |
455 | ((if (listp (cdr-safe elt)) |
456 | (or (keymapp (cdr-safe elt)) | |
457 | (eq (car (cdr-safe elt)) 'lambda)) | |
458 | (fboundp (cdr-safe elt))) | |
459 | (setq km (cdr elt)) | |
460 | (and (stringp (car elt)) (setq str (car elt)))) | |
0595722b | 461 | |
42d140b4 KH |
462 | ((if (listp (cdr-safe (cdr-safe elt))) |
463 | (or (keymapp (cdr-safe (cdr-safe elt))) | |
464 | (eq (car (cdr-safe (cdr-safe elt))) 'lambda)) | |
465 | (fboundp (cdr-safe (cdr-safe elt)))) | |
466 | (setq km (cdr (cdr elt))) | |
467 | (and (stringp (car elt)) (setq str (car elt))) | |
468 | (and str | |
469 | (stringp (cdr (car (cdr elt)))) ; keyseq cache | |
470 | (setq cache (cdr (car (cdr elt)))) | |
471 | cache (setq str (concat str cache)))) | |
0595722b | 472 | |
42d140b4 | 473 | ((eq (car-safe elt) 'menu-item) |
0595722b | 474 | ;; (menu-item TITLE COMMAND KEY ...) |
dd81ca0d | 475 | (setq plist (cdr-safe (cdr-safe (cdr-safe elt)))) |
0595722b GM |
476 | (when (consp (car-safe plist)) |
477 | (setq plist (cdr-safe plist))) | |
42d140b4 | 478 | (setq km (nth 2 elt)) |
c19cc275 | 479 | (setq str (eval (nth 1 elt))) |
dd81ca0d RS |
480 | (setq filter (plist-get plist :filter)) |
481 | (if filter | |
482 | (setq km (funcall filter km))) | |
97895167 PJ |
483 | (setq visible (plist-get plist :visible)) |
484 | (if visible | |
485 | (setq km (and (eval visible) km))) | |
7ae862e2 NR |
486 | (setq enable (plist-get plist :enable)) |
487 | (if enable | |
04a5d30f | 488 | (setq km (if (eval enable) km 'ignore))) |
42d140b4 | 489 | (and str |
ae3f2f3c | 490 | (consp (nth 3 elt)) |
437d9247 RS |
491 | (stringp (cdr (nth 3 elt))) ; keyseq cache |
492 | (setq cache (cdr (nth 3 elt))) | |
42d140b4 KH |
493 | cache |
494 | (setq str (concat str cache)))) | |
0595722b | 495 | |
42d140b4 KH |
496 | ((if (listp (cdr-safe (cdr-safe (cdr-safe elt)))) |
497 | (or (keymapp (cdr-safe (cdr-safe (cdr-safe elt)))) | |
498 | (eq (car (cdr-safe (cdr-safe (cdr-safe elt)))) 'lambda)) | |
499 | (fboundp (cdr-safe (cdr-safe (cdr-safe elt))))) | |
500 | ; New style of easy-menu | |
501 | (setq km (cdr (cdr (cdr elt)))) | |
502 | (and (stringp (car elt)) (setq str (car elt))) | |
503 | (and str | |
504 | (stringp (cdr (car (cdr (cdr elt))))) ; keyseq cache | |
505 | (setq cache (cdr (car (cdr (cdr elt))))) | |
506 | cache (setq str (concat str cache)))) | |
0595722b | 507 | |
42d140b4 KH |
508 | ((stringp event) ; x-popup or x-popup element |
509 | (if (or in-x-menu (stringp (car-safe elt))) | |
510 | (setq str event event nil km elt) | |
511 | (setq str event event nil km (cons 'keymap elt)) | |
512 | )))) | |
20062d6b | 513 | (and km (stringp km) (setq str km)) |
2a9f2437 RS |
514 | ;; Verify that the command is enabled; |
515 | ;; if not, don't mention it. | |
516 | (when (and km (symbolp km) (get km 'menu-enable)) | |
04a5d30f | 517 | (setq km (if (eval (get km 'menu-enable)) km 'ignore))) |
20062d6b RS |
518 | (and km str |
519 | (or (assoc str tmm-km-list) | |
c19cc275 | 520 | (push (cons str (cons event km)) tmm-km-list)))))) |
20062d6b | 521 | |
20062d6b | 522 | (defun tmm-get-keybind (keyseq) |
fc225f66 | 523 | "Return the current binding of KEYSEQ, merging prefix definitions. |
91a5e367 | 524 | If KEYSEQ is a prefix key that has local and global bindings, |
fc225f66 RS |
525 | we merge them into a single keymap which shows the proper order of the menu. |
526 | However, for the menu bar itself, the value does not take account | |
527 | of `menu-bar-final-items'." | |
93253b0e | 528 | (let (allbind bind minorbind localbind globalbind) |
fc225f66 RS |
529 | (setq bind (key-binding keyseq)) |
530 | ;; If KEYSEQ is a prefix key, then BIND is either nil | |
531 | ;; or a symbol defined as a keymap (which satisfies keymapp). | |
532 | (if (keymapp bind) | |
533 | (setq bind nil)) | |
534 | ;; If we have a non-keymap definition, return that. | |
535 | (or bind | |
536 | (progn | |
537 | ;; Otherwise, it is a prefix, so make a list of the subcommands. | |
538 | ;; Make a list of all the bindings in all the keymaps. | |
93253b0e NR |
539 | (setq minorbind (mapcar 'cdr (minor-mode-key-binding keyseq))) |
540 | (setq localbind (local-key-binding keyseq)) | |
e47b7019 | 541 | (setq globalbind (copy-sequence (cdr (global-key-binding keyseq)))) |
93253b0e NR |
542 | |
543 | ;; If items have been redefined/undefined locally, remove them from | |
544 | ;; the global list. | |
545 | (dolist (minor minorbind) | |
546 | (dolist (item (cdr minor)) | |
0bb48bff | 547 | (setq globalbind (assq-delete-all (car-safe item) globalbind)))) |
93253b0e | 548 | (dolist (item (cdr localbind)) |
0bb48bff | 549 | (setq globalbind (assq-delete-all (car-safe item) globalbind))) |
93253b0e NR |
550 | |
551 | (setq globalbind (cons 'keymap globalbind)) | |
552 | (setq allbind (cons globalbind (cons localbind minorbind))) | |
553 | ||
fc225f66 | 554 | ;; Merge all the elements of ALLBIND into one keymap. |
e07436e1 DL |
555 | (mapc (lambda (in) |
556 | (if (and (symbolp in) (keymapp in)) | |
557 | (setq in (symbol-function in))) | |
558 | (and in (keymapp in) | |
559 | (if (keymapp bind) | |
560 | (setq bind (nconc bind (copy-sequence (cdr in)))) | |
561 | (setq bind (copy-sequence in))))) | |
fc225f66 RS |
562 | allbind) |
563 | ;; Return that keymap. | |
564 | bind)))) | |
20062d6b | 565 | |
c03aab72 | 566 | ;; Huh? What's that about? --Stef |
20062d6b RS |
567 | (add-hook 'calendar-load-hook (lambda () (require 'cal-menu))) |
568 | ||
20062d6b RS |
569 | (provide 'tmm) |
570 | ||
c03aab72 | 571 | ;; arch-tag: e7ddbdb6-4b95-4da3-afbe-ad6063d112f4 |
20062d6b | 572 | ;;; tmm.el ends here |