Save last closed page into file (#9).
authorDaniel Carl <danielcarl@gmx.de>
Sun, 6 Jan 2013 19:13:46 +0000 (20:13 +0100)
committerDaniel Carl <danielcarl@gmx.de>
Sun, 6 Jan 2013 19:13:46 +0000 (20:13 +0100)
Added command to reopen the last closed page 'open-closed' and
'tabopen-closed'.

doc/vimp.1.txt
src/command.c
src/command.h
src/config.h
src/main.c
src/main.h

index c6c816d..5212ce5 100644 (file)
@@ -66,6 +66,12 @@ Opens the configured 'home-page' into current window.
 .TP
 .B tabopen-home
 Opens the home page into a new window.
+.TP
+.B open-closed
+Open the last closed page into current window.
+.TP
+.B tabopen-closed
+Open the last closed page into new window.
 .SS Input
 Switches the browser into Command Mode and prefill the inputbox on th bottom of
 the browser with various prefilled content.
index 10801a6..58abc48 100644 (file)
 
 static CommandInfo cmd_list[] = {
     /* command              function             arg                                                                                 mode */
-    {"open",                command_open,        {VP_TARGET_CURRENT, ""},                                                            VP_MODE_NORMAL},
-    {"tabopen",             command_open,        {VP_TARGET_NEW, ""},                                                                VP_MODE_NORMAL},
-    {"open-home",           command_open_home,   {VP_TARGET_CURRENT, ""},                                                            VP_MODE_NORMAL},
-    {"tabopen-home",        command_open_home,   {VP_TARGET_NEW, ""},                                                                VP_MODE_NORMAL},
+    {"open",                command_open,        {VP_TARGET_CURRENT},                                                                VP_MODE_NORMAL},
+    {"tabopen",             command_open,        {VP_TARGET_NEW},                                                                    VP_MODE_NORMAL},
+    {"open-home",           command_open_home,   {VP_TARGET_CURRENT},                                                                VP_MODE_NORMAL},
+    {"tabopen-home",        command_open_home,   {VP_TARGET_NEW},                                                                    VP_MODE_NORMAL},
+    {"open-closed",         command_open_closed, {VP_TARGET_CURRENT},                                                                VP_MODE_NORMAL},
+    {"tabopen-closed",      command_open_closed, {VP_TARGET_NEW},                                                                    VP_MODE_NORMAL},
     {"input",               command_input,       {0, ":"},                                                                           VP_MODE_COMMAND},
     {"inputopen",           command_input,       {0, ":open "},                                                                      VP_MODE_COMMAND},
     {"inputtabopen",        command_input,       {0, ":tabopen "},                                                                   VP_MODE_COMMAND},
@@ -129,11 +131,22 @@ gboolean command_open(const Arg* arg)
 }
 
 gboolean command_open_home(const Arg* arg)
+{
+    Arg a = {arg->i, vp.config.home_page};
+    return vp_load_uri(&a);
+}
+
+/**
+ * Reopens the last closed page.
+ */
+gboolean command_open_closed(const Arg* arg)
 {
     gboolean result;
 
-    Arg a = {.i = arg->i, .s = vp.config.home_page};
+    Arg a = {arg->i};
+    g_file_get_contents(vp.files[FILES_CLOSED], &a.s, NULL, NULL);
     result = vp_load_uri(&a);
+    g_free(a.s);
 
     return result;
 }
index 62fc6d7..8c5a235 100644 (file)
@@ -44,6 +44,7 @@ gboolean command_run(const gchar* name, const gchar* param);
 
 gboolean command_open(const Arg* arg);
 gboolean command_open_home(const Arg* arg);
+gboolean command_open_closed(const Arg* arg);
 gboolean command_input(const Arg* arg);
 gboolean command_close(const Arg* arg);
 gboolean command_view_source(const Arg* arg);
index 045ed54..dd30068 100644 (file)
@@ -36,6 +36,8 @@ const struct {
     {"nmap T=inputtabopencurrent"},
     {"nmap gh=open-home"},
     {"nmap gH=tabopen-home"},
+    {"nmap u=open-closed"},
+    {"nmap U=tabopen-closed"},
     {"nmap d=quit"},
     {"nmap <ctrl-o>=back"},
     {"nmap <ctrl-i>=forward"},
index f37c337..c345f8a 100644 (file)
@@ -334,6 +334,12 @@ static const gchar* vp_get_cookies(SoupURI *uri)
 
 void vp_clean_up(void)
 {
+    const gchar* uri = CURRENT_URL();
+    /* write last URL into file for recreation */
+    if (uri) {
+        g_file_set_contents(vp.files[FILES_CLOSED], uri, -1, NULL);
+    }
+
     if (vp.behave.commands) {
         g_hash_table_destroy(vp.behave.commands);
         vp.behave.commands = NULL;
@@ -662,6 +668,9 @@ static void vp_init_files(void)
     vp.files[FILES_COOKIE] = g_build_filename(path, "cookies", NULL);
     util_create_file_if_not_exists(vp.files[FILES_COOKIE]);
 
+    vp.files[FILES_CLOSED] = g_build_filename(path, "closed", NULL);
+    util_create_file_if_not_exists(vp.files[FILES_CLOSED]);
+
     g_free(path);
 }
 
index d36cb55..031f71c 100644 (file)
@@ -151,6 +151,7 @@ enum {
     FILES_FIRST = 0,
     FILES_CONFIG = 0,
     FILES_COOKIE,
+    FILES_CLOSED,
     FILES_LAST
 };