2021-04-05 15:03:24 +00:00
script_name = " Dupe and Comment "
2021-10-09 14:20:50 +00:00
script_description = " Copies a line and comments out the original. \n because i like seeing the original while editing, and being able to go back to it easily "
2021-04-05 15:03:24 +00:00
script_author = " garret "
2023-01-17 22:50:58 +00:00
script_version = " 3.0.1 "
2021-10-09 14:20:50 +00:00
script_namespace = " garret.dupe-and-comment "
local haveDepCtrl , DependencyControl , depctrl = pcall ( require , " l0.DependencyControl " )
if haveDepCtrl then
depctrl = DependencyControl {
--feed="TODO",
}
end
2021-04-05 15:03:24 +00:00
2023-01-13 11:56:34 +00:00
local function comment ( subs , sel , act )
2021-04-05 15:03:24 +00:00
for i =# sel , 1 , - 1 do
2023-01-15 00:05:27 +00:00
local line = subs [ sel [ i ] ] -- grab copy of current line
-- now use that copy for a different line
line.comment = true -- comment out the new dupe line
subs.insert ( sel [ i ] + 1 , line ) -- and put it below
2023-01-29 23:45:41 +00:00
-- if #sel > 1 then
-- sort out sel/act
local preceding_lines = i - 1
local on_act = act == sel [ i ]
2023-01-15 00:05:27 +00:00
2023-01-29 23:45:41 +00:00
sel [ i ] = sel [ i ] + preceding_lines
if on_act then act = sel [ i ] end
-- end
2021-04-05 15:03:24 +00:00
end
aegisub.set_undo_point ( script_name )
2023-01-13 11:56:34 +00:00
return sel , act
2021-04-05 15:03:24 +00:00
end
2023-01-13 11:56:34 +00:00
local function undo ( subs , sel , act )
2021-04-10 10:49:58 +00:00
for i =# sel , 1 , - 1 do
local edit = subs [ sel [ i ] ]
2023-01-15 01:19:53 +00:00
if not ( sel [ i ] + 1 > # subs ) then -- preventing out-of-range errors
2022-12-24 16:00:13 +00:00
local original = subs [ sel [ i ] + 1 ]
2023-01-15 01:19:53 +00:00
2022-12-24 16:00:13 +00:00
if edit.comment == false and original.comment == true then
original.comment = false
subs [ sel [ i ] + 1 ] = original
subs.delete ( sel [ i ] )
2023-01-15 01:19:53 +00:00
2023-01-29 23:45:41 +00:00
if # sel > 1 then
2023-01-15 01:19:53 +00:00
-- sort out selection. same as `do`, but the other way round.
2023-01-17 22:50:58 +00:00
local preceding_lines = i + 1
local on_act = act == sel [ i ]
2023-01-29 23:45:41 +00:00
sel [ i ] = sel [ i ] - preceding_lines
2023-01-17 22:50:58 +00:00
if on_act then act = sel [ i ] end
2023-01-29 23:45:41 +00:00
end
2023-01-15 01:19:53 +00:00
2022-12-24 16:00:13 +00:00
end
2021-04-10 10:49:58 +00:00
end
end
aegisub.set_undo_point ( " Undo " .. script_name )
2023-01-13 11:56:34 +00:00
return sel , act
2021-04-10 10:49:58 +00:00
end
2021-10-09 14:20:50 +00:00
local macros = {
{ " Do " , script_description , comment } ,
{ " Undo " , " Deletes selected line and restores the original " , undo }
}
if haveDepCtrl then
depctrl : registerMacros ( macros )
else
for _ , macro in ipairs ( macros ) do
local name , desc , fun = unpack ( macro )
aegisub.register_macro ( script_name .. ' / ' .. name , desc , fun )
end
end
-- i trust what petzku from the cartel has to say on adding multiple macros with depctrl