Skip to content

Commit

Permalink
Setting ans to a expression in REPLBlocks. (#926)
Browse files Browse the repository at this point in the history
* fix #923: setting ans to a expression in REPLBlocks.

(cherry picked from commit 8d17f68)
  • Loading branch information
fredrikekre authored and mortenpi committed Jan 27, 2019
1 parent 0414325 commit 2055205
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
12 changes: 2 additions & 10 deletions src/DocTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,27 +158,19 @@ function eval_repl(block, sandbox, meta::Dict, doc::Documents.Document, page)
result.hide = REPL.ends_with_semicolon(str)
(value, success, backtrace, text) = Utilities.withoutput() do
disable_color() do
Core.eval(sandbox, ex)
Core.eval(sandbox, Expr(:(=), :ans, ex))
end
end
result.value = value
print(result.stdout, text)
if success
# Redefine the magic `ans` binding available in the REPL.
__ans__!(sandbox, result.value)
else
if !success
result.bt = backtrace
end
end
checkresult(sandbox, result, meta, doc)
end
end

function __ans__!(m::Module, value)
isdefined(m, :__ans__!) || Core.eval(m, :(__ans__!(value) = global ans = value))
return Core.eval(m, Expr(:call, () -> m.__ans__!(value)))
end

function eval_script(block, sandbox, meta::Dict, doc::Documents.Document, page)
# TODO: decide whether to keep `# output` syntax for this. It's a bit ugly.
# Maybe use double blank lines, i.e.
Expand Down
2 changes: 1 addition & 1 deletion src/Expanders.jl
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ function Selectors.runner(::Type{REPLBlocks}, x, page, doc)
input = droplines(str)
(value, success, backtrace, text) = Utilities.withoutput() do
cd(dirname(page.build)) do
Core.eval(mod, :(ans = $(Core.eval(mod, ex))))
Core.eval(mod, Expr(:(=), :ans, ex))
end
end
result = value
Expand Down
5 changes: 5 additions & 0 deletions test/examples/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,11 @@ ex = :(1 + 5)
eval(ex)
```

```@example
a = 1
:(a + 1)
```

# Issue #793
```jldoctest
julia> write("issue793.jl", "\"Hello!\"");
Expand Down

0 comments on commit 2055205

Please sign in to comment.