ido-ubiquitous.el 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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. (let* ((func ',func)
  343. (override (get func 'ido-ubiquitous-override)))
  344. (when override
  345. (ido-ubiquitous--debug-message
  346. "Using override `%s' for function `%s'"
  347. override func))
  348. (ido-ubiquitous-with-override override
  349. ad-do-it)))))))
  350. (defun ido-ubiquitous-set-function-overrides (sym newval)
  351. "Custom setter function for `ido-ubiquitous-function-overrides'.
  352. In addition to setting the variable, this also sets up advice on
  353. each function to apply the appropriate override."
  354. ;; Unset all previous overrides
  355. (when (boundp sym)
  356. (let ((oldval (eval sym)))
  357. (cl-loop for (_action _match-type func) in oldval
  358. do (ido-ubiquitous-apply-function-override func nil))))
  359. ;; Ensure that function names are strings, not symbols
  360. (setq newval
  361. (cl-loop for (action match-type func) in newval
  362. collect (list action match-type
  363. (ido-ubiquitous--as-string func))))
  364. (set-default sym newval)
  365. ;; set new overrides
  366. (cl-loop for (action _match-type func) in (eval sym)
  367. do (ido-ubiquitous-apply-function-override func action)))
  368. (defcustom ido-ubiquitous-function-overrides ido-ubiquitous-default-function-overrides
  369. "List of function override specifications for ido-ubiquitous
  370. Function override specifications have a similar structure to
  371. command override specifications (see
  372. `ido-ubiquitous-command-overrides'). A function override
  373. specification has the form `(BEHAVIOR MATCH-TYPE MATCH-TEXT)'.
  374. However, `MATCH-TYPE' may ONLY be `exact'; No other match type is
  375. supported.
  376. If you need to add a new specification to this list, please also file a
  377. bug report at https://github.com/DarwinAwardWinner/ido-ubiquitous/issues
  378. Setting this variable directly has no effect. You must set it
  379. through Customize."
  380. :type '(repeat ido-ubiquitous-function-override-spec)
  381. :set 'ido-ubiquitous-set-function-overrides
  382. :group 'ido-ubiquitous)
  383. (defcustom ido-ubiquitous-allow-on-functional-collection nil
  384. "Allow ido completion when COLLECTION is a function.
  385. The `completing-read' function allows its COLLECTION argument to
  386. be a function instead of a list of choices. Some such functions
  387. simply return a list of completions and are suitable for use with
  388. ido, but others implement more complex behavior and will result
  389. in incorrect behavior if used with ido. Since there is no way to
  390. tell the difference, this preference defaults to nil, which means
  391. that ido-ubiquitous will not work when COLLECTION is a function
  392. unless there is a specific override in effect. To disable this
  393. safeguard and GUARANTEE ERRORS on some functions, you may set
  394. this to non-nil, but this is not recommended."
  395. :type 'boolean
  396. :group 'ido-ubiquitous)
  397. ;;; ido-ubiquitous core
  398. ;; These variable are used to make ido-ubiquitous work properly in the
  399. ;; case that `completing-read' is called recursively (which is
  400. ;; possible when `enable-recursive-minibuffers' is non-nil.)
  401. (defvar ido-ubiquitous-enable-next-call nil
  402. "If non-nil, then the next call to `ido-completing-read' is by ido-ubiquitous.")
  403. (defvar ido-ubiquitous-enable-this-call nil
  404. "If non-nil, then the current call to `ido-completing-read' is by ido-ubiquitous.")
  405. (defvar ido-ubiquitous-next-override nil
  406. "This holds the override to be applied on the next call to `completing-read'.
  407. It's value can be nil or one of the symbols `disable', `enable', or
  408. `enable-old'.
  409. You should not modify this variable directly. Instead use the
  410. macro `ido-ubiquitous-with-override'.")
  411. (defvar ido-ubiquitous-active-override nil
  412. "This holds the override being applied to the current call to `completing-read'.
  413. It's value can be nil or one of the symbols `disable', `enable', or
  414. `enable-old'.
  415. You should not modify this variable directly. Instead use the
  416. macro `ido-ubiquitous-with-override'.")
  417. (defvar ido-ubiquitous-active-state nil
  418. "This holds the ido-ubiquitous mode of operation for the current call to `completing-read'.
  419. It's value can be nil or one of the symbols `disable', `enable', or
  420. `enable-old'.
  421. You should not modify this variable directly.")
  422. (defadvice ido-completing-read (around ido-ubiquitous activate)
  423. "Enable ido-ubiquitous features if this call was done through ido-ubiquitous.
  424. This advice ensures that `ido-ubiquitous-enable-this-call' is set
  425. properly while `ido-completing-read' is executing. This variable
  426. is used to determine whether to enable certain behaviors only for
  427. ido-ubiquitous, not for ordinary ido completion."
  428. ;; Set "this" and clear "next" so it doesn't apply to nested calls.
  429. (let* ((ido-ubiquitous-enable-this-call ido-ubiquitous-enable-next-call)
  430. (ido-ubiquitous-enable-next-call nil)
  431. (ido-ubiquitous-initial-item nil))
  432. ad-do-it))
  433. ;; Signal used to trigger fallback
  434. (define-error 'ido-ubiquitous-fallback "ido-ubiquitous-fallback")
  435. (defun completing-read-ido-ubiquitous
  436. (prompt collection &optional predicate
  437. require-match initial-input
  438. hist def inherit-input-method)
  439. "ido-based method for reading from the minibuffer with completion.
  440. See `completing-read' for the meaning of the arguments.
  441. This function is a wrapper for `ido-completing-read' designed to
  442. be used as the value of `completing-read-function'. Importantly,
  443. it detects edge cases that ido cannot handle and uses normal
  444. completion for them."
  445. (let (;; Save the original arguments in case we need to do the
  446. ;; fallback
  447. (orig-args
  448. (list prompt collection predicate require-match
  449. initial-input hist def inherit-input-method)))
  450. (condition-case sig
  451. (let* (;; Set the active override and clear the "next" one so it
  452. ;; doesn't apply to nested calls.
  453. (ido-ubiquitous-active-override ido-ubiquitous-next-override)
  454. (ido-ubiquitous-next-override nil)
  455. ;; Apply the active override, if any
  456. (ido-ubiquitous-active-state
  457. (or ido-ubiquitous-active-override
  458. ido-ubiquitous-default-state
  459. 'enable)))
  460. ;; If ido-ubiquitous is disabled this time, fall back
  461. (when (eq ido-ubiquitous-active-state 'disable)
  462. (signal 'ido-ubiquitous-fallback
  463. "`ido-ubiquitous-active-state' is `disable'"))
  464. ;; Handle a collection that is a function: either expand
  465. ;; completion list now or fall back
  466. (when (functionp collection)
  467. (if (or ido-ubiquitous-allow-on-functional-collection
  468. (memq ido-ubiquitous-active-override
  469. '(enable enable-old)))
  470. (setq collection (all-completions "" collection predicate))
  471. (signal 'ido-ubiquitous-fallback
  472. "COLLECTION is a function and there is no override")))
  473. (let ((ido-ubiquitous-enable-next-call t))
  474. (ido-completing-read+
  475. prompt collection predicate require-match
  476. initial-input hist def inherit-input-method)))
  477. ;; Handler for ido-ubiquitous-fallback signal
  478. (ido-ubiquitous-fallback
  479. (ido-ubiquitous--explain-fallback sig)
  480. (apply ido-cr+-fallback-function orig-args)))))
  481. (define-obsolete-function-alias #'completing-read-ido #'completing-read-ido-ubiquitous
  482. "ido-ubiquitous 3.0")
  483. ;;; Old-style default support
  484. (defvar ido-ubiquitous-initial-item nil
  485. "The first item selected when ido starts.
  486. This is initialized to the first item in the list of completions
  487. when ido starts, and is cleared when any character is entered
  488. into the prompt or the list is cycled. If it is non-nil and still
  489. equal to the first item in the completion list when ido exits,
  490. then if `ido-ubiquitous-active-state' is `enable-old', ido
  491. returns an empty string instead of the first item on the list.")
  492. (defmacro ido-ubiquitous-set-initial-item (item)
  493. "Wrapper for `(setq ido-ubiquitous-initial-item ITEM)'.
  494. This wrapper differs from simply doing `(setq
  495. ido-ubiquitous-initial-item ITEM)' in several ways. First, it has
  496. no effect (and does not evaluate ITEM) unless
  497. `ido-ubiquitous-active-state' is `enable-old'. Second, it emits
  498. appropriate debug messages."
  499. `(when (eq ido-ubiquitous-active-state 'enable-old)
  500. (let ((item ,item))
  501. (cond
  502. (item
  503. (ido-ubiquitous--debug-message
  504. "Setting `ido-ubiquitous-initial-item' to `%S'."
  505. item))
  506. (ido-ubiquitous-initial-item
  507. (ido-ubiquitous--debug-message "Clearing `ido-ubiquitous-initial-item'.")))
  508. (setq ido-ubiquitous-initial-item item))))
  509. (defadvice ido-read-internal (before clear-initial-item activate)
  510. (ido-ubiquitous-set-initial-item nil))
  511. (defadvice ido-make-choice-list (after set-initial-item activate)
  512. (ido-ubiquitous-set-initial-item
  513. (when (and ad-return-value (listp ad-return-value))
  514. (car ad-return-value))))
  515. (defadvice ido-next-match (after clear-initial-item activate)
  516. (ido-ubiquitous-set-initial-item nil))
  517. (defadvice ido-prev-match (after clear-initial-item activate)
  518. (ido-ubiquitous-set-initial-item nil))
  519. ;; Clear initial item after `self-insert-command'
  520. (add-hook
  521. 'ido-minibuffer-setup-hook
  522. (lambda ()
  523. (add-hook
  524. 'post-self-insert-hook
  525. (lambda () (ido-ubiquitous-set-initial-item nil))
  526. nil
  527. 'local)))
  528. (defun ido-ubiquitous-should-use-old-style-default ()
  529. "Returns non nil if ido-ubiquitous should emulate old-style default.
  530. This function simply encapsulates all the checks that need to be
  531. done in order to decide whether to swap RET and C-j. See
  532. `ido-ubiquitous-default-state' for more information."
  533. ;; These checks outside the loop don't produce debug messages
  534. (and
  535. (bound-and-true-p ido-cur-item)
  536. ;; Only if completing a list, not a buffer or file
  537. (eq ido-cur-item 'list)
  538. ;; Only if this call was done through ido-ubiquitous
  539. ido-ubiquitous-enable-this-call
  540. ;; Only if old-style default enabled
  541. (eq ido-ubiquitous-active-state 'enable-old)
  542. ;; This loop is just an implementation of `and' that reports which
  543. ;; arg was nil for debugging purposes.
  544. (loop
  545. for test in
  546. '(;; Only if default is nil
  547. (null ido-default-item)
  548. ;; Only if input is empty
  549. (string= ido-text "")
  550. ;; Only if `ido-ubiquitous-initial-item' hasn't been cleared
  551. ido-ubiquitous-initial-item
  552. ;; Only if initial item hasn't changed
  553. (string= (car ido-cur-list)
  554. ido-ubiquitous-initial-item))
  555. for test-result = (eval test)
  556. if (not test-result)
  557. do (ido-ubiquitous--debug-message
  558. "Not enabling old-style default selection because `%S' is nil"
  559. test)
  560. and return nil
  561. finally do (ido-ubiquitous--debug-message
  562. "Enabling old-style default selection")
  563. finally return t)))
  564. (defadvice ido-exit-minibuffer (around old-style-default-compat activate)
  565. "Emulate a quirk of `completing-read'.
  566. > If the input is null, `completing-read' returns DEF, or the
  567. > first element of the list of default values, or an empty string
  568. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  569. See `ido-ubiquitous-default-state', which controls whether this
  570. advice has any effect."
  571. (condition-case nil
  572. (if (ido-ubiquitous-should-use-old-style-default)
  573. (let ((ido-ubiquitous-active-state 'enable))
  574. (ido-select-text))
  575. ad-do-it)
  576. (error ad-do-it))
  577. (ido-ubiquitous-set-initial-item nil))
  578. (defadvice ido-select-text (around old-style-default-compat activate)
  579. "Emulate a quirk of `completing-read'.
  580. > If the input is null, `completing-read' returns DEF, or the
  581. > first element of the list of default values, or an empty string
  582. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  583. See `ido-ubiquitous-default-state', which controls whether this
  584. advice has any effect."
  585. (condition-case nil
  586. (if (ido-ubiquitous-should-use-old-style-default)
  587. (let ((ido-ubiquitous-active-state 'enable))
  588. (ido-exit-minibuffer))
  589. ad-do-it)
  590. (error ad-do-it))
  591. (ido-ubiquitous-set-initial-item nil))
  592. ;;; Overrides
  593. (defun ido-ubiquitous-restore-default-overrides (&optional save)
  594. "Re-add the default overrides for ido-ubiquitous.
  595. This will ensure that the default overrides are all present and
  596. at the head of the list in `ido-ubiquitous-command-overrides' and
  597. `ido-ubiquitous-function-overrides'. User-added overrides will
  598. not be removed, but they may be masked if one of the default
  599. overrides affects the same functions.
  600. With a prefix arg, also save the above variables' new values for
  601. future sessions."
  602. (interactive "P")
  603. (let ((setter (if save
  604. 'customize-save-variable
  605. 'customize-set-variable)))
  606. (cl-loop for (var def) in '((ido-ubiquitous-command-overrides
  607. ido-ubiquitous-default-command-overrides)
  608. (ido-ubiquitous-function-overrides
  609. ido-ubiquitous-default-function-overrides))
  610. do (let* ((curval (eval var))
  611. (defval (eval def))
  612. (newval (delete-dups (append defval curval))))
  613. (funcall setter var newval)))
  614. (message (if save
  615. "ido-ubiquitous: Restored default command and function overrides and saved for future sessions."
  616. "ido-ubiquitous: Restored default command and function overrides for current session only. Call again with prefix to save for future sessions."))))
  617. (defun ido-ubiquitous-spec-match (spec symbol)
  618. "Returns t if SPEC matches SYMBOL (which should be a function name).
  619. See `ido-ubiquitous-command-overrides'."
  620. (when (and symbol (symbolp symbol))
  621. (cl-destructuring-bind (type text) spec
  622. (let ((matcher (cdr (assoc type ido-ubiquitous-spec-matchers)))
  623. (text (ido-ubiquitous--as-string text))
  624. (symname (ido-ubiquitous--as-string symbol)))
  625. (when (null matcher)
  626. (error "ido-ubiquitous: Unknown match spec type \"%s\". See `ido-ubiquitous-spec-matchers' for valid types." type))
  627. (funcall matcher text symname)))))
  628. (defun ido-ubiquitous-get-command-override (cmd)
  629. "Return the override associated with the command CMD.
  630. If there is no override set for CMD in
  631. `ido-ubiquitous-command-overrides', return nil."
  632. (when (and cmd (symbolp cmd))
  633. (cl-loop for (action . spec) in ido-ubiquitous-command-overrides
  634. when (memq action '(disable enable enable-old nil))
  635. when (ido-ubiquitous-spec-match spec cmd)
  636. return action
  637. finally return nil)))
  638. ;;; Workaround for https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/24
  639. ;; When `call-interactively' is advised, `called-interactively-p'
  640. ;; always returns nil. So we redefine it (and `interactive-p') to test
  641. ;; the correct condition.
  642. (defsubst ido-ubiquitous--looks-like-advised-orig (func)
  643. "Returns t if FUNC is a symbol starting with \"ad-Orig-\".
  644. Such symbols are used to store the original definitions of
  645. functions that have been advised by `defadvice' or similar."
  646. (and (symbolp func)
  647. (string-prefix-p "ad-Orig-" (symbol-name func))))
  648. (defsubst ido-ubiquitous--looks-like-call-interactively (func)
  649. "Returns t if FUNC looks like the function `call-interactively'.
  650. FUNC \"looks like\" `call-interactively' if it is the literal
  651. symbol `call-interactively', or the value of `(symbol-function
  652. 'call-interactively)', or a symbol whose `symbol-function' is the
  653. same as that of `call-interactively'.
  654. This function is used to determine whether a given function was
  655. \"called by\" `call-interactively' and therefore was called
  656. interactively."
  657. (when func
  658. (eq (symbol-function 'call-interactively)
  659. (if (symbolp func)
  660. (symbol-function func)
  661. func))))
  662. (defun ido-ubiquitous--backtrace-from (fun)
  663. "Return all backtrace frames, starting with the one for FUN.
  664. FUN may be a list of functions, in which case the first one found
  665. on the stack will be used."
  666. (let ((stack
  667. (cl-loop for i upfrom 0
  668. for frame = (backtrace-frame i)
  669. while frame
  670. collect frame))
  671. (funcs (if (functionp fun)
  672. (list fun)
  673. fun)))
  674. (while (and stack
  675. (not (memq (cl-cadar stack) funcs)))
  676. (setq stack (cdr stack)))
  677. stack))
  678. (defun ido-ubiquitous--clean-advice-from-backtrace (stack)
  679. "Takes a stack trace and cleans all evidence of advice.
  680. Specifically, for each call to a function starting with
  681. \"ad-Orig-\", that call and all prior calls up to but not
  682. including the advised function's original name are deleted from
  683. the stack."
  684. (let ((skipping-until nil))
  685. (cl-loop for frame in stack
  686. for func = (cadr frame)
  687. ;; Check if we found the frame we we're skipping to
  688. if (and skipping-until
  689. (eq func skipping-until))
  690. do (setq skipping-until nil)
  691. ;; If we're looking at an the original form of an advised
  692. ;; function, skip until the real name of that function.
  693. if (and (not skipping-until)
  694. (ido-ubiquitous--looks-like-advised-orig func))
  695. do (setq skipping-until
  696. (intern
  697. (substring (symbol-name func)
  698. (eval-when-compile (length "ad-Orig-")))))
  699. unless skipping-until collect frame)))
  700. (defsubst ido-ubiquitous--interactive-internal ()
  701. "Eqivalent of the INTERACTIVE macro in the Emacs C source.
  702. This is an internal function that should never be called
  703. directly.
  704. See the C source for the logic behind this function."
  705. (and (not executing-kbd-macro)
  706. (not noninteractive)))
  707. (defun ido-ubiquitous--interactive-p-internal ()
  708. "Equivalent of C function \"interactive_p\".
  709. This is an internal function that should never be called
  710. directly.
  711. See the C source for the logic behind this function."
  712. (let ((stack
  713. ;; We clean advice from the backtrace. This ensures that we
  714. ;; get the right answer even if `call-interactively' has been
  715. ;; advised.
  716. (ido-ubiquitous--clean-advice-from-backtrace
  717. (cdr
  718. (ido-ubiquitous--backtrace-from
  719. '(called-interactively-p interactive-p))))))
  720. ;; See comments in the C function for the logic here.
  721. (while (and stack
  722. (or (eq (cl-cadar stack) 'bytecode)
  723. (null (caar stack))))
  724. (setq stack (cdr stack)))
  725. ;; Top of stack is now the function that we want to know
  726. ;; about. Pop it, then check if the next function is
  727. ;; `call-interactively', using a more permissive test than the default.
  728. (ido-ubiquitous--looks-like-call-interactively (cl-cadadr stack))))
  729. (defadvice call-interactively (around ido-ubiquitous activate)
  730. "Implements the behavior specified in `ido-ubiquitous-command-overrides'."
  731. (let* ((cmd (ad-get-arg 0))
  732. (override (ido-ubiquitous-get-command-override cmd)))
  733. (when override
  734. (ido-ubiquitous--debug-message "Using override `%s' for command `%s'"
  735. override cmd))
  736. (ido-ubiquitous-with-override override
  737. ad-do-it)))
  738. ;; Work around `called-interactively-p' in Emacs 24.3 and earlier,
  739. ;; which always returns nil when `call-interactively' is advised.
  740. (when (not (and (featurep 'nadvice)
  741. (boundp 'called-interactively-p-functions)))
  742. (defadvice interactive-p (around ido-ubiquitous activate)
  743. "Return the correct result when `call-interactively' is advised.
  744. This advice completely overrides the original definition."
  745. (condition-case nil
  746. (setq ad-return-value
  747. (and (ido-ubiquitous--interactive-internal)
  748. (ido-ubiquitous--interactive-p-internal)))
  749. ;; In case of error in the advice, fall back to the default
  750. ;; implementation
  751. (error ad-do-it)))
  752. (defadvice called-interactively-p (around ido-ubiquitous activate)
  753. "Return the correct result when `call-interactively' is advised.
  754. This advice completely overrides the original definition."
  755. (condition-case nil
  756. (setq ad-return-value
  757. (and (or (ido-ubiquitous--interactive-internal)
  758. (not (eq kind 'interactive)))
  759. (ido-ubiquitous--interactive-p-internal)))
  760. ;; In case of error in the advice, fall back to the default
  761. ;; implementation
  762. (error ad-do-it))))
  763. ;;; Debug messages
  764. (define-minor-mode ido-ubiquitous-debug-mode
  765. "If non-nil, ido-ubiquitous will print debug info.
  766. Debug info is printed to the *Messages* buffer."
  767. nil
  768. :global t
  769. :group 'ido-ubiquitous)
  770. ;; Defined as a macro for efficiency (args are not evaluated unless
  771. ;; debug mode is on)
  772. (defmacro ido-ubiquitous--debug-message (format-string &rest args)
  773. `(when ido-ubiquitous-debug-mode
  774. (message (concat "ido-ubiquitous: " ,format-string) ,@args)))
  775. (defun ido-ubiquitous--explain-fallback (arg)
  776. ;; This function accepts a string, or an ido-ubiquitous-fallback
  777. ;; signal.
  778. (when ido-ubiquitous-debug-mode
  779. (when (and (listp arg)
  780. (eq (car arg) 'ido-ubiquitous-fallback))
  781. (setq arg (cdr arg)))
  782. (ido-ubiquitous--debug-message "Falling back to `%s' because %s."
  783. ido-cr+-fallback-function arg)))
  784. ;;; Other
  785. (defun ido-ubiquitous-initialize ()
  786. "Do initial setup for ido-ubiquitous.
  787. This only needs to be called once when the file is first loaded."
  788. ;; Clean up old versions of ido-ubiquitous advice if they exist
  789. (ignore-errors (ad-remove-advice 'completing-read 'around 'ido-ubiquitous))
  790. (ignore-errors (ad-remove-advice 'ido-completing-read 'around 'detect-replacing-cr))
  791. (ignore-errors (ad-remove-advice 'ido-magic-forward-char 'before 'ido-ubiquitous-fallback))
  792. (ignore-errors (ad-remove-advice 'ido-magic-backward-char 'before 'ido-ubiquitous-fallback))
  793. (ignore-errors (ad-remove-advice 'ido-exit-minibuffer 'around 'compatibility))
  794. (ad-activate-all)
  795. ;; Make sure the mode is turned on/off as specified by the value of
  796. ;; the mode variable
  797. (ido-ubiquitous-mode (if ido-ubiquitous-mode 1 0)))
  798. (ido-ubiquitous-initialize)
  799. (provide 'ido-ubiquitous)
  800. ;; Local Variables:
  801. ;; indent-tabs-mode: nil
  802. ;; End:
  803. ;;; ido-ubiquitous.el ends here