From 3b7757793a46beac6e9c31ebce595cd3c41bac77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Sat, 11 Apr 2015 12:13:09 +0200 Subject: Remove global mode state Once again show mode in window status bar. --- config.def.h | 4 ++-- editor.h | 3 +++ ui-curses.c | 2 +- vis.c | 58 ++++++++++++++++++++++++++++------------------------------ 4 files changed, 34 insertions(+), 33 deletions(-) diff --git a/config.def.h b/config.def.h index f83918e..087b678 100644 --- a/config.def.h +++ b/config.def.h @@ -224,7 +224,7 @@ static void vis_mode_operator_leave(Mode *new) { static void vis_mode_operator_input(const char *str, size_t len) { /* invalid operator */ action_reset(&vis->action); - switchmode_to(mode_prev); + switchmode_to(vis->mode_prev); } static KeyBinding vis_operator_options[] = { @@ -504,7 +504,7 @@ static void vis_mode_prompt_input(const char *str, size_t len) { static void vis_mode_prompt_enter(Mode *old) { if (old->isuser && old != &vis_modes[VIS_MODE_PROMPT]) - mode_before_prompt = old; + vis->mode_before_prompt = old; } static void vis_mode_prompt_leave(Mode *new) { diff --git a/editor.h b/editor.h index c9c3bbe..10da800 100644 --- a/editor.h +++ b/editor.h @@ -239,6 +239,9 @@ struct Editor { Action action; /* current action which is in progress */ Action action_prev; /* last operator action used by the repeat '.' key */ + Mode *mode; /* currently active mode, used to search for keybindings */ + Mode *mode_prev; /* previsouly active user mode */ + Mode *mode_before_prompt; /* user mode which was active before entering prompt */ }; Editor *editor_new(Ui*); diff --git a/ui-curses.c b/ui-curses.c index f92d787..6217da5 100644 --- a/ui-curses.c +++ b/ui-curses.c @@ -172,7 +172,7 @@ static void ui_window_draw_status(UiWin *w) { wattrset(win->winstatus, focused ? A_REVERSE|A_BOLD : A_REVERSE); mvwhline(win->winstatus, 0, 0, ' ', win->width); mvwprintw(win->winstatus, 0, 0, "%s %s %s %s", - "", // TODO mode->name && mode->name[0] == '-' ? mode->name : "", + vis->mode->name && vis->mode->name[0] == '-' ? vis->mode->name : "", filename ? filename : "[No Name]", text_modified(win->text) ? "[+]" : "", vis->recording ? "recording": ""); diff --git a/vis.c b/vis.c index 5fd0a3b..95f04ce 100644 --- a/vis.c +++ b/vis.c @@ -42,9 +42,6 @@ /** global variables */ static volatile bool running = true; /* exit main loop once this becomes false */ static Editor *vis; /* global editor instance, keeps track of all windows etc. */ -static Mode *mode; /* currently active mode, used to search for keybindings */ -static Mode *mode_prev; /* previsouly active user mode */ -static Mode *mode_before_prompt; /* user mode which was active before entering prompt */ /** operators */ static void op_change(OperatorContext *c); @@ -792,7 +789,7 @@ static void linewise(const Arg *arg) { static void operator(const Arg *arg) { Operator *op = &ops[arg->i]; - if (mode->visual) { + if (vis->mode->visual) { vis->action.op = op; action_do(&vis->action); return; @@ -947,7 +944,7 @@ static void prompt_enter(const Arg *arg) { * focused editor window *before* anything is executed which depends * on vis->win. */ - switchmode_to(mode_before_prompt); + switchmode_to(vis->mode_before_prompt); if (s && *s && exec_command(vis->prompt_type, s) && running) switchmode(&(const Arg){ .i = VIS_MODE_NORMAL }); free(s); @@ -1138,7 +1135,7 @@ static void action_do(Action *a) { c.range.end = it.pos; } } else if (a->textobj) { - if (mode->visual) + if (vis->mode->visual) c.range = window_selection_get(win); else c.range.start = c.range.end = pos; @@ -1162,18 +1159,18 @@ static void action_do(Action *a) { } } - if (mode->visual) { + if (vis->mode->visual) { window_selection_set(win, &c.range); pos = c.range.end; window_cursor_to(win, pos); } - } else if (mode->visual) { + } else if (vis->mode->visual) { c.range = window_selection_get(win); if (!text_range_valid(&c.range)) c.range.start = c.range.end = pos; } - if (mode == &vis_modes[VIS_MODE_VISUAL_LINE] && (a->movement || a->textobj)) { + if (vis->mode == &vis_modes[VIS_MODE_VISUAL_LINE] && (a->movement || a->textobj)) { Filerange sel = window_selection_get(win); sel.end = text_char_prev(txt, sel.end); size_t start = text_line_begin(txt, sel.start); @@ -1191,9 +1188,9 @@ static void action_do(Action *a) { if (a->op) { a->op->func(&c); - if (mode == &vis_modes[VIS_MODE_OPERATOR]) - switchmode_to(mode_prev); - else if (mode->visual) + if (vis->mode == &vis_modes[VIS_MODE_OPERATOR]) + switchmode_to(vis->mode_prev); + else if (vis->mode->visual) switchmode(&(const Arg){ .i = VIS_MODE_NORMAL }); text_snapshot(txt); } @@ -1215,17 +1212,17 @@ static void action_reset(Action *a) { } static void switchmode_to(Mode *new_mode) { - if (mode == new_mode) + if (vis->mode == new_mode) return; - if (mode->leave) - mode->leave(new_mode); - if (mode->isuser) - mode_prev = mode; - mode = new_mode; - if (mode == config->mode || (mode->name && mode->name[0] == '-')) + if (vis->mode->leave) + vis->mode->leave(new_mode); + if (vis->mode->isuser) + vis->mode_prev = vis->mode; + vis->mode = new_mode; + if (new_mode == config->mode || (new_mode->name && new_mode->name[0] == '-')) vis->win->ui->draw_status(vis->win->ui); - if (mode->enter) - mode->enter(mode_prev); + if (new_mode->enter) + new_mode->enter(vis->mode_prev); } /** ':'-command implementations */ @@ -1849,7 +1846,7 @@ static void keypress(Key *key) { keys[keylen++] = *key; - KeyBinding *action = keybinding(mode, keys); + KeyBinding *action = keybinding(vis->mode, keys); if (action) { int combolen = 0; @@ -1863,8 +1860,8 @@ static void keypress(Key *key) { memset(keys, 0, sizeof(keys)); if (action->func) action->func(&action->arg); - } else if (keylen == 1 && key->code == 0 && mode->input) { - mode->input(key->str, strlen(key->str)); + } else if (keylen == 1 && key->code == 0 && vis->mode->input) { + vis->mode->input(key->str, strlen(key->str)); } keylen = 0; @@ -1920,7 +1917,7 @@ static void mainloop() { FD_SET(STDIN_FILENO, &fds); editor_update(vis); - idle.tv_sec = mode->idle_timeout; + idle.tv_sec = vis->mode->idle_timeout; int r = pselect(1, &fds, NULL, NULL, timeout, &emptyset); if (r == -1 && errno == EINTR) continue; @@ -1931,8 +1928,8 @@ static void mainloop() { } if (!FD_ISSET(STDIN_FILENO, &fds)) { - if (mode->idle) - mode->idle(); + if (vis->mode->idle) + vis->mode->idle(); timeout = NULL; continue; } @@ -1940,7 +1937,7 @@ static void mainloop() { Key key = getkey(); keypress(&key); - if (mode->idle) + if (vis->mode->idle) timeout = &idle; } } @@ -1958,10 +1955,11 @@ int main(int argc, char *argv[]) { } } - mode_prev = mode = config->mode; - if (!(vis = editor_new(ui_curses_new()))) die("Could not allocate editor core\n"); + + vis->mode_prev = vis->mode = config->mode; + if (!editor_syntax_load(vis, syntaxes, colors)) die("Could not load syntax highlighting definitions\n"); -- cgit v1.2.3