Index: gtksourceview/gtksourceview.c
===================================================================
RCS file: /cvs/gnome/gtksourceview/gtksourceview/gtksourceview.c,v
retrieving revision 1.39
diff -u -p -r1.39 gtksourceview.c
--- gtksourceview/gtksourceview.c	3 Aug 2005 16:52:10 -0000	1.39
+++ gtksourceview/gtksourceview.c	27 Aug 2005 18:26:16 -0000
@@ -1726,9 +1726,10 @@ gtk_source_view_get_marker_pixbuf (GtkSo
 	return pixbuf;
 }
 
-static gchar*
+static gchar *
 compute_indentation (GtkSourceView *view, 
-		     GtkTextIter   *cur)
+		     GtkTextIter   *cur,
+		     gboolean      *just_whitespace)
 {
 	GtkTextIter start;
 	GtkTextIter end;
@@ -1736,6 +1737,8 @@ compute_indentation (GtkSourceView *view
 	gunichar ch;
 	gint line;
 
+	*just_whitespace = FALSE;
+
 	line = gtk_text_iter_get_line (cur);
 
 	gtk_text_buffer_get_iter_at_line (gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)), 
@@ -1748,15 +1751,24 @@ compute_indentation (GtkSourceView *view
 
 	while (g_unichar_isspace (ch) && 
 	       (ch != '\n') && 
-	       (ch != '\r') && 
-	       (gtk_text_iter_compare (&end, cur) < 0))
+	       (ch != '\r'))
 	{
 		if (!gtk_text_iter_forward_char (&end))
+		{
+			/* while we are at it check if the line is just made of
+			 * whitespaces... used to prune trailing whitespaces */
+			if (gtk_text_iter_ends_line (&end))
+				*just_whitespace = TRUE;
+
 			break;
+		}
 
 		ch = gtk_text_iter_get_char (&end);
 	}
 
+	if (gtk_text_iter_compare (&end, cur) > 0)
+		end = *cur;
+
 	if (gtk_text_iter_equal (&start, &end))
 		return NULL;
 
@@ -1787,9 +1799,10 @@ key_press_event (GtkWidget *widget, GdkE
 		 * level as the previous line.
 		 */
 		gchar *indent = NULL;
+		gboolean whiteline = FALSE;
 
 		/* Calculate line indentation and create indent string. */
-		indent = compute_indentation (view, &cur);
+		indent = compute_indentation (view, &cur, &whiteline);
 
 		if (indent != NULL)
 		{
@@ -1803,14 +1816,29 @@ key_press_event (GtkWidget *widget, GdkE
 			 * the cur iterm may be invalid, so get the iter again */
 			gtk_text_buffer_get_iter_at_mark (buf, &cur, mark);
 	
-			/* Insert new line and auto-indent. */
 			gtk_text_buffer_begin_user_action (buf);
-			gtk_text_buffer_insert (buf, &cur, "\n", 1);
-			gtk_text_buffer_insert (buf, &cur, indent, strlen (indent));
-			g_free (indent);
+
+			/* if the current line is just indentation, move it down one line
+			 * and just insert \n at its place so that we don't leave trailing
+			 * whitespaces around */
+			if (whiteline)
+			{
+				gtk_text_iter_set_line_offset (&cur, 0);
+				gtk_text_buffer_insert (buf, &cur, "\n", 1);
+			}
+			else
+			{
+				gtk_text_buffer_insert (buf, &cur, "\n", 1);
+				gtk_text_buffer_insert (buf, &cur, indent, strlen (indent));
+			}
+			
 			gtk_text_buffer_end_user_action (buf);
+
+			g_free (indent);
+
 			gtk_text_view_scroll_mark_onscreen (GTK_TEXT_VIEW (widget),
 							    mark);
+
 			return TRUE;
 		}
 	}
