Made python tools builtin
parent
cf274e3d4a
commit
137ab18205
|
@ -38,6 +38,7 @@ plat/win32/gtk-win/bdist-release/
|
|||
api/moo.xml
|
||||
api/gtk.xml
|
||||
moo/moopython/pygtk/moo-generated.defs
|
||||
moo/moopython/plugins/old/
|
||||
moo/moolua/moo-lua-api.cpp
|
||||
moo/moolua/gtk-lua-api.cpp
|
||||
doc/help/
|
||||
|
|
|
@ -14,6 +14,18 @@ moo_python_sources += \
|
|||
moopython/moopython-utils.h \
|
||||
moopython/moopython-utils.c
|
||||
|
||||
moo_sources += \
|
||||
moopython/medit-python.h \
|
||||
moopython/medit-python.c
|
||||
|
||||
EXTRA_DIST += moopython/medit-python-init.py
|
||||
built_moo_sources += moopython/medit-python-init.h
|
||||
moopython/medit-python-init.h: moopython/medit-python-init.py $(top_srcdir)/tools/xml2h.py
|
||||
$(AM_V_at)$(MKDIR_P) moopython
|
||||
$(AM_V_GEN)$(PYTHON) $(top_srcdir)/tools/xml2h.py $(srcdir)/moopython/medit-python-init.py \
|
||||
moopython/medit-python-init.h.tmp MEDIT_PYTHON_INIT_PY \
|
||||
&& mv moopython/medit-python-init.h.tmp moopython/medit-python-init.h
|
||||
|
||||
if MOO_ENABLE_PYTHON
|
||||
moo_sources += $(moo_python_sources)
|
||||
built_moo_sources += $(built_moo_python_sources)
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
import moo
|
||||
app = moo.app_instance()
|
||||
editor = app.get_editor()
|
|
@ -0,0 +1,225 @@
|
|||
#include <config.h>
|
||||
#ifdef MOO_ENABLE_PYTHON
|
||||
#include <Python.h>
|
||||
#endif
|
||||
#include "medit-python.h"
|
||||
#include "mooutils/mooutils.h"
|
||||
#include "moopython/medit-python-init.h"
|
||||
|
||||
#ifdef MOO_ENABLE_PYTHON
|
||||
|
||||
struct MooPythonState
|
||||
{
|
||||
PyObject *locals;
|
||||
};
|
||||
|
||||
static PyObject *
|
||||
create_script_dict (const char *name)
|
||||
{
|
||||
PyObject *dict, *builtins;
|
||||
|
||||
builtins = PyImport_ImportModule ("__builtin__");
|
||||
g_return_val_if_fail (builtins != NULL, NULL);
|
||||
|
||||
dict = PyDict_New ();
|
||||
PyDict_SetItemString (dict, "__builtins__", builtins);
|
||||
|
||||
if (name)
|
||||
{
|
||||
PyObject *py_name = PyString_FromString (name);
|
||||
PyDict_SetItemString (dict, "__name__", py_name);
|
||||
Py_XDECREF (py_name);
|
||||
}
|
||||
|
||||
Py_XDECREF (builtins);
|
||||
return dict;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
run_string (const char *str,
|
||||
PyObject *globals,
|
||||
PyObject *locals)
|
||||
{
|
||||
PyObject *ret;
|
||||
|
||||
g_return_val_if_fail (str != NULL, NULL);
|
||||
|
||||
if (!locals)
|
||||
locals = create_script_dict ("__script__");
|
||||
else
|
||||
Py_INCREF ((PyObject*) locals);
|
||||
|
||||
g_return_val_if_fail (locals != NULL, NULL);
|
||||
|
||||
if (!globals)
|
||||
globals = locals;
|
||||
|
||||
ret = PyRun_String (str, Py_file_input, globals, locals);
|
||||
|
||||
Py_DECREF (locals);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// static PyObject *
|
||||
// run_code (const char *str,
|
||||
// PyObject *globals,
|
||||
// PyObject *locals)
|
||||
// {
|
||||
// PyObject *ret;
|
||||
//
|
||||
// g_return_val_if_fail (str != NULL, NULL);
|
||||
//
|
||||
// if (!locals)
|
||||
// locals = create_script_dict ("__script__");
|
||||
// else
|
||||
// Py_INCREF (locals);
|
||||
//
|
||||
// g_return_val_if_fail (locals != NULL, NULL);
|
||||
//
|
||||
// if (!globals)
|
||||
// globals = locals;
|
||||
//
|
||||
// ret = PyRun_String (str, Py_file_input, globals, locals);
|
||||
//
|
||||
// if (ret)
|
||||
// {
|
||||
// Py_DECREF (ret);
|
||||
//
|
||||
// if (PyMapping_HasKeyString (locals, (char*) "__retval__"))
|
||||
// ret = PyMapping_GetItemString (locals, (char*) "__retval__");
|
||||
// else
|
||||
// ret = NULL;
|
||||
// }
|
||||
//
|
||||
// Py_DECREF (locals);
|
||||
// return ret;
|
||||
// }
|
||||
|
||||
MooPythonState *
|
||||
moo_python_state_new (gboolean default_init)
|
||||
{
|
||||
MooPythonState *state;
|
||||
|
||||
state = g_slice_new0 (MooPythonState);
|
||||
state->locals = create_script_dict ("__script__");
|
||||
|
||||
if (!state->locals)
|
||||
{
|
||||
moo_python_state_free (state);
|
||||
moo_return_val_if_reached (NULL);
|
||||
}
|
||||
|
||||
if (default_init && !moo_python_run_string (state, MEDIT_PYTHON_INIT_PY))
|
||||
{
|
||||
moo_python_state_free (state);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
void
|
||||
moo_python_state_free (MooPythonState *state)
|
||||
{
|
||||
if (state)
|
||||
{
|
||||
Py_XDECREF (state->locals);
|
||||
g_slice_free (MooPythonState, state);
|
||||
}
|
||||
}
|
||||
|
||||
gboolean
|
||||
moo_python_run_string (MooPythonState *state,
|
||||
const char *string)
|
||||
{
|
||||
PyObject *pyret;
|
||||
gboolean ret = TRUE;
|
||||
|
||||
moo_return_val_if_fail (state && state->locals, FALSE);
|
||||
moo_return_val_if_fail (string != NULL, FALSE);
|
||||
|
||||
pyret = run_string (string, NULL, state->locals);
|
||||
|
||||
if (!pyret)
|
||||
{
|
||||
moo_message ("error running python script '%s'", string);
|
||||
if (PyErr_Occurred ())
|
||||
PyErr_Print ();
|
||||
else
|
||||
moo_critical ("oops");
|
||||
ret = FALSE;
|
||||
}
|
||||
|
||||
Py_XDECREF (pyret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
gboolean
|
||||
moo_python_run_file (MooPythonState *state,
|
||||
const char *filename)
|
||||
{
|
||||
char *contents;
|
||||
GError *error = NULL;
|
||||
gboolean ret;
|
||||
|
||||
moo_return_val_if_fail (state && state->locals, FALSE);
|
||||
moo_return_val_if_fail (filename != NULL, FALSE);
|
||||
|
||||
if (!g_file_get_contents (filename, &contents, NULL, &error))
|
||||
{
|
||||
moo_warning ("could not read file '%s': %s",
|
||||
filename, moo_error_message (error));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
ret = moo_python_run_string (state, contents);
|
||||
|
||||
g_free (contents);
|
||||
return ret;
|
||||
}
|
||||
|
||||
gboolean
|
||||
medit_python_run_string (const char *string,
|
||||
gboolean default_init)
|
||||
{
|
||||
MooPythonState *state;
|
||||
gboolean ret;
|
||||
|
||||
moo_return_val_if_fail (string != NULL, FALSE);
|
||||
|
||||
state = moo_python_state_new (default_init);
|
||||
|
||||
if (!state)
|
||||
return FALSE;
|
||||
|
||||
ret = moo_python_run_string (state, string);
|
||||
|
||||
moo_python_state_free (state);
|
||||
return ret;
|
||||
}
|
||||
|
||||
gboolean
|
||||
medit_python_run_file (const char *filename,
|
||||
gboolean default_init)
|
||||
{
|
||||
char *contents;
|
||||
GError *error = NULL;
|
||||
gboolean ret;
|
||||
|
||||
moo_return_val_if_fail (filename != NULL, FALSE);
|
||||
|
||||
if (!g_file_get_contents (filename, &contents, NULL, &error))
|
||||
{
|
||||
moo_warning ("could not read file '%s': %s",
|
||||
filename, moo_error_message (error));
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
ret = medit_python_run_string (contents, default_init);
|
||||
|
||||
g_free (contents);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#else /* !MOO_ENABLE_PYTHON */
|
||||
#endif /* !MOO_ENABLE_PYTHON */
|
|
@ -0,0 +1,27 @@
|
|||
#ifndef MEDIT_PYTHON_H
|
||||
#define MEDIT_PYTHON_H
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
typedef struct MooPythonState MooPythonState;
|
||||
|
||||
gboolean moo_python_enabled (void);
|
||||
|
||||
MooPythonState *moo_python_state_new (gboolean default_init);
|
||||
void moo_python_state_free (MooPythonState *state);
|
||||
|
||||
gboolean moo_python_run_string (MooPythonState *state,
|
||||
const char *string);
|
||||
gboolean moo_python_run_file (MooPythonState *state,
|
||||
const char *filename);
|
||||
|
||||
gboolean medit_python_run_string (const char *string,
|
||||
gboolean default_init);
|
||||
gboolean medit_python_run_file (const char *filename,
|
||||
gboolean default_init);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* MEDIT_PYTHON_H */
|
|
@ -1,12 +1,10 @@
|
|||
moo_python_ini_in_in_files = \
|
||||
moopython/plugins/terminal.ini.in.in \
|
||||
moopython/plugins/python.ini.in.in \
|
||||
moopython/plugins/pycmd.ini.in.in
|
||||
moopython/plugins/python.ini.in.in
|
||||
|
||||
moo_python_plugins = \
|
||||
moopython/plugins/terminal.py \
|
||||
moopython/plugins/python.py \
|
||||
moopython/plugins/pycmd.py
|
||||
moopython/plugins/python.py
|
||||
|
||||
moo_python_lib_files = \
|
||||
moopython/plugins/lib/pyconsole.py \
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
[module]
|
||||
type=Python
|
||||
file=pycmd.py
|
||||
version=@MOO_MODULE_MAJOR_VERSION@.@MOO_MODULE_MINOR_VERSION@
|
|
@ -1,91 +0,0 @@
|
|||
#
|
||||
# pycmd.py
|
||||
#
|
||||
# Copyright (C) 2004-2010 by Yevgen Muntyan <emuntyan@users.sourceforge.net>
|
||||
#
|
||||
# This file is part of medit. medit is free software; you can
|
||||
# redistribute it and/or modify it under the terms of the
|
||||
# GNU Lesser General Public License as published by the
|
||||
# Free Software Foundation; either version 2.1 of the License,
|
||||
# or (at your option) any later version.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public
|
||||
# License along with medit. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
import moo
|
||||
import gobject
|
||||
import gtk
|
||||
from moo import _
|
||||
|
||||
class PyCmd(moo.Command):
|
||||
def __init__(self, code, options):
|
||||
moo.Command.__init__(self)
|
||||
if code and code[-1] != '\n' and code[-1] != '\r':
|
||||
self.code = code + '\n'
|
||||
else:
|
||||
self.code = code
|
||||
self.set_options(options)
|
||||
|
||||
def do_run(self, ctx):
|
||||
dic = {}
|
||||
dic['doc'] = ctx.get_doc()
|
||||
dic['view'] = ctx.get_doc().get_view()
|
||||
dic['window'] = ctx.get_window()
|
||||
dic['editor'] = moo.editor_instance()
|
||||
dic['moo'] = moo
|
||||
|
||||
buf = (ctx.get_doc() and ctx.get_doc().get_buffer()) or None
|
||||
|
||||
if buf is not None:
|
||||
buf.begin_user_action()
|
||||
|
||||
exc = None
|
||||
|
||||
try:
|
||||
exec self.code in dic
|
||||
except Exception, e:
|
||||
exc = e
|
||||
|
||||
if buf is not None:
|
||||
buf.end_user_action()
|
||||
|
||||
if exc is not None:
|
||||
raise exc
|
||||
|
||||
class PyCmdFactory(moo.CommandFactory):
|
||||
def do_create_command(self, data, options):
|
||||
return PyCmd(data.get_code(), moo.parse_command_options(options))
|
||||
|
||||
def do_create_widget(self):
|
||||
swin = gtk.ScrolledWindow()
|
||||
swin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
|
||||
swin.set_shadow_type(gtk.SHADOW_ETCHED_IN)
|
||||
textview = moo.TextView()
|
||||
swin.add(textview)
|
||||
swin.show_all()
|
||||
textview.set_font_from_string("Monospace")
|
||||
textview.set_lang_by_id("python")
|
||||
swin.textview = textview
|
||||
return swin
|
||||
|
||||
def do_load_data(self, widget, data):
|
||||
code = data.get_code()
|
||||
if not code:
|
||||
code = ""
|
||||
elif not code.endswith("\n"):
|
||||
code = code + "\n"
|
||||
widget.textview.get_buffer().set_text(code)
|
||||
|
||||
def do_save_data(self, widget, data):
|
||||
new_code = widget.textview.get_buffer().props.text or None
|
||||
old_code = data.get_code() or None
|
||||
if new_code != old_code:
|
||||
data.set_code(new_code)
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
gobject.type_register(PyCmd)
|
||||
gobject.type_register(PyCmdFactory)
|
||||
moo.command_factory_register("python", _("Python script"), PyCmdFactory(), None, ".py")
|
|
@ -19,12 +19,12 @@ plugins_sources += \
|
|||
|
||||
EXTRA_DIST += \
|
||||
plugins/usertools/glade/mooedittools-exe.glade \
|
||||
plugins/usertools/glade/mooedittools-lua.glade \
|
||||
plugins/usertools/glade/mooedittools-script.glade\
|
||||
plugins/usertools/glade/moousertools.glade
|
||||
|
||||
built_plugins_sources += \
|
||||
plugins/usertools/mooedittools-exe-gxml.h \
|
||||
plugins/usertools/mooedittools-lua-gxml.h \
|
||||
plugins/usertools/mooedittools-script-gxml.h \
|
||||
plugins/usertools/moousertools-gxml.h
|
||||
|
||||
EXTRA_DIST += plugins/usertools/lua-tool-setup.lua
|
||||
|
@ -32,9 +32,25 @@ built_plugins_sources += plugins/usertools/lua-tool-setup.h
|
|||
plugins/usertools/lua-tool-setup.h: plugins/usertools/lua-tool-setup.lua $(top_srcdir)/tools/xml2h.py
|
||||
$(AM_V_at)$(MKDIR_P) plugins/usertools
|
||||
$(AM_V_GEN)$(PYTHON) $(top_srcdir)/tools/xml2h.py $(srcdir)/plugins/usertools/lua-tool-setup.lua \
|
||||
plugins/usertools/lua-tool-setup.h.tmp LUA_SETUP_CODE \
|
||||
plugins/usertools/lua-tool-setup.h.tmp LUA_TOOL_SETUP_LUA \
|
||||
&& mv plugins/usertools/lua-tool-setup.h.tmp plugins/usertools/lua-tool-setup.h
|
||||
|
||||
EXTRA_DIST += \
|
||||
plugins/usertools/moocommand-python.c \
|
||||
plugins/usertools/moocommand-python.h \
|
||||
plugins/usertools/python-tool-setup.py
|
||||
if MOO_ENABLE_PYTHON
|
||||
plugins_sources += \
|
||||
plugins/usertools/moocommand-python.c \
|
||||
plugins/usertools/moocommand-python.h
|
||||
built_plugins_sources += plugins/usertools/python-tool-setup.h
|
||||
plugins/usertools/python-tool-setup.h: plugins/usertools/python-tool-setup.py $(top_srcdir)/tools/xml2h.py
|
||||
$(AM_V_at)$(MKDIR_P) plugins/usertools
|
||||
$(AM_V_GEN)$(PYTHON) $(top_srcdir)/tools/xml2h.py $(srcdir)/plugins/usertools/python-tool-setup.py \
|
||||
plugins/usertools/python-tool-setup.h.tmp PYTHON_TOOL_SETUP_PY \
|
||||
&& mv plugins/usertools/python-tool-setup.h.tmp plugins/usertools/python-tool-setup.h
|
||||
endif
|
||||
|
||||
cfgdir = $(MOO_DATA_DIR)
|
||||
cfg_DATA = \
|
||||
plugins/usertools/filters.xml \
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
<property name="visible">True</property>
|
||||
<property name="title">window2</property>
|
||||
<child>
|
||||
<widget class="GtkVBox" id="LuaPage">
|
||||
<widget class="GtkVBox" id="ScriptPage">
|
||||
<property name="visible">True</property>
|
||||
<property name="orientation">vertical</property>
|
||||
<property name="spacing">6</property>
|
|
@ -13,15 +13,13 @@
|
|||
* License along with medit. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#define MOOEDIT_COMPILATION
|
||||
#include "moocommand-lua.h"
|
||||
#include "plugins/usertools/lua-tool-setup.h"
|
||||
#include "mooedit/mooeditor.h"
|
||||
#include "mooedit/mootext-private.h"
|
||||
#include "mooutils/mooi18n.h"
|
||||
#include "mooutils/mooutils-misc.h"
|
||||
#include "mooutils/mootype-macros.h"
|
||||
#include "plugins/usertools/mooedittools-lua-gxml.h"
|
||||
#include "plugins/usertools/mooedittools-script-gxml.h"
|
||||
#include "moolua/medit-lua.h"
|
||||
#include <string.h>
|
||||
|
||||
|
@ -47,18 +45,18 @@ moo_command_lua_run (MooCommand *cmd_base,
|
|||
GtkTextBuffer *buffer = NULL;
|
||||
lua_State *L;
|
||||
|
||||
g_return_if_fail (cmd->priv->code != NULL);
|
||||
g_return_if_fail (cmd->code != NULL);
|
||||
|
||||
L = medit_lua_new (TRUE);
|
||||
g_return_if_fail (L != NULL);
|
||||
|
||||
if (!medit_lua_do_string (L, LUA_SETUP_CODE))
|
||||
if (!medit_lua_do_string (L, LUA_TOOL_SETUP_LUA))
|
||||
{
|
||||
medit_lua_free (L);
|
||||
return;
|
||||
}
|
||||
|
||||
if (luaL_loadstring (L, cmd->priv->code) != 0)
|
||||
if (luaL_loadstring (L, cmd->code) != 0)
|
||||
{
|
||||
const char *msg = lua_tostring (L, -1);
|
||||
g_critical ("%s: %s", G_STRLOC, msg ? msg : "ERROR");
|
||||
|
@ -91,11 +89,8 @@ moo_command_lua_dispose (GObject *object)
|
|||
{
|
||||
MooCommandLua *cmd = MOO_COMMAND_LUA (object);
|
||||
|
||||
if (cmd->priv)
|
||||
{
|
||||
g_free (cmd->priv->code);
|
||||
cmd->priv = NULL;
|
||||
}
|
||||
g_free (cmd->code);
|
||||
cmd->code = NULL;
|
||||
|
||||
G_OBJECT_CLASS(_moo_command_lua_parent_class)->dispose (object);
|
||||
}
|
||||
|
@ -122,14 +117,14 @@ lua_factory_create_command (G_GNUC_UNUSED MooCommandFactory *factory,
|
|||
static GtkWidget *
|
||||
lua_factory_create_widget (G_GNUC_UNUSED MooCommandFactory *factory)
|
||||
{
|
||||
LuaPageXml *xml;
|
||||
ScriptPageXml *xml;
|
||||
|
||||
xml = lua_page_xml_new ();
|
||||
xml = script_page_xml_new ();
|
||||
|
||||
moo_text_view_set_font_from_string (xml->textview, "Monospace");
|
||||
moo_text_view_set_lang_by_id (xml->textview, "lua");
|
||||
|
||||
return GTK_WIDGET (xml->LuaPage);
|
||||
return GTK_WIDGET (xml->ScriptPage);
|
||||
}
|
||||
|
||||
|
||||
|
@ -138,11 +133,11 @@ lua_factory_load_data (G_GNUC_UNUSED MooCommandFactory *factory,
|
|||
GtkWidget *page,
|
||||
MooCommandData *data)
|
||||
{
|
||||
LuaPageXml *xml;
|
||||
ScriptPageXml *xml;
|
||||
GtkTextBuffer *buffer;
|
||||
const char *code;
|
||||
|
||||
xml = lua_page_xml_get (page);
|
||||
xml = script_page_xml_get (page);
|
||||
g_return_if_fail (xml != NULL);
|
||||
|
||||
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (xml->textview));
|
||||
|
@ -157,12 +152,12 @@ lua_factory_save_data (G_GNUC_UNUSED MooCommandFactory *factory,
|
|||
GtkWidget *page,
|
||||
MooCommandData *data)
|
||||
{
|
||||
LuaPageXml *xml;
|
||||
ScriptPageXml *xml;
|
||||
const char *code;
|
||||
char *new_code;
|
||||
gboolean changed = FALSE;
|
||||
|
||||
xml = lua_page_xml_get (page);
|
||||
xml = script_page_xml_get (page);
|
||||
g_return_val_if_fail (xml != NULL, FALSE);
|
||||
|
||||
new_code = moo_text_view_get_text (GTK_TEXT_VIEW (xml->textview));
|
||||
|
@ -199,10 +194,8 @@ _moo_command_lua_class_init (MooCommandLuaClass *klass)
|
|||
{
|
||||
MooCommandFactory *factory;
|
||||
|
||||
G_OBJECT_CLASS(klass)->dispose = moo_command_lua_dispose;
|
||||
MOO_COMMAND_CLASS(klass)->run = moo_command_lua_run;
|
||||
|
||||
g_type_class_add_private (klass, sizeof (MooCommandLuaPrivate));
|
||||
G_OBJECT_CLASS (klass)->dispose = moo_command_lua_dispose;
|
||||
MOO_COMMAND_CLASS (klass)->run = moo_command_lua_run;
|
||||
|
||||
factory = MOO_COMMAND_FACTORY (g_object_new (_moo_command_factory_lua_get_type (), (const char*) NULL));
|
||||
moo_command_factory_register ("lua", _("Lua script"), factory, NULL, ".lua");
|
||||
|
@ -211,11 +204,8 @@ _moo_command_lua_class_init (MooCommandLuaClass *klass)
|
|||
|
||||
|
||||
static void
|
||||
_moo_command_lua_init (MooCommandLua *cmd)
|
||||
_moo_command_lua_init (G_GNUC_UNUSED MooCommandLua *cmd)
|
||||
{
|
||||
cmd->priv = G_TYPE_INSTANCE_GET_PRIVATE (cmd,
|
||||
MOO_TYPE_COMMAND_LUA,
|
||||
MooCommandLuaPrivate);
|
||||
}
|
||||
|
||||
|
||||
|
@ -228,7 +218,7 @@ moo_command_lua_new (const char *code,
|
|||
g_return_val_if_fail (code != NULL, NULL);
|
||||
|
||||
cmd = MOO_COMMAND_LUA (g_object_new (MOO_TYPE_COMMAND_LUA, "options", options, (const char*) NULL));
|
||||
cmd->priv->code = g_strdup (code);
|
||||
cmd->code = g_strdup (code);
|
||||
|
||||
return MOO_COMMAND (cmd);
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ typedef struct _MooCommandLuaClass MooCommandLuaClass;
|
|||
|
||||
struct _MooCommandLua {
|
||||
MooCommand base;
|
||||
MooCommandLuaPrivate *priv;
|
||||
char *code;
|
||||
};
|
||||
|
||||
struct _MooCommandLuaClass {
|
||||
|
|
|
@ -0,0 +1,199 @@
|
|||
/*
|
||||
* moocommand-python.cpp
|
||||
*
|
||||
* Copyright (C) 2004-2010 by Yevgen Muntyan <emuntyan@users.sourceforge.net>
|
||||
*
|
||||
* This file is part of medit. medit is free software; you can
|
||||
* redistribute it and/or modify it under the terms of the
|
||||
* GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation; either version 2.1 of the License,
|
||||
* or (at your option) any later version.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with medit. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "moocommand-python.h"
|
||||
#include "plugins/usertools/python-tool-setup.h"
|
||||
#include "mooedit/mooeditor.h"
|
||||
#include "mooutils/mooi18n.h"
|
||||
#include "mooutils/mooutils-misc.h"
|
||||
#include "mooutils/mootype-macros.h"
|
||||
#include "plugins/usertools/mooedittools-script-gxml.h"
|
||||
#include "moopython/medit-python.h"
|
||||
#include <string.h>
|
||||
|
||||
G_DEFINE_TYPE (MooCommandPython, _moo_command_python, MOO_TYPE_COMMAND)
|
||||
|
||||
typedef MooCommandFactory MooCommandFactoryPython;
|
||||
typedef MooCommandFactoryClass MooCommandFactoryPythonClass;
|
||||
MOO_DEFINE_TYPE_STATIC (MooCommandFactoryPython, _moo_command_factory_python, MOO_TYPE_COMMAND_FACTORY)
|
||||
|
||||
static MooCommand *moo_command_python_new (const char *code,
|
||||
MooCommandOptions options);
|
||||
|
||||
static void
|
||||
moo_command_python_run (MooCommand *cmd_base,
|
||||
MooCommandContext *ctx)
|
||||
{
|
||||
MooCommandPython *cmd = MOO_COMMAND_PYTHON (cmd_base);
|
||||
GtkTextBuffer *buffer = NULL;
|
||||
MooPythonState *state;
|
||||
|
||||
g_return_if_fail (cmd->code != NULL);
|
||||
|
||||
state = moo_python_state_new (TRUE);
|
||||
moo_return_if_fail (state != NULL);
|
||||
|
||||
if (!moo_python_run_string (state, PYTHON_TOOL_SETUP_PY))
|
||||
{
|
||||
moo_python_state_free (state);
|
||||
return;
|
||||
}
|
||||
|
||||
if (moo_command_context_get_doc (ctx))
|
||||
buffer = moo_edit_get_buffer (moo_command_context_get_doc (ctx));
|
||||
|
||||
if (buffer)
|
||||
gtk_text_buffer_begin_user_action (buffer);
|
||||
|
||||
moo_python_run_string (state, cmd->code);
|
||||
|
||||
if (buffer)
|
||||
gtk_text_buffer_end_user_action (buffer);
|
||||
|
||||
moo_python_state_free (state);
|
||||
}
|
||||
|
||||
static void
|
||||
moo_command_python_dispose (GObject *object)
|
||||
{
|
||||
MooCommandPython *cmd = MOO_COMMAND_PYTHON (object);
|
||||
|
||||
g_free (cmd->code);
|
||||
cmd->code = NULL;
|
||||
|
||||
G_OBJECT_CLASS (_moo_command_python_parent_class)->dispose (object);
|
||||
}
|
||||
|
||||
static MooCommand *
|
||||
python_factory_create_command (G_GNUC_UNUSED MooCommandFactory *factory,
|
||||
MooCommandData *data,
|
||||
const char *options)
|
||||
{
|
||||
MooCommand *cmd;
|
||||
const char *code;
|
||||
|
||||
code = moo_command_data_get_code (data);
|
||||
moo_return_val_if_fail (code && *code, NULL);
|
||||
|
||||
cmd = moo_command_python_new (code, moo_parse_command_options (options));
|
||||
moo_return_val_if_fail (cmd != NULL, NULL);
|
||||
|
||||
return cmd;
|
||||
}
|
||||
|
||||
static GtkWidget *
|
||||
python_factory_create_widget (G_GNUC_UNUSED MooCommandFactory *factory)
|
||||
{
|
||||
ScriptPageXml *xml;
|
||||
|
||||
xml = script_page_xml_new ();
|
||||
|
||||
moo_text_view_set_font_from_string (xml->textview, "Monospace");
|
||||
moo_text_view_set_lang_by_id (xml->textview, "python");
|
||||
|
||||
return GTK_WIDGET (xml->ScriptPage);
|
||||
}
|
||||
|
||||
static void
|
||||
python_factory_load_data (G_GNUC_UNUSED MooCommandFactory *factory,
|
||||
GtkWidget *page,
|
||||
MooCommandData *data)
|
||||
{
|
||||
ScriptPageXml *xml;
|
||||
GtkTextBuffer *buffer;
|
||||
const char *code;
|
||||
|
||||
xml = script_page_xml_get (page);
|
||||
g_return_if_fail (xml != NULL);
|
||||
|
||||
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (xml->textview));
|
||||
|
||||
code = moo_command_data_get_code (data);
|
||||
gtk_text_buffer_set_text (buffer, code ? code : "", -1);
|
||||
}
|
||||
|
||||
|
||||
static gboolean
|
||||
python_factory_save_data (G_GNUC_UNUSED MooCommandFactory *factory,
|
||||
GtkWidget *page,
|
||||
MooCommandData *data)
|
||||
{
|
||||
ScriptPageXml *xml;
|
||||
const char *code;
|
||||
char *new_code;
|
||||
gboolean changed = FALSE;
|
||||
|
||||
xml = script_page_xml_get (page);
|
||||
moo_return_val_if_fail (xml != NULL, FALSE);
|
||||
|
||||
new_code = moo_text_view_get_text (GTK_TEXT_VIEW (xml->textview));
|
||||
code = moo_command_data_get_code (data);
|
||||
|
||||
if (!_moo_str_equal (code, new_code))
|
||||
{
|
||||
moo_command_data_set_code (data, new_code);
|
||||
changed = TRUE;
|
||||
}
|
||||
|
||||
g_free (new_code);
|
||||
return changed;
|
||||
}
|
||||
|
||||
static void
|
||||
_moo_command_factory_python_init (G_GNUC_UNUSED MooCommandFactory *factory)
|
||||
{
|
||||
}
|
||||
|
||||
static void
|
||||
_moo_command_factory_python_class_init (MooCommandFactoryClass *klass)
|
||||
{
|
||||
klass->create_command = python_factory_create_command;
|
||||
klass->create_widget = python_factory_create_widget;
|
||||
klass->load_data = python_factory_load_data;
|
||||
klass->save_data = python_factory_save_data;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
_moo_command_python_class_init (MooCommandPythonClass *klass)
|
||||
{
|
||||
MooCommandFactory *factory;
|
||||
|
||||
G_OBJECT_CLASS (klass)->dispose = moo_command_python_dispose;
|
||||
MOO_COMMAND_CLASS (klass)->run = moo_command_python_run;
|
||||
|
||||
factory = MOO_COMMAND_FACTORY (g_object_new (_moo_command_factory_python_get_type (), (const char*) NULL));
|
||||
moo_command_factory_register ("python", _("Python script"), factory, NULL, ".py");
|
||||
g_object_unref (factory);
|
||||
}
|
||||
|
||||
static void
|
||||
_moo_command_python_init (G_GNUC_UNUSED MooCommandPython *cmd)
|
||||
{
|
||||
}
|
||||
|
||||
static MooCommand *
|
||||
moo_command_python_new (const char *code,
|
||||
MooCommandOptions options)
|
||||
{
|
||||
MooCommandPython *cmd;
|
||||
|
||||
moo_return_val_if_fail (code != NULL, NULL);
|
||||
|
||||
cmd = MOO_COMMAND_PYTHON (g_object_new (MOO_TYPE_COMMAND_PYTHON, "options", options, (const char*) NULL));
|
||||
cmd->code = g_strdup (code);
|
||||
|
||||
return MOO_COMMAND (cmd);
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* moocommand-python.h
|
||||
*
|
||||
* Copyright (C) 2004-2010 by Yevgen Muntyan <emuntyan@users.sourceforge.net>
|
||||
*
|
||||
* This file is part of medit. medit is free software; you can
|
||||
* redistribute it and/or modify it under the terms of the
|
||||
* GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation; either version 2.1 of the License,
|
||||
* or (at your option) any later version.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with medit. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef MOO_COMMAND_PYTHON_H
|
||||
#define MOO_COMMAND_PYTHON_H
|
||||
|
||||
#include "plugins/usertools/moocommand.h"
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
|
||||
#define MOO_TYPE_COMMAND_PYTHON (_moo_command_python_get_type ())
|
||||
#define MOO_COMMAND_PYTHON(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), MOO_TYPE_COMMAND_PYTHON, MooCommandPython))
|
||||
#define MOO_COMMAND_PYTHON_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MOO_TYPE_COMMAND_PYTHON, MooCommandPythonClass))
|
||||
#define MOO_IS_COMMAND_PYTHON(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), MOO_TYPE_COMMAND_PYTHON))
|
||||
#define MOO_IS_COMMAND_PYTHON_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MOO_TYPE_COMMAND_PYTHON))
|
||||
#define MOO_COMMAND_PYTHON_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MOO_TYPE_COMMAND_PYTHON, MooCommandPythonClass))
|
||||
|
||||
typedef struct _MooCommandPython MooCommandPython;
|
||||
typedef struct _MooCommandPythonPrivate MooCommandPythonPrivate;
|
||||
typedef struct _MooCommandPythonClass MooCommandPythonClass;
|
||||
|
||||
struct _MooCommandPython {
|
||||
MooCommand base;
|
||||
char *code;
|
||||
};
|
||||
|
||||
struct _MooCommandPythonClass {
|
||||
MooCommandClass base_class;
|
||||
};
|
||||
|
||||
|
||||
GType _moo_command_python_get_type (void) G_GNUC_CONST;
|
||||
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* MOO_COMMAND_PYTHON_H */
|
|
@ -13,26 +13,6 @@
|
|||
* License along with medit. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* class:MooCommand: (parent GObject) (moo.private 1)
|
||||
**/
|
||||
|
||||
/**
|
||||
* class:MooCommandContext: (parent GObject) (constructable) (moo.private 1)
|
||||
**/
|
||||
|
||||
/**
|
||||
* class:MooCommandFactory: (parent GObject) (moo.private 1)
|
||||
**/
|
||||
|
||||
/**
|
||||
* boxed:MooCommandData: (moo.private 1)
|
||||
**/
|
||||
|
||||
/**
|
||||
* flags:MooCommandOptions: (moo.private 1)
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
@ -40,6 +20,7 @@
|
|||
#define MOOEDIT_COMPILATION
|
||||
#include "moocommand-private.h"
|
||||
#include "moocommand-lua.h"
|
||||
#include "moocommand-python.h"
|
||||
#include "moocommand-exe.h"
|
||||
#include "moooutputfilterregex.h"
|
||||
#include "mooedit/mooeditwindow.h"
|
||||
|
@ -124,15 +105,6 @@ moo_command_create (const char *name,
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* moo_command_factory_register: (moo.private 1)
|
||||
*
|
||||
* @name:
|
||||
* @display_name:
|
||||
* @factory:
|
||||
* @keys: (type strv) (allow-none) (default NULL)
|
||||
* @extension: (allow-none) (default NULL)
|
||||
**/
|
||||
void
|
||||
moo_command_factory_register (const char *name,
|
||||
const char *display_name,
|
||||
|
@ -549,9 +521,6 @@ moo_command_check_sensitive (MooCommand *cmd,
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* moo_command_set_options:
|
||||
**/
|
||||
void
|
||||
moo_command_set_options (MooCommand *cmd,
|
||||
MooCommandOptions options)
|
||||
|
@ -568,9 +537,6 @@ moo_command_set_options (MooCommand *cmd,
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* moo_command_get_options:
|
||||
**/
|
||||
MooCommandOptions
|
||||
moo_command_get_options (MooCommand *cmd)
|
||||
{
|
||||
|
@ -579,9 +545,6 @@ moo_command_get_options (MooCommand *cmd)
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* moo_parse_command_options:
|
||||
**/
|
||||
MooCommandOptions
|
||||
moo_parse_command_options (const char *string)
|
||||
{
|
||||
|
@ -917,9 +880,6 @@ moo_command_context_set_window (MooCommandContext *ctx,
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* moo_command_context_get_doc:
|
||||
**/
|
||||
MooEdit *
|
||||
moo_command_context_get_doc (MooCommandContext *ctx)
|
||||
{
|
||||
|
@ -928,9 +888,6 @@ moo_command_context_get_doc (MooCommandContext *ctx)
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* moo_command_context_get_window:
|
||||
**/
|
||||
MooEditWindow *
|
||||
moo_command_context_get_window (MooCommandContext *ctx)
|
||||
{
|
||||
|
@ -1373,9 +1330,6 @@ moo_command_data_unref (MooCommandData *data)
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* moo_command_data_set:
|
||||
**/
|
||||
void
|
||||
moo_command_data_set (MooCommandData *data,
|
||||
guint index,
|
||||
|
@ -1387,9 +1341,6 @@ moo_command_data_set (MooCommandData *data,
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* moo_command_data_get:
|
||||
**/
|
||||
const char *
|
||||
moo_command_data_get (MooCommandData *data,
|
||||
guint index)
|
||||
|
@ -1410,9 +1361,6 @@ moo_command_data_take_code (MooCommandData *data,
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* moo_command_data_set_code:
|
||||
**/
|
||||
void
|
||||
moo_command_data_set_code (MooCommandData *data,
|
||||
const char *code)
|
||||
|
@ -1421,9 +1369,6 @@ moo_command_data_set_code (MooCommandData *data,
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* moo_command_data_get_code:
|
||||
**/
|
||||
const char *
|
||||
moo_command_data_get_code (MooCommandData *data)
|
||||
{
|
||||
|
@ -1440,6 +1385,9 @@ _moo_command_init (void)
|
|||
if (!been_here)
|
||||
{
|
||||
g_type_class_unref (g_type_class_ref (MOO_TYPE_COMMAND_LUA));
|
||||
#ifdef MOO_ENABLE_PYTHON
|
||||
g_type_class_unref (g_type_class_ref (MOO_TYPE_COMMAND_PYTHON));
|
||||
#endif
|
||||
g_type_class_unref (g_type_class_ref (MOO_TYPE_COMMAND_EXE));
|
||||
_moo_command_filter_regex_load ();
|
||||
been_here = TRUE;
|
||||
|
|
|
@ -73,11 +73,9 @@ struct _MooCommand {
|
|||
struct _MooCommandClass {
|
||||
GObjectClass base_class;
|
||||
|
||||
/**vtable:MooCommand**/
|
||||
gboolean (*check_sensitive) (MooCommand *cmd,
|
||||
MooEdit *doc,
|
||||
MooEditWindow *window);
|
||||
/**vtable:MooCommand**/
|
||||
void (*run) (MooCommand *cmd,
|
||||
MooCommandContext *ctx);
|
||||
};
|
||||
|
@ -94,21 +92,16 @@ struct _MooCommandFactory {
|
|||
struct _MooCommandFactoryClass {
|
||||
GObjectClass base_class;
|
||||
|
||||
/**vtable:MooCommandFactory**/
|
||||
MooCommand *(*create_command) (MooCommandFactory *factory,
|
||||
MooCommandData *data,
|
||||
const char *options);
|
||||
/**vtable:MooCommandFactory**/
|
||||
GtkWidget *(*create_widget) (MooCommandFactory *factory);
|
||||
/**vtable:MooCommandFactory**/
|
||||
void (*load_data) (MooCommandFactory *factory,
|
||||
GtkWidget *widget,
|
||||
MooCommandData *data);
|
||||
/**vtable:MooCommandFactory**/
|
||||
gboolean (*save_data) (MooCommandFactory *factory,
|
||||
GtkWidget *widget,
|
||||
MooCommandData *data);
|
||||
/**vtable:MooCommandFactory**/
|
||||
gboolean (*data_equal) (MooCommandFactory *factory,
|
||||
MooCommandData *data1,
|
||||
MooCommandData *data2);
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
doc = editor.get_active_doc()
|
||||
window = editor.get_active_window()
|
|
@ -11,7 +11,7 @@ files = subprocess.Popen(['hg', 'log', '-r', 'tip', '--template', '{files}'],
|
|||
status = 0
|
||||
|
||||
for name in files:
|
||||
if not name.endswith('.glade'):
|
||||
if not name.endswith('.glade') or not os.path.exists(name):
|
||||
continue
|
||||
for line in open(name):
|
||||
if re.match(r'\s+<.*', line):
|
||||
|
|
Loading…
Reference in New Issue