[gedit/docstream2: 14/16] Fixed newline detection and newline stripping in document loader



commit 703f2edf6f5944a9bcec6e197d0761e2881d67ff
Author: Jesse van den Kieboom <jesse icecrew nl>
Date:   Sat Jan 23 14:27:18 2010 +0100

    Fixed newline detection and newline stripping in document loader

 gedit/gedit-gio-document-loader.c |   73 ++++++++++++++++++++-----------------
 1 files changed, 40 insertions(+), 33 deletions(-)
---
diff --git a/gedit/gedit-gio-document-loader.c b/gedit/gedit-gio-document-loader.c
index 1209722..5e8d76d 100644
--- a/gedit/gedit-gio-document-loader.c
+++ b/gedit/gedit-gio-document-loader.c
@@ -280,64 +280,70 @@ get_newline_type (GtkTextIter *end)
 	copy = *end;
 	c = gtk_text_iter_get_char (&copy);
 
-	if (g_unichar_break_type (c) == G_UNICODE_BREAK_LINE_FEED)
-	{
-		c = gtk_text_iter_backward_char (&copy);
+	GtkTextIter tt = copy;
+	gtk_text_iter_forward_chars (&tt, 2);
 
-		if (g_unichar_break_type (c) == G_UNICODE_BREAK_CARRIAGE_RETURN)
+	if (g_unichar_break_type (c) == G_UNICODE_BREAK_CARRIAGE_RETURN)
+	{
+		if (gtk_text_iter_forward_char (&copy) &&
+		    g_unichar_break_type (gtk_text_iter_get_char (&copy)) == G_UNICODE_BREAK_LINE_FEED)
+		{
 			res = GEDIT_DOCUMENT_NEWLINE_TYPE_CR_LF;
+		}
 		else
-			res = GEDIT_DOCUMENT_NEWLINE_TYPE_LF;
+		{
+			res = GEDIT_DOCUMENT_NEWLINE_TYPE_CR;
+		}
 	}
 	else
 	{
-		res = GEDIT_DOCUMENT_NEWLINE_TYPE_CR;
+		res = GEDIT_DOCUMENT_NEWLINE_TYPE_LF;
 	}
 
 	return res;
 }
 
-/* FIXME: This doesn't work */
 static void
 detect_newline_type (GeditDocumentLoader *loader)
 {
-	GtkTextIter end;
+	GtkTextIter iter;
 
-	gtk_text_buffer_get_end_iter (GTK_TEXT_BUFFER (loader->document), &end);
+	gtk_text_buffer_get_end_iter (GTK_TEXT_BUFFER (loader->document), &iter);
 
-	/* if ends line try with the first line */
-	if (gtk_text_iter_ends_line (&end) && !gtk_text_iter_starts_line (&end))
+	if (!gtk_text_iter_backward_line (&iter))
 	{
-		gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (loader->document),
-						&end);
-
-		/* rely on default value if we don't have a newline */
-		if (!gtk_text_iter_forward_line (&end))
-			return;
-
-		if (gtk_text_iter_ends_line (&end))
-			return;
-
-		loader->auto_detected_newline_type = get_newline_type (&end);
+		gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (loader->document), &iter);
 	}
-	else
+
+	if (gtk_text_iter_ends_line (&iter) || gtk_text_iter_forward_to_line_end (&iter))
 	{
-		GtkTextIter start;
+		loader->auto_detected_newline_type = get_newline_type (&iter);
+	}
+}
 
-		start = end;
+static void
+remove_ending_newline (GeditDocumentLoader *loader)
+{
+	GtkTextIter end;
+	GtkTextIter start;
 
-		gtk_text_iter_set_line_offset (&start, 0);
+	gtk_text_buffer_get_end_iter (GTK_TEXT_BUFFER (loader->document), &end);
+	start = end;
 
-		if (!gtk_text_iter_ends_line (&start) &&
-		    !gtk_text_iter_forward_to_line_end (&start))
-			return;
+	gtk_text_iter_set_line_offset (&start, 0);
 
-		loader->auto_detected_newline_type = get_newline_type (&end);
+	if (gtk_text_iter_ends_line (&start) &&
+	    gtk_text_iter_backward_line (&start))
+	{
+		if (!gtk_text_iter_ends_line (&start))
+		{
+			gtk_text_iter_forward_to_line_end (&start);
+		}
 
-		/* If the last char is a newline, remove it from the buffer (otherwise
-		   GtkTextView shows it as an empty line). See bug #324942. */
+		/* Delete the empty line which is from 'start' to 'end' */
 		gtk_text_buffer_delete (GTK_TEXT_BUFFER (loader->document),
-					&start, &end);
+		                        &start,
+		                        &end);
 	}
 }
 
@@ -345,6 +351,7 @@ static void
 end_append_text_to_document (GeditDocumentLoader *loader)
 {
 	detect_newline_type (loader);
+	remove_ending_newline (loader);
 
 	gtk_text_buffer_set_modified (GTK_TEXT_BUFFER (loader->document), FALSE);
 



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