Skip to content

Commit 4f37766

Browse files
committed
Move the cursor faster if an arrow key is held in
1 parent b52c252 commit 4f37766

File tree

3 files changed

+119
-94
lines changed

3 files changed

+119
-94
lines changed

Diff for: v2/editor.go

+53
Original file line numberDiff line numberDiff line change
@@ -2928,6 +2928,59 @@ func (e *Editor) CursorBackward(c *vt100.Canvas, status *StatusBar) {
29282928
}
29292929
}
29302930

2931+
// CursorUpward moves the cursor up 1 step
2932+
func (e *Editor) CursorUpward(c *vt100.Canvas, status *StatusBar) {
2933+
if e.DataY() > 0 {
2934+
// Move the position up in the current screen
2935+
if e.UpEnd(c) != nil {
2936+
// If below the top, scroll the contents up
2937+
if e.DataY() > 0 {
2938+
e.redraw.Store(e.ScrollUp(c, status, 1))
2939+
e.pos.Down(c)
2940+
e.UpEnd(c)
2941+
}
2942+
}
2943+
// If the cursor is after the length of the current line, move it to the end of the current line
2944+
if e.AfterLineScreenContents() {
2945+
e.End(c)
2946+
}
2947+
}
2948+
// If the cursor is after the length of the current line, move it to the end of the current line
2949+
if e.AfterLineScreenContents() || e.AfterEndOfLine() {
2950+
e.End(c)
2951+
// Then, if the rune to the left is '}', move one step to the left
2952+
if r := e.LeftRune(); r == '}' {
2953+
e.Prev(c)
2954+
}
2955+
e.redraw.Store(true)
2956+
}
2957+
}
2958+
2959+
// CursorDownward moves the cursor down 1 step
2960+
func (e *Editor) CursorDownward(c *vt100.Canvas, status *StatusBar) {
2961+
if e.DataY() < LineIndex(e.Len()) {
2962+
// Move the position down in the current screen
2963+
if e.DownEnd(c) != nil {
2964+
// If at the bottom, don't move down, but scroll the contents
2965+
// Output a helpful message
2966+
if !e.AfterEndOfDocument() {
2967+
canvasHeight := int(c.Height())
2968+
e.redraw.Store(e.ScrollDown(c, status, 1, canvasHeight))
2969+
e.pos.Up()
2970+
e.DownEnd(c)
2971+
}
2972+
}
2973+
// If the cursor is after the length of the current line, move it to the end of the current line
2974+
if e.AfterLineScreenContents() {
2975+
e.End(c)
2976+
// Then, if the rune to the left is '}', move one step to the left
2977+
if r := e.LeftRune(); r == '}' {
2978+
e.Prev(c)
2979+
}
2980+
}
2981+
}
2982+
}
2983+
29312984
// JoinLineWithNext tries to join the current line with the next.
29322985
// If the next line is empty, the next line is removed.
29332986
// Returns true if this and the next line had text and they were joined to this line.

Diff for: v2/keyhist.go

-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ func (kh *KeyHistory) AllWithin(dur time.Duration) bool {
162162
func (kh *KeyHistory) PrevWithin(dur time.Duration) bool {
163163
khMut.RLock()
164164
defer khMut.RUnlock()
165-
166165
prevTime := kh.t[2]
167166
return time.Since(prevTime) < dur
168167
}

Diff for: v2/keyloop.go

+66-93
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,11 @@ func Loop(tty *vt100.TTY, fnord FilenameOrData, lineNumber LineNumber, colNumber
678678

679679
e.CursorBackward(c, status)
680680

681+
// Check if it has been pressed 2 times the last 200ms, if so, give it an extra boost
682+
if kh.TwoLastAre(leftArrow) && kh.AllWithin(200*time.Millisecond) {
683+
e.CursorBackward(c, status)
684+
}
685+
681686
if e.highlightCurrentLine || e.highlightCurrentText {
682687
e.redraw.Store(true)
683688
e.drawFuncName.Store(true)
@@ -699,10 +704,71 @@ func Loop(tty *vt100.TTY, fnord FilenameOrData, lineNumber LineNumber, colNumber
699704

700705
e.CursorForward(c, status)
701706

707+
if kh.TwoLastAre(leftArrow) && kh.AllWithin(200*time.Millisecond) {
708+
e.CursorForward(c, status)
709+
}
710+
711+
// Check if it has been pressed 2 times the last 200 ms, if so, give it an extra boost
712+
if kh.TwoLastAre(rightArrow) && kh.AllWithin(200*time.Millisecond) {
713+
e.CursorForward(c, status)
714+
e.CursorForward(c, status)
715+
}
716+
717+
if e.highlightCurrentLine || e.highlightCurrentText {
718+
e.redraw.Store(true)
719+
e.drawFuncName.Store(true)
720+
}
721+
722+
case upArrow: // up arrow
723+
// Check if it's a special case
724+
if kh.SpecialArrowKeypressWith(upArrow) {
725+
// Ask the user for a command and run it
726+
e.CommandPrompt(c, tty, status, bookmark, undo)
727+
// It's important to reset the key history after hitting this combo
728+
clearKeyHistory = true
729+
break
730+
}
731+
732+
e.CursorUpward(c, status)
733+
734+
// Check if it has been pressed 2 times the last 200 ms, if so, give it an extra boost
735+
if kh.TwoLastAre(upArrow) && kh.AllWithin(200*time.Millisecond) {
736+
e.CursorUpward(c, status)
737+
}
738+
739+
if e.highlightCurrentLine || e.highlightCurrentText {
740+
e.redraw.Store(true)
741+
e.drawFuncName.Store(true)
742+
}
743+
e.redrawCursor.Store(true)
744+
745+
case downArrow: // down arrow
746+
// Check if it's a special case
747+
if kh.SpecialArrowKeypressWith(downArrow) {
748+
// Ask the user for a command and run it
749+
e.CommandPrompt(c, tty, status, bookmark, undo)
750+
// It's important to reset the key history after hitting this combo
751+
clearKeyHistory = true
752+
break
753+
}
754+
755+
e.CursorDownward(c, status)
756+
757+
// Check if it has been pressed 2 times the last 200 ms, if so, give it an extra boost
758+
if kh.TwoLastAre(downArrow) && kh.AllWithin(200*time.Millisecond) {
759+
e.CursorDownward(c, status)
760+
}
761+
762+
// If the cursor is after the length of the current line, move it to the end of the current line
763+
if e.AfterLineScreenContents() || e.AfterEndOfLine() {
764+
e.End(c)
765+
e.redraw.Store(true)
766+
}
702767
if e.highlightCurrentLine || e.highlightCurrentText {
703768
e.redraw.Store(true)
704769
e.drawFuncName.Store(true)
705770
}
771+
e.redrawCursor.Store(true)
706772

707773
case "c:16": // ctrl-p, scroll up or jump to the previous match, using the sticky search term. In debug mode, change the pane layout.
708774

@@ -781,52 +847,6 @@ func Loop(tty *vt100.TTY, fnord FilenameOrData, lineNumber LineNumber, colNumber
781847

782848
fallthrough // ctrl-p in nano mode
783849

784-
case upArrow: // up arrow
785-
786-
// Check if it's a special case
787-
if kh.SpecialArrowKeypressWith(upArrow) {
788-
// Ask the user for a command and run it
789-
e.CommandPrompt(c, tty, status, bookmark, undo)
790-
// It's important to reset the key history after hitting this combo
791-
clearKeyHistory = true
792-
break
793-
}
794-
795-
if e.DataY() > 0 {
796-
// Move the position up in the current screen
797-
if e.UpEnd(c) != nil {
798-
// If below the top, scroll the contents up
799-
if e.DataY() > 0 {
800-
e.redraw.Store(e.ScrollUp(c, status, 1))
801-
e.pos.Down(c)
802-
e.UpEnd(c)
803-
}
804-
}
805-
// If the cursor is after the length of the current line, move it to the end of the current line
806-
if e.AfterLineScreenContents() {
807-
e.End(c)
808-
}
809-
}
810-
811-
// If the cursor is after the length of the current line, move it to the end of the current line
812-
if e.AfterLineScreenContents() || e.AfterEndOfLine() {
813-
e.End(c)
814-
815-
// Then, if the rune to the left is '}', move one step to the left
816-
if r := e.LeftRune(); r == '}' {
817-
e.Prev(c)
818-
}
819-
820-
e.redraw.Store(true)
821-
}
822-
823-
if e.highlightCurrentLine || e.highlightCurrentText {
824-
e.redraw.Store(true)
825-
e.drawFuncName.Store(true)
826-
}
827-
828-
e.redrawCursor.Store(true)
829-
830850
case "c:14": // ctrl-n, scroll down or jump to next match, using the sticky search term
831851

832852
if !e.nanoMode.Load() {
@@ -989,53 +1009,6 @@ func Loop(tty *vt100.TTY, fnord FilenameOrData, lineNumber LineNumber, colNumber
9891009

9901010
fallthrough // nano mode: ctrl-n
9911011

992-
case downArrow: // down arrow
993-
994-
// Check if it's a special case
995-
if kh.SpecialArrowKeypressWith(downArrow) {
996-
// Ask the user for a command and run it
997-
e.CommandPrompt(c, tty, status, bookmark, undo)
998-
// It's important to reset the key history after hitting this combo
999-
clearKeyHistory = true
1000-
break
1001-
}
1002-
1003-
if e.DataY() < LineIndex(e.Len()) {
1004-
// Move the position down in the current screen
1005-
if e.DownEnd(c) != nil {
1006-
// If at the bottom, don't move down, but scroll the contents
1007-
// Output a helpful message
1008-
if !e.AfterEndOfDocument() {
1009-
canvasHeight := int(c.Height())
1010-
e.redraw.Store(e.ScrollDown(c, status, 1, canvasHeight))
1011-
e.pos.Up()
1012-
e.DownEnd(c)
1013-
}
1014-
}
1015-
// If the cursor is after the length of the current line, move it to the end of the current line
1016-
if e.AfterLineScreenContents() {
1017-
e.End(c)
1018-
1019-
// Then, if the rune to the left is '}', move one step to the left
1020-
if r := e.LeftRune(); r == '}' {
1021-
e.Prev(c)
1022-
}
1023-
}
1024-
}
1025-
1026-
// If the cursor is after the length of the current line, move it to the end of the current line
1027-
if e.AfterLineScreenContents() || e.AfterEndOfLine() {
1028-
e.End(c)
1029-
e.redraw.Store(true)
1030-
}
1031-
1032-
if e.highlightCurrentLine || e.highlightCurrentText {
1033-
e.redraw.Store(true)
1034-
e.drawFuncName.Store(true)
1035-
}
1036-
1037-
e.redrawCursor.Store(true)
1038-
10391012
case "c:12": // ctrl-l, go to line number or percentage
10401013
if !e.nanoMode.Load() {
10411014
regularEditingRightNow = false

0 commit comments

Comments
 (0)