Keep the edit cursor when opening an item in grid mode

Opening an item in the MIDI editor parks the edit cursor at the item
start, and the open action then made that stick. Listening to a phrase
and hitting Alt+Shift+G to write on top of it meant arrowing back to
where you already were, one cell at a time, on every entry.

The cursor now stays where it was and is only tidied onto the nearest
grid line, so the first left/right press moves a whole cell rather than
a remainder. The action reads the cursor before the open command runs,
since the open command is what moves it.

snapCursorToCell rounds in quarter notes, like everything else here, so
the landing spot survives a tempo change, and clamps the cell index into
the item: the floor is the cell containing the item start, which is
where the action used to land, and the ceiling is the last cell that
starts inside the item. Clamping the index rather than the time means a
cursor parked before an off-grid item start rounds forward into the
first whole cell instead of into the partial one, where a note would
begin outside the item.

The action also re-reads the take from the editor handle now, so the
grid size and the spoken position come from the take actually being
edited rather than from the selected item's active take.

Toggling grid mode on with Alt+G is left alone: it does not move the
cursor at all, and cell() floors, so an off-grid cursor still works.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-24 01:21:21 +02:00
co-authored by Claude Opus 5
parent c55ab24ae4
commit 0f06554a18
4 changed files with 80 additions and 4 deletions
+35
View File
@@ -304,4 +304,39 @@ eq(row, 67, "any back landed nearest the pitch it came from")
said = at(6, 69, function() G.runNoteJump(1) end)
eq(said, "No later notes", "any forward past the last note")
--------------------------------------------------- entering grid mode
-- Opening an item in grid mode keeps the cursor where it already was, rounded
-- to the nearest cell, instead of dropping back to the item start.
local itemPos, itemLen = 0, 10
reaper.GetMediaItemTake_Item = function() return "item" end
reaper.GetMediaItemInfo_Value = function(_, k)
return k == "D_POSITION" and itemPos or itemLen
end
local function snap(t) return G.snapCursorToCell(take, t) end
eq(snap(2.4), 2, "snap rounds back to the nearer cell")
eq(snap(2.6), 3, "snap rounds forward to the nearer cell")
eq(snap(3), 3, "snap leaves a cursor already on a cell alone")
eq(cursor, 3, "snap moves the edit cursor to the cell start")
cursor = 6.7
eq(snap(nil), 7, "snap defaults to the edit cursor")
-- A cursor outside the item lands on the item's first or last cell.
itemPos, itemLen = 2, 4 -- the item spans cells 2 to 5
eq(snap(0), 2, "snap from before the item lands on its first cell")
eq(snap(99), 5, "snap from past the item lands on its last cell")
eq(snap(6), 5, "snap at the item end lands on the last cell inside it")
-- Cells stay aligned to the timeline, not the item, so an item that starts
-- off-grid still uses the cell that contains its start as the floor.
itemPos, itemLen = 2.5, 4
eq(snap(0), 2, "snap floors to the cell containing an off-grid item start")
-- The rounding follows the grid size, not a fixed quarter note.
grid, itemPos, itemLen = 2, 0, 10
eq(snap(2.9), 1, "snap rounds to half-note cells on a half-note grid")
eq(snap(3.1), 2, "snap rounds up to half-note cells on a half-note grid")
eq(cursor, 4, "half-note cell 2 starts at QN 4")
print(fails == 0 and "ALL PASS" or (fails .. " FAILURES"))