From 90796b5b5cf5739718dbd601d0902940f3de9a26 Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Sat, 24 Mar 2012 18:27:10 +0100 Subject: [PATCH] Added :offset command and some style updates --- commands.c | 37 +++++++++++++++++++++++++++++++++++-- commands.h | 9 +++++++++ config.c | 1 + database-plain.c | 8 ++++---- database-sqlite.c | 8 ++++---- database.c | 25 +++++++++++++++---------- database.h | 9 +++++---- document.c | 4 ++-- document.h | 3 ++- shortcuts.c | 5 +++++ zathura.1.rst | 2 ++ zathura.c | 6 +++--- 12 files changed, 87 insertions(+), 30 deletions(-) diff --git a/commands.c b/commands.c index 08de4cd..47b8427 100644 --- a/commands.c +++ b/commands.c @@ -1,6 +1,7 @@ /* See LICENSE file for license and copyright information */ #include +#include #include #include "commands.h" @@ -347,8 +348,7 @@ cmd_export(girara_session_t* session, girara_list_t* argument_list) return false; } - const unsigned int argc = girara_list_size(argument_list); - if (argc != 2) { + if (girara_list_size(argument_list) != 2) { girara_notify(session, GIRARA_ERROR, _("Invalid number of arguments given.")); return false; } @@ -371,3 +371,36 @@ cmd_export(girara_session_t* session, girara_list_t* argument_list) return true; } + +bool +cmd_offset(girara_session_t* session, girara_list_t* argument_list) +{ + g_return_val_if_fail(session != NULL, false); + g_return_val_if_fail(session->global.data != NULL, false); + zathura_t* zathura = session->global.data; + if (zathura->document == NULL) { + girara_notify(session, GIRARA_ERROR, _("No document opened.")); + return false; + } + + /* no argument: take current page as offset */ + unsigned int page_offset = zathura->document->current_page_number; + + /* retrieve offset from argument */ + if (girara_list_size(argument_list) == 1) { + const char* value = girara_list_nth(argument_list, 0); + if (value != NULL) { + page_offset = atoi(value); + if (page_offset == 0 && strcmp(value, "0") != 0) { + girara_notify(session, GIRARA_WARNING, _("Argument must be a number.")); + return false; + } + } + } + + if (page_offset < zathura->document->number_of_pages) { + zathura->document->page_offset = page_offset; + } + + return true; +} diff --git a/commands.h b/commands.h index bdae52c..f1f87a4 100644 --- a/commands.h +++ b/commands.h @@ -124,4 +124,13 @@ bool cmd_search(girara_session_t* session, const char* input, girara_argument_t* */ bool cmd_export(girara_session_t* session, girara_list_t* argument_list); +/** + * Set page offset + * + * @param session The used girara session + * @param argument_list List of passed arguments + * @return true if no error occured + */ +bool cmd_offset(girara_session_t* session, girara_list_t* argument_list); + #endif // COMMANDS_H diff --git a/config.c b/config.c index 37b4cd3..47a717f 100644 --- a/config.c +++ b/config.c @@ -252,6 +252,7 @@ config_load_default(zathura_t* zathura) girara_inputbar_command_add(gsession, "write", NULL, cmd_save, cc_write, _("Save document")); girara_inputbar_command_add(gsession, "write!", NULL, cmd_savef, cc_write, _("Save document (and force overwriting)")); girara_inputbar_command_add(gsession, "export", NULL, cmd_export, cc_export, _("Save attachments")); + girara_inputbar_command_add(gsession, "offset", NULL, cmd_offset, NULL, _("Set page offset")); girara_special_command_add(gsession, '/', cmd_search, true, FORWARD, NULL); girara_special_command_add(gsession, '?', cmd_search, true, BACKWARD, NULL); diff --git a/database-plain.c b/database-plain.c index a23db31..e10093f 100644 --- a/database-plain.c +++ b/database-plain.c @@ -40,9 +40,9 @@ static bool plain_remove_bookmark(zathura_database_t* db, const char* file, static girara_list_t* plain_load_bookmarks(zathura_database_t* db, const char* file); static bool plain_set_fileinfo(zathura_database_t* db, const char* file, - unsigned int page, int offset, double scale, int rotation); + unsigned int page, unsigned int offset, double scale, unsigned int rotation); static bool plain_get_fileinfo(zathura_database_t* db, const char* file, - unsigned int* page, int* offset, double* scale, int* rotation); + unsigned int* page, unsigned int* offset, double* scale, unsigned int* rotation); static void plain_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec); @@ -363,7 +363,7 @@ plain_load_bookmarks(zathura_database_t* db, const char* file) static bool plain_set_fileinfo(zathura_database_t* db, const char* file, unsigned int - page, int offset, double scale, int rotation) + page, unsigned int offset, double scale, unsigned int rotation) { zathura_plaindatabase_private_t* priv = ZATHURA_PLAINDATABASE_GET_PRIVATE(db); if (priv->history == NULL) { @@ -388,7 +388,7 @@ plain_set_fileinfo(zathura_database_t* db, const char* file, unsigned int static bool plain_get_fileinfo(zathura_database_t* db, const char* file, unsigned int* - page, int* offset, double* scale, int* rotation) + page, unsigned int* offset, double* scale, unsigned int* rotation) { zathura_plaindatabase_private_t* priv = ZATHURA_PLAINDATABASE_GET_PRIVATE(db); if (priv->history == NULL) { diff --git a/database-sqlite.c b/database-sqlite.c index a920783..eec30b3 100644 --- a/database-sqlite.c +++ b/database-sqlite.c @@ -20,9 +20,9 @@ static bool sqlite_remove_bookmark(zathura_database_t* db, const char* file, static girara_list_t* sqlite_load_bookmarks(zathura_database_t* db, const char* file); static bool sqlite_set_fileinfo(zathura_database_t* db, const char* file, - unsigned int page, int offset, double scale, int rotation); + unsigned int page, unsigned int offset, double scale, unsigned int rotation); static bool sqlite_get_fileinfo(zathura_database_t* db, const char* file, - unsigned int* page, int* offset, double* scale, int* rotation); + unsigned int* page, unsigned int* offset, double* scale, unsigned int* rotation); static void sqlite_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec); @@ -273,7 +273,7 @@ sqlite_load_bookmarks(zathura_database_t* db, const char* file) static bool sqlite_set_fileinfo(zathura_database_t* db, const char* file, unsigned int - page, int offset, double scale, int rotation) + page, unsigned int offset, double scale, unsigned int rotation) { zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db); @@ -303,7 +303,7 @@ sqlite_set_fileinfo(zathura_database_t* db, const char* file, unsigned int static bool sqlite_get_fileinfo(zathura_database_t* db, const char* file, unsigned int* - page, int* offset, double* scale, int* rotation) + page, unsigned int* offset, double* scale, unsigned int* rotation) { zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db); diff --git a/database.c b/database.c index 4812115..48c1518 100644 --- a/database.c +++ b/database.c @@ -9,7 +9,8 @@ zathura_database_default_init(ZathuraDatabaseInterface* GIRARA_UNUSED(iface)) { } -void zathura_db_free(zathura_database_t* db) +void +zathura_db_free(zathura_database_t* db) { if (db == NULL) { return; @@ -18,7 +19,8 @@ void zathura_db_free(zathura_database_t* db) g_object_unref(db); } -bool zathura_db_add_bookmark(zathura_database_t* db, const char* file, +bool +zathura_db_add_bookmark(zathura_database_t* db, const char* file, zathura_bookmark_t* bookmark) { g_return_val_if_fail(ZATHURA_IS_DATABASE(db) && file != NULL && bookmark != NULL, false); @@ -26,24 +28,26 @@ bool zathura_db_add_bookmark(zathura_database_t* db, const char* file, return ZATHURA_DATABASE_GET_INTERFACE(db)->add_bookmark(db, file, bookmark); } -bool zathura_db_remove_bookmark(zathura_database_t* db, const char* file, const - char* id) +bool +zathura_db_remove_bookmark(zathura_database_t* db, const char* file, const char* + id) { g_return_val_if_fail(ZATHURA_IS_DATABASE(db) && file != NULL && id != NULL, false); return ZATHURA_DATABASE_GET_INTERFACE(db)->remove_bookmark(db, file, id); } -girara_list_t* zathura_db_load_bookmarks(zathura_database_t* db, const char* - file) +girara_list_t* +zathura_db_load_bookmarks(zathura_database_t* db, const char* file) { g_return_val_if_fail(ZATHURA_IS_DATABASE(db) && file != NULL, NULL); return ZATHURA_DATABASE_GET_INTERFACE(db)->load_bookmarks(db, file); } -bool zathura_db_set_fileinfo(zathura_database_t* db, const char* file, unsigned - int page, int offset, double scale, int rotation) +bool +zathura_db_set_fileinfo(zathura_database_t* db, const char* file, unsigned int + page, unsigned int offset, double scale, unsigned int rotation) { g_return_val_if_fail(ZATHURA_IS_DATABASE(db) && file != NULL, false); @@ -51,8 +55,9 @@ bool zathura_db_set_fileinfo(zathura_database_t* db, const char* file, unsigned scale, rotation); } -bool zathura_db_get_fileinfo(zathura_database_t* db, const char* file, unsigned - int* page, int* offset, double* scale, int* rotation) +bool +zathura_db_get_fileinfo(zathura_database_t* db, const char* file, unsigned int* + page, unsigned int* offset, double* scale, unsigned int* rotation) { g_return_val_if_fail(ZATHURA_IS_DATABASE(db) && file != NULL && page != NULL && offset != NULL && scale != NULL && rotation != NULL, false); diff --git a/database.h b/database.h index 6039f5e..f1c2fac 100644 --- a/database.h +++ b/database.h @@ -27,14 +27,15 @@ struct _ZathuraDatabaseInterface /* interface methords */ bool (*add_bookmark)(ZathuraDatabase* db, const char* file, zathura_bookmark_t* bookmark); + bool (*remove_bookmark)(ZathuraDatabase* db, const char* file, const char* id); girara_list_t* (*load_bookmarks)(ZathuraDatabase* db, const char* file); bool (*set_fileinfo)(ZathuraDatabase* db, const char* file, unsigned - int page, int offset, double scale, int rotation); + int page, unsigned int offset, double scale, unsigned int rotation); bool (*get_fileinfo)(ZathuraDatabase* db, const char* file, unsigned - int* page, int* offset, double* scale, int* rotation); + int* page, unsigned int* offset, double* scale, unsigned int* rotation); }; GType zathura_database_get_type(void); @@ -90,7 +91,7 @@ girara_list_t* zathura_db_load_bookmarks(zathura_database_t* db, const char* * @return true on success, false otherwise. */ bool zathura_db_set_fileinfo(zathura_database_t* db, const char* file, unsigned - int page, int offset, double scale, int rotation); + int page, unsigned int offset, double scale, unsigned int rotation); /* Get file info (last site, ...) from the database. * @@ -103,6 +104,6 @@ bool zathura_db_set_fileinfo(zathura_database_t* db, const char* file, unsigned * @return true on success, false otherwise. */ bool zathura_db_get_fileinfo(zathura_database_t* db, const char* file, unsigned - int* page, int* offset, double* scale, int* rotation); + int* page, unsigned int* offset, double* scale, unsigned int* rotation); #endif // DATABASE_H diff --git a/document.c b/document.c index e4b2e47..6248b50 100644 --- a/document.c +++ b/document.c @@ -345,9 +345,9 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password } /* read history file */ - int offset = 0; zathura_db_get_fileinfo(zathura->database, document->file_path, - &document->current_page_number, &offset, &document->scale, &document->rotate); + &document->current_page_number, &document->page_offset, &document->scale, + &document->rotate); /* check for valid scale value */ if (document->scale <= FLT_EPSILON) { diff --git a/document.h b/document.h index 398191c..05c2280 100644 --- a/document.h +++ b/document.h @@ -257,10 +257,11 @@ struct zathura_document_s unsigned int current_page_number; /**< Current page number */ unsigned int number_of_pages; /**< Number of pages */ double scale; /**< Scale value */ - int rotate; /**< Rotation */ + unsigned int rotate; /**< Rotation */ void* data; /**< Custom data */ zathura_t* zathura; /** Zathura object */ int adjust_mode; /**< Adjust mode (best-fit, width) */ + unsigned int page_offset; /**< Page offset */ struct { diff --git a/shortcuts.c b/shortcuts.c index b491265..42a8244 100644 --- a/shortcuts.c +++ b/shortcuts.c @@ -246,6 +246,11 @@ sc_goto(girara_session_t* session, girara_argument_t* argument, girara_event_t* g_return_val_if_fail(zathura->document != NULL, false); if (t != 0) { + /* add offset */ + if (zathura->document->page_offset > 0) { + t += zathura->document->page_offset; + } + page_set(zathura, t - 1); } else if (argument->n == TOP) { page_set(zathura, 0); diff --git a/zathura.1.rst b/zathura.1.rst index d108d8e..e558480 100644 --- a/zathura.1.rst +++ b/zathura.1.rst @@ -139,6 +139,8 @@ help Show help page open, o Open a document +offset + Set page offset print Print document write, write! diff --git a/zathura.c b/zathura.c index 727f526..031888b 100644 --- a/zathura.c +++ b/zathura.c @@ -588,9 +588,9 @@ document_close(zathura_t* zathura, bool keep_monitor) } /* store last seen page */ - zathura_db_set_fileinfo(zathura->database, zathura->document->file_path, zathura->document->current_page_number, - /* zathura->document->offset TODO */ 0, zathura->document->scale, - zathura->document->rotate); + zathura_db_set_fileinfo(zathura->database, zathura->document->file_path, + zathura->document->current_page_number, zathura->document->page_offset, + zathura->document->scale, zathura->document->rotate); render_free(zathura->sync.render_thread); zathura->sync.render_thread = NULL;