From a65982bffb1192db0b8ba0a184d9bfa5e3bdaace Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Fri, 14 Feb 2020 19:06:39 +0100 Subject: [PATCH] tweaks: relocate eleven functions to before they are called --- src/prompt.c | 276 +++++++++++++++++++++++++-------------------------- src/proto.h | 13 --- 2 files changed, 138 insertions(+), 151 deletions(-) diff --git a/src/prompt.c b/src/prompt.c index 81ffc8d7..ca50577a 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -28,6 +28,115 @@ static char *prompt = NULL; static size_t typing_x = HIGHEST_POSITIVE; /* The cursor position in answer. */ +/* Move to the beginning of the answer. */ +void do_statusbar_home(void) +{ + typing_x = 0; +} + +/* Move to the end of the answer. */ +void do_statusbar_end(void) +{ + typing_x = strlen(answer); +} + +#ifndef NANO_TINY +/* Move to the next word in the answer. */ +void do_statusbar_next_word(void) +{ + bool seen_space = !is_word_mbchar(answer + typing_x, FALSE); + bool seen_word = !seen_space; + + /* Move forward until we reach either the end or the start of a word, + * depending on whether the AFTER_ENDS flag is set or not. */ + while (answer[typing_x] != '\0') { + typing_x = step_right(answer, typing_x); + + if (ISSET(AFTER_ENDS)) { + /* If this is a word character, continue; else it's a separator, + * and if we've already seen a word, then it's a word end. */ + if (is_word_mbchar(answer + typing_x, FALSE)) + seen_word = TRUE; + else if (seen_word) + break; + } else { + /* If this is not a word character, then it's a separator; else + * if we've already seen a separator, then it's a word start. */ + if (!is_word_mbchar(answer + typing_x, FALSE)) + seen_space = TRUE; + else if (seen_space) + break; + } + } +} + +/* Move to the previous word in the answer. */ +void do_statusbar_prev_word(void) +{ + bool seen_a_word = FALSE, step_forward = FALSE; + + /* Move backward until we pass over the start of a word. */ + while (typing_x != 0) { + typing_x = step_left(answer, typing_x); + + if (is_word_mbchar(answer + typing_x, FALSE)) + seen_a_word = TRUE; + else if (seen_a_word) { + /* This is space now: we've overshot the start of the word. */ + step_forward = TRUE; + break; + } + } + + if (step_forward) + /* Move one character forward again to sit on the start of the word. */ + typing_x = step_right(answer, typing_x); +} +#endif /* !NANO_TINY */ + +/* Move left one character in the answer. */ +void do_statusbar_left(void) +{ + if (typing_x > 0) + typing_x = step_left(answer, typing_x); +} + +/* Move right one character in the answer. */ +void do_statusbar_right(void) +{ + if (answer[typing_x] != '\0') + typing_x = step_right(answer, typing_x); +} + +/* Delete one character in the answer. */ +void do_statusbar_delete(void) +{ + if (answer[typing_x] != '\0') { + int charlen = char_length(answer + typing_x); + + memmove(answer + typing_x, answer + typing_x + charlen, + strlen(answer) - typing_x - charlen + 1); + } +} + +/* Backspace over one character in the answer. */ +void do_statusbar_backspace(void) +{ + if (typing_x > 0) { + typing_x = step_left(answer, typing_x); + do_statusbar_delete(); + } +} + +/* Zap some or all text from the answer. */ +void do_statusbar_cut_text(void) +{ + if (!ISSET(CUT_FROM_CURSOR)) + typing_x = 0; + + answer[typing_x] = '\0'; +} + /* Paste the first line of the cutbuffer into the current answer. */ void paste_into_answer(void) { @@ -67,6 +176,35 @@ int do_statusbar_mouse(void) } #endif +/* Insert the given short burst of bytes into the answer. */ +void inject_into_answer(char *burst, size_t count) +{ + /* First encode any embedded NUL byte as 0x0A. */ + for (size_t index = 0; index < count; index++) + if (burst[index] == '\0') + burst[index] = '\n'; + + answer = charealloc(answer, strlen(answer) + count + 1); + memmove(answer + typing_x + count, answer + typing_x, + strlen(answer) - typing_x + 1); + strncpy(answer + typing_x, burst , count); + + typing_x += count; +} + +/* Get a verbatim keystroke and insert it into the answer. */ +void do_statusbar_verbatim_input(void) +{ + char *bytes; + size_t count; + + bytes = get_verbatim_kbinput(bottomwin, &count); + + inject_into_answer(bytes, count); + + free(bytes); +} + /* Read in a keystroke, interpret it if it is a shortcut or toggle, and * return it. Set finished to TRUE if we're done after running * or trying to run a function associated with a shortcut key. */ @@ -190,144 +328,6 @@ int do_statusbar_input(bool *finished) return input; } -/* Insert the given short burst of bytes into the answer. */ -void inject_into_answer(char *burst, size_t count) -{ - /* First encode any embedded NUL byte as 0x0A. */ - for (size_t index = 0; index < count; index++) - if (burst[index] == '\0') - burst[index] = '\n'; - - answer = charealloc(answer, strlen(answer) + count + 1); - memmove(answer + typing_x + count, answer + typing_x, - strlen(answer) - typing_x + 1); - strncpy(answer + typing_x, burst , count); - - typing_x += count; -} - -/* Move to the beginning of the answer. */ -void do_statusbar_home(void) -{ - typing_x = 0; -} - -/* Move to the end of the answer. */ -void do_statusbar_end(void) -{ - typing_x = strlen(answer); -} - -/* Move left one character. */ -void do_statusbar_left(void) -{ - if (typing_x > 0) - typing_x = step_left(answer, typing_x); -} - -/* Move right one character. */ -void do_statusbar_right(void) -{ - if (answer[typing_x] != '\0') - typing_x = step_right(answer, typing_x); -} - -/* Delete one character. */ -void do_statusbar_delete(void) -{ - if (answer[typing_x] != '\0') { - int charlen = char_length(answer + typing_x); - - memmove(answer + typing_x, answer + typing_x + charlen, - strlen(answer) - typing_x - charlen + 1); - } -} - -/* Backspace over one character. */ -void do_statusbar_backspace(void) -{ - if (typing_x > 0) { - typing_x = step_left(answer, typing_x); - do_statusbar_delete(); - } -} - -/* Zap some or all text from the answer. */ -void do_statusbar_cut_text(void) -{ - if (!ISSET(CUT_FROM_CURSOR)) - typing_x = 0; - - answer[typing_x] = '\0'; -} - -#ifndef NANO_TINY -/* Move to the next word in the answer. */ -void do_statusbar_next_word(void) -{ - bool seen_space = !is_word_mbchar(answer + typing_x, FALSE); - bool seen_word = !seen_space; - - /* Move forward until we reach either the end or the start of a word, - * depending on whether the AFTER_ENDS flag is set or not. */ - while (answer[typing_x] != '\0') { - typing_x = step_right(answer, typing_x); - - if (ISSET(AFTER_ENDS)) { - /* If this is a word character, continue; else it's a separator, - * and if we've already seen a word, then it's a word end. */ - if (is_word_mbchar(answer + typing_x, FALSE)) - seen_word = TRUE; - else if (seen_word) - break; - } else { - /* If this is not a word character, then it's a separator; else - * if we've already seen a separator, then it's a word start. */ - if (!is_word_mbchar(answer + typing_x, FALSE)) - seen_space = TRUE; - else if (seen_space) - break; - } - } -} - -/* Move to the previous word in the answer. */ -void do_statusbar_prev_word(void) -{ - bool seen_a_word = FALSE, step_forward = FALSE; - - /* Move backward until we pass over the start of a word. */ - while (typing_x != 0) { - typing_x = step_left(answer, typing_x); - - if (is_word_mbchar(answer + typing_x, FALSE)) - seen_a_word = TRUE; - else if (seen_a_word) { - /* This is space now: we've overshot the start of the word. */ - step_forward = TRUE; - break; - } - } - - if (step_forward) - /* Move one character forward again to sit on the start of the word. */ - typing_x = step_right(answer, typing_x); -} -#endif /* !NANO_TINY */ - -/* Get a verbatim keystroke and insert it into the answer. */ -void do_statusbar_verbatim_input(void) -{ - char *bytes; - size_t count; - - bytes = get_verbatim_kbinput(bottomwin, &count); - - inject_into_answer(bytes, count); - - free(bytes); -} - /* Return the column number of the first character of the answer that is * displayed in the status bar when the cursor is at the given column, * with the available room for the answer starting at base. Note that diff --git a/src/proto.h b/src/proto.h index dd1a4d6a..c4204d56 100644 --- a/src/proto.h +++ b/src/proto.h @@ -445,19 +445,6 @@ bool okay_for_view(const keystruct *shortcut); void inject(char *burst, size_t count); /* Most functions in prompt.c. */ -void inject_into_answer(char *burst, size_t count); -void do_statusbar_home(void); -void do_statusbar_end(void); -void do_statusbar_left(void); -void do_statusbar_right(void); -void do_statusbar_backspace(void); -void do_statusbar_delete(void); -void do_statusbar_cut_text(void); -#ifndef NANO_TINY -void do_statusbar_prev_word(void); -void do_statusbar_next_word(void); -#endif -void do_statusbar_verbatim_input(void); size_t get_statusbar_page_start(size_t start_col, size_t column); void put_cursor_at_end_of_answer(void); void add_or_remove_pipe_symbol_from_answer(void);