SF.net SVN: geany-plugins:[1886] trunk/geanylatex

frlan at users.sourceforge.net frlan at xxxxx
Sat Jan 29 13:40:55 UTC 2011


Revision: 1886
          http://geany-plugins.svn.sourceforge.net/geany-plugins/?rev=1886&view=rev
Author:   frlan
Date:     2011-01-29 13:40:55 +0000 (Sat, 29 Jan 2011)

Log Message:
-----------
GeanyLaTeX: Backport update from convert to table function from HTMLTable-plugin

Modified Paths:
--------------
    trunk/geanylatex/ChangeLog
    trunk/geanylatex/src/latexkeybindings.c
    trunk/geanylatex/src/latexutils.c
    trunk/geanylatex/src/latexutils.h

Modified: trunk/geanylatex/ChangeLog
===================================================================
--- trunk/geanylatex/ChangeLog	2011-01-29 13:40:33 UTC (rev 1885)
+++ trunk/geanylatex/ChangeLog	2011-01-29 13:40:55 UTC (rev 1886)
@@ -1,3 +1,8 @@
+2011-01-29  Frank Lanitz  <frank(at)frank(dot)uvena(dot)de>
+
+ * Backport update from convert to table function from HTMLTable-plugin
+
+
 2011-01-23  Frank Lanitz  <frank(at)frank(dot)uvena(dot)de>
 
  * Adding a feature which is converting a tabulator separated list into

Modified: trunk/geanylatex/src/latexkeybindings.c
===================================================================
--- trunk/geanylatex/src/latexkeybindings.c	2011-01-29 13:40:33 UTC (rev 1885)
+++ trunk/geanylatex/src/latexkeybindings.c	2011-01-29 13:40:55 UTC (rev 1886)
@@ -30,7 +30,7 @@
 
 void glatex_kbref_insert(G_GNUC_UNUSED guint key_id)
 {
-	g_return_if_fail(document_get_current() != NULL);	
+	g_return_if_fail(document_get_current() != NULL);
 	glatex_insert_ref_activated(NULL, NULL);
 }
 
@@ -167,5 +167,5 @@
 void glatex_kb_convert_to_table(G_GNUC_UNUSED guint key_id)
 {
 	g_return_if_fail(document_get_current() != NULL);
-	glatex_convert_to_table();
+	glatex_convert_to_table(FALSE);
 }

Modified: trunk/geanylatex/src/latexutils.c
===================================================================
--- trunk/geanylatex/src/latexutils.c	2011-01-29 13:40:33 UTC (rev 1885)
+++ trunk/geanylatex/src/latexutils.c	2011-01-29 13:40:55 UTC (rev 1886)
@@ -26,10 +26,10 @@
 {
 	gchar **result = NULL;
 	gchar *data;
-	
-	g_return_val_if_fail((filename != NULL), NULL);	
+
+	g_return_val_if_fail((filename != NULL), NULL);
 	g_return_val_if_fail(g_file_get_contents(filename, &data, NULL, NULL), NULL);
-	
+
 	if (data != NULL)
 	{
 		result = g_strsplit_set(data, "\r\n", -1);
@@ -147,7 +147,7 @@
 
 			if (entity != NULL)
 			{
-			
+
 				g_string_append(replacement, entity);
 			}
 			else
@@ -163,38 +163,79 @@
 }
 
 
-void glatex_convert_to_table()
+/* Based on reimplementation at HTMLTable plugin of Geany-Plugins package
+ * originally in GPL2+ */
+/* gboolean header is not yet used but to don't break API later its already
+ * defined */
+void glatex_convert_to_table(G_GNUC_UNUSED gboolean header)
 {
 	GeanyDocument *doc = NULL;
 	doc = document_get_current();
 
 	g_return_if_fail(doc != NULL);
-
 	if (sci_has_selection(doc->editor->sci))
 	{
 		gchar *selection = NULL;
-		gchar *new = NULL;
-		gchar *rowending = NULL;
-		GString *table = NULL;
-		GString *inner_table = NULL;
+		gchar **rows = NULL;
+		GString *replacement_str = NULL;
+		gchar *replacement = NULL;
 
+		/* Actually grabbing selection and splitting it into single
+		 * lines we will work on later */
 		selection = sci_get_selection_contents(doc->editor->sci);
-		inner_table = g_string_new(selection);
-		utils_string_replace_all(inner_table, "\t", "  &  ");
-		rowending = g_strconcat(" \\\\", editor_get_eol_char(doc->editor), NULL);
-		utils_string_replace_all(inner_table, editor_get_eol_char(doc->editor), rowending);
-		new = g_string_free(inner_table, FALSE);
+		rows = g_strsplit_set(selection, "\r\n", -1);
+		g_free(selection);
 
-		table = g_string_new(NULL);
-		g_string_printf(table, "\\begin{tabular}\n%s\n\\end{tabular}", new);
+		/* Checking whether we do have something we can work on - Returning if not */
+		if (rows != NULL)
+		{
+			/* Adding header to replacement */
+			guint i;
+			guint j;
 
-		g_free(new);
-		
-		new = g_string_free(table, FALSE);
-		sci_replace_sel(doc->editor->sci, new);
+			replacement_str = g_string_new("\\begin{tabular}{}\n");
 
-		g_free(selection);
-		g_free(new);
-		g_free(rowending);
+			/* Iteration onto rows and building up lines of table for
+			 * replacement */
+			for (i = 0; rows[i] != NULL ; i++)
+			{
+				gchar **columns = NULL;
+				columns = g_strsplit_set(rows[i], "\t", -1);
+
+				for (j = 0; columns[j] != NULL; j++)
+				{
+					if (j > 0)
+					{
+						g_string_append(replacement_str, "  &  ");
+					}
+					g_string_append(replacement_str, columns[j]);
+				}
+
+				g_string_append(replacement_str, "\\\\\n");
+
+				g_free(columns);
+			}
+		}
+
+		/* Adding the footer of table */
+
+		g_string_append(replacement_str, "\\end{tabular}\n");
+
+		/* Replacing selection with new table */
+		replacement = g_string_free(replacement_str, FALSE);
+		sci_replace_sel(doc->editor->sci, replacement);
+
+		g_free(rows);
+		g_free(replacement);
 	}
+	else
+	{
+		/* OK. Something went not as expected.
+		 * We did have a selection but cannot parse it into rows.
+		 * Aborting */
+		g_warning(_("Something went went wrong on parsing selection. Aborting"));
+		return;
+	} /* Selection was given -- end
+	   * in case of there was no selection we are just doing nothing */
+	return;
 }

Modified: trunk/geanylatex/src/latexutils.h
===================================================================
--- trunk/geanylatex/src/latexutils.h	2011-01-29 13:40:33 UTC (rev 1885)
+++ trunk/geanylatex/src/latexutils.h	2011-01-29 13:40:55 UTC (rev 1886)
@@ -28,6 +28,6 @@
 void glatex_enter_key_pressed_in_entry(G_GNUC_UNUSED GtkWidget *widget, gpointer dialog);
 void glatex_insert_string(const gchar *string, gboolean reset_position);
 void glatex_replace_special_character(void);
-void glatex_convert_to_table();
+void glatex_convert_to_table(G_GNUC_UNUSED gboolean header);
 
 #endif


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



More information about the Plugins-Commits mailing list