nano

nano with my custom patches
git clone git://bsandro.tech/nano
Log | Files | Refs | README | LICENSE

commit 29d493a9867cd06a31cac9f4cda443dd493976a1
parent 940a17268babf4cb8903b57b052ffbfe7fb22891
Author: Benno Schulenberg <bensberg@telfort.nl>
Date:   Sun,  5 Jul 2020 18:58:30 +0200

tweaks: move an initialization function to before the one that calls it

Also, frob two comments in that function,
and delete three unneeded blank lines.

Diffstat:
Msrc/help.c | 510+++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/prototypes.h | 3+--
2 files changed, 255 insertions(+), 258 deletions(-)

diff --git a/src/help.c b/src/help.c @@ -29,265 +29,15 @@ static char *help_text = NULL; /* The text displayed in the help window. */ - static const char *start_of_body = NULL; /* The point in the help text just after the title. */ - static char *end_of_intro = NULL; /* The point in the help text where the shortcut descriptions begin. */ - static size_t location; /* The offset (in bytes) of the topleft of the shown help text. */ -/* Hard-wrap the concatenated help text, and write it into a new buffer. */ -void wrap_help_text_into_buffer(void) -{ - size_t sum = 0; - /* Avoid overtight and overwide paragraphs in the introductory text. */ - size_t wrapping_point = ((COLS < 40) ? 40 : (COLS > 74) ? 74 : COLS) - thebar; - const char *ptr = start_of_body; - - make_new_buffer(); - - /* Copy the help text into the just-created new buffer. */ - while (*ptr != '\0') { - int length, shim; - char *oneline; - - if (ptr == end_of_intro) - wrapping_point = ((COLS < 40) ? 40 : COLS) - thebar; - - if (ptr < end_of_intro || *(ptr - 1) == '\n') { - length = break_line(ptr, wrapping_point, TRUE); - oneline = nmalloc(length + 1); - shim = (*(ptr + length - 1) == ' ') ? 0 : 1; - snprintf(oneline, length + shim, "%s", ptr); - } else { - length = break_line(ptr, ((COLS < 40) ? 22 : COLS - 18) - thebar, TRUE); - oneline = nmalloc(length + 5); - snprintf(oneline, length + 5, "\t\t %s", ptr); - } - - free(openfile->current->data); - openfile->current->data = oneline; - - ptr += length; - if (*ptr != '\n') - ptr--; - - /* Create a new line, and then one more for each extra \n. */ - do { - openfile->current->next = make_new_node(openfile->current); - openfile->current = openfile->current->next; - openfile->current->data = copy_of(""); - } while (*(++ptr) == '\n'); - } - - openfile->filebot = openfile->current; - openfile->current = openfile->filetop; - - remove_magicline(); -#ifdef ENABLE_COLOR - find_and_prime_applicable_syntax(); -#endif - prepare_for_display(); - - /* Move to the position in the file where we were before. */ - while (TRUE) { - sum += strlen(openfile->current->data); - if (sum > location) - break; - openfile->current = openfile->current->next; - } - - openfile->edittop = openfile->current; -} - -/* Assemble a help text, display it, and allow scrolling through it. */ -void show_help(void) -{ - int kbinput = ERR; - functionptrtype func; - /* The function of the key the user typed in. */ - int oldmenu = currmenu; - /* The menu we were called from. */ -#ifdef ENABLE_LINENUMBERS - int was_margin = margin; -#endif - ssize_t was_tabsize = tabsize; -#ifdef ENABLE_COLOR - char *was_syntax = syntaxstr; -#endif - char *saved_answer = (answer != NULL) ? copy_of(answer) : NULL; - /* The current answer when the user invokes help at the prompt. */ - unsigned stash[sizeof(flags) / sizeof(flags[0])]; - /* A storage place for the current flag settings. */ - linestruct *line; - int length; - - /* Save the settings of all flags. */ - memcpy(stash, flags, sizeof(flags)); - - /* Ensure that the help screen's shortcut list can be displayed. */ - if (ISSET(NO_HELP) && LINES > 4) { - UNSET(NO_HELP); - window_init(); - } else - blank_statusbar(); - - /* When searching, do it forward, case insensitive, and without regexes. */ - UNSET(BACKWARDS_SEARCH); - UNSET(CASE_SENSITIVE); - UNSET(USE_REGEXP); - - UNSET(WHITESPACE_DISPLAY); - UNSET(NOREAD_MODE); - -#ifdef ENABLE_LINENUMBERS - UNSET(LINE_NUMBERS); - editwincols = COLS - thebar; - margin = 0; -#endif - tabsize = 8; -#ifdef ENABLE_COLOR - syntaxstr = "nanohelp"; -#endif - curs_set(0); - - /* Compose the help text from all the relevant pieces. */ - help_init(); - - inhelp = TRUE; - location = 0; - didfind = 0; - - bottombars(MHELP); - - /* Extract the title from the head of the help text. */ - length = break_line(help_text, HIGHEST_POSITIVE, TRUE); - title = measured_copy(help_text, length); - - titlebar(title); - - /* Skip over the title to point at the start of the body text. */ - start_of_body = help_text + length; - while (*start_of_body == '\n') - start_of_body++; - - wrap_help_text_into_buffer(); - edit_refresh(); - - while (TRUE) { - lastmessage = VACUUM; - focusing = TRUE; - - /* Show the cursor when we searched and found something. */ - kbinput = get_kbinput(edit, didfind == 1 || ISSET(SHOW_CURSOR)); - didfind = 0; - -#ifndef NANO_TINY - if (bracketed_paste || kbinput == BRACKETED_PASTE_MARKER) { - beep(); - continue; - } -#endif - func = interpret(&kbinput); - - if (func == full_refresh) { - full_refresh(); - } else if (ISSET(SHOW_CURSOR) && (func == do_left || func == do_right || - func == do_up || func == do_down)) { - func(); - } else if (func == do_up || func == do_scroll_up) { - do_scroll_up(); - } else if (func == do_down || func == do_scroll_down) { - if (openfile->edittop->lineno + editwinrows - 1 < - openfile->filebot->lineno) - do_scroll_down(); - } else if (func == do_page_up || func == do_page_down || - func == to_first_line || func == to_last_line || - func == do_findprevious || func == do_findnext) { - func(); - } else if (func == do_search_forward || func == do_search_backward) { - func(); - bottombars(MHELP); -#ifdef ENABLE_NANORC - } else if (func == (functionptrtype)implant) { - implant(first_sc_for(MHELP, func)->expansion); -#endif -#ifdef ENABLE_MOUSE - } else if (kbinput == KEY_MOUSE) { - int dummy_row, dummy_col; - get_mouseinput(&dummy_row, &dummy_col, TRUE); -#endif -#ifndef NANO_TINY - } else if (kbinput == KEY_WINCH) { - ; /* Nothing to do. */ -#endif - } else if (func == do_exit) { - break; - } else - unbound_key(kbinput); - - currmenu = MHELP; - edit_refresh(); - - location = 0; - line = openfile->filetop; - - /* Count how far (in bytes) edittop is into the file. */ - while (line != openfile->edittop) { - location += strlen(line->data); - line = line->next; - } - } - - /* Discard the help-text buffer. */ - close_buffer(); - - /* Restore the settings of all flags. */ - memcpy(flags, stash, sizeof(flags)); - -#ifdef ENABLE_LINENUMBERS - margin = was_margin; - editwincols = COLS - margin - thebar; -#endif - tabsize = was_tabsize; -#ifdef ENABLE_COLOR - syntaxstr = was_syntax; - have_palette = FALSE; -#endif - - free(title); - title = NULL; - free(answer); - answer = saved_answer; - free(help_text); - inhelp = FALSE; - - curs_set(0); - - if (ISSET(NO_HELP)) { - currmenu = oldmenu; - window_init(); - } else { - blank_statusbar(); - bottombars(oldmenu); - } - -#ifdef ENABLE_BROWSER - if (oldmenu == MBROWSER || oldmenu == MWHEREISFILE || oldmenu == MGOTODIR) - browser_refresh(); - else -#endif - { - titlebar(NULL); - edit_refresh(); - } -} - -/* Allocate space for the help text for the current menu, and concatenate - * the different pieces of text into it. */ +/* Allocate space for the help text for the current menu, + * and concatenate the different pieces of text into it. */ void help_init(void) { size_t allocsize = 0; @@ -295,9 +45,9 @@ void help_init(void) const char *htx[3]; /* Untranslated help introduction. We break it up into three chunks * in case the full string is too long for the compiler to handle. */ - char *ptr; const funcstruct *f; const keystruct *s; + char *ptr; /* First, set up the initial help text for the current function. */ if (currmenu == MWHEREIS || currmenu == MREPLACE || currmenu == MREPLACEWITH) { @@ -469,9 +219,9 @@ void help_init(void) if (htx[2] != NULL) allocsize += strlen(htx[2]); - /* Calculate the length of the shortcut help text. Each entry has - * one or two keys, which fill 17 cells, plus translated text, - * plus one or two \n's. */ + /* Calculate the length of the descriptions of the shortcuts. + * Each entry has one or two keystrokes, which fill 17 cells, + * plus translated text, plus one or two \n's. */ for (f = allfuncs; f != NULL; f = f->next) if (f->menus & currmenu) allocsize += strlen(_(f->help)) + 21; @@ -562,6 +312,254 @@ void help_init(void) } #endif /* !NANO_TINY */ } + +/* Hard-wrap the concatenated help text, and write it into a new buffer. */ +void wrap_help_text_into_buffer(void) +{ + size_t sum = 0; + /* Avoid overtight and overwide paragraphs in the introductory text. */ + size_t wrapping_point = ((COLS < 40) ? 40 : (COLS > 74) ? 74 : COLS) - thebar; + const char *ptr = start_of_body; + + make_new_buffer(); + + /* Copy the help text into the just-created new buffer. */ + while (*ptr != '\0') { + int length, shim; + char *oneline; + + if (ptr == end_of_intro) + wrapping_point = ((COLS < 40) ? 40 : COLS) - thebar; + + if (ptr < end_of_intro || *(ptr - 1) == '\n') { + length = break_line(ptr, wrapping_point, TRUE); + oneline = nmalloc(length + 1); + shim = (*(ptr + length - 1) == ' ') ? 0 : 1; + snprintf(oneline, length + shim, "%s", ptr); + } else { + length = break_line(ptr, ((COLS < 40) ? 22 : COLS - 18) - thebar, TRUE); + oneline = nmalloc(length + 5); + snprintf(oneline, length + 5, "\t\t %s", ptr); + } + + free(openfile->current->data); + openfile->current->data = oneline; + + ptr += length; + if (*ptr != '\n') + ptr--; + + /* Create a new line, and then one more for each extra \n. */ + do { + openfile->current->next = make_new_node(openfile->current); + openfile->current = openfile->current->next; + openfile->current->data = copy_of(""); + } while (*(++ptr) == '\n'); + } + + openfile->filebot = openfile->current; + openfile->current = openfile->filetop; + + remove_magicline(); +#ifdef ENABLE_COLOR + find_and_prime_applicable_syntax(); +#endif + prepare_for_display(); + + /* Move to the position in the file where we were before. */ + while (TRUE) { + sum += strlen(openfile->current->data); + if (sum > location) + break; + openfile->current = openfile->current->next; + } + + openfile->edittop = openfile->current; +} + +/* Assemble a help text, display it, and allow scrolling through it. */ +void show_help(void) +{ + int kbinput = ERR; + functionptrtype func; + /* The function of the key the user typed in. */ + int oldmenu = currmenu; + /* The menu we were called from. */ +#ifdef ENABLE_LINENUMBERS + int was_margin = margin; +#endif + ssize_t was_tabsize = tabsize; +#ifdef ENABLE_COLOR + char *was_syntax = syntaxstr; +#endif + char *saved_answer = (answer != NULL) ? copy_of(answer) : NULL; + /* The current answer when the user invokes help at the prompt. */ + unsigned stash[sizeof(flags) / sizeof(flags[0])]; + /* A storage place for the current flag settings. */ + linestruct *line; + int length; + + /* Save the settings of all flags. */ + memcpy(stash, flags, sizeof(flags)); + + /* Ensure that the help screen's shortcut list can be displayed. */ + if (ISSET(NO_HELP) && LINES > 4) { + UNSET(NO_HELP); + window_init(); + } else + blank_statusbar(); + + /* When searching, do it forward, case insensitive, and without regexes. */ + UNSET(BACKWARDS_SEARCH); + UNSET(CASE_SENSITIVE); + UNSET(USE_REGEXP); + + UNSET(WHITESPACE_DISPLAY); + UNSET(NOREAD_MODE); + +#ifdef ENABLE_LINENUMBERS + UNSET(LINE_NUMBERS); + editwincols = COLS - thebar; + margin = 0; +#endif + tabsize = 8; +#ifdef ENABLE_COLOR + syntaxstr = "nanohelp"; +#endif + curs_set(0); + + /* Compose the help text from all the relevant pieces. */ + help_init(); + + inhelp = TRUE; + location = 0; + didfind = 0; + + bottombars(MHELP); + + /* Extract the title from the head of the help text. */ + length = break_line(help_text, HIGHEST_POSITIVE, TRUE); + title = measured_copy(help_text, length); + + titlebar(title); + + /* Skip over the title to point at the start of the body text. */ + start_of_body = help_text + length; + while (*start_of_body == '\n') + start_of_body++; + + wrap_help_text_into_buffer(); + edit_refresh(); + + while (TRUE) { + lastmessage = VACUUM; + focusing = TRUE; + + /* Show the cursor when we searched and found something. */ + kbinput = get_kbinput(edit, didfind == 1 || ISSET(SHOW_CURSOR)); + didfind = 0; + +#ifndef NANO_TINY + if (bracketed_paste || kbinput == BRACKETED_PASTE_MARKER) { + beep(); + continue; + } +#endif + func = interpret(&kbinput); + + if (func == full_refresh) { + full_refresh(); + } else if (ISSET(SHOW_CURSOR) && (func == do_left || func == do_right || + func == do_up || func == do_down)) { + func(); + } else if (func == do_up || func == do_scroll_up) { + do_scroll_up(); + } else if (func == do_down || func == do_scroll_down) { + if (openfile->edittop->lineno + editwinrows - 1 < + openfile->filebot->lineno) + do_scroll_down(); + } else if (func == do_page_up || func == do_page_down || + func == to_first_line || func == to_last_line || + func == do_findprevious || func == do_findnext) { + func(); + } else if (func == do_search_forward || func == do_search_backward) { + func(); + bottombars(MHELP); +#ifdef ENABLE_NANORC + } else if (func == (functionptrtype)implant) { + implant(first_sc_for(MHELP, func)->expansion); +#endif +#ifdef ENABLE_MOUSE + } else if (kbinput == KEY_MOUSE) { + int dummy_row, dummy_col; + get_mouseinput(&dummy_row, &dummy_col, TRUE); +#endif +#ifndef NANO_TINY + } else if (kbinput == KEY_WINCH) { + ; /* Nothing to do. */ +#endif + } else if (func == do_exit) { + break; + } else + unbound_key(kbinput); + + currmenu = MHELP; + edit_refresh(); + + location = 0; + line = openfile->filetop; + + /* Count how far (in bytes) edittop is into the file. */ + while (line != openfile->edittop) { + location += strlen(line->data); + line = line->next; + } + } + + /* Discard the help-text buffer. */ + close_buffer(); + + /* Restore the settings of all flags. */ + memcpy(flags, stash, sizeof(flags)); + +#ifdef ENABLE_LINENUMBERS + margin = was_margin; + editwincols = COLS - margin - thebar; +#endif + tabsize = was_tabsize; +#ifdef ENABLE_COLOR + syntaxstr = was_syntax; + have_palette = FALSE; +#endif + + free(title); + title = NULL; + free(answer); + answer = saved_answer; + free(help_text); + inhelp = FALSE; + + curs_set(0); + + if (ISSET(NO_HELP)) { + currmenu = oldmenu; + window_init(); + } else { + blank_statusbar(); + bottombars(oldmenu); + } + +#ifdef ENABLE_BROWSER + if (oldmenu == MBROWSER || oldmenu == MWHEREISFILE || oldmenu == MGOTODIR) + browser_refresh(); + else +#endif + { + titlebar(NULL); + edit_refresh(); + } +} + #endif /* ENABLE_HELP */ /* Start the help viewer, or indicate that there is no help. */ diff --git a/src/prototypes.h b/src/prototypes.h @@ -331,10 +331,9 @@ int keycode_from_string(const char *keystring); void shortcut_init(void); const char *flagtostr(int flag); -/* Most functions in help.c. */ +/* Some functions in help.c. */ #ifdef ENABLE_HELP void wrap_help_text_into_buffer(void); -void help_init(void); #endif void do_help(void);