Add util.Find{FirstEOL LastSOL}; use it

This commit is contained in:
Cheer Xiao 2014-03-10 00:31:22 +08:00
parent 8460fa8b4c
commit 6b7e1296c1
2 changed files with 16 additions and 10 deletions

View File

@ -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
}

View File

@ -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
}