Skip to content

Instantly share code, notes, and snippets.

@adsr
Created April 27, 2025 23:50
Show Gist options
  • Save adsr/4b124bf793c48210dec8f95e8857fbf2 to your computer and use it in GitHub Desktop.
Save adsr/4b124bf793c48210dec8f95e8857fbf2 to your computer and use it in GitHub Desktop.
diff --git a/cmd.c b/cmd.c
index 6f9f7f9..6021f1c 100644
--- a/cmd.c
+++ b/cmd.c
@@ -974,6 +974,33 @@ int cmd_close(cmd_context_t *ctx) {
return MLE_OK;
}
+// Execute command by string
+int cmd_cmd(cmd_context_t *ctx) {
+ char *cmd_str, *cmd_name, *cmd_param;
+ cmd_context_t ctx2;
+ cmd_t *cmd2 = NULL;
+
+ do {
+ editor_prompt(ctx->editor, "cmd_cmd: Command?", NULL, &cmd_str);
+ if (!cmd_str) break;
+
+ cmd_name = strtok(cmd_str, " ");
+ cmd_param = strtok(NULL, " ");
+ if (!cmd_name) break;
+
+ editor_get_cmd(ctx->editor, cmd_name, &cmd2);
+ if (!cmd2) break;
+
+ memcpy(&ctx2, ctx, sizeof(ctx2));
+ ctx2.cmd = cmd2;
+ ctx2.static_param = cmd_param;
+ ctx2.cmd->func(&ctx2);
+ } while (0);
+
+ if (cmd_str) free(cmd_str);
+ return MLE_OK;
+}
+
// Quit editor
int cmd_quit(cmd_context_t *ctx) {
bview_t *bview;
diff --git a/editor.c b/editor.c
index dc7cd55..b369089 100644
--- a/editor.c
+++ b/editor.c
@@ -1093,6 +1093,14 @@ int editor_input_to_key(kinput_t *input, char *keyname) {
return _editor_event_to_key(&ev, keyname);
}
+// Get command by name
+int editor_get_cmd(editor_t *editor, char *cmd_name, cmd_t **ret_cmd) {
+ cmd_t *cmd = NULL;
+ _editor_resolve_cmd(editor, &cmd, cmd_name);
+ *ret_cmd = cmd;
+ return cmd ? MLE_OK : MLE_ERR;
+}
+
// If input == editor->macro_toggle_key, toggle macro mode and return 1. Else
// return 0.
static int _editor_maybe_toggle_macro(editor_t *editor, kinput_t *input) {
@@ -1687,6 +1695,7 @@ static void _editor_register_cmds(editor_t *editor) {
_editor_register_cmd_fn(editor, "cmd_blist", cmd_blist);
_editor_register_cmd_fn(editor, "cmd_browse", cmd_browse);
_editor_register_cmd_fn(editor, "cmd_close", cmd_close);
+ _editor_register_cmd_fn(editor, "cmd_cmd", cmd_cmd);
_editor_register_cmd_fn(editor, "cmd_copy_by", cmd_copy_by);
_editor_register_cmd_fn(editor, "cmd_copy", cmd_copy);
_editor_register_cmd_fn(editor, "cmd_ctag", cmd_ctag);
@@ -1947,6 +1956,7 @@ static void _editor_init_kmaps(editor_t *editor) {
MLE_KBINDING_DEF("cmd_jump", "M-j"),
MLE_KBINDING_DEF("cmd_close", "M-c"),
MLE_KBINDING_DEF("cmd_suspend", "f4"),
+ MLE_KBINDING_DEF("cmd_cmd", "f7"),
MLE_KBINDING_DEF("cmd_quit", "C-x"),
MLE_KBINDING_DEF(NULL, NULL)
});
diff --git a/mle.h b/mle.h
index 19335d6..eb82d7d 100644
--- a/mle.h
+++ b/mle.h
@@ -431,6 +431,7 @@ int editor_set_active(editor_t *editor, bview_t *bview);
int editor_bview_edit_count(editor_t *editor);
int editor_register_cmd(editor_t *editor, cmd_t *cmd);
int editor_register_observer(editor_t *editor, char *event_patt, void *udata, observer_func_t fn_callback, observer_t **optret_observer);
+int editor_get_cmd(editor_t *editor, char *cmd_name, cmd_t **ret_cmd);
int editor_notify_observers(editor_t *editor, char *event_name, void *event_data);
int editor_destroy_observer(editor_t *editor, observer_t *observer);
int editor_get_input(editor_t *editor, loop_context_t *loop_ctx, cmd_context_t *ctx);
@@ -499,6 +500,7 @@ int cmd_apply_macro_last(cmd_context_t *ctx);
int cmd_blist(cmd_context_t *ctx);
int cmd_browse(cmd_context_t *ctx);
int cmd_close(cmd_context_t *ctx);
+int cmd_cmd(cmd_context_t *ctx);
int cmd_copy_by(cmd_context_t *ctx);
int cmd_copy(cmd_context_t *ctx);
int cmd_ctag(cmd_context_t *ctx);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment