Extend support for alt key chords
Before adoption: this commit came in with the history the project adopted, so no landing records it.
"alt-" with any case-sensitive character is allowed
authored by Junegunn Choi · committed by Junegunn Choi · parent 0de7ab1 · Browse files · 8 files·+317 added−305 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+317 added−305 removed
- other:+317 added−305 removed
Provenance
- Commit
7f8e0db- Tree
deeac2d- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
0de7ab1
8 files changed+317 added−305 removed
modified:man/man1/fzf.1+1 added-5 removed
664 unmodified lines665 \fIctrl-/\fR (\fIctrl-_\fR)666 .br667 \fIctrl-alt-[a-z]\fR668 .br669-\fIalt-[a-z]\fR669+\fIalt-[*]\fR (Any case-sensitive single character is allowed)670 .br671-\fIalt-[0-9]\fR672-.br671 \fIf[1-12]\fR672 .br673 \fIenter\fR (\fIreturn\fR \fIctrl-m\fR)674 .br14 unmodified lines689 \fIalt-space\fR690 .br691 \fIalt-bspace\fR (\fIalt-bs\fR)692 .br695-\fIalt-/\fR696-.br693 \fItab\fR694 .br695 \fIbtab\fR (\fIshift-tab\fR)696 .br244 unmodified linesmodified:src/tui/light.go+17 added-26 removed
1 package tui2 3 import (4+ "bytes"5 "fmt"6 "os"7 "regexp"8 "strconv"221 unmodified lines230 errorExit("Failed to read " + consoleDevice)231 }232 233 retries := 0233- if c == ESC || nonblock {234+ if c == ESC.Int() || nonblock {235 retries = r.escDelay / escPollInterval236 }237 buffer = append(buffer, byte(c))238 6 unmodified lines245 time.Sleep(escPollInterval * time.Millisecond)246 continue247 }248 break248- } else if c == ESC && pc != c {249+ } else if c == ESC.Int() && pc != c {250 retries = r.escDelay / escPollInterval251 } else {252 retries = 0253 }24 unmodified lines278 r.buffer = r.buffer[sz:]279 }()280 281 switch r.buffer[0] {281- case CtrlC:282+ case CtrlC.Byte():283 return Event{CtrlC, 0, nil}283- case CtrlG:284+ case CtrlG.Byte():285 return Event{CtrlG, 0, nil}285- case CtrlQ:286+ case CtrlQ.Byte():287 return Event{CtrlQ, 0, nil}288 case 127:289 return Event{BSpace, 0, nil}290 case 0:5 unmodified lines296 case 30:297 return Event{CtrlCaret, 0, nil}298 case 31:299 return Event{CtrlSlash, 0, nil}299- case ESC:300+ case ESC.Byte():301 ev := r.escSequence(&sz)302 // Second chance303 if ev.Type == Invalid {304 r.buffer = r.getBytes()2 unmodified lines307 return ev308 }309 310 // CTRL-A ~ CTRL-Z310- if r.buffer[0] <= CtrlZ {311- return Event{int(r.buffer[0]), 0, nil}311+ if r.buffer[0] <= CtrlZ.Byte() {312+ return Event{EventType(r.buffer[0]), 0, nil}313 }314 char, rsz := utf8.DecodeRune(r.buffer)315 if char == utf8.RuneError {316 return Event{ESC, 0, nil}14 unmodified lines331 }332 333 *sz = 2334 if r.buffer[1] >= 1 && r.buffer[1] <= 'z'-'a'+1 {334- return Event{int(CtrlAltA + r.buffer[1] - 1), 0, nil}335+ return CtrlAltKey(rune(r.buffer[1] + 'a' - 1))336 }337 alt := false337- if len(r.buffer) > 2 && r.buffer[1] == ESC {338+ if len(r.buffer) > 2 && r.buffer[1] == ESC.Byte() {339 r.buffer = r.buffer[1:]340 alt = true341 }342 switch r.buffer[1] {342- case ESC:343+ case ESC.Byte():344 return Event{ESC, 0, nil}344- case ' ':345- return Event{AltSpace, 0, nil}346- case '/':347- return Event{AltSlash, 0, nil}348- case 'b':349- return Event{AltB, 0, nil}350- case 'd':351- return Event{AltD, 0, nil}352- case 'f':353- return Event{AltF, 0, nil}345 case 127:346 return Event{AltBS, 0, nil}347 case '[', 'O':348 if len(r.buffer) < 3 {159 unmodified lines508 } // r.buffer[3]509 } // r.buffer[2]510 } // r.buffer[2]511 } // r.buffer[1]521- if r.buffer[1] >= 'a' && r.buffer[1] <= 'z' {522- return Event{AltA + int(r.buffer[1]) - 'a', 0, nil}512+ rest := bytes.NewBuffer(r.buffer[1:])513+ c, size, err := rest.ReadRune()514+ if err == nil {515+ *sz = 1 + size516+ return AltKey(c)517 }524- if r.buffer[1] >= '0' && r.buffer[1] <= '9' {525- return Event{Alt0 + int(r.buffer[1]) - '0', 0, nil}526- }518 return Event{Invalid, 0, nil}519 }520 521 func (r *LightRenderer) mouseSequence(sz *int) Event {449 unmodified linesmodified:src/tui/tcell.go+30 added-41 removed
224 unmodified lines225 mods := ev.Modifiers()226 alt := (mods & tcell.ModAlt) > 0227 shift := (mods & tcell.ModShift) > 0228 altShift := alt && shift229- keyfn := func(r rune) int {229+ keyfn := func(r rune) Event {230 if alt {231- return CtrlAltA - 'a' + int(r)231+ return CtrlAltKey(r)232 }233- return CtrlA - 'a' + int(r)233+ return EventType(CtrlA.Int() - 'a' + int(r)).AsEvent()234 }235 switch ev.Key() {236 case tcell.KeyCtrlA:237- return Event{keyfn('a'), 0, nil}237+ return keyfn('a')238 case tcell.KeyCtrlB:239- return Event{keyfn('b'), 0, nil}239+ return keyfn('b')240 case tcell.KeyCtrlC:241- return Event{keyfn('c'), 0, nil}241+ return keyfn('c')242 case tcell.KeyCtrlD:243- return Event{keyfn('d'), 0, nil}243+ return keyfn('d')244 case tcell.KeyCtrlE:245- return Event{keyfn('e'), 0, nil}245+ return keyfn('e')246 case tcell.KeyCtrlF:247- return Event{keyfn('f'), 0, nil}247+ return keyfn('f')248 case tcell.KeyCtrlG:249- return Event{keyfn('g'), 0, nil}249+ return keyfn('g')250 case tcell.KeyCtrlH:251- return Event{keyfn('h'), 0, nil}251+ return keyfn('h')252 case tcell.KeyCtrlI:253- return Event{keyfn('i'), 0, nil}253+ return keyfn('i')254 case tcell.KeyCtrlJ:255- return Event{keyfn('j'), 0, nil}255+ return keyfn('j')256 case tcell.KeyCtrlK:257- return Event{keyfn('k'), 0, nil}257+ return keyfn('k')258 case tcell.KeyCtrlL:259- return Event{keyfn('l'), 0, nil}259+ return keyfn('l')260 case tcell.KeyCtrlM:261- return Event{keyfn('m'), 0, nil}261+ return keyfn('m')262 case tcell.KeyCtrlN:263- return Event{keyfn('n'), 0, nil}263+ return keyfn('n')264 case tcell.KeyCtrlO:265- return Event{keyfn('o'), 0, nil}265+ return keyfn('o')266 case tcell.KeyCtrlP:267- return Event{keyfn('p'), 0, nil}267+ return keyfn('p')268 case tcell.KeyCtrlQ:269- return Event{keyfn('q'), 0, nil}269+ return keyfn('q')270 case tcell.KeyCtrlR:271- return Event{keyfn('r'), 0, nil}271+ return keyfn('r')272 case tcell.KeyCtrlS:273- return Event{keyfn('s'), 0, nil}273+ return keyfn('s')274 case tcell.KeyCtrlT:275- return Event{keyfn('t'), 0, nil}275+ return keyfn('t')276 case tcell.KeyCtrlU:277- return Event{keyfn('u'), 0, nil}277+ return keyfn('u')278 case tcell.KeyCtrlV:279- return Event{keyfn('v'), 0, nil}279+ return keyfn('v')280 case tcell.KeyCtrlW:281- return Event{keyfn('w'), 0, nil}281+ return keyfn('w')282 case tcell.KeyCtrlX:283- return Event{keyfn('x'), 0, nil}283+ return keyfn('x')284 case tcell.KeyCtrlY:285- return Event{keyfn('y'), 0, nil}285+ return keyfn('y')286 case tcell.KeyCtrlZ:287- return Event{keyfn('z'), 0, nil}287+ return keyfn('z')288 case tcell.KeyCtrlSpace:289 return Event{CtrlSpace, 0, nil}290 case tcell.KeyCtrlBackslash:291 return Event{CtrlBackSlash, 0, nil}96 unmodified lines388 // ev.Ch doesn't work for some reason for space:389 case tcell.KeyRune:390 r := ev.Rune()391 if alt {392- switch r {393- case ' ':394- return Event{AltSpace, 0, nil}395- case '/':396- return Event{AltSlash, 0, nil}397- }398- if r >= 'a' && r <= 'z' {399- return Event{AltA + int(r) - 'a', 0, nil}400- }401- if r >= '0' && r <= '9' {402- return Event{Alt0 + int(r) - '0', 0, nil}403- }392+ return AltKey(r)393 }394 return Event{Rune, r, nil}395 396 case tcell.KeyEsc:258 unmodified linesmodified:src/tui/tui.go+34 added-16 removed
6 unmodified lines7 "time"8 )9 10 // Types of user action11+type EventType int12+13 const (12- Rune = iota14+ Rune EventType = iota15 16 CtrlA17 CtrlB18 CtrlC71 unmodified lines90 91 Change92 BackwardEOF93 92- AltSpace93- AltSlash94 AltBS95 96 AltUp97 AltDown4 unmodified lines102 AltSDown103 AltSLeft104 AltSRight105 106- Alt0106+ Alt107+ CtrlAlt108 )109 109-const ( // Reset iota110- AltA = Alt0 + 'a' - '0' + iota111- AltB112- AltC113- AltD114- AltE115- AltF116- AltZ = AltA + 'z' - 'a'117- CtrlAltA = AltZ + 1118- CtrlAltM = CtrlAltA + 'm' - 'a'119-)110+func (t EventType) AsEvent() Event {111+ return Event{t, 0, nil}112+}113 114+func (t EventType) Int() int {115+ return int(t)116+}117+118+func (t EventType) Byte() byte {119+ return byte(t)120+}121+122+func (e Event) Comparable() Event {123+ // Ignore MouseEvent pointer124+ return Event{e.Type, e.Char, nil}125+}126+127+func Key(r rune) Event {128+ return Event{Rune, r, nil}129+}130+131+func AltKey(r rune) Event {132+ return Event{Alt, r, nil}133+}134+135+func CtrlAltKey(r rune) Event {136+ return Event{CtrlAlt, r, nil}137+}138+139 const (140 doubleClickDuration = 500 * time.Millisecond141 )142 125 unmodified lines268 Border ColorAttr269 }270 271 type Event struct {254- Type int272+ Type EventType273 Char rune274 MouseEvent *MouseEvent275 }276 340 unmodified linesmodified:src/options_test.go+63 added-58 removed
123 unmodified lines124 }125 126 func TestParseKeys(t *testing.T) {127 pairs := parseKeyChords("ctrl-z,alt-z,f2,@,Alt-a,!,ctrl-G,J,g,ctrl-alt-a,ALT-enter,alt-SPACE", "")128- check := func(i int, s string) {129- if pairs[i] != s {130- t.Errorf("%s != %s", pairs[i], s)128+ checkEvent := func(e tui.Event, s string) {129+ if pairs[e] != s {130+ t.Errorf("%s != %s", pairs[e], s)131 }132 }133+ check := func(et tui.EventType, s string) {134+ checkEvent(et.AsEvent(), s)135+ }136 if len(pairs) != 12 {137 t.Error(12)138 }139 check(tui.CtrlZ, "ctrl-z")137- check(tui.AltZ, "alt-z")140 check(tui.F2, "f2")139- check(tui.AltZ+'@', "@")140- check(tui.AltA, "Alt-a")141- check(tui.AltZ+'!', "!")142- check(tui.CtrlA+'g'-'a', "ctrl-G")143- check(tui.AltZ+'J', "J")144- check(tui.AltZ+'g', "g")145- check(tui.CtrlAltA, "ctrl-alt-a")146- check(tui.CtrlAltM, "ALT-enter")147- check(tui.AltSpace, "alt-SPACE")141+ check(tui.CtrlG, "ctrl-G")142+ checkEvent(tui.AltKey('z'), "alt-z")143+ checkEvent(tui.Key('@'), "@")144+ checkEvent(tui.AltKey('a'), "Alt-a")145+ checkEvent(tui.Key('!'), "!")146+ checkEvent(tui.Key('J'), "J")147+ checkEvent(tui.Key('g'), "g")148+ checkEvent(tui.CtrlAltKey('a'), "ctrl-alt-a")149+ checkEvent(tui.CtrlAltKey('m'), "ALT-enter")150+ checkEvent(tui.AltKey(' '), "alt-SPACE")151 152 // Synonyms153 pairs = parseKeyChords("enter,Return,space,tab,btab,esc,up,down,left,right", "")154 if len(pairs) != 9 {155 t.Error(9)156 }157 check(tui.CtrlM, "Return")155- check(tui.AltZ+' ', "space")158+ checkEvent(tui.Key(' '), "space")159 check(tui.Tab, "tab")160 check(tui.BTab, "btab")161 check(tui.ESC, "esc")162 check(tui.Up, "up")23 unmodified lines186 if a != b {187 t.Errorf("%d != %d", a, b)188 }189 }187- check := func(pairs map[int]string, i int, s string) {188- if pairs[i] != s {189- t.Errorf("%s != %s", pairs[i], s)190+ check := func(pairs map[tui.Event]string, e tui.Event, s string) {191+ if pairs[e] != s {192+ t.Errorf("%s != %s", pairs[e], s)193 }194 }195 196 pairs := parseKeyChords(",", "")197 checkN(len(pairs), 1)195- check(pairs, tui.AltZ+',', ",")198+ check(pairs, tui.Key(','), ",")199 200 pairs = parseKeyChords(",,a,b", "")201 checkN(len(pairs), 3)199- check(pairs, tui.AltZ+'a', "a")200- check(pairs, tui.AltZ+'b', "b")201- check(pairs, tui.AltZ+',', ",")202+ check(pairs, tui.Key('a'), "a")203+ check(pairs, tui.Key('b'), "b")204+ check(pairs, tui.Key(','), ",")205 206 pairs = parseKeyChords("a,b,,", "")207 checkN(len(pairs), 3)205- check(pairs, tui.AltZ+'a', "a")206- check(pairs, tui.AltZ+'b', "b")207- check(pairs, tui.AltZ+',', ",")208+ check(pairs, tui.Key('a'), "a")209+ check(pairs, tui.Key('b'), "b")210+ check(pairs, tui.Key(','), ",")211 212 pairs = parseKeyChords("a,,,b", "")213 checkN(len(pairs), 3)211- check(pairs, tui.AltZ+'a', "a")212- check(pairs, tui.AltZ+'b', "b")213- check(pairs, tui.AltZ+',', ",")214+ check(pairs, tui.Key('a'), "a")215+ check(pairs, tui.Key('b'), "b")216+ check(pairs, tui.Key(','), ",")217 218 pairs = parseKeyChords("a,,,b,c", "")219 checkN(len(pairs), 4)217- check(pairs, tui.AltZ+'a', "a")218- check(pairs, tui.AltZ+'b', "b")219- check(pairs, tui.AltZ+'c', "c")220- check(pairs, tui.AltZ+',', ",")220+ check(pairs, tui.Key('a'), "a")221+ check(pairs, tui.Key('b'), "b")222+ check(pairs, tui.Key('c'), "c")223+ check(pairs, tui.Key(','), ",")224 225 pairs = parseKeyChords(",,,", "")226 checkN(len(pairs), 1)224- check(pairs, tui.AltZ+',', ",")227+ check(pairs, tui.Key(','), ",")228 }229 230 func TestBind(t *testing.T) {231 keymap := defaultKeymap()229- check := func(keyName int, arg1 string, types ...actionType) {230- if len(keymap[keyName]) != len(types) {231- t.Errorf("invalid number of actions (%d != %d)", len(types), len(keymap[keyName]))232+ check := func(event tui.Event, arg1 string, types ...actionType) {233+ if len(keymap[event]) != len(types) {234+ t.Errorf("invalid number of actions for %v (%d != %d)",235+ event, len(types), len(keymap[event]))236 return237 }234- for idx, action := range keymap[keyName] {238+ for idx, action := range keymap[event] {239 if types[idx] != action.t {240 t.Errorf("invalid action type (%d != %d)", types[idx], action.t)241 }242 }239- if len(arg1) > 0 && keymap[keyName][0].a != arg1 {240- t.Errorf("invalid action argument: (%s != %s)", arg1, keymap[keyName][0].a)243+ if len(arg1) > 0 && keymap[event][0].a != arg1 {244+ t.Errorf("invalid action argument: (%s != %s)", arg1, keymap[event][0].a)245 }246 }243- check(tui.CtrlA, "", actBeginningOfLine)247+ check(tui.CtrlA.AsEvent(), "", actBeginningOfLine)248 parseKeymap(keymap,249 "ctrl-a:kill-line,ctrl-b:toggle-sort+up+down,c:page-up,alt-z:page-down,"+250 "f1:execute(ls {+})+abort+execute(echo {+})+select-all,f2:execute/echo {}, {}, {}/,f3:execute[echo '({})'],f4:execute;less {};,"+251 "alt-a:execute-Multi@echo (,),[,],/,:,;,%,{}@,alt-b:execute;echo (,),[,],/,:,@,%,{};,"+252 "x:Execute(foo+bar),X:execute/bar+baz/"+253 ",f1:+first,f1:+top"+254 ",,:abort,::accept,+:execute:++\nfoobar,Y:execute(baz)+up")251- check(tui.CtrlA, "", actKillLine)252- check(tui.CtrlB, "", actToggleSort, actUp, actDown)253- check(tui.AltZ+'c', "", actPageUp)254- check(tui.AltZ+',', "", actAbort)255- check(tui.AltZ+':', "", actAccept)256- check(tui.AltZ, "", actPageDown)257- check(tui.F1, "ls {+}", actExecute, actAbort, actExecute, actSelectAll, actFirst, actFirst)258- check(tui.F2, "echo {}, {}, {}", actExecute)259- check(tui.F3, "echo '({})'", actExecute)260- check(tui.F4, "less {}", actExecute)261- check(tui.AltZ+'x', "foo+bar", actExecute)262- check(tui.AltZ+'X', "bar+baz", actExecute)263- check(tui.AltA, "echo (,),[,],/,:,;,%,{}", actExecuteMulti)264- check(tui.AltB, "echo (,),[,],/,:,@,%,{}", actExecute)265- check(tui.AltZ+'+', "++\nfoobar,Y:execute(baz)+up", actExecute)255+ check(tui.CtrlA.AsEvent(), "", actKillLine)256+ check(tui.CtrlB.AsEvent(), "", actToggleSort, actUp, actDown)257+ check(tui.Key('c'), "", actPageUp)258+ check(tui.Key(','), "", actAbort)259+ check(tui.Key(':'), "", actAccept)260+ check(tui.AltKey('z'), "", actPageDown)261+ check(tui.F1.AsEvent(), "ls {+}", actExecute, actAbort, actExecute, actSelectAll, actFirst, actFirst)262+ check(tui.F2.AsEvent(), "echo {}, {}, {}", actExecute)263+ check(tui.F3.AsEvent(), "echo '({})'", actExecute)264+ check(tui.F4.AsEvent(), "less {}", actExecute)265+ check(tui.Key('x'), "foo+bar", actExecute)266+ check(tui.Key('X'), "bar+baz", actExecute)267+ check(tui.AltKey('a'), "echo (,),[,],/,:,;,%,{}", actExecuteMulti)268+ check(tui.AltKey('b'), "echo (,),[,],/,:,@,%,{}", actExecute)269+ check(tui.Key('+'), "++\nfoobar,Y:execute(baz)+up", actExecute)270 271 for idx, char := range []rune{'~', '!', '@', '#', '$', '%', '^', '&', '*', '|', ';', '/'} {272 parseKeymap(keymap, fmt.Sprintf("%d:execute%cfoobar%c", idx%10, char, char))269- check(tui.AltZ+int([]rune(fmt.Sprintf("%d", idx%10))[0]), "foobar", actExecute)273+ check(tui.Key([]rune(fmt.Sprintf("%d", idx%10))[0]), "foobar", actExecute)274 }275 276 parseKeymap(keymap, "f1:abort")273- check(tui.F1, "", actAbort)277+ check(tui.F1.AsEvent(), "", actAbort)278 }279 280 func TestColorSpec(t *testing.T) {281 theme := tui.Dark25635 unmodified lines317 }318 }319 320 func TestDefaultCtrlNP(t *testing.T) {317- check := func(words []string, key int, expected actionType) {321+ check := func(words []string, et tui.EventType, expected actionType) {322+ e := et.AsEvent()323 opts := defaultOptions()324 parseOptions(opts, words)325 postProcessOptions(opts)321- if opts.Keymap[key][0].t != expected {326+ if opts.Keymap[e][0].t != expected {327 t.Error()328 }329 }330 check([]string{}, tui.CtrlN, actDown)123 unmodified linesmodified:src/options.go+83 added-79 removed
5 unmodified lines6 "regexp"7 "strconv"8 "strings"9 "unicode"10- "unicode/utf8"10 11 "github.com/junegunn/fzf/src/algo"12 "github.com/junegunn/fzf/src/tui"13 195 unmodified lines209 Select1 bool210 Exit0 bool211 Filter *string212 ToggleSort bool214- Expect map[int]string215- Keymap map[int][]action213+ Expect map[tui.Event]string214+ Keymap map[tui.Event][]action215 Preview previewOpts216 PrintQuery bool217 ReadZero bool218 Printer func(string)51 unmodified lines270 Select1: false,271 Exit0: false,272 Filter: nil,273 ToggleSort: false,275- Expect: make(map[int]string),276- Keymap: make(map[int][]action),274+ Expect: make(map[tui.Event]string),275+ Keymap: make(map[tui.Event][]action),276 Preview: defaultPreviewOpts(""),277 PrintQuery: false,278 ReadZero: false,279 Printer: func(str string) { fmt.Println(str) },163 unmodified lines443 }444 return tui.BorderNone445 }446 448-func parseKeyChords(str string, message string) map[int]string {447+func parseKeyChords(str string, message string) map[tui.Event]string {448 if len(str) == 0 {449 errorExit(message)450 }451 1 unmodified line453 if str == "," || strings.HasPrefix(str, ",,") || strings.HasSuffix(str, ",,") || strings.Contains(str, ",,,") {454 tokens = append(tokens, ",")455 }456 458- chords := make(map[int]string)457+ chords := make(map[tui.Event]string)458 for _, key := range tokens {459 if len(key) == 0 {460 continue // ignore461 }462 lkey := strings.ToLower(key)464- chord := 0463+ add := func(e tui.EventType) {464+ chords[e.AsEvent()] = key465+ }466 switch lkey {467 case "up":467- chord = tui.Up468+ add(tui.Up)469 case "down":469- chord = tui.Down470+ add(tui.Down)471 case "left":471- chord = tui.Left472+ add(tui.Left)473 case "right":473- chord = tui.Right474+ add(tui.Right)475 case "enter", "return":475- chord = tui.CtrlM476+ add(tui.CtrlM)477 case "space":477- chord = tui.AltZ + int(' ')478+ chords[tui.Key(' ')] = key479 case "bspace", "bs":479- chord = tui.BSpace480+ add(tui.BSpace)481 case "ctrl-space":481- chord = tui.CtrlSpace482+ add(tui.CtrlSpace)483 case "ctrl-^", "ctrl-6":483- chord = tui.CtrlCaret484+ add(tui.CtrlCaret)485 case "ctrl-/", "ctrl-_":485- chord = tui.CtrlSlash486+ add(tui.CtrlSlash)487 case "ctrl-\\":487- chord = tui.CtrlBackSlash488+ add(tui.CtrlBackSlash)489 case "ctrl-]":489- chord = tui.CtrlRightBracket490+ add(tui.CtrlRightBracket)491 case "change":491- chord = tui.Change492+ add(tui.Change)493 case "backward-eof":493- chord = tui.BackwardEOF494+ add(tui.BackwardEOF)495 case "alt-enter", "alt-return":495- chord = tui.CtrlAltM496+ chords[tui.CtrlAltKey('m')] = key497 case "alt-space":497- chord = tui.AltSpace498- case "alt-/":499- chord = tui.AltSlash498+ chords[tui.AltKey(' ')] = key499 case "alt-bs", "alt-bspace":501- chord = tui.AltBS500+ add(tui.AltBS)501 case "alt-up":503- chord = tui.AltUp502+ add(tui.AltUp)503 case "alt-down":505- chord = tui.AltDown504+ add(tui.AltDown)505 case "alt-left":507- chord = tui.AltLeft506+ add(tui.AltLeft)507 case "alt-right":509- chord = tui.AltRight508+ add(tui.AltRight)509 case "tab":511- chord = tui.Tab510+ add(tui.Tab)511 case "btab", "shift-tab":513- chord = tui.BTab512+ add(tui.BTab)513 case "esc":515- chord = tui.ESC514+ add(tui.ESC)515 case "del":517- chord = tui.Del516+ add(tui.Del)517 case "home":519- chord = tui.Home518+ add(tui.Home)519 case "end":521- chord = tui.End520+ add(tui.End)521 case "insert":523- chord = tui.Insert522+ add(tui.Insert)523 case "pgup", "page-up":525- chord = tui.PgUp524+ add(tui.PgUp)525 case "pgdn", "page-down":527- chord = tui.PgDn526+ add(tui.PgDn)527 case "alt-shift-up", "shift-alt-up":529- chord = tui.AltSUp528+ add(tui.AltSUp)529 case "alt-shift-down", "shift-alt-down":531- chord = tui.AltSDown530+ add(tui.AltSDown)531 case "alt-shift-left", "shift-alt-left":533- chord = tui.AltSLeft532+ add(tui.AltSLeft)533 case "alt-shift-right", "shift-alt-right":535- chord = tui.AltSRight534+ add(tui.AltSRight)535 case "shift-up":537- chord = tui.SUp536+ add(tui.SUp)537 case "shift-down":539- chord = tui.SDown538+ add(tui.SDown)539 case "shift-left":541- chord = tui.SLeft540+ add(tui.SLeft)541 case "shift-right":543- chord = tui.SRight542+ add(tui.SRight)543 case "left-click":545- chord = tui.LeftClick544+ add(tui.LeftClick)545 case "right-click":547- chord = tui.RightClick546+ add(tui.RightClick)547 case "double-click":549- chord = tui.DoubleClick548+ add(tui.DoubleClick)549 case "f10":551- chord = tui.F10550+ add(tui.F10)551 case "f11":553- chord = tui.F11552+ add(tui.F11)553 case "f12":555- chord = tui.F12554+ add(tui.F12)555 default:556+ runes := []rune(key)557 if len(key) == 10 && strings.HasPrefix(lkey, "ctrl-alt-") && isAlphabet(lkey[9]) {558- chord = tui.CtrlAltA + int(lkey[9]) - 'a'558+ chords[tui.CtrlAltKey(rune(key[9]))] = key559 } else if len(key) == 6 && strings.HasPrefix(lkey, "ctrl-") && isAlphabet(lkey[5]) {560- chord = tui.CtrlA + int(lkey[5]) - 'a'561- } else if len(key) == 5 && strings.HasPrefix(lkey, "alt-") && isAlphabet(lkey[4]) {562- chord = tui.AltA + int(lkey[4]) - 'a'563- } else if len(key) == 5 && strings.HasPrefix(lkey, "alt-") && isNumeric(lkey[4]) {564- chord = tui.Alt0 + int(lkey[4]) - '0'560+ add(tui.EventType(tui.CtrlA.Int() + int(lkey[5]) - 'a'))561+ } else if len(runes) == 5 && strings.HasPrefix(lkey, "alt-") {562+ r := runes[4]563+ switch r {564+ case escapedColon:565+ r = ':'566+ case escapedComma:567+ r = ','568+ case escapedPlus:569+ r = '+'570+ }571+ chords[tui.AltKey(r)] = key572 } else if len(key) == 2 && strings.HasPrefix(lkey, "f") && key[1] >= '1' && key[1] <= '9' {566- chord = tui.F1 + int(key[1]) - '1'567- } else if utf8.RuneCountInString(key) == 1 {568- chord = tui.AltZ + int([]rune(key)[0])573+ add(tui.EventType(tui.F1.Int() + int(key[1]) - '1'))574+ } else if len(runes) == 1 {575+ chords[tui.Key(runes[0])] = key576 } else {577 errorExit("unsupported key: " + key)578 }579 }573- if chord > 0 {574- chords[chord] = key575- }580 }581 return chords582 }583 139 unmodified lines723 }724 725 var executeRegexp *regexp.Regexp726 723-func firstKey(keymap map[int]string) int {727+func firstKey(keymap map[tui.Event]string) tui.Event {728 for k := range keymap {729 return k730 }727- return 0731+ return tui.EventType(0).AsEvent()732 }733 734 const (735 escapedColon = 07 unmodified lines743 executeRegexp = regexp.MustCompile(744 `(?si)[:+](execute(?:-multi|-silent)?|reload|preview|change-prompt):.+|[:+](execute(?:-multi|-silent)?|reload|preview|change-prompt)(\([^)]*\)|\[[^\]]*\]|~[^~]*~|![^!]*!|@[^@]*@|\#[^\#]*\#|\$[^\$]*\$|%[^%]*%|\^[^\^]*\^|&[^&]*&|\*[^\*]*\*|;[^;]*;|/[^/]*/|\|[^\|]*\|)`)745 }746 743-func parseKeymap(keymap map[int][]action, str string) {747+func parseKeymap(keymap map[tui.Event][]action, str string) {748 masked := executeRegexp.ReplaceAllStringFunc(str, func(src string) string {749 symbol := ":"750 if strings.HasPrefix(src, "+") {751 symbol = "+"27 unmodified lines779 pair := strings.SplitN(pairStr, ":", 2)780 if len(pair) < 2 {781 errorExit("bind action not specified: " + origPairStr)782 }779- var key int783+ var key tui.Event784 if len(pair[0]) == 1 && pair[0][0] == escapedColon {781- key = ':' + tui.AltZ785+ key = tui.Key(':')786 } else if len(pair[0]) == 1 && pair[0][0] == escapedComma {783- key = ',' + tui.AltZ787+ key = tui.Key(',')788 } else if len(pair[0]) == 1 && pair[0][0] == escapedPlus {785- key = '+' + tui.AltZ789+ key = tui.Key('+')790 } else {791 keys := parseKeyChords(pair[0], "key name required")792 key = firstKey(keys)793 }190 unmodified lines984 }985 return actIgnore986 }987 984-func parseToggleSort(keymap map[int][]action, str string) {988+func parseToggleSort(keymap map[tui.Event][]action, str string) {989 keys := parseKeyChords(str, "key name required")990 if len(keys) != 1 {991 errorExit("multiple keys specified")992 }198 unmodified lines1191 for k, v := range parseKeyChords(nextString(allArgs, &i, "key names required"), "key names required") {1192 opts.Expect[k] = v1193 }1194 case "--no-expect":1191- opts.Expect = make(map[int]string)1195+ opts.Expect = make(map[tui.Event]string)1196 case "--no-phony":1197 opts.Phony = false1198 case "--phony":1199 opts.Phony = true315 unmodified lines1515 errorExit("--height option is currently not supported on this platform")1516 }1517 // Default actions for CTRL-N / CTRL-P when --history is set1518 if opts.History != nil {1515- if _, prs := opts.Keymap[tui.CtrlP]; !prs {1516- opts.Keymap[tui.CtrlP] = toActions(actPreviousHistory)1519+ if _, prs := opts.Keymap[tui.CtrlP.AsEvent()]; !prs {1520+ opts.Keymap[tui.CtrlP.AsEvent()] = toActions(actPreviousHistory)1521 }1518- if _, prs := opts.Keymap[tui.CtrlN]; !prs {1519- opts.Keymap[tui.CtrlN] = toActions(actNextHistory)1522+ if _, prs := opts.Keymap[tui.CtrlN.AsEvent()]; !prs {1523+ opts.Keymap[tui.CtrlN.AsEvent()] = toActions(actNextHistory)1524 }1525 }1526 1527 // Extend the default key map58 unmodified linesmodified:src/terminal.go+85 added-80 removed
107 unmodified lines108 multi int109 sort bool110 toggleSort bool111 delimiter Delimiter112- expect map[int]string113- keymap map[int][]action112+ expect map[tui.Event]string113+ keymap map[tui.Event][]action114 pressed string115 printQuery bool116 history *History117 cycle bool185 unmodified lines303 }304 return actions305 }306 307-func defaultKeymap() map[int][]action {308- keymap := make(map[int][]action)309- keymap[tui.Invalid] = toActions(actInvalid)310- keymap[tui.Resize] = toActions(actClearScreen)311- keymap[tui.CtrlA] = toActions(actBeginningOfLine)312- keymap[tui.CtrlB] = toActions(actBackwardChar)313- keymap[tui.CtrlC] = toActions(actAbort)314- keymap[tui.CtrlG] = toActions(actAbort)315- keymap[tui.CtrlQ] = toActions(actAbort)316- keymap[tui.ESC] = toActions(actAbort)317- keymap[tui.CtrlD] = toActions(actDeleteCharEOF)318- keymap[tui.CtrlE] = toActions(actEndOfLine)319- keymap[tui.CtrlF] = toActions(actForwardChar)320- keymap[tui.CtrlH] = toActions(actBackwardDeleteChar)321- keymap[tui.BSpace] = toActions(actBackwardDeleteChar)322- keymap[tui.Tab] = toActions(actToggleDown)323- keymap[tui.BTab] = toActions(actToggleUp)324- keymap[tui.CtrlJ] = toActions(actDown)325- keymap[tui.CtrlK] = toActions(actUp)326- keymap[tui.CtrlL] = toActions(actClearScreen)327- keymap[tui.CtrlM] = toActions(actAccept)328- keymap[tui.CtrlN] = toActions(actDown)329- keymap[tui.CtrlP] = toActions(actUp)330- keymap[tui.CtrlU] = toActions(actUnixLineDiscard)331- keymap[tui.CtrlW] = toActions(actUnixWordRubout)332- keymap[tui.CtrlY] = toActions(actYank)307+func defaultKeymap() map[tui.Event][]action {308+ keymap := make(map[tui.Event][]action)309+ add := func(e tui.EventType, a actionType) {310+ keymap[e.AsEvent()] = toActions(a)311+ }312+ addEvent := func(e tui.Event, a actionType) {313+ keymap[e] = toActions(a)314+ }315+316+ add(tui.Invalid, actInvalid)317+ add(tui.Resize, actClearScreen)318+ add(tui.CtrlA, actBeginningOfLine)319+ add(tui.CtrlB, actBackwardChar)320+ add(tui.CtrlC, actAbort)321+ add(tui.CtrlG, actAbort)322+ add(tui.CtrlQ, actAbort)323+ add(tui.ESC, actAbort)324+ add(tui.CtrlD, actDeleteCharEOF)325+ add(tui.CtrlE, actEndOfLine)326+ add(tui.CtrlF, actForwardChar)327+ add(tui.CtrlH, actBackwardDeleteChar)328+ add(tui.BSpace, actBackwardDeleteChar)329+ add(tui.Tab, actToggleDown)330+ add(tui.BTab, actToggleUp)331+ add(tui.CtrlJ, actDown)332+ add(tui.CtrlK, actUp)333+ add(tui.CtrlL, actClearScreen)334+ add(tui.CtrlM, actAccept)335+ add(tui.CtrlN, actDown)336+ add(tui.CtrlP, actUp)337+ add(tui.CtrlU, actUnixLineDiscard)338+ add(tui.CtrlW, actUnixWordRubout)339+ add(tui.CtrlY, actYank)340 if !util.IsWindows() {334- keymap[tui.CtrlZ] = toActions(actSigStop)341+ add(tui.CtrlZ, actSigStop)342 }343 337- keymap[tui.AltB] = toActions(actBackwardWord)338- keymap[tui.SLeft] = toActions(actBackwardWord)339- keymap[tui.AltF] = toActions(actForwardWord)340- keymap[tui.SRight] = toActions(actForwardWord)341- keymap[tui.AltD] = toActions(actKillWord)342- keymap[tui.AltBS] = toActions(actBackwardKillWord)344+ addEvent(tui.AltKey('b'), actBackwardWord)345+ add(tui.SLeft, actBackwardWord)346+ addEvent(tui.AltKey('f'), actForwardWord)347+ add(tui.SRight, actForwardWord)348+ addEvent(tui.AltKey('d'), actKillWord)349+ add(tui.AltBS, actBackwardKillWord)350 344- keymap[tui.Up] = toActions(actUp)345- keymap[tui.Down] = toActions(actDown)346- keymap[tui.Left] = toActions(actBackwardChar)347- keymap[tui.Right] = toActions(actForwardChar)351+ add(tui.Up, actUp)352+ add(tui.Down, actDown)353+ add(tui.Left, actBackwardChar)354+ add(tui.Right, actForwardChar)355 349- keymap[tui.Home] = toActions(actBeginningOfLine)350- keymap[tui.End] = toActions(actEndOfLine)351- keymap[tui.Del] = toActions(actDeleteChar)352- keymap[tui.PgUp] = toActions(actPageUp)353- keymap[tui.PgDn] = toActions(actPageDown)356+ add(tui.Home, actBeginningOfLine)357+ add(tui.End, actEndOfLine)358+ add(tui.Del, actDeleteChar)359+ add(tui.PgUp, actPageUp)360+ add(tui.PgDn, actPageDown)361 355- keymap[tui.SUp] = toActions(actPreviewUp)356- keymap[tui.SDown] = toActions(actPreviewDown)362+ add(tui.SUp, actPreviewUp)363+ add(tui.SDown, actPreviewDown)364 358- keymap[tui.Rune] = toActions(actRune)359- keymap[tui.Mouse] = toActions(actMouse)360- keymap[tui.DoubleClick] = toActions(actAccept)361- keymap[tui.LeftClick] = toActions(actIgnore)362- keymap[tui.RightClick] = toActions(actToggle)365+ add(tui.Mouse, actMouse)366+ add(tui.DoubleClick, actAccept)367+ add(tui.LeftClick, actIgnore)368+ add(tui.RightClick, actToggle)369 return keymap370 }371 372 func trimQuery(query string) []rune {1084 unmodified lines1457 t.yanked = copySlice(t.input[t.cx:pcx])1458 t.input = append(t.input[:t.cx], after...)1459 }1460 1455-func keyMatch(key int, event tui.Event) bool {1456- return event.Type == key ||1457- event.Type == tui.Rune && int(event.Char) == key-tui.AltZ ||1458- event.Type == tui.Mouse && key == tui.DoubleClick && event.MouseEvent.Double1461+func keyMatch(key tui.Event, event tui.Event) bool {1462+ return event.Type == key.Type && event.Char == key.Char ||1463+ key.Type == tui.DoubleClick && event.Type == tui.Mouse && event.MouseEvent.Double1464 }1465 1466 func quoteEntryCmd(entry string) string {1467 escaped := strings.Replace(entry, `\`, `\\`, -1)699 unmodified lines2167 return2168 }2169 }2170 2171+ actionsFor := func(eventType tui.EventType) []action {2172+ return t.keymap[eventType.AsEvent()]2173+ }2174+2166- var doAction func(action, int) bool2167- doActions := func(actions []action, mapkey int) bool {2175+ var doAction func(action) bool2176+ doActions := func(actions []action) bool {2177 for _, action := range actions {2169- if !doAction(action, mapkey) {2178+ if !doAction(action) {2179 return false2180 }2181 }2182 return true2183 }2175- doAction = func(a action, mapkey int) bool {2184+ doAction = func(a action) bool {2185 switch a.t {2186 case actIgnore:2187 case actExecute, actExecuteSilent:2188 t.executeCommand(a.a, false, a.t == actExecuteSilent)145 unmodified lines2334 req(reqList, reqInfo)2335 }2336 case actToggleIn:2337 if t.layout != layoutDefault {2329- return doAction(action{t: actToggleUp}, mapkey)2338+ return doAction(action{t: actToggleUp})2339 }2331- return doAction(action{t: actToggleDown}, mapkey)2340+ return doAction(action{t: actToggleDown})2341 case actToggleOut:2342 if t.layout != layoutDefault {2334- return doAction(action{t: actToggleDown}, mapkey)2343+ return doAction(action{t: actToggleDown})2344 }2336- return doAction(action{t: actToggleUp}, mapkey)2345+ return doAction(action{t: actToggleUp})2346 case actToggleDown:2347 if t.multi > 0 && t.merger.Length() > 0 && toggle() {2348 t.vmove(-1, true)2349 req(reqList)148 unmodified lines2498 if me.Double {2499 // Double-click2500 if my >= min {2501 if t.vset(t.offset+my-min) && t.cy < t.merger.Length() {2493- return doActions(t.keymap[tui.DoubleClick], tui.DoubleClick)2502+ return doActions(actionsFor(tui.DoubleClick))2503 }2504 }2505 } else if me.Down {2506 if my == 0 && mx >= 0 {5 unmodified lines2512 toggle()2513 }2514 req(reqList)2515 if me.Left {2507- return doActions(t.keymap[tui.LeftClick], tui.LeftClick)2516+ return doActions(actionsFor(tui.LeftClick))2517 }2509- return doActions(t.keymap[tui.RightClick], tui.RightClick)2518+ return doActions(actionsFor(tui.RightClick))2519 }2520 }2521 }2522 case actReload:13 unmodified lines2536 }2537 }2538 return true2539 }2531- mapkey := event.Type2540+2541 if t.jumping == jumpDisabled {2533- actions := t.keymap[mapkey]2534- if mapkey == tui.Rune {2535- mapkey = int(event.Char) + int(tui.AltZ)2536- if act, prs := t.keymap[mapkey]; prs {2537- actions = act2538- }2539- }2540- if !doActions(actions, mapkey) {2542+ actions := t.keymap[event.Comparable()]2543+ if len(actions) == 0 && event.Type == tui.Rune {2544+ doAction(action{t: actRune})2545+ } else if !doActions(actions) {2546 continue2547 }2548 t.truncateQuery()2549 queryChanged = string(previousInput) != string(t.input)2550 changed = changed || queryChanged2546- if onChanges, prs := t.keymap[tui.Change]; queryChanged && prs {2547- if !doActions(onChanges, tui.Change) {2551+ if onChanges, prs := t.keymap[tui.Change.AsEvent()]; queryChanged && prs {2552+ if !doActions(onChanges) {2553 continue2554 }2555 }2551- if onEOFs, prs := t.keymap[tui.BackwardEOF]; beof && prs {2552- if !doActions(onEOFs, tui.BackwardEOF) {2556+ if onEOFs, prs := t.keymap[tui.BackwardEOF.AsEvent()]; beof && prs {2557+ if !doActions(onEOFs) {2558 continue2559 }2560 }2561 } else {2557- if mapkey == tui.Rune {2562+ if event.Type == tui.Rune {2563 if idx := strings.IndexRune(t.jumpLabels, event.Char); idx >= 0 && idx < t.maxItems() && idx < t.merger.Length() {2564 t.cy = idx + t.offset2565 if t.jumping == jumpAcceptEnabled {2566 req(reqClose)78 unmodified linesmodified:CHANGELOG.md+4 added
24 unmodified lines25 ```26 - Added `last` action to move the cursor to the last match27 - The opposite action `top` is renamed to `first`, but `top` is still28 recognized as a synonym for backward compatibility29+- Extended support for alt key chords: alt with any case-sensitive single character30+ ```sh31+ fzf --bind alt-,:first,alt-.:last32+ ```33 34 0.24.435 ------36 - Added `--preview-window` option `follow`1032 unmodified lines