(tmm-add-one-shortcut): New subroutine.
[emacs.git] / lisp / tmm.el
blobb016e8231bf103202ddc213e70ccd42f56d705c3
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 (unwind-protect
188 (setq out
189 (completing-read
190 (concat gl-str " (up/down to change, PgUp to menu): ")
191 tmm-km-list nil t nil
192 (cons 'history (- (* 2 history-len) index-of-default))))
193 (save-excursion
194 (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
195 (if (get-buffer "*Completions*")
196 (progn
197 (set-buffer "*Completions*")
198 (use-local-map tmm-old-comp-map)
199 (bury-buffer (current-buffer)))))
201 (setq choice (cdr (assoc out tmm-km-list)))
202 (and (null choice)
203 (> (length out) (length tmm-c-prompt))
204 (string= (substring out 0 (length tmm-c-prompt)) tmm-c-prompt)
205 (setq out (substring out (length tmm-c-prompt))
206 choice (cdr (assoc out tmm-km-list))))
207 (and (null choice)
208 (setq out (try-completion out tmm-km-list)
209 choice (cdr (assoc out tmm-km-list)))))
210 ;; CHOICE is now (STRING . MEANING). Separate the two parts.
211 (setq chosen-string (car choice))
212 (setq choice (cdr choice))
213 (cond (in-popup
214 ;; We just did the inner level of a -popup menu.
215 choice)
216 ;; We just did the outer level. Do the inner level now.
217 (not-menu (tmm-prompt choice t))
218 ;; We just handled a menu keymap and found another keymap.
219 ((keymapp choice)
220 (if (symbolp choice)
221 (setq choice (indirect-function choice)))
222 (condition-case nil
223 (require 'mouse)
224 (error nil))
225 (condition-case nil
226 (x-popup-menu nil choice) ; Get the shortcuts
227 (error nil))
228 (tmm-prompt choice))
229 ;; We just handled a menu keymap and found a command.
230 (choice
231 (if chosen-string
232 (progn
233 (setq last-command-event chosen-string)
234 (call-interactively choice))
235 choice)))))
237 (defun tmm-add-shortcuts (list)
238 "Adds shortcuts to cars of elements of the list.
239 Takes a list of lists with a string as car, returns list with
240 shortcuts added to these cars.
241 Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
242 (let ((tmm-next-shortcut-digit ?0))
243 (mapcar 'tmm-add-one-shortcut (reverse list))))
245 (defsubst tmm-add-one-shortcut (elt)
246 ;; uses the free vars tmm-next-shortcut-digit and tmm-short-cuts
247 (let* ((str (car elt))
248 (paren (string-match "(" str))
249 (pos 0) (word 0) char)
250 (catch 'done ; ??? is this slow?
251 (while (and (or (not tmm-shortcut-words) ; no limit on words
252 (< word tmm-shortcut-words)) ; try n words
253 (setq pos (string-match "\\w+" str pos)) ; get next word
254 (not (and paren (> pos paren)))) ; don't go past "(binding.."
255 (if (or (= pos 0)
256 (/= (aref str (1- pos)) ?.)) ; avoid file extensions
257 (let ((shortcut-style
258 (if (listp tmm-shortcut-style) ; convert to list
259 tmm-shortcut-style
260 (list tmm-shortcut-style))))
261 (while shortcut-style ; try upcase and downcase variants
262 (setq char (funcall (car shortcut-style) (aref str pos)))
263 (if (not (memq char tmm-short-cuts)) (throw 'done char))
264 (setq shortcut-style (cdr shortcut-style)))))
265 (setq word (1+ word))
266 (setq pos (match-end 0)))
267 (while (<= tmm-next-shortcut-digit ?9) ; no letter shortcut, pick a digit
268 (setq char tmm-next-shortcut-digit)
269 (setq tmm-next-shortcut-digit (1+ tmm-next-shortcut-digit))
270 (if (not (memq char tmm-short-cuts)) (throw 'done char)))
271 (setq char nil))
272 (if char (setq tmm-short-cuts (cons char tmm-short-cuts)))
273 (cons (concat (if char (concat (char-to-string char) tmm-mid-prompt)
274 ;; keep them lined up in columns
275 (make-string (1+ (length tmm-mid-prompt)) ?\ ))
276 str)
277 (cdr elt))))
279 ;; This returns the old map.
280 (defun tmm-define-keys (minibuffer)
281 (let ((map (make-sparse-keymap)))
282 (suppress-keymap map t)
283 (mapcar
284 (function
285 (lambda (c)
286 (if (listp tmm-shortcut-style)
287 (define-key map (char-to-string c) 'tmm-shortcut)
288 ;; only one kind of letters are shortcuts, so map both upcase and
289 ;; downcase input to the same
290 (define-key map (char-to-string (downcase c)) 'tmm-shortcut)
291 (define-key map (char-to-string (upcase c)) 'tmm-shortcut))))
292 tmm-short-cuts)
293 (if minibuffer
294 (progn
295 (define-key map [pageup] 'tmm-goto-completions)
296 (define-key map [prior] 'tmm-goto-completions)
297 (define-key map "\ev" 'tmm-goto-completions)
298 (define-key map "\C-n" 'next-history-element)
299 (define-key map "\C-p" 'previous-history-element)))
300 (prog1 (current-local-map)
301 (use-local-map (append map (current-local-map))))))
303 (defun tmm-completion-delete-prompt ()
304 (set-buffer standard-output)
305 (goto-char 1)
306 (delete-region 1 (search-forward "Possible completions are:\n")))
308 (defun tmm-add-prompt ()
309 (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
310 (make-local-hook 'minibuffer-exit-hook)
311 (add-hook 'minibuffer-exit-hook 'tmm-delete-map nil t)
312 (let ((win (selected-window)))
313 (setq tmm-old-mb-map (tmm-define-keys t))
314 ;; Get window and hide it for electric mode to get correct size
315 (save-window-excursion
316 (let ((completions
317 (mapcar 'car minibuffer-completion-table)))
318 (or tmm-completion-prompt
319 (add-hook 'completion-setup-hook
320 'tmm-completion-delete-prompt 'append))
321 (with-output-to-temp-buffer "*Completions*"
322 (display-completion-list completions))
323 (remove-hook 'completion-setup-hook 'tmm-completion-delete-prompt))
324 (if tmm-completion-prompt
325 (progn
326 (set-buffer "*Completions*")
327 (goto-char 1)
328 (insert tmm-completion-prompt)))
330 (save-excursion
331 (other-window 1) ; Electric-pop-up-window does
332 ; not work in minibuffer
333 (set-buffer (window-buffer (Electric-pop-up-window "*Completions*")))
335 (setq tmm-old-comp-map (tmm-define-keys nil))
337 (select-window win) ; Cannot use
338 ; save-window-excursion, since
339 ; it restores the size
341 (insert tmm-c-prompt)))
343 (defun tmm-delete-map ()
344 (remove-hook 'minibuffer-exit-hook 'tmm-delete-map t)
345 (if tmm-old-mb-map
346 (use-local-map tmm-old-mb-map)))
348 (defun tmm-shortcut ()
349 "Choose the shortcut that the user typed."
350 (interactive)
351 (let ((c last-command-char) s)
352 (if (symbolp tmm-shortcut-style)
353 (setq c (funcall tmm-shortcut-style c)))
354 (if (memq c tmm-short-cuts)
355 (if (equal (buffer-name) "*Completions*")
356 (progn
357 (beginning-of-buffer)
358 (re-search-forward
359 (concat "\\(^\\|[ \t]\\)" (char-to-string c) tmm-mid-prompt))
360 (choose-completion))
361 (erase-buffer) ; In minibuffer
362 (mapcar (lambda (elt)
363 (if (string=
364 (substring (car elt) 0
365 (min (1+ (length tmm-mid-prompt))
366 (length (car elt))))
367 (concat (char-to-string c) tmm-mid-prompt))
368 (setq s (car elt))))
369 tmm-km-list)
370 (insert s)
371 (exit-minibuffer)))))
373 (defun tmm-goto-completions ()
374 (interactive)
375 (setq tmm-c-prompt (buffer-string))
376 (erase-buffer)
377 (switch-to-buffer-other-window "*Completions*")
378 (search-forward tmm-c-prompt)
379 (search-backward tmm-c-prompt))
381 (defun tmm-get-keymap (elt &optional in-x-menu)
382 "Prepends (DOCSTRING EVENT BINDING) to free variable `tmm-km-list'.
383 The values are deduced from the argument ELT, that should be an
384 element of keymap, an `x-popup-menu' argument, or an element of
385 `x-popup-menu' argument (when IN-X-MENU is not-nil).
386 This function adds the element only if it is not already present.
387 It uses the free variable `tmm-table-undef' to keep undefined keys."
388 (let (km str cache (event (car elt)))
389 (setq elt (cdr elt))
390 (if (eq elt 'undefined)
391 (setq tmm-table-undef (cons (cons event nil) tmm-table-undef))
393 (assoc event tmm-table-undef)
394 (and (if (listp elt)
395 (keymapp elt)
396 (fboundp elt))
397 (setq km elt))
398 (and (if (listp (cdr-safe elt))
399 (keymapp (cdr-safe elt))
400 (fboundp (cdr-safe elt)))
401 (setq km (cdr elt))
402 (and (stringp (car elt)) (setq str (car elt))))
403 (and (if (listp (cdr-safe (cdr-safe elt)))
404 (keymapp (cdr-safe (cdr-safe elt)))
405 (fboundp (cdr-safe (cdr-safe elt))))
406 (setq km (cdr (cdr elt)))
407 (and (stringp (car elt)) (setq str (car elt)))
408 (or (and str
409 (stringp (cdr (car (cdr elt)))) ; keyseq cache
410 (setq cache (cdr (car (cdr elt))))
411 cache (setq str (concat str cache))) str))
412 (and (if (listp (cdr-safe (cdr-safe (cdr-safe elt))))
413 (keymapp (cdr-safe (cdr-safe (cdr-safe elt))))
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