]> git.lizzy.rs Git - micro.git/blob - internal/clipboard/multi.go
Add support for copy-paste via OSC 52
[micro.git] / internal / clipboard / multi.go
1 package clipboard
2
3 import (
4         "bytes"
5         "hash/fnv"
6 )
7
8 // For storing multi cursor clipboard contents
9 type multiClipboard map[Register][]string
10
11 var multi multiClipboard
12
13 func (c multiClipboard) getAllText(r Register) string {
14         content := c[r]
15         if content == nil {
16                 return ""
17         }
18
19         buf := &bytes.Buffer{}
20         for _, s := range content {
21                 buf.WriteString(s)
22                 buf.WriteByte('\n')
23         }
24         return buf.String()
25 }
26
27 func (c multiClipboard) getText(r Register, num int) string {
28         content := c[r]
29         if content == nil || len(content) <= num {
30                 return ""
31         }
32
33         return content[num]
34 }
35
36 func hash(s string) uint32 {
37         h := fnv.New32a()
38         h.Write([]byte(s))
39         return h.Sum32()
40 }
41
42 // isValid checks if the text stored in this multi-clipboard is the same as the
43 // text stored in the system clipboard (provided as an argument), and therefore
44 // if it is safe to use the multi-clipboard for pasting instead of the system
45 // clipboard.
46 func (c multiClipboard) isValid(r Register, ncursors int, clipboard string) bool {
47         content := c[r]
48         if content == nil || len(content) != ncursors {
49                 return false
50         }
51
52         return hash(clipboard) == hash(c.getAllText(r))
53 }
54
55 func (c multiClipboard) writeText(text string, r Register, num int) {
56         content := c[r]
57         if content == nil || num >= cap(content) {
58                 content = make([]string, num+1, num+1)
59         }
60
61         content[num] = text
62 }
63
64 func init() {
65         multi = make(multiClipboard)
66 }