From e96933c17a25d6a4a90e35d68ab3d2f40a4ec8f9 Mon Sep 17 00:00:00 2001 From: garret Date: Thu, 16 Feb 2023 02:23:58 +0000 Subject: dupe and comment: handle fold boundaries --- macros/garret.dupe-and-comment.lua | 51 +++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 9 deletions(-) (limited to 'macros/garret.dupe-and-comment.lua') diff --git a/macros/garret.dupe-and-comment.lua b/macros/garret.dupe-and-comment.lua index 19f97a0..e89d774 100644 --- a/macros/garret.dupe-and-comment.lua +++ b/macros/garret.dupe-and-comment.lua @@ -1,7 +1,7 @@ script_name="Dupe and Comment" script_description="Copies a line and comments out the original.\nbecause i like seeing the original while editing, and being able to go back to it easily" script_author = "garret" -script_version = "3.0.2" +script_version = "4.0.0" script_namespace = "garret.dupe-and-comment" local haveDepCtrl, DependencyControl, depctrl = pcall(require, "l0.DependencyControl") @@ -11,23 +11,48 @@ if haveDepCtrl then } end +local function strnumtobool(i) if i == "0" then return false else return true end end + +local function find_fold_boundary(line) + local fold = line.extra["_aegi_folddata"] + + if fold then -- we are indeed at a fold boundary + -- now work out which one + local at_fold_end = strnumtobool(string.sub(fold, 1, 1)) + -- first char is a bool, 0 = at start, 1 = at end + return at_fold_end + end +end + local function comment(subs, sel, act) for i=#sel,1,-1 do local edit=subs[sel[i]] -- current line local original=subs[sel[i]] -- and a copy of it - -- now use that copy for a different line + -- deal with being at the start/end of a fold + local at_fold_end = find_fold_boundary(edit) + + if at_fold_end then + edit.extra["_aegi_folddata"] = nil -- remove fold data from the edit + -- but not from the dupe, so effectively the end of the fold gets moved down one + elseif at_fold_end == false then -- if at fold start + original.extra["_aegi_folddata"] = nil -- ditto ^, but from the original + -- so we don't have loads of extra fold starts that might interfere later + end + + subs[sel[i]] = edit + + -- now use that copy we made to make a different line original.comment = true -- comment out the new dupe line subs.insert(sel[i]+1, original) -- and put it below --- if #sel > 1 then - -- sort out sel/act - local preceding_lines = i - 1 - local on_act = act == sel[i] + -- sort out sel/act + local preceding_lines = i - 1 + local on_act = act == sel[i] + + sel[i] = sel[i] + preceding_lines + if on_act then act = sel[i] end - sel[i] = sel[i] + preceding_lines - if on_act then act = sel[i] end --- end end aegisub.set_undo_point(script_name) return sel, act @@ -41,6 +66,14 @@ local function undo(subs, sel, act) if edit.comment == false and original.comment == true then original.comment = false + -- deal with being at the start/end of a fold + + local at_fold_end = find_fold_boundary(edit) + if at_fold_end == false then -- that is, if we're at the start + original.extra["_aegi_folddata"] = edit.extra["_aegi_folddata"] + -- preserve the original fold boundary so the fold doesnt magically disappear + end + subs[sel[i]+1] = original subs.delete(sel[i]) -- cgit v1.2.3-70-g09d2