Kaydet (Commit) 7159c7c3 authored tarafından Enrico Tröger's avatar Enrico Tröger

Add new command line option --list-documents to return a list of currently…

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

git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@4820 ea778897-0a13-0410-b9d1-a72fbfd435f5
üst 10bdf624
...@@ -13,6 +13,10 @@ ...@@ -13,6 +13,10 @@
* src/toolbar.c: * src/toolbar.c:
Instantly update the toolbar icon size and style when the Instantly update the toolbar icon size and style when the
corresponding global GTK settings are changed. 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> 2010-04-09 Frank Lanitz <frank(at)frank(dot)uvena(dot)de>
......
...@@ -42,6 +42,12 @@ Don't open files in a running instance, force opening a new instance. ...@@ -42,6 +42,12 @@ Don't open files in a running instance, force opening a new instance.
Only available if Geany was compiled with support for Sockets. Only available if Geany was compiled with support for Sockets.
.IP "\fB-l\fP, \fB\-\-line\fP " 10 .IP "\fB-l\fP, \fB\-\-line\fP " 10
Set initial line number for the first opened file. 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 .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 Don't show the message window. Use this option if you don't need compiler messages
or VTE support. or VTE support.
......
...@@ -334,6 +334,14 @@ Short option Long option Function ...@@ -334,6 +334,14 @@ Short option Long option Function
-l --line Set initial line number for the first opened file. -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 -m --no-msgwin Do not show the message window. Use this option if you
do not need compiler messages or VTE support. do not need compiler messages or VTE support.
......
...@@ -127,6 +127,7 @@ static GOptionEntry entries[] = ...@@ -127,6 +127,7 @@ static GOptionEntry entries[] =
#ifdef HAVE_SOCKET #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 }, { "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 }, { "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 #endif
{ "line", 'l', 0, G_OPTION_ARG_INT, &cl_options.goto_line, N_("Set initial line number for the first opened file"), NULL }, { "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 }, { "no-msgwin", 'm', 0, G_OPTION_ARG_NONE, &no_msgwin, N_("Don't show message window at startup"), NULL },
...@@ -478,7 +479,7 @@ static void parse_command_line_options(gint *argc, gchar ***argv) ...@@ -478,7 +479,7 @@ static void parse_command_line_options(gint *argc, gchar ***argv)
GError *error = NULL; GError *error = NULL;
GOptionContext *context; GOptionContext *context;
gint i; 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 */ /* first initialise cl_options fields with default values */
cl_options = def_clo; cl_options = def_clo;
...@@ -945,10 +946,12 @@ gint main(gint argc, gchar **argv) ...@@ -945,10 +946,12 @@ gint main(gint argc, gchar **argv)
socket_info.lock_socket = -1; socket_info.lock_socket = -1;
socket_info.lock_socket_tag = 0; socket_info.lock_socket_tag = 0;
socket_info.lock_socket = socket_init(argc, argv); socket_info.lock_socket = socket_init(argc, argv);
/* Socket exists */
if (socket_info.lock_socket == -2) if (socket_info.lock_socket == -2)
{ {
/* Socket exists */ /* Quit if filenames were sent to first instance or the list of open
if (argc > 1) /* filenames were sent to first instance, so quit */ * documents has been sent */
if (argc > 1 || cl_options.list_documents)
{ {
gdk_notify_startup_complete(); gdk_notify_startup_complete();
g_free(app->configdir); g_free(app->configdir);
......
...@@ -33,6 +33,7 @@ typedef struct ...@@ -33,6 +33,7 @@ typedef struct
gint goto_line; gint goto_line;
gint goto_column; gint goto_column;
gboolean ignore_global_tags; gboolean ignore_global_tags;
gboolean list_documents;
} }
CommandLineOptions; CommandLineOptions;
......
...@@ -50,7 +50,7 @@ ...@@ -50,7 +50,7 @@
enum { enum {
/** The Application Programming Interface (API) version, incremented /** The Application Programming Interface (API) version, incremented
* whenever any plugin data types are modified or appended to. */ * 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 /** The Application Binary Interface (ABI) version, incremented whenever
* existing fields in the plugin data types have to be changed or reordered. */ * existing fields in the plugin data types have to be changed or reordered. */
......
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
* The command window is only available on Windows and takes no additional data, instead it * 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). * 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: * 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 * Geany creates a socket in /tmp (or any other directory returned by g_get_tmp_dir()) and
...@@ -59,7 +59,6 @@ ...@@ -59,7 +59,6 @@
#ifdef HAVE_SOCKET #ifdef HAVE_SOCKET
#ifndef G_OS_WIN32 #ifndef G_OS_WIN32
# include <string.h>
# include <sys/time.h> # include <sys/time.h>
# include <sys/types.h> # include <sys/types.h>
# include <sys/socket.h> # include <sys/socket.h>
...@@ -72,6 +71,7 @@ ...@@ -72,6 +71,7 @@
# include <winsock2.h> # include <winsock2.h>
# include <ws2tcpip.h> # include <ws2tcpip.h>
#endif #endif
#include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
...@@ -97,7 +97,7 @@ ...@@ -97,7 +97,7 @@
#define SOCKET_IS_VALID(s) (G_LIKELY((s) >= 0)) #define SOCKET_IS_VALID(s) (G_LIKELY((s) >= 0))
#define INVALID_SOCKET (-1) #define INVALID_SOCKET (-1)
#endif #endif
#define BUFFER_LENGTH 4096
struct socket_info_struct socket_info; struct socket_info_struct socket_info;
...@@ -191,6 +191,26 @@ static void remove_socket_link_full(void) ...@@ -191,6 +191,26 @@ static void remove_socket_link_full(void)
#endif #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 /* (Unix domain) socket support to replace the old FIFO code
* (taken from Sylpheed, thanks) * (taken from Sylpheed, thanks)
* Returns the created socket, -1 if an error occurred or -2 if another socket exists and files * Returns the created socket, -1 if an error occurred or -2 if another socket exists and files
...@@ -268,6 +288,11 @@ gint socket_init(gint argc, gchar **argv) ...@@ -268,6 +288,11 @@ gint socket_init(gint argc, gchar **argv)
send_open_command(sock, argc, argv); send_open_command(sock, argc, argv);
} }
if (cl_options.list_documents)
{
socket_get_document_list(sock);
}
socket_fd_close(sock); socket_fd_close(sock);
return -2; return -2;
} }
...@@ -515,10 +540,26 @@ static void handle_input_filename(const gchar *buf) ...@@ -515,10 +540,26 @@ static void handle_input_filename(const gchar *buf)
} }
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) gboolean socket_lock_input_cb(GIOChannel *source, GIOCondition condition, gpointer data)
{ {
gint fd, sock; gint fd, sock;
gchar buf[4096]; gchar buf[BUFFER_LENGTH];
struct sockaddr_in caddr; struct sockaddr_in caddr;
guint caddr_len = sizeof(caddr); guint caddr_len = sizeof(caddr);
GtkWidget *window = data; GtkWidget *window = data;
...@@ -538,6 +579,17 @@ gboolean socket_lock_input_cb(GIOChannel *source, GIOCondition condition, gpoint ...@@ -538,6 +579,17 @@ gboolean socket_lock_input_cb(GIOChannel *source, GIOCondition condition, gpoint
} }
popup = TRUE; 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) else if (strncmp(buf, "line", 4) == 0)
{ {
while (socket_fd_gets(sock, buf, sizeof(buf)) != -1 && *buf != '.') while (socket_fd_gets(sock, buf, sizeof(buf)) != -1 && *buf != '.')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment