ido-completing-read+.el 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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.0
  7. ;; Package-Requires: ((emacs "24.4") (cl-lib "0.5"))
  8. ;; URL: https://github.com/DarwinAwardWinner/ido-ubiquitous
  9. ;; Keywords: ido, completion, convenience
  10. ;; This file is NOT part of GNU Emacs.
  11. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  12. ;;
  13. ;;; Commentary:
  14. ;; If you use the excellent `ido-mode' for efficient completion of
  15. ;; file names and buffers, you might wonder if you can get ido-style
  16. ;; completion everywhere else too. Well, that's what this package
  17. ;; does! ido-ubiquitous is here to enable ido-style completion for
  18. ;; (almost) every function that uses the standard completion function
  19. ;; `completing-read'.
  20. ;; This package implements the `ido-completing-read+' function, which
  21. ;; is a wrapper for `ido-completing-read'. Importantly, it detects
  22. ;; edge cases that ordinary ido cannot handle and either adjusts them
  23. ;; so ido *can* handle them, or else simply falls back to Emacs'
  24. ;; standard completion instead. Hence, you can safely set
  25. ;; `completing-read-function' to `ido-completing-read+' without
  26. ;; worrying about breaking completion features that are incompatible
  27. ;; with ido.
  28. ;; To use this package, call `ido-ubiquitous-mode' to enable the mode,
  29. ;; or use `M-x customize-variable ido-ubiquitous-mode' it to enable it
  30. ;; permanently. Once the mode is enabled, most functions that use
  31. ;; `completing-read' will now have ido completion. If you decide in
  32. ;; the middle of a command that you would rather not use ido, just use
  33. ;; C-f or C-b at the end/beginning of the input to fall back to
  34. ;; non-ido completion (this is the same shortcut as when using ido for
  35. ;; buffers or files).
  36. ;; Note that `completing-read-default' is a very general function with
  37. ;; many complex behaviors that ido cannot emulate. This package
  38. ;; attempts to detect some of these cases and avoid using ido when it
  39. ;; sees them. So some functions will not have ido completion even when
  40. ;; this mode is enabled. Some other functions have ido disabled in
  41. ;; them because their packages already provide support for ido via
  42. ;; other means (for example, magit). See `M-x describe-variable
  43. ;; ido-cr+-function-blacklist' for more information.
  44. ;; ido-completing-read+ version 4.0 is a major update. The formerly
  45. ;; separate package ido-ubiquitous has been subsumed into
  46. ;; ido-completing-read+, so ido-ubiquitous 4.0 is just a wrapper that
  47. ;; loads ido-completing-read+ and displays a warning about being
  48. ;; obsolete. If you have previously customized ido-ubiquitous, be sure
  49. ;; to check out `M-x customize-group ido-completing-read-plus' after
  50. ;; updating to 4.0 and make sure the new settings are to your liking.
  51. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  52. ;;
  53. ;; This program is free software: you can redistribute it and/or modify
  54. ;; it under the terms of the GNU General Public License as published by
  55. ;; the Free Software Foundation, either version 3 of the License, or (at
  56. ;; your option) any later version.
  57. ;;
  58. ;; This program is distributed in the hope that it will be useful, but
  59. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  60. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  61. ;; General Public License for more details.
  62. ;;
  63. ;; You should have received a copy of the GNU General Public License
  64. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  65. ;;
  66. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  67. ;;
  68. ;;; Code:
  69. (defconst ido-completing-read+-version "4.0"
  70. "Currently running version of ido-completing-read+.
  71. Note that when you update ido-completing-read+, this variable may
  72. not be updated until you restart Emacs.")
  73. (require 'ido)
  74. (require 'cl-lib)
  75. (require 'cus-edit)
  76. ;;; Debug messages
  77. (define-minor-mode ido-cr+-debug-mode
  78. "If non-nil, ido-cr+ will print debug info.
  79. Debug info is printed to the *Messages* buffer."
  80. nil
  81. :global t
  82. :group 'ido-completing-read-plus)
  83. (defsubst ido-cr+--debug-message (format-string &rest args)
  84. (when ido-cr+-debug-mode
  85. (apply #'message (concat "ido-completing-read+: " format-string) args)))
  86. ;;; Core code
  87. (defvar ido-cur-list nil
  88. "Internal ido variable.
  89. This variable is originally declared in `ido.el', but it is not
  90. given a value (or a docstring). This documentation comes from a
  91. re-declaration in `ido-completing-read+.el' that initializes it
  92. to nil, which should suppress some byte-compilation warnings in
  93. Emacs 25. Setting another package's variable is not safe in
  94. general, but in this case it should be, because ido always
  95. let-binds this variable before using it, so the initial value
  96. shouldn't matter.")
  97. (defvar ido-cr+-minibuffer-depth -1
  98. "Minibuffer depth of the most recent ido-cr+ activation.
  99. If this equals the current minibuffer depth, then the minibuffer
  100. is currently being used by ido-cr+, and ido-cr+ feature will be
  101. active. Otherwise, something else is using the minibuffer and
  102. ido-cr+ features will be deactivated to avoid interfering with
  103. the other command.
  104. This is set to -1 by default, since `(minibuffer-depth)' should
  105. never return this value.")
  106. (defvar ido-cr+-assume-static-collection nil
  107. "If non-nil, ido-cr+ will assume that the collection is static.
  108. This is used to avoid unnecessary work in the case where the
  109. collection is a function, since a function collection could
  110. potentially change the set of completion candidates
  111. dynamically.")
  112. (defvar ido-cr+-current-command nil
  113. "Command most recently invoked by `call-interactively'.
  114. This is necessary because `command-execute' and
  115. `call-interactively' do not set `this-command'. Instead, the C
  116. code that calls `command-execute' sets it beforehand, so using
  117. either of those functions directly won't set `this-command'.")
  118. (defvar ido-cr+-dynamic-collection nil
  119. "Stores the collection argument if it is a function.
  120. This allows ido-cr+ to update the set of completion candidates
  121. dynamically.")
  122. (defvar ido-cr+-no-default-action 'prepend-empty-string
  123. "Controls the behavior of ido-cr+ when DEF is nil and REQUIRE-MATCH is non-nil.
  124. Possible values:
  125. - `prepend-empty-string': The empty string will be added to the
  126. front of COLLECTION, making it the default. This is the
  127. standard behavior since it mimics the semantics of
  128. `completing-read-default'.
  129. - `append-empty-string': The empty string will be added to the
  130. end of COLLECTION, thus keeping the original default while
  131. making the empty string available as a completion.
  132. - `nil': No action will be taken.
  133. - Any other value: The value will be interpreted as a 1-argument
  134. function, which will receive the current collection as its
  135. argument and return the collection with any necessary
  136. modifications applied.
  137. This is not meant to be set permanently, but rather let-bound
  138. before calling `ido-completing-read+' under controlled
  139. circumstances.")
  140. (defvar ido-cr+-orig-completing-read-args nil
  141. "Original arguments passed to `ido-completing-read+'.
  142. These are used for falling back to `completing-read-default'.")
  143. (defgroup ido-completing-read-plus nil
  144. "Extra features and compatibility for `ido-completing-read'."
  145. :group 'ido)
  146. (defcustom ido-cr+-fallback-function
  147. ;; Initialize to the current value of `completing-read-function',
  148. ;; unless that is already set to the ido completer, in which case
  149. ;; use `completing-read-default'.
  150. (if (memq completing-read-function
  151. '(ido-completing-read+
  152. ido-completing-read
  153. ;; Current ido-ubiquitous function
  154. completing-read-ido-ubiquitous
  155. ;; Old ido-ubiquitous functions that shouldn't be used
  156. completing-read-ido
  157. ido-ubiquitous-completing-read))
  158. 'completing-read-default
  159. completing-read-function)
  160. "Alternate completing-read function to use when ido is not wanted.
  161. This will be used for functions that are incompatible with ido
  162. or if ido cannot handle the completion arguments. It will also be
  163. used when the user requests non-ido completion manually via C-f
  164. or C-b."
  165. :type '(choice (const :tag "Standard emacs completion"
  166. completing-read-default)
  167. (function :tag "Other function"))
  168. :group 'ido-completing-read-plus)
  169. (defcustom ido-cr+-max-items 30000
  170. "Max collection size to use ido-cr+ on.
  171. If `ido-completing-read+' is called on a collection larger than
  172. this, the fallback completion method will be used instead. To
  173. disable fallback based on collection size, set this to nil."
  174. :type '(choice (const :tag "No limit" nil)
  175. (integer
  176. :tag "Limit" :value 30000
  177. :validate
  178. (lambda (widget)
  179. (let ((v (widget-value widget)))
  180. (if (and (integerp v)
  181. (> v 0))
  182. nil
  183. (widget-put widget :error "This field should contain a positive integer")
  184. widget)))))
  185. :group 'ido-completing-read-plus)
  186. (defcustom ido-cr+-function-blacklist
  187. '(read-file-name-internal
  188. read-buffer
  189. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/60
  190. todo-add-category
  191. ;; Gnus already supports ido on its own
  192. gnus-emacs-completing-read
  193. gnus-iswitchb-completing-read
  194. grep-read-files
  195. ;; Magit already supports ido on its own
  196. magit-builtin-completing-read
  197. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/39
  198. Info-read-node-name
  199. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/44
  200. tmm-prompt)
  201. "Functions & commands for which ido-cr+ should be disabled.
  202. Each entry can be either a symbol or a string. A symbol means to
  203. fall back specifically for the named function. A regular
  204. expression means to fall back for any function whose name matches
  205. that regular expression. When ido-cr+ is called through
  206. `completing-read', if any function in the call stack of the
  207. current command matches any of the blacklist entries, ido-cr+
  208. will be disabled for that command. Additionally, if the
  209. collection in the call to `completing-read' matches any of the
  210. blacklist entries, ido-cr+ will be disabled.
  211. Note that using specific function names is generally preferable
  212. to regular expressions, because the associated function
  213. definitions will be compared directly, so if the same function is
  214. called by another name, it should still trigger the fallback. For
  215. regular expressions, only name-based matching is possible."
  216. :group 'ido-completing-read-plus
  217. :type '(repeat (choice (symbol :tag "Function or command name")
  218. (string :tag "Regexp"))))
  219. (defcustom ido-cr+-function-whitelist
  220. nil
  221. "Functions & commands for which ido-cr+ should be enabled.
  222. If this variable is nil, the whitelist will not be used, and
  223. ido-cr+ will be allowed in all functions/commands not listed in
  224. `ido-cr+-function-backlist'.
  225. If this variable is non-nil, ido-cr+'s whitelisting mode will be
  226. enabled, and ido-cr+ will be disabled for *all* functions unless
  227. they match one of the entries. Matching is done in the same
  228. manner as `ido-cr+-function-blacklist', and blacklisting takes
  229. precedence over whitelisting."
  230. :group 'ido-completing-read-plus
  231. :type '(repeat (choice (symbol :tag "Function or command name")
  232. (string :tag "Regexp"))))
  233. ;;;###autoload
  234. (defcustom ido-cr+-replace-completely nil
  235. "If non-nil, replace `ido-completeing-read' completely with ido-cr+.
  236. Enabling this may interfere with or cause errors in other
  237. packages that use `ido-completing-read'. If you discover any such
  238. incompatibilities, please file a bug report at
  239. https://github.com/DarwinAwardWinner/ido-ubiquitous/issues"
  240. :type 'boolean)
  241. ;; Signal used to trigger fallback (don't use `define-error' because
  242. ;; it's only supported in 24.4 and up)
  243. (put 'ido-cr+-fallback 'error-conditions '(ido-cr+-fallback error))
  244. (put 'ido-cr+-fallback 'error-message "ido-cr+-fallback")
  245. (defsubst ido-cr+--explain-fallback (arg)
  246. ;; This function accepts a string, or an ido-cr+-fallback
  247. ;; signal.
  248. (when ido-cr+-debug-mode
  249. (when (and (listp arg)
  250. (eq (car arg) 'ido-cr+-fallback))
  251. (setq arg (cadr arg)))
  252. (ido-cr+--debug-message "Falling back to `%s' because %s."
  253. ido-cr+-fallback-function arg)))
  254. ;;;###autoload
  255. (defsubst ido-cr+-active ()
  256. "Returns non-nil if ido-cr+ is currently using the minibuffer."
  257. (>= ido-cr+-minibuffer-depth (minibuffer-depth)))
  258. (defsubst ido-cr+-default-was-provided ()
  259. "Returns non-nil if ido-cr+ was passed a non-nil default argument."
  260. (and (nth 6 ido-cr+-orig-completing-read-args)))
  261. (defun ido-cr+--called-from-completing-read ()
  262. "Returns non-nil if the most recent call to ido-cr+ was from `completing-read'."
  263. (equal (cadr (backtrace-frame 1 'ido-completing-read+))
  264. 'completing-read))
  265. (defun ido-cr+-function-is-blacklisted (fun)
  266. (cl-loop
  267. for entry in ido-cr+-function-blacklist
  268. if (cond
  269. ;; Nil: Never matches anything
  270. ((null entry)
  271. nil)
  272. ;; Symbol: Compare names and function definitions
  273. ((symbolp entry)
  274. (or (eq entry fun)
  275. (eq (indirect-function entry)
  276. (indirect-function fun))))
  277. ;; String: Do regexp matching against function name if it is a
  278. ;; symbol
  279. ((stringp entry)
  280. (and (symbolp fun)
  281. (string-match-p entry (symbol-name fun))))
  282. ;; Anything else: invalid blacklist entry
  283. (t
  284. (ido-cr+--debug-message "Ignoring invalid entry in ido-cr+-function-blacklist: `%S'" entry)
  285. nil))
  286. return entry
  287. ;; If no blacklist entry matches, return nil
  288. finally return nil))
  289. (defun ido-cr+-function-is-whitelisted (fun)
  290. (if (null ido-cr+-function-whitelist)
  291. ;; Empty whitelist means everything is whitelisted
  292. t
  293. (cl-loop
  294. for entry in ido-cr+-function-whitelist
  295. if (cond
  296. ;; Nil: Never matches anything
  297. ((null entry)
  298. nil)
  299. ;; Symbol: Compare names and function definitions
  300. ((symbolp entry)
  301. (or (eq entry fun)
  302. (eq (indirect-function entry)
  303. (indirect-function fun))))
  304. ;; String: Do regexp matching against function name if it is a
  305. ;; symbol
  306. ((stringp entry)
  307. (and (symbolp fun)
  308. (string-match-p entry (symbol-name fun))))
  309. ;; Anything else: invalid whitelist entry
  310. (t
  311. (ido-cr+--debug-message "Ignoring invalid entry in ido-cr+-function-whitelist: `%S'" entry)
  312. nil))
  313. return entry
  314. ;; If no whitelist entry matches, return nil
  315. finally return nil)))
  316. ;;;###autoload
  317. (defun ido-completing-read+ (prompt collection &optional predicate
  318. require-match initial-input
  319. hist def inherit-input-method)
  320. "ido-based method for reading from the minibuffer with completion.
  321. See `completing-read' for the meaning of the arguments.
  322. This function is a wrapper for `ido-completing-read' designed to
  323. be used as the value of `completing-read-function'. Importantly,
  324. it detects edge cases that ido cannot handle and uses normal
  325. completion for them."
  326. (let (;; Save the original arguments in case we need to do the
  327. ;; fallback
  328. (ido-cr+-orig-completing-read-args
  329. (list prompt collection predicate require-match
  330. initial-input hist def inherit-input-method))
  331. ;; Need to save this since activating the minibuffer once will
  332. ;; clear out any temporary minibuffer hooks, which need to get
  333. ;; restored before falling back.
  334. (orig-minibuffer-setup-hook minibuffer-setup-hook)
  335. ;; If collection is a function, save it for later, unless
  336. ;; instructed not to
  337. (ido-cr+-dynamic-collection
  338. (when (and (not ido-cr+-assume-static-collection)
  339. (functionp collection))
  340. collection))
  341. ;; If the whitelist is empty, everything is whitelisted
  342. (whitelisted (not ido-cr+-function-whitelist)))
  343. (condition-case sig
  344. (progn
  345. ;; Check a bunch of fallback conditions
  346. (when inherit-input-method
  347. (signal 'ido-cr+-fallback
  348. '("ido cannot handle non-nil INHERIT-INPUT-METHOD")))
  349. (when (bound-and-true-p completion-extra-properties)
  350. (signal 'ido-cr+-fallback
  351. '("ido cannot handle non-nil `completion-extra-properties'")))
  352. ;; Check for black/white-listed collection function
  353. (when (functionp collection)
  354. ;; Blacklist
  355. (when (ido-cr+-function-is-blacklisted collection)
  356. (if (symbolp collection)
  357. (signal 'ido-cr+-fallback
  358. (list (format "collection function `%S' is blacklisted" collection)))
  359. (signal 'ido-cr+-fallback
  360. (list "collection function is blacklisted"))))
  361. ;; Whitelist
  362. (when (and (not whitelisted)
  363. (ido-cr+-function-is-whitelisted collection))
  364. (ido-cr+--debug-message
  365. (if (symbolp collection)
  366. (format "Collection function `%S' is whitelisted" collection)
  367. "Collection function is whitelisted"))
  368. (setq whitelisted t)))
  369. ;; Expand all currently-known completions.
  370. (setq collection (all-completions "" collection predicate))
  371. ;; No point in using ido unless there's a collection
  372. (when (and (= (length collection) 0)
  373. (not ido-cr+-dynamic-collection))
  374. (signal 'ido-cr+-fallback '("ido is not needed for an empty collection")))
  375. ;; Check for excessively large collection
  376. (when (and ido-cr+-max-items
  377. (> (length collection) ido-cr+-max-items))
  378. (signal 'ido-cr+-fallback
  379. (list
  380. (format
  381. "there are more than %i items in COLLECTION (see `ido-cr+-max-items')"
  382. ido-cr+-max-items))))
  383. ;; If called from `completing-read', check for
  384. ;; black/white-listed commands/callers
  385. (when (ido-cr+--called-from-completing-read)
  386. ;; Check calling command
  387. (when (ido-cr+-function-is-blacklisted this-command)
  388. (signal 'ido-cr+-fallback
  389. (list "calling command `%S' is blacklisted" this-command)))
  390. (when (and (not whitelisted)
  391. (ido-cr+-function-is-whitelisted this-command))
  392. (ido-cr+--debug-message "Command `%S' is whitelisted" this-command)
  393. (setq whitelisted t))
  394. ;; Also need to check `ido-cr+-current-command'
  395. (when (ido-cr+-function-is-blacklisted ido-cr+-current-command)
  396. (signal 'ido-cr+-fallback
  397. (list "calling command `%S' is blacklisted" ido-cr+-current-command)))
  398. (when (and (not whitelisted)
  399. (ido-cr+-function-is-whitelisted ido-cr+-current-command))
  400. (ido-cr+--debug-message "Command `%S' is whitelisted" ido-cr+-current-command)
  401. (setq whitelisted t))
  402. ;; Check every function in the call stack starting after
  403. ;; `completing-read' until to the first
  404. ;; `funcall-interactively' (for a call from the function
  405. ;; body) or `call-interactively' (for a call from the
  406. ;; interactive form, in which the function hasn't actually
  407. ;; been called yet, so `funcall-interactively' won't be on
  408. ;; the stack.)
  409. (cl-loop for i upfrom 1
  410. for caller = (cadr (backtrace-frame i 'completing-read))
  411. while caller
  412. while (not (memq (indirect-function caller)
  413. '(internal--funcall-interactively
  414. (indirect-function 'call-interactively))))
  415. if (ido-cr+-function-is-blacklisted caller)
  416. do (signal 'ido-cr+-fallback
  417. (list (if (symbolp caller)
  418. (format "calling function `%S' is blacklisted" caller)
  419. "a calling function is blacklisted")))
  420. if (and (not whitelisted)
  421. (ido-cr+-function-is-whitelisted caller))
  422. do (progn
  423. (ido-cr+--debug-message
  424. (if (symbolp caller)
  425. (format "Calling function `%S' is whitelisted" caller)
  426. "A calling function is whitelisted"))
  427. (setq whitelisted t))))
  428. (unless whitelisted
  429. (signal 'ido-cr+-fallback
  430. (list "no functions or commands matched the whitelist for this call")))
  431. ;; In ido, the semantics of "default" are simply "put it at
  432. ;; the front of the list". Furthermore, ido has certain
  433. ;; issues with a non-nil DEF arg. Specifically, it can't
  434. ;; handle list defaults or providing both DEF and
  435. ;; INITIAL-INPUT. So, just pre-process the collection to put
  436. ;; the default(s) at the front and then set DEF to nil in
  437. ;; the call to ido to avoid these issues.
  438. (unless (listp def)
  439. ;; Ensure DEF is a list
  440. (setq def (list def)))
  441. (when def
  442. (setq collection (append def (cl-set-difference collection def
  443. :test #'equal))
  444. def nil))
  445. ;; If DEF was nil and REQUIRE-MATCH was non-nil, then we need to
  446. ;; add the empty string as the first option, because RET on
  447. ;; an empty input needs to return "". (Or possibly we need
  448. ;; to take some other action based on the value of
  449. ;; `ido-cr+-no-default-action'.)
  450. (when (and require-match
  451. ido-cr+-no-default-action
  452. (not (ido-cr+-default-was-provided)))
  453. (cl-case ido-cr+-no-default-action
  454. (nil
  455. ;; Take no action
  456. t)
  457. (prepend-empty-string
  458. (ido-cr+--debug-message "Adding \"\" as the default completion since no default was provided.")
  459. (setq collection (cons "" collection)))
  460. (append-empty-string
  461. (ido-cr+--debug-message "Adding \"\" as a completion option since no default was provided.")
  462. (setq collection (append collection '(""))))
  463. (otherwise
  464. (ido-cr+--debug-message "Running custom action function since no default was provided.")
  465. (setq collection (funcall ido-cr+-no-default-action collection)))))
  466. ;; Check for a specific bug
  467. (when (and ido-enable-dot-prefix
  468. (version< emacs-version "26.1")
  469. (member "" collection))
  470. (signal 'ido-cr+-fallback
  471. '("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")))
  472. ;; Finally ready to do actual ido completion
  473. (prog1
  474. (let ((ido-cr+-minibuffer-depth (1+ (minibuffer-depth)))
  475. ;; Reset these for the next call to ido-cr+
  476. (ido-cr+-no-default-action 'prepend-empty-string)
  477. (ido-cr+-assume-static-collection nil))
  478. (ido-completing-read
  479. prompt collection
  480. predicate require-match initial-input hist def
  481. inherit-input-method))
  482. ;; This detects when the user triggered fallback mode
  483. ;; manually.
  484. (when (eq ido-exit 'fallback)
  485. (signal 'ido-cr+-fallback '("user manually triggered fallback")))))
  486. ;; Handler for ido-cr+-fallback signal
  487. (ido-cr+-fallback
  488. (let (;; Reset `minibuffer-setup-hook' to original value
  489. (minibuffer-setup-hook orig-minibuffer-setup-hook)
  490. ;; Reset these for the next call to ido-cr+
  491. (ido-cr+-no-default-action 'prepend-empty-string)
  492. (ido-cr+-assume-static-collection nil))
  493. (ido-cr+--explain-fallback sig)
  494. (apply ido-cr+-fallback-function ido-cr+-orig-completing-read-args))))))
  495. ;;;###autoload
  496. (defadvice ido-completing-read (around ido-cr+ activate)
  497. "This advice is the implementation of `ido-cr+-replace-completely'."
  498. ;; If this advice is autoloaded, then we need to force loading of
  499. ;; the rest of the file so all the variables will be defined.
  500. (when (not (featurep 'ido-completing-read+))
  501. (require 'ido-completing-read+))
  502. (if (or (ido-cr+-active)
  503. (not ido-cr+-replace-completely))
  504. ;; ido-cr+ has either already activated or isn't going to
  505. ;; activate, so just run the function as normal
  506. ad-do-it
  507. ;; Otherwise, we need to activate ido-cr+.
  508. (setq ad-return-value (apply #'ido-completing-read+ (ad-get-args 0)))))
  509. ;;;###autoload
  510. (defadvice call-interactively (around ido-cr+-record-command-name activate)
  511. "Record the command being interactively called.
  512. See `ido-cr+-current-command'."
  513. (let ((ido-cr+-current-command (ad-get-arg 0)))
  514. ad-do-it))
  515. ;; Fallback on magic C-f and C-b
  516. ;;;###autoload
  517. (defvar ido-context-switch-command nil
  518. "Variable holding the command used for switching to another completion mode.
  519. This variable is originally declared in `ido.el', but it is not
  520. given a value (or a docstring). This documentation comes from a
  521. re-declaration in `ido-completing-read+.el' that initializes it
  522. to nil, which should suppress some byte-compilation warnings in
  523. Emacs 25. Setting another package's variable is not safe in
  524. general, but in this case it should be, because ido always
  525. let-binds this variable before using it, so the initial value
  526. shouldn't matter.")
  527. (defadvice ido-magic-forward-char (before ido-cr+-fallback activate)
  528. "Allow falling back in ido-completing-read+."
  529. (when (ido-cr+-active)
  530. ;; `ido-context-switch-command' is already let-bound at this
  531. ;; point.
  532. (setq ido-context-switch-command #'ido-fallback-command)))
  533. (defadvice ido-magic-backward-char (before ido-cr+-fallback activate)
  534. "Allow falling back in ido-completing-read+."
  535. (when (ido-cr+-active)
  536. ;; `ido-context-switch-command' is already let-bound at this
  537. ;; point.
  538. (setq ido-context-switch-command #'ido-fallback-command)))
  539. (defadvice ido-select-text (around fix-require-match-behavior activate)
  540. "Fix ido behavior when `require-match' is non-nil.
  541. Standard ido will allow C-j to exit with an incomplete completion
  542. even when `require-match' is non-nil. Ordinary completion does
  543. not allow this. In ordinary completion, RET on an incomplete
  544. match is equivalent to TAB, and C-j selects the first match.
  545. Since RET in ido already selects the first match, this advice
  546. sets up C-j to be equivalent to TAB in the same situation."
  547. (if (and
  548. ;; Only override C-j behavior if...
  549. ;; We're using ico-cr+
  550. (ido-cr+-active)
  551. ;; Require-match is non-nil
  552. (with-no-warnings ido-require-match)
  553. ;; Current text is not a complete choice
  554. (not (member ido-text (with-no-warnings ido-cur-list))))
  555. (progn
  556. (ido-cr+--debug-message
  557. "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)")
  558. (ido-complete))
  559. ad-do-it))
  560. (defadvice ido-exhibit (before ido-cr+-update-dynamic-collection activate)
  561. "Maybe update the set of completions when ido-text changes."
  562. (when ido-cr+-dynamic-collection
  563. (let ((prev-ido-text ido-text)
  564. (current-ido-text (buffer-substring-no-properties (minibuffer-prompt-end) (point-max))))
  565. (when (not (string= prev-ido-text current-ido-text))
  566. (let ((current-match (car ido-matches))
  567. (def (nth 6 ido-cr+-orig-completing-read-args))
  568. (predicate (nth 2 ido-cr+-orig-completing-read-args)))
  569. (setq ido-cur-list
  570. (delete-dups
  571. (append
  572. (all-completions
  573. current-ido-text
  574. ido-cr+-dynamic-collection
  575. predicate)
  576. (all-completions
  577. ""
  578. ido-cr+-dynamic-collection
  579. predicate))))
  580. (unless (listp def)
  581. (setq def (list def)))
  582. (when def
  583. (setq ido-cur-list
  584. (append def (cl-set-difference ido-cur-list def
  585. :test #'equal))))
  586. (when (and current-match (member current-match ido-cur-list))
  587. (setq ido-cur-list (ido-chop ido-cur-list current-match))))
  588. (ido-cr+--debug-message "Updated completion candidates for dynamic collection because `ido-text' changed from %S to %S. `ido-cur-list' now has %s elements" prev-ido-text current-ido-text (length ido-cur-list))))))
  589. ;; Interoperation with minibuffer-electric-default-mode: only show the
  590. ;; default when the input is empty and the empty string is the selected
  591. (defadvice minibuf-eldef-update-minibuffer (around ido-cr+-compat activate)
  592. "This advice allows minibuffer-electric-default-mode to work with ido-cr+."
  593. (if (ido-cr+-active)
  594. (unless (eq minibuf-eldef-showing-default-in-prompt
  595. (and (string= (car ido-cur-list) "")
  596. (string= ido-text "")))
  597. ;; Swap state.
  598. (setq minibuf-eldef-showing-default-in-prompt
  599. (not minibuf-eldef-showing-default-in-prompt))
  600. (overlay-put minibuf-eldef-overlay 'invisible
  601. (not minibuf-eldef-showing-default-in-prompt)))
  602. ad-do-it))
  603. ;;;###autoload
  604. (define-minor-mode ido-ubiquitous-mode
  605. "Use ido completion instead of standard completion almost everywhere.
  606. If this mode causes problems for a function, you can customize
  607. when ido completion is or is not used by customizing
  608. `ido-cr+-function-blacklist'."
  609. nil
  610. :global t
  611. :group 'ido-completing-read-plus
  612. ;; Actually enable/disable the mode by setting
  613. ;; `completing-read-function'.
  614. (setq completing-read-function
  615. (if ido-ubiquitous-mode
  616. #'ido-completing-read+
  617. ido-cr+-fallback-function)))
  618. (defcustom ido-cr+-auto-update-blacklist 'notify
  619. "Whether to add new overrides when updating ido-cr+.
  620. This variable has 3 possible values, with the following meanings:
  621. `t': Auto-update the blacklist
  622. `notify': Notify you about updates but do not apply them
  623. `nil': Ignore all blacklist updates
  624. Ido-cr+ comes with a default blacklist for commands that are
  625. known to be incompatible with ido completion. New versions of
  626. ido-cr+ may come with updates to this blacklist as more
  627. incompatible commands are discovered. However, customizing your
  628. own overrides would normally prevent you from receiving these
  629. updates, since Emacs will not overwrite your customizations.
  630. To resolve this problem, you can set this variable to `t', and
  631. then ido-cr+ can automatically add any new built-in overrides
  632. whenever it is updated. (Actually, the update will happen the
  633. next time Emacs is restarted after the update.) This allows you
  634. to add your own overrides but still receive updates to the
  635. default set.
  636. If you want ido-cr+ to just notify you about new default
  637. overrides instead of adding them itself, set this variable to
  638. `notify'. If you don't want this auto-update behavior at all, set
  639. it to `nil'.
  640. (Note that having this option enabled effectively prevents you
  641. from removing any of the built-in default blacklist entries,
  642. since they will simply be re-added the next time Emacs starts.)"
  643. :type '(choice :tag "When new overrides are available:"
  644. (const :menu-tag "Auto-add"
  645. :tag "Add them automatically"
  646. t)
  647. (const :menu-tag "Notify"
  648. :tag "Notify me about them"
  649. notify)
  650. (const :menu-tag "Ignore"
  651. :tag "Ignore them"
  652. nil))
  653. :group 'ido-completing-read-plus)
  654. (defun ido-cr+-update-blacklist (&optional save quiet)
  655. "Re-add any missing default blacklist entries.
  656. This is useful after an update of ido-ubiquitous that adds new
  657. default overrides. See `ido-cr+-auto-update-blacklist' for more
  658. information.
  659. If SAVE is non-nil, also save the new blacklist to the user's
  660. Custom file (but only if it was already customized beforehand).
  661. When called interactively, a prefix argument triggers a save.
  662. When called from Lisp code, this function returns non-nil if the
  663. blacklist was modified."
  664. (interactive "P")
  665. (let* ((var-state (custom-variable-state 'ido-cr+-function-blacklist
  666. ido-cr+-function-blacklist))
  667. (curval ido-cr+-function-blacklist)
  668. (defval (eval (car (get 'ido-cr+-function-blacklist 'standard-value))))
  669. (newval (delete-dups (append defval curval)))
  670. (new-entries (cl-set-difference curval defval :test #'equal))
  671. (modified nil)
  672. (saved nil)
  673. (message-lines ()))
  674. (cl-case var-state
  675. (standard
  676. ;; Var is not customized, just set the new default
  677. (ido-cr+--debug-message "Blacklist was not customized, so it has been updated to the new default value.")
  678. (setq ido-cr+-function-blacklist defval
  679. modified new-entries))
  680. ((saved set changed)
  681. ;; Var has been customized and saved by the user, so set the
  682. ;; new value and maybe save it
  683. (ido-cr+--debug-message "Updating user-customized blacklist with new default entries.")
  684. (setq ido-cr+-function-blacklist newval
  685. modified t)
  686. (when (and save (eq var-state 'saved))
  687. (ido-cr+--debug-message "Saving new blacklist value to Custom file.")
  688. (customize-save-variable 'ido-cr+-function-blacklist ido-cr+-function-blacklist)
  689. (setq saved t)))
  690. (otherwise
  691. (ido-cr+--debug-message "Customization status of blacklist is unknown. Not modifying it.")))
  692. (if (and modified (not quiet))
  693. (progn
  694. (push (format "Added the following entries to `ido-cr+-function-blacklist': %S" new-entries)
  695. message-lines)
  696. (if saved
  697. (push "Saved the new value of `ido-cr+-function-blacklist' to your Custom file."
  698. message-lines)
  699. (push "Hoever, 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'."
  700. message-lines)))
  701. (push "No updates were required to `ido-cr+-function-blacklist'." message-lines))
  702. (unless quiet
  703. (message (mapconcat #'identity (nreverse message-lines) "\n")))
  704. modified))
  705. (defun ido-cr+-maybe-update-blacklist ()
  706. "Maybe call `ico-cr+-update-blacklist.
  707. See `ido-cr+-auto-update-blacklist' for more information."
  708. (if ido-cr+-auto-update-blacklist
  709. (let* ((curval ido-cr+-function-blacklist)
  710. (defval (eval (car (get 'ido-cr+-function-blacklist 'standard-value))))
  711. (new-entries (cl-set-difference curval defval :test #'equal)))
  712. (if new-entries
  713. (if (eq ido-cr+-auto-update-blacklist 'notify)
  714. (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.)")
  715. (ido-cr+--debug-message "Initiating blacklist update.")
  716. (ido-cr+-update-blacklist t))
  717. (ido-cr+--debug-message "No blacklist updates available.")))
  718. (ido-cr+--debug-message "Skipping blacklist update by user request.")))
  719. (ido-cr+-maybe-update-blacklist)
  720. (provide 'ido-completing-read+)
  721. ;;; ido-completing-read+.el ends here