From: Daniel Carl Date: Mon, 5 Aug 2013 17:35:12 +0000 (+0200) Subject: Added command queue-unshift. X-Git-Url: https://git.owens.tech/dummy.html/dummy.html/git?a=commitdiff_plain;h=19de6173d00a69d49c9b3a7b9818b7b0383ad5b5;p=vimb.git Added command queue-unshift. New command allows to push new uri to the beginning of read it later list. --- diff --git a/doc/vimb.1 b/doc/vimb.1 index 7ddb19e..faf1595 100644 --- a/doc/vimb.1 +++ b/doc/vimb.1 @@ -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 diff --git a/src/bookmark.c b/src/bookmark.c index 0edac3e..2fecaa7 100644 --- a/src/bookmark.c +++ b/src/bookmark.c @@ -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. * diff --git a/src/bookmark.h b/src/bookmark.h index c94be30..a346905 100644 --- a/src/bookmark.h +++ b/src/bookmark.h @@ -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 diff --git a/src/command.c b/src/command.c index 59460f2..7b82409 100644 --- a/src/command.c +++ b/src/command.c @@ -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}); diff --git a/src/command.h b/src/command.h index 3e7672a..50e5ab0 100644 --- a/src/command.h +++ b/src/command.h @@ -47,6 +47,7 @@ enum { #ifdef FEATURE_QUEUE enum { COMMAND_QUEUE_PUSH, + COMMAND_QUEUE_UNSHIFT, COMMAND_QUEUE_POP, COMMAND_QUEUE_CLEAR }; diff --git a/src/util.c b/src/util.c index 4d2de65..b3b79d5 100644 --- a/src/util.c +++ b/src/util.c @@ -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; diff --git a/src/util.h b/src/util.h index 7c9d65b..b1eaa53 100644 --- a/src/util.h +++ b/src/util.h @@ -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);