Skip to content

Commit bee9f1e

Browse files
committed
bracketed paste
1 parent 22ac982 commit bee9f1e

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

emacs.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package readline
33
import (
44
"fmt"
55
"io"
6+
"slices"
67
"sort"
78
"strings"
89
"unicode"
@@ -12,6 +13,7 @@ import (
1213
"github.com/reeflective/readline/inputrc"
1314
"github.com/reeflective/readline/internal/color"
1415
"github.com/reeflective/readline/internal/completion"
16+
"github.com/reeflective/readline/internal/core"
1517
"github.com/reeflective/readline/internal/keymap"
1618
"github.com/reeflective/readline/internal/strutil"
1719
"github.com/reeflective/readline/internal/term"
@@ -448,8 +450,27 @@ func (rl *Shell) selfInsert() {
448450
}
449451

450452
func (rl *Shell) bracketedPasteBegin() {
451-
// keys, _ := rl.Keys.PeekAllBytes()
452-
// fmt.Println(string(keys))
453+
// Length of bracketed paste escape code; this is the minimum length
454+
// we will see here.
455+
sequence := make([]byte, 0, 6)
456+
457+
for {
458+
key, empty := core.PopKey(rl.Keys)
459+
if empty {
460+
core.WaitAvailableKeys(rl.Keys, rl.Config)
461+
continue
462+
}
463+
464+
sequence = append(sequence, key)
465+
466+
if len(sequence) >= 6 && slices.Equal(sequence[len(sequence)-6:], []byte{'\x1b', '[', '2', '0', '1', '~'}) {
467+
break
468+
}
469+
}
470+
471+
if len(sequence) > 6 {
472+
rl.cursor.InsertAt([]rune(string(sequence[:len(sequence)-6]))...)
473+
}
453474
}
454475

455476
// Drag the character before point forward over the character

readline.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ func (rl *Shell) Readline() (string, error) {
5757
}
5858
defer term.Restore(descriptor, state)
5959

60+
if rl.Config.GetBool("enable-bracketed-paste") {
61+
fmt.Print("\x1b[?2004h")
62+
defer fmt.Print("\x1b[?2004l")
63+
}
64+
6065
// Prompts and cursor styles
6166
rl.Display.PrintPrimaryPrompt()
6267
defer rl.Display.RefreshTransient()

0 commit comments

Comments
 (0)