From 6e476859c06b0f41e23f6f93befdeb3f4d72ac27 Mon Sep 17 00:00:00 2001
From: garret <garret@airmail.cc>
Date: Sun, 30 Oct 2022 13:11:05 +0000
Subject: consistency assistant: add includes

holy recursion
also, "cleaned" up a bit
---
 macros/garret.ctrl-c-ctrl-v.lua | 82 ++++++++++++++++++++++++++++++++---------
 1 file changed, 65 insertions(+), 17 deletions(-)

diff --git a/macros/garret.ctrl-c-ctrl-v.lua b/macros/garret.ctrl-c-ctrl-v.lua
index 28902ce..04c994d 100644
--- a/macros/garret.ctrl-c-ctrl-v.lua
+++ b/macros/garret.ctrl-c-ctrl-v.lua
@@ -1,37 +1,85 @@
-script_name = "consistency assistant"
-script_description = "ctrl-c ctrl-v"
-script_version = "1.1.0"
+script_name = "Consistency Assistant"
+script_description = "ctrl-c ctrl-v and now ctrl-o"
+script_version = "1.2.0"
 script_author = "garret"
 script_namespace = "garret.ctrl-c-ctrl-v"
 
 local haveDepCtrl, DependencyControl, depctrl = pcall(require, "l0.DependencyControl")
-local util
+local parser
 if haveDepCtrl then
 	depctrl = DependencyControl({
 		--feed="TODO",
+		{
+			{
+				"myaa.ASSParser",
+				version = "0.0.4", -- very sketch about this
+				url = "http://github.com/TypesettingTools/Myaamori-Aegisub-Scripts",
+				feed = "https://raw.githubusercontent.com/TypesettingTools/Myaamori-Aegisub-Scripts/master/DependencyControl.json",
+				optional = true,
+			},
+		},
 	})
+	parser = depctrl:requireModules()
 end
 
-local function main(sub, sel)
-	local src = {}
+--inspect = require "inspect"
+
+-- it looks more complicated than it is because of all the aegisub.log()s
+local function main(sub, is_recursion)
+	local clipboard = {}
 	for i = 1, #sub do
 		local line = sub[i]
 		if line.class == "dialogue" then
-			if line.comment ~= true then
-				local copy_name = line.effect:match("ctrl%-c ?([%S]*)")
-				local paste_name = line.effect:match("ctrl%-v ?([%S]*)")
-				if copy_name ~= nil then
-					aegisub.log(5, "ctrl-c " .. copy_name .. ": " .. line.text .. "\n")
-					src[copy_name] = src[copy_name] or {}
-					table.insert(src[copy_name], line)
-				elseif paste_name ~= nil then
-					aegisub.log(5, "ctrl-v " .. paste_name .. ": " .. line.text .. "\n")
-					line.text = src[paste_name][1].text
+			local op, name = line.effect:match("ctrl%-([cvo]) ?([%S]*)")
+			if op == "o" and parser ~= "nil" then -- o = open. check for parser or it wont work
+				clipboard["__imports"] = clipboard["__imports"] or {} -- declare field
+				clipboard["__imports"][name] = import(line.text)
+			elseif line.comment ~= true then
+				if op == "c" then
+					aegisub.log(5, 5, "ctrl-c " .. name .. ": " .. line.text .. "\n")
+					clipboard[name] = clipboard[name] or {}
+					table.insert(clipboard[name], line)
+				elseif op == "v" and is_recursion ~= true then -- don't want to affect the imported files (useless)
+				-- have to specifically check ~= true because normally it'd be sel, which is a table of numbers (not nil/false)
+					local file, slash, capture = name:match("([^%s/]+)(/?)([%S]*)") -- match filename/capture
+					local clippings													-- names = anything not a space or a /
+					aegisub.log(5, file.."/"..capture.."\n")
+					aegisub.log(5, "slash: "..slash.."\n")
+					if slash ~= "" and parser ~= nil then
+						clippings = clipboard["__imports"][file][capture]
+						aegisub.log(5, "clippings = clipboard[\"__imports\"][\""..file.."\"][\""..capture.."\"]\n")
+					else
+						clippings = clipboard[file] or {line} -- error handling if the clipboard doesnt exist or parser isnt there
+						aegisub.log(5, "clippings = clipboard[\""..file.."\"]\n")
+					end
+					aegisub.log(5, "ctrl-v " .. name .. ": " .. line.text .. "\n")
+--					aegisub.log(5, inspect(clippings).."\n")
+					line.text = clippings[1].text
 					sub[i] = line
-					table.remove(src[paste_name], 1)
+					table.remove(clippings, 1)
 				end
 			end
 		end
+	aegisub.progress.set(i / #sub * 100)
+	end
+--	aegisub.log(5, inspect(clipboard).."\n")
+	return clipboard
+end
+
+function import(path)
+--local function import(path)
+	if parser then
+		aegisub.log(5, "recursing\n")
+		local f = io.open(aegisub.decode_path(path), "r")
+		local sub = parser.parse_file(f).events
+		f:close()
+		local clipboard = main(sub, true)
+		aegisub.log(5, "recursion complete\n")
+		return clipboard
+	else
+		aegisub.log(1, "You don't appear to have myaa.ASSParser, which is required for including other files.\n")
+		aegisub.log(1, "The script will continue, but any includes you've specified won't work.\n")
+		return {}
 	end
 end
 
-- 
cgit v1.2.3-70-g09d2