Skip to content

Commit

Permalink
Support for paste option
Browse files Browse the repository at this point in the history
  • Loading branch information
zyedidia committed Jan 2, 2020
1 parent 36d68eb commit e02c8a0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
1 change: 1 addition & 0 deletions console_win.go
Original file line number Diff line number Diff line change
Expand Up @@ -1032,3 +1032,4 @@ func (s *cScreen) HasKey(k Key) bool {
}

func (s *cScreen) RegisterRawSeq(string) {}
func (s *cScreen) SetPaste(bool) {}
6 changes: 6 additions & 0 deletions screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,3 +508,4 @@ func (s *simscreen) HasKey(Key) bool {
}

func (s *simscreen) RegisterRawSeq(string) {}
func (s *simscreen) SetPaste(bool) {}
20 changes: 13 additions & 7 deletions tscreen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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~"
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{})

Expand All @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit e02c8a0

Please sign in to comment.