ido-completing-read+.el 8.9 KB

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