--- gedit-2.11.91.orig/data/gedit.schemas.in	2005-08-08 11:02:28.000000000 +0200
+++ gedit-2.11.91/data/gedit.schemas.in	2005-08-21 13:44:34.000000000 +0200
@@ -159,6 +159,20 @@
     </schema>
 
     <schema>
+      <key>/schemas/apps/gedit-2/preferences/editor/save/trailing_whitespace</key>
+      <applyto>/apps/gedit-2/preferences/editor/save/trailing_whitespace</applyto>
+      <owner>gedit</owner>
+      <type>bool</type>
+      <default>FALSE</default>
+      <locale name="C">
+	<short>Remove Trailing Whitespace</short>
+	<long>Whether gedit should automatically remove all trailing
+	whitespace (spaces and tabs) from the end of each line when
+	saving the file.</long>
+      </locale>
+    </schema>
+
+    <schema>
       <key>/schemas/apps/gedit-2/preferences/editor/undo/undo_actions_limit</key>
       <applyto>/apps/gedit-2/preferences/editor/undo/undo_actions_limit</applyto>
       <owner>gedit</owner>
--- gedit-2.11.91.orig/gedit/dialogs/gedit-preferences-dialog.c	2005-08-08 11:02:28.000000000 +0200
+++ gedit-2.11.91/gedit/dialogs/gedit-preferences-dialog.c	2005-08-21 13:50:17.000000000 +0200
@@ -93,6 +93,7 @@
 	GtkWidget	*auto_save_checkbutton;
 	GtkWidget	*auto_save_spinbutton;
 	GtkWidget	*autosave_hbox;
+	GtkWidget	*trailing_whitespace_checkbutton;
 	
 	/* Line numbers */
 	GtkWidget	*display_line_numbers_checkbutton;
@@ -250,6 +251,17 @@
 }
 
 static void
+trailing_whitespace_checkbutton_toggled (GtkToggleButton        *button,
+					 GeditPreferencesDialog *dlg)
+{
+	gedit_debug (DEBUG_PREFS, "");
+
+	g_return_if_fail (button == GTK_TOGGLE_BUTTON (dlg->trailing_whitespace_checkbutton));
+
+	gedit_prefs_manager_set_trailing_whitespace (gtk_toggle_button_get_active (button));
+}
+
+static void
 setup_editor_page (GeditPreferencesDialog *dlg)
 {
 	gboolean auto_save;
@@ -281,6 +293,9 @@
 	gtk_spin_button_set_value (GTK_SPIN_BUTTON (dlg->auto_save_spinbutton),
 				   auto_save_interval);
 
+	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dlg->trailing_whitespace_checkbutton),
+				      gedit_prefs_manager_get_trailing_whitespace ());
+
 	/* Set widget sensitivity */
 	gtk_widget_set_sensitive (dlg->tabs_width_hbox, 
 				  gedit_prefs_manager_tabs_size_can_set ());
@@ -300,6 +315,9 @@
 			          auto_save &&
 				  gedit_prefs_manager_auto_save_interval_can_set ());
 
+	gtk_widget_set_sensitive (dlg->trailing_whitespace_checkbutton,
+				  gedit_prefs_manager_trailing_whitespace_can_set ());
+
 	/* Connect signal */
 	g_signal_connect (G_OBJECT (dlg->tabs_width_spinbutton), "value_changed",
 			  G_CALLBACK (tabs_width_spinbutton_value_changed),
@@ -324,6 +342,10 @@
 	g_signal_connect (G_OBJECT (dlg->auto_save_spinbutton), "value_changed",
 			  G_CALLBACK (auto_save_spinbutton_value_changed),
 			  dlg);
+
+	g_signal_connect (G_OBJECT (dlg->trailing_whitespace_checkbutton), "toggled", 
+			  G_CALLBACK (trailing_whitespace_checkbutton_toggled), 
+			  dlg);
 }
 
 static void
@@ -1270,6 +1292,7 @@
 	dialog->backup_copy_checkbutton = glade_xml_get_widget (gui, "backup_copy_checkbutton");
 	dialog->auto_save_checkbutton = glade_xml_get_widget (gui, "auto_save_checkbutton");
 	dialog->auto_save_spinbutton = glade_xml_get_widget (gui, "auto_save_spinbutton");
+	dialog->trailing_whitespace_checkbutton = glade_xml_get_widget (gui, "trailing_whitespace_checkbutton");
 
 	dialog->plugin_manager_place_holder = glade_xml_get_widget (gui, "plugin_manager_place_holder");
 
@@ -1314,6 +1337,7 @@
 	    !dialog->backup_copy_checkbutton ||
 	    !dialog->auto_save_checkbutton ||
 	    !dialog->auto_save_spinbutton ||
+	    !dialog->trailing_whitespace_checkbutton ||
 	    !dialog->plugin_manager_place_holder ||
 	    !dialog->enable_syntax_hl_checkbutton ||
 	    !dialog->hl_vbox ||
--- gedit-2.11.91.orig/gedit/dialogs/gedit-preferences.glade2	2005-08-08 11:02:28.000000000 +0200
+++ gedit-2.11.91/gedit/dialogs/gedit-preferences.glade2	2005-08-21 13:44:34.000000000 +0200
@@ -1157,6 +1157,25 @@
 			      <property name="fill">True</property>
 			    </packing>
 			  </child>
+
+			  <child>
+			    <widget class="GtkCheckButton" id="trailing_whitespace_checkbutton">
+			      <property name="visible">True</property>
+			      <property name="can_focus">True</property>
+			      <property name="label" translatable="yes">Remove trailing _whitespace</property>
+			      <property name="use_underline">True</property>
+			      <property name="relief">GTK_RELIEF_NORMAL</property>
+			      <property name="focus_on_click">True</property>
+			      <property name="active">False</property>
+			      <property name="inconsistent">False</property>
+			      <property name="draw_indicator">True</property>
+			    </widget>
+			    <packing>
+			      <property name="padding">0</property>
+			      <property name="expand">False</property>
+			      <property name="fill">False</property>
+			    </packing>
+			  </child>
 			</widget>
 			<packing>
 			  <property name="padding">0</property>
--- gedit-2.11.91.orig/gedit/gedit-document.c	2005-08-04 15:49:38.000000000 +0200
+++ gedit-2.11.91/gedit/gedit-document.c	2005-08-21 13:44:34.000000000 +0200
@@ -1816,6 +1816,7 @@
 	mode_t saved_umask;
 	struct stat st;
 	gchar *chars = NULL;
+	gchar *last_nws, *curch;
 	gint fd;
 	gint retval;
 	gsize chars_len = 0;
@@ -1947,6 +1948,30 @@
 
 	chars_len = strlen (chars);
 
+	if (gedit_prefs_manager_get_trailing_whitespace())
+	{
+		curch = chars;
+		last_nws = chars - 1;
+		do
+		{
+			if ((*curch == '\n') || (*curch == '\0'))
+			{
+				if (last_nws != (curch - 1))
+				{
+					memmove(last_nws + 1, curch, chars_len + 1);
+					curch = last_nws + 1;
+				}
+			}
+
+			if (!isblank(*curch))
+				last_nws = curch;
+
+			chars_len--;
+		} while (*(curch++) != '\0');
+
+		chars_len = strlen (chars);
+	}
+
 	add_cr = FALSE;
 	
 	if (chars_len >= 1)
--- gedit-2.11.91.orig/gedit/gedit-prefs-manager.h	2005-08-08 11:02:28.000000000 +0200
+++ gedit-2.11.91/gedit/gedit-prefs-manager.h	2005-08-21 13:53:11.000000000 +0200
@@ -57,6 +57,8 @@
 #define GPM_AUTO_SAVE			GPM_SAVE_DIR "/auto_save"
 #define GPM_AUTO_SAVE_INTERVAL		GPM_SAVE_DIR "/auto_save_interval"
 
+#define GPM_TRAILING_WHITESPACE	GPM_SAVE_DIR "/trailing_whitespace"
+
 #define GPM_UNDO_DIR			GPM_PREFS_DIR  "/editor/undo"
 #define GPM_UNDO_ACTIONS_LIMIT		GPM_UNDO_DIR "/max_undo_actions"
 
@@ -141,6 +143,8 @@
 #define GPM_DEFAULT_AUTO_SAVE		0 /* FALSE */
 #define GPM_DEFAULT_AUTO_SAVE_INTERVAL	10 /* minutes */
 
+#define GPM_DEFAULT_TRAILING_WHITESPACE	0 /* FALSE */
+
 #define GPM_DEFAULT_UNDO_ACTIONS_LIMIT	2000 /* actions */
 
 #define GPM_DEFAULT_WRAP_MODE		"GTK_WRAP_WORD"
@@ -258,6 +262,11 @@
 void			 gedit_prefs_manager_set_auto_save_interval	(gint asi);
 gboolean		 gedit_prefs_manager_auto_save_interval_can_set	(void);
 
+/* Remove trailing whitespace */
+gboolean		 gedit_prefs_manager_get_trailing_whitespace (void);
+void			 gedit_prefs_manager_set_trailing_whitespace (gboolean as);
+gboolean		 gedit_prefs_manager_trailing_whitespace_can_set (void);
+
 /* Undo actions limit: if < 1 then no limits */
 gint 			 gedit_prefs_manager_get_undo_actions_limit	(void);
 void			 gedit_prefs_manager_set_undo_actions_limit	(gint ual);
--- gedit-2.11.91.orig/gedit/gedit-prefs-manager.c	2005-07-23 10:41:59.000000000 +0200
+++ gedit-2.11.91/gedit/gedit-prefs-manager.c	2005-08-21 13:53:53.000000000 +0200
@@ -545,6 +545,11 @@
 		 GPM_AUTO_SAVE_INTERVAL,
 		 GPM_DEFAULT_AUTO_SAVE_INTERVAL);
 
+/* Remove trailing whitespace */
+DEFINE_BOOL_PREF (trailing_whitespace,
+		 GPM_TRAILING_WHITESPACE,
+		 GPM_DEFAULT_TRAILING_WHITESPACE);
+
 
 /* Undo actions limit: if < 1 then no limits */
 DEFINE_INT_PREF (undo_actions_limit,
