From 0c010e5d548bd90f67b1d48076a26a3db102e58f Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Tue, 24 May 2016 14:57:20 -0400 Subject: [PATCH] deprecate `writemime` to methods of `show`. part of #14052 --- base/Enums.jl | 12 ++++++++---- base/REPL.jl | 4 ++-- base/Terminals.jl | 3 +-- base/datafmt.jl | 6 +++--- base/deprecated.jl | 1 + base/docs/helpdb/Base.jl | 26 ++++++++++++++----------- base/docs/utils.jl | 14 ++++++------- base/exports.jl | 1 - base/markdown/IPython/IPython.jl | 2 +- base/markdown/Markdown.jl | 2 +- base/markdown/render/html.jl | 2 +- base/markdown/render/latex.jl | 5 ++--- base/markdown/render/plain.jl | 10 +++++----- base/markdown/render/rich.jl | 6 +++--- base/markdown/render/rst.jl | 8 ++++---- base/markdown/render/terminal/render.jl | 4 ++-- base/methodshow.jl | 12 ++++++------ base/multimedia.jl | 24 +++++++++++------------ base/precompile.jl | 2 +- base/range.jl | 4 ++-- base/replutil.jl | 4 ++-- base/show.jl | 3 ++- base/sparse.jl | 2 +- doc/manual/documentation.rst | 2 +- doc/stdlib/io-network.rst | 14 +++++++------ test/datafmt.jl | 6 +++--- test/docs.jl | 18 ++++++++--------- test/markdown.jl | 18 ++++++++--------- test/ranges.jl | 4 ++-- test/show.jl | 12 ++++++------ test/sparsedir/cholmod.jl | 2 +- 31 files changed, 121 insertions(+), 112 deletions(-) diff --git a/base/Enums.jl b/base/Enums.jl index 1be3206a7a354..a7bffbb292da8 100644 --- a/base/Enums.jl +++ b/base/Enums.jl @@ -99,10 +99,14 @@ macro enum(T,syms...) print(io, x, "::", $(esc(typename)), " = ", Int(x)) end end - function Base.writemime(io::IO,::MIME"text/plain",::Type{$(esc(typename))}) - print(io, "Enum ", $(esc(typename)), ":") - for (sym, i) in $vals - print(io, "\n", sym, " = ", i) + function Base.show(io::IO,t::Type{$(esc(typename))}) + if get(io, :multiline, false) + print(io, "Enum ", $(esc(typename)).name.name, ":") + for (sym, i) in $vals + print(io, "\n", sym, " = ", i) + end + else + Base.show_datatype(io, t) end end end diff --git a/base/REPL.jl b/base/REPL.jl index 7ea36f7862cdd..cae964c5cc4c0 100644 --- a/base/REPL.jl +++ b/base/REPL.jl @@ -15,7 +15,7 @@ export import Base: Display, display, - writemime, + show, AnyDict, == @@ -111,7 +111,7 @@ end function display(d::REPLDisplay, ::MIME"text/plain", x) io = outstream(d.repl) Base.have_color && write(io, answer_color(d.repl)) - writemime(IOContext(io, multiline=true, limit=true), MIME("text/plain"), x) + show(IOContext(io, multiline=true, limit=true), MIME("text/plain"), x) println(io) end display(d::REPLDisplay, x) = display(d, MIME("text/plain"), x) diff --git a/base/Terminals.jl b/base/Terminals.jl index 0a4bc09325f5c..d0c1ac173b586 100644 --- a/base/Terminals.jl +++ b/base/Terminals.jl @@ -31,8 +31,7 @@ import Base: pipe_reader, pipe_writer, read, - readuntil, - writemime + readuntil ## TextTerminal ## diff --git a/base/datafmt.jl b/base/datafmt.jl index 7741ab3480fa5..da9e198f40833 100644 --- a/base/datafmt.jl +++ b/base/datafmt.jl @@ -4,7 +4,7 @@ module DataFmt -import Base: _default_delims, tryparse_internal, writemime +import Base: _default_delims, tryparse_internal, show export countlines, readdlm, readcsv, writedlm, writecsv @@ -623,7 +623,7 @@ end writedlm(io, a; opts...) = writedlm(io, a, '\t'; opts...) writecsv(io, a; opts...) = writedlm(io, a, ','; opts...) -writemime(io::IO, ::MIME"text/csv", a) = writedlm(io, a, ',') -writemime(io::IO, ::MIME"text/tab-separated-values", a) = writedlm(io, a, '\t') +show(io::IO, ::MIME"text/csv", a) = writedlm(io, a, ',') +show(io::IO, ::MIME"text/tab-separated-values", a) = writedlm(io, a, '\t') end # module DataFmt diff --git a/base/deprecated.jl b/base/deprecated.jl index 5dde191588179..4c3e9f7f70ce0 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1253,6 +1253,7 @@ end @deprecate_binding WORD_SIZE Sys.WORD_SIZE +@deprecate writemime show # During the 0.5 development cycle, do not add any deprecations below this line # To be deprecated in 0.6 diff --git a/base/docs/helpdb/Base.jl b/base/docs/helpdb/Base.jl index 70775c035f636..2365d7848ced1 100644 --- a/base/docs/helpdb/Base.jl +++ b/base/docs/helpdb/Base.jl @@ -6662,28 +6662,32 @@ Return, but do not print, the time elapsed since the last [`tic`](:func:`tic`). toq """ - writemime(stream, mime, x) + show(stream, mime, x) -The `display` functions ultimately call `writemime` in order to write an object `x` as a +The `display` functions ultimately call `show` in order to write an object `x` as a given `mime` type to a given I/O `stream` (usually a memory buffer), if possible. In order to provide a rich multimedia representation of a user-defined type `T`, it is only necessary -to define a new `writemime` method for `T`, via: `writemime(stream, ::MIME"mime", x::T) = ...`, +to define a new `show` method for `T`, via: `show(stream, ::MIME"mime", x::T) = ...`, where `mime` is a MIME-type string and the function body calls `write` (or similar) to write that representation of `x` to `stream`. (Note that the `MIME""` notation only supports literal strings; to construct `MIME` types in a more flexible manner use `MIME{Symbol("")}`.) For example, if you define a `MyImage` type and know how to write it to a PNG file, you -could define a function `writemime(stream, ::MIME"image/png", x::MyImage) = ...` to allow +could define a function `show(stream, ::MIME"image/png", x::MyImage) = ...` to allow your images to be displayed on any PNG-capable `Display` (such as IJulia). As usual, be sure -to `import Base.writemime` in order to add new methods to the built-in Julia function -`writemime`. +to `import Base.show` in order to add new methods to the built-in Julia function +`show`. Technically, the `MIME"mime"` macro defines a singleton type for the given `mime` string, which allows us to exploit Julia's dispatch mechanisms in determining how to display objects of any given type. + +The default MIME type is `MIME"text/plain"`. There is a fallback definition for `text/plain` +output that calls `show` with 2 arguments. Therefore, this case should be handled by +defining a 2-argument `show` method. """ -writemime +show(stream, mime, x) """ mean!(r, v) @@ -7303,7 +7307,7 @@ Write an informative text representation of a value to the current output stream should overload `show(io, x)` where the first argument is a stream. The representation used by `show` generally includes Julia-specific formatting and type information. """ -show +show(x) """ @allocated @@ -7981,8 +7985,8 @@ rethrow reprmime(mime, x) Returns an `AbstractString` or `Vector{UInt8}` containing the representation of `x` in the -requested `mime` type, as written by `writemime` (throwing a `MethodError` if no appropriate -`writemime` is available). An `AbstractString` is returned for MIME types with textual +requested `mime` type, as written by `show` (throwing a `MethodError` if no appropriate +`show` is available). An `AbstractString` is returned for MIME types with textual representations (such as `"text/html"` or `"application/postscript"`), whereas binary data is returned as `Vector{UInt8}`. (The function `istextmime(mime)` returns whether or not Julia treats a given `mime` type as text.) @@ -8375,7 +8379,7 @@ showall Returns a boolean value indicating whether or not the object `x` can be written as the given `mime` type. (By default, this is determined automatically by the existence of the -corresponding `writemime` function for `typeof(x)`.) +corresponding `show` function for `typeof(x)`.) """ mimewritable diff --git a/base/docs/utils.jl b/base/docs/utils.jl index d03dcdc1ac0a4..2c51f500d4824 100644 --- a/base/docs/utils.jl +++ b/base/docs/utils.jl @@ -2,7 +2,7 @@ # Text / HTML objects -import Base: print, writemime +import Base: print, show export HTML, @html_str @@ -26,13 +26,13 @@ end function HTML(xs...) HTML() do io for x in xs - writemime(io, MIME"text/html"(), x) + show(io, MIME"text/html"(), x) end end end -writemime(io::IO, ::MIME"text/html", h::HTML) = print(io, h.content) -writemime{F <: Function}(io::IO, ::MIME"text/html", h::HTML{F}) = h.content(io) +show(io::IO, ::MIME"text/html", h::HTML) = print(io, h.content) +show{F <: Function}(io::IO, ::MIME"text/html", h::HTML{F}) = h.content(io) """ @html_str -> Docs.HTML @@ -46,7 +46,7 @@ end function catdoc(xs::HTML...) HTML() do io for x in xs - writemime(io, MIME"text/html"(), x) + show(io, MIME"text/html"(), x) end end end @@ -70,7 +70,7 @@ end print(io::IO, t::Text) = print(io, t.content) print{F <: Function}(io::IO, t::Text{F}) = t.content(io) -writemime(io::IO, ::MIME"text/plain", t::Text) = print(io, t) +show(io::IO, t::Text) = print(io, t) """ @text_str -> Docs.Text @@ -84,7 +84,7 @@ end function catdoc(xs::Text...) Text() do io for x in xs - writemime(io, MIME"text/plain"(), x) + show(io, MIME"text/plain"(), x) end end end diff --git a/base/exports.jl b/base/exports.jl index db13c1fbbaaf4..571530702ada1 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -1233,7 +1233,6 @@ export @MIME_str, reprmime, stringmime, - writemime, mimewritable, popdisplay, pushdisplay, diff --git a/base/markdown/IPython/IPython.jl b/base/markdown/IPython/IPython.jl index 0cc81ff227682..47d564b2df21d 100644 --- a/base/markdown/IPython/IPython.jl +++ b/base/markdown/IPython/IPython.jl @@ -22,7 +22,7 @@ function blocktex(stream::IO, md::MD) end end -writemime(io::IO, ::MIME"text/plain", tex::LaTeX) = +show(io::IO, tex::LaTeX) = print(io, '$', tex.formula, '$') latex(io::IO, tex::LaTeX) = diff --git a/base/markdown/Markdown.jl b/base/markdown/Markdown.jl index 571a7b82fe308..453bbc59106a0 100644 --- a/base/markdown/Markdown.jl +++ b/base/markdown/Markdown.jl @@ -2,7 +2,7 @@ module Markdown -import Base: writemime, == +import Base: show, == import Core: @doc_str include("parse/config.jl") diff --git a/base/markdown/render/html.jl b/base/markdown/render/html.jl index 51eea3f970f35..f7d662eb2dda3 100644 --- a/base/markdown/render/html.jl +++ b/base/markdown/render/html.jl @@ -158,7 +158,7 @@ export html html(md) = sprint(html, md) -function writemime(io::IO, ::MIME"text/html", md::MD) +function show(io::IO, ::MIME"text/html", md::MD) withtag(io, :div, :class=>"markdown") do html(io, md) end diff --git a/base/markdown/render/latex.jl b/base/markdown/render/latex.jl index ae4b0af7fd98b..efc9ad2e774c8 100644 --- a/base/markdown/render/latex.jl +++ b/base/markdown/render/latex.jl @@ -69,7 +69,7 @@ function latex(io::IO, md::List) end end -function writemime(io::IO, ::MIME"text/latex", md::HorizontalRule) +function show(io::IO, ::MIME"text/latex", md::HorizontalRule) println(io, "\\rule{\\textwidth}{1pt}") end @@ -136,5 +136,4 @@ latex(md) = sprint(latex, md) latexinline(md) = sprint(latexinline, md) latexesc(s) = sprint(latexesc, s) -writemime(io::IO, ::MIME"text/latex", md::MD) = latex(io, md) -#writemime(io::IO, ::MIME"text/latex", md::MD) = writemime(io, "text/plain", md) +show(io::IO, ::MIME"text/latex", md::MD) = latex(io, md) diff --git a/base/markdown/render/plain.jl b/base/markdown/render/plain.jl index 8eff6d430bdd9..2b1d6d058545d 100644 --- a/base/markdown/render/plain.jl +++ b/base/markdown/render/plain.jl @@ -60,7 +60,7 @@ function plain(io::IO, l::LaTeX) end function plain(io::IO, md) - writemime(io, MIME"text/plain"(), md) + show(io, MIME"text/plain"(), md) println(io) end @@ -95,9 +95,9 @@ plaininline(io::IO, md::Code) = print(io, "`", md.code, "`") plaininline(io::IO, br::LineBreak) = println(io) -plaininline(io::IO, x) = writemime(io, MIME"text/plain"(), x) +plaininline(io::IO, x) = show(io, MIME"text/plain"(), x) -# writemime +# show -Base.writemime(io::IO, ::MIME"text/plain", md::MD) = plain(io, md) -Base.writemime(io::IO, ::MIME"text/markdown", md::MD) = plain(io, md) +Base.show(io::IO, md::MD) = plain(io, md) +Base.show(io::IO, ::MIME"text/markdown", md::MD) = plain(io, md) diff --git a/base/markdown/render/rich.jl b/base/markdown/render/rich.jl index 79fbd87beba1a..f3bdcefad460e 100644 --- a/base/markdown/render/rich.jl +++ b/base/markdown/render/rich.jl @@ -1,11 +1,11 @@ # This file is a part of Julia. License is MIT: http://julialang.org/license function tohtml(io::IO, m::MIME"text/html", x) - writemime(io, m, x) + show(io, m, x) end function tohtml(io::IO, m::MIME"text/plain", x) - htmlesc(io, sprint(writemime, m, x)) + htmlesc(io, sprint(show, m, x)) end function tohtml(io::IO, m::MIME"image/png", img) @@ -15,7 +15,7 @@ function tohtml(io::IO, m::MIME"image/png", img) end function tohtml(m::MIME"image/svg+xml", img) - writemime(io, m, img) + show(io, m, img) end # Display infrastructure diff --git a/base/markdown/render/rst.jl b/base/markdown/render/rst.jl index 3845a961a2a61..ba0638cd2216c 100644 --- a/base/markdown/render/rst.jl +++ b/base/markdown/render/rst.jl @@ -63,7 +63,7 @@ function rst(io::IO, l::LaTeX) end end -rst(io::IO, md) = writemime(io, "text/rst", md) +rst(io::IO, md) = show(io, "text/rst", md) # Inline elements @@ -113,8 +113,8 @@ rstinline(io::IO, br::LineBreak) = println(io) rstinline(io::IO, l::LaTeX) = print(io, ":math:`", l.formula, "`") -rstinline(io::IO, x) = writemime(io, MIME"text/rst"(), x) +rstinline(io::IO, x) = show(io, MIME"text/rst"(), x) -# writemime +# show -Base.writemime(io::IO, ::MIME"text/rst", md::MD) = rst(io, md) +Base.show(io::IO, ::MIME"text/rst", md::MD) = rst(io, md) diff --git a/base/markdown/render/terminal/render.jl b/base/markdown/render/terminal/render.jl index eeb498b6f995f..035e8cc27bf5b 100644 --- a/base/markdown/render/terminal/render.jl +++ b/base/markdown/render/terminal/render.jl @@ -79,7 +79,7 @@ function term(io::IO, br::HorizontalRule, columns) println(io, " " ^ margin, "-" ^ (columns - 2margin)) end -term(io::IO, x, _) = writemime(io, MIME"text/plain"(), x) +term(io::IO, x, _) = show(io, MIME"text/plain"(), x) # Inline Content @@ -124,7 +124,7 @@ function terminline(io::IO, code::Code) print_with_format(:cyan, io, code.code) end -terminline(io::IO, x) = writemime(io, MIME"text/plain"(), x) +terminline(io::IO, x) = show(io, MIME"text/plain"(), x) # Show in terminal diff --git a/base/methodshow.jl b/base/methodshow.jl index 2b6409adc6585..2d50529eb19bc 100644 --- a/base/methodshow.jl +++ b/base/methodshow.jl @@ -203,7 +203,7 @@ function url(m::Method) end end -function writemime(io::IO, ::MIME"text/html", m::Method; kwtype::Nullable{DataType}=Nullable{DataType}()) +function show(io::IO, ::MIME"text/html", m::Method; kwtype::Nullable{DataType}=Nullable{DataType}()) tv, decls, file, line = arg_decl_parts(m) ft = m.sig.parameters[1] d1 = decls[1] @@ -249,7 +249,7 @@ function writemime(io::IO, ::MIME"text/html", m::Method; kwtype::Nullable{DataTy end end -function writemime(io::IO, mime::MIME"text/html", ms::MethodList) +function show(io::IO, mime::MIME"text/html", ms::MethodList) mt = ms.mt name = mt.name n = length(ms) @@ -260,23 +260,23 @@ function writemime(io::IO, mime::MIME"text/html", ms::MethodList) kwtype = isdefined(mt, :kwsorter) ? Nullable{DataType}(typeof(mt.kwsorter)) : Nullable{DataType}() for meth in ms print(io, "
  • ") - writemime(io, mime, meth; kwtype=kwtype) + show(io, mime, meth; kwtype=kwtype) print(io, "
  • ") end print(io, "") end -writemime(io::IO, mime::MIME"text/html", mt::MethodTable) = writemime(io, mime, MethodList(mt)) +show(io::IO, mime::MIME"text/html", mt::MethodTable) = show(io, mime, MethodList(mt)) # pretty-printing of Vector{Method} for output of methodswith: -function writemime(io::IO, mime::MIME"text/html", mt::AbstractVector{Method}) +function show(io::IO, mime::MIME"text/html", mt::AbstractVector{Method}) print(io, summary(mt)) if !isempty(mt) print(io, ":") end diff --git a/base/multimedia.jl b/base/multimedia.jl index 9f089802ec703..416b4df051257 100644 --- a/base/multimedia.jl +++ b/base/multimedia.jl @@ -3,13 +3,13 @@ module Multimedia export Display, display, pushdisplay, popdisplay, displayable, redisplay, - MIME, @MIME, @MIME_str, writemime, reprmime, stringmime, istextmime, + MIME, @MIME, @MIME_str, reprmime, stringmime, istextmime, mimewritable, TextDisplay ########################################################################### # We define a singleton type MIME{mime symbol} for each MIME type, so # that Julia's dispatch and overloading mechanisms can be used to -# dispatch writemime and to add conversions for new types. +# dispatch show and to add conversions for new types. immutable MIME{mime} end @@ -33,14 +33,14 @@ macro MIME_str(s) end ########################################################################### -# For any type T one can define writemime(io, ::MIME"type", x::T) = ... +# For any type T one can define show(io, ::MIME"type", x::T) = ... # in order to provide a way to export T as a given mime type. mimewritable{mime}(::MIME{mime}, x) = - method_exists(writemime, Tuple{IO, MIME{mime}, typeof(x)}) + method_exists(show, Tuple{IO, MIME{mime}, typeof(x)}) # it is convenient to accept strings instead of ::MIME -writemime(io::IO, m::AbstractString, x) = writemime(io, MIME(m), x) +show(io::IO, m::AbstractString, x) = show(io, MIME(m), x) mimewritable(m::AbstractString, x) = mimewritable(MIME(m), x) ########################################################################### @@ -57,21 +57,21 @@ mimewritable(m::AbstractString, x) = mimewritable(MIME(m), x) # format and is returned unmodified. This is useful so that raw data can be # passed to display(m::MIME, x). -verbose_writemime(io, m, x) = writemime(IOContext(io,multiline=true,limit=false), m, x) +verbose_show(io, m, x) = show(IOContext(io,multiline=true,limit=false), m, x) macro textmime(mime) quote mimeT = MIME{Symbol($mime)} # avoid method ambiguities with the general definitions below: # (Q: should we treat Vector{UInt8} as a String?) - Base.Multimedia.reprmime(m::mimeT, x::Vector{UInt8}) = sprint(verbose_writemime, m, x) + Base.Multimedia.reprmime(m::mimeT, x::Vector{UInt8}) = sprint(verbose_show, m, x) Base.Multimedia.stringmime(m::mimeT, x::Vector{UInt8}) = reprmime(m, x) Base.Multimedia.istextmime(::mimeT) = true if $(mime != "text/plain") # strings are shown escaped for text/plain Base.Multimedia.reprmime(m::mimeT, x::AbstractString) = x end - Base.Multimedia.reprmime(m::mimeT, x) = sprint(verbose_writemime, m, x) + Base.Multimedia.reprmime(m::mimeT, x) = sprint(verbose_show, m, x) Base.Multimedia.stringmime(m::mimeT, x) = reprmime(m, x) end end @@ -79,11 +79,11 @@ end istextmime(::MIME) = false function reprmime(m::MIME, x) s = IOBuffer() - verbose_writemime(s, m, x) + verbose_show(s, m, x) takebuf_array(s) end reprmime(m::MIME, x::Vector{UInt8}) = x -stringmime(m::MIME, x) = base64encode(verbose_writemime, m, x) +stringmime(m::MIME, x) = base64encode(verbose_show, m, x) stringmime(m::MIME, x::Vector{UInt8}) = base64encode(write, x) # it is convenient to accept strings instead of ::MIME @@ -118,7 +118,7 @@ displayable(mime::AbstractString) = displayable(MIME(mime)) immutable TextDisplay <: Display io::IO end -display(d::TextDisplay, M::MIME"text/plain", x) = writemime(d.io, M, x) +display(d::TextDisplay, M::MIME"text/plain", x) = show(d.io, M, x) display(d::TextDisplay, x) = display(d, MIME"text/plain"(), x) import Base: close, flush @@ -152,7 +152,7 @@ macro try_display(expr) quote try $(esc(expr)) catch e - isa(e, MethodError) && e.f in (display, redisplay, writemime) || + isa(e, MethodError) && e.f in (display, redisplay, show) || rethrow() end end diff --git a/base/precompile.jl b/base/precompile.jl index b70c331c18f0e..e2467b8b21ae6 100644 --- a/base/precompile.jl +++ b/base/precompile.jl @@ -408,7 +408,7 @@ precompile(Base.wait, (RemoteChannel,)) precompile(Base.write, (Base.Terminals.TTYTerminal, String)) precompile(Base.write, (Base.Terminals.TerminalBuffer, String)) precompile(Base.write, (IOBuffer, Vector{UInt8})) -precompile(Base.writemime, (Base.Terminals.TTYTerminal, Base.Multimedia.MIME{Symbol("text/plain")}, Int)) +precompile(Base.show, (Base.Terminals.TTYTerminal, Base.Multimedia.MIME{Symbol("text/plain")}, Int)) # The following are intended to help speed Pkg.update() precompile(Base.Pkg.Entry.update, (String,)) diff --git a/base/range.jl b/base/range.jl index 1940451f61b62..ca793230bfb39 100644 --- a/base/range.jl +++ b/base/range.jl @@ -237,7 +237,7 @@ linspace(start::Real, stop::Real, len::Real=50) = function show(io::IO, r::LinSpace) if get(io, :multiline, false) - # writemime for linspace, e.g. + # show for linspace, e.g. # linspace(1,3,7) # 7-element LinSpace{Float64}: # 1.0,1.33333,1.66667,2.0,2.33333,2.66667,3.0 @@ -276,7 +276,7 @@ function print_range(io::IO, r::Range, post::AbstractString = "", hdots::AbstractString = ",\u2026,") # horiz ellipsis # This function borrows from print_matrix() in show.jl - # and should be called by writemime (replutil.jl) and by display() + # and should be called by show and display limit = get(io, :limit, false) sz = displaysize(io) if !haskey(io, :compact) diff --git a/base/replutil.jl b/base/replutil.jl index 6cef41b054f29..dc7ceb6666621 100644 --- a/base/replutil.jl +++ b/base/replutil.jl @@ -1,7 +1,7 @@ # This file is a part of Julia. License is MIT: http://julialang.org/license # fallback text/plain representation of any type: -writemime(io::IO, ::MIME"text/plain", x) = show(io, x) +show(io::IO, ::MIME"text/plain", x) = show(io, x) # showing exception objects as descriptive error messages @@ -15,7 +15,7 @@ function showerror(io::IO, ex::BoundsError) if isa(ex.a, AbstractArray) print(io, summary(ex.a)) else - writemime(io, MIME"text/plain"(), ex.a) + show(io, MIME"text/plain"(), ex.a) end if isdefined(ex, :i) !isa(ex.a, AbstractArray) && print(io, "\n ") diff --git a/base/show.jl b/base/show.jl index 9b498e26c742f..c2666793b2b40 100644 --- a/base/show.jl +++ b/base/show.jl @@ -184,7 +184,8 @@ function show_type_parameter(io::IO, p::ANY, has_tvar_env::Bool) end end -function show(io::IO, x::DataType) +show(io::IO, x::DataType) = show_datatype(io, x) +function show_datatype(io::IO, x::DataType) show(io, x.name) # tvar_env is a `::Vector{Any}` when we are printing a method signature # and `true` if we are printing type parameters outside a method signature. diff --git a/base/sparse.jl b/base/sparse.jl index 015c5399f6780..9dd8294743996 100644 --- a/base/sparse.jl +++ b/base/sparse.jl @@ -18,7 +18,7 @@ import Base: @get!, acos, acosd, acot, acotd, acsch, asech, asin, asind, asinh, exp10, exp2, eye, findn, floor, hash, indmin, inv, issymmetric, istril, istriu, log10, log2, lu, maxabs, minabs, next, sec, secd, sech, show, sin, sinc, sind, sinh, sinpi, squeeze, start, sum, sumabs, sumabs2, summary, tan, - tand, tanh, trace, transpose!, tril!, triu!, trunc, vecnorm, writemime, abs, abs2, + tand, tanh, trace, transpose!, tril!, triu!, trunc, vecnorm, abs, abs2, broadcast, ceil, complex, cond, conj, convert, copy, copy!, ctranspose, diagm, exp, expm1, factorize, find, findmax, findmin, findnz, float, full, getindex, hcat, hvcat, imag, indmax, ishermitian, kron, length, log, log1p, max, min, diff --git a/doc/manual/documentation.rst b/doc/manual/documentation.rst index 53eb266ab9026..305137d358431 100644 --- a/doc/manual/documentation.rst +++ b/doc/manual/documentation.rst @@ -505,7 +505,7 @@ like tables is in the works. Markdown.jl supports interpolation in a very similar way to basic string literals, with the difference that it will store the object itself in the Markdown tree (as opposed to converting it to a string). When the -Markdown content is rendered the usual ``writemime`` methods will be +Markdown content is rendered the usual ``show`` methods will be called, and these can be overridden as usual. This design allows the Markdown to be extended with arbitrarily complex features (such as references) without cluttering the basic syntax. diff --git a/doc/stdlib/io-network.rst b/doc/stdlib/io-network.rst index 58ee02fd2b9b6..41176bfe65b12 100644 --- a/doc/stdlib/io-network.rst +++ b/doc/stdlib/io-network.rst @@ -666,7 +666,7 @@ of three parts: * A function ``display(x)`` to request the richest available multimedia display of a Julia object ``x`` (with a plain-text fallback). -* Overloading ``writemime`` allows one to indicate arbitrary multimedia +* Overloading ``show`` allows one to indicate arbitrary multimedia representations (keyed by standard MIME types) of user-defined types. * Multimedia-capable display backends may be registered by subclassing a generic ``Display`` type and pushing them onto a stack of display @@ -703,27 +703,29 @@ Julia environments (such as the IPython-based IJulia notebook). Returns a boolean value indicating whether the given ``mime`` type (string) is displayable by any of the displays in the current display stack, or specifically by the display ``d`` in the second variant. -.. function:: writemime(stream, mime, x) +.. function:: show(stream, mime, x) .. Docstring generated from Julia source - The ``display`` functions ultimately call ``writemime`` in order to write an object ``x`` as a given ``mime`` type to a given I/O ``stream`` (usually a memory buffer), if possible. In order to provide a rich multimedia representation of a user-defined type ``T``\ , it is only necessary to define a new ``writemime`` method for ``T``\ , via: ``writemime(stream, ::MIME"mime", x::T) = ...``\ , where ``mime`` is a MIME-type string and the function body calls ``write`` (or similar) to write that representation of ``x`` to ``stream``\ . (Note that the ``MIME""`` notation only supports literal strings; to construct ``MIME`` types in a more flexible manner use ``MIME{Symbol("")}``\ .) + The ``display`` functions ultimately call ``show`` in order to write an object ``x`` as a given ``mime`` type to a given I/O ``stream`` (usually a memory buffer), if possible. In order to provide a rich multimedia representation of a user-defined type ``T``\ , it is only necessary to define a new ``show`` method for ``T``\ , via: ``show(stream, ::MIME"mime", x::T) = ...``\ , where ``mime`` is a MIME-type string and the function body calls ``write`` (or similar) to write that representation of ``x`` to ``stream``\ . (Note that the ``MIME""`` notation only supports literal strings; to construct ``MIME`` types in a more flexible manner use ``MIME{Symbol("")}``\ .) - For example, if you define a ``MyImage`` type and know how to write it to a PNG file, you could define a function ``writemime(stream, ::MIME"image/png", x::MyImage) = ...`` to allow your images to be displayed on any PNG-capable ``Display`` (such as IJulia). As usual, be sure to ``import Base.writemime`` in order to add new methods to the built-in Julia function ``writemime``\ . + For example, if you define a ``MyImage`` type and know how to write it to a PNG file, you could define a function ``show(stream, ::MIME"image/png", x::MyImage) = ...`` to allow your images to be displayed on any PNG-capable ``Display`` (such as IJulia). As usual, be sure to ``import Base.show`` in order to add new methods to the built-in Julia function ``show``\ . Technically, the ``MIME"mime"`` macro defines a singleton type for the given ``mime`` string, which allows us to exploit Julia's dispatch mechanisms in determining how to display objects of any given type. + The default MIME type is ``MIME"text/plain"``\ . There is a fallback definition for ``text/plain`` output that calls ``show`` with 2 arguments. Therefore, this case should be handled by defining a 2-argument ``show`` method. + .. function:: mimewritable(mime, x) .. Docstring generated from Julia source - Returns a boolean value indicating whether or not the object ``x`` can be written as the given ``mime`` type. (By default, this is determined automatically by the existence of the corresponding ``writemime`` function for ``typeof(x)``\ .) + Returns a boolean value indicating whether or not the object ``x`` can be written as the given ``mime`` type. (By default, this is determined automatically by the existence of the corresponding ``show`` function for ``typeof(x)``\ .) .. function:: reprmime(mime, x) .. Docstring generated from Julia source - Returns an ``AbstractString`` or ``Vector{UInt8}`` containing the representation of ``x`` in the requested ``mime`` type, as written by ``writemime`` (throwing a ``MethodError`` if no appropriate ``writemime`` is available). An ``AbstractString`` is returned for MIME types with textual representations (such as ``"text/html"`` or ``"application/postscript"``\ ), whereas binary data is returned as ``Vector{UInt8}``\ . (The function ``istextmime(mime)`` returns whether or not Julia treats a given ``mime`` type as text.) + Returns an ``AbstractString`` or ``Vector{UInt8}`` containing the representation of ``x`` in the requested ``mime`` type, as written by ``show`` (throwing a ``MethodError`` if no appropriate ``show`` is available). An ``AbstractString`` is returned for MIME types with textual representations (such as ``"text/html"`` or ``"application/postscript"``\ ), whereas binary data is returned as ``Vector{UInt8}``\ . (The function ``istextmime(mime)`` returns whether or not Julia treats a given ``mime`` type as text.) As a special case, if ``x`` is an ``AbstractString`` (for textual MIME types) or a ``Vector{UInt8}`` (for binary MIME types), the ``reprmime`` function assumes that ``x`` is already in the requested ``mime`` format and simply returns ``x``\ . diff --git a/test/datafmt.jl b/test/datafmt.jl index fe32b81f6408e..d0df898357569 100644 --- a/test/datafmt.jl +++ b/test/datafmt.jl @@ -244,10 +244,10 @@ let data = "1 2 3" readdlm(IOBuffer(data), ' ', BigInt) == BigInt[1 2 3] end -# test writemime -@test sprint(io -> writemime(io, "text/csv", [1 2; 3 4])) == "1,2\n3,4\n" +# test show with MIME types +@test sprint(io -> show(io, "text/csv", [1 2; 3 4])) == "1,2\n3,4\n" -for writefunc in ((io,x) -> writemime(io, "text/csv", x), +for writefunc in ((io,x) -> show(io, "text/csv", x), (io,x) -> invoke(writedlm, (IO, Any, Any), io, x, ",")) # iterable collections of iterable rows: let x = [(1,2), (3,4)], io = IOBuffer() diff --git a/test/docs.jl b/test/docs.jl index 56b525c5d5a25..333a29a824313 100644 --- a/test/docs.jl +++ b/test/docs.jl @@ -6,8 +6,8 @@ import Base.Docs: meta, @var, DocStr, parsedoc function docstrings_equal(d1, d2) io1 = IOBuffer() io2 = IOBuffer() - writemime(io1, MIME"text/markdown"(), d1) - writemime(io2, MIME"text/markdown"(), d2) + show(io1, MIME"text/markdown"(), d1) + show(io2, MIME"text/markdown"(), d2) takebuf_string(io1) == takebuf_string(io2) end docstrings_equal(d1::DocStr, d2) = docstrings_equal(parsedoc(d1), d2) @@ -15,8 +15,8 @@ docstrings_equal(d1::DocStr, d2) = docstrings_equal(parsedoc(d1), d2) function docstring_startswith(d1, d2) io1 = IOBuffer() io2 = IOBuffer() - writemime(io1, MIME"text/markdown"(), d1) - writemime(io2, MIME"text/markdown"(), d2) + show(io1, MIME"text/markdown"(), d1) + show(io2, MIME"text/markdown"(), d2) startswith(takebuf_string(io1), takebuf_string(io2)) end docstring_startswith(d1::DocStr, d2) = docstring_startswith(parsedoc(d1), d2) @@ -486,16 +486,16 @@ immutable LazyHelp text end -function Base.writemime(io::IO, ::MIME"text/plain", h::LazyHelp) +function Base.show(io::IO, ::MIME"text/plain", h::LazyHelp) print(io, h.text) end -Base.show(io::IO, h::LazyHelp) = writemime(io, "text/plain", h) +Base.show(io::IO, h::LazyHelp) = show(io, "text/plain", h) function Base.Docs.catdoc(hs::LazyHelp...) Base.Docs.Text() do io for h in hs - writemime(io, MIME"text/plain"(), h) + show(io, MIME"text/plain"(), h) end end end @@ -752,7 +752,7 @@ three :: Float64 let d = @doc Undocumented.f io = IOBuffer() - writemime(io, MIME"text/markdown"(), d) + show(io, MIME"text/markdown"(), d) @test startswith(takebuf_string(io),""" No documentation found. @@ -762,7 +762,7 @@ end let d = @doc Undocumented.undocumented io = IOBuffer() - writemime(io, MIME"text/markdown"(), d) + show(io, MIME"text/markdown"(), d) @test startswith(takebuf_string(io), """ No documentation found. diff --git a/test/markdown.jl b/test/markdown.jl index 142fdd171422e..b9e03115ce2cd 100644 --- a/test/markdown.jl +++ b/test/markdown.jl @@ -2,7 +2,7 @@ using Base.Markdown import Base.Markdown: MD, Paragraph, Header, Italic, Bold, LineBreak, plain, term, html, rst, Table, Code, LaTeX, Footnote -import Base: writemime +import Base: show # Basics # Equality is checked by making sure the HTML output is @@ -161,7 +161,7 @@ Some **bolded** """ @test latex(book) == "\\section{Title}\nSome discussion\n\\begin{quote}\nA quote\n\\end{quote}\n\\subsection{Section \\emph{important}}\nSome \\textbf{bolded}\n\\begin{itemize}\n\\item list1\n\\item list2\n\\end{itemize}\n" -# writemime output +# mime output let out = """ @@ -179,8 +179,8 @@ let out = * list1 * list2 """ - @test sprint(io -> writemime(io, "text/plain", book)) == out - @test sprint(io -> writemime(io, "text/markdown", book)) == out + @test sprint(io -> show(io, "text/plain", book)) == out + @test sprint(io -> show(io, "text/markdown", book)) == out end let out = """ @@ -196,7 +196,7 @@ let out =
  • list2
  • """ - @test sprint(io -> writemime(io, "text/html", book)) == out + @test sprint(io -> show(io, "text/html", book)) == out end let out = """ @@ -212,7 +212,7 @@ let out = \\item list2 \\end{itemize} """ - @test sprint(io -> writemime(io, "text/latex", book)) == out + @test sprint(io -> show(io, "text/latex", book)) == out end let out = """ @@ -234,7 +234,7 @@ let out = * list1 * list2 """ - @test sprint(io -> writemime(io, "text/rst", book)) == out + @test sprint(io -> show(io, "text/rst", book)) == out end # rst rendering @@ -275,14 +275,14 @@ ref(x) = Reference(x) ref(mean) -writemime(io::IO, m::MIME"text/plain", r::Reference) = +show(io::IO, m::MIME"text/plain", r::Reference) = print(io, "$(r.ref) (see Julia docs)") mean_ref = md"Behaves like $(ref(mean))" @test plain(mean_ref) == "Behaves like mean (see Julia docs)\n" @test html(mean_ref) == "

    Behaves like mean (see Julia docs)

    \n" -writemime(io::IO, m::MIME"text/html", r::Reference) = +show(io::IO, m::MIME"text/html", r::Reference) = Markdown.withtag(io, :a, :href=>"test") do Markdown.htmlesc(io, Markdown.plaininline(r)) end diff --git a/test/ranges.jl b/test/ranges.jl index f8c5f8e337f2d..8b04717e6df21 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -567,9 +567,9 @@ for x in r end @test i == 7 -# stringmime/writemime should display the range or linspace nicely +# stringmime/show should display the range or linspace nicely # to test print_range in range.jl -replstrmime(x) = sprint((io,x) -> writemime(IOContext(io, multiline=true, limit=true), MIME("text/plain"), x), x) +replstrmime(x) = sprint((io,x) -> show(IOContext(io, multiline=true, limit=true), MIME("text/plain"), x), x) @test replstrmime(1:4) == "1:4" @test stringmime("text/plain", 1:4) == "1:4" @test stringmime("text/plain", linspace(1,5,7)) == "7-element LinSpace{Float64}:\n 1.0,1.66667,2.33333,3.0,3.66667,4.33333,5.0" diff --git a/test/show.jl b/test/show.jl index 45f069f6f7830..fd0ee68fc5fb5 100644 --- a/test/show.jl +++ b/test/show.jl @@ -1,6 +1,6 @@ # This file is a part of Julia. License is MIT: http://julialang.org/license -replstr(x) = sprint((io,x) -> writemime(IOContext(io, multiline=true, limit=true), MIME("text/plain"), x), x) +replstr(x) = sprint((io,x) -> show(IOContext(io, multiline=true, limit=true), MIME("text/plain"), x), x) @test replstr(cell(2)) == "2-element Array{Any,1}:\n #undef\n #undef" @test replstr(cell(2,2)) == "2×2 Array{Any,2}:\n #undef #undef\n #undef #undef" @@ -316,18 +316,18 @@ end @test Base.inbase(LinAlg) @test !Base.inbase(Core) -let repr = sprint(io -> writemime(io,"text/plain", methods(Base.inbase))) +let repr = sprint(io -> show(io,"text/plain", methods(Base.inbase))) @test contains(repr, "inbase(m::Module)") end -let repr = sprint(io -> writemime(io,"text/html", methods(Base.inbase))) +let repr = sprint(io -> show(io,"text/html", methods(Base.inbase))) @test contains(repr, "inbase(m::Module)") end f5971(x, y...; z=1, w...) = nothing -let repr = sprint(io -> writemime(io,"text/plain", methods(f5971))) +let repr = sprint(io -> show(io,"text/plain", methods(f5971))) @test contains(repr, "f5971(x, y...; z)") end -let repr = sprint(io -> writemime(io,"text/html", methods(f5971))) +let repr = sprint(io -> show(io,"text/html", methods(f5971))) @test contains(repr, "f5971(x, y...; z)") end @@ -338,7 +338,7 @@ else end # print_matrix should be able to handle small and large objects easily, test by -# calling writemime. This also indirectly tests print_matrix_row, which +# calling show. This also indirectly tests print_matrix_row, which # is used repeatedly by print_matrix. # This fits on screen: @test replstr(eye(10)) == "10×10 Array{Float64,2}:\n 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0\n 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0\n 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0\n 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0\n 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0\n 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0\n 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0\n 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0\n 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0\n 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0" diff --git a/test/sparsedir/cholmod.jl b/test/sparsedir/cholmod.jl index 40b7724b664d9..02a15b075d420 100644 --- a/test/sparsedir/cholmod.jl +++ b/test/sparsedir/cholmod.jl @@ -591,7 +591,7 @@ Dp = spdiagm(dp) sparse(cholfact(sparse(Float64[ 10 1 1 1; 1 10 0 0; 1 0 10 0; 1 0 0 10]))); gc() # Issue 11747 - Wrong show method defined for FactorComponent -Base.writemime(IOBuffer(), MIME"text/plain"(), cholfact(sparse(Float64[ 10 1 1 1; 1 10 0 0; 1 0 10 0; 1 0 0 10]))[:L]) +Base.show(IOBuffer(), MIME"text/plain"(), cholfact(sparse(Float64[ 10 1 1 1; 1 10 0 0; 1 0 10 0; 1 0 0 10]))[:L]) # Element promotion and type inference @inferred cholfact(As)\ones(Int, size(As, 1))