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