ido-ubiquitous.el 39 KB

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