diff --git a/PlotsBase/src/Commons/attrs.jl b/PlotsBase/src/Commons/attrs.jl index 48da09c12..2d46a7b17 100644 --- a/PlotsBase/src/Commons/attrs.jl +++ b/PlotsBase/src/Commons/attrs.jl @@ -410,6 +410,7 @@ const _plot_defaults = KW( :thickness_scaling => 1, :display_type => :auto, :warn_on_unsupported => true, + :safe_saving => true, :extra_plot_kwargs => Dict(), :extra_kwargs => :series, # directs collection of extra_kwargs ) diff --git a/PlotsBase/src/arg_desc.jl b/PlotsBase/src/arg_desc.jl index f448804cb..1599be5fe 100644 --- a/PlotsBase/src/arg_desc.jl +++ b/PlotsBase/src/arg_desc.jl @@ -83,6 +83,7 @@ const _arg_desc = KW( Example: `pgfplotsx(); scatter(1:5, extra_kwargs=Dict(:subplot=>Dict("axis line shift" => "10pt"))`."""), :fontfamily => (Union{AStr,Symbol}, "Default font family for title, legend entries, tick labels and guides."), :warn_on_unsupported => (Bool, "Warn on unsupported attributes, series types and marker shapes."), + :safe_saving => (Bool, "Do not override existing files when saving to disk. Choose from (true, false), i.e. `plot(rand(10), safe_saving=false)` for enable file overriding"), # subplot args :title => (AStr, "Subplot title."), diff --git a/PlotsBase/src/output.jl b/PlotsBase/src/output.jl index 68464aee3..90ba74c80 100644 --- a/PlotsBase/src/output.jl +++ b/PlotsBase/src/output.jl @@ -138,7 +138,10 @@ file types, some also support svg, ps, eps, html and tex. """ function savefig(plt::Plot, fn) # fn might be an `AbstractString` or an `AbstractPath` from `FilePaths.jl` fn = abspath(expanduser(fn)) - + if isfile(fn) && plt[:safe_saving] + @warn "Filename $fn already exists, defaulting to prevent overriding. To disable this behavior, provide `:safe_saving=false` kwarg, i.e. `plot(rand(10), safe_saving=false)`" + return + end # get the extension _, ext = splitext(fn) ext = chop(ext, head = 1, tail = 0)