softwrap: move the updating of a softwrapped line to a new function

The new function, update_softwrapped_line(), is called from inside
update_line() when softwrap mode is on, so that existing calls remain
unchanged.  It takes no index, instead displaying edittop from column
firstcolumn, and all other lines from column zero.

If current is on edittop, it's displayed using the edittop rules, but
this is not a problem: if current[current_x] is above edittop at column
firstcolumn, it's offscreen, and that should be handled before calling
update_line() anyway.

Together with the preceding bunch of changes,
this fixes https://savannah.gnu.org/bugs/?47667.
master
David Lawrence Ramsey 2017-01-31 18:11:05 -06:00 committed by Benno Schulenberg
parent 98b029cf3e
commit 2f6c8987ea
2 changed files with 62 additions and 35 deletions

View File

@ -706,6 +706,9 @@ void reset_cursor(void);
void edit_draw(filestruct *fileptr, const char *converted,
int line, size_t from_col);
int update_line(filestruct *fileptr, size_t index);
#ifndef NANO_TINY
int update_softwrapped_line(filestruct *fileptr);
#endif
bool line_needs_update(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);

View File

@ -2644,8 +2644,9 @@ void edit_draw(filestruct *fileptr, const char *converted,
/* Redraw the line at fileptr. The line will be displayed so that the
* character with the given index is visible -- if necessary, the line
* will be horizontally scrolled. In softwrap mode, however, the entire
* line will be displayed. Likely values of index are current_x or zero.
* Return the number of additional rows consumed (when softwrapping). */
* line will be passed to update_softwrapped_line(). Likely values of
* index are current_x or zero. Return the number of additional rows
* consumed (when softwrapping). */
int update_line(filestruct *fileptr, size_t index)
{
int row = 0;
@ -2656,45 +2657,16 @@ int update_line(filestruct *fileptr, size_t index)
/* From which column a horizontally scrolled line is displayed. */
#ifndef NANO_TINY
if (ISSET(SOFTWRAP)) {
filestruct *line = openfile->edittop;
/* Find out on which screen row the target line should be shown. */
while (line != fileptr && line != NULL) {
row += (strlenpt(line->data) / editwincols) + 1;
line = line->next;
}
} else
if (ISSET(SOFTWRAP))
return update_softwrapped_line(fileptr);
#endif
row = fileptr->lineno - openfile->edittop->lineno;
row = fileptr->lineno - openfile->edittop->lineno;
/* If the line is offscreen, don't even try to display it. */
if (row < 0 || row >= editwinrows)
return 0;
#ifndef NANO_TINY
if (ISSET(SOFTWRAP)) {
size_t full_length = strlenpt(fileptr->data);
int starting_row = row;
for (from_col = 0; from_col <= full_length &&
row < editwinrows; from_col += editwincols) {
/* First, blank out the row. */
blank_row(edit, row, 0, COLS);
/* Expand the line, replacing tabs with spaces, and control
* characters with their displayed forms. */
converted = display_string(fileptr->data, from_col, editwincols, TRUE);
/* Draw the line. */
edit_draw(fileptr, converted, row++, from_col);
free(converted);
}
return (row - starting_row);
}
#endif
/* First, blank out the row. */
blank_row(edit, row, 0, COLS);
@ -2717,6 +2689,58 @@ int update_line(filestruct *fileptr, size_t index)
return 1;
}
#ifndef NANO_TINY
/* Redraw all the chunks of the given line (as far as they fit onscreen),
* unless it's edittop, which will be displayed from column firstcolumn.
* Return the number of additional rows consumed. */
int update_softwrapped_line(filestruct *fileptr)
{
int row = 0;
/* The row in the edit window we will write to. */
filestruct *line = openfile->edittop;
/* An iterator needed to find the relevant row. */
int starting_row;
/* The first row in the edit window that gets updated. */
size_t from_col = 0;
/* The starting column of the current chunk. */
char *converted;
/* The data of the chunk with tabs and control characters expanded. */
size_t full_length;
/* The length of the expanded line. */
if (fileptr == openfile->edittop)
from_col = openfile->firstcolumn;
else
row -= (openfile->firstcolumn / editwincols);
/* Find out on which screen row the target line should be shown. */
while (line != fileptr && line != NULL) {
row += (strlenpt(line->data) / editwincols) + 1;
line = line->next;
}
/* If the line is offscreen, don't even try to display it. */
if (row < 0 || row >= editwinrows)
return 0;
full_length = strlenpt(fileptr->data);
starting_row = row;
while (from_col <= full_length && row < editwinrows) {
blank_row(edit, row, 0, COLS);
/* Convert the chunk to its displayable form and draw it. */
converted = display_string(fileptr->data, from_col, editwincols, TRUE);
edit_draw(fileptr, converted, row++, from_col);
free(converted);
from_col += editwincols;
}
return (row - starting_row);
}
#endif
/* Check whether the mark is on, or whether old_column and new_column are on
* different "pages" (in softwrap mode, only the former applies), which means
* that the relevant line needs to be redrawn. */