mirror of
https://github.com/junegunn/fzf.git
synced 2025-08-15 20:23:50 -07:00
Extend support for alt key chords
"alt-" with any case-sensitive character is allowed
This commit is contained in:
167
src/terminal.go
167
src/terminal.go
@@ -109,8 +109,8 @@ type Terminal struct {
|
||||
sort bool
|
||||
toggleSort bool
|
||||
delimiter Delimiter
|
||||
expect map[int]string
|
||||
keymap map[int][]action
|
||||
expect map[tui.Event]string
|
||||
keymap map[tui.Event][]action
|
||||
pressed string
|
||||
printQuery bool
|
||||
history *History
|
||||
@@ -304,62 +304,68 @@ func toActions(types ...actionType) []action {
|
||||
return actions
|
||||
}
|
||||
|
||||
func defaultKeymap() map[int][]action {
|
||||
keymap := make(map[int][]action)
|
||||
keymap[tui.Invalid] = toActions(actInvalid)
|
||||
keymap[tui.Resize] = toActions(actClearScreen)
|
||||
keymap[tui.CtrlA] = toActions(actBeginningOfLine)
|
||||
keymap[tui.CtrlB] = toActions(actBackwardChar)
|
||||
keymap[tui.CtrlC] = toActions(actAbort)
|
||||
keymap[tui.CtrlG] = toActions(actAbort)
|
||||
keymap[tui.CtrlQ] = toActions(actAbort)
|
||||
keymap[tui.ESC] = toActions(actAbort)
|
||||
keymap[tui.CtrlD] = toActions(actDeleteCharEOF)
|
||||
keymap[tui.CtrlE] = toActions(actEndOfLine)
|
||||
keymap[tui.CtrlF] = toActions(actForwardChar)
|
||||
keymap[tui.CtrlH] = toActions(actBackwardDeleteChar)
|
||||
keymap[tui.BSpace] = toActions(actBackwardDeleteChar)
|
||||
keymap[tui.Tab] = toActions(actToggleDown)
|
||||
keymap[tui.BTab] = toActions(actToggleUp)
|
||||
keymap[tui.CtrlJ] = toActions(actDown)
|
||||
keymap[tui.CtrlK] = toActions(actUp)
|
||||
keymap[tui.CtrlL] = toActions(actClearScreen)
|
||||
keymap[tui.CtrlM] = toActions(actAccept)
|
||||
keymap[tui.CtrlN] = toActions(actDown)
|
||||
keymap[tui.CtrlP] = toActions(actUp)
|
||||
keymap[tui.CtrlU] = toActions(actUnixLineDiscard)
|
||||
keymap[tui.CtrlW] = toActions(actUnixWordRubout)
|
||||
keymap[tui.CtrlY] = toActions(actYank)
|
||||
if !util.IsWindows() {
|
||||
keymap[tui.CtrlZ] = toActions(actSigStop)
|
||||
func defaultKeymap() map[tui.Event][]action {
|
||||
keymap := make(map[tui.Event][]action)
|
||||
add := func(e tui.EventType, a actionType) {
|
||||
keymap[e.AsEvent()] = toActions(a)
|
||||
}
|
||||
addEvent := func(e tui.Event, a actionType) {
|
||||
keymap[e] = toActions(a)
|
||||
}
|
||||
|
||||
keymap[tui.AltB] = toActions(actBackwardWord)
|
||||
keymap[tui.SLeft] = toActions(actBackwardWord)
|
||||
keymap[tui.AltF] = toActions(actForwardWord)
|
||||
keymap[tui.SRight] = toActions(actForwardWord)
|
||||
keymap[tui.AltD] = toActions(actKillWord)
|
||||
keymap[tui.AltBS] = toActions(actBackwardKillWord)
|
||||
add(tui.Invalid, actInvalid)
|
||||
add(tui.Resize, actClearScreen)
|
||||
add(tui.CtrlA, actBeginningOfLine)
|
||||
add(tui.CtrlB, actBackwardChar)
|
||||
add(tui.CtrlC, actAbort)
|
||||
add(tui.CtrlG, actAbort)
|
||||
add(tui.CtrlQ, actAbort)
|
||||
add(tui.ESC, actAbort)
|
||||
add(tui.CtrlD, actDeleteCharEOF)
|
||||
add(tui.CtrlE, actEndOfLine)
|
||||
add(tui.CtrlF, actForwardChar)
|
||||
add(tui.CtrlH, actBackwardDeleteChar)
|
||||
add(tui.BSpace, actBackwardDeleteChar)
|
||||
add(tui.Tab, actToggleDown)
|
||||
add(tui.BTab, actToggleUp)
|
||||
add(tui.CtrlJ, actDown)
|
||||
add(tui.CtrlK, actUp)
|
||||
add(tui.CtrlL, actClearScreen)
|
||||
add(tui.CtrlM, actAccept)
|
||||
add(tui.CtrlN, actDown)
|
||||
add(tui.CtrlP, actUp)
|
||||
add(tui.CtrlU, actUnixLineDiscard)
|
||||
add(tui.CtrlW, actUnixWordRubout)
|
||||
add(tui.CtrlY, actYank)
|
||||
if !util.IsWindows() {
|
||||
add(tui.CtrlZ, actSigStop)
|
||||
}
|
||||
|
||||
keymap[tui.Up] = toActions(actUp)
|
||||
keymap[tui.Down] = toActions(actDown)
|
||||
keymap[tui.Left] = toActions(actBackwardChar)
|
||||
keymap[tui.Right] = toActions(actForwardChar)
|
||||
addEvent(tui.AltKey('b'), actBackwardWord)
|
||||
add(tui.SLeft, actBackwardWord)
|
||||
addEvent(tui.AltKey('f'), actForwardWord)
|
||||
add(tui.SRight, actForwardWord)
|
||||
addEvent(tui.AltKey('d'), actKillWord)
|
||||
add(tui.AltBS, actBackwardKillWord)
|
||||
|
||||
keymap[tui.Home] = toActions(actBeginningOfLine)
|
||||
keymap[tui.End] = toActions(actEndOfLine)
|
||||
keymap[tui.Del] = toActions(actDeleteChar)
|
||||
keymap[tui.PgUp] = toActions(actPageUp)
|
||||
keymap[tui.PgDn] = toActions(actPageDown)
|
||||
add(tui.Up, actUp)
|
||||
add(tui.Down, actDown)
|
||||
add(tui.Left, actBackwardChar)
|
||||
add(tui.Right, actForwardChar)
|
||||
|
||||
keymap[tui.SUp] = toActions(actPreviewUp)
|
||||
keymap[tui.SDown] = toActions(actPreviewDown)
|
||||
add(tui.Home, actBeginningOfLine)
|
||||
add(tui.End, actEndOfLine)
|
||||
add(tui.Del, actDeleteChar)
|
||||
add(tui.PgUp, actPageUp)
|
||||
add(tui.PgDn, actPageDown)
|
||||
|
||||
keymap[tui.Rune] = toActions(actRune)
|
||||
keymap[tui.Mouse] = toActions(actMouse)
|
||||
keymap[tui.DoubleClick] = toActions(actAccept)
|
||||
keymap[tui.LeftClick] = toActions(actIgnore)
|
||||
keymap[tui.RightClick] = toActions(actToggle)
|
||||
add(tui.SUp, actPreviewUp)
|
||||
add(tui.SDown, actPreviewDown)
|
||||
|
||||
add(tui.Mouse, actMouse)
|
||||
add(tui.DoubleClick, actAccept)
|
||||
add(tui.LeftClick, actIgnore)
|
||||
add(tui.RightClick, actToggle)
|
||||
return keymap
|
||||
}
|
||||
|
||||
@@ -1452,10 +1458,9 @@ func (t *Terminal) rubout(pattern string) {
|
||||
t.input = append(t.input[:t.cx], after...)
|
||||
}
|
||||
|
||||
func keyMatch(key int, event tui.Event) bool {
|
||||
return event.Type == key ||
|
||||
event.Type == tui.Rune && int(event.Char) == key-tui.AltZ ||
|
||||
event.Type == tui.Mouse && key == tui.DoubleClick && event.MouseEvent.Double
|
||||
func keyMatch(key tui.Event, event tui.Event) bool {
|
||||
return event.Type == key.Type && event.Char == key.Char ||
|
||||
key.Type == tui.DoubleClick && event.Type == tui.Mouse && event.MouseEvent.Double
|
||||
}
|
||||
|
||||
func quoteEntryCmd(entry string) string {
|
||||
@@ -2163,16 +2168,20 @@ func (t *Terminal) Loop() {
|
||||
}
|
||||
}
|
||||
|
||||
var doAction func(action, int) bool
|
||||
doActions := func(actions []action, mapkey int) bool {
|
||||
actionsFor := func(eventType tui.EventType) []action {
|
||||
return t.keymap[eventType.AsEvent()]
|
||||
}
|
||||
|
||||
var doAction func(action) bool
|
||||
doActions := func(actions []action) bool {
|
||||
for _, action := range actions {
|
||||
if !doAction(action, mapkey) {
|
||||
if !doAction(action) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
doAction = func(a action, mapkey int) bool {
|
||||
doAction = func(a action) bool {
|
||||
switch a.t {
|
||||
case actIgnore:
|
||||
case actExecute, actExecuteSilent:
|
||||
@@ -2326,14 +2335,14 @@ func (t *Terminal) Loop() {
|
||||
}
|
||||
case actToggleIn:
|
||||
if t.layout != layoutDefault {
|
||||
return doAction(action{t: actToggleUp}, mapkey)
|
||||
return doAction(action{t: actToggleUp})
|
||||
}
|
||||
return doAction(action{t: actToggleDown}, mapkey)
|
||||
return doAction(action{t: actToggleDown})
|
||||
case actToggleOut:
|
||||
if t.layout != layoutDefault {
|
||||
return doAction(action{t: actToggleDown}, mapkey)
|
||||
return doAction(action{t: actToggleDown})
|
||||
}
|
||||
return doAction(action{t: actToggleUp}, mapkey)
|
||||
return doAction(action{t: actToggleUp})
|
||||
case actToggleDown:
|
||||
if t.multi > 0 && t.merger.Length() > 0 && toggle() {
|
||||
t.vmove(-1, true)
|
||||
@@ -2490,7 +2499,7 @@ func (t *Terminal) Loop() {
|
||||
// Double-click
|
||||
if my >= min {
|
||||
if t.vset(t.offset+my-min) && t.cy < t.merger.Length() {
|
||||
return doActions(t.keymap[tui.DoubleClick], tui.DoubleClick)
|
||||
return doActions(actionsFor(tui.DoubleClick))
|
||||
}
|
||||
}
|
||||
} else if me.Down {
|
||||
@@ -2504,9 +2513,9 @@ func (t *Terminal) Loop() {
|
||||
}
|
||||
req(reqList)
|
||||
if me.Left {
|
||||
return doActions(t.keymap[tui.LeftClick], tui.LeftClick)
|
||||
return doActions(actionsFor(tui.LeftClick))
|
||||
}
|
||||
return doActions(t.keymap[tui.RightClick], tui.RightClick)
|
||||
return doActions(actionsFor(tui.RightClick))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2528,33 +2537,29 @@ func (t *Terminal) Loop() {
|
||||
}
|
||||
return true
|
||||
}
|
||||
mapkey := event.Type
|
||||
|
||||
if t.jumping == jumpDisabled {
|
||||
actions := t.keymap[mapkey]
|
||||
if mapkey == tui.Rune {
|
||||
mapkey = int(event.Char) + int(tui.AltZ)
|
||||
if act, prs := t.keymap[mapkey]; prs {
|
||||
actions = act
|
||||
}
|
||||
}
|
||||
if !doActions(actions, mapkey) {
|
||||
actions := t.keymap[event.Comparable()]
|
||||
if len(actions) == 0 && event.Type == tui.Rune {
|
||||
doAction(action{t: actRune})
|
||||
} else if !doActions(actions) {
|
||||
continue
|
||||
}
|
||||
t.truncateQuery()
|
||||
queryChanged = string(previousInput) != string(t.input)
|
||||
changed = changed || queryChanged
|
||||
if onChanges, prs := t.keymap[tui.Change]; queryChanged && prs {
|
||||
if !doActions(onChanges, tui.Change) {
|
||||
if onChanges, prs := t.keymap[tui.Change.AsEvent()]; queryChanged && prs {
|
||||
if !doActions(onChanges) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if onEOFs, prs := t.keymap[tui.BackwardEOF]; beof && prs {
|
||||
if !doActions(onEOFs, tui.BackwardEOF) {
|
||||
if onEOFs, prs := t.keymap[tui.BackwardEOF.AsEvent()]; beof && prs {
|
||||
if !doActions(onEOFs) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if mapkey == tui.Rune {
|
||||
if event.Type == tui.Rune {
|
||||
if idx := strings.IndexRune(t.jumpLabels, event.Char); idx >= 0 && idx < t.maxItems() && idx < t.merger.Length() {
|
||||
t.cy = idx + t.offset
|
||||
if t.jumping == jumpAcceptEnabled {
|
||||
|
Reference in New Issue
Block a user