]> git.mdlowis.com Git - projs/tide.git/commitdiff
fixed line number updates for scroll up and optimized line number updates to only...
authorMichael D. Lowis <mike@mdlowis.com>
Tue, 6 Jun 2017 02:47:13 +0000 (22:47 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Tue, 6 Jun 2017 02:47:13 +0000 (22:47 -0400)
inc/edit.h
lib/view.c
lib/win.c

index 25efe33270b2c0ac527c4e314e18d5183ca7a5d3..6b8cca29c73ea077ce466d929fc60dccc99d2011 100644 (file)
@@ -138,6 +138,7 @@ typedef struct {
 typedef struct {
     bool sync_needed; /* whether the view needs to be synced with cursor */
     bool sync_center; /* cursor should be centered on screen if possible */
+    bool sync_lines;  /* whether the line numbers should be recalculated */
     size_t nrows;     /* number of rows in the view */
     size_t ncols;     /* number of columns in the view */
     Row** rows;       /* array of row data structures */
index 542a7db72e4d01b35c1d3f63ce9611baf4749a5e..dd70b7d011c59018ac693d122621a65192e87f53 100644 (file)
@@ -34,6 +34,7 @@ void view_init(View* view, char* file, void (*errfn)(char*)) {
     view->selection   = (Sel){ 0 };
     view->sync_needed = true;
     view->sync_center = true;
+    view->sync_lines  = true;
     view->prev_csr    = 0;
     /* load the file and jump to the address returned from the load function */
     buf_init(&(view->buffer), errfn);
@@ -78,7 +79,6 @@ void view_resize(View* view, size_t nrows, size_t ncols) {
         view->rows[i] = calloc(1, sizeof(Row) + (ncols * sizeof(UGlyph)));
     /* update dimensions */
     view->rows[0]->off = off;
-    //view->rows[0]->off = line;
     view->nrows = nrows;
     view->ncols = ncols;
 }
@@ -87,7 +87,11 @@ void view_update(View* view, size_t* csrx, size_t* csry) {
     if (!view->nrows) return;
     size_t csr = view->selection.end;
     /* scroll the view and reflow the screen lines */
-    size_t pos = view->rows[0]->off, line = buf_getln(&(view->buffer), pos);
+    size_t pos = view->rows[0]->off, line = view->rows[0]->line;
+    if (!line || view->sync_lines) {
+        line = buf_getln(&(view->buffer), pos);
+        view->sync_lines = false;
+    }
     for (size_t y = 0; y < view->nrows; y++)
         pos = fill_row(view, y, pos, &line);
     if (view->sync_needed)
@@ -188,10 +192,20 @@ bool view_findstr(View* view, int dir, char* str) {
     return found;
 }
 
+static void update_lines(View* view) {
+    if (!view->nrows) return;
+    if (view->selection.beg <= view->rows[0]->off ||
+        view->selection.end <= view->rows[0]->off)
+        view->sync_lines = true;
+}
+
 void view_insert(View* view, bool indent, Rune rune) {
     /* ignore non-printable control characters */
     if (!isspace(rune) && rune < 0x20)
         return;
+
+    update_lines(view);
+
     if (num_selected(view->selection)) {
         Sel sel = view->selection;
         selswap(&sel);
@@ -203,6 +217,8 @@ void view_insert(View* view, bool indent, Rune rune) {
 }
 
 void view_delete(View* view, int dir, bool byword) {
+    update_lines(view);
+
     Sel* sel = &(view->selection);
     if (sel->beg == sel->end)
         (byword ? view_byword : view_byrune)(view, dir, true);
@@ -569,9 +585,10 @@ static unsigned prev_screen_line(View* view, unsigned bol, unsigned off) {
 }
 
 static unsigned scroll_up(View* view) {
-    size_t first  = view->rows[0]->off;
-    size_t bol    = buf_bol(&(view->buffer), first);
-    size_t prevln = (first == bol ? buf_byline(&(view->buffer), bol, -1) : bol);
+    size_t first   = view->rows[0]->off;
+    size_t bol     = buf_bol(&(view->buffer), first);
+    size_t prevln  = (first == bol ? buf_byline(&(view->buffer), bol, -1) : bol);
+    size_t linenum = (prevln < bol ? view->rows[0]->line-1 : view->rows[0]->line);
     if (!first) return first;
     prevln = prev_screen_line(view, prevln, first);
     /* delete the last row and shift the others */
@@ -579,6 +596,7 @@ static unsigned scroll_up(View* view) {
     memmove(&view->rows[1], &view->rows[0], sizeof(Row*) * (view->nrows-1));
     view->rows[0] = calloc(1, sizeof(Row) + (view->ncols * sizeof(UGlyph)));
     view->rows[0]->off = prevln;
+    view->rows[0]->line = linenum;
     /* fill in row content */
     fill_row(view, 0, view->rows[0]->off, NULL);
     return view->rows[0]->off;
index 557a01cf93f1d1b4220d215b309243671bc08dd9..1387d0099cd7a32b78c412642391951d3ead7f2d 100644 (file)
--- a/lib/win.c
+++ b/lib/win.c
@@ -225,10 +225,12 @@ static void onredraw(int width, int height) {
         }
 
         size_t gcols = gutter_cols();
-        for (size_t y = 0; y < view->nrows; y++) {
+        for (size_t line = 0, y = 0; y < view->nrows; y++) {
             Row* row = view_getrow(view, y);
-            draw_line_num(Regions[i].x - (gcols * fwidth) - 5, Regions[i].y + ((y+1) * fheight), gcols, row->line);
+            if (line != row->line)
+                draw_line_num(Regions[i].x - (gcols * fwidth) - 5, Regions[i].y + ((y+1) * fheight), gcols, row->line);
             draw_glyphs(Regions[i].x, Regions[i].y + ((y+1) * fheight), row->cols, row->rlen, row->len);
+            line = row->line;
         }
     }