Skip to content

Commit

Permalink
Merge pull request #29 from jw3126/replaceD
Browse files Browse the repository at this point in the history
Replace d by plotattributes
  • Loading branch information
mkborregaard authored Sep 29, 2017
2 parents 9d3149c + e71f429 commit 21721a6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
40 changes: 31 additions & 9 deletions RecipesBase/src/RecipesBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,15 @@ end
function _equals_symbol(arg::Expr, sym::Symbol)
arg.head == :quote && arg.args[1] == sym
end
_equals_symbol(x, sym::Symbol) = false

# build an apply_recipe function header from the recipe function header
function get_function_def(func_signature::Expr, args::Vector)
front = func_signature.args[1]
if func_signature.head == :where
Expr(:where, get_function_def(front, args), esc.(func_signature.args[2:end])...)
elseif func_signature.head == :call
func = Expr(:call, :(RecipesBase.apply_recipe), esc.([:(d::Dict{Symbol, Any}); args])...)
func = Expr(:call, :(RecipesBase.apply_recipe), esc.([:($PLOTATTRIBUTES::Dict{Symbol, Any}); args])...)
if isa(front, Expr) && front.head == :curly
Expr(:where, func, esc.(front.args[2:end])...)
else
Expand All @@ -101,8 +102,8 @@ function create_kw_body(func_signature::Expr)
if isa(args[1], Expr) && args[1].head == :parameters
for kwpair in args[1].args
k, v = kwpair.args
push!(kw_body.args, :($k = get!(d, $(QuoteNode(k)), $v)))
push!(cleanup_body.args, :(RecipesBase.is_key_supported($(QuoteNode(k))) || delete!(d, $(QuoteNode(k)))))
push!(kw_body.args, :($k = get!($PLOTATTRIBUTES, $(QuoteNode(k)), $v)))
push!(cleanup_body.args, :(RecipesBase.is_key_supported($(QuoteNode(k))) || delete!($PLOTATTRIBUTES, $(QuoteNode(k)))))
end
args = args[2:end]
end
Expand Down Expand Up @@ -155,10 +156,10 @@ function process_recipe_body!(expr::Expr)

set_expr = if force
# forced override user settings
:(d[$k] = $v)
:($PLOTATTRIBUTES[$k] = $v)
else
# if the user has set this keyword, use theirs
:(get!(d, $k, $v))
:(get!($PLOTATTRIBUTES, $k, $v))
end

expr.args[i] = if quiet
Expand All @@ -182,6 +183,20 @@ function process_recipe_body!(expr::Expr)
end
end

# PLOTATTRIBUTES_OLD and_replace can be removed after d for accessing plot attributes is
# is not supported anymore
const PLOTATTRIBUTES = :plotattributes
const PLOTATTRIBUTES_OLD = :d
_replace(ex, old, new) = _replace(ex, 0, old, new)
_replace(ex, n, old, new) = (ex == old) ? (new, n+1) : (ex, n)
function _replace(ex::Expr, n, old, new)
new_args = []
for old_arg in ex.args
new_arg, n = _replace(old_arg, n, old, new)
push!(new_args, new_arg)
end
Expr(ex.head, new_args...), n
end
# --------------------------------------------------------------------------

"""
Expand Down Expand Up @@ -233,8 +248,15 @@ number of series to display. User-defined keyword arguments are passed through,
- force: Don't allow user override for this keyword
"""
macro recipe(funcexpr::Expr)
funcexpr, n_depr = _replace(funcexpr, PLOTATTRIBUTES_OLD, PLOTATTRIBUTES)
func_signature, func_body = funcexpr.args

if n_depr > 0
msg = "Usage of `$PLOTATTRIBUTES_OLD` for accessing plot attributes is deprecated. Found $n_depr usages of `$PLOTATTRIBUTES_OLD` inside `@recipe`. Use `$PLOTATTRIBUTES` instead.\n"
deprecation_body = :(Base.depwarn($msg, :d))
else
deprecation_body = :()
end
if !(funcexpr.head in (:(=), :function))
error("Must wrap a valid function call!")
end
Expand Down Expand Up @@ -264,7 +286,7 @@ macro recipe(funcexpr::Expr)
series_list = RecipesBase.RecipeData[]
func_return = $func_body
if func_return != nothing
push!(series_list, RecipesBase.RecipeData(d, RecipesBase.wrap_tuple(func_return)))
push!(series_list, RecipesBase.RecipeData($PLOTATTRIBUTES, RecipesBase.wrap_tuple(func_return)))
end
series_list
end))
Expand Down Expand Up @@ -298,9 +320,9 @@ end
"""
macro series(expr::Expr)
esc(quote
let d = copy(d)
let $PLOTATTRIBUTES = copy($PLOTATTRIBUTES)
args = $expr
push!(series_list, RecipesBase.RecipeData(d, RecipesBase.wrap_tuple(args)))
push!(series_list, RecipesBase.RecipeData($PLOTATTRIBUTES, RecipesBase.wrap_tuple(args)))
nothing
end
end)
Expand Down Expand Up @@ -344,7 +366,7 @@ function _userplot(expr::Expr)
end

function _userplot(sym::Symbol)
_userplot(:(type $sym
_userplot(:(mutable struct $sym
args
end))
end
Expand Down
24 changes: 23 additions & 1 deletion RecipesBase/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const KW = Dict{Symbol, Any}

RecipesBase.is_key_supported(k::Symbol) = true

for t in [Symbol(:T, i) for i in 1:3]
for t in [Symbol(:T, i) for i in 1:4]
@eval struct $t end
end

Expand Down Expand Up @@ -91,4 +91,26 @@ end
end


@testset "manual access of plotattributes" begin
@recipe function plot(t::T4, n = 1; customcolor = :green)
:markershape --> :auto, :require
:markercolor --> customcolor, :force
:xrotation --> 5
:zrotation --> 6, :quiet
plotattributes[:hello] = "hi"
d[:world] = "world"
rand(10,n)
end
srand(1)
check_apply_recipe(T4, KW(
:customcolor => :red,
:markershape => :auto,
:markercolor => :red,
:xrotation => 5,
:zrotation => 6,
:hello => "hi",
:world => "world"
))
end

end # @testset "@recipe"

0 comments on commit 21721a6

Please sign in to comment.