ido-completing-read+.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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.4
  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. (defconst ido-completing-read+-version "3.4"
  38. "Currently running version of ido-ubiquitous.
  39. Note that when you update ido-completing-read+, this variable may
  40. not be updated until you restart Emacs.")
  41. (require 'ido)
  42. (require 'cl-macs)
  43. ;;; Debug messages
  44. (defvar ido-cr+-debug-mode)
  45. ;; Defined as a macro for efficiency (args are not evaluated unless
  46. ;; debug mode is on)
  47. (defmacro ido-cr+--debug-message (format-string &rest args)
  48. `(when ido-cr+-debug-mode
  49. (message (concat "ido-completing-read+: " ,format-string) ,@args)))
  50. ;;; Core code
  51. (defvar ido-cr+-enable-next-call nil
  52. "If non-nil, then the next call to `ido-completing-read' is by `ido-completing-read+'.")
  53. (defvar ido-cr+-enable-this-call nil
  54. "If non-nil, then the current call to `ido-completing-read' is by `ido-completing-read+'")
  55. (defgroup ido-completing-read-plus nil
  56. "Extra features and compatibility for `ido-completing-read'."
  57. :group 'ido)
  58. (defcustom ido-cr+-fallback-function
  59. ;; Initialize to the current value of `completing-read-function',
  60. ;; unless that is already set to the ido completer, in which case
  61. ;; use `completing-read-default'.
  62. (if (memq completing-read-function
  63. '(ido-completing-read+
  64. ido-completing-read
  65. ;; Old ido-ubiquitous functions that shouldn't be used
  66. completing-read-ido
  67. ido-ubiquitous-completing-read))
  68. 'completing-read-default
  69. completing-read-function)
  70. "Alternate completing-read function to use when ido is not wanted.
  71. This will be used for functions that are incompatibile with ido
  72. or if ido cannot handle the completion arguments. It will also be
  73. used when the user requests non-ido completion manually via C-f
  74. or C-b."
  75. :type '(choice (const :tag "Standard emacs completion"
  76. completing-read-default)
  77. (function :tag "Other function"))
  78. :group 'ido-completing-read-plus)
  79. (defcustom ido-cr+-max-items 30000
  80. "Max collection size to use ido-cr+ on.
  81. If `ido-completing-read+' is called on a collection larger than
  82. this, the fallback completion method will be used instead. To
  83. disable fallback based on collection size, set this to nil."
  84. :type '(choice (const :tag "No limit" nil)
  85. (integer
  86. :tag "Limit" :value 30000
  87. :validate
  88. (lambda (widget)
  89. (let ((v (widget-value widget)))
  90. (if (and (integerp v)
  91. (> v 0))
  92. nil
  93. (widget-put widget :error "This field should contain a positive integer")
  94. widget)))))
  95. :group 'ido-completing-read-plus)
  96. (defcustom ido-cr+-replace-completely nil
  97. "If non-nil, replace `ido-completeing-read' completely with ido-cr+.
  98. Enabling this may interfere with or cause errors in other
  99. packages that use `ido-completing-read'. If you discover any such
  100. incompatibilites, please file a bug report at
  101. https://github.com/DarwinAwardWinner/ido-ubiquitous/issues"
  102. :type 'boolean)
  103. ;; Signal used to trigger fallback
  104. (put 'ido-cr+-fallback 'error-conditions '(ido-cr+-fallback error))
  105. (put 'ido-cr+-fallback 'error-message "ido-cr+-fallback")
  106. (defun ido-cr+--explain-fallback (arg)
  107. ;; This function accepts a string, or an ido-cr+-fallback
  108. ;; signal.
  109. (when ido-cr+-debug-mode
  110. (when (and (listp arg)
  111. (eq (car arg) 'ido-cr+-fallback))
  112. (setq arg (cdr arg)))
  113. (ido-cr+--debug-message "Falling back to `%s' because %s."
  114. ido-cr+-fallback-function arg)))
  115. ;;;###autoload
  116. (defun ido-completing-read+ (prompt collection &optional predicate
  117. require-match initial-input
  118. hist def inherit-input-method)
  119. "ido-based method for reading from the minibuffer with completion.
  120. See `completing-read' for the meaning of the arguments.
  121. This function is a wrapper for `ido-completing-read' designed to
  122. be used as the value of `completing-read-function'. Importantly,
  123. it detects edge cases that ido cannot handle and uses normal
  124. completion for them."
  125. (let (;; Save the original arguments in case we need to do the
  126. ;; fallback
  127. (orig-args
  128. (list prompt collection predicate require-match
  129. initial-input hist def inherit-input-method)))
  130. (condition-case sig
  131. (progn
  132. (cond
  133. (inherit-input-method
  134. (signal 'ido-cr+-fallback
  135. "ido cannot handle non-nil INHERIT-INPUT-METHOD"))
  136. ((bound-and-true-p completion-extra-properties)
  137. (signal 'ido-cr+-fallback
  138. "ido cannot handle non-nil `completion-extra-properties'"))
  139. ((functionp collection)
  140. (signal 'ido-cr+-fallback
  141. "ido cannot handle COLLECTION being a function")))
  142. ;; Expand all possible completions
  143. (setq collection (all-completions "" collection predicate))
  144. ;; Check for excessively large collection
  145. (when (and ido-cr+-max-items
  146. (> (length collection) ido-cr+-max-items))
  147. (signal 'ido-cr+-fallback
  148. (format
  149. "there are more than %i items in COLLECTION (see `ido-cr+-max-items')"
  150. ido-cr+-max-items)))
  151. ;; ido doesn't natively handle DEF being a list. If DEF is a
  152. ;; list, prepend it to COLLECTION and set DEF to just the
  153. ;; car of the default list.
  154. (when (and def (listp def))
  155. (setq collection
  156. (append def
  157. (nreverse (cl-set-difference collection def)))
  158. def (car def)))
  159. ;; Work around a bug in ido when both INITIAL-INPUT and
  160. ;; DEF are provided.
  161. (let ((initial
  162. (or (if (consp initial-input)
  163. (car initial-input)
  164. initial-input)
  165. "")))
  166. (when (and def initial
  167. (stringp initial)
  168. (not (string= initial "")))
  169. ;; Both default and initial input were provided. So keep
  170. ;; the initial input and preprocess the collection list
  171. ;; to put the default at the head, then proceed with
  172. ;; default = nil.
  173. (setq collection (cons def (remove def collection))
  174. def nil)))
  175. ;; Ready to do actual ido completion
  176. (prog1
  177. (let ((ido-cr+-enable-next-call t))
  178. (ido-completing-read
  179. prompt collection
  180. predicate require-match initial-input hist def
  181. inherit-input-method))
  182. ;; This detects when the user triggered fallback mode
  183. ;; manually.
  184. (when (eq ido-exit 'fallback)
  185. (signal 'ido-cr+-fallback "user manually triggered fallback"))))
  186. ;; Handler for ido-cr+-fallback signal
  187. (ido-cr+-fallback
  188. (ido-cr+--explain-fallback sig)
  189. (apply ido-cr+-fallback-function orig-args)))))
  190. ;;;###autoload
  191. (defadvice ido-completing-read (around ido-cr+ activate)
  192. "This advice handles application of ido-completing-read+ features.
  193. First, it ensures that `ido-cr+-enable-this-call' is set
  194. properly. This variable should be non-nil during execution of
  195. `ido-completing-read' if it was called from
  196. `ido-completing-read+'.
  197. Second, if `ido-cr+-replace-completely' is non-nil, then this
  198. advice completely replaces `ido-completing-read' with
  199. `ido-completing-read+'."
  200. ;; If this advice is autoloaded, then we need to force loading of
  201. ;; the rest of the file so all the variables will be defined.
  202. (when (not (featurep 'ido-completing-read+))
  203. (require 'ido-completing-read+))
  204. (let ((ido-cr+-enable-this-call ido-cr+-enable-next-call)
  205. (ido-cr+-enable-next-call nil))
  206. (if (or
  207. ido-cr+-enable-this-call ; Avoid recursion
  208. (not ido-cr+-replace-completely))
  209. ad-do-it
  210. (message "Replacing ido-completing-read")
  211. (setq ad-return-value (apply #'ido-completing-read+ (ad-get-args 0))))))
  212. ;; Fallback on magic C-f and C-b
  213. ;; Need to defvar this to avoid bytecomp warnings. This makes sense
  214. ;; since we are relying on ido dynamically let-binding it.
  215. (defvar ido-context-switch-command)
  216. (defadvice ido-magic-forward-char (before ido-cr+-fallback activate)
  217. "Allow falling back in ido-completing-read+."
  218. (when ido-cr+-enable-this-call
  219. ;; `ido-context-switch-command' is already let-bound at this
  220. ;; point.
  221. (setq ido-context-switch-command #'ido-fallback-command)))
  222. (defadvice ido-magic-backward-char (before ido-cr+-fallback activate)
  223. "Allow falling back in ido-completing-read+."
  224. (when ido-cr+-enable-this-call
  225. ;; `ido-context-switch-command' is already let-bound at this
  226. ;; point.
  227. (setq ido-context-switch-command #'ido-fallback-command)))
  228. ;;; Debug mode
  229. ;; This is defined at the end so it goes at the bottom of the
  230. ;; customization group
  231. (define-minor-mode ido-cr+-debug-mode
  232. "If non-nil, ido-cr+ will print debug info.
  233. Debug info is printed to the *Messages* buffer."
  234. nil
  235. :global t
  236. :group 'ido-cr+)
  237. (provide 'ido-completing-read+)
  238. ;;; ido-completing-read+.el ends here