ido-ubiquitous.el 39 KB

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