Debian Codegraph @

internal/clipboard/clipboard.go

raw
1 package clipboard
2
3 import (
4 "errors"
5
6 "github.com/zyedidia/clipper"
7 )
8
9 type Method int
10
11 const (
12 // External relies on external tools for accessing the clipboard
13 // These include xclip, xsel, wl-clipboard for linux, pbcopy/pbpaste on Mac,
14 // and Syscalls on Windows.
15 External Method = iota
16 // Terminal uses the terminal to manage the clipboard via OSC 52. Many
17 // terminals do not support OSC 52, in which case this method won't work.
18 Terminal
19 // Internal just manages the clipboard with an internal buffer and doesn't
20 // attempt to interface with the system clipboard
21 Internal
22 )
23
24 // CurrentMethod is the method used to store clipboard information
25 var CurrentMethod Method = Internal
26
27 // A Register is a buffer used to store text. The system clipboard has the 'clipboard'
28 // and 'primary' (linux-only) registers, but other registers may be used internal to micro.
29 type Register int
30
31 const (
32 // ClipboardReg is the main system clipboard
33 ClipboardReg Register = -1
34 // PrimaryReg is the system primary clipboard (linux only)
35 PrimaryReg = -2
36 )
37
38 var clipboard clipper.Clipboard
39
40 // Initialize attempts to initialize the clipboard using the given method
41 func Initialize(m Method) error {
42 var err error
43 switch m {
44 case External:
45 clips := make([]clipper.Clipboard, 0, len(clipper.Clipboards)+1)
46 clips = append(clips, &clipper.Custom{
47 Name: "micro-clip",
48 })
49 clips = append(clips, clipper.Clipboards...)
50 clipboard, err = clipper.GetClipboard(clips...)
51 }
52 if err != nil {
53 CurrentMethod = Internal
54 }
55 return err
56 }
57
58 // SetMethod changes the clipboard access method
59 func SetMethod(m string) Method {
60 switch m {
61 case "internal":
62 CurrentMethod = Internal
63 case "external":
64 CurrentMethod = External
65 case "terminal":
66 CurrentMethod = Terminal
67 }
68 return CurrentMethod
69 }
70
71 // Read reads from a clipboard register
72 func Read(r Register) (string, error) {
73 return read(r, CurrentMethod)
74 }
75
76 // Write writes text to a clipboard register
77 func Write(text string, r Register) error {
78 return write(text, r, CurrentMethod)
79 }
80
81 // ReadMulti reads text from a clipboard register for a certain multi-cursor
82 func ReadMulti(r Register, num, ncursors int) (string, error) {
83 clip, err := Read(r)
84 if err != nil {
85 return "", err
86 }
87 if ValidMulti(r, clip, ncursors) {
88 return multi.getText(r, num), nil
89 }
90 return clip, nil
91 }
92
93 // WriteMulti writes text to a clipboard register for a certain multi-cursor
94 func WriteMulti(text string, r Register, num int, ncursors int) error {
95 return writeMulti(text, r, num, ncursors, CurrentMethod)
96 }
97
98 // ValidMulti checks if the internal multi-clipboard is valid and up-to-date
99 // with the system clipboard
100 func ValidMulti(r Register, clip string, ncursors int) bool {
101 return multi.isValid(r, clip, ncursors)
102 }
103
104 func writeMulti(text string, r Register, num int, ncursors int, m Method) error {
105 multi.writeText(text, r, num, ncursors)
106 return write(multi.getAllText(r), r, m)
107 }
108
109 func read(r Register, m Method) (string, error) {
110 switch m {
111 case External:
112 switch r {
113 case ClipboardReg:
114 b, e := clipboard.ReadAll(clipper.RegClipboard)
115 return string(b), e
116 case PrimaryReg:
117 b, e := clipboard.ReadAll(clipper.RegPrimary)
118 return string(b), e
119 default:
120 return internal.read(r), nil
121 }
122 case Internal:
123 return internal.read(r), nil
124 case Terminal:
125 switch r {
126 case ClipboardReg:
127 // terminal paste works by sending an esc sequence to the
128 // terminal to trigger a paste event
129 return terminal.read("clipboard")
130 case PrimaryReg:
131 return terminal.read("primary")
132 default:
133 return internal.read(r), nil
134 }
135 }
136 return "", errors.New("Invalid clipboard method")
137 }
138
139 func write(text string, r Register, m Method) error {
140 switch m {
141 case External:
142 switch r {
143 case ClipboardReg:
144 return clipboard.WriteAll(clipper.RegClipboard, []byte(text))
145 case PrimaryReg:
146 return clipboard.WriteAll(clipper.RegPrimary, []byte(text))
147 default:
148 internal.write(text, r)
149 }
150 case Internal:
151 internal.write(text, r)
152 case Terminal:
153 switch r {
154 case ClipboardReg:
155 return terminal.write(text, "c")
156 case PrimaryReg:
157 return terminal.write(text, "p")
158 default:
159 internal.write(text, r)
160 }
161 }
162 return nil
163 }