Convert backward synchronization to synctex_parser

Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
This commit is contained in:
Sebastian Ramacher 2014-05-30 17:07:07 +02:00
parent 79eb7d88c8
commit 87f0ee8d5b
2 changed files with 47 additions and 16 deletions

View file

@ -8,13 +8,12 @@
#include "page.h" #include "page.h"
#include "document.h" #include "document.h"
#include "utils.h" #include "utils.h"
#include "synctex/synctex_parser.h" #include "synctex/synctex_parser.h"
void void
synctex_edit(zathura_t* zathura, zathura_page_t* page, int x, int y) synctex_edit(zathura_t* zathura, zathura_page_t* page, int x, int y)
{ {
if (zathura == NULL || page == NULL) { if (zathura == NULL || page == NULL || zathura->synctex.editor == NULL) {
return; return;
} }
@ -28,24 +27,55 @@ synctex_edit(zathura_t* zathura, zathura_page_t* page, int x, int y)
return; return;
} }
char** argv = g_try_malloc0(sizeof(char*) * (zathura->synctex.editor != NULL ? synctex_scanner_t scanner = synctex_scanner_new_with_output_file(filename, NULL, 1);
7 : 5)); if (scanner == NULL) {
if (argv == NULL) { girara_debug("Failed to create synctex scanner.");
return; return;
} }
argv[0] = g_strdup("synctex"); synctex_scanner_t temp = synctex_scanner_parse(scanner);
argv[1] = g_strdup("edit"); if (temp == NULL) {
argv[2] = g_strdup("-o"); girara_debug("Failed to parse synctex file.");
argv[3] = g_strdup_printf("%d:%d:%d:%s", zathura_page_get_index(page) + 1, x, synctex_scanner_free(scanner);
y, filename); return;
if (zathura->synctex.editor != NULL) {
argv[4] = g_strdup("-x");
argv[5] = g_strdup(zathura->synctex.editor);
} }
g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL); if (synctex_edit_query(scanner, zathura_page_get_index(page) + 1, x, y) > 0) {
g_strfreev(argv); /* Assume that a backward search returns either at most one result. */
synctex_node_t node = synctex_next_result(scanner);
if (node != NULL) {
const char* input_file = synctex_scanner_get_name(scanner, synctex_node_tag(node));
const int line = synctex_node_line(node);
const int column = synctex_node_column (node);
char* linestr = g_strdup_printf("%d", line);
char* columnstr = g_strdup_printf("%d", column);
gchar** argv = NULL;
gint argc = 0;
if (g_shell_parse_argv(zathura->synctex.editor, &argc, &argv, NULL) == TRUE) {
for (gint i = 0; i != argc; ++i) {
char* temp = girara_replace_substring(argv[i], "%{line}", linestr);
g_free(argv[i]);
argv[i] = temp;
temp = girara_replace_substring(argv[i], "%{column}", columnstr);
g_free(argv[i]);
argv[i] = temp;
temp = girara_replace_substring(argv[i], "%{input}", input_file);
g_free(argv[i]);
argv[i] = temp;
}
g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL);
g_strfreev(argv);
}
g_free(linestr);
g_free(columnstr);
}
}
synctex_scanner_free(scanner);
} }
girara_list_t* girara_list_t*

View file

@ -12,6 +12,7 @@ typedef struct synctex_page_rect_s {
void synctex_edit(zathura_t* zathura, zathura_page_t* page, int x, int y); void synctex_edit(zathura_t* zathura, zathura_page_t* page, int x, int y);
girara_list_t* synctex_rectangles_from_position(const char* filename, girara_list_t* synctex_rectangles_from_position(const char* filename,
const char* position, unsigned int* page, girara_list_t** secondary_rects); const char* input_file, int line, int column, unsigned int* page,
girara_list_t** secondary_rects);
#endif #endif