ido-ubiquitous.el 36 KB

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