ido-completing-read+.el 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. ;;; ido-completing-read+.el --- A completing-read-function using ido -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2011-2017 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: 4.0
  7. ;; Package-Requires: ((emacs "24.4") (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. ;; If you use the excellent `ido-mode' for efficient completion of
  15. ;; file names and buffers, you might wonder if you can get ido-style
  16. ;; completion everywhere else too. Well, that's what this package
  17. ;; does! ido-ubiquitous is here to enable ido-style completion for
  18. ;; (almost) every function that uses the standard completion function
  19. ;; `completing-read'.
  20. ;; This package implements the `ido-completing-read+' function, which
  21. ;; is a wrapper for `ido-completing-read'. Importantly, it detects
  22. ;; edge cases that ordinary ido cannot handle and either adjusts them
  23. ;; so ido *can* handle them, or else simply falls back to Emacs'
  24. ;; standard completion instead. Hence, you can safely set
  25. ;; `completing-read-function' to `ido-completing-read+' without
  26. ;; worrying about breaking completion features that are incompatible
  27. ;; with ido.
  28. ;; To use this package, call `ido-ubiquitous-mode' to enable the mode,
  29. ;; or use `M-x customize-variable ido-ubiquitous-mode' it to enable it
  30. ;; permanently. Once the mode is enabled, most functions that use
  31. ;; `completing-read' will now have ido completion. If you decide in
  32. ;; the middle of a command that you would rather not use ido, just use
  33. ;; C-f or C-b at the end/beginning of the input to fall back to
  34. ;; non-ido completion (this is the same shortcut as when using ido for
  35. ;; buffers or files).
  36. ;; Note that `completing-read-default' is a very general function with
  37. ;; many complex behaviors that ido cannot emulate. This package
  38. ;; attempts to detect some of these cases and avoid using ido when it
  39. ;; sees them. So some functions will not have ido completion even when
  40. ;; this mode is enabled. Some other functions have ido disabled in
  41. ;; them because their packages already provide support for ido via
  42. ;; other means (for example, magit). See `M-x describe-variable
  43. ;; ido-cr+-function-blacklist' for more information.
  44. ;; ido-completing-read+ version 4.0 is a major update. The formerly
  45. ;; separate package ido-ubiquitous has been subsumed into
  46. ;; ido-completing-read+, so ido-ubiquitous 4.0 is just a wrapper that
  47. ;; loads ido-completing-read+ and displays a warning about being
  48. ;; obsolete. If you have previously customized ido-ubiquitous, be sure
  49. ;; to check out `M-x customize-group ido-completing-read-plus' after
  50. ;; updating to 4.0 and make sure the new settings are to your liking.
  51. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  52. ;;
  53. ;; This program is free software: you can redistribute it and/or modify
  54. ;; it under the terms of the GNU General Public License as published by
  55. ;; the Free Software Foundation, either version 3 of the License, or (at
  56. ;; your option) any later version.
  57. ;;
  58. ;; This program is distributed in the hope that it will be useful, but
  59. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  60. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  61. ;; General Public License for more details.
  62. ;;
  63. ;; You should have received a copy of the GNU General Public License
  64. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  65. ;;
  66. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  67. ;;
  68. ;;; Code:
  69. (defconst ido-completing-read+-version "4.0"
  70. "Currently running version of ido-completing-read+.
  71. Note that when you update ido-completing-read+, this variable may
  72. not be updated until you restart Emacs.")
  73. (require 'ido)
  74. (require 'cl-lib)
  75. (require 'cus-edit)
  76. ;;; Debug messages
  77. (define-minor-mode ido-cr+-debug-mode
  78. "If non-nil, ido-cr+ will print debug info.
  79. Debug info is printed to the *Messages* buffer."
  80. nil
  81. :global t
  82. :group 'ido-completing-read-plus)
  83. (defsubst ido-cr+--debug-message (format-string &rest args)
  84. (when ido-cr+-debug-mode
  85. (apply #'message (concat "ido-completing-read+: " format-string) args)))
  86. ;;; Ido variables
  87. ;; For unknown reasons, these variables need to be re-declared here to
  88. ;; silence byte-compiler warnings, despite already being declared in
  89. ;; ido.el.
  90. (defmacro define-ido-internal-var (symbol &optional initvalue docstring)
  91. "Declare and initialize an ido internal variable.
  92. This is used to suppress byte-compilation warnings about
  93. reference to free variables when ido-cr+ attempts to access
  94. internal ido variables with no initial value set. Such variables
  95. are originally declared like `(defvar VARNAME)'.
  96. This is a wrapper for `defvar' that supplies a default for the
  97. INITVALUE and DOCSTRING arguments."
  98. `(defvar ,symbol ,initvalue
  99. ,(or docstring
  100. "Internal ido variable.
  101. This variable was originally declared in `ido.el' without an
  102. initial value or docstring. The documentation you're reading
  103. comes from re-declaring it in `ido-completing-read+.el' in order
  104. to suppress some byte-compilation warnings. Setting another
  105. package's variable is not safe in general, but in this case it
  106. should be, because ido always let-binds this variable before
  107. using it, so the initial value shouldn't matter.")))
  108. (define-ido-internal-var ido-context-switch-command)
  109. (define-ido-internal-var ido-cur-list)
  110. (define-ido-internal-var ido-require-match)
  111. ;;;###autoload
  112. (defvar ido-cr+-minibuffer-depth -1
  113. "Minibuffer depth of the most recent ido-cr+ activation.
  114. If this equals the current minibuffer depth, then the minibuffer
  115. is currently being used by ido-cr+, and ido-cr+ feature will be
  116. active. Otherwise, something else is using the minibuffer and
  117. ido-cr+ features will be deactivated to avoid interfering with
  118. the other command.
  119. This is set to -1 by default, since `(minibuffer-depth)' should
  120. never return this value.")
  121. (defvar ido-cr+-assume-static-collection nil
  122. "If non-nil, ido-cr+ will assume that the collection is static.
  123. This is used to avoid unnecessary work in the case where the
  124. collection is a function, since a function collection could
  125. potentially change the set of completion candidates
  126. dynamically.")
  127. (defvar ido-cr+-current-command nil
  128. "Command most recently invoked by `call-interactively'.
  129. This is necessary because `command-execute' and
  130. `call-interactively' do not set `this-command'. Instead, the C
  131. code that calls `command-execute' sets it beforehand, so using
  132. either of those functions directly won't set `this-command'.")
  133. (defvar ido-cr+-dynamic-collection nil
  134. "Stores the collection argument if it is a function.
  135. This allows ido-cr+ to update the set of completion candidates
  136. dynamically.")
  137. (defvar ido-cr+-last-dynamic-update-text nil
  138. "Value of `ido-text' last time the dynamic collection was updated.
  139. This is used in `ido-cr+-update-dynamic-collection' to determine
  140. whether an update is necessary.")
  141. (defvar ido-cr+-dynamic-update-idle-time 0.25
  142. "Time to wait before updating dynamic completion list.")
  143. (defvar ido-cr+-dynamic-update-timer nil
  144. "Idle timer for updating dynamic completion list.")
  145. (defvar ido-cr+-exhibit-pending nil
  146. "This is non-nil after calling `ido-tidy' until the next call to `ido-exhibit'.
  147. Typically this is non-nil while any command is running and nil at all
  148. other times, since those two functions are in `pre-command-hook'
  149. and `post-command-hook' respectively. In particular, this will
  150. generally be nil while running an idle timer.")
  151. (defvar ido-cr+-no-default-action 'prepend-empty-string
  152. "Controls the behavior of ido-cr+ when DEF is nil and REQUIRE-MATCH is non-nil.
  153. Possible values:
  154. - `prepend-empty-string': The empty string will be added to the
  155. front of COLLECTION, making it the default. This is the
  156. standard behavior since it mimics the semantics of
  157. `completing-read-default'.
  158. - `append-empty-string': The empty string will be added to the
  159. end of COLLECTION, thus keeping the original default while
  160. making the empty string available as a completion.
  161. - `nil': No action will be taken.
  162. - Any other value: The value will be interpreted as a 1-argument
  163. function, which will receive the current collection as its
  164. argument and return the collection with any necessary
  165. modifications applied.
  166. This is not meant to be set permanently, but rather let-bound
  167. before calling `ido-completing-read+' under controlled
  168. circumstances.")
  169. (defvar ido-cr+-orig-completing-read-args nil
  170. "Original arguments passed to `ido-completing-read+'.
  171. These are used for falling back to `completing-read-default'.")
  172. (defgroup ido-completing-read-plus nil
  173. "Extra features and compatibility for `ido-completing-read'."
  174. :group 'ido)
  175. (defcustom ido-cr+-fallback-function
  176. ;; Initialize to the current value of `completing-read-function',
  177. ;; unless that is already set to the ido completer, in which case
  178. ;; use `completing-read-default'.
  179. (if (memq completing-read-function
  180. '(ido-completing-read+
  181. ido-completing-read
  182. ;; Current ido-ubiquitous function
  183. completing-read-ido-ubiquitous
  184. ;; Old ido-ubiquitous functions that shouldn't be used
  185. completing-read-ido
  186. ido-ubiquitous-completing-read))
  187. 'completing-read-default
  188. completing-read-function)
  189. "Alternate completing-read function to use when ido is not wanted.
  190. This will be used for functions that are incompatible with ido
  191. or if ido cannot handle the completion arguments. It will also be
  192. used when the user requests non-ido completion manually via C-f
  193. or C-b."
  194. :type '(choice (const :tag "Standard emacs completion"
  195. completing-read-default)
  196. (function :tag "Other function"))
  197. :group 'ido-completing-read-plus)
  198. (defcustom ido-cr+-max-items 30000
  199. "Max collection size to use ido-cr+ on.
  200. If `ido-completing-read+' is called on a collection larger than
  201. this, the fallback completion method will be used instead. To
  202. disable fallback based on collection size, set this to nil."
  203. :type '(choice (const :tag "No limit" nil)
  204. (integer
  205. :tag "Limit" :value 30000
  206. :validate
  207. (lambda (widget)
  208. (let ((v (widget-value widget)))
  209. (if (and (integerp v)
  210. (> v 0))
  211. nil
  212. (widget-put widget :error "This field should contain a positive integer")
  213. widget)))))
  214. :group 'ido-completing-read-plus)
  215. (defcustom ido-cr+-function-blacklist
  216. '(read-file-name-internal
  217. read-buffer
  218. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/60
  219. todo-add-category
  220. ;; Gnus already supports ido on its own
  221. gnus-emacs-completing-read
  222. gnus-iswitchb-completing-read
  223. grep-read-files
  224. ;; Magit already supports ido on its own
  225. magit-builtin-completing-read
  226. ;; ESS already supports ido on its own
  227. ess-completing-read
  228. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/39
  229. Info-read-node-name
  230. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/44
  231. tmm-prompt)
  232. "Functions & commands for which ido-cr+ should be disabled.
  233. Each entry can be either a symbol or a string. A symbol means to
  234. fall back specifically for the named function. A regular
  235. expression means to fall back for any function whose name matches
  236. that regular expression. When ido-cr+ is called through
  237. `completing-read', if any function in the call stack of the
  238. current command matches any of the blacklist entries, ido-cr+
  239. will be disabled for that command. Additionally, if the
  240. collection in the call to `completing-read' matches any of the
  241. blacklist entries, ido-cr+ will be disabled.
  242. Note that using specific function names is generally preferable
  243. to regular expressions, because the associated function
  244. definitions will be compared directly, so if the same function is
  245. called by another name, it should still trigger the fallback. For
  246. regular expressions, only name-based matching is possible."
  247. :group 'ido-completing-read-plus
  248. :type '(repeat (choice (symbol :tag "Function or command name")
  249. (string :tag "Regexp"))))
  250. (defcustom ido-cr+-function-whitelist
  251. nil
  252. "Functions & commands for which ido-cr+ should be enabled.
  253. If this variable is nil, the whitelist will not be used, and
  254. ido-cr+ will be allowed in all functions/commands not listed in
  255. `ido-cr+-function-backlist'.
  256. If this variable is non-nil, ido-cr+'s whitelisting mode will be
  257. enabled, and ido-cr+ will be disabled for *all* functions unless
  258. they match one of the entries. Matching is done in the same
  259. manner as `ido-cr+-function-blacklist', and blacklisting takes
  260. precedence over whitelisting."
  261. :group 'ido-completing-read-plus
  262. :type '(repeat (choice (symbol :tag "Function or command name")
  263. (string :tag "Regexp"))))
  264. ;;;###autoload
  265. (defcustom ido-cr+-replace-completely nil
  266. "If non-nil, replace `ido-completeing-read' completely with ido-cr+.
  267. Enabling this may interfere with or cause errors in other
  268. packages that use `ido-completing-read'. If you discover any such
  269. incompatibilities, please file a bug report at
  270. https://github.com/DarwinAwardWinner/ido-ubiquitous/issues"
  271. :type 'boolean)
  272. ;; Signal used to trigger fallback
  273. (define-error 'ido-cr+-fallback "ido-cr+-fallback")
  274. (defsubst ido-cr+--explain-fallback (arg)
  275. ;; This function accepts a string, or an ido-cr+-fallback
  276. ;; signal.
  277. (when ido-cr+-debug-mode
  278. (when (and (listp arg)
  279. (eq (car arg) 'ido-cr+-fallback))
  280. (setq arg (cadr arg)))
  281. (ido-cr+--debug-message "Falling back to `%s' because %s."
  282. ido-cr+-fallback-function arg)))
  283. ;;;###autoload
  284. (defsubst ido-cr+-active ()
  285. "Returns non-nil if ido-cr+ is currently using the minibuffer."
  286. (>= ido-cr+-minibuffer-depth (minibuffer-depth)))
  287. (defsubst ido-cr+-default-was-provided ()
  288. "Returns non-nil if ido-cr+ was passed a non-nil default argument."
  289. (and (nth 6 ido-cr+-orig-completing-read-args)))
  290. (defun ido-cr+--called-from-completing-read ()
  291. "Returns non-nil if the most recent call to ido-cr+ was from `completing-read'."
  292. (equal (cadr (backtrace-frame 1 'ido-completing-read+))
  293. 'completing-read))
  294. (defmacro ido-cr+-function-is-in-list (fun fun-list &optional list-name)
  295. "Return non-nil if FUN matches an entry in FUN-LIST.
  296. This is used to check for matches to `ido-cr+-function-blacklist'
  297. and `ido-cr+-function-whitelist'.
  298. This is declared as macro only in order to extract the variable
  299. name used for the second argument so it can be used in a debug
  300. message. It should be called as if it were a normal function."
  301. (when (null list-name)
  302. (if (symbolp fun-list)
  303. (setq list-name (symbol-name fun-list))
  304. (setq list-name "list")))
  305. `(cl-loop
  306. for entry in ,fun-list
  307. if (cond
  308. ;; Nil: Never matches anything
  309. ((null entry)
  310. nil)
  311. ;; Symbol: Compare names and function definitions
  312. ((symbolp entry)
  313. (or (eq entry ,fun)
  314. (let ((entry-def (ignore-errors (indirect-function entry)))
  315. (fun-def (ignore-errors (indirect-function ,fun))))
  316. (and
  317. fun-def entry-def
  318. (eq
  319. (indirect-function entry-def)
  320. (indirect-function fun-def))))))
  321. ;; String: Do regexp matching against function name if it is a
  322. ;; symbol
  323. ((stringp entry)
  324. (and (symbolp ,fun)
  325. (string-match-p entry (symbol-name ,fun))))
  326. ;; Anything else: invalid blacklist entry
  327. (t
  328. (ido-cr+--debug-message "Ignoring invalid entry in %s: `%S'" ,list-name entry)
  329. nil))
  330. return entry
  331. ;; If no blacklist entry matches, return nil
  332. finally return nil))
  333. (defun ido-cr+-function-is-blacklisted (fun)
  334. "Return non-nil if FUN is blacklisted.
  335. See `ido-cr+-function-blacklist'."
  336. (ido-cr+-function-is-in-list fun ido-cr+-function-blacklist))
  337. (defun ido-cr+-function-is-whitelisted (fun)
  338. "Return non-nil if FUN is whitelisted.
  339. See `ido-cr+-function-whitelist'."
  340. (or (null ido-cr+-function-whitelist)
  341. (ido-cr+-function-is-in-list fun ido-cr+-function-whitelist)))
  342. ;;;###autoload
  343. (defun ido-completing-read+ (prompt collection &optional predicate
  344. require-match initial-input
  345. hist def inherit-input-method)
  346. "ido-based method for reading from the minibuffer with completion.
  347. See `completing-read' for the meaning of the arguments.
  348. This function is a wrapper for `ido-completing-read' designed to
  349. be used as the value of `completing-read-function'. Importantly,
  350. it detects edge cases that ido cannot handle and uses normal
  351. completion for them."
  352. (let (;; Save the original arguments in case we need to do the
  353. ;; fallback
  354. (ido-cr+-orig-completing-read-args
  355. (list prompt collection predicate require-match
  356. initial-input hist def inherit-input-method))
  357. ;; Need to save this since activating the minibuffer once will
  358. ;; clear out any temporary minibuffer hooks, which need to get
  359. ;; restored before falling back.
  360. (orig-minibuffer-setup-hook minibuffer-setup-hook)
  361. ;; If collection is a function, save it for later, unless
  362. ;; instructed not to
  363. (ido-cr+-dynamic-collection
  364. (when (and (not ido-cr+-assume-static-collection)
  365. (functionp collection))
  366. collection))
  367. ;; If the whitelist is empty, everything is whitelisted
  368. (whitelisted (not ido-cr+-function-whitelist)))
  369. (condition-case sig
  370. (progn
  371. ;; Check a bunch of fallback conditions
  372. (when inherit-input-method
  373. (signal 'ido-cr+-fallback
  374. '("ido cannot handle non-nil INHERIT-INPUT-METHOD")))
  375. (when (bound-and-true-p completion-extra-properties)
  376. (signal 'ido-cr+-fallback
  377. '("ido cannot handle non-nil `completion-extra-properties'")))
  378. ;; Check for black/white-listed collection function
  379. (when (functionp collection)
  380. ;; Blacklist
  381. (when (ido-cr+-function-is-blacklisted collection)
  382. (if (symbolp collection)
  383. (signal 'ido-cr+-fallback
  384. (list (format "collection function `%S' is blacklisted" collection)))
  385. (signal 'ido-cr+-fallback
  386. (list "collection function is blacklisted"))))
  387. ;; Whitelist
  388. (when (and (not whitelisted)
  389. (ido-cr+-function-is-whitelisted collection))
  390. (ido-cr+--debug-message
  391. (if (symbolp collection)
  392. (format "Collection function `%S' is whitelisted" collection)
  393. "Collection function is whitelisted"))
  394. (setq whitelisted t)))
  395. ;; Expand all currently-known completions.
  396. (setq collection (all-completions "" collection predicate))
  397. ;; No point in using ido unless there's a collection
  398. (when (and (= (length collection) 0)
  399. (not ido-cr+-dynamic-collection))
  400. (signal 'ido-cr+-fallback '("ido is not needed for an empty collection")))
  401. ;; Check for excessively large collection
  402. (when (and ido-cr+-max-items
  403. (> (length collection) ido-cr+-max-items))
  404. (signal 'ido-cr+-fallback
  405. (list
  406. (format
  407. "there are more than %i items in COLLECTION (see `ido-cr+-max-items')"
  408. ido-cr+-max-items))))
  409. ;; If called from `completing-read', check for
  410. ;; black/white-listed commands/callers
  411. (when (ido-cr+--called-from-completing-read)
  412. ;; Check calling command
  413. (when (ido-cr+-function-is-blacklisted this-command)
  414. (signal 'ido-cr+-fallback
  415. (list "calling command `%S' is blacklisted" this-command)))
  416. (when (and (not whitelisted)
  417. (ido-cr+-function-is-whitelisted this-command))
  418. (ido-cr+--debug-message "Command `%S' is whitelisted" this-command)
  419. (setq whitelisted t))
  420. ;; Also need to check `ido-cr+-current-command'
  421. (when (ido-cr+-function-is-blacklisted ido-cr+-current-command)
  422. (signal 'ido-cr+-fallback
  423. (list "calling command `%S' is blacklisted" ido-cr+-current-command)))
  424. (when (and (not whitelisted)
  425. (ido-cr+-function-is-whitelisted ido-cr+-current-command))
  426. (ido-cr+--debug-message "Command `%S' is whitelisted" ido-cr+-current-command)
  427. (setq whitelisted t))
  428. ;; Check every function in the call stack starting after
  429. ;; `completing-read' until to the first
  430. ;; `funcall-interactively' (for a call from the function
  431. ;; body) or `call-interactively' (for a call from the
  432. ;; interactive form, in which the function hasn't actually
  433. ;; been called yet, so `funcall-interactively' won't be on
  434. ;; the stack.)
  435. (cl-loop for i upfrom 1
  436. for caller = (cadr (backtrace-frame i 'completing-read))
  437. while caller
  438. while (not (memq (indirect-function caller)
  439. '(internal--funcall-interactively
  440. (indirect-function 'call-interactively))))
  441. if (ido-cr+-function-is-blacklisted caller)
  442. do (signal 'ido-cr+-fallback
  443. (list (if (symbolp caller)
  444. (format "calling function `%S' is blacklisted" caller)
  445. "a calling function is blacklisted")))
  446. if (and (not whitelisted)
  447. (ido-cr+-function-is-whitelisted caller))
  448. do (progn
  449. (ido-cr+--debug-message
  450. (if (symbolp caller)
  451. (format "Calling function `%S' is whitelisted" caller)
  452. "A calling function is whitelisted"))
  453. (setq whitelisted t))))
  454. (unless whitelisted
  455. (signal 'ido-cr+-fallback
  456. (list "no functions or commands matched the whitelist for this call")))
  457. ;; In ido, the semantics of "default" are simply "put it at
  458. ;; the front of the list". Furthermore, ido has certain
  459. ;; issues with a non-nil DEF arg. Specifically, it can't
  460. ;; handle list defaults or providing both DEF and
  461. ;; INITIAL-INPUT. So, just pre-process the collection to put
  462. ;; the default(s) at the front and then set DEF to nil in
  463. ;; the call to ido to avoid these issues.
  464. (unless (listp def)
  465. ;; Ensure DEF is a list
  466. (setq def (list def)))
  467. (when def
  468. (setq collection (append def (cl-set-difference collection def
  469. :test #'equal))
  470. def nil))
  471. ;; If DEF was nil and REQUIRE-MATCH was non-nil, then we need to
  472. ;; add the empty string as the first option, because RET on
  473. ;; an empty input needs to return "". (Or possibly we need
  474. ;; to take some other action based on the value of
  475. ;; `ido-cr+-no-default-action'.)
  476. (when (and require-match
  477. ido-cr+-no-default-action
  478. (not (ido-cr+-default-was-provided)))
  479. (cl-case ido-cr+-no-default-action
  480. (nil
  481. ;; Take no action
  482. t)
  483. (prepend-empty-string
  484. (ido-cr+--debug-message "Adding \"\" as the default completion since no default was provided.")
  485. (setq collection (cons "" collection)))
  486. (append-empty-string
  487. (ido-cr+--debug-message "Adding \"\" as a completion option since no default was provided.")
  488. (setq collection (append collection '(""))))
  489. (otherwise
  490. (ido-cr+--debug-message "Running custom action function since no default was provided.")
  491. (setq collection (funcall ido-cr+-no-default-action collection)))))
  492. ;; Check for a specific bug
  493. (when (and ido-enable-dot-prefix
  494. (version< emacs-version "26.1")
  495. (member "" collection))
  496. (signal 'ido-cr+-fallback
  497. '("ido cannot handle the empty string as an option when `ido-enable-dot-prefix' is non-nil; see https://debbugs.gnu.org/cgi/bugreport.cgi?bug=26997")))
  498. ;; Finally ready to do actual ido completion
  499. (prog1
  500. (let ((ido-cr+-minibuffer-depth (1+ (minibuffer-depth)))
  501. ;; Initialize dynamic update vars
  502. (ido-cr+-last-dynamic-update-text "")
  503. (ido-cr+-dynamic-update-timer nil)
  504. (ido-cr+-exhibit-pending t)
  505. ;; Reset these for the next call to ido-cr+
  506. (ido-cr+-no-default-action 'prepend-empty-string)
  507. (ido-cr+-assume-static-collection nil))
  508. (unwind-protect
  509. (ido-completing-read
  510. prompt collection
  511. predicate require-match initial-input hist def
  512. inherit-input-method)
  513. (when ido-cr+-dynamic-update-timer
  514. (cancel-timer ido-cr+-dynamic-update-timer))))
  515. ;; This detects when the user triggered fallback mode
  516. ;; manually.
  517. (when (eq ido-exit 'fallback)
  518. (signal 'ido-cr+-fallback '("user manually triggered fallback")))))
  519. ;; Handler for ido-cr+-fallback signal
  520. (ido-cr+-fallback
  521. (let (;; Reset `minibuffer-setup-hook' to original value
  522. (minibuffer-setup-hook orig-minibuffer-setup-hook)
  523. ;; Reset these for the next call to ido-cr+
  524. (ido-cr+-no-default-action 'prepend-empty-string)
  525. (ido-cr+-assume-static-collection nil))
  526. (ido-cr+--explain-fallback sig)
  527. (apply ido-cr+-fallback-function ido-cr+-orig-completing-read-args))))))
  528. ;;;###autoload
  529. (defun ido-completing-read@ido-cr+-replace (orig-fun &rest args)
  530. "This advice allows ido-cr+ to completely replace `ido-completing-read'.
  531. See the varaible `ido-cr+-replace-completely' for more information."
  532. ;; If this advice is autoloaded, then we need to force loading of
  533. ;; the rest of the file so all the variables will be defined.
  534. (when (not (featurep 'ido-completing-read+))
  535. (require 'ido-completing-read+))
  536. (if (or (ido-cr+-active)
  537. (not ido-cr+-replace-completely))
  538. ;; ido-cr+ has either already activated or isn't going to
  539. ;; activate, so just run the function as normal
  540. (apply orig-fun args)
  541. ;; Otherwise, we need to activate ido-cr+.
  542. (apply #'ido-completing-read+ args)))
  543. ;;;###autoload
  544. (advice-add 'ido-completing-read :around
  545. #'ido-completing-read@ido-cr+-replace)
  546. ;;;###autoload
  547. (defun call-interactively@ido-cr+-record-current-command
  548. (orig-fun command &rest args)
  549. "Let-bind the command being interactively called.
  550. See `ido-cr+-current-command' for more information."
  551. (let ((ido-cr+-current-command command))
  552. (apply orig-fun command args)))
  553. ;;;###autoload
  554. (advice-add 'call-interactively :around
  555. #'call-interactively@ido-cr+-record-current-command)
  556. ;; Fallback on magic C-f and C-b
  557. (defun ido-magic-forward-char@ido-cr+-fallback (&rest args)
  558. "Allow falling back in ido-completing-read+."
  559. (when (ido-cr+-active)
  560. ;; `ido-context-switch-command' is already let-bound at this
  561. ;; point.
  562. (setq ido-context-switch-command #'ido-fallback-command)))
  563. (advice-add 'ido-magic-forward-char :before
  564. #'ido-magic-forward-char@ido-cr+-fallback)
  565. (defun ido-magic-backward-char@ido-cr+-fallback (&rest args)
  566. "Allow falling back in ido-completing-read+."
  567. (when (ido-cr+-active)
  568. ;; `ido-context-switch-command' is already let-bound at this
  569. ;; point.
  570. (setq ido-context-switch-command #'ido-fallback-command)))
  571. (advice-add 'ido-magic-backward-char :before
  572. #'ido-magic-backward-char@ido-cr+-fallback)
  573. (defun ido-select-text@ido-cr+-fix-require-match (orig-fun &rest args)
  574. "Fix ido behavior when `require-match' is non-nil.
  575. Standard ido will allow C-j to exit with an incomplete completion
  576. even when `require-match' is non-nil. Ordinary completion does
  577. not allow this. In ordinary completion, RET on an incomplete
  578. match is equivalent to TAB, and C-j selects the first match.
  579. Since RET in ido already selects the first match, this advice
  580. sets up C-j to be equivalent to TAB in the same situation.
  581. This advice only activates if the current ido completion was
  582. called through ido-cr+."
  583. (if (and
  584. ;; Only override C-j behavior if...
  585. ;; We're using ico-cr+
  586. (ido-cr+-active)
  587. ;; Require-match is non-nil
  588. ido-require-match
  589. ;; Current text is incomplete
  590. (not (member ido-text ido-cur-list)))
  591. (progn
  592. (ido-cr+--debug-message
  593. "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)")
  594. (ido-complete))
  595. (apply orig-fun args)))
  596. (advice-add 'ido-select-text :around
  597. #'ido-select-text@ido-cr+-fix-require-match)
  598. (defun ido-tidy@ido-cr+-set-exhibit-pending (&rest args)
  599. (setq ido-cr+-exhibit-pending t))
  600. (advice-add 'ido-tidy :after 'ido-tidy@ido-cr+-set-exhibit-pending)
  601. (defun ido-exhibit@ido-cr+-clear-exhibit-pending (&rest args)
  602. (setq ido-cr+-exhibit-pending nil))
  603. (advice-add 'ido-exhibit :before 'ido-exhibit@ido-cr+-clear-exhibit-pending)
  604. (defun ido-cr+-update-dynamic-collection ()
  605. "Update the set of completions for a dynamic collection.
  606. This has no effect unless `ido-cr+-dynamic-collection' is non-nil."
  607. (when (and (ido-cr+-active)
  608. ido-cr+-dynamic-collection)
  609. (unwind-protect
  610. (let ((ido-text (buffer-substring-no-properties (minibuffer-prompt-end) ido-eoinput)))
  611. (when (not (string= ido-text
  612. ido-cr+-last-dynamic-update-text))
  613. (setq
  614. ido-cur-list
  615. (cl-loop
  616. with predicate = (nth 2 ido-cr+-orig-completing-read-args)
  617. with def = (nth 6 ido-cr+-orig-completing-read-args)
  618. with new-cur-list = '()
  619. ;; Any strings in the common prefix have already been
  620. ;; checked for completions, so we can start after the
  621. ;; common prefix.
  622. with common-prefix = (s-shared-start ido-cr+-last-dynamic-update-text
  623. ido-text)
  624. ;; In addition to the currently-input text, we also
  625. ;; check the first match for completions, since each
  626. ;; match might be the prefix for a set of matches that
  627. ;; are dynamically revealed once the match is input. By
  628. ;; checking now, we can optimistically add these to the
  629. ;; completion list before the user has finished typing
  630. ;; the match. TODO: Maybe do a configurable number of
  631. ;; top matches rather than just the first?
  632. with first-match = (car ido-matches)
  633. initially do
  634. (when (and first-match
  635. (not (string= ido-text first-match)))
  636. (ido-cr+--debug-message "Getting completions for first match %S"
  637. first-match)
  638. (setq new-cur-list
  639. (all-completions
  640. first-match
  641. ido-cr+-dynamic-collection
  642. predicate)))
  643. for i from (1+ (length common-prefix)) upto (length ido-text)
  644. do (ido-cr+--debug-message "Getting completions for %S"
  645. (s-left i ido-text))
  646. append (all-completions
  647. (s-left i ido-text)
  648. ido-cr+-dynamic-collection
  649. predicate)
  650. into new-cur-list
  651. finally return (delete-dups (append ido-cur-list new-cur-list))))
  652. (let ((current-match (car ido-matches)))
  653. (when (and current-match (member current-match ido-cur-list))
  654. (setq ido-cur-list (ido-chop ido-cur-list current-match))))
  655. (ido-cr+--debug-message
  656. "Updated completion candidates for dynamic collection because `ido-text' changed from %S to %S. `ido-cur-list' now has %s elements"
  657. ido-cr+-last-dynamic-update-text ido-text
  658. (length ido-cur-list))
  659. (setq ido-cr+-last-dynamic-update-text ido-text)))
  660. ;; Compute new matches
  661. (setq ido-rescan t)
  662. (ido-set-matches)
  663. ;; Rebuild the completion display unless ido is already planning
  664. ;; to do it anyway
  665. (unless ido-cr+-exhibit-pending
  666. (ido-tidy)
  667. (ido-exhibit))))
  668. (when ido-cr+-dynamic-update-timer
  669. (cancel-timer ido-cr+-dynamic-update-timer)))
  670. (defun ido-cr+-schedule-dynamic-collection-update ()
  671. "Schedule a dynamic collection update for now or in the future."
  672. (when (and (ido-cr+-active)
  673. ido-cr+-dynamic-collection)
  674. ;; Cancel the previous timer
  675. (when ido-cr+-dynamic-update-timer
  676. (cancel-timer ido-cr+-dynamic-update-timer))
  677. (if (<= (length ido-matches) 1)
  678. ;; If we've narrowed it down to zero or one matches, update
  679. ;; immediately.
  680. (ido-cr+-update-dynamic-collection)
  681. ;; If there are still several choices, defer update until idle
  682. (setq ido-cr+-dynamic-update-timer
  683. (run-with-idle-timer ido-cr+-dynamic-update-idle-time nil
  684. #'ido-cr+-update-dynamic-collection)))))
  685. (defun ido-cr+-minibuffer-setup ()
  686. "set up minibuffer `post-command-hook' for ido-cr+ "
  687. (when (ido-cr+-active)
  688. (add-hook 'post-command-hook
  689. 'ido-cr+-schedule-dynamic-collection-update)))
  690. (add-hook 'ido-minibuffer-setup-hook
  691. 'ido-cr+-minibuffer-setup)
  692. ;; (defadvice ido-complete (around dynamic activate)
  693. ;; (let ((ido-confirm-unique-completion
  694. ;; (if ido-cr+-dynamic-collection
  695. ;; t
  696. ;; ido-confirm-unique-completion)))
  697. ;; ad-do-it))
  698. ;; Also need to update dynamic collections on TAB, and do so *before*
  699. ;; deciding to exit based on `ido-confirm-unique-completion'
  700. (defun ido-complete@ido-cr+-update-dynamic-collection (oldfun &rest args)
  701. "Maybe update the set of completions when pressing TAB."
  702. (when ido-cr+-dynamic-collection
  703. ;; First run with `ido-confirm-unique-completion' non-nil so it
  704. ;; can't exit
  705. (let ((ido-confirm-unique-completion t))
  706. (apply oldfun args))
  707. ;; Update `ido-eoinput'
  708. (setq ido-eoinput (point-max))
  709. ;; Now do update
  710. (ido-cr+-update-dynamic-collection))
  711. ;; After maybe updating the dynamic collection, if there's still
  712. ;; only one completion, now it's allowed to exit
  713. (apply oldfun args))
  714. (advice-add 'ido-complete :around 'ido-complete@ido-cr+-update-dynamic-collection)
  715. ;; Interoperation with minibuffer-electric-default-mode: only show the
  716. ;; default when the input is empty and the empty string is the
  717. ;; selected choice
  718. (defun minibuf-eldef-update-minibuffer@ido-cr+-compat (orig-fun &rest args)
  719. "This advice allows minibuffer-electric-default-mode to work with ido-cr+."
  720. (if (ido-cr+-active)
  721. (unless (eq minibuf-eldef-showing-default-in-prompt
  722. (and (string= (car ido-cur-list) "")
  723. (string= ido-text "")))
  724. ;; Swap state.
  725. (setq minibuf-eldef-showing-default-in-prompt
  726. (not minibuf-eldef-showing-default-in-prompt))
  727. (overlay-put minibuf-eldef-overlay 'invisible
  728. (not minibuf-eldef-showing-default-in-prompt)))
  729. (apply orig-fun args)))
  730. (advice-add 'minibuf-eldef-update-minibuffer :around
  731. #'minibuf-eldef-update-minibuffer@ido-cr+-compat)
  732. ;;;###autoload
  733. (define-minor-mode ido-ubiquitous-mode
  734. "Use ido completion instead of standard completion almost everywhere.
  735. If this mode causes problems for a function, you can customize
  736. when ido completion is or is not used by customizing
  737. `ido-cr+-function-blacklist'."
  738. nil
  739. :global t
  740. :group 'ido-completing-read-plus
  741. ;; Actually enable/disable the mode by setting
  742. ;; `completing-read-function'.
  743. (setq completing-read-function
  744. (if ido-ubiquitous-mode
  745. #'ido-completing-read+
  746. ido-cr+-fallback-function)))
  747. (defcustom ido-cr+-auto-update-blacklist 'notify
  748. "Whether to add new overrides when updating ido-cr+.
  749. This variable has 3 possible values, with the following meanings:
  750. `t': Auto-update the blacklist
  751. `notify': Notify you about updates but do not apply them
  752. `nil': Ignore all blacklist updates
  753. Ido-cr+ comes with a default blacklist for commands that are
  754. known to be incompatible with ido completion. New versions of
  755. ido-cr+ may come with updates to this blacklist as more
  756. incompatible commands are discovered. However, customizing your
  757. own overrides would normally prevent you from receiving these
  758. updates, since Emacs will not overwrite your customizations.
  759. To resolve this problem, you can set this variable to `t', and
  760. then ido-cr+ can automatically add any new built-in overrides
  761. whenever it is updated. (Actually, the update will happen the
  762. next time Emacs is restarted after the update.) This allows you
  763. to add your own overrides but still receive updates to the
  764. default set.
  765. If you want ido-cr+ to just notify you about new default
  766. overrides instead of adding them itself, set this variable to
  767. `notify'. If you don't want this auto-update behavior at all, set
  768. it to `nil'.
  769. (Note that having this option enabled effectively prevents you
  770. from removing any of the built-in default blacklist entries,
  771. since they will simply be re-added the next time Emacs starts.)"
  772. :type '(choice :tag "When new overrides are available:"
  773. (const :menu-tag "Auto-add"
  774. :tag "Add them automatically"
  775. t)
  776. (const :menu-tag "Notify"
  777. :tag "Notify me about them"
  778. notify)
  779. (const :menu-tag "Ignore"
  780. :tag "Ignore them"
  781. nil))
  782. :group 'ido-completing-read-plus)
  783. (defun ido-cr+-update-blacklist (&optional save quiet)
  784. "Re-add any missing default blacklist entries.
  785. This is useful after an update of ido-ubiquitous that adds new
  786. default overrides. See `ido-cr+-auto-update-blacklist' for more
  787. information.
  788. If SAVE is non-nil, also save the new blacklist to the user's
  789. Custom file (but only if it was already customized beforehand).
  790. When called interactively, a prefix argument triggers a save.
  791. When called from Lisp code, this function returns non-nil if the
  792. blacklist was modified."
  793. (interactive "P")
  794. (let* ((var-state (custom-variable-state 'ido-cr+-function-blacklist
  795. ido-cr+-function-blacklist))
  796. (curval ido-cr+-function-blacklist)
  797. (defval (eval (car (get 'ido-cr+-function-blacklist 'standard-value))))
  798. (newval (delete-dups (append defval curval)))
  799. (new-entries (cl-set-difference defval curval :test #'equal))
  800. (modified nil)
  801. (saved nil)
  802. (message-lines ()))
  803. (cl-case var-state
  804. (standard
  805. ;; Var is not customized, just set the new default
  806. (ido-cr+--debug-message "Blacklist was not customized, so it has been updated to the new default value.")
  807. (setq ido-cr+-function-blacklist defval
  808. modified new-entries))
  809. ((saved set changed)
  810. ;; Var has been customized and saved by the user, so set the
  811. ;; new value and maybe save it
  812. (ido-cr+--debug-message "Updating user-customized blacklist with new default entries.")
  813. (setq ido-cr+-function-blacklist newval
  814. modified t)
  815. (when (and save (eq var-state 'saved))
  816. (ido-cr+--debug-message "Saving new blacklist value to Custom file.")
  817. (customize-save-variable 'ido-cr+-function-blacklist ido-cr+-function-blacklist)
  818. (setq saved t)))
  819. (otherwise
  820. (ido-cr+--debug-message "Customization status of blacklist is unknown. Not modifying it.")))
  821. (if (and modified (not quiet))
  822. (progn
  823. (push (format "Added the following entries to `ido-cr+-function-blacklist': %S" new-entries)
  824. message-lines)
  825. (if saved
  826. (push "Saved the new value of `ido-cr+-function-blacklist' to your Custom file."
  827. message-lines)
  828. (push "However, the new value of `ido-cr+-function-blacklist' has not yet been saved for future sessions. To save it. re-run this command with a prefix argument: `C-u M-x ido-cr+-update-blacklist'; or else manually inspect and save the value using `M-x customize-variable ido-cr+-function-blacklist'."
  829. message-lines)))
  830. (push "No updates were required to `ido-cr+-function-blacklist'." message-lines))
  831. (unless quiet
  832. (message (mapconcat #'identity (nreverse message-lines) "\n")))
  833. modified))
  834. (defun ido-cr+-maybe-update-blacklist ()
  835. "Maybe call `ico-cr+-update-blacklist.
  836. See `ido-cr+-auto-update-blacklist' for more information."
  837. (if ido-cr+-auto-update-blacklist
  838. (let* ((curval ido-cr+-function-blacklist)
  839. (defval (eval (car (get 'ido-cr+-function-blacklist 'standard-value))))
  840. (new-entries (cl-set-difference curval defval :test #'equal)))
  841. (if new-entries
  842. (if (eq ido-cr+-auto-update-blacklist 'notify)
  843. (display-warning 'ido-completing-read+ "There are %s new blacklist entries available. Use `M-x ido-cr+-update-blacklist' to install them. (See `ido-cr+-auto-update-blacklist' for more information.)")
  844. (ido-cr+--debug-message "Initiating blacklist update.")
  845. (ido-cr+-update-blacklist t))
  846. (ido-cr+--debug-message "No blacklist updates available.")))
  847. (ido-cr+--debug-message "Skipping blacklist update by user request.")))
  848. (ido-cr+-maybe-update-blacklist)
  849. (provide 'ido-completing-read+)
  850. ;;; ido-completing-read+.el ends here