From: Daniel Carl Date: Tue, 30 Jul 2013 19:33:43 +0000 (+0200) Subject: Added queue commands :push and :pop. X-Git-Url: https://git.owens.tech///git?a=commitdiff_plain;h=af1b17aaa5a3e49c8fcd5ebcb9264f4fee9570a6;p=vimb.git Added queue commands :push and :pop. --- diff --git a/doc/vimb.1 b/doc/vimb.1 index 27e0f04..4553632 100644 --- a/doc/vimb.1 +++ b/doc/vimb.1 @@ -325,6 +325,17 @@ Save the current opened uri with \fITAGS\fP to the bookmark file. Removes all bookmarks for given \fIURI\fP or if not given the current opened page. +.SS Queue +The queue allows to mark URLs for later reding (something like a read it later +list). This list is shared between the single instances of PROJECT. +.TP +.BI "push [" URI ] +Push \fIURI\fP or if not given current URI into the queue. +.TP +.B pop +Open the oldest queue entry in current browser window and remove it from the +queue. + .SS Misc .TP .B next, n, prev, p @@ -653,6 +664,10 @@ box. .RS Holds the bookmarks saved with command `bookmark-add'. .RE +.I $XDG_CONFIG_HOME/PROJECT/queue +.RS +Holds the read it later queue filled by `hint-push' or `push'. +.RE .I $XDG_CONFIG_HOME/PROJECT/scripts.js .RS This file can be used to run user scripts, that are injected into every paged diff --git a/src/bookmark.c b/src/bookmark.c index e3aa925..337ad2f 100644 --- a/src/bookmark.c +++ b/src/bookmark.c @@ -43,25 +43,14 @@ static void free_bookmark(Bookmark *bm); */ gboolean bookmark_add(const char *uri, const char *title, const char *tags) { - FILE *f; - - if ((f = fopen(vb.files[FILES_BOOKMARK], "a+"))) { - file_lock_set(fileno(f), F_WRLCK); - - if (tags) { - fprintf(f, "%s\t%s\t%s\n", uri, title ? title : "", tags); - } else if (title) { - fprintf(f, "%s\t%s\n", uri, title); - } else { - fprintf(f, "%s\n", uri); - } - - file_lock_set(fileno(f), F_UNLCK); - fclose(f); - - return true; + const char *file = vb.files[FILES_BOOKMARK]; + if (tags) { + return util_file_append(file, "%s\t%s\t%s\n", uri, title ? title : "", tags); + } + if (title) { + return util_file_append(file, "%s\t%s\n", uri, title); } - return false; + return util_file_append(file, "%s\n", uri); } gboolean bookmark_remove(const char *uri) @@ -159,6 +148,43 @@ gboolean bookmark_fill_completion(GtkListStore *store, const char *input) return found; } +/** + * Push a uri to the end of the queue. + * + * @uri: URI to put into the queue + */ +gboolean bookmark_queue_push(const char *uri) +{ + return util_file_append(vb.files[FILES_QUEUE], "%s\n", uri); +} + +/** + * Retrieves the oldest entry from queue. + * Retruned uri must be freed with g_free. + */ +char *bookmark_queue_pop(void) +{ + int len, i; + char **lines, *uri = NULL; + + lines = util_get_lines(vb.files[FILES_QUEUE]); + if (lines && (len = g_strv_length(lines))) { + GString *new = g_string_new(NULL); + + uri = g_strdup(lines[0]); + /* don't process last empty line */ + len -= 1; + /* skip the first list that should be removed */ + for (i = 1; i < len; i++) { + g_string_append_printf(new, "%s\n", lines[i]); + } + g_strfreev(lines); + g_file_set_contents(vb.files[FILES_QUEUE], new->str, -1, NULL); + g_string_free(new, true); + } + return uri; +} + static GList *load(const char *file) { return util_file_to_unique_list( diff --git a/src/bookmark.h b/src/bookmark.h index 1a2e1d1..c1986d4 100644 --- a/src/bookmark.h +++ b/src/bookmark.h @@ -23,5 +23,7 @@ gboolean bookmark_add(const char *uri, const char *title, const char *tags); gboolean bookmark_remove(const char *uri); gboolean bookmark_fill_completion(GtkListStore *store, const char *input); +gboolean bookmark_queue_push(const char *uri); +char *bookmark_queue_pop(void); #endif /* end of include guard: _BOOKMARK_H */ diff --git a/src/command.c b/src/command.c index 24feb1d..6f33e19 100644 --- a/src/command.c +++ b/src/command.c @@ -121,6 +121,8 @@ static CommandInfo cmd_list[] = { {"descent!", NULL, command_descent, {1}}, {"save", NULL, command_save, {COMMAND_SAVE_CURRENT}}, {"shellcmd", NULL, command_shellcmd, {0}}, + {"push", NULL, command_queue, {COMMAND_QUEUE_PUSH}}, + {"pop", NULL, command_queue, {COMMAND_QUEUE_POP}}, }; static void editor_resume(GPid pid, int status, OpenEditorData *data); @@ -731,12 +733,12 @@ gboolean command_bookmark(const Arg *arg) if (!arg->i) { if (bookmark_remove(arg->s ? arg->s : GET_URI())) { - vb_echo_force(VB_MSG_NORMAL, false, "Bookmark removed"); + vb_echo_force(VB_MSG_NORMAL, false, " Bookmark removed"); return true; } } else if (bookmark_add(GET_URI(), webkit_web_view_get_title(vb.gui.webview), arg->s)) { - vb_echo_force(VB_MSG_NORMAL, false, "Bookmark added"); + vb_echo_force(VB_MSG_NORMAL, false, " Bookmark added"); return true; } @@ -886,6 +888,31 @@ gboolean command_shellcmd(const Arg *arg) return false; } +gboolean command_queue(const Arg *arg) +{ + gboolean res = false; + char *uri; + + vb_set_mode(VB_MODE_NORMAL, false); + + if (arg->i == COMMAND_QUEUE_PUSH) { + res = bookmark_queue_push(arg->s ? arg->s : GET_URI()); + if (res) { + vb_echo(VB_MSG_NORMAL, false, " Pushed to queue"); + } + return res; + } + + /* pop last added url from queue and open it */ + if ((uri = bookmark_queue_pop())) { + res = vb_load_uri(&(Arg){VB_TARGET_CURRENT, uri}); + g_free(uri); + } else { + vb_echo(VB_MSG_NORMAL, false, " Queue is empty"); + } + return res; +} + gboolean command_editor(const Arg *arg) { char *file_path = NULL; diff --git a/src/command.h b/src/command.h index 32c8f0b..b12dccb 100644 --- a/src/command.h +++ b/src/command.h @@ -44,6 +44,11 @@ enum { COMMAND_SAVE_URI }; +enum { + COMMAND_QUEUE_PUSH, + COMMAND_QUEUE_POP +}; + typedef gboolean (*Command)(const Arg *arg); void command_init(void); @@ -81,5 +86,6 @@ gboolean command_nextprev(const Arg *arg); gboolean command_descent(const Arg *arg); gboolean command_save(const Arg *arg); gboolean command_shellcmd(const Arg *arg); +gboolean command_queue(const Arg *arg); #endif /* end of include guard: _COMMAND_H */ diff --git a/src/history.c b/src/history.c index aedf71b..e7335e3 100644 --- a/src/history.c +++ b/src/history.c @@ -75,20 +75,12 @@ void history_cleanup(void) */ void history_add(HistoryType type, const char *value, const char *additional) { - FILE *f; const char *file = get_file_by_type(type); - if ((f = fopen(file, "a+"))) { - file_lock_set(fileno(f), F_WRLCK); - if (additional) { - fprintf(f, "%s\t%s\n", value, additional); - } else { - fprintf(f, "%s\n", value); - } - - file_lock_set(fileno(f), F_UNLCK); - fclose(f); + if (additional) { + util_file_append(file, "%s\t%s\n", value, additional); } + util_file_append(file, "%s\n", value); } /** diff --git a/src/main.c b/src/main.c index b6cd352..c95256e 100644 --- a/src/main.c +++ b/src/main.c @@ -863,6 +863,9 @@ static void init_files(void) vb.files[FILES_BOOKMARK] = g_build_filename(path, "bookmark", NULL); util_create_file_if_not_exists(vb.files[FILES_BOOKMARK]); + vb.files[FILES_QUEUE] = g_build_filename(path, "queue", NULL); + util_create_file_if_not_exists(vb.files[FILES_QUEUE]); + vb.files[FILES_SCRIPT] = g_build_filename(path, "scripts.js", NULL); vb.files[FILES_USER_STYLE] = g_build_filename(path, "style.css", NULL); diff --git a/src/main.h b/src/main.h index 15fc24a..060aa5f 100644 --- a/src/main.h +++ b/src/main.h @@ -210,6 +210,7 @@ typedef enum { FILES_COMMAND, FILES_SEARCH, FILES_BOOKMARK, + FILES_QUEUE, FILES_USER_STYLE, FILES_LAST } VbFile; diff --git a/src/util.c b/src/util.c index 8b536a1..4d2de65 100644 --- a/src/util.c +++ b/src/util.c @@ -154,6 +154,32 @@ GList *util_file_to_unique_list(const char *filename, Util_Content_Func func, return gl; } +/** + * Append new data to file. + * + * @file: File to appen the data + * @format: Format string used to process va_list + */ +gboolean util_file_append(const char *file, const char *format, ...) +{ + va_list args; + FILE *f; + + if ((f = fopen(file, "a+"))) { + file_lock_set(fileno(f), F_WRLCK); + + va_start(args, format); + vfprintf(f, format, args); + va_end(args); + + file_lock_set(fileno(f), F_UNLCK); + fclose(f); + + return true; + } + return false; +} + char *util_strcasestr(const char *haystack, const char *needle) { unsigned char c1, c2; diff --git a/src/util.h b/src/util.h index 388c064..7c9d65b 100644 --- a/src/util.h +++ b/src/util.h @@ -34,6 +34,7 @@ char* util_get_file_contents(const char* filename, gsize* length); 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, ...); 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);