Break lines on enter when needed

This commit is contained in:
Roi Martin 2015-02-14 20:17:44 +01:00
parent 0193dee642
commit f53d985c4e
2 changed files with 22 additions and 11 deletions

View File

@ -54,7 +54,7 @@ func (v *View) editDelete(back bool) error {
// editLine inserts a new line under the cursor.
func (v *View) editLine() error {
v.addLine(v.cy)
v.breakLine(v.cx, v.cy)
if err := v.SetCursor(v.cx, v.cy+1); err != nil {
if err := v.SetOrigin(v.ox, v.oy+1); err != nil {
return err

27
view.go
View File

@ -368,28 +368,39 @@ func (v *View) deleteRune(x, y int) error {
if x < 0 || y < 0 || y >= len(v.lines) || v.lines[y] == nil || x >= len(v.lines[y]) {
return errors.New("invalid point")
}
copy(v.lines[y][x:], v.lines[y][x+1:])
v.lines[y][len(v.lines[y])-1] = ' '
v.lines[y] = append(v.lines[y][:x], v.lines[y][x+1:]...)
return nil
}
// addLine adds a line into the view's internal buffer under the position
// corresponding to the point (x, y).
func (v *View) addLine(y int) error {
// breakLine breaks a line of the internal buffer at the position corresponding
// to the point (x, y).
func (v *View) breakLine(x, y int) error {
v.tainted = true
_, y, err := v.realPosition(0, y)
x, y, err := v.realPosition(x, y)
if err != nil {
return err
}
y = y + 1 // the line must be added under (x, y)
if y < 0 || y >= len(v.lines) {
return errors.New("invalid point")
}
if x < len(v.lines[y]) { // break line
left := make([]rune, len(v.lines[y][x:]))
copy(left, v.lines[y][x:])
right := make([]rune, len(v.lines[y][:x]))
copy(right, v.lines[y][:x])
v.lines[y] = left
v.lines = append(v.lines, nil)
copy(v.lines[y+1:], v.lines[y:])
v.lines[y] = nil
v.lines[y] = right
} else { // new empty line
v.lines = append(v.lines, nil)
copy(v.lines[y+2:], v.lines[y+1:])
v.lines[y+1] = nil
}
return nil
}