diff --git a/ChangeLog b/ChangeLog index b5435fe3..276e9f15 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2014-07-02 Benno Schulenberg + * src/proto.h: Add a typedef for a pointer to a function. + * src/global.c (func_from_key): New wrapper. + * src/prompt.c (get_prompt_string, do_prompt): Use the new + wrapper to make the code a bit cleaner. + 2014-07-01 Benno Schulenberg * src/browser.c (do_browser), src/help.c (do_help): Make sure to always set 'currmenu', so that we can rely on it. diff --git a/src/global.c b/src/global.c index a6610d93..2f34fefb 100644 --- a/src/global.c +++ b/src/global.c @@ -405,6 +405,17 @@ int sc_seq_or(void (*func)(void), int defaultval) return defaultval; } +/* Return a pointer to the function that is bound to the given key. */ +functionptrtype func_from_key(int *kbinput) +{ + const sc *s = get_shortcut(kbinput); + + if (s) + return s->scfunc; + else + return NULL; +} + /* Assign the info to the shortcut struct. * Assumes keystr is already assigned, naturally. */ void assign_keyinfo(sc *s) diff --git a/src/prompt.c b/src/prompt.c index 719f2154..f3511c82 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -721,7 +721,7 @@ void total_statusbar_refresh(void (*refresh_func)(void)) /* Get a string of input at the statusbar prompt. This should only be * called from do_prompt(). */ -const sc *get_prompt_string(int *actual, bool allow_tabs, +functionptrtype get_prompt_string(int *actual, bool allow_tabs, #ifndef DISABLE_TABCOMP bool allow_files, bool *list, @@ -735,7 +735,7 @@ const sc *get_prompt_string(int *actual, bool allow_tabs, int kbinput = ERR; bool ran_func, finished; size_t curranswer_len; - const sc *s; + functionptrtype func; #ifndef DISABLE_TABCOMP bool tabbed = FALSE; /* Whether we've pressed Tab. */ @@ -796,17 +796,16 @@ fprintf(stderr, "get_prompt_string: answer = \"%s\", statusbar_x = %lu\n", answe kbinput = do_statusbar_input(&ran_func, &finished, refresh_func); assert(statusbar_x <= strlen(answer)); - s = get_shortcut(&kbinput); + func = func_from_key(&kbinput); - if (s) - if (s->scfunc == do_cancel || s->scfunc == do_enter_void) - break; + if (func == do_cancel || func == do_enter_void) + break; #ifndef DISABLE_TABCOMP - if (s && s->scfunc != do_tab) + if (func != do_tab) tabbed = FALSE; - if (s && s->scfunc == do_tab) { + if (func == do_tab) { #ifndef DISABLE_HISTORIES if (history_list != NULL) { if (last_kbinput != sc_seq_or(do_tab, NANO_CONTROL_I)) @@ -828,7 +827,7 @@ fprintf(stderr, "get_prompt_string: answer = \"%s\", statusbar_x = %lu\n", answe } else #endif /* !DISABLE_TABCOMP */ #ifndef DISABLE_HISTORIES - if (s && s->scfunc == get_history_older_void) { + if (func == get_history_older_void) { if (history_list != NULL) { /* If we're scrolling up at the bottom of the history list * and answer isn't blank, save answer in magichistory. */ @@ -850,7 +849,7 @@ fprintf(stderr, "get_prompt_string: answer = \"%s\", statusbar_x = %lu\n", answe * we aren't kicked out of the statusbar prompt. */ finished = FALSE; } - } else if (s && s->scfunc == get_history_newer_void) { + } else if (func == get_history_newer_void) { if (history_list != NULL) { /* Get the newer search from the history list and save it in * answer. If there is no newer search, don't do anything. */ @@ -878,7 +877,7 @@ fprintf(stderr, "get_prompt_string: answer = \"%s\", statusbar_x = %lu\n", answe } } else #endif /* !DISABLE_HISTORIES */ - if (s && s->scfunc == do_help_void) { + if (func == do_help_void) { update_statusbar_line(answer, statusbar_x); /* This key has a shortcut-list entry when it's used to go to @@ -919,9 +918,8 @@ fprintf(stderr, "get_prompt_string: answer = \"%s\", statusbar_x = %lu\n", answe * associated function, so reset statusbar_x and statusbar_pww. If * we've finished putting in an answer, reset the statusbar cursor * position too. */ - if (s) { - if (s->scfunc == do_cancel || s->scfunc == do_enter_void || - ran_func) { + if (func) { + if (func == do_cancel || func == do_enter_void || ran_func) { statusbar_x = old_statusbar_x; statusbar_pww = old_pww; @@ -935,7 +933,8 @@ fprintf(stderr, "get_prompt_string: answer = \"%s\", statusbar_x = %lu\n", answe } *actual = kbinput; - return s; + + return func; } /* Ask a question on the statusbar. The prompt will be stored in the @@ -962,7 +961,7 @@ int do_prompt(bool allow_tabs, { va_list ap; int retval; - const sc *s; + functionptrtype func; #ifndef DISABLE_TABCOMP bool list = FALSE; #endif @@ -982,7 +981,7 @@ int do_prompt(bool allow_tabs, va_end(ap); null_at(&prompt, actual_x(prompt, COLS - 4)); - s = get_prompt_string(&retval, allow_tabs, + func = get_prompt_string(&retval, allow_tabs, #ifndef DISABLE_TABCOMP allow_files, &list, @@ -1003,9 +1002,9 @@ int do_prompt(bool allow_tabs, /* If we left the prompt via Cancel or Enter, set the return value * properly. */ - if (s && s->scfunc == do_cancel) + if (func == do_cancel) retval = -1; - else if (s && s->scfunc == do_enter_void) + else if (func == do_enter_void) retval = (*answer == '\0') ? -2 : 0; blank_statusbar(); diff --git a/src/proto.h b/src/proto.h index 3fa197aa..3cae50ea 100644 --- a/src/proto.h +++ b/src/proto.h @@ -139,6 +139,8 @@ extern color_pair interface_color_pair[NUMBER_OF_ELEMENTS]; extern char *homedir; +typedef void (*functionptrtype)(void); + /* All functions in browser.c. */ #ifndef DISABLE_BROWSER char *do_browser(char *path, DIR *dir); @@ -352,6 +354,7 @@ size_t length_of_list(int menu); key_type strtokeytype(const char *str); const sc *first_sc_for(int menu, void (*func)(void)); int sc_seq_or(void (*func)(void), int defaultval); +functionptrtype func_from_key(int *kbinput); void assign_keyinfo(sc *s); void print_sclist(void); void shortcut_init(void); @@ -522,7 +525,7 @@ void reset_statusbar_cursor(void); void update_statusbar_line(const char *curranswer, size_t index); bool need_statusbar_horizontal_update(size_t pww_save); void total_statusbar_refresh(void (*refresh_func)(void)); -const sc *get_prompt_string(int *value, bool allow_tabs, +functionptrtype get_prompt_string(int *value, bool allow_tabs, #ifndef DISABLE_TABCOMP bool allow_files, bool *list,