- 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")
- ws := GetLeadingWhitespace(v.buf.lines[v.cursor.y])
- v.cursor.Right()
-
- if settings.AutoIndent {
- v.eh.Insert(v.cursor.Loc(), ws)
- for i := 0; i < len(ws); i++ {
- v.cursor.Right()
- }
- }
- v.cursor.lastVisualX = v.cursor.GetVisualX()
- 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()
- case tcell.KeyBackspace2, tcell.KeyBackspace:
- // Delete a character
- if v.cursor.HasSelection() {
- v.cursor.DeleteSelection()
- v.cursor.ResetSelection()
- } 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
-
- // If the user is using spaces instead of tabs and they are deleting
- // whitespace at the start of the line, we should delete as if its a
- // tab (tabSize number of spaces)
- lineStart := v.buf.lines[v.cursor.y][:v.cursor.x]
- if settings.TabsToSpaces && IsSpaces(lineStart) && len(lineStart) != 0 && len(lineStart)%settings.TabSize == 0 {
- loc := v.cursor.Loc()
- v.cursor.SetLoc(loc - settings.TabSize)
- cx, cy := v.cursor.x, v.cursor.y
- v.cursor.SetLoc(loc)
- v.eh.Remove(loc-settings.TabSize, loc)
- v.cursor.x, v.cursor.y = cx, cy
- } else {
- 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
- }
- }
- v.cursor.lastVisualX = v.cursor.GetVisualX()
- case tcell.KeyTab:
- // Insert a tab
- if v.cursor.HasSelection() {
- v.cursor.DeleteSelection()
- v.cursor.ResetSelection()