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.
master
Benno Schulenberg 2020-01-26 15:22:22 +01:00
parent e3f6638a76
commit b8ba27bbc0
4 changed files with 47 additions and 70 deletions

View File

@ -151,7 +151,7 @@ char *do_browser(char *path)
continue; continue;
} }
#endif #endif
func = parse_browser_input(&kbinput); func = interpret(&kbinput);
if (func == total_refresh) { if (func == total_refresh) {
total_redraw(); total_redraw();
@ -453,44 +453,6 @@ void read_the_list(const char *path, DIR *dir)
width = (COLS + 2) / (longest + 2); 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, /* Set width to the number of files that we can display per screen row,
* if necessary, and display the list of files. */ * if necessary, and display the list of files. */
void browser_refresh(void) void browser_refresh(void)

View File

@ -482,6 +482,48 @@ functionptrtype func_from_key(int *kbinput)
return NULL; 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, /* Parse the given keystring and return the corresponding keycode,
* or return -1 when the string is invalid. */ * or return -1 when the string is invalid. */
int keycode_from_string(const char *keystring) int keycode_from_string(const char *keystring)

View File

@ -193,7 +193,7 @@ void show_help(void)
continue; continue;
} }
#endif #endif
func = parse_help_input(&kbinput); func = interpret(&kbinput);
if (func == total_refresh) { if (func == total_refresh) {
total_redraw(); total_redraw();
@ -563,36 +563,6 @@ void help_init(void)
} }
#endif /* !NANO_TINY */ #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 */ #endif /* ENABLE_HELP */
/* Start the help viewer, or indicate that there is no help. */ /* Start the help viewer, or indicate that there is no help. */

View File

@ -334,6 +334,9 @@ int the_code_for(void (*func)(void), int defaultval);
size_t shown_entries_for(int menu); size_t shown_entries_for(int menu);
const keystruct *get_shortcut(int *kbinput); const keystruct *get_shortcut(int *kbinput);
functionptrtype func_from_key(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); int keycode_from_string(const char *keystring);
void shortcut_init(void); void shortcut_init(void);
const char *flagtostr(int flag); const char *flagtostr(int flag);