ido-completing-read+.el 10 KB

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