[latexila/wip/latexila-next] PostProcessorLatex: code clean-up



commit 94cc01ebc82fbcc842ec357b127b86dbb74fb7e5
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Sep 13 23:43:24 2014 +0200

    PostProcessorLatex: code clean-up

 src/liblatexila/latexila-post-processor-latex.c |  162 +++++++++++++----------
 1 files changed, 89 insertions(+), 73 deletions(-)
---
diff --git a/src/liblatexila/latexila-post-processor-latex.c b/src/liblatexila/latexila-post-processor-latex.c
index 7ea02e4..d46c0b5 100644
--- a/src/liblatexila/latexila-post-processor-latex.c
+++ b/src/liblatexila/latexila-post-processor-latex.c
@@ -51,20 +51,39 @@ typedef enum
   STATE_FILENAME_HEURISTIC
 } State;
 
-/* Opened file. They are present in a stack. It is used to know on which file an
- * error or warning occurred.
+/* File opened by TeX. It is used to know on which file an error or warning
+ * occurred. File's are pushed and popped in a stack.
  */
-typedef struct
+typedef struct _File File;
+struct _File
 {
   gchar *filename;
+
+  /* There are basically two ways to detect the current file TeX is processing:
+   * 1) Use \Input (srctex or srcltx package) and \include exclusively. This will
+   *    cause (La)TeX to print the line ":<+ filename" in the log file when opening
+   *    a file, ":<-" when closing a file. Filenames pushed on the stack in this mode
+   *    are marked as reliable.
+   *
+   * 2) Since people will probably also use the \input command, we also have to
+   *    detect the old-fashioned way. TeX prints '(filename' when opening a file
+   *    and a ')' when closing one. It is impossible to detect this with 100%
+   *    certainty (TeX prints many messages and even text (a context) from the
+   *    TeX source file, there could be unbalanced parentheses), so we use an
+   *    heuristic algorithm. In heuristic mode a ')' will only be considered as
+   *    a signal that TeX is closing a file if the top of the stack is not
+   *    marked as "reliable".
+   *
+   * The method used here is almost the same as in Kile.
+   */
   guint reliable : 1;
 
   /* Non-existent files are also pushed on the stack, because the corresponding
-   * ')' will pop it. If we don't push them, wrong files are popped.
+   * ')' will pop them. If we don't push them, wrong files are popped.
    * When a new message is added, the last _existing_ file is taken.
    */
   guint exists : 1;
-} OpenedFile;
+};
 
 struct _LatexilaPostProcessorLatexPrivate
 {
@@ -82,24 +101,27 @@ struct _LatexilaPostProcessorLatexPrivate
    * line_buffer.
    */
   GString *line_buffer;
-  gint nb_lines;
+
+  /* Number of lines in @line_buffer. */
+  gint lines_count;
 
   /* If a filename is split into several lines. */
   GString *filename_buffer;
 
   /* The stack containing the files that TeX is processing. The top of the stack
    * is the beginning of the list.
-   * Elements type: pointer to OpenedFile
+   * Elements type: pointer to File
    */
   GSList *stack_files;
 
   /* The directory where the document is compiled. */
+  /* FIXME why not storing the GFile? */
   gchar *directory_path;
 
   /* For statistics. */
-  gint nb_badboxes;
-  gint nb_warnings;
-  gint nb_errors;
+  gint badboxes_count;
+  gint warnings_count;
+  gint errors_count;
 };
 
 G_DEFINE_TYPE_WITH_PRIVATE (LatexilaPostProcessorLatex,
@@ -107,18 +129,38 @@ G_DEFINE_TYPE_WITH_PRIVATE (LatexilaPostProcessorLatex,
                             LATEXILA_TYPE_POST_PROCESSOR)
 
 #if 0
-static OpenedFile *
-opened_file_new (void)
+static File *
+file_new (void)
 {
-  return g_slice_new0 (OpenedFile);
+  return g_slice_new0 (File);
 }
 #endif
 
 static void
-opened_file_free (OpenedFile *opened_file)
+file_free (File *file)
+{
+  if (file != NULL)
+    g_slice_free (File, file);
+}
+
+static void
+set_line_buffer (LatexilaPostProcessorLatex *pp,
+                 const gchar                *line)
 {
-  if (opened_file != NULL)
-    g_slice_free (OpenedFile, opened_file);
+  if (pp->priv->line_buffer != NULL)
+    g_string_free (pp->priv->line_buffer, TRUE);
+
+  pp->priv->line_buffer = g_string_new (new_content);
+  pp->priv->lines_count = 1;
+}
+
+static void
+append_to_line_buffer (LatexilaPostProcessorLatex *pp,
+                       const gchar                *line)
+{
+  g_return_if_fail (pp->priv->line_buffer != NULL);
+  g_string_append (pp->priv->line_buffer, line);
+  pp->priv->lines_count++;
 }
 
 static gchar *
@@ -128,7 +170,7 @@ get_current_filename (LatexilaPostProcessorLatex *pp)
 
   for (l = pp->priv->stack_files; l != NULL; l = l->next)
     {
-      OpenedFile *file = l->data;
+      File *file = l->data;
 
       /* TODO check lazily if the file exists */
       if (file->exists)
@@ -205,15 +247,15 @@ add_message (LatexilaPostProcessorLatex *pp,
   switch (cur_msg->type)
     {
     case LATEXILA_BUILD_MSG_TYPE_BADBOX:
-      pp->priv->nb_badboxes++;
+      pp->priv->badboxes_count++;
       break;
 
     case LATEXILA_BUILD_MSG_TYPE_WARNING:
-      pp->priv->nb_warnings++;
+      pp->priv->warnings_count++;
       break;
 
     case LATEXILA_BUILD_MSG_TYPE_ERROR:
-      pp->priv->nb_errors++;
+      pp->priv->errors_count++;
       break;
 
     default:
@@ -255,12 +297,12 @@ latexila_post_processor_latex_end (LatexilaPostProcessor *post_processor)
    */
   cur_msg->type = LATEXILA_BUILD_MSG_TYPE_INFO;
   cur_msg->text = g_strdup_printf ("%d %s, %d %s, %d %s",
-                                   pp->priv->nb_errors,
-                                   pp->priv->nb_errors == 1 ? "error" : "errors",
-                                   pp->priv->nb_warnings,
-                                   pp->priv->nb_warnings == 1 ? "warning" : "warnings",
-                                   pp->priv->nb_badboxes,
-                                   pp->priv->nb_badboxes == 1 ? "badbox" : "badboxes");
+                                   pp->priv->errors_count,
+                                   pp->priv->errors_count == 1 ? "error" : "errors",
+                                   pp->priv->warnings_count,
+                                   pp->priv->warnings_count == 1 ? "warning" : "warnings",
+                                   pp->priv->badboxes_count,
+                                   pp->priv->badboxes_count == 1 ? "badbox" : "badboxes");
 
   add_message (pp, FALSE);
 }
@@ -389,7 +431,7 @@ detect_badbox_line (LatexilaPostProcessorLatex *pp,
       return TRUE;
     }
 
-  if (pp->priv->nb_lines > 4 || current_line_is_empty)
+  if (pp->priv->lines_count > 4 || current_line_is_empty)
     {
       pp->priv->state = STATE_START;
 
@@ -436,30 +478,21 @@ detect_badbox (LatexilaPostProcessorLatex *pp,
       pp->priv->cur_msg->type = LATEXILA_BUILD_MSG_TYPE_BADBOX;
 
       if (detect_badbox_line (pp, line, FALSE))
-        {
-          add_message (pp, TRUE);
-        }
+        add_message (pp, TRUE);
       else
-        {
-          if (pp->priv->line_buffer != NULL)
-            g_string_free (pp->priv->line_buffer, TRUE);
-
-          pp->priv->line_buffer = g_string_new (line);
-          pp->priv->nb_lines++;
-        }
+        set_line_buffer (pp, line);
 
       return TRUE;
 
     case STATE_BADBOX:
-      g_string_append (pp->priv->line_buffer, line);
-      pp->priv->nb_lines++;
+      append_to_line_buffer (pp, line);
 
       if (detect_badbox_line (pp,
                               pp->priv->line_buffer->str,
                               line[0] == '\0'))
         {
           add_message (pp, TRUE);
-          pp->priv->nb_lines = 0;
+          pp->priv->lines_count = 0;
         }
 
       /* The return value is not important here. */
@@ -546,7 +579,7 @@ detect_warning_line (LatexilaPostProcessorLatex *pp,
     }
 
   len = strlen (warning);
-  if (warning[len-1] == '.' || pp->priv->nb_lines > 5 || current_line_is_empty)
+  if (warning[len-1] == '.' || pp->priv->lines_count > 5 || current_line_is_empty)
     {
       pp->priv->state = STATE_START;
 
@@ -634,7 +667,7 @@ detect_warning (LatexilaPostProcessorLatex *pp,
                 g_string_free (pp->priv->line_buffer, TRUE);
 
               pp->priv->line_buffer = g_string_new (contents);
-              pp->priv->nb_lines++;
+              pp->priv->lines_count++;
             }
 
           g_free (contents);
@@ -669,14 +702,14 @@ detect_warning (LatexilaPostProcessorLatex *pp,
 
     case STATE_WARNING:
       g_string_append (pp->priv->line_buffer, line);
-      pp->priv->nb_lines++;
+      pp->priv->lines_count++;
 
       if (detect_warning_line (pp,
                                pp->priv->line_buffer->str,
                                line[0] == '\0'))
         {
           add_message (pp, TRUE);
-          pp->priv->nb_lines = 0;
+          pp->priv->lines_count = 0;
         }
 
       /* The return value is not important here. */
@@ -792,7 +825,7 @@ detect_error (LatexilaPostProcessorLatex *pp,
 
       if (found)
         {
-          pp->priv->nb_lines++;
+          pp->priv->lines_count++;
           cur_msg->type = LATEXILA_BUILD_MSG_TYPE_ERROR;
 
           len = strlen (line);
@@ -825,7 +858,7 @@ detect_error (LatexilaPostProcessorLatex *pp,
 
     case STATE_ERROR:
       g_string_append (pp->priv->line_buffer, line);
-      pp->priv->nb_lines++;
+      pp->priv->lines_count++;
 
       len = strlen (line);
 
@@ -837,7 +870,7 @@ detect_error (LatexilaPostProcessorLatex *pp,
 
           pp->priv->state = STATE_ERROR_SEARCH_LINE;
         }
-      else if (pp->priv->nb_lines > 4)
+      else if (pp->priv->lines_count > 4)
         {
           g_free (cur_msg->text);
           cur_msg->text = g_string_free (pp->priv->line_buffer, FALSE);
@@ -847,7 +880,7 @@ detect_error (LatexilaPostProcessorLatex *pp,
 
           add_message (pp, TRUE);
 
-          pp->priv->nb_lines = 0;
+          pp->priv->lines_count = 0;
           pp->priv->state = STATE_START;
         }
 
@@ -855,7 +888,7 @@ detect_error (LatexilaPostProcessorLatex *pp,
       return TRUE;
 
     case STATE_ERROR_SEARCH_LINE:
-      pp->priv->nb_lines++;
+      pp->priv->lines_count++;
 
       if (g_regex_match (regex_error_line, line, 0, NULL))
         {
@@ -864,17 +897,17 @@ detect_error (LatexilaPostProcessorLatex *pp,
 
           add_message (pp, TRUE);
 
-          pp->priv->nb_lines = 0;
+          pp->priv->lines_count = 0;
           pp->priv->state = STATE_START;
 
           g_strfreev (strings);
           return TRUE;
         }
-      else if (pp->priv->nb_lines > 11)
+      else if (pp->priv->lines_count > 11)
         {
           cur_msg->start_line = NO_LINE;
           add_message (pp, TRUE);
-          pp->priv->nb_lines = 0;
+          pp->priv->lines_count = 0;
           pp->priv->state = STATE_START;
           return TRUE;
         }
@@ -975,8 +1008,8 @@ pop_file_from_stack (LatexilaPostProcessorLatex *pp)
 {
   if (pp->priv->stack_files != NULL)
     {
-      OpenedFile *opened_file = pp->priv->stack_files->data;
-      opened_file_free (opened_file);
+      File *file = pp->priv->stack_files->data;
+      file_free (file);
     }
 
   pp->priv->stack_files = g_slist_remove_link (pp->priv->stack_files,
@@ -1134,22 +1167,6 @@ update_stack_file_heuristic (LatexilaPostProcessorLatex *pp,
     }
 }
 
-/* There are basically two ways to detect the current file TeX is processing:
- * 1) Use \Input (srctex or srcltx package) and \include exclusively. This will
- *    cause (La)TeX to print the line ":<+ filename" in the log file when opening
- *    a file, ":<-" when closing a file. Filenames pushed on the stack in this mode
- *    are marked as reliable.
- *
- * 2) Since people will probably also use the \input command, we also have to be
- *    to detect the old-fashioned way. TeX prints '(filename' when opening a file
- *    and a ')' when closing one. It is impossible to detect this with 100% certainty
- *    (TeX prints many messages and even text (a context) from the TeX source file,
- *    there could be unbalanced parentheses), so we use an heuristic algorithm.
- *    In heuristic mode a ')' will only be considered as a signal that TeX is closing
- *    a file if the top of the stack is not marked as "reliable".
- *
- * The method used here is almost the same as in Kile.
- */
 static void
 update_stack_file (LatexilaPostProcessorLatex *pp,
                    const gchar                *line)
@@ -1283,8 +1300,7 @@ process_line (LatexilaPostProcessorLatex *pp,
       break;
 
     default:
-      pp->priv->state = STATE_START;
-      break;
+      g_return_if_reached ();
     }
 }
 
@@ -1328,7 +1344,7 @@ latexila_post_processor_latex_finalize (GObject *object)
   if (pp->priv->filename_buffer != NULL)
     g_string_free (pp->priv->filename_buffer, TRUE);
 
-  g_slist_free_full (pp->priv->stack_files, (GDestroyNotify) opened_file_free);
+  g_slist_free_full (pp->priv->stack_files, (GDestroyNotify) file_free);
 
   g_free (pp->priv->directory_path);
 


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