--- gtksourceview/gtksourceview.c.old	2007-01-03 10:40:28.000000000 -0500
+++ gtksourceview/gtksourceview.c	2007-08-18 12:56:11.000000000 -0400
@@ -1925,7 +1925,8 @@ gtk_source_view_get_marker_pixbuf (GtkSo
 
 static gchar *
 compute_indentation (GtkSourceView *view, 
-		     GtkTextIter   *cur)
+		     GtkTextIter   *cur,
+		     gboolean *just_whitespace)
 {
 	GtkTextIter start;
 	GtkTextIter end;
@@ -1933,6 +1934,8 @@ compute_indentation (GtkSourceView *view
 	gunichar ch;
 	gint line;
 
+	*just_whitespace = TRUE;
+
 	line = gtk_text_iter_get_line (cur);
 
 	gtk_text_buffer_get_iter_at_line (gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)), 
@@ -1941,18 +1944,19 @@ compute_indentation (GtkSourceView *view
 
 	end = start;
 
-	ch = gtk_text_iter_get_char (&end);
-
-	while (g_unichar_isspace (ch) && 
-	       (ch != '\n') && 
-	       (ch != '\r') && 
-	       (gtk_text_iter_compare (&end, cur) < 0))
-	{
-		if (!gtk_text_iter_forward_char (&end))
+	do {
+		ch = gtk_text_iter_get_char(&end);
+		/* abort if we've reached the end of the line */
+		if (ch == '\r' || ch == '\n') {
 			break;
-
-		ch = gtk_text_iter_get_char (&end);
-	}
+		}
+		/* abort if we meet a non-space character, and note that the line is more
+		 * than just whitespace. */
+		if (!g_unichar_isspace(ch)) {
+			*just_whitespace = FALSE;
+			break;
+		}
+	} while (gtk_text_iter_forward_char(&end));
 
 	if (gtk_text_iter_equal (&start, &end))
 		return NULL;
@@ -2171,9 +2175,10 @@ gtk_source_view_key_press_event (GtkWidg
 		 * SHIFT+ENTER allows to avoid autoindentation.
 		 */
 		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)
 		{
@@ -2189,8 +2194,21 @@ gtk_source_view_key_press_event (GtkWidg
 	
 			/* 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));
+
+			/* 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));
+			}
+
 			g_free (indent);
 			gtk_text_buffer_end_user_action (buf);
 			gtk_text_view_scroll_mark_onscreen (GTK_TEXT_VIEW (widget),
