@@ -3,42 +3,78 @@ local actions = {}
3
3
4
4
function actions .projectStart () reaper .SetEditCurPos (0 , true , false ) end
5
5
6
- function actions .projectEnd ()
7
- reaper .SetEditCurPos (reaper .GetProjectLength (0 ), true , false )
8
- end
6
+ function actions .projectEnd () reaper .SetEditCurPos (reaper .GetProjectLength (0 ), true , false ) end
9
7
10
8
function actions .firstItemStart ()
11
- local start = nil
9
+ local len = reaper .GetProjectLength (0 )
10
+ local start = len
12
11
for i = 0 , reaper .CountSelectedTracks () - 1 do
13
12
local track = reaper .GetSelectedTrack (0 , i )
14
13
if reaper .GetTrackNumMediaItems (track ) > 0 then
15
14
local item = reaper .GetTrackMediaItem (track , 0 )
16
15
local pos = reaper .GetMediaItemInfo_Value (item , " D_POSITION" )
17
- if not start or pos < start then start = pos end
16
+ if pos < start then start = pos end
18
17
end
19
18
end
20
- if start then reaper .SetEditCurPos (start , true , false ) end
19
+ if start < len then reaper .SetEditCurPos (start , true , false ) end
21
20
end
22
21
23
- -- This won't work if last item on track is not the "last" one, imagine [long----[short]--],
24
- -- short is last but ends sooner. However, this is a reasonable limitation as otherwise we
25
- -- need to scan at most all items on all selected tracks
22
+ -- This won't work if last item on track is not the one with latest start, imagine [long----[short]--],
23
+ -- This is a reasonable limitation as otherwise we need to scan all items on all selected tracks
26
24
function actions .lastItemEnd ()
27
- local last_end = nil
25
+ local last_end = 0
28
26
for i = 0 , reaper .CountSelectedTracks () - 1 do
29
27
local track = reaper .GetSelectedTrack (0 , i )
30
28
local items = reaper .GetTrackNumMediaItems (track )
31
- if items > 0 then
32
- local item = reaper .GetTrackMediaItem (track , items - 1 )
29
+ if items == 0 then goto next_track end
30
+ local item = reaper .GetTrackMediaItem (track , items - 1 )
31
+ local pos = reaper .GetMediaItemInfo_Value (item , " D_POSITION" )
32
+ + reaper .GetMediaItemInfo_Value (item , " D_LENGTH" )
33
+ if pos > last_end then last_end = pos end
34
+ :: next_track::
35
+ end
36
+ if last_end > 0 then reaper .SetEditCurPos (last_end , true , false ) end
37
+ end
38
+
39
+ function actions .prevItemStart ()
40
+ local cur , start = reaper .GetCursorPosition (), - 1
41
+ for i = 0 , reaper .CountSelectedTracks () - 1 do
42
+ local track = reaper .GetSelectedTrack (0 , i )
43
+ for j = 0 , reaper .GetTrackNumMediaItems (track ) - 1 do
44
+ local item = reaper .GetTrackMediaItem (track , j )
33
45
local pos = reaper .GetMediaItemInfo_Value (item , " D_POSITION" )
34
- + reaper .GetMediaItemInfo_Value (item , " D_LENGTH" )
35
- if not last_end or pos > last_end then last_end = pos end
46
+ if pos >= cur then goto next_track elseif pos > start then start = pos end
36
47
end
48
+ :: next_track::
37
49
end
38
- if last_end then reaper .SetEditCurPos (last_end , true , false ) end
50
+ if start > - 1 then reaper .SetEditCurPos (start , true , false ) end
39
51
end
40
52
41
- local function moveToPrevItemStart (item_positions )
53
+ local function nextItem (add_length )
54
+ local cur = reaper .GetCursorPosition ()
55
+ local len = reaper .GetProjectLength (0 )
56
+ local start = len
57
+ local item = nil
58
+ for i = 0 , reaper .CountSelectedTracks () - 1 do
59
+ local track = reaper .GetSelectedTrack (0 , i )
60
+ for j = 0 , reaper .GetTrackNumMediaItems (track ) - 1 do
61
+ item = reaper .GetTrackMediaItem (track , j )
62
+ local pos = reaper .GetMediaItemInfo_Value (item , " D_POSITION" )
63
+ if pos > cur and pos < start then start = pos end
64
+ if pos > cur then goto next_track end
65
+ end
66
+ :: next_track::
67
+ end
68
+ if add_length then start = start + reaper .GetMediaItemInfo_Value (item , " D_LENGTH" ) end
69
+ if start < len then reaper .SetEditCurPos (start , true , false ) end
70
+ end
71
+
72
+ function actions .nextItemStart () nextItem (false ) end
73
+
74
+ function actions .nextItemEnd () nextItem (true ) end
75
+
76
+ function actions .prevBigItemStart ()
77
+ local item_positions = utils .getBigItemPositionsOnSelectedTracks ()
42
78
local pos = reaper .GetCursorPosition ()
43
79
local next_position = nil
44
80
for i , item in pairs (item_positions ) do
@@ -60,15 +96,8 @@ local function moveToPrevItemStart(item_positions)
60
96
if next_position then reaper .SetEditCurPos (next_position , true , false ) end
61
97
end
62
98
63
- function actions .prevBigItemStart ()
64
- moveToPrevItemStart (utils .getBigItemPositionsOnSelectedTracks ())
65
- end
66
-
67
- function actions .prevItemStart ()
68
- moveToPrevItemStart (utils .getItemPositionsOnSelectedTracks ())
69
- end
70
-
71
- local function moveToNextItemStart (item_positions )
99
+ function actions .nextBigItemStart ()
100
+ local item_positions = utils .getBigItemPositionsOnSelectedTracks ()
72
101
local pos = reaper .GetCursorPosition ()
73
102
local next_position = nil
74
103
for _ , item_position in pairs (item_positions ) do
@@ -82,15 +111,8 @@ local function moveToNextItemStart(item_positions)
82
111
if next_position then reaper .SetEditCurPos (next_position , true , false ) end
83
112
end
84
113
85
- function actions .nextBigItemStart ()
86
- moveToNextItemStart (utils .getBigItemPositionsOnSelectedTracks ())
87
- end
88
-
89
- function actions .nextItemStart ()
90
- moveToNextItemStart (utils .getItemPositionsOnSelectedTracks ())
91
- end
92
-
93
- local function moveToNextItemEnd (item_positions )
114
+ function actions .nextBigItemEnd ()
115
+ local item_positions = utils .getBigItemPositionsOnSelectedTracks ()
94
116
local current_position = reaper .GetCursorPosition ()
95
117
local next_position = nil
96
118
local tolerance = .002
@@ -106,31 +128,20 @@ local function moveToNextItemEnd(item_positions)
106
128
end
107
129
end
108
130
109
- function actions .nextBigItemEnd ()
110
- moveToNextItemEnd (utils .getBigItemPositionsOnSelectedTracks ())
111
- end
112
-
113
- function actions .nextItemEnd ()
114
- moveToNextItemEnd (utils .getItemPositionsOnSelectedTracks ())
115
- end
116
-
117
131
function actions .firstTrack ()
118
132
local track = reaper .GetTrack (0 , 0 )
119
133
if track then reaper .SetOnlyTrackSelected (track ) end
120
134
end
121
135
122
136
function actions .lastTrack ()
123
137
local num = reaper .GetNumTracks ()
124
- if num == 0 then return end
125
- local track = reaper .GetTrack (0 , num - 1 )
126
- reaper .SetOnlyTrackSelected (track )
138
+ if num ~= 0 then reaper .SetOnlyTrackSelected (reaper .GetTrack (0 , num - 1 )) end
127
139
end
128
140
129
141
function actions .trackWithNumber ()
130
- local _ , number = reaper .GetUserInputs (" Match Forward" , 1 , " Track Number" , " " )
131
- if type (number ) ~= ' number' then return end
132
-
133
- local track = reaper .GetTrack (0 , number - 1 )
142
+ local ok , num = reaper .GetUserInputs (" Match Forward" , 1 , " Track Number" , " " )
143
+ if not ok or type (num ) ~= ' number' then return end
144
+ local track = reaper .GetTrack (0 , num - 1 )
134
145
if track then reaper .SetOnlyTrackSelected (track ) end
135
146
end
136
147
@@ -152,8 +163,8 @@ function actions.snap()
152
163
end
153
164
154
165
function actions .innerProjectTimeline ()
155
- local project_end = reaper .GetProjectLength (0 )
156
- reaper .GetSet_LoopTimeRange (true , false , 0 , project_end , false )
166
+ local len = reaper .GetProjectLength (0 )
167
+ reaper .GetSet_LoopTimeRange (true , false , 0 , len , false )
157
168
end
158
169
159
170
function actions .innerItem ()
@@ -182,15 +193,13 @@ end
182
193
183
194
function actions .onlyCurrentTrack ()
184
195
local track = reaper .GetSelectedTrack (0 , 0 )
185
- if track then
186
- reaper .SetOnlyTrackSelected (track )
187
- end
196
+ if track then reaper .SetOnlyTrackSelected (track ) end
188
197
end
189
198
190
199
function actions .innerRegion ()
191
- local pos = reaper .GetCursorPosition ()
192
- local _ , region_id = reaper .GetLastMarkerAndCurRegion ( 0 , pos )
193
- utils . selectRegion ( region_id )
200
+ local _ , region_id = reaper .GetLastMarkerAndCurRegion ( 0 , reaper . GetCursorPosition () )
201
+ local ok , is_region , start_pos , end_pos , _ , _ = reaper .EnumProjectMarkers ( region_id )
202
+ if ok and is_region then reaper . GetSet_LoopTimeRange ( true , false , start_pos , end_pos , false ) end
194
203
end
195
204
196
205
function actions .clearTimeSelection ()
@@ -203,14 +212,10 @@ local function getUserGridDivisionInput()
203
212
if not ok then return end
204
213
local division = str :match (" [0-9.]+" )
205
214
local fraction = str :match (" /([0-9.]+)" )
206
- if division and fraction then
207
- return division / fraction
208
- elseif division then
209
- return division
210
- else
211
- reaper .MB (" Could not parse specified grid division" , " Error" , 0 )
212
- return nil
213
- end
215
+ if division and fraction then return division / fraction end
216
+ if division then return division end
217
+ reaper .MB (" Could not parse specified grid division " .. str , " Error" , 0 )
218
+ return nil
214
219
end
215
220
216
221
function actions .setMidiGridDivision ()
231
236
-- this one avoids splitting all items across tracks in time selection, if no items are selected
232
237
function actions .splitItemsAtTimeSelection ()
233
238
if reaper .CountSelectedMediaItems (0 ) == 0 then return end
234
- local SplitAtTimeSelection = 40061
235
- reaper .Main_OnCommand (SplitAtTimeSelection , 0 )
239
+ reaper .Main_OnCommand (40061 , 0 ) -- split at time selection
236
240
end
237
241
238
242
return actions
0 commit comments