ido-ubiquitous.el 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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.15
  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.15"
  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. "Default value of `ido-ubiquitous-function-overrides'.
  291. You can restore these using the command `ido-ubiquitous-restore-default-overrides'.")
  292. (defcustom ido-ubiquitous-command-overrides ido-ubiquitous-default-command-overrides
  293. "List of command override specifications for ido-ubiquitous
  294. Each override specification describes how ido-ubiquitous should
  295. behave one or many commands. A specification has the
  296. form `(BEHAVIOR MATCH-TYPE MATCH-TEXT)'. BEHAVIOR is one of the
  297. following:
  298. * `disable': ido-ubiquitous should not be used at all for the
  299. specified commands;
  300. * `enable': ido-ubiquitous may be used with the specified
  301. commands, without emulating the old-style default selection
  302. of `completing-read';
  303. * `enable-old': ido-ubiquitous may be used with the specified
  304. commands, and should emulate the old-style default selection
  305. of `completing-read'.
  306. MATCH-TYPE affects how MATCH-TEXT is interpreted, as follows:
  307. * `exact': the specification only affects the one command
  308. whose name is MATCH-TEXT;
  309. * `prefix': the specification affects any command whose name
  310. starts with MATCH-TEXT (This is useful for specifying a
  311. certain behavior for an entire package);
  312. * `regexp': the specification affects any command whose name
  313. matches MATCH-TEXT (with MATCH-TEXT being interpreted as a
  314. regular expression)
  315. MATCH-TEXT should be a string.
  316. Since this variable's has a somewhat complex structure, it is
  317. recommended that you set this variable through Customize.
  318. Note that this variable only affects *commands*, which are
  319. functions marked as interactive. See
  320. `ido-ubiquitous-function-overrides' for how to modify the
  321. behavior of ido-ubiquitous for arbitrary functions.
  322. If you need to add a new specification to this list, please also
  323. file a bug report at https://github.com/DarwinAwardWinner/ido-ubiquitous/issues"
  324. :type '(repeat ido-ubiquitous-command-override-spec)
  325. :group 'ido-ubiquitous)
  326. (defmacro ido-ubiquitous-with-override (override &rest body)
  327. "Eval BODY with specicified OVERRIDE in place.
  328. The OVERRIDE argument is evaluated normally, so if it is a
  329. literal symbol, it must be quoted.
  330. See `ido-ubiquitous-command-overrides' for valid override types."
  331. ;; Eval override
  332. `(let ((ido-ubiquitous-next-override ,override))
  333. ,@body))
  334. (put 'ido-ubiquitous-with-override 'lisp-indent-function
  335. (get 'prog1 'lisp-indent-function))
  336. (defun ido-ubiquitous-apply-function-override (func override)
  337. "Set the override property on FUNC to OVERRIDE and set up advice to apply the override."
  338. (setq func (ido-ubiquitous--as-symbol func)
  339. override (ido-ubiquitous--as-symbol override))
  340. (put func 'ido-ubiquitous-override override)
  341. (when override
  342. (let ((docstring
  343. (format "Override ido-ubiquitous behavior in %s if its `ido-ubiquitous-override' property is non-nil." func)))
  344. (eval
  345. `(defadvice ,func (around ido-ubiquitous-override activate)
  346. ,docstring
  347. (ido-ubiquitous-with-override
  348. (get ',func 'ido-ubiquitous-override)
  349. ad-do-it))))))
  350. (defun ido-ubiquitous-set-function-overrides (sym newval)
  351. "Custom setter function for `ido-ubiquitous-function-overrides'.
  352. In addition to setting the variable, this also sets up advice on
  353. each function to apply the appropriate override."
  354. ;; Unset all previous overrides
  355. (when (boundp sym)
  356. (let ((oldval (eval sym)))
  357. (cl-loop for (_action _match-type func) in oldval
  358. do (ido-ubiquitous-apply-function-override func nil))))
  359. ;; Ensure that function names are strings, not symbols
  360. (setq newval
  361. (cl-loop for (action match-type func) in newval
  362. collect (list action match-type
  363. (ido-ubiquitous--as-string func))))
  364. (set-default sym newval)
  365. ;; set new overrides
  366. (cl-loop for (action _match-type func) in (eval sym)
  367. do (ido-ubiquitous-apply-function-override func action)))
  368. (defcustom ido-ubiquitous-function-overrides ido-ubiquitous-default-function-overrides
  369. "List of function override specifications for ido-ubiquitous
  370. Function override specifications have a similar structure to
  371. command override specifications (see
  372. `ido-ubiquitous-command-overrides'). A function override
  373. specification has the form `(BEHAVIOR MATCH-TYPE MATCH-TEXT)'.
  374. However, `MATCH-TYPE' may ONLY be `exact'; No other match type is
  375. supported.
  376. If you need to add a new specification to this list, please also file a
  377. bug report at https://github.com/DarwinAwardWinner/ido-ubiquitous/issues
  378. Setting this variable directly has no effect. You must set it
  379. through Customize."
  380. :type '(repeat ido-ubiquitous-function-override-spec)
  381. :set 'ido-ubiquitous-set-function-overrides
  382. :group 'ido-ubiquitous)
  383. (defcustom ido-ubiquitous-allow-on-functional-collection nil
  384. "Allow ido completion when COLLECTION is a function.
  385. The `completing-read' function allows its COLLECTION argument to
  386. be a function instead of a list of choices. Some such functions
  387. simply return a list of completions and are suitable for use with
  388. ido, but others implement more complex behavior and will result
  389. in incorrect behavior if used with ido. Since there is no way to
  390. tell the difference, this preference defaults to nil, which means
  391. that ido-ubiquitous will not work when COLLECTION is a function
  392. unless there is a specific override in effect. To disable this
  393. safeguard and guarantee breakage on some functions, you may set
  394. this to non-nil, but this is not recommended."
  395. :type 'boolean
  396. :group 'ido-ubiquitous)
  397. ;;; ido-ubiquitous core
  398. (defvar ido-ubiquitous-next-call-replaces-completing-read nil
  399. "If t, then the next call to `ido-completing-read' is by ido-ubiquitous.")
  400. (defvar ido-ubiquitous-this-call-replaces-completing-read nil
  401. "If t, then the current call to `ido-completing-read' is by ido-ubiquitous.")
  402. (defvar ido-ubiquitous-next-override nil
  403. "This holds the override to be applied on the next call to `completing-read'.")
  404. (defvar ido-ubiquitous-active-override nil
  405. "This holds the override being applied to the current call to `completing-read'.")
  406. (defun ido-ubiquitous-completing-read (&rest args)
  407. "Wrapper for `ido-completing-read' that enables ido-ubiquitous features.
  408. Unlike `ido-completing-read', this function can return with
  409. `ido-exit' set to `fallback', and any function that calls this
  410. should check the value of `ido-exit' and handle this case
  411. appropriately. For example, see `completing-read-ido'."
  412. (let ((ido-ubiquitous-next-call-replaces-completing-read t))
  413. (apply 'ido-completing-read args)))
  414. (defadvice ido-completing-read (around detect-replacing-cr activate)
  415. "Enable workarounds if this call was done through ido-ubiquitous.
  416. This advice implements the logic required for
  417. `ido-completing-read' to handle a number of special cases that
  418. `completing-read' can handle. It only has an effect if
  419. `ido-completing-read' is called through
  420. `ido-ubiquitous-completing-read', so other packages that use
  421. `ido-completing-read', such as `smex', will not be affected."
  422. (let* ((orig-args (ad-get-args 0))
  423. (ido-ubiquitous-this-call-replaces-completing-read
  424. ido-ubiquitous-next-call-replaces-completing-read)
  425. (ido-ubiquitous-next-call-replaces-completing-read nil)
  426. (error-during-setup nil))
  427. (when ido-ubiquitous-this-call-replaces-completing-read
  428. (condition-case nil
  429. (progn
  430. ;; ido doesn't natively handle DEF being a list. If DEF is
  431. ;; a list, prepend it to CHOICES and set DEF to just the
  432. ;; car of the default list.
  433. (when (and def (listp def))
  434. (setq choices
  435. (append def
  436. (nreverse (cl-set-difference choices def)))
  437. def (car def)))
  438. ;; Work around a bug in ido when both INITIAL-INPUT and
  439. ;; DEF are provided More info:
  440. ;; https://github.com/technomancy/ido-ubiquitous/issues/18
  441. (let ((initial (cond ((null initial-input) "")
  442. ((stringp initial-input) initial-input)
  443. ((consp initial-input) (car initial-input))
  444. (t initial-input))))
  445. (when (and def initial
  446. (stringp initial)
  447. (not (string= initial "")))
  448. ;; Both default and initial input were provided. So
  449. ;; keep the initial input and preprocess the choices
  450. ;; list to put the default at the head, then proceed
  451. ;; with default = nil.
  452. (setq choices (cons def (remove def choices))
  453. def nil))))
  454. (error
  455. (progn
  456. (warn "ido-ubiquitous: failed during setup. Falling back to standard completion")
  457. (setq error-during-setup t)))))
  458. ;; For ido-ubiquitous, only attempt ido completion if setup
  459. ;; completed without error
  460. (if (not error-during-setup)
  461. ad-do-it
  462. (setq ad-return-value
  463. (apply ido-ubiquitous-fallback-completing-read-function
  464. orig-args)))))
  465. (defun completing-read-ido (prompt collection &optional predicate
  466. require-match initial-input
  467. hist def inherit-input-method)
  468. "ido-based method for reading from the minibuffer with completion.
  469. See `completing-read' for the meaning of the arguments.
  470. This function is a wrapper for `ido-completing-read' designed to
  471. be used as the value of `completing-read-function'. Importantly,
  472. it detects edge cases that ido cannot handle and uses normal
  473. completion for them."
  474. (let* (;; Save the original arguments in case we need to do the
  475. ;; fallback
  476. (orig-args
  477. (list prompt collection predicate require-match
  478. initial-input hist def inherit-input-method))
  479. ;; Set the active override and clear the "next" one so it
  480. ;; doesn't apply to nested calls.
  481. (ido-ubiquitous-active-override ido-ubiquitous-next-override)
  482. (ido-ubiquitous-next-override nil)
  483. ;; Check for conditions that ido can't or shouldn't handle
  484. (ido-allowed
  485. (and ido-mode
  486. ido-ubiquitous-mode
  487. ;; Check for disable override
  488. (not (eq ido-ubiquitous-active-override 'disable))
  489. ;; Can't handle this arg
  490. (not inherit-input-method)
  491. ;; Can't handle this being set
  492. (not (bound-and-true-p completion-extra-properties))))
  493. ;; Check if ido can handle this collection. If collection is
  494. ;; a function, require an override to be ok. Also,
  495. ;; collection-ok should never be true when ido-allowed is
  496. ;; false.
  497. (collection-ok
  498. (and ido-allowed
  499. (or ido-ubiquitous-allow-on-functional-collection
  500. (not (functionp collection))
  501. (memq ido-ubiquitous-active-override '(enable enable-old)))))
  502. ;; Pre-expand list of possible completions, but only if we
  503. ;; have a chance of using ido. This is executed after the
  504. ;; ido-allowed check to avoid unnecessary work if ido isn't
  505. ;; going to used.
  506. (_ignore ;; (Return value doesn't matter).
  507. (when (and ido-allowed collection-ok)
  508. (setq collection (all-completions "" collection predicate)
  509. ;; Don't need this any more
  510. predicate nil)))
  511. (collection-ok
  512. ;; Don't use ido if the collection is empty or too large.
  513. (and collection-ok
  514. collection
  515. (or (null ido-ubiquitous-max-items)
  516. (<= (length collection) ido-ubiquitous-max-items))))
  517. ;; Final check for everything
  518. (ido-allowed (and ido-allowed collection-ok))
  519. (result
  520. (if ido-allowed
  521. (ido-ubiquitous-completing-read prompt collection
  522. predicate require-match initial-input hist def
  523. inherit-input-method)
  524. (setq ido-exit 'fallback))))
  525. ;; (message "Result: %S" result)
  526. ;; (message "ido-exit: %S" ido-exit)
  527. ;; Do the fallback if necessary. This could either be because ido
  528. ;; can't handle the arguments, or the user indicated during
  529. ;; completion that they wanted to fall back to non-ido completion.
  530. (if (memq 'fallback (list ido-exit result))
  531. (apply ido-ubiquitous-fallback-completing-read-function
  532. orig-args)
  533. result)))
  534. ;; Fallback on magic C-f and C-b
  535. (defadvice ido-magic-forward-char (before ido-ubiquitous-fallback activate)
  536. "Allow falling back in ido-ubiquitous."
  537. (when ido-ubiquitous-this-call-replaces-completing-read
  538. ;; `ido-context-switch-command' is already let-bound at this
  539. ;; point.
  540. (setq ido-context-switch-command #'ido-fallback-command)))
  541. (defadvice ido-magic-backward-char (before ido-ubiquitous-fallback activate)
  542. "Allow falling back in ido-ubiquitous."
  543. (when ido-ubiquitous-this-call-replaces-completing-read
  544. ;; `ido-context-switch-command' is already let-bound at this
  545. ;; point.
  546. (setq ido-context-switch-command #'ido-fallback-command)))
  547. ;;; Old-style default support
  548. (defvar ido-ubiquitous-initial-item nil
  549. "The first item selected when ido starts.
  550. This is initialized to the first item in the list of completions
  551. when ido starts, and is cleared when any character is entered
  552. into the prompt or the list is cycled. If it is non-nil and still
  553. equal to the first item in the completion list when ido exits,
  554. then if `ido-ubiquitous-enable-old-style-default' is
  555. non-nil, ido returns an empty string instead of the first item on
  556. the list.")
  557. (defadvice ido-read-internal (before clear-initial-item activate)
  558. (setq ido-ubiquitous-initial-item nil))
  559. (defadvice ido-make-choice-list (after set-initial-item activate)
  560. (when (and ad-return-value (listp ad-return-value))
  561. (setq ido-ubiquitous-initial-item (car ad-return-value))))
  562. (defadvice ido-next-match (after clear-initial-item activate)
  563. (setq ido-ubiquitous-initial-item nil))
  564. (defadvice ido-prev-match (after clear-initial-item activate)
  565. (setq ido-ubiquitous-initial-item nil))
  566. (defadvice ido-exit-minibuffer (around compatibility activate)
  567. "Emulate a quirk of `completing-read'.
  568. > If the input is null, `completing-read' returns DEF, or the
  569. > first element of the list of default values, or an empty string
  570. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  571. See `ido-ubiquitous-enable-old-style-default', which
  572. controls whether this advice has any effect."
  573. (condition-case nil
  574. (let* ((enable-oldstyle
  575. (and
  576. ;; Completing a list, not a buffer or file
  577. (eq ido-cur-item 'list)
  578. ;; Only enable if we are replacing `completing-read'
  579. ido-ubiquitous-this-call-replaces-completing-read
  580. ;; Default is nil
  581. (null ido-default-item)
  582. ;; Input is empty
  583. (string= ido-text "")
  584. ;; Old-style default enabled
  585. (if ido-ubiquitous-active-override
  586. (eq ido-ubiquitous-active-override 'enable-old)
  587. ido-ubiquitous-enable-old-style-default)
  588. ;; First item on the list hasn't changed
  589. (string= (car ido-cur-list)
  590. ido-ubiquitous-initial-item)))
  591. ;; Prefix inverts oldstyle behavior
  592. (should-invert current-prefix-arg)
  593. (actually-enable-oldstyle
  594. (if should-invert (not enable-oldstyle) enable-oldstyle)))
  595. (if actually-enable-oldstyle
  596. (ido-select-text)
  597. ad-do-it))
  598. (error ad-do-it))
  599. (setq ido-ubiquitous-initial-item nil))
  600. ;;; Overrides
  601. (defun ido-ubiquitous-restore-default-overrides (&optional save)
  602. "Re-add the default overrides for ido-ubiquitous.
  603. This will ensure that the default overrides are all present and
  604. at the head of the list in `ido-ubiquitous-command-overrides' and
  605. `ido-ubiquitous-function-overrides'. User-added overrides will
  606. not be removed, but they may be masked if one of the default
  607. overrides affects the same functions.
  608. With a prefix arg, also save the above variables' new values for
  609. future sessions."
  610. (interactive "P")
  611. (let ((setter (if save
  612. 'customize-save-variable
  613. 'customize-set-variable)))
  614. (cl-loop for (var def) in '((ido-ubiquitous-command-overrides
  615. ido-ubiquitous-default-command-overrides)
  616. (ido-ubiquitous-function-overrides
  617. ido-ubiquitous-default-function-overrides))
  618. do (let* ((curval (eval var))
  619. (defval (eval def))
  620. (newval (delete-dups (append defval curval))))
  621. (funcall setter var newval)))
  622. (message (if save
  623. "ido-ubiquitous: Restored default command and function overrides and saved for future sessions."
  624. "ido-ubiquitous: Restored default command and function overrides for current session only."))))
  625. (defun ido-ubiquitous-spec-match (spec symbol)
  626. "Returns t if SPEC matches SYMBOL (which should be a function name).
  627. See `ido-ubiquitous-command-overrides'."
  628. (when (and symbol (symbolp symbol))
  629. (cl-destructuring-bind (type text) spec
  630. (let ((matcher (cdr (assoc type ido-ubiquitous-spec-matchers)))
  631. (text (ido-ubiquitous--as-string text))
  632. (symname (ido-ubiquitous--as-string symbol)))
  633. (when (null matcher)
  634. (error "ido-ubiquitous: Unknown match spec type \"%s\". See `ido-ubiquitous-spec-matchers' for valid types." type))
  635. (funcall matcher text symname)))))
  636. (defun ido-ubiquitous-get-command-override (cmd)
  637. "Return the override associated with the command CMD.
  638. If there is no override set for CMD in
  639. `ido-ubiquitous-command-overrides', return nil."
  640. (when (and cmd (symbolp cmd))
  641. (cl-loop for (action . spec) in ido-ubiquitous-command-overrides
  642. when (memq action '(disable enable enable-old nil))
  643. when (ido-ubiquitous-spec-match spec cmd)
  644. return action
  645. finally return nil)))
  646. ;;; Workaround for https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/24
  647. ;; When `call-interactively' is advised, `called-interactively-p'
  648. ;; always returns nil. So we redefine it (and `interactive-p') to test
  649. ;; the correct condition.
  650. (defsubst ido-ubiquitous--looks-like-advised-orig (func)
  651. "Returns t if FUNC is a symbol starting with \"ad-Orig-\".
  652. Such symbols are used to store the original definitions of
  653. functions that have been advised by `defadvice' or similar."
  654. (and (symbolp func)
  655. (string-prefix-p "ad-Orig-" (symbol-name func))))
  656. (defsubst ido-ubiquitous--looks-like-call-interactively (func)
  657. "Returns t if FUNC looks like the function `call-interactively'.
  658. FUNC \"looks like\" `call-interactively' if it is the literal
  659. symbol `call-interactively', or the value of `(symbol-function
  660. 'call-interactively)', or a symbol whose `symbol-function' is the
  661. same as that of `call-interactively'.
  662. This function is used to determine whether a given function was
  663. \"called by\" `call-interactively' and therefore was called
  664. interactively."
  665. (when func
  666. (eq (symbol-function 'call-interactively)
  667. (if (symbolp func)
  668. (symbol-function func)
  669. func))))
  670. (defun ido-ubiquitous--backtrace-from (fun)
  671. "Return all backtrace frames, starting with the one for FUN.
  672. FUN may be a list of functions, in which case the first one found
  673. on the stack will be used."
  674. (let ((stack
  675. (cl-loop for i upfrom 0
  676. for frame = (backtrace-frame i)
  677. while frame
  678. collect frame))
  679. (funcs (if (functionp fun)
  680. (list fun)
  681. fun)))
  682. (while (and stack
  683. (not (memq (cl-cadar stack) funcs)))
  684. (setq stack (cdr stack)))
  685. stack))
  686. (defun ido-ubiquitous--clean-advice-from-backtrace (stack)
  687. "Takes a stack trace and cleans all evidence of advice.
  688. Specifically, for each call to a function starting with
  689. \"ad-Orig-\", that call and all prior calls up to but not
  690. including the advised function's original name are deleted from
  691. the stack."
  692. (let ((skipping-until nil))
  693. (cl-loop for frame in stack
  694. for func = (cadr frame)
  695. ;; Check if we found the frame we we're skipping to
  696. if (and skipping-until
  697. (eq func skipping-until))
  698. do (setq skipping-until nil)
  699. ;; If we're looking at an the original form of an advised
  700. ;; function, skip until the real name of that function.
  701. if (and (not skipping-until)
  702. (ido-ubiquitous--looks-like-advised-orig func))
  703. do (setq skipping-until
  704. (intern
  705. (substring (symbol-name func)
  706. (eval-when-compile (length "ad-Orig-")))))
  707. unless skipping-until collect frame)))
  708. (defsubst ido-ubiquitous--interactive-internal ()
  709. "Eqivalent of the INTERACTIVE macro in the Emacs C source.
  710. This is an internal function that should never be called
  711. directly.
  712. See the C source for the logic behind this function."
  713. (and (not executing-kbd-macro)
  714. (not noninteractive)))
  715. (defun ido-ubiquitous--interactive-p-internal ()
  716. "Equivalent of C function \"interactive_p\".
  717. This is an internal function that should never be called
  718. directly.
  719. See the C source for the logic behind this function."
  720. (let ((stack
  721. ;; We clean advice from the backtrace. This ensures that we
  722. ;; get the right answer even if `call-interactively' has been
  723. ;; advised.
  724. (ido-ubiquitous--clean-advice-from-backtrace
  725. (cdr
  726. (ido-ubiquitous--backtrace-from
  727. '(called-interactively-p interactive-p))))))
  728. ;; See comments in the C function for the logic here.
  729. (while (and stack
  730. (or (eq (cl-cadar stack) 'bytecode)
  731. (null (caar stack))))
  732. (setq stack (cdr stack)))
  733. ;; Top of stack is now the function that we want to know
  734. ;; about. Pop it, then check if the next function is
  735. ;; `call-interactively', using a more permissive test than the default.
  736. (ido-ubiquitous--looks-like-call-interactively (cl-cadadr stack))))
  737. (defadvice call-interactively (around ido-ubiquitous activate)
  738. "Implements the behavior specified in `ido-ubiquitous-command-overrides'."
  739. (ido-ubiquitous-with-override
  740. (ido-ubiquitous-get-command-override (ad-get-arg 0))
  741. ad-do-it))
  742. ;; Work around `called-interactively-p' in Emacs 24.3 and earlier,
  743. ;; which always returns nil when `call-interactively' is advised.
  744. (when (not (and (featurep 'nadvice)
  745. (boundp 'called-interactively-p-functions)))
  746. (defadvice interactive-p (around ido-ubiquitous activate)
  747. "Return the correct result when `call-interactively' is advised.
  748. This advice completely overrides the original definition."
  749. (condition-case nil
  750. (setq ad-return-value
  751. (and (ido-ubiquitous--interactive-internal)
  752. (ido-ubiquitous--interactive-p-internal)))
  753. ;; In case of error in the advice, fall back to the default
  754. ;; implementation
  755. (error ad-do-it)))
  756. (defadvice called-interactively-p (around ido-ubiquitous activate)
  757. "Return the correct result when `call-interactively' is advised.
  758. This advice completely overrides the original definition."
  759. (condition-case nil
  760. (setq ad-return-value
  761. (and (or (ido-ubiquitous--interactive-internal)
  762. (not (eq kind 'interactive)))
  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. ;;; Other
  768. (defun ido-ubiquitous-warn-about-ido-disabled ()
  769. "Warn if ido-ubiquitous is enabled without ido.
  770. Don't warn if emacs is still initializing, since ido-ubiquitous
  771. could be enabled first during init."
  772. (when (and ido-ubiquitous-mode
  773. after-init-time
  774. (not (bound-and-true-p ido-mode)))
  775. (warn "ido-ubiquitous-mode enabled without ido mode. ido-ubiquitous requires ido mode to be enabled.")))
  776. (defun ido-ubiquitous-initialize ()
  777. "Do initial setup for ido-ubiquitous.
  778. This only needs to be called once when the file is first loaded."
  779. ;; Clean up old versions of ido-ubiquitous that defined advice on
  780. ;; `completing-read' instead of modifying
  781. ;; `completing-read-function'.
  782. (when (ad-find-advice 'completing-read 'around 'ido-ubiquitous)
  783. (ad-remove-advice 'completing-read 'around 'ido-ubiquitous)
  784. (ad-activate 'completing-read))
  785. ;; Make sure the mode is turned on/off as specified by the value of
  786. ;; the mode variable
  787. (ido-ubiquitous-mode (if ido-ubiquitous-mode 1 0)))
  788. (ido-ubiquitous-initialize)
  789. (provide 'ido-ubiquitous)
  790. ;;; ido-ubiquitous.el ends here