summaryrefslogtreecommitdiff
path: root/macro.c
diff options
context:
space:
mode:
Diffstat (limited to 'macro.c')
-rw-r--r--macro.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/macro.c b/macro.c
new file mode 100644
index 0000000..14ec016
--- /dev/null
+++ b/macro.c
@@ -0,0 +1,65 @@
+#include "ted.h"
+
+static void macro_clear(Macro *macro) {
+ arr_foreach_ptr(macro->actions, Action, act) {
+ free((char *)act->argument.string);
+ }
+ arr_free(macro->actions);
+
+ memset(macro, 0, sizeof *macro);
+}
+
+void macro_start_recording(Ted *ted, u32 index) {
+ if (index >= TED_MACRO_MAX) return;
+ if (ted->executing_macro) return;
+ if (ted->recording_macro) {
+ macro_stop_recording(ted);
+ return;
+ }
+
+ command_execute(ted, CMD_CLEAR_SELECTION, 0);
+
+ ted->recording_macro = &ted->macros[index];
+ macro_clear(ted->recording_macro);
+}
+
+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];
+ macro_clear(macro);
+ }
+}