From 910107428cf2a37d9afc28670c537f2423347104 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Tue, 6 Apr 2021 16:37:58 -0400 Subject: [PATCH] Buggy incremental search --- internal/editor/editor.go | 81 +++++++++++++++++++++++++++++++-------- internal/gilo/point.go | 5 +++ internal/key/key.go | 4 +- 3 files changed, 73 insertions(+), 17 deletions(-) diff --git a/internal/editor/editor.go b/internal/editor/editor.go index 433fe57..2e6c12c 100644 --- a/internal/editor/editor.go +++ b/internal/editor/editor.go @@ -73,7 +73,7 @@ func (e *editor) ProcessKeypress() bool { func (e *editor) save() { if e.document.Filename == "" { - e.document.Filename = e.prompt("Save as: %s (ESC to cancel)") + e.document.Filename = e.prompt("Save as: %s (ESC to cancel)", nil) if e.document.Filename == "" { e.SetStatusMessage("Save aborted") } @@ -88,7 +88,7 @@ func (e *editor) save() { } } -func (e *editor) prompt(prompt string) string { +func (e *editor) prompt(prompt string, callback func(string, string)) string { buf := gilo.NewBuffer() // Show the prompt message @@ -105,6 +105,9 @@ func (e *editor) prompt(prompt string) string { if ch == key.Enter { if buf.Len() != 0 { e.SetStatusMessage("") + if callback != nil { + callback(buf.ToString(), string(ch)) + } return buf.ToString() } @@ -116,30 +119,78 @@ func (e *editor) prompt(prompt string) string { k := parseEscapeSequence() if k == keyDelete { buf.Truncate(buf.Len() - 1) - } else if k == string(rune(key.Esc)) { + } else if k == string(key.Esc) { e.SetStatusMessage("") + if callback != nil { + callback(buf.ToString(), k) + } return "" + } else { + if callback != nil { + callback(buf.ToString(), k) + } } } + + if callback != nil { + callback(buf.ToString(), string(ch)) + } } } func (e *editor) find() { - query := e.prompt("Search: %s (ESC to cancel)") - if query == "" { - return - } + savedCursor := e.cursor.Clone() + savedOffset := e.cursor.Clone() - for i := 0; i < e.document.RowCount(); i++ { - row := e.document.GetRow(i) - matchIndex := row.Search(query) - if matchIndex != -1 { - e.cursor.Y = i - e.cursor.X = row.RenderXtoCursorX(matchIndex) - e.offset.Y = e.document.RowCount() + lastMatch := -1 + direction := 1 - break + query := e.prompt("Search: %s (ESC to cancel)", func(query string, ch string) { + if ch == string(key.Enter) || ch == string(key.Esc) { + lastMatch = -1 + direction = 1 + return + } else if ch == keyRight || ch == keyDown { + direction = 1 + } else if ch == keyLeft || ch == keyUp { + direction = -1 + } else { + lastMatch = -1 + direction = 1 } + + if lastMatch == -1 { + direction = 1 + } + + current := lastMatch + + for i := 0; i < e.document.RowCount(); i++ { + current += direction + if current == -1 { + current = e.document.RowCount() - 1 + } else if current > e.document.RowCount() { + current = 0 + } + + row := e.document.GetRow(current) + matchIndex := row.Search(query) + if matchIndex != -1 { + lastMatch = current + e.cursor.Y = current + e.cursor.X = row.RenderXtoCursorX(matchIndex) + e.offset.Y = e.document.RowCount() + + break + } + } + }) + + if query == "" { + e.cursor = savedCursor.Clone() + e.offset = savedOffset.Clone() + + return } } diff --git a/internal/gilo/point.go b/internal/gilo/point.go index ab2ed7f..87c47ac 100644 --- a/internal/gilo/point.go +++ b/internal/gilo/point.go @@ -12,3 +12,8 @@ func DefaultPoint() *Point { func NewPoint(x, y int) *Point { return &Point{x, y} } + +func (p *Point) Clone() *Point { + return &Point{p.X, p.Y} +} + diff --git a/internal/key/key.go b/internal/key/key.go index 2fe4769..e843f0e 100644 --- a/internal/key/key.go +++ b/internal/key/key.go @@ -5,8 +5,8 @@ package key // ---------------------------------------------------------------------------- const ( - Backspace = 0x7f - Esc = 0x1b + Backspace = '\x7f' + Esc = '\x1b' Enter = '\r' )