From db88e588b4ca1d1e682b6a36f5c1e9c34c8c0205 Mon Sep 17 00:00:00 2001
From: David Lawrence Ramsey <pooka109@gmail.com>
Date: Mon, 14 Aug 2017 15:58:25 -0500
Subject: [PATCH] browser: allow backward/forward searches with Alt-Up/Alt-Down

Also allow 'N' for backward searches, and 'n' for forward searches.
This matches the functionality in the help browser.
---
 src/browser.c | 53 ++++++++++++++++++++++++++++++++++++-----------------
 src/global.c  |  8 ++++++--
 src/proto.h   |  2 +-
 3 files changed, 43 insertions(+), 20 deletions(-)

diff --git a/src/browser.c b/src/browser.c
index 2617dbc0..60951fec 100644
--- a/src/browser.c
+++ b/src/browser.c
@@ -173,9 +173,12 @@ char *do_browser(char *path)
 	} else if (func == do_search) {
 	    /* Search for a filename. */
 	    do_filesearch();
-	} else if (func == do_research) {
-	    /* Search for another filename. */
-	    do_fileresearch();
+	} else if (func == do_findprevious) {
+	    /* Search for the previous filename. */
+	    do_filesearchprevnext(TRUE);
+	} else if (func == do_findnext || func == do_research) {
+	    /* Search for the next filename. */
+	    do_filesearchprevnext(FALSE);
 	} else if (func == do_left) {
 	    if (selected > 0)
 		selected--;
@@ -488,8 +491,9 @@ functionptrtype parse_browser_input(int *kbinput)
 	    case '/':
 		return do_search;
 	    case 'N':
+		return do_findprevious;
 	    case 'n':
-		return do_research;
+		return do_findnext;
 	}
     }
     return func_from_key(kbinput);
@@ -697,8 +701,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 backwards is TRUE,
+ * search for the previous file.  Otherwise, search for the next file. */
+void findfile(const char *needle, bool backwards)
 {
     size_t looking_at = selected;
 	/* The location in the file list of the filename we're looking at. */
@@ -710,20 +715,33 @@ void findnextfile(const char *needle)
     /* Save the settings of all flags. */
     memcpy(stash, flags, sizeof(flags));
 
-    /* Search forward, case insensitive, and without regexes. */
-    UNSET(BACKWARDS_SEARCH);
+    /* Search case insensitive, and without regexes. */
+    if (backwards)
+	SET(BACKWARDS_SEARCH);
+    else
+	UNSET(BACKWARDS_SEARCH);
     UNSET(CASE_SENSITIVE);
     UNSET(USE_REGEXP);
 
     /* 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 previous filename in the list, or forward to the last. */
+	if (backwards) {
+	    if (looking_at > 0)
+		looking_at--;
+	    else {
+		looking_at = filelist_len - 1;
+		statusbar(_("Search Wrapped"));
+	    }
 	/* 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"));
+	} else {
+	    if (looking_at < filelist_len - 1)
+		looking_at++;
+	    else {
+		looking_at = 0;
+		statusbar(_("Search Wrapped"));
+	    }
 	}
 
 	/* Get the bare filename, without the path. */
@@ -770,16 +788,17 @@ void do_filesearch(void)
 	update_history(&search_history, answer);
 #endif
 
-    findnextfile(answer);
+    findfile(answer, FALSE);
 }
 
-/* Search again for the last given filename, without prompting. */
-void do_fileresearch(void)
+/* Search for the last given filename, without prompting.  If backwards is
+ * TRUE, search backward.  Otherwise, search forward. */
+void do_filesearchprevnext(bool backwards)
 {
     if (*last_search == '\0')
 	statusbar(_("No current search pattern"));
     else
-	findnextfile(last_search);
+	findfile(last_search, backwards);
 }
 
 /* Select the first file in the list. */
diff --git a/src/global.c b/src/global.c
index 58166405..a3664908 100644
--- a/src/global.c
+++ b/src/global.c
@@ -1037,6 +1037,10 @@ void shortcut_init(void)
 #ifdef ENABLE_BROWSER
     add_to_funcs(do_research, MBROWSER,
 	whereis_next_tag, IFSCHELP(nano_whereis_next_msg), TOGETHER, VIEW);
+    add_to_funcs(do_findprevious, MBROWSER,
+	N_("Previous"), IFSCHELP(nano_findprev_msg), TOGETHER, VIEW);
+    add_to_funcs(do_findnext, MBROWSER,
+	N_("Next"), IFSCHELP(nano_findnext_msg), TOGETHER, VIEW);
     add_to_funcs(total_refresh, MBROWSER,
 	refresh_tag, IFSCHELP(nano_browser_refresh_msg), BLANKAFTER, VIEW);
 #ifndef NANO_TINY
@@ -1144,8 +1148,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 4f1c7032..6e2f6ffb 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_filesearchprevnext(bool backwards);
 void do_first_file(void);
 void do_last_file(void);
 char *strip_last_component(const char *path);
-- 
2.14.1

