ido-completing-read+.el 54 KB

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