From 1ff146d3c256782ad976913739a3fef7b57b3fb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Fri, 24 Oct 2014 23:39:37 +0200 Subject: Make command prompt available in visual modes --- config.def.h | 7 ++++--- editor.c | 6 ++++-- editor.h | 4 ++-- vis.c | 26 +++++++++++++++++--------- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/config.def.h b/config.def.h index 629400c..0e81845 100644 --- a/config.def.h +++ b/config.def.h @@ -151,8 +151,8 @@ static KeyBinding vis_movements[] = { { { NONE('F') }, movement_key, { .i = MOVE_LEFT_TO } }, { { NONE('t') }, movement_key, { .i = MOVE_RIGHT_TILL } }, { { NONE('T') }, movement_key, { .i = MOVE_LEFT_TILL } }, - { { NONE('/') }, prompt, { .s = "/" } }, - { { NONE('?') }, prompt, { .s = "?" } }, + { { NONE('/') }, prompt_search,{ .s = "/" } }, + { { NONE('?') }, prompt_search,{ .s = "?" } }, { /* empty last element, array terminator */ }, }; @@ -401,7 +401,7 @@ static KeyBinding vis_mode_normal[] = { { { NONE('u') }, undo, { NULL } }, { { CONTROL('R') }, redo, { NULL } }, { { CONTROL('L') }, call, { .f = editor_draw } }, - { { NONE(':') }, prompt, { .s = ":" } }, + { { NONE(':') }, prompt_cmd, { .s = "" } }, { { NONE('Z'), NONE('Z') }, cmd, { .s = "wq" } }, { { NONE('Z'), NONE('Q') }, cmd, { .s = "q!" } }, { { NONE('z'), NONE('t') }, window, { .w = window_redraw_top } }, @@ -416,6 +416,7 @@ static KeyBinding vis_mode_visual[] = { { { CONTROL('c') }, switchmode, { .i = VIS_MODE_NORMAL } }, { { NONE('v') }, switchmode, { .i = VIS_MODE_NORMAL } }, { { NONE('V') }, switchmode, { .i = VIS_MODE_VISUAL_LINE } }, + { { NONE(':') }, prompt_cmd, { .s = "'<,'>" } }, { { CONTROL('H') }, operator, { .i = OP_DELETE } }, { { NONE('d') }, operator, { .i = OP_DELETE } }, { { NONE('x') }, operator, { .i = OP_DELETE } }, diff --git a/editor.c b/editor.c index 477e069..b1e42a3 100644 --- a/editor.c +++ b/editor.c @@ -511,15 +511,17 @@ static void editor_prompt_move(Prompt *prompt, int x, int y) { editor_window_move(prompt->win, x + title_width, y); } -void editor_prompt_show(Editor *ed, const char *title) { +void editor_prompt_show(Editor *ed, const char *title, const char *text) { Prompt *prompt = ed->prompt; if (prompt->active) return; prompt->active = true; prompt->editor = ed->win; - ed->win = prompt->win; free(prompt->title); prompt->title = strdup(title); + text_insert(prompt->win->text, 0, text, strlen(text)); + window_cursor_to(prompt->win->win, text_size(prompt->win->text)); + ed->win = prompt->win; editor_resize(ed, ed->width, ed->height); } diff --git a/editor.h b/editor.h index 44c2669..718ca96 100644 --- a/editor.h +++ b/editor.h @@ -150,8 +150,8 @@ void editor_window_prev(Editor*); /* rearrange all windows either vertically or horizontally */ void editor_windows_arrange_vertical(Editor*); void editor_windows_arrange_horizontal(Editor*); -/* display a user prompt with a certain title */ -void editor_prompt_show(Editor*, const char *title); +/* display a user prompt with a certain title and default text */ +void editor_prompt_show(Editor*, const char *title, const char *text); /* hide the user prompt if it is currently shown */ void editor_prompt_hide(Editor*); /* return the content of the command prompt in a malloc(3)-ed string diff --git a/vis.c b/vis.c index d42d66c..8c5391d 100644 --- a/vis.c +++ b/vis.c @@ -436,7 +436,8 @@ static void insertmode(const Arg *arg); /* insert register content indicated by arg->i at current cursor position */ static void insert_register(const Arg *arg); /* show a user prompt to get input with title arg->s */ -static void prompt(const Arg *arg); +static void prompt_search(const Arg *arg); +static void prompt_cmd(const Arg *arg); /* evaluate user input at prompt, perform search or execute a command */ static void prompt_enter(const Arg *arg); /* cycle through past user inputs */ @@ -925,13 +926,24 @@ static void insert_register(const Arg *arg) { window_cursor_to(vis->win->win, pos + reg->len); } -static void prompt(const Arg *arg) { - editor_prompt_show(vis, arg->s); +static void prompt_search(const Arg *arg) { + editor_prompt_show(vis, arg->s, ""); + switchmode(&(const Arg){ .i = VIS_MODE_PROMPT }); +} + +static void prompt_cmd(const Arg *arg) { + editor_prompt_show(vis, ":", arg->s); switchmode(&(const Arg){ .i = VIS_MODE_PROMPT }); } static void prompt_enter(const Arg *arg) { char *s = editor_prompt_get(vis); + /* it is important to switch back to the previous mode, which hides + * the prompt and more importantly resets vis->win to the currently + * focused editor window *before* anything is executed which depends + * on vis->win. + */ + switchmode_to(mode_prev); exec_command(vis->prompt->title[0], s); free(s); editor_draw(vis); @@ -1180,7 +1192,8 @@ static void switchmode_to(Mode *new_mode) { return; if (mode->leave) mode->leave(new_mode); - mode_prev = mode; + if (mode == config->mode || (mode->name && mode->name[0] == '-')) + mode_prev = mode; mode = new_mode; if (mode == config->mode || (mode->name && mode->name[0] == '-')) statusbar(vis->win); @@ -1469,11 +1482,6 @@ static bool exec_cmdline_command(char *cmdline) { } static bool exec_command(char type, char *cmd) { - /* it is important to switch to normal mode, which hides the prompt and - * more importantly resets vis->win to the currently focused editor - * window *before* anything is executed which depends on vis->win. - */ - switchmode(&(const Arg){ .i = VIS_MODE_NORMAL }); if (!cmd || !cmd[0]) return true; switch (type) { -- cgit v1.2.3