ido-ubiquitous.el 38 KB

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