ido-completing-read+.el 33 KB

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