From b8ba27bbc099c2a1922ef1a22666e1f23da0cc2d Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Sun, 26 Jan 2020 15:22:22 +0100 Subject: [PATCH] tweaks: fuse two nearly identical functions into a single one The plain keys that are valid in the help viewer are a perfect subset of those that are valid in the file browser, so just use the same function to do the interpretation for both. It is not a problem that it returns function pointers for some keystrokes that have no meaning in the help viewer, because both NULL and an unhandled function pointer result in the "Unbound key" message. --- src/browser.c | 40 +--------------------------------------- src/global.c | 42 ++++++++++++++++++++++++++++++++++++++++++ src/help.c | 32 +------------------------------- src/proto.h | 3 +++ 4 files changed, 47 insertions(+), 70 deletions(-) diff --git a/src/browser.c b/src/browser.c index 25916252..589d35f7 100644 --- a/src/browser.c +++ b/src/browser.c @@ -151,7 +151,7 @@ char *do_browser(char *path) continue; } #endif - func = parse_browser_input(&kbinput); + func = interpret(&kbinput); if (func == total_refresh) { total_redraw(); @@ -453,44 +453,6 @@ void read_the_list(const char *path, DIR *dir) width = (COLS + 2) / (longest + 2); } -/* Return the function that is bound to the given key, accepting certain - * plain characters too, for compatibility with Pico. */ -functionptrtype parse_browser_input(int *kbinput) -{ - if (!meta_key) { - switch (*kbinput) { - case '-': - return do_page_up; - case ' ': - return do_page_down; - case 'W': - case 'w': - case '/': - return do_search_forward; - case 'N': - return do_findprevious; - case 'n': - return do_findnext; - case 'G': - case 'g': - return goto_dir_void; - case '?': - return do_help; - case 'S': - case 's': - return do_enter; - case 'E': - case 'e': - case 'Q': - case 'q': - case 'X': - case 'x': - return do_exit; - } - } - return func_from_key(kbinput); -} - /* Set width to the number of files that we can display per screen row, * if necessary, and display the list of files. */ void browser_refresh(void) diff --git a/src/global.c b/src/global.c index 4289c8cb..f34a4020 100644 --- a/src/global.c +++ b/src/global.c @@ -482,6 +482,48 @@ functionptrtype func_from_key(int *kbinput) return NULL; } +#if defined(ENABLE_BROWSER) || defined(ENABLE_HELP) +/* Return the function that is bound to the given key in the file browser or + * the help viewer. Accept also certain plain characters, for compatibility + * with Pico or to mimic 'less' and similar text viewers. */ +functionptrtype interpret(int *keycode) +{ + if (!meta_key) { + switch (*keycode) { + case '-': + return do_page_up; + case ' ': + return do_page_down; + case 'W': + case 'w': + case '/': + return do_search_forward; + case 'N': + return do_findprevious; + case 'n': + return do_findnext; + case 'G': + case 'g': + return goto_dir_void; + case '?': + return do_help; + case 'S': + case 's': + return do_enter; + case 'E': + case 'e': + case 'Q': + case 'q': + case 'X': + case 'x': + return do_exit; + } + } + + return func_from_key(keycode); +} +#endif /* ENABLE_BROWSER || ENABLE_HELP */ + /* Parse the given keystring and return the corresponding keycode, * or return -1 when the string is invalid. */ int keycode_from_string(const char *keystring) diff --git a/src/help.c b/src/help.c index cd1ce4e4..e5ff505e 100644 --- a/src/help.c +++ b/src/help.c @@ -193,7 +193,7 @@ void show_help(void) continue; } #endif - func = parse_help_input(&kbinput); + func = interpret(&kbinput); if (func == total_refresh) { total_redraw(); @@ -563,36 +563,6 @@ void help_init(void) } #endif /* !NANO_TINY */ } - -/* Return the function that is bound to the given key, accepting certain - * plain characters too, for consistency with the file browser. */ -functionptrtype parse_help_input(int *kbinput) -{ - if (!meta_key) { - switch (*kbinput) { - case '-': - return do_page_up; - case ' ': - return do_page_down; - case 'W': - case 'w': - case '/': - return do_search_forward; - case 'N': - return do_findprevious; - case 'n': - return do_findnext; - case 'E': - case 'e': - case 'Q': - case 'q': - case 'X': - case 'x': - return do_exit; - } - } - return func_from_key(kbinput); -} #endif /* ENABLE_HELP */ /* Start the help viewer, or indicate that there is no help. */ diff --git a/src/proto.h b/src/proto.h index db9a7d09..85dbab60 100644 --- a/src/proto.h +++ b/src/proto.h @@ -334,6 +334,9 @@ int the_code_for(void (*func)(void), int defaultval); size_t shown_entries_for(int menu); const keystruct *get_shortcut(int *kbinput); functionptrtype func_from_key(int *kbinput); +#if defined(ENABLE_BROWSER) || defined(ENABLE_HELP) +functionptrtype interpret(int *keycode); +#endif int keycode_from_string(const char *keystring); void shortcut_init(void); const char *flagtostr(int flag);