[geany/geany-plugins] 738b3e: Implemented go_to_file feature

federeghe git-noreply at xxxxx
Thu Sep 11 20:39:00 UTC 2014


Branch:      refs/heads/master
Author:      federeghe <federico.dev at reghe.net>
Committer:   federeghe <federico.dev at reghe.net>
Date:        Sun, 23 Mar 2014 21:28:41 UTC
Commit:      738b3eba7b4393ee25a36e1cfe87530a014a6ffd
             https://github.com/geany/geany-plugins/commit/738b3eba7b4393ee25a36e1cfe87530a014a6ffd

Log Message:
-----------
Implemented go_to_file feature


Modified Paths:
--------------
    codenav/src/codenavigation.h
    codenav/src/goto_file.c
    codenav/src/utils.c
    codenav/src/utils.h

Modified: codenav/src/codenavigation.h
44 lines changed, 24 insertions(+), 20 deletions(-)
===================================================================
@@ -1,23 +1,24 @@
 /*
- *      codenavigation.h - this file is part of "codenavigation", which is
- *      part of the "geany-plugins" project.
+ *	  codenavigation.h - this file is part of "codenavigation", which is
+ *	  part of the "geany-plugins" project.
  *
- *      Copyright 2009 Lionel Fuentes <funto66(at)gmail(dot)com>
+ *	  Copyright 2009 Lionel Fuentes <funto66(at)gmail(dot)com>
+ *	  Copyright 2014 Federico Reghenzani <federico(dot)dev(at)reghe(dot)net>
  *
- *      This program is free software; you can redistribute it and/or modify
- *      it under the terms of the GNU General Public License as published by
- *      the Free Software Foundation; either version 2 of the License, or
- *      (at your option) any later version.
+ *	  This program is free software; you can redistribute it and/or modify
+ *	  it under the terms of the GNU General Public License as published by
+ *	  the Free Software Foundation; either version 2 of the License, or
+ *	  (at your option) any later version.
  *
- *      This program is distributed in the hope that it will be useful,
- *      but WITHOUT ANY WARRANTY; without even the implied warranty of
- *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *      GNU General Public License for more details.
+ *	  This program is distributed in the hope that it will be useful,
+ *	  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *	  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *	  GNU General Public License for more details.
  *
- *      You should have received a copy of the GNU General Public License
- *      along with this program; if not, write to the Free Software
- *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- *      MA 02110-1301, USA.
+ *	  You should have received a copy of the GNU General Public License
+ *	  along with this program; if not, write to the Free Software
+ *	  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ *	  MA 02110-1301, USA.
  */
 
 #ifndef CODENAVIGATION_H
@@ -36,6 +37,7 @@
 #include "keybindings.h"
 #include "filetypes.h"
 #include <gdk/gdkkeysyms.h>
+
 #include <string.h>
 
 #include "switch_head_impl.h"
@@ -45,13 +47,15 @@
 #include "geanyfunctions.h"	/* this wraps geany_functions function pointers */
 
 /* Debug flag */
-/*#define CODE_NAVIGATION_DEBUG
-*/
+//#define CODE_NAVIGATION_DEBUG
 
-#define CODE_NAVIGATION_VERSION "0.1"
+#define CODE_NAVIGATION_VERSION "0.2"
 
 /* Log utilities */
 #ifdef CODE_NAVIGATION_DEBUG
+#include <glib/gprintf.h>
+
+
 static void log_debug(const gchar* s, ...)
 {
 	gchar* format = g_strconcat("[CODENAV DEBUG] : ", s, "\n", NULL);
@@ -64,8 +68,8 @@ static void log_debug(const gchar* s, ...)
 
 #define log_func() g_print("[CODENAV FUNC] : %s", G_STRFUNC)
 #else
-static void log_debug(const gchar* s, ...) {}
-#define log_func() {}
+#define log_debug(...)
+#define log_func() 
 #endif
 
 /* IDs for keybindings */


Modified: codenav/src/goto_file.c
211 lines changed, 171 insertions(+), 40 deletions(-)
===================================================================
@@ -1,23 +1,24 @@
 /*
- *      goto_file.c - this file is part of "codenavigation", which is
- *      part of the "geany-plugins" project.
+ *	  goto_file.c - this file is part of "codenavigation", which is
+ *	  part of the "geany-plugins" project.
  *
- *      Copyright 2009 Lionel Fuentes <funto66(at)gmail(dot)com>
+ *	  Copyright 2009 Lionel Fuentes <funto66(at)gmail(dot)com>
+ *	  Copyright 2014 Federico Reghenzani <federico(dot)dev(at)reghe(dot)net>
+ * 
+ *	  This program is free software; you can redistribute it and/or modify
+ *	  it under the terms of the GNU General Public License as published by
+ *	  the Free Software Foundation; either version 2 of the License, or
+ *	  (at your option) any later version.
  *
- *      This program is free software; you can redistribute it and/or modify
- *      it under the terms of the GNU General Public License as published by
- *      the Free Software Foundation; either version 2 of the License, or
- *      (at your option) any later version.
+ *	  This program is distributed in the hope that it will be useful,
+ *	  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *	  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *	  GNU General Public License for more details.
  *
- *      This program is distributed in the hope that it will be useful,
- *      but WITHOUT ANY WARRANTY; without even the implied warranty of
- *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *      GNU General Public License for more details.
- *
- *      You should have received a copy of the GNU General Public License
- *      along with this program; if not, write to the Free Software
- *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- *      MA 02110-1301, USA.
+ *	  You should have received a copy of the GNU General Public License
+ *	  along with this program; if not, write to the Free Software
+ *	  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ *	  MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
@@ -26,19 +27,23 @@
 #include <geanyplugin.h>
 
 #include "goto_file.h"
+#include "utils.h"
+
+#define MAX_FILENAME_LENGTH 255
 
 /******************* Global variables for the feature *****************/
 
 static GtkWidget* menu_item = NULL;
 
-/********************** Functions for the feature *********************/
-
-/* ---------------------------------------------------------------------
- * Callback when the menu item is clicked.
- * ---------------------------------------------------------------------
- */
+/********************** Prototypes *********************/
 static void
-menu_item_activate(guint key_id);
+menu_item_activate(guint);
+
+static GtkTreeModel* 
+build_file_list(gchar*);
+
+
+/********************** Functions for the feature *********************/
 
 /* ---------------------------------------------------------------------
  * Initialization
@@ -54,7 +59,7 @@ goto_file_init(void)
 	edit_menu = ui_lookup_widget(geany->main_widgets->window, "edit1_menu");
 
 	/* Add the menu item, sensitive only when a document is opened */
-	menu_item = gtk_menu_item_new_with_mnemonic(_("Goto file"));
+	menu_item = gtk_menu_item_new_with_mnemonic(_("Go to File..."));
 	gtk_widget_show(menu_item);
 	gtk_container_add(GTK_CONTAINER(edit_menu), menu_item);
 	ui_add_document_sensitive(menu_item);
@@ -68,7 +73,7 @@ goto_file_init(void)
  							(GeanyKeyCallback)(&menu_item_activate),
  							GDK_g, GDK_MOD1_MASK | GDK_SHIFT_MASK,
  							"goto_file",
- 							_("Goto file"),	/* used in the Preferences dialog */
+ 							_("Go to File"),	/* used in the Preferences dialog */
  							menu_item);
 }
 
@@ -85,29 +90,155 @@ goto_file_cleanup(void)
 }
 
 /* ---------------------------------------------------------------------
+ * Populate the file list
+ * ---------------------------------------------------------------------
+ */
+
+static GtkTreeModel* 
+build_file_list(gchar* dirname)
+{
+	GtkListStore *ret_list;
+	GtkTreeIter iter;
+	ret_list = gtk_list_store_new (1, G_TYPE_STRING);
+	
+	GSList* file_iterator;
+	GSList* files_list;
+	gchar *file;
+	gchar *pathfile;
+	guint files_n;
+
+	files_list = file_iterator = utils_get_file_list(dirname, &files_n, NULL);
+	
+	for( ; file_iterator; file_iterator = file_iterator->next) 
+	{
+		file = file_iterator->data;		
+		
+		pathfile = g_build_filename(dirname,file,NULL);   
+	
+		if ( ! g_file_test(pathfile, G_FILE_TEST_IS_DIR) )
+		{   /* File */
+			gtk_list_store_append (ret_list, &iter);
+			gtk_list_store_set (ret_list, &iter, 0, file, -1);
+
+		}
+		/* TODO: sub-directory support */
+		g_free(pathfile);
+	}
+	
+	g_slist_foreach(files_list, (GFunc) g_free, NULL);
+	g_slist_free(files_list);
+	
+	return GTK_TREE_MODEL(ret_list);
+ 
+}
+
+/* ---------------------------------------------------------------------
+ * Create the dialog, return the entry
+ * ---------------------------------------------------------------------
+ */
+static GtkWidget*
+create_dialog(GtkWidget **dialog, GtkTreeModel *completion_model)
+{
+	GtkWidget *entry;
+	GtkWidget *label;
+	GtkWidget *vbox;
+	GtkEntryCompletion *completion;
+		
+	*dialog = gtk_dialog_new_with_buttons("Go to File...", GTK_WINDOW(geany->main_widgets->window),
+	GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+	GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
+	
+	gtk_dialog_set_default_response(GTK_DIALOG(*dialog), GTK_RESPONSE_ACCEPT);
+	
+	gtk_widget_set_name(*dialog, "GotoFile");
+	vbox = ui_dialog_vbox_new(GTK_DIALOG(*dialog));
+
+	label = gtk_label_new(_("Enter the file you want to open:"));
+	gtk_container_add(GTK_CONTAINER(vbox), label);
+
+	/* Entry definition */
+	entry = gtk_entry_new();
+	gtk_container_add(GTK_CONTAINER(vbox), entry);
+	gtk_entry_set_text(GTK_ENTRY(entry), "");
+	gtk_entry_set_max_length(GTK_ENTRY(entry), MAX_FILENAME_LENGTH);
+	gtk_entry_set_width_chars(GTK_ENTRY(entry), 30);
+	gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);   /* 'enter' key */
+	
+	/* Completion definition */
+	completion = gtk_entry_completion_new();
+	gtk_entry_set_completion(GTK_ENTRY(entry), completion);
+	gtk_entry_completion_set_model (completion, completion_model);
+	
+	/* Completion options */
+	gtk_entry_completion_set_inline_completion(completion, 1);
+	gtk_entry_completion_set_text_column (completion, 0);
+
+
+	gtk_widget_show_all(*dialog);
+
+	return entry;
+}
+
+/* ---------------------------------------------------------------------
  * Callback when the menu item is clicked.
  * ---------------------------------------------------------------------
  */
 static void
 menu_item_activate(guint key_id)
 {
-	GtkWidget *dialog;
+	GtkWidget* dialog;
+	GtkWidget* dialog_new = NULL;
+	GtkWidget* dialog_entry;
+	GtkTreeModel* completion_list;
+	GeanyDocument* current_doc = document_get_current();
+	gchar *dirname, *chosen_path;
+	const gchar *chosen_file;
+	gint response;
 
 	log_func();
 
-	/* TODO */
-	dialog = gtk_message_dialog_new(
-		GTK_WINDOW(geany->main_widgets->window),
-		GTK_DIALOG_DESTROY_WITH_PARENT,
-		GTK_MESSAGE_INFO,
-		GTK_BUTTONS_OK,
-		"Open by name : TODO");
-
-	gtk_window_set_title(GTK_WINDOW(dialog), "Goto file...");
-
-	gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
-		_("(From the %s plugin)"), geany_plugin->info->name);
-
-	gtk_dialog_run(GTK_DIALOG(dialog));
+	if(current_doc == NULL || current_doc->file_name == NULL || current_doc->file_name[0] == '\0')
+		return;
+		
+	/* Build current directory listing */
+	dirname = g_path_get_dirname(current_doc->file_name);
+	completion_list = build_file_list(dirname);
+
+	/* Create the user dialog and get response */
+	dialog_entry = create_dialog(&dialog, completion_list);
+	response = gtk_dialog_run(GTK_DIALOG(dialog));
+
+	/* Filename */
+	chosen_file = gtk_entry_get_text(GTK_ENTRY(dialog_entry));
+	/* Path + Filename */
+	chosen_path = g_build_filename(dirname, chosen_file, NULL);
+
+	if ( response == GTK_RESPONSE_ACCEPT )
+	{
+		log_debug("Trying to open: %s", chosen_path);
+		if ( ! g_file_test(chosen_path, G_FILE_TEST_EXISTS) )
+		{
+			log_debug("File not found.");
+
+			dialog_new = gtk_message_dialog_new(GTK_WINDOW(geany_data->main_widgets->window),
+													GTK_DIALOG_MODAL,
+													GTK_MESSAGE_QUESTION,
+													GTK_BUTTONS_OK_CANCEL,
+													_("%s not found, create it?"), chosen_file);
+			gtk_window_set_title(GTK_WINDOW(dialog_new), "Geany");
+			if(gtk_dialog_run(GTK_DIALOG(dialog_new)) == GTK_RESPONSE_OK)
+			{
+				document_new_file(chosen_path, current_doc->file_type, NULL);
+				document_set_text_changed(document_get_current(), TRUE);
+			}
+			gtk_widget_destroy(dialog_new);
+		}
+		else
+			document_open_file(chosen_path, FALSE, NULL, NULL);
+	}
+
+	/* Freeing memory */
 	gtk_widget_destroy(dialog);
+	g_free(dirname);
+	g_object_unref (completion_list);
 }


Modified: codenav/src/utils.c
42 lines changed, 27 insertions(+), 15 deletions(-)
===================================================================
@@ -1,23 +1,24 @@
 /*
- *      utils.c - this file is part of "codenavigation", which is
- *      part of the "geany-plugins" project.
+ *	  utils.c - this file is part of "codenavigation", which is
+ *	  part of the "geany-plugins" project.
  *
- *      Copyright 2009 Lionel Fuentes <funto66(at)gmail(dot)com>
+ *	  Copyright 2009 Lionel Fuentes <funto66(at)gmail(dot)com>
+ *	  Copyright 2014 Federico Reghenzani <federico(dot)dev(at)reghe(dot)net>
  *
- *      This program is free software; you can redistribute it and/or modify
- *      it under the terms of the GNU General Public License as published by
- *      the Free Software Foundation; either version 2 of the License, or
- *      (at your option) any later version.
+ *	  This program is free software; you can redistribute it and/or modify
+ *	  it under the terms of the GNU General Public License as published by
+ *	  the Free Software Foundation; either version 2 of the License, or
+ *	  (at your option) any later version.
  *
- *      This program is distributed in the hope that it will be useful,
- *      but WITHOUT ANY WARRANTY; without even the implied warranty of
- *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *      GNU General Public License for more details.
+ *	  This program is distributed in the hope that it will be useful,
+ *	  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *	  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *	  GNU General Public License for more details.
  *
- *      You should have received a copy of the GNU General Public License
- *      along with this program; if not, write to the Free Software
- *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- *      MA 02110-1301, USA.
+ *	  You should have received a copy of the GNU General Public License
+ *	  along with this program; if not, write to the Free Software
+ *	  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ *	  MA 02110-1301, USA.
  */
 
 #include "utils.h"
@@ -79,3 +80,14 @@ compare_strings(const gchar* a, const gchar* b)
 {
 	return (gint)(!utils_str_equal(a, b));
 }
+
+/* A PHP-like strpos implementation */
+gint
+strpos(const gchar *haystack, const gchar *needle)
+{
+   char *p = g_strstr_len(haystack, -1, needle);
+   if (p)
+	  return p - haystack;
+   return -1;   // not found
+}
+


Modified: codenav/src/utils.h
4 lines changed, 4 insertions(+), 0 deletions(-)
===================================================================
@@ -3,6 +3,7 @@
  *      part of the "geany-plugins" project.
  *
  *      Copyright 2009 Lionel Fuentes <funto66(at)gmail(dot)com>
+ *      Copyright 2014 Federico Reghenzani <federico(dot)dev(at)reghe(dot)net>
  *
  *      This program is free software; you can redistribute it and/or modify
  *      it under the terms of the GNU General Public License as published by
@@ -40,4 +41,7 @@ copy_and_remove_extension(gchar* path);
 gint
 compare_strings(const gchar* a, const gchar* b);
 
+gint
+strpos(const gchar *haystack, const gchar *needle);
+
 #endif /* UTILS_H */



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Plugins-Commits mailing list