Index: gedit/gedit-document.c
===================================================================
RCS file: /cvs/gnome/gedit/gedit/gedit-document.c,v
retrieving revision 1.96
diff -u -p -r1.96 gedit-document.c
--- gedit/gedit-document.c	15 Oct 2004 12:32:04 -0000	1.96
+++ gedit/gedit-document.c	18 Oct 2004 22:37:23 -0000
@@ -1344,32 +1344,52 @@ gedit_document_load (GeditDocument      
 	}
 }
 
-gboolean
-gedit_document_load_from_stdin (GeditDocument* doc, GError **error)
+static gboolean
+handle_stdin (GIOChannel *ioc, GIOCondition condition, GeditDocument *doc)
 {
-	gchar *stdin_data;
-	GtkTextIter iter, end;
-	
-	gedit_debug (DEBUG_DOCUMENT, "");
+	static GString *contents;
+	gchar *line;
+	gsize len, pos;
+	static gsize total_len = 0;
+	GError *error = NULL;
+	GIOStatus status;
 
-	g_return_val_if_fail (GEDIT_IS_DOCUMENT (doc), FALSE);
-	
-	stdin_data = gedit_utils_get_stdin ();
+	status = g_io_channel_read_line (ioc, &line, &len, &pos, &error);
 
-	if (stdin_data != NULL && strlen (stdin_data) > 0)
+	switch (status)
 	{
-		GError *conv_error = NULL;
+	case G_IO_STATUS_NORMAL:
+		if (line)
+		{
+			if (!contents)
+				contents = g_string_new (line);
+			else
+				g_string_append (contents, line);
+
+			total_len += len;
+		}
+		g_free (line);
+		return TRUE;
+
+	case G_IO_STATUS_AGAIN:
+		return TRUE;
+
+	case G_IO_STATUS_EOF:
+		{
 		const GeditEncoding *encoding;
 		gchar *converted_text;
 
-		if (gedit_encoding_get_current () != gedit_encoding_get_utf8 ())
+		g_print ("eof\n");
+		g_print (contents->str);
+
+		encoding = gedit_encoding_get_current ();
+
+		if (encoding == gedit_encoding_get_utf8 ())
 		{
-			if (g_utf8_validate (stdin_data, -1, NULL))
+			if (g_utf8_validate (contents->str, total_len, NULL))
 			{
-				converted_text = stdin_data;
-				stdin_data = NULL;
-
-				encoding = gedit_encoding_get_utf8 ();
+				converted_text = contents->str;
+				contents->str = NULL;
 			}
 			else
 			{
@@ -1378,59 +1398,53 @@ gedit_document_load_from_stdin (GeditDoc
 		}
 		else
 		{	
-			encoding = gedit_encoding_get_current ();
-				
-			converted_text = gedit_convert_to_utf8 (stdin_data,
-								-1,
+			converted_text = gedit_convert_to_utf8 (contents->str,
+								total_len,
 								&encoding,
-								&conv_error);
-		}	
-		
-		if (converted_text == NULL)
+								NULL);
+		}
+
+		if (converted_text)
 		{
-			/* bail out */
-			if (conv_error == NULL)
-				g_set_error (error, GEDIT_DOCUMENT_IO_ERROR,
-					     GEDIT_ERROR_INVALID_UTF8_DATA,
-					     _("Invalid UTF-8 data"));
-			else
-				g_propagate_error (error, conv_error);
+			gedit_document_begin_not_undoable_action (doc);
+			gtk_text_buffer_set_text (GTK_TEXT_BUFFER (doc), converted_text, -1);
+			gedit_document_end_not_undoable_action (doc);
+
+			g_get_current_time (&doc->priv->time_of_last_save_or_load);
+		}
 
-			g_free (stdin_data);
+		g_free (converted_text);
+		g_string_free (contents, TRUE);
+		contents = NULL;
 
-			return FALSE;
+		return FALSE;
 		}
 
-		gedit_document_begin_not_undoable_action (doc);
-		/* Insert text in the buffer */
-		gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (doc), &iter, 0);
-		gtk_text_buffer_insert (GTK_TEXT_BUFFER (doc), &iter, converted_text, -1);
-
-		/* We had a newline in the buffer to begin with. (The buffer always contains
-   		 * a newline, so we delete to the end of the buffer to clean up. */
-		gtk_text_buffer_get_end_iter (GTK_TEXT_BUFFER (doc), &end);
- 		gtk_text_buffer_delete (GTK_TEXT_BUFFER (doc), &iter, &end);
-
-		/* Place the cursor at the start of the document */
-		gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (doc), &iter, 0);
-		gtk_text_buffer_place_cursor (GTK_TEXT_BUFFER (doc), &iter);
+	case G_IO_STATUS_ERROR:
+		/* Deal with error, close channel, etc. */
+		g_error_free (error);
+		return FALSE;
 
-		gedit_document_end_not_undoable_action (doc);
-		
-		g_free (converted_text);
+	default:
+		g_return_val_if_reached(FALSE);
 	}
+}
 
-	if (stdin_data != NULL)
-		g_free (stdin_data);
-			
+void
+gedit_document_load_from_stdin (GeditDocument* doc)
+{
+	GIOChannel *channel;
 
-	gedit_document_set_readonly (doc, FALSE);
-	gtk_text_buffer_set_modified (GTK_TEXT_BUFFER (doc), TRUE);
-	g_get_current_time (&doc->priv->time_of_last_save_or_load);
+	g_return_if_fail (GEDIT_IS_DOCUMENT (doc));
 
-	g_signal_emit (G_OBJECT (doc), document_signals [LOADED], 0, NULL);
+	gedit_debug (DEBUG_DOCUMENT, "");
 
-	return TRUE;
+	/* stdin */
+	channel = g_io_channel_unix_new (0);
+	g_io_channel_set_encoding (channel, NULL, NULL);
+	g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL);
+	g_io_add_watch (channel, G_IO_IN | G_IO_HUP,
+			handle_stdin, doc);
 }
 
 gboolean 
Index: gedit/gedit-document.h
===================================================================
RCS file: /cvs/gnome/gedit/gedit/gedit-document.h,v
retrieving revision 1.36
diff -u -p -r1.36 gedit-document.h
--- gedit/gedit-document.h	15 Oct 2004 12:32:04 -0000	1.36
+++ gedit/gedit-document.h	18 Oct 2004 22:37:23 -0000
@@ -131,8 +131,7 @@ void		gedit_document_load 		(GeditDocume
 						 const gchar          *uri, 
 						 const GeditEncoding  *encoding);
 
-gboolean	gedit_document_load_from_stdin 	(GeditDocument        *doc, 
-						 GError              **error);
+void		gedit_document_load_from_stdin 	(GeditDocument        *doc);
 
 gboolean 	gedit_document_is_untouched 	(const GeditDocument *doc);
 
Index: gedit/gedit-file.c
===================================================================
RCS file: /cvs/gnome/gedit/gedit/gedit-file.c,v
retrieving revision 1.76
diff -u -p -r1.76 gedit-file.c
--- gedit/gedit-file.c	5 Oct 2004 15:48:59 -0000	1.76
+++ gedit/gedit-file.c	18 Oct 2004 22:37:25 -0000
@@ -864,82 +864,48 @@ gedit_file_open_single_uri (const gchar*
 	return TRUE;
 }
 
-/* FIXME: it is broken */
-gboolean
+void
 gedit_file_open_from_stdin (GeditMDIChild *active_child)
 {
-	struct stat stats;
-	gboolean ret = TRUE;
 	GeditDocument *doc = NULL;
 	GError *error = NULL;
 	GeditMDIChild *child;
 	GeditMDIChild* new_child = NULL;
 
 	gedit_debug (DEBUG_FILE, "");
-	
-	fstat (STDIN_FILENO, &stats);
-	
-	if (stats.st_size  == 0)
-		return FALSE;
 
 	child = active_child;
 	
 	if (active_child == NULL ||
 	    !gedit_document_is_untouched (active_child->document))	     
 	{
+		gint ret;
+
 		new_child = gedit_mdi_child_new ();
 
-		g_return_val_if_fail (new_child != NULL, FALSE);
-		g_return_val_if_fail (gedit_mdi != NULL, FALSE);
+		g_return_if_fail (new_child != NULL);
+		g_return_if_fail (gedit_mdi != NULL);
 
 		ret = bonobo_mdi_add_child (BONOBO_MDI (gedit_mdi), BONOBO_MDI_CHILD (new_child));
-		g_return_val_if_fail (ret != FALSE, FALSE);
+		g_return_if_fail (ret != FALSE);
 		gedit_debug (DEBUG_FILE, "Child added.");
 
-		child= new_child;
+		child = new_child;
 	}
 
 	doc = child->document;
-	g_return_val_if_fail (doc != NULL, FALSE);
-
-	ret = gedit_document_load_from_stdin (doc, &error);
-
-	if (error)
-	{
-		GtkWidget *dialog;
-		/* FIXME: do a more user friendly error reporting */
-		gchar *errstr;
-	       	
-		errstr = g_strdup_printf (_("Could not read data from stdin."));
-		
-		dialog = gtk_message_dialog_new (
-				GTK_WINDOW (bonobo_mdi_get_active_window (BONOBO_MDI (gedit_mdi))),
-				GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
-			   	GTK_MESSAGE_ERROR,
-			   	GTK_BUTTONS_OK,
-				errstr);
-
-		gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
-
-		gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
+	g_return_if_fail (doc != NULL);
 
-		gtk_dialog_run (GTK_DIALOG (dialog));
-		gtk_widget_destroy (dialog);
+	gedit_document_load_from_stdin (doc);
 
-		g_free (errstr);
-		g_error_free (error);
-
-		ret = FALSE;
-	}
-	
 	if (new_child != NULL)
 	{
+		gint ret;
+
 		ret = bonobo_mdi_add_view (BONOBO_MDI (gedit_mdi), BONOBO_MDI_CHILD (new_child));
-		g_return_val_if_fail (ret != FALSE, FALSE);
+		g_return_if_fail (ret != FALSE);
 		gedit_debug (DEBUG_FILE, "View added.");
 	}
-	
-	return ret;
 }
 
 static void 
Index: gedit/gedit-file.h
===================================================================
RCS file: /cvs/gnome/gedit/gedit/gedit-file.h,v
retrieving revision 1.15
diff -u -p -r1.15 gedit-file.h
--- gedit/gedit-file.h	2 Oct 2004 19:05:50 -0000	1.15
+++ gedit/gedit-file.h	18 Oct 2004 22:37:26 -0000
@@ -50,7 +50,7 @@ gboolean 	gedit_file_open_uri_list (GSLi
 gboolean 	gedit_file_open_recent   (EggRecentView *view, EggRecentItem *item, gpointer data);
 gboolean 	gedit_file_open_single_uri (const gchar* uri, const GeditEncoding *encoding);
 
-gboolean	gedit_file_open_from_stdin (GeditMDIChild *active_child);
+void		gedit_file_open_from_stdin (GeditMDIChild *active_child);
 
 #endif /* __GEDIT_FILE_H__ */
 
Index: gedit/gedit2.c
===================================================================
RCS file: /cvs/gnome/gedit/gedit/gedit2.c,v
retrieving revision 1.45
diff -u -p -r1.45 gedit2.c
--- gedit/gedit2.c	5 Oct 2004 15:48:59 -0000	1.45
+++ gedit/gedit2.c	18 Oct 2004 22:37:26 -0000
@@ -78,7 +78,7 @@ struct _CommandLineData
 };
 
 static void gedit_set_default_icon ();
-static void gedit_load_file_list (CommandLineData *data);
+
 
 static const struct poptOption options [] =
 {
@@ -116,27 +116,23 @@ gedit_set_default_icon ()
 static void 
 gedit_load_file_list (CommandLineData *data)
 {	
-	gboolean res;
-
 	/* Update UI */
 	while (gtk_events_pending ())
 		gtk_main_iteration ();
 
-	res = gedit_file_open_from_stdin (NULL);
+	/* if no stdin, it creates an empty doc */
+	gedit_file_open_from_stdin (NULL);
 
-	if (data == NULL) 
+	if (data)
 	{
-		gedit_file_new ();
-		return;
-	}
-
-	/* Load files */
-	gedit_file_open_uri_list (data->file_list, data->encoding, data->line_pos, TRUE);
+		/* Load files */
+		gedit_file_open_uri_list (data->file_list, data->encoding, data->line_pos, TRUE);
 	
-	g_slist_foreach (data->file_list, (GFunc)g_free, NULL);
-	g_slist_free (data->file_list);
+		g_slist_foreach (data->file_list, (GFunc)g_free, NULL);
+		g_slist_free (data->file_list);
 	
-	g_free (data);
+		g_free (data);
+	}
 }
 
 static CommandLineData *
@@ -253,53 +249,6 @@ gedit_handle_automation_cmdline (GnomePr
 		g_free (data);
 	}
 
-	stdin_data = gedit_utils_get_stdin ();
-
-	if (stdin_data != NULL && strlen (stdin_data) > 0)
-	{
-		GError *conv_error = NULL;
-		const GeditEncoding *encoding;
-		gchar *converted_text;
-
-		if (gedit_encoding_get_current () != gedit_encoding_get_utf8 ())
-		{
-			if (g_utf8_validate (stdin_data, -1, NULL))
-			{
-				converted_text = stdin_data;
-				stdin_data = NULL;
-
-				encoding = gedit_encoding_get_utf8 ();
-			}
-			else
-			{
-				converted_text = NULL;
-			}
-		}
-		else
-		{	
-			encoding = gedit_encoding_get_current ();
-				
-			converted_text = gedit_convert_to_utf8 (stdin_data,
-								-1,
-								&encoding,
-								&conv_error);
-		}	
-		
-		if (converted_text != NULL)
-		{
-			window = GNOME_Gedit_Application_getActiveWindow (server, &env);
-			document = GNOME_Gedit_Window_newDocument (window, &env);
-
-			GNOME_Gedit_Document_insert (document, 0, converted_text,
-						     strlen (converted_text), &env);
-
-			g_free (converted_text);
-		}
-	}
-
-	if (stdin_data != NULL)
-		g_free (stdin_data);
-
 	if (!quit_option)
 	{
 		window = GNOME_Gedit_Application_getActiveWindow (server, &env);
