Skip to content

Commit

Permalink
make Selection unopaque
Browse files Browse the repository at this point in the history
  • Loading branch information
rnpnr committed May 18, 2024
1 parent 162c4c6 commit 8744af9
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 139 deletions.
23 changes: 12 additions & 11 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,7 @@ static const char *repeat(Vis *vis, const char *keys, const Arg *arg) {

static const char *selections_new(Vis *vis, const char *keys, const Arg *arg) {
View *view = vis_view(vis);
bool anchored = view_selections_anchored(view_selections_primary_get(view));
bool anchored = view_selections_primary_get(view)->anchored;
VisCountIterator it = vis_count_iterator_get(vis, 1);
while (vis_count_iterator_next(&it)) {
Selection *sel = NULL;
Expand Down Expand Up @@ -1277,7 +1277,7 @@ static const char *selections_new(Vis *vis, const char *keys, const Arg *arg) {
}
if (sel_new) {
view_selections_primary_set(sel_new);
view_selections_anchor(sel_new, anchored);
sel_new->anchored = anchored;
}
}
vis->action.count = VIS_COUNT_UNKNOWN;
Expand All @@ -1289,9 +1289,10 @@ static const char *selections_align(Vis *vis, const char *keys, const Arg *arg)
Text *txt = vis_text(vis);
int mincol = INT_MAX;
for (Selection *s = view_selections(view); s; s = view_selections_next(s)) {
int col = view_cursors_cell_get(s);
if (col >= 0 && col < mincol)
mincol = col;
if (!s->line)
continue;
if (s->col >= 0 && s->col < mincol)
mincol = s->col;
}
for (Selection *s = view_selections(view); s; s = view_selections_next(s)) {
if (view_cursors_cell_set(s, mincol) == -1) {
Expand Down Expand Up @@ -1362,7 +1363,7 @@ static Selection *selection_new(View *view, Filerange *r, bool isprimary) {
if (!s)
return NULL;
view_selections_set(s, r);
view_selections_anchor(s, true);
s->anchored = true;
if (isprimary)
view_selections_primary_set(s);
return s;
Expand Down Expand Up @@ -1432,7 +1433,7 @@ static const char *selections_match_skip(Vis *vis, const char *keys, const Arg *
static const char *selections_remove(Vis *vis, const char *keys, const Arg *arg) {
View *view = vis_view(vis);
view_selections_dispose(view_selections_primary_get(view));
view_cursor_to(view, view_cursor_get(view));
view_cursors_to(view->selection, view_cursor_get(view));
return keys;
}

Expand Down Expand Up @@ -1878,7 +1879,7 @@ static const char *undo(Vis *vis, const char *keys, const Arg *arg) {
if (pos != EPOS) {
View *view = vis_view(vis);
if (view->selection_count == 1)
view_cursor_to(view, pos);
view_cursors_to(view->selection, pos);
/* redraw all windows in case some display the same file */
vis_draw(vis);
}
Expand All @@ -1890,7 +1891,7 @@ static const char *redo(Vis *vis, const char *keys, const Arg *arg) {
if (pos != EPOS) {
View *view = vis_view(vis);
if (view->selection_count == 1)
view_cursor_to(view, pos);
view_cursors_to(view->selection, pos);
/* redraw all windows in case some display the same file */
vis_draw(vis);
}
Expand All @@ -1903,7 +1904,7 @@ static const char *earlier(Vis *vis, const char *keys, const Arg *arg) {
while (vis_count_iterator_next(&it))
pos = text_earlier(vis_text(vis));
if (pos != EPOS) {
view_cursor_to(vis_view(vis), pos);
view_cursors_to(vis_view(vis)->selection, pos);
/* redraw all windows in case some display the same file */
vis_draw(vis);
}
Expand All @@ -1916,7 +1917,7 @@ static const char *later(Vis *vis, const char *keys, const Arg *arg) {
while (vis_count_iterator_next(&it))
pos = text_later(vis_text(vis));
if (pos != EPOS) {
view_cursor_to(vis_view(vis), pos);
view_cursors_to(vis_view(vis)->selection, pos);
/* redraw all windows in case some display the same file */
vis_draw(vis);
}
Expand Down
10 changes: 5 additions & 5 deletions sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,7 @@ enum SamError sam_cmd(Vis *vis, const char *s) {
if (c->sel) {
if (visual) {
view_selections_set(c->sel, &r);
view_selections_anchor(c->sel, true);
c->sel->anchored = true;
} else {
if (memchr(c->data, '\n', c->len))
view_cursors_to(c->sel, r.start);
Expand All @@ -1281,7 +1281,7 @@ enum SamError sam_cmd(Vis *vis, const char *s) {
Selection *sel = view_selections_new(c->win->view, r.start);
if (sel) {
view_selections_set(sel, &r);
view_selections_anchor(sel, true);
sel->anchored = true;
}
}
}
Expand All @@ -1295,12 +1295,12 @@ enum SamError sam_cmd(Vis *vis, const char *s) {

if (vis->win) {
if (primary_pos != EPOS && view_selection_disposed(vis->win->view))
view_cursor_to(vis->win->view, primary_pos);
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)) {
if (view_selections_anchored(s)) {
if (s->anchored) {
completed = false;
break;
}
Expand Down Expand Up @@ -1572,7 +1572,7 @@ static bool cmd_print(Vis *vis, Win *win, Command *cmd, const char *argv[], Sele
return false;
if (range->start != range->end) {
view_selections_set(sel, range);
view_selections_anchor(sel, true);
sel->anchored = true;
} else {
view_cursors_to(sel, range->start);
view_selection_clear(sel);
Expand Down
3 changes: 1 addition & 2 deletions ui-terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,7 @@ static void ui_window_draw(UiWin *w) {
line = view->topline;
size_t prev_lineno = 0;
Selection *sel = view_selections_primary_get(view);
const Line *cursor_line = view_cursors_line_get(sel);
size_t cursor_lineno = cursor_line->lineno;
size_t cursor_lineno = sel->line->lineno;
char buf[(sizeof(size_t) * CHAR_BIT + 2) / 3 + 1 + 1];
int x = win->x, y = win->y;
int view_width = view->width;
Expand Down
69 changes: 15 additions & 54 deletions view.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,6 @@
* the necessary offset for the last character.
*/

struct Selection {
Mark cursor; /* other selection endpoint where it changes */
Mark anchor; /* position where the selection was created */
bool anchored; /* whether anchor remains fixed */
size_t pos; /* in bytes from the start of the file */
int row, col; /* in terms of zero based screen coordinates */
int lastcol; /* remembered column used when moving across lines */
Line *line; /* screen line on which cursor currently resides */
int generation; /* used to filter out newly created cursors during iteration */
int number; /* how many cursors are located before this one */
View *view; /* associated view to which this cursor belongs */
Selection *prev, *next; /* previous/next cursors ordered by location at creation time */
};

static const SyntaxSymbol symbols_none[] = {
[SYNTAX_SYMBOL_SPACE] = { " " },
[SYNTAX_SYMBOL_TAB] = { " " },
Expand All @@ -64,7 +50,7 @@ static Cell cell_unused;

/* move visible viewport n-lines up/down, redraws the view but does not change
* cursor position which becomes invalid and should be corrected by calling
* view_cursor_to. the return value indicates whether the visible area changed.
* view_cursors_to. the return value indicates whether the visible area changed.
*/
static bool view_viewport_up(View *view, int n);
static bool view_viewport_down(View *view, int n);
Expand Down Expand Up @@ -406,12 +392,6 @@ bool view_coord_get(View *view, size_t pos, Line **retline, int *retrow, int *re
return true;
}

/* move the cursor to the character at pos bytes from the beginning of the file.
* if pos is not in the current viewport, redraw the view to make it visible */
void view_cursor_to(View *view, size_t pos) {
view_cursors_to(view->selection, pos);
}

/* redraw the complete with data starting from view->start bytes into the file.
* stop once the screen is full, update view->end, view->lastline */
void view_draw(View *view) {
Expand Down Expand Up @@ -577,7 +557,7 @@ void view_free(View *view) {
void view_reload(View *view, Text *text) {
view->text = text;
view_selections_clear_all(view);
view_cursor_to(view, 0);
view_cursors_to(view->selection, 0);
}

View *view_new(Text *text) {
Expand Down Expand Up @@ -606,7 +586,7 @@ View *view_new(Text *text) {
return NULL;
}

view_cursor_to(view, 0);
view_cursors_to(view->selection, 0);
return view;
}

Expand Down Expand Up @@ -683,7 +663,8 @@ void view_redraw_top(View *view) {
for (Line *cur = view->topline; cur && cur != line; cur = cur->next)
view->start += cur->len;
view_draw(view);
view_cursor_to(view, view->selection->pos);
/* FIXME: does this logic make sense */
view_cursors_to(view->selection, view->selection->pos);
}

void view_redraw_center(View *view) {
Expand All @@ -705,7 +686,7 @@ void view_redraw_center(View *view) {
break;
}
view_draw(view);
view_cursor_to(view, pos);
view_cursors_to(view->selection, pos);
}

void view_redraw_bottom(View *view) {
Expand All @@ -721,7 +702,7 @@ size_t view_slide_up(View *view, int lines) {
if (sel->line == view->topline)
cursor_set(sel, view->topline, sel->col);
else
view_cursor_to(view, sel->pos);
view_cursors_to(view->selection, sel->pos);
} else {
view_screenline_down(sel);
}
Expand All @@ -736,7 +717,7 @@ size_t view_slide_down(View *view, int lines) {
if (lastline)
cursor_set(sel, view->lastline, col);
else
view_cursor_to(view, sel->pos);
view_cursors_to(view->selection, sel->pos);
} else {
view_screenline_up(sel);
}
Expand All @@ -749,17 +730,17 @@ size_t view_scroll_up(View *view, int lines) {
Line *line = sel->line < view->lastline ? sel->line : view->lastline;
cursor_set(sel, line, view->selection->col);
} else {
view_cursor_to(view, 0);
view_cursors_to(view->selection, 0);
}
return sel->pos;
}

size_t view_scroll_page_up(View *view) {
Selection *sel = view->selection;
if (view->start == 0) {
view_cursor_to(view, 0);
view_cursors_to(view->selection, 0);
} else {
view_cursor_to(view, view->start-1);
view_cursors_to(view->selection, view->start-1);
view_redraw_bottom(view);
view_screenline_begin(sel);
}
Expand All @@ -774,9 +755,9 @@ size_t view_scroll_page_down(View *view) {
size_t view_scroll_halfpage_up(View *view) {
Selection *sel = view->selection;
if (view->start == 0) {
view_cursor_to(view, 0);
view_cursors_to(view->selection, 0);
} else {
view_cursor_to(view, view->start-1);
view_cursors_to(view->selection, view->start-1);
view_redraw_center(view);
view_screenline_begin(sel);
}
Expand All @@ -787,7 +768,7 @@ size_t view_scroll_halfpage_down(View *view) {
size_t end = view->end;
size_t pos = view_scroll_down(view, view->height/2);
if (pos < text_size(view->text))
view_cursor_to(view, end);
view_cursors_to(view->selection, end);
return view->selection->pos;
}

Expand All @@ -797,7 +778,7 @@ size_t view_scroll_down(View *view, int lines) {
Line *line = sel->line > view->topline ? sel->line : view->topline;
cursor_set(sel, line, sel->col);
} else {
view_cursor_to(view, text_size(view->text));
view_cursors_to(view->selection, text_size(view->text));
}
return sel->pos;
}
Expand Down Expand Up @@ -885,10 +866,6 @@ size_t view_cursor_get(View *view) {
return view_cursors_pos(view->selection);
}

Line *view_cursors_line_get(Selection *sel) {
return sel->line;
}

void view_scroll_to(View *view, size_t pos) {
view_cursors_scroll_to(view->selection, pos);
}
Expand Down Expand Up @@ -1167,10 +1144,6 @@ size_t view_cursors_col(Selection *s) {
return text_line_char_get(s->view->text, pos) + 1;
}

int view_cursors_cell_get(Selection *s) {
return s->line ? s->col : -1;
}

int view_cursors_cell_set(Selection *s, int cell) {
if (!s->line || cell < 0)
return -1;
Expand Down Expand Up @@ -1231,10 +1204,6 @@ void view_cursors_place(Selection *s, size_t line, size_t col) {
view_cursors_to(s, pos);
}

void view_selections_anchor(Selection *s, bool anchored) {
s->anchored = anchored;
}

void view_selection_clear(Selection *s) {
s->anchored = false;
s->anchor = s->cursor;
Expand All @@ -1248,10 +1217,6 @@ void view_selections_flip(Selection *s) {
view_cursors_to(s, text_mark_get(s->view->text, s->cursor));
}

bool view_selections_anchored(Selection *s) {
return s->anchored;
}

void view_selections_clear_all(View *view) {
for (Selection *s = view->selections; s; s = s->next)
view_selection_clear(s);
Expand All @@ -1270,10 +1235,6 @@ void view_selections_dispose_all(View *view) {
view_draw(view);
}

Filerange view_selection_get(View *view) {
return view_selections_get(view->selection);
}

Filerange view_selections_get(Selection *s) {
if (!s)
return text_range_empty();
Expand Down
Loading

0 comments on commit 8744af9

Please sign in to comment.