ido-completing-read+.el 49 KB

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