ido-ubiquitous.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. ;;; ido-ubiquitous.el --- Use ido (nearly) everywhere.
  2. ;; Author: Ryan C. Thompson
  3. ;; URL: https://github.com/DarwinAwardWinner/ido-ubiquitous
  4. ;; Version: 1.6
  5. ;; Created: 2011-09-01
  6. ;; Keywords: convenience
  7. ;; EmacsWiki: InteractivelyDoThings
  8. ;; This file is NOT part of GNU Emacs.
  9. ;;; Commentary:
  10. ;; You may have seen the `ido-everywhere' variable in ido.el and got
  11. ;; excited that you could use ido completion for everything. Then you
  12. ;; were probably disappointed when you realized that it only applied
  13. ;; to *file names* and nothing else. Well, ido-ubiquitous is here to
  14. ;; fulfill the original promise and let you use ido completion for
  15. ;; (almost) any command that uses `completing-read' to offer you a
  16. ;; choice of several alternatives.
  17. ;; This even works in M-x, but for that, you might prefer the "smex"
  18. ;; package instead.
  19. ;; As of version 0.7, this package also makes a small modification to
  20. ;; ido's behavior so as to support a strange corner case of
  21. ;; `completing-read' that some functions rely on. Since the goal of
  22. ;; this package is to replace `completing-read' everywhere instead of
  23. ;; just selectively (as ido itself does), compatibility with all the
  24. ;; quriks of `completing-read' is important here.
  25. ;; If you find a case where enabling ido-ubiquitous causes a command
  26. ;; not to work correctly, please report it by creating an issue on
  27. ;; GitHub: https://github.com/DarwinAwardWinner/ido-ubiquitous/issues
  28. ;;; License:
  29. ;; This program is free software; you can redistribute it and/or modify
  30. ;; it under the terms of the GNU General Public License as published by
  31. ;; the Free Software Foundation; either version 3, or (at your option)
  32. ;; any later version.
  33. ;;
  34. ;; This program is distributed in the hope that it will be useful,
  35. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. ;; GNU General Public License for more details.
  38. ;;
  39. ;; You should have received a copy of the GNU General Public License
  40. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  41. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  42. ;; Boston, MA 02110-1301, USA.
  43. ;;; Code:
  44. (require 'ido)
  45. (require 'advice)
  46. (defvar ido-ubiquitous-orig-completing-read-function
  47. (bound-and-true-p completing-read-function)
  48. "The value of `completing-read-function' before ido-ubiquitous-mode was enabled.
  49. This value will be restored when `ido-ubiquitous-mode' is
  50. deactivated. It will also be used as a fallback if ido-ubiquitous
  51. detects something that ido cannot handle.")
  52. ;;;###autoload
  53. (defgroup ido-ubiquitous nil
  54. "Use ido for (almost) all completion."
  55. :group 'ido)
  56. ;;;###autoload
  57. (define-minor-mode ido-ubiquitous-mode
  58. "Use `ido-completing-read' instead of `completing-read' almost everywhere.
  59. This mode has no effect unles `ido-mode' is also enabled.
  60. If this mode causes problems for a function, you can force the
  61. function to use the original completing read by using the macro
  62. `ido-ubiquitous-disable-in'. For example, if a
  63. function `foo' cannot work with ido-style completion, evaluate
  64. the following (for example by putting it in your .emacs file):
  65. (ido-ubiquitous-disable-in foo)"
  66. nil
  67. :global t
  68. :group 'ido-ubiquitous
  69. (when ido-ubiquitous-mode
  70. (unless (bound-and-true-p ido-mode)
  71. (warn "Ido-ubiquitous-mode enabled without ido mode.")))
  72. (if (and (boundp 'completing-read-function)
  73. ido-ubiquitous-orig-completing-read-function)
  74. ;; Emacs 24 and later
  75. (progn
  76. ;; Ensure emacs 23 code disabled
  77. (ad-disable-advice 'completing-read 'around 'ido-ubiquitous-legacy)
  78. (ad-activate 'completing-read)
  79. (setq completing-read-function
  80. (if ido-ubiquitous-mode
  81. 'completing-read-ido
  82. ido-ubiquitous-orig-completing-read-function)))
  83. ;; Emacs 23 and earlier
  84. (funcall (if ido-ubiquitous-mode 'ad-enable-advice 'ad-disable-advice)
  85. 'completing-read 'around 'ido-ubiquitous-legacy)
  86. (ad-activate 'completing-read)))
  87. ;;;###autoload
  88. (define-obsolete-variable-alias 'ido-ubiquitous
  89. 'ido-ubiquitous-mode "0.8")
  90. ;;;###autoload
  91. (define-obsolete-function-alias 'ido-ubiquitous
  92. 'ido-ubiquitous-mode "0.8")
  93. ;;;###autoload
  94. (defcustom ido-ubiquitous-command-exceptions '()
  95. "List of commands that should not be affected by `ido-ubiquitous'.
  96. Even when `ido-ubiquitous' mode is enabled, these commands will
  97. continue to use `completing-read' instead of
  98. `ido-completing-read'.
  99. Only *interactive* commands should go here. To disable
  100. ido-ubiquitous in non-interactive functions, customize
  101. `ido-ubiquitous-function-exceptions'.
  102. Note: this feature depends on the variable `this-command' being
  103. properly set to the name of the currently executing command.
  104. Depending on how the command is onvoked, this may or may not
  105. happen, so this feature may simply not work in some cases."
  106. :type '(repeat (symbol :tag "Command"))
  107. :group 'ido-ubiquitous)
  108. ;;;###autoload
  109. (define-obsolete-variable-alias 'ido-ubiquitous-exceptions
  110. 'ido-ubiquitous-command-exceptions "0.4")
  111. (defvar ido-next-call-replaces-completing-read nil)
  112. (defvar ido-this-call-replaces-completing-read nil)
  113. ;; Emacs 23-
  114. (defadvice completing-read (around ido-ubiquitous-legacy activate)
  115. "Ido-based method for reading from the minibuffer with completion.
  116. See `completing-read' for the meaning of the arguments."
  117. (if (or inherit-input-method ; Can't handle this arg
  118. (bound-and-true-p completion-extra-properties) ; Can't handle this
  119. (not ido-mode)
  120. (not ido-ubiquitous-mode)
  121. ;; Avoid infinite recursion from ido calling completing-read
  122. (boundp 'ido-cur-item)
  123. (memq this-command ido-ubiquitous-command-exceptions))
  124. ad-do-it
  125. (let ((allcomp (all-completions "" collection predicate)))
  126. ;; Only use ido completion if there are actually any completions
  127. ;; to offer.
  128. (if allcomp
  129. (let ((ido-next-call-replaces-completing-read t))
  130. (setq ad-return-value
  131. (ido-completing-read prompt allcomp
  132. nil require-match initial-input hist def)))
  133. ad-do-it))))
  134. (ad-disable-advice 'completing-read 'around 'ido-ubiquitous-legacy)
  135. (ad-activate 'completing-read)
  136. ;; Emacs 24+
  137. (defun completing-read-ido (prompt collection &optional predicate
  138. require-match initial-input
  139. hist def inherit-input-method)
  140. "Ido-based method for reading from the minibuffer with completion.
  141. See `completing-read' for the meaning of the arguments.
  142. This function is a wrapper for `ido-completing-read' designed to
  143. be used as the value of `completing-read-function'."
  144. (if (or inherit-input-method ; Can't handle this arg
  145. (not ido-mode)
  146. (not ido-ubiquitous-mode)
  147. (memq this-command ido-ubiquitous-command-exceptions))
  148. (funcall ido-ubiquitous-orig-completing-read-function
  149. prompt collection predicate
  150. require-match initial-input
  151. hist def inherit-input-method)
  152. (let ((allcomp (all-completions "" collection predicate)))
  153. ;; Only use ido completion if there are actually any completions
  154. ;; to offer.
  155. (if allcomp
  156. (let ((ido-next-call-replaces-completing-read t))
  157. (ido-completing-read prompt allcomp
  158. nil require-match initial-input hist def))
  159. (funcall ido-ubiquitous-orig-completing-read-function
  160. prompt collection predicate
  161. require-match initial-input
  162. hist def inherit-input-method)))))
  163. (defadvice ido-completing-read (around detect-replacing-cr activate)
  164. "Detect whether this call was done through `completing-read-ido'."
  165. (let* ((ido-this-call-replaces-completing-read ido-next-call-replaces-completing-read)
  166. (ido-next-call-replaces-completing-read nil))
  167. (when ido-this-call-replaces-completing-read
  168. ;; If DEF is a list, prepend it to CHOICES and set DEF to just the
  169. ;; car of the default list.
  170. (when (and def (listp def))
  171. (setq choices (delete-dups (append def choices))
  172. def (car def))) ;; Work around a bug in ido when both INITIAL-INPUT and DEF are provided
  173. ;; More info: https://github.com/technomancy/ido-ubiquitous/issues/18
  174. (let ((initial (cond ((null initial-input) "")
  175. ((stringp initial-input) initial-input)
  176. ((consp initial-input) (car initial-input))
  177. (t initial-input)))
  178. (deflist (if (listp def)
  179. def
  180. (list def))))
  181. (when (and deflist initial
  182. (stringp initial)
  183. (not (string= initial "")))
  184. ;; Both default and initial input were provided. So keep the
  185. ;; initial input and preprocess the choices list to put the
  186. ;; default at the head, then proceed with default = nil.
  187. (setq choices (delete-dups (append deflist (remove def choices)))
  188. def nil))))
  189. ad-do-it))
  190. (defmacro ido-ubiquitous-disable-in (func)
  191. "Disable ido-ubiquitous in FUNC."
  192. (let ((docstring
  193. (format "Disable ido-ubiquitous in %s" func)))
  194. `(defadvice ,func (around disable-ido-ubiquitous activate)
  195. ,docstring
  196. (let (ido-ubiquitous-mode) ad-do-it))))
  197. (define-obsolete-function-alias
  198. 'disable-ido-ubiquitous-in
  199. 'ido-ubiquitous-disable-in
  200. "0.4")
  201. (defmacro ido-ubiquitous-enable-in (func)
  202. "Re-enable ido-ubiquitous in FUNC.
  203. This reverses the effect of a previous call to
  204. `ido-ubiquitous-disable-in'."
  205. `(when (ad-find-advice ',func 'around 'disable-ido-ubiquitous)
  206. (ad-disable-advice ',func 'around 'disable-ido-ubiquitous)
  207. (ad-activate ',func)))
  208. (define-obsolete-function-alias
  209. 'enable-ido-ubiquitous-in
  210. 'ido-ubiquitous-enable-in
  211. "0.4")
  212. ;; Always disable ido-ubiquitous in `find-file' and similar functions,
  213. ;; because they are not supposed to use ido.
  214. (defvar ido-ubiquitous-permanent-function-exceptions
  215. '(read-file-name
  216. read-file-name-internal
  217. read-buffer
  218. gnus-emacs-completing-read
  219. gnus-iswitchb-completing-read
  220. man)
  221. "Functions in which ido-ubiquitous should always be disabled.
  222. If you want to disable ido in a specific function or command, do
  223. not modify this variable. Instead, try `M-x customize-group
  224. ido-ubiquitous.")
  225. (dolist (func ido-ubiquitous-permanent-function-exceptions)
  226. (eval `(ido-ubiquitous-disable-in ,func)))
  227. (defun ido-ubiquitous--set-difference (list1 list2)
  228. "Replacement for `set-difference' from `cl'."
  229. (apply #'nconc
  230. (mapcar (lambda (elt) (unless (memq elt list2) (list elt)))
  231. list1)))
  232. (defun ido-ubiquitous-set-function-exceptions (sym newval)
  233. (let* ((oldval (when (boundp sym) (eval sym))))
  234. ;; Filter out the permanent exceptions so we never act on them.
  235. (setq oldval (ido-ubiquitous--set-difference oldval ido-ubiquitous-permanent-function-exceptions))
  236. (setq newval (ido-ubiquitous--set-difference newval ido-ubiquitous-permanent-function-exceptions))
  237. ;; Re-enable ido-ubiquitous on all old functions, in case they
  238. ;; were removed from the list.
  239. (dolist (oldfun oldval)
  240. (eval `(ido-ubiquitous-enable-in ,oldfun)))
  241. ;; Set the new value
  242. (set-default sym newval)
  243. ;; Disable ido-ubiquitous on all new functions
  244. (dolist (newfun newval)
  245. (eval `(ido-ubiquitous-disable-in ,newfun)))))
  246. ;;;###autoload
  247. (defcustom ido-ubiquitous-function-exceptions
  248. '(grep-read-files)
  249. "List of functions in which to disable ido-ubiquitous.
  250. If you need to add a function to this list, please also file a
  251. bug report at
  252. https://github.com/DarwinAwardWinner/ido-ubiquitous/issues
  253. Note that certain functions, such as `read-file-name', must
  254. always have ido-ubiquitous disabled, and cannot be added
  255. here. (They are effectively a permanent part of this list
  256. already.)"
  257. :group 'ido-ubiquitous
  258. :type '(repeat :tag "Functions"
  259. (symbol :tag "Function"))
  260. :set 'ido-ubiquitous-set-function-exceptions)
  261. (defcustom ido-ubiquitous-enable-compatibility t
  262. "Allow ido to emulate a quirk of `completing-read'.
  263. From the `completing-read' docstring:
  264. > If the input is null, `completing-read' returns DEF, or the
  265. > first element of the list of default values, or an empty string
  266. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  267. If this variable is non-nil, then ido-ubiquitous will attempt to
  268. emulate this behavior. Specifically, if RET is pressed
  269. immediately upon entering completion, an empty string will be
  270. returned instead of the first element in the list. This behavior
  271. is only enabled when ido is being used as a substitute for
  272. `completing-read', and not when it is used directly.
  273. This odd behavior is required for compatibility with an old-style
  274. usage pattern whereby the default was requested by returning an
  275. empty string. In this mode, the caller receives the empty string
  276. and handles the default case manually, while `completing-read'
  277. never has any knowledge of the default. This is a problem for
  278. ido, which always returns the first element in the list when the
  279. input is empty. Without knowledge of the default, it cannot
  280. ensure that the default is first on the list, so returning the
  281. first item is not the correct behavior. Instead, it must return
  282. an empty string like `completing-read'.
  283. When this mode is enabled, you can still select the first item on
  284. the list by prefixing \"RET\" with \"C-u\"."
  285. :type 'boolean
  286. :group 'ido-ubiquitous)
  287. ;;;###autoload
  288. (defcustom ido-ubiquitous-command-compatibility-exceptions '()
  289. "List of commands in which to disable compatibility.
  290. See `ido-ubiquitous-enable-compatibility' for a description of
  291. the compatibility behavior. If this behavior causes a command to
  292. break, add that command to this list to disable compatibility
  293. mode for just that command.
  294. Only *interactive* commands should go here. To disable
  295. compatibility mode in non-interactive functions, customize
  296. `ido-ubiquitous-function-compatibility-exceptions'."
  297. :type '(repeat (symbol :tag "Command"))
  298. :group 'ido-ubiquitous)
  299. (defvar ido-ubiquitous-initial-item nil
  300. "The first item selected when ido starts.")
  301. (defadvice ido-read-internal (before clear-initial-item activate)
  302. (setq ido-ubiquitous-initial-item nil))
  303. (defadvice ido-make-choice-list (after set-initial-item activate)
  304. (when (and ad-return-value (listp ad-return-value))
  305. (setq ido-ubiquitous-initial-item (car ad-return-value))))
  306. (defadvice ido-next-match (after clear-initial-item activate)
  307. (setq ido-ubiquitous-initial-item nil))
  308. (defadvice ido-prev-match (after clear-initial-item activate)
  309. (setq ido-ubiquitous-initial-item nil))
  310. (defadvice ido-exit-minibuffer (around compatibility activate)
  311. "Emulate a quirk of `completing-read'.
  312. > If the input is null, `completing-read' returns DEF, or the
  313. > first element of the list of default values, or an empty string
  314. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  315. See `ido-ubiquitous-enable-compatibility', which controls whether
  316. this advice has any effect."
  317. (if (and (eq ido-cur-item 'list)
  318. ido-ubiquitous-enable-compatibility
  319. ;; Only enable if we are replacing `completing-read'
  320. ido-this-call-replaces-completing-read
  321. ;; Disable in command exceptions
  322. (not (memq this-command ido-ubiquitous-command-compatibility-exceptions))
  323. ;; Input is empty
  324. (string= ido-text "")
  325. ;; Default is nil
  326. (null ido-default-item)
  327. ;; Prefix disables compatibility
  328. (not current-prefix-arg)
  329. (string= (car ido-cur-list)
  330. ido-ubiquitous-initial-item))
  331. (ido-select-text)
  332. ad-do-it)
  333. (setq ido-ubiquitous-initial-item nil))
  334. (defmacro ido-ubiquitous-disable-compatibility-in (func)
  335. "Disable ido-ubiquitous compatibility mode in FUNC."
  336. (let ((docstring
  337. (format "Disable ido-ubiquitous in %s" func)))
  338. `(defadvice ,func (around disable-ido-ubiquitous-compatibility activate)
  339. ,docstring
  340. (let (ido-ubiquitous-enable-compatibility) ad-do-it))))
  341. (defmacro ido-ubiquitous-enable-compatibility-in (func)
  342. "Re-enable ido-ubiquitous comaptibility mode in FUNC.
  343. This reverses the effect of a previous call to
  344. `ido-ubiquitous-disable-compatibility-in'."
  345. `(when (ad-find-advice ',func 'around 'disable-ido-ubiquitous-compatibility)
  346. (ad-disable-advice ',func 'around 'disable-ido-ubiquitous-compatibility)
  347. (ad-activate ',func)))
  348. (defun ido-ubiquitous-set-function-compatibility-exceptions (sym newval)
  349. (let* ((oldval (when (boundp sym) (eval sym))))
  350. ;; Re-enable compatibility on all old functions, in case they
  351. ;; were removed from the list.
  352. (dolist (oldfun oldval)
  353. (eval `(ido-ubiquitous-enable-compatibility-in ,oldfun)))
  354. ;; Set the new value
  355. (set-default sym newval)
  356. ;; Disable compatibility on all new functions
  357. (dolist (newfun newval)
  358. (eval `(ido-ubiquitous-disable-compatibility-in ,newfun)))))
  359. ;;;###autoload
  360. (defcustom ido-ubiquitous-function-compatibility-exceptions
  361. '()
  362. "List of functions in which to disable ido-ubiquitous compatibility mode.
  363. See `ido-ubiquitous-enable-compatibility' for a description of
  364. the compatibility behavior. If this behavior causes a function to
  365. break, add that function to this list to disable compatibility
  366. mode for just that command.
  367. If you need to add a function to this list, please also file a
  368. bug report at
  369. https://github.com/DarwinAwardWinner/ido-ubiquitous/issues"
  370. :group 'ido-ubiquitous
  371. :type '(repeat :tag "Functions"
  372. (symbol :tag "Function"))
  373. :set 'ido-ubiquitous-set-function-exceptions)
  374. (defun ido-ubiquitous-initialize ()
  375. "Do initial setup for ido-ubiquitous.
  376. This only needs to be called once when the file is first loaded."
  377. ;; Clean up old versions of ido-ubiquitous (1.3 and earlier) that
  378. ;; defined advice on `completing-read' instead of modifying
  379. ;; `completing-read-function'.
  380. (when (ad-find-advice 'completing-read 'around 'ido-ubiquitous)
  381. (ad-remove-advice 'completing-read 'around 'ido-ubiquitous)
  382. (ad-activate 'completing-read))
  383. ;; Make sure all exceptions are activated
  384. (ido-ubiquitous-set-function-exceptions
  385. 'ido-ubiquitous-function-exceptions
  386. ido-ubiquitous-function-exceptions)
  387. (ido-ubiquitous-set-function-compatibility-exceptions
  388. 'ido-ubiquitous-function-compatibility-exceptions
  389. ido-ubiquitous-function-compatibility-exceptions)
  390. ;; Make sure the mode is turned on/off as specified by the value of
  391. ;; the mode variable
  392. (ido-ubiquitous-mode (if ido-ubiquitous-mode 1 0)))
  393. (ido-ubiquitous-initialize)
  394. (provide 'ido-ubiquitous) ;;; ido-ubiquitous.el ends here