From 4f9c563e6b56d16632eebe2c40835d66017554ba Mon Sep 17 00:00:00 2001 From: David Lawrence Ramsey Date: Sun, 15 Jan 2017 16:01:17 -0600 Subject: [PATCH] softwrap: count softwrapped chunks properly in do_uncut_text() Add the new function less_than_a_screenful() to accomplish this. It uses go_back_chunks() to count the number of softwrapped chunks between the end point and the starting point of the paste. Now softwrap mode and non-softwrap mode behave the same way when uncutting fewer than editwinrows rows of text. Accordingly, remove the call to ensure_line_is_visible(), as it no longer applies. --- src/cut.c | 11 ++++++++--- src/proto.h | 1 + src/winio.c | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/cut.c b/src/cut.c index fe689757..97ccc819 100644 --- a/src/cut.c +++ b/src/cut.c @@ -257,6 +257,9 @@ void do_cut_till_eof(void) void do_uncut_text(void) { ssize_t was_lineno = openfile->current->lineno; + /* The line number where we started the paste. */ + size_t was_leftedge = 0; + /* The leftedge where we started the paste. */ /* If the cutbuffer is empty, there is nothing to do. */ if (cutbuffer == NULL) @@ -264,6 +267,9 @@ void do_uncut_text(void) #ifndef NANO_TINY add_undo(PASTE); + + if (ISSET(SOFTWRAP)) + was_leftedge = (xplustabs() / editwincols) * editwincols; #endif /* Add a copy of the text in the cutbuffer to the current filestruct @@ -274,7 +280,8 @@ void do_uncut_text(void) update_undo(PASTE); #endif - if (openfile->current->lineno - was_lineno < editwinrows) + /* If we pasted less than a screenful, don't center the cursor. */ + if (less_than_a_screenful(was_lineno, was_leftedge)) focusing = FALSE; /* Set the desired x position to where the pasted text ends. */ @@ -286,8 +293,6 @@ void do_uncut_text(void) /* Update the cursor position to account for the inserted lines. */ reset_cursor(); - ensure_line_is_visible(); - refresh_needed = TRUE; #ifndef DISABLE_COLOR diff --git a/src/proto.h b/src/proto.h index 66fb9eac..c7eeeb93 100644 --- a/src/proto.h +++ b/src/proto.h @@ -709,6 +709,7 @@ int update_line(filestruct *fileptr, size_t index); bool need_horizontal_scroll(const size_t old_column, const size_t new_column); int go_back_chunks(int nrows, filestruct **line, size_t *leftedge); int go_forward_chunks(int nrows, filestruct **line, size_t *leftedge); +bool less_than_a_screenful(size_t was_lineno, size_t was_leftedge); void edit_scroll(scroll_dir direction, int nrows); void edit_redraw(filestruct *old_current); void edit_refresh(void); diff --git a/src/winio.c b/src/winio.c index 522bdae9..39538644 100644 --- a/src/winio.c +++ b/src/winio.c @@ -2835,6 +2835,24 @@ int go_forward_chunks(int nrows, filestruct **line, size_t *leftedge) return i; } +/* Return TRUE if there are fewer than a screen's worth of lines between + * the line at line number was_lineno (and column was_leftedge, if we're + * in softwrap mode) and the line at current[current_x]. */ +bool less_than_a_screenful(size_t was_lineno, size_t was_leftedge) +{ +#ifndef NANO_TINY + if (ISSET(SOFTWRAP)) { + filestruct *line = openfile->current; + size_t leftedge = (xplustabs() / editwincols) * editwincols; + int rows_left = go_back_chunks(editwinrows - 1, &line, &leftedge); + + return (rows_left > 0 || line->lineno < was_lineno || + (line->lineno == was_lineno && leftedge <= was_leftedge)); + } else +#endif + return (openfile->current->lineno - was_lineno < editwinrows); +} + /* Scroll the edit window in the given direction and the given number of rows, * and draw new lines on the blank lines left after the scrolling. We change * edittop, and assume that current and current_x are up to date. */