tmm.el 21 KB

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