ido-completing-read+.el 49 KB

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