ido-completing-read+.el 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  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.11
  7. ;; Package-Requires: ((emacs "24.4") (cl-lib "0.5") (s "0.1") (memoize "1.1") (named-timer "0"))
  8. ;; URL: https://github.com/DarwinAwardWinner/ido-completing-read-plus
  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.11"
  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 'minibuf-eldef)
  75. (require 'cl-lib)
  76. (require 'cus-edit)
  77. (require 's)
  78. (require 'named-timer)
  79. ;; Optional dependency, only needed for optimization
  80. (require 'memoize nil t)
  81. ;; Silence some byte-compiler warnings
  82. (eval-when-compile
  83. (require 'minibuf-eldef)
  84. (require 'flx-ido nil t))
  85. ;;; Debug messages
  86. (define-minor-mode ido-cr+-debug-mode
  87. "If non-nil, ido-cr+ will print debug info.
  88. Debug info is printed to the *Messages* buffer."
  89. nil
  90. :global t
  91. :group 'ido-completing-read-plus)
  92. (defsubst ido-cr+--debug-message (format-string &rest args)
  93. (when ido-cr+-debug-mode
  94. (apply #'message (concat "ido-completing-read+: " format-string) args)))
  95. ;;; Ido variables
  96. ;; For unknown reasons, these variables need to be re-declared here to
  97. ;; silence byte-compiler warnings, despite already being declared in
  98. ;; ido.el.
  99. (defmacro define-ido-internal-var (symbol &optional initvalue docstring)
  100. "Declare and initialize an ido internal variable.
  101. This is used to suppress byte-compilation warnings about
  102. reference to free variables when ido-cr+ attempts to access
  103. internal ido variables with no initial value set. Such variables
  104. are originally declared like `(defvar VARNAME)'.
  105. This is a wrapper for `defvar' that supplies a default for the
  106. INITVALUE and DOCSTRING arguments."
  107. `(defvar ,symbol ,initvalue
  108. ,(or docstring
  109. "Internal ido variable.
  110. This variable was originally declared in `ido.el' without an
  111. initial value or docstring. The documentation you're reading
  112. comes from re-declaring it in `ido-completing-read+.el' in order
  113. to suppress some byte-compilation warnings. Setting another
  114. package's variable is not safe in general, but in this case it
  115. should be, because ido always let-binds this variable before
  116. using it, so the initial value shouldn't matter.")))
  117. (define-ido-internal-var ido-context-switch-command)
  118. (define-ido-internal-var ido-cur-list)
  119. (define-ido-internal-var ido-cur-item)
  120. (define-ido-internal-var ido-require-match)
  121. (define-ido-internal-var ido-process-ignore-lists)
  122. ;; Vars and functions from flx-ido package
  123. (defvar flx-ido-mode)
  124. (declare-function flx-ido-reset "ext:flx-ido.el")
  125. ;;;###autoload
  126. (defvar ido-cr+-minibuffer-depth -1
  127. "Minibuffer depth of the most recent ido-cr+ activation.
  128. If this equals the current minibuffer depth, then the minibuffer
  129. is currently being used by ido-cr+, and ido-cr+ feature will be
  130. active. Otherwise, something else is using the minibuffer and
  131. ido-cr+ features will be deactivated to avoid interfering with
  132. the other command.
  133. This is set to -1 by default, since `(minibuffer-depth)' should
  134. never return this value.")
  135. (defvar ido-cr+-assume-static-collection nil
  136. "If non-nil, ido-cr+ will assume that the collection is static.
  137. This is used to avoid unnecessary work in the case where the
  138. collection is a function, since a function collection could
  139. potentially change the set of completion candidates
  140. dynamically.")
  141. (defvar ido-cr+-current-command nil
  142. "Command most recently invoked by `call-interactively'.
  143. This is necessary because `command-execute' and
  144. `call-interactively' do not set `this-command'. Instead, the C
  145. code that calls `command-execute' sets it beforehand, so using
  146. either of those functions directly won't set `this-command'.")
  147. (defvar ido-cr+-dynamic-collection nil
  148. "Stores the collection argument if it is a function.
  149. This allows ido-cr+ to update the set of completion candidates
  150. dynamically.")
  151. (defvar ido-cr+-last-dynamic-update-text nil
  152. "The value of `ido-text' last time a dynamic update occurred.")
  153. (defvar ido-cr+-dynamic-update-idle-time 0.25
  154. "Time to wait before updating dynamic completion list.")
  155. (defvar ido-cr+-exhibit-pending nil
  156. "This is non-nil after calling `ido-tidy' until the next call to `ido-exhibit'.
  157. Typically this is non-nil while any command is running and nil at all
  158. other times, since those two functions are in `pre-command-hook'
  159. and `post-command-hook' respectively. In particular, this will
  160. generally be nil while running an idle timer.")
  161. (make-obsolete-variable
  162. 'ido-cr+-no-default-action
  163. " This variable no longer has any effect. Customize `ido-cr+-nil-def-alternate-behavior-list' instead."
  164. "4.2")
  165. (defvar ido-cr+-orig-completing-read-args nil
  166. "Original arguments passed to `ido-completing-read+'.
  167. These are used for falling back to `completing-read-default'.")
  168. (defvar ido-cr+-all-completions-memoized 'all-completions
  169. "Memoized version of `all-completions'.
  170. During completion with dynamic collection, this variable is set
  171. to a memoized copy of `all-completions'.")
  172. (defvar ido-cr+-all-prefix-completions-memoized 'ido-cr+-all-prefix-completions
  173. "Memoized version of `ido-cr+-all-prefix-completions'.
  174. During completion with dynamic collection, this variable is set
  175. to a memoized copy of `ido-cr+-all-prefix-completions'.")
  176. (defvar ido-cr+-active-restrictions nil
  177. "List of restrictions in place from `ido-restrict-to-matches'.
  178. Each element is a cons cell of (REMOVEP . TEXT), where REMOVEP is
  179. the prefix argument to `ido-restrict-to-matches' and TEXT is the
  180. pattern used to restrict.")
  181. (defgroup ido-completing-read-plus nil
  182. "Extra features and compatibility for `ido-completing-read'."
  183. :group 'ido)
  184. (defcustom ido-cr+-fallback-function
  185. ;; Initialize to the current value of `completing-read-function',
  186. ;; unless that is already set to the ido completer, in which case
  187. ;; use `completing-read-default'.
  188. (if (memq completing-read-function
  189. '(ido-completing-read+
  190. ido-completing-read
  191. ;; Current ido-ubiquitous function
  192. completing-read-ido-ubiquitous
  193. ;; Old ido-ubiquitous functions that shouldn't be used
  194. completing-read-ido
  195. ido-ubiquitous-completing-read))
  196. 'completing-read-default
  197. completing-read-function)
  198. "Alternate completing-read function to use when ido is not wanted.
  199. This will be used for functions that are incompatible with ido
  200. or if ido cannot handle the completion arguments. It will also be
  201. used when the user requests non-ido completion manually via C-f
  202. or C-b."
  203. :type '(choice (const :tag "Standard emacs completion"
  204. completing-read-default)
  205. (function :tag "Other function"))
  206. :group 'ido-completing-read-plus)
  207. (defcustom ido-cr+-max-items 30000
  208. "Max collection size to use ido-cr+ on.
  209. If `ido-completing-read+' is called on a collection larger than
  210. this, the fallback completion method will be used instead. To
  211. disable fallback based on collection size, set this to nil."
  212. :type '(choice (const :tag "No limit" nil)
  213. (integer
  214. :tag "Limit" :value 30000
  215. :validate
  216. (lambda (widget)
  217. (let ((v (widget-value widget)))
  218. (if (and (integerp v)
  219. (> v 0))
  220. nil
  221. (widget-put widget :error "This field should contain a positive integer")
  222. widget)))))
  223. :group 'ido-completing-read-plus)
  224. (defcustom ido-cr+-function-blacklist
  225. '(read-file-name-internal
  226. read-buffer
  227. ;; https://github.com/DarwinAwardWinner/ido-completing-read-plus/issues/60
  228. todo-add-category
  229. ;; Gnus already supports ido on its own
  230. gnus-emacs-completing-read
  231. gnus-iswitchb-completing-read
  232. grep-read-files
  233. ;; Magit already supports ido on its own
  234. magit-builtin-completing-read
  235. ;; ESS already supports ido on its own
  236. ess-completing-read
  237. ;; https://github.com/DarwinAwardWinner/ido-completing-read-plus/issues/39
  238. Info-read-node-name
  239. ;; https://github.com/DarwinAwardWinner/ido-completing-read-plus/issues/44
  240. tmm-prompt
  241. ;; https://github.com/DarwinAwardWinner/ido-completing-read-plus/issues/156
  242. org-tags-completion-function)
  243. "Functions & commands for which ido-cr+ should be disabled.
  244. Each entry can be either a symbol or a string. A symbol means to
  245. fall back specifically for the named function. A regular
  246. expression means to fall back for any function whose name matches
  247. that regular expression. When ido-cr+ is called through
  248. `completing-read', if any function in the call stack of the
  249. current command matches any of the blacklist entries, ido-cr+
  250. will be disabled for that command. Additionally, if the
  251. collection in the call to `completing-read' matches any of the
  252. blacklist entries, ido-cr+ will be disabled.
  253. Note that using specific function names is generally preferable
  254. to regular expressions, because the associated function
  255. definitions will be compared directly, so if the same function is
  256. called by another name, it should still trigger the fallback. For
  257. regular expressions, only name-based matching is possible."
  258. :group 'ido-completing-read-plus
  259. :type '(repeat (choice (symbol :tag "Function or command name")
  260. (string :tag "Regexp"))))
  261. (defcustom ido-cr+-function-whitelist
  262. nil
  263. "Functions & commands for which ido-cr+ should be enabled.
  264. If this variable is nil, the whitelist will not be used, and
  265. ido-cr+ will be allowed in all functions/commands not listed in
  266. `ido-cr+-function-backlist'.
  267. If this variable is non-nil, ido-cr+'s whitelisting mode will be
  268. enabled, and ido-cr+ will be disabled for *all* functions unless
  269. they match one of the entries. Matching is done in the same
  270. manner as `ido-cr+-function-blacklist', and blacklisting takes
  271. precedence over whitelisting."
  272. :group 'ido-completing-read-plus
  273. :type '(repeat (choice (symbol :tag "Function or command name")
  274. (string :tag "Regexp"))))
  275. (defcustom ido-cr+-nil-def-alternate-behavior-list
  276. '("\\`describe-\\(function\\|variable\\)\\'"
  277. "\\`wl-"
  278. ;; https://github.com/mrkkrp/ebal/issues/12
  279. "\\`ebal-"
  280. ;; https://github.com/DarwinAwardWinner/ido-completing-read-plus/issues/4
  281. webjump
  282. ;; https://github.com/DarwinAwardWinner/ido-completing-read-plus/issues/83
  283. where-is
  284. ;; https://github.com/DarwinAwardWinner/ido-completing-read-plus/issues/51
  285. find-tag
  286. ;; https://github.com/DarwinAwardWinner/ido-completing-read-plus/issues/89
  287. "\\`etags-select-"
  288. ;; https://github.com/DarwinAwardWinner/ido-completing-read-plus/issues/58
  289. imenu--completion-buffer
  290. ;; https://github.com/DarwinAwardWinner/ido-completing-read-plus/issues/116
  291. project--completing-read-strict
  292. ;; https://github.com/DarwinAwardWinner/ido-completing-read-plus/issues/127#issuecomment-319463217
  293. bookmark-completing-read
  294. )
  295. "Functions & commands with alternate behavior when DEF is nil.
  296. This variable has the same format as
  297. `ido-cr+-function-blacklist'. When `ido-completing-read+` is
  298. called through `completing-read' by/with any command, function,
  299. or collection matched by entries in this list, it will behave
  300. differently when DEF is nil. Instead of using the empty string as
  301. the default value, it will use the first element of COLLECTION.
  302. This is needed for optimal compatibility with commands written
  303. under the assumption that REQUIRE-MATCH means that a match is
  304. required."
  305. :group 'ido-completing-read-plus
  306. :type '(repeat (choice (symbol :tag "Function or command name")
  307. (string :tag "Regexp"))))
  308. (defvaralias 'ido-cr+-nil-def-wall-of-shame 'ido-cr+-nil-def-alternate-behavior-list
  309. "Functions and commands whose authors need to read the docstring for `completing-read'.
  310. Many functions that call `completing-read' are written with the
  311. assumption that the setting the REQUIRE-MATCH argument of
  312. `completing-read' to t means it is required to return a match.
  313. While that would make logical sense, it's wrong. the docstring
  314. for `completing-read' describes the correct behavior.
  315. > If the input is null, ‘completing-read’ returns DEF, or the
  316. > first element of the list of default values, or an empty string
  317. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  318. This can be avoided by passing an element of COLLECTION as DEF
  319. instead of leaving it as nil.")
  320. ;;;###autoload
  321. (defcustom ido-cr+-replace-completely nil
  322. "If non-nil, replace `ido-completeing-read' completely with ido-cr+.
  323. Enabling this may interfere with or cause errors in other
  324. packages that use `ido-completing-read'. If you discover any such
  325. incompatibilities, please file a bug report at
  326. https://github.com/DarwinAwardWinner/ido-completing-read-plus/issues"
  327. :type 'boolean)
  328. ;; Signal used to trigger fallback
  329. (define-error 'ido-cr+-fallback "ido-cr+-fallback")
  330. (defsubst ido-cr+--explain-fallback (arg)
  331. ;; This function accepts a string, or an ido-cr+-fallback
  332. ;; signal.
  333. (when ido-cr+-debug-mode
  334. (when (and (listp arg)
  335. (eq (car arg) 'ido-cr+-fallback))
  336. (setq arg (cadr arg)))
  337. (ido-cr+--debug-message "Falling back to `%s' because %s."
  338. ido-cr+-fallback-function arg)))
  339. ;;;###autoload
  340. (defsubst ido-cr+-active ()
  341. "Returns non-nil if ido-cr+ is currently using the minibuffer."
  342. (>= ido-cr+-minibuffer-depth (minibuffer-depth)))
  343. (defun ido-cr+--called-from-completing-read ()
  344. "Returns non-nil if the most recent call to ido-cr+ was from `completing-read'."
  345. (equal (cadr (backtrace-frame 1 'ido-completing-read+))
  346. 'completing-read))
  347. (defmacro ido-cr+-function-is-in-list (fun fun-list &optional list-name)
  348. "Return non-nil if FUN matches an entry in FUN-LIST.
  349. This is used to check for matches to `ido-cr+-function-blacklist'
  350. and `ido-cr+-function-whitelist'. Read those docstrings to see
  351. how the matching is done.
  352. This is declared as macro only in order to extract the variable
  353. name used for the second argument so it can be used in a debug
  354. message. It should be called as if it were a normal function."
  355. (when (null list-name)
  356. (if (symbolp fun-list)
  357. (setq list-name (symbol-name fun-list))
  358. (setq list-name "list")))
  359. `(cl-loop
  360. for entry in ,fun-list
  361. if (cond
  362. ;; Nil: Never matches anything
  363. ((null entry)
  364. nil)
  365. ;; Symbol: Compare names and function definitions
  366. ((symbolp entry)
  367. (or (eq entry ,fun)
  368. (let ((entry-def (ignore-errors (indirect-function entry)))
  369. (fun-def (ignore-errors (indirect-function ,fun))))
  370. (and
  371. fun-def entry-def
  372. (eq
  373. (indirect-function entry-def)
  374. (indirect-function fun-def))))))
  375. ;; String: Do regexp matching against function name if it is a
  376. ;; symbol
  377. ((stringp entry)
  378. (and (symbolp ,fun)
  379. (string-match-p entry (symbol-name ,fun))))
  380. ;; Anything else: invalid blacklist entry
  381. (t
  382. (ido-cr+--debug-message "Ignoring invalid entry in %s: `%S'" ,list-name entry)
  383. nil))
  384. return entry
  385. ;; If no blacklist entry matches, return nil
  386. finally return nil))
  387. (defun ido-cr+-function-is-blacklisted (fun)
  388. "Return non-nil if FUN is blacklisted.
  389. See `ido-cr+-function-blacklist'."
  390. (ido-cr+-function-is-in-list fun ido-cr+-function-blacklist))
  391. (defun ido-cr+-function-is-whitelisted (fun)
  392. "Return non-nil if FUN is whitelisted.
  393. See `ido-cr+-function-whitelist'."
  394. (or (null ido-cr+-function-whitelist)
  395. (ido-cr+-function-is-in-list fun ido-cr+-function-whitelist)))
  396. ;;;###autoload
  397. (defun ido-completing-read+ (prompt collection &optional predicate
  398. require-match initial-input
  399. hist def inherit-input-method)
  400. "ido-based method for reading from the minibuffer with completion.
  401. See `completing-read' for the meaning of the arguments.
  402. This function is a wrapper for `ido-completing-read' designed to
  403. be used as the value of `completing-read-function'. Importantly,
  404. it detects edge cases that ido cannot handle and uses normal
  405. completion for them."
  406. (let* (;; Save the original arguments in case we need to do the
  407. ;; fallback
  408. (ido-cr+-orig-completing-read-args
  409. (list prompt collection predicate require-match
  410. initial-input hist def inherit-input-method))
  411. ;; Need to save a copy of this since activating the
  412. ;; minibuffer once will clear out any temporary minibuffer
  413. ;; hooks, which need to get restored before falling back so
  414. ;; that they will trigger again when the fallback function
  415. ;; uses the minibuffer. We make a copy in case the original
  416. ;; list gets modified in place.
  417. (orig-minibuffer-setup-hook (cl-copy-list minibuffer-setup-hook))
  418. ;; Need just the string part of INITIAL-INPUT
  419. (initial-input-string
  420. (cond
  421. ((consp initial-input)
  422. (car initial-input))
  423. ((stringp initial-input)
  424. initial-input)
  425. ((null initial-input)
  426. "")
  427. (t
  428. (signal 'wrong-type-argument (list 'stringp initial-input)))))
  429. (ido-cr+-active-restrictions nil)
  430. ;; If collection is a function, save it for later, unless
  431. ;; instructed not to
  432. (ido-cr+-dynamic-collection
  433. (when (and (not ido-cr+-assume-static-collection)
  434. (functionp collection))
  435. collection))
  436. (ido-cr+-last-dynamic-update-text nil)
  437. ;; Only memoize if the collection is dynamic.
  438. (ido-cr+-all-prefix-completions-memoized
  439. (if (and ido-cr+-dynamic-collection (featurep 'memoize))
  440. (memoize (indirect-function 'ido-cr+-all-prefix-completions))
  441. 'ido-cr+-all-prefix-completions))
  442. (ido-cr+-all-completions-memoized
  443. (if (and ido-cr+-dynamic-collection (featurep 'memoize))
  444. (memoize (indirect-function 'all-completions))
  445. 'all-completions))
  446. ;; If the whitelist is empty, everything is whitelisted
  447. (whitelisted (not ido-cr+-function-whitelist))
  448. ;; If non-nil, we need alternate nil DEF handling
  449. (alt-nil-def nil))
  450. (condition-case sig
  451. (progn
  452. ;; Check a bunch of fallback conditions
  453. (when (and inherit-input-method current-input-method)
  454. (signal 'ido-cr+-fallback
  455. '("ido cannot handle alternate input methods")))
  456. ;; Check for black/white-listed collection function
  457. (when (functionp collection)
  458. ;; Blacklist
  459. (when (ido-cr+-function-is-blacklisted collection)
  460. (if (symbolp collection)
  461. (signal 'ido-cr+-fallback
  462. (list (format "collection function `%S' is blacklisted" collection)))
  463. (signal 'ido-cr+-fallback
  464. (list "collection function is blacklisted"))))
  465. ;; Whitelist
  466. (when (and (not whitelisted)
  467. (ido-cr+-function-is-whitelisted collection))
  468. (ido-cr+--debug-message
  469. (if (symbolp collection)
  470. (format "Collection function `%S' is whitelisted" collection)
  471. "Collection function is whitelisted"))
  472. (setq whitelisted t))
  473. ;; nil DEF list
  474. (when (and
  475. require-match (null def)
  476. (ido-cr+-function-is-in-list
  477. collection
  478. ido-cr+-nil-def-alternate-behavior-list))
  479. (ido-cr+--debug-message
  480. (if (symbolp collection)
  481. (format "Using alternate nil DEF handling for collection function `%S'" collection)
  482. "Using alternate nil DEF handling for collection function"))
  483. (setq alt-nil-def t)))
  484. ;; Expand all currently-known completions.
  485. (setq collection
  486. (if ido-cr+-dynamic-collection
  487. (funcall ido-cr+-all-prefix-completions-memoized
  488. initial-input-string collection predicate)
  489. (all-completions "" collection predicate)))
  490. ;; No point in using ido unless there's a collection
  491. (when (and (= (length collection) 0)
  492. (not ido-cr+-dynamic-collection))
  493. (signal 'ido-cr+-fallback '("ido is not needed for an empty collection")))
  494. ;; Check for excessively large collection
  495. (when (and ido-cr+-max-items
  496. (> (length collection) ido-cr+-max-items))
  497. (signal 'ido-cr+-fallback
  498. (list
  499. (format
  500. "there are more than %i items in COLLECTION (see `ido-cr+-max-items')"
  501. ido-cr+-max-items))))
  502. ;; If called from `completing-read', check for
  503. ;; black/white-listed commands/callers
  504. (when (ido-cr+--called-from-completing-read)
  505. ;; Check calling command and `ido-cr+-current-command'
  506. (cl-loop
  507. for cmd in (list this-command ido-cr+-current-command)
  508. if (ido-cr+-function-is-blacklisted cmd)
  509. do (signal 'ido-cr+-fallback
  510. (list "calling command `%S' is blacklisted" cmd))
  511. if (and (not whitelisted)
  512. (ido-cr+-function-is-whitelisted cmd))
  513. do (progn
  514. (ido-cr+--debug-message "Command `%S' is whitelisted" cmd)
  515. (setq whitelisted t))
  516. if (and
  517. require-match (null def) (not alt-nil-def)
  518. (ido-cr+-function-is-in-list
  519. cmd ido-cr+-nil-def-alternate-behavior-list))
  520. do (progn
  521. (ido-cr+--debug-message
  522. "Using alternate nil DEF handling for command `%S'" cmd)
  523. (setq alt-nil-def t)))
  524. ;; Check every function in the call stack starting after
  525. ;; `completing-read' until to the first
  526. ;; `funcall-interactively' (for a call from the function
  527. ;; body) or `call-interactively' (for a call from the
  528. ;; interactive form, in which the function hasn't actually
  529. ;; been called yet, so `funcall-interactively' won't be on
  530. ;; the stack.)
  531. (cl-loop for i upfrom 1
  532. for caller = (cadr (backtrace-frame i 'completing-read))
  533. while caller
  534. while (not (memq (indirect-function caller)
  535. '(internal--funcall-interactively
  536. (indirect-function 'call-interactively))))
  537. if (ido-cr+-function-is-blacklisted caller)
  538. do (signal 'ido-cr+-fallback
  539. (list (if (symbolp caller)
  540. (format "calling function `%S' is blacklisted" caller)
  541. "a calling function is blacklisted")))
  542. if (and (not whitelisted)
  543. (ido-cr+-function-is-whitelisted caller))
  544. do (progn
  545. (ido-cr+--debug-message
  546. (if (symbolp caller)
  547. (format "Calling function `%S' is whitelisted" caller)
  548. "A calling function is whitelisted"))
  549. (setq whitelisted t))
  550. if (and require-match (null def) (not alt-nil-def)
  551. (ido-cr+-function-is-in-list
  552. caller ido-cr+-nil-def-alternate-behavior-list))
  553. do (progn
  554. (ido-cr+--debug-message
  555. (if (symbolp caller)
  556. (format "Using alternate nil DEF handling for calling function `%S'" caller)
  557. "Using alternate nil DEF handling for a calling function"))
  558. (setq alt-nil-def t))))
  559. (unless whitelisted
  560. (signal 'ido-cr+-fallback
  561. (list "no functions or commands matched the whitelist for this call")))
  562. (when (and require-match (null def))
  563. ;; Replace nil with "" for DEF if match is required, unless
  564. ;; alternate nil DEF handling is enabled
  565. (if alt-nil-def
  566. (ido-cr+--debug-message
  567. "Leaving the default at nil because alternate nil DEF handling is enabled.")
  568. (ido-cr+--debug-message
  569. "Adding \"\" as the default completion since no default was provided.")
  570. (setq def (list ""))))
  571. ;; In ido, the semantics of "default" are simply "put it at
  572. ;; the front of the list". Furthermore, ido can't handle a
  573. ;; list of defaults, nor can it handle both DEF and
  574. ;; INITIAL-INPUT being non-nil. So, just pre-process the
  575. ;; collection to put the default(s) at the front and then
  576. ;; set DEF to nil in the call to ido to avoid these issues.
  577. (unless (listp def)
  578. ;; Ensure DEF is a list
  579. (setq def (list def)))
  580. (when def
  581. ;; Ensure DEF are strings
  582. (setq def (mapcar (apply-partially #'format "%s") def))
  583. ;; Prepend DEF to COLLECTION and remove duplicates
  584. (setq collection (delete-dups (append def collection))
  585. def nil))
  586. ;; Check for a specific bug
  587. (when (and ido-enable-dot-prefix
  588. (version< emacs-version "26.1")
  589. (member "" collection))
  590. (signal 'ido-cr+-fallback
  591. '("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")))
  592. ;; Fix ido handling of cons-style INITIAL-INPUT. TODO add a
  593. ;; version check after this bug is fixed:
  594. ;; https://debbugs.gnu.org/cgi/bugreport.cgi?bug=27807
  595. (when (consp initial-input)
  596. ;; `completing-read' uses 0-based index while
  597. ;; `read-from-minibuffer' uses 1-based index.
  598. (cl-incf (cdr initial-input)))
  599. ;; Finally ready to do actual ido completion
  600. (prog1
  601. (let ((ido-cr+-minibuffer-depth (1+ (minibuffer-depth)))
  602. (ido-cr+-exhibit-pending t)
  603. ;; Reset this for recursive calls to ido-cr+
  604. (ido-cr+-assume-static-collection nil))
  605. (unwind-protect
  606. (ido-completing-read
  607. prompt collection
  608. predicate require-match initial-input hist def
  609. inherit-input-method)
  610. (named-timer-cancel :ido-cr+-dynamic-update)))
  611. ;; This detects when the user triggered fallback mode
  612. ;; manually.
  613. (when (eq ido-exit 'fallback)
  614. (signal 'ido-cr+-fallback '("user manually triggered fallback")))))
  615. ;; Handler for ido-cr+-fallback signal
  616. (ido-cr+-fallback
  617. (let (;; Reset `minibuffer-setup-hook' to original value
  618. (minibuffer-setup-hook orig-minibuffer-setup-hook)
  619. ;; Reset this for recursive calls to ido-cr+
  620. (ido-cr+-assume-static-collection nil))
  621. (ido-cr+--explain-fallback sig)
  622. (apply ido-cr+-fallback-function ido-cr+-orig-completing-read-args))))))
  623. ;;;###autoload
  624. (defun ido-completing-read@ido-cr+-replace (orig-fun &rest args)
  625. "This advice allows ido-cr+ to completely replace `ido-completing-read'.
  626. See the varaible `ido-cr+-replace-completely' for more information."
  627. (if (or (ido-cr+-active)
  628. (not ido-cr+-replace-completely))
  629. ;; ido-cr+ has either already activated or isn't going to
  630. ;; activate, so just run the function as normal
  631. (apply orig-fun args)
  632. ;; Otherwise, we need to activate ido-cr+.
  633. (apply #'ido-completing-read+ args)))
  634. ;;;###autoload
  635. (advice-add 'ido-completing-read :around
  636. #'ido-completing-read@ido-cr+-replace)
  637. ;;;###autoload
  638. (defun call-interactively@ido-cr+-record-current-command
  639. (orig-fun command &rest args)
  640. "Let-bind the command being interactively called.
  641. See `ido-cr+-current-command' for more information."
  642. (let ((ido-cr+-current-command command))
  643. (apply orig-fun command args)))
  644. ;;;###autoload
  645. (advice-add 'call-interactively :around
  646. #'call-interactively@ido-cr+-record-current-command)
  647. ;; Fallback on magic C-f and C-b
  648. (defun ido-magic-forward-char@ido-cr+-fallback (&rest _args)
  649. "Allow falling back in ido-completing-read+."
  650. (when (ido-cr+-active)
  651. ;; `ido-context-switch-command' is already let-bound at this
  652. ;; point.
  653. (setq ido-context-switch-command #'ido-fallback-command)))
  654. (advice-add 'ido-magic-forward-char :before
  655. #'ido-magic-forward-char@ido-cr+-fallback)
  656. (defun ido-magic-backward-char@ido-cr+-fallback (&rest _args)
  657. "Allow falling back in ido-completing-read+."
  658. (when (ido-cr+-active)
  659. ;; `ido-context-switch-command' is already let-bound at this
  660. ;; point.
  661. (setq ido-context-switch-command #'ido-fallback-command)))
  662. (advice-add 'ido-magic-backward-char :before
  663. #'ido-magic-backward-char@ido-cr+-fallback)
  664. (defun ido-select-text@ido-cr+-fix-require-match (orig-fun &rest args)
  665. "Fix ido behavior when `require-match' is non-nil.
  666. Standard ido will allow C-j to exit with an incomplete completion
  667. even when `require-match' is non-nil. Ordinary completion does
  668. not allow this. In ordinary completion, RET on an incomplete
  669. match is equivalent to TAB, and C-j selects the first match.
  670. Since RET in ido already selects the first match, this advice
  671. sets up C-j to be equivalent to TAB in the same situation.
  672. This advice only activates if the current ido completion was
  673. called through ido-cr+."
  674. (if (and
  675. ;; Only override C-j behavior if...
  676. ;; We're using ico-cr+, and...
  677. (ido-cr+-active)
  678. ;; Require-match is non-nil, and...
  679. ido-require-match
  680. ;; The current input doesn't exactly match a known option, and...
  681. (not (member ido-text ido-cur-list))
  682. ;; The current input doesn't exactly match an option according
  683. ;; to `test-completion' (or the collection is not dynamic).
  684. (or (not ido-cr+-dynamic-collection)
  685. (test-completion ido-text ido-cr+-dynamic-collection
  686. (nth 2 ido-cr+-orig-completing-read-args))))
  687. (progn
  688. (ido-cr+--debug-message
  689. "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)")
  690. (ido-complete))
  691. (apply orig-fun args)))
  692. (advice-add 'ido-select-text :around
  693. #'ido-select-text@ido-cr+-fix-require-match)
  694. (defun ido-tidy@ido-cr+-set-exhibit-pending (&rest _args)
  695. (setq ido-cr+-exhibit-pending t))
  696. (advice-add 'ido-tidy :after 'ido-tidy@ido-cr+-set-exhibit-pending)
  697. (defun ido-exhibit@ido-cr+-clear-exhibit-pending (&rest _args)
  698. (setq ido-cr+-exhibit-pending nil))
  699. (advice-add 'ido-exhibit :before 'ido-exhibit@ido-cr+-clear-exhibit-pending)
  700. (defun ido-cr+-all-prefix-completions
  701. (string collection &optional predicate)
  702. "Run `all-completions' on every prefix of STRING.
  703. Arguments COLLECTION and PREDICATE are as in `all-completions'.
  704. Note that \"all prefixes\" includes both STRING itself and the
  705. empty string. The return value is the union of all the returned
  706. lists, with elements ordered by their first occurrence.
  707. This function is only useful if COLLECTION is a function that
  708. might return additional completions for certain non-empty strings
  709. that it wouldn't return for the empty string. If COLLECTION is
  710. not a function, this is equivalent to
  711. `(all-completions \"\" COLELCTION PREDICATE)'."
  712. (cond
  713. ;; Dynamic collection.
  714. ((functionp collection)
  715. ;; Collect completions for all prefixes of STRING starting from
  716. ;; "".
  717. (cl-loop
  718. for i from 0 upto (length string)
  719. append (funcall
  720. ido-cr+-all-completions-memoized
  721. (s-left i string)
  722. collection
  723. predicate)
  724. into completion-list
  725. finally return (delete-dups completion-list)))
  726. ;; If COLLECTION is not dynamic, then just call `all-completions'
  727. ;; on the empty string, which will already return every possible
  728. ;; completion.
  729. (t
  730. (all-completions "" collection predicate))))
  731. (defun ido-cr+-apply-restrictions (collection restrictions)
  732. "Filter COLLECTION through RESTRICTIONS in sequence.
  733. COLLECTION is a list of strings. RESTRICTIONS is a list of cons
  734. cells, with the cdr being the restriction text and the car being
  735. nil to include matches for that text and t to exclude matches for
  736. that text. The return value is a list of strings that satisfy all
  737. the restrictions, in the same order as they appeared in
  738. COLLECTION.
  739. RESTRICTIONS are applied one by one in order, which is important
  740. because in theory the order can make a difference to the final
  741. result."
  742. (cl-loop
  743. with filtered-collection = collection
  744. with need-reverse = nil
  745. for (removep . text) in restrictions
  746. for restriction-matches =
  747. (let ((ido-text text)
  748. (ido-cur-item (or ido-cur-item 'list)))
  749. (ido-set-matches-1 filtered-collection t))
  750. do (setq filtered-collection
  751. (if removep
  752. (seq-difference filtered-collection restriction-matches)
  753. (setq need-reverse (not need-reverse))
  754. restriction-matches))
  755. ;; Each run of `ido-set-matches-1' reverses the order, so reverse
  756. ;; it one more time if it had an odd number of reverses.
  757. finally return
  758. (if need-reverse
  759. (nreverse filtered-collection)
  760. filtered-collection)))
  761. (defun ido-cr+-cyclicp (x)
  762. "Returns non-nill if X is a list containing a circular reference."
  763. (cl-loop
  764. for tortoise on x
  765. for hare on (cdr x) by #'cddr
  766. thereis (eq tortoise hare)))
  767. (defun ido-cr+-update-dynamic-collection ()
  768. "Update the set of completions for a dynamic collection.
  769. This has no effect unless `ido-cr+-dynamic-collection' is non-nil."
  770. (when (and ido-cr+-dynamic-collection
  771. (ido-cr+-active))
  772. ;; (cl-assert (not (ido-cr+-cyclicp ido-cur-list)))
  773. (let ((orig-ido-cur-list ido-cur-list)
  774. (ido-text
  775. (buffer-substring-no-properties (minibuffer-prompt-end)
  776. ido-eoinput)))
  777. ;; If current `ido-text' is equal to or a prefix of the previous
  778. ;; one, a dynamic update is not needed.
  779. (when (or (null ido-cr+-last-dynamic-update-text)
  780. (not (s-prefix? ido-text ido-cr+-last-dynamic-update-text)))
  781. (ido-cr+--debug-message "Doing a dynamic update because `ido-text' changed from %S to %S"
  782. ido-cr+-last-dynamic-update-text ido-text)
  783. (setq ido-cr+-last-dynamic-update-text ido-text)
  784. (condition-case-unless-debug err
  785. (let* ((predicate (nth 2 ido-cr+-orig-completing-read-args))
  786. (first-match (car ido-matches))
  787. (strings-to-check
  788. (cond
  789. ;; If no match, then we only check `ido-text'
  790. ((null first-match)
  791. (list ido-text))
  792. ;; If `ido-text' is a prefix of `first-match', then we
  793. ;; only need to check `first-match'
  794. ((and first-match
  795. (s-prefix? ido-text first-match))
  796. (list first-match))
  797. ;; Otherwise we need to check both
  798. (t
  799. (list ido-text first-match))))
  800. (new-completions
  801. (cl-loop
  802. for string in strings-to-check
  803. append
  804. (funcall
  805. ido-cr+-all-prefix-completions-memoized
  806. string ido-cr+-dynamic-collection predicate)
  807. into result
  808. finally return result)))
  809. ;; (cl-assert (not (ido-cr+-cyclicp new-completions)))
  810. (if (equal new-completions ido-cur-list)
  811. (ido-cr+--debug-message "Skipping dynamic update because the completion list did not change.")
  812. (when (and (bound-and-true-p flx-ido-mode)
  813. (functionp 'flx-ido-reset))
  814. ;; Reset flx-ido since the set of completions has changed
  815. (funcall 'flx-ido-reset))
  816. (setq ido-cur-list (delete-dups (append ido-cur-list new-completions)))
  817. (when ido-cr+-active-restrictions
  818. (setq ido-cur-list (ido-cr+-apply-restrictions
  819. ido-cur-list
  820. ido-cr+-active-restrictions)))
  821. (ido-cr+--debug-message
  822. "Updated completion candidates for dynamic collection. `ido-cur-list' now has %s elements"
  823. ido-text (length ido-cur-list))
  824. ;; Recompute matches with new completions
  825. (let ((ido-rescan t))
  826. (ido-set-matches))
  827. (setq ido-rescan nil)
  828. ;; Put the pre-update first match (if any) back in
  829. ;; front
  830. (when (and first-match
  831. (not (equal first-match (car ido-matches)))
  832. (member first-match ido-matches))
  833. (ido-cr+--debug-message "Restoring first match %S after dynamic update" first-match)
  834. (setq ido-matches (ido-chop ido-matches first-match)))
  835. ;; Rebuild the completion display unless ido is already planning
  836. ;; to do it anyway
  837. (unless ido-cr+-exhibit-pending
  838. (ido-tidy)
  839. (let ((ido-rescan nil))
  840. (ido-exhibit)))))
  841. (error
  842. (display-warning 'ido-cr+
  843. (format
  844. "Disabling dynamic update due to error: %S"
  845. err))
  846. ;; Reset any variables that might have been modified during
  847. ;; the failed update
  848. (setq ido-cur-list orig-ido-cur-list)
  849. ;; Prevent any further attempts at dynamic updating
  850. (setq ido-cr+-dynamic-collection nil))))))
  851. ;; Always cancel an active timer when this function is called.
  852. (named-timer-cancel :ido-cr+-dynamic-update))
  853. (defun ido-cr+-schedule-dynamic-collection-update ()
  854. "Schedule a dynamic collection update for now or in the future."
  855. (when (and (ido-cr+-active)
  856. ido-cr+-dynamic-collection)
  857. ;; Cancel the previous timer
  858. (named-timer-cancel :ido-cr+-dynamic-update)
  859. (cl-assert (not (ido-cr+-cyclicp ido-cur-list)))
  860. (if (<= (length ido-matches) 1)
  861. ;; If we've narrowed it down to zero or one matches, update
  862. ;; immediately.
  863. (ido-cr+-update-dynamic-collection)
  864. ;; If there are still several choices, defer update until idle
  865. (named-timer-idle-run :ido-cr+-dynamic-update
  866. (max 0.01 ido-cr+-dynamic-update-idle-time) nil
  867. #'ido-cr+-update-dynamic-collection))))
  868. (defun ido-cr+-minibuffer-setup ()
  869. "set up minibuffer `post-command-hook' for ido-cr+ "
  870. (when (ido-cr+-active)
  871. (add-hook 'post-command-hook
  872. 'ido-cr+-schedule-dynamic-collection-update)))
  873. (add-hook 'ido-minibuffer-setup-hook
  874. 'ido-cr+-minibuffer-setup)
  875. ;; Also need to update dynamic collections on TAB, and do so *before*
  876. ;; deciding to exit based on `ido-confirm-unique-completion'
  877. (defun ido-complete@ido-cr+-update-dynamic-collection (oldfun &rest args)
  878. "Maybe update the set of completions when pressing TAB."
  879. (when ido-cr+-dynamic-collection
  880. ;; First run with `ido-confirm-unique-completion' non-nil so it
  881. ;; can't exit
  882. (let ((ido-confirm-unique-completion t))
  883. (apply oldfun args))
  884. ;; Update `ido-eoinput'
  885. (setq ido-eoinput (point-max))
  886. ;; Clear this var to force an update
  887. (setq ido-cr+-last-dynamic-update-text nil)
  888. ;; Now do update
  889. (ido-cr+-update-dynamic-collection))
  890. ;; After maybe updating the dynamic collection, if there's still
  891. ;; only one completion, now it's allowed to exit
  892. (apply oldfun args))
  893. (advice-add 'ido-complete :around 'ido-complete@ido-cr+-update-dynamic-collection)
  894. ;; When using `ido-restrict-to-matches', we also need to add an
  895. ;; equivalent predicate to the dynamic collection so that
  896. ;; dynamically-added completions are also properly restricted.
  897. (defun ido-restrict-to-matches@ido-cr+-record-restriction
  898. (&optional removep)
  899. "Record the restriction criterion for ido-cr+"
  900. (ido-cr+--debug-message "Appending restriction %S to `ido-cr+-active-restrictions'"
  901. (cons removep ido-text))
  902. (add-to-list 'ido-cr+-active-restrictions (cons removep ido-text) t))
  903. (advice-add 'ido-restrict-to-matches :before
  904. 'ido-restrict-to-matches@ido-cr+-record-restriction)
  905. ;; Interoperation with minibuffer-electric-default-mode: only show the
  906. ;; default when the input is empty and the empty string is the
  907. ;; selected choice
  908. (defun minibuf-eldef-update-minibuffer@ido-cr+-compat (orig-fun &rest args)
  909. "This advice allows minibuffer-electric-default-mode to work with ido-cr+."
  910. (if (ido-cr+-active)
  911. (unless (eq minibuf-eldef-showing-default-in-prompt
  912. (and (string= (car ido-cur-list) "")
  913. (string= ido-text "")))
  914. ;; Swap state.
  915. (setq minibuf-eldef-showing-default-in-prompt
  916. (not minibuf-eldef-showing-default-in-prompt))
  917. (overlay-put minibuf-eldef-overlay 'invisible
  918. (not minibuf-eldef-showing-default-in-prompt)))
  919. (apply orig-fun args)))
  920. (advice-add 'minibuf-eldef-update-minibuffer :around
  921. #'minibuf-eldef-update-minibuffer@ido-cr+-compat)
  922. ;;;###autoload
  923. (define-minor-mode ido-ubiquitous-mode
  924. "Use ido completion instead of standard completion almost everywhere.
  925. If this mode causes problems for a function, you can customize
  926. when ido completion is or is not used by customizing
  927. `ido-cr+-function-blacklist'."
  928. nil
  929. :global t
  930. :group 'ido-completing-read-plus
  931. ;; Actually enable/disable the mode by setting
  932. ;; `completing-read-function'.
  933. (setq completing-read-function
  934. (if ido-ubiquitous-mode
  935. #'ido-completing-read+
  936. ido-cr+-fallback-function)))
  937. (defcustom ido-cr+-auto-update-blacklist 'notify
  938. "Whether to add new overrides when updating ido-cr+.
  939. This variable has 3 possible values, with the following meanings:
  940. `t': Auto-update the blacklist
  941. `notify': Notify you about updates but do not apply them
  942. `nil': Ignore all blacklist updates
  943. Ido-cr+ comes with a default blacklist for commands that are
  944. known to be incompatible with ido completion. New versions of
  945. ido-cr+ may come with updates to this blacklist as more
  946. incompatible commands are discovered. However, customizing your
  947. own overrides would normally prevent you from receiving these
  948. updates, since Emacs will not overwrite your customizations.
  949. To resolve this problem, you can set this variable to `t', and
  950. then ido-cr+ can automatically add any new built-in overrides
  951. whenever it is updated. (Actually, the update will happen the
  952. next time Emacs is restarted after the update.) This allows you
  953. to add your own overrides but still receive updates to the
  954. default set.
  955. If you want ido-cr+ to just notify you about new default
  956. overrides instead of adding them itself, set this variable to
  957. `notify'. If you don't want this auto-update behavior at all, set
  958. it to `nil'.
  959. (Note that having this option enabled effectively prevents you
  960. from removing any of the built-in default blacklist entries,
  961. since they will simply be re-added the next time Emacs starts.)"
  962. :type '(choice :tag "When new overrides are available:"
  963. (const :menu-tag "Auto-add"
  964. :tag "Add them automatically"
  965. t)
  966. (const :menu-tag "Notify"
  967. :tag "Notify me about them"
  968. notify)
  969. (const :menu-tag "Ignore"
  970. :tag "Ignore them"
  971. nil))
  972. :group 'ido-completing-read-plus)
  973. (defun ido-cr+-update-blacklist (&optional save quiet)
  974. "Re-add any missing default blacklist entries.
  975. This is useful after an update of ido-ubiquitous that adds new
  976. default overrides. See `ido-cr+-auto-update-blacklist' for more
  977. information.
  978. If SAVE is non-nil, also save the new blacklist to the user's
  979. Custom file (but only if it was already customized beforehand).
  980. When called interactively, a prefix argument triggers a save.
  981. When called from Lisp code, this function returns non-nil if the
  982. blacklist was modified."
  983. (interactive "P")
  984. (let* ((var-state (custom-variable-state 'ido-cr+-function-blacklist
  985. ido-cr+-function-blacklist))
  986. (curval ido-cr+-function-blacklist)
  987. (defval (eval (car (get 'ido-cr+-function-blacklist 'standard-value))))
  988. (newval (delete-dups (append defval curval)))
  989. (new-entries (cl-set-difference defval curval :test #'equal))
  990. (modified nil)
  991. (saved nil)
  992. (message-lines ()))
  993. (cl-case var-state
  994. (standard
  995. ;; Var is not customized, just set the new default
  996. (ido-cr+--debug-message "Blacklist was not customized, so it has been updated to the new default value.")
  997. (setq ido-cr+-function-blacklist defval
  998. modified new-entries))
  999. ((saved set changed)
  1000. ;; Var has been customized and saved by the user, so set the
  1001. ;; new value and maybe save it
  1002. (ido-cr+--debug-message "Updating user-customized blacklist with new default entries.")
  1003. (setq ido-cr+-function-blacklist newval
  1004. modified t)
  1005. (when (and save (eq var-state 'saved))
  1006. (ido-cr+--debug-message "Saving new blacklist value to Custom file.")
  1007. (customize-save-variable 'ido-cr+-function-blacklist ido-cr+-function-blacklist)
  1008. (setq saved t)))
  1009. (otherwise
  1010. (ido-cr+--debug-message "Customization status of blacklist is unknown. Not modifying it.")))
  1011. (if (and modified (not quiet))
  1012. (progn
  1013. (push (format "Added the following entries to `ido-cr+-function-blacklist': %S" new-entries)
  1014. message-lines)
  1015. (if saved
  1016. (push "Saved the new value of `ido-cr+-function-blacklist' to your Custom file."
  1017. message-lines)
  1018. (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'."
  1019. message-lines)))
  1020. (push "No updates were required to `ido-cr+-function-blacklist'." message-lines))
  1021. (unless quiet
  1022. (message (mapconcat #'identity (nreverse message-lines) "\n")))
  1023. modified))
  1024. (defun ido-cr+-maybe-update-blacklist ()
  1025. "Maybe call `ico-cr+-update-blacklist.
  1026. See `ido-cr+-auto-update-blacklist' for more information."
  1027. (if ido-cr+-auto-update-blacklist
  1028. (let* ((curval ido-cr+-function-blacklist)
  1029. (defval (eval (car (get 'ido-cr+-function-blacklist 'standard-value))))
  1030. (new-entries (cl-set-difference defval curval :test #'equal)))
  1031. (if new-entries
  1032. (if (eq ido-cr+-auto-update-blacklist 'notify)
  1033. (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.)")
  1034. (ido-cr+--debug-message "Initiating blacklist update.")
  1035. (ido-cr+-update-blacklist t))
  1036. (ido-cr+--debug-message "No blacklist updates available.")))
  1037. (ido-cr+--debug-message "Skipping blacklist update by user request.")))
  1038. (ido-cr+-maybe-update-blacklist)
  1039. (provide 'ido-completing-read+)
  1040. ;;; ido-completing-read+.el ends here