From 5459e401b33de9b385af48f870e6ff4fe45c4c8b Mon Sep 17 00:00:00 2001 From: garret Date: Sun, 15 Jan 2023 01:19:53 +0000 Subject: dupe and comment: add notes about what's going on in undo function not as thorough as `do` because most of `undo` is pretty clear on its own, and all of the "magic" bits have been explained in `do`. The only difference is that here it's the other way round. --- macros/garret.dupe-and-comment.lua | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/macros/garret.dupe-and-comment.lua b/macros/garret.dupe-and-comment.lua index f819d40..f736df9 100644 --- a/macros/garret.dupe-and-comment.lua +++ b/macros/garret.dupe-and-comment.lua @@ -35,14 +35,22 @@ end local function undo(subs, sel, act) for i=#sel,1,-1 do local edit=subs[sel[i]] - if not (sel[i] + 1 > #subs) then + if not (sel[i] + 1 > #subs) then -- preventing out-of-range errors local original=subs[sel[i]+1] + if edit.comment == false and original.comment == true then original.comment = false subs[sel[i]+1] = original subs.delete(sel[i]) - for j=i+1,#sel do sel[j] = sel[j] - 1 end - if act > sel[i] then act = act - 1 end + + -- sort out selection. same as `do`, but the other way round. + for j=i+1,#sel do + sel[j] = sel[j] - 1 + end + if act > sel[i] then + act = act - 1 + end + end end end -- cgit v1.2.3-70-g09d2