ido-completing-read+.el 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  1. ;;; ido-completing-read+.el --- A completing-read-function using ido -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2011-2017 Ryan C. Thompson
  3. ;; Filename: ido-completing-read+.el
  4. ;; Author: Ryan Thompson
  5. ;; Created: Sat Apr 4 13:41:20 2015 (-0700)
  6. ;; Version: 4.13
  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+-function-blacklist' for more information.
  44. ;; ido-completing-read+ version 4.0 is a major update. The formerly
  45. ;; separate package ido-ubiquitous has been subsumed into
  46. ;; ido-completing-read+, so ido-ubiquitous 4.0 is just a wrapper that
  47. ;; loads ido-completing-read+ and displays a warning about being
  48. ;; obsolete. If you have previously customized ido-ubiquitous, be sure
  49. ;; to check out `M-x customize-group ido-completing-read-plus' after
  50. ;; updating to 4.0 and make sure the new settings are to your liking.
  51. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  52. ;;
  53. ;; This program is free software: you can redistribute it and/or modify
  54. ;; it under the terms of the GNU General Public License as published by
  55. ;; the Free Software Foundation, either version 3 of the License, or (at
  56. ;; your option) any later version.
  57. ;;
  58. ;; This program is distributed in the hope that it will be useful, but
  59. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  60. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  61. ;; General Public License for more details.
  62. ;;
  63. ;; You should have received a copy of the GNU General Public License
  64. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  65. ;;
  66. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  67. ;;
  68. ;;; Code:
  69. (defconst ido-completing-read+-version "4.13"
  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+-function-blacklist
  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. )
  277. "Functions & commands for which ido-cr+ should be disabled.
  278. Each entry can be either a symbol or a string. A symbol means to
  279. fall back specifically for the named function. A regular
  280. expression means to fall back for any function whose name matches
  281. that regular expression. When ido-cr+ is called through
  282. `completing-read', if any function in the call stack of the
  283. current command matches any of the blacklist entries, ido-cr+
  284. will be disabled for that command. Additionally, if the
  285. collection in the call to `completing-read' matches any of the
  286. blacklist entries, ido-cr+ will be disabled.
  287. Note that using specific function names is generally preferable
  288. to regular expressions, because the associated function
  289. definitions will be compared directly, so if the same function is
  290. called by another name, it should still trigger the fallback. For
  291. regular expressions, only name-based matching is possible."
  292. :group 'ido-completing-read-plus
  293. :type '(repeat (choice (symbol :tag "Function or command name")
  294. (string :tag "Regexp"))))
  295. (defcustom ido-cr+-function-whitelist
  296. nil
  297. "Functions & commands for which ido-cr+ should be enabled.
  298. If this variable is nil, the whitelist will not be used, and
  299. ido-cr+ will be allowed in all functions/commands not listed in
  300. `ido-cr+-function-backlist'.
  301. If this variable is non-nil, ido-cr+'s whitelisting mode will be
  302. enabled, and ido-cr+ will be disabled for *all* functions unless
  303. they match one of the entries. Matching is done in the same
  304. manner as `ido-cr+-function-blacklist', and blacklisting takes
  305. precedence over whitelisting."
  306. :group 'ido-completing-read-plus
  307. :type '(repeat (choice (symbol :tag "Function or command name")
  308. (string :tag "Regexp"))))
  309. (defcustom ido-cr+-nil-def-alternate-behavior-list
  310. '("\\`describe-\\(function\\|variable\\)\\'"
  311. "\\`wl-"
  312. ;; https://github.com/mrkkrp/ebal/issues/12
  313. "\\`ebal-"
  314. ;; https://github.com/DarwinAwardWinner/ido-completing-read-plus/issues/4
  315. webjump
  316. ;; https://github.com/DarwinAwardWinner/ido-completing-read-plus/issues/83
  317. where-is
  318. ;; https://github.com/DarwinAwardWinner/ido-completing-read-plus/issues/51
  319. find-tag
  320. ;; https://github.com/DarwinAwardWinner/ido-completing-read-plus/issues/89
  321. "\\`etags-select-"
  322. ;; https://github.com/DarwinAwardWinner/ido-completing-read-plus/issues/58
  323. imenu--completion-buffer
  324. ;; https://github.com/DarwinAwardWinner/ido-completing-read-plus/issues/116
  325. project--completing-read-strict
  326. ;; https://github.com/DarwinAwardWinner/ido-completing-read-plus/issues/127#issuecomment-319463217
  327. bookmark-completing-read
  328. )
  329. "Functions & commands with alternate behavior when DEF is nil.
  330. This variable has the same format as
  331. `ido-cr+-function-blacklist'. When `ido-completing-read+` is
  332. called through `completing-read' by/with any command, function,
  333. or collection matched by entries in this list, it will behave
  334. differently when DEF is nil. Instead of using the empty string as
  335. the default value, it will use the first element of COLLECTION.
  336. This is needed for optimal compatibility with commands written
  337. under the assumption that REQUIRE-MATCH means that a match is
  338. required."
  339. :group 'ido-completing-read-plus
  340. :type '(repeat (choice (symbol :tag "Function or command name")
  341. (string :tag "Regexp"))))
  342. (defvaralias 'ido-cr+-nil-def-wall-of-shame 'ido-cr+-nil-def-alternate-behavior-list
  343. "Functions and commands whose authors need to read the docstring for `completing-read'.
  344. Many functions that call `completing-read' are written with the
  345. assumption that the setting the REQUIRE-MATCH argument of
  346. `completing-read' to t means it is required to return a match.
  347. While that would make logical sense, it's wrong. the docstring
  348. for `completing-read' describes the correct behavior.
  349. > If the input is null, ‘completing-read’ returns DEF, or the
  350. > first element of the list of default values, or an empty string
  351. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  352. This can be avoided by passing an element of COLLECTION as DEF
  353. instead of leaving it as nil.")
  354. ;;;###autoload
  355. (defcustom ido-cr+-replace-completely nil
  356. "If non-nil, replace `ido-completeing-read' completely with ido-cr+.
  357. Enabling this may interfere with or cause errors in other
  358. packages that use `ido-completing-read'. If you discover any such
  359. incompatibilities, please file a bug report at
  360. https://github.com/DarwinAwardWinner/ido-completing-read-plus/issues"
  361. :type 'boolean)
  362. ;; Signal used to trigger fallback
  363. (define-error 'ido-cr+-fallback "ido-cr+-fallback")
  364. (defsubst ido-cr+--explain-fallback (arg)
  365. "Emit a debug message explaining the reason for falling back.
  366. ARG can be a string or an ido-cr+-fallback signal. In the latter
  367. case, the DATA part of the signal is used as the message."
  368. (when ido-cr+-debug-mode
  369. (when (and (listp arg)
  370. (eq (car arg) 'ido-cr+-fallback))
  371. (setq arg (cadr arg)))
  372. (ido-cr+--debug-message "Falling back to `%s' because %s."
  373. ido-cr+-fallback-function arg)))
  374. ;;;###autoload
  375. (defsubst ido-cr+-active ()
  376. "Return non-nil if ido-cr+ is currently using the minibuffer."
  377. (>= ido-cr+-minibuffer-depth (minibuffer-depth)))
  378. (defun ido-cr+--called-from-completing-read ()
  379. "Return non-nil if the most recent call to ido-cr+ was from `completing-read'."
  380. (equal (cadr (backtrace-frame 1 'ido-completing-read+))
  381. 'completing-read))
  382. (defmacro ido-cr+-function-is-in-list (fun fun-list &optional list-name)
  383. "Return non-nil if FUN matches an entry in FUN-LIST.
  384. This is used to check for matches to `ido-cr+-function-blacklist'
  385. and `ido-cr+-function-whitelist'. Read those docstrings to see
  386. how the matching is done.
  387. This is declared as macro only in order to extract the variable
  388. name used for the second argument so it can be used in a debug
  389. message. It should be called as if it were a normal function. The
  390. optional 3rd argument LIST-NAME can be used to provide this
  391. information manually if it is known."
  392. (when (null list-name)
  393. (if (symbolp fun-list)
  394. (setq list-name (symbol-name fun-list))
  395. (setq list-name "list")))
  396. `(cl-loop
  397. for entry in ,fun-list
  398. if (cond
  399. ;; Nil: Never matches anything
  400. ((null entry)
  401. nil)
  402. ;; Symbol: Compare names and function definitions
  403. ((symbolp entry)
  404. (or (eq entry ,fun)
  405. (let ((entry-def (ignore-errors (indirect-function entry)))
  406. (fun-def (ignore-errors (indirect-function ,fun))))
  407. (and
  408. fun-def entry-def
  409. (eq
  410. (indirect-function entry-def)
  411. (indirect-function fun-def))))))
  412. ;; String: Do regexp matching against function name if it is a
  413. ;; symbol
  414. ((stringp entry)
  415. (and (symbolp ,fun)
  416. (string-match-p entry (symbol-name ,fun))))
  417. ;; Anything else: invalid blacklist entry
  418. (t
  419. (ido-cr+--debug-message "Ignoring invalid entry in %s: `%S'" ,list-name entry)
  420. nil))
  421. return entry
  422. ;; If no blacklist entry matches, return nil
  423. finally return nil))
  424. (defun ido-cr+-function-is-blacklisted (fun)
  425. "Return non-nil if FUN is blacklisted.
  426. See `ido-cr+-function-blacklist'."
  427. (ido-cr+-function-is-in-list fun ido-cr+-function-blacklist))
  428. (defun ido-cr+-function-is-whitelisted (fun)
  429. "Return non-nil if FUN is whitelisted.
  430. See `ido-cr+-function-whitelist'."
  431. (or (null ido-cr+-function-whitelist)
  432. (ido-cr+-function-is-in-list fun ido-cr+-function-whitelist)))
  433. ;;;###autoload
  434. (defun ido-completing-read+ (prompt collection &optional predicate
  435. require-match initial-input
  436. hist def inherit-input-method)
  437. "Ido-based method for reading from the minibuffer with completion.
  438. See `completing-read' for the meaning of the arguments.
  439. This function is a wrapper for `ido-completing-read' designed to
  440. be used as the value of `completing-read-function'. Importantly,
  441. it detects edge cases that ido cannot handle and uses normal
  442. completion for them.
  443. See `completing-read' for the meaning of the arguments."
  444. (let* (;; Save the original arguments in case we need to do the
  445. ;; fallback
  446. (ido-cr+-orig-completing-read-args
  447. (list prompt collection predicate require-match
  448. initial-input hist def inherit-input-method))
  449. ;; Need to save a copy of this since activating the
  450. ;; minibuffer once will clear out any temporary minibuffer
  451. ;; hooks, which need to get restored before falling back so
  452. ;; that they will trigger again when the fallback function
  453. ;; uses the minibuffer. We make a copy in case the original
  454. ;; list gets modified in place.
  455. (orig-minibuffer-setup-hook (cl-copy-list minibuffer-setup-hook))
  456. ;; Need just the string part of INITIAL-INPUT
  457. (initial-input-string
  458. (cond
  459. ((consp initial-input)
  460. (car initial-input))
  461. ((stringp initial-input)
  462. initial-input)
  463. ((null initial-input)
  464. "")
  465. (t
  466. (signal 'wrong-type-argument (list 'stringp initial-input)))))
  467. (ido-cr+-active-restrictions nil)
  468. ;; If collection is a function, save it for later, unless
  469. ;; instructed not to
  470. (ido-cr+-dynamic-collection
  471. (when (and (not ido-cr+-assume-static-collection)
  472. (functionp collection))
  473. collection))
  474. (ido-cr+-last-dynamic-update-text nil)
  475. ;; Only memoize if the collection is dynamic.
  476. (ido-cr+-all-prefix-completions-memoized
  477. (if (and ido-cr+-dynamic-collection (featurep 'memoize))
  478. (memoize (indirect-function 'ido-cr+-all-prefix-completions))
  479. 'ido-cr+-all-prefix-completions))
  480. (ido-cr+-all-completions-memoized
  481. (if (and ido-cr+-dynamic-collection (featurep 'memoize))
  482. (memoize (indirect-function 'all-completions))
  483. 'all-completions))
  484. ;; If the whitelist is empty, everything is whitelisted
  485. (whitelisted (not ido-cr+-function-whitelist))
  486. ;; If non-nil, we need alternate nil DEF handling
  487. (alt-nil-def nil))
  488. (condition-case sig
  489. (progn
  490. ;; Check a bunch of fallback conditions
  491. (when (and inherit-input-method current-input-method)
  492. (signal 'ido-cr+-fallback
  493. '("ido cannot handle alternate input methods")))
  494. ;; Check for black/white-listed collection function
  495. (when (functionp collection)
  496. ;; Blacklist
  497. (when (ido-cr+-function-is-blacklisted collection)
  498. (if (symbolp collection)
  499. (signal 'ido-cr+-fallback
  500. (list (format "collection function `%S' is blacklisted" collection)))
  501. (signal 'ido-cr+-fallback
  502. (list "collection function is blacklisted"))))
  503. ;; Whitelist
  504. (when (and (not whitelisted)
  505. (ido-cr+-function-is-whitelisted collection))
  506. (ido-cr+--debug-message
  507. (if (symbolp collection)
  508. (format "Collection function `%S' is whitelisted" collection)
  509. "Collection function is whitelisted"))
  510. (setq whitelisted t))
  511. ;; nil DEF list
  512. (when (and
  513. require-match (null def)
  514. (ido-cr+-function-is-in-list
  515. collection
  516. ido-cr+-nil-def-alternate-behavior-list))
  517. (ido-cr+--debug-message
  518. (if (symbolp collection)
  519. (format "Using alternate nil DEF handling for collection function `%S'" collection)
  520. "Using alternate nil DEF handling for collection function"))
  521. (setq alt-nil-def t)))
  522. ;; Expand all currently-known completions.
  523. (setq collection
  524. (if ido-cr+-dynamic-collection
  525. (funcall ido-cr+-all-prefix-completions-memoized
  526. initial-input-string collection predicate)
  527. (all-completions "" collection predicate)))
  528. ;; No point in using ido unless there's a collection
  529. (when (and (= (length collection) 0)
  530. (not ido-cr+-dynamic-collection))
  531. (signal 'ido-cr+-fallback '("ido is not needed for an empty collection")))
  532. ;; Check for excessively large collection
  533. (when (and ido-cr+-max-items
  534. (> (length collection) ido-cr+-max-items))
  535. (signal 'ido-cr+-fallback
  536. (list
  537. (format
  538. "there are more than %i items in COLLECTION (see `ido-cr+-max-items')"
  539. ido-cr+-max-items))))
  540. ;; If called from `completing-read', check for
  541. ;; black/white-listed commands/callers
  542. (when (ido-cr+--called-from-completing-read)
  543. ;; Check calling command and `ido-cr+-current-command'
  544. (cl-loop
  545. for cmd in (list this-command ido-cr+-current-command)
  546. if (ido-cr+-function-is-blacklisted cmd)
  547. do (signal 'ido-cr+-fallback
  548. (list "calling command `%S' is blacklisted" cmd))
  549. if (and (not whitelisted)
  550. (ido-cr+-function-is-whitelisted cmd))
  551. do (progn
  552. (ido-cr+--debug-message "Command `%S' is whitelisted" cmd)
  553. (setq whitelisted t))
  554. if (and
  555. require-match (null def) (not alt-nil-def)
  556. (ido-cr+-function-is-in-list
  557. cmd ido-cr+-nil-def-alternate-behavior-list))
  558. do (progn
  559. (ido-cr+--debug-message
  560. "Using alternate nil DEF handling for command `%S'" cmd)
  561. (setq alt-nil-def t)))
  562. ;; Check every function in the call stack starting after
  563. ;; `completing-read' until to the first
  564. ;; `funcall-interactively' (for a call from the function
  565. ;; body) or `call-interactively' (for a call from the
  566. ;; interactive form, in which the function hasn't actually
  567. ;; been called yet, so `funcall-interactively' won't be on
  568. ;; the stack.)
  569. (cl-loop for i upfrom 1
  570. for caller = (cadr (backtrace-frame i 'completing-read))
  571. while caller
  572. while (not (memq (indirect-function caller)
  573. '(internal--funcall-interactively
  574. (indirect-function 'call-interactively))))
  575. if (ido-cr+-function-is-blacklisted caller)
  576. do (signal 'ido-cr+-fallback
  577. (list (if (symbolp caller)
  578. (format "calling function `%S' is blacklisted" caller)
  579. "a calling function is blacklisted")))
  580. if (and (not whitelisted)
  581. (ido-cr+-function-is-whitelisted caller))
  582. do (progn
  583. (ido-cr+--debug-message
  584. (if (symbolp caller)
  585. (format "Calling function `%S' is whitelisted" caller)
  586. "A calling function is whitelisted"))
  587. (setq whitelisted t))
  588. if (and require-match (null def) (not alt-nil-def)
  589. (ido-cr+-function-is-in-list
  590. caller ido-cr+-nil-def-alternate-behavior-list))
  591. do (progn
  592. (ido-cr+--debug-message
  593. (if (symbolp caller)
  594. (format "Using alternate nil DEF handling for calling function `%S'" caller)
  595. "Using alternate nil DEF handling for a calling function"))
  596. (setq alt-nil-def t))))
  597. (unless whitelisted
  598. (signal 'ido-cr+-fallback
  599. (list "no functions or commands matched the whitelist for this call")))
  600. (when (and require-match (null def))
  601. ;; Replace nil with "" for DEF if match is required, unless
  602. ;; alternate nil DEF handling is enabled
  603. (if alt-nil-def
  604. (ido-cr+--debug-message
  605. "Leaving the default at nil because alternate nil DEF handling is enabled.")
  606. (ido-cr+--debug-message
  607. "Adding \"\" as the default completion since no default was provided.")
  608. (setq def (list ""))))
  609. ;; In ido, the semantics of "default" are simply "put it at
  610. ;; the front of the list". Furthermore, ido can't handle a
  611. ;; list of defaults, nor can it handle both DEF and
  612. ;; INITIAL-INPUT being non-nil. So, just pre-process the
  613. ;; collection to put the default(s) at the front and then
  614. ;; set DEF to nil in the call to ido to avoid these issues.
  615. (unless (listp def)
  616. ;; Ensure DEF is a list
  617. (setq def (list def)))
  618. (when def
  619. ;; Ensure DEF are strings
  620. (setq def (mapcar (apply-partially #'format "%s") def))
  621. ;; Prepend DEF to COLLECTION and remove duplicates
  622. (setq collection (delete-dups (append def collection))
  623. def nil))
  624. ;; Check for a specific bug
  625. (when (and ido-enable-dot-prefix
  626. (version< emacs-version "26.1")
  627. (member "" collection))
  628. (signal 'ido-cr+-fallback
  629. '("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")))
  630. ;; Fix ido's broken handling of cons-style INITIAL-INPUT on
  631. ;; Emacsen older than 27. See Emacs bug #27807.
  632. (when (and (consp initial-input)
  633. ido-cr+-need-bug27807-workaround)
  634. ;; `completing-read' uses 0-based index while
  635. ;; `read-from-minibuffer' uses 1-based index.
  636. (cl-incf (cdr initial-input)))
  637. ;; Finally ready to do actual ido completion
  638. (prog1
  639. (let ((ido-cr+-minibuffer-depth (1+ (minibuffer-depth)))
  640. (ido-cr+-dynamic-update-timer nil)
  641. (ido-cr+-exhibit-pending t)
  642. ;; Reset this for recursive calls to ido-cr+
  643. (ido-cr+-assume-static-collection nil))
  644. (unwind-protect
  645. (ido-completing-read
  646. prompt collection
  647. predicate require-match initial-input hist def
  648. inherit-input-method)
  649. (when ido-cr+-dynamic-update-timer
  650. (cancel-timer ido-cr+-dynamic-update-timer)
  651. (setq ido-cr+-dynamic-update-timer nil))))
  652. ;; This detects when the user triggered fallback mode
  653. ;; manually.
  654. (when (eq ido-exit 'fallback)
  655. (signal 'ido-cr+-fallback '("user manually triggered fallback")))))
  656. ;; Handler for ido-cr+-fallback signal
  657. (ido-cr+-fallback
  658. (let (;; Reset `minibuffer-setup-hook' to original value
  659. (minibuffer-setup-hook orig-minibuffer-setup-hook)
  660. ;; Reset this for recursive calls to ido-cr+
  661. (ido-cr+-assume-static-collection nil))
  662. (ido-cr+--explain-fallback sig)
  663. (apply ido-cr+-fallback-function ido-cr+-orig-completing-read-args))))))
  664. ;;;###autoload
  665. (defun ido-completing-read@ido-cr+-replace (orig-fun &rest args)
  666. "This advice allows ido-cr+ to completely replace `ido-completing-read'.
  667. See the varaible `ido-cr+-replace-completely' for more information."
  668. (if (or (ido-cr+-active)
  669. (not ido-cr+-replace-completely))
  670. ;; ido-cr+ has either already activated or isn't going to
  671. ;; activate, so just run the function as normal
  672. (apply orig-fun args)
  673. ;; Otherwise, we need to activate ido-cr+.
  674. (apply #'ido-completing-read+ args)))
  675. ;;;###autoload
  676. (advice-add 'ido-completing-read :around
  677. #'ido-completing-read@ido-cr+-replace)
  678. ;;;###autoload
  679. (defun call-interactively@ido-cr+-record-current-command
  680. (orig-fun command &rest args)
  681. "Let-bind the command being interactively called.
  682. See `ido-cr+-current-command' for more information."
  683. (let ((ido-cr+-current-command command))
  684. (apply orig-fun command args)))
  685. ;;;###autoload
  686. (advice-add 'call-interactively :around
  687. #'call-interactively@ido-cr+-record-current-command)
  688. ;; Fallback on magic C-f and C-b
  689. (defun ido-magic-forward-char@ido-cr+-fallback (&rest _args)
  690. "Allow falling back in ido-completing-read+."
  691. (when (ido-cr+-active)
  692. ;; `ido-context-switch-command' is already let-bound at this
  693. ;; point.
  694. (setq ido-context-switch-command #'ido-fallback-command)))
  695. (advice-add 'ido-magic-forward-char :before
  696. #'ido-magic-forward-char@ido-cr+-fallback)
  697. (defun ido-magic-backward-char@ido-cr+-fallback (&rest _args)
  698. "Allow falling back in ido-completing-read+."
  699. (when (ido-cr+-active)
  700. ;; `ido-context-switch-command' is already let-bound at this
  701. ;; point.
  702. (setq ido-context-switch-command #'ido-fallback-command)))
  703. (advice-add 'ido-magic-backward-char :before
  704. #'ido-magic-backward-char@ido-cr+-fallback)
  705. (defun ido-select-text@ido-cr+-fix-require-match (orig-fun &rest args)
  706. "Fix ido behavior when `require-match' is non-nil.
  707. Standard ido will allow C-j to exit with an incomplete completion
  708. even when `require-match' is non-nil. Ordinary completion does
  709. not allow this. In ordinary completion, RET on an incomplete
  710. match is equivalent to TAB, and C-j selects the first match.
  711. Since RET in ido already selects the first match, this advice
  712. sets up C-j to be equivalent to TAB in the same situation.
  713. This advice only activates if the current ido completion was
  714. called through ido-cr+."
  715. (if (and
  716. ;; Only override C-j behavior if...
  717. ;; We're using ico-cr+, and...
  718. (ido-cr+-active)
  719. ;; Require-match is non-nil, and...
  720. ido-require-match
  721. ;; The current input doesn't exactly match a known option, and...
  722. (not (member ido-text ido-cur-list))
  723. ;; The current input doesn't exactly match an option according
  724. ;; to `test-completion' (or the collection is not dynamic).
  725. (or (not ido-cr+-dynamic-collection)
  726. (test-completion ido-text ido-cr+-dynamic-collection
  727. (nth 2 ido-cr+-orig-completing-read-args))))
  728. (progn
  729. (ido-cr+--debug-message
  730. "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)")
  731. (ido-complete))
  732. (apply orig-fun args)))
  733. (advice-add 'ido-select-text :around
  734. #'ido-select-text@ido-cr+-fix-require-match)
  735. (defun ido-tidy@ido-cr+-set-exhibit-pending (&rest _args)
  736. "Advice to manage the value of `ido-cr+-exhibit-pending'."
  737. (setq ido-cr+-exhibit-pending t))
  738. (advice-add 'ido-tidy :after 'ido-tidy@ido-cr+-set-exhibit-pending)
  739. (defun ido-exhibit@ido-cr+-clear-exhibit-pending (&rest _args)
  740. "Advice to manage the value of `ido-cr+-exhibit-pending'."
  741. (setq ido-cr+-exhibit-pending nil))
  742. (advice-add 'ido-exhibit :before 'ido-exhibit@ido-cr+-clear-exhibit-pending)
  743. (defun ido-cr+-all-prefix-completions
  744. (string collection &optional predicate)
  745. "Run `all-completions' on every prefix of STRING.
  746. Arguments COLLECTION and PREDICATE are as in `all-completions'.
  747. Note that \"all prefixes\" includes both STRING itself and the
  748. empty string. The return value is the union of all the returned
  749. lists, with elements ordered by their first occurrence.
  750. This function is only useful if COLLECTION is a function that
  751. might return additional completions for certain non-empty strings
  752. that it wouldn't return for the empty string. If COLLECTION is
  753. not a function, this is equivalent to
  754. `(all-completions \"\" COLELCTION PREDICATE)'."
  755. (cond
  756. ;; Dynamic collection.
  757. ((functionp collection)
  758. ;; Collect completions for all prefixes of STRING starting from
  759. ;; "".
  760. (cl-loop
  761. for i from 0 upto (length string)
  762. append (funcall
  763. ido-cr+-all-completions-memoized
  764. (substring string 0 i)
  765. collection
  766. predicate)
  767. into completion-list
  768. finally return (delete-dups completion-list)))
  769. ;; If COLLECTION is not dynamic, then just call `all-completions'
  770. ;; on the empty string, which will already return every possible
  771. ;; completion.
  772. (t
  773. (all-completions "" collection predicate))))
  774. (defun ido-cr+-apply-restrictions (collection restrictions)
  775. "Filter COLLECTION through RESTRICTIONS in sequence.
  776. COLLECTION is a list of strings. RESTRICTIONS is a list of cons
  777. cells, with the cdr being the restriction text and the car being
  778. nil to include matches for that text and t to exclude matches for
  779. that text. The return value is a list of strings that satisfy all
  780. the restrictions, in the same order as they appeared in
  781. COLLECTION.
  782. RESTRICTIONS are applied one by one in order, which is important
  783. because in theory the order can make a difference to the final
  784. result."
  785. (cl-loop
  786. with filtered-collection = collection
  787. with need-reverse = nil
  788. for (removep . text) in restrictions
  789. for restriction-matches =
  790. (let ((ido-text text)
  791. (ido-cur-item (or ido-cur-item 'list)))
  792. (ido-set-matches-1 filtered-collection t))
  793. do (setq filtered-collection
  794. (if removep
  795. (seq-difference filtered-collection restriction-matches)
  796. (setq need-reverse (not need-reverse))
  797. restriction-matches))
  798. ;; Each run of `ido-set-matches-1' reverses the order, so reverse
  799. ;; it one more time if it had an odd number of reverses.
  800. finally return
  801. (if need-reverse
  802. (nreverse filtered-collection)
  803. filtered-collection)))
  804. (defun ido-cr+-cyclicp (x)
  805. "Return non-nill if X is a list containing a circular reference."
  806. (cl-loop
  807. for tortoise on x
  808. for hare on (cdr x) by #'cddr
  809. thereis (eq tortoise hare)))
  810. (defun ido-cr+-update-dynamic-collection ()
  811. "Update the set of completions for a dynamic collection.
  812. This has no effect unless `ido-cr+-dynamic-collection' is non-nil."
  813. (when (and ido-cr+-dynamic-collection
  814. (ido-cr+-active))
  815. ;; (cl-assert (not (ido-cr+-cyclicp ido-cur-list)))
  816. (let ((orig-ido-cur-list ido-cur-list)
  817. (ido-text
  818. (buffer-substring-no-properties (minibuffer-prompt-end)
  819. ido-eoinput)))
  820. ;; If current `ido-text' is equal to or a prefix of the previous
  821. ;; one, a dynamic update is not needed.
  822. (when (or (null ido-cr+-last-dynamic-update-text)
  823. (not (string-prefix-p ido-text ido-cr+-last-dynamic-update-text)))
  824. (ido-cr+--debug-message "Doing a dynamic update because `ido-text' changed from %S to %S"
  825. ido-cr+-last-dynamic-update-text ido-text)
  826. (setq ido-cr+-last-dynamic-update-text ido-text)
  827. (condition-case-unless-debug err
  828. (let* ((predicate (nth 2 ido-cr+-orig-completing-read-args))
  829. (first-match (car ido-matches))
  830. (strings-to-check
  831. (cond
  832. ;; If no match, then we only check `ido-text'
  833. ((null first-match)
  834. (list ido-text))
  835. ;; If `ido-text' is a prefix of `first-match', then we
  836. ;; only need to check `first-match'
  837. ((and first-match
  838. (string-prefix-p ido-text first-match))
  839. (list first-match))
  840. ;; Otherwise we need to check both
  841. (t
  842. (list ido-text first-match))))
  843. (new-completions
  844. (cl-loop
  845. for string in strings-to-check
  846. append
  847. (funcall
  848. ido-cr+-all-prefix-completions-memoized
  849. string ido-cr+-dynamic-collection predicate)
  850. into result
  851. finally return result)))
  852. ;; (cl-assert (not (ido-cr+-cyclicp new-completions)))
  853. (if (equal new-completions ido-cur-list)
  854. (ido-cr+--debug-message "Skipping dynamic update because the completion list did not change.")
  855. (when (and (bound-and-true-p flx-ido-mode)
  856. (functionp 'flx-ido-reset))
  857. ;; Reset flx-ido since the set of completions has changed
  858. (funcall 'flx-ido-reset))
  859. (setq ido-cur-list (delete-dups (append ido-cur-list new-completions)))
  860. (when ido-cr+-active-restrictions
  861. (setq ido-cur-list (ido-cr+-apply-restrictions
  862. ido-cur-list
  863. ido-cr+-active-restrictions)))
  864. (ido-cr+--debug-message
  865. "Updated completion candidates for dynamic collection. `ido-cur-list' now has %s elements"
  866. ido-text (length ido-cur-list))
  867. ;; Recompute matches with new completions
  868. (let ((ido-rescan t))
  869. (ido-set-matches))
  870. (setq ido-rescan nil)
  871. ;; Put the pre-update first match (if any) back in
  872. ;; front
  873. (when (and first-match
  874. (not (equal first-match (car ido-matches)))
  875. (member first-match ido-matches))
  876. (ido-cr+--debug-message "Restoring first match %S after dynamic update" first-match)
  877. (setq ido-matches (ido-chop ido-matches first-match)))
  878. ;; Rebuild the completion display unless ido is already planning
  879. ;; to do it anyway
  880. (unless ido-cr+-exhibit-pending
  881. (ido-tidy)
  882. (let ((ido-rescan nil))
  883. (ido-exhibit)))))
  884. (error
  885. (display-warning 'ido-cr+
  886. (format
  887. "Disabling dynamic update due to error: %S"
  888. err))
  889. ;; Reset any variables that might have been modified during
  890. ;; the failed update
  891. (setq ido-cur-list orig-ido-cur-list)
  892. ;; Prevent any further attempts at dynamic updating
  893. (setq ido-cr+-dynamic-collection nil))))))
  894. ;; Always cancel an active timer when this function is called.
  895. (when ido-cr+-dynamic-update-timer
  896. (cancel-timer ido-cr+-dynamic-update-timer)
  897. (setq ido-cr+-dynamic-update-timer nil)))
  898. (defun ido-cr+-schedule-dynamic-collection-update ()
  899. "Schedule a dynamic collection update for now or in the future."
  900. (when (and (ido-cr+-active)
  901. ido-cr+-dynamic-collection)
  902. ;; Cancel the previous timer
  903. (when ido-cr+-dynamic-update-timer
  904. (cancel-timer ido-cr+-dynamic-update-timer)
  905. (setq ido-cr+-dynamic-update-timer nil))
  906. (cl-assert (not (ido-cr+-cyclicp ido-cur-list)))
  907. (if (<= (length ido-matches) 1)
  908. ;; If we've narrowed it down to zero or one matches, update
  909. ;; immediately.
  910. (ido-cr+-update-dynamic-collection)
  911. ;; If there are still several choices, defer update until idle
  912. (setq ido-cr+-dynamic-update-timer
  913. (run-with-idle-timer (max 0.01 ido-cr+-dynamic-update-idle-time) nil
  914. #'ido-cr+-update-dynamic-collection)))))
  915. (defun ido-cr+-minibuffer-setup ()
  916. "Set up minibuffer `post-command-hook' for ido-cr+."
  917. (when (ido-cr+-active)
  918. (add-hook 'post-command-hook
  919. 'ido-cr+-schedule-dynamic-collection-update)))
  920. (add-hook 'ido-minibuffer-setup-hook
  921. 'ido-cr+-minibuffer-setup)
  922. ;; Also need to update dynamic collections on TAB, and do so *before*
  923. ;; deciding to exit based on `ido-confirm-unique-completion'
  924. (defun ido-complete@ido-cr+-update-dynamic-collection (oldfun &rest args)
  925. "Maybe update the set of completions when pressing TAB."
  926. (when ido-cr+-dynamic-collection
  927. ;; First run with `ido-confirm-unique-completion' non-nil so it
  928. ;; can't exit
  929. (let ((ido-confirm-unique-completion t))
  930. (apply oldfun args))
  931. ;; Update `ido-eoinput'
  932. (setq ido-eoinput (point-max))
  933. ;; Clear this var to force an update
  934. (setq ido-cr+-last-dynamic-update-text nil)
  935. ;; Now do update
  936. (ido-cr+-update-dynamic-collection))
  937. ;; After maybe updating the dynamic collection, if there's still
  938. ;; only one completion, now it's allowed to exit
  939. (apply oldfun args))
  940. (advice-add 'ido-complete :around 'ido-complete@ido-cr+-update-dynamic-collection)
  941. ;; When using `ido-restrict-to-matches', we also need to add an
  942. ;; equivalent predicate to the dynamic collection so that
  943. ;; dynamically-added completions are also properly restricted.
  944. (defun ido-restrict-to-matches@ido-cr+-record-restriction
  945. (&optional removep)
  946. "Record the restriction criterion for ido-cr+."
  947. (ido-cr+--debug-message "Appending restriction %S to `ido-cr+-active-restrictions'"
  948. (cons removep ido-text))
  949. (add-to-list 'ido-cr+-active-restrictions (cons removep ido-text) t))
  950. (advice-add 'ido-restrict-to-matches :before
  951. 'ido-restrict-to-matches@ido-cr+-record-restriction)
  952. ;; Interoperation with minibuffer-electric-default-mode: only show the
  953. ;; default when the input is empty and the empty string is the
  954. ;; selected choice
  955. (defun minibuf-eldef-update-minibuffer@ido-cr+-compat (orig-fun &rest args)
  956. "This advice allows `minibuffer-electric-default-mode' to work with ido-cr+."
  957. (if (ido-cr+-active)
  958. (unless (eq minibuf-eldef-showing-default-in-prompt
  959. (and (string= (car ido-cur-list) "")
  960. (string= ido-text "")))
  961. ;; Swap state.
  962. (setq minibuf-eldef-showing-default-in-prompt
  963. (not minibuf-eldef-showing-default-in-prompt))
  964. (overlay-put minibuf-eldef-overlay 'invisible
  965. (not minibuf-eldef-showing-default-in-prompt)))
  966. (apply orig-fun args)))
  967. (advice-add 'minibuf-eldef-update-minibuffer :around
  968. #'minibuf-eldef-update-minibuffer@ido-cr+-compat)
  969. ;;;###autoload
  970. (define-minor-mode ido-ubiquitous-mode
  971. "Use ido completion instead of standard completion almost everywhere.
  972. If this mode causes problems for a function, you can customize
  973. when ido completion is or is not used by customizing
  974. `ido-cr+-function-blacklist'."
  975. nil
  976. :global t
  977. :group 'ido-completing-read-plus
  978. ;; Actually enable/disable the mode by setting
  979. ;; `completing-read-function'.
  980. (setq completing-read-function
  981. (if ido-ubiquitous-mode
  982. #'ido-completing-read+
  983. ido-cr+-fallback-function)))
  984. (defcustom ido-cr+-auto-update-blacklist 'notify
  985. "Whether to add new overrides when updating ido-cr+.
  986. This variable has 3 possible values, with the following meanings:
  987. t: Auto-update the blacklist
  988. `notify': Notify you about updates but do not apply them
  989. nil: Ignore all blacklist updates
  990. Ido-cr+ comes with a default blacklist for commands that are
  991. known to be incompatible with ido completion. New versions of
  992. ido-cr+ may come with updates to this blacklist as more
  993. incompatible commands are discovered. However, customizing your
  994. own overrides would normally prevent you from receiving these
  995. updates, since Emacs will not overwrite your customizations.
  996. To resolve this problem, you can set this variable to t, and then
  997. ido-cr+ can automatically add any new built-in overrides whenever
  998. it is updated. (Actually, the update will happen the next time
  999. Emacs is restarted after the update.) This allows you to add your
  1000. own overrides but still receive updates to the default set.
  1001. If you want ido-cr+ to just notify you about new default
  1002. overrides instead of adding them itself, set this variable to
  1003. `notify'. If you don't want this auto-update behavior at all, set
  1004. it to nil.
  1005. \(Note that having this option enabled effectively prevents you
  1006. from removing any of the built-in default blacklist entries,
  1007. since they will simply be re-added the next time Emacs starts.)"
  1008. :type '(choice :tag "When new overrides are available:"
  1009. (const :menu-tag "Auto-add"
  1010. :tag "Add them automatically"
  1011. t)
  1012. (const :menu-tag "Notify"
  1013. :tag "Notify me about them"
  1014. notify)
  1015. (const :menu-tag "Ignore"
  1016. :tag "Ignore them"
  1017. nil))
  1018. :group 'ido-completing-read-plus)
  1019. (defun ido-cr+-update-blacklist (&optional save quiet)
  1020. "Re-add any missing default blacklist entries.
  1021. This is useful after an update of ido-ubiquitous that adds new
  1022. default overrides. See `ido-cr+-auto-update-blacklist' for more
  1023. information.
  1024. If SAVE is non-nil, also save the new blacklist to the user's
  1025. Custom file (but only if it was already customized beforehand).
  1026. When called interactively, a prefix argument triggers a save.
  1027. Unless QUIET is non-nil, this function produces messages indicating
  1028. all changes that were made.
  1029. When called from Lisp code, this function returns non-nil if the
  1030. blacklist was modified."
  1031. (interactive "P")
  1032. (let* ((var-state (custom-variable-state 'ido-cr+-function-blacklist
  1033. ido-cr+-function-blacklist))
  1034. (curval ido-cr+-function-blacklist)
  1035. (defval (eval (car (get 'ido-cr+-function-blacklist 'standard-value))))
  1036. (newval (delete-dups (append defval curval)))
  1037. (new-entries (cl-set-difference defval curval :test #'equal))
  1038. (modified nil)
  1039. (saved nil)
  1040. (message-lines ()))
  1041. (cl-case var-state
  1042. (standard
  1043. ;; Var is not customized, just set the new default
  1044. (ido-cr+--debug-message "Blacklist was not customized, so it has been updated to the new default value.")
  1045. (setq ido-cr+-function-blacklist defval
  1046. modified new-entries))
  1047. ((saved set changed)
  1048. ;; Var has been customized and saved by the user, so set the
  1049. ;; new value and maybe save it
  1050. (ido-cr+--debug-message "Updating user-customized blacklist with new default entries.")
  1051. (setq ido-cr+-function-blacklist newval
  1052. modified t)
  1053. (when (and save (eq var-state 'saved))
  1054. (ido-cr+--debug-message "Saving new blacklist value to Custom file.")
  1055. (customize-save-variable 'ido-cr+-function-blacklist ido-cr+-function-blacklist)
  1056. (setq saved t)))
  1057. (otherwise
  1058. (ido-cr+--debug-message "Customization status of blacklist is unknown. Not modifying it.")))
  1059. (if (and modified (not quiet))
  1060. (progn
  1061. (push (format "Added the following entries to `ido-cr+-function-blacklist': %S" new-entries)
  1062. message-lines)
  1063. (if saved
  1064. (push "Saved the new value of `ido-cr+-function-blacklist' to your Custom file."
  1065. message-lines)
  1066. (push "However, the new value of `ido-cr+-function-blacklist' has not yet been saved for future sessions. To save it. re-run this command with a prefix argument: `C-u M-x ido-cr+-update-blacklist'; or else manually inspect and save the value using `M-x customize-variable ido-cr+-function-blacklist'."
  1067. message-lines)))
  1068. (push "No updates were required to `ido-cr+-function-blacklist'." message-lines))
  1069. (unless quiet
  1070. (message (mapconcat #'identity (nreverse message-lines) "\n")))
  1071. modified))
  1072. (defun ido-cr+-maybe-update-blacklist ()
  1073. "Maybe call `ico-cr+-update-blacklist.
  1074. See `ido-cr+-auto-update-blacklist' for more information."
  1075. (if ido-cr+-auto-update-blacklist
  1076. (let* ((curval ido-cr+-function-blacklist)
  1077. (defval (eval (car (get 'ido-cr+-function-blacklist 'standard-value))))
  1078. (new-entries (cl-set-difference defval curval :test #'equal)))
  1079. (if new-entries
  1080. (if (eq ido-cr+-auto-update-blacklist 'notify)
  1081. (display-warning 'ido-completing-read+ (format "There are %s new blacklist entries available. Use `M-x ido-cr+-update-blacklist' to install them. (See `ido-cr+-auto-update-blacklist' for more information.)" (length new-entries)))
  1082. (ido-cr+--debug-message "Initiating blacklist update.")
  1083. (ido-cr+-update-blacklist t))
  1084. (ido-cr+--debug-message "No blacklist updates available.")))
  1085. (ido-cr+--debug-message "Skipping blacklist update by user request.")))
  1086. (ido-cr+-maybe-update-blacklist)
  1087. (provide 'ido-completing-read+)
  1088. ;;; ido-completing-read+.el ends here