ido-completing-read+.el 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. ;;; ido-completing-read+.el --- A completing-read-function using ido -*- "lexical-binding": t -*-
  2. ;;
  3. ;; Filename: ido-completing-read+.el
  4. ;; Author: Ryan Thompson
  5. ;; Created: Sat Apr 4 13:41:20 2015 (-0700)
  6. ;; Version: 0.1
  7. ;; Package-Requires: ((emacs "24.1"))
  8. ;; URL: https://github.com/DarwinAwardWinner/ido-ubiquitous
  9. ;; Keywords: ido, completion, convenience
  10. ;;
  11. ;; This file is NOT part of GNU Emacs.
  12. ;;
  13. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  14. ;;
  15. ;;; Commentary:
  16. ;;
  17. ;; This package implments the `ido-completing-read+' function, which
  18. ;; is a wrapper for `ido-completing-read'. Importantly, it detects
  19. ;; edge cases that ordinary ido cannot handle and either adjusts them
  20. ;; so ido *can* handle them, or else simply falls back to Emacs'
  21. ;; standard completion instead.
  22. ;;
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;; This program is free software: you can redistribute it and/or modify
  26. ;; it under the terms of the GNU General Public License as published by
  27. ;; the Free Software Foundation, either version 3 of the License, or (at
  28. ;; your option) any later version.
  29. ;;
  30. ;; This program is distributed in the hope that it will be useful, but
  31. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  32. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  33. ;; General Public License for more details.
  34. ;;
  35. ;; You should have received a copy of the GNU General Public License
  36. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  37. ;;
  38. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  39. ;;
  40. ;;; Code:
  41. (require 'cl-macs)
  42. (defvar ido-cr+-enable-next-call nil
  43. "If non-nil, then the next call to `ido-completing-read' is by `ido-completing-read+'.")
  44. (defvar ido-cr+-enable-this-call nil
  45. "If non-nil, then the current call to `ido-completing-read' is by `ido-completing-read+'")
  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+)
  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 5000
  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+)
  80. ;; Signal used to trigger fallback
  81. (define-error 'ido-cr+-fallback "ido-cr+-fallback")
  82. (defun ido-completing-read+ (prompt collection &optional predicate
  83. require-match initial-input
  84. hist def inherit-input-method)
  85. "ido-based method for reading from the minibuffer with completion.
  86. See `completing-read' for the meaning of the arguments.
  87. This function is a wrapper for `ido-completing-read' designed to
  88. be used as the value of `completing-read-function'. Importantly,
  89. it detects edge cases that ido cannot handle and uses normal
  90. completion for them."
  91. (let (;; Save the original arguments in case we need to do the
  92. ;; fallback
  93. (orig-args
  94. (list prompt collection predicate require-match
  95. initial-input hist def inherit-input-method)))
  96. (condition-case nil
  97. (progn
  98. (when (or
  99. ;; Can't handle this arg
  100. inherit-input-method
  101. ;; Can't handle this being set
  102. (bound-and-true-p completion-extra-properties)
  103. ;; Can't handle functional collection
  104. (functionp collection))
  105. (signal 'ido-cr+-fallback nil))
  106. ;; Expand all possible completions
  107. (setq collection (all-completions "" collection predicate))
  108. ;; Check for excessively large collection
  109. (when (and ido-cr+-max-items
  110. (> (length collection) ido-cr+-max-items))
  111. (signal 'ido-cr+-fallback nil))
  112. ;; ido doesn't natively handle DEF being a list. If DEF is
  113. ;; a list, prepend it to CHOICES and set DEF to just the
  114. ;; car of the default list.
  115. (when (and def (listp def))
  116. (setq choices
  117. (append def
  118. (nreverse (cl-set-difference choices def)))
  119. def (car def)))
  120. ;; Work around a bug in ido when both INITIAL-INPUT and
  121. ;; DEF are provided.
  122. (let ((initial
  123. (or (if (consp initial-input)
  124. (car initial-input)
  125. initial-input)
  126. "")))
  127. (when (and def initial
  128. (stringp initial)
  129. (not (string= initial "")))
  130. ;; Both default and initial input were provided. So
  131. ;; keep the initial input and preprocess the choices
  132. ;; list to put the default at the head, then proceed
  133. ;; with default = nil.
  134. (setq choices (cons def (remove def choices))
  135. def nil)))
  136. ;; Ready to do actual ido completion
  137. (prog1
  138. (let ((ido-cr+-enable-next-call t))
  139. (ido-completing-read
  140. prompt collection
  141. predicate require-match initial-input hist def
  142. inherit-input-method))
  143. ;; This detects when the user triggered fallback mode
  144. ;; manually.
  145. (when (eq ido-exit 'fallback)
  146. (signal 'ido-cr+-fallback nil))))
  147. ;; Handler for ido-cr+-fallback signal
  148. (ido-cr+-fallback
  149. (apply ido-cr+-fallback-function orig-args)))))
  150. (defadvice ido-completing-read (around ido-cr+ activate)
  151. "Ensure that `ido-cr+-enable-this-call' is set properly.
  152. This variable should be non-nil while executing
  153. `ido-completing-read' if it was called from
  154. `ido-completing-read+'."
  155. (let ((ido-cr+-enable-this-call ido-cr+-enable-next-call)
  156. (ido-cr+-enable-next-call nil))
  157. ad-do-it))
  158. ;; Fallback on magic C-f and C-b
  159. (defadvice ido-magic-forward-char (before ido-cr+-fallback activate)
  160. "Allow falling back in ido-completing-read+."
  161. (message "ido-cr+-enable-this-call is %S" ido-cr+-enable-this-call)
  162. (when ido-cr+-enable-this-call
  163. ;; `ido-context-switch-command' is already let-bound at this
  164. ;; point.
  165. (setq ido-context-switch-command #'ido-fallback-command)))
  166. (defadvice ido-magic-backward-char (before ido-cr+-fallback activate)
  167. "Allow falling back in ido-completing-read+."
  168. (when ido-cr+-enable-this-call
  169. ;; `ido-context-switch-command' is already let-bound at this
  170. ;; point.
  171. (setq ido-context-switch-command #'ido-fallback-command)))
  172. (provide 'ido-completing-read+)
  173. ;;; ido-completing-read+.el ends here