Skip to content

Commit f5debdf

Browse files
authored
ignore quoted and escaped characters when splitting keybindings into actions (zyedidia#3612)
1 parent 9b3f7ff commit f5debdf

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

internal/action/bufpane.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,7 @@ func BufMapEvent(k Event, action string) {
100100
break
101101
}
102102

103-
// TODO: fix problem when complex bindings have these
104-
// characters (escape them?)
105-
idx := strings.IndexAny(action, "&|,")
103+
idx := util.IndexAnyUnquoted(action, "&|,")
106104
a := action
107105
if idx >= 0 {
108106
a = action[:idx]

internal/util/util.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,28 @@ func RunePos(b []byte, i int) int {
320320
return CharacterCount(b[:i])
321321
}
322322

323+
// IndexAnyUnquoted returns the first position in s of a character from chars.
324+
// Escaped (with backslash) and quoted (with single or double quotes) characters
325+
// are ignored. Returns -1 if not successful
326+
func IndexAnyUnquoted(s, chars string) int {
327+
var e bool
328+
var q rune
329+
for i, r := range s {
330+
if e {
331+
e = false
332+
} else if (q == 0 || q == '"') && r == '\\' {
333+
e = true
334+
} else if r == q {
335+
q = 0
336+
} else if q == 0 && (r == '\'' || r == '"') {
337+
q = r
338+
} else if q == 0 && strings.IndexRune(chars, r) >= 0 {
339+
return i
340+
}
341+
}
342+
return -1
343+
}
344+
323345
// MakeRelative will attempt to make a relative path between path and base
324346
func MakeRelative(path, base string) (string, error) {
325347
if len(path) > 0 {

runtime/help/keybindings.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ bindings, tab is bound as
6666

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

7173
## Binding commands
7274

0 commit comments

Comments
 (0)