2020-07-05 03:00:39 +03:00
|
|
|
package clipboard
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
)
|
|
|
|
|
|
|
|
// For storing multi cursor clipboard contents
|
|
|
|
type multiClipboard map[Register][]string
|
|
|
|
|
|
|
|
var multi multiClipboard
|
|
|
|
|
|
|
|
func (c multiClipboard) getAllText(r Register) string {
|
|
|
|
content := c[r]
|
|
|
|
if content == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
for _, s := range content {
|
|
|
|
buf.WriteString(s)
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c multiClipboard) getText(r Register, num int) string {
|
|
|
|
content := c[r]
|
|
|
|
if content == nil || len(content) <= num {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return content[num]
|
|
|
|
}
|
|
|
|
|
|
|
|
// isValid checks if the text stored in this multi-clipboard is the same as the
|
|
|
|
// text stored in the system clipboard (provided as an argument), and therefore
|
|
|
|
// if it is safe to use the multi-clipboard for pasting instead of the system
|
|
|
|
// clipboard.
|
2020-07-05 08:12:35 +03:00
|
|
|
func (c multiClipboard) isValid(r Register, clipboard string, ncursors int) bool {
|
2020-07-05 03:00:39 +03:00
|
|
|
content := c[r]
|
2020-07-05 08:12:35 +03:00
|
|
|
if content == nil || len(content) != ncursors {
|
2020-07-05 03:00:39 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-05 08:12:35 +03:00
|
|
|
return clipboard == c.getAllText(r)
|
2020-07-05 03:00:39 +03:00
|
|
|
}
|
|
|
|
|
2020-07-05 08:12:35 +03:00
|
|
|
func (c multiClipboard) writeText(text string, r Register, num int, ncursors int) {
|
2020-07-05 03:00:39 +03:00
|
|
|
content := c[r]
|
2020-07-05 08:12:35 +03:00
|
|
|
if content == nil || len(content) != ncursors {
|
|
|
|
content = make([]string, ncursors, ncursors)
|
2020-07-05 04:26:36 +03:00
|
|
|
c[r] = content
|
|
|
|
}
|
|
|
|
|
2020-07-05 08:12:35 +03:00
|
|
|
if num >= ncursors {
|
|
|
|
return
|
2020-07-05 03:00:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
content[num] = text
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
multi = make(multiClipboard)
|
|
|
|
}
|