~taiite/public-inbox

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch

[PATCH senpai] editor: Ctrl+{Right,Left} moves cursor by words

Details
Message ID
<20210427162326.8135-1-yyp@disroot.org>
DKIM signature
pass
Download raw message
Patch: +43 -0
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.
---
 app.go       |  4 ++++
 ui/editor.go | 31 +++++++++++++++++++++++++++++++
 ui/ui.go     |  8 ++++++++
 3 files changed, 43 insertions(+)

diff --git a/app.go b/app.go
index 6875987..650a4d8 100644
--- a/app.go
+++ b/app.go
@@ -399,6 +399,8 @@ func (app *App) handleKeyEvent(ev *tcell.EventKey) {
		if ev.Modifiers() == tcell.ModAlt {
			app.win.NextBuffer()
			app.updatePrompt()
		} else if ev.Modifiers() == tcell.ModCtrl {
			app.win.InputRightWord()
		} else {
			app.win.InputRight()
		}
@@ -406,6 +408,8 @@ func (app *App) handleKeyEvent(ev *tcell.EventKey) {
		if ev.Modifiers() == tcell.ModAlt {
			app.win.PreviousBuffer()
			app.updatePrompt()
		} else if ev.Modifiers() == tcell.ModCtrl {
			app.win.InputLeftWord()
		} else {
			app.win.InputLeft()
		}
diff --git a/ui/editor.go b/ui/editor.go
index 5419eaa..a36fb1a 100644
--- a/ui/editor.go
+++ b/ui/editor.go
@@ -180,6 +180,21 @@ func (e *Editor) right() {
	}
}

func (e *Editor) RightWord() {
	line := e.text[e.lineIdx]

	if e.cursorIdx == len(line) {
		return
	}

	for line[e.cursorIdx] == ' ' {
		e.Right()
	}
	for i := e.cursorIdx; i < len(line) && line[i] != ' '; i += 1 {
		e.Right()
	}
}

func (e *Editor) Left() {
	if e.cursorIdx == 0 {
		return
@@ -191,6 +206,22 @@ func (e *Editor) Left() {
			e.offsetIdx = 0
		}
	}
}

func (e *Editor) LeftWord() {
	if e.cursorIdx == 0 {
		return
	}

	line := e.text[e.lineIdx]

	for line[e.cursorIdx - 1] == ' ' {
		e.Left()
	}
	for i := e.cursorIdx - 1; i >= 0 && line[i] != ' '; i -= 1 {
		e.Left()
	}

	e.autoCache = nil
}

diff --git a/ui/ui.go b/ui/ui.go
index 9ab9024..86608d1 100644
--- a/ui/ui.go
+++ b/ui/ui.go
@@ -176,10 +176,18 @@ func (ui *UI) InputRight() {
	ui.e.Right()
}

func (ui *UI) InputRightWord() {
	ui.e.RightWord()
}

func (ui *UI) InputLeft() {
	ui.e.Left()
}

func (ui *UI) InputLeftWord() {
	ui.e.LeftWord()
}

func (ui *UI) InputHome() {
	ui.e.Home()
}
-- 
2.31.1
Reply to thread Export thread (mbox)