ido-completing-read+.el 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. ;;; ido-completing-read+.el --- A completing-read-function using ido -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2015 Ryan C. Thompson
  3. ;; Filename: ido-completing-read+.el
  4. ;; Author: Ryan Thompson
  5. ;; Created: Sat Apr 4 13:41:20 2015 (-0700)
  6. ;; Version: 3.0
  7. ;; Package-Requires: ((emacs "24.1"))
  8. ;; URL: https://github.com/DarwinAwardWinner/ido-ubiquitous
  9. ;; Keywords: ido, completion, convenience
  10. ;; This file is NOT part of GNU Emacs.
  11. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  12. ;;
  13. ;;; Commentary:
  14. ;; This package implments the `ido-completing-read+' function, which
  15. ;; is a wrapper for `ido-completing-read'. Importantly, it detects
  16. ;; edge cases that ordinary ido cannot handle and either adjusts them
  17. ;; so ido *can* handle them, or else simply falls back to Emacs'
  18. ;; standard completion instead.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;; This program is free software: you can redistribute it and/or modify
  22. ;; it under the terms of the GNU General Public License as published by
  23. ;; the Free Software Foundation, either version 3 of the License, or (at
  24. ;; your option) any later version.
  25. ;;
  26. ;; This program is distributed in the hope that it will be useful, but
  27. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  28. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  29. ;; General Public License for more details.
  30. ;;
  31. ;; You should have received a copy of the GNU General Public License
  32. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  33. ;;
  34. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  35. ;;
  36. ;;; Code:
  37. (require 'ido)
  38. (defvar ido-cr+-enable-next-call nil
  39. "If non-nil, then the next call to `ido-completing-read' is by `ido-completing-read+'.")
  40. (defvar ido-cr+-enable-this-call nil
  41. "If non-nil, then the current call to `ido-completing-read' is by `ido-completing-read+'")
  42. (defgroup ido-completing-read-plus nil
  43. "Extra features and compatibility for `ido-completing-read'."
  44. :group 'ido)
  45. (defcustom ido-cr+-fallback-function
  46. ;; Initialize to the current value of `completing-read-function',
  47. ;; unless that is already set to the ido completer, in which case
  48. ;; use `completing-read-default'.
  49. (if (memq completing-read-function
  50. '(ido-completing-read+
  51. ido-completing-read
  52. ;; Old ido-ubiquitous functions that shouldn't be used
  53. completing-read-ido
  54. ido-ubiquitous-completing-read))
  55. 'completing-read-default
  56. completing-read-function)
  57. "Alternate completing-read function to use when ido is not wanted.
  58. This will be used for functions that are incompatibile with ido
  59. or if ido cannot handle the completion arguments. It will also be
  60. used when the user requests non-ido completion manually via C-f
  61. or C-b."
  62. :type '(choice (const :tag "Standard emacs completion"
  63. completing-read-default)
  64. (function :tag "Other function"))
  65. :group 'ido-completing-read-plus)
  66. (defcustom ido-cr+-max-items 30000
  67. "Max collection size to use ido-cr+ on.
  68. If `ido-completing-read+' is called on a collection larger than
  69. this, the fallback completion method will be used instead. To
  70. disable fallback based on collection size, set this to nil."
  71. :type '(choice (const :tag "No limit" nil)
  72. (integer
  73. :tag "Limit" :value 30000
  74. :validate
  75. (lambda (widget)
  76. (let ((v (widget-value widget)))
  77. (if (and (integerp v)
  78. (> v 0))
  79. nil
  80. (widget-put widget :error "This field should contain a positive integer")
  81. widget)))))
  82. :group 'ido-completing-read-plus)
  83. (defcustom ido-cr+-replace-completely nil
  84. "If non-nil, replace `ido-completeing-read' completely with ido-cr+.
  85. Enabling this may interfere with or cause errors in other
  86. packages that use `ido-completing-read'. If you discover any such
  87. incompatibilites, please file a bug report at
  88. https://github.com/DarwinAwardWinner/ido-ubiquitous/issues"
  89. :type 'boolean)
  90. ;; Signal used to trigger fallback
  91. (define-error 'ido-cr+-fallback "ido-cr+-fallback")
  92. ;;;###autoload
  93. (defun ido-completing-read+ (prompt collection &optional predicate
  94. require-match initial-input
  95. hist def inherit-input-method)
  96. "ido-based method for reading from the minibuffer with completion.
  97. See `completing-read' for the meaning of the arguments.
  98. This function is a wrapper for `ido-completing-read' designed to
  99. be used as the value of `completing-read-function'. Importantly,
  100. it detects edge cases that ido cannot handle and uses normal
  101. completion for them."
  102. (let (;; Save the original arguments in case we need to do the
  103. ;; fallback
  104. (orig-args
  105. (list prompt collection predicate require-match
  106. initial-input hist def inherit-input-method)))
  107. (condition-case nil
  108. (progn
  109. (when (or
  110. ;; Can't handle this arg
  111. inherit-input-method
  112. ;; Can't handle this being set
  113. (bound-and-true-p completion-extra-properties)
  114. ;; Can't handle functional collection
  115. (functionp collection))
  116. (signal 'ido-cr+-fallback nil))
  117. ;; Expand all possible completions
  118. (setq collection (all-completions "" collection predicate))
  119. ;; Check for excessively large collection
  120. (when (and ido-cr+-max-items
  121. (> (length collection) ido-cr+-max-items))
  122. (signal 'ido-cr+-fallback nil))
  123. ;; ido doesn't natively handle DEF being a list. If DEF is
  124. ;; a list, prepend it to CHOICES and set DEF to just the
  125. ;; car of the default list.
  126. (when (and def (listp def))
  127. (setq choices
  128. (append def
  129. (nreverse (cl-set-difference choices def)))
  130. def (car def)))
  131. ;; Work around a bug in ido when both INITIAL-INPUT and
  132. ;; DEF are provided.
  133. (let ((initial
  134. (or (if (consp initial-input)
  135. (car initial-input)
  136. initial-input)
  137. "")))
  138. (when (and def initial
  139. (stringp initial)
  140. (not (string= initial "")))
  141. ;; Both default and initial input were provided. So
  142. ;; keep the initial input and preprocess the choices
  143. ;; list to put the default at the head, then proceed
  144. ;; with default = nil.
  145. (setq choices (cons def (remove def choices))
  146. def nil)))
  147. ;; Ready to do actual ido completion
  148. (prog1
  149. (let ((ido-cr+-enable-next-call t))
  150. (ido-completing-read
  151. prompt collection
  152. predicate require-match initial-input hist def
  153. inherit-input-method))
  154. ;; This detects when the user triggered fallback mode
  155. ;; manually.
  156. (when (eq ido-exit 'fallback)
  157. (signal 'ido-cr+-fallback nil))))
  158. ;; Handler for ido-cr+-fallback signal
  159. (ido-cr+-fallback
  160. (apply ido-cr+-fallback-function orig-args)))))
  161. ;;;###autoload
  162. (defadvice ido-completing-read (around ido-cr+ activate)
  163. "This advice handles application of ido-completing-read+ features.
  164. First, it ensures that `ido-cr+-enable-this-call' is set
  165. properly. This variable should be non-nil during execution of
  166. `ido-completing-read' if it was called from
  167. `ido-completing-read+'.
  168. Second, if `ido-cr+-replace-completely' is non-nil, then this
  169. advice completely replaces `ido-completing-read' with
  170. `ido-completing-read+'."
  171. ;; If this advice is autoloaded, then we need to force loading of
  172. ;; the rest of the file so all the variables will be defined.
  173. (when (not (featurep 'ido-completing-read+))
  174. (require 'ido-completing-read+))
  175. (let ((ido-cr+-enable-this-call ido-cr+-enable-next-call)
  176. (ido-cr+-enable-next-call nil))
  177. (if (or
  178. ido-cr+-enable-this-call ; Avoid recursion
  179. (not ido-cr+-replace-completely))
  180. ad-do-it
  181. (message "Replacing ido-completing-read")
  182. (setq ad-return-value (apply #'ido-completing-read+ (ad-get-args 0))))))
  183. ;; Fallback on magic C-f and C-b
  184. (defadvice ido-magic-forward-char (before ido-cr+-fallback activate)
  185. "Allow falling back in ido-completing-read+."
  186. (message "ido-cr+-enable-this-call is %S" ido-cr+-enable-this-call)
  187. (when ido-cr+-enable-this-call
  188. ;; `ido-context-switch-command' is already let-bound at this
  189. ;; point.
  190. (setq ido-context-switch-command #'ido-fallback-command)))
  191. (defadvice ido-magic-backward-char (before ido-cr+-fallback activate)
  192. "Allow falling back in ido-completing-read+."
  193. (when ido-cr+-enable-this-call
  194. ;; `ido-context-switch-command' is already let-bound at this
  195. ;; point.
  196. (setq ido-context-switch-command #'ido-fallback-command)))
  197. (provide 'ido-completing-read+)
  198. ;;; ido-completing-read+.el ends here