Support modifiers in special key mappings (#1248)

* Support modifiers in mappings

* Update docs

* Support cmap keybindings as well

* Implement push

* Move order of escape key check

* Add safety check

* Fix safety check

* Factor out common logic
This commit is contained in:
Joe Lim authored and GitHub committed 2023-05-20 17:37:39 +03:00
1 parent 88b3c999af
commit 1be7d2689a
5 files changed
+75 -29

No files matched your search

+7 -2
View File
@@ -1142,8 +1142,13 @@ On these terminals, keys combined with the alt key are prefixed with 'a' charact
map <a-a> down
Please note that, some key combinations are not possible due to the way terminals work (e.g. control and h combination sends a backspace key instead).
The easiest way to find the name of a key combination is to press the key while lf is running and read the name of the key from the unknown mapping error.
It is possible to combine special keys with modifiers:
map <a-enter> down
WARNING: Some key combinations will likely be intercepted by your OS, window manager, or terminal.
Other key combinations cannot be recognized by lf due to the way terminals work (e.g. `Ctrl+h` combination sends a backspace key instead).
The easiest way to find out the name of a key combination and whether it will work on your system is to press the key while lf is running and read the name from the "unknown mapping" error.
Mouse buttons are prefixed with 'm' character:
+10 -4
View File
@@ -1224,10 +1224,16 @@ the alt key are prefixed with 'a' character:
map <a-a> down
Please note that, some key combinations are not possible due to the way
terminals work (e.g. control and h combination sends a backspace key instead).
The easiest way to find the name of a key combination is to press the key while
lf is running and read the name of the key from the unknown mapping error.
It is possible to combine special keys with modifiers:
map <a-enter> down
WARNING: Some key combinations will likely be intercepted by your OS,
window manager, or terminal. Other key combinations cannot be recognized by lf
due to the way terminals work (e.g. 'Ctrl+h' combination sends a backspace key
instead). The easiest way to find out the name of a key combination and whether
it will work on your system is to press the key while lf is running and read the
name from the "unknown mapping" error.
Mouse buttons are prefixed with 'm' character:
+7 -1
View File
@@ -1388,7 +1388,13 @@ Newer terminals (e.g. gnome-terminal) may prefix the key with an escape key when
map <a-a> down
.EE
.PP
Please note that, some key combinations are not possible due to the way terminals work (e.g. control and h combination sends a backspace key instead). The easiest way to find the name of a key combination is to press the key while lf is running and read the name of the key from the unknown mapping error.
It is possible to combine special keys with modifiers:
.PP
.EX
map <a-enter> down
.EE
.PP
WARNING: Some key combinations will likely be intercepted by your OS, window manager, or terminal. Other key combinations cannot be recognized by lf due to the way terminals work (e.g. `Ctrl+h` combination sends a backspace key instead). The easiest way to find out the name of a key combination and whether it will work on your system is to press the key while lf is running and read the name from the "unknown mapping" error.
.PP
Mouse buttons are prefixed with 'm' character:
.PP
+1 -1
View File
@@ -275,7 +275,7 @@ func naturalLess(s1, s2 string) bool {
}
}
var reAltKey = regexp.MustCompile(`<a-(.)>`)
var reModKey = regexp.MustCompile(`<(c|s|a)-(.+)>`)
var reWord = regexp.MustCompile(`(\pL|\pN)+`)
var reWordBeg = regexp.MustCompile(`([^\pL\pN]|^)(\pL|\pN)`)
+50 -21
View File
@@ -1105,31 +1105,43 @@ func (ui *ui) pollEvent() tcell.Event {
case val := <-ui.keyChan:
var ch rune
var mod tcell.ModMask
k := tcell.KeyRune
if utf8.RuneCountInString(val) == 1 {
if key, ok := gValKey[val]; ok {
return tcell.NewEventKey(key, ch, mod)
}
switch {
case utf8.RuneCountInString(val) == 1:
ch, _ = utf8.DecodeRuneInString(val)
} else {
switch {
case val == "<lt>":
ch = '<'
case val == "<gt>":
ch = '>'
case val == "<space>":
ch = ' '
case reAltKey.MatchString(val):
match := reAltKey.FindStringSubmatch(val)[1]
ch, _ = utf8.DecodeRuneInString(match)
mod = tcell.ModMask(tcell.ModAlt)
default:
if key, ok := gValKey[val]; ok {
k = key
} else {
k = tcell.KeyESC
ui.echoerrf("unknown key: %s", val)
}
case val == "<lt>":
ch = '<'
case val == "<gt>":
ch = '>'
case val == "<space>":
ch = ' '
case reModKey.MatchString(val):
matches := reModKey.FindStringSubmatch(val)
switch matches[1] {
case "c":
mod = tcell.ModCtrl
case "s":
mod = tcell.ModShift
case "a":
mod = tcell.ModAlt
}
val = matches[2]
if utf8.RuneCountInString(val) == 1 {
ch, _ = utf8.DecodeRuneInString(val)
break
} else if key, ok := gValKey["<"+val+">"]; ok {
k = key
break
}
fallthrough
default:
k = tcell.KeyESC
ui.echoerrf("unknown key: %s", val)
}
return tcell.NewEventKey(k, ch, mod)
@@ -1138,6 +1150,21 @@ func (ui *ui) pollEvent() tcell.Event {
}
}
func addSpecialKeyModifier(val string, mod tcell.ModMask) string {
switch {
case !strings.HasPrefix(val, "<"):
return val
case mod == tcell.ModCtrl && !strings.HasPrefix(val, "<c-"):
return "<c-" + val[1:]
case mod == tcell.ModShift:
return "<s-" + val[1:]
case mod == tcell.ModAlt:
return "<a-" + val[1:]
default:
return val
}
}
// This function is used to read a normal event on the client side. For keys,
// digits are interpreted as command counts but this is only done for digits
// preceding any non-digit characters (e.g. "42y2k" as 42 times "y2k").
@@ -1165,6 +1192,7 @@ func (ui *ui) readNormalEvent(ev tcell.Event, nav *nav) expr {
}
} else {
val := gKeyVal[tev.Key()]
val = addSpecialKeyModifier(val, tev.Modifiers())
if val == "<esc>" && string(ui.keyAcc) != "" {
ui.keyAcc = nil
ui.keyCount = nil
@@ -1332,6 +1360,7 @@ func readCmdEvent(ev tcell.Event) expr {
}
} else {
val := gKeyVal[tev.Key()]
val = addSpecialKeyModifier(val, tev.Modifiers())
if expr, ok := gOpts.cmdkeys[val]; ok {
return expr
}