summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--command.c23
-rw-r--r--macro.c14
-rw-r--r--main.c11
-rw-r--r--ted.c4
-rw-r--r--ted.h8
5 files changed, 33 insertions, 27 deletions
diff --git a/command.c b/command.c
index cce923d..c09e0fd 100644
--- a/command.c
+++ b/command.c
@@ -145,21 +145,32 @@ const char *command_to_str(Command c) {
}
void command_execute(Ted *ted, Command c, i64 argument) {
- CommandArgument arg = {
+ const CommandArgument arg = {
.number = argument,
.string = NULL,
};
- command_execute_ex(ted, c, arg, (CommandContext){0});
+ const CommandContext ctx = {0};
+ command_execute_ex(ted, c, &arg, &ctx);
}
-void command_execute_ex(Ted *ted, Command c, CommandArgument full_argument, CommandContext context) {
+void command_execute_string_argument(Ted *ted, Command c, const char *string) {
+ assert(string);
+ const CommandArgument arg = {
+ .number = 0,
+ .string = string,
+ };
+ const CommandContext ctx = {0};
+ command_execute_ex(ted, c, &arg, &ctx);
+}
+
+void command_execute_ex(Ted *ted, Command c, const CommandArgument *full_argument, const CommandContext *context) {
TextBuffer *buffer = ted->active_buffer;
Node *node = ted->active_node;
Settings *settings = ted_active_settings(ted);
if (ted->recording_macro)
macro_add(ted, c, full_argument);
- i64 argument = full_argument.number;
- const char *argument_str = full_argument.string;
+ i64 argument = full_argument->number;
+ const char *argument_str = full_argument->string;
/*
it's important that when we're playing back a macro,
we only execute commands specifically from the macro.
@@ -171,7 +182,7 @@ void command_execute_ex(Ted *ted, Command c, CommandArgument full_argument, Comm
find_next("apple") // (generated by find.c)
if we ran these commands as-is, we'd end up searching for "apple" twice!
*/
- if (ted->executing_macro && !context.from_macro)
+ if (ted->executing_macro && !context->running_macro)
return;
switch (c) {
diff --git a/macro.c b/macro.c
index 14ec016..19ebcd8 100644
--- a/macro.c
+++ b/macro.c
@@ -27,15 +27,16 @@ void macro_stop_recording(Ted *ted) {
ted->recording_macro = NULL;
}
-void macro_add(Ted *ted, Command command, CommandArgument argument) {
+void macro_add(Ted *ted, Command command, const 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);
+ CommandArgument arg = *argument;
+ if (arg.string)
+ arg.string = str_dup(arg.string);
Action action = {
.command = command,
- .argument = argument
+ .argument = arg
};
arr_add(ted->recording_macro->actions, action);
}
@@ -49,10 +50,9 @@ void macro_execute(Ted *ted, u32 index) {
}
ted->executing_macro = true;
- CommandContext context = {0};
- context.from_macro = true;
+ const CommandContext context = {.running_macro = true};
arr_foreach_ptr(macro->actions, Action, act) {
- command_execute_ex(ted, act->command, act->argument, context);
+ command_execute_ex(ted, act->command, &act->argument, &context);
}
ted->executing_macro = false;
}
diff --git a/main.c b/main.c
index 4547a54..1a53346 100644
--- a/main.c
+++ b/main.c
@@ -733,19 +733,12 @@ int main(int argc, char **argv) {
ted_press_key(ted, keycode, modifier);
} break;
case SDL_TEXTINPUT: {
- char *text = event.text.text;
+ const char *text = event.text.text;
if (buffer
// unfortunately, some key combinations like ctrl+minus still register as a "-" text input event
&& (key_modifier & ~KEY_MODIFIER_SHIFT) == 0) {
// insert the text
- {
- CommandContext ctx = {0};
- CommandArgument arg = {
- .number = 0,
- .string = text
- };
- command_execute_ex(ted, CMD_INSERT_TEXT, arg, ctx);
- }
+ command_execute_string_argument(ted, CMD_INSERT_TEXT, text);
// check for trigger character
LSP *lsp = buffer_lsp(buffer);
Settings *settings = buffer_settings(buffer);
diff --git a/ted.c b/ted.c
index 75713c6..7d51e04 100644
--- a/ted.c
+++ b/ted.c
@@ -733,8 +733,8 @@ void ted_press_key(Ted *ted, SDL_Keycode keycode, SDL_Keymod modifier) {
} else if (key_actions[mid].key_combo.value > key_combo.value) {
hi = mid;
} else {
- CommandContext context = {0};
- command_execute_ex(ted, key_actions[mid].command, key_actions[mid].argument, context);
+ const CommandContext context = {0};
+ command_execute_ex(ted, key_actions[mid].command, &key_actions[mid].argument, &context);
return;
}
}
diff --git a/ted.h b/ted.h
index 552d2ec..27be1b0 100644
--- a/ted.h
+++ b/ted.h
@@ -188,7 +188,8 @@ typedef struct {
} CommandArgument;
typedef struct {
- bool from_macro;
+ // did this command come from executing a macro?
+ bool running_macro;
} CommandContext;
/// Thing to do when a key combo is pressed.
@@ -1354,7 +1355,8 @@ void command_init(void);
Command command_from_str(const char *str);
const char *command_to_str(Command c);
void command_execute(Ted *ted, Command c, i64 argument);
-void command_execute_ex(Ted *ted, Command c, CommandArgument argument, CommandContext context);
+void command_execute_string_argument(Ted *ted, Command c, const char *string);
+void command_execute_ex(Ted *ted, Command c, const CommandArgument *argument, const CommandContext *context);
// === config.c ===
/// first, we read all config files, then we parse them.
@@ -1592,7 +1594,7 @@ void usages_frame(Ted *ted);
// === macro.c ===
void macro_start_recording(Ted *ted, u32 index);
void macro_stop_recording(Ted *ted);
-void macro_add(Ted *ted, Command command, CommandArgument argument);
+void macro_add(Ted *ted, Command command, const CommandArgument *argument);
void macro_execute(Ted *ted, u32 index);
void macros_free(Ted *ted);