ido-completing-read+.el 15 KB

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