Commit 27c29175 authored by Benoît Knecht's avatar Benoît Knecht Committed by Moritz Lipp
Browse files

Add a '--page' option to open at the specified page

If '--page [number]' (or '-P [number]') is given on the command-line,
the document is opened at the specified page number. Negative numbers
are allowed, and denote a page number starting from the end of the
document.

See issue 275 <http://bugs.pwmt.org/issue275

>.
Reported-by: default avatarbob <sean258@gmail.com>
parent fd07d3f5
...@@ -340,7 +340,8 @@ cb_password_dialog(GtkEntry* entry, zathura_password_dialog_info_t* dialog) ...@@ -340,7 +340,8 @@ cb_password_dialog(GtkEntry* entry, zathura_password_dialog_info_t* dialog)
} }
/* try to open document again */ /* try to open document again */
if (document_open(dialog->zathura, dialog->path, input) == false) { if (document_open(dialog->zathura, dialog->path, input,
ZATHURA_PAGE_NUMBER_UNSPECIFIED) == false) {
gdk_threads_add_idle(password_dialog, dialog); gdk_threads_add_idle(password_dialog, dialog);
} else { } else {
g_free(dialog->path); g_free(dialog->path);
......
...@@ -225,7 +225,9 @@ cmd_open(girara_session_t* session, girara_list_t* argument_list) ...@@ -225,7 +225,9 @@ cmd_open(girara_session_t* session, girara_list_t* argument_list)
document_close(zathura, false); document_close(zathura, false);
} }
document_open(zathura, girara_list_nth(argument_list, 0), (argc == 2) ? girara_list_nth(argument_list, 1) : NULL); document_open(zathura, girara_list_nth(argument_list, 0),
(argc == 2) ? girara_list_nth(argument_list, 1) : NULL,
ZATHURA_PAGE_NUMBER_UNSPECIFIED);
} else { } else {
girara_notify(session, GIRARA_ERROR, _("No arguments given.")); girara_notify(session, GIRARA_ERROR, _("No arguments given."));
return false; return false;
......
...@@ -43,6 +43,7 @@ main(int argc, char* argv[]) ...@@ -43,6 +43,7 @@ main(int argc, char* argv[])
bool forkback = false; bool forkback = false;
bool print_version = false; bool print_version = false;
bool synctex = false; bool synctex = false;
int page_number = ZATHURA_PAGE_NUMBER_UNSPECIFIED;
#if (GTK_MAJOR_VERSION == 3) #if (GTK_MAJOR_VERSION == 3)
Window embed = 0; Window embed = 0;
...@@ -57,6 +58,7 @@ main(int argc, char* argv[]) ...@@ -57,6 +58,7 @@ main(int argc, char* argv[])
{ "plugins-dir", 'p', 0, G_OPTION_ARG_STRING, &plugin_path, _("Path to the directories containing plugins"), "path" }, { "plugins-dir", 'p', 0, G_OPTION_ARG_STRING, &plugin_path, _("Path to the directories containing plugins"), "path" },
{ "fork", '\0',0, G_OPTION_ARG_NONE, &forkback, _("Fork into the background"), NULL }, { "fork", '\0',0, G_OPTION_ARG_NONE, &forkback, _("Fork into the background"), NULL },
{ "password", 'w', 0, G_OPTION_ARG_STRING, &password, _("Document password"), "password" }, { "password", 'w', 0, G_OPTION_ARG_STRING, &password, _("Document password"), "password" },
{ "page", 'P', 0, G_OPTION_ARG_INT, &page_number, _("Page number to go to"), "number" },
{ "debug", 'l', 0, G_OPTION_ARG_STRING, &loglevel, _("Log level (debug, info, warning, error)"), "level" }, { "debug", 'l', 0, G_OPTION_ARG_STRING, &loglevel, _("Log level (debug, info, warning, error)"), "level" },
{ "version", 'v', 0, G_OPTION_ARG_NONE, &print_version, _("Print version information"), NULL }, { "version", 'v', 0, G_OPTION_ARG_NONE, &print_version, _("Print version information"), NULL },
{ "synctex", 's', 0, G_OPTION_ARG_NONE, &synctex, _("Enable synctex support"), NULL }, { "synctex", 's', 0, G_OPTION_ARG_NONE, &synctex, _("Enable synctex support"), NULL },
...@@ -128,7 +130,9 @@ main(int argc, char* argv[]) ...@@ -128,7 +130,9 @@ main(int argc, char* argv[])
/* open document if passed */ /* open document if passed */
if (argc > 1) { if (argc > 1) {
document_open_idle(zathura, argv[1], password); if (page_number > 0)
--page_number;
document_open_idle(zathura, argv[1], password, page_number);
/* open additional files */ /* open additional files */
for (int i = 2; i < argc; i++) { for (int i = 2; i < argc; i++) {
......
...@@ -511,7 +511,9 @@ sc_reload(girara_session_t* session, girara_argument_t* UNUSED(argument), ...@@ -511,7 +511,9 @@ sc_reload(girara_session_t* session, girara_argument_t* UNUSED(argument),
document_close(zathura, true); document_close(zathura, true);
/* reopen document */ /* reopen document */
document_open(zathura, zathura->file_monitor.file_path, zathura->file_monitor.password); document_open(zathura, zathura->file_monitor.file_path,
zathura->file_monitor.password,
ZATHURA_PAGE_NUMBER_UNSPECIFIED);
return false; return false;
} }
......
...@@ -41,6 +41,11 @@ OPTIONS ...@@ -41,6 +41,11 @@ OPTIONS
will be used for the first one and zathura will ask for the passwords of the will be used for the first one and zathura will ask for the passwords of the
remaining files if needed. remaining files if needed.
-P [number], --page [number]
Open the document at the given page number. Pages are numbered starting with
1, and negative numbers indicate page numbers starting from the end of the
document, -1 being the last page.
--fork --fork
Fork into the background Fork into the background
......
...@@ -37,6 +37,7 @@ typedef struct zathura_document_info_s { ...@@ -37,6 +37,7 @@ typedef struct zathura_document_info_s {
zathura_t* zathura; zathura_t* zathura;
const char* path; const char* path;
const char* password; const char* password;
int page_number;
} zathura_document_info_t; } zathura_document_info_t;
typedef struct page_set_delayed_s { typedef struct page_set_delayed_s {
...@@ -487,7 +488,8 @@ document_info_open(gpointer data) ...@@ -487,7 +488,8 @@ document_info_open(gpointer data)
} }
if (file != NULL) { if (file != NULL) {
document_open(document_info->zathura, file, document_info->password); document_open(document_info->zathura, file, document_info->password,
document_info->page_number);
g_free(file); g_free(file);
} }
} }
...@@ -497,7 +499,8 @@ document_info_open(gpointer data) ...@@ -497,7 +499,8 @@ document_info_open(gpointer data)
} }
bool bool
document_open(zathura_t* zathura, const char* path, const char* password) document_open(zathura_t* zathura, const char* path, const char* password,
int page_number)
{ {
if (zathura == NULL || zathura->plugins.manager == NULL || path == NULL) { if (zathura == NULL || zathura->plugins.manager == NULL || path == NULL) {
goto error_out; goto error_out;
...@@ -552,11 +555,16 @@ document_open(zathura_t* zathura, const char* path, const char* password) ...@@ -552,11 +555,16 @@ document_open(zathura_t* zathura, const char* path, const char* password)
} }
/* check current page number */ /* check current page number */
if (file_info.current_page > number_of_pages) { /* if it wasn't specified on the command-line, get it from file_info */
if (page_number == ZATHURA_PAGE_NUMBER_UNSPECIFIED)
page_number = file_info.current_page;
if (page_number < 0)
page_number += number_of_pages;
if ((unsigned)page_number > number_of_pages) {
girara_warning("document info: '%s' has an invalid page number", file_path); girara_warning("document info: '%s' has an invalid page number", file_path);
zathura_document_set_current_page_number(document, 0); zathura_document_set_current_page_number(document, 0);
} else { } else {
zathura_document_set_current_page_number(document, file_info.current_page); zathura_document_set_current_page_number(document, page_number);
} }
/* check for valid rotation */ /* check for valid rotation */
...@@ -747,7 +755,8 @@ error_out: ...@@ -747,7 +755,8 @@ error_out:
} }
void void
document_open_idle(zathura_t* zathura, const char* path, const char* password) document_open_idle(zathura_t* zathura, const char* path, const char* password,
int page_number)
{ {
if (zathura == NULL || path == NULL) { if (zathura == NULL || path == NULL) {
return; return;
...@@ -755,9 +764,10 @@ document_open_idle(zathura_t* zathura, const char* path, const char* password) ...@@ -755,9 +764,10 @@ document_open_idle(zathura_t* zathura, const char* path, const char* password)
zathura_document_info_t* document_info = g_malloc0(sizeof(zathura_document_info_t)); zathura_document_info_t* document_info = g_malloc0(sizeof(zathura_document_info_t));
document_info->zathura = zathura; document_info->zathura = zathura;
document_info->path = path; document_info->path = path;
document_info->password = password; document_info->password = password;
document_info->page_number = page_number;
gdk_threads_add_idle(document_info_open, document_info); gdk_threads_add_idle(document_info_open, document_info);
} }
......
...@@ -21,6 +21,11 @@ enum { NEXT, PREVIOUS, LEFT, RIGHT, UP, DOWN, BOTTOM, TOP, HIDE, HIGHLIGHT, ...@@ -21,6 +21,11 @@ enum { NEXT, PREVIOUS, LEFT, RIGHT, UP, DOWN, BOTTOM, TOP, HIDE, HIGHLIGHT,
FULL_DOWN, HALF_LEFT, HALF_RIGHT, FULL_LEFT, FULL_RIGHT, NEXT_CHAR, FULL_DOWN, HALF_LEFT, HALF_RIGHT, FULL_LEFT, FULL_RIGHT, NEXT_CHAR,
PREVIOUS_CHAR, DELETE_TO_LINE_START, APPEND_FILEPATH, ROTATE_CW, ROTATE_CCW }; PREVIOUS_CHAR, DELETE_TO_LINE_START, APPEND_FILEPATH, ROTATE_CW, ROTATE_CCW };
/* unspecified page number */
enum {
ZATHURA_PAGE_NUMBER_UNSPECIFIED = INT_MIN
};
/* forward declaration for types form database.h */ /* forward declaration for types form database.h */
typedef struct _ZathuraDatabase zathura_database_t; typedef struct _ZathuraDatabase zathura_database_t;
...@@ -237,7 +242,8 @@ void zathura_set_argv(zathura_t* zathura, char** argv); ...@@ -237,7 +242,8 @@ void zathura_set_argv(zathura_t* zathura, char** argv);
* *
* @return If no error occured true, otherwise false, is returned. * @return If no error occured true, otherwise false, is returned.
*/ */
bool document_open(zathura_t* zathura, const char* path, const char* password); bool document_open(zathura_t* zathura, const char* path, const char* password,
int page_number);
/** /**
* Opens a file (idle) * Opens a file (idle)
...@@ -246,7 +252,8 @@ bool document_open(zathura_t* zathura, const char* path, const char* password); ...@@ -246,7 +252,8 @@ bool document_open(zathura_t* zathura, const char* path, const char* password);
* @param path The path to the file * @param path The path to the file
* @param password The password of the file * @param password The password of the file
*/ */
void document_open_idle(zathura_t* zathura, const char* path, const char* password); void document_open_idle(zathura_t* zathura, const char* path,
const char* password, int page_number);
/** /**
* Save a open file * Save a open file
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment