ido-ubiquitous.el 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. ;;; ido-ubiquitous.el --- Use ido (nearly) everywhere.
  2. ;; Author: Ryan C. Thompson
  3. ;; URL: https://github.com/DarwinAwardWinner/ido-ubiquitous
  4. ;; Version: 2.1.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.1.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 'macroexp)
  42. (eval-when-compile (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. (cl-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. ido-ubiquitous-fallback-completing-read-function))
  148. (message "Ido-ubiquitous mode %s" (if ido-ubiquitous-mode "enabled" "disabled")))
  149. (defcustom ido-ubiquitous-fallback-completing-read-function
  150. ;; Initialize to the current value of `completing-read-function',
  151. ;; unless that is already set to the ido completer, in which case
  152. ;; use `completing-read-default'.
  153. (if (eq completing-read-function 'completing-read-ido)
  154. 'completing-read-default
  155. completing-read-function)
  156. "Alternate completing-read function to use when ido is not wanted.
  157. This will be used for functions that are incompatibile with ido
  158. or if ido cannot handle the completion arguments.
  159. If you turn off ido-ubiquitous mode, `completing-read-function'
  160. will be set back to this."
  161. :type '(choice (const :tag "Standard emacs completion"
  162. completing-read-default)
  163. (function :tag "Other function"))
  164. :group 'ido-ubiquitous)
  165. (define-obsolete-variable-alias
  166. 'ido-ubiquitous-enable-compatibility-globally
  167. 'ido-ubiquitous-enable-old-style-default
  168. "2.0")
  169. (defcustom ido-ubiquitous-enable-old-style-default t
  170. "Allow ido to emulate a quirk of `completing-read'.
  171. From the `completing-read' docstring:
  172. > If the input is null, `completing-read' returns DEF, or the
  173. > first element of the list of default values, or an empty string
  174. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  175. If this variable is non-nil, then ido-ubiquitous will attempt to
  176. emulate this behavior. Specifically, if RET is pressed
  177. immediately upon entering completion, an empty string will be
  178. returned instead of the first element in the list. This behavior
  179. is only enabled when ido is being used as a substitute for
  180. `completing-read', and not when it is used directly.
  181. This odd behavior is required for compatibility with an old-style
  182. usage pattern whereby the default was requested by returning an
  183. empty string. In this mode, the caller receives the empty string
  184. and handles the default case manually, while `completing-read'
  185. never has any knowledge of the default. This is a problem for
  186. ido, which always returns the first element in the list when the
  187. input is empty. Without knowledge of the default, it cannot
  188. ensure that the default is first on the list, so returning the
  189. first item is not the correct behavior. Instead, it must return
  190. an empty string like `completing-read'.
  191. You can termporarily invert this behavior by prefixing \"RET\"
  192. with \"C-u\".
  193. If you want to enable old-style default selection selectively for
  194. specific commands or functions, set appropriate overrides in
  195. `ido-ubiquitous-command-overrides' or
  196. `ido-ubiquitous-function-overrides'."
  197. :type 'boolean
  198. :group 'ido-ubiquitous)
  199. (defconst ido-ubiquitous-default-command-overrides
  200. '(;; If you want ido for M-x, install smex
  201. (disable exact "execute-extended-command")
  202. ;; https://github.com/technomancy/ido-ubiquitous/issues/13#issuecomment-8033522
  203. (enable prefix "wl-")
  204. ;; https://github.com/technomancy/ido-ubiquitous/issues/7
  205. (enable-old prefix "Info-")
  206. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/4
  207. (enable exact "webjump"))
  208. "Default value of `ido-ubiquitous-command-overrides'.
  209. You can restore these using the command `ido-ubiquitous-restore-default-overrides'.")
  210. (defconst ido-ubiquitous-default-function-overrides
  211. '((disable exact "read-file-name")
  212. (disable exact "read-file-name-internal")
  213. (disable exact "read-buffer")
  214. (disable exact "gnus-emacs-completing-read")
  215. (disable exact "gnus-iswitchb-completing-read")
  216. (disable exact "man")
  217. ;; https://github.com/technomancy/ido-ubiquitous/issues/3
  218. (disable exact "grep-read-files")
  219. ;; https://github.com/technomancy/ido-ubiquitous/issues/9
  220. (enable exact "bookmark-completing-read")
  221. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/4
  222. (enable-old exact "webjump-read-choice")
  223. (enable-old exact "webjump-read-url-choice")
  224. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/9
  225. (disable exact "isearchp-read-unicode-char"))
  226. "Default value of `ido-ubiquitous-function-overrides'.
  227. You can restore these using the command `ido-ubiquitous-restore-default-overrides'.")
  228. (defcustom ido-ubiquitous-command-overrides ido-ubiquitous-default-command-overrides
  229. "List of command override specifications for ido-ubiquitous
  230. Each override specification describes how ido-ubiquitous should
  231. behave one or many commands. A specification has the
  232. form `(BEHAVIOR MATCH-TYPE MATCH-TEXT)'. BEHAVIOR is one of the
  233. following:
  234. * `disable': ido-ubiquitous should not be used at all for the
  235. specified commands;
  236. * `enable': ido-ubiquitous may be used with the specified
  237. commands, without emulating the old-style default selection
  238. of `completing-read';
  239. * `enable-old': ido-ubiquitous may be used with the specified
  240. commands, and should emulate the old-style default selection
  241. of `completing-read'.
  242. MATCH-TYPE affects how MATCH-TEXT is interpreted, as follows:
  243. * `exact': the specification only affects the one command
  244. whose name is MATCH-TEXT;
  245. * `prefix': the specification affects any command whose name
  246. starts with MATCH-TEXT (This is useful for specifying a
  247. certain behavior for an entire package);
  248. * `regexp': the specification affects any command whose name
  249. matches MATCH-TEXT (with MATCH-TEXT being interpreted as a
  250. regular expression)
  251. MATCH-TEXT should be a string.
  252. Since this variable's has a somewhat complex structure, it is
  253. recommended that you set this variable through Customize.
  254. Note that this variable only affects *commands*, which are
  255. functions marked as interactive. See
  256. `ido-ubiquitous-function-overrides' for how to modify the
  257. behavior of ido-ubiquitous for arbitrary functions.
  258. If you need to add a new specification to this list, please also
  259. file a bug report at https://github.com/DarwinAwardWinner/ido-ubiquitous/issues"
  260. :type '(repeat ido-ubiquitous-command-override-spec)
  261. :group 'ido-ubiquitous)
  262. (defmacro ido-ubiquitous-with-override (override &rest body)
  263. "Eval BODY with specicified OVERRIDE in place.
  264. The OVERRIDE argument is evaluated normally, so if it is a
  265. literal symbol, it must be quoted.
  266. See `ido-ubiquitous-command-overrides' for valid override types."
  267. ;; Eval override
  268. (setq override (ignore-errors (eval override)))
  269. `(let ((ido-ubiquitous-next-override ',override))
  270. ,@body))
  271. (put 'ido-ubiquitous-with-override 'lisp-indent-function
  272. (eval-when-compile (get 'prog1 'lisp-indent-function)))
  273. (defun ido-ubiquitous-apply-function-override (func override)
  274. "Set the override property on FUNC to OVERRIDE and set up advice to apply the override."
  275. (setq func (ido-ubiquitous--as-symbol func)
  276. override (ido-ubiquitous--as-symbol override))
  277. (put func 'ido-ubiquitous-override override)
  278. (when override
  279. (let ((docstring
  280. (format "Override ido-ubiquitous behavior in %s if its `ido-ubiquitous-override' property is non-nil." func)))
  281. (eval
  282. `(defadvice ,func (around ido-ubiquitous-override activate)
  283. ,docstring
  284. (ido-ubiquitous-with-override
  285. (get ',func 'ido-ubiquitous-override)
  286. ad-do-it))))))
  287. (defun ido-ubiquitous-set-function-overrides (sym newval)
  288. "Custom setter function for `ido-ubiquitous-function-overrides'.
  289. In addition to setting the variable, this also sets up advice on
  290. each function to apply the appropriate override."
  291. ;; Unset all previous overrides
  292. (when (boundp sym)
  293. (let ((oldval (eval sym)))
  294. (cl-loop for (action match-type func) in oldval
  295. do (ido-ubiquitous-apply-function-override func nil))))
  296. ;; Ensure that function names are strings, not symbols
  297. (setq newval
  298. (cl-loop for (action match-type func) in newval
  299. collect (list action match-type
  300. (ido-ubiquitous--as-string func))))
  301. (set-default sym newval)
  302. ;; set new overrides
  303. (cl-loop for (action match-type func) in (eval sym)
  304. do (ido-ubiquitous-apply-function-override func action)))
  305. (defcustom ido-ubiquitous-function-overrides ido-ubiquitous-default-function-overrides
  306. "List of function override specifications for ido-ubiquitous
  307. Function override specifications have a similar structure to
  308. command override specifications (see
  309. `ido-ubiquitous-command-overrides'). A function override
  310. specification has the form `(BEHAVIOR MATCH-TYPE MATCH-TEXT)'.
  311. However, `MATCH-TYPE' may ONLY be `exact'; No other match type is
  312. supported.
  313. If you need to add a new specification to this list, please also file a
  314. bug report at https://github.com/DarwinAwardWinner/ido-ubiquitous/issues
  315. Setting this variable directly has no effect. You must set it
  316. through Customize."
  317. :type '(repeat ido-ubiquitous-function-override-spec)
  318. :set 'ido-ubiquitous-set-function-overrides
  319. :group 'ido-ubiquitous)
  320. ;;; ido-ubiquitous core
  321. (defvar ido-ubiquitous-next-call-replaces-completing-read nil
  322. "If t, then the next call to `ido-completing-read' is by ido-ubiquitous.")
  323. (defvar ido-ubiquitous-this-call-replaces-completing-read nil
  324. "If t, then the current call to `ido-completing-read' is by ido-ubiquitous.")
  325. (defvar ido-ubiquitous-next-override nil
  326. "This holds the override to be applied on the next call to `completing-read'.")
  327. (defvar ido-ubiquitous-active-override nil
  328. "This holds the override being applied to the current call to `completing-read'.")
  329. (defun ido-ubiquitous-completing-read (&rest args)
  330. "Wrapper for `ido-completing-read' that enables ido-ubiquitous features."
  331. (let ((ido-ubiquitous-next-call-replaces-completing-read t))
  332. (apply 'ido-completing-read args)))
  333. (defadvice ido-completing-read (around detect-replacing-cr activate)
  334. "Enable workarounds if this call was done through ido-ubiquitous.
  335. This advice implements the logic required for
  336. `ido-completing-read' to handle a number of special cases that
  337. `completing-read' can handle. It only has an effect if
  338. `ido-completing-read' is called through
  339. `ido-ubiquitous-completing-read', so other packages that use
  340. `ido-completing-read', such as `smex', will not be affected."
  341. (let* ((ido-ubiquitous-this-call-replaces-completing-read ido-ubiquitous-next-call-replaces-completing-read)
  342. (ido-ubiquitous-next-call-replaces-completing-read nil)
  343. (error-during-setup nil))
  344. (when ido-ubiquitous-this-call-replaces-completing-read
  345. (condition-case nil
  346. (progn
  347. ;; ido doesn't natively handle DEF being a list. If DEF is
  348. ;; a list, prepend it to CHOICES and set DEF to just the
  349. ;; car of the default list.
  350. (when (and def (listp def))
  351. (setq choices (delete-dups (append def choices))
  352. def (car def)))
  353. ;; Work around a bug in ido when both INITIAL-INPUT and
  354. ;; DEF are provided More info:
  355. ;; https://github.com/technomancy/ido-ubiquitous/issues/18
  356. (let ((initial (cond ((null initial-input) "")
  357. ((stringp initial-input) initial-input)
  358. ((consp initial-input) (car initial-input))
  359. (t initial-input)))
  360. (deflist (if (listp def)
  361. def
  362. (list def))))
  363. (when (and deflist initial
  364. (stringp initial)
  365. (not (string= initial "")))
  366. ;; Both default and initial input were provided. So
  367. ;; keep the initial input and preprocess the choices
  368. ;; list to put the default at the head, then proceed
  369. ;; with default = nil.
  370. (setq choices (delete-dups (append deflist choices))
  371. def nil))))
  372. (error
  373. (progn
  374. (warn "ido-ubiquitous: failed during setup. Falling back to standard completion")
  375. (setq error-during-setup t)))))
  376. ;; For ido-ubiquitous, only attempt ido completion if setup completed without error
  377. (if (not error-during-setup)
  378. ad-do-it
  379. (setq ad-return-value
  380. (funcall
  381. ido-ubiquitous-fallback-completing-read-function
  382. prompt choices predicate require-match initial-input
  383. hist def inherit-input-method)))))
  384. (defun completing-read-ido (prompt collection &optional predicate
  385. require-match initial-input
  386. hist def inherit-input-method)
  387. "ido-based method for reading from the minibuffer with completion.
  388. See `completing-read' for the meaning of the arguments.
  389. This function is a wrapper for `ido-completing-read' designed to
  390. be used as the value of `completing-read-function'. Importantly,
  391. it detects edge cases that ido cannot handle and uses normal
  392. completion for them."
  393. ;; Pre-expand list of possible completions (but we can't handle a
  394. ;; collection that is a function)
  395. (unless (functionp collection)
  396. (setq collection (all-completions "" collection predicate)
  397. ;; Don't need this any more
  398. predicate nil))
  399. (let* (;; Set the active override and clear the "next" one so it
  400. ;; doesn't apply to nested calls.
  401. (ido-ubiquitous-active-override ido-ubiquitous-next-override)
  402. (ido-ubiquitous-next-override nil)
  403. ;; Check for conditions that ido can't or shouldn't handle
  404. (ido-allowed
  405. (and ido-mode
  406. ido-ubiquitous-mode
  407. ;; Don't use ido if there are no completions, or if the
  408. ;; collection is a function.
  409. (and (not (functionp collection))
  410. collection)
  411. ;; Check for disable override
  412. (not (eq ido-ubiquitous-active-override 'disable))
  413. ;; Can't handle this arg
  414. (not inherit-input-method)
  415. ;; Can't handle this being set
  416. (not (bound-and-true-p completion-extra-properties))))
  417. (comp-read-fun
  418. (if ido-allowed
  419. 'ido-ubiquitous-completing-read
  420. ido-ubiquitous-fallback-completing-read-function)))
  421. (funcall comp-read-fun
  422. prompt collection predicate
  423. require-match initial-input
  424. hist def inherit-input-method)))
  425. ;;; Old-style default support
  426. (defvar ido-ubiquitous-initial-item nil
  427. "The first item selected when ido starts.
  428. This is initialized to the first item in the list of completions
  429. when ido starts, and is cleared when any character is entered
  430. into the prompt or the list is cycled. If it is non-nil and still
  431. equal to the first item in the completion list when ido exits,
  432. then if `ido-ubiquitous-enable-old-style-default' is
  433. non-nil, ido returns an empty string instead of the first item on
  434. the list.")
  435. (defadvice ido-read-internal (before clear-initial-item activate)
  436. (setq ido-ubiquitous-initial-item nil))
  437. (defadvice ido-make-choice-list (after set-initial-item activate)
  438. (when (and ad-return-value (listp ad-return-value))
  439. (setq ido-ubiquitous-initial-item (car ad-return-value))))
  440. (defadvice ido-next-match (after clear-initial-item activate)
  441. (setq ido-ubiquitous-initial-item nil))
  442. (defadvice ido-prev-match (after clear-initial-item activate)
  443. (setq ido-ubiquitous-initial-item nil))
  444. (defadvice ido-exit-minibuffer (around compatibility activate)
  445. "Emulate a quirk of `completing-read'.
  446. > If the input is null, `completing-read' returns DEF, or the
  447. > first element of the list of default values, or an empty string
  448. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  449. See `ido-ubiquitous-enable-old-style-default', which
  450. controls whether this advice has any effect."
  451. (condition-case nil
  452. (let* ((enable-oldstyle
  453. (and
  454. ;; Completing a list, not a buffer or file
  455. (eq ido-cur-item 'list)
  456. ;; Only enable if we are replacing `completing-read'
  457. ido-ubiquitous-this-call-replaces-completing-read
  458. ;; Default is nil
  459. (null ido-default-item)
  460. ;; Input is empty
  461. (string= ido-text "")
  462. ;; Old-style default enabled
  463. (if ido-ubiquitous-active-override
  464. (eq ido-ubiquitous-active-override 'enable-old)
  465. ido-ubiquitous-enable-old-style-default)
  466. ;; First item on the list hasn't changed
  467. (string= (car ido-cur-list)
  468. ido-ubiquitous-initial-item)))
  469. ;; Prefix inverts oldstyle behavior
  470. (should-invert current-prefix-arg)
  471. (actually-enable-oldstyle
  472. (if should-invert (not enable-oldstyle) enable-oldstyle)))
  473. (if actually-enable-oldstyle
  474. (ido-select-text)
  475. ad-do-it))
  476. (error ad-do-it))
  477. (setq ido-ubiquitous-initial-item nil))
  478. ;;; Overrides
  479. (defun ido-ubiquitous-restore-default-overrides (&optional save)
  480. "Re-add the default overrides for ido-ubiquitous.
  481. This will ensure that the default overrides are all present and
  482. at the head of the list in `ido-ubiquitous-command-overrides' and
  483. `ido-ubiquitous-function-overrides'. User-added overrides will
  484. not be removed, but they may be masked if one of the default
  485. overrides affects the same functions.
  486. With a prefix arg, also save the above variables' new values for
  487. future sessions."
  488. (interactive "P")
  489. (let ((setter (if save
  490. 'customize-save-variable
  491. 'customize-set-variable)))
  492. (cl-loop for (var def) in '((ido-ubiquitous-command-overrides
  493. ido-ubiquitous-default-command-overrides)
  494. (ido-ubiquitous-function-overrides
  495. ido-ubiquitous-default-function-overrides))
  496. do (let* ((curval (eval var))
  497. (defval (eval def))
  498. (newval (delete-dups (append defval curval))))
  499. (funcall setter var newval)))
  500. (message (if save
  501. "ido-ubiquitous: Restored default command and function overrides and saved for future sessions."
  502. "ido-ubiquitous: Restored default command and function overrides for current session only."))))
  503. (defun ido-ubiquitous-spec-match (spec symbol)
  504. "Returns t if SPEC matches SYMBOL (which should be a function name).
  505. See `ido-ubiquitous-command-overrides'."
  506. (when (and symbol (symbolp symbol))
  507. (destructuring-bind (type text) spec
  508. (let ((matcher (cdr (assoc type ido-ubiquitous-spec-matchers)))
  509. (text (ido-ubiquitous--as-string text))
  510. (symname (ido-ubiquitous--as-string symbol)))
  511. (when (null matcher)
  512. (error "ido-ubiquitous: Unknown match spec type \"%s\". See `ido-ubiquitous-spec-matchers' for valid types." type))
  513. (funcall matcher text symname)))))
  514. (defun ido-ubiquitous-get-command-override (cmd)
  515. "Return the override associated with the command CMD.
  516. If there is no override set for CMD in
  517. `ido-ubiquitous-command-overrides', return nil."
  518. (when (and cmd (symbolp cmd))
  519. (cl-loop for (action . spec) in ido-ubiquitous-command-overrides
  520. when (memq action '(disable enable enable-old nil))
  521. when (ido-ubiquitous-spec-match spec cmd)
  522. return action
  523. finally return nil)))
  524. (defadvice call-interactively (around ido-ubiquitous activate)
  525. "Implements the behavior specified in `ido-ubiquitous-command-overrides'."
  526. (ido-ubiquitous-with-override
  527. (ido-ubiquitous-get-command-override (ad-get-arg 0))
  528. ad-do-it))
  529. (defadvice command-execute (around ido-ubiquitous activate)
  530. "Implements the behavior specified in `ido-ubiquitous-command-overrides'."
  531. (ido-ubiquitous-with-override
  532. (ido-ubiquitous-get-command-override
  533. ;; Ugly hack because Emacs byte compiler doesn't know that CMD
  534. ;; is defined for some reason
  535. (bound-and-true-p cmd))
  536. ad-do-it))
  537. ;;; Workaround for https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/24
  538. ;; When `call-interactively' is advised, `called-interactively-p'
  539. ;; always returns nil. So we redefine it (and `interactive-p') to test
  540. ;; the correct condition.
  541. (defsubst ido-ubiquitous--looks-like-advised-orig (func)
  542. "Returns t if FUNC is a symbol starting with \"ad-Orig-\".
  543. Such symbols are used to store the original definitions of
  544. functions that have been advised by `defadvice' or similar."
  545. (and (symbolp func)
  546. (string-prefix-p "ad-Orig-" (symbol-name func))))
  547. (defsubst ido-ubiquitous--looks-like-call-interactively (func)
  548. "Returns t if FUNC looks like the function `call-interactively'.
  549. FUNC \"looks like\" `call-interactively' if it is the literal
  550. symbol `call-interactively', or the value of `(symbol-function
  551. 'call-interactively)', or a symbol whose `symbol-function' is the
  552. same as that of `call-interactively'.
  553. This function is used to determine whether a given function was
  554. \"called by\" `call-interactively' and therefore was called
  555. interactively."
  556. (when func
  557. (eq (symbol-function 'call-interactively)
  558. (if (symbolp func)
  559. (symbol-function func)
  560. func))))
  561. (defun ido-ubiquitous--backtrace-from (fun)
  562. "Return all backtrace frames, starting with the one for FUN.
  563. FUN may be a list of functions, in which case the first one found
  564. on the stack will be used."
  565. (let ((stack (macroexp--backtrace))
  566. (funcs (if (functionp fun)
  567. (list fun)
  568. fun)))
  569. (while (and stack
  570. (not (memq (cadar stack) funcs)))
  571. (setq stack (cdr stack)))
  572. stack))
  573. (defun ido-ubiquitous--clean-advice-from-backtrace (stack)
  574. "Takes a stack trace and cleans all evidence of advice.
  575. Specifically, for each call to a function starting with
  576. \"ad-Orig-\", that call and all prior calls up to but not
  577. including the advised function's original name are deleted from
  578. the stack."
  579. (let ((skipping-until nil))
  580. (cl-loop for frame in stack
  581. for func = (cadr frame)
  582. ;; Check if we found the frame we we're skipping to
  583. if (and skipping-until
  584. (eq func skipping-until))
  585. do (setq skipping-until nil)
  586. ;; If we're looking at an the original form of an advised
  587. ;; function, skip until the real name of that function.
  588. if (and (not skipping-until)
  589. (ido-ubiquitous--looks-like-advised-orig func))
  590. do (setq skipping-until
  591. (intern
  592. (substring (symbol-name func)
  593. (eval-when-compile (length "ad-Orig-")))))
  594. unless skipping-until collect frame)))
  595. (defsubst ido-ubiquitous--interactive-internal ()
  596. "Eqivalent of the INTERACTIVE macro in the Emacs C source.
  597. This is an internal function that should never be called
  598. directly.
  599. See the C source for the logic behind this function."
  600. (and (not executing-kbd-macro)
  601. (not noninteractive)))
  602. (defun ido-ubiquitous--interactive-p-internal ()
  603. "Equivalent of C function \"interactive_p\".
  604. This is an internal function that should never be called
  605. directly.
  606. See the C source for the logic behind this function."
  607. (let ((stack
  608. ;; We clean advice from the backtrace. This ensures that we
  609. ;; get the right answer even if `call-interactively' has been
  610. ;; advised.
  611. (ido-ubiquitous--clean-advice-from-backtrace
  612. (cdr
  613. (ido-ubiquitous--backtrace-from
  614. '(called-interactively-p interactive-p))))))
  615. ;; See comments in the C function for the logic here.
  616. (while (and stack
  617. (or (eq (cadar stack) 'bytecode)
  618. (null (caar stack))))
  619. (setq stack (cdr stack)))
  620. ;; Top of stack is now the function that we want to know
  621. ;; about. Pop it, then check if the next function is
  622. ;; `call-interactively', using a more permissive test than the default.
  623. (ido-ubiquitous--looks-like-call-interactively (cadadr stack))))
  624. (defadvice interactive-p (around ido-ubiquitous activate)
  625. "Return the correct result when `call-interactively' is advised."
  626. (condition-case nil
  627. (setq ad-return-value
  628. (and (ido-ubiquitous--interactive-internal)
  629. (ido-ubiquitous--interactive-p-internal)))
  630. ;; In case of error in the advice, fall back to the default
  631. ;; implementation
  632. ad-do-it))
  633. (defadvice called-interactively-p (around ido-ubiquitous activate)
  634. "Return the correct result when `call-interactively' is advised."
  635. (condition-case nil
  636. (setq ad-return-value
  637. (and (or (ido-ubiquitous--interactive-internal)
  638. (not (eq kind 'interactive)))
  639. (ido-ubiquitous--interactive-p-internal)))
  640. ;; In case of error in the advice, fall back to the default
  641. ;; implementation
  642. ad-do-it))
  643. ;;; Other
  644. (defun ido-ubiquitous-warn-about-ido-disabled ()
  645. "Warn if ido-ubiquitous is enabled without ido.
  646. Don't warn if emacs is still initializing, since ido-ubiquitous
  647. could be enabled first during init."
  648. (when (and ido-ubiquitous-mode
  649. after-init-time
  650. (not (bound-and-true-p ido-mode)))
  651. (warn "ido-ubiquitous-mode enabled without ido mode. ido-ubiquitous requires ido mode to be enabled.")))
  652. (defun ido-ubiquitous-initialize ()
  653. "Do initial setup for ido-ubiquitous.
  654. This only needs to be called once when the file is first loaded."
  655. ;; Clean up old versions of ido-ubiquitous that defined advice on
  656. ;; `completing-read' instead of modifying
  657. ;; `completing-read-function'.
  658. (when (ad-find-advice 'completing-read 'around 'ido-ubiquitous)
  659. (ad-remove-advice 'completing-read 'around 'ido-ubiquitous)
  660. (ad-activate 'completing-read))
  661. ;; Make sure the mode is turned on/off as specified by the value of
  662. ;; the mode variable
  663. (ido-ubiquitous-mode (if ido-ubiquitous-mode 1 0)))
  664. (ido-ubiquitous-initialize)
  665. (provide 'ido-ubiquitous) ;;; ido-ubiquitous.el ends here