ido-ubiquitous.el 39 KB

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