? gedit-commands.cbak
? gedit-view.c.modeline
? stdin.patch
? tmp.patch
? dialogs/gedit-preferences-dialog.cbak
? dialogs/gedit-preferences-dialog.hbak
Index: gedit-commands.c
===================================================================
RCS file: /cvs/gnome/gedit/gedit/gedit-commands.c,v
retrieving revision 1.51.2.51
diff -u -p -r1.51.2.51 gedit-commands.c
--- gedit-commands.c	15 Sep 2005 09:12:33 -0000	1.51.2.51
+++ gedit-commands.c	22 Sep 2005 13:58:19 -0000
@@ -193,6 +193,37 @@ gedit_cmd_load_files_from_prompt (GeditW
 }
 
 void
+gedit_cmd_load_stdin (GeditWindow         *window,
+		      const GeditEncoding *encoding)
+{
+	gint loaded_files = 0;
+	gboolean ret;
+	GeditTab *tab;
+	GeditDocument *doc;
+	gboolean jump_to = TRUE;
+	gboolean flash = TRUE;
+
+	g_return_val_if_fail (GEDIT_IS_WINDOW (window), 0);
+
+	// TODO... just a hack: always create a new tab
+	// no error checking either
+
+	gedit_window_create_tab (window, TRUE);
+	tab = gedit_window_get_active_tab (window);
+	if (tab != NULL)
+	{
+		doc = gedit_tab_get_document (tab);
+
+		if (gedit_document_is_untouched (doc) &&
+		    (gedit_tab_get_state (tab) == GEDIT_TAB_STATE_NORMAL))
+		{
+			ret = _gedit_tab_load_stdin (tab,
+						     encoding);
+		}
+	}
+}
+
+void
 gedit_cmd_file_new (GtkAction   *action,
 		    GeditWindow *window)
 {
Index: gedit-commands.h
===================================================================
RCS file: /cvs/gnome/gedit/gedit/gedit-commands.h,v
retrieving revision 1.10.10.7
diff -u -p -r1.10.10.7 gedit-commands.h
--- gedit-commands.h	12 May 2005 10:43:26 -0000	1.10.10.7
+++ gedit-commands.h	22 Sep 2005 13:58:20 -0000
@@ -47,6 +47,8 @@ gint		gedit_cmd_load_files_from_prompt	(
 							 const GSList        *uris,
 							 const GeditEncoding *encoding,
 							 gint                 line_pos);
+void		gedit_cmd_load_stdin			(GeditWindow         *window,
+							 const GeditEncoding *encoding);
 
 void		gedit_cmd_file_new			(GtkAction   *action,
 							 GeditWindow *window);
Index: gedit-document-loader.c
===================================================================
RCS file: /cvs/gnome/gedit/gedit/Attic/gedit-document-loader.c,v
retrieving revision 1.1.2.19
diff -u -p -r1.1.2.19 gedit-document-loader.c
--- gedit-document-loader.c	12 Sep 2005 16:51:12 -0000	1.1.2.19
+++ gedit-document-loader.c	22 Sep 2005 13:58:21 -0000
@@ -84,6 +84,9 @@ struct _GeditDocumentLoaderPrivate
 
 	gchar                    *buffer;
 
+	/* io channel for stdin */
+	GIOChannel               *io_channel;
+
 	const GeditEncoding      *auto_detected_encoding;
 
 	GError                   *error;
@@ -884,6 +887,90 @@ load_remote_file (GeditDocumentLoader *l
 				  loader);
 }
 
+
+/* ----------- stdin ----------- */
+
+static void
+stdin_load_completed_or_failed (GeditDocumentLoader *loader)
+{
+	gedit_debug (DEBUG_DOCUMENT);
+
+	gtk_source_buffer_begin_not_undoable_action (
+				GTK_SOURCE_BUFFER (loader->priv->document));
+
+	gtk_text_buffer_set_modified (GTK_TEXT_BUFFER (loader->priv->document),
+				      FALSE);
+
+	g_io_channel_shutdown (loader->priv->io_channel, TRUE, NULL);
+	g_io_channel_unref (loader->priv->io_channel);
+
+	load_completed_or_failed (loader);
+}
+
+static gboolean
+handle_stdin (GIOChannel          *ioc,
+	      GIOCondition         condition,
+	      GeditDocumentLoader *loader)
+ {
+	GIOStatus status;
+	gchar *line;
+	gsize len, pos;
+ 
+	status = g_io_channel_read_line (ioc, &line, &len, &pos, &loader->priv->error);
+ 
+	switch (status)
+ 	{
+	case G_IO_STATUS_NORMAL:
+		if (line)
+		{
+			if (!g_utf8_validate (line, len, NULL))
+			{
+				g_set_error (&loader->priv->error,
+					     G_CONVERT_ERROR, 
+					     G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
+					     "The file you are trying to open contains an invalid byte sequence."); // change msg FIXME?
+
+				stdin_load_completed_or_failed (loader);
+				return FALSE;
+			}
+
+			// emit a loading?
+
+			gtk_source_buffer_begin_not_undoable_action (
+					GTK_SOURCE_BUFFER (loader->priv->document));
+			gtk_text_buffer_insert_at_cursor (
+					GTK_TEXT_BUFFER (loader->priv->document), line, len);
+			gtk_source_buffer_end_not_undoable_action (
+					GTK_SOURCE_BUFFER (loader->priv->document));
+
+			loader->priv->bytes_read += len;
+
+			g_free (line);
+
+			return TRUE;
+		}
+		else /* no more data, we are done */  // CHECK: is this right?
+		{
+			g_print ("Do we actually get here??\n");
+			stdin_load_completed_or_failed (loader);
+			return FALSE;
+		}
+
+	// CHECK can line be non null if status is not normal -> leak
+
+	case G_IO_STATUS_AGAIN: //CHECK.. is this right?
+		return TRUE;
+
+	case G_IO_STATUS_EOF:
+	case G_IO_STATUS_ERROR:
+		stdin_load_completed_or_failed (loader);
+		return FALSE;
+
+	default:
+		g_return_val_if_reached(FALSE);
+ 	}
+}
+
 /* ---------- public api ---------- */
 
 /* If enconding == NULL, the encoding will be autodetected */
@@ -918,6 +1005,41 @@ gedit_document_loader_load (GeditDocumen
 	{
 		load_remote_file (loader);
 	}
+
+	return TRUE;
+}
+
+gboolean
+gedit_document_loader_load_from_stdin (GeditDocumentLoader *loader,
+				       const GeditEncoding *encoding)
+{
+	g_return_val_if_fail (GEDIT_IS_DOCUMENT_LOADER (loader), FALSE);
+
+	/* the loader can be used just once, then it must be thrown away */
+	g_return_val_if_fail (loader->priv->used == FALSE, FALSE);
+	loader->priv->used = TRUE;
+
+	loader->priv->encoding = encoding;
+
+	loader->priv->io_channel = g_io_channel_unix_new (0);
+
+	if (g_io_channel_set_encoding (loader->priv->io_channel,
+				       gedit_encoding_get_charset (encoding),
+				       NULL) != G_IO_STATUS_NORMAL)
+	{
+		// TODO: async error rep with GError instead
+
+		g_io_channel_shutdown (loader->priv->io_channel, TRUE, NULL);
+		g_io_channel_unref (loader->priv->io_channel);
+
+		return FALSE;
+	}
+
+	g_io_channel_set_flags (loader->priv->io_channel, G_IO_FLAG_NONBLOCK, NULL);
+	g_io_add_watch (loader->priv->io_channel,
+			G_IO_IN | G_IO_HUP,
+			handle_stdin,
+			loader);
 
 	return TRUE;
 }
Index: gedit-document-loader.h
===================================================================
RCS file: /cvs/gnome/gedit/gedit/Attic/gedit-document-loader.h,v
retrieving revision 1.1.2.11
diff -u -p -r1.1.2.11 gedit-document-loader.h
--- gedit-document-loader.h	12 Sep 2005 16:51:12 -0000	1.1.2.11
+++ gedit-document-loader.h	22 Sep 2005 13:58:22 -0000
@@ -88,9 +88,10 @@ GeditDocumentLoader 	*gedit_document_loa
 gboolean		 gedit_document_loader_load		(GeditDocumentLoader *loader,
 							 	 const gchar         *uri,
 								 const GeditEncoding *encoding);
-#if 0
-gboolean		 gedit_document_loader_load_from_stdin	(GeditDocumentLoader *loader);
-#endif		 
+
+gboolean		 gedit_document_loader_load_from_stdin	(GeditDocumentLoader *loader,
+								 const GeditEncoding *encoding);
+
 gboolean		 gedit_document_loader_cancel		(GeditDocumentLoader *loader);
 
 
Index: gedit-document.c
===================================================================
RCS file: /cvs/gnome/gedit/gedit/gedit-document.c,v
retrieving revision 1.111.2.37
diff -u -p -r1.111.2.37 gedit-document.c
--- gedit-document.c	19 Sep 2005 15:05:24 -0000	1.111.2.37
+++ gedit-document.c	22 Sep 2005 13:58:27 -0000
@@ -862,6 +862,32 @@ gedit_document_load_cancel (GeditDocumen
 	return gedit_document_loader_cancel (doc->priv->loader);
 }
 
+gboolean
+gedit_document_load_from_stdin (GeditDocument       *doc,
+				const GeditEncoding *encoding)
+{
+	g_return_val_if_fail (GEDIT_IS_DOCUMENT (doc), FALSE);
+
+	g_return_val_if_fail (doc->priv->loader == NULL, FALSE);
+
+	/* create a loader. It will be destroyed when loading is completed */
+	doc->priv->loader = gedit_document_loader_new (doc);
+
+	g_signal_connect (doc->priv->loader,
+			  "loading",
+			  G_CALLBACK (document_loader_loading),
+			  doc);
+
+	doc->priv->create = TRUE;
+	doc->priv->requested_encoding = encoding;
+	doc->priv->requested_line_pos = 0;
+
+//	set_uri (doc, uri, NULL);
+
+	return gedit_document_loader_load_from_stdin (doc->priv->loader,
+						      encoding);
+}
+
 static void
 document_saver_saving (GeditDocumentSaver *saver,
 		       gboolean            completed,
Index: gedit-document.h
===================================================================
RCS file: /cvs/gnome/gedit/gedit/gedit-document.h,v
retrieving revision 1.41.2.16
diff -u -p -r1.41.2.16 gedit-document.h
--- gedit-document.h	19 Sep 2005 15:05:24 -0000	1.41.2.16
+++ gedit-document.h	22 Sep 2005 13:58:27 -0000
@@ -138,7 +138,10 @@ gboolean	 gedit_document_load 		(GeditDo
 						 const GeditEncoding *encoding,
 						 gint                 line_pos,
 						 gboolean             create); 
-						 
+
+gboolean	 gedit_document_load_from_stdin	(GeditDocument       *doc,
+						 const GeditEncoding *encoding);
+
 gboolean	 gedit_document_insert_file	(GeditDocument       *doc,
 						 GtkTextIter         *iter, 
 						 const gchar         *uri, 
Index: gedit-tab.c
===================================================================
RCS file: /cvs/gnome/gedit/gedit/Attic/gedit-tab.c,v
retrieving revision 1.1.2.45
diff -u -p -r1.1.2.45 gedit-tab.c
--- gedit-tab.c	19 Sep 2005 18:18:11 -0000	1.1.2.45
+++ gedit-tab.c	22 Sep 2005 13:58:36 -0000
@@ -974,7 +974,33 @@ _gedit_tab_new_from_uri (const gchar    
 	}
 
 	return GTK_WIDGET (tab);
-}		
+}
+
+GtkWidget *
+_gedit_tab_new_from_stdin (const gchar         *uri,
+			   const GeditEncoding *encoding)
+{
+	GeditTab *tab;
+	GeditDocument *doc;
+	gboolean ret;
+
+	tab = GEDIT_TAB (_gedit_tab_new ());
+
+	doc = gedit_tab_get_document (tab);
+
+	gedit_tab_set_state (tab, GEDIT_TAB_STATE_LOADING);
+
+	ret = gedit_document_load_from_stdin (doc, 
+					      encoding);
+
+	if (!ret)
+	{
+		g_object_unref (tab);
+		return NULL;
+	}
+
+	return GTK_WIDGET (tab);
+}
 
 GeditView *
 gedit_tab_get_view (GeditTab *tab)
@@ -1290,6 +1316,24 @@ _gedit_tab_load (GeditTab            *ta
 				    encoding,
 				    line_pos,
 				    create);
+}
+
+gboolean
+_gedit_tab_load_stdin (GeditTab            *tab,
+		       const GeditEncoding *encoding)
+{
+	GeditDocument *doc;
+
+	g_return_val_if_fail (GEDIT_IS_TAB (tab), FALSE);
+	g_return_val_if_fail (tab->priv->state == GEDIT_TAB_STATE_NORMAL, FALSE);
+
+	doc = gedit_tab_get_document (tab);
+	g_return_val_if_fail (GEDIT_IS_DOCUMENT (doc), FALSE);
+
+	gedit_tab_set_state (tab, GEDIT_TAB_STATE_LOADING);
+
+	return gedit_document_load_from_stdin (doc,
+					       encoding);
 }
 
 void
Index: gedit-tab.h
===================================================================
RCS file: /cvs/gnome/gedit/gedit/Attic/gedit-tab.h,v
retrieving revision 1.1.2.18
diff -u -p -r1.1.2.18 gedit-tab.h
--- gedit-tab.h	14 May 2005 10:42:10 -0000	1.1.2.18
+++ gedit-tab.h	22 Sep 2005 13:58:36 -0000
@@ -120,6 +120,8 @@ GtkWidget	*_gedit_tab_new_from_uri	(cons
 						 const GeditEncoding *encoding,
 						 gint                 line_pos,
 						 gboolean             create);
+GtkWidget 	*_gedit_tab_new_from_stdin	(const gchar         *uri,
+						 const GeditEncoding *encoding);
 gchar 		*_gedit_tab_get_name		(GeditTab            *tab);
 gchar 		*_gedit_tab_get_tooltips	(GeditTab            *tab);
 GdkPixbuf 	*_gedit_tab_get_icon		(GeditTab            *tab);
@@ -128,6 +130,8 @@ gboolean	 _gedit_tab_load		(GeditTab    
 						 const GeditEncoding *encoding,
 						 gint                 line_pos,
 						 gboolean             create);
+gboolean	 _gedit_tab_load_stdin		(GeditTab            *tab,
+						 const GeditEncoding *encoding);
 void		 _gedit_tab_revert		(GeditTab            *tab);
 void		 _gedit_tab_save		(GeditTab            *tab);
 void		 _gedit_tab_save_as		(GeditTab            *tab,
Index: gedit.c
===================================================================
RCS file: /cvs/gnome/gedit/gedit/Attic/gedit.c,v
retrieving revision 1.125.2.21
diff -u -p -r1.125.2.21 gedit.c
--- gedit.c	18 Sep 2005 13:25:25 -0000	1.125.2.21
+++ gedit.c	22 Sep 2005 13:58:37 -0000
@@ -62,6 +62,7 @@ static guint32 startup_timestamp = 0;
 static BaconMessageConnection *connection;
 
 /* command line */
+static gboolean read_stdin = FALSE;
 static gint line_position = 0;
 static gchar *encoding_charset = NULL;
 static const GeditEncoding *encoding;
@@ -80,6 +81,9 @@ static const struct poptOption options [
 	{ "new-document", '\0', POPT_ARG_NONE, &new_document_option, 0,
 	  N_("Create a new document in an existing instance of gedit"), NULL },
 
+	{ "stdin", '\0', POPT_ARG_NONE, &read_stdin, 0,
+	  N_("stdin stdin stdin!!"), NULL },
+
 	{NULL, '\0', 0, NULL, 0}
 };
 
@@ -505,13 +509,20 @@ main (int argc, char *argv[])
 
 		gedit_debug_message (DEBUG_APP, "Create main window");
 		window = gedit_app_create_window (app);
-		
+
+		/* stdin goes in the first tab */
+		if (read_stdin)
+			gedit_cmd_load_stdin (window, gedit_encoding_get_utf8());
+
+		/* then the files specified */
 		if (file_list != NULL)
 		{
 			gedit_debug_message (DEBUG_APP, "Load files");
 			gedit_cmd_load_files_from_prompt (window, file_list, encoding, line_position);
 		}
-		else
+
+		/* otherwise open an empty doc */
+		else if (!read_stdin)
 		{
 			gedit_debug_message (DEBUG_APP, "Create tab");
 			gedit_window_create_tab (window, TRUE);
@@ -529,6 +540,7 @@ main (int argc, char *argv[])
 		new_window_option = FALSE;
 		new_document_option = FALSE;
 		encoding = NULL;
+		read_stdin = FALSE;
 		line_position = 0;
 	}
 
