Added command queue-unshift.
authorDaniel Carl <danielcarl@gmx.de>
Mon, 5 Aug 2013 17:35:12 +0000 (19:35 +0200)
committerDaniel Carl <danielcarl@gmx.de>
Mon, 5 Aug 2013 17:59:45 +0000 (19:59 +0200)
New command allows to push new uri to the beginning of read it later list.

doc/vimb.1
src/bookmark.c
src/bookmark.h
src/command.c
src/command.h
src/util.c
src/util.h

index 7ddb19e..faf1595 100644 (file)
@@ -335,8 +335,10 @@ list). This list is shared between the single instances of PROJECT. Only
 available if PROJECT has been compiled with QUEUE feature.
 .TP
 .BI "queue-push [" URI ]
-Push \fIURI\fP or if not given
-current URI into the queue.
+Push \fIURI\fP or if not given current URI to the end of the queue.
+.TP
+.BI "queue-unshift [" URI ]
+Push \fIURI\fP or if not given current URI to the beginning of the queue.
 .TP
 .B queue-pop
 Open the oldest queue entry in current browser window and remove it from the
index 0edac3e..2fecaa7 100644 (file)
@@ -160,6 +160,16 @@ gboolean bookmark_queue_push(const char *uri)
     return util_file_append(vb.files[FILES_QUEUE], "%s\n", uri);
 }
 
+/**
+ * Push a uri to the bginning of the queue.
+ *
+ * @uri: URI to put into the queue
+ */
+gboolean bookmark_queue_unshift(const char *uri)
+{
+    return util_file_prepend(vb.files[FILES_QUEUE], "%s\n", uri);
+}
+
 /**
  * Retrieves the oldest entry from queue.
  *
index c94be30..a346905 100644 (file)
@@ -25,6 +25,7 @@ gboolean bookmark_remove(const char *uri);
 gboolean bookmark_fill_completion(GtkListStore *store, const char *input);
 #ifdef FEATURE_QUEUE
 gboolean bookmark_queue_push(const char *uri);
+gboolean bookmark_queue_unshift(const char *uri);
 char *bookmark_queue_pop(int *item_count);
 gboolean bookmark_queue_clear(void);
 #endif
index 59460f2..7b82409 100644 (file)
@@ -126,6 +126,7 @@ static CommandInfo cmd_list[] = {
     {"shellcmd",                  NULL,    command_shellcmd,             {0}},
 #ifdef FEATURE_QUEUE
     {"queue-push",                NULL,    command_queue,                {COMMAND_QUEUE_PUSH}},
+    {"queue-unshift",             NULL,    command_queue,                {COMMAND_QUEUE_UNSHIFT}},
     {"queue-pop",                 NULL,    command_queue,                {COMMAND_QUEUE_POP}},
     {"queue-clear",               NULL,    command_queue,                {COMMAND_QUEUE_CLEAR}},
 #endif
@@ -916,6 +917,13 @@ gboolean command_queue(const Arg *arg)
             }
             break;
 
+        case COMMAND_QUEUE_UNSHIFT:
+            res = bookmark_queue_unshift(arg->s ? arg->s : GET_URI());
+            if (res) {
+                vb_echo(VB_MSG_NORMAL, false, "Pushed to queue");
+            }
+            break;
+
         case COMMAND_QUEUE_POP:
             if ((uri = bookmark_queue_pop(&count))) {
                 res = vb_load_uri(&(Arg){VB_TARGET_CURRENT, uri});
index 3e7672a..50e5ab0 100644 (file)
@@ -47,6 +47,7 @@ enum {
 #ifdef FEATURE_QUEUE
 enum {
     COMMAND_QUEUE_PUSH,
+    COMMAND_QUEUE_UNSHIFT,
     COMMAND_QUEUE_POP,
     COMMAND_QUEUE_CLEAR
 };
index 4d2de65..b3b79d5 100644 (file)
@@ -180,6 +180,41 @@ gboolean util_file_append(const char *file, const char *format, ...)
     return false;
 }
 
+/**
+ * Prepend new data to file.
+ *
+ * @file:   File to prepend the data
+ * @format: Format string used to process va_list
+ */
+gboolean util_file_prepend(const char *file, const char *format, ...)
+{
+    gboolean res = false;
+    va_list args;
+    char *content;
+    FILE *f;
+
+    content = util_get_file_contents(file, NULL);
+    if ((f = fopen(file, "w"))) {
+        file_lock_set(fileno(f), F_WRLCK);
+
+        va_start(args, format);
+        /* write new content to the file */
+        vfprintf(f, format, args);
+        va_end(args);
+
+        /* append previous file content */
+        fputs(content, f);
+
+        file_lock_set(fileno(f), F_UNLCK);
+        fclose(f);
+
+        res = true;
+    }
+    g_free(content);
+
+    return res;
+}
+
 char *util_strcasestr(const char *haystack, const char *needle)
 {
     unsigned char c1, c2;
index 7c9d65b..b1eaa53 100644 (file)
@@ -35,6 +35,7 @@ char** util_get_lines(const char* filename);
 GList *util_file_to_unique_list(const char *filename, Util_Content_Func func,
     GCompareFunc unique_func, GDestroyNotify free_func, unsigned int max_items);
 gboolean util_file_append(const char *file, const char *format, ...);
+gboolean util_file_prepend(const char *file, const char *format, ...);
 char* util_strcasestr(const char* haystack, const char* needle);
 char *util_str_replace(const char* search, const char* replace, const char* string);
 gboolean util_create_tmp_file(const char *content, char **file);