ido-completing-read+.el 52 KB

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