synctex.c 11.6 KB
Newer Older
1 2
/* See LICENSE file for license and copyright information */

3
#include <glib.h>
4
#include <girara/utils.h>
5
#include <girara/settings.h>
Moritz Lipp's avatar
Moritz Lipp committed
6

7
#ifdef WITH_SYNCTEX
8
#include <synctex/synctex_parser.h>
Moritz Lipp's avatar
Moritz Lipp committed
9
#endif
10

11 12 13 14
#include "synctex.h"
#include "zathura.h"
#include "page.h"
#include "document.h"
15
#include "utils.h"
16
#include "adjustment.h"
17

18
#ifdef WITH_SYNCTEX
19 20 21 22 23 24 25 26
#ifdef WITH_SYNCTEX1
typedef synctex_scanner_t synctex_scanner_p;
typedef synctex_node_t synctex_node_p;

#define synctex_scanner_next_result(scanner) synctex_next_result(scanner)
#define synctex_display_query(scanner, file, line, column, page) synctex_display_query(scanner, file, line, column)
#endif

27 28 29
bool
synctex_get_input_line_column(const char* filename, unsigned int page, int x, int y,
    char** input_file, unsigned int* line, unsigned int* column)
30
{
Moritz Lipp's avatar
Moritz Lipp committed
31
  if (filename == NULL) {
32
    return false;
Moritz Lipp's avatar
Moritz Lipp committed
33 34
  }

35
  synctex_scanner_p scanner = synctex_scanner_new_with_output_file(filename, NULL, 1);
36 37
  if (scanner == NULL) {
    girara_debug("Failed to create synctex scanner.");
38
    return false;
39 40
  }

41
  synctex_scanner_p temp = synctex_scanner_parse(scanner);
42 43 44
  if (temp == NULL) {
    girara_debug("Failed to parse synctex file.");
    synctex_scanner_free(scanner);
45
    return false;
46 47
  }

48 49 50 51
  bool ret = false;

  if (synctex_edit_query(scanner, page + 1u, x, y) > 0) {
    /* Assume that a backward search returns at most one result. */
52
    synctex_node_p node = synctex_scanner_next_result(scanner);
53
    if (node != NULL) {
54 55 56 57 58 59 60 61
      if (input_file != NULL) {
        *input_file = g_strdup(synctex_scanner_get_name(scanner, synctex_node_tag(node)));
      }
      if (line != NULL) {
        *line = synctex_node_line(node);
      }
      if (column != NULL) {
        *column = synctex_node_column(node);
62 63
      }

64
      ret = true;
65
    }
66 67
  }

68
  synctex_scanner_free(scanner);
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

  return ret;
}

void
synctex_edit(const char* editor, zathura_page_t* page, int x, int y)
{
  if (editor == NULL || page == NULL) {
    return;
  }

  zathura_document_t* document = zathura_page_get_document(page);
  if (document == NULL) {
    return;
  }

  const char* filename = zathura_document_get_path(document);
  if (filename == NULL) {
    return;
  }

  unsigned int line = 0;
  unsigned int column = 0;
  char* input_file = NULL;

  if (synctex_get_input_line_column(filename, zathura_page_get_index(page), x, y,
95
        &input_file, &line, &column) == true && input_file != NULL) {
96 97 98 99 100 101 102
    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(editor, &argc, &argv, NULL) == TRUE) {
      for (gint i = 0; i != argc; ++i) {
103 104 105 106 107 108 109 110
        char* arg = argv[i];
        char* temp = girara_replace_substring(arg, "%{line}", linestr);
        arg = temp;
        temp = girara_replace_substring(arg, "%{column}", columnstr);
        g_free(arg);
        arg = temp;
        temp = girara_replace_substring(arg, "%{input}", input_file);
        g_free(arg);
111 112 113 114 115 116 117 118
        arg = temp;

        if (arg != NULL) {
          g_free(argv[i]);
          argv[i] = arg;
        } else {
          girara_error("Failed to update '%s' with line (%s), column (%s) and input file (%s) data.", argv[i], linestr, columnstr, input_file);
        }
119 120
      }

Sebastian Ramacher's avatar
Sebastian Ramacher committed
121 122 123 124 125
      GError* error = NULL;
      if (g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &error) == FALSE) {
        girara_error("Failed to execute synctex command: %s", error->message);
        g_error_free(error);
      }
126 127 128 129 130 131
      g_strfreev(argv);
    }

    g_free(linestr);
    g_free(columnstr);
    g_free(input_file);
132 133
  } else {
    girara_warning("Failed to obtain data via SyncTeX or data is incomplete.");
134
  }
135
}
136

137
girara_list_t*
138 139
synctex_rectangles_from_position(const char* filename, const char* input_file,
                                 int line, int column, unsigned int* page,
140
                                 girara_list_t** secondary_rects)
141
{
142
  if (filename == NULL || input_file == NULL || page == NULL) {
143
    return NULL;
144 145
  }

146 147 148 149
  /* We use indexes starting at 0 but SyncTeX uses 1 */
  ++line;
  ++column;

150
  synctex_scanner_p scanner = synctex_scanner_new_with_output_file(filename, NULL, 1);
151 152
  if (scanner == NULL) {
    girara_debug("Failed to create synctex scanner.");
153 154 155
    return NULL;
  }

156
  synctex_scanner_p temp = synctex_scanner_parse(scanner);
157 158 159 160
  if (temp == NULL) {
    girara_debug("Failed to parse synctex file.");
    synctex_scanner_free(scanner);
    return NULL;
161 162
  }

163
  girara_list_t* hitlist     = girara_list_new2(g_free);
164
  girara_list_t* other_rects = girara_list_new2(g_free);
165

166 167
  if (synctex_display_query(scanner, input_file, line, column, -1) > 0) {
    synctex_node_p node        = NULL;
168
    bool got_page              = false;
169

170
    while ((node = synctex_scanner_next_result(scanner)) != NULL) {
171 172 173 174 175
      const unsigned int current_page = synctex_node_page(node) - 1;
      if (got_page == false) {
        got_page = true;
        *page = current_page;
      }
176

177 178 179 180 181
      zathura_rectangle_t rect = { 0, 0, 0, 0 };
      rect.x1 = synctex_node_box_visible_h(node);
      rect.y1 = synctex_node_box_visible_v(node) - synctex_node_box_visible_height(node);
      rect.x2 = rect.x1 + synctex_node_box_visible_width(node);
      rect.y2 = synctex_node_box_visible_depth(node) + synctex_node_box_visible_height (node) + rect.y1;
182

183 184 185 186
      if (*page == current_page) {
        zathura_rectangle_t* real_rect = g_try_malloc(sizeof(zathura_rectangle_t));
        if (real_rect == NULL) {
          continue;
187 188
        }

189
        *real_rect = rect;
190
        girara_list_append(hitlist, real_rect);
191 192 193 194 195 196
      } else {
        synctex_page_rect_t* page_rect = g_try_malloc(sizeof(synctex_page_rect_t));
        if (page_rect == NULL) {
          continue;
        }

197
        page_rect->page = current_page;
198 199
        page_rect->rect = rect;

200 201
        girara_list_append(other_rects, page_rect);
      }
202
    }
203 204
  }

205
  synctex_scanner_free(scanner);
206

207 208 209 210 211 212
  if (secondary_rects != NULL) {
    *secondary_rects = other_rects;
  } else {
    girara_list_free(other_rects);
  }

213
  return hitlist;
214
}
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
#else
bool
synctex_get_input_line_column(const char* UNUSED(filename),
                              unsigned int UNUSED(page), int UNUSED(x),
                              int UNUSED(y), char** UNUSED(input_file),
                              unsigned int* UNUSED(line),
                              unsigned int* UNUSED(column))
{
  return false;
}

void
synctex_edit(const char* UNUSED(editor), zathura_page_t* UNUSED(page),
             int UNUSED(x), int UNUSED(y))
{}

girara_list_t*
synctex_rectangles_from_position(const char* UNUSED(filename),
                                 const char* UNUSED(input_file),
                                 int UNUSED(line), int UNUSED(column),
                                 unsigned int* UNUSED(page),
                                 girara_list_t** UNUSED(secondary_rects))
{
  return NULL;
}
#endif
241

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
bool
synctex_parse_input(const char* synctex, char** input_file, int* line,
                    int* column)
{
  if (synctex == NULL || input_file == NULL || line == NULL || column == NULL) {
    return false;
  }

  char** split_fwd = g_strsplit(synctex, ":", 0);
  if (split_fwd == NULL || split_fwd[0] == NULL || split_fwd[1] == NULL ||
      split_fwd[2] == NULL || split_fwd[3] != NULL) {
    g_strfreev(split_fwd);
    return false;
  }

  *line = MIN(INT_MAX, g_ascii_strtoll(split_fwd[0], NULL, 10));
  *column = MIN(INT_MAX, g_ascii_strtoll(split_fwd[1], NULL, 10));
  /* SyncTeX starts indexing at 1, but we use 0 */
  if (*line > 0) {
    --*line;
  }
  if (*column > 0) {
    --*column;
  }
  *input_file = g_strdup(split_fwd[2]);

  g_strfreev(split_fwd);
  return true;
}
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336

void
synctex_highlight_rects(zathura_t* zathura, unsigned int page,
                        girara_list_t** rectangles)
{
  const unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document);

  for (unsigned int p = 0; p != number_of_pages; ++p) {
    GObject* widget = G_OBJECT(zathura->pages[p]);

    g_object_set(widget, "draw-links", FALSE, "search-results", rectangles[p],
                 NULL);
    if (p == page) {
      g_object_set(widget, "search-current", 0, NULL);
    }
  }

  document_draw_search_results(zathura, true);

  girara_list_t* rect_list = rectangles[page];
  if (rect_list == NULL || girara_list_size(rect_list) == 0) {
    girara_debug("No rectangles for the given page. Jumping to page %u.", page);
    page_set(zathura, page);
    return;
  }

  bool search_hadjust = true;
  girara_setting_get(zathura->ui.session, "search-hadjust", &search_hadjust);

  /* compute the position of the center of the page */
  double pos_x = 0;
  double pos_y = 0;
  page_number_to_position(zathura->document, page, 0.5, 0.5, &pos_x, &pos_y);

  /* correction to center the current result                          */
  /* NOTE: rectangle is in viewport units, already scaled and rotated */
  unsigned int cell_height = 0;
  unsigned int cell_width = 0;
  zathura_document_get_cell_size(zathura->document, &cell_height, &cell_width);

  unsigned int doc_height = 0;
  unsigned int doc_width = 0;
  zathura_document_get_document_size(zathura->document, &doc_height, &doc_width);

  /* Need to adjust rectangle to page scale and orientation */
  zathura_page_t* doc_page = zathura_document_get_page(zathura->document, page);
  zathura_rectangle_t* rect = girara_list_nth(rect_list, 0);
  if (rect == NULL) {
    girara_debug("List of rectangles is broken. Jumping to page %u.", page);
    page_set(zathura, page);
    return;
  }

  zathura_rectangle_t rectangle = recalc_rectangle(doc_page, *rect);

  /* compute the center of the rectangle, which will be aligned to the center
     of the viewport */
  double center_x = (rectangle.x1 + rectangle.x2) / 2;
  double center_y = (rectangle.y1 + rectangle.y2) / 2;

  pos_y += (center_y - (double)cell_height/2) / (double)doc_height;
  if (search_hadjust == true) {
    pos_x += (center_x - (double)cell_width/2) / (double)doc_width;
  }

  /* move to position */
Sebastian Ramacher's avatar
Sebastian Ramacher committed
337
  girara_debug("Jumping to page %u position (%0.2f, %0.2f).", page, pos_x, pos_y);
338 339 340 341 342
  zathura_jumplist_add(zathura);
  position_set(zathura, pos_x, pos_y);
  zathura_jumplist_add(zathura);
}

Sebastian Ramacher's avatar
Sebastian Ramacher committed
343 344 345 346 347 348 349 350 351 352 353 354 355
static void
dup_and_append_rect(void* data, void* userdata)
{
  const synctex_page_rect_t* rect = data;
  girara_list_t** all_rectangles  = userdata;

  zathura_rectangle_t* newrect = g_try_malloc0(sizeof(zathura_rectangle_t));
  if (newrect != NULL) {
    *newrect = rect->rect;
    girara_list_append(all_rectangles[rect->page], newrect);
  }
}

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
bool
synctex_view(zathura_t* zathura, const char* input_file,
             unsigned int line, unsigned int column)
{
  if (zathura == NULL || input_file == NULL) {
    return false;
  }

  const unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document);

  unsigned int page = 0;
  girara_list_t* secondary_rects = NULL;
  girara_list_t* rectangles = synctex_rectangles_from_position(
    zathura_document_get_path(zathura->document), input_file, line,
    column, &page, &secondary_rects);

  if (rectangles == NULL) {
    return false;
  }

  girara_list_t** all_rectangles = g_try_malloc0(number_of_pages * sizeof(girara_list_t*));
  if (all_rectangles == NULL) {
    girara_list_free(rectangles);
    return false;
  }

  for (unsigned int p = 0; p != number_of_pages; ++p) {
    if (p == page) {
      all_rectangles[p] = rectangles;
    } else {
      all_rectangles[p] = girara_list_new2(g_free);
    }
  }

  if (secondary_rects != NULL) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
391
    girara_list_foreach(secondary_rects, dup_and_append_rect, all_rectangles);
392 393 394 395 396 397 398 399 400
  }

  synctex_highlight_rects(zathura, page, all_rectangles);

  girara_list_free(secondary_rects);
  g_free(all_rectangles);

  return true;
}