From 633343de2413a65a5311cfda84c252d8f9e35122 Mon Sep 17 00:00:00 2001 From: David Lawrence Ramsey Date: Mon, 14 Aug 2017 15:58:25 -0500 Subject: [PATCH] browser: allow backward/forward re-searches with Alt+Up/Alt+Down Also bind 'N' to a backward re-search, while retaining 'n' for a forward one, This matches the functionality in the help viewer. --- src/browser.c | 43 ++++++++++++++++++++++++++++--------------- src/global.c | 4 ++-- src/proto.h | 2 +- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/browser.c b/src/browser.c index 2617dbc0..34f8b8e5 100644 --- a/src/browser.c +++ b/src/browser.c @@ -171,11 +171,15 @@ char *do_browser(char *path) say_there_is_no_help(); #endif } else if (func == do_search) { - /* Search for a filename. */ do_filesearch(); } else if (func == do_research) { - /* Search for another filename. */ - do_fileresearch(); + do_fileresearch(FALSE); +#ifndef NANO_TINY + } else if (func == do_findprevious) { + do_fileresearch(TRUE); + } else if (func == do_findnext) { + do_fileresearch(FALSE); +#endif } else if (func == do_left) { if (selected > 0) selected--; @@ -488,6 +492,9 @@ functionptrtype parse_browser_input(int *kbinput) case '/': return do_search; case 'N': +#ifndef NANO_TINY + return do_findprevious; +#endif case 'n': return do_research; } @@ -697,8 +704,9 @@ int filesearch_init(void) return input; } -/* Look for the given needle in the list of files. */ -void findnextfile(const char *needle) +/* Look for the given needle in the list of files. If forwards is TRUE, + * search forward in the list; otherwise, search backward. */ +void findfile(const char *needle, bool forwards) { size_t looking_at = selected; /* The location in the file list of the filename we're looking at. */ @@ -718,12 +726,16 @@ void findnextfile(const char *needle) /* Step through each filename in the list until a match is found or * we've come back to the point where we started. */ while (TRUE) { - /* Move to the next filename in the list, or back to the first. */ - if (looking_at < filelist_len - 1) - looking_at++; - else { - looking_at = 0; - statusbar(_("Search Wrapped")); + if (forwards) { + if (looking_at++ == filelist_len - 1) { + looking_at = 0; + statusbar(_("Search Wrapped")); + } + } else { + if (looking_at-- == 0) { + looking_at = filelist_len - 1; + statusbar(_("Search Wrapped")); + } } /* Get the bare filename, without the path. */ @@ -770,16 +782,17 @@ void do_filesearch(void) update_history(&search_history, answer); #endif - findnextfile(answer); + findfile(answer, TRUE); } -/* Search again for the last given filename, without prompting. */ -void do_fileresearch(void) +/* Search again without prompting for the last given search string, + * either forwards or backwards. */ +void do_fileresearch(bool forwards) { if (*last_search == '\0') statusbar(_("No current search pattern")); else - findnextfile(last_search); + findfile(last_search, forwards); } /* Select the first file in the list. */ diff --git a/src/global.c b/src/global.c index 5d7b686e..f89d8efe 100644 --- a/src/global.c +++ b/src/global.c @@ -1143,8 +1143,8 @@ void shortcut_init(void) add_to_sclist(MMAIN, "M-\xE2\x86\x92", ALT_RIGHT, switch_to_next_buffer_void, 0); #endif #ifndef NANO_TINY - add_to_sclist(MMAIN|MHELP, "M-\xE2\x86\x91", ALT_UP, do_findprevious, 0); - add_to_sclist(MMAIN|MHELP, "M-\xE2\x86\x93", ALT_DOWN, do_findnext, 0); + add_to_sclist(MMAIN|MHELP|MBROWSER, "M-\xE2\x86\x91", ALT_UP, do_findprevious, 0); + add_to_sclist(MMAIN|MHELP|MBROWSER, "M-\xE2\x86\x93", ALT_DOWN, do_findnext, 0); #endif } else #endif diff --git a/src/proto.h b/src/proto.h index 0b63f179..5f04d325 100644 --- a/src/proto.h +++ b/src/proto.h @@ -186,7 +186,7 @@ functionptrtype parse_browser_input(int *kbinput); void browser_refresh(void); void browser_select_dirname(const char *needle); void do_filesearch(void); -void do_fileresearch(void); +void do_fileresearch(bool forwards); void do_first_file(void); void do_last_file(void); char *strip_last_component(const char *path);