From: Michael D. Lowis Date: Tue, 3 Apr 2018 16:22:26 +0000 (-0400) Subject: cleaned up some selection handling code X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=aab84e8479cb5384e8eb1a0698238f5707310342;p=projs%2Ftide.git cleaned up some selection handling code --- diff --git a/inc/edit.h b/inc/edit.h index 2c67658..b98a1c8 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -126,7 +126,6 @@ void view_eof(View* view, bool extsel); void view_undo(View* view); void view_redo(View* view); void view_putstr(View* view, char* str); -void view_append(View* view, char* str); char* view_getstr(View* view, Sel* sel); char* view_getcmd(View* view); char* view_getctx(View* view); diff --git a/lib/buf.c b/lib/buf.c index 585ce83..db1f5b4 100644 --- a/lib/buf.c +++ b/lib/buf.c @@ -28,7 +28,7 @@ void buf_init(Buf* buf) { /* reset the state to defaults */ buf->modified = false; buf->bufsize = 8192; - buf->bufstart = (char*)malloc(buf->bufsize); + buf->bufstart = malloc(buf->bufsize); buf->bufend = buf->bufstart + buf->bufsize; buf->gapstart = buf->bufstart; buf->gapend = buf->bufend; diff --git a/lib/view.c b/lib/view.c index 92e0373..6c50d68 100644 --- a/lib/view.c +++ b/lib/view.c @@ -133,9 +133,7 @@ void view_selprev(View* view) { void view_select(View* view, size_t row, size_t col) { view_setcursor(view, row, col, false); - Sel sel = view->selection; - select_context(view, risword, &sel); - view->selection = sel; + select_context(view, risword, &(view->selection)); } size_t view_selsize(View* view) { @@ -174,12 +172,10 @@ void view_insert(View* view, bool indent, Rune rune) { } void view_delete(View* view, int dir, bool byword) { - Sel* sel = &(view->selection); - if (sel->beg == sel->end) + if (view->selection.beg == view->selection.end) (byword ? view_byword : view_byrune)(view, dir, true); - selswap(sel); - buf_del(&(view->buffer), sel); - move_to(view, false, sel->beg); + buf_del(&(view->buffer), &(view->selection)); + move_to(view, false, view->selection.end); } void view_jumpto(View* view, bool extsel, size_t off) { @@ -228,39 +224,17 @@ void view_redo(View* view) { } void view_putstr(View* view, char* str) { - selswap(&(view->selection)); - unsigned beg = view->selection.beg; - while (*str) { - Rune rune = 0; - size_t length = 0; - while (!utf8decode(&rune, &length, *str++)); - view_insert(view, false, rune); - } - view->selection.beg = beg; -} - -void view_append(View* view, char* str) { - size_t end = buf_end(&(view->buffer)); - if (view->selection.end != end) - view->selection = (Sel){ .beg = end, .end = end }; - if (!num_selected(view->selection) && !buf_iseol(&(view->buffer), view->selection.end-1)) { - buf_putc(&(view->buffer), '\n', &(view->selection)); - view->selection.beg++; - } - unsigned beg = view->selection.beg; - view_putstr(view, str); - view->selection.beg = beg; + buf_puts(&(view->buffer), str, &(view->selection)); } char* view_getstr(View* view, Sel* range) { - return buf_gets(&(view->buffer), &(view->selection)); + return buf_gets(&(view->buffer), (range ? range : &(view->selection))); } char* view_getcmd(View* view) { - Sel sel = view->selection; - if (!num_selected(sel)) - select_context(view, riscmd, &sel); - return view_getstr(view, &sel); + if (!num_selected(view->selection)) + select_context(view, riscmd, &(view->selection)); + return view_getstr(view, NULL); } void view_selctx(View* view) { diff --git a/lib/x11.c b/lib/x11.c index cc60f9f..fd7aa20 100644 --- a/lib/x11.c +++ b/lib/x11.c @@ -191,8 +191,8 @@ void win_quit(void) { uint64_t now = getmillis(); if (!win_buf(EDIT)->modified || (now-before) <= (uint64_t)ClickTime) exit(0); - else - view_append(win_view(TAGS), "File is modified."); +// else +// view_append(win_view(TAGS), "File is modified."); before = now; } @@ -495,8 +495,8 @@ static void xfocus(XEvent* e) { if (X.xic) (e->type == FocusIn ? XSetICFocus : XUnsetICFocus)(X.xic); Buf* buf = win_buf(EDIT); - if (buf->path && buf->modtime != modtime(buf->path)) - view_append(win_view(TAGS), "File modified externally: Get {Put }"); +// if (buf->path && buf->modtime != modtime(buf->path)) +// view_append(win_view(TAGS), "File modified externally: Get {Put }"); } static void xkeypress(XEvent* e) {