From 9358e72d30ba4062d0aadccca02655cb19e795d5 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/cut.c | 3 +++ src/nano.c | 37 ++++++++++++++++++++++++++++++------- src/nano.h | 2 ++ src/proto.h | 1 + src/winio.c | 16 +++++++++++++--- 5 files changed, 49 insertions(+), 10 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..22e8db91 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,10 +192,25 @@ 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; } } +/* Renumbers all buffers from the top. Needed when screen + * changes size or softwrap is toggled. */ +void renumber_all(void) +{ + openfilestruct *buffer = startfile; + + do { + renumber_from(buffer->filetop); + buffer = buffer->next; + } while (buffer != startfile); +} + /* Display a warning about a key disabled in view mode. */ void print_view_warning(void) { @@ -1042,7 +1061,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; @@ -1058,6 +1077,7 @@ void regenerate_screen(void) * with their (new) sizes, and redraw the contents of these windows. */ terminal_init(); window_init(); + renumber_all(); total_refresh(); } @@ -1086,13 +1106,13 @@ 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; + renumber_all(); refresh_needed = TRUE; break; case WHITESPACE_DISPLAY: @@ -1502,6 +1522,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 +2312,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/proto.h b/src/proto.h index fe5b40d3..f4804ab3 100644 --- a/src/proto.h +++ b/src/proto.h @@ -399,6 +399,7 @@ void delete_node(linestruct *fileptr); linestruct *copy_buffer(const linestruct *src); void free_lines(linestruct *src); void renumber_from(linestruct *line); +void renumber_all(void); void print_view_warning(void); bool in_restricted_mode(void); #ifndef ENABLE_HELP diff --git a/src/winio.c b/src/winio.c index aae1b8bd..4be5d77e 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,8 +2967,13 @@ 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 totalrows = openfile->filebot->chunk_nr + + number_of_chunks_in(openfile->filebot); + + int first_row = chunk_for(openfile->firstcolumn, openfile->edittop) + + openfile->edittop->chunk_nr; + + int lowest = ((first_row - 1) * editwinrows) / totalrows; int highest = lowest + (editwinrows * editwinrows) / totalrows; if (editwinrows > totalrows) @@ -3034,6 +3039,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