Auto start instance if --synctex-forward is passed but no instance exists

This commit is contained in:
Sebastian Ramacher 2015-10-07 22:46:47 +02:00
parent 264aaed0bd
commit 1652c2aab3
5 changed files with 70 additions and 22 deletions

View file

@ -445,13 +445,13 @@ call_synctex_view(GDBusConnection* connection, const char* filename,
return true; return true;
} }
static bool static int
iterate_instances_call_synctex_view(const char* filename, iterate_instances_call_synctex_view(const char* filename,
const char* input_file, unsigned int line, const char* input_file, unsigned int line,
unsigned int column, pid_t hint) unsigned int column, pid_t hint)
{ {
if (filename == NULL) { if (filename == NULL) {
return false; return -1;
} }
GError* error = NULL; GError* error = NULL;
@ -460,7 +460,7 @@ iterate_instances_call_synctex_view(const char* filename,
if (connection == NULL) { if (connection == NULL) {
girara_error("Could not connect to session bus: %s", error->message); girara_error("Could not connect to session bus: %s", error->message);
g_error_free(error); g_error_free(error);
return false; return -1;
} }
if (hint != -1) { if (hint != -1) {
@ -468,7 +468,7 @@ iterate_instances_call_synctex_view(const char* filename,
const bool ret = call_synctex_view(connection, filename, well_known_name, const bool ret = call_synctex_view(connection, filename, well_known_name,
input_file, line, column); input_file, line, column);
g_free(well_known_name); g_free(well_known_name);
return ret; return ret ? 1 : 0;
} }
GVariant* vnames = g_dbus_connection_call_sync( GVariant* vnames = g_dbus_connection_call_sync(
@ -479,7 +479,7 @@ iterate_instances_call_synctex_view(const char* filename,
girara_error("Could not list available names: %s", error->message); girara_error("Could not list available names: %s", error->message);
g_error_free(error); g_error_free(error);
g_object_unref(connection); g_object_unref(connection);
return false; return -1;
} }
GVariantIter* iter = NULL; GVariantIter* iter = NULL;
@ -487,25 +487,22 @@ iterate_instances_call_synctex_view(const char* filename,
gchar* name = NULL; gchar* name = NULL;
bool found_one = false; bool found_one = false;
while (g_variant_iter_loop(iter, "s", &name) == TRUE) { while (found_one == false && g_variant_iter_loop(iter, "s", &name) == TRUE) {
if (g_str_has_prefix(name, "org.pwmt.zathura.PID") == FALSE) { if (g_str_has_prefix(name, "org.pwmt.zathura.PID") == FALSE) {
continue; continue;
} }
girara_debug("Found name: %s", name); girara_debug("Found name: %s", name);
if (call_synctex_view(connection, filename, name, input_file, line, column) found_one = call_synctex_view(connection, filename, name, input_file, line, column);
== true) {
found_one = true;
}
} }
g_variant_iter_free(iter); g_variant_iter_free(iter);
g_variant_unref(vnames); g_variant_unref(vnames);
g_object_unref(connection); g_object_unref(connection);
return found_one; return found_one ? 1 : 0;
} }
bool int
zathura_dbus_synctex_position(const char* filename, const char* input_file, zathura_dbus_synctex_position(const char* filename, const char* input_file,
int line, int column, pid_t hint) int line, int column, pid_t hint)
{ {

View file

@ -62,7 +62,7 @@ void zathura_dbus_edit(ZathuraDbus* dbus, unsigned int page, unsigned int x, uns
* @param column column index (starts at 0) * @param column column index (starts at 0)
* @param hint zathura process ID that has filename open * @param hint zathura process ID that has filename open
*/ */
bool zathura_dbus_synctex_position(const char* filename, const char* input_file, int zathura_dbus_synctex_position(const char* filename, const char* input_file,
int line, int column, pid_t hint); int line, int column, pid_t hint);
#endif #endif

View file

@ -127,12 +127,16 @@ main(int argc, char* argv[])
g_free(input_file); g_free(input_file);
g_free(real_path); g_free(real_path);
if (ret == false) { if (ret == -1) {
girara_error("Could not find open instance for '%s' or got no usable data from synctex.", real_path); /* D-Bus or SyncTeX failed */
girara_error("Got no usable data from SyncTeX or D-Bus failed in some way.");
return -1;
} else if (ret == 1) {
/* Found a instance */
return 0;
} }
g_free(real_path); girara_debug("No instance found. Starting new one.");
return ret == true ? 0 : -1;
} }
/* check mode */ /* check mode */
@ -195,7 +199,7 @@ main(int argc, char* argv[])
if (argc > 1) { if (argc > 1) {
if (page_number > 0) if (page_number > 0)
--page_number; --page_number;
document_open_idle(zathura, argv[1], password, page_number, mode); document_open_idle(zathura, argv[1], password, page_number, mode, synctex_fwd);
/* open additional files */ /* open additional files */
for (int i = 2; i < argc; i++) { for (int i = 2; i < argc; i++) {

View file

@ -41,6 +41,7 @@
#include "adjustment.h" #include "adjustment.h"
#include "dbus-interface.h" #include "dbus-interface.h"
#include "css-definitions.h" #include "css-definitions.h"
#include "synctex.h"
typedef struct zathura_document_info_s { typedef struct zathura_document_info_s {
zathura_t* zathura; zathura_t* zathura;
@ -48,6 +49,7 @@ typedef struct zathura_document_info_s {
const char* password; const char* password;
int page_number; int page_number;
const char* mode; const char* mode;
const char* synctex;
} zathura_document_info_t; } zathura_document_info_t;
@ -524,8 +526,13 @@ document_info_open(gpointer data)
} }
if (file != NULL) { if (file != NULL) {
if (document_info->synctex != NULL) {
document_open_synctex(document_info->zathura, file,
document_info->password, document_info->synctex);
} else {
document_open(document_info->zathura, file, document_info->password, document_open(document_info->zathura, file, document_info->password,
document_info->page_number); document_info->page_number);
}
g_free(file); g_free(file);
if (document_info->mode != NULL) { if (document_info->mode != NULL) {
@ -929,9 +936,34 @@ error_out:
return false; return false;
} }
bool
document_open_synctex(zathura_t* zathura, const char* path,
const char* password, const char* synctex)
{
bool ret = document_open(zathura, path, password,
ZATHURA_PAGE_NUMBER_UNSPECIFIED);
if (ret == false) {
return false;
}
if (synctex == NULL) {
return true;
}
int line = 0;
int column = 0;
char* input_file = NULL;
if (synctex_parse_input(synctex, &input_file, &line, &column) == false) {
return false;
}
ret = synctex_view(zathura, input_file, line, column);
g_free(input_file);
return ret;
}
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, const char* mode) int page_number, const char* mode, const char* synctex)
{ {
if (zathura == NULL || path == NULL) { if (zathura == NULL || path == NULL) {
return; return;
@ -947,6 +979,7 @@ document_open_idle(zathura_t* zathura, const char* path, const char* password,
document_info->password = password; document_info->password = password;
document_info->page_number = page_number; document_info->page_number = page_number;
document_info->mode = mode; document_info->mode = mode;
document_info->synctex = synctex;
gdk_threads_add_idle(document_info_open, document_info); gdk_threads_add_idle(document_info_open, document_info);
} }

View file

@ -291,6 +291,19 @@ void zathura_set_argv(zathura_t* zathura, char** argv);
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); int page_number);
/**
* Opens a file
*
* @param zathura The zathura session
* @param path The path to the file
* @param password The password of the file
* @param synctex Open at the given SyncTeX string
*
* @return If no error occured true, otherwise false, is returned.
*/
bool document_open_synctex(zathura_t* zathura, const char* path,
const char* password, const char* synctex);
/** /**
* Opens a file (idle) * Opens a file (idle)
* *
@ -299,10 +312,11 @@ bool document_open(zathura_t* zathura, const char* path, const char* password,
* @param password The password of the file * @param password The password of the file
* @param page_number Open given page number * @param page_number Open given page number
* @param mode Open in given page mode * @param mode Open in given page mode
* @param synctex SyncTeX string
*/ */
void document_open_idle(zathura_t* zathura, const char* path, void document_open_idle(zathura_t* zathura, const char* path,
const char* password, int page_number, const char* password, int page_number,
const char* mode); const char* mode, const char* synctex);
/** /**
* Save a open file * Save a open file