From: Michael D. Lowis Date: Thu, 5 Apr 2018 20:08:36 +0000 (-0400) Subject: refactored some more raw access of the cursor/selection. Still a few more to remove... X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=a70b9e8563548ace4cd5c5ec260b6e9fcfdc217f;p=projs%2Ftide.git refactored some more raw access of the cursor/selection. Still a few more to remove before I start refactoring the buffer internals --- diff --git a/lib/buf.c b/lib/buf.c index b8b65ee..cf47ea0 100644 --- a/lib/buf.c +++ b/lib/buf.c @@ -456,10 +456,10 @@ size_t buf_selsz(Buf* buf) { } void buf_selclr(Buf* buf, int dir) { - Sel sel = getsel(buf); - if (dir > 0) sel.beg = sel.end; - else sel.end = sel.beg; - buf->selection = sel; + if (dir > 0) + buf->selection.beg = buf->selection.end; + else + buf->selection.end = buf->selection.beg; } bool buf_insel(Buf* buf, size_t off) { diff --git a/lib/view.c b/lib/view.c index b6eff56..c19c90a 100644 --- a/lib/view.c +++ b/lib/view.c @@ -3,6 +3,9 @@ #include #include +#define BUF (&(view->buffer)) +#define CSRPOS (view->buffer.selection.end) + typedef size_t (*movefn_t)(Buf* buf, size_t pos, int count); static void move_selection(View* view, bool extsel, int move, movefn_t bything); @@ -33,13 +36,13 @@ void view_init(View* view, char* file) { view->sync_needed = true; view->sync_center = true; /* load the file and jump to the address returned from the load function */ - buf_init(&(view->buffer)); - if (file) buf_load(&(view->buffer), file); + buf_init(BUF); + if (file) buf_load(BUF, file); } void view_reload(View* view) { if (view->buffer.path) { - buf_reload(&(view->buffer)); + buf_reload(BUF); view->sync_needed = true; view->sync_center = true; } @@ -47,8 +50,8 @@ void view_reload(View* view) { size_t view_limitrows(View* view, size_t maxrows, size_t ncols) { size_t nrows = 1, pos = 0, col = 0; - while (nrows < maxrows && pos < buf_end(&(view->buffer))) { - Rune r = buf_getrat(&(view->buffer), pos++); + while (nrows < maxrows && pos < buf_end(BUF)) { + Rune r = buf_getrat(BUF, pos++); col += runewidth(col, r); if (col >= ncols || r == '\n') col = 0, nrows++; @@ -79,7 +82,7 @@ void view_resize(View* view, size_t nrows, size_t ncols) { void view_update(View* view, int clrnor, int clrsel, size_t* csrx, size_t* csry) { if (!view->nrows) return; view->clrnor = clrnor, view->clrsel = clrsel; - size_t csr = getsel(view)->end; + size_t csr = CSRPOS; /* scroll the view and reflow the screen lines */ size_t pos = view->rows[0]->off; /* fill the view and scroll if needed */ @@ -116,42 +119,43 @@ void view_setcursor(View* view, size_t row, size_t col, bool extsel) { void view_selword(View* view, size_t row, size_t col) { if (row != SIZE_MAX && col != SIZE_MAX) view_setcursor(view, row, col, false); - buf_selword(&(view->buffer), risbigword); + buf_selword(BUF, risbigword); } void view_selprev(View* view) { if (!view_selsize(view)) - buf_lastins(&(view->buffer)); + buf_lastins(BUF); else - buf_selclr(&(view->buffer), RIGHT); + buf_selclr(BUF, RIGHT); } void view_select(View* view, size_t row, size_t col) { view_setcursor(view, row, col, false); - buf_selctx(&(view->buffer), risword); + buf_selctx(BUF, risword); } size_t view_selsize(View* view) { - return buf_selsz(&(view->buffer)); + return buf_selsz(BUF); } char* view_fetch(View* view, size_t row, size_t col, bool (*isword)(Rune)) { char* str = NULL; size_t off = getoffset(view, row, col); if (off != SIZE_MAX) { + puts("fetch"); /* str = buf_fetchat(buf, isword, off) */ // Sel sel = { .beg = off, .end = off }; -// if (buf_insel(&(view->buffer), NULL, off)) +// if (buf_insel(BUF, NULL, off)) // sel = *(getsel(view)); // else -// buf_selword(&(view->buffer), isword, &sel); +// buf_selword(BUF, isword, &sel); // str = view_getstr(view, &sel); } return str; } bool view_findstr(View* view, int dir, char* str) { - bool found = buf_findstr(&(view->buffer), dir, str); + bool found = buf_findstr(BUF, dir, str); view->sync_needed = true; view->sync_center = true; return found; @@ -161,15 +165,15 @@ void view_insert(View* view, bool indent, Rune rune) { /* ignore non-printable control characters */ if (!isspace(rune) && (rune >= 0 && rune < 0x20)) return; - buf_putc(&(view->buffer), rune); - move_to(view, false, getsel(view)->end); + buf_putc(BUF, rune); + move_to(view, false, CSRPOS); } void view_delete(View* view, int dir, bool byword) { if (!view_selsize(view)) (byword ? view_byword : view_byrune)(view, dir, true); - buf_del(&(view->buffer)); - move_to(view, false, getsel(view)->end); + buf_del(BUF); + move_to(view, false, CSRPOS); } void view_jumpto(View* view, bool extsel, size_t off) { @@ -178,17 +182,17 @@ void view_jumpto(View* view, bool extsel, size_t off) { void view_bol(View* view, bool extsel) { /* determine whether we are jumping to start of content or line */ - Buf* buf = &(view->buffer); - unsigned bol = buf_bol(buf, getsel(view)->end); + Buf* buf = BUF; + unsigned bol = buf_bol(buf, CSRPOS); unsigned boi = bol; for (; ' ' == buf_getrat(buf, boi) || '\t' == buf_getrat(buf, boi); boi++); - unsigned pos = getsel(view)->end; + unsigned pos = CSRPOS; pos = (pos == bol || pos > boi ? boi : bol); move_to(view, extsel, pos); } void view_eol(View* view, bool extsel) { - move_to(view, extsel, buf_eol(&(view->buffer), getsel(view)->end)); + move_to(view, extsel, buf_eol(BUF, CSRPOS)); getsel(view)->col = -1; // Peg cursor to line end } @@ -197,43 +201,43 @@ void view_bof(View* view, bool extsel) { } void view_eof(View* view, bool extsel) { - view_jumpto(view, extsel, buf_end(&(view->buffer))); + view_jumpto(view, extsel, buf_end(BUF)); } void view_setln(View* view, size_t line) { - buf_setln(&(view->buffer), line); + buf_setln(BUF, line); view->sync_center = true; } void view_undo(View* view) { - buf_undo(&(view->buffer)); + buf_undo(BUF); view->sync_needed = true; view->sync_center = !selection_visible(view); } void view_redo(View* view) { - buf_redo(&(view->buffer)); + buf_redo(BUF); view->sync_needed = true; view->sync_center = !selection_visible(view); } void view_putstr(View* view, char* str) { - buf_puts(&(view->buffer), str); + buf_puts(BUF, str); } char* view_getstr(View* view) { - return buf_gets(&(view->buffer)); + return buf_gets(BUF); } char* view_getcmd(View* view) { if (!view_selsize(view)) - buf_selctx(&(view->buffer), riscmd); + buf_selctx(BUF, riscmd); return view_getstr(view); } void view_selctx(View* view) { if (!view_selsize(view)) - buf_selctx(&(view->buffer), risword); + buf_selctx(BUF, risword); } char* view_getctx(View* view) { @@ -258,7 +262,7 @@ void view_scrollpage(View* view, int move) { } Rune view_getrune(View* view) { - return buf_getc(&(view->buffer)); + return buf_getc(BUF); } void view_scrollto(View* view, size_t csr) { @@ -267,7 +271,7 @@ void view_scrollto(View* view, size_t csr) { unsigned last = view->rows[view->nrows-1]->off + view->rows[view->nrows-1]->rlen - 1; while (csr < first) first = scroll_up(view); - while (csr > last && last < buf_end(&(view->buffer))) + while (csr > last && last < buf_end(BUF)) last = scroll_dn(view); view->sync_needed = false; if (view->sync_center) { @@ -277,43 +281,43 @@ void view_scrollto(View* view, size_t csr) { } void view_selectall(View* view) { - buf_selall(&(view->buffer)); + buf_selall(BUF); view->sync_needed = true; } void view_selectobj(View* view, bool (*istype)(Rune)) { - buf_selword(&(view->buffer), istype); + buf_selword(BUF, istype); view->sync_needed = true; } static void move_selection(View* view, bool extsel, int move, movefn_t bything) { view->sync_needed = true; - if (buf_selsz(&(view->buffer)) && !extsel) { - buf_selclr(&(view->buffer), move); + if (buf_selsz(BUF) && !extsel) { + buf_selclr(BUF, move); } else { - getsel(view)->end = bything(&(view->buffer), getsel(view)->end, move); + CSRPOS = bything(BUF, CSRPOS, move); if (bything == buf_byline) - buf_setcol(&(view->buffer)); + buf_setcol(BUF); if (!extsel) - buf_selclr(&(view->buffer), move); + buf_selclr(BUF, move); } /* only update column if not moving vertically */ if (bything != buf_byline) - buf_getcol(&(view->buffer)); + buf_getcol(BUF); } static void move_to(View* view, bool extsel, size_t off) { - Buf* buf = &(view->buffer); - getsel(view)->end = (off > buf_end(buf) ? buf_end(buf) : off); + Buf* buf = BUF; + CSRPOS = (off > buf_end(buf) ? buf_end(buf) : off); if (!extsel) - getsel(view)->beg = getsel(view)->end; + buf_selclr(BUF, RIGHT); buf_getcol(buf); view->sync_needed = true; } static bool selection_visible(View* view) { if (!view->nrows) return true; - size_t csr = getsel(view)->end; + size_t csr = CSRPOS; size_t beg = view->rows[0]->off; size_t end = view->rows[view->nrows-1]->off + view->rows[view->nrows-1]->rlen; @@ -321,7 +325,7 @@ static bool selection_visible(View* view) { } static void find_cursor(View* view, size_t* csrx, size_t* csry) { - size_t csr = getsel(view)->end; + size_t csr = CSRPOS; for (size_t y = 0; y < view->nrows; y++) { size_t start = view->rows[y]->off; size_t end = view->rows[y]->off + view->rows[y]->rlen - 1; @@ -332,7 +336,7 @@ static void find_cursor(View* view, size_t* csrx, size_t* csry) { *csry = y, *csrx = x; break; } - x += runewidth(x, buf_getrat(&(view->buffer), pos++)); + x += runewidth(x, buf_getrat(BUF, pos++)); } break; } @@ -373,10 +377,10 @@ static size_t fill_row(View* view, unsigned row, size_t pos) { view_getrow(view, row)->off = pos; clearrow(view, row); for (size_t x = 0; x < view->ncols;) { - uint32_t attr = (buf_insel(&(view->buffer), pos) ? view->clrsel : view->clrnor); - Rune r = buf_getrat(&(view->buffer), pos++); + uint32_t attr = (buf_insel(BUF, pos) ? view->clrsel : view->clrnor); + Rune r = buf_getrat(BUF, pos++); x += setcell(view, row, x, attr, r); - if (buf_iseol(&(view->buffer), pos-1)) { + if (buf_iseol(BUF, pos-1)) { break; } } @@ -388,7 +392,7 @@ static unsigned prev_screen_line(View* view, unsigned bol, unsigned off) { while (true) { unsigned x; for (x = 0; x < view->ncols && (pos + x) < off; x++) - x += runewidth(x, buf_getrat(&(view->buffer), pos+x)); + x += runewidth(x, buf_getrat(BUF, pos+x)); if ((pos + x) >= off) break; pos += x; } @@ -397,8 +401,8 @@ 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, UP) : bol); + size_t bol = buf_bol(BUF, first); + size_t prevln = (first == bol ? buf_byline(BUF, bol, UP) : bol); if (!first) return first; prevln = prev_screen_line(view, prevln, first); /* delete the last row and shift the others */ @@ -413,7 +417,7 @@ static unsigned scroll_up(View* view) { static unsigned scroll_dn(View* view) { size_t last = view->rows[view->nrows-1]->off + view->rows[view->nrows-1]->rlen - 1; - if (last >= buf_end(&(view->buffer))) return last; + if (last >= buf_end(BUF)) return last; /* delete the first row and shift the others */ if (view->nrows > 1) { free(view->rows[0]); @@ -461,7 +465,7 @@ static size_t getoffset(View* view, size_t row, size_t col) { if (scrrow->cols[i].rune) pos++; } - if (pos >= buf_end(&(view->buffer))) - return buf_end(&(view->buffer)); + if (pos >= buf_end(BUF)) + return buf_end(BUF); return pos; }