2015-01-31 20:39:43 +01:00
|
|
|
// Copyright 2014 The gocui Authors. All rights reserved.
|
2014-01-22 23:44:42 +01:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-01-22 23:44:08 +01:00
|
|
|
package gocui
|
|
|
|
|
2015-02-23 02:01:49 +01:00
|
|
|
const MaxInt = int(^uint(0) >> 1)
|
|
|
|
|
2015-02-24 00:26:26 +01:00
|
|
|
// EditHandler allows to define the handler that manages the edition mode,
|
|
|
|
// including keybindings or cursor behaviour. DefaultEditHandler is used by
|
|
|
|
// default.
|
|
|
|
var EditHandler = DefaultEditHandler
|
|
|
|
|
|
|
|
// DefaultEditHandler is used as the default EditHandler.
|
|
|
|
func DefaultEditHandler(v *View, key Key, ch rune, mod Modifier) {
|
2015-02-13 18:40:45 +01:00
|
|
|
switch {
|
2015-02-24 00:26:26 +01:00
|
|
|
case ch != 0 && mod == 0:
|
|
|
|
v.EditWrite(ch)
|
|
|
|
case key == KeySpace:
|
|
|
|
v.EditWrite(' ')
|
|
|
|
case key == KeyBackspace || key == KeyBackspace2:
|
|
|
|
v.EditDelete(true)
|
|
|
|
case key == KeyDelete:
|
|
|
|
v.EditDelete(false)
|
|
|
|
case key == KeyInsert:
|
|
|
|
v.Overwrite = !v.Overwrite
|
|
|
|
case key == KeyEnter:
|
|
|
|
v.EditNewLine()
|
|
|
|
case key == KeyArrowDown:
|
|
|
|
v.MoveCursor(0, 1, false)
|
|
|
|
case key == KeyArrowUp:
|
|
|
|
v.MoveCursor(0, -1, false)
|
|
|
|
case key == KeyArrowLeft:
|
|
|
|
v.MoveCursor(-1, 0, false)
|
|
|
|
case key == KeyArrowRight:
|
|
|
|
v.MoveCursor(1, 0, false)
|
2015-02-13 18:40:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 00:26:26 +01:00
|
|
|
// EditWrite writes a rune at the cursor position.
|
|
|
|
func (v *View) EditWrite(ch rune) {
|
2014-01-22 23:44:08 +01:00
|
|
|
v.writeRune(v.cx, v.cy, ch)
|
2015-02-24 00:26:26 +01:00
|
|
|
v.MoveCursor(1, 0, true)
|
2014-01-22 23:44:08 +01:00
|
|
|
}
|
|
|
|
|
2015-02-24 00:26:26 +01:00
|
|
|
// EditDelete deletes a rune at the cursor position. back determines the
|
|
|
|
// direction.
|
|
|
|
func (v *View) EditDelete(back bool) {
|
2015-02-23 11:07:08 +01:00
|
|
|
x, y := v.ox+v.cx, v.oy+v.cy
|
2015-02-23 10:20:22 +01:00
|
|
|
if y < 0 {
|
|
|
|
return
|
|
|
|
} else if y >= len(v.viewLines) {
|
2015-02-24 00:26:26 +01:00
|
|
|
v.MoveCursor(-1, 0, true)
|
2015-02-23 10:20:22 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
maxX, _ := v.Size()
|
2014-01-22 23:44:08 +01:00
|
|
|
if back {
|
2015-02-23 11:07:08 +01:00
|
|
|
if x == 0 { // start of the line
|
2015-02-23 10:20:22 +01:00
|
|
|
if y < 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var maxPrevWidth int
|
|
|
|
if v.Wrap {
|
|
|
|
maxPrevWidth = maxX
|
|
|
|
} else {
|
|
|
|
maxPrevWidth = MaxInt
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.viewLines[y].linesX == 0 { // regular line
|
|
|
|
v.mergeLines(v.cy - 1)
|
|
|
|
if len(v.viewLines[y-1].line) < maxPrevWidth {
|
2015-02-24 00:26:26 +01:00
|
|
|
v.MoveCursor(-1, 0, true)
|
2015-02-23 10:20:22 +01:00
|
|
|
}
|
|
|
|
} else { // wrapped line
|
|
|
|
v.deleteRune(len(v.viewLines[y-1].line)-1, v.cy-1)
|
2015-02-24 00:26:26 +01:00
|
|
|
v.MoveCursor(-1, 0, true)
|
2015-02-23 10:20:22 +01:00
|
|
|
}
|
2015-02-23 11:13:25 +01:00
|
|
|
} else { // middle/end of the line
|
2015-02-23 00:34:41 +01:00
|
|
|
v.deleteRune(v.cx-1, v.cy)
|
2015-02-24 00:26:26 +01:00
|
|
|
v.MoveCursor(-1, 0, true)
|
2014-01-22 23:44:08 +01:00
|
|
|
}
|
2015-02-23 11:09:31 +01:00
|
|
|
} else {
|
|
|
|
if x == len(v.viewLines[y].line) { // end of the line
|
|
|
|
v.mergeLines(v.cy)
|
2015-02-23 11:13:25 +01:00
|
|
|
} else { // start/middle of the line
|
2015-02-23 11:09:31 +01:00
|
|
|
v.deleteRune(v.cx, v.cy)
|
|
|
|
}
|
2014-01-22 23:44:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 00:26:26 +01:00
|
|
|
// EditNewLine inserts a new line under the cursor.
|
|
|
|
func (v *View) EditNewLine() {
|
2015-02-14 20:17:44 +01:00
|
|
|
v.breakLine(v.cx, v.cy)
|
2015-02-23 00:34:41 +01:00
|
|
|
|
|
|
|
y := v.oy + v.cy
|
|
|
|
if y >= len(v.viewLines) || (y >= 0 && y < len(v.viewLines) &&
|
|
|
|
!(v.Wrap && v.cx == 0 && v.viewLines[y].linesX > 0)) {
|
|
|
|
// new line at the end of the buffer or
|
|
|
|
// cursor is not at the beginning of a wrapped line
|
|
|
|
v.ox = 0
|
|
|
|
v.cx = 0
|
2015-02-24 00:26:26 +01:00
|
|
|
v.MoveCursor(0, 1, true)
|
2015-02-23 00:34:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 00:26:26 +01:00
|
|
|
// MoveCursor moves the cursor taking into account the width of the line/view,
|
|
|
|
// displacing the origin if necessary.
|
|
|
|
func (v *View) MoveCursor(dx, dy int, writeMode bool) {
|
2015-02-23 00:34:41 +01:00
|
|
|
maxX, maxY := v.Size()
|
|
|
|
cx, cy := v.cx+dx, v.cy+dy
|
2015-02-23 02:01:49 +01:00
|
|
|
x, y := v.ox+cx, v.oy+cy
|
2015-02-23 00:34:41 +01:00
|
|
|
|
|
|
|
var curLineWidth, prevLineWidth int
|
|
|
|
// get the width of the current line
|
|
|
|
if writeMode {
|
2015-02-23 02:01:49 +01:00
|
|
|
if v.Wrap {
|
|
|
|
curLineWidth = maxX - 1
|
|
|
|
} else {
|
|
|
|
curLineWidth = MaxInt
|
|
|
|
}
|
2015-02-23 00:34:41 +01:00
|
|
|
} else {
|
|
|
|
if y >= 0 && y < len(v.viewLines) {
|
2015-02-23 02:01:49 +01:00
|
|
|
curLineWidth = len(v.viewLines[y].line)
|
|
|
|
if v.Wrap && curLineWidth >= maxX {
|
2015-02-23 00:34:41 +01:00
|
|
|
curLineWidth = maxX - 1
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
curLineWidth = 0
|
2014-01-22 23:44:08 +01:00
|
|
|
}
|
|
|
|
}
|
2015-02-23 00:34:41 +01:00
|
|
|
// get the width of the previous line
|
|
|
|
if y-1 >= 0 && y-1 < len(v.viewLines) {
|
|
|
|
prevLineWidth = len(v.viewLines[y-1].line)
|
|
|
|
} else {
|
|
|
|
prevLineWidth = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// adjust cursor's x position and view's x origin
|
2015-02-23 02:01:49 +01:00
|
|
|
if x > curLineWidth { // move to next line
|
2015-02-23 00:34:41 +01:00
|
|
|
if dx > 0 { // horizontal movement
|
2015-02-23 02:01:49 +01:00
|
|
|
if !v.Wrap {
|
|
|
|
v.ox = 0
|
|
|
|
}
|
2015-02-23 00:34:41 +01:00
|
|
|
v.cx = 0
|
|
|
|
cy += 1
|
|
|
|
} else { // vertical movement
|
2015-02-23 16:37:49 +01:00
|
|
|
if curLineWidth > 0 { // move cursor to the EOL
|
2015-02-23 02:01:49 +01:00
|
|
|
if v.Wrap {
|
|
|
|
v.cx = curLineWidth
|
|
|
|
} else {
|
|
|
|
ncx := curLineWidth - v.ox
|
|
|
|
if ncx < 0 {
|
|
|
|
v.ox += ncx
|
|
|
|
if v.ox < 0 {
|
|
|
|
v.ox = 0
|
|
|
|
}
|
|
|
|
v.cx = 0
|
|
|
|
} else {
|
|
|
|
v.cx = ncx
|
|
|
|
}
|
|
|
|
}
|
2015-02-23 00:34:41 +01:00
|
|
|
} else {
|
2015-02-23 02:01:49 +01:00
|
|
|
if !v.Wrap {
|
|
|
|
v.ox = 0
|
|
|
|
}
|
2015-02-23 00:34:41 +01:00
|
|
|
v.cx = 0
|
|
|
|
}
|
|
|
|
}
|
2015-02-23 02:01:49 +01:00
|
|
|
} else if cx < 0 {
|
|
|
|
if !v.Wrap && v.ox > 0 { // move origin to the left
|
|
|
|
v.ox -= 1
|
|
|
|
} else { // move to previous line
|
|
|
|
if prevLineWidth > 0 {
|
|
|
|
if !v.Wrap { // set origin so the EOL is visible
|
|
|
|
nox := prevLineWidth - maxX + 1
|
|
|
|
if nox < 0 {
|
|
|
|
v.ox = 0
|
|
|
|
} else {
|
|
|
|
v.ox = nox
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v.cx = prevLineWidth
|
|
|
|
} else {
|
|
|
|
if !v.Wrap {
|
|
|
|
v.ox = 0
|
|
|
|
}
|
|
|
|
v.cx = 0
|
|
|
|
}
|
|
|
|
cy -= 1
|
2015-02-23 00:34:41 +01:00
|
|
|
}
|
|
|
|
} else { // stay on the same line
|
2015-02-23 02:01:49 +01:00
|
|
|
if v.Wrap {
|
|
|
|
v.cx = cx
|
|
|
|
} else {
|
|
|
|
if cx >= maxX {
|
|
|
|
v.ox += 1
|
|
|
|
} else {
|
|
|
|
v.cx = cx
|
|
|
|
}
|
|
|
|
}
|
2014-01-23 21:26:53 +01:00
|
|
|
}
|
2015-02-23 00:34:41 +01:00
|
|
|
|
|
|
|
// adjust cursor's y position and view's y origin
|
|
|
|
if cy >= maxY {
|
|
|
|
v.oy += 1
|
|
|
|
} else if cy < 0 {
|
|
|
|
if v.oy > 0 {
|
|
|
|
v.oy -= 1
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
v.cy = cy
|
2014-01-23 21:26:53 +01:00
|
|
|
}
|
2014-01-22 23:44:08 +01:00
|
|
|
}
|