ido-completing-read+.el 12 KB

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