Scaffold -- Added trim command to text-plugin



Hi all,

Just a patch for adding a trim command to the text plugin.


Anyone noticed that the text-plugin only works on the first document
loaded?


Regards,
Rui Lopes
# * Add a trim command to text plugin.
# * Some minor tweaks.
# -- Rui Lopes <rui ruilopes com>
Index: libscaffold/scaffold-utils.c
===================================================================
RCS file: /cvs/gnome/scaffold/libscaffold/scaffold-utils.c,v
retrieving revision 1.21
diff -u -r1.21 scaffold-utils.c
--- libscaffold/scaffold-utils.c	31 Jul 2003 08:55:15 -0000	1.21
+++ libscaffold/scaffold-utils.c	1 Sep 2003 20:36:48 -0000
@@ -68,8 +68,6 @@
 {
 	if (!s)
 		return TRUE;
-	if (strlen (s) == 0)
-		return TRUE;
 
 	while (*s) {
 		if (!isspace (*s))
Index: plugins/text/Makefile.am
===================================================================
RCS file: /cvs/gnome/scaffold/plugins/text/Makefile.am,v
retrieving revision 1.15
diff -u -r1.15 Makefile.am
--- plugins/text/Makefile.am	31 Jul 2003 08:55:41 -0000	1.15
+++ plugins/text/Makefile.am	1 Sep 2003 20:36:49 -0000
@@ -17,7 +17,9 @@
 	text-insert.c		\
 	text-insert.h		\
 	text-replace.c		\
-	text-replace.h
+	text-replace.h		\
+	text-trim.c		\
+	text-trim.h
 
 libscaffold_text_la_LIBADD = \
 	$(COMPONENT_LIBS)
Index: plugins/text/scaffold-text-plugin.xml
===================================================================
RCS file: /cvs/gnome/scaffold/plugins/text/scaffold-text-plugin.xml,v
retrieving revision 1.2
diff -u -r1.2 scaffold-text-plugin.xml
--- plugins/text/scaffold-text-plugin.xml	20 Mar 2001 03:57:56 -0000	1.2
+++ plugins/text/scaffold-text-plugin.xml	1 Sep 2003 20:36:49 -0000
@@ -19,6 +19,8 @@
 	        <cmd name="TextReplaceTabs" _label="Replace tabs with spaces"
 		_tip="Replace all tabs in the document with spaces"
 		pixtype="stock" pixname="Convert"/>
+ 	        <cmd name="TextTrimTrailingSpaces" _label="Trim trailing spaces"
+		_tip="Removes all white-space from end of lines"/>
 	</commands>
 
 	<menu>
@@ -36,6 +38,7 @@
 				<menuitem name="TextDeleteEOL" verb=""/>
 			</submenu>
 			<menuitem name="TextReplaceTabs" verb=""/>
+			<menuitem name="TextTrimTrailingSpaces" verb=""/>
 		</submenu>
 	</menu>
 </Root>
Index: plugins/text/text-tool.c
===================================================================
RCS file: /cvs/gnome/scaffold/plugins/text/text-tool.c,v
retrieving revision 1.10
diff -u -r1.10 text-tool.c
--- plugins/text/text-tool.c	31 Jul 2003 08:55:42 -0000	1.10
+++ plugins/text/text-tool.c	1 Sep 2003 20:36:49 -0000
@@ -20,6 +20,7 @@
 #include "text-insert.h"
 #include "text-delete.h"
 #include "text-replace.h"
+#include "text-trim.h"
 
 #define PLUGIN_NAME			"scaffold-text-plugin"
 #define PLUGIN_XML			"scaffold-text-plugin.xml"
@@ -46,6 +47,7 @@
 	BONOBO_UI_UNSAFE_VERB("TextDeleteBOL", text_delete_to_bol),
 	BONOBO_UI_UNSAFE_VERB("TextDeleteEOL", text_delete_to_eol),
 	BONOBO_UI_UNSAFE_VERB("TextReplaceTabs", text_replace_tab_spaces),
+	BONOBO_UI_UNSAFE_VERB("TextTrimTrailingSpaces", text_trim_trailing_spaces),
 	BONOBO_UI_VERB_END
 };
 
Index: src/preferences-dialog.c
===================================================================
RCS file: /cvs/gnome/scaffold/src/preferences-dialog.c,v
retrieving revision 1.5
diff -u -r1.5 preferences-dialog.c
--- src/preferences-dialog.c	31 Jul 2003 08:55:48 -0000	1.5
+++ src/preferences-dialog.c	1 Sep 2003 20:36:49 -0000
@@ -132,8 +132,11 @@
 	if (gtk_tree_selection_get_selected (treesel, NULL, &iter)) {
 		gtk_tree_model_get (GTK_TREE_MODEL (dlg->priv->store), &iter, 
 				    COL_WIDGET, &page, COL_POSITION, &tab_pos, -1);
-		if (page == NULL)
+		if (page == NULL) {
+			/* TODO: Instead of returning, select a "Scaffold" page;
+			 *       similar to what gedit does. */
 			return;
+		}
 
 		gtk_notebook_set_current_page (GTK_NOTEBOOK (dlg->priv->notebook),
 					       tab_pos);
@@ -171,7 +174,7 @@
 				 GTK_TREE_MODEL (dlg->priv->store));
 
 	column = gtk_tree_view_column_new ();
-	gtk_tree_view_column_set_title (column, "Categories");
+	gtk_tree_view_column_set_title (column, _("Categories"));
 	renderer = gtk_cell_renderer_text_new ();
 	gtk_tree_view_column_pack_start (column, renderer, TRUE);
 	gtk_tree_view_column_set_cell_data_func (column, renderer,
--- /dev/null	Mon Sep  1 21:33:00 2003
+++ plugins/text/text-trim.h	Mon Sep  1 15:09:06 2003
@@ -0,0 +1,26 @@
+/*
+ * Text Trim for Scaffold text plugin.
+ *
+ * Copyright (C) 2003 Rui Lopes <rui ruilopes com>
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef PLUGIN_TEXT_TRIM_H
+#define PLUGIN_TEXT_TRIM_H
+
+void text_trim_trailing_spaces (GtkWidget* widget, gpointer data);
+
+#endif
--- /dev/null	Mon Sep  1 21:33:00 2003
+++ plugins/text/text-trim.c	Mon Sep  1 21:05:21 2003
@@ -0,0 +1,61 @@
+/*
+ * Text Trim for Scaffold text plugin.
+ *
+ * Copyright (C) 2003 Rui Lopes <rui ruilopes com>
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <libscaffold/libscaffold.h>
+#include <glib.h>
+#include "text-trim.h"
+
+
+void
+text_trim_trailing_spaces (GtkWidget* widget, gpointer data)
+{
+	ScaffoldTool* tool = (ScaffoldTool*)data;
+	long space_pos;
+	long pos;
+	long length;
+
+	/* TODO: If there is a selection, trim only the lines on that selection. */
+
+	/* trim trailing spaces from all document lines */
+	space_pos = -1;
+	length = scaffold_get_document_length (tool);
+	for (pos = 0; pos < length; ++pos) {
+		char* ch = scaffold_get_document_chars (tool, pos, pos + 1);
+
+		if (*ch == '\n') {
+			if (space_pos >= 0) { /* only trim if needed */
+				scaffold_delete_text (tool, space_pos, pos);
+				space_pos = -1;
+				length = scaffold_get_document_length (tool);
+			}
+		}
+		else if (g_ascii_isspace (*ch)) {
+			/* only update space_pos if the last character was a
+			 * non-space */
+			if (space_pos < 0)
+				space_pos = pos;
+		}
+		else
+			space_pos = -1;
+
+		g_free(ch);
+	}
+}
+


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