From b2f27bd080dd52f1a4e93019701ad4254f5d6176 Mon Sep 17 00:00:00 2001 From: Akatsumekusa Date: Fri, 13 Jan 2023 11:56:34 +0000 Subject: dupe and comment: fix sel and act selected lines and active line will now be properly set after do and undo removed aegisub.util from requiredModules --- macros/garret.dupe-and-comment.lua | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'macros') diff --git a/macros/garret.dupe-and-comment.lua b/macros/garret.dupe-and-comment.lua index a434712..f8e4b4b 100644 --- a/macros/garret.dupe-and-comment.lua +++ b/macros/garret.dupe-and-comment.lua @@ -5,29 +5,25 @@ script_version = "2.1.3" script_namespace = "garret.dupe-and-comment" local haveDepCtrl, DependencyControl, depctrl = pcall(require, "l0.DependencyControl") -local util if haveDepCtrl then depctrl = DependencyControl { --feed="TODO", - {"aegisub.util"} } - util = depctrl:requireModules() -else - util = require 'aegisub.util' end -local function comment(subs, sel) +local function comment(subs, sel, act) for i=#sel,1,-1 do local line=subs[sel[i]] - local original = util.copy(line) - line.comment = false -- going to edit it, so we probably want to see it on the video - original.comment = true -- this is the actual original one - subs.insert(sel[i]+1, original) -- putting it on the next line so i don't have to change line + line.comment = true + subs.insert(sel[i]+1, line) + for j=i+1,#sel do sel[j] = sel[j] + 1 end + if act > sel[i] then act = act + 1 end end aegisub.set_undo_point(script_name) + return sel, act end -local function undo(subs, sel) +local function undo(subs, sel, act) for i=#sel,1,-1 do local edit=subs[sel[i]] if not (sel[i] + 1 > #subs) then @@ -36,10 +32,13 @@ local function undo(subs, sel) 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 end end end aegisub.set_undo_point("Undo "..script_name) + return sel, act end local macros = { -- cgit v1.2.3-70-g09d2 From 96bd8086c75576cfd77d83e6122428490869740b Mon Sep 17 00:00:00 2001 From: garret Date: Sun, 15 Jan 2023 00:05:27 +0000 Subject: dupe and comment: format and comment new "do" implementation i find it easier to read when the different parts are separated, and while i have done one-line `if this then do that end`s before, i've only ever done it for short operations where it's immediately obvious what's going on and you don't need to do any thinking whatsoever. e.g. this function from depctrl config: local function get_log_level(num) if num == 0 then return "0: Fatal" elseif num == 1 then return "1: Error" elseif num == 2 then return "2: Warning" elseif num == 3 then return "3: Hint" elseif num == 4 then return "4: Debug" elseif num == 5 then return "5: Trace" end return nil end I don't think the bits that fix sel and act count, at least not without comments. speaking of comments, i've added some. i hope i've understood what's going on correctly, please feel free to correct me if not. --- macros/garret.dupe-and-comment.lua | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'macros') diff --git a/macros/garret.dupe-and-comment.lua b/macros/garret.dupe-and-comment.lua index f8e4b4b..f819d40 100644 --- a/macros/garret.dupe-and-comment.lua +++ b/macros/garret.dupe-and-comment.lua @@ -13,11 +13,20 @@ end local function comment(subs, sel, act) for i=#sel,1,-1 do - local line=subs[sel[i]] - line.comment = true - subs.insert(sel[i]+1, line) - for j=i+1,#sel do sel[j] = sel[j] + 1 end - if act > sel[i] then act = act + 1 end + local line=subs[sel[i]] -- grab copy of current line + + -- now use that copy for a different line + line.comment = true -- comment out the new dupe line + subs.insert(sel[i]+1, line) -- and put it below + + -- sort out selection + for j=i+1,#sel do + sel[j] = sel[j] + 1 -- bump all of sel by 1 to compensate for new line + end -- first item isnt included because it's not affected + + if act > sel[i] then -- if we've not got to the active line yet + act = act + 1 -- bump by 1 to compensate for new lines above + end end aegisub.set_undo_point(script_name) return sel, act -- cgit v1.2.3-70-g09d2 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(-) (limited to 'macros') 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 From e816a3888b3a5681e6e66a731ffed6144b864d7c Mon Sep 17 00:00:00 2001 From: garret Date: Sun, 15 Jan 2023 13:41:20 +0000 Subject: dupe and comment: chore: bump version --- macros/garret.dupe-and-comment.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'macros') diff --git a/macros/garret.dupe-and-comment.lua b/macros/garret.dupe-and-comment.lua index f736df9..14a660c 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 = "2.1.3" +script_version = "3.0.0" script_namespace = "garret.dupe-and-comment" local haveDepCtrl, DependencyControl, depctrl = pcall(require, "l0.DependencyControl") -- cgit v1.2.3-70-g09d2