From 83cebf33749d940b2919af98816fd7deeb5b981f 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 2/2] Initial softwrap support for scrollbar --- src/cut.c | 3 +++ src/nano.c | 23 ++++++++++++++++------- src/nano.h | 2 ++ src/winio.c | 29 +++++++++++++++++++++++------ 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/cut.c b/src/cut.c index fbc145c3..cc42dd3e 100644 --- a/src/cut.c +++ b/src/cut.c @@ -112,6 +112,9 @@ void do_deletion(undo_type action) #endif set_modified(); + + if (old_amount != number_of_chunks_in(openfile->current)) + renumber_from(openfile->current); } /* Delete the character under the cursor. */ diff --git a/src/nano.c b/src/nano.c index 5ede45bb..fbc06986 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->chunk_nr = (prevnode) ? + number_of_chunks_in(prevnode) + prevnode->chunk_nr + 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->chunk_nr = src->chunk_nr; #ifndef NANO_TINY dst->has_anchor = FALSE; #endif @@ -188,6 +192,9 @@ void renumber_from(linestruct *line) while (line != NULL) { line->lineno = ++number; + line->chunk_nr = (line->prev) ? + number_of_chunks_in(line->prev) + line->prev->chunk_nr + 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; @@ -1502,6 +1508,9 @@ void inject(char *burst, size_t count) #endif if (!refresh_needed) update_line(openfile->current, openfile->current_x); + + if (number_of_chunks_in(openfile->current) != old_amount) + renumber_from(openfile->current); } /* Read in a keystroke, and execute its command or insert it into the buffer. */ @@ -2289,7 +2298,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 dffabe70..88201da3 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 chunk_nr; + /* The number of this logical line. */ struct linestruct *next; /* Next node. */ struct linestruct *prev; diff --git a/src/winio.c b/src/winio.c index aae1b8bd..8d1550f6 100644 --- a/src/winio.c +++ b/src/winio.c @@ -2412,7 +2412,7 @@ void draw_row(int row, const char *converted, linestruct *line, size_t from_col) if (is_shorter || ISSET(SOFTWRAP)) wclrtoeol(edit); - if (is_shorter && thebar) + if (thebar) mvwaddch(edit, row, COLS - 1, bardata[row]); #ifdef USING_OLD_NCURSES @@ -2967,12 +2967,24 @@ 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 highest = lowest + (editwinrows * editwinrows) / totalrows; + int rows_above = chunk_for(openfile->firstcolumn, openfile->edittop) + + openfile->edittop->chunk_nr - 1; + int totalrows = openfile->filebot->chunk_nr; + int rows_below = totalrows - rows_above - editwinrows; + int top_row = openfile->edittop->chunk_nr + + chunk_for(openfile->firstcolumn, openfile->edittop); + + int lowest = rows_above; + int highest = editwinrows - rows_below - 1; + + if (lowest > highest) + lowest = highest = top_row*(editwinrows - 1)/(totalrows - editwinrows); - if (editwinrows > totalrows) - highest = editwinrows; + if (lowest > editwinrows - 1) + lowest = editwinrows - 1; + + if (totalrows < editwinrows) + lowest = highest = -1; for (int row = 0; row < editwinrows; row++) { bardata[row] = ' '|((row >= lowest && row <= highest) ? A_REVERSE : 0); @@ -3034,6 +3046,11 @@ void edit_scroll(bool direction) openfile->current_x : 0); line = line->next; } + +#ifndef NANO_TINY + if (thebar) + draw_scrollbar(); +#endif } #ifndef NANO_TINY -- 2.17.1