Set xterm click count to 1 even with no last click
[emacs.git] / lisp / tmm.el
blobca6a37d62b2067fa5367e71e232d2ea706666b6c
1 ;;; tmm.el --- text mode access to menu-bar -*- lexical-binding: t -*-
3 ;; Copyright (C) 1994-1996, 2000-2017 Free Software Foundation, Inc.
5 ;; Author: Ilya Zakharevich <ilya@math.mps.ohio-state.edu>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: convenience
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This package provides text mode access to the menu bar.
28 ;;; Code:
30 (require 'electric)
32 (defgroup tmm nil
33 "Text mode access to menu-bar."
34 :prefix "tmm-"
35 :group 'menu)
37 ;;; The following will be localized, added only to pacify the compiler.
38 (defvar tmm-short-cuts)
39 (defvar tmm-old-mb-map nil)
40 (defvar tmm-c-prompt nil)
41 (defvar tmm-km-list)
42 (defvar tmm-next-shortcut-digit)
43 (defvar tmm-table-undef)
45 ;;;###autoload (define-key global-map "\M-`" 'tmm-menubar)
46 ;;;###autoload (define-key global-map [menu-bar mouse-1] 'tmm-menubar-mouse)
48 ;;;###autoload
49 (defun tmm-menubar (&optional x-position)
50 "Text-mode emulation of looking and choosing from a menubar.
51 See the documentation for `tmm-prompt'.
52 X-POSITION, if non-nil, specifies a horizontal position within the menu bar;
53 we make that menu bar item (the one at that position) the default choice.
55 Note that \\[menu-bar-open] by default drops down TTY menus; if you want it
56 to invoke `tmm-menubar' instead, customize the variable
57 `tty-menu-open-use-tmm' to a non-nil value."
58 (interactive)
59 (run-hooks 'menu-bar-update-hook)
60 ;; Obey menu-bar-final-items; put those items last.
61 (let ((menu-bar '())
62 (menu-end '())
63 menu-bar-item)
64 (map-keymap
65 (lambda (key binding)
66 (push (cons key binding)
67 ;; If KEY is the name of an item that we want to put last,
68 ;; move it to the end.
69 (if (memq key menu-bar-final-items)
70 menu-end
71 menu-bar)))
72 (tmm-get-keybind [menu-bar]))
73 (setq menu-bar `(keymap ,@(nreverse menu-bar) ,@(nreverse menu-end)))
74 (if x-position
75 (let ((column 0)
76 prev-key)
77 (catch 'done
78 (map-keymap
79 (lambda (key binding)
80 (when (> column x-position)
81 (setq menu-bar-item prev-key)
82 (throw 'done nil))
83 (setq prev-key key)
84 (pcase binding
85 ((or `(,(and (pred stringp) name) . ,_) ;Simple menu item.
86 `(menu-item ,name ,_cmd ;Extended menu item.
87 . ,(and props
88 (guard (let ((visible
89 (plist-get props :visible)))
90 (or (null visible)
91 (eval visible)))))))
92 (setq column (+ column (length name) 1)))))
93 menu-bar))))
94 (tmm-prompt menu-bar nil menu-bar-item)))
96 ;;;###autoload
97 (defun tmm-menubar-mouse (event)
98 "Text-mode emulation of looking and choosing from a menubar.
99 This command is used when you click the mouse in the menubar
100 on a console which has no window system but does have a mouse.
101 See the documentation for `tmm-prompt'."
102 (interactive "e")
103 (tmm-menubar (car (posn-x-y (event-start event)))))
105 (defcustom tmm-mid-prompt "==>"
106 "String to insert between shortcut and menu item.
107 If nil, there will be no shortcuts. It should not consist only of spaces,
108 or else the correct item might not be found in the `*Completions*' buffer."
109 :type 'string
110 :group 'tmm)
112 (defvar tmm-mb-map nil
113 "A place to store minibuffer map.")
115 (defcustom tmm-completion-prompt
116 "Press PageUp key to reach this buffer from the minibuffer.
117 Alternatively, you can use Up/Down keys (or your History keys) to change
118 the item in the minibuffer, and press RET when you are done, or press the
119 marked letters to pick up your choice. Type C-g or ESC ESC ESC to cancel.
121 "Help text to insert on the top of the completion buffer.
122 To save space, you can set this to nil,
123 in which case the standard introduction text is deleted too."
124 :type '(choice string (const nil))
125 :group 'tmm)
127 (defcustom tmm-shortcut-style '(downcase upcase)
128 "What letters to use as menu shortcuts.
129 Must be either one of the symbols `downcase' or `upcase',
130 or else a list of the two in the order you prefer."
131 :type '(choice (const downcase)
132 (const upcase)
133 (repeat (choice (const downcase) (const upcase))))
134 :group 'tmm)
136 (defcustom tmm-shortcut-words 2
137 "How many successive words to try for shortcuts, nil means all.
138 If you use only one of `downcase' or `upcase' for `tmm-shortcut-style',
139 specify nil for this variable."
140 :type '(choice integer (const nil))
141 :group 'tmm)
143 (defface tmm-inactive
144 '((t :inherit shadow))
145 "Face used for inactive menu items."
146 :group 'tmm)
148 (defun tmm--completion-table (items)
149 (lambda (string pred action)
150 (if (eq action 'metadata)
151 '(metadata (display-sort-function . identity))
152 (complete-with-action action items string pred))))
154 (defvar tmm--history nil)
156 ;;;###autoload
157 (defun tmm-prompt (menu &optional in-popup default-item)
158 "Text-mode emulation of calling the bindings in keymap.
159 Creates a text-mode menu of possible choices. You can access the elements
160 in the menu in two ways:
161 *) via history mechanism from minibuffer;
162 *) Or via completion-buffer that is automatically shown.
163 The last alternative is currently a hack, you cannot use mouse reliably.
165 MENU is like the MENU argument to `x-popup-menu': either a
166 keymap or an alist of alists.
167 DEFAULT-ITEM, if non-nil, specifies an initial default choice.
168 Its value should be an event that has a binding in MENU."
169 ;; If the optional argument IN-POPUP is t,
170 ;; then MENU is an alist of elements of the form (STRING . VALUE).
171 ;; That is used for recursive calls only.
172 (let ((gl-str "Menu bar") ;; The menu bar itself is not a menu keymap
173 ; so it doesn't have a name.
174 tmm-km-list out history-len tmm-table-undef tmm-c-prompt
175 tmm-old-mb-map tmm-short-cuts
176 chosen-string choice
177 (not-menu (not (keymapp menu))))
178 (run-hooks 'activate-menubar-hook)
179 ;; Compute tmm-km-list from MENU.
180 ;; tmm-km-list is an alist of (STRING . MEANING).
181 ;; It has no other elements.
182 ;; The order of elements in tmm-km-list is the order of the menu bar.
183 (if (not not-menu)
184 (map-keymap (lambda (k v) (tmm-get-keymap (cons k v))) menu)
185 (dolist (elt menu)
186 (cond
187 ((stringp elt) (setq gl-str elt))
188 ((listp elt) (tmm-get-keymap elt not-menu))
189 ((vectorp elt)
190 (dotimes (i (length elt))
191 (tmm-get-keymap (cons i (aref elt i)) not-menu))))))
192 ;; Choose an element of tmm-km-list; put it in choice.
193 (if (and not-menu (= 1 (length tmm-km-list)))
194 ;; If this is the top-level of an x-popup-menu menu,
195 ;; and there is just one pane, choose that one silently.
196 ;; This way we only ask the user one question,
197 ;; for which element of that pane.
198 (setq choice (cdr (car tmm-km-list)))
199 (unless tmm-km-list
200 (error "Empty menu reached"))
201 (and tmm-km-list
202 (let ((index-of-default 0))
203 (if tmm-mid-prompt
204 (setq tmm-km-list (tmm-add-shortcuts tmm-km-list))
206 ;; Find the default item's index within the menu bar.
207 ;; We use this to decide the initial minibuffer contents
208 ;; and initial history position.
209 (if default-item
210 (let ((tail menu) visible)
211 (while (and tail
212 (not (eq (car-safe (car tail)) default-item)))
213 ;; Be careful to count only the elements of MENU
214 ;; that actually constitute menu bar items.
215 (if (and (consp (car tail))
216 (or (stringp (car-safe (cdr (car tail))))
217 (and
218 (eq (car-safe (cdr (car tail))) 'menu-item)
219 (progn
220 (setq visible
221 (plist-get
222 (nthcdr 4 (car tail)) :visible))
223 (or (not visible) (eval visible))))))
224 (setq index-of-default (1+ index-of-default)))
225 (setq tail (cdr tail)))))
226 (let ((prompt (concat "^." (regexp-quote tmm-mid-prompt))))
227 (setq tmm--history
228 (reverse (delq nil
229 (mapcar
230 (lambda (elt)
231 (if (string-match prompt (car elt))
232 (car elt)))
233 tmm-km-list)))))
234 (setq history-len (length tmm--history))
235 (setq tmm--history (append tmm--history tmm--history
236 tmm--history tmm--history))
237 (setq tmm-c-prompt (nth (- history-len 1 index-of-default)
238 tmm--history))
239 (setq out
240 (if default-item
241 (car (nth index-of-default tmm-km-list))
242 (minibuffer-with-setup-hook #'tmm-add-prompt
243 ;; tmm-km-list is reversed, because history
244 ;; needs it in LIFO order. But completion
245 ;; needs it in non-reverse order, so that the
246 ;; menu items are displayed as completion
247 ;; candidates in the order they are shown on
248 ;; the menu bar. So pass completing-read the
249 ;; reversed copy of the list.
250 (completing-read-default
251 (concat gl-str
252 " (up/down to change, PgUp to menu): ")
253 (tmm--completion-table (reverse tmm-km-list)) nil t nil
254 (cons 'tmm--history
255 (- (* 2 history-len) index-of-default))))))))
256 (setq choice (cdr (assoc out tmm-km-list)))
257 (and (null choice)
258 (string-prefix-p tmm-c-prompt out)
259 (setq out (substring out (length tmm-c-prompt))
260 choice (cdr (assoc out tmm-km-list))))
261 (and (null choice) out
262 (setq out (try-completion out tmm-km-list)
263 choice (cdr (assoc out tmm-km-list)))))
264 ;; CHOICE is now (STRING . MEANING). Separate the two parts.
265 (setq chosen-string (car choice))
266 (setq choice (cdr choice))
267 (cond (in-popup
268 ;; We just did the inner level of a -popup menu.
269 choice)
270 ;; We just did the outer level. Do the inner level now.
271 (not-menu (tmm-prompt choice t))
272 ;; We just handled a menu keymap and found another keymap.
273 ((keymapp choice)
274 (if (symbolp choice)
275 (setq choice (indirect-function choice)))
276 (condition-case nil
277 (require 'mouse)
278 (error nil))
279 (tmm-prompt choice))
280 ;; We just handled a menu keymap and found a command.
281 (choice
282 (if chosen-string
283 (progn
284 (setq last-command-event chosen-string)
285 (call-interactively choice))
286 choice)))))
288 (defun tmm-add-shortcuts (list)
289 "Add shortcuts to cars of elements of the list.
290 Takes a list of lists with a string as car, returns list with
291 shortcuts added to these cars.
292 Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
293 (let ((tmm-next-shortcut-digit ?0))
294 (mapcar 'tmm-add-one-shortcut (reverse list))))
296 (defsubst tmm-add-one-shortcut (elt)
297 ;; uses the free vars tmm-next-shortcut-digit and tmm-short-cuts
298 (cond
299 ((eq (cddr elt) 'ignore)
300 (cons (concat " " (make-string (length tmm-mid-prompt) ?\-)
301 (car elt))
302 (cdr elt)))
304 (let* ((str (car elt))
305 (paren (string-match "(" str))
306 (pos 0) (word 0) char)
307 (catch 'done ; ??? is this slow?
308 (while (and (or (not tmm-shortcut-words) ; no limit on words
309 (< word tmm-shortcut-words)) ; try n words
310 (setq pos (string-match "\\w+" str pos)) ; get next word
311 (not (and paren (> pos paren)))) ; don't go past "(binding.."
312 (if (or (= pos 0)
313 (/= (aref str (1- pos)) ?.)) ; avoid file extensions
314 (let ((shortcut-style
315 (if (listp tmm-shortcut-style) ; convert to list
316 tmm-shortcut-style
317 (list tmm-shortcut-style))))
318 (while shortcut-style ; try upcase and downcase variants
319 (setq char (funcall (car shortcut-style) (aref str pos)))
320 (if (not (memq char tmm-short-cuts)) (throw 'done char))
321 (setq shortcut-style (cdr shortcut-style)))))
322 (setq word (1+ word))
323 (setq pos (match-end 0)))
324 (while (<= tmm-next-shortcut-digit ?9) ; no letter shortcut, pick a digit
325 (setq char tmm-next-shortcut-digit)
326 (setq tmm-next-shortcut-digit (1+ tmm-next-shortcut-digit))
327 (if (not (memq char tmm-short-cuts)) (throw 'done char)))
328 (setq char nil))
329 (if char (setq tmm-short-cuts (cons char tmm-short-cuts)))
330 (cons (concat (if char (concat (char-to-string char) tmm-mid-prompt)
331 ;; keep them lined up in columns
332 (make-string (1+ (length tmm-mid-prompt)) ?\s))
333 str)
334 (cdr elt))))))
336 ;; This returns the old map.
337 (defun tmm-define-keys (minibuffer)
338 (let ((map (make-sparse-keymap)))
339 (suppress-keymap map t)
340 (dolist (c tmm-short-cuts)
341 (if (listp tmm-shortcut-style)
342 (define-key map (char-to-string c) 'tmm-shortcut)
343 ;; only one kind of letters are shortcuts, so map both upcase and
344 ;; downcase input to the same
345 (define-key map (char-to-string (downcase c)) 'tmm-shortcut)
346 (define-key map (char-to-string (upcase c)) 'tmm-shortcut)))
347 (if minibuffer
348 (progn
349 (define-key map [pageup] 'tmm-goto-completions)
350 (define-key map [prior] 'tmm-goto-completions)
351 (define-key map "\ev" 'tmm-goto-completions)
352 (define-key map "\C-n" 'next-history-element)
353 (define-key map "\C-p" 'previous-history-element)))
354 (prog1 (current-local-map)
355 (use-local-map (append map (current-local-map))))))
357 (defun tmm-completion-delete-prompt ()
358 (with-current-buffer standard-output
359 (goto-char (point-min))
360 (delete-region (point) (search-forward "Possible completions are:\n"))))
362 (defun tmm-remove-inactive-mouse-face ()
363 "Remove the mouse-face property from inactive menu items."
364 (let ((inhibit-read-only t)
365 (inactive-string
366 (concat " " (make-string (length tmm-mid-prompt) ?\-)))
367 next)
368 (save-excursion
369 (goto-char (point-min))
370 (while (not (eobp))
371 (setq next (next-single-char-property-change (point) 'mouse-face))
372 (when (looking-at inactive-string)
373 (remove-text-properties (point) next '(mouse-face))
374 (add-text-properties (point) next '(face tmm-inactive)))
375 (goto-char next)))
376 (set-buffer-modified-p nil)))
378 (defun tmm-add-prompt ()
379 (unless tmm-c-prompt
380 (error "No active menu entries"))
381 (setq tmm-old-mb-map (tmm-define-keys t))
382 (or tmm-completion-prompt
383 (add-hook 'completion-setup-hook
384 'tmm-completion-delete-prompt 'append))
385 (unwind-protect
386 (minibuffer-completion-help)
387 (remove-hook 'completion-setup-hook 'tmm-completion-delete-prompt))
388 (with-current-buffer "*Completions*"
389 (tmm-remove-inactive-mouse-face)
390 (when tmm-completion-prompt
391 (let ((inhibit-read-only t)
392 (window (get-buffer-window "*Completions*")))
393 (goto-char (point-min))
394 (insert tmm-completion-prompt)
395 (when window
396 ;; Try to show everything just inserted and preserve height of
397 ;; *Completions* window. This should fix a behavior described
398 ;; in Bug#1291.
399 (fit-window-to-buffer window nil nil nil nil t)))))
400 (insert tmm-c-prompt))
402 (defun tmm-shortcut ()
403 "Choose the shortcut that the user typed."
404 (interactive)
405 (let ((c last-command-event) s)
406 (if (symbolp tmm-shortcut-style)
407 (setq c (funcall tmm-shortcut-style c)))
408 (if (memq c tmm-short-cuts)
409 (if (equal (buffer-name) "*Completions*")
410 (progn
411 (goto-char (point-min))
412 (re-search-forward
413 (concat "\\(^\\|[ \t]\\)" (char-to-string c) tmm-mid-prompt))
414 (choose-completion))
415 ;; In minibuffer
416 (delete-region (minibuffer-prompt-end) (point-max))
417 (dolist (elt tmm-km-list)
418 (if (string=
419 (substring (car elt) 0
420 (min (1+ (length tmm-mid-prompt))
421 (length (car elt))))
422 (concat (char-to-string c) tmm-mid-prompt))
423 (setq s (car elt))))
424 (insert s)
425 (exit-minibuffer)))))
427 (defun tmm-goto-completions ()
428 "Jump to the completions buffer."
429 (interactive)
430 (let ((prompt-end (minibuffer-prompt-end)))
431 (setq tmm-c-prompt (buffer-substring prompt-end (point-max)))
432 ;; FIXME: Why?
433 (delete-region prompt-end (point-max)))
434 (switch-to-buffer-other-window "*Completions*")
435 (search-forward tmm-c-prompt)
436 (search-backward tmm-c-prompt))
438 (defun tmm-get-keymap (elt &optional in-x-menu)
439 "Prepend (DOCSTRING EVENT BINDING) to free variable `tmm-km-list'.
440 The values are deduced from the argument ELT, that should be an
441 element of keymap, an `x-popup-menu' argument, or an element of
442 `x-popup-menu' argument (when IN-X-MENU is not-nil).
443 This function adds the element only if it is not already present.
444 It uses the free variable `tmm-table-undef' to keep undefined keys."
445 (let (km str plist filter visible enable (event (car elt)))
446 (setq elt (cdr elt))
447 (if (eq elt 'undefined)
448 (setq tmm-table-undef (cons (cons event nil) tmm-table-undef))
449 (unless (assoc event tmm-table-undef)
450 (cond ((if (listp elt)
451 (or (keymapp elt) (eq (car elt) 'lambda))
452 (and (symbolp elt) (fboundp elt)))
453 (setq km elt))
455 ((if (listp (cdr-safe elt))
456 (or (keymapp (cdr-safe elt))
457 (eq (car (cdr-safe elt)) 'lambda))
458 (and (symbolp (cdr-safe elt)) (fboundp (cdr-safe elt))))
459 (setq km (cdr elt))
460 (and (stringp (car elt)) (setq str (car elt))))
462 ((if (listp (cdr-safe (cdr-safe elt)))
463 (or (keymapp (cdr-safe (cdr-safe elt)))
464 (eq (car (cdr-safe (cdr-safe elt))) 'lambda))
465 (and (symbolp (cdr-safe (cdr-safe elt)))
466 (fboundp (cdr-safe (cdr-safe elt)))))
467 (setq km (cddr elt))
468 (and (stringp (car elt)) (setq str (car elt))))
470 ((eq (car-safe elt) 'menu-item)
471 ;; (menu-item TITLE COMMAND KEY ...)
472 (setq plist (cdr-safe (cdr-safe (cdr-safe elt))))
473 (when (consp (car-safe plist))
474 (setq plist (cdr-safe plist)))
475 (setq km (nth 2 elt))
476 (setq str (eval (nth 1 elt)))
477 (setq filter (plist-get plist :filter))
478 (if filter
479 (setq km (funcall filter km)))
480 (setq visible (plist-get plist :visible))
481 (if visible
482 (setq km (and (eval visible) km)))
483 (setq enable (plist-get plist :enable))
484 (if enable
485 (setq km (if (eval enable) km 'ignore))))
487 ((if (listp (cdr-safe (cdr-safe (cdr-safe elt))))
488 (or (keymapp (cdr-safe (cdr-safe (cdr-safe elt))))
489 (eq (car (cdr-safe (cdr-safe (cdr-safe elt)))) 'lambda))
490 (and (symbolp (cdr-safe (cdr-safe (cdr-safe elt))))
491 (fboundp (cdr-safe (cdr-safe (cdr-safe elt))))))
492 ; New style of easy-menu
493 (setq km (cdr (cddr elt)))
494 (and (stringp (car elt)) (setq str (car elt))))
496 ((stringp event) ; x-popup or x-popup element
497 (setq str event)
498 (setq event nil)
499 (setq km (if (or in-x-menu (stringp (car-safe elt)))
500 elt (cons 'keymap elt)))))
501 (unless (or (eq km 'ignore) (null str))
502 (let ((binding (where-is-internal km nil t)))
503 (when binding
504 (setq binding (key-description binding))
505 ;; Try to align the keybindings.
506 (let ((colwidth (min 30 (- (/ (window-width) 2) 10))))
507 (setq str
508 (concat str
509 (make-string (max 2 (- colwidth
510 (string-width str)
511 (string-width binding)))
512 ?\s)
513 binding)))))))
514 (and km (stringp km) (setq str km))
515 ;; Verify that the command is enabled;
516 ;; if not, don't mention it.
517 (when (and km (symbolp km) (get km 'menu-enable))
518 (setq km (if (eval (get km 'menu-enable)) km 'ignore)))
519 (and km str
520 (or (assoc str tmm-km-list)
521 (push (cons str (cons event km)) tmm-km-list))))))
523 (defun tmm-get-keybind (keyseq)
524 "Return the current binding of KEYSEQ, merging prefix definitions.
525 If KEYSEQ is a prefix key that has local and global bindings,
526 we merge them into a single keymap which shows the proper order of the menu.
527 However, for the menu bar itself, the value does not take account
528 of `menu-bar-final-items'."
529 (lookup-key (cons 'keymap (nreverse (current-active-maps))) keyseq))
531 (provide 'tmm)
533 ;;; tmm.el ends here