From 9848c615670a680b502be6aa2fc3925eb39f6b3c Mon Sep 17 00:00:00 2001 From: Cheney Date: Wed, 23 Sep 2015 11:59:39 +0800 Subject: [PATCH] fix word breaker --- runebuf.go | 24 ++++++++++++++++++------ utils.go | 12 ++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/runebuf.go b/runebuf.go index 8ec56dc..e32e2e4 100644 --- a/runebuf.go +++ b/runebuf.go @@ -84,11 +84,11 @@ func (r *RuneBuffer) DeleteWord() { return } init := r.idx - for init < len(r.buf) && r.buf[init] == ' ' { + for init < len(r.buf) && IsWordBreak(r.buf[init]) { init++ } for i := init + 1; i < len(r.buf); i++ { - if r.buf[i] != ' ' && r.buf[i-1] == ' ' { + if !IsWordBreak(r.buf[i]) && IsWordBreak(r.buf[i-1]) { r.buf = append(r.buf[:r.idx], r.buf[i-1:]...) r.Refresh() return @@ -102,7 +102,7 @@ func (r *RuneBuffer) MoveToPrevWord() { return } for i := r.idx - 1; i > 0; i-- { - if r.buf[i] != ' ' && r.buf[i-1] == ' ' { + if !IsWordBreak(r.buf[i]) && IsWordBreak(r.buf[i-1]) { r.idx = i r.Refresh() return @@ -124,12 +124,24 @@ func (r *RuneBuffer) Kill() { } func (r *RuneBuffer) Transform() { - + if len(r.buf) < 2 { + if len(r.buf) == 1 { + r.idx++ + r.Refresh() + } + return + } + if r.idx == 0 { + r.idx = 1 + } + r.buf[r.idx], r.buf[r.idx-1] = r.buf[r.idx-1], r.buf[r.idx] + r.idx++ + r.Refresh() } func (r *RuneBuffer) MoveToNextWord() { for i := r.idx + 1; i < len(r.buf); i++ { - if r.buf[i] != ' ' && r.buf[i-1] == ' ' { + if !IsWordBreak(r.buf[i]) && IsWordBreak(r.buf[i-1]) { r.idx = i r.Refresh() return @@ -144,7 +156,7 @@ func (r *RuneBuffer) BackEscapeWord() { return } for i := r.idx - 1; i > 0; i-- { - if r.buf[i] != ' ' && r.buf[i-1] == ' ' { + if !IsWordBreak(r.buf[i]) && IsWordBreak(r.buf[i-1]) { r.buf = append(r.buf[:i], r.buf[r.idx:]...) r.idx = i r.Refresh() diff --git a/utils.go b/utils.go index 41cc464..a8d894c 100644 --- a/utils.go +++ b/utils.go @@ -52,6 +52,8 @@ func escapeKey(r rune) rune { r = MetaNext case 'd': r = MetaDelete + case CharTransform: + r = MetaTransform case CharBackspace: r = MetaBackspace case CharEsc: @@ -165,3 +167,13 @@ func RunesIndex(r, sub []rune) int { } return -1 } + +func IsWordBreak(i rune) bool { + if i >= 'a' && i <= 'z' { + return false + } + if i >= 'A' && i <= 'Z' { + return false + } + return true +}