ido-ubiquitous.el 36 KB

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