-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfor_each.proc
More file actions
217 lines (185 loc) · 7.09 KB
/
for_each.proc
File metadata and controls
217 lines (185 loc) · 7.09 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# This script is part of the vieweach CPrAN plugin for Praat.
# The latest version is available through CPrAN or at
# <http://cpran.net/plugins/vieweach>
#
# The vieweach plugin is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, either
# version 3 of the License, or (at your option) any later version.
#
# The vieweach plugin is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with vieweach. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2015 Jose Joaquin Atria
if !variableExists("selection.restore_nocheck")
selection.restore_nocheck = 0
endif
procedure trace ()
endproc
include ../../plugin_selection/procedures/selection.proc
#
# Hooks
#
# Called as the last thing that happens before entering the main loop
procedure for_each.before_iteration ()
endproc
# Called after the iteration has started, after the index of the current
# iteration has been stored in for_each.item
procedure for_each.at_begin_iteration ()
endproc
# Main action procedure
# Called once per iteration, this holds the entire iteration logic
procedure for_each.action ()
endproc
# Called as the last thing before the new iteration index is calculated
# at the end of the main loop. This is the last chance to chance the value of
# for_ecah.next to alter the next value of for_each.index
procedure for_each.at_end_iteration ()
endproc
# Called as the last thing before the selection table with the sets is removed
procedure for_each.finally ()
selectObject: for_each.sets
runScript: for_each.selection_scripts$ +
... "restore_selection.praat"
endproc
# Main procedure
#
procedure for_each ()
.selection_scripts$ = preferencesDirectory$ + "/plugin_selection/scripts/"
.total_sets = numberOfSelected("Table") + numberOfSelected("Strings")
if .total_sets
runScript: .selection_scripts$ + "save_selection.praat"
.sets = selected("Table")
Rename: "foreach_sets"
# Calculate the index number of the last iteration
.max_items = 0
for .set to .total_sets
selectObject: Object_'.sets'[.set, "id"]
.type$ = Object_'.sets'$[.set, "type"]
.items = do("Get number of " +
... if .type$ = "Table" then "rows" else "strings" fi)
.max_items = if .items > .max_items then .items else .max_items fi
endfor
.last = .max_items
.total_items = .last ; For backwards compatibility
# Clear selection
nocheck selectObject: undefined
# Set initial value for index
if !variableExists("for_each.start_from")
.start_from = 1
endif
.start_from = if .start_from = undefined then 1 else .start_from fi
.current = .start_from
.item = .current ; For backwards compatibility
.next = 1
@trace: "For each: before iteration"
@for_each.before_iteration()
# Iterate
while .current > 0 and .current <= .total_items
.item = .current ; For backwards compatibility
@trace: "For each: begin iteration"
@for_each.at_begin_iteration()
# Make two empty selection tables to hold the selection of the current
# iteration, and any newly created objects that need to be removed at the
# end of the current iteration
@createEmptySelectionTable()
.selection = createEmptySelectionTable.table
selectObject: .selection
Rename: "foreach_selection"
@createEmptySelectionTable()
.remove = createEmptySelectionTable.table
selectObject: .remove
Rename: "foreach_remove"
# Populate the selection table for the current selection
@trace: "For each: select"
@for_each._select()
selectObject: .selection
runScript: .selection_scripts$ + "restore_selection.praat"
# Perform the main action
@trace: "For each: action"
@for_each.action()
# Remove any temporary objects as well as the selection tables
selectObject: .remove
runScript: .selection_scripts$ + "restore_selection.praat"
nocheck Remove
removeObject: .selection, .remove
@trace: "For each: end iteration"
@for_each.at_end_iteration()
# Specify the value of the next index. If predefining this procedure,
# setting an appropriate value for this variable is essential.
# 1 = Move to next iteration
# -1 = Move to previous iteration
# 0 = Stop iteration
if !.next
.current += .last
else
.current += .next
endif
endwhile
@trace: "For each: finally"
@for_each.finally()
# Remove the final selection table, holding the IDs of the initial sets
removeObject: .sets
endif
endproc
# Make the selection for the appropriate for the current iteration
# The selection will be stored in for_each.selection
procedure for_each._select ()
# One current object needs to be selected per set
# Sets are either Tables (with objects in the Objects list) or Strings
# (pointing to the file system)
for .s to for_each.total_sets
.set = Object_'for_each.sets'[.s, "id"]
.type$ = Object_'for_each.sets'$[.s, "type"]
selectObject: .set
.items = do("Get number of " +
... if .type$ = "Table" then "rows" else "strings" fi)
# Make sure sets can be of variable length
.i = ((for_each.current - 1) mod .items) + 1
if .type$ = "Table"
.id = Object_'.set'[.i, "id"]
else
selectObject: .set
.name$ = Get string: .i
# If string looks like name of a directory, do not try to open as file
# Instead create a Strings with the list of files in the directory
if right$(.name$) == "/" or right$(.name$) == "\"
runScript: preferencesDirectory$ + "/plugin_strutils/scripts/" +
... "directory_list_full_path.praat", .name$, .name$, "*", "no"
.dirs = selected("Strings")
runScript: preferencesDirectory$ + "/plugin_strutils/scripts/" +
... "file_list_full_path.praat", .name$, .name$, "*", "no"
.files = selected("Strings")
selectObject: .dirs, .files
.id = Append
.n$ = replace_regex$(.name$, "\\", "/", 0)
Rename: replace_regex$(.n$, ".*/([^/]+)/", "\1", 0)
removeObject: .dirs, .files
else
if selection.restore_nocheck
.id = nocheck Read from file: .name$
else
.id = Read from file: .name$
endif
endif
# IDs of new objects are saved for later removal
@addToSelectionTable: for_each.remove, .id
endif
# for_each.selection holds the selection for the current iteration
@addToSelectionTable: for_each.selection, .id
endfor
endproc
# Cleanly interrupt the iterator
# Make sure to remove all the temporary objects that are active during
# iteration.
procedure for_each.clean_exit (.message$)
nocheck removeObject: for_each.sets
nocheck removeObject: for_each.selection
nocheck removeObject: for_each.remove
exitScript: .message$ + newline$
endproc