-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAdd tags.moon
174 lines (146 loc) · 6.19 KB
/
Add tags.moon
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
export script_name = 'Add tags'
export script_description = 'Add tags to selected lines'
require 'utils'
aegisub.register_macro script_name, script_description, (subs, sel, act) ->
local *
local scope, location, loadprompt, btns, config, userconfigpath
onetimeinit = ->
return if config
scope = all:'All lines', sel:'Selected lines', cur:'Current line'
location = expandlist {
{line: 'Line start'}
{first_in: '{...'}
{each_in: '{... all'}
{first_out: '...}'}
{each_out: '...} all'}
}
loadprompt = 'Previous items...'
btns = expandlist {
{ok: '&Add / reuse chosen item'}
{load: 'Loa&d only'}
{cancel:'&Cancel'}
}
config = -- defaults
all: false
location: location.line
scope: scope.sel
used: {'\\be1','\\fad(100,100)','\\fs','\\fscx','\\fscy','\\alpha&H80&'}
config.text = config.used[1]
userconfigpath = aegisub.decode_path '?user/'..script_name..'.conf'
execute = ->
cfgread!
showdialog!
cfgwrite!
txt = config.text
config.scope = scope.sel if config.scope\find '^'..scope.sel
local ovrbound, ovrtext, ovrnum
switch config.location
when location.line
ovrbound = '^(.*)$'
ovrtext = (s) ->
if s\sub(1,1) == '{'
s\gsub('^{(.-)}','{%1'..txt..'}')
else
'{'..txt..'}'..s
ovrnum = 1
when location.first_in then ovrbound, ovrtext, ovrnum = '{', '{'..txt, 1
when location.first_out then ovrbound, ovrtext, ovrnum = '}', txt..'}', 1
when location.each_in then ovrbound, ovrtext, ovrnum = '{', '{'..txt, nil
when location.each_out then ovrbound, ovrtext, ovrnum = '}', txt..'}', nil
processline = (i,line) ->
with line
if .class=='dialogue' and not .comment
.text = .text\gsub ovrbound, ovrtext, ovrnum
subs[i] = line
switch config.scope
when scope.all
for i,s in ipairs subs do processline i,s
when scope.sel
for i in *sel do processline i,subs[i]
when scope.cur
processline act,subs[act]
else
style = config.scope\gsub('^.*:%s+','')
for i,s in ipairs subs
processline i,s if s.style==style
showdialog = ->
local cfg
dlg = makedialog!
while true
btn,cfg = aegisub.dialog.display(dlg, btns.__list, btns.__namedlist)
switch btn
when btns.ok
break
when btns.load
if cfg.used != loadprompt
for v in *dlg do
if v.name=='text'
v.text = cfg.used
break
else
aegisub.cancel!
for k,v in pairs cfg
config[k] = v if type(v)==type(config[k])
config.text = cfg.used if cfg.used != loadprompt
config.used = for i=1,min 20,#config.used
if config.used[i]\trim()==config.text\trim() then continue
config.used[i]
table.insert config.used,1,config.text
makedialog = ->
_styles = {}
for s in *subs
table.insert _styles,'Style: '..s.name if s.class=='style'
break if s.class=='dialogue'
_scopeitems = {scope.all, scope.sel..' ('..tostring(#sel)..')', scope.cur, unpack _styles}
_scope = if config.scope\find '^'..scope.sel then _scopeitems[2] else config.scope
_used = table.copy config.used
table.insert _used,1,loadprompt
dlg = {
{'textbox', 0,0,6,4, name:'text', text:config.text,
hint:'Input tags to add'}
{'dropdown', 0,4,1,1, name:'location', items:location.__list, value:config.location,
hint:'Point of insertion:\n\tline start\n\tovrblock start / end'}
{'dropdown', 1,4,3,1, name:'scope', items:_scopeitems, value:_scope,
hint:'Scope of action:\n\tall lines\n\tselected lines\n\tspecific style'}
{'dropdown', 4,4,2,1, name:'used', items:_used, value:loadprompt,
hint:'Select value to use instead of the typed text'}
}
--conform the dialog
for c in *dlg do for i,k in ipairs {'class','x','y','width','height'} do c[k] = c[i]
dlg
expandlist = (extendedlist) ->
-- example:
-- input: {{ok:'&Add'}, {load:'Loa&d...'}, {cancel:'&Cancel'}}
-- output: {ok:'&Add', load:'Loa&d...', cancel:'&Cancel',
-- __list: {'&Add', 'Loa&d...', '&Cancel'},
-- __namedlist: {ok:'&Add', load:'Loa&d...', cancel:'&Cancel'}
list = __list:{}, __namedlist:{}
for LI in *extendedlist
for k,v in pairs LI
list[k] = v
list.__namedlist[k] = v
table.insert list.__list, v
list
min = (a,b) -> if a<b then a else b
string.trim = => @\gsub('^%s+','')\gsub('%s+$','')
string.split = (sepcharclass) => return for s in @\gmatch '([^'..sepcharclass..']+)' do s
cfgread = ->
if f = io.open userconfigpath,'r'
splitkv = (kv) ->
k,v = kv\match '^%s*(.-)%s*:%s*(.+)$'
if v\find '|' then k,v\split '|' else k,v
_cfg = {splitkv(kv) for kv in *f\read('*all')\split '\n\r'}
config = _cfg if _cfg and _cfg.scope
f\close!
cfgwrite = ->
if f = io.open userconfigpath,'w'
s = ''
for k,v in pairs config
s ..= k..':'..(if type(v)=='table' then table.concat v,'|' else tostring v)..'\n'
f\write s,'\n'
f\close!
else
aegisub.log 'Error writing '..userconfigpath
onetimeinit!
execute!
sel