[latexila/wip/latexila-next] PostProcessorLatex: code clean-up
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [latexila/wip/latexila-next] PostProcessorLatex: code clean-up
- Date: Wed, 17 Sep 2014 14:00:30 +0000 (UTC)
commit c579ceea4c00b0ab22bf7124f16cf107e9259bf8
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 | 206 +++++++++++------------
1 files changed, 101 insertions(+), 105 deletions(-)
---
diff --git a/src/liblatexila/latexila-post-processor-latex.c b/src/liblatexila/latexila-post-processor-latex.c
index 7ea02e4..cc7dfab 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 (pp->priv->line_buffer != NULL)
+ g_string_free (pp->priv->line_buffer, TRUE);
+
+ pp->priv->line_buffer = g_string_new (line);
+ pp->priv->lines_count = 1;
+}
+
+static void
+append_to_line_buffer (LatexilaPostProcessorLatex *pp,
+ const gchar *line)
{
- if (opened_file != NULL)
- g_slice_free (OpenedFile, opened_file);
+ 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)
@@ -154,7 +196,7 @@ add_message (LatexilaPostProcessorLatex *pp,
g_strcmp0 (cur_msg->text, "There were undefined references.") == 0)
{
latexila_build_msg_reinit (cur_msg);
- return;
+ goto end;
}
if (set_filename)
@@ -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:
@@ -225,6 +267,17 @@ add_message (LatexilaPostProcessorLatex *pp,
cur_msg);
pp->priv->cur_msg = latexila_build_msg_new ();
+
+end:
+ pp->priv->state = STATE_START;
+
+ if (pp->priv->line_buffer != NULL)
+ {
+ g_string_free (pp->priv->line_buffer, TRUE);
+ pp->priv->line_buffer = NULL;
+ }
+
+ pp->priv->lines_count = 0;
}
static void
@@ -255,12 +308,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 +442,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,31 +489,17 @@ 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++;
-
if (detect_badbox_line (pp,
pp->priv->line_buffer->str,
line[0] == '\0'))
- {
- add_message (pp, TRUE);
- pp->priv->nb_lines = 0;
- }
+ add_message (pp, TRUE);
/* The return value is not important here. */
return TRUE;
@@ -546,7 +585,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;
@@ -625,17 +664,9 @@ detect_warning (LatexilaPostProcessorLatex *pp,
}
if (detect_warning_line (pp, contents, 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 (contents);
- pp->priv->nb_lines++;
- }
+ set_line_buffer (pp, contents);
g_free (contents);
g_free (name);
@@ -668,16 +699,10 @@ detect_warning (LatexilaPostProcessorLatex *pp,
return FALSE;
case STATE_WARNING:
- g_string_append (pp->priv->line_buffer, line);
- pp->priv->nb_lines++;
-
if (detect_warning_line (pp,
pp->priv->line_buffer->str,
line[0] == '\0'))
- {
- add_message (pp, TRUE);
- pp->priv->nb_lines = 0;
- }
+ add_message (pp, TRUE);
/* The return value is not important here. */
return TRUE;
@@ -792,7 +817,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);
@@ -809,12 +834,8 @@ detect_error (LatexilaPostProcessorLatex *pp,
/* The message is split into several lines. */
else
{
- if (pp->priv->line_buffer != NULL)
- g_string_free (pp->priv->line_buffer, TRUE);
-
- pp->priv->line_buffer = g_string_new (msg);
+ set_line_buffer (pp, msg);
pp->priv->state = STATE_ERROR;
-
g_free (msg);
}
@@ -824,9 +845,6 @@ detect_error (LatexilaPostProcessorLatex *pp,
return FALSE;
case STATE_ERROR:
- g_string_append (pp->priv->line_buffer, line);
- pp->priv->nb_lines++;
-
len = strlen (line);
if (line[len-1] == '.')
@@ -835,9 +853,11 @@ detect_error (LatexilaPostProcessorLatex *pp,
cur_msg->text = g_string_free (pp->priv->line_buffer, FALSE);
pp->priv->line_buffer = NULL;
+ /* FIXME set lines_count to 0? */
+
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);
@@ -846,17 +866,12 @@ detect_error (LatexilaPostProcessorLatex *pp,
cur_msg->start_line = NO_LINE;
add_message (pp, TRUE);
-
- pp->priv->nb_lines = 0;
- pp->priv->state = STATE_START;
}
/* The return value is not important here. */
return TRUE;
case STATE_ERROR_SEARCH_LINE:
- pp->priv->nb_lines++;
-
if (g_regex_match (regex_error_line, line, 0, NULL))
{
gchar **strings = g_regex_split (regex_error_line, line, 0);
@@ -864,18 +879,13 @@ detect_error (LatexilaPostProcessorLatex *pp,
add_message (pp, TRUE);
- pp->priv->nb_lines = 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->state = STATE_START;
return TRUE;
}
break;
@@ -975,8 +985,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 +1144,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)
@@ -1251,6 +1245,9 @@ process_line (LatexilaPostProcessorLatex *pp,
{
g_assert (line != NULL);
+ if (pp->priv->state != STATE_START)
+ append_to_line_buffer (pp, line);
+
switch (pp->priv->state)
{
case STATE_START:
@@ -1283,8 +1280,7 @@ process_line (LatexilaPostProcessorLatex *pp,
break;
default:
- pp->priv->state = STATE_START;
- break;
+ g_return_if_reached ();
}
}
@@ -1328,7 +1324,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]