ido-completing-read+.el 51 KB

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