Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<C-n> flip fix #1082

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 48 additions & 36 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1313,8 +1313,8 @@ static const char *selections_align_indent(Vis *vis, const char *keys, const Arg
for (int i = 0; i < columns; i++) {
int mincol = INT_MAX, maxcol = 0;
for (Selection *s = view_selections_column(view, i); s; s = view_selections_column_next(s, i)) {
Filerange sel = view_selections_get(s);
size_t pos = left_align ? sel.start : sel.end;
Filerange range = view_selections_get(s);
size_t pos = left_align ? range.start : range.end;
int col = text_line_width_get(txt, pos);
if (col < mincol)
mincol = col;
Expand All @@ -1329,9 +1329,9 @@ static const char *selections_align_indent(Vis *vis, const char *keys, const Arg
memset(buf, ' ', len);

for (Selection *s = view_selections_column(view, i); s; s = view_selections_column_next(s, i)) {
Filerange sel = view_selections_get(s);
size_t pos = left_align ? sel.start : sel.end;
size_t ipos = sel.start;
Filerange range = view_selections_get(s);
size_t pos = left_align ? range.start : range.end;
size_t ipos = range.start;
int col = text_line_width_get(txt, pos);
if (col < maxcol) {
size_t off = maxcol - col;
Expand Down Expand Up @@ -1373,15 +1373,15 @@ static const char *selections_match_next(Vis *vis, const char *keys, const Arg *
Text *txt = vis_text(vis);
View *view = vis_view(vis);
Selection *s = view_selections_primary_get(view);
Filerange sel = view_selections_get(s);
if (!text_range_valid(&sel))
Filerange range = view_selections_get(s);
if (!text_range_valid(&range))
return keys;

static bool match_word;

if (view->selection_count == 1) {
Filerange word = text_object_word(txt, view_cursors_pos(s));
match_word = text_range_equal(&sel, &word);
match_word = text_range_equal(&range, &word);
}

Filerange (*find_next)(Text *, size_t, const char *) = text_object_word_find_next;
Expand All @@ -1391,29 +1391,41 @@ static const char *selections_match_next(Vis *vis, const char *keys, const Arg *
find_prev = text_object_find_prev;
}

char *buf = text_bytes_alloc0(txt, sel.start, text_range_size(&sel));
char *buf = text_bytes_alloc0(txt, range.start, text_range_size(&range));
if (!buf)
return keys;

bool flip;
bool match_all = arg->b;
Filerange primary = sel;
Filerange primary = range;
Selection *sel;

for (;;) {
sel = find_next(txt, sel.end, buf);
if (!text_range_valid(&sel))
flip = view_cursor_get(view) == primary.start;
range = find_next(txt, range.end, buf);
if (!text_range_valid(&range))
break;
if (selection_new(view, &sel, !match_all) && !match_all)
goto out;
if (sel = selection_new(view, &range, !match_all)) {
if (flip)
view_selections_flip(sel);
if (!match_all)
goto out;
}
}

sel = primary;
range = primary;

for (;;) {
sel = find_prev(txt, sel.start, buf);
if (!text_range_valid(&sel))
break;
if (selection_new(view, &sel, !match_all) && !match_all)
flip = view_cursor_get(view) == primary.start;
range = find_prev(txt, range.start, buf);
if (!text_range_valid(&range))
break;
if (sel = selection_new(view, &range, !match_all)) {
if (flip)
view_selections_flip(sel);
if (!match_all)
break;
}
}

out:
Expand Down Expand Up @@ -1530,12 +1542,12 @@ static const char *selections_rotate(Vis *vis, const char *keys, const Arg *arg)
next = view_selections_next(s);
size_t line_next = 0;

Filerange sel = view_selections_get(s);
Filerange range = view_selections_get(s);
Rotate rot;
rot.sel = s;
rot.len = text_range_size(&sel);
rot.len = text_range_size(&range);
if ((rot.data = malloc(rot.len)))
rot.len = text_bytes_get(txt, sel.start, rot.len, rot.data);
rot.len = text_bytes_get(txt, range.start, rot.len, rot.data);
else
rot.len = 0;
array_add(&arr, &rot);
Expand All @@ -1553,15 +1565,15 @@ static const char *selections_rotate(Vis *vis, const char *keys, const Arg *arg)
Rotate *newrot = array_get(&arr, j);
if (!oldrot || !newrot || oldrot == newrot)
continue;
Filerange newsel = view_selections_get(newrot->sel);
if (!text_range_valid(&newsel))
Filerange newrange = view_selections_get(newrot->sel);
if (!text_range_valid(&newrange))
continue;
if (!text_delete_range(txt, &newsel))
if (!text_delete_range(txt, &newrange))
continue;
if (!text_insert(txt, newsel.start, oldrot->data, oldrot->len))
if (!text_insert(txt, newrange.start, oldrot->data, oldrot->len))
continue;
newsel.end = newsel.start + oldrot->len;
view_selections_set(newrot->sel, &newsel);
newrange.end = newrange.start + oldrot->len;
view_selections_set(newrot->sel, &newrange);
free(oldrot->data);
}
array_clear(&arr);
Expand All @@ -1579,15 +1591,15 @@ static const char *selections_trim(Vis *vis, const char *keys, const Arg *arg) {
View *view = vis_view(vis);
for (Selection *s = view_selections(view), *next; s; s = next) {
next = view_selections_next(s);
Filerange sel = view_selections_get(s);
if (!text_range_valid(&sel))
Filerange range = view_selections_get(s);
if (!text_range_valid(&range))
continue;
for (char b; sel.start < sel.end && text_byte_get(txt, sel.end-1, &b)
&& isspace((unsigned char)b); sel.end--);
for (char b; sel.start <= sel.end && text_byte_get(txt, sel.start, &b)
&& isspace((unsigned char)b); sel.start++);
if (sel.start < sel.end) {
view_selections_set(s, &sel);
for (char b; range.start < range.end && text_byte_get(txt, range.end-1, &b)
&& isspace((unsigned char)b); range.end--);
for (char b; range.start <= range.end && text_byte_get(txt, range.start, &b)
&& isspace((unsigned char)b); range.start++);
if (range.start < range.end) {
view_selections_set(s, &range);
} else if (!view_selections_dispose(s)) {
vis_mode_switch(vis, VIS_MODE_NORMAL);
}
Expand Down
16 changes: 8 additions & 8 deletions view.c
Original file line number Diff line number Diff line change
Expand Up @@ -1229,10 +1229,10 @@ Filerange view_selections_get(Selection *s) {
Text *txt = s->view->text;
size_t anchor = text_mark_get(txt, s->anchor);
size_t cursor = text_mark_get(txt, s->cursor);
Filerange sel = text_range_new(anchor, cursor);
if (text_range_valid(&sel))
sel.end = text_char_next(txt, sel.end);
return sel;
Filerange range = text_range_new(anchor, cursor);
if (text_range_valid(&range))
range.end = text_char_next(txt, range.end);
return range;
}

bool view_selections_set(Selection *s, const Filerange *r) {
Expand All @@ -1255,10 +1255,10 @@ Filerange view_regions_restore(View *view, SelectionRegion *s) {
Text *txt = view->text;
size_t anchor = text_mark_get(txt, s->anchor);
size_t cursor = text_mark_get(txt, s->cursor);
Filerange sel = text_range_new(anchor, cursor);
if (text_range_valid(&sel))
sel.end = text_char_next(txt, sel.end);
return sel;
Filerange range = text_range_new(anchor, cursor);
if (text_range_valid(&range))
range.end = text_char_next(txt, range.end);
return range;
}

bool view_regions_save(View *view, Filerange *r, SelectionRegion *s) {
Expand Down
8 changes: 4 additions & 4 deletions vis.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,13 @@ static void window_draw_cursorline(Win *win) {

static void window_draw_selection(Win *win, Selection *cur) {
View *view = &win->view;
Filerange sel = view_selections_get(cur);
if (!text_range_valid(&sel))
Filerange range = view_selections_get(cur);
if (!text_range_valid(&range))
return;
Line *start_line; int start_col;
Line *end_line; int end_col;
view_coord_get(view, sel.start, &start_line, NULL, &start_col);
view_coord_get(view, sel.end, &end_line, NULL, &end_col);
view_coord_get(view, range.start, &start_line, NULL, &start_col);
view_coord_get(view, range.end, &end_line, NULL, &end_col);
if (!start_line && !end_line)
return;
if (!start_line) {
Expand Down
Loading