ido-completing-read+.el 52 KB

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