ido-completing-read+.el 33 KB

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