From 1ab712af652174ccc95c7cb427c4394bc3ffea9e Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Mon, 5 Jun 2017 09:12:09 -0400 Subject: [PATCH] first, super inefficient crack at showing line numbers --- TODO.md | 8 ++++++++ inc/edit.h | 4 +++- lib/buf.c | 7 +++++++ lib/view.c | 58 ++++++++++++++++++++++++++++++------------------------ lib/win.c | 7 ++----- 5 files changed, 52 insertions(+), 32 deletions(-) diff --git a/TODO.md b/TODO.md index 0b734dc..a810e4b 100644 --- a/TODO.md +++ b/TODO.md @@ -11,6 +11,9 @@ Up Next: The Future: +* Case insensitive search +* Ctrl+Up,Down requires two undos to revert. +* Ctrl+Up,Down with non line selection should track column * Scrolling line offset * Ctrl+Shift+Enter copies indent of wrong line * highlight all matches of search term @@ -26,6 +29,7 @@ The Future: * add command env vars to set options (Tabs, Indent, etc..) * implement command diffing logic to optimize the undo/redo log * Status line should omit characters from beginning of path to make file path fit +* pickfile directory traversal when no tags file Possible Shortcuts: @@ -48,3 +52,7 @@ Maybe think about addressing these later: * Win-like terminal emulator * File browser * Acme-like window manager + +# Syntax Highlighting + +Label, Conditional, Repeat, Character, Number, PreProc, Float, Operator, Structure diff --git a/inc/edit.h b/inc/edit.h index 4bdfc1c..25efe33 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -97,6 +97,7 @@ void buf_find(Buf* buf, int dir, size_t* beg, size_t* end); void buf_findstr(Buf* buf, int dir, char* str, size_t* beg, size_t* end); void buf_lastins(Buf* buf, size_t* beg, size_t* end); size_t buf_setln(Buf* buf, size_t line); +size_t buf_getln(Buf* buf, size_t off); size_t buf_getcol(Buf* buf, size_t pos); size_t buf_setcol(Buf* buf, size_t pos, size_t col); @@ -127,7 +128,8 @@ typedef struct { } UGlyph; typedef struct { - size_t off; /* offset of the first rune in the row */ + size_t line; /* the line number of the data in the row */ + size_t off; /* offset of the first rune in the row */ size_t rlen; /* number of runes displayed in the row */ size_t len; /* number of screen columns taken up by row */ UGlyph cols[]; /* row data */ diff --git a/lib/buf.c b/lib/buf.c index cfd20fa..4abd562 100644 --- a/lib/buf.c +++ b/lib/buf.c @@ -417,6 +417,13 @@ size_t buf_setln(Buf* buf, size_t line) { return off; } +size_t buf_getln(Buf* buf, size_t off) { + size_t line = 1, curr = 0, end = buf_end(buf); + while (curr < off && curr < end) + line++, curr = buf_byline(buf, curr, DOWN); + return line; +} + size_t buf_getcol(Buf* buf, size_t pos) { size_t curr = buf_bol(buf, pos); size_t col = 0; diff --git a/lib/view.c b/lib/view.c index 1c86c58..774c44d 100644 --- a/lib/view.c +++ b/lib/view.c @@ -7,6 +7,7 @@ typedef size_t (*movefn_t)(Buf* buf, size_t pos, int count); static void move_selection(View* view, bool extsel, Sel* sel, 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 void selswap(Sel* sel); static size_t num_selected(Sel sel); @@ -15,7 +16,7 @@ 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); static size_t setcell(View* view, size_t row, size_t col, uint32_t attr, Rune r); -static size_t fill_row(View* view, unsigned row, size_t pos); +static size_t fill_row(View* view, unsigned row, size_t pos, size_t* line); static unsigned prev_screen_line(View* view, unsigned bol, unsigned off); static unsigned scroll_up(View* view); static unsigned scroll_dn(View* view); @@ -61,11 +62,12 @@ size_t view_limitrows(View* view, size_t maxrows, size_t ncols) { } void view_resize(View* view, size_t nrows, size_t ncols) { - size_t off = 0; + size_t line = 1, off = 0; if (view->nrows == nrows && view->ncols == ncols) return; /* free the old row data */ if (view->nrows) { - off = view->rows[0]->off; + line = view->rows[0]->line; + off = view->rows[0]->off; for (size_t i = 0; i < view->nrows; i++) free(view->rows[i]); free(view->rows); @@ -76,6 +78,7 @@ 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; } @@ -84,9 +87,9 @@ 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; + size_t pos = view->rows[0]->off, line = buf_getln(&(view->buffer), pos); for (size_t y = 0; y < view->nrows; y++) - pos = fill_row(view, y, pos); + pos = fill_row(view, y, pos, &line); if (view->sync_needed) view_scrollto(view, csr); /* locate the cursor if visible */ @@ -120,11 +123,8 @@ void view_setcursor(View* view, size_t row, size_t col) { void view_selext(View* view, size_t row, size_t col) { size_t off = getoffset(view, row, col); - if (off != SIZE_MAX) { - view->selection.end = off; - view->selection.col = buf_getcol(&(view->buffer), view->selection.end); - view_scrollto(view, view->selection.end); - } + if (off != SIZE_MAX) + view_jumpto(view, true, off); } void view_selword(View* view, size_t row, size_t col) { @@ -188,15 +188,6 @@ bool view_findstr(View* view, int dir, char* str) { return found; } -static void move_to(View* view, bool extsel, size_t off) { - Buf* buf = &(view->buffer); - view->selection.end = (off > buf_end(buf) ? buf_end(buf) : off); - if (!extsel) - view->selection.beg = view->selection.end; - view->selection.col = buf_getcol(&(view->buffer), view->selection.end); - view->sync_needed = true; -} - void view_insert(View* view, bool indent, Rune rune) { /* ignore non-printable control characters */ if (!isspace(rune) && rune < 0x20) @@ -439,6 +430,15 @@ static void move_selection(View* view, bool extsel, Sel* sel, int move, movefn_t sel->col = buf_getcol(&(view->buffer), sel->end); } +static void move_to(View* view, bool extsel, size_t off) { + Buf* buf = &(view->buffer); + view->selection.end = (off > buf_end(buf) ? buf_end(buf) : off); + if (!extsel) + view->selection.beg = view->selection.end; + view->selection.col = buf_getcol(&(view->buffer), view->selection.end); + 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); @@ -538,14 +538,20 @@ static size_t setcell(View* view, size_t row, size_t col, uint32_t attr, Rune r) return ncols; } -static size_t fill_row(View* view, unsigned row, size_t pos) { - view_getrow(view, row)->off = pos; +static size_t fill_row(View* view, unsigned row, size_t pos, size_t* line) { + view_getrow(view, row)->off = pos; + if (line) + view_getrow(view, row)->line = *line; clearrow(view, row); for (size_t x = 0; x < view->ncols;) { uint32_t attr = (in_selection(view->selection, pos) ? CLR_SelectedText : CLR_NormalText); Rune r = buf_get(&(view->buffer), pos++); x += setcell(view, row, x, attr, r); - if (buf_iseol(&(view->buffer), pos-1)) break; + if (buf_iseol(&(view->buffer), pos-1)) { + if (line) + *line += 1; + break; + } } return pos; } @@ -563,7 +569,7 @@ static unsigned prev_screen_line(View* view, unsigned bol, unsigned off) { } static unsigned scroll_up(View* view) { - unsigned first = view->rows[0]->off; + unsigned first = view->rows[0]->off; unsigned bol = buf_bol(&(view->buffer), first); unsigned prevln = (first == bol ? buf_byline(&(view->buffer), bol, -1) : bol); if (!first) return first; @@ -574,7 +580,7 @@ static unsigned scroll_up(View* view) { view->rows[0] = calloc(1, sizeof(Row) + (view->ncols * sizeof(UGlyph))); view->rows[0]->off = prevln; /* fill in row content */ - fill_row(view, 0, view->rows[0]->off); + fill_row(view, 0, view->rows[0]->off, NULL); return view->rows[0]->off; } @@ -588,10 +594,10 @@ static unsigned scroll_dn(View* view) { view->rows[view->nrows-1] = calloc(1, sizeof(Row) + (view->ncols * sizeof(UGlyph))); view->rows[view->nrows-1]->off = (view->rows[view->nrows-2]->off + view->rows[view->nrows-2]->rlen); /* fill in row content */ - fill_row(view, view->nrows-1, view->rows[view->nrows-1]->off); + fill_row(view, view->nrows-1, view->rows[view->nrows-1]->off, NULL); } else { view->rows[0]->off += view->rows[0]->rlen; - fill_row(view, 0, view->rows[0]->off); + fill_row(view, 0, view->rows[0]->off, NULL); } return view->rows[view->nrows-1]->off + view->rows[view->nrows-1]->rlen - 1; } diff --git a/lib/win.c b/lib/win.c index 484f89b..aa19f12 100644 --- a/lib/win.c +++ b/lib/win.c @@ -225,8 +225,8 @@ static void onredraw(int width, int height) { size_t gcols = gutter_cols(); for (size_t y = 0; y < view->nrows; y++) { - draw_line_num(Regions[i].x - (gcols * fwidth) - 5, Regions[i].y + ((y+1) * fheight), gcols, (y % 2 ? 123 : 45)); Row* row = view_getrow(view, y); + 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); } } @@ -256,12 +256,10 @@ static void onredraw(int width, int height) { x11_mouse_set(x, y); } - printf("lines: %llu\n", win_buf(EDIT)->nlines); - uint64_t stop = getmillis(); uint64_t elapsed = stop-start; if (elapsed > maxtime) { - printf("%llu\n", elapsed); + //printf("%llu\n", elapsed); maxtime = elapsed; } } @@ -359,7 +357,6 @@ static void onwheeldn(WinRegion id, bool pressed, size_t row, size_t col) { static void draw_line_num(size_t x, size_t y, size_t gcols, size_t num) { UGlyph glyphs[gcols]; if (LineNumbers) { - printf("cols: %lu\n", gcols); for (int i = gcols-1; i >= 0; i--) { glyphs[i].attr = CLR_GutterText; if (num > 0) { -- 2.51.0