From 7554ecd77efc29601b7b44deca602724917ce019 Mon Sep 17 00:00:00 2001 From: Randy Palamar Date: Tue, 21 May 2024 11:27:08 -0600 Subject: [PATCH] remove some view pointer chasing Same as previous commit each window only has a single View. No need for it to be stored elsewhere in memory. --- sam.c | 25 ++++++++--------- ui-terminal.c | 12 ++++---- view.c | 19 +++++-------- view.h | 4 +-- vis-cmds.c | 34 +++++++++++------------ vis-core.h | 2 +- vis-lua.c | 74 ++++++++++++++++++++++++------------------------- vis-marks.c | 13 ++++----- vis-modes.c | 12 ++++---- vis-operators.c | 7 ++--- vis-prompt.c | 12 ++++---- vis-registers.c | 2 +- vis.c | 74 ++++++++++++++++++++++++------------------------- 13 files changed, 139 insertions(+), 151 deletions(-) diff --git a/sam.c b/sam.c index 8b322407f..90d19c297 100644 --- a/sam.c +++ b/sam.c @@ -1232,7 +1232,7 @@ enum SamError sam_cmd(Vis *vis, const char *s) { } bool visual = vis->mode->visual; - size_t primary_pos = vis->win ? view_cursor_get(vis->win->view) : EPOS; + size_t primary_pos = vis->win ? view_cursor_get(&vis->win->view) : EPOS; Filerange range = text_range_empty(); sam_execute(vis, vis->win, cmd, NULL, &range); @@ -1278,7 +1278,7 @@ enum SamError sam_cmd(Vis *vis, const char *s) { view_cursors_to(c->sel, r.end); } } else if (visual) { - Selection *sel = view_selections_new(c->win->view, r.start); + Selection *sel = view_selections_new(&c->win->view, r.start); if (sel) { view_selections_set(sel, &r); sel->anchored = true; @@ -1291,15 +1291,15 @@ enum SamError sam_cmd(Vis *vis, const char *s) { } for (Win *win = vis->windows; win; win = win->next) - view_selections_normalize(win->view); + view_selections_normalize(&win->view); if (vis->win) { - if (primary_pos != EPOS && view_selection_disposed(vis->win->view)) - view_cursors_to(vis->win->view->selection, primary_pos); - view_selections_primary_set(view_selections(vis->win->view)); + if (primary_pos != EPOS && view_selection_disposed(&vis->win->view)) + view_cursors_to(vis->win->view.selection, primary_pos); + view_selections_primary_set(view_selections(&vis->win->view)); vis_jumplist_save(vis); bool completed = true; - for (Selection *s = view_selections(vis->win->view); s; s = view_selections_next(s)) { + for (Selection *s = view_selections(&vis->win->view); s; s = view_selections_next(s)) { if (s->anchored) { completed = false; break; @@ -1507,7 +1507,7 @@ static bool cmd_select(Vis *vis, Win *win, Command *cmd, const char *argv[], Sel if (!win) return sam_execute(vis, NULL, cmd->cmd, NULL, &r); bool ret = true; - View *view = win->view; + View *view = &win->view; Text *txt = win->file->text; bool multiple_cursors = view->selection_count > 1; Selection *primary = view_selections_primary_get(view); @@ -1557,7 +1557,7 @@ static bool cmd_select(Vis *vis, Win *win, Command *cmd, const char *argv[], Sel break; } - if (vis->win && vis->win->view == view && primary != view_selections_primary_get(view)) + if (vis->win && &vis->win->view == view && primary != view_selections_primary_get(view)) view_selections_primary_set(view_selections(view)); return ret; } @@ -1565,9 +1565,8 @@ static bool cmd_select(Vis *vis, Win *win, Command *cmd, const char *argv[], Sel static bool cmd_print(Vis *vis, Win *win, Command *cmd, const char *argv[], Selection *sel, Filerange *range) { if (!win || !text_range_valid(range)) return false; - View *view = win->view; if (!sel) - sel = view_selections_new_force(view, range->start); + sel = view_selections_new_force(&win->view, range->start); if (!sel) return false; if (range->start != range->end) { @@ -1641,7 +1640,7 @@ static bool cmd_write(Vis *vis, Win *win, Command *cmd, const char *argv[], Sele bool visual = vis->mode->visual; - for (Selection *s = view_selections(win->view); s; s = view_selections_next(s)) { + for (Selection *s = view_selections(&win->view); s; s = view_selections_next(s)) { Filerange range = visual ? view_selections_get(s) : *r; ssize_t written = text_write_range(text, &range, file->fd); if (written == -1 || (size_t)written != text_range_size(&range)) { @@ -1709,7 +1708,7 @@ static bool cmd_write(Vis *vis, Win *win, Command *cmd, const char *argv[], Sele bool failure = false; bool visual = vis->mode->visual; - for (Selection *s = view_selections(win->view); s; s = view_selections_next(s)) { + for (Selection *s = view_selections(&win->view); s; s = view_selections_next(s)) { Filerange range = visual ? view_selections_get(s) : *r; ssize_t written = text_save_write_range(ctx, &range); failure = (written == -1 || (size_t)written != text_range_size(&range)); diff --git a/ui-terminal.c b/ui-terminal.c index d19724ab1..d540308eb 100644 --- a/ui-terminal.c +++ b/ui-terminal.c @@ -58,7 +58,7 @@ static void ui_window_resize(UiWin *win, int width, int height) { bool status = win->options & UI_OPTION_STATUSBAR; win->width = width; win->height = height; - view_resize(win->win->view, width - win->sidebar_width, status ? height - 1 : height); + view_resize(&win->win->view, width - win->sidebar_width, status ? height - 1 : height); } static void ui_window_move(UiWin *win, int x, int y) { @@ -204,7 +204,7 @@ static void ui_draw_string(Ui *tui, int x, int y, const char *str, UiWin *win, e static void ui_window_draw(UiWin *win) { Ui *ui = win->ui; - View *view = win->win->view; + View *view = &win->win->view; int width = win->width, height = win->height; const Line *line = view->topline; bool status = win->options & UI_OPTION_STATUSBAR; @@ -351,7 +351,7 @@ void ui_draw(Ui *tui) { void ui_redraw(Ui *tui) { ui_term_backend_clear(tui); for (UiWin *win = tui->windows; win; win = win->next) - win->win->view->need_update = true; + win->win->view.need_update = true; } void ui_resize(Ui *tui) { @@ -405,8 +405,8 @@ void ui_window_focus(UiWin *new) { if (new->options & UI_OPTION_STATUSBAR) new->ui->selwin = new; if (old) - old->win->view->need_update = true; - new->win->view->need_update = true; + old->win->view.need_update = true; + new->win->view.need_update = true; } void ui_window_options_set(UiWin *win, enum UiOption options) { @@ -500,7 +500,7 @@ UiWin *ui_window_new(Ui *tui, Win *w, enum UiOption options) { styles[UI_STYLE_STATUS].attr |= CELL_ATTR_REVERSE; styles[UI_STYLE_STATUS_FOCUSED].attr |= CELL_ATTR_REVERSE|CELL_ATTR_BOLD; styles[UI_STYLE_INFO].attr |= CELL_ATTR_BOLD; - w->view->ui = win; + w->view.ui = win; if (tui->windows) tui->windows->prev = win; diff --git a/view.c b/view.c index afc72da66..6e67088e9 100644 --- a/view.c +++ b/view.c @@ -71,7 +71,7 @@ void window_status_update(Vis *vis, Win *win) { size_t left_count = 0; size_t right_count = 0; - View *view = win->view; + View *view = &win->view; File *file = win->file; Text *txt = file->text; int width = win->ui->width; @@ -114,12 +114,12 @@ void window_status_update(Vis *vis, Win *win) { "%zu%%", percent); if (!(options & UI_OPTION_LARGE_FILE)) { - Selection *sel = view_selections_primary_get(win->view); + Selection *sel = view_selections_primary_get(&win->view); size_t line = view_cursors_line(sel); size_t col = view_cursors_col(sel); if (col > UI_LARGE_FILE_LINE_SIZE) { options |= UI_OPTION_LARGE_FILE; - view_options_set(win->view, options); + view_options_set(&win->view, options); } snprintf(right_parts[right_count++], sizeof(right_parts[0]), "%zu, %zu", line, col); @@ -551,7 +551,6 @@ void view_free(View *view) { free(view->textbuf); free(view->lines); free(view->breakat); - free(view); } void view_reload(View *view, Text *text) { @@ -560,12 +559,9 @@ void view_reload(View *view, Text *text) { view_cursors_to(view->selection, 0); } -View *view_new(Text *text) { +bool view_init(View *view, Text *text) { if (!text) - return NULL; - View *view = calloc(1, sizeof(View)); - if (!view) - return NULL; + return false; view->text = text; view->tabwidth = 8; @@ -582,12 +578,11 @@ View *view_new(Text *text) { !view_selections_new(view, 0) || !view_resize(view, 1, 1)) { - view_free(view); - return NULL; + return false; } view_cursors_to(view->selection, 0); - return view; + return true; } static size_t cursor_set(Selection *sel, Line *line, int col) { diff --git a/view.h b/view.h index 79275c800..b159267e0 100644 --- a/view.h +++ b/view.h @@ -87,7 +87,7 @@ typedef struct View { * @defgroup view_life * @{ */ -View *view_new(Text*); +bool view_init(View*, Text*); void view_free(View*); void view_reload(View*, Text*); /** @@ -96,7 +96,7 @@ void view_reload(View*, Text*); * @{ */ /** Get the currently displayed text range. */ -#define VIEW_VIEWPORT_GET(v) (Filerange){ .start = v->start, .end = v->end } +#define VIEW_VIEWPORT_GET(v) (Filerange){ .start = v.start, .end = v.end } /** * Get window coordinate of text position. * @param pos The position to query. diff --git a/vis-cmds.c b/vis-cmds.c index ae9ba4906..382a7d795 100644 --- a/vis-cmds.c +++ b/vis-cmds.c @@ -256,7 +256,7 @@ static bool cmd_set(Vis *vis, Win *win, Command *cmd, const char *argv[], Select vis->autoindent = toggle ? !vis->autoindent : arg.b; break; case OPTION_TABWIDTH: - view_tabwidth_set(vis->win->view, arg.i); + view_tabwidth_set(&vis->win->view, arg.i); break; case OPTION_SHOW_SPACES: case OPTION_SHOW_TABS: @@ -271,48 +271,48 @@ static bool cmd_set(Vis *vis, Win *win, Command *cmd, const char *argv[], Select [OPTION_SHOW_EOF] = UI_OPTION_SYMBOL_EOF, [OPTION_STATUSBAR] = UI_OPTION_STATUSBAR, }; - int flags = UI_OPTIONS_GET(win->view->ui); + int flags = UI_OPTIONS_GET(win->view.ui); if (arg.b || (toggle && !(flags & values[opt_index]))) flags |= values[opt_index]; else flags &= ~values[opt_index]; - view_options_set(win->view, flags); + view_options_set(&win->view, flags); break; } case OPTION_NUMBER: { - enum UiOption opt = UI_OPTIONS_GET(win->view->ui); + enum UiOption opt = UI_OPTIONS_GET(win->view.ui); if (arg.b || (toggle && !(opt & UI_OPTION_LINE_NUMBERS_ABSOLUTE))) { opt &= ~UI_OPTION_LINE_NUMBERS_RELATIVE; opt |= UI_OPTION_LINE_NUMBERS_ABSOLUTE; } else { opt &= ~UI_OPTION_LINE_NUMBERS_ABSOLUTE; } - view_options_set(win->view, opt); + view_options_set(&win->view, opt); break; } case OPTION_NUMBER_RELATIVE: { - enum UiOption opt = UI_OPTIONS_GET(win->view->ui); + enum UiOption opt = UI_OPTIONS_GET(win->view.ui); if (arg.b || (toggle && !(opt & UI_OPTION_LINE_NUMBERS_RELATIVE))) { opt &= ~UI_OPTION_LINE_NUMBERS_ABSOLUTE; opt |= UI_OPTION_LINE_NUMBERS_RELATIVE; } else { opt &= ~UI_OPTION_LINE_NUMBERS_RELATIVE; } - view_options_set(win->view, opt); + view_options_set(&win->view, opt); break; } case OPTION_CURSOR_LINE: { - enum UiOption opt = UI_OPTIONS_GET(win->view->ui); + enum UiOption opt = UI_OPTIONS_GET(win->view.ui); if (arg.b || (toggle && !(opt & UI_OPTION_CURSOR_LINE))) opt |= UI_OPTION_CURSOR_LINE; else opt &= ~UI_OPTION_CURSOR_LINE; - view_options_set(win->view, opt); + view_options_set(&win->view, opt); break; } case OPTION_COLOR_COLUMN: if (arg.i >= 0) - win->view->colorcolumn = arg.i; + win->view.colorcolumn = arg.i; break; case OPTION_SAVE_METHOD: if (strcmp("auto", arg.s) == 0) { @@ -360,14 +360,14 @@ static bool cmd_set(Vis *vis, Win *win, Command *cmd, const char *argv[], Select vis->ignorecase = toggle ? !vis->ignorecase : arg.b; break; case OPTION_BREAKAT: - if (!view_breakat_set(win->view, arg.s)) { + if (!view_breakat_set(&win->view, arg.s)) { vis_info_show(vis, "Failed to set breakat"); return false; } break; case OPTION_WRAP_COLUMN: if (arg.i >= 0) - win->view->wrapcolumn = arg.i; + win->view.wrapcolumn = arg.i; break; default: if (!opt->func) @@ -540,26 +540,26 @@ static bool cmd_qall(Vis *vis, Win *win, Command *cmd, const char *argv[], Selec static bool cmd_split(Vis *vis, Win *win, Command *cmd, const char *argv[], Selection *sel, Filerange *range) { if (!win) return false; - enum UiOption options = UI_OPTIONS_GET(win->view->ui); + enum UiOption options = UI_OPTIONS_GET(win->view.ui); ui_arrange(&vis->ui, UI_LAYOUT_HORIZONTAL); if (!argv[1]) return vis_window_split(win); bool ret = openfiles(vis, &argv[1]); if (ret) - view_options_set(vis->win->view, options); + view_options_set(&vis->win->view, options); return ret; } static bool cmd_vsplit(Vis *vis, Win *win, Command *cmd, const char *argv[], Selection *sel, Filerange *range) { if (!win) return false; - enum UiOption options = UI_OPTIONS_GET(win->view->ui); + enum UiOption options = UI_OPTIONS_GET(win->view.ui); ui_arrange(&vis->ui, UI_LAYOUT_VERTICAL); if (!argv[1]) return vis_window_split(win); bool ret = openfiles(vis, &argv[1]); if (ret) - view_options_set(vis->win->view, options); + view_options_set(&vis->win->view, options); return ret; } @@ -871,7 +871,7 @@ static bool cmd_help(Vis *vis, Win *win, Command *cmd, const char *argv[], Selec text_appendf(txt, " %-32s\t%s\n", configs[i].name, configs[i].enabled ? "yes" : "no"); text_save(txt, NULL); - view_cursors_to(vis->win->view->selection, 0); + view_cursors_to(vis->win->view.selection, 0); if (argv[1]) vis_motion(vis, VIS_MOVE_SEARCH_FORWARD, argv[1]); diff --git a/vis-core.h b/vis-core.h index 7d729f78f..c66924e9b 100644 --- a/vis-core.h +++ b/vis-core.h @@ -157,7 +157,7 @@ struct Win { Vis *vis; /* editor instance to which this window belongs */ UiWin *ui; /* ui object handling visual appearance of this window */ File *file; /* file being displayed in this window */ - View *view; /* currently displayed part of underlying text */ + View view; /* currently displayed part of underlying text */ bool expandtab; /* whether typed tabs should be converted to spaces in this window*/ MarkList jumplist; /* LRU jump management */ Array saved_selections; /* register used to store selections */ diff --git a/vis-lua.c b/vis-lua.c index 6bd8a080e..75e2e24c7 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -1107,7 +1107,7 @@ static bool command_lua(Vis *vis, Win *win, void *data, bool force, const char * if (!obj_ref_new(L, win, VIS_LUA_TYPE_WINDOW)) return false; if (!sel) - sel = view_selections_primary_get(win->view); + sel = view_selections_primary_get(&win->view); if (!obj_lightref_new(L, sel, VIS_LUA_TYPE_SELECTION)) return false; pushrange(L, range); @@ -1766,8 +1766,8 @@ static int window_index(lua_State *L) { if (strcmp(key, "viewport") == 0) { Filerange b = VIEW_VIEWPORT_GET(win->view); Filerange l; - l.start = win->view->topline->lineno; - l.end = win->view->lastline->lineno; + l.start = win->view.topline->lineno; + l.end = win->view.lastline->lineno; lua_createtable(L, 0, 4); lua_pushstring(L, "bytes"); @@ -1777,10 +1777,10 @@ static int window_index(lua_State *L) { pushrange(L, &l); lua_settable(L, -3); lua_pushstring(L, "width"); - lua_pushunsigned(L, win->view->width); + lua_pushunsigned(L, win->view.width); lua_settable(L, -3); lua_pushstring(L, "height"); - lua_pushunsigned(L, win->view->height); + lua_pushunsigned(L, win->view.height); lua_settable(L, -3); return 1; } @@ -1801,13 +1801,13 @@ static int window_index(lua_State *L) { } if (strcmp(key, "selection") == 0) { - Selection *sel = view_selections_primary_get(win->view); + Selection *sel = view_selections_primary_get(&win->view); obj_lightref_new(L, sel, VIS_LUA_TYPE_SELECTION); return 1; } if (strcmp(key, "selections") == 0) { - obj_ref_new(L, win->view, VIS_LUA_TYPE_SELECTIONS); + obj_ref_new(L, &win->view, VIS_LUA_TYPE_SELECTIONS); return 1; } @@ -1825,64 +1825,64 @@ static int window_index(lua_State *L) { } static int window_options_assign(Win *win, lua_State *L, const char *key, int next) { - enum UiOption flags = UI_OPTIONS_GET(win->view->ui); + enum UiOption flags = UI_OPTIONS_GET(win->view.ui); if (strcmp(key, "breakat") == 0 || strcmp(key, "brk") == 0) { if (lua_isstring(L, next)) - view_breakat_set(win->view, lua_tostring(L, next)); + view_breakat_set(&win->view, lua_tostring(L, next)); } else if (strcmp(key, "colorcolumn") == 0 || strcmp(key, "cc") == 0) { - win->view->colorcolumn = luaL_checkunsigned(L, next); + win->view.colorcolumn = luaL_checkunsigned(L, next); } else if (strcmp(key, "cursorline") == 0 || strcmp(key, "cul") == 0) { if (lua_toboolean(L, next)) flags |= UI_OPTION_CURSOR_LINE; else flags &= ~UI_OPTION_CURSOR_LINE; - view_options_set(win->view, flags); + view_options_set(&win->view, flags); } else if (strcmp(key, "numbers") == 0 || strcmp(key, "nu") == 0) { if (lua_toboolean(L, next)) flags |= UI_OPTION_LINE_NUMBERS_ABSOLUTE; else flags &= ~UI_OPTION_LINE_NUMBERS_ABSOLUTE; - view_options_set(win->view, flags); + view_options_set(&win->view, flags); } else if (strcmp(key, "relativenumbers") == 0 || strcmp(key, "rnu") == 0) { if (lua_toboolean(L, next)) flags |= UI_OPTION_LINE_NUMBERS_RELATIVE; else flags &= ~UI_OPTION_LINE_NUMBERS_RELATIVE; - view_options_set(win->view, flags); + view_options_set(&win->view, flags); } else if (strcmp(key, "showeof") == 0) { if (lua_toboolean(L, next)) flags |= UI_OPTION_SYMBOL_EOF; else flags &= ~UI_OPTION_SYMBOL_EOF; - view_options_set(win->view, flags); + view_options_set(&win->view, flags); } else if (strcmp(key, "shownewlines") == 0) { if (lua_toboolean(L, next)) flags |= UI_OPTION_SYMBOL_EOL; else flags &= ~UI_OPTION_SYMBOL_EOL; - view_options_set(win->view, flags); + view_options_set(&win->view, flags); } else if (strcmp(key, "showspaces") == 0) { if (lua_toboolean(L, next)) flags |= UI_OPTION_SYMBOL_SPACE; else flags &= ~UI_OPTION_SYMBOL_SPACE; - view_options_set(win->view, flags); + view_options_set(&win->view, flags); } else if (strcmp(key, "showtabs") == 0) { if (lua_toboolean(L, next)) flags |= UI_OPTION_SYMBOL_TAB; else flags &= ~UI_OPTION_SYMBOL_TAB; - view_options_set(win->view, flags); + view_options_set(&win->view, flags); } else if (strcmp(key, "statusbar") == 0) { if (lua_toboolean(L, next)) flags |= UI_OPTION_STATUSBAR; else flags &= ~UI_OPTION_STATUSBAR; - view_options_set(win->view, flags); + view_options_set(&win->view, flags); } else if (strcmp(key, "wrapcolumn") == 0 || strcmp(key, "wc") == 0) { - win->view->wrapcolumn = luaL_checkunsigned(L, next); + win->view.wrapcolumn = luaL_checkunsigned(L, next); } else if (strcmp(key, "tabwidth") == 0 || strcmp(key, "tw") == 0) { - view_tabwidth_set(win->view, luaL_checkint(L, next)); + view_tabwidth_set(&win->view, luaL_checkint(L, next)); } else if (strcmp(key, "expandtab") == 0 || strcmp(key, "et") == 0) { win->expandtab = lua_toboolean(L, next); } @@ -1942,7 +1942,7 @@ static int window_selections_iterator_next(lua_State *L) { static int window_selections_iterator(lua_State *L) { Win *win = obj_ref_check(L, 1, VIS_LUA_TYPE_WINDOW); Selection **handle = lua_newuserdata(L, sizeof *handle); - *handle = view_selections(win->view); + *handle = view_selections(&win->view); lua_pushcclosure(L, window_selections_iterator_next, 1); return 1; } @@ -1986,7 +1986,7 @@ static int window_style_define(lua_State *L) { Win *win = obj_ref_check(L, 1, VIS_LUA_TYPE_WINDOW); enum UiStyle id = luaL_checkunsigned(L, 2); const char *style = luaL_checkstring(L, 3); - bool ret = ui_style_define(win->view->ui, id, style); + bool ret = ui_style_define(win->view.ui, id, style); lua_pushboolean(L, ret); return 1; } @@ -2008,7 +2008,7 @@ static int window_style(lua_State *L) { enum UiStyle style = luaL_checkunsigned(L, 2); size_t start = checkpos(L, 3); size_t end = checkpos(L, 4); - view_style(win->view, style, start, end); + view_style(&win->view, style, start, end); return 0; } @@ -2068,7 +2068,7 @@ static int window_status(lua_State *L) { */ static int window_draw(lua_State *L) { Win *win = obj_ref_check(L, 1, VIS_LUA_TYPE_WINDOW); - view_draw(win->view); + view_draw(&win->view); return 0; } @@ -2140,43 +2140,43 @@ static int window_options_index(lua_State *L) { if (lua_isstring(L, 2)) { const char *key = lua_tostring(L, 2); if (strcmp(key, "breakat") == 0 || strcmp(key, "brk") == 0) { - lua_pushstring(L, win->view->breakat); + lua_pushstring(L, win->view.breakat); return 1; } else if (strcmp(key, "colorcolumn") == 0 || strcmp(key, "cc") == 0) { - lua_pushunsigned(L, win->view->colorcolumn); + lua_pushunsigned(L, win->view.colorcolumn); return 1; } else if (strcmp(key, "cursorline") == 0 || strcmp(key, "cul") == 0) { - lua_pushboolean(L, UI_OPTIONS_GET(win->view->ui) & UI_OPTION_CURSOR_LINE); + lua_pushboolean(L, UI_OPTIONS_GET(win->view.ui) & UI_OPTION_CURSOR_LINE); return 1; } else if (strcmp(key, "expandtab") == 0 || strcmp(key, "et") == 0) { lua_pushboolean(L, win->expandtab); return 1; } else if (strcmp(key, "numbers") == 0 || strcmp(key, "nu") == 0) { - lua_pushboolean(L, UI_OPTIONS_GET(win->view->ui) & UI_OPTION_LINE_NUMBERS_ABSOLUTE); + lua_pushboolean(L, UI_OPTIONS_GET(win->view.ui) & UI_OPTION_LINE_NUMBERS_ABSOLUTE); return 1; } else if (strcmp(key, "relativenumbers") == 0 || strcmp(key, "rnu") == 0) { - lua_pushboolean(L, UI_OPTIONS_GET(win->view->ui) & UI_OPTION_LINE_NUMBERS_RELATIVE); + lua_pushboolean(L, UI_OPTIONS_GET(win->view.ui) & UI_OPTION_LINE_NUMBERS_RELATIVE); return 1; } else if (strcmp(key, "showeof") == 0) { - lua_pushboolean(L, UI_OPTIONS_GET(win->view->ui) & UI_OPTION_SYMBOL_EOF); + lua_pushboolean(L, UI_OPTIONS_GET(win->view.ui) & UI_OPTION_SYMBOL_EOF); return 1; } else if (strcmp(key, "shownewlines") == 0) { - lua_pushboolean(L, UI_OPTIONS_GET(win->view->ui) & UI_OPTION_SYMBOL_EOL); + lua_pushboolean(L, UI_OPTIONS_GET(win->view.ui) & UI_OPTION_SYMBOL_EOL); return 1; } else if (strcmp(key, "showspaces") == 0) { - lua_pushboolean(L, UI_OPTIONS_GET(win->view->ui) & UI_OPTION_SYMBOL_SPACE); + lua_pushboolean(L, UI_OPTIONS_GET(win->view.ui) & UI_OPTION_SYMBOL_SPACE); return 1; } else if (strcmp(key, "showtabs") == 0) { - lua_pushboolean(L, UI_OPTIONS_GET(win->view->ui) & UI_OPTION_SYMBOL_TAB); + lua_pushboolean(L, UI_OPTIONS_GET(win->view.ui) & UI_OPTION_SYMBOL_TAB); return 1; } else if (strcmp(key, "statusbar") == 0) { - lua_pushboolean(L, UI_OPTIONS_GET(win->view->ui) & UI_OPTION_STATUSBAR); + lua_pushboolean(L, UI_OPTIONS_GET(win->view.ui) & UI_OPTION_STATUSBAR); return 1; } else if (strcmp(key, "tabwidth") == 0 || strcmp(key, "tw") == 0) { - lua_pushinteger(L, win->view->tabwidth); + lua_pushinteger(L, win->view.tabwidth); return 1; } else if (strcmp(key, "wrapcolumn") == 0 || strcmp(key, "wc") == 0) { - lua_pushunsigned(L, win->view->wrapcolumn); + lua_pushunsigned(L, win->view.wrapcolumn); return 1; } } @@ -3492,7 +3492,7 @@ static void vis_lua_win_close(Vis *vis, Win *win) { obj_ref_new(L, win, VIS_LUA_TYPE_WINDOW); pcall(vis, L, 1, 0); } - obj_ref_free(L, win->view); + obj_ref_free(L, &win->view); obj_ref_free(L, win); lua_pop(L, 1); } diff --git a/vis-marks.c b/vis-marks.c index aea654d71..48c065cdb 100644 --- a/vis-marks.c +++ b/vis-marks.c @@ -72,12 +72,11 @@ static Array mark_get(Win *win, Array *mark) { array_init_sized(&sel, sizeof(Filerange)); if (!mark) return sel; - View *view = win->view; size_t len = array_length(mark); array_reserve(&sel, len); for (size_t i = 0; i < len; i++) { SelectionRegion *sr = array_get(mark, i); - Filerange r = view_regions_restore(view, sr); + Filerange r = view_regions_restore(&win->view, sr); if (text_range_valid(&r)) array_add(&sel, &r); } @@ -93,11 +92,10 @@ static void mark_set(Win *win, Array *mark, Array *sel) { if (!mark) return; array_clear(mark); - View *view = win->view; for (size_t i = 0, len = array_length(sel); i < len; i++) { SelectionRegion ss; Filerange *r = array_get(sel, i); - if (view_regions_save(view, r, &ss)) + if (view_regions_save(&win->view, r, &ss)) array_add(mark, &ss); } } @@ -150,15 +148,14 @@ static bool marklist_push(Win *win, MarkList *list, Array *sel) { } bool vis_jumplist_save(Vis *vis) { - View *view = vis->win->view; - Array sel = view_selections_get_all(view); + Array sel = view_selections_get_all(&vis->win->view); bool ret = marklist_push(vis->win, &vis->win->jumplist, &sel); array_release(&sel); return ret; } static bool marklist_prev(Win *win, MarkList *list) { - View *view = win->view; + View *view = &win->view; bool restore = false; Array cur = view_selections_get_all(view); bool anchored = view_selections_primary_get(view)->anchored; @@ -191,7 +188,7 @@ static bool marklist_prev(Win *win, MarkList *list) { } static bool marklist_next(Win *win, MarkList *list) { - View *view = win->view; + View *view = &win->view; bool anchored = view_selections_primary_get(view)->anchored; for (;;) { Array *next = array_pop(&list->next); diff --git a/vis-modes.c b/vis-modes.c index 41121a863..f6d2b267a 100644 --- a/vis-modes.c +++ b/vis-modes.c @@ -148,7 +148,7 @@ static void vis_mode_normal_enter(Vis *vis, Mode *old) { return; if (vis->autoindent && strcmp(vis->key_prev, "") == 0) { Text *txt = win->file->text; - for (Selection *s = view_selections(win->view); s; s = view_selections_next(s)) { + for (Selection *s = view_selections(&win->view); s; s = view_selections_next(s)) { size_t pos = view_cursors_pos(s); size_t start = text_line_start(txt, pos); size_t end = text_line_end(txt, pos); @@ -189,7 +189,7 @@ static void vis_mode_operator_input(Vis *vis, const char *str, size_t len) { static void vis_mode_visual_enter(Vis *vis, Mode *old) { Win *win = vis->win; if (!old->visual && win) { - for (Selection *s = view_selections(win->view); s; s = view_selections_next(s)) + for (Selection *s = view_selections(&win->view); s; s = view_selections_next(s)) s->anchored = true; } } @@ -197,7 +197,7 @@ static void vis_mode_visual_enter(Vis *vis, Mode *old) { static void vis_mode_visual_line_enter(Vis *vis, Mode *old) { Win *win = vis->win; if (!old->visual && win) { - for (Selection *s = view_selections(win->view); s; s = view_selections_next(s)) + for (Selection *s = view_selections(&win->view); s; s = view_selections_next(s)) s->anchored = true; } if (!vis->action.op) @@ -211,9 +211,9 @@ static void vis_mode_visual_line_leave(Vis *vis, Mode *new) { if (!new->visual) { if (!vis->action.op) window_selection_save(win); - view_selections_clear_all(win->view); + view_selections_clear_all(&win->view); } else { - view_cursors_to(win->view->selection, view_cursor_get(win->view)); + view_cursors_to(win->view.selection, view_cursor_get(&win->view)); } } @@ -222,7 +222,7 @@ static void vis_mode_visual_leave(Vis *vis, Mode *new) { if (!new->visual && win) { if (!vis->action.op) window_selection_save(win); - view_selections_clear_all(win->view); + view_selections_clear_all(&win->view); } } diff --git a/vis-operators.c b/vis-operators.c index 5ce64c490..59abcf6bb 100644 --- a/vis-operators.c +++ b/vis-operators.c @@ -102,7 +102,7 @@ static size_t op_put(Vis *vis, Text *txt, OperatorContext *c) { static size_t op_shift_right(Vis *vis, Text *txt, OperatorContext *c) { char spaces[9] = " "; - spaces[MIN(vis->win->view->tabwidth, LENGTH(spaces) - 1)] = '\0'; + spaces[MIN(vis->win->view.tabwidth, LENGTH(spaces) - 1)] = '\0'; const char *tab = vis->win->expandtab ? spaces : "\t"; size_t tablen = strlen(tab); size_t pos = text_line_begin(txt, c->range.end), prev_pos; @@ -127,7 +127,7 @@ static size_t op_shift_right(Vis *vis, Text *txt, OperatorContext *c) { static size_t op_shift_left(Vis *vis, Text *txt, OperatorContext *c) { size_t pos = text_line_begin(txt, c->range.end), prev_pos; - size_t tabwidth = vis->win->view->tabwidth, tablen; + size_t tabwidth = vis->win->view.tabwidth, tablen; size_t newpos = c->pos; /* if range ends at the begin of a line, skip line break */ @@ -161,7 +161,6 @@ static size_t op_shift_left(Vis *vis, Text *txt, OperatorContext *c) { } static size_t op_cursor(Vis *vis, Text *txt, OperatorContext *c) { - View *view = vis->win->view; Filerange r = text_range_linewise(txt, &c->range); for (size_t line = text_range_line_first(txt, &r); line != EPOS; line = text_range_line_next(txt, &r, line)) { size_t pos; @@ -169,7 +168,7 @@ static size_t op_cursor(Vis *vis, Text *txt, OperatorContext *c) { pos = text_line_finish(txt, line); else pos = text_line_start(txt, line); - view_selections_new_force(view, pos); + view_selections_new_force(&vis->win->view, pos); } return EPOS; } diff --git a/vis-prompt.c b/vis-prompt.c index dd6716136..86699f22a 100644 --- a/vis-prompt.c +++ b/vis-prompt.c @@ -49,7 +49,7 @@ static void prompt_restore(Win *win) { static const char *prompt_enter(Vis *vis, const char *keys, const Arg *arg) { Win *prompt = vis->win; - View *view = prompt->view; + View *view = &prompt->view; Text *txt = prompt->file->text; Win *win = prompt->parent; char *cmd = NULL; @@ -114,8 +114,8 @@ static const char *prompt_enter(Vis *vis, const char *keys, const Arg *arg) { static const char *prompt_esc(Vis *vis, const char *keys, const Arg *arg) { Win *prompt = vis->win; - if (prompt->view->selection_count > 1) { - view_selections_dispose_all(prompt->view); + if (prompt->view.selection_count > 1) { + view_selections_dispose_all(&prompt->view); } else { prompt_restore(prompt); prompt_hide(prompt); @@ -126,7 +126,7 @@ static const char *prompt_esc(Vis *vis, const char *keys, const Arg *arg) { static const char *prompt_up(Vis *vis, const char *keys, const Arg *arg) { vis_motion(vis, VIS_MOVE_LINE_UP); vis_window_mode_unmap(vis->win, VIS_MODE_INSERT, ""); - view_options_set(vis->win->view, UI_OPTION_SYMBOL_EOF); + view_options_set(&vis->win->view, UI_OPTION_SYMBOL_EOF); return keys; } @@ -164,7 +164,7 @@ void vis_prompt_show(Vis *vis, const char *title) { return; Text *txt = prompt->file->text; text_appendf(txt, "%s\n", title); - Selection *sel = view_selections_primary_get(prompt->view); + Selection *sel = view_selections_primary_get(&prompt->view); view_cursors_scroll_to(sel, text_size(txt)-1); prompt->parent = active; prompt->parent_mode = vis->mode; @@ -198,6 +198,6 @@ void vis_message_show(Vis *vis, const char *msg) { size_t pos = text_size(txt); text_appendf(txt, "%s\n", msg); text_save(txt, NULL); - view_cursors_to(win->view->selection, pos); + view_cursors_to(win->view.selection, pos); vis_window_focus(win); } diff --git a/vis-registers.c b/vis-registers.c index 485284c45..6e21a5840 100644 --- a/vis-registers.c +++ b/vis-registers.c @@ -188,7 +188,7 @@ bool register_put_range(Vis *vis, Register *reg, Text *txt, Filerange *range) { size_t vis_register_count(Vis *vis, Register *reg) { if (reg->type == REGISTER_NUMBER) - return vis->win ? vis->win->view->selection_count : 0; + return vis->win ? vis->win->view.selection_count : 0; return array_length(®->values); } diff --git a/vis.c b/vis.c index 9bac2a8ca..4a025f9a2 100644 --- a/vis.c +++ b/vis.c @@ -186,8 +186,7 @@ const char *file_name_get(File *file) { void window_selection_save(Win *win) { Vis *vis = win->vis; - View *view = win->view; - Array sel = view_selections_get_all(view); + Array sel = view_selections_get_all(&win->view); vis_mark_set(win, VIS_MARK_SELECTION, &sel); array_release(&sel); vis_jumplist_save(vis); @@ -203,7 +202,7 @@ static void window_free(Win *win) { other->parent = NULL; } ui_window_free(win->ui); - view_free(win->view); + view_free(&win->view); for (size_t i = 0; i < LENGTH(win->modes); i++) map_free(win->modes[i].bindings); marklist_release(&win->jumplist); @@ -212,16 +211,15 @@ static void window_free(Win *win) { } static void window_draw_colorcolumn(Win *win) { - View *view = win->view; - int cc = view->colorcolumn; + int cc = win->view.colorcolumn; if (cc <= 0) return; size_t lineno = 0; int line_cols = 0; /* Track the number of columns we've passed on each line */ bool line_cc_set = false; /* Has the colorcolumn attribute been set for this line yet */ - int width = view->width; + int width = win->view.width; - for (Line *l = view->topline; l; l = l->next) { + for (Line *l = win->view.topline; l; l = l->next) { if (l->lineno != lineno) { line_cols = 0; line_cc_set = false; @@ -243,19 +241,18 @@ static void window_draw_colorcolumn(Win *win) { static void window_draw_cursorline(Win *win) { Vis *vis = win->vis; - View *view = win->view; - enum UiOption options = UI_OPTIONS_GET(view->ui); + enum UiOption options = UI_OPTIONS_GET(win->view.ui); if (!(options & UI_OPTION_CURSOR_LINE)) return; if (vis->mode->visual || vis->win != win) return; - if (view->selection_count > 1) + if (win->view.selection_count > 1) return; - int width = view->width; - Selection *sel = view_selections_primary_get(view); + int width = win->view.width; + Selection *sel = view_selections_primary_get(&win->view); size_t lineno = sel->line->lineno; - for (Line *l = view->topline; l; l = l->next) { + for (Line *l = win->view.topline; l; l = l->next) { if (l->lineno == lineno) { for (int x = 0; x < width; x++) ui_window_style_set(win->ui, &l->cells[x], UI_STYLE_CURSOR_LINE); @@ -266,7 +263,7 @@ static void window_draw_cursorline(Win *win) { } static void window_draw_selection(Win *win, Selection *cur) { - View *view = win->view; + View *view = &win->view; Filerange sel = view_selections_get(cur); if (!text_range_valid(&sel)) return; @@ -301,7 +298,7 @@ static void window_draw_cursor_matching(Win *win, Selection *cur) { size_t pos_match = text_bracket_match_symbol(win->file->text, pos, "(){}[]\"'`", &limits); if (pos == pos_match) return; - if (!view_coord_get(win->view, pos_match, &line_match, NULL, &col_match)) + if (!view_coord_get(&win->view, pos_match, &line_match, NULL, &col_match)) return; ui_window_style_set(win->ui, &line_match->cells[col_match], UI_STYLE_SELECTION); } @@ -312,16 +309,15 @@ static void window_draw_cursor(Win *win, Selection *cur) { Line *line = cur->line; if (!line) return; - Selection *primary = view_selections_primary_get(win->view); + Selection *primary = view_selections_primary_get(&win->view); ui_window_style_set(win->ui, &line->cells[cur->col], primary == cur ? UI_STYLE_CURSOR_PRIMARY : UI_STYLE_CURSOR); window_draw_cursor_matching(win, cur); return; } static void window_draw_selections(Win *win) { - View *view = win->view; - Filerange viewport = VIEW_VIEWPORT_GET(view); - Selection *sel = view_selections_primary_get(view); + Filerange viewport = VIEW_VIEWPORT_GET(win->view); + Selection *sel = view_selections_primary_get(&win->view); for (Selection *s = view_selections_prev(sel); s; s = view_selections_prev(s)) { window_draw_selection(win, s); size_t pos = view_cursors_pos(s); @@ -341,7 +337,7 @@ static void window_draw_selections(Win *win) { } static void window_draw_eof(Win *win) { - View *view = win->view; + View *view = &win->view; if (view->width == 0) return; for (Line *l = view->lastline->next; l; l = l->next) { @@ -351,7 +347,7 @@ static void window_draw_eof(Win *win) { } void vis_window_draw(Win *win) { - if (!win->ui || !view_update(win->view)) + if (!win->ui || !view_update(&win->view)) return; Vis *vis = win->vis; vis_event_emit(vis, VIS_EVENT_WIN_HIGHLIGHT, win); @@ -369,7 +365,7 @@ void vis_window_draw(Win *win) { void vis_window_invalidate(Win *win) { for (Win *w = win->vis->windows; w; w = w->next) { if (w->file == win->file) - view_draw(w->view); + view_draw(&w->view); } } @@ -379,17 +375,20 @@ Win *window_new_file(Vis *vis, File *file, enum UiOption options) { return NULL; win->vis = vis; win->file = file; - win->view = view_new(file->text); + if (!view_init(&win->view, file->text)) { + free(win); + return NULL; + } win->expandtab = false; win->ui = ui_window_new(&vis->ui, win, options); - if (!win->view || !win->ui) { + if (!win->ui) { window_free(win); return NULL; } marklist_init(&win->jumplist, 32); mark_init(&win->saved_selections); file->refcount++; - view_options_set(win->view, UI_OPTIONS_GET(win->view->ui)); + view_options_set(&win->view, UI_OPTIONS_GET(win->view.ui)); if (vis->windows) vis->windows->prev = win; @@ -416,7 +415,7 @@ bool vis_window_reload(Win *win) { file_free(win->vis, win->file); file->refcount = 1; win->file = file; - view_reload(win->view, file->text); + view_reload(&win->view, file->text); return true; } @@ -428,7 +427,7 @@ bool vis_window_change_file(Win *win, const char* filename) { if (win->file) file_free(win->vis, win->file); win->file = file; - view_reload(win->view, file->text); + view_reload(&win->view, file->text); return true; } @@ -444,8 +443,8 @@ bool vis_window_split(Win *original) { map_copy(win->modes[i].bindings, original->modes[i].bindings); } win->file = original->file; - view_options_set(win->view, UI_OPTIONS_GET(original->view->ui)); - view_cursors_to(win->view->selection, view_cursor_get(original->view)); + view_options_set(&win->view, UI_OPTIONS_GET(original->view.ui)); + view_cursors_to(win->view.selection, view_cursor_get(&original->view)); win->vis->ui.doupdate = true; return true; } @@ -477,7 +476,7 @@ void vis_window_prev(Vis *vis) { void vis_draw(Vis *vis) { for (Win *win = vis->windows; win; win = win->next) - view_draw(win->view); + view_draw(&win->view); } void vis_redraw(Vis *vis) { @@ -671,7 +670,7 @@ void vis_insert_key(Vis *vis, const char *data, size_t len) { Win *win = vis->win; if (!win) return; - for (Selection *s = view_selections(win->view); s; s = view_selections_next(s)) { + for (Selection *s = view_selections(&win->view); s; s = view_selections_next(s)) { size_t pos = view_cursors_pos(s); vis_insert(vis, pos, data, len); view_cursors_scroll_to(s, pos + len); @@ -696,7 +695,7 @@ void vis_replace_key(Vis *vis, const char *data, size_t len) { Win *win = vis->win; if (!win) return; - for (Selection *s = view_selections(win->view); s; s = view_selections_next(s)) { + for (Selection *s = view_selections(&win->view); s; s = view_selections_next(s)) { size_t pos = view_cursors_pos(s); vis_replace(vis, pos, data, len); view_cursors_scroll_to(s, pos + len); @@ -737,7 +736,7 @@ void vis_do(Vis *vis) { return; File *file = win->file; Text *txt = file->text; - View *view = win->view; + View *view = &win->view; Action *a = &vis->action; int count = MAX(a->count, 1); @@ -1508,8 +1507,8 @@ void vis_insert_tab(Vis *vis) { return; } char spaces[9]; - int tabwidth = MIN(vis->win->view->tabwidth, LENGTH(spaces) - 1); - for (Selection *s = view_selections(win->view); s; s = view_selections_next(s)) { + int tabwidth = MIN(vis->win->view.tabwidth, LENGTH(spaces) - 1); + for (Selection *s = view_selections(&win->view); s; s = view_selections_next(s)) { size_t pos = view_cursors_pos(s); int width = text_line_width_get(win->file->text, pos); int count = tabwidth - (width % tabwidth); @@ -1564,9 +1563,8 @@ void vis_insert_nl(Vis *vis) { Win *win = vis->win; if (!win) return; - View *view = win->view; Text *txt = win->file->text; - for (Selection *s = view_selections(view); s; s = view_selections_next(s)) { + for (Selection *s = view_selections(&win->view); s; s = view_selections_next(s)) { size_t pos = view_cursors_pos(s); size_t newpos = vis_text_insert_nl(vis, txt, pos); /* This is a bit of a hack to fix cursor positioning when @@ -1882,5 +1880,5 @@ Text *vis_text(Vis *vis) { View *vis_view(Vis *vis) { Win *win = vis->win; - return win ? win->view : NULL; + return win ? &win->view : NULL; }