Skip to content

Commit a90584d

Browse files
committed
Update ostinato_maker.lua
Couldn't work out how to "resolve" the previous PR so started fresh. Converted all global variables to local, assorted minor improvements. Now shouldn't require any LINT warnings.
1 parent 6830280 commit a90584d

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

src/ostinato_maker.lua

+34-32
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ function plugindef()
44
finaleplugin.Author = "Carl Vine after Michael McClennan & Jacob Winkler"
55
finaleplugin.AuthorURL = "https://carlvine.com/lua/"
66
finaleplugin.Copyright = "https://creativecommons.org/licenses/by/4.0/"
7-
finaleplugin.Version = "v0.13"
8-
finaleplugin.Date = "2024/01/15"
7+
finaleplugin.Version = "v0.19"
8+
finaleplugin.Date = "2024/01/31"
99
finaleplugin.MinJWLuaVersion = 0.62
1010
finaleplugin.Notes = [[
1111
Copy the current selection and paste it consecutively
@@ -57,24 +57,25 @@ Key Commands:
5757
info_notes = info_notes:gsub("\n%s*", " "):gsub("*", "\n"):gsub("@t", "\t")
5858
.. "\n(" .. finaleplugin.Version .. ")"
5959

60-
--luacheck: ignore 11./global_dialog 11./global_selection
61-
global_timer_id = 1
62-
6360
local configuration = require("library.configuration")
6461
local mixin = require("library.mixin")
65-
local script_name = "ostinato_maker"
62+
local library = require("library.general_library")
63+
local script_name = library.calc_script_name()
64+
local dialog, current_selection
6665

6766
local config = {
68-
num_repeats = 1,
67+
num_repeats = 1,
68+
timer_id = 1,
6969
window_pos_x = false,
7070
window_pos_y = false,
7171
}
7272
local dialog_options = { -- and populate config values (unchecked)
73-
"copy_articulations", "copy_expressions", "copy_smartshapes", "copy_lyrics", "copy_chords"
73+
"copy_articulations", "copy_expressions", "copy_smartshapes",
74+
"copy_lyrics", "copy_chords"
7475
}
7576
for _, v in ipairs(dialog_options) do config[v] = 0 end -- (default unchecked)
7677

77-
local bounds = { -- primary region selection boundary
78+
local bounds = { -- primary region selection boundary properties
7879
"StartStaff", "StartMeasure", "StartMeasurePos",
7980
"EndStaff", "EndMeasure", "EndMeasurePos",
8081
}
@@ -87,15 +88,15 @@ local function copy_region_bounds()
8788
return copy
8889
end
8990

90-
function dialog_set_position(dialog)
91+
local function dialog_set_position()
9192
if config.window_pos_x and config.window_pos_y then
9293
dialog:StorePosition()
9394
dialog:SetRestorePositionOnlyData(config.window_pos_x, config.window_pos_y)
9495
dialog:RestorePosition()
9596
end
9697
end
9798

98-
function dialog_save_position(dialog)
99+
local function dialog_save_position()
99100
dialog:StorePosition()
100101
config.window_pos_x = dialog.StoredX
101102
config.window_pos_y = dialog.StoredY
@@ -276,8 +277,9 @@ local function paste_many_copies()
276277
local undo_str = "Ostinato Maker " .. selection_id()
277278
finenv.StartNewUndoBlock(undo_str, false)
278279
-- ---- DO THE WORK
279-
if rgn.EndMeasurePos >= measure_duration(rgn.EndMeasure) then
280-
rgn.EndMeasurePos = measure_duration(rgn.EndMeasure) - 1
280+
local m_d = measure_duration(rgn.EndMeasure)
281+
if rgn.EndMeasurePos >= m_d then
282+
rgn.EndMeasurePos = m_d - 1
281283
end
282284
rgn:CopyMusic() -- save a copy of the current selection
283285
local duration = region_duration(rgn)
@@ -309,35 +311,36 @@ end
309311
local function on_timer()
310312
local changed = false
311313
for _, v in ipairs(bounds) do
312-
if global_selection[v] ~= finenv.Region()[v] then
314+
if current_selection[v] ~= finenv.Region()[v] then
313315
changed = true
314316
break
315317
end
316318
end
317319
if changed then
318-
global_selection = copy_region_bounds()
319-
global_dialog:GetControl("info")
320+
current_selection = copy_region_bounds()
321+
dialog:GetControl("info")
320322
:SetText("Selection " .. selection_id() .. "\n" .. staff_id())
321323
end
322324
end
323325

324-
local function create_dialog_box()
326+
local function create_dialog()
325327
local edit_x, e_wide, y_step = 110, 40, 18
326328
local y_offset = finenv.UI():IsOnMac() and 3 or 0
327-
local save_rpt, answer = config.num_repeats, {}
328-
local dialog = mixin.FCXCustomLuaWindow():SetTitle(plugindef())
329+
local save_rpt = config.num_repeats
330+
local name = plugindef():gsub("%.%.%.", "")
331+
dialog = mixin.FCXCustomLuaWindow():SetTitle(name)
329332
local y = 0
330333
local function flip_check(id)
331-
local ctl = answer[dialog_options[id]]
334+
local ctl = dialog:GetControl(dialog_options[id])
332335
ctl:SetCheck((ctl:GetCheck() + 1) % 2)
333336
end
334337
local function check_all_state(state)
335338
for _, v in ipairs(dialog_options) do
336-
answer[v]:SetCheck(state)
339+
dialog:GetControl(v):SetCheck(state)
337340
end
338341
end
339342
local function info_dialog()
340-
finenv.UI():AlertInfo(info_notes, "About " .. plugindef())
343+
finenv.UI():AlertInfo(info_notes, "About " .. name)
341344
end
342345
local function key_check(ctl) -- some stray key commands
343346
local s = ctl:GetText():lower()
@@ -370,40 +373,39 @@ local function create_dialog_box()
370373
:AddHandleCommand(function(self) key_check(self) end)
371374
dialog:CreateStatic(e_wide, y + 2):SetText("times"):SetWidth(edit_x)
372375
for _, v in ipairs(dialog_options) do
373-
answer[v] = dialog:CreateCheckbox(edit_x, y):SetCheck(config[v])
376+
dialog:CreateCheckbox(edit_x, y, v):SetCheck(config[v])
374377
:SetText(v:sub(6, -1):gsub("^%l", string.upper)):SetWidth(90)
375378
y = y + y_step
376379
end
377380
local q = dialog:CreateButton(0, y - 19):SetText("?"):SetWidth(20)
378381
:AddHandleCommand(function() info_dialog() end)
379382
dialog:CreateOkButton()
380383
dialog:CreateCancelButton()
381-
dialog_set_position(dialog)
384+
dialog_set_position()
382385
dialog:RegisterHandleTimer(on_timer)
383386
dialog:RegisterInitWindow(function()
384387
q:SetFont(q:CreateFontInfo():SetBold(true))
385388
num_repeats:SetKeyboardFocus()
386-
dialog:SetTimer(global_timer_id, 125)
389+
dialog:SetTimer(config.timer_id, 125)
387390
end)
388391
dialog:RegisterCloseWindow(function()
389-
dialog_save_position(dialog)
390-
dialog:StopTimer(global_timer_id)
392+
dialog_save_position()
393+
dialog:StopTimer(config.timer_id)
391394
end)
392395
dialog:RegisterHandleOkButtonPressed(function()
393396
config.num_repeats = num_repeats:GetInteger()
394397
for _, v in ipairs(dialog_options) do
395-
config[v] = answer[v]:GetCheck()
398+
config[v] = dialog:GetControl(v):GetCheck()
396399
end
397400
paste_many_copies()
398401
end)
399-
return dialog
400402
end
401403

402404
local function make_ostinato()
403405
configuration.get_user_settings(script_name, config, true)
404-
global_selection = global_selection or copy_region_bounds()
405-
global_dialog = global_dialog or create_dialog_box()
406-
global_dialog:RunModeless()
406+
current_selection = copy_region_bounds()
407+
create_dialog()
408+
dialog:RunModeless()
407409
end
408410

409411
make_ostinato()

0 commit comments

Comments
 (0)