vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

commit f72776eb6f43e2316121094b2760502d1a8d50ac
parent 541461cbd61bd7e5f2bd8cf7fc664c12ded28840
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Tue, 30 Sep 2014 19:57:35 +0200

Clean up visual mode handling

Diffstat:
Mconfig.def.h | 10++++++----
Mvis.c | 7++++---
2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -427,12 +427,12 @@ static KeyBinding vis_mode_visual[] = { }; static void vis_mode_visual_enter(Mode *old) { - if (old != &vis_modes[VIS_MODE_VISUAL_LINE]) + if (!old->visual) window_selection_start(vis->win->win); } static void vis_mode_visual_leave(Mode *new) { - if (new != &vis_modes[VIS_MODE_VISUAL_LINE]) + if (!new->visual) window_selection_clear(vis->win->win); } @@ -445,13 +445,13 @@ static KeyBinding vis_mode_visual_line[] = { static void vis_mode_visual_line_enter(Mode *old) { Win *win = vis->win->win; window_cursor_to(win, text_line_begin(vis->win->text, window_cursor_get(win))); - if (old != &vis_modes[VIS_MODE_VISUAL]) + if (!old->visual) window_selection_start(vis->win->win); movement(&(const Arg){ .i = MOVE_LINE_END }); } static void vis_mode_visual_line_leave(Mode *new) { - if (new != &vis_modes[VIS_MODE_VISUAL]) + if (!new->visual) window_selection_clear(vis->win->win); } @@ -676,6 +676,7 @@ static Mode vis_modes[] = { .bindings = vis_mode_visual, .enter = vis_mode_visual_enter, .leave = vis_mode_visual_leave, + .visual = true, }, [VIS_MODE_VISUAL_LINE] = { .name = "--VISUAL LINE--", @@ -683,6 +684,7 @@ static Mode vis_modes[] = { .bindings = vis_mode_visual_line, .enter = vis_mode_visual_line_enter, .leave = vis_mode_visual_line_leave, + .visual = true, }, [VIS_MODE_READLINE] = { .name = "READLINE", diff --git a/vis.c b/vis.c @@ -76,6 +76,7 @@ struct Mode { void (*input)(const char*, size_t); /* called whenever a key is not found in this mode and all its parent modes */ void (*idle)(void); /* called whenever a certain idle time i.e. without any user input elapsed */ time_t idle_timeout; /* idle time in seconds after which the registered function will be called */ + bool visual; /* whether text selection is possible in this mode */ }; typedef struct { @@ -809,7 +810,7 @@ static void linewise(const Arg *arg) { static void operator(const Arg *arg) { Operator *op = &ops[arg->i]; - if (mode == &vis_modes[VIS_MODE_VISUAL] || mode == &vis_modes[VIS_MODE_VISUAL_LINE]) { + if (mode->visual) { action.op = op; action_do(&action); return; @@ -1152,7 +1153,7 @@ static void action_do(Action *a) { } } } - } else if (mode == &vis_modes[VIS_MODE_VISUAL] || mode == &vis_modes[VIS_MODE_VISUAL_LINE]) { + } else if (mode->visual) { c.range = window_selection_get(win); if (!text_range_valid(&c.range)) c.range.start = c.range.end = pos; @@ -1162,7 +1163,7 @@ static void action_do(Action *a) { a->op->func(&c); if (mode == &vis_modes[VIS_MODE_OPERATOR]) switchmode_to(mode_prev); - else if (mode == &vis_modes[VIS_MODE_VISUAL] || mode == &vis_modes[VIS_MODE_VISUAL_LINE]) + else if (mode->visual) switchmode(&(const Arg){ .i = VIS_MODE_NORMAL }); text_snapshot(txt); }