Change all references from point to PT.
[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)
45(defvar tmm-table-undef)
46
e6a5c7de 47;;;###autoload (define-key global-map "\M-`" 'tmm-menubar)
b46324e6 48;;;###autoload (define-key global-map [f10] 'tmm-menubar)
77cc5db0 49;;;###autoload (define-key global-map [menu-bar mouse-1] 'tmm-menubar-mouse)
afb1835e 50
20062d6b 51;;;###autoload
77cc5db0 52(defun tmm-menubar (&optional x-position)
20062d6b 53 "Text-mode emulation of looking and choosing from a menubar.
77cc5db0
RS
54See the documentation for `tmm-prompt'.
55X-POSITION, if non-nil, specifies a horizontal position within the menu bar;
56we make that menu bar item (the one at that position) the default choice."
20062d6b
RS
57 (interactive)
58 (run-hooks 'menu-bar-update-hook)
fc225f66 59 ;; Obey menu-bar-final-items; put those items last.
77cc5db0
RS
60 (let ((menu-bar (tmm-get-keybind [menu-bar]))
61 menu-bar-item)
fc225f66
RS
62 (let ((list menu-bar-final-items))
63 (while list
64 (let ((item (car list)))
65 ;; ITEM is the name of an item that we want to put last.
66 ;; Find it in MENU-BAR and move it to the end.
67 (let ((this-one (assq item menu-bar)))
68 (setq menu-bar (append (delq this-one menu-bar)
69 (list this-one)))))
70 (setq list (cdr list))))
77cc5db0
RS
71 (if x-position
72 (let ((tail menu-bar)
73 this-one
74 (column 0))
75 (while (and tail (< column x-position))
76 (setq this-one (car tail))
77 (if (and (consp (car tail))
78 (consp (cdr (car tail)))
79 (stringp (nth 1 (car tail))))
80 (setq column (+ column
81 (length (nth 1 (car tail)))
82 1)))
83 (setq tail (cdr tail)))
84 (setq menu-bar-item (car this-one))))
85 (tmm-prompt menu-bar nil menu-bar-item)))
86
8e735883 87;;;###autoload
77cc5db0
RS
88(defun tmm-menubar-mouse (event)
89 "Text-mode emulation of looking and choosing from a menubar.
90This command is used when you click the mouse in the menubar
91on a console which has no window system but does have a mouse.
92See the documentation for `tmm-prompt'."
93 (interactive "e")
94 (tmm-menubar (car (posn-x-y (event-start event)))))
20062d6b
RS
95
96(defvar tmm-mid-prompt "==>"
97 "String to insert between shortcut and menu item or nil.")
98
99(defvar tmm-mb-map nil
100 "A place to store minibuffer map.")
101
102(defvar tmm-completion-prompt
103 "Press PageUp Key to reach this buffer from the minibuffer.
104Alternatively, you can use Up/Down keys (or your History keys) to change
105the item in the minibuffer, and press RET when you are done, or press the
10fe2d38 106marked letters to pick up your choice. Type C-g or ESC ESC ESC to cancel.
20062d6b 107"
9f80907a
RS
108 "String to insert at top of completion buffer.
109If this is nil, delete even the usual help text
110and show just the alternatives.")
20062d6b
RS
111
112;;;###autoload
bdbc7685 113(defun tmm-prompt (menu &optional in-popup default-item)
20062d6b 114 "Text-mode emulation of calling the bindings in keymap.
77cc5db0
RS
115Creates a text-mode menu of possible choices. You can access the elements
116in the menu in two ways:
117 *) via history mechanism from minibuffer;
20062d6b
RS
118 *) Or via completion-buffer that is automatically shown.
119The last alternative is currently a hack, you cannot use mouse reliably.
bdbc7685
RS
120
121MENU is like the MENU argument to `x-popup-menu': either a
122keymap or an alist of alists.
123DEFAULT-ITEM, if non-nil, specifies an initial default choice.
124Its value should be an event that has a binding in MENU."
125 ;; If the optional argument IN-POPUP is t,
126 ;; then MENU is an alist of elements of the form (STRING . VALUE).
127 ;; That is used for recursive calls only.
128 (let ((gl-str "Menu bar") ;; The menu bar itself is not a menu keymap
129 ; so it doesn't have a name.
130 tmm-km-list out history history-len tmm-table-undef tmm-c-prompt
131 tmm-old-mb-map tmm-old-comp-map tmm-short-cuts
132 chosen-string choice
133 (not-menu (not (keymapp menu))))
20062d6b 134 (run-hooks 'activate-menubar-hook)
bdbc7685
RS
135 ;; Compute tmm-km-list from MENU.
136 ;; tmm-km-list is an alist of (STRING . MEANING).
137 ;; It has no other elements.
138 ;; The order of elements in tmm-km-list is the order of the menu bar.
20062d6b
RS
139 (mapcar (function (lambda (elt)
140 (if (stringp elt)
141 (setq gl-str elt)
bdbc7685
RS
142 (and (listp elt) (tmm-get-keymap elt not-menu)))))
143 menu)
144 ;; Choose an element of tmm-km-list; put it in choice.
145 (if (and not-menu (= 1 (length tmm-km-list)))
146 ;; If this is the top-level of an x-popup-menu menu,
147 ;; and there is just one pane, choose that one silently.
148 ;; This way we only ask the user one question,
149 ;; for which element of that pane.
150 (setq choice (cdr (car tmm-km-list)))
151 (and tmm-km-list
152 (let ((index-of-default 0))
153 (if tmm-mid-prompt
154 (setq tmm-km-list (tmm-add-shortcuts tmm-km-list))
155 t)
156 ;; Find the default item's index within the menu bar.
157 ;; We use this to decide the initial minibuffer contents
158 ;; and initial history position.
159 (if default-item
160 (let ((tail menu))
161 (while (and tail
162 (not (eq (car-safe (car tail)) default-item)))
163 ;; Be careful to count only the elements of MENU
164 ;; that actually constitute menu bar items.
165 (if (and (consp (car tail))
166 (stringp (car-safe (cdr (car tail)))))
167 (setq index-of-default (1+ index-of-default)))
168 (setq tail (cdr tail)))))
169 (setq history (reverse (mapcar 'car tmm-km-list)))
170 (setq history-len (length history))
171 (setq history (append history history history history))
172 (setq tmm-c-prompt (nth (- history-len 1 index-of-default) history))
173 (add-hook 'minibuffer-setup-hook 'tmm-add-prompt)
174 (unwind-protect
175 (setq out
176 (completing-read
177 (concat gl-str " (up/down to change, PgUp to menu): ")
178 tmm-km-list nil t nil
179 (cons 'history (- (* 2 history-len) index-of-default))))
180 (save-excursion
181 (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
182 (if (get-buffer "*Completions*")
183 (progn
184 (set-buffer "*Completions*")
185 (use-local-map tmm-old-comp-map)
186 (bury-buffer (current-buffer)))))
187 )))
188 (setq choice (cdr (assoc out tmm-km-list)))
189 (and (null choice)
190 (> (length out) (length tmm-c-prompt))
191 (string= (substring out 0 (length tmm-c-prompt)) tmm-c-prompt)
192 (setq out (substring out (length tmm-c-prompt))
193 choice (cdr (assoc out tmm-km-list))))
194 (and (null choice)
195 (setq out (try-completion out tmm-km-list)
196 choice (cdr (assoc out tmm-km-list)))))
197 ;; CHOICE is now (STRING . MEANING). Separate the two parts.
198 (setq chosen-string (car choice))
199 (setq choice (cdr choice))
200 (cond (in-popup
201 ;; We just did the inner level of a -popup menu.
202 choice)
203 ;; We just did the outer level. Do the inner level now.
204 (not-menu (tmm-prompt choice t))
205 ;; We just handled a menu keymap and found another keymap.
206 ((keymapp choice)
207 (if (symbolp choice)
208 (setq choice (indirect-function choice)))
209 (condition-case nil
210 (require 'mouse)
211 (error nil))
212 (condition-case nil
213 (x-popup-menu nil choice) ; Get the shortcuts
214 (error nil))
215 (tmm-prompt choice))
216 ;; We just handled a menu keymap and found a command.
217 (choice
218 (if chosen-string
3132f319
KH
219 (progn
220 (setq last-command-event chosen-string)
221 (call-interactively choice))
bdbc7685 222 choice)))))
20062d6b 223
20062d6b
RS
224
225(defun tmm-add-shortcuts (list)
226 "Adds shortcuts to cars of elements of the list.
227Takes a list of lists with a string as car, returns list with
fc225f66
RS
228shortcuts added to these cars.
229Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
230 (let ((next-shortcut-number 0))
231 (mapcar (lambda (elt)
232 (let ((str (car elt)) f b)
233 (setq f (upcase (substring str 0 1)))
234 ;; If does not work, try beginning of the other word
235 (if (and (member f tmm-short-cuts)
236 (string-match " \\([^ ]\\)" str))
237 (setq f (upcase (substring
238 str
239 (setq b (match-beginning 1)) (1+ b)))))
240 ;; If we don't have an unique letter shortcut,
241 ;; pick a digit as a shortcut instead.
242 (if (member f tmm-short-cuts)
243 (if (< next-shortcut-number 10)
244 (setq f (format "%d" next-shortcut-number)
245 next-shortcut-number (1+ next-shortcut-number))
246 (setq f nil)))
247 (if (null f)
248 elt
249 (setq tmm-short-cuts (cons f tmm-short-cuts))
250 (cons (concat f tmm-mid-prompt str) (cdr elt)))))
251 (reverse list))))
20062d6b 252
fe03654a 253(defun tmm-define-keys (minibuffer)
b46324e6
RS
254 (mapcar (lambda (str)
255 (define-key (current-local-map) str 'tmm-shortcut)
256 (define-key (current-local-map) (downcase str) 'tmm-shortcut))
257 tmm-short-cuts)
fe03654a
RS
258 (if minibuffer
259 (progn
260 (define-key (current-local-map) [pageup] 'tmm-goto-completions)
261 (define-key (current-local-map) [prior] 'tmm-goto-completions)
262 (define-key (current-local-map) "\ev" 'tmm-goto-completions)
263 (define-key (current-local-map) "\C-n" 'next-history-element)
264 (define-key (current-local-map) "\C-p" 'previous-history-element))))
b46324e6 265
20062d6b
RS
266(defun tmm-add-prompt ()
267 (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
afb1835e
RS
268 (make-local-hook 'minibuffer-exit-hook)
269 (add-hook 'minibuffer-exit-hook 'tmm-delete-map nil t)
b46324e6 270 (let ((win (selected-window)))
20062d6b 271 (setq tmm-old-mb-map (current-local-map))
b46324e6 272 (use-local-map (append (make-sparse-keymap) tmm-old-mb-map))
fe03654a 273 (tmm-define-keys t)
20062d6b
RS
274 ;; Get window and hide it for electric mode to get correct size
275 (save-window-excursion
fc225f66
RS
276 (let ((completions
277 (mapcar 'car minibuffer-completion-table)))
278 (with-output-to-temp-buffer "*Completions*"
279 (display-completion-list completions)))
20062d6b
RS
280 (set-buffer "*Completions*")
281 (goto-char 1)
9f80907a
RS
282 (if tmm-completion-prompt
283 (insert tmm-completion-prompt)
284 ;; Delete even the usual help info that all completion buffers have.
285 (goto-char 1)
286 (delete-region 1 (search-forward "Possible completions are:\n")))
20062d6b
RS
287 )
288 (save-excursion
289 (other-window 1) ; Electric-pop-up-window does
290 ; not work in minibuffer
291 (set-buffer (window-buffer (Electric-pop-up-window "*Completions*")))
292 (setq tmm-old-comp-map (current-local-map))
b46324e6 293 (use-local-map (append (make-sparse-keymap) tmm-old-comp-map))
fe03654a 294 (tmm-define-keys nil)
20062d6b
RS
295 (select-window win) ; Cannot use
296 ; save-window-excursion, since
297 ; it restores the size
298 )
299 (insert tmm-c-prompt)))
300
301(defun tmm-delete-map ()
afb1835e 302 (remove-hook 'minibuffer-exit-hook 'tmm-delete-map t)
fc225f66
RS
303 (if tmm-old-mb-map
304 (use-local-map tmm-old-mb-map)))
20062d6b
RS
305
306(defun tmm-shortcut ()
fc225f66 307 "Choose the shortcut that the user typed."
20062d6b
RS
308 (interactive)
309 (let ((c (upcase (char-to-string last-command-char))) s)
310 (if (member c tmm-short-cuts)
fc225f66
RS
311 (if (equal (buffer-name) "*Completions*")
312 (progn
313 (beginning-of-buffer)
314 (re-search-forward
315 (concat "\\(^\\|[ \t]\\)" c tmm-mid-prompt))
316 (choose-completion))
317 (erase-buffer) ; In minibuffer
318 (mapcar (lambda (elt)
319 (if (string=
320 (substring (car elt) 0
321 (min (1+ (length tmm-mid-prompt))
322 (length (car elt))))
323 (concat c tmm-mid-prompt))
324 (setq s (car elt))))
325 tmm-km-list)
326 (insert s)
327 (exit-minibuffer)))))
20062d6b
RS
328
329(defun tmm-goto-completions ()
330 (interactive)
331 (setq tmm-c-prompt (buffer-string))
332 (erase-buffer)
fc225f66 333 (switch-to-buffer-other-window "*Completions*")
20062d6b
RS
334 (search-forward tmm-c-prompt)
335 (search-backward tmm-c-prompt))
336
337
338(defun tmm-get-keymap (elt &optional in-x-menu)
339 "Prepends (DOCSTRING EVENT BINDING) to free variable `tmm-km-list'.
340The values are deduced from the argument ELT, that should be an
fc225f66 341element of keymap, an `x-popup-menu' argument, or an element of
20062d6b 342`x-popup-menu' argument (when IN-X-MENU is not-nil).
77cc5db0
RS
343This function adds the element only if it is not already present.
344It uses the free variable `tmm-table-undef' to keep undefined keys."
20062d6b
RS
345 (let (km str cache (event (car elt)))
346 (setq elt (cdr elt))
347 (if (eq elt 'undefined)
348 (setq tmm-table-undef (cons (cons event nil) tmm-table-undef))
349 (or
350 (assoc event tmm-table-undef)
351 (and (if (listp elt)
352 (keymapp elt)
353 (fboundp elt))
354 (setq km elt))
355 (and (if (listp (cdr-safe elt))
356 (keymapp (cdr-safe elt))
357 (fboundp (cdr-safe elt)))
358 (setq km (cdr elt))
359 (and (stringp (car elt)) (setq str (car elt))))
360 (and (if (listp (cdr-safe (cdr-safe elt)))
361 (keymapp (cdr-safe (cdr-safe elt)))
362 (fboundp (cdr-safe (cdr-safe elt))))
363 (setq km (cdr (cdr elt)))
364 (and (stringp (car elt)) (setq str (car elt)))
365 (or (and str
366 (stringp (cdr (car (cdr elt)))) ; keyseq cache
367 (setq cache (cdr (car (cdr elt))))
368 cache (setq str (concat str cache))) str))
369 (and (if (listp (cdr-safe (cdr-safe (cdr-safe elt))))
370 (keymapp (cdr-safe (cdr-safe (cdr-safe elt))))
371 (fboundp (cdr-safe (cdr-safe (cdr-safe elt)))))
372 ; New style of easy-menu
373 (setq km (cdr (cdr (cdr elt))))
374 (and (stringp (car elt)) (setq str (car elt)))
375 (or (and str
376 (stringp (cdr (car (cdr (cdr elt))))) ; keyseq cache
377 (setq cache (cdr (car (cdr (cdr elt)))))
378 cache (setq str (concat str cache)))
379 str))
380 (and (stringp event) ; x-popup or x-popup element
381 (if (or in-x-menu (stringp (car-safe elt)))
382 (setq str event event nil km elt)
383 (setq str event event nil km (cons 'keymap elt))
384 )))
385 (and km (stringp km) (setq str km))
386 (and km str
387 (or (assoc str tmm-km-list)
388 (setq tmm-km-list
389 (cons (cons str (cons event km)) tmm-km-list)))
390 ))))
391
392
393(defun tmm-get-keybind (keyseq)
fc225f66 394 "Return the current binding of KEYSEQ, merging prefix definitions.
91a5e367 395If KEYSEQ is a prefix key that has local and global bindings,
fc225f66
RS
396we merge them into a single keymap which shows the proper order of the menu.
397However, for the menu bar itself, the value does not take account
398of `menu-bar-final-items'."
20062d6b 399 (let (allbind bind)
fc225f66
RS
400 (setq bind (key-binding keyseq))
401 ;; If KEYSEQ is a prefix key, then BIND is either nil
402 ;; or a symbol defined as a keymap (which satisfies keymapp).
403 (if (keymapp bind)
404 (setq bind nil))
405 ;; If we have a non-keymap definition, return that.
406 (or bind
407 (progn
408 ;; Otherwise, it is a prefix, so make a list of the subcommands.
409 ;; Make a list of all the bindings in all the keymaps.
410 (setq allbind (mapcar 'cdr (minor-mode-key-binding keyseq)))
411 (setq allbind (cons (local-key-binding keyseq) allbind))
412 (setq allbind (cons (global-key-binding keyseq) allbind))
413 ;; Merge all the elements of ALLBIND into one keymap.
414 (mapcar (lambda (in)
415 (if (and (symbolp in) (keymapp in))
416 (setq in (symbol-function in)))
417 (and in (keymapp in)
418 (if (keymapp bind)
419 (setq bind (nconc bind (copy-sequence (cdr in))))
420 (setq bind (copy-sequence in)))))
421 allbind)
422 ;; Return that keymap.
423 bind))))
20062d6b
RS
424
425(add-hook 'calendar-load-hook (lambda () (require 'cal-menu)))
426
20062d6b
RS
427(provide 'tmm)
428
20062d6b 429;;; tmm.el ends here