[Geany-Devel] Querying current file and cursor location

Thomas Young thomasyoung at free.fr
Fri Jan 18 08:10:14 UTC 2013


Cheers Steve.

Ok, I changed spaces to tabs, and a patch from diff output is attached.
Will try and do things more properly next time!

Thomas

On 17/01/13 17:15, Steven Blatnick wrote:
> I'm new too, but the first time I submitted something, they took a patch
> I attached to an email sent to this mailing list.  Subsequent
> submissions, I signed up for a github account and forked geany and
> geany-plugins.  Using git hub, changes can be submitted through a "pull
> request".  I also signed up to SourceForge for submitting bugs.
>
> Here is where I read about the process:
> http://www.geany.org/Contribute/Developers
>
> So far I've found the people on this project have been helpful and
> friendly compared to my minor previous experiences with open source
> projects.
>
> Welcome :-)
>
> Steven Blatnick
>
>
> On 01/17/2013 06:17 AM, Thomas Young wrote:
>> Hello,
>>
>> I've hacked a small change into Geany locally, to help with
>> integration with gdb.
>>
>> Basically, what I've done is to add a '--print-location' command line
>> option, which essentially just duplicates the functionality of the
>> existing '--list-documents' option (through main.c/h and socket.c/h),
>> but with the actual string generation bit that corresponds to
>> build_document_list() changed to the following:
>>
>> static gchar *build_location(void)
>> {
>>     GString *location = g_string_new(NULL);
>>     const gchar *filename;
>>     gint pos, line, col;
>>
>>     GeanyDocument *doc = document_get_current();
>>     if(doc)
>>     {
>>         filename = DOC_FILENAME(doc);
>>         pos = sci_get_current_position(doc->editor->sci);
>>         line = sci_get_line_from_position(doc->editor->sci, pos);
>>         col = sci_get_col_from_position(doc->editor->sci, pos);
>>         g_string_append_printf(location, "%s:%d:%d", filename, line +
>> 1, col);
>>         g_string_append_c(location, '\n');
>>     }
>>     return g_string_free(location, FALSE);
>> }
>>
>> So what the overall change does then is basically to allow you to
>> query for the currently selected file, and current cursor location
>> within that file, from outside of geany, like so:
>>
>> thomas at MS-7752 ~ $ geany --print-location
>> /home/thomas/Downloads/geany_git/src/socket.c:600:33
>>
>> I can then use this in a python command within gdb (running completely
>> separately within a terminal) to set break points for positions
>> selected in files in Geany within having to actually type the whole
>> filename and line number bit each time.
>>
>> This was based on the nightly build for January 17th, and seems to be
>> working correctly.
>>
>> Should I submit a patch for this change?
>> If so, how?
>> (Not used to this whole open source malarky!)
>>
>> I got the nightly build archive instead of using git, but I have
>> copies of the source archive both before and after the change.
>>
>> Is it possible for this change (or something similar) to be included
>> in the Geany mainline?
>>
>> Best regards,
>>
>> Thomas
>> _______________________________________________
>> Devel mailing list
>> Devel at lists.geany.org
>> https://lists.geany.org/cgi-bin/mailman/listinfo/devel
>
> _______________________________________________
> Devel mailing list
> Devel at lists.geany.org
> https://lists.geany.org/cgi-bin/mailman/listinfo/devel
>
-------------- next part --------------
diff -u geany_before_changes_17_jan/src/main.c geany_git/src/main.c
--- geany_before_changes_17_jan/src/main.c	2013-01-15 22:28:00.000000000 +0100
+++ geany_git/src/main.c	2013-01-17 13:08:27.607352186 +0100
@@ -127,6 +127,7 @@
 	{ "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 },
+	{ "print-location", 0, 0, G_OPTION_ARG_NONE, &cl_options.print_location, N_("Prints current open file and cursor location 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 },
@@ -1007,7 +1008,7 @@
 		{
 			/* Quit if filenames were sent to first instance or the list of open
 			 * documents has been sent */
-			if (argc > 1 || cl_options.list_documents)
+			if (argc > 1 || cl_options.list_documents || cl_options.print_location)
 			{
 				gdk_notify_startup_complete();
 				g_free(app->configdir);
diff -u geany_before_changes_17_jan/src/main.h geany_git/src/main.h
--- geany_before_changes_17_jan/src/main.h	2013-01-15 22:28:00.000000000 +0100
+++ geany_git/src/main.h	2013-01-17 13:08:27.675352227 +0100
@@ -32,6 +32,7 @@
 	gint		goto_column;
 	gboolean	ignore_global_tags;
 	gboolean	list_documents;
+	gboolean	print_location;
 	gboolean 	readonly;
 }
 CommandLineOptions;
diff -u geany_before_changes_17_jan/src/socket.c geany_git/src/socket.c
--- geany_before_changes_17_jan/src/socket.c	2013-01-15 22:28:00.000000000 +0100
+++ geany_git/src/socket.c	2013-01-18 08:46:47.673275312 +0100
@@ -40,7 +40,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, doclist, open, openro, line and column are available.
+ * At the moment the commands window, doclist, location, open, openro, 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
@@ -211,6 +211,24 @@
 	if (! utils_str_equal(doc_list, "\3"))
 		printf("%s", doc_list);
 }
+static void socket_get_location(gint sock)
+{
+	gchar location[BUFFER_LENGTH];
+	gint location_len;
+
+	if (sock < 0)
+		return;
+
+	socket_fd_write_all(sock, "location\n", 9);
+
+	location_len = socket_fd_read(sock, location, sizeof(location));
+	if (location_len >= BUFFER_LENGTH)
+		location_len = BUFFER_LENGTH -1;
+	location[location_len] = '\0';
+	/* if we received ETX (end-of-text), there were no open files, so print only otherwise */
+	if (! utils_str_equal(location, "\3"))
+		printf("%s", location);
+}
 
 
 #ifndef G_OS_WIN32
@@ -324,6 +342,10 @@
 	{
 		socket_get_document_list(sock);
 	}
+	if (cl_options.print_location)
+	{
+		socket_get_location(sock);
+	}
 
 	socket_fd_close(sock);
 	return -2;
@@ -589,6 +611,23 @@
 	}
 	return g_string_free(doc_list, FALSE);
 }
+static gchar *build_location(void)
+{
+	GString *location = g_string_new(NULL);
+	const gchar *filename;
+	gint pos, line, col;
+
+	GeanyDocument *doc = document_get_current();
+	if(doc)
+	{
+		filename = DOC_FILENAME(doc);
+		pos = sci_get_current_position(doc->editor->sci);
+		line = sci_get_line_from_position(doc->editor->sci, pos);
+		col = sci_get_col_from_position(doc->editor->sci, pos);
+		g_string_append_printf(location, "%s:%d:%d\n", filename, line + 1, col);
+	}
+	return g_string_free(location, FALSE);
+}
 
 
 gboolean socket_lock_input_cb(GIOChannel *source, GIOCondition condition, gpointer data)
@@ -632,6 +671,17 @@
 				socket_fd_write_all(sock, "\3", 1);
 			g_free(doc_list);
 		}
+		else if (strncmp(buf, "location", 8) == 0)
+		{
+			gchar *location = build_location();
+			if (NZV(location))
+				socket_fd_write_all(sock, location, strlen(location));
+			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(location);
+		}
 		else if (strncmp(buf, "line", 4) == 0)
 		{
 			while (socket_fd_gets(sock, buf, sizeof(buf)) != -1 && *buf != '.')



More information about the Devel mailing list