ido-ubiquitous.el 39 KB

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