Skip to content

Commit

Permalink
add cmd_goto
Browse files Browse the repository at this point in the history
  • Loading branch information
adsr committed Apr 14, 2023
1 parent 535dff1 commit 619ff20
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
33 changes: 33 additions & 0 deletions cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,39 @@ int cmd_prev(cmd_context_t *ctx) {
return MLE_OK;
}

// Go to nth bview
int cmd_goto(cmd_context_t *ctx) {
bview_t *bview;
int bview_num, num_bviews, bview_i;
char *bview_num_str;
char prompt[64];

num_bviews = editor_bview_edit_count(ctx->editor);
snprintf(prompt, sizeof(prompt), "goto: Buffer #? (1-%d)", num_bviews);

// Get bview_num
if (ctx->static_param) {
bview_num_str = strdup(ctx->static_param);
} else {
editor_prompt(ctx->editor, prompt, NULL, &bview_num_str);
if (!bview_num_str) return MLE_OK;
}
bview_num = atoi(bview_num_str);
free(bview_num_str);

// Check
if (bview_num < 1 || bview_num > num_bviews) return MLE_OK;

bview_i = 1;
CDL_FOREACH2(ctx->editor->all_bviews, bview, all_next) {
if (!MLE_BVIEW_IS_EDIT(bview)) continue;
if (bview_i == bview_num) return editor_set_active(ctx->editor, bview);
bview_i += 1;
}

return MLE_OK;
}

// Split a bview vertically
int cmd_split_vertical(cmd_context_t *ctx) {
bview_t *child;
Expand Down
10 changes: 10 additions & 0 deletions editor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1639,6 +1639,7 @@ static void _editor_register_cmds(editor_t *editor) {
_editor_register_cmd_fn(editor, "cmd_find_word", cmd_find_word);
_editor_register_cmd_fn(editor, "cmd_fsearch", cmd_fsearch);
_editor_register_cmd_fn(editor, "cmd_fsearch_fzy", cmd_fsearch_fzy);
_editor_register_cmd_fn(editor, "cmd_goto", cmd_goto);
_editor_register_cmd_fn(editor, "cmd_grep", cmd_grep);
_editor_register_cmd_fn(editor, "cmd_indent", cmd_indent);
_editor_register_cmd_fn(editor, "cmd_insert_data", cmd_insert_data);
Expand Down Expand Up @@ -1827,6 +1828,15 @@ static void _editor_init_kmaps(editor_t *editor) {
MLE_KBINDING_DEF("cmd_apply_macro_by", "M-m **"),
MLE_KBINDING_DEF("cmd_next", "M-n"),
MLE_KBINDING_DEF("cmd_prev", "M-p"),
MLE_KBINDING_DEF_EX("cmd_goto", "M-1", "1"),
MLE_KBINDING_DEF_EX("cmd_goto", "M-2", "2"),
MLE_KBINDING_DEF_EX("cmd_goto", "M-3", "3"),
MLE_KBINDING_DEF_EX("cmd_goto", "M-4", "4"),
MLE_KBINDING_DEF_EX("cmd_goto", "M-5", "5"),
MLE_KBINDING_DEF_EX("cmd_goto", "M-6", "6"),
MLE_KBINDING_DEF_EX("cmd_goto", "M-7", "7"),
MLE_KBINDING_DEF_EX("cmd_goto", "M-8", "8"),
MLE_KBINDING_DEF_EX("cmd_goto", "M-9", "9"),
MLE_KBINDING_DEF("cmd_split_vertical", "M-v"),
MLE_KBINDING_DEF("cmd_split_horizontal", "M-h"),
MLE_KBINDING_DEF("cmd_grep", "M-q"),
Expand Down
1 change: 1 addition & 0 deletions mle.h
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ int cmd_drop_sleeping_cursor(cmd_context_t *ctx);
int cmd_find_word(cmd_context_t *ctx);
int cmd_fsearch(cmd_context_t *ctx);
int cmd_fsearch_fzy(cmd_context_t *ctx);
int cmd_goto(cmd_context_t *ctx);
int cmd_grep(cmd_context_t *ctx);
int cmd_indent(cmd_context_t *ctx);
int cmd_insert_data(cmd_context_t *ctx);
Expand Down

0 comments on commit 619ff20

Please sign in to comment.