ido-ubiquitous.el 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. ;; -*- lexical-binding: t -*-
  2. ;;; ido-ubiquitous.el --- Use ido (nearly) everywhere.
  3. ;; Author: Ryan C. Thompson
  4. ;; URL: https://github.com/DarwinAwardWinner/ido-ubiquitous
  5. ;; Version: 2.5
  6. ;; Created: 2011-09-01
  7. ;; Keywords: convenience
  8. ;; EmacsWiki: InteractivelyDoThings
  9. ;; Package-Requires: ((emacs "24.1"))
  10. ;; This file is NOT part of GNU Emacs.
  11. ;;; Commentary:
  12. ;; If you use the excellent `ido-mode' for efficient completion of
  13. ;; file names and buffers, you might wonder if you can get ido-style
  14. ;; completion everywhere else too. Well, that's what this package
  15. ;; does! ido-ubiquitous is here to enable ido-style completion for
  16. ;; (almost) every function that uses the standard completion function
  17. ;; `completing-read'.
  18. ;;; License:
  19. ;; This program is free software; you can redistribute it and/or modify
  20. ;; it under the terms of the GNU General Public License as published by
  21. ;; the Free Software Foundation; either version 3, or (at your option)
  22. ;; any later version.
  23. ;;
  24. ;; This program is distributed in the hope that it will be useful,
  25. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. ;; GNU General Public License for more details.
  28. ;;
  29. ;; You should have received a copy of the GNU General Public License
  30. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  31. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  32. ;; Boston, MA 02110-1301, USA.
  33. ;;; Code:
  34. (defconst ido-ubiquitous-version "2.5"
  35. "Currently running version of ido-ubiquitous.
  36. Note that when you update ido-ubiquitous, this variable may not
  37. be updated until you restart Emacs.")
  38. (eval-when-compile
  39. (when (or (not (boundp 'completing-read-function))
  40. (< emacs-major-version 24))
  41. (error "Could not find required 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.7.")))
  42. (require 'ido)
  43. (require 'advice)
  44. (require 'cl)
  45. ;; Declare this ahead of time to quiet the compiler
  46. (defvar ido-ubiquitous-fallback-completing-read-function)
  47. ;;; Internal utility functions
  48. (defun ido-ubiquitous--as-string (sym-or-str)
  49. "Return name of symbol, return string as is."
  50. (if (symbolp sym-or-str)
  51. (symbol-name sym-or-str)
  52. sym-or-str))
  53. (defun ido-ubiquitous--as-symbol (sym-or-str)
  54. "Return name of symbol, return string as is."
  55. (if (symbolp sym-or-str)
  56. sym-or-str
  57. (intern sym-or-str)))
  58. ;;; Custom widget definitions
  59. ;; We need to define some custom widget types for use in the override
  60. ;; variables.
  61. (define-widget 'lazy-notag 'lazy
  62. "Like lazy widget, but does not display its tag, only its value."
  63. :format "%v")
  64. ;; Define matcher functions and widgets for match specifications
  65. (defvar ido-ubiquitous-match-spec-widget-types nil
  66. "List of widget names for match specs.")
  67. (defvar ido-ubiquitous-spec-matchers nil
  68. "Alist of functions for matching function specs against function names.")
  69. (loop for (widget-name widget-tag key field-type matcher) in
  70. '((exact-match "Exact match" exact string string=)
  71. (prefix-match "Prefix match" prefix string string-prefix-p)
  72. (regexp-match "Regexp match" regexp regexp string-match-p))
  73. do (define-widget (ido-ubiquitous--as-symbol widget-name) 'lazy-notag widget-tag
  74. :menu-tag widget-tag
  75. :type `(list :tag ,widget-tag :format "%v"
  76. (const :format ""
  77. :tag ,widget-tag
  78. ,key)
  79. (,field-type :tag ,widget-tag)))
  80. do (add-to-list 'ido-ubiquitous-match-spec-widget-types
  81. widget-name 'append)
  82. do (add-to-list 'ido-ubiquitous-spec-matchers
  83. (cons key matcher) 'append))
  84. (define-widget 'ido-ubiquitous-match-spec 'lazy-notag
  85. "Choice of exact, prefix, or regexp match."
  86. :type `(choice :tag "Match type"
  87. ,@ido-ubiquitous-match-spec-widget-types))
  88. (define-widget 'ido-ubiquitous-command-override-spec 'lazy-notag
  89. "Choice of override action plus match specification."
  90. :type '(cons :tag "Override rule"
  91. (choice :tag "For matching commands"
  92. (const :menu-tag "Disable"
  93. :tag "Disable ido-ubiquitous"
  94. disable)
  95. (const :menu-tag "Enable"
  96. :tag "Enable ido-ubiquitous in normal default mode"
  97. enable)
  98. (const :menu-tag "Enable old-style default"
  99. :tag "Enable ido-ubiquitous in old-style default mode"
  100. enable-old))
  101. ido-ubiquitous-match-spec))
  102. (define-widget 'ido-ubiquitous-function-override-spec 'lazy-notag
  103. "Choice of override action and function name. (Exact match only.)"
  104. :type '(list :tag "Override rule"
  105. (choice :tag "Do the following"
  106. (const :menu-tag "Disable"
  107. :tag "Disable ido-ubiquitous"
  108. disable)
  109. (const :menu-tag "Enable"
  110. :tag "Enable ido-ubiquitous in normal default mode"
  111. enable)
  112. (const :menu-tag "Enable old-style default"
  113. :tag "Enable ido-ubiquitous in old-style default mode"
  114. enable-old))
  115. (const :format "" exact)
  116. (string :tag "For function")))
  117. ;;; Custom Declarations
  118. (defgroup ido-ubiquitous nil
  119. "Use ido for (almost) all completion."
  120. :group 'ido)
  121. ;;;###autoload
  122. (define-obsolete-variable-alias 'ido-ubiquitous
  123. 'ido-ubiquitous-mode "0.8")
  124. ;;;###autoload
  125. (define-obsolete-function-alias 'ido-ubiquitous
  126. 'ido-ubiquitous-mode "0.8")
  127. ;;;###autoload
  128. (define-minor-mode ido-ubiquitous-mode
  129. "Use `ido-completing-read' instead of `completing-read' almost everywhere.
  130. This mode has no effect unles `ido-mode' is also enabled.
  131. If this mode causes problems for a function, you can customize
  132. when ido completion is or is not used by customizing
  133. `ido-ubiquitous-command-overrides' or
  134. `ido-ubiquitous-function-overrides'."
  135. nil
  136. :global t
  137. :group 'ido-ubiquitous
  138. ;; Handle warning about ido disabled
  139. (when ido-ubiquitous-mode
  140. (ido-ubiquitous-warn-about-ido-disabled))
  141. ;; Ensure emacs 23 code disabled (in case user upgraded in this session)
  142. (ignore-errors
  143. (ad-disable-advice 'completing-read 'around 'ido-ubiquitous-legacy)
  144. (ad-activate 'completing-read))
  145. ;; Actually enable/disable the mode
  146. (setq completing-read-function
  147. (if ido-ubiquitous-mode
  148. 'completing-read-ido
  149. (or ido-ubiquitous-fallback-completing-read-function
  150. 'completing-read-default))))
  151. (defcustom ido-ubiquitous-max-items 5000
  152. "Max collection size to use ido-ubiquitous on.
  153. If `ido-ubiquitous-mode' is active and `completing-read' is
  154. called on a COLLECTION with greater than this number of items in
  155. it, the fallback completion method will be usedd instead. To
  156. fallback based on collection size, set this to nil."
  157. :type '(choice (const :tag "No limit" nil)
  158. (integer
  159. :tag "Limit" :value 5000
  160. :validate
  161. (lambda (widget)
  162. (let ((v (widget-value widget)))
  163. (if (and (integerp v)
  164. (> v 0))
  165. nil
  166. (widget-put widget :error "This field should contain a positive integer")
  167. widget)))))
  168. :group 'ido-ubiquitous)
  169. (defcustom ido-ubiquitous-fallback-completing-read-function
  170. ;; Initialize to the current value of `completing-read-function',
  171. ;; unless that is already set to the ido completer, in which case
  172. ;; use `completing-read-default'.
  173. (if (eq completing-read-function 'completing-read-ido)
  174. 'completing-read-default
  175. completing-read-function)
  176. "Alternate completing-read function to use when ido is not wanted.
  177. This will be used for functions that are incompatibile with ido
  178. or if ido cannot handle the completion arguments.
  179. If you turn off ido-ubiquitous mode, `completing-read-function'
  180. will be set back to this."
  181. :type '(choice (const :tag "Standard emacs completion"
  182. completing-read-default)
  183. (function :tag "Other function"))
  184. :group 'ido-ubiquitous)
  185. (define-obsolete-variable-alias
  186. 'ido-ubiquitous-enable-compatibility-globally
  187. 'ido-ubiquitous-enable-old-style-default
  188. "2.0")
  189. (defcustom ido-ubiquitous-enable-old-style-default t
  190. "Allow ido to emulate a quirk of `completing-read'.
  191. From the `completing-read' docstring:
  192. > If the input is null, `completing-read' returns DEF, or the
  193. > first element of the list of default values, or an empty string
  194. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  195. If this variable is non-nil, then ido-ubiquitous will attempt to
  196. emulate this behavior. Specifically, if RET is pressed
  197. immediately upon entering completion, an empty string will be
  198. returned instead of the first element in the list. This behavior
  199. is only enabled when ido is being used as a substitute for
  200. `completing-read', and not when it is used directly.
  201. This odd behavior is required for compatibility with an old-style
  202. usage pattern whereby the default was requested by returning an
  203. empty string. In this mode, the caller receives the empty string
  204. and handles the default case manually, while `completing-read'
  205. never has any knowledge of the default. This is a problem for
  206. ido, which always returns the first element in the list when the
  207. input is empty. Without knowledge of the default, it cannot
  208. ensure that the default is first on the list, so returning the
  209. first item is not the correct behavior. Instead, it must return
  210. an empty string like `completing-read'.
  211. You can termporarily invert this behavior by prefixing \"RET\"
  212. with \"C-u\".
  213. If you want to enable old-style default selection selectively for
  214. specific commands or functions, set appropriate overrides in
  215. `ido-ubiquitous-command-overrides' or
  216. `ido-ubiquitous-function-overrides'."
  217. :type 'boolean
  218. :group 'ido-ubiquitous)
  219. (defconst ido-ubiquitous-default-command-overrides
  220. '(;; If you want ido for M-x, install smex
  221. (disable exact "execute-extended-command")
  222. ;; https://github.com/technomancy/ido-ubiquitous/issues/13#issuecomment-8033522
  223. (enable prefix "wl-")
  224. ;; https://github.com/technomancy/ido-ubiquitous/issues/7
  225. (enable-old prefix "Info-")
  226. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/4
  227. (enable exact "webjump")
  228. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/28
  229. (enable regexp "\\`\\(find\\|load\\|locate\\)-library\\'"))
  230. "Default value of `ido-ubiquitous-command-overrides'.
  231. You can restore these using the command `ido-ubiquitous-restore-default-overrides'.")
  232. (defconst ido-ubiquitous-default-function-overrides
  233. '((disable exact "read-file-name")
  234. (disable exact "read-file-name-internal")
  235. (disable exact "read-buffer")
  236. (disable exact "gnus-emacs-completing-read")
  237. (disable exact "gnus-iswitchb-completing-read")
  238. (disable exact "grep-read-files")
  239. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/36
  240. (enable exact "bookmark-completing-read")
  241. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/4
  242. (enable-old exact "webjump-read-choice")
  243. (enable-old exact "webjump-read-url-choice")
  244. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/9
  245. (disable exact "isearchp-read-unicode-char"))
  246. "Default value of `ido-ubiquitous-function-overrides'.
  247. You can restore these using the command `ido-ubiquitous-restore-default-overrides'.")
  248. (defcustom ido-ubiquitous-command-overrides ido-ubiquitous-default-command-overrides
  249. "List of command override specifications for ido-ubiquitous
  250. Each override specification describes how ido-ubiquitous should
  251. behave one or many commands. A specification has the
  252. form `(BEHAVIOR MATCH-TYPE MATCH-TEXT)'. BEHAVIOR is one of the
  253. following:
  254. * `disable': ido-ubiquitous should not be used at all for the
  255. specified commands;
  256. * `enable': ido-ubiquitous may be used with the specified
  257. commands, without emulating the old-style default selection
  258. of `completing-read';
  259. * `enable-old': ido-ubiquitous may be used with the specified
  260. commands, and should emulate the old-style default selection
  261. of `completing-read'.
  262. MATCH-TYPE affects how MATCH-TEXT is interpreted, as follows:
  263. * `exact': the specification only affects the one command
  264. whose name is MATCH-TEXT;
  265. * `prefix': the specification affects any command whose name
  266. starts with MATCH-TEXT (This is useful for specifying a
  267. certain behavior for an entire package);
  268. * `regexp': the specification affects any command whose name
  269. matches MATCH-TEXT (with MATCH-TEXT being interpreted as a
  270. regular expression)
  271. MATCH-TEXT should be a string.
  272. Since this variable's has a somewhat complex structure, it is
  273. recommended that you set this variable through Customize.
  274. Note that this variable only affects *commands*, which are
  275. functions marked as interactive. See
  276. `ido-ubiquitous-function-overrides' for how to modify the
  277. behavior of ido-ubiquitous for arbitrary functions.
  278. If you need to add a new specification to this list, please also
  279. file a bug report at https://github.com/DarwinAwardWinner/ido-ubiquitous/issues"
  280. :type '(repeat ido-ubiquitous-command-override-spec)
  281. :group 'ido-ubiquitous)
  282. (defmacro ido-ubiquitous-with-override (override &rest body)
  283. "Eval BODY with specicified OVERRIDE in place.
  284. The OVERRIDE argument is evaluated normally, so if it is a
  285. literal symbol, it must be quoted.
  286. See `ido-ubiquitous-command-overrides' for valid override types."
  287. ;; Eval override
  288. `(let ((ido-ubiquitous-next-override ,override))
  289. ,@body))
  290. (put 'ido-ubiquitous-with-override 'lisp-indent-function
  291. (get 'prog1 'lisp-indent-function))
  292. (defun ido-ubiquitous-apply-function-override (func override)
  293. "Set the override property on FUNC to OVERRIDE and set up advice to apply the override."
  294. (setq func (ido-ubiquitous--as-symbol func)
  295. override (ido-ubiquitous--as-symbol override))
  296. (put func 'ido-ubiquitous-override override)
  297. (when override
  298. (let ((docstring
  299. (format "Override ido-ubiquitous behavior in %s if its `ido-ubiquitous-override' property is non-nil." func)))
  300. (eval
  301. `(defadvice ,func (around ido-ubiquitous-override activate)
  302. ,docstring
  303. (ido-ubiquitous-with-override
  304. (get ',func 'ido-ubiquitous-override)
  305. ad-do-it))))))
  306. (defun ido-ubiquitous-set-function-overrides (sym newval)
  307. "Custom setter function for `ido-ubiquitous-function-overrides'.
  308. In addition to setting the variable, this also sets up advice on
  309. each function to apply the appropriate override."
  310. ;; Unset all previous overrides
  311. (when (boundp sym)
  312. (let ((oldval (eval sym)))
  313. (loop for (_action _match-type func) in oldval
  314. do (ido-ubiquitous-apply-function-override func nil))))
  315. ;; Ensure that function names are strings, not symbols
  316. (setq newval
  317. (loop for (action match-type func) in newval
  318. collect (list action match-type
  319. (ido-ubiquitous--as-string func))))
  320. (set-default sym newval)
  321. ;; set new overrides
  322. (loop for (action _match-type func) in (eval sym)
  323. do (ido-ubiquitous-apply-function-override func action)))
  324. (defcustom ido-ubiquitous-function-overrides ido-ubiquitous-default-function-overrides
  325. "List of function override specifications for ido-ubiquitous
  326. Function override specifications have a similar structure to
  327. command override specifications (see
  328. `ido-ubiquitous-command-overrides'). A function override
  329. specification has the form `(BEHAVIOR MATCH-TYPE MATCH-TEXT)'.
  330. However, `MATCH-TYPE' may ONLY be `exact'; No other match type is
  331. supported.
  332. If you need to add a new specification to this list, please also file a
  333. bug report at https://github.com/DarwinAwardWinner/ido-ubiquitous/issues
  334. Setting this variable directly has no effect. You must set it
  335. through Customize."
  336. :type '(repeat ido-ubiquitous-function-override-spec)
  337. :set 'ido-ubiquitous-set-function-overrides
  338. :group 'ido-ubiquitous)
  339. ;;; ido-ubiquitous core
  340. (defvar ido-ubiquitous-next-call-replaces-completing-read nil
  341. "If t, then the next call to `ido-completing-read' is by ido-ubiquitous.")
  342. (defvar ido-ubiquitous-this-call-replaces-completing-read nil
  343. "If t, then the current call to `ido-completing-read' is by ido-ubiquitous.")
  344. (defvar ido-ubiquitous-next-override nil
  345. "This holds the override to be applied on the next call to `completing-read'.")
  346. (defvar ido-ubiquitous-active-override nil
  347. "This holds the override being applied to the current call to `completing-read'.")
  348. (defun ido-ubiquitous-completing-read (&rest args)
  349. "Wrapper for `ido-completing-read' that enables ido-ubiquitous features."
  350. (let ((ido-ubiquitous-next-call-replaces-completing-read t))
  351. (apply 'ido-completing-read args)))
  352. (defadvice ido-completing-read (around detect-replacing-cr activate)
  353. "Enable workarounds if this call was done through ido-ubiquitous.
  354. This advice implements the logic required for
  355. `ido-completing-read' to handle a number of special cases that
  356. `completing-read' can handle. It only has an effect if
  357. `ido-completing-read' is called through
  358. `ido-ubiquitous-completing-read', so other packages that use
  359. `ido-completing-read', such as `smex', will not be affected."
  360. (let* ((ido-ubiquitous-this-call-replaces-completing-read ido-ubiquitous-next-call-replaces-completing-read)
  361. (ido-ubiquitous-next-call-replaces-completing-read nil)
  362. (error-during-setup nil))
  363. (when ido-ubiquitous-this-call-replaces-completing-read
  364. (condition-case nil
  365. (progn
  366. ;; ido doesn't natively handle DEF being a list. If DEF is
  367. ;; a list, prepend it to CHOICES and set DEF to just the
  368. ;; car of the default list.
  369. (when (and def (listp def))
  370. (setq choices
  371. (append def
  372. (nreverse (cl-set-difference choices def)))
  373. def (car def)))
  374. ;; Work around a bug in ido when both INITIAL-INPUT and
  375. ;; DEF are provided More info:
  376. ;; https://github.com/technomancy/ido-ubiquitous/issues/18
  377. (let ((initial (cond ((null initial-input) "")
  378. ((stringp initial-input) initial-input)
  379. ((consp initial-input) (car initial-input))
  380. (t initial-input))))
  381. (when (and def initial
  382. (stringp initial)
  383. (not (string= initial "")))
  384. ;; Both default and initial input were provided. So
  385. ;; keep the initial input and preprocess the choices
  386. ;; list to put the default at the head, then proceed
  387. ;; with default = nil.
  388. (setq choices (cons def (remove def choices))
  389. def nil))))
  390. (error
  391. (progn
  392. (warn "ido-ubiquitous: failed during setup. Falling back to standard completion")
  393. (setq error-during-setup t)))))
  394. ;; For ido-ubiquitous, only attempt ido completion if setup
  395. ;; completed without error
  396. (if (not error-during-setup)
  397. ad-do-it
  398. (setq ad-return-value
  399. (funcall
  400. ido-ubiquitous-fallback-completing-read-function
  401. prompt choices predicate require-match initial-input
  402. hist def inherit-input-method)))))
  403. (defun completing-read-ido (prompt collection &optional predicate
  404. require-match initial-input
  405. hist def inherit-input-method)
  406. "ido-based method for reading from the minibuffer with completion.
  407. See `completing-read' for the meaning of the arguments.
  408. This function is a wrapper for `ido-completing-read' designed to
  409. be used as the value of `completing-read-function'. Importantly,
  410. it detects edge cases that ido cannot handle and uses normal
  411. completion for them."
  412. (let* (;; Set the active override and clear the "next" one so it
  413. ;; doesn't apply to nested calls.
  414. (ido-ubiquitous-active-override ido-ubiquitous-next-override)
  415. (ido-ubiquitous-next-override nil)
  416. ;; Check if ido can handle this collection. If collection is
  417. ;; a function, require an override to be ok.
  418. (collection-ok
  419. (or (not (functionp collection))
  420. (memq ido-ubiquitous-active-override '(enable enable-old))))
  421. ;; Check for conditions that ido can't or shouldn't handle
  422. (ido-allowed
  423. (and ido-mode
  424. ido-ubiquitous-mode
  425. ;; Check for disable override
  426. (not (eq ido-ubiquitous-active-override 'disable))
  427. ;; Can't handle this arg
  428. (not inherit-input-method)
  429. ;; Can't handle this being set
  430. (not (bound-and-true-p completion-extra-properties))))
  431. ;; Pre-expand list of possible completions, but only if we
  432. ;; have a chance of using ido. This is executed after the
  433. ;; ido-allowed check to avoid unnecessary work if ido isn't
  434. ;; going to used.
  435. (_ignore ;; (Return value doesn't matter).
  436. (when (and ido-allowed collection-ok)
  437. (setq collection (all-completions "" collection predicate)
  438. ;; Don't need this any more
  439. predicate nil)))
  440. (colection-ok
  441. ;; Don't use ido if the collection is empty or too large.
  442. (and collection-ok
  443. collection
  444. (or (null ido-ubiquitous-max-items)
  445. (<= (length collection) ido-ubiquitous-max-items))))
  446. ;; Final check for everything
  447. (ido-allowed (and ido-allowed collection-ok))
  448. (comp-read-fun
  449. (if ido-allowed
  450. 'ido-ubiquitous-completing-read
  451. ido-ubiquitous-fallback-completing-read-function)))
  452. (funcall comp-read-fun
  453. prompt collection predicate
  454. require-match initial-input
  455. hist def inherit-input-method)))
  456. ;;; Old-style default support
  457. (defvar ido-ubiquitous-initial-item nil
  458. "The first item selected when ido starts.
  459. This is initialized to the first item in the list of completions
  460. when ido starts, and is cleared when any character is entered
  461. into the prompt or the list is cycled. If it is non-nil and still
  462. equal to the first item in the completion list when ido exits,
  463. then if `ido-ubiquitous-enable-old-style-default' is
  464. non-nil, ido returns an empty string instead of the first item on
  465. the list.")
  466. (defadvice ido-read-internal (before clear-initial-item activate)
  467. (setq ido-ubiquitous-initial-item nil))
  468. (defadvice ido-make-choice-list (after set-initial-item activate)
  469. (when (and ad-return-value (listp ad-return-value))
  470. (setq ido-ubiquitous-initial-item (car ad-return-value))))
  471. (defadvice ido-next-match (after clear-initial-item activate)
  472. (setq ido-ubiquitous-initial-item nil))
  473. (defadvice ido-prev-match (after clear-initial-item activate)
  474. (setq ido-ubiquitous-initial-item nil))
  475. (defadvice ido-exit-minibuffer (around compatibility activate)
  476. "Emulate a quirk of `completing-read'.
  477. > If the input is null, `completing-read' returns DEF, or the
  478. > first element of the list of default values, or an empty string
  479. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  480. See `ido-ubiquitous-enable-old-style-default', which
  481. controls whether this advice has any effect."
  482. (condition-case nil
  483. (let* ((enable-oldstyle
  484. (and
  485. ;; Completing a list, not a buffer or file
  486. (eq ido-cur-item 'list)
  487. ;; Only enable if we are replacing `completing-read'
  488. ido-ubiquitous-this-call-replaces-completing-read
  489. ;; Default is nil
  490. (null ido-default-item)
  491. ;; Input is empty
  492. (string= ido-text "")
  493. ;; Old-style default enabled
  494. (if ido-ubiquitous-active-override
  495. (eq ido-ubiquitous-active-override 'enable-old)
  496. ido-ubiquitous-enable-old-style-default)
  497. ;; First item on the list hasn't changed
  498. (string= (car ido-cur-list)
  499. ido-ubiquitous-initial-item)))
  500. ;; Prefix inverts oldstyle behavior
  501. (should-invert current-prefix-arg)
  502. (actually-enable-oldstyle
  503. (if should-invert (not enable-oldstyle) enable-oldstyle)))
  504. (if actually-enable-oldstyle
  505. (ido-select-text)
  506. ad-do-it))
  507. (error ad-do-it))
  508. (setq ido-ubiquitous-initial-item nil))
  509. ;;; Overrides
  510. (defun ido-ubiquitous-restore-default-overrides (&optional save)
  511. "Re-add the default overrides for ido-ubiquitous.
  512. This will ensure that the default overrides are all present and
  513. at the head of the list in `ido-ubiquitous-command-overrides' and
  514. `ido-ubiquitous-function-overrides'. User-added overrides will
  515. not be removed, but they may be masked if one of the default
  516. overrides affects the same functions.
  517. With a prefix arg, also save the above variables' new values for
  518. future sessions."
  519. (interactive "P")
  520. (let ((setter (if save
  521. 'customize-save-variable
  522. 'customize-set-variable)))
  523. (loop for (var def) in '((ido-ubiquitous-command-overrides
  524. ido-ubiquitous-default-command-overrides)
  525. (ido-ubiquitous-function-overrides
  526. ido-ubiquitous-default-function-overrides))
  527. do (let* ((curval (eval var))
  528. (defval (eval def))
  529. (newval (delete-dups (append defval curval))))
  530. (funcall setter var newval)))
  531. (message (if save
  532. "ido-ubiquitous: Restored default command and function overrides and saved for future sessions."
  533. "ido-ubiquitous: Restored default command and function overrides for current session only."))))
  534. (defun ido-ubiquitous-spec-match (spec symbol)
  535. "Returns t if SPEC matches SYMBOL (which should be a function name).
  536. See `ido-ubiquitous-command-overrides'."
  537. (when (and symbol (symbolp symbol))
  538. (destructuring-bind (type text) spec
  539. (let ((matcher (cdr (assoc type ido-ubiquitous-spec-matchers)))
  540. (text (ido-ubiquitous--as-string text))
  541. (symname (ido-ubiquitous--as-string symbol)))
  542. (when (null matcher)
  543. (error "ido-ubiquitous: Unknown match spec type \"%s\". See `ido-ubiquitous-spec-matchers' for valid types." type))
  544. (funcall matcher text symname)))))
  545. (defun ido-ubiquitous-get-command-override (cmd)
  546. "Return the override associated with the command CMD.
  547. If there is no override set for CMD in
  548. `ido-ubiquitous-command-overrides', return nil."
  549. (when (and cmd (symbolp cmd))
  550. (loop for (action . spec) in ido-ubiquitous-command-overrides
  551. when (memq action '(disable enable enable-old nil))
  552. when (ido-ubiquitous-spec-match spec cmd)
  553. return action
  554. finally return nil)))
  555. ;;; Workaround for https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/24
  556. ;; When `call-interactively' is advised, `called-interactively-p'
  557. ;; always returns nil. So we redefine it (and `interactive-p') to test
  558. ;; the correct condition.
  559. (defsubst ido-ubiquitous--looks-like-advised-orig (func)
  560. "Returns t if FUNC is a symbol starting with \"ad-Orig-\".
  561. Such symbols are used to store the original definitions of
  562. functions that have been advised by `defadvice' or similar."
  563. (and (symbolp func)
  564. (string-prefix-p "ad-Orig-" (symbol-name func))))
  565. (defsubst ido-ubiquitous--looks-like-call-interactively (func)
  566. "Returns t if FUNC looks like the function `call-interactively'.
  567. FUNC \"looks like\" `call-interactively' if it is the literal
  568. symbol `call-interactively', or the value of `(symbol-function
  569. 'call-interactively)', or a symbol whose `symbol-function' is the
  570. same as that of `call-interactively'.
  571. This function is used to determine whether a given function was
  572. \"called by\" `call-interactively' and therefore was called
  573. interactively."
  574. (when func
  575. (eq (symbol-function 'call-interactively)
  576. (if (symbolp func)
  577. (symbol-function func)
  578. func))))
  579. (defun ido-ubiquitous--backtrace-from (fun)
  580. "Return all backtrace frames, starting with the one for FUN.
  581. FUN may be a list of functions, in which case the first one found
  582. on the stack will be used."
  583. (let ((stack
  584. (loop for i upfrom 0
  585. for frame = (backtrace-frame i)
  586. while frame
  587. collect frame))
  588. (funcs (if (functionp fun)
  589. (list fun)
  590. fun)))
  591. (while (and stack
  592. (not (memq (cadar stack) funcs)))
  593. (setq stack (cdr stack)))
  594. stack))
  595. (defun ido-ubiquitous--clean-advice-from-backtrace (stack)
  596. "Takes a stack trace and cleans all evidence of advice.
  597. Specifically, for each call to a function starting with
  598. \"ad-Orig-\", that call and all prior calls up to but not
  599. including the advised function's original name are deleted from
  600. the stack."
  601. (let ((skipping-until nil))
  602. (loop for frame in stack
  603. for func = (cadr frame)
  604. ;; Check if we found the frame we we're skipping to
  605. if (and skipping-until
  606. (eq func skipping-until))
  607. do (setq skipping-until nil)
  608. ;; If we're looking at an the original form of an advised
  609. ;; function, skip until the real name of that function.
  610. if (and (not skipping-until)
  611. (ido-ubiquitous--looks-like-advised-orig func))
  612. do (setq skipping-until
  613. (intern
  614. (substring (symbol-name func)
  615. (eval-when-compile (length "ad-Orig-")))))
  616. unless skipping-until collect frame)))
  617. (defsubst ido-ubiquitous--interactive-internal ()
  618. "Eqivalent of the INTERACTIVE macro in the Emacs C source.
  619. This is an internal function that should never be called
  620. directly.
  621. See the C source for the logic behind this function."
  622. (and (not executing-kbd-macro)
  623. (not noninteractive)))
  624. (defun ido-ubiquitous--interactive-p-internal ()
  625. "Equivalent of C function \"interactive_p\".
  626. This is an internal function that should never be called
  627. directly.
  628. See the C source for the logic behind this function."
  629. (let ((stack
  630. ;; We clean advice from the backtrace. This ensures that we
  631. ;; get the right answer even if `call-interactively' has been
  632. ;; advised.
  633. (ido-ubiquitous--clean-advice-from-backtrace
  634. (cdr
  635. (ido-ubiquitous--backtrace-from
  636. '(called-interactively-p interactive-p))))))
  637. ;; See comments in the C function for the logic here.
  638. (while (and stack
  639. (or (eq (cadar stack) 'bytecode)
  640. (null (caar stack))))
  641. (setq stack (cdr stack)))
  642. ;; Top of stack is now the function that we want to know
  643. ;; about. Pop it, then check if the next function is
  644. ;; `call-interactively', using a more permissive test than the default.
  645. (ido-ubiquitous--looks-like-call-interactively (cadadr stack))))
  646. (if (boundp 'called-interactively-p-functions)
  647. ;; Emacs trunk, in which called-interactively-p logic has
  648. ;; changed, so we can't work around the "advice on
  649. ;; call-interactively" bug. Define advice instead on
  650. ;; `command-execute', which will not work for key bindings, but
  651. ;; will work for many other things.
  652. (defadvice command-execute (around ido-ubiquitous activate)
  653. "Implements the behavior specified in `ido-ubiquitous-command-overrides'.
  654. Does not work for keybindings or anything else that skips `command-execute'."
  655. (ido-ubiquitous-with-override
  656. (ido-ubiquitous-get-command-override
  657. ;; Ugly hack because Emacs byte compiler doesn't know that CMD
  658. ;; is defined for some reason
  659. (bound-and-true-p cmd))
  660. ad-do-it))
  661. ;; Emacs release 24.3.1: Old called-interactively-p logic & advice
  662. ;; system, which we know how to manipulate.
  663. (defadvice call-interactively (around ido-ubiquitous activate)
  664. "Implements the behavior specified in `ido-ubiquitous-command-overrides'."
  665. (ido-ubiquitous-with-override
  666. (ido-ubiquitous-get-command-override (ad-get-arg 0))
  667. ad-do-it))
  668. (defadvice interactive-p (around ido-ubiquitous activate)
  669. "Return the correct result when `call-interactively' is advised."
  670. (condition-case nil
  671. (setq ad-return-value
  672. (and (ido-ubiquitous--interactive-internal)
  673. (ido-ubiquitous--interactive-p-internal)))
  674. ;; In case of error in the advice, fall back to the default
  675. ;; implementation
  676. ad-do-it))
  677. (defadvice called-interactively-p (around ido-ubiquitous activate)
  678. "Return the correct result when `call-interactively' is advised."
  679. (condition-case nil
  680. (setq ad-return-value
  681. (and (or (ido-ubiquitous--interactive-internal)
  682. (not (eq kind 'interactive)))
  683. (ido-ubiquitous--interactive-p-internal)))
  684. ;; In case of error in the advice, fall back to the default
  685. ;; implementation
  686. ad-do-it)))
  687. ;;; Other
  688. (defun ido-ubiquitous-warn-about-ido-disabled ()
  689. "Warn if ido-ubiquitous is enabled without ido.
  690. Don't warn if emacs is still initializing, since ido-ubiquitous
  691. could be enabled first during init."
  692. (when (and ido-ubiquitous-mode
  693. after-init-time
  694. (not (bound-and-true-p ido-mode)))
  695. (warn "ido-ubiquitous-mode enabled without ido mode. ido-ubiquitous requires ido mode to be enabled.")))
  696. (defun ido-ubiquitous-initialize ()
  697. "Do initial setup for ido-ubiquitous.
  698. This only needs to be called once when the file is first loaded."
  699. ;; Clean up old versions of ido-ubiquitous that defined advice on
  700. ;; `completing-read' instead of modifying
  701. ;; `completing-read-function'.
  702. (when (ad-find-advice 'completing-read 'around 'ido-ubiquitous)
  703. (ad-remove-advice 'completing-read 'around 'ido-ubiquitous)
  704. (ad-activate 'completing-read))
  705. ;; Make sure the mode is turned on/off as specified by the value of
  706. ;; the mode variable
  707. (ido-ubiquitous-mode (if ido-ubiquitous-mode 1 0)))
  708. (ido-ubiquitous-initialize)
  709. (provide 'ido-ubiquitous)
  710. ;;; ido-ubiquitous.el ends here