ido-ubiquitous.el 39 KB

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