Simplified the command_search function.
authorDaniel Carl <danielcarl@gmx.de>
Fri, 27 Sep 2013 22:38:42 +0000 (00:38 +0200)
committerDaniel Carl <danielcarl@gmx.de>
Fri, 27 Sep 2013 22:38:42 +0000 (00:38 +0200)
src/command.c
src/command.h
src/ex.c
src/normal.c

index 3aabcfa..9089f38 100644 (file)
 
 extern VbCore vb;
 
-gboolean command_search(const Arg *arg, unsigned int count)
+gboolean command_search(const Arg *arg)
 {
-    static SearchDirection dir;
+    static short dir;   /* last direction 1 forward, -1 backward*/
     static char *query = NULL;
 #ifdef FEATURE_SEARCH_HIGHLIGHT
     static gboolean highlight = false;
 #endif
     gboolean forward;
 
-    if (arg->i == COMMAND_SEARCH_OFF) {
+    if (arg->i == 0) {
 #ifdef FEATURE_SEARCH_HIGHLIGHT
         webkit_web_view_unmark_text_matches(vb.gui.webview);
         highlight = false;
@@ -50,12 +50,13 @@ gboolean command_search(const Arg *arg, unsigned int count)
     if (arg->s) {
         OVERWRITE_STRING(query, arg->s);
         /* set dearch dir only when the searching is started */
-        dir = arg->i;
+        dir = arg->i > 0 ? 1 : -1;
     }
 
-    forward = !(arg->i ^ dir);
+    forward = (arg->i * dir) > 0;
 
     if (query) {
+        unsigned int count = abs(arg->i);
 #ifdef FEATURE_SEARCH_HIGHLIGHT
         if (!highlight) {
             /* highlight matches if the search is started new or continued
index 42426a8..185ed1c 100644 (file)
 #ifndef _COMMAND_H
 #define _COMMAND_H
 
-typedef enum {
-    COMMAND_SEARCH_OFF,
-    COMMAND_SEARCH_FORWARD  = (1<<0),
-    COMMAND_SEARCH_BACKWARD = (1<<1),
-} SearchDirection;
-
 enum {
     COMMAND_YANK_ARG,
     COMMAND_YANK_URI,
@@ -46,7 +40,7 @@ enum {
 };
 #endif
 
-gboolean command_search(const Arg *arg, unsigned int count);
+gboolean command_search(const Arg *arg);
 gboolean command_history(const Arg *arg);
 gboolean command_yank(const Arg *arg);
 gboolean command_save(const Arg *arg);
index ecc74a1..8872de4 100644 (file)
--- a/src/ex.c
+++ b/src/ex.c
@@ -342,7 +342,7 @@ gboolean ex_fill_completion(GtkListStore *store, const char *input)
  */
 static void input_activate(void)
 {
-    gboolean forward = false;
+    int count = -1;
     char *text, *cmd;
     text = vb_get_input_text();
 
@@ -353,11 +353,11 @@ static void input_activate(void)
      * does vim also skip history recording for such mapped commands */
     cmd = text + 1;
     switch (*text) {
-        case '/': forward = true; /* fall throught */
+        case '/': count = 1; /* fall throught */
         case '?':
             history_add(HISTORY_SEARCH, cmd, NULL);
             mode_enter('n');
-            command_search(&((Arg){forward ? COMMAND_SEARCH_FORWARD : COMMAND_SEARCH_BACKWARD, cmd}), 1);
+            command_search(&((Arg){count, cmd}));
             break;
 
         case ';':
index 12f5599..af0b4e9 100644 (file)
@@ -218,7 +218,7 @@ void normal_enter(void)
 void normal_leave(void)
 {
     /* TODO clean those only if they where active */
-    command_search(&((Arg){COMMAND_SEARCH_OFF}), 0);
+    command_search(&((Arg){0}));
 }
 
 /**
@@ -272,7 +272,7 @@ static VbResult normal_clear_input(const NormalCmdInfo *info)
 {
     vb_set_input_text("");
     gtk_widget_grab_focus(GTK_WIDGET(vb.gui.webview));
-    command_search(&((Arg){COMMAND_SEARCH_OFF}), 0);
+    command_search(&((Arg){0}));
 
     return RESULT_COMPLETE;
 }
@@ -581,27 +581,27 @@ static VbResult normal_scroll(const NormalCmdInfo *info)
 
 static VbResult normal_search(const NormalCmdInfo *info)
 {
-    command_search(
-        &((Arg){info->cmd == 'n' ? COMMAND_SEARCH_FORWARD : COMMAND_SEARCH_BACKWARD}),
-        info->count > 0 ? info->count : 1
-    );
+    int count = (info->count > 0) ? info->count : 1;
+
+    command_search(&((Arg){info->cmd == 'n' ? count : -count}));
     return RESULT_COMPLETE;
 }
 
 static VbResult normal_search_selection(const NormalCmdInfo *info)
 {
+    int count;
+    char *query;
+
     /* there is no function to get the selected text so we copy current
      * selection to clipboard */
     webkit_web_view_copy_clipboard(vb.gui.webview);
-    char *query = gtk_clipboard_wait_for_text(PRIMARY_CLIPBOARD());
+    query = gtk_clipboard_wait_for_text(PRIMARY_CLIPBOARD());
     if (!query) {
         return RESULT_ERROR;
     }
+    count = (info->count > 0) ? info->count : 1;
 
-    command_search(
-        &((Arg){info->cmd == '*' ? COMMAND_SEARCH_FORWARD : COMMAND_SEARCH_BACKWARD, query}),
-        info->count > 0 ? info->count : 1
-    );
+    command_search(&((Arg){info->cmd == '*' ? count : -count}));
     g_free(query);
 
     return RESULT_COMPLETE;