softwrap: adjust firstcolumn when the window width changes

If the number of columns in the edit window changes (which currently
only happens in two places: in regenerate_screen(), called when the
window is resized; and in main(), when line numbering mode is toggled),
the display will break if we're in softwrap mode and firstcolumn is
nonzero.  This is because the column width of softwrapped chunks has
changed, and firstcolumn is no longer the starting column of a chunk,
an assumption that all code using firstcolumn relies on.

To fix this problem, add a new function, ensure_firstcolumn_is_aligned(),
to adjust firstcolumn to the starting column of the chunk it's on, and
use it when the number of columns in the edit window changes.

(Note that this function uses the simplest possible fix, and could
probably be made more sophisticated.)
master
David Lawrence Ramsey 2017-01-23 13:40:03 -06:00 committed by Benno Schulenberg
parent c1f70185ba
commit 0389a1d939
3 changed files with 19 additions and 0 deletions

View File

@ -1332,6 +1332,9 @@ void regenerate_screen(void)
#endif
editwincols = COLS - margin;
/* Ensure that firstcolumn is the starting column of its chunk. */
ensure_firstcolumn_is_aligned();
#ifdef USE_SLANG
/* Slang curses emulation brain damage, part 1: If we just do what
* curses does here, it'll only work properly if the resize made the
@ -2643,6 +2646,10 @@ int main(int argc, char **argv)
if (needed_margin != margin) {
margin = needed_margin;
editwincols = COLS - margin;
/* Ensure that firstcolumn is the starting column of its chunk. */
ensure_firstcolumn_is_aligned();
/* The margin has changed -- schedule a full refresh. */
refresh_needed = TRUE;
}

View File

@ -714,6 +714,7 @@ 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 ensure_firstcolumn_is_aligned(void);
bool current_is_above_screen(void);
bool current_is_below_screen(void);
bool current_is_offscreen(void);

View File

@ -2930,6 +2930,17 @@ void edit_scroll(scroll_dir direction, int nrows)
}
}
/* Ensure that firstcolumn is at the startting column of the softwrapped chunk
* it's on. We need to do this when the number of columns of the edit window
* has changed, because then the width of softwrapped chunks has changed. */
void ensure_firstcolumn_is_aligned(void)
{
#ifndef NANO_TINY
if (openfile->firstcolumn % editwincols != 0)
openfile->firstcolumn -= (openfile->firstcolumn % editwincols);
#endif
}
/* Return TRUE if current[current_x] is above the top of the screen, and FALSE
* otherwise. */
bool current_is_above_screen(void)