Skip to content

Commit 176f6ac

Browse files
authored
Merge pull request #757 from cv-on-hub/cv-cross-staff
CORRECTION to cross_staff_offset.lua
2 parents f8c4bb8 + c61097c commit 176f6ac

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

src/cross_staff_offset.lua

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
function plugindef()
2-
finaleplugin.RequireSelection = true
2+
finaleplugin.RequireSelection = false
33
finaleplugin.HandlesUndo = true
44
finaleplugin.Author = "Carl Vine"
55
finaleplugin.AuthorURL = "http://carlvine.com/lua/"
66
finaleplugin.Copyright = "https://creativecommons.org/licenses/by/4.0/"
7-
finaleplugin.Version = "v1.64" -- Modeless option
8-
finaleplugin.Date = "2024/04/13"
7+
finaleplugin.Version = "v1.66" -- with Modeless option
8+
finaleplugin.Date = "2024/07/28"
99
finaleplugin.MinJWLuaVersion = 0.62
1010
finaleplugin.Notes = [[
1111
When crossing notes to adjacent staves the stems of _crossed_ notes
@@ -22,6 +22,7 @@ function plugindef()
2222
2323
> - __u__: reset default __up__ values
2424
> - __d__: reset default __down__ values
25+
> - __m__: toggle __Modeless__
2526
> - __q__: display these notes
2627
> - __0 - 4__: layer number (delete key not needed)
2728
> - To change measurement units:
@@ -74,16 +75,29 @@ local function get_staff_name(staff_num)
7475
staff:Load(staff_num)
7576
local staff_name = staff:CreateDisplayAbbreviatedNameString().LuaString
7677
if not staff_name or staff_name == "" then
77-
staff_name = "Staff " .. staff_num
78+
staff_name = "Staff" .. staff_num
7879
end
7980
return staff_name
8081
end
8182

82-
local function change_offsets()
83+
local function nil_region_error(dialog)
84+
if finenv.Region():IsEmpty() then
85+
local ui = dialog and dialog:CreateChildUI() or finenv.UI()
86+
ui:AlertError(
87+
"Please select some music\nbefore running this script.",
88+
finaleplugin.ScriptGroupName
89+
)
90+
return true
91+
end
92+
return false
93+
end
94+
95+
local function change_offsets(dialog)
96+
if nil_region_error(dialog) then return end
8397
local rgn = finenv.Region()
8498
finenv.StartNewUndoBlock(
8599
string.format("%s %s m.%d-%d",
86-
name, get_staff_name(rgn.StartStaff), rgn.StartMeasure, rgn.EndMeasure
100+
name:gsub(" ", ""), get_staff_name(rgn.StartStaff),rgn.StartMeasure, rgn.EndMeasure
87101
)
88102
)
89103
for entry in eachentrysaved(rgn, config.layer_num) do
@@ -98,7 +112,7 @@ end
98112
local function run_the_dialog()
99113
local x_grid = { 0, 113, 184 }
100114
local y, y_step = 3, 23
101-
local default_value = 24
115+
local default_value = 24 -- assumed staff line spacing in EVPUs
102116
local e_width = 64
103117
local box, save_value = {}, {}
104118
local max = layer.max_layers()
@@ -142,8 +156,9 @@ local function run_the_dialog()
142156
or (id == 3 and s:find("[^0-" .. max .. "]"))
143157
then
144158
if s:find("[?q]") then show_info()
145-
elseif s:find("u") then set_defaults( 1) -- up
146-
elseif s:find("d") then set_defaults(-1) -- down
159+
elseif s:find("[ud]") then -- toggle up/down
160+
box[id]:SetText(save_value[id]) -- pre-save old value
161+
set_defaults(s:find("u") and 1 or -1)
147162
elseif s:find("m") then -- toggle modeless
148163
box[modeless]:SetCheck((box[modeless]:GetCheck() + 1) % 2)
149164
elseif s:find("[eicoas]") then -- change measurement unit
@@ -157,19 +172,18 @@ local function run_the_dialog()
157172
end
158173
end
159174
end
160-
box[id]:SetText(save_value[id])
161-
elseif s ~= "" then -- save new "clean" numnber
162-
if id == 3 then
163-
s = s:sub(-1) -- 1-char layer number
175+
else
176+
if id == 3 then -- layer number
177+
s = s:sub(-1) -- 1-char max
164178
else
165179
if s == "." then s = "0." -- offsets, leading zero
166180
elseif s == "-." then s = "-0."
167181
end
168182
s = s:sub(1, 8)
169183
end
170-
box[id]:SetText(s)
171-
save_value[id] = s
184+
save_value[id] = (s == "") and "0" or s
172185
end
186+
box[id]:SetText(save_value[id])
173187
end
174188
local function submission_error()
175189
local values = { 576, config.cross_staff_offset, config.non_cross_offset }
@@ -226,7 +240,7 @@ local function run_the_dialog()
226240
if submission_error() then
227241
user_error = true
228242
else -- go ahead and change the offsets
229-
change_offsets()
243+
change_offsets(dialog)
230244
end
231245
end)
232246
dialog:RegisterCloseWindow(function(self)
@@ -239,19 +253,20 @@ local function run_the_dialog()
239253
dialog:RunModeless()
240254
else
241255
dialog:ExecuteModal() -- "modal"
242-
if refocus_document then finenv.UI():ActivateDocumentWindow() end
243256
end
244257
return change_mode or user_error
245258
end
246259

247260
local function cross_staff_offset()
261+
if not config.modeless and nil_region_error() then return end
248262
local qim = finenv.QueryInvokedModifierKeys
249263
local shift_key = qim and (qim(finale.CMDMODKEY_ALT) or qim(finale.CMDMODKEY_SHIFT))
250264

251-
if shift_key then
265+
if shift_key then
252266
change_offsets()
253267
else
254268
while run_the_dialog() do end
269+
if refocus_document then finenv.UI():ActivateDocumentWindow() end
255270
end
256271
end
257272

0 commit comments

Comments
 (0)