From 2374633488d871f137af66ab0de1efa3b3d293da Mon Sep 17 00:00:00 2001 From: Yura Siamashka Date: Mon, 24 Dec 2007 17:10:34 +0200 Subject: [PATCH] load .geanyprj file as geany project on open/switch tabs --- INSTALL | 6 +- configure.in | 2 +- geanyprj.c | 415 ++++++++++++++++++++++++++++++++-------------------------- indent-all.sh | 2 +- 4 files changed, 233 insertions(+), 192 deletions(-) rewrite geanyprj.c (77%) diff --git a/INSTALL b/INSTALL index 9ed2937..6960fcc 100644 --- a/INSTALL +++ b/INSTALL @@ -12,10 +12,10 @@ option for instance: % make install ( By default configure will look for the relative path ../geany/ - so as long as /geanyvc/ and /geany/ are in the same parent directory, + so as long as /geanyprg/ and /geany/ are in the same parent directory, you will not need to specify the location. ) -The "make install" command copies geanyvc.so to your personal ~/.geany/plugins/ +The "make install" command copies geanyprj.so to your personal ~/.geany/plugins/ directory, so you don't need to be root to install. ( If you are logged in as root when you install, it will only put the files in /root/.geany/plugins/ which might not be what you expected! ) @@ -26,6 +26,6 @@ plugins directory. ( That would probably be /usr/local/lib/geany/ or maybe /usr/lib/geany/ ) There is no "make uninstall" target, so if you want to remove the plugin, -just delete the "geanyvc.so" from your plugins directory. +just delete the "geanyprj.so" from your plugins directory. diff --git a/configure.in b/configure.in index 000d531..da3d241 100644 --- a/configure.in +++ b/configure.in @@ -2,7 +2,7 @@ dnl Process this file with autoconf to produce a configure script. AC_PREREQ(2.59) -AC_INIT([geanyvc], [0.2], [yurand2@gmail.com]) +AC_INIT([geanyprj], [0.2], [yurand2@gmail.com]) AM_INIT_AUTOMAKE([1.9 foreign]) diff --git a/geanyprj.c b/geanyprj.c dissimilarity index 77% index 7c941ca..66d059f 100644 --- a/geanyprj.c +++ b/geanyprj.c @@ -1,187 +1,228 @@ -/* - * geanyvc.c - Plugin to geany light IDE to work with vc - * - * Copyright 2007 Frank Lanitz - * Copyright 2007 Enrico Tröger - * Copyright 2007 Nick Treleaven - * Copyright 2007 Yura Siamashka - * - * 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. - * - * 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. - */ - -/* VC plugin */ -/* This plugin allow to works with cvs/svn/git inside geany light IDE. */ - -#include "geany.h" -#include "support.h" -#include "plugindata.h" -#include "document.h" -#include "filetypes.h" -#include "utils.h" -#include "project.h" -#include "pluginmacros.h" - - -PluginFields *plugin_fields; -GeanyData *geany_data; - - -VERSION_CHECK(35); -PLUGIN_INFO(_("Project"), _("Alternative project support."), VERSION, - _("Yura Siamashka ")); - - -/* Called by Geany to initialize the plugin */ -void -init(GeanyData * data) -{ -/* - GtkWidget *menu_vc = NULL; - GtkWidget *menu_vc_menu = NULL; - GtkTooltips *tooltips = NULL; - - config_file = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "plugins", G_DIR_SEPARATOR_S, - "VC", G_DIR_SEPARATOR_S, "VC.conf", NULL); - - load_config(); - registrate(); - - tooltips = gtk_tooltips_new(); - - menu_vc = gtk_image_menu_item_new_with_mnemonic(_("_VC")); - gtk_container_add(GTK_CONTAINER(data->tools_menu), menu_vc); - - g_signal_connect((gpointer) menu_vc, "activate", G_CALLBACK(update_menu_items), NULL); - - menu_vc_menu = gtk_menu_new(); - gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_vc), menu_vc_menu); - - // Single file - menu_vc_diff_file = gtk_menu_item_new_with_mnemonic(_("Diff From Current File")); - gtk_container_add(GTK_CONTAINER(menu_vc_menu), menu_vc_diff_file); - gtk_tooltips_set_tip(tooltips, menu_vc_diff_file, - _("Make a diff from the current active file"), NULL); - - g_signal_connect((gpointer) menu_vc_diff_file, "activate", - G_CALLBACK(vcfile_activated), NULL); - - // Directory - menu_vc_diff_dir = gtk_menu_item_new_with_mnemonic(_("Diff From Current Directory")); - gtk_container_add(GTK_CONTAINER(menu_vc_menu), menu_vc_diff_dir); - gtk_tooltips_set_tip(tooltips, menu_vc_diff_dir, - _("Make a diff from the directory of the current active file"), NULL); - - g_signal_connect((gpointer) menu_vc_diff_dir, "activate", - G_CALLBACK(vcdirectory_activated), NULL); - - // Project - menu_vc_diff_project = gtk_menu_item_new_with_mnemonic(_("Diff From Current Project")); - gtk_container_add(GTK_CONTAINER(menu_vc_menu), menu_vc_diff_project); - gtk_tooltips_set_tip(tooltips, menu_vc_diff_project, - _("Make a diff from the current project's base path"), NULL); - - g_signal_connect((gpointer) menu_vc_diff_project, "activate", - G_CALLBACK(vcproject_activated), NULL); - - gtk_container_add(GTK_CONTAINER(menu_vc_menu), gtk_separator_menu_item_new()); - - // Log - menu_vc_log_file = gtk_menu_item_new_with_mnemonic(_("Log Of Current File")); - gtk_container_add(GTK_CONTAINER(menu_vc_menu), menu_vc_log_file); - gtk_tooltips_set_tip(tooltips, menu_vc_log_file, - _("Shows the log of the current file"), NULL); - - g_signal_connect((gpointer) menu_vc_log_file, "activate", - G_CALLBACK(vclog_file_activated), NULL); - - menu_vc_log_dir = gtk_menu_item_new_with_mnemonic(_("Log Of Current Directory")); - gtk_container_add(GTK_CONTAINER(menu_vc_menu), menu_vc_log_dir); - gtk_tooltips_set_tip(tooltips, menu_vc_log_dir, - _("Shows the log of the current directory"), NULL); - - g_signal_connect((gpointer) menu_vc_log_dir, "activate", - G_CALLBACK(vclog_dir_activated), NULL); - - menu_vc_log_project = gtk_menu_item_new_with_mnemonic(_("Log Of Current Project")); - gtk_container_add(GTK_CONTAINER(menu_vc_menu), menu_vc_log_project); - gtk_tooltips_set_tip(tooltips, menu_vc_log_project, - _("Shows the log of the current project"), NULL); - - g_signal_connect((gpointer) menu_vc_log_project, "activate", - G_CALLBACK(vclog_project_activated), NULL); - - gtk_container_add(GTK_CONTAINER(menu_vc_menu), gtk_separator_menu_item_new()); - - // Status - menu_vc_status = gtk_menu_item_new_with_mnemonic(_("_Status")); - gtk_container_add(GTK_CONTAINER(menu_vc_menu), menu_vc_status); - gtk_tooltips_set_tip(tooltips, menu_vc_status, _("Show status."), NULL); - - g_signal_connect((gpointer) menu_vc_status, "activate", - G_CALLBACK(vcstatus_activated), NULL); - - gtk_container_add(GTK_CONTAINER(menu_vc_menu), gtk_separator_menu_item_new()); - - // Revert file - menu_vc_revert_file = gtk_menu_item_new_with_mnemonic(_("_Revert File")); - gtk_container_add(GTK_CONTAINER(menu_vc_menu), menu_vc_revert_file); - gtk_tooltips_set_tip(tooltips, menu_vc_revert_file, - _("Restore pristine working copy file (undo local edits)."), NULL); - - g_signal_connect((gpointer) menu_vc_revert_file, "activate", - G_CALLBACK(vcrevert_activated), NULL); - - gtk_container_add(GTK_CONTAINER(menu_vc_menu), gtk_separator_menu_item_new()); - - // Add file - menu_vc_add_file = gtk_menu_item_new_with_mnemonic(_("_Add File")); - gtk_container_add(GTK_CONTAINER(menu_vc_menu), menu_vc_add_file); - gtk_tooltips_set_tip(tooltips, menu_vc_add_file, _("Add file to repository."), NULL); - - g_signal_connect((gpointer) menu_vc_add_file, "activate", - G_CALLBACK(vcadd_activated), NULL); - - // Remove file - menu_vc_remove_file = gtk_menu_item_new_with_mnemonic(_("Remove File")); - gtk_container_add(GTK_CONTAINER(menu_vc_menu), menu_vc_remove_file); - gtk_tooltips_set_tip(tooltips, menu_vc_remove_file, - _("Remove file from repository."), NULL); - - g_signal_connect((gpointer) menu_vc_remove_file, "activate", - G_CALLBACK(vcremove_activated), NULL); - - gtk_container_add(GTK_CONTAINER(menu_vc_menu), gtk_separator_menu_item_new()); - - // Commit - menu_vc_commit = gtk_menu_item_new_with_mnemonic(_("_Commit")); - gtk_container_add(GTK_CONTAINER(menu_vc_menu), menu_vc_commit); - gtk_tooltips_set_tip(tooltips, menu_vc_commit, _("Commit changes."), NULL); - - g_signal_connect((gpointer) menu_vc_commit, "activate", - G_CALLBACK(vccommit_activated), NULL); - - gtk_widget_show_all(menu_vc); - - plugin_fields->menu_item = menu_vc; - plugin_fields->flags = PLUGIN_IS_DOCUMENT_SENSITIVE; -*/ -} - - -/* Called by Geany before unloading the plugin. */ -void -cleanup() -{ -} +/* + * geanyprj.c - Alternative project support for geany light IDE. + * + * Copyright 2007 Frank Lanitz + * Copyright 2007 Enrico Tröger + * Copyright 2007 Nick Treleaven + * Copyright 2007 Yura Siamashka + * + * 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. + * + * 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 "geany.h" +#include "support.h" +#include "plugindata.h" +#include "document.h" +#include "filetypes.h" +#include "utils.h" +#include "project.h" +#include "pluginmacros.h" + +VERSION_CHECK(36); +PLUGIN_INFO(_("Project"), _("Alternative project support."), VERSION, + _("Yura Siamashka ")); + +#define DEBUG + +#ifdef DEBUG +#define debug printf +#else +#define debug +#endif + +PluginFields *plugin_fields; +GeanyData *geany_data; + + +static gchar * +find_file_path(const gchar * dir, const gchar * filename) +{ + gboolean ret = FALSE; + gchar *base; + gchar *gitdir = NULL; + gchar *base_prev = g_strdup(":"); + + base = g_strdup(dir); + + while (strcmp(base, base_prev) != 0) + { + gitdir = g_build_path("/", base, filename, NULL); + ret = g_file_test(gitdir, G_FILE_TEST_IS_REGULAR); + if (ret) + { + g_free(base_prev); + g_free(base); + return gitdir; + } + g_free(gitdir); + g_free(base_prev); + base_prev = base; + base = g_path_get_dirname(base); + } + + g_free(base_prev); + g_free(base); + return NULL; +} + +/* Reads the given filename and creates a new project with the data found in the file. + * At this point there should not be an already opened project in Geany otherwise it will just + * return. + * The filename is expected in the locale encoding. */ +static gboolean +xload_config(const gchar * filename) +{ + GKeyFile *config; + GeanyProject *p; + + // there should not be an open project + g_return_val_if_fail(project == NULL && filename != NULL, FALSE); + + config = g_key_file_new(); + if (!g_key_file_load_from_file(config, filename, G_KEY_FILE_NONE, NULL)) + { + g_key_file_free(config); + return FALSE; + } + + p = project = g_new0(GeanyProject, 1); + + p->name = utils->get_setting_string(config, "project", "name", GEANY_STRING_UNTITLED); + p->description = utils->get_setting_string(config, "project", "description", ""); + p->file_name = utils->get_utf8_from_locale(filename); + p->base_path = utils->get_setting_string(config, "project", "base_path", ""); + p->run_cmd = utils->get_setting_string(config, "project", "run_cmd", ""); + p->file_patterns = + g_key_file_get_string_list(config, "project", "file_patterns", NULL, NULL); + + g_key_file_free(config); + + return TRUE; +} + + +static gboolean +xproject_load_file(const gchar * locale_file_name) +{ + g_return_val_if_fail(locale_file_name != NULL, FALSE); + + if (xload_config(locale_file_name)) + { + ui->set_statusbar(TRUE, _("Project \"%s\" opened."), project->name); + return TRUE; + } + else + { + gchar *utf8_filename = utils->get_utf8_from_locale(locale_file_name); + ui->set_statusbar(TRUE, _("Project file \"%s\" could not be loaded."), + utf8_filename); + g_free(utf8_filename); + } + return FALSE; +} + + +/* This fonction should keep in sync with geany code */ +static void +xproject_close() +{ + g_return_if_fail(project != NULL); + + ui->set_statusbar(TRUE, _("Project \"%s\" closed."), project->name); + + g_free(project->name); + g_free(project->description); + g_free(project->file_name); + g_free(project->base_path); + g_free(project->run_cmd); + + g_free(project); + project = NULL; +} + +static void +reload_project() +{ + gint idx; + gchar *dir; + gchar *proj; + + idx = documents->get_cur_idx(); + g_return_if_fail(DOC_IDX_VALID(idx) && doc_list[idx].file_name != NULL); + + dir = g_path_get_dirname(doc_list[idx].file_name); + proj = find_file_path(dir, ".geanyprj"); + + + if (!proj) + { + if (project) + xproject_close(); + return; + } + + if (!project) + { + xproject_load_file(proj); + } + else if (strcmp(proj, project->file_name) != 0) + { + xproject_close(); + xproject_load_file(proj); + } + if (proj) + g_free(proj); +} + +static void +on_doc_save(GObject * obj, gint idx, gpointer user_data) +{ + debug("%s obj=%p, idx = %d, user_data = %p\n", __FUNCTION__, obj, idx, user_data); + reload_project(); +} + +static void +on_doc_open(GObject * obj, gint idx, gpointer user_data) +{ + debug("%s obj=%p, idx = %d, user_data = %p\n", __FUNCTION__, obj, idx, user_data); + reload_project(); +} + +static void +on_doc_activate(GObject * obj, gint idx, gpointer user_data) +{ + debug("%s obj=%p, idx = %d, user_data = %p\n", __FUNCTION__, obj, idx, user_data); + reload_project(); +} + +GeanyCallback geany_callbacks[] = { + {"document-open", (GCallback) & on_doc_open, TRUE, NULL}, + {"document-save", (GCallback) & on_doc_save, TRUE, NULL}, + {"document-activate", (GCallback) & on_doc_activate, TRUE, NULL}, + {NULL, NULL, FALSE, NULL} +}; + +/* Called by Geany to initialize the plugin */ +void +init(GeanyData * data) +{ +} + + +/* Called by Geany before unloading the plugin. */ +void +cleanup() +{ +} diff --git a/indent-all.sh b/indent-all.sh index 9dd4aa8..9fbac92 100755 --- a/indent-all.sh +++ b/indent-all.sh @@ -1,4 +1,4 @@ #!/bin/sh -indent -bli0 -i8 -cli8 -npcs -l100 *.c *.h +indent -bli0 -i8 -cli8 -npcs -l100 *.c rm *~ -- 2.11.4.GIT