Skip to content

Commit

Permalink
vis: indicate primary cursor number in status bar
Browse files Browse the repository at this point in the history
If there exist multiple cursors, [n/m] is added to the status bar.
Meaning the n-th cursor out of the existing m cursors is currently
the primary one.
  • Loading branch information
martanne committed Apr 8, 2016
1 parent b9b6b8f commit a2abb7d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
18 changes: 12 additions & 6 deletions ui-curses.c
Original file line number Diff line number Diff line change
Expand Up @@ -641,15 +641,21 @@ static void ui_window_draw_status(UiWin *w) {
text_modified(vis_file_text(win->file)) ? "[+]" : "",
vis_macro_recording(vis) ? "recording": "");

char buf[4*32] = "", *msg = buf;
int cursor_count = view_cursors_count(win->view);
if (cursor_count > 1) {
Cursor *c = view_cursors_primary_get(win->view);
int cursor_number = view_cursors_number(c) + 1;
msg += sprintf(msg, "[%d/%d] ", cursor_number, cursor_count);
}

if (!(win->options & UI_OPTION_LARGE_FILE)) {
CursorPos pos = view_cursor_getpos(win->view);
char buf[win->width + 1];
int len = snprintf(buf, win->width, "%zd, %zd", pos.line, pos.col);
if (len > 0) {
buf[len] = '\0';
mvwaddstr(win->winstatus, 0, win->width - len - 1, buf);
}
msg += sprintf(msg, "%zd, %zd", pos.line, pos.col);
}

if (buf[0])
mvwaddstr(win->winstatus, 0, win->width - (msg - buf) - 1, buf);
}

static void ui_window_draw(UiWin *w) {
Expand Down
29 changes: 21 additions & 8 deletions view.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ struct Cursor { /* cursor position */
Mark lastsel_cursor;/* used to restore it */
Register reg; /* per cursor register to support yank/put operation */
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 */
Cursor *prev, *next;/* previous/next cursors in no particular order */
Cursor *prev, *next;/* previous/next cursors ordered by location at creation time */
};

/* Viewable area, showing part of a file. Keeps track of cursors and selections.
Expand All @@ -79,6 +80,7 @@ struct View {
Line *lastline; /* last currently used line, always <= bottomline */
Line *bottomline; /* bottom of view, might be unused if lastline < bottomline */
Cursor *cursor; /* main cursor, always placed within the visible viewport */
int cursor_count; /* how many cursors do currently exist */
Line *line; /* used while drawing view content, line where next char will be drawn */
int col; /* used while drawing view content, column where next char will be drawn */
const SyntaxSymbol *symbols[SYNTAX_SYMBOL_LAST]; /* symbols to use for white spaces etc */
Expand Down Expand Up @@ -1066,6 +1068,7 @@ Cursor *view_cursors_new(View *view, size_t pos) {
if (!view->cursors) {
view->cursor = c;
view->cursors = c;
view->cursor_count = 1;
return c;
}

Expand All @@ -1090,15 +1093,21 @@ Cursor *view_cursors_new(View *view, size_t pos) {
if (pos == cur)
goto err;

for (Cursor *after = next; after; after = after->next)
after->number++;

c->prev = prev;
c->next = next;
if (prev)
prev->next = c;
if (next)
next->prev = c;
if (!prev)
if (prev) {
prev->next = c;
c->number = prev->number + 1;
} else {
view->cursors = c;
}
view->cursor = c;
view->cursor_count++;;
view_cursors_to(c, pos);
return c;
err:
Expand All @@ -1107,10 +1116,11 @@ Cursor *view_cursors_new(View *view, size_t pos) {
}

int view_cursors_count(View *view) {
int i = 0;
for (Cursor *c = view->cursors; c; c = c->next)
i++;
return i;
return view->cursor_count;
}

int view_cursors_number(Cursor *c) {
return c->number;
}

int view_cursors_column_count(View *view) {
Expand Down Expand Up @@ -1174,6 +1184,8 @@ static void view_cursors_free(Cursor *c) {
if (!c)
return;
register_release(&c->reg);
for (Cursor *after = c->next; after; after = after->next)
after->number--;
if (c->prev)
c->prev->next = c->next;
if (c->next)
Expand All @@ -1182,6 +1194,7 @@ static void view_cursors_free(Cursor *c) {
c->view->cursors = c->next;
if (c->view->cursor == c)
c->view->cursor = c->next ? c->next : c->prev;
c->view->cursor_count--;
free(c);
}

Expand Down
2 changes: 2 additions & 0 deletions view.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ void view_cursor_to(View*, size_t pos);
Cursor *view_cursors_new(View*, size_t pos);
/* get number of active cursors */
int view_cursors_count(View*);
/* get index/relative order at time of creation of a cursor [0,count-1] */
int view_cursors_number(Cursor*);
/* exist there more than 1 cursor */
bool view_cursors_multiple(View*);
/* dispose an existing cursor with its associated selection (if any),
Expand Down

0 comments on commit a2abb7d

Please sign in to comment.