ido-ubiquitous.el 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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. ;; These variable are used to make ido-ubiquitous work properly in the
  402. ;; case that `completing-read' is called recursively (which is
  403. ;; possible when `enable-recursive-minibuffers' is non-nil.)
  404. (defvar ido-ubiquitous-next-call-replaces-completing-read nil
  405. "If non-nil, then the next call to `ido-completing-read' is by ido-ubiquitous.")
  406. (defvar ido-ubiquitous-this-call-replaces-completing-read nil
  407. "If non-nil, then the current call to `ido-completing-read' is by ido-ubiquitous.")
  408. (defvar ido-ubiquitous-next-override nil
  409. "This holds the override to be applied on the next call to `completing-read'.
  410. It's value can be nil or one of the symbols `disable', `enable', or `enable-old'.
  411. You should not modify this variable directly. Instead use the macro `ido-ubiquitous-with-override'.")
  412. (defvar ido-ubiquitous-active-override nil
  413. "This holds the override being applied to the current call to `completing-read'.
  414. It's value can be nil or one of the symbols `disable', `enable', or `enable-old'.
  415. You should not modify this variable directly. Instead use the macro `ido-ubiquitous-with-override'.")
  416. (defun ido-ubiquitous-completing-read (&rest args)
  417. "Wrapper for `ido-completing-read' that enables ido-ubiquitous features.
  418. Unlike `ido-completing-read', this function can return with
  419. `ido-exit' set to `fallback', and any function that calls this
  420. should check the value of `ido-exit' and handle this case
  421. appropriately. For example, see `completing-read-ido'."
  422. (let ((ido-ubiquitous-next-call-replaces-completing-read t))
  423. (apply 'ido-completing-read args)))
  424. (defadvice ido-completing-read (around detect-replacing-cr activate)
  425. "Enable workarounds if this call was done through ido-ubiquitous.
  426. This advice implements the logic required for
  427. `ido-completing-read' to handle a number of special cases that
  428. `completing-read' can handle. It only has an effect if
  429. `ido-completing-read' is called through
  430. `ido-ubiquitous-completing-read', so other packages that use
  431. `ido-completing-read', such as `smex', will not be affected."
  432. (let* ((orig-args (ad-get-args 0))
  433. (ido-ubiquitous-this-call-replaces-completing-read
  434. ido-ubiquitous-next-call-replaces-completing-read)
  435. (ido-ubiquitous-next-call-replaces-completing-read nil)
  436. (error-during-setup nil))
  437. (when ido-ubiquitous-this-call-replaces-completing-read
  438. (condition-case nil
  439. (progn
  440. ;; ido doesn't natively handle DEF being a list. If DEF is
  441. ;; a list, prepend it to CHOICES and set DEF to just the
  442. ;; car of the default list.
  443. (when (and def (listp def))
  444. (setq choices
  445. (append def
  446. (nreverse (cl-set-difference choices def)))
  447. def (car def)))
  448. ;; Work around a bug in ido when both INITIAL-INPUT and
  449. ;; DEF are provided More info:
  450. ;; https://github.com/technomancy/ido-ubiquitous/issues/18
  451. (let ((initial (cond ((null initial-input) "")
  452. ((stringp initial-input) initial-input)
  453. ((consp initial-input) (car initial-input))
  454. (t initial-input))))
  455. (when (and def initial
  456. (stringp initial)
  457. (not (string= initial "")))
  458. ;; Both default and initial input were provided. So
  459. ;; keep the initial input and preprocess the choices
  460. ;; list to put the default at the head, then proceed
  461. ;; with default = nil.
  462. (setq choices (cons def (remove def choices))
  463. def nil))))
  464. (error
  465. (progn
  466. (warn "ido-ubiquitous: failed during setup. Falling back to standard completion")
  467. (setq error-during-setup t)))))
  468. ;; For ido-ubiquitous, only attempt ido completion if setup
  469. ;; completed without error
  470. (if (not error-during-setup)
  471. ad-do-it
  472. (setq ad-return-value
  473. (apply ido-ubiquitous-fallback-completing-read-function
  474. orig-args)))))
  475. (defun completing-read-ido (prompt collection &optional predicate
  476. require-match initial-input
  477. hist def inherit-input-method)
  478. "ido-based method for reading from the minibuffer with completion.
  479. See `completing-read' for the meaning of the arguments.
  480. This function is a wrapper for `ido-completing-read' designed to
  481. be used as the value of `completing-read-function'. Importantly,
  482. it detects edge cases that ido cannot handle and uses normal
  483. completion for them."
  484. (let* (;; Save the original arguments in case we need to do the
  485. ;; fallback
  486. (orig-args
  487. (list prompt collection predicate require-match
  488. initial-input hist def inherit-input-method))
  489. ;; Set the active override and clear the "next" one so it
  490. ;; doesn't apply to nested calls.
  491. (ido-ubiquitous-active-override ido-ubiquitous-next-override)
  492. (ido-ubiquitous-next-override nil)
  493. ;; Check for conditions that ido can't or shouldn't handle
  494. (ido-allowed
  495. (and ido-mode
  496. ido-ubiquitous-mode
  497. ;; Check for disable override
  498. (not (eq ido-ubiquitous-active-override 'disable))
  499. ;; Can't handle this arg
  500. (not inherit-input-method)
  501. ;; Can't handle this being set
  502. (not (bound-and-true-p completion-extra-properties))))
  503. ;; Check if ido can handle this collection. If collection is
  504. ;; a function, require an override to be ok. Also,
  505. ;; collection-ok should never be true when ido-allowed is
  506. ;; false.
  507. (collection-ok
  508. (and ido-allowed
  509. (or ido-ubiquitous-allow-on-functional-collection
  510. (not (functionp collection))
  511. (memq ido-ubiquitous-active-override '(enable enable-old)))))
  512. ;; Pre-expand list of possible completions, but only if we
  513. ;; have a chance of using ido. This is executed after the
  514. ;; ido-allowed check to avoid unnecessary work if ido isn't
  515. ;; going to used.
  516. (_ignore ;; (Return value doesn't matter).
  517. (when (and ido-allowed collection-ok)
  518. (setq collection (all-completions "" collection predicate)
  519. ;; Don't need this any more
  520. predicate nil)))
  521. (collection-ok
  522. ;; Don't use ido if the collection is empty or too large.
  523. (and collection-ok
  524. collection
  525. (or (null ido-ubiquitous-max-items)
  526. (<= (length collection) ido-ubiquitous-max-items))))
  527. ;; Final check for everything
  528. (ido-allowed (and ido-allowed collection-ok))
  529. (result
  530. (if ido-allowed
  531. (ido-ubiquitous-completing-read
  532. prompt collection
  533. predicate require-match initial-input hist def
  534. inherit-input-method)
  535. (setq ido-exit 'fallback))))
  536. ;; (message "Result: %S" result)
  537. ;; (message "ido-exit: %S" ido-exit)
  538. ;; Do the fallback if necessary. This could either be because ido
  539. ;; can't handle the arguments, or the user indicated during
  540. ;; completion that they wanted to fall back to non-ido completion.
  541. (if (memq 'fallback (list ido-exit result))
  542. (apply ido-ubiquitous-fallback-completing-read-function
  543. orig-args)
  544. result)))
  545. ;; Fallback on magic C-f and C-b
  546. (defadvice ido-magic-forward-char (before ido-ubiquitous-fallback activate)
  547. "Allow falling back in ido-ubiquitous."
  548. (when ido-ubiquitous-this-call-replaces-completing-read
  549. ;; `ido-context-switch-command' is already let-bound at this
  550. ;; point.
  551. (setq ido-context-switch-command #'ido-fallback-command)))
  552. (defadvice ido-magic-backward-char (before ido-ubiquitous-fallback activate)
  553. "Allow falling back in ido-ubiquitous."
  554. (when ido-ubiquitous-this-call-replaces-completing-read
  555. ;; `ido-context-switch-command' is already let-bound at this
  556. ;; point.
  557. (setq ido-context-switch-command #'ido-fallback-command)))
  558. ;;; Old-style default support
  559. (defvar ido-ubiquitous-initial-item nil
  560. "The first item selected when ido starts.
  561. This is initialized to the first item in the list of completions
  562. when ido starts, and is cleared when any character is entered
  563. into the prompt or the list is cycled. If it is non-nil and still
  564. equal to the first item in the completion list when ido exits,
  565. then if `ido-ubiquitous-enable-old-style-default' is
  566. non-nil, ido returns an empty string instead of the first item on
  567. the list.")
  568. (defadvice ido-read-internal (before clear-initial-item activate)
  569. (setq ido-ubiquitous-initial-item nil))
  570. (defadvice ido-make-choice-list (after set-initial-item activate)
  571. (when (and ad-return-value (listp ad-return-value))
  572. (setq ido-ubiquitous-initial-item (car ad-return-value))))
  573. (defadvice ido-next-match (after clear-initial-item activate)
  574. (setq ido-ubiquitous-initial-item nil))
  575. (defadvice ido-prev-match (after clear-initial-item activate)
  576. (setq ido-ubiquitous-initial-item nil))
  577. (defadvice ido-exit-minibuffer (around compatibility activate)
  578. "Emulate a quirk of `completing-read'.
  579. > If the input is null, `completing-read' returns DEF, or the
  580. > first element of the list of default values, or an empty string
  581. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  582. See `ido-ubiquitous-enable-old-style-default', which
  583. controls whether this advice has any effect."
  584. (condition-case nil
  585. (let* ((enable-oldstyle
  586. (and
  587. ;; Completing a list, not a buffer or file
  588. (eq ido-cur-item 'list)
  589. ;; Only enable if we are replacing `completing-read'
  590. ido-ubiquitous-this-call-replaces-completing-read
  591. ;; Default is nil
  592. (null ido-default-item)
  593. ;; Input is empty
  594. (string= ido-text "")
  595. ;; Old-style default enabled
  596. (if ido-ubiquitous-active-override
  597. (eq ido-ubiquitous-active-override 'enable-old)
  598. ido-ubiquitous-enable-old-style-default)
  599. ;; First item on the list hasn't changed
  600. (string= (car ido-cur-list)
  601. ido-ubiquitous-initial-item)))
  602. ;; Prefix inverts oldstyle behavior
  603. (should-invert current-prefix-arg)
  604. (actually-enable-oldstyle
  605. (if should-invert (not enable-oldstyle) enable-oldstyle)))
  606. (if actually-enable-oldstyle
  607. (ido-select-text)
  608. ad-do-it))
  609. (error ad-do-it))
  610. (setq ido-ubiquitous-initial-item nil))
  611. ;;; Overrides
  612. (defun ido-ubiquitous-restore-default-overrides (&optional save)
  613. "Re-add the default overrides for ido-ubiquitous.
  614. This will ensure that the default overrides are all present and
  615. at the head of the list in `ido-ubiquitous-command-overrides' and
  616. `ido-ubiquitous-function-overrides'. User-added overrides will
  617. not be removed, but they may be masked if one of the default
  618. overrides affects the same functions.
  619. With a prefix arg, also save the above variables' new values for
  620. future sessions."
  621. (interactive "P")
  622. (let ((setter (if save
  623. 'customize-save-variable
  624. 'customize-set-variable)))
  625. (cl-loop for (var def) in '((ido-ubiquitous-command-overrides
  626. ido-ubiquitous-default-command-overrides)
  627. (ido-ubiquitous-function-overrides
  628. ido-ubiquitous-default-function-overrides))
  629. do (let* ((curval (eval var))
  630. (defval (eval def))
  631. (newval (delete-dups (append defval curval))))
  632. (funcall setter var newval)))
  633. (message (if save
  634. "ido-ubiquitous: Restored default command and function overrides and saved for future sessions."
  635. "ido-ubiquitous: Restored default command and function overrides for current session only. Call again with prefix to save for future sessions."))))
  636. (defun ido-ubiquitous-spec-match (spec symbol)
  637. "Returns t if SPEC matches SYMBOL (which should be a function name).
  638. See `ido-ubiquitous-command-overrides'."
  639. (when (and symbol (symbolp symbol))
  640. (cl-destructuring-bind (type text) spec
  641. (let ((matcher (cdr (assoc type ido-ubiquitous-spec-matchers)))
  642. (text (ido-ubiquitous--as-string text))
  643. (symname (ido-ubiquitous--as-string symbol)))
  644. (when (null matcher)
  645. (error "ido-ubiquitous: Unknown match spec type \"%s\". See `ido-ubiquitous-spec-matchers' for valid types." type))
  646. (funcall matcher text symname)))))
  647. (defun ido-ubiquitous-get-command-override (cmd)
  648. "Return the override associated with the command CMD.
  649. If there is no override set for CMD in
  650. `ido-ubiquitous-command-overrides', return nil."
  651. (when (and cmd (symbolp cmd))
  652. (cl-loop for (action . spec) in ido-ubiquitous-command-overrides
  653. when (memq action '(disable enable enable-old nil))
  654. when (ido-ubiquitous-spec-match spec cmd)
  655. return action
  656. finally return nil)))
  657. ;;; Workaround for https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/24
  658. ;; When `call-interactively' is advised, `called-interactively-p'
  659. ;; always returns nil. So we redefine it (and `interactive-p') to test
  660. ;; the correct condition.
  661. (defsubst ido-ubiquitous--looks-like-advised-orig (func)
  662. "Returns t if FUNC is a symbol starting with \"ad-Orig-\".
  663. Such symbols are used to store the original definitions of
  664. functions that have been advised by `defadvice' or similar."
  665. (and (symbolp func)
  666. (string-prefix-p "ad-Orig-" (symbol-name func))))
  667. (defsubst ido-ubiquitous--looks-like-call-interactively (func)
  668. "Returns t if FUNC looks like the function `call-interactively'.
  669. FUNC \"looks like\" `call-interactively' if it is the literal
  670. symbol `call-interactively', or the value of `(symbol-function
  671. 'call-interactively)', or a symbol whose `symbol-function' is the
  672. same as that of `call-interactively'.
  673. This function is used to determine whether a given function was
  674. \"called by\" `call-interactively' and therefore was called
  675. interactively."
  676. (when func
  677. (eq (symbol-function 'call-interactively)
  678. (if (symbolp func)
  679. (symbol-function func)
  680. func))))
  681. (defun ido-ubiquitous--backtrace-from (fun)
  682. "Return all backtrace frames, starting with the one for FUN.
  683. FUN may be a list of functions, in which case the first one found
  684. on the stack will be used."
  685. (let ((stack
  686. (cl-loop for i upfrom 0
  687. for frame = (backtrace-frame i)
  688. while frame
  689. collect frame))
  690. (funcs (if (functionp fun)
  691. (list fun)
  692. fun)))
  693. (while (and stack
  694. (not (memq (cl-cadar stack) funcs)))
  695. (setq stack (cdr stack)))
  696. stack))
  697. (defun ido-ubiquitous--clean-advice-from-backtrace (stack)
  698. "Takes a stack trace and cleans all evidence of advice.
  699. Specifically, for each call to a function starting with
  700. \"ad-Orig-\", that call and all prior calls up to but not
  701. including the advised function's original name are deleted from
  702. the stack."
  703. (let ((skipping-until nil))
  704. (cl-loop for frame in stack
  705. for func = (cadr frame)
  706. ;; Check if we found the frame we we're skipping to
  707. if (and skipping-until
  708. (eq func skipping-until))
  709. do (setq skipping-until nil)
  710. ;; If we're looking at an the original form of an advised
  711. ;; function, skip until the real name of that function.
  712. if (and (not skipping-until)
  713. (ido-ubiquitous--looks-like-advised-orig func))
  714. do (setq skipping-until
  715. (intern
  716. (substring (symbol-name func)
  717. (eval-when-compile (length "ad-Orig-")))))
  718. unless skipping-until collect frame)))
  719. (defsubst ido-ubiquitous--interactive-internal ()
  720. "Eqivalent of the INTERACTIVE macro in the Emacs C source.
  721. This is an internal function that should never be called
  722. directly.
  723. See the C source for the logic behind this function."
  724. (and (not executing-kbd-macro)
  725. (not noninteractive)))
  726. (defun ido-ubiquitous--interactive-p-internal ()
  727. "Equivalent of C function \"interactive_p\".
  728. This is an internal function that should never be called
  729. directly.
  730. See the C source for the logic behind this function."
  731. (let ((stack
  732. ;; We clean advice from the backtrace. This ensures that we
  733. ;; get the right answer even if `call-interactively' has been
  734. ;; advised.
  735. (ido-ubiquitous--clean-advice-from-backtrace
  736. (cdr
  737. (ido-ubiquitous--backtrace-from
  738. '(called-interactively-p interactive-p))))))
  739. ;; See comments in the C function for the logic here.
  740. (while (and stack
  741. (or (eq (cl-cadar stack) 'bytecode)
  742. (null (caar stack))))
  743. (setq stack (cdr stack)))
  744. ;; Top of stack is now the function that we want to know
  745. ;; about. Pop it, then check if the next function is
  746. ;; `call-interactively', using a more permissive test than the default.
  747. (ido-ubiquitous--looks-like-call-interactively (cl-cadadr stack))))
  748. (defadvice call-interactively (around ido-ubiquitous activate)
  749. "Implements the behavior specified in `ido-ubiquitous-command-overrides'."
  750. (ido-ubiquitous-with-override
  751. (ido-ubiquitous-get-command-override (ad-get-arg 0))
  752. ad-do-it))
  753. ;; Work around `called-interactively-p' in Emacs 24.3 and earlier,
  754. ;; which always returns nil when `call-interactively' is advised.
  755. (when (not (and (featurep 'nadvice)
  756. (boundp 'called-interactively-p-functions)))
  757. (defadvice interactive-p (around ido-ubiquitous activate)
  758. "Return the correct result when `call-interactively' is advised.
  759. This advice completely overrides the original definition."
  760. (condition-case nil
  761. (setq ad-return-value
  762. (and (ido-ubiquitous--interactive-internal)
  763. (ido-ubiquitous--interactive-p-internal)))
  764. ;; In case of error in the advice, fall back to the default
  765. ;; implementation
  766. (error ad-do-it)))
  767. (defadvice called-interactively-p (around ido-ubiquitous activate)
  768. "Return the correct result when `call-interactively' is advised.
  769. This advice completely overrides the original definition."
  770. (condition-case nil
  771. (setq ad-return-value
  772. (and (or (ido-ubiquitous--interactive-internal)
  773. (not (eq kind 'interactive)))
  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. ;;; Other
  779. (defun ido-ubiquitous-warn-about-ido-disabled ()
  780. "Warn if ido-ubiquitous is enabled without ido.
  781. Don't warn if emacs is still initializing, since ido-ubiquitous
  782. could be enabled first during init."
  783. (when (and ido-ubiquitous-mode
  784. after-init-time
  785. (not (bound-and-true-p ido-mode)))
  786. (warn "ido-ubiquitous-mode enabled without ido mode. ido-ubiquitous requires ido mode to be enabled.")))
  787. (defun ido-ubiquitous-initialize ()
  788. "Do initial setup for ido-ubiquitous.
  789. This only needs to be called once when the file is first loaded."
  790. ;; Clean up old versions of ido-ubiquitous that defined advice on
  791. ;; `completing-read' instead of modifying
  792. ;; `completing-read-function'.
  793. (when (ad-find-advice 'completing-read 'around 'ido-ubiquitous)
  794. (ad-remove-advice 'completing-read 'around 'ido-ubiquitous)
  795. (ad-activate 'completing-read))
  796. ;; Make sure the mode is turned on/off as specified by the value of
  797. ;; the mode variable
  798. (ido-ubiquitous-mode (if ido-ubiquitous-mode 1 0)))
  799. (ido-ubiquitous-initialize)
  800. (provide 'ido-ubiquitous)
  801. ;; Local Variables:
  802. ;; indent-tabs-mode: nil
  803. ;; End:
  804. ;;; ido-ubiquitous.el ends here