SF.net SVN: geany:[4820] trunk

eht16 at users.sourceforge.net eht16 at xxxxx
Sun Apr 11 21:56:50 UTC 2010


Revision: 4820
          http://geany.svn.sourceforge.net/geany/?rev=4820&view=rev
Author:   eht16
Date:     2010-04-11 21:56:50 +0000 (Sun, 11 Apr 2010)

Log Message:
-----------
Add new command line option --list-documents to return a list of currently opened documents (closes #2979933).

Modified Paths:
--------------
    trunk/ChangeLog
    trunk/doc/geany.1.in
    trunk/doc/geany.txt
    trunk/src/main.c
    trunk/src/main.h
    trunk/src/plugindata.h
    trunk/src/socket.c

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2010-04-11 21:56:34 UTC (rev 4819)
+++ trunk/ChangeLog	2010-04-11 21:56:50 UTC (rev 4820)
@@ -13,6 +13,10 @@
  * src/toolbar.c:
    Instantly update the toolbar icon size and style when the
    corresponding global GTK settings are changed.
+ * doc/geany.1.in, doc/geany.txt, src/main.c, src/main.h,
+   src/plugindata.h, src/socket.c:
+   Add new command line option --list-documents to return a list
+   of currently opened documents (closes #2979933).
 
 
 2010-04-09  Frank Lanitz  <frank(at)frank(dot)uvena(dot)de>

Modified: trunk/doc/geany.1.in
===================================================================
--- trunk/doc/geany.1.in	2010-04-11 21:56:34 UTC (rev 4819)
+++ trunk/doc/geany.1.in	2010-04-11 21:56:50 UTC (rev 4820)
@@ -42,6 +42,12 @@
 Only available if Geany was compiled with support for Sockets.
 .IP "\fB-l\fP, \fB\-\-line\fP         " 10
 Set initial line number for the first opened file.
+.IP "\fB\fP    \fB\-\-list\-documents\fP         " 10
+Return a list of open documents in a running Geany instance.
+This can be used to read the currently opened documents in Geany from an external script
+or tool. The returned list is separated by newlines (LF) and consists of the full,
+UTF\-8 encoded filenames of the documents.
+Only available if Geany was compiled with support for Sockets.
 .IP "\fB-m\fP, \fB\-\-no-msgwin\fP         " 10
 Don't show the message window. Use this option if you don't need compiler messages
 or VTE support.

Modified: trunk/doc/geany.txt
===================================================================
--- trunk/doc/geany.txt	2010-04-11 21:56:34 UTC (rev 4819)
+++ trunk/doc/geany.txt	2010-04-11 21:56:50 UTC (rev 4820)
@@ -334,6 +334,14 @@
 
 -l            --line                   Set initial line number for the first opened file.
 
+*none*        --list-documents         Return a list of open documents in a running Geany instance.
+                                       This can be used to read the currently opened documents in
+                                       Geany from an external script or tool. The returned list
+                                       is separated by newlines (LF) and consists of the full,
+                                       UTF-8 encoded filenames of the documents.
+                                       Only available if Geany was compiled with support for
+                                       Sockets.
+
 -m            --no-msgwin              Do not show the message window. Use this option if you
                                        do not need compiler messages or VTE support.
 

Modified: trunk/src/main.c
===================================================================
--- trunk/src/main.c	2010-04-11 21:56:34 UTC (rev 4819)
+++ trunk/src/main.c	2010-04-11 21:56:50 UTC (rev 4820)
@@ -127,6 +127,7 @@
 #ifdef HAVE_SOCKET
 	{ "new-instance", 'i', 0, G_OPTION_ARG_NONE, &cl_options.new_instance, N_("Don't open files in a running instance, force opening a new instance"), NULL },
 	{ "socket-file", 0, 0, G_OPTION_ARG_FILENAME, &cl_options.socket_filename, N_("Use this socket filename for communication with a running Geany instance"), NULL },
+	{ "list-documents", 0, 0, G_OPTION_ARG_NONE, &cl_options.list_documents, N_("Return a list of open documents in a running Geany instance"), NULL },
 #endif
 	{ "line", 'l', 0, G_OPTION_ARG_INT, &cl_options.goto_line, N_("Set initial line number for the first opened file"), NULL },
 	{ "no-msgwin", 'm', 0, G_OPTION_ARG_NONE, &no_msgwin, N_("Don't show message window at startup"), NULL },
@@ -478,7 +479,7 @@
 	GError *error = NULL;
 	GOptionContext *context;
 	gint i;
-	CommandLineOptions def_clo = {FALSE, NULL, TRUE, -1, -1, FALSE};
+	CommandLineOptions def_clo = {FALSE, NULL, TRUE, -1, -1, FALSE, FALSE};
 
 	/* first initialise cl_options fields with default values */
 	cl_options = def_clo;
@@ -945,10 +946,12 @@
 		socket_info.lock_socket = -1;
 		socket_info.lock_socket_tag = 0;
 		socket_info.lock_socket = socket_init(argc, argv);
+		/* Socket exists */
 		if (socket_info.lock_socket == -2)
 		{
-			/* Socket exists */
-			if (argc > 1)	/* filenames were sent to first instance, so quit */
+			/* Quit if filenames were sent to first instance or the list of open
+			 * documents has been sent */
+			if (argc > 1 || cl_options.list_documents)
 			{
 				gdk_notify_startup_complete();
 				g_free(app->configdir);

Modified: trunk/src/main.h
===================================================================
--- trunk/src/main.h	2010-04-11 21:56:34 UTC (rev 4819)
+++ trunk/src/main.h	2010-04-11 21:56:50 UTC (rev 4820)
@@ -33,6 +33,7 @@
 	gint		goto_line;
 	gint		goto_column;
 	gboolean	ignore_global_tags;
+	gboolean	list_documents;
 }
 CommandLineOptions;
 

Modified: trunk/src/plugindata.h
===================================================================
--- trunk/src/plugindata.h	2010-04-11 21:56:34 UTC (rev 4819)
+++ trunk/src/plugindata.h	2010-04-11 21:56:50 UTC (rev 4820)
@@ -50,7 +50,7 @@
 enum {
 	/** The Application Programming Interface (API) version, incremented
 	 * whenever any plugin data types are modified or appended to. */
-	GEANY_API_VERSION = 181,
+	GEANY_API_VERSION = 182,
 
 	/** The Application Binary Interface (ABI) version, incremented whenever
 	 * existing fields in the plugin data types have to be changed or reordered. */

Modified: trunk/src/socket.c
===================================================================
--- trunk/src/socket.c	2010-04-11 21:56:34 UTC (rev 4819)
+++ trunk/src/socket.c	2010-04-11 21:56:50 UTC (rev 4820)
@@ -42,7 +42,7 @@
  * The command window is only available on Windows and takes no additional data, instead it
  * writes back a Windows handle (HWND) for the main window to set it to the foreground (focus).
  *
- * At the moment the commands window, open, line and column are available.
+ * At the moment the commands window, doclist, open, line and column are available.
  *
  * About the socket files on Unix-like systems:
  * Geany creates a socket in /tmp (or any other directory returned by g_get_tmp_dir()) and
@@ -59,7 +59,6 @@
 #ifdef HAVE_SOCKET
 
 #ifndef G_OS_WIN32
-# include <string.h>
 # include <sys/time.h>
 # include <sys/types.h>
 # include <sys/socket.h>
@@ -72,6 +71,7 @@
 # include <winsock2.h>
 # include <ws2tcpip.h>
 #endif
+#include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <fcntl.h>
@@ -97,8 +97,8 @@
 #define SOCKET_IS_VALID(s)	(G_LIKELY((s) >= 0))
 #define INVALID_SOCKET		(-1)
 #endif
+#define BUFFER_LENGTH 4096
 
-
 struct socket_info_struct socket_info;
 
 
@@ -191,6 +191,26 @@
 #endif
 
 
+static void socket_get_document_list(gint sock)
+{
+	gchar doc_list[BUFFER_LENGTH];
+	gint doc_list_len;
+
+	if (sock < 0)
+		return;
+
+	socket_fd_write_all(sock, "doclist\n", 8);
+
+	doc_list_len = socket_fd_read(sock, doc_list, sizeof(doc_list));
+	if (doc_list_len >= BUFFER_LENGTH)
+		doc_list_len = BUFFER_LENGTH -1;
+	doc_list[doc_list_len] = '\0';
+	/* if we received ETX (end-of-text), there were no open files, so print only otherwise */
+	if (! utils_str_equal(doc_list, "\3"))
+		printf("%s", doc_list);
+}
+
+
 /* (Unix domain) socket support to replace the old FIFO code
  * (taken from Sylpheed, thanks)
  * Returns the created socket, -1 if an error occurred or -2 if another socket exists and files
@@ -268,6 +288,11 @@
 		send_open_command(sock, argc, argv);
 	}
 
+	if (cl_options.list_documents)
+	{
+		socket_get_document_list(sock);
+	}
+
 	socket_fd_close(sock);
 	return -2;
 }
@@ -515,10 +540,26 @@
 }
 
 
+static gchar *build_document_list(void)
+{
+	GString *doc_list = g_string_new(NULL);
+	guint i;
+	const gchar *filename;
+
+	foreach_document(i)
+	{
+		filename = DOC_FILENAME(documents[i]);
+		g_string_append(doc_list, filename);
+		g_string_append_c(doc_list, '\n');
+	}
+	return g_string_free(doc_list, FALSE);
+}
+
+
 gboolean socket_lock_input_cb(GIOChannel *source, GIOCondition condition, gpointer data)
 {
 	gint fd, sock;
-	gchar buf[4096];
+	gchar buf[BUFFER_LENGTH];
 	struct sockaddr_in caddr;
 	guint caddr_len = sizeof(caddr);
 	GtkWidget *window = data;
@@ -538,6 +579,17 @@
 			}
 			popup = TRUE;
 		}
+		else if (strncmp(buf, "doclist", 7) == 0)
+		{
+			gchar *doc_list = build_document_list();
+			if (NZV(doc_list))
+				socket_fd_write_all(sock, doc_list, strlen(doc_list));
+			else
+				/* send ETX (end-of-text) in case we have no open files, we must send anything
+				 * otherwise the client would hang on reading */
+				socket_fd_write_all(sock, "\3", 1);
+			g_free(doc_list);
+		}
 		else if (strncmp(buf, "line", 4) == 0)
 		{
 			while (socket_fd_gets(sock, buf, sizeof(buf)) != -1 && *buf != '.')


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



More information about the Commits mailing list