Skip to content

Commit d6d0b26

Browse files
authored
Fix non-working raw escape bindings after restarting the screen (zyedidia#3468)
When we temporarily disable the screen (e.g. during RunInteractiveShell) and then enable it again, we reinitialize tcell.Screen from scratch, so we need to register all previously registered raw escape sequences once again. Otherwise raw escape bindings stop working, since the list of raw escape sequences of this newly create tcell.Screen is empty. Fixes zyedidia#3392
1 parent f22252e commit d6d0b26

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

internal/action/bindings.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func BindKey(k, v string, bind func(e Event, a string)) {
8989
}
9090

9191
if strings.HasPrefix(k, "\x1b") {
92-
screen.Screen.RegisterRawSeq(k)
92+
screen.RegisterRawSeq(k)
9393
}
9494

9595
bind(event, v)
@@ -342,7 +342,7 @@ func UnbindKey(k string) error {
342342
}
343343

344344
if strings.HasPrefix(k, "\x1b") {
345-
screen.Screen.UnregisterRawSeq(k)
345+
screen.UnregisterRawSeq(k)
346346
}
347347

348348
defaults := DefaultBindings("buffer")

internal/screen/screen.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ var lock sync.Mutex
3333
// written to even if no event user event has occurred
3434
var drawChan chan bool
3535

36+
// rawSeq is the list of raw escape sequences that are bound to some actions
37+
// via keybindings and thus should be parsed by tcell. We need to register
38+
// them in tcell every time we reinitialize the screen, so we need to remember
39+
// them in a list
40+
var rawSeq = make([]string, 0)
41+
3642
// Lock locks the screen lock
3743
func Lock() {
3844
lock.Lock()
@@ -121,6 +127,34 @@ func SetContent(x, y int, mainc rune, combc []rune, style tcell.Style) {
121127
}
122128
}
123129

130+
// RegisterRawSeq registers a raw escape sequence that should be parsed by tcell
131+
func RegisterRawSeq(r string) {
132+
for _, seq := range rawSeq {
133+
if seq == r {
134+
return
135+
}
136+
}
137+
rawSeq = append(rawSeq, r)
138+
139+
if Screen != nil {
140+
Screen.RegisterRawSeq(r)
141+
}
142+
}
143+
144+
// UnregisterRawSeq unregisters a raw escape sequence that should be parsed by tcell
145+
func UnregisterRawSeq(r string) {
146+
for i, seq := range rawSeq {
147+
if seq == r {
148+
rawSeq[i] = rawSeq[len(rawSeq)-1]
149+
rawSeq = rawSeq[:len(rawSeq)-1]
150+
}
151+
}
152+
153+
if Screen != nil {
154+
Screen.UnregisterRawSeq(r)
155+
}
156+
}
157+
124158
// TempFini shuts the screen down temporarily
125159
func TempFini() bool {
126160
screenWasNil := Screen == nil
@@ -195,6 +229,10 @@ func Init() error {
195229
Screen.EnableMouse()
196230
}
197231

232+
for _, r := range rawSeq {
233+
Screen.RegisterRawSeq(r)
234+
}
235+
198236
return nil
199237
}
200238

0 commit comments

Comments
 (0)