(xcar, xcdr): Print with /x.
[emacs.git] / lisp / tmm.el
blob3ca00def6e1a4407dcc9eadaa4d3847c6d81fb63
1 ;;; tmm.el --- text mode access to menu-bar
3 ;; Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
5 ;; Author: Ilya Zakharevich <ilya@math.mps.ohio-state.edu>
6 ;; Maintainer: FSF
8 ;; This file is part of GNU Emacs.
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.
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.
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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; To use this package add
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.
35 ;;; Code:
37 (require 'electric)
39 ;;; The following will be localized, added only to pacify the compiler.
40 (defvar tmm-short-cuts)
41 (defvar tmm-old-mb-map nil)
42 (defvar tmm-old-comp-map)
43 (defvar tmm-c-prompt)
44 (defvar tmm-km-list)
45 (defvar tmm-next-shortcut-digit)
46 (defvar tmm-table-undef)
48 ;;;###autoload (define-key global-map "\M-`" 'tmm-menubar)
49 ;;;###autoload (define-key global-map [f10] 'tmm-menubar)
50 ;;;###autoload (define-key global-map [menu-bar mouse-1] 'tmm-menubar-mouse)
52 ;;;###autoload
53 (defun tmm-menubar (&optional x-position)
54 "Text-mode emulation of looking and choosing from a menubar.
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."
58 (interactive)
59 (run-hooks 'menu-bar-update-hook)
60 ;; Obey menu-bar-final-items; put those items last.
61 (let ((menu-bar (tmm-get-keybind [menu-bar]))
62 menu-bar-item)
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))))
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)))
88 ;;;###autoload
89 (defun tmm-menubar-mouse (event)
90 "Text-mode emulation of looking and choosing from a menubar.
91 This command is used when you click the mouse in the menubar
92 on a console which has no window system but does have a mouse.
93 See the documentation for `tmm-prompt'."
94 (interactive "e")
95 (tmm-menubar (car (posn-x-y (event-start event)))))
97 (defvar tmm-mid-prompt "==>"
98 "*String to insert between shortcut and menu item.
99 If nil, there will be no shortcuts. It should not consist only of spaces,
100 or else the correct item might not be found in the `*Completions*' buffer.")
102 (defvar tmm-mb-map nil
103 "A place to store minibuffer map.")
105 (defvar tmm-completion-prompt
106 "Press PageUp Key to reach this buffer from the minibuffer.
107 Alternatively, you can use Up/Down keys (or your History keys) to change
108 the item in the minibuffer, and press RET when you are done, or press the
109 marked letters to pick up your choice. Type C-g or ESC ESC ESC to cancel.
111 "*Help text to insert on the top of the completion buffer.
112 To save space, you can set this to nil,
113 in which case the standard introduction text is deleted too.")
115 (defvar tmm-shortcut-style '(downcase upcase)
116 "*What letters to use as menu shortcuts.
117 Must be either one of the symbols `downcase' or `upcase',
118 or else a list of the two in the order you prefer.")
120 (defvar tmm-shortcut-words 2
121 "*How many successive words to try for shortcuts, nil means all.
122 If you use only one of `downcase' or `upcase' for `tmm-shortcut-style',
123 specify nil for this variable.")
125 ;;;###autoload
126 (defun tmm-prompt (menu &optional in-popup default-item)
127 "Text-mode emulation of calling the bindings in keymap.
128 Creates a text-mode menu of possible choices. You can access the elements
129 in the menu in two ways:
130 *) via history mechanism from minibuffer;
131 *) Or via completion-buffer that is automatically shown.
132 The last alternative is currently a hack, you cannot use mouse reliably.
134 MENU is like the MENU argument to `x-popup-menu': either a
135 keymap or an alist of alists.
136 DEFAULT-ITEM, if non-nil, specifies an initial default choice.
137 Its 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))))
147 (run-hooks 'activate-menubar-hook)
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.
152 (mapcar (function (lambda (elt)
153 (if (stringp elt)
154 (setq gl-str elt)
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))
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)
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 ))))
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
233 (progn
234 (setq last-command-event chosen-string)
235 (call-interactively choice))
236 choice)))))
238 (defun tmm-add-shortcuts (list)
239 "Adds shortcuts to cars of elements of the list.
240 Takes a list of lists with a string as car, returns list with
241 shortcuts added to these cars.
242 Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
243 (let ((tmm-next-shortcut-digit ?0))
244 (mapcar 'tmm-add-one-shortcut (reverse list))))
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))))
280 ;; This returns the old map.
281 (defun tmm-define-keys (minibuffer)
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))))))
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")))
309 (defun tmm-add-prompt ()
310 (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
311 (make-local-hook 'minibuffer-exit-hook)
312 (add-hook 'minibuffer-exit-hook 'tmm-delete-map nil t)
313 (let ((win (selected-window)))
314 (setq tmm-old-mb-map (tmm-define-keys t))
315 ;; Get window and hide it for electric mode to get correct size
316 (save-window-excursion
317 (let ((completions
318 (mapcar 'car minibuffer-completion-table)))
319 (or tmm-completion-prompt
320 (add-hook 'completion-setup-hook
321 'tmm-completion-delete-prompt 'append))
322 (with-output-to-temp-buffer "*Completions*"
323 (display-completion-list completions))
324 (remove-hook 'completion-setup-hook 'tmm-completion-delete-prompt))
325 (if tmm-completion-prompt
326 (progn
327 (set-buffer "*Completions*")
328 (goto-char 1)
329 (insert tmm-completion-prompt)))
331 (save-selected-window
332 (other-window 1) ; Electric-pop-up-window does
333 ; not work in minibuffer
334 (Electric-pop-up-window "*Completions*")
335 (with-current-buffer "*Completions*"
336 (setq tmm-old-comp-map (tmm-define-keys nil))))
338 (insert tmm-c-prompt)))
340 (defun tmm-delete-map ()
341 (remove-hook 'minibuffer-exit-hook 'tmm-delete-map t)
342 (if tmm-old-mb-map
343 (use-local-map tmm-old-mb-map)))
345 (defun tmm-shortcut ()
346 "Choose the shortcut that the user typed."
347 (interactive)
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)
352 (if (equal (buffer-name) "*Completions*")
353 (progn
354 (beginning-of-buffer)
355 (re-search-forward
356 (concat "\\(^\\|[ \t]\\)" (char-to-string c) tmm-mid-prompt))
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))))
364 (concat (char-to-string c) tmm-mid-prompt))
365 (setq s (car elt))))
366 tmm-km-list)
367 (insert s)
368 (exit-minibuffer)))))
370 (defun tmm-goto-completions ()
371 (interactive)
372 (setq tmm-c-prompt (buffer-string))
373 (erase-buffer)
374 (switch-to-buffer-other-window "*Completions*")
375 (search-forward tmm-c-prompt)
376 (search-backward tmm-c-prompt))
378 (defun tmm-get-keymap (elt &optional in-x-menu)
379 "Prepends (DOCSTRING EVENT BINDING) to free variable `tmm-km-list'.
380 The values are deduced from the argument ELT, that should be an
381 element of keymap, an `x-popup-menu' argument, or an element of
382 `x-popup-menu' argument (when IN-X-MENU is not-nil).
383 This function adds the element only if it is not already present.
384 It uses the free variable `tmm-table-undef' to keep undefined keys."
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))
390 (assoc event tmm-table-undef)
391 (and (if (listp elt)
392 (or (keymapp elt) (eq (car elt) 'lambda))
393 (fboundp elt))
394 (setq km elt))
395 (and (if (listp (cdr-safe elt))
396 (or (keymapp (cdr-safe elt))
397 (eq (car (cdr-safe elt)) 'lambda))
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)))
402 (or (keymapp (cdr-safe (cdr-safe elt)))
403 (eq (car (cdr-safe (cdr-safe elt))) 'lambda))
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))))
412 (or (keymapp (cdr-safe (cdr-safe (cdr-safe elt))))
413 (eq (car (cdr-safe (cdr-safe (cdr-safe elt)))) 'lambda))
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))
428 (and km (stringp km) (setq str km))
429 (and km str
430 (or (assoc str tmm-km-list)
431 (setq tmm-km-list
432 (cons (cons str (cons event km)) tmm-km-list)))
433 ))))
435 (defun tmm-get-keybind (keyseq)
436 "Return the current binding of KEYSEQ, merging prefix definitions.
437 If KEYSEQ is a prefix key that has local and global bindings,
438 we merge them into a single keymap which shows the proper order of the menu.
439 However, for the menu bar itself, the value does not take account
440 of `menu-bar-final-items'."
441 (let (allbind bind)
442 (setq bind (key-binding keyseq))
443 ;; If KEYSEQ is a prefix key, then BIND is either nil
444 ;; or a symbol defined as a keymap (which satisfies keymapp).
445 (if (keymapp bind)
446 (setq bind nil))
447 ;; If we have a non-keymap definition, return that.
448 (or bind
449 (progn
450 ;; Otherwise, it is a prefix, so make a list of the subcommands.
451 ;; Make a list of all the bindings in all the keymaps.
452 (setq allbind (mapcar 'cdr (minor-mode-key-binding keyseq)))
453 (setq allbind (cons (local-key-binding keyseq) allbind))
454 (setq allbind (cons (global-key-binding keyseq) allbind))
455 ;; Merge all the elements of ALLBIND into one keymap.
456 (mapcar (lambda (in)
457 (if (and (symbolp in) (keymapp in))
458 (setq in (symbol-function in)))
459 (and in (keymapp in)
460 (if (keymapp bind)
461 (setq bind (nconc bind (copy-sequence (cdr in))))
462 (setq bind (copy-sequence in)))))
463 allbind)
464 ;; Return that keymap.
465 bind))))
467 (add-hook 'calendar-load-hook (lambda () (require 'cal-menu)))
469 (provide 'tmm)
471 ;;; tmm.el ends here