ido-ubiquitous.el 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. ;;; ido-ubiquitous.el --- Use ido (nearly) everywhere. -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2011-2015 Ryan C. Thompson
  3. ;; Author: Ryan C. Thompson
  4. ;; URL: https://github.com/DarwinAwardWinner/ido-ubiquitous
  5. ;; Version: 3.5
  6. ;; Created: 2011-09-01
  7. ;; Keywords: convenience, completion, ido
  8. ;; EmacsWiki: InteractivelyDoThings
  9. ;; Package-Requires: ((emacs "24.1") (ido-completing-read+ "3.0"))
  10. ;; Filename: ido-ubiquitous.el
  11. ;; This file is NOT part of GNU Emacs.
  12. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  13. ;;
  14. ;;; Commentary:
  15. ;; If you use the excellent `ido-mode' for efficient completion of
  16. ;; file names and buffers, you might wonder if you can get ido-style
  17. ;; completion everywhere else too. Well, that's what this package
  18. ;; does! ido-ubiquitous is here to enable ido-style completion for
  19. ;; (almost) every function that uses the standard completion function
  20. ;; `completing-read'.
  21. ;; To use this package, call `ido-ubiquitous-mode' to enable the mode,
  22. ;; or use `M-x customize-variable ido-ubiquitous-mode' it to enable it
  23. ;; permanently. Once the mode is enabled, most functions that use
  24. ;; `completing-read' will now have ido completion. If you decide in
  25. ;; the middle of a command that you would rather not use ido, just C-f
  26. ;; or C-b at the end/beginning of the input to fall back to non-ido
  27. ;; completion (this is the same shortcut as when using ido for buffers
  28. ;; or files).
  29. ;; Note that `completing-read' has some quirks and complex behavior
  30. ;; that ido cannot emulate. Ido-ubiquitous attempts to detect some of
  31. ;; these quirks and avoid using ido when it sees them. So some
  32. ;; functions will not have ido completion even when this mode is
  33. ;; enabled. Some other functions have ido disabled in them because
  34. ;; their packages already provide support for ido via other means (for
  35. ;; example, org-mode and magit). See `M-x customize-group
  36. ;; ido-ubiquitous' and read about the override variables for more
  37. ;; information.
  38. ;; ido-ubiquitous version 3.0 is a major update, including a split
  39. ;; into two packages, and some of the configuration options have
  40. ;; changed in non-backwards-compatible ways. If you have customized
  41. ;; ido-ubiquitous, be sure to check out `M-x customize-group
  42. ;; ido-ubiquitous' and `M-x customize-group ido-completing-read+'
  43. ;; after updating to 3.0 and make sure the new settings are to your
  44. ;; liking.
  45. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  46. ;;
  47. ;; This program is free software: you can redistribute it and/or modify
  48. ;; it under the terms of the GNU General Public License as published by
  49. ;; the Free Software Foundation, either version 3 of the License, or (at
  50. ;; your option) any later version.
  51. ;;
  52. ;; This program is distributed in the hope that it will be useful, but
  53. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  54. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  55. ;; General Public License for more details.
  56. ;;
  57. ;; You should have received a copy of the GNU General Public License
  58. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  59. ;;
  60. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  61. ;;
  62. ;;; Code:
  63. (defconst ido-ubiquitous-version "3.5"
  64. "Currently running version of ido-ubiquitous.
  65. Note that when you update ido-ubiquitous, this variable may not
  66. be updated until you restart Emacs.")
  67. (eval-when-compile
  68. (when (or (not (boundp 'completing-read-function))
  69. (< emacs-major-version 24))
  70. (error "Could not find required variable `completing-read-function'. Are you using Emacs version 24 or higher? If you have Emacs 23 or lower, please downgrade to ido-ubiquitous version 1.7.")))
  71. (require 'ido)
  72. (require 'advice)
  73. (require 'cl-lib)
  74. (require 'ido-completing-read+)
  75. ;; Only exists in emacs 24.4 and up; we don't use this library
  76. ;; directly, but we load it here so we can test if it's available,
  77. ;; because if it isn't we need enable a workaround.
  78. (require 'nadvice nil 'noerror)
  79. ;;; Debug messages
  80. (defvar ido-ubiquitous-debug-mode)
  81. ;; Defined as a macro for efficiency (args are not evaluated unless
  82. ;; debug mode is on)
  83. (defmacro ido-ubiquitous--debug-message (format-string &rest args)
  84. `(when ido-ubiquitous-debug-mode
  85. (message (concat "ido-ubiquitous: " ,format-string) ,@args)))
  86. (defun ido-ubiquitous--explain-fallback (arg)
  87. ;; This function accepts a string, or an ido-ubiquitous-fallback
  88. ;; signal.
  89. (when ido-ubiquitous-debug-mode
  90. (when (and (listp arg)
  91. (eq (car arg) 'ido-ubiquitous-fallback))
  92. (setq arg (cdr arg)))
  93. (ido-ubiquitous--debug-message "Falling back to `%s' because %s."
  94. ido-cr+-fallback-function arg)))
  95. ;;; Internal utility functions
  96. (defun ido-ubiquitous--as-string (sym-or-str)
  97. "Return name of symbol, return string as is."
  98. (if (symbolp sym-or-str)
  99. (symbol-name sym-or-str)
  100. sym-or-str))
  101. (defun ido-ubiquitous--as-symbol (sym-or-str)
  102. "Return string as symbol, return symbol as is."
  103. (if (symbolp sym-or-str)
  104. sym-or-str
  105. (intern sym-or-str)))
  106. ;;; Custom widget definitions
  107. ;; We need to define some custom widget types for use in the override
  108. ;; variables.
  109. (define-widget 'lazy-notag 'lazy
  110. "Like lazy widget, but does not display its tag, only its value."
  111. :format "%v")
  112. ;; Define matcher functions and widgets for match specifications
  113. (defvar ido-ubiquitous-match-spec-widget-types nil
  114. "List of widget names for match specs.")
  115. (defvar ido-ubiquitous-spec-matchers nil
  116. "Alist of functions for matching function specs against function names.")
  117. (cl-loop for (widget-name widget-tag key field-type matcher) in
  118. '((exact-match "Exact match" exact string string=)
  119. (prefix-match "Prefix match" prefix string string-prefix-p)
  120. (regexp-match "Regexp match" regexp regexp string-match-p))
  121. do (define-widget (ido-ubiquitous--as-symbol widget-name) 'lazy-notag widget-tag
  122. :menu-tag widget-tag
  123. :type `(list :tag ,widget-tag :format "%v"
  124. (const :format ""
  125. :tag ,widget-tag
  126. ,key)
  127. (,field-type :tag ,widget-tag)))
  128. do (add-to-list 'ido-ubiquitous-match-spec-widget-types
  129. widget-name 'append)
  130. do (add-to-list 'ido-ubiquitous-spec-matchers
  131. (cons key matcher) 'append))
  132. (define-widget 'ido-ubiquitous-match-spec 'lazy-notag
  133. "Choice of exact, prefix, or regexp match."
  134. :type `(choice :tag "Match type"
  135. ,@ido-ubiquitous-match-spec-widget-types))
  136. (define-widget 'ido-ubiquitous-command-override-spec 'lazy-notag
  137. "Choice of override action plus match specification."
  138. :type '(cons :tag "Override rule"
  139. (choice :tag "For matching commands"
  140. (const :menu-tag "Disable"
  141. :tag "Disable ido-ubiquitous"
  142. disable)
  143. (const :menu-tag "Enable"
  144. :tag "Enable ido-ubiquitous in normal default mode"
  145. enable)
  146. (const :menu-tag "Enable old-style default"
  147. :tag "Enable ido-ubiquitous in old-style default mode"
  148. enable-old))
  149. ido-ubiquitous-match-spec))
  150. (define-widget 'ido-ubiquitous-function-override-spec 'lazy-notag
  151. "Choice of override action and function name. (Exact match only.)"
  152. :type '(list :tag "Override rule"
  153. (choice :tag "Do the following"
  154. (const :menu-tag "Disable"
  155. :tag "Disable ido-ubiquitous"
  156. disable)
  157. (const :menu-tag "Enable"
  158. :tag "Enable ido-ubiquitous in normal default mode"
  159. enable)
  160. (const :menu-tag "Enable old-style default"
  161. :tag "Enable ido-ubiquitous in old-style default mode"
  162. enable-old))
  163. (const :format "" exact)
  164. (string :tag "For function")))
  165. ;;; Custom Declarations
  166. (defgroup ido-ubiquitous nil
  167. "Use ido for (almost) all completion."
  168. :group 'ido
  169. :group 'ido-completing-read-plus)
  170. ;;;###autoload
  171. (define-obsolete-variable-alias 'ido-ubiquitous
  172. 'ido-ubiquitous-mode "ido-ubiquitous 0.8")
  173. ;;;###autoload
  174. (define-obsolete-function-alias 'ido-ubiquitous
  175. 'ido-ubiquitous-mode "ido-ubiquitous 0.8")
  176. ;;;###autoload
  177. (define-minor-mode ido-ubiquitous-mode
  178. "Use `ido-completing-read' instead of `completing-read' almost everywhere.
  179. If this mode causes problems for a function, you can customize
  180. when ido completion is or is not used by customizing
  181. `ido-ubiquitous-command-overrides' or
  182. `ido-ubiquitous-function-overrides'."
  183. nil
  184. :global t
  185. :group 'ido-ubiquitous
  186. ;; Actually enable/disable the mode by setting
  187. ;; `completing-read-function'.
  188. (setq completing-read-function
  189. (if ido-ubiquitous-mode
  190. #'completing-read-ido-ubiquitous
  191. ido-cr+-fallback-function)))
  192. ;; Variables for functionality that has moved to ido-completing-read+
  193. (define-obsolete-variable-alias
  194. 'ido-ubiquitous-max-items
  195. 'ido-cr+-max-items
  196. "ido-ubiquitous 3.0")
  197. (define-obsolete-variable-alias
  198. 'ido-ubiquitous-fallback-completing-read-function
  199. 'ido-cr+-fallback-function
  200. "ido-ubiquitous 3.0")
  201. (define-obsolete-variable-alias
  202. 'ido-ubiquitous-enable-compatibility-globally
  203. 'ido-ubiquitous-enable-old-style-default
  204. "ido-ubiquitous 2.0")
  205. (defcustom ido-ubiquitous-default-state 'enable
  206. "Default ido-ubiquitous mode of operation for commands with no override.
  207. This can be set to one of three options:
  208. * `enable': use normal ido completion;
  209. * `enable-old': use ido completion, but with emulation of the
  210. old-style default selection of `completing-read';
  211. * `disable': use non-ido completion.
  212. Command-specific and function-specific overrides are available to
  213. override this default for specific commands/functions. See
  214. `ido-ubiquitous-command-overrides' and
  215. `ido-ubiquitous-function-overrides'.
  216. The `enable-old' option swaps the behavior of RET and C-j but
  217. only for the first keypress after beginning completion.
  218. Specifically, on the first keypress, RET will return an empty
  219. string and C-j will return the first item on the list. The
  220. purpose of this is to emulate a legacy compatibility quirk of
  221. `completing-read'. From the `completing-read' docstring:
  222. > If the input is null, `completing-read' returns DEF, or the
  223. > first element of the list of default values, or an empty string
  224. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  225. This odd behavior is required for compatibility with an old-style
  226. usage pattern whereby the default was requested by returning an
  227. empty string. In this mode, the caller receives the empty string
  228. and handles the default case manually, while `completing-read'
  229. never has any knowledge of the default. This is a problem for
  230. ido, which normally returns the first element in the list, not an
  231. empty string, when the input is empty and you press RET. Without
  232. knowledge of the default, it cannot ensure that the default is
  233. first on the list, so returning the first item is not the correct
  234. behavior. Instead, it must return an empty string like
  235. `completing-read'.
  236. The `disable' mode is available as a default, which seems
  237. counterintuitive. But this allows you, if you so desire, to
  238. enable ido-ubiquitous selectively for only a few specifc commands
  239. using overrides and disable it for everything else."
  240. :type '(choice :tag "Default mode"
  241. (const :menu-tag "Disable"
  242. :tag "Disable ido-ubiquitous"
  243. disable)
  244. (const :menu-tag "Enable"
  245. :tag "Enable ido-ubiquitous in normal default mode"
  246. enable)
  247. (const :menu-tag "Enable old-style default"
  248. :tag "Enable ido-ubiquitous in old-style default mode"
  249. enable-old))
  250. :group 'ido-ubiquitous)
  251. (make-obsolete-variable
  252. 'ido-ubiquitous-enable-old-style-default
  253. "This variable no longer has any effect. Set
  254. `ido-ubiquitous-default-state' to `enable-old' instead."
  255. "ido-ubiquitous 3.0")
  256. (defconst ido-ubiquitous-default-command-overrides
  257. '(;; If you want ido for M-x, install smex
  258. (disable exact "execute-extended-command")
  259. ;; Wanderlust uses new-style default
  260. (enable prefix "wl-")
  261. ;; Info functions use old-style default selection
  262. (enable-old prefix "Info-")
  263. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/4
  264. (enable exact "webjump")
  265. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/28
  266. (enable regexp "\\`\\(find\\|load\\|locate\\)-library\\'")
  267. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/37
  268. ;; Org already supports ido natively
  269. (disable prefix "org-")
  270. ;; https://github.com/bbatsov/prelude/issues/488
  271. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/44
  272. ;; tmm implements its own non-standard completion mechanics
  273. (disable prefix "tmm-")
  274. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/47
  275. ;; theme functions don't need old-style compatibility
  276. (enable regexp "\\`\\(load\\|enable\\|disable\\|describe\\|custom-theme-visit\\)-theme\\'")
  277. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/79
  278. (enable-old prefix "bbdb-")
  279. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/83
  280. (enable-old exact "where-is")
  281. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/85
  282. (enable prefix "xref-")
  283. ) ; Close paren on separate line for better VC diffs
  284. "Default value of `ido-ubiquitous-command-overrides'.
  285. You can restore these using the command `ido-ubiquitous-restore-default-overrides'.")
  286. (defconst ido-ubiquitous-default-function-overrides
  287. '((disable exact "read-file-name")
  288. (disable exact "read-file-name-internal")
  289. (disable exact "read-buffer")
  290. (disable exact "gnus-emacs-completing-read")
  291. (disable exact "gnus-iswitchb-completing-read")
  292. (disable exact "grep-read-files")
  293. (disable exact "magit-builtin-completing-read")
  294. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/36
  295. (enable exact "bookmark-completing-read")
  296. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/4
  297. (enable-old exact "webjump-read-choice")
  298. (enable-old exact "webjump-read-url-choice")
  299. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/9
  300. (disable exact "isearchp-read-unicode-char")
  301. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/37
  302. (disable exact "org-completing-read")
  303. (disable exact "org-completing-read-no-i")
  304. (disable exact "org-iswitchb-completing-read")
  305. (disable exact "org-icompleting-read")
  306. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/38
  307. (enable exact "read-char-by-name")
  308. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/39
  309. (disable exact "Info-read-node-name")
  310. ;; https://github.com/purcell/emacs.d/issues/182#issuecomment-44212927
  311. (disable exact "tmm-menubar")
  312. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/58
  313. ;; https://github.com/mooz/js2-mode/issues/181
  314. (enable exact "imenu--completion-buffer")
  315. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/74
  316. (enable-old exact "auto-insert"))
  317. "Default value of `ido-ubiquitous-function-overrides'.
  318. You can restore these using the command `ido-ubiquitous-restore-default-overrides'.")
  319. (defcustom ido-ubiquitous-command-overrides ido-ubiquitous-default-command-overrides
  320. "List of command override specifications for ido-ubiquitous
  321. Each override specification describes how ido-ubiquitous should
  322. behave one or many commands. A specification has the
  323. form `(BEHAVIOR MATCH-TYPE MATCH-TEXT)'. BEHAVIOR is one of the
  324. following:
  325. * `disable': ido-ubiquitous should not be used at all for the
  326. specified commands;
  327. * `enable': ido-ubiquitous may be used with the specified
  328. commands, without emulating the old-style default selection
  329. of `completing-read';
  330. * `enable-old': ido-ubiquitous may be used with the specified
  331. commands, and should emulate the old-style default selection
  332. of `completing-read'.
  333. MATCH-TYPE affects how MATCH-TEXT is interpreted, as follows:
  334. * `exact': the specification only affects the one command
  335. whose name is MATCH-TEXT;
  336. * `prefix': the specification affects any command whose name
  337. starts with MATCH-TEXT (This is useful for specifying a
  338. certain behavior for an entire package);
  339. * `regexp': the specification affects any command whose name
  340. matches MATCH-TEXT (with MATCH-TEXT being interpreted as a
  341. regular expression)
  342. MATCH-TEXT should be a string.
  343. Since this variable's has a somewhat complex structure, it is
  344. recommended that you set this variable through Customize.
  345. Note that this variable only affects *commands*, which are
  346. functions marked as interactive. See
  347. `ido-ubiquitous-function-overrides' for how to modify the
  348. behavior of ido-ubiquitous for arbitrary functions.
  349. If you need to add a new specification to this list, please also
  350. file a bug report at https://github.com/DarwinAwardWinner/ido-ubiquitous/issues"
  351. :type '(repeat ido-ubiquitous-command-override-spec)
  352. :group 'ido-ubiquitous)
  353. (defmacro ido-ubiquitous-with-override (override &rest body)
  354. "Eval BODY with specicified OVERRIDE in place.
  355. The OVERRIDE argument is evaluated normally, so if it is a
  356. literal symbol, it must be quoted.
  357. See `ido-ubiquitous-command-overrides' for valid override types."
  358. (declare (indent 1))
  359. ;; Eval override
  360. `(let ((ido-ubiquitous-next-override ,override))
  361. ,@body))
  362. (defun ido-ubiquitous-apply-function-override (func override)
  363. "Set the override property on FUNC to OVERRIDE and set up advice to apply the override."
  364. (setq func (ido-ubiquitous--as-symbol func)
  365. override (ido-ubiquitous--as-symbol override))
  366. (if (memq override '(disable enable enable-old nil))
  367. (progn
  368. (put func 'ido-ubiquitous-override override)
  369. (when override
  370. (let ((docstring
  371. (format "Override ido-ubiquitous behavior in %s if its `ido-ubiquitous-override' property is non-nil." func)))
  372. (eval
  373. `(defadvice ,func (around ido-ubiquitous-override activate)
  374. ,docstring
  375. (let* ((func ',func)
  376. (override (get func 'ido-ubiquitous-override)))
  377. (when override
  378. (ido-ubiquitous--debug-message
  379. "Using override `%s' for function `%s'"
  380. override func))
  381. (ido-ubiquitous-with-override override
  382. ad-do-it)))))))
  383. (display-warning
  384. 'ido-ubiquitous
  385. (format "Ignoring invalid override action `%s' for function `%s' found in `ido-ubiquitous-function-overrides'."
  386. override func)
  387. :warning)))
  388. (defun ido-ubiquitous-set-function-overrides (sym newval)
  389. "Custom setter function for `ido-ubiquitous-function-overrides'.
  390. In addition to setting the variable, this also sets up advice on
  391. each function to apply the appropriate override."
  392. ;; Unset all previous overrides
  393. (when (boundp sym)
  394. (let ((oldval (eval sym)))
  395. (cl-loop for (_action _match-type func) in oldval
  396. do (ido-ubiquitous-apply-function-override func nil))))
  397. ;; Ensure that function names are strings, not symbols
  398. (setq newval
  399. (cl-loop for (action match-type func) in newval
  400. collect (list action match-type
  401. (ido-ubiquitous--as-string func))))
  402. (set-default sym newval)
  403. ;; set new overrides
  404. (cl-loop for override in (eval sym)
  405. for (action match-type func) = override
  406. if (eq match-type 'exact)
  407. do (ido-ubiquitous-apply-function-override func action)
  408. else
  409. do (display-warning
  410. 'ido-ubiquitous
  411. (format
  412. "Ignoring invalid function override match-type `%s' for function `%s'; only match-type `exact' is supported in `ido-ubiquitous-function-overrides'."
  413. match-type func))))
  414. (defcustom ido-ubiquitous-function-overrides ido-ubiquitous-default-function-overrides
  415. "List of function override specifications for ido-ubiquitous
  416. Function override specifications have a similar structure to
  417. command override specifications (see
  418. `ido-ubiquitous-command-overrides'). A function override
  419. specification has the form `(BEHAVIOR MATCH-TYPE MATCH-TEXT)'.
  420. However, `MATCH-TYPE' may ONLY be `exact'; No other match type is
  421. supported.
  422. If you need to add a new specification to this list, please also file a
  423. bug report at https://github.com/DarwinAwardWinner/ido-ubiquitous/issues
  424. Setting this variable directly has no effect. You must set it
  425. through Customize."
  426. :type '(repeat ido-ubiquitous-function-override-spec)
  427. :set 'ido-ubiquitous-set-function-overrides
  428. :group 'ido-ubiquitous)
  429. (defcustom ido-ubiquitous-allow-on-functional-collection nil
  430. "Allow ido completion when COLLECTION is a function.
  431. The `completing-read' function allows its COLLECTION argument to
  432. be a function instead of a list of choices. Some such functions
  433. simply return a list of completions and are suitable for use with
  434. ido, but others implement more complex behavior and will result
  435. in incorrect behavior if used with ido. Since there is no way to
  436. tell the difference, this preference defaults to nil, which means
  437. that ido-ubiquitous will not work when COLLECTION is a function
  438. unless there is a specific override in effect. To disable this
  439. safeguard and GUARANTEE ERRORS on some functions, you may set
  440. this to non-nil, but this is not recommended."
  441. :type 'boolean
  442. :group 'ido-ubiquitous)
  443. ;;; ido-ubiquitous core
  444. ;; These variable are used to make ido-ubiquitous work properly in the
  445. ;; case that `completing-read' is called recursively (which is
  446. ;; possible when `enable-recursive-minibuffers' is non-nil.)
  447. (defvar ido-ubiquitous-enable-next-call nil
  448. "If non-nil, then the next call to `ido-completing-read' is by ido-ubiquitous.")
  449. (defvar ido-ubiquitous-enable-this-call nil
  450. "If non-nil, then the current call to `ido-completing-read' is by ido-ubiquitous.")
  451. (defvar ido-ubiquitous-next-override nil
  452. "This holds the override to be applied on the next call to `completing-read'.
  453. It's value can be nil or one of the symbols `disable', `enable', or
  454. `enable-old'.
  455. You should not modify this variable directly. Instead use the
  456. macro `ido-ubiquitous-with-override'.")
  457. (defvar ido-ubiquitous-active-override nil
  458. "This holds the override being applied to the current call to `completing-read'.
  459. It's value can be nil or one of the symbols `disable', `enable', or
  460. `enable-old'.
  461. You should not modify this variable directly. Instead use the
  462. macro `ido-ubiquitous-with-override'.")
  463. (defvar ido-ubiquitous-active-state nil
  464. "This holds the ido-ubiquitous mode of operation for the current call to `completing-read'.
  465. It's value can be nil or one of the symbols `disable', `enable', or
  466. `enable-old'.
  467. You should not modify this variable directly.")
  468. (defadvice ido-completing-read (around ido-ubiquitous activate)
  469. "Enable ido-ubiquitous features if this call was done through ido-ubiquitous.
  470. This advice ensures that `ido-ubiquitous-enable-this-call' is set
  471. properly while `ido-completing-read' is executing. This variable
  472. is used to determine whether to enable certain behaviors only for
  473. ido-ubiquitous, not for ordinary ido completion."
  474. ;; Set "this" and clear "next" so it doesn't apply to nested calls.
  475. (let* ((ido-ubiquitous-enable-this-call ido-ubiquitous-enable-next-call)
  476. (ido-ubiquitous-enable-next-call nil)
  477. (ido-ubiquitous-initial-item nil))
  478. ad-do-it))
  479. ;; Signal used to trigger fallback
  480. (put 'ido-ubiquitous-fallback 'error-conditions '(ido-ubiquitous-fallback error))
  481. (put 'ido-ubiquitous-fallback 'error-message "ido-ubiquitous-fallback")
  482. (defun completing-read-ido-ubiquitous
  483. (prompt collection &optional predicate
  484. require-match initial-input
  485. hist def inherit-input-method)
  486. "ido-based method for reading from the minibuffer with completion.
  487. See `completing-read' for the meaning of the arguments.
  488. This function is a wrapper for `ido-completing-read' designed to
  489. be used as the value of `completing-read-function'. Importantly,
  490. it detects edge cases that ido cannot handle and uses normal
  491. completion for them."
  492. (let (;; Save the original arguments in case we need to do the
  493. ;; fallback
  494. (orig-args
  495. (list prompt collection predicate require-match
  496. initial-input hist def inherit-input-method)))
  497. (condition-case sig
  498. (let* (;; Set the active override and clear the "next" one so it
  499. ;; doesn't apply to nested calls.
  500. (ido-ubiquitous-active-override ido-ubiquitous-next-override)
  501. (ido-ubiquitous-next-override nil)
  502. ;; Apply the active override, if any
  503. (ido-ubiquitous-active-state
  504. (or ido-ubiquitous-active-override
  505. ido-ubiquitous-default-state
  506. 'enable)))
  507. ;; If ido-ubiquitous is disabled this time, fall back
  508. (when (eq ido-ubiquitous-active-state 'disable)
  509. (signal 'ido-ubiquitous-fallback
  510. "`ido-ubiquitous-active-state' is `disable'"))
  511. ;; Handle a collection that is a function: either expand
  512. ;; completion list now or fall back
  513. (when (functionp collection)
  514. (if (or ido-ubiquitous-allow-on-functional-collection
  515. (memq ido-ubiquitous-active-override
  516. '(enable enable-old)))
  517. (setq collection (all-completions "" collection predicate))
  518. (signal 'ido-ubiquitous-fallback
  519. "COLLECTION is a function and there is no override")))
  520. (let ((ido-ubiquitous-enable-next-call t))
  521. (ido-completing-read+
  522. prompt collection predicate require-match
  523. initial-input hist def inherit-input-method)))
  524. ;; Handler for ido-ubiquitous-fallback signal
  525. (ido-ubiquitous-fallback
  526. (ido-ubiquitous--explain-fallback sig)
  527. (apply ido-cr+-fallback-function orig-args)))))
  528. (define-obsolete-function-alias 'completing-read-ido #'completing-read-ido-ubiquitous
  529. "ido-ubiquitous 3.0")
  530. ;;; Old-style default support
  531. (defvar ido-ubiquitous-initial-item nil
  532. "The first item selected when ido starts.
  533. This is initialized to the first item in the list of completions
  534. when ido starts, and is cleared when any character is entered
  535. into the prompt or the list is cycled. If it is non-nil and still
  536. equal to the first item in the completion list when ido exits,
  537. then if `ido-ubiquitous-active-state' is `enable-old', ido
  538. returns an empty string instead of the first item on the list.")
  539. (defmacro ido-ubiquitous-set-initial-item (item)
  540. "Wrapper for `(setq ido-ubiquitous-initial-item ITEM)'.
  541. This wrapper differs from simply doing `(setq
  542. ido-ubiquitous-initial-item ITEM)' in several ways. First, it has
  543. no effect (and does not evaluate ITEM) unless
  544. `ido-ubiquitous-active-state' is `enable-old'. Second, it emits
  545. appropriate debug messages."
  546. `(when (eq ido-ubiquitous-active-state 'enable-old)
  547. (let ((item ,item))
  548. (cond
  549. (item
  550. (ido-ubiquitous--debug-message
  551. "Setting `ido-ubiquitous-initial-item' to `%S'."
  552. item))
  553. (ido-ubiquitous-initial-item
  554. (ido-ubiquitous--debug-message "Clearing `ido-ubiquitous-initial-item'.")))
  555. (setq ido-ubiquitous-initial-item item))))
  556. (defadvice ido-read-internal (before ido-ubiquitous-clear-initial-item activate)
  557. (ido-ubiquitous-set-initial-item nil))
  558. (defadvice ido-make-choice-list (after ido-ubiquitous-set-initial-item activate)
  559. (ido-ubiquitous-set-initial-item
  560. (when (and ad-return-value (listp ad-return-value))
  561. (car ad-return-value))))
  562. (defadvice ido-next-match (after ido-ubiquitous-clear-initial-item activate)
  563. (ido-ubiquitous-set-initial-item nil))
  564. (defadvice ido-prev-match (after ido-ubiquitous-clear-initial-item activate)
  565. (ido-ubiquitous-set-initial-item nil))
  566. ;; Clear initial item after `self-insert-command'
  567. (defun ido-ubiquitous-post-insert-hook ()
  568. (eval '(ido-ubiquitous-set-initial-item nil)))
  569. (defun ido-ubiquitous-ido-minibuffer-setup-hook ()
  570. (add-hook
  571. 'post-self-insert-hook
  572. #'ido-ubiquitous-post-insert-hook
  573. nil
  574. 'local))
  575. (add-hook 'ido-minibuffer-setup-hook
  576. #'ido-ubiquitous-ido-minibuffer-setup-hook)
  577. (defun ido-ubiquitous-should-use-old-style-default ()
  578. "Returns non nil if ido-ubiquitous should emulate old-style default.
  579. This function simply encapsulates all the checks that need to be
  580. done in order to decide whether to swap RET and C-j. See
  581. `ido-ubiquitous-default-state' for more information."
  582. ;; These checks outside the loop don't produce debug messages
  583. (and
  584. ;; Only if old-style default enabled
  585. (eq ido-ubiquitous-active-state 'enable-old)
  586. ;; This loop is just an implementation of `and' that reports which
  587. ;; arg was nil for debugging purposes.
  588. (cl-loop
  589. for test in
  590. '((bound-and-true-p ido-cur-item)
  591. ;; Only if completing a list, not a buffer or file
  592. (eq ido-cur-item 'list)
  593. ;; Only if this call was done through ido-ubiquitous
  594. ido-ubiquitous-enable-this-call
  595. ;; Only if default is nil
  596. (null ido-default-item)
  597. ;; Only if input is empty
  598. (string= ido-text "")
  599. ;; Only if `ido-ubiquitous-initial-item' hasn't been cleared
  600. ido-ubiquitous-initial-item
  601. ;; Only if initial item hasn't changed
  602. (string= (car ido-cur-list)
  603. ido-ubiquitous-initial-item))
  604. for test-result = (eval test)
  605. if (not test-result)
  606. do (ido-ubiquitous--debug-message
  607. "Not enabling old-style default selection because `%S' is nil"
  608. test)
  609. and return nil
  610. finally do (ido-ubiquitous--debug-message
  611. "Enabling old-style default selection")
  612. finally return t)))
  613. (defadvice ido-exit-minibuffer (around ido-ubiquitous-old-style-default-compat activate)
  614. "Emulate a quirk of `completing-read'.
  615. > If the input is null, `completing-read' returns DEF, or the
  616. > first element of the list of default values, or an empty string
  617. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  618. See `ido-ubiquitous-default-state', which controls whether this
  619. advice has any effect."
  620. (condition-case nil
  621. (if (ido-ubiquitous-should-use-old-style-default)
  622. (let ((ido-ubiquitous-active-state 'enable))
  623. (ido-select-text))
  624. ad-do-it)
  625. (error
  626. (display-warning 'ido-ubiquitous "Advice on `ido-exit-minibuffer' failed." :warning)
  627. ad-do-it))
  628. (ido-ubiquitous-set-initial-item nil))
  629. (defadvice ido-select-text (around ido-ubiquitous-old-style-default-compat activate)
  630. "Emulate a quirk of `completing-read'.
  631. > If the input is null, `completing-read' returns DEF, or the
  632. > first element of the list of default values, or an empty string
  633. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  634. See `ido-ubiquitous-default-state', which controls whether this
  635. advice has any effect."
  636. (condition-case nil
  637. (if (ido-ubiquitous-should-use-old-style-default)
  638. (let ((ido-ubiquitous-active-state 'enable))
  639. (ido-exit-minibuffer))
  640. ad-do-it)
  641. (error
  642. (display-warning 'ido-ubiquitous "Advice on `ido-select-text' failed." :warning)
  643. ad-do-it))
  644. (ido-ubiquitous-set-initial-item nil))
  645. ;;; Overrides
  646. (defun ido-ubiquitous-restore-default-overrides (&optional save)
  647. "Re-add the default overrides for ido-ubiquitous.
  648. This will ensure that the default overrides are all present and
  649. at the head of the list in `ido-ubiquitous-command-overrides' and
  650. `ido-ubiquitous-function-overrides'. User-added overrides will
  651. not be removed, but they may be masked if one of the default
  652. overrides affects the same functions.
  653. With a prefix arg, also save the above variables' new values for
  654. future sessions."
  655. (interactive "P")
  656. (let ((setter (if save
  657. 'customize-save-variable
  658. 'customize-set-variable)))
  659. (cl-loop for (var def) in '((ido-ubiquitous-command-overrides
  660. ido-ubiquitous-default-command-overrides)
  661. (ido-ubiquitous-function-overrides
  662. ido-ubiquitous-default-function-overrides))
  663. do (let* ((curval (eval var))
  664. (defval (eval def))
  665. (newval (delete-dups (append defval curval))))
  666. (funcall setter var newval)))
  667. (message (if save
  668. "ido-ubiquitous: Restored default command and function overrides and saved for future sessions."
  669. "ido-ubiquitous: Restored default command and function overrides for current session only. Call again with prefix to save for future sessions."))))
  670. (defun ido-ubiquitous-spec-match (spec symbol)
  671. "Returns t if SPEC matches SYMBOL (which should be a function name).
  672. See `ido-ubiquitous-command-overrides'. If the match spec is
  673. invalid or any other error occurs, the error is demoted to a
  674. warning and the function returns nil."
  675. (condition-case err
  676. (when (and symbol (symbolp symbol))
  677. (cl-destructuring-bind (type text) spec
  678. (let ((matcher (cdr (assoc type ido-ubiquitous-spec-matchers)))
  679. (text (ido-ubiquitous--as-string text))
  680. (symname (ido-ubiquitous--as-string symbol)))
  681. (if matcher
  682. (funcall matcher text symname)
  683. ;; If the matcher is invalid, issue a warning and return
  684. ;; nil.
  685. (error "Unknown match spec type \"%s\". See `ido-ubiquitous-spec-matchers' for valid types." type)
  686. nil))))
  687. (error
  688. (display-warning 'ido-ubiquitous "Error during ido-ubiquitous spec matching: %S" err)
  689. nil)))
  690. (defun ido-ubiquitous-get-command-override (cmd)
  691. "Return the override associated with the command CMD.
  692. If there is no override set for CMD in
  693. `ido-ubiquitous-command-overrides', return nil."
  694. (when (and cmd (symbolp cmd))
  695. (cl-loop for override in ido-ubiquitous-command-overrides
  696. for (action . spec) = override
  697. for valid-action = (and (memq action '(disable enable enable-old nil))
  698. (assoc (car spec) ido-ubiquitous-spec-matchers))
  699. unless valid-action
  700. do (progn
  701. (display-warning
  702. 'ido-ubiquitous
  703. (format "Removing invalid override `%S' from `ido-ubiquitous-command-overrides'"
  704. (cons action spec) action)
  705. :warning)
  706. (setq ido-ubiquitous-command-overrides
  707. (remove override ido-ubiquitous-command-overrides)))
  708. when (and valid-action (ido-ubiquitous-spec-match spec cmd))
  709. return action
  710. finally return nil)))
  711. ;;; Workaround for https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/24
  712. ;; When `call-interactively' is advised, `called-interactively-p'
  713. ;; always returns nil. So we redefine it (and `interactive-p') to test
  714. ;; the correct condition.
  715. (defsubst ido-ubiquitous--looks-like-advised-orig (func)
  716. "Returns t if FUNC is a symbol starting with \"ad-Orig-\".
  717. Such symbols are used to store the original definitions of
  718. functions that have been advised by `defadvice' or similar."
  719. (and (symbolp func)
  720. (string-prefix-p "ad-Orig-" (symbol-name func))))
  721. (defsubst ido-ubiquitous--looks-like-call-interactively (func)
  722. "Returns t if FUNC looks like the function `call-interactively'.
  723. FUNC \"looks like\" `call-interactively' if it is the literal
  724. symbol `call-interactively', or the value of `(symbol-function
  725. 'call-interactively)', or a symbol whose `symbol-function' is the
  726. same as that of `call-interactively'.
  727. This function is used to determine whether a given function was
  728. \"called by\" `call-interactively' and therefore was called
  729. interactively."
  730. (when func
  731. (eq (symbol-function 'call-interactively)
  732. (if (symbolp func)
  733. (symbol-function func)
  734. func))))
  735. (defun ido-ubiquitous--backtrace-from (fun)
  736. "Return all backtrace frames, starting with the one for FUN.
  737. FUN may be a list of functions, in which case the first one found
  738. on the stack will be used."
  739. (let ((stack
  740. (cl-loop for i upfrom 0
  741. for frame = (backtrace-frame i)
  742. while frame
  743. collect frame))
  744. (funcs (if (functionp fun)
  745. (list fun)
  746. fun)))
  747. (while (and stack
  748. (not (memq (cl-cadar stack) funcs)))
  749. (setq stack (cdr stack)))
  750. stack))
  751. (defun ido-ubiquitous--clean-advice-from-backtrace (stack)
  752. "Takes a stack trace and cleans all evidence of advice.
  753. Specifically, for each call to a function starting with
  754. \"ad-Orig-\", that call and all prior calls up to but not
  755. including the advised function's original name are deleted from
  756. the stack."
  757. (let ((skipping-until nil))
  758. (cl-loop for frame in stack
  759. for func = (cadr frame)
  760. ;; Check if we found the frame we we're skipping to
  761. if (and skipping-until
  762. (eq func skipping-until))
  763. do (setq skipping-until nil)
  764. ;; If we're looking at an the original form of an advised
  765. ;; function, skip until the real name of that function.
  766. if (and (not skipping-until)
  767. (ido-ubiquitous--looks-like-advised-orig func))
  768. do (setq skipping-until
  769. (intern
  770. (substring (symbol-name func)
  771. (eval-when-compile (length "ad-Orig-")))))
  772. unless skipping-until collect frame)))
  773. (defsubst ido-ubiquitous--interactive-internal ()
  774. "Eqivalent of the INTERACTIVE macro in the Emacs C source.
  775. This is an internal function that should never be called
  776. directly.
  777. See the C source for the logic behind this function."
  778. (and (not executing-kbd-macro)
  779. (not noninteractive)))
  780. (defun ido-ubiquitous--interactive-p-internal ()
  781. "Equivalent of C function \"interactive_p\".
  782. This is an internal function that should never be called
  783. directly.
  784. See the C source for the logic behind this function."
  785. (let ((stack
  786. ;; We clean advice from the backtrace. This ensures that we
  787. ;; get the right answer even if `call-interactively' has been
  788. ;; advised.
  789. (ido-ubiquitous--clean-advice-from-backtrace
  790. (cdr
  791. (ido-ubiquitous--backtrace-from
  792. '(called-interactively-p interactive-p))))))
  793. ;; See comments in the C function for the logic here.
  794. (while (and stack
  795. (or (eq (cl-cadar stack) 'bytecode)
  796. (null (caar stack))))
  797. (setq stack (cdr stack)))
  798. ;; Top of stack is now the function that we want to know
  799. ;; about. Pop it, then check if the next function is
  800. ;; `call-interactively', using a more permissive test than the default.
  801. (ido-ubiquitous--looks-like-call-interactively (cl-cadadr stack))))
  802. (defadvice call-interactively (around ido-ubiquitous activate)
  803. "Implements the behavior specified in `ido-ubiquitous-command-overrides'."
  804. (let* ((cmd (ad-get-arg 0))
  805. (override (ido-ubiquitous-get-command-override cmd)))
  806. (when override
  807. (ido-ubiquitous--debug-message "Using override `%s' for command `%s'"
  808. override cmd))
  809. (ido-ubiquitous-with-override override
  810. ad-do-it)))
  811. ;; Work around `called-interactively-p' in Emacs 24.3 and earlier,
  812. ;; which always returns nil when `call-interactively' is advised.
  813. (when (not (and (featurep 'nadvice)
  814. (boundp 'called-interactively-p-functions)))
  815. (defadvice interactive-p (around ido-ubiquitous activate)
  816. "Return the correct result when `call-interactively' is advised.
  817. This advice completely overrides the original definition."
  818. (condition-case nil
  819. (setq ad-return-value
  820. (and (ido-ubiquitous--interactive-internal)
  821. (ido-ubiquitous--interactive-p-internal)))
  822. ;; In case of error in the advice, fall back to the default
  823. ;; implementation
  824. (error ad-do-it)))
  825. (defadvice called-interactively-p (around ido-ubiquitous activate)
  826. "Return the correct result when `call-interactively' is advised.
  827. This advice completely overrides the original definition."
  828. (condition-case nil
  829. (setq ad-return-value
  830. (and (or (ido-ubiquitous--interactive-internal)
  831. (not (eq kind 'interactive)))
  832. (ido-ubiquitous--interactive-p-internal)))
  833. ;; In case of error in the advice, fall back to the default
  834. ;; implementation
  835. (error ad-do-it))))
  836. ;;; Other
  837. ;; This is defined at the end so it goes at the bottom of the
  838. ;; customization group
  839. ;;;###autoload
  840. (define-minor-mode ido-ubiquitous-debug-mode
  841. "If non-nil, ido-ubiquitous will print debug info.
  842. Debug info is printed to the *Messages* buffer."
  843. nil
  844. :global t
  845. :group 'ido-ubiquitous)
  846. (defsubst ido-ubiquitous--fixup-old-advice ()
  847. ;; Clean up old versions of ido-ubiquitous advice if they exist
  848. (ignore-errors (ad-remove-advice 'completing-read 'around 'ido-ubiquitous))
  849. (ignore-errors (ad-remove-advice 'ido-completing-read 'around 'detect-replacing-cr))
  850. (ignore-errors (ad-remove-advice 'ido-magic-forward-char 'before 'ido-ubiquitous-fallback))
  851. (ignore-errors (ad-remove-advice 'ido-magic-backward-char 'before 'ido-ubiquitous-fallback))
  852. (ignore-errors (ad-remove-advice 'ido-exit-minibuffer 'around 'compatibility))
  853. (ad-activate-all))
  854. (defsubst ido-ubiquitous--fixup-old-magit-overrides ()
  855. (let ((old-override '(disable prefix "magit-"))
  856. (new-override '(disable exact "magit-builtin-completing-read")))
  857. (when (member old-override
  858. ido-ubiquitous-command-overrides)
  859. (customize-set-variable
  860. 'ido-ubiquitous-command-overrides
  861. (remove old-override ido-ubiquitous-command-overrides))
  862. (unless (member new-override ido-ubiquitous-function-overrides)
  863. (customize-set-variable 'ido-ubiquitous-function-overrides
  864. (append ido-ubiquitous-function-overrides
  865. (list new-override))))
  866. (display-warning
  867. 'ido-ubiquitous
  868. "Fixing obsolete magit overrides.
  869. Magit has changed recently such that the old override that
  870. ido-ubiquitous defined for it now causes problems. This old
  871. override has been automatically removed and the new one added.
  872. Please use `M-x customize-group ido-ubiquitous' and review the
  873. override variables and save them to your customization file."
  874. :warning))))
  875. (defun ido-ubiquitous-initialize ()
  876. "Do initial setup for ido-ubiquitous.
  877. This only needs to be called once when the file is first loaded.
  878. It cleans up any traces of old versions of ido-ubiquitous and
  879. then sets up the mode."
  880. (ido-ubiquitous--fixup-old-advice)
  881. (ido-ubiquitous--fixup-old-magit-overrides)
  882. ;; Make sure the mode is turned on/off as specified by the value of
  883. ;; the mode variable
  884. (ido-ubiquitous-mode (if ido-ubiquitous-mode 1 0)))
  885. (ido-ubiquitous-initialize)
  886. (provide 'ido-ubiquitous)
  887. ;; Local Variables:
  888. ;; indent-tabs-mode: nil
  889. ;; End:
  890. ;;; ido-ubiquitous.el ends here