ido-completing-read+.el 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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: 4.0
  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 "4.0"
  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+-no-default-action 'prepend-empty-string
  69. "Controls the behavior of ido-cr+ when DEF is nil and REQUIRE-MATCH is non-nil.
  70. Possible values:
  71. - `prepend-empty-string': The empty string will be added to the
  72. front of COLLECTION, making it the default. This is the
  73. standard behavior since it mimics the semantics of
  74. `completing-read-default'.
  75. - `append-empty-string': The empty string will be added to the
  76. end of COLLECTION, thus keeping the original default while
  77. making the empty string available as a completion.
  78. - `nil': No action will be taken.
  79. - Any other value: The value will be interpreted as a 1-argument
  80. function, which will receive the current collection as its
  81. argument and return the collection with any necessary
  82. modifications applied.
  83. This is not meant to be set permanently, but rather let-bound
  84. before calling `ido-completing-read+' under controlled
  85. circumstances.")
  86. (defvar ido-cr+-orig-completing-read-args nil
  87. "Original arguments passed to `ido-completing-read+'.
  88. These are used for falling back to `completing-read-default'.")
  89. (defvar ido-cr+-before-fallback-hook nil
  90. "Hook run when ido-cr+ triggers a fallback.
  91. The hook is run right before calling `ido-cr+-fallback-function'.")
  92. (defgroup ido-completing-read-plus nil
  93. "Extra features and compatibility for `ido-completing-read'."
  94. :group 'ido)
  95. (defcustom ido-cr+-fallback-function
  96. ;; Initialize to the current value of `completing-read-function',
  97. ;; unless that is already set to the ido completer, in which case
  98. ;; use `completing-read-default'.
  99. (if (memq completing-read-function
  100. '(ido-completing-read+
  101. ido-completing-read
  102. ;; Current ido-ubiquitous function
  103. completing-read-ido-ubiquitous
  104. ;; Old ido-ubiquitous functions that shouldn't be used
  105. completing-read-ido
  106. ido-ubiquitous-completing-read))
  107. 'completing-read-default
  108. completing-read-function)
  109. "Alternate completing-read function to use when ido is not wanted.
  110. This will be used for functions that are incompatible with ido
  111. or if ido cannot handle the completion arguments. It will also be
  112. used when the user requests non-ido completion manually via C-f
  113. or C-b."
  114. :type '(choice (const :tag "Standard emacs completion"
  115. completing-read-default)
  116. (function :tag "Other function"))
  117. :group 'ido-completing-read-plus)
  118. (defcustom ido-cr+-max-items 30000
  119. "Max collection size to use ido-cr+ on.
  120. If `ido-completing-read+' is called on a collection larger than
  121. this, the fallback completion method will be used instead. To
  122. disable fallback based on collection size, set this to nil."
  123. :type '(choice (const :tag "No limit" nil)
  124. (integer
  125. :tag "Limit" :value 30000
  126. :validate
  127. (lambda (widget)
  128. (let ((v (widget-value widget)))
  129. (if (and (integerp v)
  130. (> v 0))
  131. nil
  132. (widget-put widget :error "This field should contain a positive integer")
  133. widget)))))
  134. :group 'ido-completing-read-plus)
  135. ;;;###autoload
  136. (defcustom ido-cr+-replace-completely nil
  137. "If non-nil, replace `ido-completeing-read' completely with ido-cr+.
  138. Enabling this may interfere with or cause errors in other
  139. packages that use `ido-completing-read'. If you discover any such
  140. incompatibilities, please file a bug report at
  141. https://github.com/DarwinAwardWinner/ido-ubiquitous/issues"
  142. :type 'boolean)
  143. ;; Signal used to trigger fallback
  144. (put 'ido-cr+-fallback 'error-conditions '(ido-cr+-fallback error))
  145. (put 'ido-cr+-fallback 'error-message "ido-cr+-fallback")
  146. (defsubst ido-cr+--explain-fallback (arg)
  147. ;; This function accepts a string, or an ido-cr+-fallback
  148. ;; signal.
  149. (when ido-cr+-debug-mode
  150. (when (and (listp arg)
  151. (eq (car arg) 'ido-cr+-fallback))
  152. (setq arg (cadr arg)))
  153. (ido-cr+--debug-message "Falling back to `%s' because %s."
  154. ido-cr+-fallback-function arg)))
  155. (defsubst ido-cr+-active ()
  156. "Returns non-nil if ido-cr+ is currently using the minibuffer."
  157. (>= ido-cr+-minibuffer-depth (minibuffer-depth)))
  158. (defsubst ido-cr+-default-was-provided ()
  159. "Returns non-nil if ido-cr+ was passed a non-nil default argument."
  160. (and (nth 6 ido-cr+-orig-completing-read-args)))
  161. ;;;###autoload
  162. (defun ido-completing-read+ (prompt collection &optional predicate
  163. require-match initial-input
  164. hist def inherit-input-method)
  165. "ido-based method for reading from the minibuffer with completion.
  166. See `completing-read' for the meaning of the arguments.
  167. This function is a wrapper for `ido-completing-read' designed to
  168. be used as the value of `completing-read-function'. Importantly,
  169. it detects edge cases that ido cannot handle and uses normal
  170. completion for them."
  171. (let (;; Save the original arguments in case we need to do the
  172. ;; fallback
  173. (ido-cr+-orig-completing-read-args
  174. (list prompt collection predicate require-match
  175. initial-input hist def inherit-input-method)))
  176. (condition-case sig
  177. (progn
  178. ;; Check a bunch of fallback conditions
  179. (cond
  180. (inherit-input-method
  181. (signal 'ido-cr+-fallback
  182. '("ido cannot handle non-nil INHERIT-INPUT-METHOD")))
  183. ((bound-and-true-p completion-extra-properties)
  184. (signal 'ido-cr+-fallback
  185. '("ido cannot handle non-nil `completion-extra-properties'")))
  186. ((and (functionp collection)
  187. (not ido-cr+-force-on-functional-collection))
  188. (signal 'ido-cr+-fallback
  189. '("ido cannot handle COLLECTION being a function (but see `ido-cr+-force-on-functional-collection')"))))
  190. ;; Expand all possible completions. (Apologies to everyone
  191. ;; who worked so hard to make lazy collections work; ido
  192. ;; doesn't know how to handle those.)
  193. (setq collection (all-completions "" collection predicate))
  194. ;; No point in using ido unless there's a collection
  195. (when (= (length collection) 0)
  196. (signal 'ido-cr+-fallback '("ido is not needed for an empty collection")))
  197. ;; Check for excessively large collection
  198. (when (and ido-cr+-max-items
  199. (> (length collection) ido-cr+-max-items))
  200. (signal 'ido-cr+-fallback
  201. (list
  202. (format
  203. "there are more than %i items in COLLECTION (see `ido-cr+-max-items')"
  204. ido-cr+-max-items))))
  205. ;; In ido, the semantics of "default" are simply "put it at
  206. ;; the front of the list". Furthermore, ido has certain
  207. ;; issues with a non-nil DEF arg. Specifically, it can't
  208. ;; handle list defaults or providing both DEF and
  209. ;; INITIAL-INPUT. So, just pre-process the collection to put
  210. ;; the default(s) at the front and then set DEF to nil in
  211. ;; the call to ido to avoid these issues.
  212. (unless (listp def)
  213. ;; Ensure DEF is a list
  214. (setq def (list def)))
  215. (when def
  216. (setq collection (append def (cl-set-difference collection def
  217. :test #'equal))
  218. def nil))
  219. ;; If DEF was nil and REQUIRE-MATCH was non-nil, then we need to
  220. ;; add the empty string as the first option, because RET on
  221. ;; an empty input needs to return "". (Or possibly we need
  222. ;; to take some other action based on the value of
  223. ;; `ido-cr+-no-default-action'.)
  224. (when (and require-match
  225. ido-cr+-no-default-action
  226. (not (ido-cr+-default-was-provided)))
  227. (cl-case ido-cr+-no-default-action
  228. (nil
  229. ;; Take no action
  230. t)
  231. (prepend-empty-string
  232. (ido-cr+--debug-message "Adding \"\" as the default completion since no default was provided.")
  233. (setq collection (cons "" collection)))
  234. (append-empty-string
  235. (ido-cr+--debug-message "Adding \"\" as a completion option since no default was provided.")
  236. (setq collection (append collection '(""))))
  237. (otherwise
  238. (ido-cr+--debug-message "Running custom action function since no default was provided.")
  239. (setq collection (funcall ido-cr+-no-default-action collection)))))
  240. ;; Check for a specific bug
  241. (when (and ido-enable-dot-prefix
  242. (version< emacs-version "26.1")
  243. (member "" collection))
  244. (signal 'ido-cr+-fallback
  245. '("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")))
  246. ;; Finally ready to do actual ido completion
  247. (prog1
  248. (let ((ido-cr+-minibuffer-depth (1+ (minibuffer-depth)))
  249. ;; Reset this for the next call to ido-cr+
  250. (ido-cr+-no-default-action 'prepend-empty-string))
  251. (ido-completing-read
  252. prompt collection
  253. predicate require-match initial-input hist def
  254. inherit-input-method))
  255. ;; This detects when the user triggered fallback mode
  256. ;; manually.
  257. (when (eq ido-exit 'fallback)
  258. (signal 'ido-cr+-fallback '("user manually triggered fallback")))))
  259. ;; Handler for ido-cr+-fallback signal
  260. (ido-cr+-fallback
  261. (let ( ;; Reset this for the next call to ido-cr+
  262. (ido-cr+-no-default-action 'prepend-empty-string))
  263. (ido-cr+--explain-fallback sig)
  264. (run-hooks 'ido-cr+-before-fallback-hook)
  265. (apply ido-cr+-fallback-function ido-cr+-orig-completing-read-args))))))
  266. ;;;###autoload
  267. (defadvice ido-completing-read (around ido-cr+ activate)
  268. "This advice is the implementation of `ido-cr+-replace-completely'."
  269. ;; If this advice is autoloaded, then we need to force loading of
  270. ;; the rest of the file so all the variables will be defined.
  271. (when (not (featurep 'ido-completing-read+))
  272. (require 'ido-completing-read+))
  273. (if (or (ido-cr+-active)
  274. (not ido-cr+-replace-completely))
  275. ;; ido-cr+ has either already activated or isn't going to
  276. ;; activate, so just run the function as normal
  277. ad-do-it
  278. ;; Otherwise, we need to activate ido-cr+.
  279. (setq ad-return-value (apply #'ido-completing-read+ (ad-get-args 0)))))
  280. ;; Fallback on magic C-f and C-b
  281. ;;;###autoload
  282. (defvar ido-context-switch-command nil
  283. "Variable holding the command used for switching to another completion mode.
  284. This variable is originally declared in `ido.el', but it is not
  285. given a value (or a docstring). This documentation comes from a
  286. re-declaration in `ido-completing-read+.el' that initializes it
  287. to nil, which should suppress some byte-compilation warnings in
  288. Emacs 25. Setting another package's variable is not safe in
  289. general, but in this case it should be, because ido always
  290. let-binds this variable before using it, so the initial value
  291. shouldn't matter.")
  292. (defadvice ido-magic-forward-char (before ido-cr+-fallback activate)
  293. "Allow falling back in ido-completing-read+."
  294. (when (ido-cr+-active)
  295. ;; `ido-context-switch-command' is already let-bound at this
  296. ;; point.
  297. (setq ido-context-switch-command #'ido-fallback-command)))
  298. (defadvice ido-magic-backward-char (before ido-cr+-fallback activate)
  299. "Allow falling back in ido-completing-read+."
  300. (when (ido-cr+-active)
  301. ;; `ido-context-switch-command' is already let-bound at this
  302. ;; point.
  303. (setq ido-context-switch-command #'ido-fallback-command)))
  304. ;;; Workaround for https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/93
  305. (defadvice ido-select-text (around fix-require-match-behavior activate)
  306. "Fix ido behavior when `require-match' is non-nil.
  307. Standard ido will allow C-j to exit with an incomplete completion
  308. even when `require-match' is non-nil. Ordinary completion does
  309. not allow this. In ordinary completion, RET on an incomplete
  310. match is equivalent to TAB, and C-j selects the first match.
  311. Since RET in ido already selects the first match, this advice
  312. sets up C-j to be equivalent to TAB in the same situation."
  313. (if (and
  314. ;; Only override C-j behavior if...
  315. ;; We're using ico-cr+
  316. (ido-cr+-active)
  317. ;; Require-match is non-nil
  318. (with-no-warnings ido-require-match)
  319. ;; Current text is not a complete choice
  320. (not (member ido-text (with-no-warnings ido-cur-list))))
  321. (progn
  322. (ido-cr+--debug-message
  323. "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)")
  324. (ido-complete))
  325. ad-do-it))
  326. ;; Interoperation with minibuffer-electric-default-mode: only show the
  327. ;; default when the input is empty and the empty string is the selected
  328. (defadvice minibuf-eldef-update-minibuffer (around ido-cr+-compat activate)
  329. (if (ido-active)
  330. (unless (eq minibuf-eldef-showing-default-in-prompt
  331. (and (string= (car ido-cur-list) "")
  332. (string= ido-text "")))
  333. ;; Swap state.
  334. (setq minibuf-eldef-showing-default-in-prompt
  335. (not minibuf-eldef-showing-default-in-prompt))
  336. (overlay-put minibuf-eldef-overlay 'invisible
  337. (not minibuf-eldef-showing-default-in-prompt)))
  338. ad-do-it))
  339. (provide 'ido-completing-read+)
  340. ;;; ido-completing-read+.el ends here