ido-ubiquitous.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. ;;; ido-ubiquitous.el --- Use ido (nearly) everywhere.
  2. ;; Author: Ryan C. Thompson
  3. ;; URL: https://github.com/DarwinAwardWinner/ido-ubiquitous
  4. ;; Version: 1.3
  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. ;; Clean up old versions of ido-ubiquitous (1.3 and earlier) that
  46. ;; defined advice on `completing-read' instead of modifying
  47. ;; `completing-read-function'.
  48. (when (ad-find-advice 'completing-read 'around 'ido-ubiquitous)
  49. (ad-remove-advice 'completing-read 'around 'ido-ubiquitous)
  50. (ad-activate 'completing-read))
  51. (defvar ido-ubiquitous-orig-completing-read-function
  52. completing-read-function
  53. "The value of `completing-read-function' before ido-ubiquitous-mode was enabled.
  54. This value will be restored when `ido-ubiquitous-mode' is
  55. deactivated. It will also be used as a fallback if ido-ubiquitous
  56. detects something that ido cannot handle.")
  57. ;;;###autoload
  58. (defgroup ido-ubiquitous nil
  59. "Use ido for (almost) all completion."
  60. :group 'ido)
  61. ;;;###autoload
  62. (define-minor-mode ido-ubiquitous-mode
  63. "Use `ido-completing-read' instead of `completing-read' almost everywhere.
  64. This mode has no effect unles `ido-mode' is also enabled.
  65. If this mode causes problems for a function, you can force the
  66. function to use the original completing read by using the macro
  67. `ido-ubiquitous-disable-in'. For example, if a
  68. function `foo' cannot work with ido-style completion, evaluate
  69. the following (for example by putting it in your .emacs file):
  70. (ido-ubiquitous-disable-in foo)"
  71. nil
  72. :global t
  73. :group 'ido-ubiquitous
  74. (setq completing-read-function
  75. (if ido-ubiquitous-mode
  76. 'completing-read-ido
  77. ido-ubiquitous-orig-completing-read-function)))
  78. ;;;###autoload
  79. (define-obsolete-variable-alias 'ido-ubiquitous
  80. 'ido-ubiquitous-mode "0.8")
  81. ;;;###autoload
  82. (define-obsolete-function-alias 'ido-ubiquitous
  83. 'ido-ubiquitous-mode "0.8")
  84. ;;;###autoload
  85. (defcustom ido-ubiquitous-command-exceptions '()
  86. "List of commands that should not be affected by `ido-ubiquitous'.
  87. Even when `ido-ubiquitous' mode is enabled, these commands will
  88. continue to use `completing-read' instead of
  89. `ido-completing-read'.
  90. Only *interactive* commands should go here. To disable
  91. ido-ubiquitous in non-interactive functions, customize
  92. `ido-ubiquitous-function-exceptions'."
  93. :type '(repeat (symbol :tag "Command"))
  94. :group 'ido-ubiquitous)
  95. ;;;###autoload
  96. (define-obsolete-variable-alias 'ido-ubiquitous-exceptions
  97. 'ido-ubiquitous-command-exceptions "0.4")
  98. (defun completing-read-ido (prompt collection &optional predicate
  99. require-match initial-input
  100. hist def inherit-input-method)
  101. "Ido-based method for reading from the minibuffer with completion.
  102. See `completing-read' for the meaning of the arguments.
  103. This function is a wrapper for `ido-completing-read' designed to
  104. be used as the value of `completing-read-function'."
  105. (if (or inherit-input-method ; Can't handle this arg
  106. (not ido-mode)
  107. (not ido-ubiquitous-mode)
  108. (memq this-command ido-ubiquitous-command-exceptions))
  109. (funcall ido-ubiquitous-orig-completing-read-function
  110. prompt collection predicate
  111. require-match initial-input
  112. hist def inherit-input-method)
  113. (let ((allcomp (all-completions "" collection predicate)))
  114. ;; Only use ido completion if there are actually any completions
  115. ;; to offer.
  116. (if allcomp
  117. (ido-completing-read prompt allcomp
  118. nil require-match initial-input hist def)
  119. (funcall ido-ubiquitous-orig-completing-read-function
  120. prompt collection predicate
  121. require-match initial-input
  122. hist def inherit-input-method)))))
  123. (defmacro ido-ubiquitous-disable-in (func)
  124. "Disable ido-ubiquitous in FUNC."
  125. (let ((docstring
  126. (format "Disable ido-ubiquitous in %s" func)))
  127. `(defadvice ,func (around disable-ido-ubiquitous activate)
  128. ,docstring
  129. (let (ido-ubiquitous-mode) ad-do-it))))
  130. (define-obsolete-function-alias
  131. 'disable-ido-ubiquitous-in
  132. 'ido-ubiquitous-disable-in
  133. "0.4")
  134. (defmacro ido-ubiquitous-enable-in (func)
  135. "Re-enable ido-ubiquitous in FUNC.
  136. This reverses the effect of a previous call to
  137. `ido-ubiquitous-disable-in'."
  138. `(when (ad-find-advice ',func 'around 'disable-ido-ubiquitous)
  139. (ad-disable-advice ',func 'around 'disable-ido-ubiquitous)
  140. (ad-activate ',func)))
  141. (define-obsolete-function-alias
  142. 'enable-ido-ubiquitous-in
  143. 'ido-ubiquitous-enable-in
  144. "0.4")
  145. ;; Always disable ido-ubiquitous in `find-file' and similar functions,
  146. ;; because they are not supposed to use ido.
  147. (defvar ido-ubiquitous-permanent-function-exceptions
  148. '(read-file-name
  149. read-file-name-internal
  150. gnus-emacs-completing-read
  151. gnus-iswitchb-completing-read
  152. man)
  153. "Functions in which ido-ubiquitous should always be disabled.
  154. If you want to disable ido in a specific function or command, do
  155. not modify this variable. Instead, try `M-x customize-group
  156. ido-ubiquitous.")
  157. (mapc (lambda (func) (eval `(ido-ubiquitous-disable-in ,func)))
  158. ido-ubiquitous-permanent-function-exceptions)
  159. (defun ido-ubiquitous--set-difference (list1 list2)
  160. "Replacement for `set-difference' from `cl'."
  161. (apply #'nconc
  162. (mapcar (lambda (elt) (unless (memq elt list2) (list elt)))
  163. list1)))
  164. (defun ido-ubiquitous-set-function-exceptions (sym newval)
  165. (let* ((oldval (when (boundp sym) (eval sym))))
  166. ;; Filter out the permanent exceptions so we never act on them.
  167. (setq oldval (ido-ubiquitous--set-difference oldval ido-ubiquitous-permanent-function-exceptions))
  168. (setq newval (ido-ubiquitous--set-difference newval ido-ubiquitous-permanent-function-exceptions))
  169. ;; Re-enable ido-ubiquitous on all old functions, in case they
  170. ;; were removed from the list.
  171. (dolist (oldfun oldval)
  172. (eval `(ido-ubiquitous-enable-in ,oldfun)))
  173. ;; Set the new value
  174. (set-default sym newval)
  175. ;; Disable ido-ubiquitous on all new functions
  176. (dolist (newfun newval)
  177. (eval `(ido-ubiquitous-disable-in ,newfun)))))
  178. ;;;###autoload
  179. (defcustom ido-ubiquitous-function-exceptions
  180. '(grep-read-files)
  181. "List of functions in which to disable ido-ubiquitous.
  182. If you need to add a function to this list, please also file a
  183. bug report at
  184. https://github.com/DarwinAwardWinner/ido-ubiquitous/issues
  185. Note that certain functions, such as `read-file-name', must
  186. always have ido-ubiquitous disabled, and cannot be added
  187. here. (They are effectively a permanent part of this list
  188. already.)"
  189. :group 'ido-ubiquitous
  190. :type '(repeat :tag "Functions"
  191. (symbol :tag "Function"))
  192. :set 'ido-ubiquitous-set-function-exceptions)
  193. (defcustom ido-ubiquitous-enable-compatibility t
  194. "Emulate a quirk of `completing-read'.
  195. In the past, the `completing-read' function had no way of
  196. specifying a default item, so instead the convention was to
  197. request the default by returning an empty string. This occurrs in
  198. standard emacs completion system when you press \"RET\" without
  199. typing anything first. This is a problem for ido completion
  200. because when ido is used this way, it does not know which item is
  201. the default, so it cannot put the default at the head of the
  202. list. Hence, simply pressing \"RET\" will not properly select the
  203. advertised default. Setting this variable to non-nil will force
  204. ido to emulate this quirk of the standard emacs completion system
  205. in order to maintain compatibility with old functions that still
  206. use the empty-string-as-default convention.
  207. Specifically, when this variable is non-nil, ido will return an
  208. empty string (thereby requesting the default) if you press \"RET\"
  209. without entering any text or cycling through the offered choices.
  210. This replaces the standard ido behavior of returning the first
  211. item on the list. Enabling this option improves compatibility
  212. with many older functions that use `completing-read' in this way,
  213. but may also break compatibility with others, since it changes
  214. what ido returns.
  215. If you want this enabled most of the time but once in a while you
  216. really want to select the first item on the list, you can do so
  217. by prefixing \"RET\" with \"C-u\".
  218. This has no effect when ido is completing buffers or files."
  219. :type 'boolean
  220. :group 'ido-ubiquitous)
  221. (defvar ido-ubiquitous-initial-item nil
  222. "The first item selected when ido starts.")
  223. (defadvice ido-read-internal (before clear-initial-item activate)
  224. (setq ido-ubiquitous-initial-item nil))
  225. (defadvice ido-make-choice-list (after set-initial-item activate)
  226. (when (and ad-return-value (listp ad-return-value))
  227. (setq ido-ubiquitous-initial-item (car ad-return-value))))
  228. (defadvice ido-next-match (after clear-initial-item activate)
  229. (setq ido-ubiquitous-initial-item nil))
  230. (defadvice ido-prev-match (after clear-initial-item activate)
  231. (setq ido-ubiquitous-initial-item nil))
  232. (defadvice ido-exit-minibuffer (around required-allow-empty-string activate)
  233. "Emulate a quirk of `completing-read'.
  234. Apparently, in the past `completing-read' used to request the
  235. default item by returning an empty string when RET was pressed
  236. with an empty input. This forces `ido-completing-read' to do the
  237. same (instead of returning the first choice in the list),
  238. allowing the default to be properly selected.
  239. This has no effect when ido is completing buffers or files.
  240. This behavior is disabled by setting
  241. `ido-ubiquitous-enable-compatibility' to nil."
  242. (if (and ido-ubiquitous-enable-compatibility
  243. (eq ido-cur-item 'list)
  244. ido-require-match
  245. (null ido-default-item)
  246. (not current-prefix-arg)
  247. (string= ido-text "")
  248. (string= (car ido-cur-list)
  249. ido-ubiquitous-initial-item))
  250. (ido-select-text)
  251. ad-do-it))
  252. (defadvice bookmark-completing-read (around disable-ido-compatibility activate)
  253. "`bookmark-completing-read' uses `completing-read' in an odd
  254. way that conflicts with the compatibilty mode of
  255. ido-ubiquitous."
  256. (let (ido-ubiquitous-enable-compatibility)
  257. ad-do-it))
  258. ;; Make sure the mode is initialized for the first time
  259. (ido-ubiquitous-mode (if ido-ubiquitous-mode 1 0))
  260. (provide 'ido-ubiquitous) ;;; ido-ubiquitous.el ends here