Skip to content

Commit

Permalink
fix JuliaLang#6428 (emacs shell troubles)
Browse files Browse the repository at this point in the history
  • Loading branch information
nolta committed Apr 14, 2014
1 parent 8a618e8 commit c8ae026
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 33 deletions.
88 changes: 59 additions & 29 deletions base/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ using Base.Terminals
using Base.LineEdit
using Base.REPLCompletions

export StreamREPL, BasicREPL
export
BasicREPL,
LineEditREPL,
StreamREPL

import Base: AsyncStream,
Display,
display,
writemime

import Base.Terminals: raw!
import Base:
AsyncStream,
Display,
display,
writemime

import Base.LineEdit:
CompletionProvider,
Expand All @@ -27,13 +29,15 @@ import Base.LineEdit:

abstract AbstractREPL

answer_color(::AbstractREPL) = ""

type REPLBackend
repl_channel::RemoteRef
response_channel::RemoteRef
ans
end

function eval_user_input(ast::ANY, backend)
function eval_user_input(ast::ANY, backend::REPLBackend)
iserr, lasterr, bt = false, (), nothing
while true
try
Expand Down Expand Up @@ -73,7 +77,7 @@ function parse_input_line(s::String)
ccall(:jl_parse_input_line, Any, (Ptr{Uint8},), s)
end

function start_repl_backend(repl_channel, response_channel)
function start_repl_backend(repl_channel::RemoteRef, response_channel::RemoteRef)
backend = REPLBackend(repl_channel, response_channel, nothing)
@async begin
# include looks at this to determine the relative include path
Expand All @@ -88,7 +92,6 @@ function start_repl_backend(repl_channel, response_channel)
end
eval_user_input(ast, backend)
end

end
end

Expand All @@ -110,7 +113,9 @@ function display(d::REPLDisplay, ::MIME"text/plain", x)
end
display(d::REPLDisplay, x) = display(d, MIME("text/plain"), x)

function print_response(d::REPLDisplay, errio::IO, r::AbstractREPL, val::ANY, bt, show_value, have_color)
print_response(d::REPLDisplay, val::ANY, bt, show_value::Bool, have_color::Bool) =
print_response(d, outstream(d.repl), val, bt, show_value, have_color)
function print_response(d::REPLDisplay, errio::IO, val::ANY, bt, show_value::Bool, have_color::Bool)
while true
try
if bt !== nothing
Expand Down Expand Up @@ -138,6 +143,48 @@ function print_response(d::REPLDisplay, errio::IO, r::AbstractREPL, val::ANY, bt
end
end
end

function run_repl(repl::AbstractREPL)
repl_channel = RemoteRef()
response_channel = RemoteRef()
start_repl_backend(repl_channel, response_channel)
run_frontend(repl, repl_channel, response_channel)
end

## BasicREPL ##

type BasicREPL <: AbstractREPL
terminal::TextTerminal
end

outstream(r::BasicREPL) = r.terminal

function run_frontend(repl::BasicREPL, repl_channel::RemoteRef, response_channel::RemoteRef)
d = REPLDisplay(repl)
while true
write(repl.terminal, "julia> ")
line = ""
ast = nothing
while true
line *= readline(repl.terminal)
ast = Base.parse_input_line(line)
(isa(ast,Expr) && ast.head == :incomplete) || break
end
if !isempty(line)
put!(repl_channel, (ast, 1))
val, bt = take!(response_channel)
if !ends_with_semicolon(line)
print_response(d, val, bt, true, false)
end
end
write(repl.terminal, '\n')
end
# terminate backend
put!(repl_channel, (nothing, -1))
end

## LineEditREPL ##

type LineEditREPL <: AbstractREPL
t::TextTerminal
prompt_color::String
Expand Down Expand Up @@ -433,8 +480,6 @@ function reset(d::REPLDisplay{LineEditREPL})
print(Base.text_colors[:normal])
end



function setup_interface(d::REPLDisplay, req, rep; extra_repl_keymap = Dict{Any,Any}[])
###
#
Expand Down Expand Up @@ -631,18 +676,7 @@ else
banner(io,t) = Base.banner(io)
end

function run_repl(repl::LineEditREPL)
repl_channel = RemoteRef()
response_channel = RemoteRef()
start_repl_backend(repl_channel, response_channel)
run_frontend(repl, repl_channel, response_channel)
end
run_repl(t::TextTerminal) = run_repl(LineEditREPL(t))

type BasicREPL <: AbstractREPL
end

outstream(::BasicREPL) = STDOUT
## StreamREPL ##

type StreamREPL <: AbstractREPL
stream::IO
Expand All @@ -657,10 +691,6 @@ StreamREPL(stream::AsyncStream) = StreamREPL(stream, julia_green, Base.text_colo

answer_color(r::LineEditREPL) = r.answer_color
answer_color(r::StreamREPL) = r.answer_color
answer_color(::BasicREPL) = Base.text_colors[:white]

print_response(d::REPLDisplay{StreamREPL}, args...) = print_response(d, d.repl.stream, d.repl, args...)
print_response(d::REPLDisplay{LineEditREPL}, args...) = print_response(d, d.repl.t, d.repl, args...)

function run_repl(stream::AsyncStream)
repl =
Expand Down
3 changes: 2 additions & 1 deletion base/Terminals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export
getY,
hascolor,
pos,
raw!,
writepos

import Base:
Expand Down Expand Up @@ -136,7 +137,7 @@ end_keypad_transmit_mode(t::UnixTerminal) = # tput rmkx
write(t.out_stream, "$(CSI)?1l\x1b>")

function size(t::TTYTerminal)
s = Array(Int32, 2)
s = zeros(Int32, 2)
Base.uv_error("size (TTY)", ccall((@windows ? :jl_tty_get_winsize : :uv_tty_get_winsize),
Int32, (Ptr{Void}, Ptr{Int32}, Ptr{Int32}),
t.out_stream.handle, pointer(s,1), pointer(s,2)) != 0)
Expand Down
11 changes: 8 additions & 3 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ function _start()
global is_interactive = !isa(STDIN,Union(File,IOStream))
color_set || (global have_color = false)
else
term = Terminals.TTYTerminal(get(ENV,"TERM",""),STDIN,STDOUT,STDERR)
term = Terminals.TTYTerminal(get(ENV,"TERM","dumb"),STDIN,STDOUT,STDERR)
global is_interactive = true
color_set || (global have_color = Terminals.hascolor(term))
end
Expand All @@ -359,8 +359,13 @@ function _start()
end
quiet || REPL.banner(term,term)
ccall(:jl_install_sigint_handler, Void, ())
repl = REPL.LineEditREPL(term)
repl.no_history_file = no_history_file
local repl
if term.term_type == "dumb"
repl = REPL.BasicREPL(term)
else
repl = REPL.LineEditREPL(term)
repl.no_history_file = no_history_file
end
REPL.run_repl(repl)
end
catch err
Expand Down

0 comments on commit c8ae026

Please sign in to comment.