- switch e.Key() {
- case tcell.KeyUp:
- // Cursor up
- v.cursor.ResetSelection()
- v.cursor.Up()
- case tcell.KeyDown:
- // Cursor down
- v.cursor.ResetSelection()
- v.cursor.Down()
- case tcell.KeyLeft:
- // Cursor left
- v.cursor.ResetSelection()
- v.cursor.Left()
- case tcell.KeyRight:
- // Cursor right
- v.cursor.ResetSelection()
- v.cursor.Right()
- case tcell.KeyEnter:
- // Insert a newline
- if v.cursor.HasSelection() {
- v.cursor.DeleteSelection()
- v.cursor.ResetSelection()
- }
- v.eh.Insert(v.cursor.Loc(), "\n")
- v.cursor.Right()
- // Rehighlight the entire buffer
- v.UpdateLines(v.topline, v.topline+v.height)
- v.cursor.lastVisualX = v.cursor.GetVisualX()
- // v.UpdateLines(v.cursor.y-1, v.cursor.y)
- case tcell.KeySpace:
- // Insert a space
- if v.cursor.HasSelection() {
- v.cursor.DeleteSelection()
- v.cursor.ResetSelection()
- }
- v.eh.Insert(v.cursor.Loc(), " ")
- v.cursor.Right()
- v.UpdateLines(v.cursor.y, v.cursor.y)
- case tcell.KeyBackspace2, tcell.KeyBackspace:
- // Delete a character
- if v.cursor.HasSelection() {
- v.cursor.DeleteSelection()
- v.cursor.ResetSelection()
- // Rehighlight the entire buffer
- v.UpdateLines(v.topline, v.topline+v.height)
- } else if v.cursor.Loc() > 0 {
- // We have to do something a bit hacky here because we want to
- // delete the line by first moving left and then deleting backwards
- // but the undo redo would place the cursor in the wrong place
- // So instead we move left, save the position, move back, delete
- // and restore the position
- v.cursor.Left()
- cx, cy := v.cursor.x, v.cursor.y
- v.cursor.Right()
- loc := v.cursor.Loc()
- v.eh.Remove(loc-1, loc)
- v.cursor.x, v.cursor.y = cx, cy
- // Rehighlight the entire buffer
- v.UpdateLines(v.topline, v.topline+v.height)
- // v.UpdateLines(v.cursor.y, v.cursor.y+1)
- }
- v.cursor.lastVisualX = v.cursor.GetVisualX()
- case tcell.KeyTab:
- // Insert a tab
- if v.cursor.HasSelection() {
- v.cursor.DeleteSelection()
- v.cursor.ResetSelection()