Split command history into command and search history.
authorDaniel Carl <danielcarl@gmx.de>
Sun, 24 Mar 2013 17:14:30 +0000 (18:14 +0100)
committerDaniel Carl <danielcarl@gmx.de>
Sun, 24 Mar 2013 17:14:30 +0000 (18:14 +0100)
Allow special history also for :open and :tabopen commands.

doc/vimb.1.txt
src/command.c
src/history.c
src/history.h
src/main.c
src/main.h

index 39f51bf..7aa0a49 100644 (file)
@@ -325,6 +325,11 @@ This file holds the history of unique opened URIs.
 This file holds the history of commands and search queries performed via input
 box.
 .RE
+.I $XDG_CONFIG_HOME/PROJECT/search
+.RS
+This file holds the history of search queries.
+box.
+.RE
 .I $XDG_CONFIG_HOME/PROJECT/scripts.js
 .RS
 This file can be used to run user scripts, that are injected into every paged
index b1468a9..326ebf4 100644 (file)
@@ -525,14 +525,33 @@ gboolean command_zoom(const Arg* arg)
 
 gboolean command_history(const Arg* arg)
 {
-    const int count = vb.state.count ? vb.state.count : 1;
-    const gint step = count * arg->i;
-    const char* entry = history_get(HISTORY_COMMAND, step);
+    const char* input = GET_TEXT();
+    int step          = vb.state.count ? vb.state.count * arg->i : arg->i;
+    const char* entry;
+    char* prefix = NULL;
+
+    /* use the right history type according to current input text */
+    if (!strncmp(input, ":open ", 6)) {
+        entry  = history_get(HISTORY_URL, step, input + 6);
+        prefix = ":open ";
+    } else if (!strncmp(input, ":tabopen ", 9)) {
+        entry  = history_get(HISTORY_URL, step, input + 9);
+        prefix = ":tabopen ";
+    } else if (*input == ':') {
+        entry  = history_get(HISTORY_COMMAND, step, input + 1);
+        prefix = ":";
+    } else if (*input == '/' || *input == '?') {
+        entry  = history_get(HISTORY_SEARCH, step, input + 1);
+        prefix = *input == '/' ? "/" : "?";
+    }
 
     if (!entry) {
         return FALSE;
     }
-    command_write_input(entry);
+
+    char* value = g_strconcat(prefix, entry, NULL);
+    command_write_input(value);
+    g_free(value);
 
     return TRUE;
 }
index 4b31317..fd69d42 100644 (file)
@@ -25,6 +25,7 @@ extern VbCore vb;
 /* map history types to files */
 static const VbFile file_map[HISTORY_LAST] = {
     FILES_COMMAND,
+    FILES_SEARCH,
     FILES_HISTORY
 };
 
@@ -70,13 +71,13 @@ GList* history_get_all(HistoryType type)
     return history_load(history_get_file_by_type(type));
 }
 
-const char* history_get(HistoryType type, int step)
+const char* history_get(HistoryType type, int step, const char* query)
 {
     const char* command;
 
     /* get the search prefix only on start of history search */
     if (!vb.state.history_active) {
-        OVERWRITE_STRING(vb.state.history_prefix, GET_TEXT());
+        OVERWRITE_STRING(vb.state.history_prefix, query);
 
         GList* src = history_load(history_get_file_by_type(type));
 
index 6e618d4..4928353 100644 (file)
@@ -23,6 +23,7 @@
 typedef enum {
     HISTORY_FIRST   = 0,
     HISTORY_COMMAND = 0,
+    HISTORY_SEARCH,
     HISTORY_URL,
     HISTORY_LAST
 } HistoryType;
@@ -30,7 +31,7 @@ typedef enum {
 void history_cleanup(void);
 void history_add(HistoryType type, const char* value);
 GList* history_get_all(HistoryType type);
-const char* history_get(HistoryType type, int step);
+const char* history_get(HistoryType type, int step, const char* query);
 void history_rewind(void);
 void history_list_free(GList** list);
 
index 7466e5f..7451cbd 100644 (file)
@@ -430,7 +430,6 @@ static void vb_destroy_window_cb(GtkWidget* widget)
 static void vb_inputbox_activate_cb(GtkEntry *entry)
 {
     const char* text;
-    gboolean hist_save = FALSE;
     char* command  = NULL;
     guint16 length = gtk_entry_get_text_length(entry);
 
@@ -459,20 +458,16 @@ static void vb_inputbox_activate_cb(GtkEntry *entry)
             a.i = *text == '/' ? VB_SEARCH_FORWARD : VB_SEARCH_BACKWARD;
             a.s = (command + 1);
             command_search(&a);
-            hist_save = TRUE;
+            history_add(HISTORY_SEARCH, command + 1);
             break;
 
         case ':':
             completion_clean();
             vb_process_input((command + 1));
-            hist_save = TRUE;
+            history_add(HISTORY_COMMAND, command + 1);
             break;
     }
 
-    if (hist_save) {
-        /* save the command in history */
-        history_add(HISTORY_COMMAND, command);
-    }
     g_free(command);
 }
 
@@ -839,6 +834,9 @@ static void vb_init_files(void)
     vb.files[FILES_COMMAND] = g_build_filename(path, "command", NULL);
     util_create_file_if_not_exists(vb.files[FILES_COMMAND]);
 
+    vb.files[FILES_SEARCH] = g_build_filename(path, "search", NULL);
+    util_create_file_if_not_exists(vb.files[FILES_SEARCH]);
+
     vb.files[FILES_SCRIPT] = g_build_filename(path, "scripts.js", NULL);
 
     vb.files[FILES_USER_STYLE] = g_build_filename(path, "style.css", NULL);
index bf0202c..76f3829 100644 (file)
@@ -183,6 +183,7 @@ typedef enum {
     FILES_SCRIPT,
     FILES_HISTORY,
     FILES_COMMAND,
+    FILES_SEARCH,
     FILES_USER_STYLE,
     FILES_LAST
 } VbFile;