Skip to content

Commit

Permalink
remove some view pointer chasing
Browse files Browse the repository at this point in the history
Same as previous commit each window only has a single View. No
need for it to be stored elsewhere in memory.
  • Loading branch information
rnpnr committed May 21, 2024
1 parent 3bbc0cc commit 18aa569
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 151 deletions.
25 changes: 12 additions & 13 deletions sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1557,17 +1557,16 @@ 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;
}

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) {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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));
Expand Down
12 changes: 6 additions & 6 deletions ui-terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 7 additions & 12 deletions view.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions view.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*);
/**
Expand All @@ -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.
Expand Down
34 changes: 17 additions & 17 deletions vis-cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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]);
Expand Down
2 changes: 1 addition & 1 deletion vis-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
Loading

0 comments on commit 18aa569

Please sign in to comment.