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