diff --git a/console_win.go b/console_win.go index 44d541f7..d7ae1d10 100644 --- a/console_win.go +++ b/console_win.go @@ -1032,3 +1032,4 @@ func (s *cScreen) HasKey(k Key) bool { } func (s *cScreen) RegisterRawSeq(string) {} +func (s *cScreen) SetPaste(bool) {} diff --git a/screen.go b/screen.go index 2f98f176..e8f47a36 100644 --- a/screen.go +++ b/screen.go @@ -198,6 +198,12 @@ type Screen interface { // be parsed by the screen // Not defined for non-posix systems RegisterRawSeq(string) + + // SetPaste sets whether or not this screen should be expecting paste + // events. When paste is true, all key events with multiple bytes + // will be treated as pastes rather than as the user typing really + // fast. This is to enable a feature similar to Vim's "paste" option. + SetPaste(bool) } // NewScreen returns a default Screen suitable for the user's terminal diff --git a/simulation.go b/simulation.go index 52f828a6..9896d83e 100644 --- a/simulation.go +++ b/simulation.go @@ -508,3 +508,4 @@ func (s *simscreen) HasKey(Key) bool { } func (s *simscreen) RegisterRawSeq(string) {} +func (s *simscreen) SetPaste(bool) {} diff --git a/tscreen.go b/tscreen.go index 7e51c303..66b6296d 100644 --- a/tscreen.go +++ b/tscreen.go @@ -35,8 +35,8 @@ import ( // Magic strings for bracketed paste // See http://cirw.in/blog/bracketed-paste const ( - enablePaste = "\x1b[?2004h" - disablePaste = "\x1b[?2004l" + pasteEnable = "\x1b[?2004h" + pasteDisable = "\x1b[?2004l" pasteBegin = "\x1b[200~" pasteEnd = "\x1b[201~" ) @@ -94,6 +94,7 @@ type tScreen struct { buffering bool // true if we are collecting writes to buf instead of sending directly to out buf bytes.Buffer escbuf *bytes.Buffer + paste bool curstyle Style style Style evch chan Event @@ -180,7 +181,7 @@ func (t *tScreen) Init() error { t.TPuts(ti.HideCursor) t.TPuts(ti.EnableAcs) t.TPuts(ti.Clear) - t.TPuts(enablePaste) + t.TPuts(pasteEnable) t.quit = make(chan struct{}) @@ -200,6 +201,10 @@ func (t *tScreen) Init() error { return nil } +func (t *tScreen) SetPaste(p bool) { + t.paste = p +} + func (t *tScreen) RegisterRawSeq(r string) { t.rawseq = append(t.rawseq, r) } @@ -420,7 +425,7 @@ func (t *tScreen) Fini() { t.TPuts(ti.ExitCA) t.TPuts(ti.ExitKeypad) t.TPuts(ti.TParm(ti.MouseMode, 0)) - t.TPuts(disablePaste) + t.TPuts(pasteDisable) t.curstyle = Style(-1) t.clear = false t.fini = true @@ -1246,12 +1251,13 @@ func (t *tScreen) parsePaste(buf *bytes.Buffer, evs *[]Event) bool { if esci != -1 { b = b[:esci] } - if len(b) > 8 { + if len(b) > 1 { for i := 0; i < len(b); i++ { by, _ := buf.ReadByte() t.escbuf.WriteByte(by) } - *evs = append(*evs, NewEventPaste(string(b), t.escbuf.String())) + str := string(bytes.Replace(b, []byte{'\r'}, []byte{'\n'}, -1)) + *evs = append(*evs, NewEventPaste(str, t.escbuf.String())) t.escbuf.Reset() return true } @@ -1314,7 +1320,7 @@ func (t *tScreen) collectEventsFromInput(buf *bytes.Buffer, expire bool) []Event partials := 0 - if t.parsePaste(buf, &res) { + if t.paste && t.parsePaste(buf, &res) { continue }