ido-ubiquitous.el 40 KB

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