ido-completing-read+.el 13 KB

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