ido-completing-read+.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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.0
  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. (define-error 'ido-cr+-fallback "ido-cr+-fallback")
  109. ;;;###autoload
  110. (defun ido-completing-read+ (prompt collection &optional predicate
  111. require-match initial-input
  112. hist def inherit-input-method)
  113. "ido-based method for reading from the minibuffer with completion.
  114. See `completing-read' for the meaning of the arguments.
  115. This function is a wrapper for `ido-completing-read' designed to
  116. be used as the value of `completing-read-function'. Importantly,
  117. it detects edge cases that ido cannot handle and uses normal
  118. completion for them."
  119. (let (;; Save the original arguments in case we need to do the
  120. ;; fallback
  121. (orig-args
  122. (list prompt collection predicate require-match
  123. initial-input hist def inherit-input-method)))
  124. (condition-case sig
  125. (progn
  126. (cond
  127. (inherit-input-method
  128. (signal 'ido-cr+-fallback
  129. "ido cannot handle non-nil INHERIT-INPUT-METHOD"))
  130. ((bound-and-true-p completion-extra-properties)
  131. (signal 'ido-cr+-fallback
  132. "ido cannot handle non-nil `completion-extra-properties'"))
  133. ((functionp collection)
  134. (signal 'ido-cr+-fallback
  135. "ido cannot handle COLLECTION being a function")))
  136. ;; Expand all possible completions
  137. (setq collection (all-completions "" collection predicate))
  138. ;; Check for excessively large collection
  139. (when (and ido-cr+-max-items
  140. (> (length collection) ido-cr+-max-items))
  141. (signal 'ido-cr+-fallback
  142. (format
  143. "there are more than %i items in COLLECTION (see `ido-cr+-max-items')"
  144. ido-cr+-max-items)))
  145. ;; ido doesn't natively handle DEF being a list. If DEF is
  146. ;; a list, prepend it to CHOICES and set DEF to just the
  147. ;; car of the default list.
  148. (when (and def (listp def))
  149. (setq choices
  150. (append def
  151. (nreverse (cl-set-difference choices def)))
  152. def (car def)))
  153. ;; Work around a bug in ido when both INITIAL-INPUT and
  154. ;; DEF are provided.
  155. (let ((initial
  156. (or (if (consp initial-input)
  157. (car initial-input)
  158. initial-input)
  159. "")))
  160. (when (and def initial
  161. (stringp initial)
  162. (not (string= initial "")))
  163. ;; Both default and initial input were provided. So
  164. ;; keep the initial input and preprocess the choices
  165. ;; list to put the default at the head, then proceed
  166. ;; with default = nil.
  167. (setq choices (cons def (remove def choices))
  168. def nil)))
  169. ;; Ready to do actual ido completion
  170. (prog1
  171. (let ((ido-cr+-enable-next-call t))
  172. (ido-completing-read
  173. prompt collection
  174. predicate require-match initial-input hist def
  175. inherit-input-method))
  176. ;; This detects when the user triggered fallback mode
  177. ;; manually.
  178. (when (eq ido-exit 'fallback)
  179. (signal 'ido-cr+-fallback "user manually triggered fallback"))))
  180. ;; Handler for ido-cr+-fallback signal
  181. (ido-cr+-fallback
  182. (ido-cr+--explain-fallback sig)
  183. (apply ido-cr+-fallback-function orig-args)))))
  184. ;;;###autoload
  185. (defadvice ido-completing-read (around ido-cr+ activate)
  186. "This advice handles application of ido-completing-read+ features.
  187. First, it ensures that `ido-cr+-enable-this-call' is set
  188. properly. This variable should be non-nil during execution of
  189. `ido-completing-read' if it was called from
  190. `ido-completing-read+'.
  191. Second, if `ido-cr+-replace-completely' is non-nil, then this
  192. advice completely replaces `ido-completing-read' with
  193. `ido-completing-read+'."
  194. ;; If this advice is autoloaded, then we need to force loading of
  195. ;; the rest of the file so all the variables will be defined.
  196. (when (not (featurep 'ido-completing-read+))
  197. (require 'ido-completing-read+))
  198. (let ((ido-cr+-enable-this-call ido-cr+-enable-next-call)
  199. (ido-cr+-enable-next-call nil))
  200. (if (or
  201. ido-cr+-enable-this-call ; Avoid recursion
  202. (not ido-cr+-replace-completely))
  203. ad-do-it
  204. (message "Replacing ido-completing-read")
  205. (setq ad-return-value (apply #'ido-completing-read+ (ad-get-args 0))))))
  206. ;; Fallback on magic C-f and C-b
  207. (defadvice ido-magic-forward-char (before ido-cr+-fallback activate)
  208. "Allow falling back in ido-completing-read+."
  209. (when ido-cr+-enable-this-call
  210. ;; `ido-context-switch-command' is already let-bound at this
  211. ;; point.
  212. (setq ido-context-switch-command #'ido-fallback-command)))
  213. (defadvice ido-magic-backward-char (before ido-cr+-fallback activate)
  214. "Allow falling back in ido-completing-read+."
  215. (when ido-cr+-enable-this-call
  216. ;; `ido-context-switch-command' is already let-bound at this
  217. ;; point.
  218. (setq ido-context-switch-command #'ido-fallback-command)))
  219. ;;; Debug mode
  220. ;; This is defined at the end so it goes at the bottom of the
  221. ;; customization group
  222. (define-minor-mode ido-cr+-debug-mode
  223. "If non-nil, ido-cr+ will print debug info.
  224. Debug info is printed to the *Messages* buffer."
  225. nil
  226. :global t
  227. :group 'ido-cr+)
  228. (provide 'ido-completing-read+)
  229. ;;; ido-completing-read+.el ends here