From 6b7e1296c1533d6c4ea76dfc2f3d2373db1b1392 Mon Sep 17 00:00:00 2001 From: Cheer Xiao Date: Mon, 10 Mar 2014 00:31:22 +0800 Subject: [PATCH] Add util.Find{FirstEOL LastSOL}; use it --- edit/builtins.go | 14 ++++---------- util/strings.go | 12 ++++++++++++ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/edit/builtins.go b/edit/builtins.go index a151c7a7..0a6132c1 100644 --- a/edit/builtins.go +++ b/edit/builtins.go @@ -2,9 +2,10 @@ package edit import ( "fmt" - "strings" "unicode" "unicode/utf8" + + "github.com/xiaq/elvish/util" ) // Line editor builtins. @@ -85,21 +86,14 @@ func startCommand(ed *Editor, k Key) *leReturn { } func killLineLeft(ed *Editor, k Key) *leReturn { - // Find last start of line - sol := strings.LastIndex(ed.line[:ed.dot], "\n") + 1 + sol := util.FindLastSOL(ed.line[:ed.dot]) ed.line = ed.line[:sol] + ed.line[ed.dot:] ed.dot = sol return nil } func killLineRight(ed *Editor, k Key) *leReturn { - // Find next end of line - eol := strings.IndexRune(ed.line[ed.dot:], '\n') - if eol == -1 { - eol = len(ed.line) - } else { - eol += ed.dot - } + eol := util.FindFirstEOL(ed.line[ed.dot:]) + ed.dot ed.line = ed.line[:ed.dot] + ed.line[eol:] return nil } diff --git a/util/strings.go b/util/strings.go index 28b21142..d7e54e85 100644 --- a/util/strings.go +++ b/util/strings.go @@ -24,3 +24,15 @@ func FindContext(text string, pos int) (lineno, colno int, line string) { line = strings.SplitN(text[p-colno:], "\n", 2)[0] return } + +func FindFirstEOL(s string) int { + eol := strings.IndexRune(s, '\n') + if eol == -1 { + eol = len(s) + } + return eol +} + +func FindLastSOL(s string) int { + return strings.LastIndex(s, "\n") + 1 +}