Skip to content

Commit f772f08

Browse files
Pasting multi cursor clipboard with newlines for non matching cursors
1 parent 7492195 commit f772f08

File tree

3 files changed

+76
-33
lines changed

3 files changed

+76
-33
lines changed

internal/action/actions.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,23 +1583,31 @@ func (h *BufPane) MoveLinesDown() bool {
15831583
// Paste whatever is in the system clipboard into the buffer
15841584
// Delete and paste if the user has a selection
15851585
func (h *BufPane) Paste() bool {
1586-
clip, err := clipboard.ReadMulti(clipboard.ClipboardReg, h.Cursor.Num, h.Buf.NumCursors())
1587-
if err != nil {
1588-
InfoBar.Error(err)
1589-
} else {
1590-
h.paste(clip)
1591-
}
1592-
h.Relocate()
1593-
return true
1586+
return h.pasteReg(clipboard.ClipboardReg)
15941587
}
15951588

15961589
// PastePrimary pastes from the primary clipboard (only use on linux)
15971590
func (h *BufPane) PastePrimary() bool {
1598-
clip, err := clipboard.ReadMulti(clipboard.PrimaryReg, h.Cursor.Num, h.Buf.NumCursors())
1591+
return h.pasteReg(clipboard.PrimaryReg)
1592+
}
1593+
1594+
func (h *BufPane) pasteReg(reg clipboard.Register) bool {
1595+
clips, err := clipboard.ReadMulti(reg)
15991596
if err != nil {
16001597
InfoBar.Error(err)
16011598
} else {
1602-
h.paste(clip)
1599+
if h.Buf.NumCursors() != len(clips) {
1600+
// If cursor clipboard length doesn't match number of cursors, paste each cursor
1601+
// clipboard to a newline
1602+
for i, clip := range clips {
1603+
h.paste(clip)
1604+
if i != len(clips)-1 {
1605+
h.InsertNewline()
1606+
}
1607+
}
1608+
} else {
1609+
h.paste(clips[h.Cursor.Num])
1610+
}
16031611
}
16041612
h.Relocate()
16051613
return true

internal/clipboard/clipboard.go

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

33
import (
4+
"bytes"
45
"errors"
56

67
"github.com/zyedidia/clipper"
@@ -78,16 +79,27 @@ func Write(text string, r Register) error {
7879
return write(text, r, CurrentMethod)
7980
}
8081

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

93105
// WriteMulti writes text to a clipboard register for a certain multi-cursor
@@ -97,13 +109,27 @@ func WriteMulti(text string, r Register, num int, ncursors int) error {
97109

98110
// ValidMulti checks if the internal multi-clipboard is valid and up-to-date
99111
// with the system clipboard
100-
func ValidMulti(r Register, clip string, ncursors int) bool {
101-
return multi.isValid(r, clip, ncursors)
112+
func ValidMulti(r Register, clip *string) bool {
113+
return multi.isValid(r, clip)
102114
}
103115

104116
func writeMulti(text string, r Register, num int, ncursors int, m Method) error {
117+
// Write to multi cursor clipboard
105118
multi.writeText(text, r, num, ncursors)
106-
return write(multi.getAllText(r), r, m)
119+
multitext := multi.getAllText(r)
120+
if multitext == nil {
121+
return write("", r, m)
122+
}
123+
124+
// Write to normal cliipboard
125+
buf := &bytes.Buffer{}
126+
for i, s := range multitext {
127+
buf.WriteString(s)
128+
if i != len(multitext)-1 {
129+
buf.WriteString("\n")
130+
}
131+
}
132+
return write(buf.String(), r, m)
107133
}
108134

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

internal/clipboard/multi.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,9 @@ 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 {
15-
return ""
16-
}
12+
func (c multiClipboard) getAllText(r Register) []string {
13+
return c[r]
1714

18-
buf := &bytes.Buffer{}
19-
for _, s := range content {
20-
buf.WriteString(s)
21-
}
22-
return buf.String()
2315
}
2416

2517
func (c multiClipboard) getText(r Register, num int) string {
@@ -35,13 +27,30 @@ func (c multiClipboard) getText(r Register, num int) string {
3527
// text stored in the system clipboard (provided as an argument), and therefore
3628
// if it is safe to use the multi-clipboard for pasting instead of the system
3729
// clipboard.
38-
func (c multiClipboard) isValid(r Register, clipboard string, ncursors int) bool {
30+
func (c multiClipboard) isValid(r Register, clipboard *string) bool {
3931
content := c[r]
40-
if content == nil || len(content) != ncursors {
32+
if content == nil {
4133
return false
4234
}
4335

44-
return clipboard == c.getAllText(r)
36+
multiclips := c.getAllText(r)
37+
if multiclips == nil || len(multiclips) == 0 {
38+
return false
39+
}
40+
41+
if clipboard == nil {
42+
return true
43+
}
44+
45+
buf := &bytes.Buffer{}
46+
for i, s := range multiclips {
47+
buf.WriteString(s)
48+
if i != len(multiclips)-1 {
49+
buf.WriteString("\n")
50+
}
51+
}
52+
53+
return buf.String() == *clipboard
4554
}
4655

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

0 commit comments

Comments
 (0)