From 277c6b4897e11a5449fc1cfab3ab6e83307a7989 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Mon, 12 Oct 2015 13:47:36 +0200 Subject: view: cleanup option handling --- editor.c | 6 +----- editor.h | 1 - ui-curses.c | 22 ++++++++++++---------- ui.h | 3 ++- view.c | 8 ++++++++ view.h | 3 +++ vis.c | 26 ++++++++++++++++++++------ 7 files changed, 46 insertions(+), 23 deletions(-) diff --git a/editor.c b/editor.c index 70cc162..c07eed7 100644 --- a/editor.c +++ b/editor.c @@ -64,6 +64,7 @@ bool editor_window_split(Win *original) { win->file = original->file; win->file->refcount++; view_syntax_set(win->view, view_syntax_get(original->view)); + view_options_set(win->view, view_options_get(original->view)); view_cursor_to(win->view, view_cursor_get(original->view)); editor_draw(win->editor); return true; @@ -506,8 +507,3 @@ void editor_info_show(Editor *ed, const char *msg, ...) { void editor_info_hide(Editor *ed) { ed->ui->info_hide(ed->ui); } - -void editor_window_options(Win *win, enum UiOption options) { - win->ui->options(win->ui, options); -} - diff --git a/editor.h b/editor.h index 80fe844..87d9aeb 100644 --- a/editor.h +++ b/editor.h @@ -325,7 +325,6 @@ void editor_prompt_set(Editor*, const char *line); /* display a message to the user */ void editor_info_show(Editor*, const char *msg, ...); void editor_info_hide(Editor*); -void editor_window_options(Win*, enum UiOption options); /* look up a curses color pair for the given combination of fore and * background color */ diff --git a/ui-curses.c b/ui-curses.c index 7e74919..cd26508 100644 --- a/ui-curses.c +++ b/ui-curses.c @@ -798,26 +798,27 @@ static void ui_window_focus(UiWin *w) { ui_window_draw(w); } -static void ui_window_options(UiWin *w, enum UiOption options) { +static void ui_window_options_set(UiWin *w, enum UiOption options) { UiCursesWin *win = (UiCursesWin*)w; win->options = options; - switch (options) { - case UI_OPTION_LINE_NUMBERS_NONE: + if (options & (UI_OPTION_LINE_NUMBERS_ABSOLUTE|UI_OPTION_LINE_NUMBERS_RELATIVE)) { + if (!win->winside) + win->winside = newwin(1, 1, 1, 1); + } else { if (win->winside) { delwin(win->winside); win->winside = NULL; win->sidebar_width = 0; } - break; - case UI_OPTION_LINE_NUMBERS_ABSOLUTE: - case UI_OPTION_LINE_NUMBERS_RELATIVE: - if (!win->winside) - win->winside = newwin(1, 1, 1, 1); - break; } ui_window_draw(w); } +static enum UiOption ui_window_options_get(UiWin *w) { + UiCursesWin *win = (UiCursesWin*)w; + return win->options; +} + static UiWin *ui_window_new(Ui *ui, View *view, File *file) { UiCurses *uic = (UiCurses*)ui; UiCursesWin *win = calloc(1, sizeof(UiCursesWin)); @@ -828,7 +829,8 @@ static UiWin *ui_window_new(Ui *ui, View *view, File *file) { .draw = ui_window_draw, .draw_status = ui_window_draw_status, .draw_text = ui_window_draw_text, - .options = ui_window_options, + .options_set = ui_window_options_set, + .options_get = ui_window_options_get, .reload = ui_window_reload, .syntax_style = ui_window_syntax_style, }; diff --git a/ui.h b/ui.h index 06738cf..1275f37 100644 --- a/ui.h +++ b/ui.h @@ -55,7 +55,8 @@ struct UiWin { void (*draw_text)(UiWin*, const Line*); void (*draw_status)(UiWin*); void (*reload)(UiWin*, File*); - void (*options)(UiWin*, enum UiOption); + void (*options_set)(UiWin*, enum UiOption); + enum UiOption (*options_get)(UiWin*); bool (*syntax_style)(UiWin*, int id, const char *style); }; diff --git a/view.c b/view.c index df8aa93..02393af 100644 --- a/view.c +++ b/view.c @@ -873,6 +873,14 @@ int view_symbols_get(View *view) { return flags; } +void view_options_set(View *view, enum UiOption options) { + view->ui->options_set(view->ui, options); +} + +enum UiOption view_options_get(View *view) { + return view->ui->options_get(view->ui); +} + size_t view_screenline_goto(View *view, int n) { size_t pos = view->start; for (Line *line = view->topline; --n > 0 && line != view->lastline; line = line->next) diff --git a/view.h b/view.h index 42b05c8..8fbb15c 100644 --- a/view.h +++ b/view.h @@ -97,6 +97,9 @@ Syntax *view_syntax_get(View*); void view_symbols_set(View*, int flags); int view_symbols_get(View*); +void view_options_set(View*, enum UiOption options); +enum UiOption view_options_get(View*); + /* A view can manage multiple cursors, one of which (the main cursor) is always * placed within the visible viewport. All functions named view_cursor_* operate * on this cursor. Additional cursor can be created and manipulated using the diff --git a/vis.c b/vis.c index e686b73..10ba6b8 100644 --- a/vis.c +++ b/vis.c @@ -1783,15 +1783,29 @@ static bool cmd_set(Filerange *range, enum CmdOpt cmdopt, const char *argv[]) { } view_symbols_set(vis->win->view, flags); break; - case OPTION_NUMBER: - editor_window_options(vis->win, arg.b ? UI_OPTION_LINE_NUMBERS_ABSOLUTE : - UI_OPTION_LINE_NUMBERS_NONE); + case OPTION_NUMBER: { + enum UiOption opt = view_options_get(vis->win->view); + if (arg.b) { + opt &= ~UI_OPTION_LINE_NUMBERS_RELATIVE; + opt |= UI_OPTION_LINE_NUMBERS_ABSOLUTE; + } else { + opt &= ~UI_OPTION_LINE_NUMBERS_ABSOLUTE; + } + view_options_set(vis->win->view, opt); break; - case OPTION_NUMBER_RELATIVE: - editor_window_options(vis->win, arg.b ? UI_OPTION_LINE_NUMBERS_RELATIVE : - UI_OPTION_LINE_NUMBERS_NONE); + } + case OPTION_NUMBER_RELATIVE: { + enum UiOption opt = view_options_get(vis->win->view); + if (arg.b) { + opt &= ~UI_OPTION_LINE_NUMBERS_ABSOLUTE; + opt |= UI_OPTION_LINE_NUMBERS_RELATIVE; + } else { + opt &= ~UI_OPTION_LINE_NUMBERS_RELATIVE; + } + view_options_set(vis->win->view, opt); break; } + } return true; } -- cgit v1.2.3