}
size_t buf_setln(Buf* buf, size_t line) {
- size_t off = 0;
- while (line > 1 && off < buf_end(buf))
- line--, off = buf_byline(buf, off, DOWN);
- return off;
+ size_t curr = 0, end = buf_end(buf);
+ while (line > 1 && curr < end) {
+ size_t next = buf_byline(buf, curr, DOWN);
+ if (curr == next) break;
+ line--, curr = next;
+ }
+ return curr;
}
size_t buf_getln(Buf* buf, size_t off) {
size_t line = 1, curr = 0, end = buf_end(buf);
- while (curr < off && curr < end)
- line++, curr = buf_byline(buf, curr, DOWN);
+ while (curr < off && curr < end-1) {
+ size_t next = buf_byline(buf, curr, DOWN);
+ if (curr == next) break;
+ line++, curr = next;
+ }
return line;
}
static unsigned scroll_dn(View* view);
static void sync_center(View* view, size_t csr);
static size_t getoffset(View* view, size_t row, size_t col);
-static void sync_line_numbers(View* view);
+static void sync_line_numbers(View* view, size_t pos);
static void apply_colors(View* view);
void view_init(View* view, char* file, void (*errfn)(char*)) {
/* synchronize, scan for, and apply highlighted regions */
if (!view->syntax) return;
-
size_t first = (view->nrows ? view->rows[0]->off : 0),
last = buf_end(&(view->buffer));
if (view->nrows)
/* ignore non-printable control characters */
if (!isspace(rune) && rune < 0x20)
return;
- sync_line_numbers(view);
if (num_selected(view->selection)) {
Sel sel = view->selection;
selswap(&sel);
view->selection = sel;
}
unsigned newpos = buf_insert(&(view->buffer), indent, view->selection.end, rune);
+ sync_line_numbers(view, newpos);
move_to(view, false, newpos);
}
void view_delete(View* view, int dir, bool byword) {
- sync_line_numbers(view);
Sel* sel = &(view->selection);
if (sel->beg == sel->end)
(byword ? view_byword : view_byrune)(view, dir, true);
selswap(sel);
unsigned newpos = buf_delete(&(view->buffer), sel->beg, sel->end);
+ sync_line_numbers(view, newpos);
move_to(view, false, newpos);
}
buf_undo(&(view->buffer), &(view->selection));
view_jumpto(view, true, view->selection.end);
view->sync_center = !selection_visible(view);
- view->sync_lines = true;
+ sync_line_numbers(view, 0);
}
void view_redo(View* view) {
buf_redo(&(view->buffer), &(view->selection));
view_jumpto(view, true, view->selection.end);
view->sync_center = !selection_visible(view);
- view->sync_lines = true;
+ sync_line_numbers(view, 0);
}
void view_putstr(View* view, char* str) {
return pos;
}
-static void sync_line_numbers(View* view) {
- if (!view->nrows ||
- view->selection.beg <= view->rows[0]->off ||
- view->selection.end <= view->rows[0]->off)
+static void sync_line_numbers(View* view, size_t newpos) {
+ if (!view->nrows || newpos <= view->rows[0]->off) {
view->sync_lines = true;
+ if (view->nrows)
+ view->rows[0]->off = buf_bol(&(view->buffer), newpos);
+ }
+
}
static void apply_colors(View* view) {