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: bob <sean258@gmail.com>
This commit is contained in:
Benoît Knecht 2013-03-17 10:56:43 +01:00 committed by Moritz Lipp
parent fd07d3f5cf
commit 27c291758e
7 changed files with 45 additions and 14 deletions

View file

@ -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);

View file

@ -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;

6
main.c
View file

@ -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[])
{ "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[])
/* 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++) {

View file

@ -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;
} }

View file

@ -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

View file

@ -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)
} }
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)
} }
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)
} }
/* 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:
} }
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;
@ -758,6 +767,7 @@ document_open_idle(zathura_t* zathura, const char* path, const char* password)
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);
} }

View file

@ -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);
* *
* @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);
* @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