fix word breaker

This commit is contained in:
Cheney 2015-09-23 11:59:39 +08:00
parent 4ab9a96399
commit 9848c61567
2 changed files with 30 additions and 6 deletions

View File

@ -84,11 +84,11 @@ func (r *RuneBuffer) DeleteWord() {
return return
} }
init := r.idx init := r.idx
for init < len(r.buf) && r.buf[init] == ' ' { for init < len(r.buf) && IsWordBreak(r.buf[init]) {
init++ init++
} }
for i := init + 1; i < len(r.buf); i++ { 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.buf = append(r.buf[:r.idx], r.buf[i-1:]...)
r.Refresh() r.Refresh()
return return
@ -102,7 +102,7 @@ func (r *RuneBuffer) MoveToPrevWord() {
return return
} }
for i := r.idx - 1; i > 0; i-- { 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.idx = i
r.Refresh() r.Refresh()
return return
@ -124,12 +124,24 @@ func (r *RuneBuffer) Kill() {
} }
func (r *RuneBuffer) Transform() { 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() { func (r *RuneBuffer) MoveToNextWord() {
for i := r.idx + 1; i < len(r.buf); i++ { 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.idx = i
r.Refresh() r.Refresh()
return return
@ -144,7 +156,7 @@ func (r *RuneBuffer) BackEscapeWord() {
return return
} }
for i := r.idx - 1; i > 0; i-- { 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.buf = append(r.buf[:i], r.buf[r.idx:]...)
r.idx = i r.idx = i
r.Refresh() r.Refresh()

View File

@ -52,6 +52,8 @@ func escapeKey(r rune) rune {
r = MetaNext r = MetaNext
case 'd': case 'd':
r = MetaDelete r = MetaDelete
case CharTransform:
r = MetaTransform
case CharBackspace: case CharBackspace:
r = MetaBackspace r = MetaBackspace
case CharEsc: case CharEsc:
@ -165,3 +167,13 @@ func RunesIndex(r, sub []rune) int {
} }
return -1 return -1
} }
func IsWordBreak(i rune) bool {
if i >= 'a' && i <= 'z' {
return false
}
if i >= 'A' && i <= 'Z' {
return false
}
return true
}