ido-completing-read+.el 49 KB

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