Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand word separators list for alt-{f, b, d}, ctrl-w combinations #112

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions line.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,19 +816,19 @@ mainLoop:
fmt.Print(beep)
break
}
// Remove whitespace to the left
// Remove word separators to the left
var buf []rune // Store the deleted chars in a buffer
for {
if pos == 0 || !unicode.IsSpace(line[pos-1]) {
if pos == 0 || !isWordSeparator(line[pos-1]) {
break
}
buf = append(buf, line[pos-1])
line = append(line[:pos-1], line[pos:]...)
pos--
}
// Remove non-whitespace to the left
// Remove non-word separators to the left
for {
if pos == 0 || unicode.IsSpace(line[pos-1]) {
if pos == 0 || isWordSeparator(line[pos-1]) {
break
}
buf = append(buf, line[pos-1])
Expand Down Expand Up @@ -897,19 +897,22 @@ mainLoop:
}
case wordLeft, altB:
if pos > 0 {
var spaceHere, spaceLeft, leftKnown bool
var atWordSeparator, wordSeparatorLeft, leftKnown bool
for {
pos--
if pos == 0 {
break
}
if leftKnown {
spaceHere = spaceLeft
atWordSeparator = wordSeparatorLeft
} else {
spaceHere = unicode.IsSpace(line[pos])
atWordSeparator = isWordSeparator(line[pos])
}
spaceLeft, leftKnown = unicode.IsSpace(line[pos-1]), true
if !spaceHere && spaceLeft {

wordSeparatorLeft = isWordSeparator(line[pos-1])
leftKnown = true

if !atWordSeparator && wordSeparatorLeft {
break
}
}
Expand All @@ -924,19 +927,22 @@ mainLoop:
}
case wordRight, altF:
if pos < len(line) {
var spaceHere, spaceLeft, hereKnown bool
var atWordSeparator, wordSeparatorLeft, hereKnown bool
for {
pos++
if pos == len(line) {
break
}
if hereKnown {
spaceLeft = spaceHere
wordSeparatorLeft = atWordSeparator
} else {
spaceLeft = unicode.IsSpace(line[pos-1])
wordSeparatorLeft = isWordSeparator(line[pos-1])
}
spaceHere, hereKnown = unicode.IsSpace(line[pos]), true
if spaceHere && !spaceLeft {

atWordSeparator = isWordSeparator(line[pos])
hereKnown = true

if atWordSeparator && !wordSeparatorLeft {
break
}
}
Expand Down Expand Up @@ -987,18 +993,18 @@ mainLoop:
fmt.Print(beep)
break
}
// Remove whitespace to the right
// Remove word separators to the right
var buf []rune // Store the deleted chars in a buffer
for {
if pos == len(line) || !unicode.IsSpace(line[pos]) {
if pos == len(line) || !isWordSeparator(line[pos]) {
break
}
buf = append(buf, line[pos])
line = append(line[:pos], line[pos+1:]...)
}
// Remove non-whitespace to the right
// Remove non-word separators to the right
for {
if pos == len(line) || unicode.IsSpace(line[pos]) {
if pos == len(line) || isWordSeparator(line[pos]) {
break
}
buf = append(buf, line[pos])
Expand Down
8 changes: 8 additions & 0 deletions rune.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package liner

import "unicode"

// isWordSeparator is used by alt-{F, B, D}, ctrl-{W} functions to determinate where to stop
func isWordSeparator(r rune) bool {
return unicode.IsSpace(r) || unicode.IsPunct(r)
}