[gnome-latex: 18/205] compile (pdflatex) and view PDF



commit f61b4aa36421305a4dde274992b5169eb0857eba
Author: Sébastien Wilmet <sebastien wilmet gmail com>
Date:   Thu Aug 6 17:54:11 2009 +0200

    compile (pdflatex) and view PDF

 TODO            |  5 ++++
 src/callbacks.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 src/callbacks.h |  1 +
 src/main.c      |  2 +-
 4 files changed, 92 insertions(+), 2 deletions(-)
---
diff --git a/TODO b/TODO
index 5d03c2c..8a93168 100644
--- a/TODO
+++ b/TODO
@@ -20,3 +20,8 @@ Jul 31, 2009 to Aug 7, 2009
        x undo/redo
        x font monospace
 
+[-] compile and view
+       x compile (pdflatex)
+       x view PDF
+       - compile (latex)
+       - view DVI
diff --git a/src/callbacks.c b/src/callbacks.c
index 4f1b61b..e20ea84 100644
--- a/src/callbacks.c
+++ b/src/callbacks.c
@@ -3,6 +3,8 @@
 #include <locale.h>
 #include <libintl.h>
 #include <gtk/gtk.h>
+#include <glib.h>
+#include <glib/gstdio.h>
 #include <gtksourceview/gtksourceview.h>
 #include <gtksourceview/gtksourcelanguage.h>
 #include <gtksourceview/gtksourcelanguagemanager.h>
@@ -223,28 +225,110 @@ cb_pdflatex (void)
 {
        if (docs.active != NULL)
        {
+               GtkTextBuffer *log_buffer = gtk_text_view_get_buffer (docs.log);
+
+               // the current document is a *.tex file?
+               gboolean tex_file = g_regex_match_simple ("\\.tex$", docs.active->path, 0, 0);
+               if (! tex_file)
+               {
+                       gchar *text = g_strdup_printf (_("compilation failed: %s is not a *.tex file"),
+                                       g_path_get_basename (docs.active->path));
+
+                       gtk_text_buffer_set_text (log_buffer, text, -1);
+                       g_free (text);
+                       return;
+               }
+
+               // run the command in the directory where the .tex is
+               gchar *dir_backup = g_get_current_dir ();
+               gchar *dir = g_path_get_dirname (docs.active->path);
+               g_chdir (dir);
                gchar *command = g_strdup_printf ("pdflatex -interaction=nonstopmode %s",
                                docs.active->path);
                gchar *output;
                GError *error = NULL;
+
+               print_info ("execution of the command: %s", command);
                
                g_spawn_command_line_sync (command, &output, NULL, NULL, &error);
+               g_chdir (dir_backup);
                
                // an error occured
                if (error != NULL)
                {
                        print_warning ("execution failed: %s", error->message);
+                       g_error_free (error);
                        return;
                }
 
-               GtkTextBuffer *log_buffer = gtk_text_view_get_buffer (docs.log);
+               // show the message of the command
                gtk_text_buffer_set_text (log_buffer, output, -1);
 
+               g_free (dir_backup);
+               g_free (dir);
                g_free (command);
                g_free (output);
        }
 }
 
+void
+cb_view_pdf (void)
+{
+       if (docs.active != NULL)
+       {
+               GtkTextBuffer *log_buffer = gtk_text_view_get_buffer (docs.log);
+
+               GError *error = NULL;
+               GRegex *regex = g_regex_new ("\\.tex$", 0, 0, NULL);
+
+               // the current document is a *.tex file?
+               gboolean tex_file = g_regex_match (regex, docs.active->path, 0, NULL);
+               if (! tex_file)
+               {
+                       gchar *text = g_strdup_printf (_("view PDF failed: %s is not a *.tex file"),
+                                       g_path_get_basename (docs.active->path));
+
+                       gtk_text_buffer_set_text (log_buffer, text, -1);
+                       g_free (text);
+                       g_regex_unref (regex);
+                       return;
+               }
+
+               // replace .tex by .pdf
+               gchar *pdf_path = g_regex_replace_literal (regex, docs.active->path,
+                               -1, 0, ".pdf", 0, NULL);
+
+               // the PDF file exist?
+               if (! g_file_test (pdf_path, G_FILE_TEST_IS_REGULAR))
+               {
+                       gchar *text = g_strdup_printf (
+                                       _("%s does not exist. If this is not already made, compile the 
document with pdflatex."),
+                                       g_path_get_basename (pdf_path));
+                       
+                       gtk_text_buffer_set_text (log_buffer, text, -1);
+                       g_free (text);
+                       g_free (pdf_path);
+                       g_regex_unref (regex);
+                       return;
+               }
+
+               // run the command
+               gchar *command = g_strdup_printf ("evince %s", pdf_path);
+               print_info ("execution of the command: %s", command);
+               g_spawn_command_line_async (command, &error);
+
+               if (error != NULL)
+               {
+                       print_warning ("execution failed: %s", error->message);
+                       g_error_free (error);
+               }
+
+               g_regex_unref (regex);
+               g_free (pdf_path);
+               g_free (command);
+       }
+}
+
 void
 cb_about_dialog (void)
 {
diff --git a/src/callbacks.h b/src/callbacks.h
index b9645db..e56ad63 100644
--- a/src/callbacks.h
+++ b/src/callbacks.h
@@ -10,6 +10,7 @@ void cb_quit (void);
 void cb_undo (void);
 void cb_redo (void);
 void cb_pdflatex (void);
+void cb_view_pdf (void);
 void cb_about_dialog (void);
 void cb_text_changed (GtkWidget *widget, gpointer user_data);
 void cb_page_change (GtkNotebook *notebook, GtkNotebookPage *page, guint page_num, gpointer user_data);
diff --git a/src/main.c b/src/main.c
index 5624102..bbd8e6a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -77,7 +77,7 @@ main (int argc, char *argv[])
                {"compile_pdflatex", GTK_STOCK_EXECUTE, _("Compile (pdflatex)"), "<Release>F7",
                        _("Produce the document in PDF format"), G_CALLBACK (cb_pdflatex)},
                {"viewPDF", GTK_STOCK_FILE, _("View PDF"), "<Release>F8",
-                       _("View the PDF file"), NULL},
+                       _("View the PDF file"), G_CALLBACK (cb_view_pdf)},
                
                {"Help", NULL, _("Help"), NULL, NULL, NULL},
                {"HelpAbout", GTK_STOCK_ABOUT, _("About"), NULL,


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