ido-completing-read+.el 14 KB

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