Skip to content

Commit

Permalink
Fix #6939
Browse files Browse the repository at this point in the history
  • Loading branch information
Keno committed Jun 9, 2014
1 parent 35cc52a commit 953a1d4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
44 changes: 30 additions & 14 deletions base/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,19 +203,19 @@ function refresh_multi_line(termbuf::TerminalBuffer, terminal::TTYTerminal, buf,

l = ""

plength = length(prompt)
plength = strwidth(prompt)
pslength = length(prompt.data)
# Now go through the buffer line by line
while cur_row == 0 || (!isempty(l) && l[end] == '\n')
l = readline(buf)
hasnl = !isempty(l) && l[end] == '\n'
cur_row += 1
# We need to deal with UTF8 characters. Since the IOBuffer is a bytearray, we just count bytes
llength = length(l)
llength = strwidth(l)
slength = length(l.data)
if cur_row == 1 # First line
if line_pos < slength
num_chars = length(l[1:line_pos])
num_chars = strwidth(l[1:line_pos])
curs_row = div(plength+num_chars-1, cols) + 1
curs_pos = (plength+num_chars-1) % cols + 1
end
Expand All @@ -229,7 +229,7 @@ function refresh_multi_line(termbuf::TerminalBuffer, terminal::TTYTerminal, buf,
# the '\n' at the end of the previous line)
if curs_row == -1
if line_pos < slength
num_chars = length(l[1:line_pos])
num_chars = strwidth(l[1:line_pos])
curs_row = cur_row + div(indent+num_chars-1, cols)
curs_pos = (indent+num_chars-1) % cols + 1
end
Expand Down Expand Up @@ -310,13 +310,20 @@ function char_move_left(buf::IOBuffer)
c
end

function edit_move_left(s::PromptState)
if position(s.input_buffer) > 0
#move to the next UTF8 character to the left
char_move_left(s.input_buffer)
refresh_line(s)
function edit_move_left(buf::IOBuffer)
if position(buf) > 0
#move to the next base UTF8 character to the left
while true
c = char_move_left(buf)
if charwidth(c) != 0 || position(buf) == 0
break
end
end
return true
end
return false
end
edit_move_left(s::PromptState) = edit_move_left(s.input_buffer) && refresh_line(s)

function edit_move_word_left(s)
if position(s.input_buffer) > 0
Expand Down Expand Up @@ -357,13 +364,22 @@ end
char_move_word_right(s) = char_move_word_right(buffer(s))
char_move_word_left(s) = char_move_word_left(buffer(s))

function edit_move_right(s)
if !eof(s.input_buffer)
# move to the next UTF8 character to the right
char_move_right(s)
refresh_line(s)
function edit_move_right(buf::IOBuffer)
if !eof(buf)
# move to the next base UTF8 character to the right
while true
c = char_move_right(buf)
eof(buf) && break
pos = position(buf)
nextc = read(buf,Char)
seek(buf,pos)
(charwidth(nextc) != 0) && break
end
return true
end
return false
end
edit_move_right(s::PromptState) = edit_move_right(s.input_buffer) && refresh_line(s)

function edit_move_word_right(s)
if !eof(s.input_buffer)
Expand Down
11 changes: 11 additions & 0 deletions test/lineedit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ LineEdit.char_move_word_left(buf)
@test LineEdit.edit_delete_prev_word(buf)
@test bytestring(buf.data[1:buf.size]) == "x = arg3)"

# Unicode combining characters
let buf = IOBuffer()
LineEdit.edit_insert(buf, "")
LineEdit.edit_move_left(buf)
@test position(buf) == 0
LineEdit.edit_move_right(buf)
@test nb_available(buf) == 0
LineEdit.edit_backspace(buf)
@test bytestring(buf.data[1:buf.size]) == "a"
end

## edit_transpose ##
let buf = IOBuffer()
LineEdit.edit_insert(buf, "abcde")
Expand Down

0 comments on commit 953a1d4

Please sign in to comment.