Skip to content

Commit

Permalink
view: cleanup option handling
Browse files Browse the repository at this point in the history
  • Loading branch information
martanne committed Oct 14, 2015
1 parent 9e391f5 commit 277c6b4
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 23 deletions.
6 changes: 1 addition & 5 deletions editor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

1 change: 0 additions & 1 deletion editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
22 changes: 12 additions & 10 deletions ui-curses.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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,
};
Expand Down
3 changes: 2 additions & 1 deletion ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
8 changes: 8 additions & 0 deletions view.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions view.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 20 additions & 6 deletions vis.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 277c6b4

Please sign in to comment.