-
Notifications
You must be signed in to change notification settings - Fork 0
/
rgplua_built_in_functions.lua
254 lines (237 loc) · 8.28 KB
/
rgplua_built_in_functions.lua
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
--[[
These functions are converted to rgplua_built_in_functions.xxd by means of the Unix xxd command.
They were originally scraped out of the jwlua64 binary for macOS.
]]
finaleplugin = {} -- allows the plugin to create its header
function dumpproperties(o, include_bases)
if type(o) ~= "userdata" then return nil end
if o["ClassName"] == nil then return nil end
local classname = o:ClassName()
for k,v in pairs(_G.finale) do
if k == classname then
local returnval = {}
local populate_returnval
populate_returnval = function(classtable)
if include_bases then
local parent_table = classtable.__parent
if parent_table then
for parent_name, _ in pairs(parent_table) do
populate_returnval(_G.finale[parent_name])
end
end
end
-- include this instance's properties last, so that parent properties of the same name are overwritten
local props = classtable.__propget -- with JW Lua v0.54 and earlier, this would have been v.__class.__propget
if props == nil then return end
for k1, v1 in pairs(props) do
-- Some classes have properties that cannot be called in lower Finale versions, so skip any errors.
-- (An example is FCGeneralPrefs.)
local success, value = pcall(function() return o[k1] end)
if success then returnval[k1] = value end
end
end
populate_returnval(v)
return returnval
end
end
return nil
end
function pairsbykeys (t, f)
local a = {}
for n in pairs(t) do table.insert(a, n) end
table.sort(a, f)
local i = 0
local iter = function ()
i = i + 1
if a[i] == nil then return nil
else return a[i], t[a[i]]
end
end
return iter
end
function eachentry(region, layer)
local measure = region.StartMeasure
local slotno = region:GetStartSlot()
local i = 0
local layertouse = 0
if layer ~= nil then layertouse = layer end
local c = finale.FCNoteEntryCell(measure, region:CalcStaffNumber(slotno))
c:SetLoadLayerMode(layertouse)
c:Load()
return function ()
while true do
i = i + 1;
local returnvalue = c:GetItemAt(i - 1)
if returnvalue ~= nil then
if (region:IsEntryPosWithin(returnvalue)) then return returnvalue end
else
measure = measure + 1
if measure > region.EndMeasure then
measure = region.StartMeasure
slotno = slotno + 1
if (slotno > region:GetEndSlot()) then return nil end
c = finale.FCNoteEntryCell(measure, region:CalcStaffNumber(slotno))
c:SetLoadLayerMode(layertouse)
c:Load()
i = 0
else
c = finale.FCNoteEntryCell(measure, region:CalcStaffNumber(slotno))
c:SetLoadLayerMode(layertouse)
c:Load()
i = 0
end
end
end
end
end
function eachentrysaved(region, layer)
local measure = region.StartMeasure
local slotno = region:GetStartSlot()
local i = 0
local layertouse = 0
if layer ~= nil then layertouse = layer end
local c = finale.FCNoteEntryCell(measure, region:CalcStaffNumber(slotno))
c:SetLoadMirrors(false)
c:SetLoadLayerMode(layertouse)
c:Load()
return function ()
while true do
i = i + 1;
local returnvalue = c:GetItemAt(i - 1)
if returnvalue ~= nil then
if (region:IsEntryPosWithin(returnvalue)) then return returnvalue end
else
c:DeleteAllNullEntries()
c:Save()
measure = measure + 1
if measure > region.EndMeasure then
measure = region.StartMeasure
slotno = slotno + 1
if (slotno > region:GetEndSlot()) then return nil end
c = finale.FCNoteEntryCell(measure, region:CalcStaffNumber(slotno))
c:SetLoadLayerMode(layertouse)
c:Load()
i = 0
else
c = finale.FCNoteEntryCell(measure, region:CalcStaffNumber(slotno))
c:SetLoadLayerMode(layertouse)
c:Load()
i = 0
end
end
end
end
end
function loadall(t)
if t == nil then return nil end
if t["LoadAll"] == nil then return nil end
if t["GetItemAt"] == nil then return nil end
t:LoadAll()
local i = 0
return function ()
i = i + 1; return t:GetItemAt(i - 1)
end
end
function loadallforregion(t, r)
if t == nil then return nil end
if t["LoadAllForRegion"] == nil then return nil end
if t["GetItemAt"] == nil then return nil end
t:LoadAllForRegion(r)
local i = 0
return function ()
i = i + 1; return t:GetItemAt(i - 1)
end
end
function each(t)
if t == nil then return nil end
if t["GetItemAt"] == nil then return nil end
local i = 0
return function ()
i = i + 1; return t:GetItemAt(i - 1)
end
end
function eachbackwards(t)
if t == nil then return nil end
if t["GetItemAt"] == nil then return nil end
if t["GetCount"] == nil then return nil end
local i = t:GetCount()
return function ()
i = i - 1; return t:GetItemAt(i)
end
end
function coll2table(t)
local returnval = {}
local i = 1
for v in each(t) do
returnval[i] = v
i = i + 1
end
return returnval
end
function eachcell(region)
local measure = region.StartMeasure - 1
local slotno = region:GetStartSlot()
return function()
measure = measure + 1
if measure > region.EndMeasure then
measure = region.StartMeasure
slotno = slotno + 1
if slotno > region:GetEndSlot() then return nil end
end
return measure, region:CalcStaffNumber(slotno)
end
end
function eachstaff(region)
local measure = region.StartMeasure
local slotno = region:GetStartSlot() - 1
return function ()
while true do
slotno = slotno + 1
if slotno <= region:GetEndSlot() then
local staff_num = region:CalcStaffNumber(slotno)
if staff_num > 0 then
return staff_num
end
end
return nil
end
end
end
function prettyformatjson(json_string, tab_width)
tab_width = tab_width or 3
local level = 0
local formatted_parts = {} -- accumulate formatted string into this table for speed
local in_string = false
for i = 1, #json_string do
local char = json_string:sub(i, i)
if char == '"' and (i == 1 or json_string:sub(i - 1, i - 1) ~= "\\") then
in_string = not in_string
end
if not in_string then
if char == "{" or char == "[" then
level = level + 1
table.insert(formatted_parts, char)
table.insert(formatted_parts, "\n")
table.insert(formatted_parts, string.rep(" ", tab_width * level))
elseif char == "}" or char == "]" then
level = level - 1
table.insert(formatted_parts, "\n")
table.insert(formatted_parts, string.rep(" ", tab_width * level))
table.insert(formatted_parts, char)
elseif char == "," then
table.insert(formatted_parts, char)
table.insert(formatted_parts, "\n")
table.insert(formatted_parts, string.rep(" ", tab_width * level))
elseif char == ":" then
table.insert(formatted_parts, char)
table.insert(formatted_parts, " ")
else
table.insert(formatted_parts, char)
end
else
table.insert(formatted_parts, char)
end
end
table.insert(formatted_parts, "\n")
return table.concat(formatted_parts)
end