Repository navigation
Expand file tree
/
Copy pathCopy-clip.lua
More file actions
63 lines (54 loc) · 2.38 KB
/
Copy pathCopy-clip.lua
File metadata and controls
63 lines (54 loc) · 2.38 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
script_name = "Copy Clip Within Selection"
script_description = "Apply clip/iclip from first selected line to all other selected lines"
script_author = "Claude"
script_version = "1.1"
function copy_clip_within_selection(subtitles, selected_lines, active_line)
if #selected_lines < 2 then
aegisub.debug.out("Select at least 2 lines (one source with \\clip or \\iclip and target lines)\n")
return
end
-- Find the first clip or iclip in selection (in the order of selected_lines)
local chosen_cmd = nil -- "clip" or "iclip"
local clip_value = nil
local source_index = nil
for _, line_index in ipairs(selected_lines) do
local line = subtitles[line_index]
-- capture optional 'i' and the arguments inside parentheses
local i_flag, args = line.text:match("\\(i?)clip%(([^%)]+)%)")
if args then
if i_flag == "i" then
chosen_cmd = "iclip"
else
chosen_cmd = "clip"
end
clip_value = args
source_index = line_index
break
end
end
if not clip_value then
aegisub.debug.out("No \\clip or \\iclip command found in any selected line\n")
return
end
-- Apply chosen clip/iclip to all other selected lines:
for _, line_index in ipairs(selected_lines) do
if line_index ~= source_index then
local line = subtitles[line_index]
-- If the line already has any clip or iclip, replace all occurrences with chosen
if line.text:find("\\clip") or line.text:find("\\iclip") then
line.text = line.text:gsub("\\i?clip%([^%)]+%)", "\\" .. chosen_cmd .. "(" .. clip_value .. ")")
else
-- If the line has no override block at the start, add one
if not line.text:find("^{") then
line.text = "{" .. "\\" .. chosen_cmd .. "(" .. clip_value .. ")}" .. line.text
else
-- Has an override block but no clip -> insert clip right after '{'
line.text = line.text:gsub("^{", "{\\" .. chosen_cmd .. "(" .. clip_value .. ")")
end
end
subtitles[line_index] = line
end
end
aegisub.set_undo_point("Copy clip/iclip within selection")
end
aegisub.register_macro(script_name, script_description, copy_clip_within_selection)