ido-ubiquitous.el 6.0 KB

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