ido-completing-read+.el 55 KB

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