ido-ubiquitous.el 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. ;;; ido-ubiquitous.el --- Use ido (nearly) everywhere.
  2. ;; Author: Ryan C. Thompson
  3. ;; URL: https://github.com/DarwinAwardWinner/ido-ubiquitous
  4. ;; Version: 2.0
  5. ;; Created: 2011-09-01
  6. ;; Keywords: convenience
  7. ;; EmacsWiki: InteractivelyDoThings
  8. ;; This file is NOT part of GNU Emacs.
  9. ;;; Commentary:
  10. ;; You may have seen the `ido-everywhere' variable in ido.el and got
  11. ;; excited that you could use ido completion for everything. Then you
  12. ;; were probably disappointed when you realized that it only applied
  13. ;; to *file names* and nothing else. Well, ido-ubiquitous is here to
  14. ;; fulfill the original promise and let you use ido completion for
  15. ;; (almost) any command that uses `completing-read' to offer you a
  16. ;; choice of several alternatives.
  17. ;; This even works in M-x, but for that, you might prefer the "smex"
  18. ;; package instead.
  19. ;; As of version 0.7, this package also makes a small modification to
  20. ;; ido's behavior so as to support a strange corner case of
  21. ;; `completing-read' that some functions rely on. Since the goal of
  22. ;; this package is to replace `completing-read' everywhere instead of
  23. ;; just selectively (as ido itself does), compatibility with all the
  24. ;; quriks of `completing-read' is important here.
  25. ;; If you find a case where enabling ido-ubiquitous causes a command
  26. ;; not to work correctly, please report it by creating an issue on
  27. ;; GitHub: https://github.com/DarwinAwardWinner/ido-ubiquitous/issues
  28. ;;; License:
  29. ;; This program is free software; you can redistribute it and/or modify
  30. ;; it under the terms of the GNU General Public License as published by
  31. ;; the Free Software Foundation; either version 3, or (at your option)
  32. ;; any later version.
  33. ;;
  34. ;; This program is distributed in the hope that it will be useful,
  35. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. ;; GNU General Public License for more details.
  38. ;;
  39. ;; You should have received a copy of the GNU General Public License
  40. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  41. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  42. ;; Boston, MA 02110-1301, USA.
  43. ;;; Code:
  44. ;; Check for required feature
  45. (eval-when-compile
  46. (when (not (boundp 'completing-read-function))
  47. (error "Could not find variable `completing-read-function'. Are you using Emacs version 24 or higher? If you have Emacs 23 or lower, please downgrade to ido-ubiquitous version 1.6.")))
  48. (require 'ido)
  49. (require 'advice)
  50. (require 'cl)
  51. ;;; Custom Declarations
  52. ;;;###autoload
  53. (defgroup ido-ubiquitous nil
  54. "Use ido for (almost) all completion."
  55. :group 'ido)
  56. ;;;###autoload
  57. (define-minor-mode ido-ubiquitous-mode
  58. "Use `ido-completing-read' instead of `completing-read' almost everywhere.
  59. This mode has no effect unles `ido-mode' is also enabled.
  60. If this mode causes problems for a function, you can force the
  61. function to use the original completing read by using the macro
  62. `ido-ubiquitous-disable-in'. For example, if a
  63. function `foo' cannot work with ido-style completion, evaluate
  64. the following (for example by putting it in your .emacs file):
  65. (ido-ubiquitous-disable-in foo)"
  66. nil
  67. :global t
  68. :group 'ido-ubiquitous
  69. ;; Handle warning about ido disabled
  70. (when ido-ubiquitous-mode
  71. (unless (bound-and-true-p ido-mode)
  72. (if after-init-time
  73. (ido-ubiquitous-warn-about-ido-disabled)
  74. (add-hook 'after-init-hook 'ido-ubiquitous-warn-about-ido-disabled))))
  75. ;; Ensure emacs 23 code disabled
  76. (ignore-errors
  77. (ad-disable-advice 'completing-read 'around 'ido-ubiquitous-legacy)
  78. (ad-activate 'completing-read))
  79. ;; Actually enable/disable the mode
  80. (setq completing-read-function
  81. (if ido-ubiquitous-mode
  82. 'completing-read-ido
  83. ido-ubiquitous-fallback-completing-read-function)))
  84. ;;;###autoload
  85. (define-obsolete-variable-alias 'ido-ubiquitous
  86. 'ido-ubiquitous-mode "0.8")
  87. ;;;###autoload
  88. (define-obsolete-function-alias 'ido-ubiquitous
  89. 'ido-ubiquitous-mode "0.8")
  90. ;;;###autoload
  91. (defcustom ido-ubiquitous-fallback-completing-read-function
  92. ;; Initialize to the current value of `completing-read-function',
  93. ;; unless that is already set to the ido completer, in which case
  94. ;; use `completing-read-default'.
  95. (if (eq completing-read-function 'completing-read-ido)
  96. 'completing-read-default
  97. completing-read-function)
  98. "Alternate completing-read function to use when ido is not wanted.
  99. This will be used for functions that are incompatibile with ido
  100. or if ido cannot handle the completion arguments.
  101. If you turn off ido-ubiquitous mode, `completing-read-function'
  102. will be set to this."
  103. :type '(choice (const :tag "Standard emacs completion"
  104. completing-read-default)
  105. (function :tag "Other function"))
  106. :group 'ido-ubiquitous)
  107. ;;;###autoload
  108. (defcustom ido-ubiquitous-command-exceptions '()
  109. "List of commands that should not be affected by `ido-ubiquitous'.
  110. Even when `ido-ubiquitous' mode is enabled, these commands will
  111. continue to use `completing-read' instead of
  112. `ido-completing-read'.
  113. Only *interactive* commands should go here. To disable
  114. ido-ubiquitous in non-interactive functions, customize
  115. `ido-ubiquitous-function-exceptions'."
  116. ;; This isn't actually a hook, but it is a list of functions, so
  117. ;; that's close enough.
  118. :type 'hook
  119. :group 'ido-ubiquitous)
  120. ;;;###autoload
  121. (define-obsolete-variable-alias 'ido-ubiquitous-exceptions
  122. 'ido-ubiquitous-command-exceptions "0.4")
  123. (defvar ido-ubiquitous-default-function-exceptions
  124. '(read-file-name
  125. read-file-name-internal
  126. read-buffer
  127. gnus-emacs-completing-read
  128. gnus-iswitchb-completing-read
  129. man
  130. grep-read-files)
  131. "Default value of `ido-ubiquitous-function-exceptions'")
  132. (defun ido-ubiquitous-set-function-exceptions (sym newval)
  133. ;; Loop through all functions, enabling or disabling ido-ubiquitous
  134. ;; as appropriate.
  135. (mapatoms
  136. (lambda (sym)
  137. (if (memq sym newval)
  138. (eval `(ido-ubiquitous-disable-in ,sym))
  139. (eval `(ido-ubiquitous-enable-in ,sym)))))
  140. ;; Set the new value
  141. (set-default sym newval))
  142. (defmacro ido-ubiquitous-disable-in (func)
  143. "Disable ido-ubiquitous in FUNC."
  144. (put func 'ido-ubiquitous-disable t)
  145. (let ((docstring
  146. (format "Disable ido-ubiquitous in %s if its `idu-ubiquitous-disable' property is non-nil." func)))
  147. `(defadvice ,func (around disable-ido-ubiquitous activate)
  148. ,docstring
  149. (let ((ido-ubiquitous-mode
  150. (and ido-ubiquitous-mode
  151. (not (get ',func 'ido-ubiquitous-disable)))))
  152. ad-do-it))))
  153. (define-obsolete-function-alias
  154. 'disable-ido-ubiquitous-in
  155. 'ido-ubiquitous-disable-in
  156. "0.4")
  157. (defmacro ido-ubiquitous-enable-in (func)
  158. "Re-enable ido-ubiquitous in FUNC.
  159. This reverses the effect of a previous call to
  160. `ido-ubiquitous-disable-in' (if no such call was made, this is
  161. a no-op.)"
  162. `(when (get ',func 'ido-ubiquitous-disable)
  163. (put ,func 'ido-ubiquitous-disable nil)))
  164. (define-obsolete-function-alias
  165. 'enable-ido-ubiquitous-in
  166. 'ido-ubiquitous-enable-in
  167. "0.4")
  168. ;;;###autoload
  169. (defcustom ido-ubiquitous-function-exceptions
  170. ido-ubiquitous-default-function-exceptions
  171. "List of functions in which to disable ido-ubiquitous.
  172. If you need to add a function to this list, please also file a
  173. bug report at
  174. https://github.com/DarwinAwardWinner/ido-ubiquitous/issues
  175. Setting this variable directly has no effect. You must set it
  176. through Customize."
  177. :group 'ido-ubiquitous
  178. :type 'hook
  179. :options '(read-file-name
  180. read-file-name-internal
  181. read-buffer
  182. gnus-emacs-completing-read
  183. gnus-iswitchb-completing-read
  184. man
  185. grep-read-files)
  186. :set 'ido-ubiquitous-set-function-exceptions)
  187. ;;; Ido-ubiquitous core
  188. (defvar ido-ubiquitous-next-call-replaces-completing-read nil
  189. "If t, then the next call to ido-completing-read is by ido-ubiquitous.")
  190. (defvar ido-ubiquitous-this-call-replaces-completing-read nil
  191. "If t, then the current call to ido-completing-read is by ido-ubiquitous.")
  192. (defvar ido-ubiquitous-disable-for-one-command nil
  193. "If t, disable ido-ubiquitous for the next command.")
  194. (defun completing-read-ido (prompt collection &optional predicate
  195. require-match initial-input
  196. hist def inherit-input-method)
  197. "Ido-based method for reading from the minibuffer with completion.
  198. See `completing-read' for the meaning of the arguments.
  199. This function is a wrapper for `ido-completing-read' designed to
  200. be used as the value of `completing-read-function'."
  201. (let ((comp-read-fun
  202. (cond
  203. (ido-ubiquitous-disable-for-one-command
  204. (prog1
  205. ido-ubiquitous-fallback-completing-read-function
  206. (setq ido-ubiquitous-disable-for-one-command nil)))
  207. ((or inherit-input-method ; Can't handle this arg
  208. (bound-and-true-p completion-extra-properties) ; Can't handle this
  209. (not ido-mode)
  210. (not ido-ubiquitous-mode)
  211. (memq this-command ido-ubiquitous-command-exceptions))
  212. ido-ubiquitous-fallback-completing-read-function)
  213. ((let ((allcomp (all-completions "" collection predicate)))
  214. ;; Only use ido completion if there are actually any completions
  215. ;; to offer.
  216. (if allcomp
  217. (prog1 'ido-ubiquitous-completing-read
  218. (setq collection allcomp
  219. predicate nil))
  220. ido-ubiquitous-fallback-completing-read-function))))))
  221. (funcall comp-read-fun
  222. prompt collection predicate
  223. require-match initial-input
  224. hist def inherit-input-method)))
  225. (defun ido-ubiquitous-completing-read (&rest args)
  226. "Wrapper for `ido-completing-read' that enables ido-ubiquitous features."
  227. (let ((ido-ubiquitous-next-call-replaces-completing-read t))
  228. (apply 'ido-completing-read args)))
  229. (defadvice ido-completing-read (around detect-replacing-cr activate)
  230. "Detect whether this call was done through ido-ubiquitous.
  231. If so enable, extra features for ido-ubiquitous that handle
  232. special cases that `ido-completing-read' needs to handle to more
  233. fully emulate `completing-read'. This allows ido-ubiquitous extra
  234. features to avoid interfering with the normal operation of ido."
  235. (let* ((ido-ubiquitous-this-call-replaces-completing-read ido-ubiquitous-next-call-replaces-completing-read)
  236. (ido-ubiquitous-next-call-replaces-completing-read nil))
  237. (when ido-ubiquitous-this-call-replaces-completing-read
  238. ;; If DEF is a list, prepend it to CHOICES and set DEF to just the
  239. ;; car of the default list.
  240. (when (and def (listp def))
  241. (setq choices (delete-dups (append def choices))
  242. def (car def)))
  243. ;; Work around a bug in ido when both INITIAL-INPUT and DEF are provided
  244. ;; More info: https://github.com/technomancy/ido-ubiquitous/issues/18
  245. (let ((initial (cond ((null initial-input) "")
  246. ((stringp initial-input) initial-input)
  247. ((consp initial-input) (car initial-input))
  248. (t initial-input)))
  249. (deflist (if (listp def)
  250. def
  251. (list def))))
  252. (when (and deflist initial
  253. (stringp initial)
  254. (not (string= initial "")))
  255. ;; Both default and initial input were provided. So keep the
  256. ;; initial input and preprocess the choices list to put the
  257. ;; default at the head, then proceed with default = nil.
  258. (setq choices (delete-dups (append deflist (remove def choices)))
  259. def nil))))
  260. ad-do-it))
  261. ;;; Ido-ubiquitous interactive command exceptions
  262. ;; The following advices should allow ido-ubiquitous to apply to the
  263. ;; interactive forms of commands as well as their bodies.
  264. (defadvice call-interactively (around ido-ubiquitous-command-exceptions activate)
  265. (let ((ido-ubiquitous-disable-for-one-command
  266. (memq function ido-ubiquitous-command-exceptions)))
  267. ad-do-it))
  268. (defadvice command-execute (around ido-ubiquitous-command-exceptions activate)
  269. (let ((ido-ubiquitous-disable-for-one-command
  270. (and (commandp cmd t)
  271. (memq cmd ido-ubiquitous-command-exceptions))))
  272. ad-do-it))
  273. ;;; Ido-ubiquitous function exceptions
  274. ;;; Ido-ubiquitous compatibility with old completing-read
  275. ;;;###autoload
  276. (defgroup ido-ubiquitous-compatibility nil
  277. "Use ido for (almost) all completion."
  278. :group 'ido-ubiquitous)
  279. (defcustom ido-ubiquitous-enable-compatibility-globally t
  280. "Allow ido to emulate a quirk of `completing-read'.
  281. From the `completing-read' docstring:
  282. > If the input is null, `completing-read' returns DEF, or the
  283. > first element of the list of default values, or an empty string
  284. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  285. If this variable is non-nil, then ido-ubiquitous will attempt to
  286. emulate this behavior. Specifically, if RET is pressed
  287. immediately upon entering completion, an empty string will be
  288. returned instead of the first element in the list. This behavior
  289. is only enabled when ido is being used as a substitute for
  290. `completing-read', and not when it is used directly.
  291. This odd behavior is required for compatibility with an old-style
  292. usage pattern whereby the default was requested by returning an
  293. empty string. In this mode, the caller receives the empty string
  294. and handles the default case manually, while `completing-read'
  295. never has any knowledge of the default. This is a problem for
  296. ido, which always returns the first element in the list when the
  297. input is empty. Without knowledge of the default, it cannot
  298. ensure that the default is first on the list, so returning the
  299. first item is not the correct behavior. Instead, it must return
  300. an empty string like `completing-read'.
  301. When this mode is enabled, you can still select the first item on
  302. the list by prefixing \"RET\" with \"C-u\".
  303. If you want to enable compatibility selectively for specific
  304. commands or functions, see the other options in the
  305. `ido-ubiquitous-compatibility' group."
  306. :type 'boolean
  307. :group 'ido-ubiquitous-compatibility)
  308. ;;;###autoload
  309. (defcustom ido-ubiquitous-command-compatibility-list '()
  310. "List of commands in which to enable old-style compatibility.
  311. See `ido-ubiquitous-enable-compatibility-globally' for a
  312. description of the compatibility behavior. If ido doesn't
  313. properly select the default for a command, try adding it to this
  314. list.
  315. Only *interactive* commands should go here. To disable
  316. compatibility mode in non-interactive functions, customize
  317. `ido-ubiquitous-function-compatibility-exceptions'."
  318. :type 'hook
  319. :group 'ido-ubiquitous-compatibility)
  320. (defmacro ido-ubiquitous-enable-compatibility-in (func)
  321. "Enable ido-ubiquitous old-style compatibility in FUNC."
  322. (let ((docstring
  323. (format "Enable ido-ubiquitous old-style compatibility in %s if its `idu-ubiquitous-enable-compatibility' property is non-nil." func)))
  324. `(progn
  325. (defadvice ,func (around disable-ido-ubiquitous activate)
  326. ,docstring
  327. (let ((ido-ubiquitous-enable-compatibility-globally
  328. (or ido-ubiquitous-enable-compatibility-globally
  329. (get ',func 'ido-ubiquitous-enable-compatibility))))
  330. ad-do-it))
  331. (put func 'ido-ubiquitous-enable-compatibility t))))
  332. (defmacro ido-ubiquitous-disable-compatibility-in (func)
  333. "Enable ido-ubiquitous old-style compatibility in FUNC.
  334. This reverses the effect of a previous call to
  335. `ido-ubiquitous-enable-compatibility-in' (if no such call was
  336. made, this is a no-op.)"
  337. `(when (get ',func 'ido-ubiquitous-enable-compatibility)
  338. (put ,func 'ido-ubiquitous-enable-compatibility nil)))
  339. (defun ido-ubiquitous-set-function-compatibility-list (sym newval)
  340. ;; Loop through all functions, enabling or disabling ido-ubiquitous
  341. ;; as appropriate.
  342. (mapatoms
  343. (lambda (sym)
  344. (if (memq sym newval)
  345. (eval `(ido-ubiquitous-enable-compatibility-in ,sym))
  346. (eval `(ido-ubiquitous-disable-compatibility-in ,sym)))))
  347. ;; Set the new value
  348. (set-default sym newval))
  349. ;;;###autoload
  350. (defcustom ido-ubiquitous-function-compatibility-list
  351. '()
  352. "List of functions in which to enable old-style compatibility.
  353. See `ido-ubiquitous-enable-compatibility-globally' for a
  354. description of the compatibility behavior. If ido doesn't
  355. properly select the default selection for a function, try adding
  356. it to this list.
  357. If you need to add a function to this list, please also file a
  358. bug report at
  359. https://github.com/DarwinAwardWinner/ido-ubiquitous/issues"
  360. :group 'ido-ubiquitous-compatibility
  361. :type 'hook
  362. :set 'ido-ubiquitous-set-function-compatibility-list)
  363. (defvar ido-ubiquitous-initial-item nil
  364. "The first item selected when ido starts.")
  365. (defadvice ido-read-internal (before clear-initial-item activate)
  366. (setq ido-ubiquitous-initial-item nil))
  367. (defadvice ido-make-choice-list (after set-initial-item activate)
  368. (when (and ad-return-value (listp ad-return-value))
  369. (setq ido-ubiquitous-initial-item (car ad-return-value))))
  370. (defadvice ido-next-match (after clear-initial-item activate)
  371. (setq ido-ubiquitous-initial-item nil))
  372. (defadvice ido-prev-match (after clear-initial-item activate)
  373. (setq ido-ubiquitous-initial-item nil))
  374. (defadvice ido-exit-minibuffer (around compatibility activate)
  375. "Emulate a quirk of `completing-read'.
  376. > If the input is null, `completing-read' returns DEF, or the
  377. > first element of the list of default values, or an empty string
  378. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  379. See `ido-ubiquitous-enable-compatibility-globally', which
  380. controls whether this advice has any effect."
  381. (if (and (eq ido-cur-item 'list)
  382. ;; Only enable if we are replacing `completing-read'
  383. ido-ubiquitous-this-call-replaces-completing-read
  384. ;; Compatibiliy enabled
  385. (or ido-ubiquitous-enable-compatibility-globally
  386. (memq this-command ido-ubiquitous-command-compatibility-list))
  387. ;; Input is empty
  388. (string= ido-text "")
  389. ;; Default is nil
  390. (null ido-default-item)
  391. ;; Prefix disables compatibility
  392. (not current-prefix-arg)
  393. (string= (car ido-cur-list)
  394. ido-ubiquitous-initial-item))
  395. (ido-select-text)
  396. ad-do-it)
  397. (setq ido-ubiquitous-initial-item nil))
  398. (defadvice call-interactively (around ido-ubiquitous-oldstyle-compatibility activate)
  399. (let ((ido-ubiquitous-enable-compatibility-globally
  400. (or ido-ubiquitous-enable-compatibility-globally
  401. (memq function ido-ubiquitous-command-compatibility-list))))
  402. ad-do-it))
  403. (defadvice command-execute (around ido-ubiquitous-oldstyle-compatibility activate)
  404. (let ((ido-ubiquitous-enable-compatibility-globally
  405. (or ido-ubiquitous-enable-compatibility-globally
  406. (memq function ido-ubiquitous-command-compatibility-list))))
  407. ad-do-it))
  408. ;;; Other
  409. (defun ido-ubiquitous-initialize ()
  410. "Do initial setup for ido-ubiquitous.
  411. This only needs to be called once when the file is first loaded."
  412. ;; Clean up old versions of ido-ubiquitous that defined advice on
  413. ;; `completing-read' instead of modifying
  414. ;; `completing-read-function'.
  415. (when (ad-find-advice 'completing-read 'around 'ido-ubiquitous)
  416. (ad-remove-advice 'completing-read 'around 'ido-ubiquitous)
  417. (ad-activate 'completing-read))
  418. ;; Make sure all exceptions are activated
  419. (ido-ubiquitous-set-function-exceptions
  420. 'ido-ubiquitous-function-exceptions
  421. ido-ubiquitous-function-exceptions)
  422. (ido-ubiquitous-set-function-compatibility-list
  423. 'ido-ubiquitous-function-compatibility-list
  424. ido-ubiquitous-function-compatibility-list)
  425. ;; Make sure the mode is turned on/off as specified by the value of
  426. ;; the mode variable
  427. (ido-ubiquitous-mode (if ido-ubiquitous-mode 1 0)))
  428. (ido-ubiquitous-initialize)
  429. (defun ido-ubiquitous-warn-about-ido-disabled ()
  430. "Warn if ido-ubiquitous is enabled without ido.
  431. Don't warn if emacs is still initializing, since ido-ubiquitous
  432. could be enabled first during init."
  433. (if (and after-init-time
  434. (not (bound-and-true-p ido-mode)))
  435. (warn "Ido-ubiquitous-mode enabled without ido mode. Ido-ubiquitous requires ido mode to be enabled.")))
  436. (provide 'ido-ubiquitous) ;;; ido-ubiquitous.el ends here