Skip to content

Instantly share code, notes, and snippets.

@pfitzseb
Last active September 23, 2021 09:11
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfitzseb/8fab21f09d6c15246d4a0960af3c65b1 to your computer and use it in GitHub Desktop.
Save pfitzseb/8fab21f09d6c15246d4a0960af3c65b1 to your computer and use it in GitHub Desktop.
using REPL
using REPL.LineEdit
# basically the same as Base's `display_error`, just with different frames removed
function display_error(io, err, st)
ind = findfirst(frame -> frame.file == Symbol(@__FILE__) && frame.func == :repleval, st)
st = st[1:(ind == nothing ? end : ind - 2)]
printstyled(io, "ERROR: "; bold=true, color=Base.error_color())
showerror(IOContext(io, :limit => true), err, st)
println(io)
end
function display_parse_error(io, err::Meta.ParseError)
printstyled(io, "ERROR: "; bold=true, color=Base.error_color())
printstyled(io, "syntax: ", err.msg; color=Base.error_color())
println(io)
end
# don't inline this so we can find it in the stacktrace
@noinline repleval(mod, line) = Core.eval(mod, line)
function evalrepl(mod, line)
global ans
try
# this is slow:
errored = false
try
line = Meta.parse(line)
catch err
errored = true
display_parse_error(stderr, err)
end
errored && return nothing
try
ans = repleval(mod, line)
catch err
# #FIXME: This is a bit weird (there shouldn't be any printing done here), but
# seems to work just fine.
errored = true
display_error(stderr, err, stacktrace(catch_backtrace()))
end
errored ? nothing : ans
catch err
# This is for internal errors only.
display_error(stderr, err, stacktrace(catch_backtrace()))
end
end
function get_main_mode()
mode = Base.active_repl.interface.modes[1]
mode isa LineEdit.Prompt || error("no julia repl mode found")
mode
end
function changeREPLmodule(mod)
main_mode = get_main_mode()
main_mode.prompt = string(mod, ">", " ")
main_mode.on_done = REPL.respond(Base.active_repl, main_mode; pass_empty = false) do line
if !isempty(line)
quote
$evalrepl($mod, $line)
end
end
end
nothing
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment