ido-ubiquitous.el 36 KB

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