ido-ubiquitous.el 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. ;;; ido-ubiquitous.el --- Use ido (nearly) everywhere. -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2011-2015 Ryan C. Thompson
  3. ;; Author: Ryan C. Thompson
  4. ;; URL: https://github.com/DarwinAwardWinner/ido-ubiquitous
  5. ;; Version: 3.12
  6. ;; Created: 2011-09-01
  7. ;; Keywords: convenience, completion, ido
  8. ;; EmacsWiki: InteractivelyDoThings
  9. ;; Package-Requires: ((emacs "24.1") (ido-completing-read+ "3.12") (cl-lib "0.5"))
  10. ;; Filename: ido-ubiquitous.el
  11. ;; This file is NOT part of GNU Emacs.
  12. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  13. ;;
  14. ;;; Commentary:
  15. ;; If you use the excellent `ido-mode' for efficient completion of
  16. ;; file names and buffers, you might wonder if you can get ido-style
  17. ;; completion everywhere else too. Well, that's what this package
  18. ;; does! ido-ubiquitous is here to enable ido-style completion for
  19. ;; (almost) every function that uses the standard completion function
  20. ;; `completing-read'.
  21. ;; To use this package, call `ido-ubiquitous-mode' to enable the mode,
  22. ;; or use `M-x customize-variable ido-ubiquitous-mode' it to enable it
  23. ;; permanently. Once the mode is enabled, most functions that use
  24. ;; `completing-read' will now have ido completion. If you decide in
  25. ;; the middle of a command that you would rather not use ido, just C-f
  26. ;; or C-b at the end/beginning of the input to fall back to non-ido
  27. ;; completion (this is the same shortcut as when using ido for buffers
  28. ;; or files).
  29. ;; Note that `completing-read' has some quirks and complex behavior
  30. ;; that ido cannot emulate. Ido-ubiquitous attempts to detect some of
  31. ;; these quirks and avoid using ido when it sees them. So some
  32. ;; functions will not have ido completion even when this mode is
  33. ;; enabled. Some other functions have ido disabled in them because
  34. ;; their packages already provide support for ido via other means (for
  35. ;; example, magit). See `M-x customize-group ido-ubiquitous' and read
  36. ;; about the override variables for more information.
  37. ;; ido-ubiquitous version 3.0 is a major update, including a split
  38. ;; into two packages, and some of the configuration options have
  39. ;; changed in non-backwards-compatible ways. If you have customized
  40. ;; ido-ubiquitous, be sure to check out `M-x customize-group
  41. ;; ido-ubiquitous' and `M-x customize-group ido-completing-read+'
  42. ;; after updating to 3.0 and make sure the new settings are to your
  43. ;; liking.
  44. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  45. ;;
  46. ;; This program is free software: you can redistribute it and/or modify
  47. ;; it under the terms of the GNU General Public License as published by
  48. ;; the Free Software Foundation, either version 3 of the License, or (at
  49. ;; your option) any later version.
  50. ;;
  51. ;; This program is distributed in the hope that it will be useful, but
  52. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  53. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  54. ;; General Public License for more details.
  55. ;;
  56. ;; You should have received a copy of the GNU General Public License
  57. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  58. ;;
  59. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  60. ;;
  61. ;;; Code:
  62. (defconst ido-ubiquitous-version "3.12"
  63. "Currently running version of ido-ubiquitous.
  64. Note that when you update ido-ubiquitous, this variable may not
  65. be updated until you restart Emacs.")
  66. (eval-when-compile
  67. (when (or (not (boundp 'completing-read-function))
  68. (< emacs-major-version 24))
  69. (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 (or upgrade Emacs).")))
  70. (require 'ido)
  71. (require 'advice)
  72. (require 'cl-lib)
  73. (require 'cus-edit)
  74. (require 'ido-completing-read+)
  75. ;; Only exists in emacs 24.4 and up; we don't use this library
  76. ;; directly, but we load it here so we can test if it's available,
  77. ;; because if it isn't we need enable a workaround.
  78. (require 'nadvice nil 'noerror)
  79. ;;; Debug messages
  80. ;;;###autoload
  81. (define-minor-mode ido-ubiquitous-debug-mode
  82. "If non-nil, ido-ubiquitous will print debug info.
  83. Debug info is printed to the *Messages* buffer."
  84. nil
  85. :global t
  86. :group 'ido-ubiquitous)
  87. ;; Defined as a macro for efficiency (args are not evaluated unless
  88. ;; debug mode is on)
  89. (defmacro ido-ubiquitous--debug-message (format-string &rest args)
  90. `(when ido-ubiquitous-debug-mode
  91. (message (concat "ido-ubiquitous: " ,format-string) ,@args)))
  92. (defun ido-ubiquitous--explain-fallback (arg)
  93. ;; This function accepts a string, or an ido-ubiquitous-fallback
  94. ;; signal.
  95. (when ido-ubiquitous-debug-mode
  96. (when (and (listp arg)
  97. (eq (car arg) 'ido-ubiquitous-fallback))
  98. (setq arg (cdr arg)))
  99. (ido-ubiquitous--debug-message "Falling back to `%s' because %s."
  100. ido-cr+-fallback-function arg)))
  101. ;;; Internal utility functions
  102. (defun ido-ubiquitous--as-string (sym-or-str)
  103. "Return name of symbol, return string as is."
  104. (if (symbolp sym-or-str)
  105. (symbol-name sym-or-str)
  106. sym-or-str))
  107. (defun ido-ubiquitous--as-symbol (sym-or-str)
  108. "Return string as symbol, return symbol as is."
  109. (if (symbolp sym-or-str)
  110. sym-or-str
  111. (intern sym-or-str)))
  112. ;;; Custom widget definitions
  113. ;; We need to define some custom widget types for use in the override
  114. ;; variables.
  115. (define-widget 'lazy-notag 'lazy
  116. "Like lazy widget, but does not display its tag, only its value."
  117. :format "%v")
  118. ;; Define matcher functions and widgets for match specifications
  119. (defvar ido-ubiquitous-match-spec-widget-types nil
  120. "List of widget names for match specs.")
  121. (defvar ido-ubiquitous-spec-matchers nil
  122. "Alist of functions for matching function specs against function names.")
  123. (cl-loop for (widget-name widget-tag key field-type matcher) in
  124. '((exact-match "Exact match" exact string string=)
  125. (prefix-match "Prefix match" prefix string string-prefix-p)
  126. (regexp-match "Regexp match" regexp regexp string-match-p))
  127. do (define-widget (ido-ubiquitous--as-symbol widget-name) 'lazy-notag widget-tag
  128. :menu-tag widget-tag
  129. :type `(list :tag ,widget-tag :format "%v"
  130. (const :format ""
  131. :tag ,widget-tag
  132. ,key)
  133. (,field-type :tag ,widget-tag)))
  134. do (add-to-list 'ido-ubiquitous-match-spec-widget-types
  135. widget-name 'append)
  136. do (add-to-list 'ido-ubiquitous-spec-matchers
  137. (cons key matcher) 'append))
  138. (define-widget 'ido-ubiquitous-match-spec 'lazy-notag
  139. "Choice of exact, prefix, or regexp match."
  140. :type `(choice :tag "Match type"
  141. ,@ido-ubiquitous-match-spec-widget-types))
  142. (define-widget 'ido-ubiquitous-command-override-spec 'lazy-notag
  143. "Choice of override action plus match specification."
  144. :type '(cons :tag "Override rule"
  145. (choice :tag "For matching commands"
  146. (const :menu-tag "Disable"
  147. :tag "Disable ido-ubiquitous"
  148. disable)
  149. (const :menu-tag "Enable"
  150. :tag "Enable ido-ubiquitous in normal default mode"
  151. enable)
  152. (const :menu-tag "Enable old-style default"
  153. :tag "Enable ido-ubiquitous in old-style default mode"
  154. enable-old))
  155. ido-ubiquitous-match-spec))
  156. (define-widget 'ido-ubiquitous-function-override-spec 'lazy-notag
  157. "Choice of override action and function name. (Exact match only.)"
  158. :type '(list :tag "Override rule"
  159. (choice :tag "Do the following"
  160. (const :menu-tag "Disable"
  161. :tag "Disable ido-ubiquitous"
  162. disable)
  163. (const :menu-tag "Enable"
  164. :tag "Enable ido-ubiquitous in normal default mode"
  165. enable)
  166. (const :menu-tag "Enable old-style default"
  167. :tag "Enable ido-ubiquitous in old-style default mode"
  168. enable-old))
  169. (const :format "" exact)
  170. (string :tag "For function")))
  171. ;;; Custom Declarations
  172. (defgroup ido-ubiquitous nil
  173. "Use ido for (almost) all completion."
  174. :group 'ido
  175. :group 'ido-completing-read-plus)
  176. ;;;###autoload
  177. (define-obsolete-variable-alias 'ido-ubiquitous
  178. 'ido-ubiquitous-mode "ido-ubiquitous 0.8")
  179. ;;;###autoload
  180. (define-obsolete-function-alias 'ido-ubiquitous
  181. 'ido-ubiquitous-mode "ido-ubiquitous 0.8")
  182. ;;;###autoload
  183. (define-minor-mode ido-ubiquitous-mode
  184. "Use `ido-completing-read' instead of `completing-read' almost everywhere.
  185. If this mode causes problems for a function, you can customize
  186. when ido completion is or is not used by customizing
  187. `ido-ubiquitous-command-overrides' or
  188. `ido-ubiquitous-function-overrides'."
  189. nil
  190. :global t
  191. :group 'ido-ubiquitous
  192. ;; Actually enable/disable the mode by setting
  193. ;; `completing-read-function'.
  194. (setq completing-read-function
  195. (if ido-ubiquitous-mode
  196. #'completing-read-ido-ubiquitous
  197. ido-cr+-fallback-function)))
  198. ;; Variables for functionality that has moved to ido-completing-read+
  199. (define-obsolete-variable-alias
  200. 'ido-ubiquitous-max-items
  201. 'ido-cr+-max-items
  202. "ido-ubiquitous 3.0")
  203. (define-obsolete-variable-alias
  204. 'ido-ubiquitous-fallback-completing-read-function
  205. 'ido-cr+-fallback-function
  206. "ido-ubiquitous 3.0")
  207. (define-obsolete-variable-alias
  208. 'ido-ubiquitous-enable-compatibility-globally
  209. 'ido-ubiquitous-enable-old-style-default
  210. "ido-ubiquitous 2.0")
  211. (defcustom ido-ubiquitous-default-state 'enable
  212. "Default ido-ubiquitous mode of operation for commands with no override.
  213. This can be set to one of three options:
  214. * `enable': use normal ido completion;
  215. * `enable-old': use ido completion, but with emulation of the
  216. old-style default selection of `completing-read';
  217. * `disable': use non-ido completion.
  218. Command-specific and function-specific overrides are available to
  219. override this default for specific commands/functions. See
  220. `ido-ubiquitous-command-overrides' and
  221. `ido-ubiquitous-function-overrides'.
  222. The `enable-old' option swaps the behavior of RET and C-j but
  223. only for the first keypress after beginning completion.
  224. Specifically, on the first keypress, RET will return an empty
  225. string and C-j will return the first item on the list. The
  226. purpose of this is to emulate a legacy compatibility quirk of
  227. `completing-read'. From the `completing-read' docstring:
  228. > If the input is null, `completing-read' returns DEF, or the
  229. > first element of the list of default values, or an empty string
  230. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  231. This odd behavior is required for compatibility with an old-style
  232. usage pattern whereby the default was requested by returning an
  233. empty string. In this mode, the caller receives the empty string
  234. and handles the default case manually, while `completing-read'
  235. never has any knowledge of the default. This is a problem for
  236. ido, which normally returns the first element in the list, not an
  237. empty string, when the input is empty and you press RET. Without
  238. knowledge of the default, it cannot ensure that the default is
  239. first on the list, so returning the first item is not the correct
  240. behavior. Instead, it must return an empty string like
  241. `completing-read'.
  242. The `disable' mode is available as a default, which seems
  243. counterintuitive. But this allows you, if you so desire, to
  244. enable ido-ubiquitous selectively for only a few specific commands
  245. using overrides and disable it for everything else."
  246. :type '(choice :tag "Default mode"
  247. (const :menu-tag "Disable"
  248. :tag "Disable ido-ubiquitous"
  249. disable)
  250. (const :menu-tag "Enable"
  251. :tag "Enable ido-ubiquitous in normal default mode"
  252. enable)
  253. (const :menu-tag "Enable old-style default"
  254. :tag "Enable ido-ubiquitous in old-style default mode"
  255. enable-old))
  256. :group 'ido-ubiquitous)
  257. (make-obsolete-variable
  258. 'ido-ubiquitous-enable-old-style-default
  259. "This variable no longer has any effect. Set
  260. `ido-ubiquitous-default-state' to `enable-old' instead."
  261. "ido-ubiquitous 3.0")
  262. (defconst ido-ubiquitous-default-command-overrides
  263. '(;; If you want ido for M-x, install smex
  264. (disable exact "execute-extended-command")
  265. ;; Wanderlust uses new-style default
  266. (enable prefix "wl-")
  267. ;; Info functions use old-style default selection
  268. (enable-old prefix "Info-")
  269. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/4
  270. (enable exact "webjump")
  271. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/28
  272. (enable regexp "\\`\\(find\\|load\\|locate\\)-library\\'")
  273. ;; https://github.com/bbatsov/prelude/issues/488
  274. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/44
  275. ;; tmm implements its own non-standard completion mechanics
  276. (disable prefix "tmm-")
  277. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/47
  278. ;; theme functions don't need old-style compatibility
  279. (enable regexp "\\`\\(load\\|enable\\|disable\\|describe\\|custom-theme-visit\\)-theme\\'")
  280. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/79
  281. (enable-old prefix "bbdb-")
  282. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/83
  283. (enable-old exact "where-is")
  284. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/60
  285. (disable exact "todo-add-category")
  286. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/51
  287. (enable exact "find-tag")
  288. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/89
  289. (enable prefix "etags-select-")
  290. ) ; Close paren on separate line for better VC diffs
  291. "Default value of `ido-ubiquitous-command-overrides'.
  292. You can restore these using the command `ido-ubiquitous-restore-default-overrides'.")
  293. (defconst ido-ubiquitous-default-function-overrides
  294. '((disable exact "read-file-name")
  295. (disable exact "read-file-name-internal")
  296. (disable exact "read-buffer")
  297. (disable exact "gnus-emacs-completing-read")
  298. (disable exact "gnus-iswitchb-completing-read")
  299. (disable exact "grep-read-files")
  300. (disable exact "magit-builtin-completing-read")
  301. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/36
  302. (enable exact "bookmark-completing-read")
  303. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/4
  304. (enable-old exact "webjump-read-choice")
  305. (enable-old exact "webjump-read-url-choice")
  306. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/9
  307. (disable exact "isearchp-read-unicode-char")
  308. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/38
  309. (enable exact "read-char-by-name")
  310. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/39
  311. (disable exact "Info-read-node-name")
  312. ;; https://github.com/purcell/emacs.d/issues/182#issuecomment-44212927
  313. (disable exact "tmm-menubar")
  314. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/58
  315. ;; https://github.com/mooz/js2-mode/issues/181
  316. (enable exact "imenu--completion-buffer")
  317. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/74
  318. (enable-old exact "auto-insert"))
  319. "Default value of `ido-ubiquitous-function-overrides'.
  320. You can restore these using the command `ido-ubiquitous-restore-default-overrides'.")
  321. (defcustom ido-ubiquitous-command-overrides ido-ubiquitous-default-command-overrides
  322. "List of command override specifications for ido-ubiquitous
  323. Each override specification describes how ido-ubiquitous should
  324. behave one or many commands. A specification has the
  325. form `(BEHAVIOR MATCH-TYPE MATCH-TEXT)'. BEHAVIOR is one of the
  326. following:
  327. * `disable': ido-ubiquitous should not be used at all for the
  328. specified commands;
  329. * `enable': ido-ubiquitous may be used with the specified
  330. commands, without emulating the old-style default selection
  331. of `completing-read';
  332. * `enable-old': ido-ubiquitous may be used with the specified
  333. commands, and should emulate the old-style default selection
  334. of `completing-read'.
  335. MATCH-TYPE affects how MATCH-TEXT is interpreted, as follows:
  336. * `exact': the specification only affects the one command
  337. whose name is MATCH-TEXT;
  338. * `prefix': the specification affects any command whose name
  339. starts with MATCH-TEXT (This is useful for specifying a
  340. certain behavior for an entire package);
  341. * `regexp': the specification affects any command whose name
  342. matches MATCH-TEXT (with MATCH-TEXT being interpreted as a
  343. regular expression)
  344. MATCH-TEXT should be a string.
  345. Since this variable's has a somewhat complex structure, it is
  346. recommended that you set this variable through Customize.
  347. Note that this variable only affects *commands*, which are
  348. functions marked as interactive. See
  349. `ido-ubiquitous-function-overrides' for how to modify the
  350. behavior of ido-ubiquitous for arbitrary functions.
  351. Note: If multiple overrides match the same commmand, the first
  352. one in the list will take precedence.
  353. If you need to add a new specification to this list, please also
  354. file a bug report at https://github.com/DarwinAwardWinner/ido-ubiquitous/issues"
  355. :type '(repeat ido-ubiquitous-command-override-spec)
  356. :group 'ido-ubiquitous)
  357. (defmacro ido-ubiquitous-with-override (override &rest body)
  358. "Eval BODY with specified OVERRIDE in place.
  359. The OVERRIDE argument is evaluated normally, so if it is a
  360. literal symbol, it must be quoted.
  361. See `ido-ubiquitous-command-overrides' for valid override types."
  362. (declare (indent 1))
  363. ;; Eval override
  364. `(let ((ido-ubiquitous-next-override ,override))
  365. ,@body))
  366. (defun ido-ubiquitous-apply-function-override (func override)
  367. "Set the override property on FUNC to OVERRIDE and set up advice to apply the override."
  368. (setq func (ido-ubiquitous--as-symbol func)
  369. override (ido-ubiquitous--as-symbol override))
  370. (if (memq override '(disable enable enable-old nil))
  371. (progn
  372. (put func 'ido-ubiquitous-override override)
  373. (when override
  374. (let ((docstring
  375. (format "Override ido-ubiquitous behavior in %s if its `ido-ubiquitous-override' property is non-nil." func)))
  376. (eval
  377. `(defadvice ,func (around ido-ubiquitous-override activate)
  378. ,docstring
  379. (let* ((func ',func)
  380. (override (get func 'ido-ubiquitous-override)))
  381. (when override
  382. (ido-ubiquitous--debug-message
  383. "Using override `%s' for function `%s'"
  384. override func))
  385. (ido-ubiquitous-with-override override
  386. ad-do-it)))))))
  387. (display-warning
  388. 'ido-ubiquitous
  389. (format "Ignoring invalid override action `%s' for function `%s' found in `ido-ubiquitous-function-overrides'."
  390. override func)
  391. :warning)))
  392. (defun ido-ubiquitous-set-function-overrides (sym newval)
  393. "Custom setter function for `ido-ubiquitous-function-overrides'.
  394. In addition to setting the variable, this also sets up advice on
  395. each function to apply the appropriate override."
  396. ;; Unset all previous overrides
  397. (when (boundp sym)
  398. (let ((oldval (eval sym)))
  399. (cl-loop for (_action _match-type func) in oldval
  400. do (ido-ubiquitous-apply-function-override func nil))))
  401. ;; Ensure that function names are strings, not symbols
  402. (setq newval
  403. (cl-loop for (action match-type func) in newval
  404. collect (list action match-type
  405. (ido-ubiquitous--as-string func))))
  406. ;; set new overrides
  407. (cl-loop for override in newval
  408. for (action match-type func) = override
  409. ;; Remove duplicate overrides
  410. if (member func overridden-functions)
  411. do (display-warning
  412. 'ido-ubiquitous
  413. (format
  414. "Removing duplicate override for function `%s'" func))
  415. ;; Apply valid overrides
  416. else if (eq match-type 'exact)
  417. do (ido-ubiquitous-apply-function-override func action)
  418. and collect func into overridden-functions
  419. and collect override into final-value
  420. ;; Remove invalid overrides
  421. else
  422. do (display-warning
  423. 'ido-ubiquitous
  424. (format
  425. "Removing invalid function override match-type `%s' for function `%s'; only match-type `exact' is supported in `ido-ubiquitous-function-overrides'."
  426. match-type func))
  427. ;; Set the value to only the overrides that were actually
  428. ;; applied.
  429. finally return
  430. (set-default sym final-value)))
  431. (defcustom ido-ubiquitous-auto-update-overrides 'notify
  432. "Whether to add new overrides when updating ido-ubiquitous.
  433. Ido-ubiquitous comes with a default set of overrides for commands
  434. that are known to require them. New versions of ido-ubiquitous
  435. may come with updates to the default overrides as more commands
  436. are discovered to require them. However, customizing your own
  437. overrides would normally prevent you from receiving these
  438. updates, since Emacs will not overwrite your customizations.
  439. To resolve this problem, you can set this variable to `t', and
  440. then ido-ubiquitous can automatically add any new built-in
  441. overrides whenever it is updated. (Actually, the update will
  442. happen the next time Emacs is restarted after the update.) This
  443. allows you to add your own overrides but still receive updates to
  444. the default set. The default overrides will always be added with
  445. lower precedence than user-added ones.
  446. If you want ido-ubiquitous to just notify you about new default
  447. overrides instead of adding them itself, set this variable to
  448. `notify'. If you don't want this auto-update behavior at all, set
  449. it to `nil'.
  450. (Note that having this option enabled effectively prevents you
  451. from removing any of the built-in default overrides, since they
  452. will simply be re-added the next time Emacs starts. However, your
  453. custom overrides will still take precedence, so this shouldn't be
  454. a problem.)"
  455. :type '(choice :tag "When new overrides are available:"
  456. (const :menu-tag "Auto-add"
  457. :tag "Add them automatically"
  458. t)
  459. (const :menu-tag "Notify"
  460. :tag "Notify the user about them"
  461. notify)
  462. (const :menu-tag "Ignore"
  463. :tag "Ignore them"
  464. nil))
  465. :group 'ido-ubiquitous)
  466. (defcustom ido-ubiquitous-function-overrides ido-ubiquitous-default-function-overrides
  467. "List of function override specifications for ido-ubiquitous
  468. Function override specifications have a similar structure to
  469. command override specifications (see
  470. `ido-ubiquitous-command-overrides'). A function override
  471. specification has the form `(BEHAVIOR MATCH-TYPE MATCH-TEXT)'.
  472. However, `MATCH-TYPE' may ONLY be `exact'; No other match type is
  473. supported.
  474. Note: If multiple overrides are set for the same function, the
  475. first one in the list will take precedence, and the rest will be
  476. ignored and deleted from the override list, with a warning.
  477. Invalid override specifications will also be deleted with a
  478. warning.
  479. If you need to add a new specification to this list, please also file a
  480. bug report at https://github.com/DarwinAwardWinner/ido-ubiquitous/issues
  481. Setting this variable directly has no effect. You must set it
  482. through Customize."
  483. :type '(repeat ido-ubiquitous-function-override-spec)
  484. :set 'ido-ubiquitous-set-function-overrides
  485. :group 'ido-ubiquitous)
  486. (defcustom ido-ubiquitous-allow-on-functional-collection nil
  487. "Allow ido completion when COLLECTION is a function.
  488. The `completing-read' function allows its COLLECTION argument to
  489. be a function instead of a list of choices. Some such functions
  490. simply return a list of completions and are suitable for use with
  491. ido, but others implement more complex behavior and will result
  492. in incorrect behavior if used with ido. Since there is no way to
  493. tell the difference, this preference defaults to nil, which means
  494. that ido-ubiquitous will not work when COLLECTION is a function
  495. unless there is a specific override in effect. To disable this
  496. safeguard and GUARANTEE ERRORS on some functions, you may set
  497. this to non-nil, but this is not recommended."
  498. :type 'boolean
  499. :group 'ido-ubiquitous)
  500. ;;; ido-ubiquitous core
  501. ;; These variable are used to make ido-ubiquitous work properly in the
  502. ;; case that `completing-read' is called recursively (which is
  503. ;; possible when `enable-recursive-minibuffers' is non-nil.)
  504. (defvar ido-ubiquitous-enable-next-call nil
  505. "If non-nil, then the next call to `ido-completing-read' is by ido-ubiquitous.")
  506. (defvar ido-ubiquitous-enable-this-call nil
  507. "If non-nil, then the current call to `ido-completing-read' is by ido-ubiquitous.")
  508. (defvar ido-ubiquitous-next-override nil
  509. "This holds the override to be applied on the next call to `completing-read'.
  510. It's value can be nil or one of the symbols `disable', `enable', or
  511. `enable-old'.
  512. You should not modify this variable directly. Instead use the
  513. macro `ido-ubiquitous-with-override'.")
  514. (defvar ido-ubiquitous-active-override nil
  515. "This holds the override being applied to the current call to `completing-read'.
  516. It's value can be nil or one of the symbols `disable', `enable', or
  517. `enable-old'.
  518. You should not modify this variable directly. Instead use the
  519. macro `ido-ubiquitous-with-override'.")
  520. (defvar ido-ubiquitous-active-state nil
  521. "This holds the ido-ubiquitous mode of operation for the current call to `completing-read'.
  522. It's value can be nil or one of the symbols `disable', `enable', or
  523. `enable-old'.
  524. You should not modify this variable directly.")
  525. (defadvice ido-completing-read (around ido-ubiquitous activate)
  526. "Enable ido-ubiquitous features if this call was done through ido-ubiquitous.
  527. This advice ensures that `ido-ubiquitous-enable-this-call' is set
  528. properly while `ido-completing-read' is executing. This variable
  529. is used to determine whether to enable certain behaviors only for
  530. ido-ubiquitous, not for ordinary ido completion."
  531. ;; Set "this" and clear "next" so it doesn't apply to nested calls.
  532. (let* ((ido-ubiquitous-enable-this-call ido-ubiquitous-enable-next-call)
  533. (ido-ubiquitous-enable-next-call nil)
  534. (ido-ubiquitous-initial-item nil))
  535. ad-do-it))
  536. ;; Signal used to trigger fallback
  537. (put 'ido-ubiquitous-fallback 'error-conditions '(ido-ubiquitous-fallback error))
  538. (put 'ido-ubiquitous-fallback 'error-message "ido-ubiquitous-fallback")
  539. (defun completing-read-ido-ubiquitous
  540. (prompt collection &optional predicate
  541. require-match initial-input
  542. hist def inherit-input-method)
  543. "ido-based method for reading from the minibuffer with completion.
  544. See `completing-read' for the meaning of the arguments.
  545. This function is a wrapper for `ido-completing-read' designed to
  546. be used as the value of `completing-read-function'. Importantly,
  547. it detects edge cases that ido cannot handle and uses normal
  548. completion for them."
  549. (let (;; Save the original arguments in case we need to do the
  550. ;; fallback
  551. (orig-args
  552. (list prompt collection predicate require-match
  553. initial-input hist def inherit-input-method)))
  554. (condition-case sig
  555. (let* (;; Set the active override and clear the "next" one so it
  556. ;; doesn't apply to nested calls.
  557. (ido-ubiquitous-active-override ido-ubiquitous-next-override)
  558. (ido-ubiquitous-next-override nil)
  559. ;; Apply the active override, if any
  560. (ido-ubiquitous-active-state
  561. (or ido-ubiquitous-active-override
  562. ido-ubiquitous-default-state
  563. 'enable)))
  564. ;; If ido-ubiquitous is disabled this time, fall back
  565. (when (eq ido-ubiquitous-active-state 'disable)
  566. (signal 'ido-ubiquitous-fallback
  567. "`ido-ubiquitous-active-state' is `disable'"))
  568. ;; Handle a collection that is a function: either expand
  569. ;; completion list now or fall back
  570. (when (functionp collection)
  571. (if (or ido-ubiquitous-allow-on-functional-collection
  572. (memq ido-ubiquitous-active-override
  573. '(enable enable-old)))
  574. (setq collection (all-completions "" collection predicate))
  575. (signal 'ido-ubiquitous-fallback
  576. "COLLECTION is a function and there is no override")))
  577. (let ((ido-ubiquitous-enable-next-call t))
  578. (ido-completing-read+
  579. prompt collection predicate require-match
  580. initial-input hist def inherit-input-method)))
  581. ;; Handler for ido-ubiquitous-fallback signal
  582. (ido-ubiquitous-fallback
  583. (ido-ubiquitous--explain-fallback sig)
  584. (apply ido-cr+-fallback-function orig-args)))))
  585. (define-obsolete-function-alias 'completing-read-ido 'completing-read-ido-ubiquitous
  586. "ido-ubiquitous 3.0")
  587. ;;; Old-style default support
  588. (defvar ido-ubiquitous-initial-item nil
  589. "The first item selected when ido starts.
  590. This is initialized to the first item in the list of completions
  591. when ido starts, and is cleared when any character is entered
  592. into the prompt or the list is cycled. If it is non-nil and still
  593. equal to the first item in the completion list when ido exits,
  594. then if `ido-ubiquitous-active-state' is `enable-old', ido
  595. returns an empty string instead of the first item on the list.")
  596. (defmacro ido-ubiquitous-set-initial-item (item)
  597. "Wrapper for `(setq ido-ubiquitous-initial-item ITEM)'.
  598. This wrapper differs from simply doing `(setq
  599. ido-ubiquitous-initial-item ITEM)' in several ways. First, it has
  600. no effect (and does not evaluate ITEM) unless
  601. `ido-ubiquitous-active-state' is `enable-old'. Second, it emits
  602. appropriate debug messages."
  603. `(when (eq ido-ubiquitous-active-state 'enable-old)
  604. (let ((item ,item))
  605. (cond
  606. (item
  607. (ido-ubiquitous--debug-message
  608. "Setting `ido-ubiquitous-initial-item' to `%S'."
  609. item))
  610. (ido-ubiquitous-initial-item
  611. (ido-ubiquitous--debug-message "Clearing `ido-ubiquitous-initial-item'.")))
  612. (setq ido-ubiquitous-initial-item item))))
  613. (defadvice ido-read-internal (before ido-ubiquitous-clear-initial-item activate)
  614. (ido-ubiquitous-set-initial-item nil))
  615. (defadvice ido-make-choice-list (after ido-ubiquitous-set-initial-item activate)
  616. (ido-ubiquitous-set-initial-item
  617. (when (and ad-return-value (listp ad-return-value))
  618. (car ad-return-value))))
  619. (defadvice ido-next-match (after ido-ubiquitous-clear-initial-item activate)
  620. (ido-ubiquitous-set-initial-item nil))
  621. (defadvice ido-prev-match (after ido-ubiquitous-clear-initial-item activate)
  622. (ido-ubiquitous-set-initial-item nil))
  623. ;; Clear initial item after `self-insert-command'
  624. (defun ido-ubiquitous-post-insert-hook ()
  625. (eval '(ido-ubiquitous-set-initial-item nil)))
  626. (defun ido-ubiquitous-ido-minibuffer-setup-hook ()
  627. (add-hook
  628. 'post-self-insert-hook
  629. #'ido-ubiquitous-post-insert-hook
  630. nil
  631. 'local))
  632. (add-hook 'ido-minibuffer-setup-hook
  633. #'ido-ubiquitous-ido-minibuffer-setup-hook)
  634. (defun ido-ubiquitous-should-use-old-style-default ()
  635. "Returns non nil if ido-ubiquitous should emulate old-style default.
  636. This function simply encapsulates all the checks that need to be
  637. done in order to decide whether to swap RET and C-j. See
  638. `ido-ubiquitous-default-state' for more information."
  639. ;; These checks outside the loop don't produce debug messages
  640. (and
  641. ;; Only if old-style default enabled
  642. (eq ido-ubiquitous-active-state 'enable-old)
  643. ;; This loop is just an implementation of `and' that reports which
  644. ;; arg was nil for debugging purposes.
  645. (cl-loop
  646. for test in
  647. '((bound-and-true-p ido-cur-item)
  648. ;; Only if completing a list, not a buffer or file
  649. (eq ido-cur-item 'list)
  650. ;; Only if this call was done through ido-ubiquitous
  651. ido-ubiquitous-enable-this-call
  652. ;; Only if default is nil
  653. (null ido-default-item)
  654. ;; Only if input is empty
  655. (string= ido-text "")
  656. ;; Only if `ido-ubiquitous-initial-item' hasn't been cleared
  657. ido-ubiquitous-initial-item
  658. ;; Only if initial item hasn't changed
  659. (string= (car ido-cur-list)
  660. ido-ubiquitous-initial-item))
  661. for test-result = (eval test)
  662. if (not test-result)
  663. do (ido-ubiquitous--debug-message
  664. "Not enabling old-style default selection because `%S' is nil"
  665. test)
  666. and return nil
  667. finally do (ido-ubiquitous--debug-message
  668. "Enabling old-style default selection")
  669. finally return t)))
  670. (defadvice ido-exit-minibuffer (around ido-ubiquitous-old-style-default-compat activate)
  671. "Emulate a quirk of `completing-read'.
  672. > If the input is null, `completing-read' returns DEF, or the
  673. > first element of the list of default values, or an empty string
  674. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  675. See `ido-ubiquitous-default-state', which controls whether this
  676. advice has any effect."
  677. (condition-case nil
  678. (if (ido-ubiquitous-should-use-old-style-default)
  679. (let ((ido-ubiquitous-active-state 'enable))
  680. (ido-select-text))
  681. ad-do-it)
  682. (error
  683. (display-warning 'ido-ubiquitous "Advice on `ido-exit-minibuffer' failed." :warning)
  684. ad-do-it))
  685. (ido-ubiquitous-set-initial-item nil))
  686. (defadvice ido-select-text (around ido-ubiquitous-old-style-default-compat activate)
  687. "Emulate a quirk of `completing-read'.
  688. > If the input is null, `completing-read' returns DEF, or the
  689. > first element of the list of default values, or an empty string
  690. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  691. See `ido-ubiquitous-default-state', which controls whether this
  692. advice has any effect."
  693. (condition-case nil
  694. (if (ido-ubiquitous-should-use-old-style-default)
  695. (let ((ido-ubiquitous-active-state 'enable))
  696. (ido-exit-minibuffer))
  697. ad-do-it)
  698. (error
  699. (display-warning 'ido-ubiquitous "Advice on `ido-select-text' failed." :warning)
  700. ad-do-it))
  701. (ido-ubiquitous-set-initial-item nil))
  702. ;;; Overrides
  703. (defun ido-ubiquitous--overrides-have-same-target-p (o1 o2)
  704. (cl-destructuring-bind (oride1 type1 text1) o1
  705. (cl-destructuring-bind(oride2 type2 text2) o2
  706. ;; Avoid warnings about unused vars
  707. oride1 oride2
  708. (and (string= text1 text2)
  709. (eq type1 type2)))))
  710. (defun ido-ubiquitous--combine-override-lists (olist1 olist2)
  711. "Append OLIST2 to OLIST1, but remove redundant elements.
  712. Redundancy is determined using
  713. `ido-ubiquitous--overrides-have-same-target-p'."
  714. (let ((olist2
  715. (cl-remove-if
  716. (lambda (o2) (cl-member
  717. o2 olist1
  718. :test #'ido-ubiquitous--overrides-have-same-target-p))
  719. olist2)))
  720. (append olist1 olist2)))
  721. (defun ido-ubiquitous-update-overrides (&optional save quiet)
  722. "Re-add the default overrides without erasing custom overrides.
  723. This is useful after an update of ido-ubiquitous that adds new
  724. default overrides. See `ido-ubiquitous-auto-update-overrides' for
  725. more information.
  726. If SAVE is non-nil, also save the overrides to the user's custom
  727. file (but only if they were already customized). When called
  728. interactively, a prefix argument triggers a save.
  729. When called from Lisp code, this function returns the list of
  730. variables whose values were modified. In particular, it returns
  731. non-nil if any variables were modified, and nil if no modifications
  732. were made."
  733. (interactive "P")
  734. (let ((unmodified-vars nil)
  735. (updated-vars nil)
  736. (final-message-lines nil)
  737. (final-message-is-warning nil))
  738. (cl-loop
  739. for (var def) in
  740. '((ido-ubiquitous-command-overrides
  741. ido-ubiquitous-default-command-overrides)
  742. (ido-ubiquitous-function-overrides
  743. ido-ubiquitous-default-function-overrides))
  744. do (let* ((var-state (custom-variable-state var (eval var)))
  745. (curval (eval var))
  746. (defval (eval def))
  747. (newval (ido-ubiquitous--combine-override-lists
  748. curval defval)))
  749. (cond
  750. ;; Nothing to add to var, do nothing
  751. ((and (equal curval newval)
  752. (eq var-state 'saved))
  753. (ido-ubiquitous--debug-message
  754. "No need to modify value of option `%s'"
  755. var)
  756. (push var unmodified-vars))
  757. ;; Var is not customized, just set the new default
  758. ((eq var-state 'standard)
  759. (ido-ubiquitous--debug-message
  760. "Setting uncustomized option `%s' to its default value"
  761. var)
  762. (push var unmodified-vars)
  763. (set var defval))
  764. ;; Var is saved to custom.el, set and save new value (if
  765. ;; SAVE is t)
  766. ((and save
  767. (eq var-state 'saved))
  768. (ido-ubiquitous--debug-message
  769. "Updating option `%s' with new overrides and saving it."
  770. var)
  771. (push var updated-vars)
  772. (customize-save-variable var newval))
  773. ;; Var is modified but not saved (or SAVE is nil), update it
  774. ;; but don't save it
  775. (t
  776. (ido-ubiquitous--debug-message
  777. "Updating option `%s' with new overrides but not saving it for future sessions."
  778. var)
  779. (push var updated-vars)
  780. (customize-set-variable var newval)))))
  781. (unless quiet
  782. ;; Now compose a single message that summarizes what was done
  783. (if (null updated-vars)
  784. (push "No updates to ido-ubiquitous override variables were needed."
  785. final-message-lines)
  786. (push
  787. (format "Updated the following ido-ubiquitous override variables: %S"
  788. (sort updated-vars #'string<))
  789. final-message-lines)
  790. (if save
  791. (push
  792. "All updated variables were successfully saved."
  793. final-message-lines)
  794. (push
  795. "However, they have not been saved for future sessions. To save them, re-run this command with a prefix argument: `C-u M-x ido-ubiquitous-update-overrides'; or else manually inspect and save their values using `M-x customize-group ido-ubiquitous'."
  796. final-message-lines)
  797. (setq final-message-is-warning t)))
  798. (if final-message-is-warning
  799. (display-warning 'ido-ubiquitous
  800. (mapconcat 'identity (nreverse final-message-lines) "\n"))
  801. (message (mapconcat 'identity (nreverse final-message-lines) "\n"))))
  802. updated-vars))
  803. (defun ido-ubiquitous--find-override-updates (current-value available-updates)
  804. (cl-set-difference (ido-ubiquitous--combine-override-lists
  805. current-value available-updates)
  806. current-value))
  807. (defun ido-ubiquitous--maybe-update-overrides ()
  808. "Maybe call `ido-ubiquitous-update-overrides.
  809. See `ido-ubiquitous-auto-update-overrides."
  810. (if ido-ubiquitous-auto-update-overrides
  811. (let* ((command-override-updates
  812. (ido-ubiquitous--find-override-updates
  813. ido-ubiquitous-command-overrides
  814. ido-ubiquitous-default-command-overrides))
  815. (function-override-updates
  816. (ido-ubiquitous--find-override-updates
  817. ido-ubiquitous-function-overrides
  818. ido-ubiquitous-default-function-overrides))
  819. (update-count
  820. (+ (length command-override-updates)
  821. (length function-override-updates))))
  822. (if (> update-count 0)
  823. (if (eq ido-ubiquitous-auto-update-overrides 'notify)
  824. (display-warning
  825. 'ido-ubiquitous
  826. (format "There are %s new overrides available. Use `M-x ido-ubiquitous-update-overrides' to enable them. (See `ido-ubiquitous-auto-update-overrides' for more information.)"
  827. update-count))
  828. (ido-ubiquitous--debug-message "Applying override updates.")
  829. (ido-ubiquitous-update-overrides t))
  830. (ido-ubiquitous--debug-message "No override updates availble.")))
  831. (ido-ubiquitous--debug-message "Skipping override updates by user preference.")))
  832. (define-obsolete-function-alias
  833. 'ido-ubiquitous-restore-default-overrides
  834. 'ido-ubiquitous-update-overrides
  835. "ido-ubiquitous 3.9")
  836. (defun ido-ubiquitous-spec-match (spec symbol)
  837. "Returns t if SPEC matches SYMBOL (which should be a function name).
  838. See `ido-ubiquitous-command-overrides'. If the match spec is
  839. invalid or any other error occurs, the error is demoted to a
  840. warning and the function returns nil."
  841. (condition-case err
  842. (when (and symbol (symbolp symbol))
  843. (cl-destructuring-bind (type text) spec
  844. (let ((matcher (cdr (assoc type ido-ubiquitous-spec-matchers)))
  845. (text (ido-ubiquitous--as-string text))
  846. (symname (ido-ubiquitous--as-string symbol)))
  847. (if matcher
  848. (funcall matcher text symname)
  849. ;; If the matcher is invalid, issue a warning and return
  850. ;; nil.
  851. (error "Unknown match spec type \"%s\". See `ido-ubiquitous-spec-matchers' for valid types." type)
  852. nil))))
  853. (error
  854. (display-warning 'ido-ubiquitous "Error during ido-ubiquitous spec matching: %S" err)
  855. nil)))
  856. (defun ido-ubiquitous-get-command-override (cmd)
  857. "Return the override associated with the command CMD.
  858. If there is no override set for CMD in
  859. `ido-ubiquitous-command-overrides', return nil."
  860. (when (and cmd (symbolp cmd))
  861. (cl-loop for override in ido-ubiquitous-command-overrides
  862. for (action . spec) = override
  863. for valid-action = (and (memq action '(disable enable enable-old nil))
  864. (assoc (car spec) ido-ubiquitous-spec-matchers))
  865. unless valid-action
  866. do (progn
  867. (display-warning
  868. 'ido-ubiquitous
  869. (format "Removing invalid override `%S' from `ido-ubiquitous-command-overrides'"
  870. (cons action spec))
  871. :warning)
  872. (setq ido-ubiquitous-command-overrides
  873. (remove override ido-ubiquitous-command-overrides)))
  874. when (and valid-action (ido-ubiquitous-spec-match spec cmd))
  875. return action
  876. finally return nil)))
  877. ;;; Workaround for https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/24
  878. ;; When `call-interactively' is advised, `called-interactively-p'
  879. ;; always returns nil. So we redefine it (and `interactive-p') to test
  880. ;; the correct condition.
  881. (defsubst ido-ubiquitous--looks-like-advised-orig (func)
  882. "Returns t if FUNC is a symbol starting with \"ad-Orig-\".
  883. Such symbols are used to store the original definitions of
  884. functions that have been advised by `defadvice' or similar."
  885. (and (symbolp func)
  886. (string-prefix-p "ad-Orig-" (symbol-name func))))
  887. (defsubst ido-ubiquitous--looks-like-call-interactively (func)
  888. "Returns t if FUNC looks like the function `call-interactively'.
  889. FUNC \"looks like\" `call-interactively' if it is the literal
  890. symbol `call-interactively', or the value of `(symbol-function
  891. 'call-interactively)', or a symbol whose `symbol-function' is the
  892. same as that of `call-interactively'.
  893. This function is used to determine whether a given function was
  894. \"called by\" `call-interactively' and therefore was called
  895. interactively."
  896. (when func
  897. (eq (symbol-function 'call-interactively)
  898. (if (symbolp func)
  899. (symbol-function func)
  900. func))))
  901. (defun ido-ubiquitous--backtrace-from (fun)
  902. "Return all backtrace frames, starting with the one for FUN.
  903. FUN may be a list of functions, in which case the first one found
  904. on the stack will be used."
  905. (let ((stack
  906. (cl-loop for i upfrom 0
  907. for frame = (backtrace-frame i)
  908. while frame
  909. collect frame))
  910. (funcs (if (functionp fun)
  911. (list fun)
  912. fun)))
  913. (while (and stack
  914. (not (memq (cl-cadar stack) funcs)))
  915. (setq stack (cdr stack)))
  916. stack))
  917. (defun ido-ubiquitous--clean-advice-from-backtrace (stack)
  918. "Takes a stack trace and cleans all evidence of advice.
  919. Specifically, for each call to a function starting with
  920. \"ad-Orig-\", that call and all prior calls up to but not
  921. including the advised function's original name are deleted from
  922. the stack."
  923. (let ((skipping-until nil))
  924. (cl-loop for frame in stack
  925. for func = (cadr frame)
  926. ;; Check if we found the frame we we're skipping to
  927. if (and skipping-until
  928. (eq func skipping-until))
  929. do (setq skipping-until nil)
  930. ;; If we're looking at an the original form of an advised
  931. ;; function, skip until the real name of that function.
  932. if (and (not skipping-until)
  933. (ido-ubiquitous--looks-like-advised-orig func))
  934. do (setq skipping-until
  935. (intern
  936. (substring (symbol-name func)
  937. (eval-when-compile (length "ad-Orig-")))))
  938. unless skipping-until collect frame)))
  939. (defsubst ido-ubiquitous--interactive-internal ()
  940. "Equivalent of the INTERACTIVE macro in the Emacs C source.
  941. This is an internal function that should never be called
  942. directly.
  943. See the C source for the logic behind this function."
  944. (and (not executing-kbd-macro)
  945. (not noninteractive)))
  946. (defun ido-ubiquitous--interactive-p-internal ()
  947. "Equivalent of C function \"interactive_p\".
  948. This is an internal function that should never be called
  949. directly.
  950. See the C source for the logic behind this function."
  951. (let ((stack
  952. ;; We clean advice from the backtrace. This ensures that we
  953. ;; get the right answer even if `call-interactively' has been
  954. ;; advised.
  955. (ido-ubiquitous--clean-advice-from-backtrace
  956. (cdr
  957. (ido-ubiquitous--backtrace-from
  958. '(called-interactively-p interactive-p))))))
  959. ;; See comments in the C function for the logic here.
  960. (while (and stack
  961. (or (eq (cl-cadar stack) 'bytecode)
  962. (null (caar stack))))
  963. (setq stack (cdr stack)))
  964. ;; Top of stack is now the function that we want to know
  965. ;; about. Pop it, then check if the next function is
  966. ;; `call-interactively', using a more permissive test than the default.
  967. (ido-ubiquitous--looks-like-call-interactively (cl-cadadr stack))))
  968. (defadvice call-interactively (around ido-ubiquitous activate)
  969. "Implements the behavior specified in `ido-ubiquitous-command-overrides'."
  970. (let* ((cmd (ad-get-arg 0))
  971. (override (ido-ubiquitous-get-command-override cmd)))
  972. (when override
  973. (ido-ubiquitous--debug-message "Using override `%s' for command `%s'"
  974. override cmd))
  975. (ido-ubiquitous-with-override override
  976. ad-do-it)))
  977. ;; Work around `called-interactively-p' in Emacs 24.3 and earlier,
  978. ;; which always returns nil when `call-interactively' is advised.
  979. (when (not (and (featurep 'nadvice)
  980. (boundp 'called-interactively-p-functions)))
  981. (defadvice interactive-p (around ido-ubiquitous activate)
  982. "Return the correct result when `call-interactively' is advised.
  983. This advice completely overrides the original definition."
  984. (condition-case nil
  985. (setq ad-return-value
  986. (and (ido-ubiquitous--interactive-internal)
  987. (ido-ubiquitous--interactive-p-internal)))
  988. ;; In case of error in the advice, fall back to the default
  989. ;; implementation
  990. (error ad-do-it)))
  991. (defadvice called-interactively-p (around ido-ubiquitous activate)
  992. "Return the correct result when `call-interactively' is advised.
  993. This advice completely overrides the original definition."
  994. (condition-case nil
  995. (setq ad-return-value
  996. (and (or (ido-ubiquitous--interactive-internal)
  997. (not (eq kind 'interactive)))
  998. (ido-ubiquitous--interactive-p-internal)))
  999. ;; In case of error in the advice, fall back to the default
  1000. ;; implementation
  1001. (error ad-do-it))))
  1002. ;;; Other
  1003. (defsubst ido-ubiquitous--fixup-old-advice ()
  1004. ;; Clean up old versions of ido-ubiquitous advice if they exist
  1005. (ignore-errors (ad-remove-advice 'completing-read 'around 'ido-ubiquitous))
  1006. (ignore-errors (ad-remove-advice 'ido-completing-read 'around 'detect-replacing-cr))
  1007. (ignore-errors (ad-remove-advice 'ido-magic-forward-char 'before 'ido-ubiquitous-fallback))
  1008. (ignore-errors (ad-remove-advice 'ido-magic-backward-char 'before 'ido-ubiquitous-fallback))
  1009. (ignore-errors (ad-remove-advice 'ido-exit-minibuffer 'around 'compatibility))
  1010. (ad-activate-all))
  1011. (defsubst ido-ubiquitous--fixup-old-magit-overrides ()
  1012. (let ((old-override '(disable prefix "magit-"))
  1013. (new-override '(disable exact "magit-builtin-completing-read")))
  1014. (when (member old-override
  1015. ido-ubiquitous-command-overrides)
  1016. (customize-set-variable
  1017. 'ido-ubiquitous-command-overrides
  1018. (remove old-override ido-ubiquitous-command-overrides))
  1019. (unless (member new-override ido-ubiquitous-function-overrides)
  1020. (customize-set-variable 'ido-ubiquitous-function-overrides
  1021. (append ido-ubiquitous-function-overrides
  1022. (list new-override))))
  1023. (display-warning
  1024. 'ido-ubiquitous
  1025. "Fixing obsolete magit overrides.
  1026. Magit has changed recently such that the old override that
  1027. ido-ubiquitous defined for it now causes problems. This old
  1028. override has been automatically removed and the new one added.
  1029. Please use `M-x customize-group ido-ubiquitous' and review the
  1030. override variables and save them to your customization file."
  1031. :warning))))
  1032. (defun ido-ubiquitous-initialize ()
  1033. "Do initial setup for ido-ubiquitous.
  1034. This only needs to be called once when the file is first loaded.
  1035. It cleans up any traces of old versions of ido-ubiquitous and
  1036. then sets up the mode."
  1037. (ido-ubiquitous--fixup-old-advice)
  1038. (ido-ubiquitous--fixup-old-magit-overrides)
  1039. (ido-ubiquitous--maybe-update-overrides)
  1040. ;; Make sure the mode is turned on/off as specified by the value of
  1041. ;; the mode variable
  1042. (ido-ubiquitous-mode (if ido-ubiquitous-mode 1 0)))
  1043. (ido-ubiquitous-initialize)
  1044. (provide 'ido-ubiquitous)
  1045. ;; Local Variables:
  1046. ;; indent-tabs-mode: nil
  1047. ;; End:
  1048. ;;; ido-ubiquitous.el ends here