commit d12564c

Alexey Yerin  ·  2021-04-27 16:23:26 +0000 UTC
parent c7eceaa
editor: Ctrl+{Right,Left} moves cursor by words

A very useful readline feature when combined with ^W can be a really
quick way to reformat a sentence, which is what I do on IRC quite often.
3 files changed,  +43, -0
M app.go
M app.go
+4, -0
 1@@ -399,6 +399,8 @@ func (app *App) handleKeyEvent(ev *tcell.EventKey) {
 2 		if ev.Modifiers() == tcell.ModAlt {
 3 			app.win.NextBuffer()
 4 			app.updatePrompt()
 5+		} else if ev.Modifiers() == tcell.ModCtrl {
 6+			app.win.InputRightWord()
 7 		} else {
 8 			app.win.InputRight()
 9 		}
10@@ -406,6 +408,8 @@ func (app *App) handleKeyEvent(ev *tcell.EventKey) {
11 		if ev.Modifiers() == tcell.ModAlt {
12 			app.win.PreviousBuffer()
13 			app.updatePrompt()
14+		} else if ev.Modifiers() == tcell.ModCtrl {
15+			app.win.InputLeftWord()
16 		} else {
17 			app.win.InputLeft()
18 		}
+31, -0
 1@@ -180,6 +180,21 @@ func (e *Editor) right() {
 2 	}
 3 }
 4 
 5+func (e *Editor) RightWord() {
 6+	line := e.text[e.lineIdx]
 7+
 8+	if e.cursorIdx == len(line) {
 9+		return
10+	}
11+
12+	for line[e.cursorIdx] == ' ' {
13+		e.Right()
14+	}
15+	for i := e.cursorIdx; i < len(line) && line[i] != ' '; i += 1 {
16+		e.Right()
17+	}
18+}
19+
20 func (e *Editor) Left() {
21 	if e.cursorIdx == 0 {
22 		return
23@@ -191,6 +206,22 @@ func (e *Editor) Left() {
24 			e.offsetIdx = 0
25 		}
26 	}
27+}
28+
29+func (e *Editor) LeftWord() {
30+	if e.cursorIdx == 0 {
31+		return
32+	}
33+
34+	line := e.text[e.lineIdx]
35+
36+	for line[e.cursorIdx - 1] == ' ' {
37+		e.Left()
38+	}
39+	for i := e.cursorIdx - 1; i >= 0 && line[i] != ' '; i -= 1 {
40+		e.Left()
41+	}
42+
43 	e.autoCache = nil
44 }
45 
+8, -0
 1@@ -176,10 +176,18 @@ func (ui *UI) InputRight() {
 2 	ui.e.Right()
 3 }
 4 
 5+func (ui *UI) InputRightWord() {
 6+	ui.e.RightWord()
 7+}
 8+
 9 func (ui *UI) InputLeft() {
10 	ui.e.Left()
11 }
12 
13+func (ui *UI) InputLeftWord() {
14+	ui.e.LeftWord()
15+}
16+
17 func (ui *UI) InputHome() {
18 	ui.e.Home()
19 }