ido-completing-read+.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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.11
  7. ;; Package-Requires: ((emacs "24.1") (cl-lib "0.5"))
  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 implements 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.11"
  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-lib)
  43. ;;; Debug messages
  44. (define-minor-mode ido-cr+-debug-mode
  45. "If non-nil, ido-cr+ will print debug info.
  46. Debug info is printed to the *Messages* buffer."
  47. nil
  48. :global t
  49. :group 'ido-completing-read-plus)
  50. ;; Defined as a macro for efficiency (args are not evaluated unless
  51. ;; debug mode is on)
  52. (defmacro ido-cr+--debug-message (format-string &rest args)
  53. `(when ido-cr+-debug-mode
  54. (message (concat "ido-completing-read+: " ,format-string) ,@args)))
  55. ;;; Core code
  56. ;;;###autoload
  57. (defvar ido-cr+-enable-next-call nil
  58. "If non-nil, then the next call to `ido-completing-read' is by `ido-completing-read+'.")
  59. ;;;###autoload
  60. (defvar ido-cr+-enable-this-call nil
  61. "If non-nil, then the current call to `ido-completing-read' is by `ido-completing-read+'")
  62. (defgroup ido-completing-read-plus nil
  63. "Extra features and compatibility for `ido-completing-read'."
  64. :group 'ido)
  65. (defcustom ido-cr+-fallback-function
  66. ;; Initialize to the current value of `completing-read-function',
  67. ;; unless that is already set to the ido completer, in which case
  68. ;; use `completing-read-default'.
  69. (if (memq completing-read-function
  70. '(ido-completing-read+
  71. ido-completing-read
  72. ;; Current ido-ubiquitous function
  73. completing-read-ido-ubiquitous
  74. ;; Old ido-ubiquitous functions that shouldn't be used
  75. completing-read-ido
  76. ido-ubiquitous-completing-read))
  77. 'completing-read-default
  78. completing-read-function)
  79. "Alternate completing-read function to use when ido is not wanted.
  80. This will be used for functions that are incompatible with ido
  81. or if ido cannot handle the completion arguments. It will also be
  82. used when the user requests non-ido completion manually via C-f
  83. or C-b."
  84. :type '(choice (const :tag "Standard emacs completion"
  85. completing-read-default)
  86. (function :tag "Other function"))
  87. :group 'ido-completing-read-plus)
  88. (defcustom ido-cr+-max-items 30000
  89. "Max collection size to use ido-cr+ on.
  90. If `ido-completing-read+' is called on a collection larger than
  91. this, the fallback completion method will be used instead. To
  92. disable fallback based on collection size, set this to nil."
  93. :type '(choice (const :tag "No limit" nil)
  94. (integer
  95. :tag "Limit" :value 30000
  96. :validate
  97. (lambda (widget)
  98. (let ((v (widget-value widget)))
  99. (if (and (integerp v)
  100. (> v 0))
  101. nil
  102. (widget-put widget :error "This field should contain a positive integer")
  103. widget)))))
  104. :group 'ido-completing-read-plus)
  105. ;;;###autoload
  106. (defcustom ido-cr+-replace-completely nil
  107. "If non-nil, replace `ido-completeing-read' completely with ido-cr+.
  108. Enabling this may interfere with or cause errors in other
  109. packages that use `ido-completing-read'. If you discover any such
  110. incompatibilities, please file a bug report at
  111. https://github.com/DarwinAwardWinner/ido-ubiquitous/issues"
  112. :type 'boolean)
  113. ;; Signal used to trigger fallback
  114. (put 'ido-cr+-fallback 'error-conditions '(ido-cr+-fallback error))
  115. (put 'ido-cr+-fallback 'error-message "ido-cr+-fallback")
  116. (defun ido-cr+--explain-fallback (arg)
  117. ;; This function accepts a string, or an ido-cr+-fallback
  118. ;; signal.
  119. (when ido-cr+-debug-mode
  120. (when (and (listp arg)
  121. (eq (car arg) 'ido-cr+-fallback))
  122. (setq arg (cdr arg)))
  123. (ido-cr+--debug-message "Falling back to `%s' because %s."
  124. ido-cr+-fallback-function arg)))
  125. ;;;###autoload
  126. (defun ido-completing-read+ (prompt collection &optional predicate
  127. require-match initial-input
  128. hist def inherit-input-method)
  129. "ido-based method for reading from the minibuffer with completion.
  130. See `completing-read' for the meaning of the arguments.
  131. This function is a wrapper for `ido-completing-read' designed to
  132. be used as the value of `completing-read-function'. Importantly,
  133. it detects edge cases that ido cannot handle and uses normal
  134. completion for them."
  135. (let (;; Save the original arguments in case we need to do the
  136. ;; fallback
  137. (orig-args
  138. (list prompt collection predicate require-match
  139. initial-input hist def inherit-input-method)))
  140. (condition-case sig
  141. (progn
  142. (cond
  143. (inherit-input-method
  144. (signal 'ido-cr+-fallback
  145. "ido cannot handle non-nil INHERIT-INPUT-METHOD"))
  146. ((bound-and-true-p completion-extra-properties)
  147. (signal 'ido-cr+-fallback
  148. "ido cannot handle non-nil `completion-extra-properties'"))
  149. ((functionp collection)
  150. (signal 'ido-cr+-fallback
  151. "ido cannot handle COLLECTION being a function")))
  152. ;; Expand all possible completions
  153. (setq collection (all-completions "" collection predicate))
  154. ;; Check for excessively large collection
  155. (when (and ido-cr+-max-items
  156. (> (length collection) ido-cr+-max-items))
  157. (signal 'ido-cr+-fallback
  158. (format
  159. "there are more than %i items in COLLECTION (see `ido-cr+-max-items')"
  160. ido-cr+-max-items)))
  161. ;; ido doesn't natively handle DEF being a list. If DEF is a
  162. ;; list, prepend it to COLLECTION and set DEF to just the
  163. ;; car of the default list.
  164. (when (and def (listp def))
  165. (setq collection
  166. (append def
  167. (nreverse (cl-set-difference collection def)))
  168. def (car def)))
  169. ;; Work around a bug in ido when both INITIAL-INPUT and
  170. ;; DEF are provided.
  171. (let ((initial
  172. (or (if (consp initial-input)
  173. (car initial-input)
  174. initial-input)
  175. "")))
  176. (when (and def initial
  177. (stringp initial)
  178. (not (string= initial "")))
  179. ;; Both default and initial input were provided. So keep
  180. ;; the initial input and preprocess the collection list
  181. ;; to put the default at the head, then proceed with
  182. ;; default = nil.
  183. (setq collection (cons def (remove def collection))
  184. def nil)))
  185. ;; Ready to do actual ido completion
  186. (prog1
  187. (let ((ido-cr+-enable-next-call t))
  188. (ido-completing-read
  189. prompt collection
  190. predicate require-match initial-input hist def
  191. inherit-input-method))
  192. ;; This detects when the user triggered fallback mode
  193. ;; manually.
  194. (when (eq ido-exit 'fallback)
  195. (signal 'ido-cr+-fallback "user manually triggered fallback"))))
  196. ;; Handler for ido-cr+-fallback signal
  197. (ido-cr+-fallback
  198. (ido-cr+--explain-fallback sig)
  199. (apply ido-cr+-fallback-function orig-args)))))
  200. ;;;###autoload
  201. (defadvice ido-completing-read (around ido-cr+ activate)
  202. "This advice handles application of ido-completing-read+ features.
  203. First, it ensures that `ido-cr+-enable-this-call' is set
  204. properly. This variable should be non-nil during execution of
  205. `ido-completing-read' if it was called from
  206. `ido-completing-read+'.
  207. Second, if `ido-cr+-replace-completely' is non-nil, then this
  208. advice completely replaces `ido-completing-read' with
  209. `ido-completing-read+'."
  210. ;; If this advice is autoloaded, then we need to force loading of
  211. ;; the rest of the file so all the variables will be defined.
  212. (when (not (featurep 'ido-completing-read+))
  213. (require 'ido-completing-read+))
  214. (let ((ido-cr+-enable-this-call ido-cr+-enable-next-call)
  215. (ido-cr+-enable-next-call nil))
  216. (if (or
  217. ido-cr+-enable-this-call ; Avoid recursion
  218. (not ido-cr+-replace-completely))
  219. ad-do-it
  220. (message "Replacing ido-completing-read")
  221. (setq ad-return-value (apply #'ido-completing-read+ (ad-get-args 0))))))
  222. ;; Fallback on magic C-f and C-b
  223. ;;;###autoload
  224. (defvar ido-context-switch-command nil
  225. "Variable holding the command used for switching to another completion mode.
  226. This variable is originally declared in `ido.el', but it is not
  227. given a value (or a docstring). This documentation comes from a
  228. re-declaration in `ido-completing-read+.el' that initializes it
  229. to nil, which should suppress some byte-compilation warnings in
  230. Emacs 25. Setting another package's variable is not safe in
  231. general, but in this case it should be, because ido always
  232. let-binds this variable before using it, so the initial value
  233. shouldn't matter.")
  234. (defadvice ido-magic-forward-char (before ido-cr+-fallback activate)
  235. "Allow falling back in ido-completing-read+."
  236. (when ido-cr+-enable-this-call
  237. ;; `ido-context-switch-command' is already let-bound at this
  238. ;; point.
  239. (setq ido-context-switch-command #'ido-fallback-command)))
  240. (defadvice ido-magic-backward-char (before ido-cr+-fallback activate)
  241. "Allow falling back in ido-completing-read+."
  242. (when ido-cr+-enable-this-call
  243. ;; `ido-context-switch-command' is already let-bound at this
  244. ;; point.
  245. (setq ido-context-switch-command #'ido-fallback-command)))
  246. ;;; Workaround for https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/93
  247. (defadvice ido-select-text (around fix-require-match-behavior activate)
  248. "Fix ido behavior when `require-match' is non-nil.
  249. Standard ido will allow C-j to exit with an incomplete completion
  250. even when `require-match' is non-nil. Ordinary completion does
  251. not allow this. In ordinary completion, RET on an incomplete
  252. match is equivalent to TAB, and C-j selects the first match.
  253. Since RET in ido already selects the first match, this advice
  254. sets up C-j to be equivalent to TAB in the same situation."
  255. (if (and
  256. ;; Only override C-j behavior if...
  257. ;; We're using ico-cr+
  258. ido-cr+-enable-this-call
  259. ;; Require-match is non-nil
  260. (with-no-warnings ido-require-match)
  261. ;; A default was provided, or ido-text is non-empty
  262. (or (with-no-warnings ido-default-item)
  263. (not (string= ido-text "")))
  264. ;; Only if current text is not a complete choice
  265. (not (member ido-text (with-no-warnings ido-cur-list))))
  266. (progn
  267. (ido-cr+--debug-message
  268. "Overriding C-j behavior for require-match: performing completion instead of exiting.")
  269. (ido-complete))
  270. ad-do-it))
  271. (provide 'ido-completing-read+)
  272. ;;; ido-completing-read+.el ends here