ido-completing-read+.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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.16
  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.16"
  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. (defun 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. ;;;###autoload
  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. ;;;###autoload
  58. (defvar ido-cr+-enable-this-call nil
  59. "If non-nil, then the current call to `ido-completing-read' is by `ido-completing-read+'")
  60. (defvar ido-cr+-orig-completing-read-args nil
  61. "Original arguments passed to `ido-completing-read+'.
  62. These are used for falling back to `completing-read-default'.")
  63. (defvar ido-cr+-before-fallback-hook nil
  64. "Hook run when ido-cr+ triggers a fallback.
  65. The hook is run right before calling `ido-cr+-fallback-function'.")
  66. (defgroup ido-completing-read-plus nil
  67. "Extra features and compatibility for `ido-completing-read'."
  68. :group 'ido)
  69. (defcustom ido-cr+-fallback-function
  70. ;; Initialize to the current value of `completing-read-function',
  71. ;; unless that is already set to the ido completer, in which case
  72. ;; use `completing-read-default'.
  73. (if (memq completing-read-function
  74. '(ido-completing-read+
  75. ido-completing-read
  76. ;; Current ido-ubiquitous function
  77. completing-read-ido-ubiquitous
  78. ;; Old ido-ubiquitous functions that shouldn't be used
  79. completing-read-ido
  80. ido-ubiquitous-completing-read))
  81. 'completing-read-default
  82. completing-read-function)
  83. "Alternate completing-read function to use when ido is not wanted.
  84. This will be used for functions that are incompatible with ido
  85. or if ido cannot handle the completion arguments. It will also be
  86. used when the user requests non-ido completion manually via C-f
  87. or C-b."
  88. :type '(choice (const :tag "Standard emacs completion"
  89. completing-read-default)
  90. (function :tag "Other function"))
  91. :group 'ido-completing-read-plus)
  92. (defcustom ido-cr+-max-items 30000
  93. "Max collection size to use ido-cr+ on.
  94. If `ido-completing-read+' is called on a collection larger than
  95. this, the fallback completion method will be used instead. To
  96. disable fallback based on collection size, set this to nil."
  97. :type '(choice (const :tag "No limit" nil)
  98. (integer
  99. :tag "Limit" :value 30000
  100. :validate
  101. (lambda (widget)
  102. (let ((v (widget-value widget)))
  103. (if (and (integerp v)
  104. (> v 0))
  105. nil
  106. (widget-put widget :error "This field should contain a positive integer")
  107. widget)))))
  108. :group 'ido-completing-read-plus)
  109. ;;;###autoload
  110. (defcustom ido-cr+-replace-completely nil
  111. "If non-nil, replace `ido-completeing-read' completely with ido-cr+.
  112. Enabling this may interfere with or cause errors in other
  113. packages that use `ido-completing-read'. If you discover any such
  114. incompatibilities, please file a bug report at
  115. https://github.com/DarwinAwardWinner/ido-ubiquitous/issues"
  116. :type 'boolean)
  117. ;; Signal used to trigger fallback
  118. (put 'ido-cr+-fallback 'error-conditions '(ido-cr+-fallback error))
  119. (put 'ido-cr+-fallback 'error-message "ido-cr+-fallback")
  120. (defun ido-cr+--explain-fallback (arg)
  121. ;; This function accepts a string, or an ido-cr+-fallback
  122. ;; signal.
  123. (when ido-cr+-debug-mode
  124. (when (and (listp arg)
  125. (eq (car arg) 'ido-cr+-fallback))
  126. (setq arg (cadr arg)))
  127. (ido-cr+--debug-message "Falling back to `%s' because %s."
  128. ido-cr+-fallback-function arg)))
  129. (defvar ido-cr+--predicate nil)
  130. ;;; ido-cr+--update-help-prefixes needs to know the completion
  131. ;;; predicate but ido discards that information
  132. (defun ido-cr+--update-help-prefixes ()
  133. (let ((ido-cr+--old-load-history load-history))
  134. (help--load-prefixes
  135. (radix-tree-prefixes
  136. (help-definition-prefixes)
  137. (buffer-substring-no-properties (minibuffer-prompt-end) (point-max))))
  138. ;; load new prefixes the same way as help--symbol-completion-table
  139. ;; but without doing any completion
  140. (unless (eq ido-cr+--old-load-history load-history)
  141. ;; hackish way to see if help--load-prefixes loaded any new files
  142. (with-no-warnings
  143. ;; ido dynamically binds ido-cur-list
  144. (setq ido-cur-list (all-completions "" obarray ido-cr+--predicate))))))
  145. (defun ido-cr+--help-symbol-completion-minibuffer-hook ()
  146. (remove-hook 'post-self-insert-hook
  147. #'ido-cr+--update-help-prefixes)
  148. ;; prefixes are updated with each self-insert as necessary
  149. (remove-hook 'ido-setup-hook
  150. #'ido-cr+--help-symbol-completion-ido-hook)
  151. (remove-hook 'minibuffer-exit-hook
  152. #'ido-cr+--help-symbol-completion-minibuffer-hook)
  153. (setq ido-cr+--predicate nil))
  154. (defun ido-cr+--help-symbol-completion-ido-hook ()
  155. (add-hook 'post-self-insert-hook
  156. #'ido-cr+--update-help-prefixes)
  157. (add-hook 'minibuffer-exit-hook
  158. #'ido-cr+--help-symbol-completion-minibuffer-hook))
  159. (defun ido-cr+--handle-help-symbol-completion (predicate)
  160. ;; ido-setup-hook does all the setup for the prefix completion and
  161. ;; minibuffer-exit-hook breaks it all down
  162. (setq ido-cr+--predicate predicate)
  163. (add-hook 'ido-setup-hook
  164. #'ido-cr+--help-symbol-completion-ido-hook))
  165. ;;;###autoload
  166. (defun ido-completing-read+ (prompt collection &optional predicate
  167. require-match initial-input
  168. hist def inherit-input-method)
  169. "ido-based method for reading from the minibuffer with completion.
  170. See `completing-read' for the meaning of the arguments.
  171. This function is a wrapper for `ido-completing-read' designed to
  172. be used as the value of `completing-read-function'. Importantly,
  173. it detects edge cases that ido cannot handle and uses normal
  174. completion for them."
  175. (let (;; Save the original arguments in case we need to do the
  176. ;; fallback
  177. (ido-cr+-orig-completing-read-args
  178. (list prompt collection predicate require-match
  179. initial-input hist def inherit-input-method)))
  180. (condition-case sig
  181. (progn
  182. (cond
  183. (inherit-input-method
  184. (signal 'ido-cr+-fallback
  185. '("ido cannot handle non-nil INHERIT-INPUT-METHOD")))
  186. ((bound-and-true-p completion-extra-properties)
  187. (signal 'ido-cr+-fallback
  188. '("ido cannot handle non-nil `completion-extra-properties'")))
  189. ((eq collection 'help--symbol-completion-table)
  190. ;; In newer Emacs help--symbol-completion-table loads libs
  191. ;; based on the prefix of the completion in
  192. ;; describe-variable and describe-function as the user
  193. ;; types. Detect and handle that case with ido.
  194. (ido-cr+--handle-help-symbol-completion predicate)
  195. (setq collection obarray))
  196. ((functionp collection)
  197. (signal 'ido-cr+-fallback
  198. '("ido cannot handle COLLECTION being a function"))))
  199. ;; Expand all possible completions
  200. (setq collection (all-completions "" collection predicate))
  201. ;; Check for excessively large collection
  202. (when (and ido-cr+-max-items
  203. (> (length collection) ido-cr+-max-items))
  204. (signal 'ido-cr+-fallback
  205. (list
  206. (format
  207. "there are more than %i items in COLLECTION (see `ido-cr+-max-items')"
  208. ido-cr+-max-items))))
  209. ;; ido doesn't natively handle DEF being a list. If DEF is a
  210. ;; list, prepend it to COLLECTION and set DEF to just the
  211. ;; car of the default list.
  212. (when (and def (listp def))
  213. (setq collection
  214. (append def
  215. (nreverse (cl-set-difference collection def)))
  216. def (car def)))
  217. ;; Work around a bug in ido when both INITIAL-INPUT and
  218. ;; DEF are provided.
  219. (let ((initial
  220. (or (if (consp initial-input)
  221. (car initial-input)
  222. initial-input)
  223. "")))
  224. (when (and def initial
  225. (stringp initial)
  226. (not (string= initial "")))
  227. ;; Both default and initial input were provided. So keep
  228. ;; the initial input and preprocess the collection list
  229. ;; to put the default at the head, then proceed with
  230. ;; default = nil.
  231. (setq collection (cons def (remove def collection))
  232. def nil)))
  233. ;; Ready to do actual ido completion
  234. (prog1
  235. (let ((ido-cr+-enable-next-call t))
  236. (ido-completing-read
  237. prompt collection
  238. predicate require-match initial-input hist def
  239. inherit-input-method))
  240. ;; This detects when the user triggered fallback mode
  241. ;; manually.
  242. (when (eq ido-exit 'fallback)
  243. (signal 'ido-cr+-fallback '("user manually triggered fallback")))))
  244. ;; Handler for ido-cr+-fallback signal
  245. (ido-cr+-fallback
  246. (ido-cr+--explain-fallback sig)
  247. (run-hooks 'ido-cr+-before-fallback-hook)
  248. (apply ido-cr+-fallback-function ido-cr+-orig-completing-read-args)))))
  249. ;;;###autoload
  250. (defadvice ido-completing-read (around ido-cr+ activate)
  251. "This advice handles application of ido-completing-read+ features.
  252. First, it ensures that `ido-cr+-enable-this-call' is set
  253. properly. This variable should be non-nil during execution of
  254. `ido-completing-read' if it was called from
  255. `ido-completing-read+'.
  256. Second, if `ido-cr+-replace-completely' is non-nil, then this
  257. advice completely replaces `ido-completing-read' with
  258. `ido-completing-read+'."
  259. ;; If this advice is autoloaded, then we need to force loading of
  260. ;; the rest of the file so all the variables will be defined.
  261. (when (not (featurep 'ido-completing-read+))
  262. (require 'ido-completing-read+))
  263. (let ((ido-cr+-enable-this-call ido-cr+-enable-next-call)
  264. (ido-cr+-enable-next-call nil))
  265. (if (or
  266. ido-cr+-enable-this-call ; Avoid recursion
  267. (not ido-cr+-replace-completely))
  268. ad-do-it
  269. (message "Replacing ido-completing-read")
  270. (setq ad-return-value (apply #'ido-completing-read+ (ad-get-args 0))))))
  271. ;; Fallback on magic C-f and C-b
  272. ;;;###autoload
  273. (defvar ido-context-switch-command nil
  274. "Variable holding the command used for switching to another completion mode.
  275. This variable is originally declared in `ido.el', but it is not
  276. given a value (or a docstring). This documentation comes from a
  277. re-declaration in `ido-completing-read+.el' that initializes it
  278. to nil, which should suppress some byte-compilation warnings in
  279. Emacs 25. Setting another package's variable is not safe in
  280. general, but in this case it should be, because ido always
  281. let-binds this variable before using it, so the initial value
  282. shouldn't matter.")
  283. (defadvice ido-magic-forward-char (before ido-cr+-fallback activate)
  284. "Allow falling back in ido-completing-read+."
  285. (when ido-cr+-enable-this-call
  286. ;; `ido-context-switch-command' is already let-bound at this
  287. ;; point.
  288. (setq ido-context-switch-command #'ido-fallback-command)))
  289. (defadvice ido-magic-backward-char (before ido-cr+-fallback activate)
  290. "Allow falling back in ido-completing-read+."
  291. (when ido-cr+-enable-this-call
  292. ;; `ido-context-switch-command' is already let-bound at this
  293. ;; point.
  294. (setq ido-context-switch-command #'ido-fallback-command)))
  295. ;;; Workaround for https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/93
  296. (defadvice ido-select-text (around fix-require-match-behavior activate)
  297. "Fix ido behavior when `require-match' is non-nil.
  298. Standard ido will allow C-j to exit with an incomplete completion
  299. even when `require-match' is non-nil. Ordinary completion does
  300. not allow this. In ordinary completion, RET on an incomplete
  301. match is equivalent to TAB, and C-j selects the first match.
  302. Since RET in ido already selects the first match, this advice
  303. sets up C-j to be equivalent to TAB in the same situation."
  304. (if (and
  305. ;; Only override C-j behavior if...
  306. ;; We're using ico-cr+
  307. ido-cr+-enable-this-call
  308. ;; Require-match is non-nil
  309. (with-no-warnings ido-require-match)
  310. ;; A default was provided, or ido-text is non-empty
  311. (or (with-no-warnings ido-default-item)
  312. (not (string= ido-text "")))
  313. ;; Only if current text is not a complete choice
  314. (not (member ido-text (with-no-warnings ido-cur-list))))
  315. (progn
  316. (ido-cr+--debug-message
  317. "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)")
  318. (ido-complete))
  319. ad-do-it))
  320. (provide 'ido-completing-read+)
  321. ;;; ido-completing-read+.el ends here