clipboard.rex
1.97 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Sample rexx code to justified paste the clipboard contents.
*
* Autor: Perry Werneck <perry.werneck@gmail.com>
*
*/
trace "?R"
host = .rx3270~new("pw3270:a")
if host~connected() = 0 then
do
host~popup("error","Can't start script","Disconnected from host")
return 0
end
text = strip(host~GetClipboard())
if text = "" then
do
say "Clipboard is empty"
return 0
end
if host~WaitForReady(60) <> 0 then
do
host~popup("error","Failed","Timeout waiting for host response")
return 0
end
cursor = host~GetCursorAddr()
next = cursor
do while text <> ""
addr = host~GetFieldStart(next)
next = host~GetNextUnprotected(addr)
host~SetCursorAddr(addr)
field_len = host~GetFieldLen()
s = strip(left(text,field_len))
p = lastpos(" ",s)
n = pos(d2c(10),s)
select
when n <> 0 then
do
s = strip(left(text,n-1))
text = strip(substr(text,n+1))
end
when length(text) < field_len then
do
s = justify(strip(text),field_len)
text = ""
end
when p = 0 then
do
s = strip(left(text,field_len))
text = substr(text,field_len+1)
end
otherwise
s = strip(left(text,p))
text = strip(substr(text,p+1))
end
/* Insert new string */
host~input(s)
if next <= cursor then
do
/* Next field is before the original position */
host~SetClipboard(text)
return 0
end
end
return 0
justify: procedure
use arg text, len
wlen = words(text)
if wlen < 3
then return text
ln = .array~new()
sz = 0
do f = 1 to wlen
ln[f] = word(text,f)||" "
sz = sz + length(ln[f])
end
do while sz < len
do f = wlen-1 to 1 by -1
ln[f] = ln[f]||"."
sz = sz+1
if sz >= len
then leave
end
end
str = ""
do f = 1 to wlen
str = str||ln[f]
end
return strip(str)
::requires "rx3270.cls"