ido-completing-read+.el 7.6 KB

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