ido-completing-read+.el 35 KB

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