Moved inputbox text decisions into main.c.
authorDaniel Carl <danielcarl@gmx.de>
Wed, 24 Apr 2013 22:37:20 +0000 (00:37 +0200)
committerDaniel Carl <danielcarl@gmx.de>
Wed, 24 Apr 2013 22:53:36 +0000 (00:53 +0200)
So we have the decision for completion types and history lookup types in a
central place.

src/completion.c
src/history.c
src/main.c
src/main.h

index 128ea9c..64114b9 100644 (file)
@@ -17,6 +17,7 @@
  * along with this program. If not, see http://www.gnu.org/licenses/.
  */
 
+#include "main.h"
 #include "completion.h"
 #include "util.h"
 #include "history.h"
@@ -51,9 +52,13 @@ static void free_completion(Completion *completion);
 
 gboolean completion_complete(gboolean back)
 {
-    const char *input = GET_TEXT();
+    VbInputType type;
+    const char *input, *prefix, *suffix;
     GList *source = NULL, *tmp = NULL;
 
+    input = GET_TEXT();
+    type  = vb_get_input_parts(input, &prefix, &suffix);
+
     if (comps.completions && comps.active
         && (vb.state.mode & VB_MODE_COMPLETE)
     ) {
@@ -85,57 +90,40 @@ gboolean completion_complete(gboolean back)
 #endif
     gtk_box_pack_start(GTK_BOX(vb.gui.box), vb.gui.compbox, false, false, 0);
 
-    /* TODO move these decision to a more generic place */
-    if (!strncmp(input, ":set ", 5)) {
+    if (type == VB_INPUT_SET) {
         source = g_list_sort(setting_get_all(), (GCompareFunc)g_strcmp0);
         comps.completions = init_completion(
             comps.completions,
-            filter_list(tmp, source, (Comp_Func)g_str_has_prefix, &input[5]),
-            ":set "
+            filter_list(tmp, source, (Comp_Func)g_str_has_prefix, suffix),
+            prefix
         );
         g_list_free(source);
-    } else if (!strncmp(input, ":open ", 6)) {
+    } else if (type == VB_INPUT_OPEN || type == VB_INPUT_TABOPEN) {
         source = history_get_all(HISTORY_URL);
-        tmp = filter_list(tmp, source, (Comp_Func)util_strcasestr, &input[6]),
+        tmp = filter_list(tmp, source, (Comp_Func)util_strcasestr, suffix),
         /* prepend the bookmark items */
-        tmp = g_list_concat(bookmark_get_by_tags(&input[6]), tmp);
-        comps.completions = init_completion(
-            comps.completions,
-            tmp,
-            ":open "
-        );
-
-        history_list_free(&source);
-    } else if (!strncmp(input, ":tabopen ", 9)) {
-        source = history_get_all(HISTORY_URL);
-        tmp = filter_list(tmp, source, (Comp_Func)util_strcasestr, &input[9]),
-        /* prepend the bookmark items */
-        tmp = g_list_concat(bookmark_get_by_tags(&input[9]), tmp);
-        comps.completions = init_completion(
-            comps.completions,
-            tmp,
-            ":tabopen "
-        );
+        tmp = g_list_concat(bookmark_get_by_tags(suffix), tmp);
+        comps.completions = init_completion(comps.completions, tmp, prefix);
 
         history_list_free(&source);
-    } else if (*input == ':') {
+    } else if (type == VB_INPUT_COMMAND) {
         char *command = NULL;
         /* remove counts before command and save it to print it later in inputbox */
-        comps.count = g_ascii_strtoll(&input[1], &command, 10);
+        comps.count = g_ascii_strtoll(suffix, &command, 10);
 
         source = g_list_sort(command_get_all(), (GCompareFunc)g_strcmp0);
         comps.completions = init_completion(
             comps.completions,
             filter_list(tmp, source, (Comp_Func)g_str_has_prefix, command),
-            ":"
+            prefix
         );
         g_list_free(source);
-    } else if (*input == '/' || *input == '?') {
+    } else if (type == VB_INPUT_SEARCH_FORWARD || type == VB_INPUT_SEARCH_BACKWARD) {
         source = g_list_sort(history_get_all(HISTORY_SEARCH), (GCompareFunc)g_strcmp0);
         comps.completions = init_completion(
             comps.completions,
-            filter_list(tmp, source, (Comp_Func)g_str_has_prefix, &input[1]),
-            *input == '/' ? "/" : "?"
+            filter_list(tmp, source, (Comp_Func)g_str_has_prefix, suffix),
+            prefix
         );
         g_list_free(source);
     }
index c4ce593..ecbc42f 100644 (file)
@@ -102,7 +102,7 @@ char *history_get(const char *input, gboolean prev)
     } else if ((new = g_list_next(history.active))) {
         history.active = new;
     }
-    
+
     return g_strconcat(history.prefix, (char*)history.active->data, NULL);
 }
 
@@ -131,26 +131,30 @@ void history_list_free(GList **list)
  */
 static GList *get_list(const char *input)
 {
+    VbInputType input_type;
     HistoryType type;
     GList *result = NULL;
+    const char *prefix, *suffix;
+
+    input_type = vb_get_input_parts(input, &prefix, &suffix);
 
     /* get the right history type and command prefix */
-    if (!strncmp(input, ":open ", 6)) {
-        type = HISTORY_URL;
-        OVERWRITE_STRING(history.query, input + 6);
-        OVERWRITE_STRING(history.prefix, ":open ");
-    } else if (!strncmp(input, ":tabopen ", 9)) {
+    if (input_type == VB_INPUT_OPEN
+        || input_type == VB_INPUT_TABOPEN
+    ) {
         type = HISTORY_URL;
-        OVERWRITE_STRING(history.query, input + 9);
-        OVERWRITE_STRING(history.prefix, ":tabopen ");
-    } else if (*input == ':') {
+        OVERWRITE_STRING(history.query, suffix);
+        OVERWRITE_STRING(history.prefix, prefix);
+    } else if (input_type == VB_INPUT_COMMAND) {
         type = HISTORY_COMMAND;
-        OVERWRITE_STRING(history.query, input + 1);
-        OVERWRITE_STRING(history.prefix, ":");
-    } else if (*input == '/' || *input == '?') {
+        OVERWRITE_STRING(history.query, suffix);
+        OVERWRITE_STRING(history.prefix, prefix);
+    } else if (input_type == VB_INPUT_SEARCH_FORWARD
+        || input_type == VB_INPUT_SEARCH_BACKWARD
+    ) {
         type = HISTORY_SEARCH;
-        OVERWRITE_STRING(history.query, input + 1);
-        OVERWRITE_STRING(history.prefix, (*input == '/') ? "/" : "?");
+        OVERWRITE_STRING(history.query, suffix);
+        OVERWRITE_STRING(history.prefix, prefix);
     } else {
         return NULL;
     }
index e846a88..2146103 100644 (file)
@@ -72,7 +72,7 @@ static void setup_signals();
 static void init_files(void);
 static gboolean hide_message();
 static void set_status(const StatusType status);
-void inputbox_print(gboolean force, const MessageType type, gboolean hide, const char *message);
+static void inputbox_print(gboolean force, const MessageType type, gboolean hide, const char *message);
 static void destroy_client();
 
 void vb_echo_force(const MessageType type, gboolean hide, const char *error, ...)
@@ -338,6 +338,48 @@ void vb_update_urlbar(const char *uri)
     gtk_label_set_text(GTK_LABEL(vb.gui.statusbar.left), uri);
 }
 
+/**
+ * Analyzes the given input string for known prefixes (':set ', ':open ', '/',
+ * ...) and set the given prefix pointer to the found prefix and the given
+ * suffix pointer to the suffix.
+ */
+VbInputType vb_get_input_parts(const char* input, const char **prefix, const char **clean)
+{
+    if (!strncmp(input, ":open ", 6)) {
+        *prefix = ":open ";
+        *clean  = input + 6;
+        return VB_INPUT_OPEN;
+    }
+    if (!strncmp(input, ":tabopen ", 9)) {
+        *prefix = ":tabopen ";
+        *clean  = input + 9;
+        return VB_INPUT_TABOPEN;
+    }
+    if (!strncmp(input, ":set ", 5)) {
+        *prefix = ":set ";
+        *clean  = input + 5;
+        return VB_INPUT_SET;
+    }
+    if (*input == ':') {
+        *prefix = ":";
+        *clean  = input + 1;
+        return VB_INPUT_COMMAND;
+    }
+    if (*input == '/') {
+        *prefix = "/";
+        *clean  = input + 1;
+        return VB_INPUT_SEARCH_FORWARD;
+    }
+    if (*input == '?') {
+        *prefix = "?";
+        *clean  = input + 1;
+        return VB_INPUT_SEARCH_BACKWARD;
+    }
+    *prefix = NULL;
+    *clean  = input;
+    return VB_INPUT_UNKNOWN;
+}
+
 static gboolean hide_message()
 {
     inputbox_print(false, VB_MSG_NORMAL, false, "");
@@ -539,7 +581,7 @@ static void set_status(const StatusType status)
     }
 }
 
-void inputbox_print(gboolean force, const MessageType type, gboolean hide, const char *message)
+static void inputbox_print(gboolean force, const MessageType type, gboolean hide, const char *message)
 {
     /* don't print message if the input is focussed */
     if (!force && gtk_widget_is_focus(GTK_WIDGET(vb.gui.inputbox))) {
index fa721c8..011f7e0 100644 (file)
@@ -111,6 +111,16 @@ typedef enum _vb_mode {
     VB_MODE_HINTING       = 1<<5, /* command mode */
 } Mode;
 
+typedef enum {
+    VB_INPUT_UNKNOWN,
+    VB_INPUT_SET,
+    VB_INPUT_OPEN,
+    VB_INPUT_TABOPEN,
+    VB_INPUT_COMMAND,
+    VB_INPUT_SEARCH_FORWARD,
+    VB_INPUT_SEARCH_BACKWARD
+} VbInputType;
+
 enum {
     VB_NAVIG_BACK,
     VB_NAVIG_FORWARD,
@@ -311,5 +321,6 @@ void vb_update_statusbar(void);
 void vb_update_status_style(void);
 void vb_update_input_style(void);
 void vb_update_urlbar(const char *uri);
+VbInputType vb_get_input_parts(const char* input, const char **prefix, const char **clean);
 
 #endif /* end of include guard: _MAIN_H */