gedit r6243 - in branches/jessevdk-plugins: bindings/python gedit/plugins
- From: jessevdk svn gnome org
- To: svn-commits-list gnome org
- Subject: gedit r6243 - in branches/jessevdk-plugins: bindings/python gedit/plugins
- Date: Mon, 7 Apr 2008 16:39:52 +0100 (BST)
Author: jessevdk
Date: Mon Apr 7 16:39:51 2008
New Revision: 6243
URL: http://svn.gnome.org/viewvc/gedit?rev=6243&view=rev
Log:
* gedit/plugins/gedit-python-module.c: removed spurious debug message
* gedit/plugins/gedit-plugin-python-utils.[ch]: provide utility functions
for C plugins to install python bindings
* gedit/plugins/gedit-python-plugin.[ch]: fix use of private data
* gedit/plugins/gedit-plugins-engine.[ch]: added function to go from
plugin to plugin info
* gedit/plugins/gedit-plugin.[ch]: added register_bindings class function
which should be called if a plugin needs to register bindings for a
certain language. Added is_binding_registered and register_binding
functions
* bindings/python/geditplugins.override: fix get_plugin helper to get
the correct instance from GeditPythonPlugin instances and to
correctly request registering of bindings for C plugins
Added:
branches/jessevdk-plugins/gedit/plugins/gedit-plugin-python-utils.c
branches/jessevdk-plugins/gedit/plugins/gedit-plugin-python-utils.h
Modified:
branches/jessevdk-plugins/bindings/python/geditplugins.override
branches/jessevdk-plugins/gedit/plugins/Makefile.am
branches/jessevdk-plugins/gedit/plugins/gedit-plugin.c
branches/jessevdk-plugins/gedit/plugins/gedit-plugin.h
branches/jessevdk-plugins/gedit/plugins/gedit-plugins-engine.c
branches/jessevdk-plugins/gedit/plugins/gedit-plugins-engine.h
branches/jessevdk-plugins/gedit/plugins/gedit-python-module.c
branches/jessevdk-plugins/gedit/plugins/gedit-python-plugin.c
branches/jessevdk-plugins/gedit/plugins/gedit-python-plugin.h
Modified: branches/jessevdk-plugins/bindings/python/geditplugins.override
==============================================================================
--- branches/jessevdk-plugins/bindings/python/geditplugins.override (original)
+++ branches/jessevdk-plugins/bindings/python/geditplugins.override Mon Apr 7 16:39:51 2008
@@ -6,6 +6,7 @@
#include "plugins/gedit-plugins-engine.h"
#include "plugins/gedit-plugin.h"
+#include "plugins/gedit-python-plugin.h"
#include "plugins/gedit-plugin-info.h"
static PyObject *
@@ -64,8 +65,34 @@
static PyObject *
_helper_get_plugin_instance (GeditPlugin *plugin)
{
- Py_INCREF (Py_None);
- return Py_None;
+ gboolean registered;
+
+ if (!plugin)
+ {
+ Py_INCREF (Py_None);
+ return Py_None;
+ }
+
+ if (GEDIT_IS_PYTHON_PLUGIN (plugin))
+ {
+ PyObject *obj = _gedit_python_plugin_get_instance (GEDIT_PYTHON_PLUGIN (plugin));
+ Py_INCREF (obj);
+
+ return obj;
+ }
+
+ registered = gedit_plugin_is_binding_registered (plugin, "python");
+
+ if (!registered)
+ registered = gedit_plugin_register_binding (plugin, "python");
+
+ if (!registered)
+ {
+ Py_INCREF (Py_None);
+ return Py_None;
+ }
+
+ return pygobject_new ((GObject *)plugin);
}
%%
Modified: branches/jessevdk-plugins/gedit/plugins/Makefile.am
==============================================================================
--- branches/jessevdk-plugins/gedit/plugins/Makefile.am (original)
+++ branches/jessevdk-plugins/gedit/plugins/Makefile.am Mon Apr 7 16:39:51 2008
@@ -15,7 +15,7 @@
-DGEDIT_ICONDIR=\""$(datadir)/gedit-2/icons"\"
if ENABLE_PYTHON
-INCLUDES += \
+INCLUDES += \
$(NO_STRICT_ALIASING_CFLAGS) \
$(PYGTK_CFLAGS) \
$(PYTHON_CFLAGS) \
@@ -30,7 +30,7 @@
gedit-module.h
if ENABLE_PYTHON
-NOINST_H_FILES += \
+NOINST_H_FILES += \
gedit-python-module.h \
gedit-python-plugin.h
endif
@@ -40,6 +40,10 @@
gedit-plugin-info.h \
gedit-plugins-engine.h
+if ENABLE_PYTHON
+INST_H_FILES += \
+ gedit-plugin-python-utils.h
+endif
headerdir = $(prefix)/include/gedit- GEDIT_API_VERSION@/gedit/plugins
@@ -58,5 +62,6 @@
if ENABLE_PYTHON
libplugins_la_SOURCES += \
gedit-python-module.c \
- gedit-python-plugin.c
+ gedit-python-plugin.c \
+ gedit-plugin-python-utils.c
endif
Added: branches/jessevdk-plugins/gedit/plugins/gedit-plugin-python-utils.c
==============================================================================
--- (empty file)
+++ branches/jessevdk-plugins/gedit/plugins/gedit-plugin-python-utils.c Mon Apr 7 16:39:51 2008
@@ -0,0 +1,87 @@
+/*
+ * gedit-plugin-python-utils.c
+ * This file is part of gedit
+ *
+ * Copyright (C) 2008 - Jesse van den Kieboom
+ *
+ * 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., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <string.h>
+#include "gedit-plugin-info.h"
+#include "gedit-plugin-python-utils.h"
+#include "gedit-plugins-engine.h"
+#include "gedit-python-module.h"
+
+#define TYPE_PREFIX "Gedit"
+
+PyObject *gedit_plugin_python_utils_init(GeditPlugin *plugin,
+ GeditPluginPythonUtilsClasses classreg,
+ PyMethodDef *functions,
+ GeditPluginPythonUtilsConstants constreg,
+ const gchar *prefix)
+{
+ PyObject *plugins, *mdict, *module;
+ const gchar *name;
+ gchar *mname;
+ GeditPluginInfo *info;
+
+ /* Register bindings for the plugin in a separate namespace in
+ gedit.plugins.(modulename) */
+
+ // Make sure python is initialized
+ gedit_python_init ();
+
+ plugins = PyImport_ImportModule("gedit.plugins");
+
+ if (plugins == NULL)
+ {
+ PyErr_SetString (PyExc_ImportError,
+ "could not import gedit.plugins");
+ return NULL;
+ }
+
+ mdict = PyModule_GetDict (plugins);
+
+ /* Find plugin info, to get module name */
+ info = _gedit_plugins_engine_info_for_plugin (gedit_plugins_engine_get_default (),
+ plugin);
+
+ if (!info)
+ {
+ g_warning ("Corresponding GeditPluginInfo could not be found!");
+ return NULL;
+ }
+
+ name = g_type_name (plugin);
+
+ if (g_str_has_prefix (name, TYPE_PREFIX))
+ name = name + strlen(TYPE_PREFIX);
+
+ mname = g_strdup_printf ("gedit.plugins.%s", name);
+ module = Py_InitModule (mname, functions);
+ g_free (mname);
+
+ mdict = PyModule_GetDict (module);
+
+ if (classreg)
+ classreg (mdict);
+
+ if (constreg)
+ constreg (module, prefix);
+
+ return module;
+}
Added: branches/jessevdk-plugins/gedit/plugins/gedit-plugin-python-utils.h
==============================================================================
--- (empty file)
+++ branches/jessevdk-plugins/gedit/plugins/gedit-plugin-python-utils.h Mon Apr 7 16:39:51 2008
@@ -0,0 +1,41 @@
+/*
+ * gedit-plugin-python-utils.h
+ * This file is part of gedit
+ *
+ * Copyright (C) 2008 - Jesse van den Kieboom
+ *
+ * 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., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __GEDIT_PLUGIN_PYTHON_UTILS_H__
+#define __GEDIT_PLUGIN_PYTHON_UTILS_H__
+
+#include <gedit/plugins/gedit-plugin.h>
+#include <pygobject.h>
+
+/* Utility functions which C plugins can use to register python bindings */
+typedef void (*GeditPluginPythonUtilsClasses)(PyObject *classes);
+typedef void (*GeditPluginPythonUtilsConstants)(PyObject *module, const gchar *strip_prefix);
+
+PyObject *gedit_plugin_python_utils_init (GeditPlugin *plugin,
+ GeditPluginPythonUtilsClasses classreg,
+ PyMethodDef *plugins,
+ GeditPluginPythonUtilsConstants constreg,
+ const gchar *prefix);
+
+
+
+#endif /* __GEDIT_PLUGIN_PYTHON_UTILS_H__ */
Modified: branches/jessevdk-plugins/gedit/plugins/gedit-plugin.c
==============================================================================
--- branches/jessevdk-plugins/gedit/plugins/gedit-plugin.c (original)
+++ branches/jessevdk-plugins/gedit/plugins/gedit-plugin.c Mon Apr 7 16:39:51 2008
@@ -34,6 +34,12 @@
#include "gedit-plugin.h"
+#define GEDIT_PLUGIN_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), GEDIT_TYPE_PLUGIN, GeditPluginPrivate))
+
+struct _GeditPluginPrivate {
+ GList *registered;
+};
+
G_DEFINE_TYPE(GeditPlugin, gedit_plugin, G_TYPE_OBJECT)
static void
@@ -55,21 +61,45 @@
create_configure_dialog);
}
+static gboolean
+register_bindings (GeditPlugin *plugin, const gchar *language)
+{
+ if (!gedit_plugin_is_binding_registered (plugin, language))
+ plugin->priv->registered = g_list_prepend (plugin->priv->registered,
+ g_strdup (language));
+ return FALSE;
+}
+
+static void
+gedit_plugin_finalize (GObject *object)
+{
+ GList *registered = GEDIT_PLUGIN(object)->priv->registered;
+
+ g_list_foreach (registered, (GFunc)g_free, NULL);
+ g_list_free (registered);
+}
+
static void
gedit_plugin_class_init (GeditPluginClass *klass)
{
+ g_type_class_add_private (klass, sizeof (GeditPluginPrivate));
+ G_OBJECT_CLASS (klass)->finalize = gedit_plugin_finalize;
+
klass->activate = dummy;
klass->deactivate = dummy;
klass->update_ui = dummy;
klass->create_configure_dialog = create_configure_dialog;
klass->is_configurable = is_configurable;
+
+ klass->register_bindings = register_bindings;
}
static void
gedit_plugin_init (GeditPlugin *plugin)
{
- /* Empty */
+ plugin->priv = GEDIT_PLUGIN_GET_PRIVATE (plugin);
+ plugin->priv->registered = NULL;
}
void
@@ -117,3 +147,26 @@
return GEDIT_PLUGIN_GET_CLASS (plugin)->create_configure_dialog (plugin);
}
+
+gboolean
+gedit_plugin_register_binding (GeditPlugin *plugin,
+ const gchar *language)
+{
+ g_return_val_if_fail (GEDIT_IS_PLUGIN (plugin), FALSE);
+
+ return GEDIT_PLUGIN_GET_CLASS (plugin)->register_bindings (plugin, language);
+}
+
+gboolean
+gedit_plugin_is_binding_registered (GeditPlugin *plugin,
+ const gchar *language)
+{
+ GList *item;
+
+ g_return_val_if_fail (GEDIT_IS_PLUGIN (plugin), FALSE);
+
+ item = g_list_find_custom (plugin->priv->registered,
+ language,
+ (GCompareFunc)g_strcmp0);
+ return item != NULL;
+}
Modified: branches/jessevdk-plugins/gedit/plugins/gedit-plugin.h
==============================================================================
--- branches/jessevdk-plugins/gedit/plugins/gedit-plugin.h (original)
+++ branches/jessevdk-plugins/gedit/plugins/gedit-plugin.h Mon Apr 7 16:39:51 2008
@@ -51,6 +51,9 @@
#define GEDIT_IS_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GEDIT_TYPE_PLUGIN))
#define GEDIT_PLUGIN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GEDIT_TYPE_PLUGIN, GeditPluginClass))
+/* Private structure type */
+typedef struct _GeditPluginPrivate GeditPluginPrivate;
+
/*
* Main object structure
*/
@@ -59,6 +62,9 @@
struct _GeditPlugin
{
GObject parent;
+
+ /*< private > */
+ GeditPluginPrivate *priv;
};
/*
@@ -87,12 +93,13 @@
the GeditPluginClass */
gboolean (*is_configurable)
(GeditPlugin *plugin);
+ gboolean (*register_bindings) (GeditPlugin *plugin,
+ const gchar *language);
/* Padding for future expansion */
void (*_gedit_reserved1) (void);
void (*_gedit_reserved2) (void);
void (*_gedit_reserved3) (void);
- void (*_gedit_reserved4) (void);
};
/*
@@ -112,6 +119,13 @@
GtkWidget *gedit_plugin_create_configure_dialog
(GeditPlugin *plugin);
+gboolean gedit_plugin_is_binding_registered
+ (GeditPlugin *plugin,
+ const gchar *language);
+gboolean gedit_plugin_register_binding (GeditPlugin *plugin,
+ const gchar *language);
+
+
/*
* Utility macro used to register plugins
*
Modified: branches/jessevdk-plugins/gedit/plugins/gedit-plugins-engine.c
==============================================================================
--- branches/jessevdk-plugins/gedit/plugins/gedit-plugins-engine.c (original)
+++ branches/jessevdk-plugins/gedit/plugins/gedit-plugins-engine.c Mon Apr 7 16:39:51 2008
@@ -362,7 +362,6 @@
const gchar *name)
{
GList *item;
- GeditPluginInfo *info;
gedit_debug(DEBUG_PLUGINS);
@@ -690,6 +689,28 @@
gtk_widget_show (conf_dlg);
}
+static gint
+match_plugin (GeditPluginInfo *info,
+ GeditPlugin *plugin)
+{
+ /* return 0 if match, 1 otherwise */
+ return info->plugin != plugin;
+}
+
+GeditPluginInfo *
+_gedit_plugins_engine_info_for_plugin (GeditPluginsEngine *engine,
+ GeditPlugin *plugin)
+{
+ g_return_val_if_fail (GEDIT_IS_PLUGINS_ENGINE (engine), NULL);
+ g_return_val_if_fail (plugin != NULL, NULL);
+
+ GList *item = g_list_find_custom (engine->priv->plugin_list,
+ plugin,
+ (GCompareFunc)match_plugin);
+
+ return GEDIT_PLUGIN_INFO (item->data);
+}
+
static void
gedit_plugins_engine_active_plugins_changed (GConfClient *client,
guint cnxn_id,
Modified: branches/jessevdk-plugins/gedit/plugins/gedit-plugins-engine.h
==============================================================================
--- branches/jessevdk-plugins/gedit/plugins/gedit-plugins-engine.h (original)
+++ branches/jessevdk-plugins/gedit/plugins/gedit-plugins-engine.h Mon Apr 7 16:39:51 2008
@@ -97,6 +97,9 @@
GeditWindow *window,
gboolean new_window);
+GeditPluginInfo *_gedit_plugins_engine_info_for_plugin (GeditPluginsEngine *engine,
+ GeditPlugin *plugin);
+
G_END_DECLS
#endif /* __GEDIT_PLUGINS_ENGINE_H__ */
Modified: branches/jessevdk-plugins/gedit/plugins/gedit-python-module.c
==============================================================================
--- branches/jessevdk-plugins/gedit/plugins/gedit-python-module.c (original)
+++ branches/jessevdk-plugins/gedit/plugins/gedit-python-module.c Mon Apr 7 16:39:51 2008
@@ -173,7 +173,6 @@
then get the actual gobject instance back */
PyObject *instance = PyObject_CallObject (priv->type, NULL);
- gedit_debug_message (DEBUG_PLUGINS, "Created %d", instance);
/* CHECKME: not sure if we want to decrease the reference here, but
should be OK since GeditPythonPlugin adds a reference */
Py_XDECREF(instance);
Modified: branches/jessevdk-plugins/gedit/plugins/gedit-python-plugin.c
==============================================================================
--- branches/jessevdk-plugins/gedit/plugins/gedit-python-plugin.c (original)
+++ branches/jessevdk-plugins/gedit/plugins/gedit-python-plugin.c Mon Apr 7 16:39:51 2008
@@ -105,7 +105,7 @@
GeditWindow *window)
{
PyGILState_STATE state = pyg_gil_state_ensure ();
- GeditPythonPluginPrivate *priv = GEDIT_PYTHON_PLUGIN_GET_PRIVATE(plugin);
+ GeditPythonPluginPrivate *priv = GEDIT_PYTHON_PLUGIN(plugin)->priv;
if (PyObject_HasAttrString (priv->instance, "update_ui"))
{
@@ -127,7 +127,7 @@
GeditWindow *window)
{
PyGILState_STATE state = pyg_gil_state_ensure ();
- GeditPythonPluginPrivate *priv = GEDIT_PYTHON_PLUGIN_GET_PRIVATE(plugin);
+ GeditPythonPluginPrivate *priv = GEDIT_PYTHON_PLUGIN(plugin)->priv;
if (PyObject_HasAttrString (priv->instance, "deactivate"))
{
@@ -149,7 +149,7 @@
GeditWindow *window)
{
PyGILState_STATE state = pyg_gil_state_ensure ();
- GeditPythonPluginPrivate *priv = GEDIT_PYTHON_PLUGIN_GET_PRIVATE(plugin);
+ GeditPythonPluginPrivate *priv = GEDIT_PYTHON_PLUGIN(plugin)->priv;
if (PyObject_HasAttrString (priv->instance, "activate"))
{
@@ -170,7 +170,7 @@
impl_create_configure_dialog (GeditPlugin *plugin)
{
PyGILState_STATE state = pyg_gil_state_ensure ();
- GeditPythonPluginPrivate *priv = GEDIT_PYTHON_PLUGIN_GET_PRIVATE(plugin);
+ GeditPythonPluginPrivate *priv = GEDIT_PYTHON_PLUGIN(plugin)->priv;
GtkWidget *ret = NULL;
if (PyObject_HasAttrString (priv->instance, "create_configure_dialog"))
@@ -205,7 +205,7 @@
impl_is_configurable (GeditPlugin *plugin)
{
PyGILState_STATE state = pyg_gil_state_ensure ();
- GeditPythonPluginPrivate *priv = GEDIT_PYTHON_PLUGIN_GET_PRIVATE(plugin);
+ GeditPythonPluginPrivate *priv = GEDIT_PYTHON_PLUGIN(plugin)->priv;
PyObject *dict = priv->instance->ob_type->tp_dict;
gboolean result;
@@ -226,29 +226,33 @@
PyObject *instance)
{
PyGILState_STATE state = pyg_gil_state_ensure ();
- GeditPythonPluginPrivate *priv = GEDIT_PYTHON_PLUGIN_GET_PRIVATE(plugin);
- Py_XDECREF(priv->instance);
+ Py_XDECREF(plugin->priv->instance);
/* CHECK: is the increment actually needed? */
Py_INCREF(instance);
- GEDIT_PYTHON_PLUGIN_GET_PRIVATE(plugin)->instance = instance;
+ plugin->priv->instance = instance;
pyg_gil_state_release (state);
}
+PyObject *
+_gedit_python_plugin_get_instance (GeditPythonPlugin *plugin)
+{
+ return plugin->priv->instance;
+}
+
void
_gedit_python_plugin_destroy (GeditPythonPlugin *plugin)
{
PyGILState_STATE state;
PyObject *instance;
- GeditPythonPluginPrivate *priv = GEDIT_PYTHON_PLUGIN_GET_PRIVATE (plugin);
- if (priv->instance)
+ if (plugin->priv->instance)
{
state = pyg_gil_state_ensure ();
- instance = priv->instance;
- priv->instance = 0;
+ instance = plugin->priv->instance;
+ plugin->priv->instance = 0;
Py_XDECREF (instance);
pyg_gil_state_release (state);
@@ -261,22 +265,21 @@
static void
gedit_python_plugin_init (GeditPythonPlugin *plugin)
{
- GeditPythonPluginPrivate *priv = GEDIT_PYTHON_PLUGIN_GET_PRIVATE(plugin);
+ plugin->priv = GEDIT_PYTHON_PLUGIN_GET_PRIVATE(plugin);
gedit_debug_message (DEBUG_PLUGINS, "Creating Python plugin instance");
- priv->instance = 0;
+ plugin->priv->instance = 0;
}
static void
gedit_python_plugin_finalize (GObject *object)
{
PyGILState_STATE state;
- GeditPythonPluginPrivate *priv = GEDIT_PYTHON_PLUGIN_GET_PRIVATE (object);
gedit_debug_message (DEBUG_PLUGINS, "Finalizing Python plugin instance");
state = pyg_gil_state_ensure ();
- Py_XDECREF (priv->instance);
+ Py_XDECREF (GEDIT_PYTHON_PLUGIN(object)->priv->instance);
pyg_gil_state_release (state);
G_OBJECT_CLASS (parent_class)->finalize (object);
Modified: branches/jessevdk-plugins/gedit/plugins/gedit-python-plugin.h
==============================================================================
--- branches/jessevdk-plugins/gedit/plugins/gedit-python-plugin.h (original)
+++ branches/jessevdk-plugins/gedit/plugins/gedit-python-plugin.h Mon Apr 7 16:39:51 2008
@@ -75,9 +75,10 @@
/*
* Private methods
*/
-void _gedit_python_plugin_set_instance (GeditPythonPlugin *plugin,
+void _gedit_python_plugin_set_instance (GeditPythonPlugin *plugin,
PyObject *instance);
-void _gedit_python_plugin_destroy (GeditPythonPlugin *plugin);
+PyObject *_gedit_python_plugin_get_instance (GeditPythonPlugin *plugin);
+void _gedit_python_plugin_destroy (GeditPythonPlugin *plugin);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]