ido-ubiquitous.el 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. ;;; ido-ubiquitous.el --- Use ido (nearly) everywhere.
  2. ;; Author: Ryan C. Thompson
  3. ;; URL: https://github.com/DarwinAwardWinner/ido-ubiquitous
  4. ;; Version: 0.7
  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. ;;; License:
  26. ;; This program is free software; you can redistribute it and/or modify
  27. ;; it under the terms of the GNU General Public License as published by
  28. ;; the Free Software Foundation; either version 3, or (at your option)
  29. ;; any later version.
  30. ;;
  31. ;; This program is distributed in the hope that it will be useful,
  32. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. ;; GNU General Public License for more details.
  35. ;;
  36. ;; You should have received a copy of the GNU General Public License
  37. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  38. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  39. ;; Boston, MA 02110-1301, USA.
  40. ;;; Code:
  41. (require 'ido)
  42. ;;;###autoload
  43. (defgroup ido-ubiquitous nil
  44. "Use ido for (almost) all completion."
  45. :group 'ido)
  46. ;;;###autoload
  47. (define-minor-mode ido-ubiquitous
  48. "Use `ido-completing-read' instead of `completing-read' almost everywhere.
  49. This mode has no effect unles `ido-mode' is also enabled.
  50. If this mode causes problems for a function, you can force the
  51. function to use the original completing read by using the macro
  52. `ido-ubiquitous-disable-in'. For example, if a
  53. function `foo' cannot work with ido-style completion, evaluate
  54. the following (for example by putting it in your .emacs file):
  55. (ido-ubiquitous-disable-in foo)"
  56. nil
  57. :global t
  58. :group 'ido-ubiquitous)
  59. ;;;###autoload
  60. (defcustom ido-ubiquitous-command-exceptions '()
  61. "List of commands that should not be affected by `ido-ubiquitous'.
  62. Even when `ido-ubiquitous' mode is enabled, these commands will
  63. continue to use `completing-read' instead of
  64. `ido-completing-read'.
  65. Only *interactive* commands should go here. To disable
  66. ido-ubiquitous in non-interactive functions, customize
  67. `ido-ubiquitous-function-exceptions'."
  68. :type '(repeat (symbol :tag "Command"))
  69. :group 'ido-ubiquitous)
  70. (define-obsolete-variable-alias 'ido-ubiquitous-exceptions
  71. 'ido-ubiquitous-command-exceptions "0.4")
  72. (defadvice completing-read (around ido-ubiquitous activate)
  73. (if (or (not ido-mode)
  74. (not ido-ubiquitous)
  75. (memq this-command ido-ubiquitous-command-exceptions)
  76. ;; Avoid infinite recursion from ido calling completing-read
  77. (boundp 'ido-cur-item))
  78. ad-do-it
  79. (let ((allcomp (all-completions "" collection predicate)))
  80. ;; Only use ido completion if there are actually any completions
  81. ;; to offer.
  82. (if allcomp
  83. (setq ad-return-value
  84. (ido-completing-read prompt allcomp
  85. nil require-match initial-input hist def))
  86. ad-do-it))))
  87. (defmacro ido-ubiquitous-disable-in (func)
  88. "Disable ido-ubiquitous in FUNC."
  89. (let ((docstring
  90. (format "Disable ido-ubiquitous in %s" func)))
  91. `(defadvice ,func (around disable-ido-ubiquitous activate)
  92. ,docstring
  93. (let (ido-ubiquitous) ad-do-it))))
  94. (define-obsolete-function-alias
  95. 'disable-ido-ubiquitous-in
  96. 'ido-ubiquitous-disable-in
  97. "0.4")
  98. (defmacro ido-ubiquitous-enable-in (func)
  99. "Re-enable ido-ubiquitous in FUNC.
  100. This reverses the effect of `ido-ubiquitous-disable-in'."
  101. ;; In my experience, simply using `ad-remove-advice' or
  102. ;; `ad-disable-advice' doesn't work correctly (in Emacs 23).
  103. ;; Instead, I've found that one must redefine the advice under the
  104. ;; same name ("disable-ido-ubiquitous") to simply call the original
  105. ;; function with no modifications. This has the same effect
  106. ;; (disables the advice), but is presumably less efficient.
  107. (let ((docstring
  108. (format "DO NOT disable ido-ubiquitous in %s" func)))
  109. `(defadvice ,func (around disable-ido-ubiquitous activate)
  110. ,docstring
  111. ad-do-it)))
  112. (define-obsolete-function-alias
  113. 'enable-ido-ubiquitous-in
  114. 'ido-ubiquitous-enable-in
  115. "0.4")
  116. ;; Always disable ido-ubiquitous in `find-file' and similar functions,
  117. ;; because they are not supposed to use ido.
  118. (defvar ido-ubiquitous-permanent-function-exceptions
  119. '(read-file-name)
  120. "Functions in which ido-ubiquitous should always be disabled.
  121. If you want to disable ido in a specific function or command, do
  122. not modify this variable. Instead, try `M-x customize-group
  123. ido-ubiquitous..")
  124. (dolist (func ido-ubiquitous-permanent-function-exceptions)
  125. (eval `(ido-ubiquitous-disable-in ,func)))
  126. (defun ido-ubiquitous--set-difference (list1 list2)
  127. "Replacement for `set-difference' from `cl'."
  128. (apply #'nconc
  129. (mapcar (lambda (elt) (unless (memq elt list2) (list elt)))
  130. list1)))
  131. (defun ido-ubiquitous-set-function-exceptions (sym newval)
  132. (let* ((oldval (when (boundp sym) (eval sym))))
  133. ;; Filter out permanent fixtures
  134. (setq oldval (ido-ubiquitous--set-difference oldval ido-ubiquitous-permanent-function-exceptions))
  135. (setq newval (ido-ubiquitous--set-difference newval ido-ubiquitous-permanent-function-exceptions))
  136. ;; Re-enable ido-ubiquitous on all old functions, in case they
  137. ;; were removed from the list.
  138. (dolist (oldfun oldval)
  139. (eval `(ido-ubiquitous-enable-in ,oldfun)))
  140. ;; Set the new value
  141. (set-default sym newval)
  142. ;; Disable ido-ubiquitous on all new functions
  143. (dolist (newfun newval)
  144. (eval `(ido-ubiquitous-disable-in ,newfun)))))
  145. ;;;###autoload
  146. (defcustom ido-ubiquitous-function-exceptions
  147. '(grep-read-files)
  148. "List of functions in which to disable ido-ubiquitous.
  149. Certain functions, such as `read-file-name', always have
  150. ido-ubiquitous disabled, and cannot be added here. (They are
  151. effectively permanently part of this list already.)"
  152. :group 'ido-ubiquitous
  153. :type '(repeat :tag "Functions"
  154. (symbol :tag "Function"))
  155. :set 'ido-ubiquitous-set-function-exceptions)
  156. (defadvice ido-exit-minibuffer (around required-allow-empty-string activate)
  157. "Emulate a quirk of `completing-read'.
  158. Apparently, `completing-read' used to request the default item by
  159. returning an empty string when RET was pressed with an empty input.
  160. This forces `ido-completing-read' to do the same (instead of returning
  161. the first choice in the list).
  162. This has no effect when ido is completing buffers or files."
  163. (if (and (eq ido-cur-item 'list)
  164. ido-require-match
  165. (null ido-default-item)
  166. (string= ido-text ""))
  167. (ido-select-text)
  168. ad-do-it))
  169. (provide 'ido-ubiquitous) ;;; ido-ubiquitous.el ends here