From: Michael D. Lowis Date: Wed, 4 Apr 2018 01:21:45 +0000 (-0400) Subject: moved context selection to buffer X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=4becab2084d49f4333a6c78ab5665039dc5535cc;p=projs%2Ftide.git moved context selection to buffer --- diff --git a/inc/edit.h b/inc/edit.h index 8736aac..ea0a088 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -67,6 +67,7 @@ void buf_selline(Buf* buf, Sel* sel); void buf_selword(Buf* buf, bool (*isword)(Rune), Sel* sel); void buf_selblock(Buf* buf, Rune beg, Rune end, Sel* sel); void buf_selall(Buf* buf, Sel* sel); +void buf_selctx(Buf* buf, bool (*isword)(Rune), Sel* p_sel); size_t buf_byrune(Buf* buf, size_t pos, int count); size_t buf_byword(Buf* buf, size_t pos, int count); diff --git a/lib/buf.c b/lib/buf.c index a2cee70..d7cd6fa 100644 --- a/lib/buf.c +++ b/lib/buf.c @@ -330,6 +330,27 @@ void buf_selall(Buf* buf, Sel* p_sel) { setsel(buf, p_sel, &sel); } +void buf_selctx(Buf* buf, bool (*isword)(Rune), Sel* p_sel) { + Sel sel = getsel(buf, p_sel); + size_t bol = buf_bol(buf, sel.end); + Rune r = buf_getc(buf, &sel); + if (r == '(' || r == ')') + buf_selblock(buf, '(', ')', &sel); + else if (r == '[' || r == ']') + buf_selblock(buf, '[', ']', &sel); + else if (r == '{' || r == '}') + buf_selblock(buf, '{', '}', &sel); + else if (sel.end == bol || r == '\n') + buf_selline(buf, &sel); + else if (risword(r)) + buf_selword(buf, isword, &sel); + else + buf_selword(buf, risbigword, &sel); + buf_getcol(buf, &sel); + setsel(buf, p_sel, &sel); +} + + size_t buf_byrune(Buf* buf, size_t pos, int count) { int move = (count < 0 ? -1 : 1); count *= move; // remove the sign if there is one diff --git a/lib/view.c b/lib/view.c index 13110ef..31f36bf 100644 --- a/lib/view.c +++ b/lib/view.c @@ -7,7 +7,6 @@ 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); static void move_to(View* view, bool extsel, size_t off); -static void select_context(View* view, bool (*isword)(Rune), Sel* sel); static bool selection_visible(View* view); static void find_cursor(View* view, size_t* csrx, size_t* csry); static void clearrow(View* view, size_t row); @@ -117,19 +116,19 @@ 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, getsel(view)); + buf_selword(&(view->buffer), risbigword, NULL); } void view_selprev(View* view) { if (!view_selsize(view)) - buf_lastins(&(view->buffer), getsel(view)); + buf_lastins(&(view->buffer), NULL); else buf_selclr(&(view->buffer), NULL, RIGHT); } void view_select(View* view, size_t row, size_t col) { view_setcursor(view, row, col, false); - select_context(view, risword, getsel(view)); + buf_selctx(&(view->buffer), risword, NULL); } size_t view_selsize(View* view) { @@ -151,7 +150,7 @@ char* view_fetch(View* view, size_t row, size_t col, bool (*isword)(Rune)) { } bool view_findstr(View* view, int dir, char* str) { - bool found = buf_findstr(&(view->buffer), getsel(view), dir, str); + bool found = buf_findstr(&(view->buffer), NULL, dir, str); view->sync_needed = true; view->sync_center = true; return found; @@ -161,14 +160,14 @@ 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, getsel(view)); + buf_putc(&(view->buffer), rune, NULL); move_to(view, false, getsel(view)->end); } 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), getsel(view)); + buf_del(&(view->buffer), NULL); move_to(view, false, getsel(view)->end); } @@ -201,24 +200,24 @@ void view_eof(View* view, bool extsel) { } void view_setln(View* view, size_t line) { - buf_setln(&(view->buffer), getsel(view), line); + buf_setln(&(view->buffer), NULL, line); view->sync_center = true; } void view_undo(View* view) { - buf_undo(&(view->buffer), getsel(view)); + buf_undo(&(view->buffer), NULL); view->sync_needed = true; view->sync_center = !selection_visible(view); } void view_redo(View* view) { - buf_redo(&(view->buffer), getsel(view)); + buf_redo(&(view->buffer), NULL); view->sync_needed = true; view->sync_center = !selection_visible(view); } void view_putstr(View* view, char* str) { - buf_puts(&(view->buffer), str, getsel(view)); + buf_puts(&(view->buffer), str, NULL); } char* view_getstr(View* view, Sel* range) { @@ -227,13 +226,13 @@ char* view_getstr(View* view, Sel* range) { char* view_getcmd(View* view) { if (!view_selsize(view)) - select_context(view, riscmd, getsel(view)); + buf_selctx(&(view->buffer), riscmd, NULL); return view_getstr(view, NULL); } void view_selctx(View* view) { if (!view_selsize(view)) - select_context(view, risword, getsel(view)); + buf_selctx(&(view->buffer), risword, NULL); } char* view_getctx(View* view) { @@ -258,7 +257,7 @@ void view_scrollpage(View* view, int move) { } Rune view_getrune(View* view) { - return buf_getc(&(view->buffer), getsel(view)); + return buf_getc(&(view->buffer), NULL); } void view_scrollto(View* view, size_t csr) { @@ -277,12 +276,12 @@ void view_scrollto(View* view, size_t csr) { } void view_selectall(View* view) { - buf_selall(&(view->buffer), getsel(view)); + buf_selall(&(view->buffer), NULL); view->sync_needed = true; } void view_selectobj(View* view, bool (*istype)(Rune)) { - buf_selword(&(view->buffer), istype, getsel(view)); + buf_selword(&(view->buffer), istype, NULL); view->sync_needed = true; } @@ -294,7 +293,7 @@ static void move_selection(View* view, bool extsel, int move, movefn_t bything) Sel* sel = getsel(view); sel->end = bything(&(view->buffer), sel->end, move); if (bything == buf_byline) - buf_setcol(&(view->buffer), getsel(view)); + buf_setcol(&(view->buffer), NULL); if (!extsel) buf_selclr(&(view->buffer), sel, move); } /* only update column if not moving vertically */ @@ -307,29 +306,10 @@ static void move_to(View* view, bool extsel, size_t off) { getsel(view)->end = (off > buf_end(buf) ? buf_end(buf) : off); if (!extsel) getsel(view)->beg = getsel(view)->end; - buf_getcol(buf, getsel(view)); + buf_getcol(buf, NULL); view->sync_needed = true; } -static void select_context(View* view, bool (*isword)(Rune), Sel* sel) { - Buf* buf = &(view->buffer); - size_t bol = buf_bol(buf, sel->end); - Rune r = buf_getc(buf, sel); - if (r == '(' || r == ')') - buf_selblock(buf, '(', ')', sel); - else if (r == '[' || r == ']') - buf_selblock(buf, '[', ']', sel); - else if (r == '{' || r == '}') - buf_selblock(buf, '{', '}', sel); - else if (sel->end == bol || r == '\n') - buf_selline(buf, sel); - else if (risword(r)) - buf_selword(buf, isword, sel); - else - buf_selword(buf, risbigword, sel); - buf_getcol(&(view->buffer), getsel(view)); -} - static bool selection_visible(View* view) { if (!view->nrows) return true; size_t csr = getsel(view)->end;