ido-completing-read+.el 33 KB

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