ido-completing-read+.el 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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: 2.17
  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. ;; TODO: autoloads, obsolete aliases
  38. (require 'ido)
  39. (defvar ido-cr+-enable-next-call nil
  40. "If non-nil, then the next call to `ido-completing-read' is by `ido-completing-read+'.")
  41. (defvar ido-cr+-enable-this-call nil
  42. "If non-nil, then the current call to `ido-completing-read' is by `ido-completing-read+'")
  43. (defgroup ido-completing-read-plus nil
  44. "Extra features and compatibility for `ido-completing-read'."
  45. :group 'ido)
  46. (defcustom ido-cr+-fallback-function
  47. ;; Initialize to the current value of `completing-read-function',
  48. ;; unless that is already set to the ido completer, in which case
  49. ;; use `completing-read-default'.
  50. (if (memq completing-read-function
  51. '(ido-completing-read+ ido-completing-read))
  52. 'completing-read-default
  53. completing-read-function)
  54. "Alternate completing-read function to use when ido is not wanted.
  55. This will be used for functions that are incompatibile with ido
  56. or if ido cannot handle the completion arguments. It will also be
  57. used when the user requests non-ido completion manually via C-f
  58. or C-b."
  59. :type '(choice (const :tag "Standard emacs completion"
  60. completing-read-default)
  61. (function :tag "Other function"))
  62. :group 'ido-completing-read-plus)
  63. (defcustom ido-cr+-max-items 30000
  64. "Max collection size to use ido-cr+ on.
  65. If `ido-completing-read+' is called on a collection larger than
  66. this, the fallback completion method will be used instead. To
  67. disable fallback based on collection size, set this to nil."
  68. :type '(choice (const :tag "No limit" nil)
  69. (integer
  70. :tag "Limit" :value 30000
  71. :validate
  72. (lambda (widget)
  73. (let ((v (widget-value widget)))
  74. (if (and (integerp v)
  75. (> v 0))
  76. nil
  77. (widget-put widget :error "This field should contain a positive integer")
  78. widget)))))
  79. :group 'ido-completing-read-plus)
  80. (define-obsolete-variable-alias 'ido-ubiquitous-max-items 'ido-cr+-max-items "2.17")
  81. (defcustom ido-cr+-replace-completely nil
  82. "If non-nil, replace `ido-completeing-read' completely with ido-cr+.
  83. Enabling this may interfere with or cause errors in other
  84. packages that use `ido-completing-read'. If you discover any such
  85. incompatibilites, please file a bug report at
  86. https://github.com/DarwinAwardWinner/ido-ubiquitous/issues"
  87. :type 'boolean)
  88. ;; Signal used to trigger fallback
  89. (define-error 'ido-cr+-fallback "ido-cr+-fallback")
  90. (defun ido-completing-read+ (prompt collection &optional predicate
  91. require-match initial-input
  92. hist def inherit-input-method)
  93. "ido-based method for reading from the minibuffer with completion.
  94. See `completing-read' for the meaning of the arguments.
  95. This function is a wrapper for `ido-completing-read' designed to
  96. be used as the value of `completing-read-function'. Importantly,
  97. it detects edge cases that ido cannot handle and uses normal
  98. completion for them."
  99. (let (;; Save the original arguments in case we need to do the
  100. ;; fallback
  101. (orig-args
  102. (list prompt collection predicate require-match
  103. initial-input hist def inherit-input-method)))
  104. (condition-case nil
  105. (progn
  106. (when (or
  107. ;; Can't handle this arg
  108. inherit-input-method
  109. ;; Can't handle this being set
  110. (bound-and-true-p completion-extra-properties)
  111. ;; Can't handle functional collection
  112. (functionp collection))
  113. (signal 'ido-cr+-fallback nil))
  114. ;; Expand all possible completions
  115. (setq collection (all-completions "" collection predicate))
  116. ;; Check for excessively large collection
  117. (when (and ido-cr+-max-items
  118. (> (length collection) ido-cr+-max-items))
  119. (signal 'ido-cr+-fallback nil))
  120. ;; ido doesn't natively handle DEF being a list. If DEF is
  121. ;; a list, prepend it to CHOICES and set DEF to just the
  122. ;; car of the default list.
  123. (when (and def (listp def))
  124. (setq choices
  125. (append def
  126. (nreverse (cl-set-difference choices def)))
  127. def (car def)))
  128. ;; Work around a bug in ido when both INITIAL-INPUT and
  129. ;; DEF are provided.
  130. (let ((initial
  131. (or (if (consp initial-input)
  132. (car initial-input)
  133. initial-input)
  134. "")))
  135. (when (and def initial
  136. (stringp initial)
  137. (not (string= initial "")))
  138. ;; Both default and initial input were provided. So
  139. ;; keep the initial input and preprocess the choices
  140. ;; list to put the default at the head, then proceed
  141. ;; with default = nil.
  142. (setq choices (cons def (remove def choices))
  143. def nil)))
  144. ;; Ready to do actual ido completion
  145. (prog1
  146. (let ((ido-cr+-enable-next-call t))
  147. (ido-completing-read
  148. prompt collection
  149. predicate require-match initial-input hist def
  150. inherit-input-method))
  151. ;; This detects when the user triggered fallback mode
  152. ;; manually.
  153. (when (eq ido-exit 'fallback)
  154. (signal 'ido-cr+-fallback nil))))
  155. ;; Handler for ido-cr+-fallback signal
  156. (ido-cr+-fallback
  157. (apply ido-cr+-fallback-function orig-args)))))
  158. (defadvice ido-completing-read (around ido-cr+ activate)
  159. "This advice handles application of ido-completing-read+ features.
  160. First, it ensures that `ido-cr+-enable-this-call' is set
  161. properly. This variable should be non-nil during execution of
  162. `ido-completing-read' if it was called from
  163. `ido-completing-read+'.
  164. Second, if `ido-cr+-replace-completely' is non-nil, then this
  165. advice completely replaces `ido-completing-read' with
  166. `ido-completing-read+'."
  167. (let ((ido-cr+-enable-this-call ido-cr+-enable-next-call)
  168. (ido-cr+-enable-next-call nil))
  169. (if (or
  170. ido-cr+-enable-this-call ; Avoid recursion
  171. (not ido-cr+-replace-completely))
  172. ad-do-it
  173. (message "Replacing ido-completing-read")
  174. (setq ad-return-value (apply #'ido-completing-read+ (ad-get-args 0))))))
  175. ;; Fallback on magic C-f and C-b
  176. (defadvice ido-magic-forward-char (before ido-cr+-fallback activate)
  177. "Allow falling back in ido-completing-read+."
  178. (message "ido-cr+-enable-this-call is %S" ido-cr+-enable-this-call)
  179. (when ido-cr+-enable-this-call
  180. ;; `ido-context-switch-command' is already let-bound at this
  181. ;; point.
  182. (setq ido-context-switch-command #'ido-fallback-command)))
  183. (defadvice ido-magic-backward-char (before ido-cr+-fallback activate)
  184. "Allow falling back in ido-completing-read+."
  185. (when ido-cr+-enable-this-call
  186. ;; `ido-context-switch-command' is already let-bound at this
  187. ;; point.
  188. (setq ido-context-switch-command #'ido-fallback-command)))
  189. (provide 'ido-completing-read+)
  190. ;;; ido-completing-read+.el ends here