n/N restart the previous search if none is active
authorYoann Blein <yoann.blein@free.fr>
Sat, 28 Oct 2017 16:22:53 +0000 (18:22 +0200)
committerYoann Blein <yoann.blein@free.fr>
Sat, 28 Oct 2017 16:39:23 +0000 (18:39 +0200)
vimb no longer ignores n/N if no search is active. Instead, it restarts
a search with the same query that was used in the last search, just like
Vim does. This is very convenient when searching for the same query in
different pages.

src/command.c
src/main.c
src/main.h

index dbb5b66..c147cec 100644 (file)
@@ -22,6 +22,7 @@
  * together.
  */
 #include <stdlib.h>
+#include <string.h>
 
 #include "config.h"
 #ifdef FEATURE_QUEUE
@@ -44,6 +45,7 @@ gboolean command_search(Client *c, const Arg *arg, bool commit)
     WebKitFindController *fc;
     const char *query;
     guint count;
+    int direction;
 
     fc = webkit_web_view_get_find_controller(c->webview);
 
@@ -62,7 +64,6 @@ gboolean command_search(Client *c, const Arg *arg, bool commit)
         }
 
         c->state.search.active = FALSE;
-        c->state.search.direction = 0;
         c->state.search.matches = 0;
 
         vb_statusbar_update(c);
@@ -72,6 +73,14 @@ gboolean command_search(Client *c, const Arg *arg, bool commit)
 
     query = arg->s;
     count = abs(arg->i);
+    direction = arg->i > 0 ? 1 : -1;
+
+    /* restart the last search if a search result is asked for and no
+     * search was active */
+    if (!query && !c->state.search.active) {
+        query = c->state.search.last_query;
+        direction = c->state.search.direction;
+    }
 
     /* Only committed search strings adjust registers and are recorded in
      * history, intermediate strings (while using incsearch) don't. */
@@ -95,14 +104,21 @@ gboolean command_search(Client *c, const Arg *arg, bool commit)
             webkit_find_controller_search(fc, "", WEBKIT_FIND_OPTIONS_NONE, G_MAXUINT);
         }
 
+        if (!c->state.search.last_query) {
+            c->state.search.last_query = g_strdup(query);
+        } else if (strcmp(c->state.search.last_query, query)) {
+            g_free(c->state.search.last_query);
+            c->state.search.last_query = g_strdup(query);
+        }
+
         webkit_find_controller_search(fc, query,
                 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
                 WEBKIT_FIND_OPTIONS_WRAP_AROUND |
-                (arg->i > 0 ?  WEBKIT_FIND_OPTIONS_NONE : WEBKIT_FIND_OPTIONS_BACKWARDS),
+                (direction > 0 ?  WEBKIT_FIND_OPTIONS_NONE : WEBKIT_FIND_OPTIONS_BACKWARDS),
                 G_MAXUINT);
 
         c->state.search.active = TRUE;
-        c->state.search.direction = arg->i > 0 ? 1 : -1;
+        c->state.search.direction = direction;
         /* TODO get the number of matches */
 
         /* Skip first search because the first match is already
index c440902..b56b17a 100644 (file)
@@ -643,6 +643,10 @@ static void client_destroy(Client *c)
         vb.clients = c->next;
     }
 
+    if (c->state.search.last_query) {
+        g_free(c->state.search.last_query);
+    }
+
     completion_cleanup(c);
     map_cleanup(c);
     register_cleanup(c);
index de66fc5..7c51fac 100644 (file)
@@ -176,6 +176,7 @@ struct State {
         gboolean    active;         /* indicate if there is a active search */
         short       direction;      /* last direction 1 forward, -1 backward */
         int         matches;        /* number of matching search results */
+        char        *last_query;    /* last search query */
     } search;
 };