Skip to content

Commit

Permalink
vis: ESC in normal mode clears all cursors
Browse files Browse the repository at this point in the history
  • Loading branch information
martanne committed Jul 28, 2015
1 parent 6205189 commit b134abd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions config.def.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ static KeyBinding vis_marks_set[] = {
};

static KeyBinding vis_mode_normal[] = {
{ { NONE(ESC) }, cursors_clear, { } },
{ { CONTROL('w'), NONE('n') }, cmd, { .s = "open" } },
{ { CONTROL('w'), NONE('c') }, cmd, { .s = "q" } },
{ { CONTROL('w'), NONE('s') }, cmd, { .s = "split" } },
Expand Down
12 changes: 10 additions & 2 deletions view.c
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,13 @@ Cursor *view_cursors_new(View *view) {
return c;
}

int view_cursors_count(View *view) {
int i = 0;
for (Cursor *c = view_cursors(view); c; c = view_cursors_next(c))
i++;
return i;
}

void view_cursors_free(Cursor *c) {
if (!c)
return;
Expand Down Expand Up @@ -1051,9 +1058,10 @@ void view_selections_clear(View *view) {
void view_cursors_clear(View *view) {
for (Cursor *c = view->cursors, *next; c; c = next) {
next = c->next;
view_selections_free(c->sel);
if (c != view->cursor)
if (c != view->cursor) {
view_selections_free(c->sel);
view_cursors_free(c);
}
}
view_draw(view);
}
Expand Down
2 changes: 2 additions & 0 deletions view.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ void view_scroll_to(View*, size_t pos);
void view_cursor_to(View*, size_t pos);
/* create a new cursor */
Cursor *view_cursors_new(View*);
/* get number of active cursors */
int view_cursors_count(View*);
/* dispose an existing cursor */
void view_cursors_free(Cursor*);
/* only keep the main cursor, release all others together with their
Expand Down
15 changes: 12 additions & 3 deletions vis.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ static void totill_repeat(const Arg *arg);
static void totill_reverse(const Arg *arg);
/* replace character at cursor with one read form keyboard */
static void replace(const Arg *arg);
/* remove all but the primary cursor and their selections */
static void cursors_clear(const Arg *arg);
/* adjust action.count by arg->i */
static void count(const Arg *arg);
/* move to the action.count-th line or if not given either to the first (arg->i < 0)
Expand Down Expand Up @@ -825,6 +827,14 @@ static void totill_reverse(const Arg *arg) {
movement(&(const Arg){ .i = type });
}

static void cursors_clear(const Arg *arg) {
View *view = vis->win->view;
if (view_cursors_count(view) > 1)
view_cursors_clear(view);
else
view_cursors_selection_clear(view_cursor(view));
}

static void replace(const Arg *arg) {
Key k = getkey();
if (!k.str[0])
Expand Down Expand Up @@ -1227,10 +1237,9 @@ static void action_do(Action *a) {
Text *txt = vis->win->file->text;
View *view = vis->win->view;
int count = MAX(1, a->count);
Cursor *cursor = view_cursors(view), *next;
bool multiple_cursors = cursor && view_cursors_next(cursor);
bool multiple_cursors = view_cursors_count(view) > 1;

for (; cursor; cursor = next) {
for (Cursor *cursor = view_cursors(view), *next; cursor; cursor = next) {

next = view_cursors_next(cursor);
size_t pos = view_cursors_pos(cursor);
Expand Down

0 comments on commit b134abd

Please sign in to comment.