-
Notifications
You must be signed in to change notification settings - Fork 0
/
clipboard.red
59 lines (52 loc) · 2.08 KB
/
clipboard.red
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
Red [
title: "Extended clipboard for Spaces"
purpose: "Allow copy/paste of rich content within document"
author: @hiiamboris
license: BSD-3
notes: {
Cloning (deeply replicating anew every object) vs copying (getting new clip with the same data):
- clipboard/write must clone - so any change in the source does not affect clipped contents
- clipboard/read must clone - so any change in the pasted data does not affect clipped contents
and also so spaces can be pasted multiple times without any effort on the pasting code's side
- insertion/removal must not clone, but copy - so undo/redo preserves sameness
}
]
exports: [clipboard]
clipboard: context [
;; prototype for custom clipboard formats
text!: make classy-object! format!: declare-class 'clipboard-format [
name: 'text #type [word!]
data: {}
length: does [length? data] #type [function!]
format: does [system/words/copy data] #type [function!] ;-- must return text only
copy: does [system/words/copy self] #type [function!] ;-- must return shallow copy
clone: does [system/words/copy self] #type [function!] ;-- must clone everything inside or omit
]
;; text is used to detect if some other program wrote to clipboard
;; if read text = last written text, we can use `data`
;; otherwise data is invalid and clipboard is read as plain text string
data: copy text! ;-- last copied data
read: function [
"Get clipboard contents"
/text "Return text even if data is non-textual"
/extern data
][
read: read-clipboard
unless string? read [read: copy {}] ;@@ support wrapping of other clipboard formats? image?
unless read == as-text: data/format [ ;-- last copy comes from outside the running script
self/data: make text! [data: read]
if text [as-text: data/format]
]
either text [as-text][data/clone]
]
write: function [
"Write data to clipboard"
content [object! ('clipboard-format = class? content) string!]
][
self/data: either string? content
[make text! [data: system/words/copy content]]
[content/clone]
write-clipboard self/data/format
]
]
export exports