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.
This commit is contained in:
David Lawrence Ramsey 2017-01-15 16:01:17 -06:00 committed by Benno Schulenberg
parent 1570651e30
commit 4f9c563e6b
3 changed files with 27 additions and 3 deletions

View File

@ -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

View File

@ -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);

View File

@ -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. */