summaryrefslogtreecommitdiff
path: root/macro.c
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2023-03-23 13:55:50 -0400
committerpommicket <pommicket@gmail.com>2023-03-23 13:55:50 -0400
commiteb088ebd059ba00735723fb394d3bfe1b3530026 (patch)
treed7bfedce0a1f859ba89525afe1ae6a1afce427ae /macro.c
parent63f54d13aa7233986f784ef857faef259a889ed6 (diff)
get rid of ARG_STRING, start macros
Diffstat (limited to 'macro.c')
-rw-r--r--macro.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/macro.c b/macro.c
new file mode 100644
index 0000000..81f1898
--- /dev/null
+++ b/macro.c
@@ -0,0 +1,51 @@
+#include "ted.h"
+
+void macro_start_recording(Ted *ted, u32 index) {
+ if (index >= TED_MACRO_MAX) return;
+ if (ted->executing_macro) return;
+ ted->recording_macro = &ted->macros[index];
+}
+
+void macro_stop_recording(Ted *ted) {
+ ted->recording_macro = NULL;
+}
+
+void macro_add(Ted *ted, Command command, CommandArgument argument) {
+ if (!ted->recording_macro) return;
+ if (command == CMD_MACRO_EXECUTE || command == CMD_MACRO_RECORD || command == CMD_MACRO_STOP)
+ return;
+ if (argument.string)
+ argument.string = str_dup(argument.string);
+ Action action = {
+ .command = command,
+ .argument = argument
+ };
+ arr_add(ted->recording_macro->actions, action);
+}
+
+void macro_execute(Ted *ted, u32 index) {
+ if (index >= TED_MACRO_MAX) return;
+ Macro *macro = &ted->macros[index];
+ if (ted->recording_macro == macro) {
+ // don't allow running a macro while it's being recorded
+ return;
+ }
+
+ ted->executing_macro = true;
+ CommandContext context = {0};
+ context.from_macro = true;
+ arr_foreach_ptr(macro->actions, Action, act) {
+ command_execute_ex(ted, act->command, act->argument, context);
+ }
+ ted->executing_macro = false;
+}
+
+void macros_free(Ted *ted) {
+ for (int i = 0; i < TED_MACRO_MAX; ++i) {
+ Macro *macro = &ted->macros[i];
+ arr_foreach_ptr(macro->actions, Action, act) {
+ free((char *)act->argument.string);
+ }
+ arr_free(macro->actions);
+ }
+}