diff --git a/newedit/editutil/basic_mode.go b/newedit/editutil/basic_mode.go index e0e925df..ea15c37a 100644 --- a/newedit/editutil/basic_mode.go +++ b/newedit/editutil/basic_mode.go @@ -1,6 +1,7 @@ package editutil import ( + "unicode" "unicode/utf8" "github.com/elves/elvish/edit/tty" @@ -57,7 +58,7 @@ func BasicHandler(e tty.Event, st *types.State) types.HandlerAction { _, skip := utf8.DecodeRuneInString(raw.Code[raw.Dot:]) raw.Dot += skip default: - if k.Mod == 0 { + if IsChar(k) { s := string(k.Rune) raw.Code = raw.Code[:raw.Dot] + s + raw.Code[raw.Dot:] raw.Dot += len(s) @@ -67,3 +68,8 @@ func BasicHandler(e tty.Event, st *types.State) types.HandlerAction { } return types.NoAction } + +// IsChar returns whether the given key is not a character insertion. +func IsChar(k ui.Key) bool { + return k.Mod == 0 && k.Rune > 0 && unicode.IsGraphic(k.Rune) +} diff --git a/newedit/editutil/basic_mode_test.go b/newedit/editutil/basic_mode_test.go index 5826d0f6..6778f341 100644 --- a/newedit/editutil/basic_mode_test.go +++ b/newedit/editutil/basic_mode_test.go @@ -8,6 +8,7 @@ import ( "github.com/elves/elvish/edit/tty" "github.com/elves/elvish/edit/ui" "github.com/elves/elvish/newedit/types" + "github.com/elves/elvish/tt" ) var basicHandlerKeyEventsTests = []struct { @@ -85,3 +86,12 @@ func TestBasicHandler_IgnoresOtherEvents(t *testing.T) { }) } } + +func TestIsChar(t *testing.T) { + tt.Test(t, tt.Fn("IsChar", IsChar), tt.Table{ + Args(ui.K(ui.Up)).Rets(false), + Args(ui.K('A', ui.Ctrl)).Rets(false), + Args(ui.K('a')).Rets(true), + Args(ui.K('好')).Rets(true), + }) +}