ido-ubiquitous.el 6.2 KB

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