From 6671f8df99329358cdcb11a5346576c0f7c2a666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Diego=20Aur=C3=A9lio=20Mesquita?= Date: Sun, 26 Apr 2020 15:25:15 -0300 Subject: [PATCH] Initial softwrap support for scrollbar --- src/nano.c | 20 +++++++++++++------- src/nano.h | 2 ++ src/text.c | 10 ++++++++++ src/winio.c | 12 +++++++++--- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/nano.c b/src/nano.c index 1f37bf1a..ba67af14 100644 --- a/src/nano.c +++ b/src/nano.c @@ -75,6 +75,9 @@ linestruct *make_new_node(linestruct *prevnode) newnode->multidata = NULL; #endif newnode->lineno = (prevnode) ? prevnode->lineno + 1 : 1; + newnode->llineno = (prevnode) ? + number_of_chunks_in(prevnode) + prevnode->llineno + 1 + : 1; #ifndef NANO_TINY newnode->has_anchor = FALSE; #endif @@ -150,6 +153,7 @@ linestruct *copy_node(const linestruct *src) dst->multidata = NULL; #endif dst->lineno = src->lineno; + dst->llineno = src->llineno; #ifndef NANO_TINY dst->has_anchor = FALSE; #endif @@ -188,6 +192,9 @@ void renumber_from(linestruct *line) while (line != NULL) { line->lineno = ++number; + line->llineno = (line->prev) ? + number_of_chunks_in(line->prev) + line->prev->llineno + 1 + : 1; line = line->next; } } @@ -1042,7 +1049,7 @@ void regenerate_screen(void) LINES = win.ws_row; #endif #ifndef NANO_TINY - thebar = (LINES > 5 && COLS > 9 && !ISSET(SOFTWRAP)) ? 1 : 0; + thebar = (LINES > 5 && COLS > 9) ? 1 : 0; bardata = nrealloc(bardata, LINES * sizeof(int)); #endif editwincols = COLS - margin - thebar; @@ -1086,12 +1093,11 @@ void do_toggle(int flag) signal_init(); break; case SOFTWRAP: - if (!ISSET(SOFTWRAP)) { - thebar = (LINES > 5 && COLS > 9) ? 1 : 0; - bardata = nrealloc(bardata, LINES * sizeof(int)); + if (!ISSET(SOFTWRAP)) openfile->firstcolumn = 0; - } else - thebar = 0; + + thebar = (LINES > 5 && COLS > 9) ? 1 : 0; + bardata = nrealloc(bardata, LINES * sizeof(int)); editwincols = COLS - margin - thebar; refresh_needed = TRUE; break; @@ -2283,7 +2289,7 @@ int main(int argc, char **argv) curs_set(0); #ifndef NANO_TINY - thebar = (LINES > 5 && COLS > 9 && !ISSET(SOFTWRAP)) ? 1 : 0; + thebar = (LINES > 5 && COLS > 9) ? 1 : 0; bardata = nrealloc(bardata, LINES * sizeof(int)); #endif editwincols = COLS - thebar; diff --git a/src/nano.h b/src/nano.h index 75f6f3c7..af0c167d 100644 --- a/src/nano.h +++ b/src/nano.h @@ -284,6 +284,8 @@ typedef struct linestruct { /* The text of this line. */ ssize_t lineno; /* The number of this line. */ + ssize_t llineno; + /* The number of this logical line. */ struct linestruct *next; /* Next node. */ struct linestruct *prev; diff --git a/src/text.c b/src/text.c index a002280e..84df5f65 100644 --- a/src/text.c +++ b/src/text.c @@ -941,6 +941,11 @@ void add_undo(undo_type action, const char *message) undostruct *u = nmalloc(sizeof(undostruct)); linestruct *thisline = openfile->current; + /* If a softwrapped line is modfied, we may need to renumber + * logical lines */ + if(ISSET(SOFTWRAP) && number_of_chunks_in(openfile->current)) + renumber_from(thisline); + /* Initialize the newly allocated undo item. */ u->type = action; u->strdata = NULL; @@ -1114,6 +1119,11 @@ void update_undo(undo_type action) char *textposition; int charlen; + /* If a softwrapped line is modfied, we may need to renumber + * logical lines */ + if(ISSET(SOFTWRAP) && number_of_chunks_in(openfile->current)) + renumber_from(openfile->current); + if (u->type != action) die("Mismatching undo type -- please report a bug\n"); diff --git a/src/winio.c b/src/winio.c index 3eb836d3..12fafd79 100644 --- a/src/winio.c +++ b/src/winio.c @@ -2393,7 +2393,7 @@ void draw_row(int row, const char *converted, linestruct *line, size_t from_col) mvwprintw(edit, row, 0, "%*s", margin - 1, " "); else #endif - mvwprintw(edit, row, 0, "%*zd", margin - 1, line->lineno); + mvwprintw(edit, row, 0, "%*zd", margin - 1, line->llineno); wattroff(edit, interface_color_pair[LINE_NUMBER]); #ifndef NANO_TINY if (line->has_anchor && from_col == 0) @@ -2746,6 +2746,11 @@ void draw_row(int row, const char *converted, linestruct *line, size_t from_col) wattroff(edit, interface_color_pair[SELECTED_TEXT]); } } + + /* If drawing a softwrapped line, we may need to renumber logical lines */ + if(ISSET(SOFTWRAP) && number_of_chunks_in(openfile->current)) + draw_scrollbar(); + #endif /* !NANO_TINY */ } @@ -2967,8 +2972,9 @@ bool less_than_a_screenful(size_t was_lineno, size_t was_leftedge) /* Draw a scroll bar on the righthand side of the screen. */ void draw_scrollbar(void) { - int totalrows = openfile->filebot->lineno; - int lowest = ((openfile->edittop->lineno - 1) * editwinrows) / totalrows; + int chunks_above = chunk_for(openfile->firstcolumn, openfile->edittop); + int totalrows = openfile->filebot->llineno; + int lowest = ((openfile->edittop->llineno + chunks_above - 1) * editwinrows) / totalrows; int highest = lowest + (editwinrows * editwinrows) / totalrows; if (editwinrows > totalrows) -- 2.17.1