ido-ubiquitous.el 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. ;;;###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. `disable-ido-ubiquitous-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. (disable-ido-ubiquitous-in foo)"
  52. nil
  53. :global t
  54. :group 'ido-ubiquitous)
  55. ;;;###autoload
  56. (defcustom ido-ubiquitous-exceptions '(grep-read-files)
  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. :type '(repeat symbol)
  62. :group 'ido-ubiquitous)
  63. (defadvice completing-read (around ido-ubiquitous activate)
  64. (if (or (not ido-mode)
  65. (not ido-ubiquitous)
  66. (memq this-command ido-ubiquitous-exceptions)
  67. ;; Avoid infinite recursion from ido calling completing-read
  68. (boundp 'ido-cur-item))
  69. ad-do-it
  70. (let ((allcomp (all-completions "" collection predicate)))
  71. ;; Only use ido completion if there are actually any completions to offer.
  72. (if allcomp
  73. (setq ad-return-value
  74. (ido-completing-read prompt allcomp
  75. nil require-match initial-input hist def))
  76. ad-do-it))))
  77. (defmacro disable-ido-ubiquitous-in (func)
  78. "Disable ido-ubiquitous in FUNC."
  79. `(defadvice ,func (around disable-ido-ubiquitous activate)
  80. (let (ido-ubiquitous) ad-do-it)))
  81. (defmacro enable-ido-ubiquitous-in (func)
  82. "Re-enable ido-ubiquitous in FUNC.
  83. This reverses the effect of `disable-ido-ubiquitous-in'."
  84. ;; In my experience, simply using `ad-remove-advice' or
  85. ;; `ad-disable-advice' doesn't work correctly (in Emacs 23).
  86. ;; Instead, I've found that one must redefine the advice under the
  87. ;; same name ("disable-ido-ubiquitous") to simply call the original
  88. ;; function with no modifications. This has the same effect
  89. ;; (disables the advice), but is presumably less efficient.
  90. `(defadvice ,func (around disable-ido-ubiquitous activate)
  91. ad-do-it))
  92. ;; Disable ido-ubiquitous in `find-file' and similar functions,
  93. ;; because they are not supposed to use ido.
  94. (disable-ido-ubiquitous-in read-file-name)
  95. (provide 'ido-ubiquitous) ;;; ido-ubiquitous.el ends here