ido-ubiquitous.el 41 KB

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