[gnome-latex: 84/205] Other method for the log zone



commit 8ab3f0027866ff407014f22c99360923902dc813
Author: Sébastien Wilmet <sebastien wilmet gmail com>
Date:   Mon Oct 5 00:14:18 2009 +0200

    Other method for the log zone
    
    For each action (compile, view or convert a document) there is a new
    entry in the list, which contain now only 2 things: the title, and a
    pointer to a GtkTextBuffer, which contain all the details (command
    output, etc). So when we select an other entry in the list, we set the
    right GtkTextBuffer to the GtkTextView. When we do an new action, a new
    GtkTextBuffer is created and filled.

 TODO                                   |   2 +-
 src/CMakeLists.txt                     |   2 +-
 src/callbacks.c                        |  14 ++-
 src/{actions.c => external_commands.c} | 157 +++++++++++++--------------------
 src/{actions.h => external_commands.h} |   6 +-
 src/main.c                             |   3 +-
 src/main.h                             |   5 +-
 7 files changed, 74 insertions(+), 115 deletions(-)
---
diff --git a/TODO b/TODO
index 0c1da39..20036b4 100644
--- a/TODO
+++ b/TODO
@@ -7,7 +7,7 @@ Fri Oct 2, 2009 to Fri Oct 9, 2009
 
 [-] execution of the actions
        - verify that the method used for compile_document () is correct
-       - optimisations?
+       - optimizations?
        - same behavior than compile_document () for:
                * convert_document ()
                * view_document ()
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 91b3da0..076da59 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -3,7 +3,7 @@ SET(latexila_src main.c                 main.h
                 cb_latex.c             cb_latex.h
                 print.c                print.h
                 symbols.c              symbols.h
-                actions.c              actions.h
+                external_commands.c    external_commands.h
                 prefs.c                prefs.h
                 ui.c                   ui.h
                 tool_menu_action.c     tool_menu_action.h)
diff --git a/src/callbacks.c b/src/callbacks.c
index 5786cd7..93c8004 100644
--- a/src/callbacks.c
+++ b/src/callbacks.c
@@ -34,7 +34,7 @@
 #include "config.h"
 #include "callbacks.h"
 #include "print.h"
-#include "actions.h"
+#include "external_commands.h"
 #include "prefs.h"
 
 static void create_document_in_new_tab (const gchar *path, const gchar *text,
@@ -617,16 +617,12 @@ cb_action_list_changed (GtkTreeSelection *selection, gpointer user_data)
        GtkTreeModel *model;
        if (gtk_tree_selection_get_selected (selection, &model, &iter))
        {
-               gchar *title, *command, *command_output;
-               gboolean error;
+               GtkTextBuffer *text_buffer;
                gtk_tree_model_get (model, &iter,
-                               COLUMN_ACTION_TITLE, &title,
-                               COLUMN_ACTION_COMMAND, &command,
-                               COLUMN_ACTION_COMMAND_OUTPUT, &command_output,
-                               COLUMN_ACTION_ERROR, &error,
+                               COLUMN_ACTION_TEXTBUFFER, &text_buffer,
                                -1);
-               print_log (latexila.action_log->text_buffer, title, command,
-                               command_output, error);
+
+               gtk_text_view_set_buffer (latexila.action_log->text_view, text_buffer);
        }
 }
 
diff --git a/src/actions.c b/src/external_commands.c
similarity index 75%
rename from src/actions.c
rename to src/external_commands.c
index 4518abb..825afd2 100644
--- a/src/actions.c
+++ b/src/external_commands.c
@@ -31,7 +31,7 @@
 
 #include "main.h"
 #include "config.h"
-#include "actions.h"
+#include "external_commands.h"
 #include "print.h"
 
 static double difftimeval (struct timeval start, struct timeval end);
@@ -39,12 +39,9 @@ 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, gchar *command_output,
-               gboolean error);
+static void add_action (gchar *title, gchar *command);
 
-static GSList *command_output_list = NULL;
 static struct timeval time_start;
-static int nb_lines = 0;
 
 static double
 difftimeval (struct timeval start, struct timeval end)
@@ -78,32 +75,14 @@ get_command_line (gchar **command)
 static void
 command_running_finished (void)
 {
-       // build the string containing all the lines of the command output
-       gchar *command_output_string = g_strdup ("");
-       gchar *tmp;
-       command_output_list = g_slist_reverse (command_output_list);
+       // the magic formula
+       while (gtk_events_pending ())
+               gtk_main_iteration ();
 
-       GSList *current = command_output_list;
-       do
-       {
-               gchar *line = g_slist_nth_data (current, 0);
-               tmp = g_strdup_printf ("%s%s", command_output_string, line);
-               g_free (command_output_string);
-               command_output_string = tmp;
-       }
-       while ((current = g_slist_next (current)) != NULL);
-       
-       // store the string to the action list store
-       GtkTreeIter iter;
-       if (gtk_tree_selection_get_selected (latexila.action_log->list_selection,
-                               NULL, &iter))
-       {
-               gtk_list_store_set (latexila.action_log->list_store, &iter,
-                               COLUMN_ACTION_COMMAND_OUTPUT, command_output_string,
-                               -1);
-       }
-       else
-               print_warning ("no action selected in the list => the output command is not saved");
+       // 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);
@@ -112,30 +91,19 @@ command_running_finished (void)
        guint context_id = gtk_statusbar_get_context_id (latexila.statusbar,
                        "running-action");
        gtk_statusbar_pop (latexila.statusbar, context_id);
-
-       g_free (command_output_string);
-       g_slist_foreach (command_output_list, (GFunc) g_free, NULL);
-       g_slist_free (command_output_list);
-       command_output_list = NULL;
-       nb_lines = 0;
 }
 
 static gboolean
 cb_watch_output_command (GIOChannel *channel, GIOCondition condition,
                gpointer user_data)
 {
+       static int nb_lines = 0;
+
        if (condition == G_IO_HUP)
        {
-               struct timeval time_end;
-               gettimeofday (&time_end, NULL);
-               print_info ("time: %f", difftimeval (time_start, time_end));
-
-               // the magic formula
-               while (gtk_events_pending ())
-                       gtk_main_iteration ();
-
                g_io_channel_unref (channel);
                command_running_finished ();
+               nb_lines = 0;
                return FALSE;
        }
        
@@ -154,11 +122,6 @@ cb_watch_output_command (GIOChannel *channel, GIOCondition condition,
        {
                // print the command output line to the log zone
                print_log_add (latexila.action_log->text_view, line, FALSE);
-
-               // store temporarily the line to the GList
-               // We insert the line at the beginning of the list, so we avoid to traverse
-               // the entire list. The list is reversed when all elements have been added.
-               command_output_list = g_slist_prepend (command_output_list, line);
        }
        
        /* Apply the magic formula for the 200 first lines and then every 20 lines.
@@ -185,9 +148,11 @@ compile_document (gchar *title, gchar **command)
        if (latexila.active_doc == NULL)
                return;
 
-       gchar *command_line = get_command_line (command);
        gchar *command_output;
-       gboolean is_error = TRUE;
+
+       gchar *command_line = get_command_line (command);
+       add_action (title, command_line);
+       g_free (command_line);
 
        /* the current document is a *.tex file? */
        gboolean tex_file = g_str_has_suffix (latexila.active_doc->path, ".tex");
@@ -195,10 +160,8 @@ compile_document (gchar *title, gchar **command)
        {
                command_output = g_strdup_printf (_("compilation failed: %s is not a *.tex file"),
                                g_path_get_basename (latexila.active_doc->path));
-
-               add_action (title, command_line, command_output, is_error);
+               print_log_add (latexila.action_log->text_view, command_output, TRUE);
                g_free (command_output);
-               g_free (command_line);
                return;
        }
 
@@ -212,9 +175,10 @@ compile_document (gchar *title, gchar **command)
        while (gtk_events_pending ())
                gtk_main_iteration ();
 
+
        /* run the command */
 
-       // time
+       // measure the time
        gettimeofday (&time_start, NULL);
 
        gchar *dir = g_path_get_dirname (latexila.active_doc->path);
@@ -223,18 +187,17 @@ compile_document (gchar *title, gchar **command)
        gint out;
        g_spawn_async_with_pipes (dir, command, NULL, G_SPAWN_SEARCH_PATH, NULL,
                        NULL, &pid, NULL, &out, NULL, &error);
+       g_free (dir);
 
        // an error occured
        if (error != NULL)
        {
                command_output = g_strdup_printf (_("execution failed: %s"),
                                error->message);
-               add_action (title, command_line, command_output, is_error);
+               print_log_add (latexila.action_log->text_view, command_output, TRUE);
 
                g_free (command_output);
-               g_free (command_line);
                g_error_free (error);
-               error = NULL;
                return;
        }
 
@@ -251,38 +214,22 @@ compile_document (gchar *title, gchar **command)
        {
                command_output = g_strdup_printf (
                                "conversion of the command output failed: %s", error->message);
-               add_action (title, command_line, command_output, is_error);
+               print_log_add (latexila.action_log->text_view, command_output, TRUE);
 
                g_free (command_output);
-               g_free (command_line);
                g_error_free (error);
-               error = NULL;
                return;
        }
 
-       is_error = FALSE;
-
        // 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_action (title, command_line, "", is_error);
-
-       // All the lines of the command output will be store temporarily in a GSList.
-       // When the command is finished, the GSList is traversed and the full string
-       // is store to the action list.
-       // We use here a GSList and not a GArray or a GList because insertions
-       // of new elements must be as fast as possible, and the list is iterated in
-       // only one direction.
-
        // add watches to channels
        g_io_add_watch (out_channel, G_IO_IN | G_IO_HUP,
                        (GIOFunc) cb_watch_output_command, NULL);
-
-       g_free (command_line);
-       g_free (dir);
 }
 
 void
@@ -291,30 +238,32 @@ view_document (gchar *title, gchar *doc_extension)
        if (latexila.active_doc == NULL)
                return;
 
-       gchar *command, *command_output;
-       gboolean is_error = TRUE;
-
        GError *error = NULL;
+       gchar *command_output;
+
        GRegex *regex = g_regex_new ("\\.tex$", 0, 0, NULL);
 
        /* replace .tex by doc_extension (.pdf, .dvi, ...) */
        gchar *doc_path = g_regex_replace_literal (regex, latexila.active_doc->path,
                        -1, 0, doc_extension, 0, NULL);
 
-       command = g_strdup_printf ("%s %s", latexila.prefs->command_view, doc_path);
+       gchar *command = g_strdup_printf ("%s %s", latexila.prefs->command_view,
+                       doc_path);
+       add_action (title, command);
 
        /* the current document is a *.tex file? */
        gboolean tex_file = g_regex_match (regex, latexila.active_doc->path, 0, NULL);
+       g_regex_unref (regex);
+
        if (! tex_file)
        {
                command_output = g_strdup_printf (_("failed: %s is not a *.tex file"),
                                g_path_get_basename (latexila.active_doc->path));
+               print_log_add (latexila.action_log->text_view, command_output, TRUE);
 
-               add_action (title, command, command_output, is_error);
                g_free (command);
                g_free (command_output);
                g_free (doc_path);
-               g_regex_unref (regex);
                return;
        }
 
@@ -324,12 +273,11 @@ view_document (gchar *title, gchar *doc_extension)
                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, TRUE);
 
-               add_action (title, command, command_output, is_error);
                g_free (command);
                g_free (command_output);
                g_free (doc_path);
-               g_regex_unref (regex);
                return;
        }
 
@@ -337,6 +285,8 @@ view_document (gchar *title, gchar *doc_extension)
        print_info ("execution of the command: %s", command);
        g_spawn_command_line_async (command, &error);
 
+       gboolean is_error = TRUE;
+
        if (error != NULL)
        {
                command_output = g_strdup_printf (_("execution failed: %s"),
@@ -350,12 +300,11 @@ view_document (gchar *title, gchar *doc_extension)
                is_error = FALSE;
        }
 
-       add_action (title, command, command_output, is_error);
+       print_log_add (latexila.action_log->text_view, command_output, is_error);
 
        g_free (command);
        g_free (command_output);
        g_free (doc_path);
-       g_regex_unref (regex);
 }
 
 void
@@ -364,17 +313,19 @@ 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;
 
-       GError *error = NULL;
        GRegex *regex = g_regex_new ("\\.tex$", 0, 0, NULL);
 
        /* replace .tex by doc_extension (.pdf, .dvi, ...) */
        gchar *doc_path = g_regex_replace_literal (regex,
                        latexila.active_doc->path, -1, 0, doc_extension, 0, NULL);
+       g_regex_unref (regex);
 
        full_command = g_strdup_printf ("%s %s", command, doc_path);
+       add_action (title, full_command);
 
        /* the document to convert exist? */
        if (! g_file_test (doc_path, G_FILE_TEST_IS_REGULAR))
@@ -382,12 +333,11 @@ 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);
 
-               add_action (title, full_command, command_output, is_error);
                g_free (full_command);
                g_free (command_output);
                g_free (doc_path);
-               g_regex_unref (regex);
                return;
        }
 
@@ -421,34 +371,48 @@ convert_document (gchar *title, gchar *doc_extension, gchar *command)
        else
                is_error = FALSE;
 
-       add_action (title, full_command, command_output, is_error);
+       print_log_add (latexila.action_log->text_view, command_output, is_error);
 
        gtk_statusbar_pop (latexila.statusbar, context_id);
 
        g_free (full_command);
        g_free (command_output);
        g_free (doc_path);
-       g_regex_unref (regex);
 }
 
 static void
-add_action (gchar *title, gchar *command, gchar *command_output, gboolean error)
+add_action (gchar *title, gchar *command)
 {
        static gint num = 1;
        gchar *title2 = g_strdup_printf ("%d. %s", num, title);
 
+       // create a new text buffer
+       GtkTextBuffer *new_text_buffer = gtk_text_buffer_new (
+                       latexila.action_log->tag_table);
+       latexila.action_log->text_buffer = new_text_buffer;
+       gtk_text_view_set_buffer (latexila.action_log->text_view, new_text_buffer);
+
+       // title
+       GtkTextIter end;
+       gtk_text_buffer_get_end_iter (new_text_buffer, &end);
+       gtk_text_buffer_insert_with_tags_by_name (new_text_buffer, &end, title2, -1,
+                       "bold", NULL);
+
+       // command
+    gtk_text_buffer_get_end_iter (new_text_buffer, &end);
+    gchar *command2 = g_strdup_printf ("\n$ %s\n", command);
+    gtk_text_buffer_insert (new_text_buffer, &end, command2, -1);
+    g_free (command2);
+
        // append an new entry to the action list
        GtkTreeIter iter;
        gtk_list_store_append (latexila.action_log->list_store, &iter);
        gtk_list_store_set (latexila.action_log->list_store, &iter,
                        COLUMN_ACTION_TITLE, title2,
-                       COLUMN_ACTION_COMMAND, command,
-                       COLUMN_ACTION_COMMAND_OUTPUT, command_output,
-                       COLUMN_ACTION_ERROR, error,
+                       COLUMN_ACTION_TEXTBUFFER, new_text_buffer,
                        -1);
 
-       // the new entry is selected
-       // cb_action_list_changed () is called, so the details are showed
+       // select the new entry
        gtk_tree_selection_select_iter (latexila.action_log->list_selection, &iter);
 
        // scroll to the end
@@ -460,4 +424,3 @@ add_action (gchar *title, gchar *command, gchar *command_output, gboolean error)
        num++;
        g_free (title2);
 }
-
diff --git a/src/actions.h b/src/external_commands.h
similarity index 68%
rename from src/actions.h
rename to src/external_commands.h
index f7e2a29..a207049 100644
--- a/src/actions.h
+++ b/src/external_commands.h
@@ -1,8 +1,8 @@
-#ifndef ACTIONS_H
-#define ACTIONS_H
+#ifndef EXTERNAL_COMMANDS_H
+#define EXTERNAL_COMMANDS_H
 
 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);
 
-#endif /* ACTIONS_H */
+#endif /* EXTERNAL_COMMANDS_H */
diff --git a/src/main.c b/src/main.c
index 6d697ce..eed6c4f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -208,7 +208,7 @@ main (int argc, char *argv[])
 
        // action history
        GtkListStore *list_store = gtk_list_store_new (N_COLUMNS_ACTION,
-                       G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN);
+                       G_TYPE_STRING, G_TYPE_POINTER);
        latexila.action_log->list_store = list_store;
        
        GtkWidget *list_view = gtk_tree_view_new_with_model (
@@ -261,6 +261,7 @@ main (int argc, char *argv[])
        gtk_text_buffer_create_tag (log_buffer, "error",
                        "foreground", "red",
                        NULL);
+       latexila.action_log->tag_table = gtk_text_buffer_get_tag_table (log_buffer);
 
        /* statusbar */
        GtkWidget *statusbar = gtk_statusbar_new ();
diff --git a/src/main.h b/src/main.h
index 31080e1..78f4824 100644
--- a/src/main.h
+++ b/src/main.h
@@ -51,6 +51,7 @@ typedef struct
        GtkTreeSelection        *list_selection;
        GtkTextView                     *text_view;
        GtkTextBuffer           *text_buffer;
+       GtkTextTagTable         *tag_table;
 } action_log_t;
 
 // symbols tables
@@ -110,9 +111,7 @@ extern latexila_t latexila;
 enum action
 {
        COLUMN_ACTION_TITLE,
-       COLUMN_ACTION_COMMAND,
-       COLUMN_ACTION_COMMAND_OUTPUT,
-       COLUMN_ACTION_ERROR,
+       COLUMN_ACTION_TEXTBUFFER,
        N_COLUMNS_ACTION
 };
 


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