2006-08-15 22:18:16 -07:00
|
|
|
/*
|
|
|
|
* moocommand-script.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004-2006 by Yevgen Muntyan <muntyan@math.tamu.edu>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* See COPYING file that comes with this distribution.
|
|
|
|
*/
|
|
|
|
|
2006-08-18 22:21:29 -07:00
|
|
|
#define MOOEDIT_COMPILATION
|
2006-08-15 22:18:16 -07:00
|
|
|
#include "mooedit/moocommand-script.h"
|
|
|
|
#include "mooedit/mooedit-script.h"
|
2006-08-16 18:27:19 -07:00
|
|
|
#include "mooedit/mooedittools-glade.h"
|
2006-08-17 21:53:34 -07:00
|
|
|
#include "mooedit/mooeditor.h"
|
2006-08-18 22:21:29 -07:00
|
|
|
#include "mooedit/mootext-private.h"
|
2006-08-15 22:18:16 -07:00
|
|
|
#include "mooscript/mooscript-parser.h"
|
2006-08-16 18:27:19 -07:00
|
|
|
#include "mooutils/mooi18n.h"
|
|
|
|
#include "mooutils/mooglade.h"
|
2006-09-02 00:29:39 -07:00
|
|
|
#include "mooutils/mooutils-misc.h"
|
2006-08-16 18:27:19 -07:00
|
|
|
#include <string.h>
|
2006-08-15 22:18:16 -07:00
|
|
|
|
|
|
|
|
|
|
|
struct _MooCommandScriptPrivate {
|
|
|
|
MSNode *script;
|
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (MooCommandScript, moo_command_script, MOO_TYPE_COMMAND)
|
|
|
|
|
2006-08-16 22:08:49 -07:00
|
|
|
typedef MooCommandType MooCommandTypeScript;
|
|
|
|
typedef MooCommandTypeClass MooCommandTypeScriptClass;
|
|
|
|
GType _moo_command_type_script_get_type (void) G_GNUC_CONST;
|
|
|
|
G_DEFINE_TYPE (MooCommandTypeScript, _moo_command_type_script, MOO_TYPE_COMMAND_TYPE)
|
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
|
|
|
|
static void
|
|
|
|
set_variable (const char *name,
|
|
|
|
const GValue *value,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
MSContext *ctx = data;
|
|
|
|
MSValue *ms_value;
|
|
|
|
|
|
|
|
ms_value = ms_value_from_gvalue (value);
|
|
|
|
g_return_if_fail (ms_value != NULL);
|
|
|
|
|
|
|
|
ms_context_assign_variable (ctx, name, ms_value);
|
|
|
|
ms_value_unref (ms_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
moo_command_script_run (MooCommand *cmd_base,
|
|
|
|
MooCommandContext *ctx)
|
|
|
|
{
|
2006-08-18 22:21:29 -07:00
|
|
|
gpointer doc;
|
2006-08-15 22:18:16 -07:00
|
|
|
MSValue *ret;
|
|
|
|
MSContext *script_ctx;
|
|
|
|
MooCommandScript *cmd = MOO_COMMAND_SCRIPT (cmd_base);
|
2006-08-20 01:49:33 -07:00
|
|
|
GtkTextBuffer *buffer;
|
2006-08-15 22:18:16 -07:00
|
|
|
|
|
|
|
g_return_if_fail (cmd->priv->script != NULL);
|
|
|
|
|
|
|
|
script_ctx = moo_edit_script_context_new (moo_command_context_get_doc (ctx),
|
|
|
|
moo_command_context_get_window (ctx));
|
|
|
|
g_return_if_fail (script_ctx != NULL);
|
|
|
|
|
|
|
|
moo_command_context_foreach (ctx, set_variable, script_ctx);
|
|
|
|
|
2006-08-18 22:21:29 -07:00
|
|
|
doc = moo_command_context_get_doc (ctx);
|
2006-08-20 01:49:33 -07:00
|
|
|
buffer = doc ? gtk_text_view_get_buffer (doc) : NULL;
|
2006-08-18 22:21:29 -07:00
|
|
|
|
2006-08-20 01:49:33 -07:00
|
|
|
if (buffer)
|
|
|
|
gtk_text_buffer_begin_user_action (buffer);
|
2006-08-18 22:21:29 -07:00
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
ret = ms_top_node_eval (cmd->priv->script, script_ctx);
|
|
|
|
|
2006-08-20 01:49:33 -07:00
|
|
|
if (buffer)
|
|
|
|
gtk_text_buffer_end_user_action (buffer);
|
2006-08-18 22:21:29 -07:00
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
g_print ("%s\n", ms_context_get_error_msg (script_ctx));
|
|
|
|
ms_context_clear_error (script_ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
ms_value_unref (ret);
|
|
|
|
g_object_unref (script_ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
moo_command_script_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
MooCommandScript *cmd = MOO_COMMAND_SCRIPT (object);
|
|
|
|
|
|
|
|
if (cmd->priv)
|
|
|
|
{
|
|
|
|
ms_node_unref (cmd->priv->script);
|
|
|
|
cmd->priv = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
G_OBJECT_CLASS(moo_command_script_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static MooCommand *
|
2006-08-16 22:08:49 -07:00
|
|
|
script_type_create_command (G_GNUC_UNUSED MooCommandType *type,
|
|
|
|
MooCommandData *data,
|
|
|
|
const char *options)
|
2006-08-15 22:18:16 -07:00
|
|
|
{
|
|
|
|
MooCommand *cmd;
|
|
|
|
const char *code;
|
|
|
|
|
2006-08-31 08:21:39 -07:00
|
|
|
code = moo_command_data_get_code (data);
|
2006-08-15 22:18:16 -07:00
|
|
|
|
|
|
|
g_return_val_if_fail (code && *code, NULL);
|
|
|
|
|
2006-08-16 18:27:19 -07:00
|
|
|
cmd = moo_command_script_new (code, moo_command_options_parse (options));
|
2006-08-15 22:18:16 -07:00
|
|
|
g_return_val_if_fail (cmd != NULL, NULL);
|
|
|
|
|
|
|
|
return cmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-16 18:27:19 -07:00
|
|
|
static GtkWidget *
|
2006-08-16 22:08:49 -07:00
|
|
|
script_type_create_widget (G_GNUC_UNUSED MooCommandType *type)
|
2006-08-16 18:27:19 -07:00
|
|
|
{
|
|
|
|
GtkWidget *page;
|
|
|
|
MooGladeXML *xml;
|
2006-08-17 21:53:34 -07:00
|
|
|
MooTextView *textview;
|
2006-08-16 18:27:19 -07:00
|
|
|
|
|
|
|
xml = moo_glade_xml_new_empty (GETTEXT_PACKAGE);
|
2006-08-17 21:53:34 -07:00
|
|
|
moo_glade_xml_map_id (xml, "textview", MOO_TYPE_TEXT_VIEW);
|
2006-08-16 18:27:19 -07:00
|
|
|
moo_glade_xml_parse_memory (xml, MOO_EDIT_TOOLS_GLADE_XML, -1, "moo_script_page", NULL);
|
|
|
|
page = moo_glade_xml_get_widget (xml, "moo_script_page");
|
|
|
|
g_return_val_if_fail (page != NULL, NULL);
|
|
|
|
|
2006-08-17 21:53:34 -07:00
|
|
|
textview = moo_glade_xml_get_widget (xml, "textview");
|
2006-08-20 01:49:33 -07:00
|
|
|
moo_text_view_set_font_from_string (textview, "Monospace");
|
2006-08-26 02:46:29 -07:00
|
|
|
moo_text_view_set_lang_by_id (textview, "mooscript");
|
2006-08-17 21:53:34 -07:00
|
|
|
|
2006-08-16 18:27:19 -07:00
|
|
|
g_object_set_data_full (G_OBJECT (page), "moo-glade-xml", xml, g_object_unref);
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2006-08-16 22:08:49 -07:00
|
|
|
script_type_load_data (G_GNUC_UNUSED MooCommandType *type,
|
|
|
|
GtkWidget *page,
|
|
|
|
MooCommandData *data)
|
2006-08-16 18:27:19 -07:00
|
|
|
{
|
|
|
|
MooGladeXML *xml;
|
|
|
|
GtkTextView *textview;
|
|
|
|
GtkTextBuffer *buffer;
|
|
|
|
const char *code;
|
|
|
|
|
|
|
|
xml = g_object_get_data (G_OBJECT (page), "moo-glade-xml");
|
|
|
|
textview = moo_glade_xml_get_widget (xml, "textview");
|
|
|
|
buffer = gtk_text_view_get_buffer (textview);
|
|
|
|
|
2006-08-31 08:21:39 -07:00
|
|
|
code = moo_command_data_get_code (data);
|
2006-08-16 18:27:19 -07:00
|
|
|
gtk_text_buffer_set_text (buffer, code ? code : "", -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2006-08-16 22:08:49 -07:00
|
|
|
script_type_save_data (G_GNUC_UNUSED MooCommandType *type,
|
|
|
|
GtkWidget *page,
|
|
|
|
MooCommandData *data)
|
2006-08-16 18:27:19 -07:00
|
|
|
{
|
|
|
|
MooGladeXML *xml;
|
|
|
|
GtkTextView *textview;
|
|
|
|
const char *code;
|
|
|
|
char *new_code;
|
|
|
|
gboolean changed = FALSE;
|
|
|
|
|
|
|
|
xml = g_object_get_data (G_OBJECT (page), "moo-glade-xml");
|
|
|
|
textview = moo_glade_xml_get_widget (xml, "textview");
|
|
|
|
g_assert (GTK_IS_TEXT_VIEW (textview));
|
|
|
|
|
|
|
|
new_code = moo_text_view_get_text (textview);
|
2006-08-31 08:21:39 -07:00
|
|
|
code = moo_command_data_get_code (data);
|
2006-08-16 18:27:19 -07:00
|
|
|
|
2006-09-02 00:29:39 -07:00
|
|
|
if (!_moo_str_equal (code, new_code))
|
2006-08-16 18:27:19 -07:00
|
|
|
{
|
2006-08-31 08:21:39 -07:00
|
|
|
moo_command_data_set_code (data, new_code);
|
2006-08-16 18:27:19 -07:00
|
|
|
changed = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (new_code);
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-23 00:38:16 -07:00
|
|
|
static gboolean
|
|
|
|
script_type_data_equal (G_GNUC_UNUSED MooCommandType *type,
|
|
|
|
MooCommandData *data1,
|
|
|
|
MooCommandData *data2)
|
|
|
|
{
|
2006-08-31 08:21:39 -07:00
|
|
|
const char *val1 = moo_command_data_get_code (data1);
|
|
|
|
const char *val2 = moo_command_data_get_code (data2);
|
2006-09-02 00:29:39 -07:00
|
|
|
return _moo_str_equal (val1, val2);
|
2006-08-23 00:38:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-16 22:08:49 -07:00
|
|
|
static void
|
|
|
|
_moo_command_type_script_init (G_GNUC_UNUSED MooCommandType *type)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_moo_command_type_script_class_init (MooCommandTypeClass *klass)
|
|
|
|
{
|
|
|
|
klass->create_command = script_type_create_command;
|
|
|
|
klass->create_widget = script_type_create_widget;
|
|
|
|
klass->load_data = script_type_load_data;
|
|
|
|
klass->save_data = script_type_save_data;
|
2006-08-23 00:38:16 -07:00
|
|
|
klass->data_equal = script_type_data_equal;
|
2006-08-16 22:08:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
static void
|
|
|
|
moo_command_script_class_init (MooCommandScriptClass *klass)
|
|
|
|
{
|
2006-08-16 22:08:49 -07:00
|
|
|
MooCommandType *type;
|
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
G_OBJECT_CLASS(klass)->dispose = moo_command_script_dispose;
|
|
|
|
MOO_COMMAND_CLASS(klass)->run = moo_command_script_run;
|
|
|
|
|
2006-08-18 23:15:42 -07:00
|
|
|
g_type_class_add_private (klass, sizeof (MooCommandScriptPrivate));
|
|
|
|
|
2006-08-16 22:08:49 -07:00
|
|
|
type = g_object_new (_moo_command_type_script_get_type (), NULL);
|
2006-08-31 08:21:39 -07:00
|
|
|
moo_command_type_register ("moo-script", _("MooScript"), type, NULL);
|
2006-08-16 22:08:49 -07:00
|
|
|
g_object_unref (type);
|
2006-08-15 22:18:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
moo_command_script_init (MooCommandScript *cmd)
|
|
|
|
{
|
2006-08-18 23:15:42 -07:00
|
|
|
cmd->priv = G_TYPE_INSTANCE_GET_PRIVATE (cmd,
|
|
|
|
MOO_TYPE_COMMAND_SCRIPT,
|
|
|
|
MooCommandScriptPrivate);
|
2006-08-15 22:18:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MooCommand *
|
2006-08-16 18:27:19 -07:00
|
|
|
moo_command_script_new (const char *script,
|
|
|
|
MooCommandOptions options)
|
2006-08-15 22:18:16 -07:00
|
|
|
{
|
|
|
|
MooCommandScript *cmd;
|
|
|
|
MSNode *node;
|
|
|
|
|
|
|
|
g_return_val_if_fail (script != NULL, NULL);
|
|
|
|
|
|
|
|
node = ms_script_parse (script);
|
|
|
|
g_return_val_if_fail (node != NULL, NULL);
|
|
|
|
|
2006-08-16 18:27:19 -07:00
|
|
|
cmd = g_object_new (MOO_TYPE_COMMAND_SCRIPT, "options", options, NULL);
|
2006-08-15 22:18:16 -07:00
|
|
|
cmd->priv->script = node;
|
|
|
|
|
|
|
|
return MOO_COMMAND (cmd);
|
|
|
|
}
|