[latexila] Synctex: backward search



commit 14448cf84c74ed17d8ff5b5f48da5be612a4ebef
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Thu May 22 00:03:16 2014 +0200

    Synctex: backward search
    
    LatexilaSynctex sends a signal, and LatexilaApp (in Vala) connects to it
    to open the file and jumps at the given position.

 src/latexila_app.vala              |   20 ++++++++++++++++
 src/liblatexila/latexila-synctex.c |   43 ++++++++++++++++++++++++++++++++++-
 2 files changed, 61 insertions(+), 2 deletions(-)
---
diff --git a/src/latexila_app.vala b/src/latexila_app.vala
index e76181c..35b695c 100644
--- a/src/latexila_app.vala
+++ b/src/latexila_app.vala
@@ -115,6 +115,7 @@ public class LatexilaApp : Gtk.Application
         create_window ();
         reopen_files ();
         Gtk.AccelMap.load (get_accel_filename ());
+        support_backward_search ();
         release ();
     }
 
@@ -259,4 +260,23 @@ public class LatexilaApp : Gtk.Application
         return Path.build_filename (Environment.get_user_config_dir (),
             "latexila", "accels");
     }
+
+    private void support_backward_search ()
+    {
+        Latexila.Synctex synctex = Latexila.Synctex.get_instance ();
+
+        synctex.backward_search.connect ((tex_uri, line, timestamp) =>
+        {
+            File tex_file = File.new_for_uri (tex_uri);
+            if (! tex_file.query_exists ())
+            {
+                warning (@"Backward search: the file \"$tex_uri\" doesn't exist.");
+                return;
+            }
+
+            MainWindow main_window = active_window as MainWindow;
+            main_window.jump_to_file_position (tex_file, line, line + 1);
+            main_window.present_with_time (timestamp);
+        });
+    }
 }
diff --git a/src/liblatexila/latexila-synctex.c b/src/liblatexila/latexila-synctex.c
index 0703ecc..75416a3 100644
--- a/src/liblatexila/latexila-synctex.c
+++ b/src/liblatexila/latexila-synctex.c
@@ -30,6 +30,9 @@
  *
  * D-Bus is used to communicate between LaTeXila and Evince. The implementation
  * uses the asynchronous gdbus generated functions.
+ *
+ * For the position, only the line is used, not the column. The column is a bit
+ * buggy.
  */
 
 #include "latexila-synctex.h"
@@ -37,8 +40,6 @@
 #include "evince-gdbus-generated.h"
 #include "latexila-utils.h"
 
-static LatexilaSynctex *instance = NULL;
-
 struct _LatexilaSynctexPrivate
 {
   /* PDF URI -> EvinceWindow object */
@@ -58,8 +59,17 @@ typedef struct
   gchar *owner;
 } ConnectEvinceWindowData;
 
+enum
+{
+  SIGNAL_BACKWARD_SEARCH,
+  LAST_SIGNAL
+};
+
 G_DEFINE_TYPE_WITH_PRIVATE (LatexilaSynctex, latexila_synctex, G_TYPE_OBJECT)
 
+static LatexilaSynctex *instance = NULL;
+static guint signals[LAST_SIGNAL];
+
 static ForwardSearchData *
 forward_search_data_new (void)
 {
@@ -115,6 +125,25 @@ latexila_synctex_class_init (LatexilaSynctexClass *klass)
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
   object_class->dispose = latexila_synctex_dispose;
+
+  /**
+   * LatexilaSynctex::backward-search:
+   * @synctex: the #LatexilaSynctex instance.
+   * @tex_uri: the *.tex file URI.
+   * @line: the line to jump to.
+   * @timestamp: timestamp of the event.
+   *
+   * The ::backward-search signal is emitted to perform a backward search, i.e.
+   * switching from the PDF to the source *.tex file.
+   */
+  signals[SIGNAL_BACKWARD_SEARCH] = g_signal_new ("backward-search",
+                                                  LATEXILA_TYPE_SYNCTEX,
+                                                  G_SIGNAL_RUN_LAST,
+                                                  0, NULL, NULL, NULL,
+                                                  G_TYPE_NONE, 3,
+                                                  G_TYPE_STRING,
+                                                  G_TYPE_INT,
+                                                  G_TYPE_UINT);
 }
 
 static void
@@ -216,6 +245,16 @@ sync_source_cb (EvinceWindow    *window,
                 guint            timestamp,
                 LatexilaSynctex *synctex)
 {
+  gint line;
+  gint column; /* not used */
+
+  g_variant_get (pos, "(ii)", &line, &column);
+
+  g_signal_emit (synctex,
+                 signals[SIGNAL_BACKWARD_SEARCH], 0,
+                 tex_uri,
+                 line - 1,
+                 timestamp);
 }
 
 static void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]