forked from unanimated/luaegisub
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmultiedit.lua
151 lines (138 loc) · 5.66 KB
/
multiedit.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
script_name="Multi-line Editor"
script_description="Multi-line Editor"
script_author="unanimated"
script_version="1.33"
require "clipboard"
function editlines(subs, sel)
editext=""
dura=""
for x, i in ipairs(sel) do
cancelled=aegisub.progress.is_cancelled()
if cancelled then aegisub.cancel() end
aegisub.progress.title(string.format("Reading line: %d/%d",x,#sel))
line=subs[i]
text=line.text
dur=line.end_time-line.start_time
dur=dur/1000
char=text:gsub("{[^}]-}","") :gsub("\\[Nn]","*") :gsub("%s?%*+%s?"," ") :gsub(" ","") :gsub("[%.,%?!'\"—]","")
linelen=char:len()
cps=math.ceil(linelen/dur)
if tostring(dur):match("%.%d$") then dur=dur.."0" end
if not tostring(dur):match("%.") then dur=dur..".00" end
if cps<10 then cps=" "..cps end
if dur=="0.00" then cps="n/a" end
if x~=#sel then editext=editext..text.."\n" dura=dura..dur.." .. "..cps.." .. "..linelen.."\n" end
if x==#sel then editext=editext..text dura=dura..dur.." .. "..cps.." .. "..linelen end
end
editbox(subs, sel)
if failt==1 then editext=res.dat editbox(subs, sel) end
return sel
end
function esc(str)
str=str
:gsub("%%","%%%%")
:gsub("%(","%%%(")
:gsub("%)","%%%)")
:gsub("%[","%%%[")
:gsub("%]","%%%]")
:gsub("%.","%%%.")
:gsub("%*","%%%*")
:gsub("%-","%%%-")
:gsub("%+","%%%+")
:gsub("%?","%%%?")
return str
end
function editbox(subs, sel)
aegisub.progress.title("Loading Editor...")
if #sel<=4 then boxheight=7 end
if #sel>=5 and #sel<9 then boxheight=8 end
if #sel>=9 and #sel<15 then boxheight=math.ceil(#sel*0.8) end
if #sel>=15 and #sel<18 then boxheight=12 end
if #sel>=18 then boxheight=15 end
if editext:len()>1500 and boxheight==7 then boxheight=8 end
if editext:len()>1800 and boxheight==8 then boxheight=9 end
nocom=editext:gsub("{[^}]-}","")
words=0
plaintxt=nocom:gsub("%p","")
for wrd in plaintxt:gmatch("%w+") do
words=words+1
end
if lastrep1==nil then lastrep1="" end
if lastrep2==nil then lastrep2="" end
dialog=
{
{x=0,y=0,width=52,height=1,class="label",label="Text"},
{x=52,y=0,width=5,height=1,class="label",label="Duration | CPS | chrctrs"},
{x=0,y=boxheight+1,width=1,height=1,class="label",label="Replace:"},
{x=1,y=boxheight+1,width=15,height=1,class="edit",name="rep1",value=lastrep1},
{x=16,y=boxheight+1,width=1,height=1,class="label",label="with"},
{x=17,y=boxheight+1,width=15,height=1,class="edit",name="rep2",value=lastrep2},
{x=0,y=1,width=52,height=boxheight,class="textbox",name="dat",value=editext},
{x=52,y=1,width=5,height=boxheight,class="textbox",name="durr",value=dura,hint="This is informative only. \nCPS=Characters Per Second"},
{x=32,y=boxheight+1,width=20,height=1,class="edit",name="info",value="Lines loaded: "..#sel..", Characters: "..editext:len()..", Words: "..words },
{x=52,y=boxheight+1,width=5,height=1,class="label",label="Multi-Line Editor v"..script_version},
}
buttons={"Save","Replace","Remove tags","Rm. comments","Remove \"- \"","Remove \\N","Add italics","Add \\an8","Reload text","Taller GUI","Cancel"}
repeat
if pressed=="Replace" or pressed=="Add italics" or pressed=="Add \\an8" or pressed=="Remove \\N" or pressed=="Reload text"
or pressed=="Remove tags" or pressed=="Rm. comments" or pressed=="Remove \"- \"" or pressed=="Taller GUI" then
if pressed=="Add italics" then
res.dat=res.dat :gsub("$","\n") :gsub("(.-)\n","{\\i1}%1\n") :gsub("{\\i1}{\\","{\\i1\\") :gsub("\n$","") end
if pressed=="Add \\an8" then
res.dat=res.dat :gsub("$","\n") :gsub("(.-)\n","{\\an8}%1\n") :gsub("{\\an8}{\\","{\\an8\\") :gsub("\n$","") end
if pressed=="Remove \\N" then res.dat=res.dat :gsub("%s?\\N%s?"," ") end
if pressed=="Remove tags" then res.dat=res.dat:gsub("{%*?\\[^}]-}","") end
if pressed=="Rm. comments" then res.dat=res.dat:gsub("{[^\\}]-}","") :gsub("{[^\\}]-\\N[^\\}]-}","") end
if pressed=="Remove \"- \"" then res.dat=res.dat:gsub("%- ","") end
if pressed=="Replace" then rep1=esc(res.rep1)
res.dat=res.dat:gsub(rep1,res.rep2)
end
if pressed=="Taller GUI" then boxheight=boxheight+1
for key,val in ipairs(dialog) do
if val.y==1 then val.height=val.height+1 end
if val.y>1 then val.y=val.y+1 end
end
end
for key,val in ipairs(dialog) do
if pressed~="Reload text" then
if val.name=="dat" then val.value=res.dat end
if val.name=="durr" then val.value=res.durr end
if val.name=="info" then val.value=res.info end
if val.name=="oneline" then val.value=res.oneline end
if val.name=="rep1" then val.value=res.rep1 end
if val.name=="rep2" then val.value=res.rep2 end
else
if val.name=="dat" then val.value=editext end
end
end
end
pressed, res=aegisub.dialog.display(dialog,buttons,{save='Save',close='Cancel'})
until pressed=="Save" or pressed=="Cancel"
if pressed=="Cancel" then aegisub.cancel() end
if pressed=="Save" then savelines(subs, sel) end
lastrep1=res.rep1
lastrep2=res.rep2
return sel
end
function savelines(subs, sel)
aegisub.progress.title("Saving...")
local data={} raw=res.dat.."\n"
if #sel==1 then raw=raw:gsub("\n(.)","\\N%1") raw=raw:gsub("\\N "," \\N") end
for dataline in raw:gmatch("(.-)\n") do table.insert(data,dataline) end
failt=0
if #sel~=#data and #sel>1 then failt=1 else
for x, i in ipairs(sel) do
line=subs[i]
text=subs[i].text
text=data[x]
line.text=text
subs[i]=line
end
end
if failt==1 then aegisub.dialog.display({{class="label",
label="Line count of edited text does not \nmatch the number of selected lines.",x=0,y=0,width=1,height=2}},{"OK"})
clipboard.set(res.dat) end
aegisub.set_undo_point(script_name)
return sel
end
aegisub.register_macro(script_name, script_description, editlines)