(file-cache-minibuffer-complete): Accept a prefix arg.
[bpt/emacs.git] / lisp / tmm.el
CommitLineData
be010748 1;;; tmm.el --- text mode access to menu-bar
20062d6b 2
f5632218 3;; Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
20062d6b
RS
4
5;; Author: Ilya Zakharevich <ilya@math.mps.ohio-state.edu>
fc225f66 6;; Maintainer: FSF
20062d6b 7
d440e474 8;; This file is part of GNU Emacs.
20062d6b
RS
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
12;; the Free Software Foundation; either version 2, or (at your option)
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
b578f267
EN
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
20062d6b 24
b578f267 25;;; Commentary:
20062d6b 26
b578f267 27;; To use this package add
20062d6b 28
b578f267
EN
29;; (autoload 'tmm-menubar 'tmm "Text mode substitute for menubar" t)
30;; (global-set-key [f10] 'tmm-menubar)
31;; to your .emacs file. You can also add your own access to different
32;; menus available in Window System Emacs modeling definition after
33;; tmm-menubar.
20062d6b 34
b578f267 35;;; Code:
20062d6b
RS
36
37(require 'electric)
20062d6b
RS
38
39;;; The following will be localized, added only to pacify the compiler.
40(defvar tmm-short-cuts)
fc225f66 41(defvar tmm-old-mb-map nil)
20062d6b
RS
42(defvar tmm-old-comp-map)
43(defvar tmm-c-prompt)
44(defvar tmm-km-list)
670ce6ea 45(defvar tmm-next-shortcut-digit)
20062d6b
RS
46(defvar tmm-table-undef)
47
e6a5c7de 48;;;###autoload (define-key global-map "\M-`" 'tmm-menubar)
b46324e6 49;;;###autoload (define-key global-map [f10] '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
55See the documentation for `tmm-prompt'.
56X-POSITION, if non-nil, specifies a horizontal position within the menu bar;
57we 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
RS
72 (if x-position
73 (let ((tail menu-bar)
74 this-one
75 (column 0))
76 (while (and tail (< column x-position))
77 (setq this-one (car tail))
78 (if (and (consp (car tail))
79 (consp (cdr (car tail)))
80 (stringp (nth 1 (car tail))))
81 (setq column (+ column
82 (length (nth 1 (car tail)))
83 1)))
84 (setq tail (cdr tail)))
85 (setq menu-bar-item (car this-one))))
86 (tmm-prompt menu-bar nil menu-bar-item)))
87
8e735883 88;;;###autoload
77cc5db0
RS
89(defun tmm-menubar-mouse (event)
90 "Text-mode emulation of looking and choosing from a menubar.
91This command is used when you click the mouse in the menubar
92on a console which has no window system but does have a mouse.
93See the documentation for `tmm-prompt'."
94 (interactive "e")
95 (tmm-menubar (car (posn-x-y (event-start event)))))
20062d6b
RS
96
97(defvar tmm-mid-prompt "==>"
670ce6ea
RS
98 "*String to insert between shortcut and menu item.
99If nil, there will be no shortcuts. It should not consist only of spaces,
100or else the correct item might not be found in the `*Completions*' buffer.")
20062d6b
RS
101
102(defvar tmm-mb-map nil
103 "A place to store minibuffer map.")
104
105(defvar tmm-completion-prompt
106 "Press PageUp Key to reach this buffer from the minibuffer.
107Alternatively, you can use Up/Down keys (or your History keys) to change
108the item in the minibuffer, and press RET when you are done, or press the
10fe2d38 109marked letters to pick up your choice. Type C-g or ESC ESC ESC to cancel.
20062d6b 110"
670ce6ea
RS
111 "*Help text to insert on the top of the completion buffer.
112To save space, you can set this to nil,
113in which case the standard introduction text is deleted too.")
114
115(defvar tmm-shortcut-style '(downcase upcase)
116 "*What letters to use as menu shortcuts.
117Must be either one of the symbols `downcase' or `upcase',
118or else a list of the two in the order you prefer.")
119
120(defvar tmm-shortcut-words 2
121 "*How many successive words to try for shortcuts, nil means all.
122If you use only one of `downcase' or `upcase' for `tmm-shortcut-style',
123specify nil for this variable.")
20062d6b
RS
124
125;;;###autoload
bdbc7685 126(defun tmm-prompt (menu &optional in-popup default-item)
20062d6b 127 "Text-mode emulation of calling the bindings in keymap.
77cc5db0
RS
128Creates a text-mode menu of possible choices. You can access the elements
129in the menu in two ways:
130 *) via history mechanism from minibuffer;
20062d6b
RS
131 *) Or via completion-buffer that is automatically shown.
132The last alternative is currently a hack, you cannot use mouse reliably.
bdbc7685
RS
133
134MENU is like the MENU argument to `x-popup-menu': either a
135keymap or an alist of alists.
136DEFAULT-ITEM, if non-nil, specifies an initial default choice.
137Its value should be an event that has a binding in MENU."
138 ;; If the optional argument IN-POPUP is t,
139 ;; then MENU is an alist of elements of the form (STRING . VALUE).
140 ;; That is used for recursive calls only.
141 (let ((gl-str "Menu bar") ;; The menu bar itself is not a menu keymap
142 ; so it doesn't have a name.
143 tmm-km-list out history history-len tmm-table-undef tmm-c-prompt
144 tmm-old-mb-map tmm-old-comp-map tmm-short-cuts
145 chosen-string choice
146 (not-menu (not (keymapp menu))))
20062d6b 147 (run-hooks 'activate-menubar-hook)
bdbc7685
RS
148 ;; Compute tmm-km-list from MENU.
149 ;; tmm-km-list is an alist of (STRING . MEANING).
150 ;; It has no other elements.
151 ;; The order of elements in tmm-km-list is the order of the menu bar.
20062d6b
RS
152 (mapcar (function (lambda (elt)
153 (if (stringp elt)
154 (setq gl-str elt)
bdbc7685
RS
155 (and (listp elt) (tmm-get-keymap elt not-menu)))))
156 menu)
157 ;; Choose an element of tmm-km-list; put it in choice.
158 (if (and not-menu (= 1 (length tmm-km-list)))
159 ;; If this is the top-level of an x-popup-menu menu,
160 ;; and there is just one pane, choose that one silently.
161 ;; This way we only ask the user one question,
162 ;; for which element of that pane.
163 (setq choice (cdr (car tmm-km-list)))
164 (and tmm-km-list
165 (let ((index-of-default 0))
166 (if tmm-mid-prompt
167 (setq tmm-km-list (tmm-add-shortcuts tmm-km-list))
168 t)
169 ;; Find the default item's index within the menu bar.
170 ;; We use this to decide the initial minibuffer contents
171 ;; and initial history position.
172 (if default-item
173 (let ((tail menu))
174 (while (and tail
175 (not (eq (car-safe (car tail)) default-item)))
176 ;; Be careful to count only the elements of MENU
177 ;; that actually constitute menu bar items.
178 (if (and (consp (car tail))
179 (stringp (car-safe (cdr (car tail)))))
180 (setq index-of-default (1+ index-of-default)))
181 (setq tail (cdr tail)))))
182 (setq history (reverse (mapcar 'car tmm-km-list)))
183 (setq history-len (length history))
184 (setq history (append history history history history))
185 (setq tmm-c-prompt (nth (- history-len 1 index-of-default) history))
186 (add-hook 'minibuffer-setup-hook 'tmm-add-prompt)
ca85cf8a
RS
187 (save-excursion
188 (unwind-protect
189 (setq out
190 (completing-read
191 (concat gl-str " (up/down to change, PgUp to menu): ")
192 tmm-km-list nil t nil
193 (cons 'history (- (* 2 history-len) index-of-default))))
194 (save-excursion
195 (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
196 (if (get-buffer "*Completions*")
197 (progn
198 (set-buffer "*Completions*")
199 (use-local-map tmm-old-comp-map)
200 (bury-buffer (current-buffer)))))
201 ))))
bdbc7685
RS
202 (setq choice (cdr (assoc out tmm-km-list)))
203 (and (null choice)
204 (> (length out) (length tmm-c-prompt))
205 (string= (substring out 0 (length tmm-c-prompt)) tmm-c-prompt)
206 (setq out (substring out (length tmm-c-prompt))
207 choice (cdr (assoc out tmm-km-list))))
208 (and (null choice)
209 (setq out (try-completion out tmm-km-list)
210 choice (cdr (assoc out tmm-km-list)))))
211 ;; CHOICE is now (STRING . MEANING). Separate the two parts.
212 (setq chosen-string (car choice))
213 (setq choice (cdr choice))
214 (cond (in-popup
215 ;; We just did the inner level of a -popup menu.
216 choice)
217 ;; We just did the outer level. Do the inner level now.
218 (not-menu (tmm-prompt choice t))
219 ;; We just handled a menu keymap and found another keymap.
220 ((keymapp choice)
221 (if (symbolp choice)
222 (setq choice (indirect-function choice)))
223 (condition-case nil
224 (require 'mouse)
225 (error nil))
226 (condition-case nil
227 (x-popup-menu nil choice) ; Get the shortcuts
228 (error nil))
229 (tmm-prompt choice))
230 ;; We just handled a menu keymap and found a command.
231 (choice
232 (if chosen-string
3132f319
KH
233 (progn
234 (setq last-command-event chosen-string)
235 (call-interactively choice))
bdbc7685 236 choice)))))
20062d6b 237
20062d6b
RS
238(defun tmm-add-shortcuts (list)
239 "Adds shortcuts to cars of elements of the list.
240Takes a list of lists with a string as car, returns list with
fc225f66
RS
241shortcuts added to these cars.
242Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
670ce6ea
RS
243 (let ((tmm-next-shortcut-digit ?0))
244 (mapcar 'tmm-add-one-shortcut (reverse list))))
20062d6b 245
670ce6ea
RS
246(defsubst tmm-add-one-shortcut (elt)
247;; uses the free vars tmm-next-shortcut-digit and tmm-short-cuts
248 (let* ((str (car elt))
249 (paren (string-match "(" str))
250 (pos 0) (word 0) char)
251 (catch 'done ; ??? is this slow?
252 (while (and (or (not tmm-shortcut-words) ; no limit on words
253 (< word tmm-shortcut-words)) ; try n words
254 (setq pos (string-match "\\w+" str pos)) ; get next word
255 (not (and paren (> pos paren)))) ; don't go past "(binding.."
256 (if (or (= pos 0)
257 (/= (aref str (1- pos)) ?.)) ; avoid file extensions
258 (let ((shortcut-style
259 (if (listp tmm-shortcut-style) ; convert to list
260 tmm-shortcut-style
261 (list tmm-shortcut-style))))
262 (while shortcut-style ; try upcase and downcase variants
263 (setq char (funcall (car shortcut-style) (aref str pos)))
264 (if (not (memq char tmm-short-cuts)) (throw 'done char))
265 (setq shortcut-style (cdr shortcut-style)))))
266 (setq word (1+ word))
267 (setq pos (match-end 0)))
268 (while (<= tmm-next-shortcut-digit ?9) ; no letter shortcut, pick a digit
269 (setq char tmm-next-shortcut-digit)
270 (setq tmm-next-shortcut-digit (1+ tmm-next-shortcut-digit))
271 (if (not (memq char tmm-short-cuts)) (throw 'done char)))
272 (setq char nil))
273 (if char (setq tmm-short-cuts (cons char tmm-short-cuts)))
274 (cons (concat (if char (concat (char-to-string char) tmm-mid-prompt)
275 ;; keep them lined up in columns
276 (make-string (1+ (length tmm-mid-prompt)) ?\ ))
277 str)
278 (cdr elt))))
279
280;; This returns the old map.
fe03654a 281(defun tmm-define-keys (minibuffer)
670ce6ea
RS
282 (let ((map (make-sparse-keymap)))
283 (suppress-keymap map t)
284 (mapcar
285 (function
286 (lambda (c)
287 (if (listp tmm-shortcut-style)
288 (define-key map (char-to-string c) 'tmm-shortcut)
289 ;; only one kind of letters are shortcuts, so map both upcase and
290 ;; downcase input to the same
291 (define-key map (char-to-string (downcase c)) 'tmm-shortcut)
292 (define-key map (char-to-string (upcase c)) 'tmm-shortcut))))
293 tmm-short-cuts)
294 (if minibuffer
295 (progn
296 (define-key map [pageup] 'tmm-goto-completions)
297 (define-key map [prior] 'tmm-goto-completions)
298 (define-key map "\ev" 'tmm-goto-completions)
299 (define-key map "\C-n" 'next-history-element)
300 (define-key map "\C-p" 'previous-history-element)))
301 (prog1 (current-local-map)
302 (use-local-map (append map (current-local-map))))))
303
304(defun tmm-completion-delete-prompt ()
305 (set-buffer standard-output)
306 (goto-char 1)
307 (delete-region 1 (search-forward "Possible completions are:\n")))
b46324e6 308
20062d6b
RS
309(defun tmm-add-prompt ()
310 (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
afb1835e
RS
311 (make-local-hook 'minibuffer-exit-hook)
312 (add-hook 'minibuffer-exit-hook 'tmm-delete-map nil t)
b46324e6 313 (let ((win (selected-window)))
670ce6ea 314 (setq tmm-old-mb-map (tmm-define-keys t))
20062d6b
RS
315 ;; Get window and hide it for electric mode to get correct size
316 (save-window-excursion
fc225f66
RS
317 (let ((completions
318 (mapcar 'car minibuffer-completion-table)))
670ce6ea
RS
319 (or tmm-completion-prompt
320 (add-hook 'completion-setup-hook
321 'tmm-completion-delete-prompt 'append))
fc225f66 322 (with-output-to-temp-buffer "*Completions*"
670ce6ea
RS
323 (display-completion-list completions))
324 (remove-hook 'completion-setup-hook 'tmm-completion-delete-prompt))
325 (if tmm-completion-prompt
326 (progn
ca85cf8a
RS
327 (set-buffer "*Completions*")
328 (goto-char 1)
670ce6ea 329 (insert tmm-completion-prompt)))
20062d6b 330 )
ca85cf8a 331 (save-selected-window
20062d6b
RS
332 (other-window 1) ; Electric-pop-up-window does
333 ; not work in minibuffer
ca85cf8a
RS
334 (Electric-pop-up-window "*Completions*")
335 (with-current-buffer "*Completions*"
336 (setq tmm-old-comp-map (tmm-define-keys nil))))
670ce6ea 337
20062d6b
RS
338 (insert tmm-c-prompt)))
339
340(defun tmm-delete-map ()
afb1835e 341 (remove-hook 'minibuffer-exit-hook 'tmm-delete-map t)
fc225f66
RS
342 (if tmm-old-mb-map
343 (use-local-map tmm-old-mb-map)))
20062d6b
RS
344
345(defun tmm-shortcut ()
fc225f66 346 "Choose the shortcut that the user typed."
20062d6b 347 (interactive)
670ce6ea
RS
348 (let ((c last-command-char) s)
349 (if (symbolp tmm-shortcut-style)
350 (setq c (funcall tmm-shortcut-style c)))
351 (if (memq c tmm-short-cuts)
fc225f66
RS
352 (if (equal (buffer-name) "*Completions*")
353 (progn
354 (beginning-of-buffer)
355 (re-search-forward
670ce6ea 356 (concat "\\(^\\|[ \t]\\)" (char-to-string c) tmm-mid-prompt))
fc225f66
RS
357 (choose-completion))
358 (erase-buffer) ; In minibuffer
359 (mapcar (lambda (elt)
360 (if (string=
361 (substring (car elt) 0
362 (min (1+ (length tmm-mid-prompt))
363 (length (car elt))))
670ce6ea 364 (concat (char-to-string c) tmm-mid-prompt))
fc225f66
RS
365 (setq s (car elt))))
366 tmm-km-list)
367 (insert s)
368 (exit-minibuffer)))))
20062d6b
RS
369
370(defun tmm-goto-completions ()
371 (interactive)
372 (setq tmm-c-prompt (buffer-string))
373 (erase-buffer)
fc225f66 374 (switch-to-buffer-other-window "*Completions*")
20062d6b
RS
375 (search-forward tmm-c-prompt)
376 (search-backward tmm-c-prompt))
377
20062d6b
RS
378(defun tmm-get-keymap (elt &optional in-x-menu)
379 "Prepends (DOCSTRING EVENT BINDING) to free variable `tmm-km-list'.
380The values are deduced from the argument ELT, that should be an
fc225f66 381element of keymap, an `x-popup-menu' argument, or an element of
20062d6b 382`x-popup-menu' argument (when IN-X-MENU is not-nil).
77cc5db0
RS
383This function adds the element only if it is not already present.
384It uses the free variable `tmm-table-undef' to keep undefined keys."
20062d6b
RS
385 (let (km str cache (event (car elt)))
386 (setq elt (cdr elt))
387 (if (eq elt 'undefined)
388 (setq tmm-table-undef (cons (cons event nil) tmm-table-undef))
389 (or
390 (assoc event tmm-table-undef)
391 (and (if (listp elt)
1146fab3 392 (or (keymapp elt) (eq (car elt) 'lambda))
20062d6b
RS
393 (fboundp elt))
394 (setq km elt))
395 (and (if (listp (cdr-safe elt))
1146fab3
RS
396 (or (keymapp (cdr-safe elt))
397 (eq (car (cdr-safe elt)) 'lambda))
20062d6b
RS
398 (fboundp (cdr-safe elt)))
399 (setq km (cdr elt))
400 (and (stringp (car elt)) (setq str (car elt))))
401 (and (if (listp (cdr-safe (cdr-safe elt)))
1146fab3
RS
402 (or (keymapp (cdr-safe (cdr-safe elt)))
403 (eq (car (cdr-safe (cdr-safe elt))) 'lambda))
20062d6b
RS
404 (fboundp (cdr-safe (cdr-safe elt))))
405 (setq km (cdr (cdr elt)))
406 (and (stringp (car elt)) (setq str (car elt)))
407 (or (and str
408 (stringp (cdr (car (cdr elt)))) ; keyseq cache
409 (setq cache (cdr (car (cdr elt))))
410 cache (setq str (concat str cache))) str))
411 (and (if (listp (cdr-safe (cdr-safe (cdr-safe elt))))
1146fab3
RS
412 (or (keymapp (cdr-safe (cdr-safe (cdr-safe elt))))
413 (eq (car (cdr-safe (cdr-safe (cdr-safe elt)))) 'lambda))
20062d6b
RS
414 (fboundp (cdr-safe (cdr-safe (cdr-safe elt)))))
415 ; New style of easy-menu
416 (setq km (cdr (cdr (cdr elt))))
417 (and (stringp (car elt)) (setq str (car elt)))
418 (or (and str
419 (stringp (cdr (car (cdr (cdr elt))))) ; keyseq cache
420 (setq cache (cdr (car (cdr (cdr elt)))))
421 cache (setq str (concat str cache)))
422 str))
423 (and (stringp event) ; x-popup or x-popup element
424 (if (or in-x-menu (stringp (car-safe elt)))
425 (setq str event event nil km elt)
426 (setq str event event nil km (cons 'keymap elt))
427 )))
428 (and km (stringp km) (setq str km))
2a9f2437
RS
429 ;; Verify that the command is enabled;
430 ;; if not, don't mention it.
431 (when (and km (symbolp km) (get km 'menu-enable))
432 (unless (eval (get km 'menu-enable))
433 (setq km nil)))
20062d6b
RS
434 (and km str
435 (or (assoc str tmm-km-list)
436 (setq tmm-km-list
437 (cons (cons str (cons event km)) tmm-km-list)))
438 ))))
439
20062d6b 440(defun tmm-get-keybind (keyseq)
fc225f66 441 "Return the current binding of KEYSEQ, merging prefix definitions.
91a5e367 442If KEYSEQ is a prefix key that has local and global bindings,
fc225f66
RS
443we merge them into a single keymap which shows the proper order of the menu.
444However, for the menu bar itself, the value does not take account
445of `menu-bar-final-items'."
20062d6b 446 (let (allbind bind)
fc225f66
RS
447 (setq bind (key-binding keyseq))
448 ;; If KEYSEQ is a prefix key, then BIND is either nil
449 ;; or a symbol defined as a keymap (which satisfies keymapp).
450 (if (keymapp bind)
451 (setq bind nil))
452 ;; If we have a non-keymap definition, return that.
453 (or bind
454 (progn
455 ;; Otherwise, it is a prefix, so make a list of the subcommands.
456 ;; Make a list of all the bindings in all the keymaps.
457 (setq allbind (mapcar 'cdr (minor-mode-key-binding keyseq)))
458 (setq allbind (cons (local-key-binding keyseq) allbind))
459 (setq allbind (cons (global-key-binding keyseq) allbind))
460 ;; Merge all the elements of ALLBIND into one keymap.
461 (mapcar (lambda (in)
462 (if (and (symbolp in) (keymapp in))
463 (setq in (symbol-function in)))
464 (and in (keymapp in)
465 (if (keymapp bind)
466 (setq bind (nconc bind (copy-sequence (cdr in))))
467 (setq bind (copy-sequence in)))))
468 allbind)
469 ;; Return that keymap.
470 bind))))
20062d6b
RS
471
472(add-hook 'calendar-load-hook (lambda () (require 'cal-menu)))
473
20062d6b
RS
474(provide 'tmm)
475
20062d6b 476;;; tmm.el ends here