synctex.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include "synctex/synctex_parser.h"
  6. void print_usage() {
  7. fprintf (stderr, "Usage: synctex code <synctex_file> <filename> <line> <column>\n");
  8. fprintf (stderr, " synctex pdf <synctex_file> <page> <h> <v>\n");
  9. }
  10. int main(int argc, char *argv[], char *envp[]) {
  11. synctex_scanner_t scanner;
  12. if (argc < 6 || (strcmp(argv[1], "code") != 0 && strcmp(argv[1], "pdf") != 0)) {
  13. print_usage();
  14. return EXIT_FAILURE;
  15. }
  16. const char* direction = argv[1];
  17. const char* synctex_file = argv[2];
  18. scanner = synctex_scanner_new_with_output_file(synctex_file, NULL, 1);
  19. if(!(scanner = synctex_scanner_parse(scanner))) {
  20. fprintf (stderr, "Could not parse output file\n");
  21. return EXIT_FAILURE;
  22. }
  23. if (strcmp(direction, "code") == 0) {
  24. const char* name = argv[3];
  25. int line = atoi(argv[4]);
  26. int column = atoi(argv[5]);
  27. if(synctex_display_query(scanner, name, line, column) > 0) {
  28. synctex_node_t node;
  29. while((node = synctex_next_result(scanner))) {
  30. int page = synctex_node_page(node);
  31. float h = synctex_node_box_visible_h(node);
  32. float v = synctex_node_box_visible_v(node);
  33. float width = synctex_node_box_visible_width(node);
  34. float height = synctex_node_box_visible_height(node);
  35. printf ("NODE\t%d\t%.2f\t%.2f\t%.2f\t%.2f\n", page, h, v, width, height);
  36. }
  37. }
  38. } else if (strcmp(direction, "pdf") == 0) {
  39. int page = atoi(argv[3]);
  40. float h = atof(argv[4]);
  41. float v = atof(argv[5]);
  42. if(synctex_edit_query(scanner, page, h, v) > 0) {
  43. synctex_node_t node;
  44. while((node = synctex_next_result(scanner))) {
  45. int tag = synctex_node_tag(node);
  46. const char* name = synctex_scanner_get_name(scanner, tag);
  47. int line = synctex_node_line(node);
  48. int column = synctex_node_column(node);
  49. printf ("NODE\t%s\t%d\t%d\n", name, line, column);
  50. }
  51. }
  52. }
  53. return 0;
  54. }