ido-completing-read+.el 11 KB

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