Skip to content

Commit 38183b0

Browse files
Merge branch 'MultiCursorPaste' into dev
2 parents 4e188d1 + bb60611 commit 38183b0

File tree

3 files changed

+60
-28
lines changed

3 files changed

+60
-28
lines changed

internal/action/actions.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,23 +1579,31 @@ func (h *BufPane) MoveLinesDown() bool {
15791579
// Paste whatever is in the system clipboard into the buffer
15801580
// Delete and paste if the user has a selection
15811581
func (h *BufPane) Paste() bool {
1582-
clip, err := clipboard.ReadMulti(clipboard.ClipboardReg, h.Cursor.Num, h.Buf.NumCursors())
1583-
if err != nil {
1584-
InfoBar.Error(err)
1585-
} else {
1586-
h.paste(clip)
1587-
}
1588-
h.Relocate()
1589-
return true
1582+
return h.pasteReg(clipboard.ClipboardReg)
15901583
}
15911584

15921585
// PastePrimary pastes from the primary clipboard (only use on linux)
15931586
func (h *BufPane) PastePrimary() bool {
1594-
clip, err := clipboard.ReadMulti(clipboard.PrimaryReg, h.Cursor.Num, h.Buf.NumCursors())
1587+
return h.pasteReg(clipboard.PrimaryReg)
1588+
}
1589+
1590+
func (h *BufPane) pasteReg(reg clipboard.Register) bool {
1591+
clips, err := clipboard.ReadMulti(reg)
15951592
if err != nil {
15961593
InfoBar.Error(err)
15971594
} else {
1598-
h.paste(clip)
1595+
if h.Buf.NumCursors() != len(clips) {
1596+
// If cursor clipboard length doesn't match number of cursors, paste each cursor
1597+
// clipboard to a newline
1598+
for i, clip := range clips {
1599+
h.paste(clip)
1600+
if i != len(clips)-1 {
1601+
h.InsertNewline()
1602+
}
1603+
}
1604+
} else {
1605+
h.paste(clips[h.Cursor.Num])
1606+
}
15991607
}
16001608
h.Relocate()
16011609
return true

internal/clipboard/clipboard.go

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package clipboard
22

33
import (
44
"errors"
5-
65
"github.com/zyedidia/clipper"
76
)
87

@@ -78,16 +77,27 @@ func Write(text string, r Register) error {
7877
return write(text, r, CurrentMethod)
7978
}
8079

81-
// ReadMulti reads text from a clipboard register for a certain multi-cursor
82-
func ReadMulti(r Register, num, ncursors int) (string, error) {
80+
// ReadMulti reads text array from a clipboard register, which can be a multi cursor clipboard
81+
func ReadMulti(r Register) ([]string, error) {
8382
clip, err := Read(r)
84-
if err != nil {
85-
return "", err
83+
multivalid := false
84+
if err == nil {
85+
multivalid = ValidMulti(r, &clip)
86+
} else {
87+
multivalid = ValidMulti(r, nil)
8688
}
87-
if ValidMulti(r, clip, ncursors) {
88-
return multi.getText(r, num), nil
89+
90+
if !multivalid {
91+
returnarray := make([]string, 1, 1)
92+
if err == nil {
93+
returnarray[0] = clip
94+
} else {
95+
returnarray[0] = ""
96+
}
97+
return returnarray, err
8998
}
90-
return clip, nil
99+
100+
return multi.getAllText(r), nil
91101
}
92102

93103
// WriteMulti writes text to a clipboard register for a certain multi-cursor
@@ -97,13 +107,20 @@ func WriteMulti(text string, r Register, num int, ncursors int) error {
97107

98108
// ValidMulti checks if the internal multi-clipboard is valid and up-to-date
99109
// with the system clipboard
100-
func ValidMulti(r Register, clip string, ncursors int) bool {
101-
return multi.isValid(r, clip, ncursors)
110+
func ValidMulti(r Register, clip *string) bool {
111+
return multi.isValid(r, clip)
102112
}
103113

104114
func writeMulti(text string, r Register, num int, ncursors int, m Method) error {
115+
// Write to multi cursor clipboard
105116
multi.writeText(text, r, num, ncursors)
106-
return write(multi.getAllText(r), r, m)
117+
118+
// Write to normal cliipboard
119+
multitext := multi.getAllTextConcated(r)
120+
if multitext == "" {
121+
return write("", r, m)
122+
}
123+
return write(multitext, r, m)
107124
}
108125

109126
func read(r Register, m Method) (string, error) {

internal/clipboard/multi.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,26 @@ type multiClipboard map[Register][]string
99

1010
var multi multiClipboard
1111

12-
func (c multiClipboard) getAllText(r Register) string {
13-
content := c[r]
14-
if content == nil {
12+
func (c multiClipboard) getAllTextConcated(r Register) string {
13+
multitext := c.getAllText(r)
14+
if multitext == nil {
1515
return ""
1616
}
1717

1818
buf := &bytes.Buffer{}
19-
for _, s := range content {
19+
for i, s := range multitext {
2020
buf.WriteString(s)
21+
if i != len(multitext)-1 {
22+
buf.WriteString("\n")
23+
}
2124
}
2225
return buf.String()
2326
}
2427

28+
func (c multiClipboard) getAllText(r Register) []string {
29+
return c[r]
30+
}
31+
2532
func (c multiClipboard) getText(r Register, num int) string {
2633
content := c[r]
2734
if content == nil || len(content) <= num {
@@ -35,13 +42,13 @@ func (c multiClipboard) getText(r Register, num int) string {
3542
// text stored in the system clipboard (provided as an argument), and therefore
3643
// if it is safe to use the multi-clipboard for pasting instead of the system
3744
// clipboard.
38-
func (c multiClipboard) isValid(r Register, clipboard string, ncursors int) bool {
45+
func (c multiClipboard) isValid(r Register, clipboard *string) bool {
3946
content := c[r]
40-
if content == nil || len(content) != ncursors {
47+
if content == nil {
4148
return false
4249
}
4350

44-
return clipboard == c.getAllText(r)
51+
return c.getAllTextConcated(r) == *clipboard
4552
}
4653

4754
func (c multiClipboard) writeText(text string, r Register, num int, ncursors int) {

0 commit comments

Comments
 (0)