From b2d1512afea60669d515e154ce679ea0d5cad0a9 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Fri, 20 Apr 2018 22:36:47 -0400 Subject: [PATCH] fixed cursor limit handling for movements --- lib/buf.c | 5 +++-- lib/view.c | 4 +--- lib/x11.c | 44 ++++++++++++++++++-------------------------- 3 files changed, 22 insertions(+), 31 deletions(-) diff --git a/lib/buf.c b/lib/buf.c index 64c8385..a5ebcd2 100644 --- a/lib/buf.c +++ b/lib/buf.c @@ -295,9 +295,10 @@ void buf_selctx(Buf* buf, bool (*isword)(Rune)) { 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 - for (; count > 0; count--) - if (pos > 0 || pos < buf_end(buf)) + for (; count > 0; count--) { + if ((pos > 0 && move < 0) || (pos < buf_end(buf) && move > 0)) pos += move; + } return pos; } diff --git a/lib/view.c b/lib/view.c index 31485b0..b3463f1 100644 --- a/lib/view.c +++ b/lib/view.c @@ -163,14 +163,12 @@ void view_byline(View* view, int move, bool extsel) { void view_setcursor(View* view, size_t row, size_t col, bool extsel) { size_t i = 0, y = 0, idx = view->index + row; if (idx >= view->nrows) return; - printf("row: %ld %ld\n", row, idx); Row* selrow = view->rows[idx]; for (; i < selrow->len; i++) { y += selrow->cols[i].width; if (col < y) break; } - getsel(view)->end = selrow[i].off; - printf("clicked: %ld\n", getsel(view)->end); + getsel(view)->end = selrow->cols[i].off; } void view_selword(View* view, size_t row, size_t col) { diff --git a/lib/x11.c b/lib/x11.c index 74a7105..6106b88 100644 --- a/lib/x11.c +++ b/lib/x11.c @@ -285,23 +285,6 @@ static void draw_rect(int color, int x, int y, int width, int height) { XftColorFree(X.display, X.visual, X.colormap, &clr); } -static void draw_glyphs(size_t x, size_t y, UGlyph* glyphs, size_t len) { - XftGlyphSpec specs[len]; - for (size_t i = 0; i < len; i++) { - int rune = glyphs[i].rune; - if (rune == '\r' || rune == '\n' || rune == '\t') - rune = ' '; - specs[i].glyph = XftCharIndex(X.display, X.font, rune); - specs[i].x = x; - specs[i].y = y + X.font->ascent; - x += glyphs[i].width; - } - XftColor fgc; - xftcolor(&fgc, EditFg); - XftDrawGlyphSpec(X.xft, &fgc, X.font, specs, len); - XftColorFree(X.display, X.visual, X.colormap, &fgc); -} - static void draw_view(int i, size_t nrows, drawcsr* csr, int bg, int fg, int sel) { size_t fwidth = font_width(); size_t fheight = X.font->height; @@ -313,15 +296,24 @@ static void draw_view(int i, size_t nrows, drawcsr* csr, int bg, int fg, int sel draw_rect(bg, csr->x, csr->y, csr->w, (nrows * fheight) + 9); for (size_t i = 0; i < nrows; i++) { Row* row = view_getrow(view, i + view->index); - draw_glyphs(csr->x + 2, csr->y + 2 + (i * fheight), row->cols, row->len); - } - /* place the cursor on screen */ - if (!view_selsize(view) && csrx != SIZE_MAX && csry != SIZE_MAX) { - draw_rect( - (i == TAGS ? TagsCsr : EditCsr), - csr->x + 2 + (csrx * fwidth), - csr->y + 2 + (csry * fheight), - 1, fheight); + size_t x = (csr->x + 2), y = (csr->y + 2 + (i * fheight)); + XftGlyphSpec specs[row->len]; + for (size_t i = 0; i < row->len; i++) { + int rune = row->cols[i].rune; + if (rune == '\r' || rune == '\n' || rune == '\t') + rune = ' '; + if (row->cols[i].off == view->buffer.selection.end) + draw_rect((i == TAGS ? TagsCsr : EditCsr), x, y, 1, fheight); + specs[i].glyph = XftCharIndex(X.display, X.font, rune); + specs[i].x = x; + specs[i].y = y + X.font->ascent; + x += row->cols[i].width; + } + XftColor fgc; + xftcolor(&fgc, EditFg); + XftDrawGlyphSpec(X.xft, &fgc, X.font, specs, row->len); + XftColorFree(X.display, X.visual, X.colormap, &fgc); + } csr->y += (nrows * fheight) + 3; } -- 2.49.0