Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
integrate REPL.jl code into Base
Browse files Browse the repository at this point in the history
  • Loading branch information
nolta committed Mar 26, 2014
1 parent 02f5553 commit edfbda4
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 133 deletions.
18 changes: 8 additions & 10 deletions base/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ module REPL
end
end

using Terminals
using Readline
using Base.Terminals
using Base.Readline

import Readline: char_move_left, char_move_word_left, CompletionProvider, completeLine
import Base.Readline: char_move_left, char_move_word_left, CompletionProvider, completeLine

type ReadlineREPL <: AbstractREPL
t::TextTerminal
Expand All @@ -153,7 +153,7 @@ module REPL
r::ReadlineREPL
end

using REPLCompletions
using Base.REPLCompletions

function completeLine(c::REPLCompletionProvider,s)
partial = bytestring(s.input_buffer.data[1:position(s.input_buffer)])
Expand All @@ -168,7 +168,7 @@ module REPL
return (ret, partial[range])
end

import Readline: HistoryProvider, add_history, history_prev, history_next, history_search
import Base.Readline: HistoryProvider, add_history, history_prev, history_next, history_search

type REPLHistoryProvider <: HistoryProvider
history::Array{String,1}
Expand Down Expand Up @@ -329,7 +329,6 @@ module REPL
Readline.reset_state(hist::REPLHistoryProvider) = history_reset_state(hist)

const julia_green = "\033[1m\033[32m"
const color_normal = Base.color_normal

function return_callback(repl,s)
if position(s.input_buffer) != 0 && eof(s.input_buffer) &&
Expand Down Expand Up @@ -383,7 +382,7 @@ module REPL
end
end

import Terminals: raw!
import Base.Terminals: raw!

function reset(d::REPLDisplay)
raw!(d.repl.t,false)
Expand Down Expand Up @@ -473,7 +472,7 @@ module REPL

(hkp,hkeymap) = Readline.setup_search_keymap(hp)

# Canoniczlize user keymap input
# Canonicalize user keymap input
if isa(extra_repl_keymap,Dict)
extra_repl_keymap = [extra_repl_keymap]
end
Expand Down Expand Up @@ -569,7 +568,6 @@ module REPL
repl_channel = RemoteRef()
response_channel = RemoteRef()
start_repl_backend(repl_channel, response_channel)
banner(t,t)
run_frontend(ReadlineREPL(t),repl_channel,response_channel)
end

Expand Down Expand Up @@ -639,7 +637,7 @@ module REPL
if !isempty(line)
ast = Base.parse_input_line(line)
if have_color
print(repl.stream,color_normal)
print(repl.stream, Base.color_normal)
end
put!(repl_channel, (ast,1))
(val, bt) = take!(response_channel)
Expand Down
10 changes: 5 additions & 5 deletions base/Readline.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Readline
using Terminals
module Readline
using Base.Terminals

import Terminals: raw!, width, height, cmove, Rect, Size, getX,
getY, clear_line, beep
import Base.Terminals: raw!, width, height, cmove, Rect, Size, getX,
getY, clear_line, beep

import Base: ensureroom, peek

Expand Down Expand Up @@ -1134,4 +1134,4 @@
raw!(terminal,false) && Terminals.Unix.disable_bracketed_paste(terminal)
end
end
end
end
25 changes: 8 additions & 17 deletions base/Terminals.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Terminals
import Base.size, Base.write, Base.flush
abstract TextTerminal <: Base.IO
export TextTerminal, NCurses, writepos, cmove, pos, getX, getY, hascolor
export TextTerminal, writepos, cmove, pos, getX, getY, hascolor

# Stuff that really should be in a Geometry package
immutable Rect
Expand All @@ -16,7 +16,6 @@ module Terminals
height
end


# INTERFACE
size(::TextTerminal) = error("Unimplemented")
writepos(t::TextTerminal,x,y,s::Array{Uint8,1}) = error("Unimplemented")
Expand Down Expand Up @@ -83,7 +82,7 @@ module Terminals
module Attributes
# This is just to get started and will have to be revised

import Terminals.TextAttribute, Terminals.TextTerminal
import ..Terminals: TextAttribute, TextTerminal

export Standout, Underline, Reverse, Blink, Dim, Bold, AltCharset, Invisible, Protect, Left, Right, Top,
Vertical, Horizontal, Low
Expand Down Expand Up @@ -129,10 +128,9 @@ module Terminals
end

module Colors
import Terminals.TextAttribute, Terminals.TextTerminal, Terminals.Attributes.attr_simplify
using Color
import ..Terminals: TextAttribute, TextTerminal

export TerminalColor, TextColor, BackgroundColor, ForegroundColor, approximate,
export TerminalColor, TextColor, BackgroundColor, ForegroundColor,
lookup_color, terminal_color, maxcolors, maxcolorpairs, palette, numcolors

# Represents a color actually displayable by the current terminal
Expand All @@ -145,13 +143,6 @@ module Terminals
c::TerminalColor
end

function approximate(t::TextTerminal, c::ColorValue)
x = keys(palette(t))
lookup_color(t,x[indmin(map(x->colordiff(c,x),x))])
end

attr_simplify(t::TextTerminal, c::ColorValue) = TextColor(lookup_color(t,c))

# Terminals should implement this
lookup_color(t::TextTerminal) = error("Unimplemented")
maxcolors(t::TextTerminal) = error("Unimplemented")
Expand All @@ -161,10 +152,10 @@ module Terminals
end

module Unix
importall Terminals
importall ..Terminals

import Terminals: width, height, cmove, Rect, Size, getX,
getY, raw!, clear, clear_line, beep, hascolor
import ..Terminals: width, height, cmove, Rect, Size, getX,
getY, raw!, clear, clear_line, beep, hascolor
import Base: size, read, write, flush, TTY, writemime, readuntil, start_reading, stop_reading

export UnixTerminal
Expand Down Expand Up @@ -218,4 +209,4 @@ module Terminals
end
importall .Unix
export UnixTerminal
end
end
75 changes: 9 additions & 66 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,65 +145,6 @@ end

_repl_start = Condition()

function run_repl()
global const repl_channel = RemoteRef()

ccall(:jl_init_repl, Void, (Cint,), _use_history)

# install Ctrl-C interrupt handler (InterruptException)
ccall(:jl_install_sigint_handler, Void, ())
buf = Array(Uint8)
global _repl_enough_stdin = true
input = @async begin
try
while true
if _repl_enough_stdin
wait(_repl_start)
end
try
if eof(STDIN) # if TTY, can throw InterruptException, must be in try/catch block
return
end
read!(STDIN, buf)
ccall(:jl_read_buffer,Void,(Ptr{Void},Cssize_t),buf,length(buf))
catch ex
if isa(ex,InterruptException)
println(STDOUT, "^C")
ccall(:jl_reset_input,Void,())
repl_callback(nothing, 0)
else
rethrow(ex)
end
end
end
finally
put!(repl_channel,(nothing,-1))
end
end

while !istaskdone(input)
if have_color
prompt_string = "\01\033[1m\033[32m\02julia> \01\033[0m"*input_color()*"\02"
else
prompt_string = "julia> "
end
ccall(:repl_callback_enable, Void, (Ptr{Uint8},), prompt_string)
global _repl_enough_stdin = false
notify(_repl_start)
start_reading(STDIN)
(ast, show_value) = take!(repl_channel)
if show_value == -1
# exit flag
break
end
eval_user_input(ast, show_value!=0)
end

if have_color
print(color_normal)
end
end

function parse_input_line(s::String)
# s = bytestring(s)
# (expr, pos) = parse(s, 1)
Expand Down Expand Up @@ -373,6 +314,9 @@ function early_init()
start_gc_msgs_task()
end

import .Terminals
import .REPL

function _start()
early_init()

Expand All @@ -382,17 +326,15 @@ function _start()
(quiet,repl,startup,color_set,history) = process_options(copy(ARGS))
global _use_history = history

local term
if repl
term = Terminals.UnixTerminal(get(ENV,"TERM",""),STDIN,STDOUT,STDERR)
if !isa(STDIN,TTY)
global is_interactive = !isa(STDIN,Union(File,IOStream))
color_set || (global have_color = false)
else
global is_interactive = true
if !color_set
@windows_only global have_color = true
@unix_only global have_color =
(beginswith(get(ENV,"TERM",""),"xterm") || success(`tput setaf 0`))
end
color_set || (global have_color = Terminals.hascolor(term))
end
end

Expand All @@ -415,8 +357,9 @@ function _start()
end
quit()
end
quiet || banner()
run_repl()
quiet || REPL.banner(term,term)
ccall(:jl_install_sigint_handler, Void, ())
REPL.run_repl(term)
end
catch err
display_error(err,catch_backtrace())
Expand Down
1 change: 0 additions & 1 deletion base/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ precompile(isempty, (Array{Any,1},))
precompile(getindex, (Dict{Any,Any}, Int32))
precompile(_start, ())
precompile(process_options, (Array{Any,1},))
precompile(run_repl, ())
precompile(any, (Function, Array{Any,1}))
precompile(Dict{Any,Any}, (Int,))
precompile(Set, ())
Expand Down
File renamed without changes.
12 changes: 9 additions & 3 deletions base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ include("multi.jl")
# Polling (requires multi.jl)
include("poll.jl")

# front end & code loading
include("repl.jl")
include("client.jl")
# code loading
include("loading.jl")

begin
Expand Down Expand Up @@ -185,6 +183,14 @@ using .I18n
using .Help
push!(I18n.CALLBACKS, Help.clear_cache)

# frontend
include("Terminals.jl")
include("Readline.jl")
include("REPLCompletions.jl")
include("REPL.jl")
include("replutil.jl")
include("client.jl")

# sparse matrices and linear algebra
include("sparse.jl")
importall .SparseMatrix
Expand Down
5 changes: 3 additions & 2 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ JULIAHOME = $(abspath ..)
include ../Make.inc

TESTS = all core keywordargs numbers strings unicode collections hashing \
remote iobuffer arrayops linalg blas fft dsp sparse bitarray random \
remote iobuffer arrayops linalg blas fft dsp sparse bitarray random \
math functional bigint sorting statistics spawn parallel arpack file \
git pkg resolve suitesparse complex version pollfd mpfr broadcast \
socket floatapprox priorityqueue readdlm regex float16 combinatorics \
sysinfo rounding ranges mod2pi euler show linalg1 linalg2
sysinfo rounding ranges mod2pi euler show linalg1 linalg2 readline \
replcompletions

default: all

Expand Down
4 changes: 2 additions & 2 deletions test/readline.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Base.Test
using Base.Readline

a_foo = 0

Expand Down Expand Up @@ -43,4 +43,4 @@ run_test(test2_func,IOBuffer("aaabb"))

run_test(test3_func,IOBuffer("aab"))
@test a_bar == 2
@test b_bar == 1
@test b_bar == 1
Loading

0 comments on commit edfbda4

Please sign in to comment.