[gnome-latex: 85/205] execution of the actions: convert document



commit 679feac96e9c09d38d72576906d895d9370c4e17
Author: Sébastien Wilmet <sebastien wilmet gmail com>
Date:   Tue Oct 6 23:34:55 2009 +0200

    execution of the actions: convert document
    
    Now the output of a conversion is showed with the same method as for a
    compilation.

 TODO                    |  11 ++--
 src/callbacks.c         |  26 +++------
 src/external_commands.c | 145 ++++++++++++++++++++++++------------------------
 src/external_commands.h |   2 +-
 4 files changed, 90 insertions(+), 94 deletions(-)
---
diff --git a/TODO b/TODO
index 20036b4..44f4597 100644
--- a/TODO
+++ b/TODO
@@ -5,12 +5,13 @@ Fri Oct 2, 2009 to Fri Oct 9, 2009
 [-] undo/redo and the "saved" document property
        - detect when the buffer is the same as in the file currently saved
 
-[-] execution of the actions
-       - verify that the method used for compile_document () is correct
-       - optimizations?
-       - same behavior than compile_document () for:
+[x] execution of the actions
+       ~ verify that the method used for compile_document () is correct
+       x same behavior than compile_document () for:
                * convert_document ()
-               * view_document ()
+
+[-] log zone
+       - keep only the 5 last actions
 
 [-] symbol tables
        - mathematical sets (N, Z, Q, R, C) in misc math
diff --git a/src/callbacks.c b/src/callbacks.c
index 93c8004..69f8960 100644
--- a/src/callbacks.c
+++ b/src/callbacks.c
@@ -531,18 +531,15 @@ cb_latex (void)
        if (latexila.active_doc == NULL)
                return;
 
+       // TODO save document if not saved
+
        gchar *title = _("Compile (latex)");
-       gchar *command[] = {
-               g_strdup (latexila.prefs->command_latex),
-               "-interaction=nonstopmode",
-               g_strdup (latexila.active_doc->path),
-               NULL
-       };
+       gchar *command = g_strdup_printf ("%s -interaction=nonstopmode %s", 
+                       latexila.prefs->command_latex, latexila.active_doc->path);
 
        compile_document (title, command);
-
-       g_free (command[0]);
-       g_free (command[2]);
+       
+       g_free (command);
 }
 
 void
@@ -552,17 +549,12 @@ cb_pdflatex (void)
                return;
 
        gchar *title = _("Compile (pdflatex)");
-       gchar *command[] = {
-               g_strdup (latexila.prefs->command_pdflatex),
-               "-interaction=nonstopmode",
-               g_strdup (latexila.active_doc->path),
-               NULL
-       };
+       gchar *command = g_strdup_printf ("%s -interaction=nonstopmode %s", 
+                       latexila.prefs->command_pdflatex, latexila.active_doc->path);
 
        compile_document (title, command);
 
-       g_free (command[0]);
-       g_free (command[2]);
+       g_free (command);
 }
 
 void
diff --git a/src/external_commands.c b/src/external_commands.c
index 825afd2..87e2d66 100644
--- a/src/external_commands.c
+++ b/src/external_commands.c
@@ -27,51 +27,16 @@
 #include <glib/gstdio.h>
 #include <gtksourceview/gtksourceview.h>
 
-#include <sys/time.h>
-
 #include "main.h"
 #include "config.h"
 #include "external_commands.h"
 #include "print.h"
 
-static double difftimeval (struct timeval start, struct timeval end);
-static gchar * get_command_line (gchar **command);
 static void command_running_finished (void);
 static gboolean cb_watch_output_command (GIOChannel *channel,
                GIOCondition condition, gpointer user_data);
 static void add_action (gchar *title, gchar *command);
 
-static struct timeval time_start;
-
-static double
-difftimeval (struct timeval start, struct timeval end)
-{
-    double seconds = end.tv_sec - start.tv_sec;
-    double microsec = end.tv_usec - start.tv_usec;
-    return seconds + microsec / 1000000.0;
-}
-
-static gchar *
-get_command_line (gchar **command)
-{
-       if (command[0] == NULL)
-               return NULL;
-
-       gchar *command_line = g_strdup (command[0]);
-       gchar *tmp;
-       gchar **arg = command;
-       arg++;
-       while (*arg != NULL)
-       {
-               tmp = g_strdup_printf ("%s %s", command_line, *arg);
-               g_free (command_line);
-               command_line = tmp;
-               arg++;
-       }
-       
-       return command_line;
-}
-
 static void
 command_running_finished (void)
 {
@@ -79,11 +44,6 @@ command_running_finished (void)
        while (gtk_events_pending ())
                gtk_main_iteration ();
 
-       // measure the time
-       struct timeval time_end;
-       gettimeofday (&time_end, NULL);
-       print_info ("execution time: %f", difftimeval (time_start, time_end));
-
        // unlock the action list
        gtk_widget_set_sensitive (GTK_WIDGET (latexila.action_log->list_view), TRUE);
 
@@ -143,16 +103,14 @@ cb_watch_output_command (GIOChannel *channel, GIOCondition condition,
 }
 
 void
-compile_document (gchar *title, gchar **command)
+compile_document (gchar *title, gchar *command)
 {
        if (latexila.active_doc == NULL)
                return;
 
        gchar *command_output;
 
-       gchar *command_line = get_command_line (command);
-       add_action (title, command_line);
-       g_free (command_line);
+       add_action (title, command);
 
        /* the current document is a *.tex file? */
        gboolean tex_file = g_str_has_suffix (latexila.active_doc->path, ".tex");
@@ -177,16 +135,21 @@ compile_document (gchar *title, gchar **command)
 
 
        /* run the command */
+       print_info ("execution of the command: %s", command);
 
-       // measure the time
-       gettimeofday (&time_start, NULL);
+       gchar *argv[] = {
+               "/bin/sh",
+               "-c",
+               command,
+               NULL
+       };
 
        gchar *dir = g_path_get_dirname (latexila.active_doc->path);
        GError *error = NULL;
        GPid pid;
-       gint out;
-       g_spawn_async_with_pipes (dir, command, NULL, G_SPAWN_SEARCH_PATH, NULL,
-                       NULL, &pid, NULL, &out, NULL, &error);
+       gint out, err;
+       g_spawn_async_with_pipes (dir, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
+                       NULL, &pid, NULL, &out, &err, &error);
        g_free (dir);
 
        // an error occured
@@ -203,12 +166,12 @@ compile_document (gchar *title, gchar **command)
 
        // create the channels
        GIOChannel *out_channel = g_io_channel_unix_new (out);
+       GIOChannel *err_channel = g_io_channel_unix_new (err);
 
        // the encoding of the output of the latex and the pdflatex commands is not
        // UTF-8...
        g_io_channel_set_encoding (out_channel, "ISO-8859-1", &error);
-
-       //g_io_channel_set_flags (out_channel, G_IO_FLAG_NONBLOCK, NULL);
+       g_io_channel_set_encoding (err_channel, "ISO-8859-1", &error);
 
        if (error != NULL)
        {
@@ -230,6 +193,8 @@ compile_document (gchar *title, gchar **command)
        // add watches to channels
        g_io_add_watch (out_channel, G_IO_IN | G_IO_HUP,
                        (GIOFunc) cb_watch_output_command, NULL);
+       g_io_add_watch (err_channel, G_IO_IN | G_IO_HUP,
+                       (GIOFunc) cb_watch_output_command, NULL);
 }
 
 void
@@ -313,9 +278,7 @@ convert_document (gchar *title, gchar *doc_extension, gchar *command)
        if (latexila.active_doc == NULL)
                return;
 
-       GError *error = NULL;
-       gchar *full_command, *command_output;
-       gboolean is_error = TRUE;
+       gchar *command_output;
 
        GRegex *regex = g_regex_new ("\\.tex$", 0, 0, NULL);
 
@@ -324,7 +287,7 @@ convert_document (gchar *title, gchar *doc_extension, gchar *command)
                        latexila.active_doc->path, -1, 0, doc_extension, 0, NULL);
        g_regex_unref (regex);
 
-       full_command = g_strdup_printf ("%s %s", command, doc_path);
+       gchar *full_command = g_strdup_printf ("%s %s", command, doc_path);
        add_action (title, full_command);
 
        /* the document to convert exist? */
@@ -333,14 +296,16 @@ convert_document (gchar *title, gchar *doc_extension, gchar *command)
                command_output = g_strdup_printf (
                                _("%s does not exist. If this is not already made, compile the document with 
the right command."),
                                g_path_get_basename (doc_path));
-               print_log_add (latexila.action_log->text_view, command_output, is_error);
+               print_log_add (latexila.action_log->text_view, command_output, TRUE);
 
-               g_free (full_command);
                g_free (command_output);
+               g_free (full_command);
                g_free (doc_path);
                return;
        }
 
+       g_free (doc_path);
+
        /* print a message in the statusbar */
        guint context_id = gtk_statusbar_get_context_id (latexila.statusbar,
                        "running-action");
@@ -351,33 +316,71 @@ convert_document (gchar *title, gchar *doc_extension, gchar *command)
        while (gtk_events_pending ())
                gtk_main_iteration ();
 
-       /* run the command in the directory where the .tex is */
+       /* run the command */
        print_info ("execution of the command: %s", full_command);
 
-       gchar *dir_backup = g_get_current_dir ();
+       gchar *argv[] = {
+               "/bin/sh",
+               "-c",
+               full_command,
+               NULL
+       };
+
+       GError *error = NULL;
        gchar *dir = g_path_get_dirname (latexila.active_doc->path);
-       g_chdir (dir);
-       g_spawn_command_line_sync (full_command, &command_output, NULL, NULL, &error);
-       g_chdir (dir_backup);
-       
+       GPid pid;
+       gint out, err;
+       g_spawn_async_with_pipes (dir, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
+                       NULL, &pid, NULL, &out, &err, &error);
+
+       g_free (dir);
+       g_free (full_command);
+
        // an error occured
        if (error != NULL)
        {
                command_output = g_strdup_printf (_("execution failed: %s"),
                                error->message);
+               print_log_add (latexila.action_log->text_view, command_output, TRUE);
+
+               g_free (command_output);
                g_error_free (error);
-               error = NULL;
+               return;
        }
-       else
-               is_error = FALSE;
 
-       print_log_add (latexila.action_log->text_view, command_output, is_error);
+       // create the channels
+       GIOChannel *out_channel = g_io_channel_unix_new (out);
+       GIOChannel *err_channel = g_io_channel_unix_new (err);
 
-       gtk_statusbar_pop (latexila.statusbar, context_id);
+       /*
+       // the encoding of the output of the latex and the pdflatex commands is not
+       // UTF-8...
+       g_io_channel_set_encoding (out_channel, "ISO-8859-1", &error);
+       g_io_channel_set_encoding (err_channel, "ISO-8859-1", &error);
+       
+       if (error != NULL)
+       {
+               command_output = g_strdup_printf (
+                               "conversion of the command output failed: %s", error->message);
+               print_log_add (latexila.action_log->text_view, command_output, TRUE);
 
-       g_free (full_command);
-       g_free (command_output);
-       g_free (doc_path);
+               g_free (command_output);
+               g_error_free (error);
+               return;
+       }
+       */
+
+       // Lock the action list so the user can not view an other action while the
+       // compilation is running.
+       // It will be unlock when the compilation is finished.
+       gtk_widget_set_sensitive (GTK_WIDGET (latexila.action_log->list_view),
+                       FALSE);
+
+       // add watches to channels
+       g_io_add_watch (out_channel, G_IO_IN | G_IO_HUP,
+                       (GIOFunc) cb_watch_output_command, NULL);
+       g_io_add_watch (err_channel, G_IO_IN | G_IO_HUP,
+                       (GIOFunc) cb_watch_output_command, NULL);
 }
 
 static void
diff --git a/src/external_commands.h b/src/external_commands.h
index a207049..b131aef 100644
--- a/src/external_commands.h
+++ b/src/external_commands.h
@@ -1,7 +1,7 @@
 #ifndef EXTERNAL_COMMANDS_H
 #define EXTERNAL_COMMANDS_H
 
-void compile_document (gchar *title, gchar **command);
+void compile_document (gchar *title, gchar *command);
 void view_document (gchar *title, gchar *doc_extension);
 void convert_document (gchar *title, gchar *doc_extension, gchar *command);
 


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