--- old/gtk/SciTEGTK.cxx	2007-07-02 00:05:27.000000000 +0200
+++ new/gtk/SciTEGTK.cxx	2007-07-02 00:10:13.000000000 +0200
@@ -331,9 +331,12 @@
 	int inputHandle;
 	ElapsedTime commandTime;

+#ifdef NO_FILER
 	// Command Pipe variables
 	int  pipeFD;
 	char pipeName[MAX_PATH];
+	GIOChannel *pipeChannel;
+#endif

 	enum FileFormat { sfSource, sfCopy, sfHTML, sfRTF, sfPDF, sfTEX, sfXML } saveFormat;
 	Dialog dlgFileSelector;
@@ -453,7 +456,9 @@
 	void Command(unsigned long wParam, long lParam = 0);
 	void ContinueExecute(int fromPoll);

-	static void ReadPipe(gpointer data, gint source, GdkInputCondition condition);
+#ifdef NO_FILER
+	static gboolean ReadPipe(GIOChannel *channel, GIOCondition condition, gpointer data);
+#endif
 	void SendFileName(int sendPipe, const char* filename);
 	bool CheckForRunningInstance(int argc, char* argv[]);

@@ -562,7 +567,9 @@
 	        static_cast<int>(getpid()));
 	inputHandle = 0;

+#ifdef NO_FILER
 	pipeFD = -1;
+#endif

 	PropSet::SetCaseSensitiveFilenames(true);
 	propsEmbed.Set("PLAT_GTK", "1");
@@ -2108,12 +2115,14 @@

 void SciTEGTK::QuitProgram() {
 	if (SaveIfUnsureAll() != IDCANCEL) {
+#ifdef NO_FILER
 		//clean up any pipes that are ours
 		if (pipeFD != -1) {
-			//printf("Cleaning up pipe\n");
+			g_io_channel_shutdown(pipeChannel, FALSE, NULL);
 			close(pipeFD);
 			unlink(pipeName);
 		}
+#endif
 		gtk_main_quit();
 	}
 }
@@ -3190,59 +3199,56 @@
 	gdk_window_set_icon(PWidget(wSciTE)->window, NULL, icon_pix, mask);
 }

+#ifdef NO_FILER
 // Callback function that gets called when there is data to be read
 // from the pipe.
-void SciTEGTK::ReadPipe(gpointer data, gint source, GdkInputCondition condition) {
-
-	// Shouldn't happen.  We're just looking for data to read.
-	if (condition != GDK_INPUT_READ)
-		return;
+gboolean SciTEGTK::ReadPipe(GIOChannel *channel, GIOCondition, gpointer data) {

-	int readLength;
-	char pipeData[8192];
+	GIOStatus status;
+	gchar *pipeData;
 	SciTEGTK* scitew = reinterpret_cast<SciTEGTK *>(data);

-	// Multiple filenames could be read in one read call.  They will be NULL
-	// separated.  An empty string means the window should be brought forward.
-	while ((readLength = read(source, pipeData, sizeof(pipeData))) > 0) {
-		char *ii = pipeData;
-		char *start = pipeData;
-		char *end = pipeData + readLength;
-
-		while (ii < end) {
-			while ((ii < end) && (*ii != '\0'))
-				++ii;
-
-			if (strlen(start) > 0)
-				scitew->Open(start);
-#if GTK_MAJOR_VERSION >= 2
-			else
-				gtk_window_present(GTK_WINDOW(scitew->GetID()));
-#endif
-			start = ++ii;
+	status = g_io_channel_read_line(channel, &pipeData, NULL, NULL, NULL);
+	if (status == G_IO_STATUS_NORMAL) {
+		if (g_str_has_prefix(pipeData, "open:")) {
+			char *filename = pipeData + 5; // Skip "open:"
+			scitew->Open(filename);
+		} else {
+			// TODO : message to front the existing instance
+			//gtk_window_present(GTK_WINDOW(scitew->GetID()));
 		}
 	}
+	g_free(pipeData);
+	return TRUE;
 }
+#endif

-// Send the filename through the pipe.  Make the path absolute if it is not
-// already.  If filename is empty, one NULL character will be written.
-// This signifies that the existing instance should present itself.
+// Send the filename through the pipe using the director command "open:"
+// Make the path absolute if it is not already.
+// If filename is empty, we send a message to the existing instance to tell
+// it to present itself (ie. the window should come to the front)
 void SciTEGTK::SendFileName(int sendPipe, const char* filename) {

-	// Create the command to send thru the pipe.
-	char pipeData[MAX_PATH];
+	SString command;
+	const char *pipeData;

-	// Check to see if path is already absolute.  If it isn't then add the
-	// absolute path to the front of the command to send.
-	if (g_path_is_absolute(filename) || (strlen(filename) == 0)) {
-		snprintf(pipeData, sizeof(pipeData) - 1, "%s", filename);
+	if (strlen(filename) != 0) {
+		// Create the command to send thru the pipe.
+		command = "open:";
+
+		// Check to see if path is already absolute.
+		if (!g_path_is_absolute(filename)) {
+			gchar *currentPath = g_get_current_dir();
+			command += currentPath;
+			command += '/';
+			g_free(currentPath);
+		}
+		command += filename;
 	} else {
-		gchar *currentPath = g_get_current_dir();
-		snprintf(pipeData, sizeof(pipeData) - 1, "%s/%s", currentPath, filename);
-		g_free(currentPath);
+		// TODO
 	}
-	pipeData[sizeof(pipeData) - 1] = '\0';

+	pipeData = command.c_str();
 	// Send it.
 	if (write(sendPipe, pipeData, strlen(pipeData) + 1) == -1)
 		perror("Unable to write to pipe");
@@ -3250,38 +3256,59 @@

 bool SciTEGTK::CheckForRunningInstance(int argc, char *argv[]) {

-	// Use ipc.scite.name for the pipe name if it exists.
-	const SString pipeFilename = props.Get("ipc.scite.name");
-
-	if (pipeFilename.size() > 0)
-		snprintf(pipeName, sizeof(pipeName), "%s", pipeFilename.c_str());
-	else
-		snprintf(pipeName, sizeof(pipeName), "%s/.SciTE.%s.ipc", g_get_tmp_dir (), g_get_user_name());
+	const gchar *tmpdir = g_get_tmp_dir();
+	GDir *dir = g_dir_open(tmpdir, 0, NULL);
+	if (dir == NULL)
+		return false; // Couldn't open the directory
+
+	GPatternSpec *pattern = g_pattern_spec_new("SciTE.*.in");
+
+	char *pipeFileName = NULL;
+	const char *filename;
+
+	// Find a working pipe in our temporary directory
+	while ((filename = g_dir_read_name(dir))) {
+		if (g_pattern_match_string(pattern, filename)) {
+			pipeFileName = g_build_filename(tmpdir, filename, NULL);
+
+			// Attempt to open the pipe as a writer to send out data.
+			int sendPipe = open(pipeFileName, O_WRONLY | O_NONBLOCK);
+
+			// If open succeeded, write filename data.
+			if (sendPipe != -1) {
+				for (int ii = 1; ii < argc; ++ii) {
+					if (argv[ii][0] != '-')
+						SendFileName(sendPipe, argv[ii]);
+				}

-	// Attempt to open the pipe as a writer to send out data.
-	int sendPipe = open(pipeName, O_WRONLY | O_NONBLOCK);
+				// Force the SciTE instance to come to the front.
+				SendFileName(sendPipe, "");

-	// If open succeeded, write filename data.
-	if (sendPipe != -1) {
-		for (int ii = 1; ii < argc; ++ii) {
-			if (argv[ii][0] != '-')
-				SendFileName(sendPipe, argv[ii]);
+				// We're done
+				if (close(sendPipe) == -1)
+					perror("Unable to close pipe");
+				break;
+			} else {
+				// We don't care about the error. Try another pipe.
+				pipeFileName = NULL;
+			}
 		}
-
-		// Force the SciTE instance to come to the front.
-		SendFileName(sendPipe, "");
-		return true;
 	}
+	g_pattern_spec_free(pattern);
+	g_dir_close(dir);

-	// If pipe doesn't exist, create it.  If pipe exists without a
-	// reader, do nothing.  Return an error on any other condition.
-	if (errno == ENOENT)
-		MakePipe(pipeName);
-	else if (errno != ENXIO) {
-		perror("Unable to open pipe as writer");
+	if (pipeFileName != NULL) {
+		// We need to call this since we're not displaying a window
+		gdk_notify_startup_complete();
+		g_free(pipeFileName);
 		return true;
 	}

+#ifdef NO_FILER
+	// If pipe doesn't exist, create it. Normally created by director extension.
+	snprintf(pipeName, sizeof(pipeName), "/%s/SciTE.%d.in", tmpdir, getpid());
+	MakePipe(pipeName);
+
 	// Now open it as a reader to receive data.
 	pipeFD = open(pipeName, O_RDWR | O_NONBLOCK);
 	if (pipeFD == -1) {
@@ -3291,7 +3318,13 @@
 	}

 	// Handler to read data.
-	gdk_input_add(pipeFD, GDK_INPUT_READ, ReadPipe, this);
+	pipeChannel = g_io_channel_unix_new(pipeFD);
+	g_io_channel_set_encoding(pipeChannel, NULL, NULL);
+	g_io_add_watch(pipeChannel, G_IO_IN, ReadPipe, this);
+#endif
+
+	// If we arrived here, there is no SciTE instance we could talk to.
+	// We'll start a new one
 	return false;
 }

