Skip to content

Commit

Permalink
ignore quoted and escaped characters when splitting keybindings into …
Browse files Browse the repository at this point in the history
…actions
  • Loading branch information
matthias314 committed Jan 16, 2025
1 parent f49487d commit 98d7e28
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
4 changes: 1 addition & 3 deletions internal/action/bufpane.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ func BufMapEvent(k Event, action string) {
break
}

// TODO: fix problem when complex bindings have these
// characters (escape them?)
idx := strings.IndexAny(action, "&|,")
idx := util.IndexAnyUnquoted(action, "&|,")
a := action
if idx >= 0 {
a = action[:idx]
Expand Down
19 changes: 19 additions & 0 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,25 @@ func RunePos(b []byte, i int) int {
return CharacterCount(b[:i])
}

// IndexAnyUnquoted returns the first position is s of a character from chars.
// Escaped (with '\\') and quoted (with single or double quotes) characters
// are ignored. Returns -1 if not successful
func IndexAnyUnquoted(s, chars string) int {
var q byte = 0
for i := 0; i < len(s); i++ {
if (q == 0 || q == '"') && s[i] == '\\' {
i++
} else if s[i] == q {
q = 0
} else if q == 0 && (s[i] == '\'' || s[i] == '"') {
q = s[i]
} else if q == 0 && strings.IndexByte(chars, s[i]) >= 0 {
return i
}
}
return -1
}

// MakeRelative will attempt to make a relative path between path and base
func MakeRelative(path, base string) (string, error) {
if len(path) > 0 {
Expand Down
4 changes: 3 additions & 1 deletion runtime/help/keybindings.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ bindings, tab is bound as

This means that if the `Autocomplete` action is successful, the chain will
abort. Otherwise, it will try `IndentSelection`, and if that fails too, it
will execute `InsertTab`.
will execute `InsertTab`. To use `,`, `|` or `&` in an action (as an argument
to a command, for example), escape it with `\` or wrap it in single or double
quotes.

## Binding commands

Expand Down

0 comments on commit 98d7e28

Please sign in to comment.