Added command to navigate in command history (#7).
authorDaniel Carl <danielcarl@gmx.de>
Fri, 22 Feb 2013 21:23:06 +0000 (22:23 +0100)
committerDaniel Carl <danielcarl@gmx.de>
Fri, 22 Feb 2013 21:23:06 +0000 (22:23 +0100)
doc/vimp.1.txt
src/command.c
src/command.h
src/config.h
src/history.c [new file with mode: 0644]
src/history.h [new file with mode: 0644]
src/main.c
src/main.h

index 019c0e5..da66b56 100644 (file)
@@ -277,6 +277,12 @@ Zoom \fIN\fP steps \fIin\fP or \fIout\fP of the current page \- effecting all el
 .TP
 .B zoomreset
 Reset the zoomlevel to the default value.
+.SS Command-History
+.TP
+.BI [ N "]command-hist-prev [" VALUE "], [" N "]command-hist-next [" VALUE ]
+Prints the \fIprev\fPious or \fInext\fP cammand from history into inputbox. If
+\fIVALUE\fP is given, this will be used as prefix for the inserted command,
+else the default ':' will be used.
 .SS Misc
 .TP
 .BI [ N "]search-{" forward ", " "backward" "}"
index 8f5d621..ecfa688 100644 (file)
@@ -89,6 +89,8 @@ static CommandInfo cmd_list[] = {
     {"zoominfull",          command_zoom,        {COMMAND_ZOOM_IN | COMMAND_ZOOM_FULL}},
     {"zoomoutfull",         command_zoom,        {COMMAND_ZOOM_OUT | COMMAND_ZOOM_FULL}},
     {"zoomreset",           command_zoom,        {COMMAND_ZOOM_RESET}},
+    {"command-hist-next",   command_history,     {VP_SEARCH_FORWARD, ":"}},
+    {"command-hist-prev",   command_history,     {VP_SEARCH_BACKWARD, ":"}},
 };
 
 static void command_write_input(const char* str);
@@ -526,6 +528,30 @@ gboolean command_zoom(const Arg* arg)
 
 }
 
+gboolean command_history(const Arg* arg)
+{
+    State* state    = &vp.state;
+    const int len   = g_list_length(state->history);
+    char* message   = NULL;
+    const int count = state->count ? state->count : 1;
+   
+    if (!len) {
+        return FALSE;
+    }
+    if (arg->i == VP_SEARCH_BACKWARD) {
+        state->history_pointer = (len + state->history_pointer - count) % len;
+    } else {
+        state->history_pointer = (len + state->history_pointer + count) % len;
+    }
+    
+    const char* command = (char*)g_list_nth_data(state->history, state->history_pointer);
+    message = g_strconcat(arg->s, command, NULL);
+    command_write_input(message);
+    g_free(message);
+
+    return TRUE;
+}
+
 static void command_write_input(const char* str)
 {
     int pos = 0;
index b09cf88..f9914c7 100644 (file)
@@ -68,5 +68,6 @@ gboolean command_paste(const Arg* arg);
 gboolean command_search(const Arg* arg);
 gboolean command_searchengine(const Arg* arg);
 gboolean command_zoom(const Arg* arg);
+gboolean command_history(const Arg* arg);
 
 #endif /* end of include guard: _COMMAND_H */
index 13e8fec..8771316 100644 (file)
@@ -28,6 +28,8 @@
 #define SETTING_MAX_CONNS           25
 #define SETTING_MAX_CONNS_PER_HOST  5
 
+const int COMMAND_HISTORY_SIZE = 30;
+
 const struct {
     char* command;
 } default_config[] = {
@@ -80,6 +82,8 @@ const struct {
     {"nmap zz=zoomreset"},
     {"cmap <tab>=complete"},
     {"cmap <shift-tab>=complete-back"},
+    {"cmap <ctrl-p>=command-hist-prev"},
+    {"cmap <ctrl-n>=command-hist-next"},
     {"hmap <tab>=hint-focus-next"},
     {"hmap <shift-tab>=hint-focus-prev"},
     {"searchengine-add dl=https://duckduckgo.com/lite/?q=%s"},
diff --git a/src/history.c b/src/history.c
new file mode 100644 (file)
index 0000000..8c6494c
--- /dev/null
@@ -0,0 +1,44 @@
+/**
+ * vimp - a webkit based vim like browser.
+ *
+ * Copyright (C) 2012-2013 Daniel Carl
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see http://www.gnu.org/licenses/.
+ */
+
+#include "main.h"
+#include "history.h"
+
+extern const int COMMAND_HISTORY_SIZE;
+
+void history_cleanup(void)
+{
+    g_list_free(vp.state.history);
+}
+
+void history_append(const char* line)
+{
+    State *s = &vp.state;
+
+    if (COMMAND_HISTORY_SIZE <= g_list_length(s->history)) {
+        /* if list is too long - remove items from beginning */
+        s->history = g_list_delete_link(s->history, g_list_first(s->history));
+    }
+    s->history = g_list_append(s->history, g_strdup(line));
+}
+
+void history_rewind(void)
+{
+    vp.state.history_pointer = 0;
+}
diff --git a/src/history.h b/src/history.h
new file mode 100644 (file)
index 0000000..476a46c
--- /dev/null
@@ -0,0 +1,27 @@
+/**
+ * vimp - a webkit based vim like browser.
+ *
+ * Copyright (C) 2012-2013 Daniel Carl
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see http://www.gnu.org/licenses/.
+ */
+
+#ifndef _HISTORY_H
+#define _HISTORY_H
+
+void history_cleanup(void);
+void history_append(const char* line);
+void history_rewind(void);
+
+#endif /* end of include guard: _HISTORY_H */
index c6cc1e4..ee44c43 100644 (file)
@@ -27,6 +27,7 @@
 #include "dom.h"
 #include "hints.h"
 #include "searchengine.h"
+#include "history.h"
 
 /* variables */
 static char **args;
@@ -303,6 +304,9 @@ static gboolean vp_process_input(const char* input)
     success = command_run(token[0], token[1] ? token[1] : NULL);
     g_strfreev(token);
 
+    /* save the command in history */
+    history_append(command);
+
     return success;
 }
 
@@ -462,6 +466,10 @@ gboolean vp_set_mode(Mode mode, gboolean clean)
     }
     switch (CLEAN_MODE(mode)) {
         case VP_MODE_NORMAL:
+            /* do this only if the mode is really switched */
+            if (GET_CLEAN_MODE() != VP_MODE_NORMAL) {
+                history_rewind();
+            }
             if (GET_CLEAN_MODE() == VP_MODE_HINTING) {
                 /* if previous mode was hinting clear the hints */
                 hints_clear();
index b11eaa6..41b8c3d 100644 (file)
@@ -227,6 +227,8 @@ typedef struct {
     gboolean        is_inspecting;
     SearchDirection search_dir;
     char*           search_query;
+    GList*          history;
+    int             history_pointer;
 } State;
 
 /* behaviour */