This patch adds support for the "hi"/"history" setting for changing
the command history length.

Morgan
From 1d60d03935602ba5ac0d7f497fb3cbdf5fb87708 Mon Sep 17 00:00:00 2001
From: Morgan Howe <[email protected]>
Date: Fri, 26 Dec 2014 22:38:06 +0800
Subject: [PATCH 1/2] Add "hi"/"history" setting for command history length.

---
 config.h    |  2 ++
 main.c      | 10 +++++++++-
 utilities.c | 30 ++++++++++++++++++++++++++----
 utilities.h |  4 +---
 4 files changed, 38 insertions(+), 8 deletions(-)

diff --git a/config.h b/config.h
index c737a74..5f44fa3 100644
--- a/config.h
+++ b/config.h
@@ -246,4 +246,6 @@ static Setting browsersettings[] = {
     { "strictssl",       NULL,               "",                            FALSE,          TRUE,            FALSE,          FALSE  },
     { "cabundle",        ca_bundle,          "",                            FALSE,          FALSE,           FALSE,          FALSE  },
     { "tempdir",         temp_dir,           "",                            FALSE,          FALSE,           FALSE,          FALSE  },
+    { "history",         NULL,               "",                            TRUE,           FALSE,           FALSE,          FALSE  },
+    { "hi",              NULL,               "",                            TRUE,           FALSE,           FALSE,          FALSE  },
 };
diff --git a/main.c b/main.c
index eac7f8e..795876b 100644
--- a/main.c
+++ b/main.c
@@ -2285,6 +2285,7 @@ static gboolean
 process_set_line(char *line) {
     char              *c;
     int               listlen, i;
+    int               intval;
     gboolean          boolval;
     WebKitWebSettings *settings;
 
@@ -2327,7 +2328,9 @@ process_set_line(char *line) {
                 if (!parse_colour(my_pair.value)) {
                     return FALSE;
                 }
-            }
+            } else if (browsersettings[i].intval) {
+		intval = atoi(my_pair.value);
+	    }
             if (browsersettings[i].var != NULL) {
                 strncpy(browsersettings[i].var, my_pair.value, MAX_SETTING_SIZE);
                 if (strlen(my_pair.value) > MAX_SETTING_SIZE - 1) {
@@ -2417,6 +2420,11 @@ process_set_line(char *line) {
             if (strlen(my_pair.what) == 10 && strncmp("windowsize", my_pair.what, 10) == 0) {
                 set_default_winsize(my_pair.value);
             }
+	    if ((strlen(my_pair.what) == 2 && strncmp("hi", my_pair.what, 2) == 0) ||
+                (strlen(my_pair.what) == 7 && strncmp("history", my_pair.what, 7) == 0)) {
+		if (intval >= 0)
+                    set_command_history_len(intval);
+	    }
 
             /* reload page? */
             if (browsersettings[i].reload)
diff --git a/utilities.c b/utilities.c
index 77bb7ae..a72cfa6 100644
--- a/utilities.c
+++ b/utilities.c
@@ -18,9 +18,24 @@ extern Command commands[COMMANDSIZE];
 extern Key keys[];
 extern gboolean complete_case_sensitive;
 static GList *dynamic_searchengines = NULL, *dynamic_uri_handlers = NULL;
+static int command_histsize = 50;
 
 void add_modkeys(char key);
 
+static void clear_history(int maxlen)
+{
+    State *s = &client.state;
+    GList *l = s->commandhistory;
+    int len = g_list_length(s->commandhistory);
+
+    while (l != NULL && len > maxlen) {
+	GList *next = l->next;
+        s->commandhistory = g_list_delete_link(s->commandhistory, l);
+	l = next;
+	len--;
+    }
+}
+
 void save_command_history(char *line)
 {
     State *s = &client.state;
@@ -31,11 +46,18 @@ void save_command_history(char *line)
 	if (!strlen(c))
 		return;
 
-    if (COMMANDHISTSIZE <= g_list_length(s->commandhistory)) {
-        /* if list is too long - remove items from beginning */
-        s->commandhistory = g_list_delete_link(s->commandhistory, g_list_first(s->commandhistory));
+    if (command_histsize > 0) {
+        if (command_histsize <= g_list_length(s->commandhistory)) {
+	    clear_history(command_histsize - 1);
+        }
+        s->commandhistory = g_list_append(s->commandhistory, g_strdup(c));
     }
-    s->commandhistory = g_list_append(s->commandhistory, g_strdup(c));
+}
+
+void set_command_history_len(int len)
+{
+    command_histsize = len;
+    clear_history(command_histsize);
 }
 
 gboolean
diff --git a/utilities.h b/utilities.h
index 4085988..ccfdb45 100644
--- a/utilities.h
+++ b/utilities.h
@@ -10,11 +10,9 @@
 /* config file */
 #define             RCFILE                      "%s/vimprobable/vimprobablerc", client.config.config_base
 
-/* max entries in command history */
-#define COMMANDHISTSIZE 50
-
 enum ConfigFileError read_rcfile(const char *config);
 void save_command_history(char *line);
+void set_command_history_len(int len);
 gboolean process_save_qmark(const char *bm, WebKitWebView *webview);
 void make_keyslist(void);
 gboolean parse_colour(char *color);
-- 
1.9.1

------------------------------------------------------------------------------
Dive into the World of Parallel Programming! The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net
_______________________________________________
Vimprobable-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/vimprobable-users

Reply via email to