ido-ubiquitous.el 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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. (disable prefix "magit-")
  248. ;; https://github.com/bbatsov/prelude/issues/488
  249. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/44
  250. ;; tmm implements its own non-standard completion mechanics
  251. (disable prefix "tmm-")
  252. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/47
  253. ;; theme functions don't need old-style compatibility
  254. (enable regexp "\\`\\(load\\|enable\\|disable\\|describe\\|custom-theme-visit\\)-theme\\'")
  255. )
  256. "Default value of `ido-ubiquitous-command-overrides'.
  257. You can restore these using the command `ido-ubiquitous-restore-default-overrides'.")
  258. (defconst ido-ubiquitous-default-function-overrides
  259. '((disable exact "read-file-name")
  260. (disable exact "read-file-name-internal")
  261. (disable exact "read-buffer")
  262. (disable exact "gnus-emacs-completing-read")
  263. (disable exact "gnus-iswitchb-completing-read")
  264. (disable exact "grep-read-files")
  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. ad-do-it))
  427. ;; Signal used to trigger fallback
  428. (define-error 'ido-ubiquitous-fallback "ido-ubiquitous-fallback")
  429. (defun completing-read-ido-ubiquitous
  430. (prompt collection &optional predicate
  431. require-match initial-input
  432. hist def inherit-input-method)
  433. "ido-based method for reading from the minibuffer with completion.
  434. See `completing-read' for the meaning of the arguments.
  435. This function is a wrapper for `ido-completing-read' designed to
  436. be used as the value of `completing-read-function'. Importantly,
  437. it detects edge cases that ido cannot handle and uses normal
  438. completion for them."
  439. (let (;; Save the original arguments in case we need to do the
  440. ;; fallback
  441. (orig-args
  442. (list prompt collection predicate require-match
  443. initial-input hist def inherit-input-method)))
  444. (condition-case nil
  445. (let* (;; Set the active override and clear the "next" one so it
  446. ;; doesn't apply to nested calls.
  447. (ido-ubiquitous-active-override ido-ubiquitous-next-override)
  448. (ido-ubiquitous-next-override nil)
  449. ;; Apply the active override, if any
  450. (ido-ubiquitous-active-state
  451. (or ido-ubiquitous-active-override
  452. ido-ubiquitous-default-state
  453. 'enable)))
  454. ;; If ido-ubiquitous is disabled this time, fall back
  455. (when (eq ido-ubiquitous-active-state 'disable)
  456. (signal 'ido-ubiquitous-fallback nil))
  457. ;; Handle a collection that is a function: either expand
  458. ;; completion list now or fall back
  459. (when (functionp collection)
  460. (if (or ido-ubiquitous-allow-on-functional-collection
  461. (memq ido-ubiquitous-active-override
  462. '(enable enable-old)))
  463. (setq collection (all-completions "" collection predicate))
  464. (signal 'ido-ubiquitous-fallback nil)))
  465. (let ((ido-ubiquitous-enable-next-call t))
  466. (ido-completing-read+
  467. prompt collection predicate require-match
  468. initial-input hist def inherit-input-method)))
  469. ;; Handler for ido-ubiquitous-fallback signal
  470. (ido-ubiquitous-fallback
  471. (message "Ido-ubiquitous falling back")
  472. (apply ido-cr+-fallback-function orig-args)))))
  473. ;;; Old-style default support
  474. (defvar ido-ubiquitous-initial-item nil
  475. "The first item selected when ido starts.
  476. TODO Rewrite this
  477. This is initialized to the first item in the list of completions
  478. when ido starts, and is cleared when any character is entered
  479. into the prompt or the list is cycled. If it is non-nil and still
  480. equal to the first item in the completion list when ido exits,
  481. then if `ido-ubiquitous-enable-old-style-default' is
  482. non-nil, ido returns an empty string instead of the first item on
  483. the list.")
  484. (defadvice ido-read-internal (before clear-initial-item activate)
  485. (setq ido-ubiquitous-initial-item nil))
  486. (defadvice ido-make-choice-list (after set-initial-item activate)
  487. (setq ido-ubiquitous-initial-item
  488. (when (and ad-return-value (listp ad-return-value))
  489. (car ad-return-value))))
  490. (defadvice ido-next-match (after clear-initial-item activate)
  491. (setq ido-ubiquitous-initial-item nil))
  492. (defadvice ido-prev-match (after clear-initial-item activate)
  493. (setq ido-ubiquitous-initial-item nil))
  494. (defun ido-ubiquitous-should-use-old-style-default ()
  495. "Returns non nil if ido-ubiquitous should emulate old-style default.
  496. This function simply encapsulates all the checks that need to be
  497. done in order to decide whether to swap RET and C-j. See
  498. `ido-ubiquitous-default-state' for more information."
  499. (and
  500. ;; Only if completing a list, not a buffer or file
  501. (eq ido-cur-item 'list)
  502. ;; Only if this call was done through ido-ubiquitous
  503. ido-ubiquitous-enable-this-call
  504. ;; Only if default is nil
  505. (null ido-default-item)
  506. ;; Only if input is empty
  507. (string= ido-text "")
  508. ;; Only if old-style default enabled
  509. (eq ido-ubiquitous-active-state 'enable-old)
  510. ;; Only if `ido-ubiquitous-initial-item' hasn't been cleared
  511. ido-ubiquitous-initial-item
  512. ;; Only if initial item hasn't changed
  513. (string= (car ido-cur-list)
  514. ido-ubiquitous-initial-item)))
  515. (defadvice ido-exit-minibuffer (around old-style-default-compat activate)
  516. "Emulate a quirk of `completing-read'.
  517. > If the input is null, `completing-read' returns DEF, or the
  518. > first element of the list of default values, or an empty string
  519. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  520. See `ido-ubiquitous-default-state', which controls whether this
  521. advice has any effect."
  522. (condition-case nil
  523. (if (ido-ubiquitous-should-use-old-style-default)
  524. (ido-select-text)
  525. ad-do-it)
  526. (error ad-do-it))
  527. (setq ido-ubiquitous-initial-item nil))
  528. (defadvice ido-select-text (around old-style-default-compat activate)
  529. "Emulate a quirk of `completing-read'.
  530. > If the input is null, `completing-read' returns DEF, or the
  531. > first element of the list of default values, or an empty string
  532. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  533. See `ido-ubiquitous-default-state', which controls whether this
  534. advice has any effect."
  535. (condition-case nil
  536. (if (ido-ubiquitous-should-use-old-style-default)
  537. (ido-exit-minibuffer)
  538. ad-do-it)
  539. (error ad-do-it))
  540. (setq ido-ubiquitous-initial-item nil))
  541. ;;; Overrides
  542. (defun ido-ubiquitous-restore-default-overrides (&optional save)
  543. "Re-add the default overrides for ido-ubiquitous.
  544. This will ensure that the default overrides are all present and
  545. at the head of the list in `ido-ubiquitous-command-overrides' and
  546. `ido-ubiquitous-function-overrides'. User-added overrides will
  547. not be removed, but they may be masked if one of the default
  548. overrides affects the same functions.
  549. With a prefix arg, also save the above variables' new values for
  550. future sessions."
  551. (interactive "P")
  552. (let ((setter (if save
  553. 'customize-save-variable
  554. 'customize-set-variable)))
  555. (cl-loop for (var def) in '((ido-ubiquitous-command-overrides
  556. ido-ubiquitous-default-command-overrides)
  557. (ido-ubiquitous-function-overrides
  558. ido-ubiquitous-default-function-overrides))
  559. do (let* ((curval (eval var))
  560. (defval (eval def))
  561. (newval (delete-dups (append defval curval))))
  562. (funcall setter var newval)))
  563. (message (if save
  564. "ido-ubiquitous: Restored default command and function overrides and saved for future sessions."
  565. "ido-ubiquitous: Restored default command and function overrides for current session only. Call again with prefix to save for future sessions."))))
  566. (defun ido-ubiquitous-spec-match (spec symbol)
  567. "Returns t if SPEC matches SYMBOL (which should be a function name).
  568. See `ido-ubiquitous-command-overrides'."
  569. (when (and symbol (symbolp symbol))
  570. (cl-destructuring-bind (type text) spec
  571. (let ((matcher (cdr (assoc type ido-ubiquitous-spec-matchers)))
  572. (text (ido-ubiquitous--as-string text))
  573. (symname (ido-ubiquitous--as-string symbol)))
  574. (when (null matcher)
  575. (error "ido-ubiquitous: Unknown match spec type \"%s\". See `ido-ubiquitous-spec-matchers' for valid types." type))
  576. (funcall matcher text symname)))))
  577. (defun ido-ubiquitous-get-command-override (cmd)
  578. "Return the override associated with the command CMD.
  579. If there is no override set for CMD in
  580. `ido-ubiquitous-command-overrides', return nil."
  581. (when (and cmd (symbolp cmd))
  582. (cl-loop for (action . spec) in ido-ubiquitous-command-overrides
  583. when (memq action '(disable enable enable-old nil))
  584. when (ido-ubiquitous-spec-match spec cmd)
  585. return action
  586. finally return nil)))
  587. ;;; Workaround for https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/24
  588. ;; When `call-interactively' is advised, `called-interactively-p'
  589. ;; always returns nil. So we redefine it (and `interactive-p') to test
  590. ;; the correct condition.
  591. (defsubst ido-ubiquitous--looks-like-advised-orig (func)
  592. "Returns t if FUNC is a symbol starting with \"ad-Orig-\".
  593. Such symbols are used to store the original definitions of
  594. functions that have been advised by `defadvice' or similar."
  595. (and (symbolp func)
  596. (string-prefix-p "ad-Orig-" (symbol-name func))))
  597. (defsubst ido-ubiquitous--looks-like-call-interactively (func)
  598. "Returns t if FUNC looks like the function `call-interactively'.
  599. FUNC \"looks like\" `call-interactively' if it is the literal
  600. symbol `call-interactively', or the value of `(symbol-function
  601. 'call-interactively)', or a symbol whose `symbol-function' is the
  602. same as that of `call-interactively'.
  603. This function is used to determine whether a given function was
  604. \"called by\" `call-interactively' and therefore was called
  605. interactively."
  606. (when func
  607. (eq (symbol-function 'call-interactively)
  608. (if (symbolp func)
  609. (symbol-function func)
  610. func))))
  611. (defun ido-ubiquitous--backtrace-from (fun)
  612. "Return all backtrace frames, starting with the one for FUN.
  613. FUN may be a list of functions, in which case the first one found
  614. on the stack will be used."
  615. (let ((stack
  616. (cl-loop for i upfrom 0
  617. for frame = (backtrace-frame i)
  618. while frame
  619. collect frame))
  620. (funcs (if (functionp fun)
  621. (list fun)
  622. fun)))
  623. (while (and stack
  624. (not (memq (cl-cadar stack) funcs)))
  625. (setq stack (cdr stack)))
  626. stack))
  627. (defun ido-ubiquitous--clean-advice-from-backtrace (stack)
  628. "Takes a stack trace and cleans all evidence of advice.
  629. Specifically, for each call to a function starting with
  630. \"ad-Orig-\", that call and all prior calls up to but not
  631. including the advised function's original name are deleted from
  632. the stack."
  633. (let ((skipping-until nil))
  634. (cl-loop for frame in stack
  635. for func = (cadr frame)
  636. ;; Check if we found the frame we we're skipping to
  637. if (and skipping-until
  638. (eq func skipping-until))
  639. do (setq skipping-until nil)
  640. ;; If we're looking at an the original form of an advised
  641. ;; function, skip until the real name of that function.
  642. if (and (not skipping-until)
  643. (ido-ubiquitous--looks-like-advised-orig func))
  644. do (setq skipping-until
  645. (intern
  646. (substring (symbol-name func)
  647. (eval-when-compile (length "ad-Orig-")))))
  648. unless skipping-until collect frame)))
  649. (defsubst ido-ubiquitous--interactive-internal ()
  650. "Eqivalent of the INTERACTIVE macro in the Emacs C source.
  651. This is an internal function that should never be called
  652. directly.
  653. See the C source for the logic behind this function."
  654. (and (not executing-kbd-macro)
  655. (not noninteractive)))
  656. (defun ido-ubiquitous--interactive-p-internal ()
  657. "Equivalent of C function \"interactive_p\".
  658. This is an internal function that should never be called
  659. directly.
  660. See the C source for the logic behind this function."
  661. (let ((stack
  662. ;; We clean advice from the backtrace. This ensures that we
  663. ;; get the right answer even if `call-interactively' has been
  664. ;; advised.
  665. (ido-ubiquitous--clean-advice-from-backtrace
  666. (cdr
  667. (ido-ubiquitous--backtrace-from
  668. '(called-interactively-p interactive-p))))))
  669. ;; See comments in the C function for the logic here.
  670. (while (and stack
  671. (or (eq (cl-cadar stack) 'bytecode)
  672. (null (caar stack))))
  673. (setq stack (cdr stack)))
  674. ;; Top of stack is now the function that we want to know
  675. ;; about. Pop it, then check if the next function is
  676. ;; `call-interactively', using a more permissive test than the default.
  677. (ido-ubiquitous--looks-like-call-interactively (cl-cadadr stack))))
  678. (defadvice call-interactively (around ido-ubiquitous activate)
  679. "Implements the behavior specified in `ido-ubiquitous-command-overrides'."
  680. (ido-ubiquitous-with-override
  681. (ido-ubiquitous-get-command-override (ad-get-arg 0))
  682. ad-do-it))
  683. ;; Work around `called-interactively-p' in Emacs 24.3 and earlier,
  684. ;; which always returns nil when `call-interactively' is advised.
  685. (when (not (and (featurep 'nadvice)
  686. (boundp 'called-interactively-p-functions)))
  687. (defadvice interactive-p (around ido-ubiquitous activate)
  688. "Return the correct result when `call-interactively' is advised.
  689. This advice completely overrides the original definition."
  690. (condition-case nil
  691. (setq ad-return-value
  692. (and (ido-ubiquitous--interactive-internal)
  693. (ido-ubiquitous--interactive-p-internal)))
  694. ;; In case of error in the advice, fall back to the default
  695. ;; implementation
  696. (error ad-do-it)))
  697. (defadvice called-interactively-p (around ido-ubiquitous activate)
  698. "Return the correct result when `call-interactively' is advised.
  699. This advice completely overrides the original definition."
  700. (condition-case nil
  701. (setq ad-return-value
  702. (and (or (ido-ubiquitous--interactive-internal)
  703. (not (eq kind 'interactive)))
  704. (ido-ubiquitous--interactive-p-internal)))
  705. ;; In case of error in the advice, fall back to the default
  706. ;; implementation
  707. (error ad-do-it))))
  708. ;;; Other
  709. (defun ido-ubiquitous-initialize ()
  710. "Do initial setup for ido-ubiquitous.
  711. This only needs to be called once when the file is first loaded."
  712. ;; Clean up old versions of ido-ubiquitous advice if they exist
  713. (ignore-errors (ad-remove-advice 'completing-read 'around 'ido-ubiquitous))
  714. (ignore-errors (ad-remove-advice 'ido-completing-read 'around 'detect-replacing-cr))
  715. (ignore-errors (ad-remove-advice 'ido-magic-forward-char 'before 'ido-ubiquitous-fallback))
  716. (ignore-errors (ad-remove-advice 'ido-magic-backward-char 'before 'ido-ubiquitous-fallback))
  717. (ignore-errors (ad-remove-advice 'ido-exit-minibuffer 'around 'compatibility))
  718. (ad-activate-all)
  719. ;; Make sure the mode is turned on/off as specified by the value of
  720. ;; the mode variable
  721. (ido-ubiquitous-mode (if ido-ubiquitous-mode 1 0)))
  722. (ido-ubiquitous-initialize)
  723. (provide 'ido-ubiquitous)
  724. ;; Local Variables:
  725. ;; indent-tabs-mode: nil
  726. ;; End:
  727. ;;; ido-ubiquitous.el ends here