6 "github.com/zyedidia/clipboard"
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.
19 // Internal just manages the clipboard with an internal buffer and doesn't
20 // attempt to interface with the system clipboard
24 // CurrentMethod is the method used to store clipboard information
25 var CurrentMethod Method = Internal
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.
32 // ClipboardReg is the main system clipboard
33 ClipboardReg Register = -1
34 // PrimaryReg is the system primary clipboard (linux only)
38 // Initialize attempts to initialize the clipboard using the given method
39 func Initialize(m Method) error {
43 err = clipboard.Initialize()
45 CurrentMethod = Internal
49 // SetMethod changes the clipboard access method
50 func SetMethod(m string) Method {
53 CurrentMethod = Internal
55 CurrentMethod = External
57 CurrentMethod = Terminal
62 // Read reads from a clipboard register
63 func Read(r Register) (string, error) {
64 return read(r, CurrentMethod)
67 // Write writes text to a clipboard register
68 func Write(text string, r Register) error {
69 return write(text, r, CurrentMethod)
72 // ReadMulti reads text from a clipboard register for a certain multi-cursor
73 func ReadMulti(r Register, num, ncursors int) (string, error) {
78 if ValidMulti(r, clip, ncursors) {
79 return multi.getText(r, num), nil
84 // WriteMulti writes text to a clipboard register for a certain multi-cursor
85 func WriteMulti(text string, r Register, num int, ncursors int) error {
86 return writeMulti(text, r, num, ncursors, CurrentMethod)
89 // ValidMulti checks if the internal multi-clipboard is valid and up-to-date
90 // with the system clipboard
91 func ValidMulti(r Register, clip string, ncursors int) bool {
92 return multi.isValid(r, clip, ncursors)
95 func writeMulti(text string, r Register, num int, ncursors int, m Method) error {
96 multi.writeText(text, r, num, ncursors)
97 return write(multi.getAllText(r), r, m)
100 func read(r Register, m Method) (string, error) {
105 return clipboard.ReadAll("clipboard")
107 return clipboard.ReadAll("primary")
109 return internal.read(r), nil
112 return internal.read(r), nil
116 // terminal paste works by sending an esc sequence to the
117 // terminal to trigger a paste event
118 return terminal.read("clipboard")
120 return terminal.read("primary")
122 return internal.read(r), nil
125 return "", errors.New("Invalid clipboard method")
128 func write(text string, r Register, m Method) error {
133 return clipboard.WriteAll(text, "clipboard")
135 return clipboard.WriteAll(text, "primary")
137 internal.write(text, r)
140 internal.write(text, r)
144 return terminal.write(text, "c")
146 return terminal.write(text, "p")
148 internal.write(text, r)