Skip to content

Commit

Permalink
cosmetic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed Aug 15, 2017
1 parent 7333272 commit 771ecdb
Showing 1 changed file with 51 additions and 59 deletions.
110 changes: 51 additions & 59 deletions base/repl/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -478,49 +478,49 @@ function edit_insert(buf::IOBuffer, c)
end
end

function edit_backspace(s::PromptState, align::Bool=false, adjust=align)
if edit_backspace(s.input_buffer, align)
refresh_line(s)
else
beep(terminal(s))
end
end
# align: delete up to 4 spaces to align to a multiple of 4 chars
# adjust: also delete spaces on the right of the cursor to try to keep aligned what is
# on the right
edit_backspace(s::PromptState, align::Bool=false, adjust=align) =
edit_backspace(s.input_buffer, align) ? refresh_line(s) : beep(terminal(s))

const _newline = UInt8('\n')
const _space = UInt8(' ')

# align: delete up to 4 spaces to align to a multiple of 4 chars
# adjust: also delete spaces to the right of the cursor to try to keep aligned what is
# on the right
_notspace(c) = c != _space

beginofline(buf, pos=position(buf)) = findprev(buf.data, _newline, pos)

function endofline(buf, pos=position(buf))
eol = findnext(buf.data[pos+1:buf.size], _newline, 1)
eol == 0 ? buf.size : pos + eol - 1
end

function edit_backspace(buf::IOBuffer, align::Bool=false, adjust::Bool=align)
!align && adjust &&
throw(DomainError((align, adjust),
"if `adjust` is `true`, `align` must be `true`"))
oldpos = position(buf)
if oldpos > 0
c = char_move_left(buf)
newpos = position(buf)
if align && c == ' ' # maybe delete multiple spaces
beg = beginofline(buf, newpos)
align = strwidth(String(buf.data[1+beg:newpos])) % 4
nonspace = findprev(c -> c != _space, buf.data, newpos)
if newpos-align >= nonspace
newpos -= align
seek(buf, newpos)
if adjust
spaces = findnext(c -> c!= _space,
buf.data[newpos+2:buf.size], 1)
oldpos = spaces == 0 ? buf.size :
buf.data[newpos+1+spaces] == _newline ? newpos+spaces :
newpos + min(spaces, 4)
end
oldpos == 0 && return false
c = char_move_left(buf)
newpos = position(buf)
if align && c == ' ' # maybe delete multiple spaces
beg = beginofline(buf, newpos)
align = strwidth(String(buf.data[1+beg:newpos])) % 4
nonspace = findprev(_notspace, buf.data, newpos)
if newpos - align >= nonspace
newpos -= align
seek(buf, newpos)
if adjust
spaces = findnext(_notspace, buf.data[newpos+2:buf.size], 1)
oldpos = spaces == 0 ? buf.size :
buf.data[newpos+1+spaces] == _newline ? newpos+spaces :
newpos + min(spaces, 4)
end
end
splice_buffer!(buf, newpos:oldpos-1)
return true
else
return false
end
splice_buffer!(buf, newpos:oldpos-1)
return true
end

edit_delete(s) = edit_delete(buffer(s)) ? refresh_line(s) : beep(terminal(s))
Expand Down Expand Up @@ -1364,43 +1364,35 @@ function bracketed_paste(s)
return replace(input, '\t', " "^tabwidth)
end

beginofline(buf, pos=position(buf)) = findprev(buf.data, '\n' % UInt8, pos)

function endofline(buf, pos=position(buf))
eol = findnext(buf.data[pos+1:buf.size], '\n' % UInt8, 1)
eol == 0 ? buf.size : pos + eol - 1
end

# jump_spaces: if cursor is on a ' ', move it to the first non-' ' char on the right
# if `delete_trailing`, ignore trailing ' ' by deleting them
function edit_tab(s, jump_spaces=false, delete_trailing=jump_spaces)
function tab_should_complete(s)
# Yes, we are ignoring the possiblity
# the we could be in the middle of a multi-byte
# sequence, here but that's ok, since any
# whitespace we're interested in is only one byte
buf = buffer(s)
i = position(buf)
if i != 0 &&
let c = buf.data[i]
c == UInt8('\n') || c == UInt8('\t') ||
# hack to allow path completion in cmds
# after a space, e.g., `cd <tab>`, while still
# allowing multiple indent levels
(c == UInt8(' ') && i > 3 && buf.data[i-1] == UInt8(' '))
end
edit_tab(buf, jump_spaces, delete_trailing)
else
complete_line(s)
end
pos = position(buf)
pos == 0 && return true
c = buf.data[pos]
c != _newline && c != UInt8('\t') &&
# hack to allow path completion in cmds
# after a space, e.g., `cd <tab>`, while still
# allowing multiple indent levels
(c != _space || pos <= 3 || buf.data[pos-1] != _space)
end

# jump_spaces: if cursor is on a ' ', move it to the first non-' ' char on the right
# if `delete_trailing`, ignore trailing ' ' by deleting them
function edit_tab(s, jump_spaces=false, delete_trailing=jump_spaces)
tab_should_complete(s) ?
complete_line(s) :
edit_tab(buffer(s), jump_spaces, delete_trailing)
refresh_line(s)
end

function Base.LineEdit.edit_tab(buf::IOBuffer,
jump_spaces=false, delete_trailing=jump_spaces)
function edit_tab(buf::IOBuffer, jump_spaces=false, delete_trailing=jump_spaces)
i = position(buf)
if jump_spaces && i < buf.size && buf.data[i+1] == _space
spaces = findnext(c -> c != _space,
buf.data[i+1:buf.size], 1)
spaces = findnext(_notspace, buf.data[i+1:buf.size], 1)
if delete_trailing && (spaces == 0 || buf.data[i+spaces] == _newline)
splice_buffer!(buf, i:(spaces == 0 ? buf.size-1 : i+spaces-2))
else
Expand All @@ -1409,7 +1401,7 @@ function Base.LineEdit.edit_tab(buf::IOBuffer,
end
end
# align to multiples of 4:
align = 4 - strwidth(String(buf.data[1+beginofline(buf,i):i])) % 4
align = 4 - strwidth(String(buf.data[1+beginofline(buf, i):i])) % 4
return edit_insert(buf, ' '^align)
end

Expand Down

0 comments on commit 771ecdb

Please sign in to comment.