ido-completing-read+.el 14 KB

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