MooEditContext

master
Yevgen Muntyan 2006-04-08 00:39:13 -05:00
parent 816c169876
commit 15062558d7
17 changed files with 697 additions and 460 deletions

View File

@ -286,16 +286,16 @@
</kdevdoctreeview>
<kdevfilecreate>
<filetypes>
<type icon="source" ext="g" create="template" name="GAP source" >
<type icon="source" ext="g" name="GAP source" create="template" >
<descr>A new empty GAP source file</descr>
</type>
<type icon="source_cpp" ext="cpp" create="template" name="C++ Source" >
<type icon="source_cpp" ext="cpp" name="C++ Source" create="template" >
<descr>A new empty C++ file.</descr>
</type>
<type icon="source_h" ext="h" create="template" name="C/C++ Header" >
<type icon="source_h" ext="h" name="C/C++ Header" create="template" >
<descr>A new empty header file for C/C++.</descr>
</type>
<type icon="source_c" ext="c" create="template" name="C Source" >
<type icon="source_c" ext="c" name="C Source" create="template" >
<descr>A new empty C file.</descr>
</type>
</filetypes>

View File

@ -36,6 +36,7 @@
#include <mooedit/mootextstyle.h>
#include <mooedit/mootextstylescheme.h>
#include <mooedit/mootextview.h>
#include <mooedit/mooedit-script.h>
#include <mooterm/mooterm.h>
#include <mooterm/mootermwindow.h>

View File

@ -23,6 +23,7 @@
#include "mooedit/mooeditprefs.h"
#include "mooedit/mooeditor.h"
#include "mooedit/mooplugin.h"
#include "mooedit/mooedit-script.h"
#include "mooedit/plugins/mooeditplugins.h"
#include "mooutils/moopython.h"
#include "mooutils/moomarshals.h"
@ -1596,14 +1597,10 @@ static MSContext *
moo_app_get_context_real (G_GNUC_UNUSED MooApp *app,
MooWindow *window)
{
MSContext *ctx;
ctx = g_object_new (MS_TYPE_CONTEXT, "window", window, NULL);
if (MOO_IS_EDIT_WINDOW (window))
moo_edit_window_setup_context (MOO_EDIT_WINDOW (window), ctx);
return ctx;
return moo_edit_context_new (MOO_EDIT_WINDOW (window));
else
return ms_context_new (window);
}

View File

@ -15,6 +15,7 @@
#define __MOO_APP_H__
#include <mooedit/mooeditor.h>
#include <mooscript/mooscript-context.h>
G_BEGIN_DECLS

View File

@ -11,6 +11,7 @@ mooedit_include_headers = \
$(mooedit)/mooeditconfig.h \
$(mooedit)/mooeditor.h \
$(mooedit)/mooeditprefs.h \
$(mooedit)/mooedit-script.h \
$(mooedit)/mootextsearch.h \
$(mooedit)/mooeditwindow.h \
$(mooedit)/mooindenter.h \
@ -67,6 +68,7 @@ mooedit_sources = \
$(mooedit)/mooeditprefspage.c \
$(mooedit)/mootextsearch.c \
$(mooedit)/mooeditwindow.c \
$(mooedit)/mooedit-script.c \
$(mooedit)/moofold.c \
$(mooedit)/moohighlighter.c \
$(mooedit)/mooindenter.c \

View File

@ -0,0 +1,593 @@
/*
* mooedit-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.
*/
#include "mooedit/mooedit-script.h"
#define VAR_DOC "doc"
#define DOC_ATTR_FILE "file"
#define VAR_WINDOW "window"
static void moo_edit_context_init_api (MSContext *ctx);
G_DEFINE_TYPE (MooEditContext, moo_edit_context, MS_TYPE_CONTEXT)
enum {
PROP_0,
PROP_DOC
};
static void
moo_edit_context_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
MooEditContext *ctx = MOO_EDIT_CONTEXT (object);
switch (prop_id)
{
case PROP_DOC:
moo_edit_context_set_doc (ctx, g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
moo_edit_context_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
MooEditContext *ctx = MOO_EDIT_CONTEXT (object);
switch (prop_id)
{
case PROP_DOC:
g_value_set_object (value, ctx->doc);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
moo_edit_context_class_init (MooEditContextClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->set_property = moo_edit_context_set_property;
gobject_class->get_property = moo_edit_context_get_property;
g_object_class_install_property (gobject_class,
PROP_DOC,
g_param_spec_object ("doc",
"doc",
"doc",
MOO_TYPE_EDIT,
G_PARAM_READWRITE));
}
static void
moo_edit_context_init (MooEditContext *ctx)
{
moo_edit_context_init_api (MS_CONTEXT (ctx));
}
void
moo_edit_context_set_doc (MooEditContext *ctx,
MooEdit *doc)
{
MSValue *val;
GtkWidget *window = NULL;
g_return_if_fail (MOO_IS_EDIT_CONTEXT (ctx));
g_return_if_fail (!doc || MOO_IS_EDIT (doc));
if (doc)
{
GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (doc));
if (GTK_IS_WINDOW (toplevel))
window = toplevel;
}
g_object_set (ctx, "window", window, NULL);
if (doc)
{
val = ms_value_dict ();
ms_value_dict_set_string (val, DOC_ATTR_FILE,
moo_edit_get_filename (doc));
}
else
{
val = ms_value_none ();
}
ms_context_assign_variable (MS_CONTEXT (ctx), VAR_DOC, val);
ms_value_unref (val);
if (moo_python_running ())
{
ms_context_assign_py_object (MS_CONTEXT (ctx), VAR_DOC, doc);
ms_context_assign_py_object (MS_CONTEXT (ctx), VAR_WINDOW, window);
}
ctx->doc = doc;
g_object_notify (G_OBJECT (ctx), "doc");
}
MSContext *
moo_edit_context_new (MooEditWindow *window)
{
g_return_val_if_fail (MOO_IS_EDIT_WINDOW (window), NULL);
return g_object_new (MOO_TYPE_EDIT_CONTEXT,
"window", window,
"doc", moo_edit_window_get_active_doc (window),
NULL);
}
/******************************************************************/
/* API
*/
enum {
FUNC_BACKSPACE,
FUNC_DELETE,
FUNC_UP,
FUNC_DOWN,
FUNC_LEFT,
FUNC_RIGHT,
FUNC_INSERT,
FUNC_NEWLINE,
FUNC_SELECT,
FUNC_SELECTION,
FUNC_CUT,
FUNC_COPY,
FUNC_PASTE,
N_BUILTIN_FUNCS
};
static const char *builtin_func_names[N_BUILTIN_FUNCS] = {
"Backspace", "Delete", "Up", "Down", "Left", "Right",
"Insert", "NewLine", "Select", "Selection",
"Cut", "Copy", "Paste"
};
static MSFunc *builtin_funcs[N_BUILTIN_FUNCS];
#define CHECK_DOC(ctx,doc) \
G_STMT_START { \
doc = MOO_EDIT_CONTEXT(ctx)->doc; \
\
if (!doc) \
{ \
ms_context_set_error (MS_CONTEXT (ctx), \
MS_ERROR_TYPE, \
"Document not set"); \
return NULL; \
} \
} G_STMT_END
static gboolean
check_one_arg (MSValue **args,
guint n_args,
MSContext *ctx,
gboolean nonnegative,
int *dest,
int default_val)
{
int val;
if (n_args > 1)
{
ms_context_set_error (ctx, MS_ERROR_TYPE,
"number of args must be zero or one");
return FALSE;
}
if (!n_args)
{
*dest = default_val;
return TRUE;
}
if (!ms_value_get_int (args[0], &val))
{
ms_context_set_error (ctx, MS_ERROR_TYPE,
"argument must be integer");
return FALSE;
}
if (nonnegative && val < 0)
{
ms_context_set_error (ctx, MS_ERROR_VALUE,
"argument must be non-negative");
return FALSE;
}
*dest = val;
return TRUE;
}
static MSValue *
cfunc_backspace (MSValue **args,
guint n_args,
MSContext *ctx)
{
int n;
GtkTextIter start, end;
GtkTextBuffer *buffer;
MooEdit *doc;
CHECK_DOC (ctx, doc);
if (!check_one_arg (args, n_args, ctx, TRUE, &n, 1))
return NULL;
if (!n)
return ms_value_none ();
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (doc));
if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
{
gtk_text_buffer_delete (buffer, &start, &end);
n--;
}
if (n)
{
gtk_text_iter_backward_chars (&start, n);
gtk_text_buffer_delete (buffer, &start, &end);
}
return ms_value_none ();
}
static MSValue *
cfunc_delete (MSValue **args,
guint n_args,
MSContext *ctx)
{
int n;
GtkTextIter start, end;
GtkTextBuffer *buffer;
MooEdit *doc;
CHECK_DOC (ctx, doc);
if (!check_one_arg (args, n_args, ctx, TRUE, &n, 1))
return NULL;
if (!n)
return ms_value_none ();
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (doc));
if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
{
gtk_text_buffer_delete (buffer, &start, &end);
n--;
}
if (n)
{
gtk_text_iter_forward_chars (&end, n);
gtk_text_buffer_delete (buffer, &start, &end);
}
return ms_value_none ();
}
static MSValue *
cfunc_newline (MSValue **args,
guint n_args,
MSContext *ctx)
{
int n;
GtkTextBuffer *buffer;
MooEdit *doc;
char *freeme = NULL;
const char *insert;
CHECK_DOC (ctx, doc);
if (!check_one_arg (args, n_args, ctx, TRUE, &n, 1))
return NULL;
if (!n)
return ms_value_none ();
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (doc));
insert = "\n";
if (n > 1)
insert = freeme = g_strnfill (n, '\n');
gtk_text_buffer_insert_at_cursor (buffer, insert, n);
g_free (freeme);
return ms_value_none ();
}
static void
get_cursor (MooEdit *doc,
int *line,
int *col)
{
GtkTextBuffer *buffer;
GtkTextIter iter;
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (doc));
gtk_text_buffer_get_iter_at_mark (buffer, &iter,
gtk_text_buffer_get_insert (buffer));
*line = gtk_text_iter_get_line (&iter);
*col = gtk_text_iter_get_line_offset (&iter);
}
static MSValue *
cfunc_up (MSValue **args,
guint n_args,
MSContext *ctx)
{
int line, col, n;
MooEdit *doc;
CHECK_DOC (ctx, doc);
if (!check_one_arg (args, n_args, ctx, FALSE, &n, 1))
return NULL;
if (!n)
return ms_value_none ();
get_cursor (doc, &line, &col);
if (line > 0)
moo_text_view_move_cursor (MOO_TEXT_VIEW (doc),
MAX (line - n, 0), col,
FALSE);
return ms_value_none ();
}
static MSValue *
cfunc_down (MSValue **args,
guint n_args,
MSContext *ctx)
{
int line, col, n, line_count;
GtkTextBuffer *buffer;
MooEdit *doc;
CHECK_DOC (ctx, doc);
if (!check_one_arg (args, n_args, ctx, FALSE, &n, 1))
return NULL;
if (!n)
return ms_value_none ();
get_cursor (doc, &line, &col);
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (doc));
line_count = gtk_text_buffer_get_line_count (buffer);
moo_text_view_move_cursor (MOO_TEXT_VIEW (doc),
MIN (line + n, line_count - 1), col,
FALSE);
return ms_value_none ();
}
static MSValue *
cfunc_select (MSValue *arg,
MSContext *ctx)
{
int n;
GtkTextBuffer *buffer;
GtkTextIter start, end;
MooEdit *doc;
CHECK_DOC (ctx, doc);
if (!ms_value_get_int (arg, &n))
return ms_context_set_error (MS_CONTEXT (ctx), MS_ERROR_TYPE,
"argument must be integer");
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (doc));
gtk_text_buffer_get_iter_at_mark (buffer, &start,
gtk_text_buffer_get_insert (buffer));
end = start;
gtk_text_iter_forward_chars (&end, n);
gtk_text_buffer_select_range (buffer, &end, &start);
return ms_value_none ();
}
static void
move_cursor (MooEdit *doc,
int howmany)
{
GtkTextBuffer *buffer;
GtkTextIter iter;
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (doc));
gtk_text_buffer_get_iter_at_mark (buffer, &iter,
gtk_text_buffer_get_insert (buffer));
gtk_text_iter_forward_chars (&iter, howmany);
gtk_text_buffer_place_cursor (buffer, &iter);
}
static MSValue *
cfunc_left (MSValue **args,
guint n_args,
MSContext *ctx)
{
int n;
MooEdit *doc;
CHECK_DOC (ctx, doc);
if (!check_one_arg (args, n_args, ctx, FALSE, &n, 1))
return NULL;
move_cursor (doc, -n);
return ms_value_none ();
}
static MSValue *
cfunc_right (MSValue **args,
guint n_args,
MSContext *ctx)
{
int n;
MooEdit *doc;
CHECK_DOC (ctx, doc);
if (!check_one_arg (args, n_args, ctx, FALSE, &n, 1))
return NULL;
move_cursor (doc, n);
return ms_value_none ();
}
static MSValue *
cfunc_insert (MSValue **args,
guint n_args,
MSContext *ctx)
{
guint i;
GtkTextIter start, end;
GtkTextBuffer *buffer;
MooEdit *doc;
CHECK_DOC (ctx, doc);
if (!n_args)
return ms_value_none ();
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (doc));
if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
gtk_text_buffer_delete (buffer, &start, &end);
for (i = 0; i < n_args; ++i)
{
char *s = ms_value_print (args[i]);
gtk_text_buffer_insert (buffer, &start, s, -1);
g_free (s);
}
return ms_value_none ();
}
static MSValue *
cfunc_selection (MSContext *ctx)
{
MooEdit *doc;
char *text;
CHECK_DOC (ctx, doc);
text = moo_text_view_get_selection (MOO_TEXT_VIEW (doc));
return ms_value_take_string (text);
}
#define DEFINE_CLIP_FUNC(name,sig) \
static MSValue * \
name (MSContext *ctx) \
{ \
MooEdit *doc; \
CHECK_DOC (ctx, doc); \
g_signal_emit_by_name (doc, sig); \
return ms_value_none (); \
}
DEFINE_CLIP_FUNC(cfunc_cut, "cut-clipboard")
DEFINE_CLIP_FUNC(cfunc_copy, "copy-clipboard")
DEFINE_CLIP_FUNC(cfunc_paste, "paste-clipboard")
static void
init_api (void)
{
if (builtin_funcs[0])
return;
builtin_funcs[FUNC_BACKSPACE] = ms_cfunc_new_var (cfunc_backspace);
builtin_funcs[FUNC_DELETE] = ms_cfunc_new_var (cfunc_delete);
builtin_funcs[FUNC_INSERT] = ms_cfunc_new_var (cfunc_insert);
builtin_funcs[FUNC_UP] = ms_cfunc_new_var (cfunc_up);
builtin_funcs[FUNC_DOWN] = ms_cfunc_new_var (cfunc_down);
builtin_funcs[FUNC_LEFT] = ms_cfunc_new_var (cfunc_left);
builtin_funcs[FUNC_RIGHT] = ms_cfunc_new_var (cfunc_right);
builtin_funcs[FUNC_SELECT] = ms_cfunc_new_1 (cfunc_select);
builtin_funcs[FUNC_SELECTION] = ms_cfunc_new_0 (cfunc_selection);
builtin_funcs[FUNC_NEWLINE] = ms_cfunc_new_var (cfunc_newline);
builtin_funcs[FUNC_CUT] = ms_cfunc_new_0 (cfunc_cut);
builtin_funcs[FUNC_COPY] = ms_cfunc_new_0 (cfunc_copy);
builtin_funcs[FUNC_PASTE] = ms_cfunc_new_0 (cfunc_paste);
}
static void
moo_edit_context_init_api (MSContext *ctx)
{
guint i;
init_api ();
for (i = 0; i < N_BUILTIN_FUNCS; ++i)
ms_context_set_func (ctx, builtin_func_names[i],
builtin_funcs[i]);
}

View File

@ -0,0 +1,54 @@
/*
* mooedit-script.h
*
* 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.
*/
#ifndef __MOO_EDIT_SCRIPT_H__
#define __MOO_EDIT_SCRIPT_H__
#include <mooscript/mooscript-context.h>
#include <mooedit/mooeditor.h>
G_BEGIN_DECLS
#define MOO_TYPE_EDIT_CONTEXT (moo_edit_context_get_type ())
#define MOO_EDIT_CONTEXT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), MOO_TYPE_EDIT_CONTEXT, MooEditContext))
#define MOO_EDIT_CONTEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MOO_TYPE_EDIT_CONTEXT, MooEditContextClass))
#define MOO_IS_EDIT_CONTEXT(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), MOO_TYPE_EDIT_CONTEXT))
#define MOO_IS_EDIT_CONTEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MOO_TYPE_EDIT_CONTEXT))
#define MOO_EDIT_CONTEXT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MOO_TYPE_EDIT_CONTEXT, MooEditContextClass))
typedef struct _MooEditContext MooEditContext;
typedef struct _MooEditContextClass MooEditContextClass;
struct _MooEditContext {
MSContext context;
MooEdit *doc;
};
struct _MooEditContextClass {
MSContextClass context_class;
};
GType moo_edit_context_get_type (void) G_GNUC_CONST;
MSContext *moo_edit_context_new (MooEditWindow *window);
void moo_edit_context_set_doc (MooEditContext *ctx,
MooEdit *doc);
G_END_DECLS
#endif /* __MOO_EDIT_SCRIPT_H__ */

View File

@ -635,6 +635,14 @@ get_moo_buffer (MooEdit *edit)
}
MooEditor *
moo_edit_get_editor (MooEdit *doc)
{
g_return_val_if_fail (MOO_IS_EDIT (doc), NULL);
return doc->priv->editor;
}
static void
moo_edit_set_lang (MooEdit *edit,
MooLang *lang)

View File

@ -115,6 +115,7 @@ void moo_editor_set_ui_xml (MooEditor *editor,
MooUIXML *xml);
MooEditor *moo_edit_window_get_editor (MooEditWindow *window);
MooEditor *moo_edit_get_editor (MooEdit *doc);
MooLangMgr *moo_editor_get_lang_mgr (MooEditor *editor);
void moo_editor_set_default_lang(MooEditor *editor,

View File

@ -2592,53 +2592,3 @@ notebook_drag_data_recv (GtkWidget *widget,
out:
gtk_drag_finish (context, FALSE, FALSE, time);
}
/*****************************************************************************/
/* MooScript
*/
#define VAR_DOC "doc"
#define DOC_ATTR_FILE "file"
static void
moo_edit_window_setup_python (MooEditWindow *window,
MSContext *ctx)
{
MooEdit *doc;
doc = ACTIVE_DOC (window);
ms_context_assign_py_object (ctx, VAR_DOC, doc);
}
void
moo_edit_window_setup_context (MooEditWindow *window,
MSContext *ctx)
{
MooEdit *doc;
MSValue *val;
g_return_if_fail (MOO_IS_EDIT_WINDOW (window));
g_return_if_fail (MS_IS_CONTEXT (ctx));
g_return_if_fail (ctx->window == MOO_WINDOW (window));
doc = ACTIVE_DOC (window);
if (doc)
{
val = ms_value_dict ();
ms_value_dict_set_string (val, DOC_ATTR_FILE,
moo_edit_get_filename (doc));
}
else
{
val = ms_value_none ();
}
ms_context_assign_variable (ctx, VAR_DOC, val);
ms_value_unref (val);
if (moo_python_running ())
moo_edit_window_setup_python (window, ctx);
}

View File

@ -18,7 +18,6 @@
#include <mooedit/mooedit.h>
#include <mooutils/moowindow.h>
#include <mooutils/moobigpaned.h>
#include <mooscript/mooscript-context.h>
#include <gtk/gtkstatusbar.h>
G_BEGIN_DECLS
@ -102,9 +101,6 @@ guint moo_edit_window_push_message (MooEditWindow *window,
void moo_edit_window_pop_message (MooEditWindow *window,
const char *id);
void moo_edit_window_setup_context (MooEditWindow *window,
MSContext *ctx);
G_END_DECLS

View File

@ -13,36 +13,18 @@
*/
#include "as-plugin-script.h"
static void as_plugin_context_init_api (ASPluginContext *ctx);
G_DEFINE_TYPE (ASPluginContext, _as_plugin_context, MS_TYPE_CONTEXT)
static void
_as_plugin_context_init (ASPluginContext *ctx)
{
as_plugin_context_init_api (ctx);
}
static void
_as_plugin_context_class_init (G_GNUC_UNUSED ASPluginContextClass *klass)
{
}
#include "mooedit/mooedit-script.h"
MSContext *
_as_plugin_context_new (void)
{
return g_object_new (AS_TYPE_PLUGIN_CONTEXT, NULL);
return g_object_new (MOO_TYPE_EDIT_CONTEXT, NULL);
}
static void
as_plugin_context_setup (ASPluginContext *ctx,
as_plugin_context_setup (MSContext *ctx,
MooEdit *doc,
char *match,
char **parens,
@ -52,31 +34,30 @@ as_plugin_context_setup (ASPluginContext *ctx,
MSValue *val;
val = ms_value_string (match);
ms_context_assign_positional (MS_CONTEXT (ctx), 0, val);
ms_context_assign_positional (ctx, 0, val);
ms_value_unref (val);
for (i = 0; i < n_parens; ++i)
{
val = ms_value_string (parens[i]);
ms_context_assign_positional (MS_CONTEXT (ctx), i + 1, val);
ms_context_assign_positional (ctx, i + 1, val);
ms_value_unref (val);
}
ctx->doc = g_object_ref (doc);
moo_edit_context_set_doc (MOO_EDIT_CONTEXT (ctx), doc);
}
static void
as_plugin_context_clear (ASPluginContext *ctx,
guint n_parens)
as_plugin_context_clear (MSContext *ctx,
guint n_parens)
{
guint i;
for (i = 0; i < n_parens + 1; ++i)
ms_context_assign_positional (MS_CONTEXT (ctx), i, NULL);
ms_context_assign_positional (ctx, i, NULL);
g_object_ref (ctx->doc);
ctx->doc = NULL;
moo_edit_context_set_doc (MOO_EDIT_CONTEXT (ctx), NULL);
}
@ -92,15 +73,14 @@ _as_plugin_context_exec (MSContext *ctx,
MSValue *val;
gboolean success;
g_return_val_if_fail (AS_IS_PLUGIN_CONTEXT (ctx), FALSE);
g_return_val_if_fail (MOO_IS_EDIT_CONTEXT (ctx), FALSE);
g_return_val_if_fail (script != NULL, FALSE);
g_return_val_if_fail (MOO_IS_EDIT (doc), FALSE);
g_return_val_if_fail (insert != NULL, FALSE);
g_return_val_if_fail (match != NULL, FALSE);
g_return_val_if_fail (!n_parens || parens, FALSE);
as_plugin_context_setup (AS_PLUGIN_CONTEXT (ctx),
doc, match, parens, n_parens);
as_plugin_context_setup (ctx, doc, match, parens, n_parens);
val = ms_top_node_eval (script, ctx);
success = val != NULL;
@ -112,335 +92,7 @@ _as_plugin_context_exec (MSContext *ctx,
ms_context_clear_error (ctx);
}
as_plugin_context_clear (AS_PLUGIN_CONTEXT (ctx), n_parens);
as_plugin_context_clear (ctx, n_parens);
return success;
}
enum {
FUNC_BS,
FUNC_DEL,
FUNC_INS,
FUNC_UP,
FUNC_DOWN,
FUNC_LEFT,
FUNC_RIGHT,
FUNC_SEL,
N_BUILTIN_FUNCS
};
static const char *builtin_func_names[N_BUILTIN_FUNCS] = {
"Bs", "Del", "Ins", "Up", "Down", "Left", "Right", "Sel"
};
static MSFunc *builtin_funcs[N_BUILTIN_FUNCS];
static gboolean
check_one_arg (MSValue **args,
guint n_args,
ASPluginContext *ctx,
gboolean nonnegative,
int *dest,
int default_val)
{
int val;
if (n_args > 1)
{
ms_context_set_error (MS_CONTEXT (ctx), MS_ERROR_TYPE,
"number of args must be zero or one");
return FALSE;
}
if (!n_args)
{
*dest = default_val;
return TRUE;
}
if (!ms_value_get_int (args[0], &val))
{
ms_context_set_error (MS_CONTEXT (ctx), MS_ERROR_TYPE,
"argument must be integer");
return FALSE;
}
if (nonnegative && val < 0)
{
ms_context_set_error (MS_CONTEXT (ctx), MS_ERROR_VALUE,
"argument must be non-negative");
return FALSE;
}
*dest = val;
return TRUE;
}
static MSValue *
cfunc_bs (MSValue **args,
guint n_args,
ASPluginContext *ctx)
{
int n;
GtkTextIter start, end;
GtkTextBuffer *buffer;
if (!check_one_arg (args, n_args, ctx, TRUE, &n, 1))
return NULL;
if (!n)
return ms_value_none ();
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (ctx->doc));
if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
{
gtk_text_buffer_delete (buffer, &start, &end);
n--;
}
if (n)
{
gtk_text_iter_backward_chars (&start, n);
gtk_text_buffer_delete (buffer, &start, &end);
}
return ms_value_none ();
}
static MSValue *
cfunc_del (MSValue **args,
guint n_args,
ASPluginContext *ctx)
{
int n;
GtkTextIter start, end;
GtkTextBuffer *buffer;
if (!check_one_arg (args, n_args, ctx, TRUE, &n, 1))
return NULL;
if (!n)
return ms_value_none ();
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (ctx->doc));
if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
{
gtk_text_buffer_delete (buffer, &start, &end);
n--;
}
if (n)
{
gtk_text_iter_forward_chars (&end, n);
gtk_text_buffer_delete (buffer, &start, &end);
}
return ms_value_none ();
}
static void
get_cursor (MooEdit *doc,
int *line,
int *col)
{
GtkTextBuffer *buffer;
GtkTextIter iter;
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (doc));
gtk_text_buffer_get_iter_at_mark (buffer, &iter,
gtk_text_buffer_get_insert (buffer));
*line = gtk_text_iter_get_line (&iter);
*col = gtk_text_iter_get_line_offset (&iter);
}
static MSValue *
cfunc_up (MSValue **args,
guint n_args,
ASPluginContext *ctx)
{
int line, col, n;
if (!check_one_arg (args, n_args, ctx, FALSE, &n, 1))
return NULL;
if (!n)
return ms_value_none ();
get_cursor (ctx->doc, &line, &col);
if (line > 0)
moo_text_view_move_cursor (MOO_TEXT_VIEW (ctx->doc),
MAX (line - n, 0), col,
FALSE);
return ms_value_none ();
}
static MSValue *
cfunc_down (MSValue **args,
guint n_args,
ASPluginContext *ctx)
{
int line, col, n, line_count;
GtkTextBuffer *buffer;
if (!check_one_arg (args, n_args, ctx, FALSE, &n, 1))
return NULL;
if (!n)
return ms_value_none ();
get_cursor (ctx->doc, &line, &col);
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (ctx->doc));
line_count = gtk_text_buffer_get_line_count (buffer);
moo_text_view_move_cursor (MOO_TEXT_VIEW (ctx->doc),
MIN (line + n, line_count - 1), col,
FALSE);
return ms_value_none ();
}
static MSValue *
cfunc_sel (MSValue *arg,
ASPluginContext *ctx)
{
int n;
GtkTextBuffer *buffer;
GtkTextIter start, end;
if (!ms_value_get_int (arg, &n))
return ms_context_set_error (MS_CONTEXT (ctx), MS_ERROR_TYPE,
"argument must be integer");
if (!n)
return ms_context_set_error (MS_CONTEXT (ctx), MS_ERROR_TYPE,
"argument must be non zero integer");
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (ctx->doc));
gtk_text_buffer_get_iter_at_mark (buffer, &start,
gtk_text_buffer_get_insert (buffer));
end = start;
gtk_text_iter_forward_chars (&end, n);
gtk_text_buffer_select_range (buffer, &end, &start);
return ms_value_none ();
}
static void
move_cursor (MooEdit *doc,
int howmany)
{
GtkTextBuffer *buffer;
GtkTextIter iter;
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (doc));
gtk_text_buffer_get_iter_at_mark (buffer, &iter,
gtk_text_buffer_get_insert (buffer));
gtk_text_iter_forward_chars (&iter, howmany);
gtk_text_buffer_place_cursor (buffer, &iter);
}
static MSValue *
cfunc_left (MSValue **args,
guint n_args,
ASPluginContext *ctx)
{
int n;
if (!check_one_arg (args, n_args, ctx, FALSE, &n, 1))
return NULL;
move_cursor (ctx->doc, -n);
return ms_value_none ();
}
static MSValue *
cfunc_right (MSValue **args,
guint n_args,
ASPluginContext *ctx)
{
int n;
if (!check_one_arg (args, n_args, ctx, FALSE, &n, 1))
return NULL;
move_cursor (ctx->doc, n);
return ms_value_none ();
}
static MSValue *
cfunc_ins (MSValue **args,
guint n_args,
ASPluginContext *ctx)
{
guint i;
GtkTextIter start, end;
GtkTextBuffer *buffer;
if (!n_args)
return ms_value_none ();
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (ctx->doc));
if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
gtk_text_buffer_delete (buffer, &start, &end);
for (i = 0; i < n_args; ++i)
{
char *s = ms_value_print (args[i]);
gtk_text_buffer_insert (buffer, &start, s, -1);
g_free (s);
}
return ms_value_none ();
}
static void
init_api (void)
{
if (builtin_funcs[0])
return;
builtin_funcs[FUNC_BS] = ms_cfunc_new_var ((MSCFunc_Var) cfunc_bs);
builtin_funcs[FUNC_DEL] = ms_cfunc_new_var ((MSCFunc_Var) cfunc_del);
builtin_funcs[FUNC_INS] = ms_cfunc_new_var ((MSCFunc_Var) cfunc_ins);
builtin_funcs[FUNC_UP] = ms_cfunc_new_var ((MSCFunc_Var) cfunc_up);
builtin_funcs[FUNC_DOWN] = ms_cfunc_new_var ((MSCFunc_Var) cfunc_down);
builtin_funcs[FUNC_LEFT] = ms_cfunc_new_var ((MSCFunc_Var) cfunc_left);
builtin_funcs[FUNC_RIGHT] = ms_cfunc_new_var ((MSCFunc_Var) cfunc_right);
builtin_funcs[FUNC_SEL] = ms_cfunc_new_1 ((MSCFunc_1) cfunc_sel);
}
static void
as_plugin_context_init_api (ASPluginContext *ctx)
{
guint i;
init_api ();
for (i = 0; i < N_BUILTIN_FUNCS; ++i)
ms_context_set_func (MS_CONTEXT (ctx),
builtin_func_names[i],
builtin_funcs[i]);
}

View File

@ -22,31 +22,7 @@
G_BEGIN_DECLS
#define AS_TYPE_PLUGIN_CONTEXT (_as_plugin_context_get_type ())
#define AS_PLUGIN_CONTEXT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), AS_TYPE_PLUGIN_CONTEXT, ASPluginContext))
#define AS_PLUGIN_CONTEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), AS_TYPE_PLUGIN_CONTEXT, ASPluginContextClass))
#define AS_IS_PLUGIN_CONTEXT(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), AS_TYPE_PLUGIN_CONTEXT))
#define AS_IS_PLUGIN_CONTEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), AS_TYPE_PLUGIN_CONTEXT))
#define AS_PLUGIN_CONTEXT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), AS_TYPE_PLUGIN_CONTEXT, ASPluginContextClass))
typedef struct _ASPluginContext ASPluginContext;
typedef struct _ASPluginContextClass ASPluginContextClass;
struct _ASPluginContext {
MSContext context;
MooEdit *doc;
};
struct _ASPluginContextClass {
MSContextClass context_class;
};
GType _as_plugin_context_get_type (void) G_GNUC_CONST;
MSContext *_as_plugin_context_new (void);
gboolean _as_plugin_context_exec (MSContext *ctx,
MSNode *script,
MooEdit *doc,

View File

@ -102,11 +102,7 @@ static MooPyObject *
py_object_from_gobject (gpointer gobj)
{
g_return_val_if_fail (!gobj || G_IS_OBJECT (gobj), NULL);
if (gobj)
return (MooPyObject*) pygobject_new (gobj);
else
return (MooPyObject*) Py_INCREF (Py_None);
return (MooPyObject*) pygobject_new (gobj);
}

View File

@ -231,6 +231,12 @@
;; From mooedit.h
(define-method get_editor
(of-object "MooEdit")
(c-name "moo_edit_get_editor")
(return-type "MooEditor*")
)
(define-method get_filename
(of-object "MooEdit")
(c-name "moo_edit_get_filename")

View File

@ -187,9 +187,10 @@ ms_context_class_init (MSContextClass *klass)
MSContext *
ms_context_new (void)
ms_context_new (gpointer window)
{
MSContext *ctx = g_object_new (MS_TYPE_CONTEXT, NULL);
MSContext *ctx = g_object_new (MS_TYPE_CONTEXT,
"window", window, NULL);
return ctx;
}
@ -546,8 +547,11 @@ ms_context_assign_py_var (MSContext *ctx,
g_return_if_fail (MS_IS_CONTEXT (ctx));
g_return_if_fail (var != NULL);
g_return_if_fail (obj != NULL);
g_return_if_fail (ctx->py_dict != NULL);
g_return_if_fail (moo_python_running ());
if (!ctx->py_dict)
ctx->py_dict = moo_py_get_script_dict ("__moo_script__");
moo_py_dict_set_item (ctx->py_dict, var, obj);
}
@ -562,7 +566,7 @@ ms_context_assign_py_object (MSContext *ctx,
g_return_if_fail (moo_python_running ());
g_return_if_fail (MS_IS_CONTEXT (ctx));
g_return_if_fail (var != NULL);
g_return_if_fail (G_IS_OBJECT (gobj));
g_return_if_fail (!gobj || G_IS_OBJECT (gobj));
pyobj = moo_py_object_from_gobject (gobj);
g_return_if_fail (pyobj != NULL);

View File

@ -81,7 +81,7 @@ MSVariable *ms_variable_new_func (MSFunc *func);
MSVariable *ms_variable_ref (MSVariable *var);
void ms_variable_unref (MSVariable *var);
MSContext *ms_context_new (void);
MSContext *ms_context_new (gpointer window);
void _ms_context_add_builtin (MSContext *ctx);
MSValue *ms_context_eval_variable (MSContext *ctx,