[latexila] Use LatexilaSynctex



commit 07e80f672ec43a206282d1dcbe77d6633e38188f
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed May 21 22:22:59 2014 +0200

    Use LatexilaSynctex
    
    And rewrite some utils functions in C.

 docs/reference/latexila-sections.txt |    2 +
 po/POTFILES.in                       |    1 -
 src/Makefile.am                      |    1 -
 src/document_view.vala               |    5 +-
 src/file_browser.vala                |    2 +-
 src/liblatexila/latexila-utils.c     |   78 +++++++++-
 src/liblatexila/latexila-utils.h     |    8 +-
 src/main_window.vala                 |    5 +-
 src/structure.vala                   |    2 +-
 src/synctex.vala                     |  281 ----------------------------------
 src/utils.vala                       |   35 +----
 tests/test-utils.c                   |   19 +++
 12 files changed, 114 insertions(+), 325 deletions(-)
---
diff --git a/docs/reference/latexila-sections.txt b/docs/reference/latexila-sections.txt
index eaa205a..88cf82e 100644
--- a/docs/reference/latexila-sections.txt
+++ b/docs/reference/latexila-sections.txt
@@ -155,9 +155,11 @@ latexila_synctex_get_type
 <FILE>utils</FILE>
 <TITLE>LatexilaUtils</TITLE>
 latexila_utils_get_shortname
+latexila_utils_get_extension
 latexila_utils_replace_home_dir_with_tilde
 latexila_utils_register_icons
 latexila_utils_str_replace
 latexila_utils_file_query_exists_async
 latexila_utils_file_query_exists_finish
+latexila_utils_show_uri
 </SECTION>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index a9b5988..5f46729 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -50,7 +50,6 @@ src/structure_model.vala
 src/structure.vala
 src/symbols.vala
 src/symbols_view.vala
-src/synctex.vala
 src/tab_info_bar.vala
 src/templates_dialogs.vala
 src/templates.vala
diff --git a/src/Makefile.am b/src/Makefile.am
index 92f905e..73341af 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -52,7 +52,6 @@ vala_files =                          \
        structure.vala                  \
        symbols.vala                    \
        symbols_view.vala               \
-       synctex.vala                    \
        tab_info_bar.vala               \
        templates_dialogs.vala          \
        templates.vala                  \
diff --git a/src/document_view.vala b/src/document_view.vala
index 3da3fbc..d405967 100644
--- a/src/document_view.vala
+++ b/src/document_view.vala
@@ -203,8 +203,9 @@ public class DocumentView : Gtk.SourceView
         if (event.button == 1 &&
             Gdk.ModifierType.CONTROL_MASK in event.state)
         {
-            Synctex synctex = Synctex.get_default ();
-            synctex.forward_search (this.buffer as Document);
+            Latexila.Synctex synctex = Latexila.Synctex.get_instance ();
+            Document doc = this.buffer as Document;
+            synctex.forward_search (this.buffer, doc.location, doc.get_main_file ());
         }
 
         // propagate the event further
diff --git a/src/file_browser.vala b/src/file_browser.vala
index 29e0e6e..df8a524 100644
--- a/src/file_browser.vala
+++ b/src/file_browser.vala
@@ -186,7 +186,7 @@ public class FileBrowser : Grid
 
             try
             {
-                Utils.show_uri (this.get_screen (), file.get_uri ());
+                Latexila.utils_show_uri (this.get_screen (), file.get_uri ());
             }
             catch (Error e)
             {
diff --git a/src/liblatexila/latexila-utils.c b/src/liblatexila/latexila-utils.c
index 00f4bcf..0d6de02 100644
--- a/src/liblatexila/latexila-utils.c
+++ b/src/liblatexila/latexila-utils.c
@@ -31,8 +31,8 @@
  */
 
 #include "latexila-utils.h"
-#include <gtk/gtk.h>
 #include <string.h>
+#include "latexila-synctex.h"
 
 static gint
 get_extension_position (const gchar *filename)
@@ -80,6 +80,20 @@ latexila_utils_get_shortname (const gchar *filename)
 }
 
 /**
+ * latexila_utils_get_extension:
+ * @filename: a filename.
+ *
+ * Returns: the @filename's extension with the dot, in lowercase. Free with
+ * g_free().
+ */
+gchar *
+latexila_utils_get_extension (const gchar *filename)
+{
+  gint pos = get_extension_position (filename);
+  return g_ascii_strdown (filename + pos, -1);
+}
+
+/**
  * latexila_utils_replace_home_dir_with_tilde:
  * @filename: the filename.
  *
@@ -290,3 +304,65 @@ latexila_utils_file_query_exists_finish (GFile        *file,
 
   return FALSE;
 }
+
+static gboolean
+default_document_viewer_is_evince (const gchar *uri)
+{
+  GFile *file;
+  GAppInfo *app_info;
+  const gchar *executable;
+  gboolean ret;
+  GError *error = NULL;
+
+  file = g_file_new_for_uri (uri);
+  app_info = g_file_query_default_handler (file, NULL, &error);
+  g_object_unref (file);
+
+  if (error != NULL)
+    {
+      g_warning ("Impossible to know if evince is the default document viewer: %s",
+                 error->message);
+
+      g_error_free (error);
+      return FALSE;
+    }
+
+  executable = g_app_info_get_executable (app_info);
+  ret = strstr (executable, "evince") != NULL;
+
+  g_object_unref (app_info);
+  return ret;
+}
+
+/**
+ * latexila_utils_show_uri:
+ * @screen: (nullable): a #GdkScreen, or %NULL.
+ * @uri: the URI to show
+ * @error: (out) (optional): a %NULL #GError, or %NULL.
+ *
+ * Shows the @uri. If the URI is a PDF file and if Evince is the default
+ * document viewer, this function also connects the Evince window so the
+ * backward search works (switch from the PDF to the source file).
+ */
+void
+latexila_utils_show_uri (GdkScreen    *screen,
+                         const gchar  *uri,
+                         GError      **error)
+{
+  g_return_if_fail (uri != NULL);
+  g_return_if_fail (error == NULL || *error == NULL);
+
+  if (gtk_show_uri (screen, uri, GDK_CURRENT_TIME, error))
+    {
+      gchar *extension = latexila_utils_get_extension (uri);
+
+      if (g_strcmp0 (extension, ".pdf") == 0 &&
+          default_document_viewer_is_evince (uri))
+        {
+          LatexilaSynctex *synctex = latexila_synctex_get_instance ();
+          latexila_synctex_connect_evince_window (synctex, uri);
+        }
+
+      g_free (extension);
+    }
+}
diff --git a/src/liblatexila/latexila-utils.h b/src/liblatexila/latexila-utils.h
index c1b44a9..ff12893 100644
--- a/src/liblatexila/latexila-utils.h
+++ b/src/liblatexila/latexila-utils.h
@@ -20,12 +20,14 @@
 #ifndef __LATEXILA_UTILS_H__
 #define __LATEXILA_UTILS_H__
 
-#include <gio/gio.h>
+#include <gtk/gtk.h>
 
 G_BEGIN_DECLS
 
 gchar *         latexila_utils_get_shortname                    (const gchar *filename);
 
+gchar *         latexila_utils_get_extension                    (const gchar *filename);
+
 gchar *         latexila_utils_replace_home_dir_with_tilde      (const gchar *filename);
 
 void            latexila_utils_register_icons                   (void);
@@ -42,6 +44,10 @@ void            latexila_utils_file_query_exists_async          (GFile
 gboolean        latexila_utils_file_query_exists_finish         (GFile        *file,
                                                                  GAsyncResult *result);
 
+void            latexila_utils_show_uri                         (GdkScreen    *screen,
+                                                                 const gchar  *uri,
+                                                                 GError      **error);
+
 G_END_DECLS
 
 #endif /* __LATEXILA_UTILS_H__ */
diff --git a/src/main_window.vala b/src/main_window.vala
index 2510b9a..5e2e277 100644
--- a/src/main_window.vala
+++ b/src/main_window.vala
@@ -1139,8 +1139,9 @@ public class MainWindow : Window
     {
         return_if_fail (active_tab != null);
 
-        Synctex synctex = Synctex.get_default ();
-        synctex.forward_search (active_document);
+        Latexila.Synctex synctex = Latexila.Synctex.get_instance ();
+        synctex.forward_search (active_document, active_document.location,
+            active_document.get_main_file ());
     }
 
     /* Projects */
diff --git a/src/structure.vala b/src/structure.vala
index 6d91191..2cbab8c 100644
--- a/src/structure.vala
+++ b/src/structure.vala
@@ -712,7 +712,7 @@ public class Structure : Grid
     {
         try
         {
-            Utils.show_uri (get_screen (), referenced_file.get_uri ());
+            Latexila.utils_show_uri (get_screen (), referenced_file.get_uri ());
         }
         catch (Error e)
         {
diff --git a/src/utils.vala b/src/utils.vala
index 1b45cf3..274c5c5 100644
--- a/src/utils.vala
+++ b/src/utils.vala
@@ -183,7 +183,7 @@ namespace Utils
         return true;
     }
 
-    // Retruns null on error.
+    // Returns null on error.
     public string? load_file (File file)
     {
         try
@@ -268,39 +268,6 @@ namespace Utils
         return relative_path;
     }
 
-    public void show_uri (Gdk.Screen? screen, string uri) throws Error
-    {
-        if (! Gtk.show_uri (screen, uri, Gdk.CURRENT_TIME))
-            return;
-
-        // Backward search for PDF documents.
-        if (get_extension (uri) == ".pdf" &&
-            default_document_viewer_is_evince (uri))
-        {
-            Synctex synctex = Synctex.get_default ();
-            synctex.create_evince_window (uri);
-        }
-    }
-
-    private bool default_document_viewer_is_evince (string uri)
-    {
-        File file = File.new_for_uri (uri);
-        AppInfo app;
-
-        try
-        {
-            app = file.query_default_handler ();
-        }
-        catch (Error e)
-        {
-            warning ("Impossible to know if evince is the default document viewer: %s",
-                e.message);
-            return false;
-        }
-
-        return app.get_executable ().contains ("evince");
-    }
-
 
     /*************************************************************************/
     // UI stuff
diff --git a/tests/test-utils.c b/tests/test-utils.c
index 4bfff00..e771ed5 100644
--- a/tests/test-utils.c
+++ b/tests/test-utils.c
@@ -42,6 +42,24 @@ test_get_shortname (void)
 }
 
 static void
+test_get_extension (void)
+{
+  gchar *extension;
+
+  extension = latexila_utils_get_extension ("file.pdf");
+  g_assert_cmpstr (extension, ==, ".pdf");
+  g_free (extension);
+
+  extension = latexila_utils_get_extension ("file.tar.gz");
+  g_assert_cmpstr (extension, ==, ".gz");
+  g_free (extension);
+
+  extension = latexila_utils_get_extension ("file");
+  g_assert_cmpstr (extension, ==, "");
+  g_free (extension);
+}
+
+static void
 test_replace_home_dir_with_tilde (void)
 {
   const gchar *homedir = g_get_home_dir ();
@@ -92,6 +110,7 @@ main (gint    argc,
   g_test_init (&argc, &argv, NULL);
 
   g_test_add_func ("/utils/get-shortname", test_get_shortname);
+  g_test_add_func ("/utils/get-extension", test_get_extension);
   g_test_add_func ("/utils/replace-home-dir-with-tilde", test_replace_home_dir_with_tilde);
   g_test_add_func ("/utils/str-replace", test_str_replace);
 


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