From 7cdec9ef4be675ddde317a21aa5bbc78c23950c6 Mon Sep 17 00:00:00 2001 From: Rishabh Dave Date: Sat, 10 Dec 2016 17:49:53 +0530 Subject: [PATCH] help: stay in same place when window size changes For horizontally resizing, nano will only roughly stay in the same place. Signed-off-by: Rishabh Dave --- src/help.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/help.c b/src/help.c index 9245805..e4309ca 100644 --- a/src/help.c +++ b/src/help.c @@ -44,6 +44,33 @@ char *tempfilename = NULL; /* Name of the safe temporary file that we will use for wrapping * and writing the help text. */ +bool first_time; + /* For making the line at the center to be the current line. Needs + * to be done only when redisplaying the help text for the first + * time. */ + +/* Return the maximum length at which help text is wrapped. */ +int max_wrapping_len() +{ + int max = 0; + int len; + const char *ptr = beg_of_intro; + + while (strlen(ptr) > 0) { + len = help_line_len(ptr); + + if (max < len) + max = len; + + if (*ptr == '\n') + ++ptr; + + ptr += len; + } + + return max; +} + /* Writes the hard wrapped help text in the temp file and displays it. */ void display_the_help_text(bool redisplaying) { @@ -51,12 +78,26 @@ void display_the_help_text(bool redisplaying) const char *ptr = beg_of_intro; /* The current line of the help text. */ FILE *fp = fopen(tempfilename, "w+b"); + size_t saved_current_lineno = openfile->current->lineno; + /* For staying at, or at least, close to the current position. */ + static int last_wrap_len = 0; + + /* If window size has changed and there is for no need for rewrapping, + * only refresh the window contents. */ + if (redisplaying && (COLS > 76 || last_wrap_len == max_wrapping_len())) { + total_refresh(); + + fclose(fp); + return; + } if (fp == NULL) { statusline(ALERT, _("Error writing temp file: %s"), strerror(errno)); return; } + last_wrap_len = max_wrapping_len(); + /* Wrap and copy the rest of the help_text into the temporary file. */ while (strlen(ptr) > 0) { line_size = help_line_len(ptr); @@ -83,6 +124,23 @@ void display_the_help_text(bool redisplaying) open_buffer(tempfilename, FALSE); display_buffer(); + + if (redisplaying) { + if (first_time) { + /* Current line must be one at the center for proper scrolling + * of text while rewrapping. */ + saved_current_lineno += 10; + first_time = FALSE; + } + + while (openfile->current->lineno < saved_current_lineno) { + /* do_down only if we can. */ + if (openfile->current->lineno + 9 > openfile->filebot->lineno) + break; + else + do_down(TRUE); + } + } } /* Our main help-viewer function. */ @@ -106,6 +164,7 @@ void do_help(void) * if any, by the user at the prompt. */ inhelp = TRUE; + first_time = TRUE; blank_statusbar(); -- 2.7.4