ido-completing-read+.el 18 KB

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