diff --git a/src/help.c b/src/help.c index 9245805..db70354 100644 --- a/src/help.c +++ b/src/help.c @@ -44,10 +44,17 @@ char *tempfilename = NULL; /* Name of the safe temporary file that we will use for wrapping * and writing the help text. */ +static int diff; + /* Difference, in terms of bytes, between the fileage and the + * edittop of the file contaning the help text; helps to + * reposition the help text to the original location when window + * size changes and/or help text is rewrapped. */ + /* Writes the hard wrapped help text in the temp file and displays it. */ void display_the_help_text(bool redisplaying) { int line_size; + int diff2; const char *ptr = beg_of_intro; /* The current line of the help text. */ FILE *fp = fopen(tempfilename, "w+b"); @@ -83,6 +90,25 @@ void display_the_help_text(bool redisplaying) open_buffer(tempfilename, FALSE); display_buffer(); + + /* Since the help text buffer is closed, reopened and redisplayed, + * reposition to the original location. */ + if (redisplaying) { + edit_refresh(); + ptr = beg_of_intro; + line_size = help_line_len(ptr); + for (diff2 = 0; diff2 + line_size < diff; diff2 += line_size) { + do_down(TRUE); + if (strcmp(openfile->current->data, "") == 0) + do_down(TRUE); + + line_size = help_line_len(ptr); + ptr += line_size; + while (*ptr == '\n') + ++ptr; + + } + } } /* Our main help-viewer function. */ @@ -104,6 +130,10 @@ void do_help(void) char *saved_answer = answer != NULL ? strdup(answer) : NULL; /* In case user chooses help at prompt, save the string entered, * if any, by the user at the prompt. */ + char *line; + const char *edittop_line; + /* Points to the topmost line or to the next line if topmost line + * is blank. */ inhelp = TRUE; @@ -218,8 +248,36 @@ void do_help(void) break; } else unbound_key(kbinput); - } + /* To reposition the help text when the window size changes and/or + * the help text is rewrapped, remember the difference between + * fileage and edittop of file containing help text in terms of + * number of bytes. */ + ptr = beg_of_intro; + diff = strlen(ptr); + while (strlen(ptr) > 0) { + /* If the current line is blank, let its next line be the + * target for the repositioning. */ + if (strcmp(openfile->current->data, "") == 0) + edittop_line = openfile->current->next->data; + else + edittop_line = openfile->current->data; + + line_size = help_line_len(ptr); + line = charalloc((line_size + 1) * sizeof(char)); + snprintf(line, line_size + 1, "%s\n", ptr); + if (strcmp(edittop_line, line) == 0) { + free(line); + break; + } + + free(line); + ptr += line_size; + while (*ptr == '\n') + ++ptr; + } + diff -= strlen(ptr); + } /* We're exiting from the help screen. So, restore the flags and the * original menu, refresh the entire screen and deallocate the memory. */