2006-04-07 22:39:13 -07:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2006-04-19 14:45:56 -07:00
|
|
|
#define MOOEDIT_COMPILATION
|
2006-04-07 22:39:13 -07:00
|
|
|
#include "mooedit/mooedit-script.h"
|
2006-04-19 14:45:56 -07:00
|
|
|
#include "mooedit/mooedit-private.h"
|
2006-04-18 12:42:55 -07:00
|
|
|
#include <string.h>
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
#define MS_VAR_WINDOW "window"
|
|
|
|
#define MS_VAR_DOC "doc"
|
|
|
|
#define MS_VAR_FILE "file"
|
|
|
|
#define MS_VAR_NAME "name"
|
|
|
|
#define MS_VAR_BASE "base"
|
|
|
|
#define MS_VAR_DIR "dir"
|
|
|
|
#define MS_VAR_EXT "ext"
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
static void add_text_api (MSContext *ctx);
|
|
|
|
static void add_editor_api (MSContext *ctx);
|
|
|
|
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (MooEditScriptContext, moo_edit_script_context, MS_TYPE_CONTEXT)
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
enum {
|
|
|
|
PROP_0,
|
|
|
|
PROP_DOC
|
|
|
|
};
|
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
|
2006-04-07 22:39:13 -07:00
|
|
|
static void
|
2006-08-15 22:18:16 -07:00
|
|
|
moo_edit_script_context_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
2006-04-07 22:39:13 -07:00
|
|
|
{
|
2006-08-15 22:18:16 -07:00
|
|
|
MooEditScriptContext *ctx = MOO_EDIT_SCRIPT_CONTEXT (object);
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_DOC:
|
2006-08-15 22:18:16 -07:00
|
|
|
moo_edit_script_context_set_doc (ctx, g_value_get_object (value));
|
2006-04-07 22:39:13 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2006-08-15 22:18:16 -07:00
|
|
|
moo_edit_script_context_get_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
2006-04-07 22:39:13 -07:00
|
|
|
{
|
2006-08-15 22:18:16 -07:00
|
|
|
MooEditScriptContext *ctx = MOO_EDIT_SCRIPT_CONTEXT (object);
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
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
|
2006-08-15 22:18:16 -07:00
|
|
|
moo_edit_script_context_class_init (MooEditScriptContextClass *klass)
|
2006-04-07 22:39:13 -07:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
gobject_class->set_property = moo_edit_script_context_set_property;
|
|
|
|
gobject_class->get_property = moo_edit_script_context_get_property;
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_DOC,
|
2006-08-15 22:18:16 -07:00
|
|
|
g_param_spec_object ("doc", "doc", "doc",
|
|
|
|
GTK_TYPE_TEXT_VIEW,
|
|
|
|
G_PARAM_READWRITE));
|
2006-04-07 22:39:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2006-08-15 22:18:16 -07:00
|
|
|
moo_edit_script_context_init (MooEditScriptContext *ctx)
|
2006-04-07 22:39:13 -07:00
|
|
|
{
|
2006-08-15 22:18:16 -07:00
|
|
|
add_text_api (MS_CONTEXT (ctx));
|
|
|
|
add_editor_api (MS_CONTEXT (ctx));
|
2006-04-07 22:39:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-19 14:45:56 -07:00
|
|
|
static void
|
|
|
|
get_extension (const char *string,
|
|
|
|
char **base,
|
|
|
|
char **ext)
|
2006-04-18 12:42:55 -07:00
|
|
|
{
|
|
|
|
char *dot;
|
|
|
|
|
2006-04-19 14:45:56 -07:00
|
|
|
g_return_if_fail (string != NULL);
|
2006-04-18 12:42:55 -07:00
|
|
|
|
|
|
|
dot = strrchr (string, '.');
|
|
|
|
|
|
|
|
if (dot)
|
2006-04-19 14:45:56 -07:00
|
|
|
{
|
|
|
|
*base = g_strndup (string, dot - string);
|
|
|
|
*ext = g_strdup (dot);
|
|
|
|
}
|
2006-04-18 12:42:55 -07:00
|
|
|
else
|
2006-04-19 14:45:56 -07:00
|
|
|
{
|
|
|
|
*base = g_strdup (string);
|
|
|
|
*ext = g_strdup ("");
|
|
|
|
}
|
2006-04-18 12:42:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-07 22:39:13 -07:00
|
|
|
void
|
2006-08-15 22:18:16 -07:00
|
|
|
moo_edit_script_context_set_doc (MooEditScriptContext *ctx,
|
|
|
|
GtkTextView *doc)
|
2006-04-07 22:39:13 -07:00
|
|
|
{
|
|
|
|
MSValue *val;
|
2006-08-15 22:18:16 -07:00
|
|
|
GtkWidget *window = NULL;
|
2006-04-07 22:39:13 -07:00
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
g_return_if_fail (MOO_IS_EDIT_SCRIPT_CONTEXT (ctx));
|
|
|
|
g_return_if_fail (!doc || GTK_IS_TEXT_VIEW (doc));
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
if (doc)
|
2006-08-15 22:18:16 -07:00
|
|
|
{
|
|
|
|
window = gtk_widget_get_toplevel (GTK_WIDGET (doc));
|
|
|
|
if (!GTK_IS_WINDOW (window))
|
|
|
|
window = NULL;
|
|
|
|
}
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
g_object_set (ctx, "window", window, NULL);
|
|
|
|
|
2006-04-18 12:42:55 -07:00
|
|
|
val = ms_value_dict ();
|
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
if (MOO_IS_EDIT (doc))
|
2006-04-07 22:39:13 -07:00
|
|
|
{
|
2006-04-19 14:45:56 -07:00
|
|
|
char *dirname = NULL, *base = NULL, *ext = NULL;
|
2006-04-18 12:42:55 -07:00
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
if (moo_edit_get_basename (MOO_EDIT (doc)))
|
|
|
|
get_extension (moo_edit_get_basename (MOO_EDIT (doc)), &base, &ext);
|
2006-04-18 12:42:55 -07:00
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
if (moo_edit_get_filename (MOO_EDIT (doc)))
|
|
|
|
dirname = g_path_get_dirname (moo_edit_get_filename (MOO_EDIT (doc)));
|
2006-04-18 12:42:55 -07:00
|
|
|
|
|
|
|
ms_value_dict_set_string (val, MS_VAR_FILE,
|
2006-08-15 22:18:16 -07:00
|
|
|
moo_edit_get_filename (MOO_EDIT (doc)));
|
2006-04-18 12:42:55 -07:00
|
|
|
ms_value_dict_set_string (val, MS_VAR_NAME,
|
2006-08-15 22:18:16 -07:00
|
|
|
moo_edit_get_basename (MOO_EDIT (doc)));
|
2006-04-18 12:42:55 -07:00
|
|
|
ms_value_dict_set_string (val, MS_VAR_BASE, base);
|
|
|
|
ms_value_dict_set_string (val, MS_VAR_DIR, dirname);
|
2006-04-19 14:45:56 -07:00
|
|
|
ms_value_dict_set_string (val, MS_VAR_EXT, ext);
|
2006-04-18 12:42:55 -07:00
|
|
|
|
|
|
|
g_free (base);
|
2006-04-19 14:45:56 -07:00
|
|
|
g_free (ext);
|
2006-04-18 12:42:55 -07:00
|
|
|
g_free (dirname);
|
2006-04-07 22:39:13 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-04-18 12:42:55 -07:00
|
|
|
ms_value_dict_set_string (val, MS_VAR_FILE, NULL);
|
|
|
|
ms_value_dict_set_string (val, MS_VAR_NAME, NULL);
|
|
|
|
ms_value_dict_set_string (val, MS_VAR_BASE, NULL);
|
|
|
|
ms_value_dict_set_string (val, MS_VAR_DIR, NULL);
|
2006-04-19 14:45:56 -07:00
|
|
|
ms_value_dict_set_string (val, MS_VAR_EXT, NULL);
|
2006-04-07 22:39:13 -07:00
|
|
|
}
|
|
|
|
|
2006-04-18 12:42:55 -07:00
|
|
|
ms_context_assign_variable (MS_CONTEXT (ctx), MS_VAR_DOC, val);
|
2006-04-07 22:39:13 -07:00
|
|
|
ms_value_unref (val);
|
|
|
|
|
|
|
|
ctx->doc = doc;
|
|
|
|
g_object_notify (G_OBJECT (ctx), "doc");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MSContext *
|
2006-08-15 22:18:16 -07:00
|
|
|
moo_edit_script_context_new (GtkTextView *doc,
|
|
|
|
GtkWindow *window)
|
2006-04-19 11:05:09 -07:00
|
|
|
{
|
2006-08-15 22:18:16 -07:00
|
|
|
g_return_val_if_fail (!window || GTK_IS_WINDOW (window), NULL);
|
|
|
|
g_return_val_if_fail (!doc || GTK_IS_TEXT_VIEW (doc), NULL);
|
2006-04-18 12:42:55 -07:00
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
if (window)
|
|
|
|
return g_object_new (MOO_TYPE_EDIT_SCRIPT_CONTEXT,
|
|
|
|
"window", window,
|
|
|
|
"doc", doc,
|
|
|
|
NULL);
|
2006-04-18 12:42:55 -07:00
|
|
|
else
|
2006-08-15 22:18:16 -07:00
|
|
|
return g_object_new (MOO_TYPE_EDIT_SCRIPT_CONTEXT,
|
|
|
|
"doc", doc,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-17 22:24:35 -07:00
|
|
|
#if 0
|
2006-08-15 22:18:16 -07:00
|
|
|
// MSContext *
|
|
|
|
// moo_text_context_new (GtkTextView *doc)
|
|
|
|
// {
|
|
|
|
// GtkWidget *window;
|
|
|
|
// MSContext *ctx;
|
|
|
|
//
|
|
|
|
// g_return_val_if_fail (GTK_IS_TEXT_VIEW (doc), NULL);
|
|
|
|
//
|
|
|
|
// if (MOO_IS_EDIT (doc))
|
|
|
|
// return moo_edit_script_context_new (MOO_EDIT (doc), NULL);
|
|
|
|
//
|
|
|
|
// window = gtk_widget_get_toplevel (GTK_WIDGET (doc));
|
|
|
|
//
|
|
|
|
// ctx = g_object_new (MS_TYPE_CONTEXT,
|
|
|
|
// "window", GTK_IS_WINDOW (window) ? window : NULL,
|
|
|
|
// NULL);
|
|
|
|
//
|
|
|
|
// moo_edit_script_context_init_text_api (ctx);
|
|
|
|
//
|
|
|
|
// return ctx;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// static void
|
|
|
|
// moo_edit_set_shell_vars (MooCommand *cmd,
|
|
|
|
// MooEdit *doc,
|
|
|
|
// MooEditWindow *window)
|
|
|
|
// {
|
|
|
|
// if (doc)
|
|
|
|
// {
|
|
|
|
// char *dirname = NULL, *base = NULL, *ext = NULL;
|
|
|
|
//
|
|
|
|
// if (moo_edit_get_filename (doc))
|
|
|
|
// dirname = g_path_get_dirname (moo_edit_get_filename (doc));
|
|
|
|
//
|
|
|
|
// if (moo_edit_get_basename (doc))
|
|
|
|
// get_extension (moo_edit_get_basename (doc), &base, &ext);
|
|
|
|
//
|
|
|
|
// moo_command_set_shell_var (cmd, MS_VAR_FILE,
|
|
|
|
// moo_edit_get_filename (doc));
|
|
|
|
// moo_command_set_shell_var (cmd, MS_VAR_NAME,
|
|
|
|
// moo_edit_get_basename (doc));
|
|
|
|
// moo_command_set_shell_var (cmd, MS_VAR_BASE, base);
|
|
|
|
// moo_command_set_shell_var (cmd, MS_VAR_EXT, ext);
|
|
|
|
// moo_command_set_shell_var (cmd, MS_VAR_DIR, dirname);
|
|
|
|
//
|
|
|
|
// g_free (base);
|
|
|
|
// g_free (ext);
|
|
|
|
// g_free (dirname);
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// moo_command_set_shell_var (cmd, MS_VAR_FILE, NULL);
|
|
|
|
// moo_command_set_shell_var (cmd, MS_VAR_BASE, NULL);
|
|
|
|
// moo_command_set_shell_var (cmd, MS_VAR_EXT, NULL);
|
|
|
|
// moo_command_set_shell_var (cmd, MS_VAR_DIR, NULL);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// void
|
|
|
|
// moo_edit_setup_command (MooCommand *cmd,
|
|
|
|
// MooEdit *doc,
|
|
|
|
// MooEditWindow *window)
|
|
|
|
// {
|
|
|
|
// MSContext *ctx;
|
|
|
|
//
|
|
|
|
// g_return_if_fail (MOO_IS_COMMAND (cmd));
|
|
|
|
// g_return_if_fail (!window || MOO_IS_EDIT_WINDOW (window));
|
|
|
|
// g_return_if_fail (!doc || MOO_IS_EDIT (doc));
|
|
|
|
//
|
|
|
|
// if (!doc && window)
|
|
|
|
// doc = moo_edit_window_get_active_doc (window);
|
|
|
|
//
|
|
|
|
// ctx = moo_edit_script_context_new (doc, window);
|
|
|
|
//
|
|
|
|
// moo_command_set_context (cmd, ctx);
|
|
|
|
//
|
|
|
|
// if (ctx->py_dict)
|
|
|
|
// moo_command_set_py_dict (cmd, ctx->py_dict);
|
|
|
|
//
|
|
|
|
// g_object_unref (ctx);
|
|
|
|
//
|
|
|
|
// moo_edit_set_shell_vars (cmd, doc, window);
|
|
|
|
// }
|
2006-10-17 22:24:35 -07:00
|
|
|
#endif
|
2006-04-09 00:29:08 -07:00
|
|
|
|
|
|
|
|
2006-04-07 22:39:13 -07:00
|
|
|
/******************************************************************/
|
2006-04-19 14:45:56 -07:00
|
|
|
/* text API
|
2006-04-07 22:39:13 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
enum {
|
|
|
|
FUNC_BACKSPACE,
|
|
|
|
FUNC_DELETE,
|
|
|
|
|
|
|
|
FUNC_UP,
|
|
|
|
FUNC_DOWN,
|
|
|
|
FUNC_LEFT,
|
|
|
|
FUNC_RIGHT,
|
|
|
|
|
|
|
|
FUNC_INSERT,
|
2006-04-24 01:59:35 -07:00
|
|
|
FUNC_INSERT_PLACEHOLDER,
|
2006-04-07 22:39:13 -07:00
|
|
|
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",
|
2006-04-24 01:59:35 -07:00
|
|
|
"Insert", "InsertPlaceholder", "NewLine", "Select", "Selection",
|
2006-04-07 22:39:13 -07:00
|
|
|
"Cut", "Copy", "Paste"
|
|
|
|
};
|
|
|
|
|
|
|
|
static MSFunc *builtin_funcs[N_BUILTIN_FUNCS];
|
|
|
|
|
|
|
|
|
|
|
|
#define CHECK_DOC(ctx,doc) \
|
|
|
|
G_STMT_START { \
|
2006-08-15 22:18:16 -07:00
|
|
|
doc = MOO_EDIT_SCRIPT_CONTEXT(ctx)->doc; \
|
2006-04-07 22:39:13 -07:00
|
|
|
\
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-10 02:22:11 -07:00
|
|
|
static void
|
|
|
|
scroll_to_cursor (GtkTextView *view)
|
|
|
|
{
|
|
|
|
GtkTextBuffer *buffer = gtk_text_view_get_buffer (view);
|
|
|
|
GtkTextMark *insert = gtk_text_buffer_get_insert (buffer);
|
|
|
|
gtk_text_view_scroll_mark_onscreen (view, insert);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
insert_text (GtkTextView *view,
|
|
|
|
const char *text,
|
|
|
|
int len)
|
|
|
|
{
|
|
|
|
GtkTextBuffer *buffer = gtk_text_view_get_buffer (view);
|
|
|
|
gtk_text_buffer_insert_at_cursor (buffer, text, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
insert_and_scroll (GtkTextView *view,
|
|
|
|
const char *text,
|
|
|
|
int len)
|
|
|
|
{
|
|
|
|
insert_text (view, text, len);
|
|
|
|
scroll_to_cursor (view);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-07 22:39:13 -07:00
|
|
|
static MSValue *
|
|
|
|
cfunc_backspace (MSValue **args,
|
|
|
|
guint n_args,
|
|
|
|
MSContext *ctx)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
GtkTextIter start, end;
|
|
|
|
GtkTextBuffer *buffer;
|
2006-08-15 22:18:16 -07:00
|
|
|
GtkTextView *doc;
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
CHECK_DOC (ctx, doc);
|
|
|
|
|
|
|
|
if (!check_one_arg (args, n_args, ctx, TRUE, &n, 1))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!n)
|
|
|
|
return ms_value_none ();
|
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
buffer = gtk_text_view_get_buffer (doc);
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
scroll_to_cursor (doc);
|
2006-04-07 22:39:13 -07:00
|
|
|
return ms_value_none ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static MSValue *
|
|
|
|
cfunc_delete (MSValue **args,
|
|
|
|
guint n_args,
|
|
|
|
MSContext *ctx)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
GtkTextIter start, end;
|
|
|
|
GtkTextBuffer *buffer;
|
2006-08-15 22:18:16 -07:00
|
|
|
GtkTextView *doc;
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
CHECK_DOC (ctx, doc);
|
|
|
|
|
|
|
|
if (!check_one_arg (args, n_args, ctx, TRUE, &n, 1))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!n)
|
|
|
|
return ms_value_none ();
|
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
buffer = gtk_text_view_get_buffer (doc);
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
|
|
|
|
{
|
|
|
|
gtk_text_buffer_delete (buffer, &start, &end);
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n)
|
|
|
|
{
|
2006-04-24 11:44:25 -07:00
|
|
|
gtk_text_iter_forward_chars (&end, n);
|
2006-04-07 22:39:13 -07:00
|
|
|
gtk_text_buffer_delete (buffer, &start, &end);
|
|
|
|
}
|
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
scroll_to_cursor (doc);
|
2006-04-07 22:39:13 -07:00
|
|
|
return ms_value_none ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static MSValue *
|
|
|
|
cfunc_newline (MSValue **args,
|
|
|
|
guint n_args,
|
|
|
|
MSContext *ctx)
|
|
|
|
{
|
|
|
|
int n;
|
2006-08-15 22:18:16 -07:00
|
|
|
GtkTextView *doc;
|
2006-04-07 22:39:13 -07:00
|
|
|
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 ();
|
|
|
|
|
|
|
|
insert = "\n";
|
|
|
|
|
|
|
|
if (n > 1)
|
|
|
|
insert = freeme = g_strnfill (n, '\n');
|
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
insert_and_scroll (doc, insert, n);
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
g_free (freeme);
|
|
|
|
return ms_value_none ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2006-08-15 22:18:16 -07:00
|
|
|
get_cursor (GtkTextView *doc,
|
|
|
|
int *line,
|
|
|
|
int *col)
|
2006-04-07 22:39:13 -07:00
|
|
|
{
|
|
|
|
GtkTextBuffer *buffer;
|
|
|
|
GtkTextIter iter;
|
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
buffer = gtk_text_view_get_buffer (doc);
|
2006-04-07 22:39:13 -07:00
|
|
|
gtk_text_buffer_get_iter_at_mark (buffer, &iter,
|
|
|
|
gtk_text_buffer_get_insert (buffer));
|
|
|
|
|
|
|
|
*line = gtk_text_iter_get_line (&iter);
|
2006-04-22 06:42:02 -07:00
|
|
|
*col = moo_text_iter_get_visual_line_offset (&iter, 8);
|
2006-04-07 22:39:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static MSValue *
|
|
|
|
cfunc_up (MSValue **args,
|
|
|
|
guint n_args,
|
|
|
|
MSContext *ctx)
|
|
|
|
{
|
|
|
|
int line, col, n;
|
2006-08-15 22:18:16 -07:00
|
|
|
GtkTextView *doc;
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
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)
|
2006-08-15 22:18:16 -07:00
|
|
|
moo_text_view_move_cursor (doc, MAX (line - n, 0), col, TRUE, FALSE);
|
|
|
|
|
2006-04-10 02:22:11 -07:00
|
|
|
scroll_to_cursor (GTK_TEXT_VIEW (doc));
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
return ms_value_none ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static MSValue *
|
|
|
|
cfunc_down (MSValue **args,
|
|
|
|
guint n_args,
|
|
|
|
MSContext *ctx)
|
|
|
|
{
|
|
|
|
int line, col, n, line_count;
|
|
|
|
GtkTextBuffer *buffer;
|
2006-08-15 22:18:16 -07:00
|
|
|
GtkTextView *doc;
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
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);
|
2006-08-15 22:18:16 -07:00
|
|
|
buffer = gtk_text_view_get_buffer (doc);
|
2006-04-07 22:39:13 -07:00
|
|
|
line_count = gtk_text_buffer_get_line_count (buffer);
|
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
moo_text_view_move_cursor (doc, MIN (line + n, line_count - 1), col, TRUE, FALSE);
|
2006-04-10 02:22:11 -07:00
|
|
|
scroll_to_cursor (GTK_TEXT_VIEW (doc));
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
return ms_value_none ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static MSValue *
|
|
|
|
cfunc_select (MSValue *arg,
|
|
|
|
MSContext *ctx)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
GtkTextBuffer *buffer;
|
|
|
|
GtkTextIter start, end;
|
2006-08-15 22:18:16 -07:00
|
|
|
GtkTextView *doc;
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
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");
|
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
buffer = gtk_text_view_get_buffer (doc);
|
2006-04-07 22:39:13 -07:00
|
|
|
gtk_text_buffer_get_iter_at_mark (buffer, &start,
|
|
|
|
gtk_text_buffer_get_insert (buffer));
|
|
|
|
end = start;
|
2006-04-24 11:44:25 -07:00
|
|
|
gtk_text_iter_forward_chars (&end, n);
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
gtk_text_buffer_select_range (buffer, &end, &start);
|
2006-08-15 22:18:16 -07:00
|
|
|
scroll_to_cursor (doc);
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
return ms_value_none ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2006-08-15 22:18:16 -07:00
|
|
|
move_cursor (GtkTextView *doc,
|
|
|
|
int howmany)
|
2006-04-07 22:39:13 -07:00
|
|
|
{
|
|
|
|
GtkTextBuffer *buffer;
|
|
|
|
GtkTextIter iter;
|
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
buffer = gtk_text_view_get_buffer (doc);
|
2006-04-07 22:39:13 -07:00
|
|
|
gtk_text_buffer_get_iter_at_mark (buffer, &iter,
|
|
|
|
gtk_text_buffer_get_insert (buffer));
|
|
|
|
|
2006-04-24 11:44:25 -07:00
|
|
|
gtk_text_iter_forward_chars (&iter, howmany);
|
2006-04-07 22:39:13 -07:00
|
|
|
gtk_text_buffer_place_cursor (buffer, &iter);
|
2006-08-15 22:18:16 -07:00
|
|
|
scroll_to_cursor (doc);
|
2006-04-07 22:39:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static MSValue *
|
|
|
|
cfunc_left (MSValue **args,
|
|
|
|
guint n_args,
|
|
|
|
MSContext *ctx)
|
|
|
|
{
|
|
|
|
int n;
|
2006-08-15 22:18:16 -07:00
|
|
|
GtkTextView *doc;
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
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;
|
2006-08-15 22:18:16 -07:00
|
|
|
GtkTextView *doc;
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
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;
|
2006-08-15 22:18:16 -07:00
|
|
|
GtkTextView *doc;
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
CHECK_DOC (ctx, doc);
|
|
|
|
|
|
|
|
if (!n_args)
|
|
|
|
return ms_value_none ();
|
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
buffer = gtk_text_view_get_buffer (doc);
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
scroll_to_cursor (doc);
|
2006-04-07 22:39:13 -07:00
|
|
|
return ms_value_none ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-24 01:59:35 -07:00
|
|
|
static MSValue *
|
2006-04-24 23:42:01 -07:00
|
|
|
cfunc_insert_placeholder (MSValue **args,
|
|
|
|
guint n_args,
|
|
|
|
MSContext *ctx)
|
2006-04-24 01:59:35 -07:00
|
|
|
{
|
|
|
|
GtkTextIter start, end;
|
|
|
|
GtkTextBuffer *buffer;
|
2006-08-15 22:18:16 -07:00
|
|
|
GtkTextView *doc;
|
2006-04-24 23:42:01 -07:00
|
|
|
char *text = NULL;
|
2006-04-24 01:59:35 -07:00
|
|
|
|
|
|
|
CHECK_DOC (ctx, doc);
|
|
|
|
|
2006-04-24 23:42:01 -07:00
|
|
|
if (n_args > 1)
|
|
|
|
return ms_context_set_error (ctx, MS_ERROR_TYPE,
|
|
|
|
"number of args must be zero or one");
|
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
if (!MOO_IS_TEXT_VIEW (doc))
|
|
|
|
return ms_context_set_error (ctx, MS_ERROR_TYPE,
|
|
|
|
"document does not support placeholders");
|
|
|
|
|
2006-04-24 23:42:01 -07:00
|
|
|
if (n_args)
|
|
|
|
text = ms_value_print (args[0]);
|
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
buffer = gtk_text_view_get_buffer (doc);
|
2006-04-24 01:59:35 -07:00
|
|
|
|
|
|
|
if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
|
|
|
|
gtk_text_buffer_delete (buffer, &start, &end);
|
|
|
|
|
2006-04-24 23:42:01 -07:00
|
|
|
moo_text_view_insert_placeholder (MOO_TEXT_VIEW (doc), &start, text);
|
2006-08-15 22:18:16 -07:00
|
|
|
scroll_to_cursor (doc);
|
2006-04-24 23:42:01 -07:00
|
|
|
|
|
|
|
g_free (text);
|
2006-04-24 01:59:35 -07:00
|
|
|
return ms_value_none ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-07 22:39:13 -07:00
|
|
|
static MSValue *
|
|
|
|
cfunc_selection (MSContext *ctx)
|
|
|
|
{
|
2006-08-15 22:18:16 -07:00
|
|
|
GtkTextView *doc;
|
2006-04-07 22:39:13 -07:00
|
|
|
char *text;
|
|
|
|
|
|
|
|
CHECK_DOC (ctx, doc);
|
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
text = moo_text_view_get_selection (doc);
|
2006-04-07 22:39:13 -07:00
|
|
|
return ms_value_take_string (text);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define DEFINE_CLIP_FUNC(name,sig) \
|
|
|
|
static MSValue * \
|
|
|
|
name (MSContext *ctx) \
|
|
|
|
{ \
|
2006-08-15 22:18:16 -07:00
|
|
|
GtkTextView *doc; \
|
2006-04-07 22:39:13 -07:00
|
|
|
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")
|
|
|
|
|
2006-04-24 23:42:01 -07:00
|
|
|
#undef DEFINE_CLIP_FUNC
|
|
|
|
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
static void
|
2006-08-15 22:18:16 -07:00
|
|
|
init_text_api (void)
|
2006-04-07 22:39:13 -07:00
|
|
|
{
|
|
|
|
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);
|
2006-04-24 23:42:01 -07:00
|
|
|
builtin_funcs[FUNC_INSERT_PLACEHOLDER] = ms_cfunc_new_var (cfunc_insert_placeholder);
|
2006-04-07 22:39:13 -07:00
|
|
|
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
|
2006-08-15 22:18:16 -07:00
|
|
|
add_text_api (MSContext *ctx)
|
2006-04-07 22:39:13 -07:00
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
init_text_api ();
|
2006-04-07 22:39:13 -07:00
|
|
|
|
|
|
|
for (i = 0; i < N_BUILTIN_FUNCS; ++i)
|
|
|
|
ms_context_set_func (ctx, builtin_func_names[i],
|
|
|
|
builtin_funcs[i]);
|
|
|
|
}
|
2006-04-19 14:45:56 -07:00
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************/
|
|
|
|
/* editor API
|
|
|
|
*/
|
|
|
|
|
|
|
|
enum {
|
|
|
|
FUNC_OPEN,
|
|
|
|
N_BUILTIN_EDITOR_FUNCS
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static const char *builtin_editor_func_names[N_BUILTIN_EDITOR_FUNCS] = {
|
|
|
|
"Open"
|
|
|
|
};
|
|
|
|
|
|
|
|
static MSFunc *builtin_editor_funcs[N_BUILTIN_EDITOR_FUNCS];
|
|
|
|
|
|
|
|
|
|
|
|
static MSValue *
|
|
|
|
cfunc_open (MSValue *arg,
|
|
|
|
MSContext *ctx)
|
|
|
|
{
|
2006-08-15 22:18:16 -07:00
|
|
|
gpointer doc;
|
|
|
|
MooEdit *new_doc;
|
2006-04-19 14:45:56 -07:00
|
|
|
MooEditor *editor;
|
|
|
|
MooEditWindow *window;
|
|
|
|
char *filename;
|
|
|
|
|
2006-08-15 22:18:16 -07:00
|
|
|
g_return_val_if_fail (MOO_IS_EDIT_SCRIPT_CONTEXT (ctx), NULL);
|
|
|
|
|
|
|
|
doc = MOO_EDIT_SCRIPT_CONTEXT(ctx)->doc;
|
|
|
|
editor = MOO_IS_EDIT (doc) ? MOO_EDIT (doc)->priv->editor : moo_editor_instance ();
|
|
|
|
g_return_val_if_fail (MOO_IS_EDITOR (editor), NULL);
|
|
|
|
|
2006-04-19 14:45:56 -07:00
|
|
|
window = MOO_IS_EDIT_WINDOW (ctx->window) ? MOO_EDIT_WINDOW (ctx->window) : NULL;
|
|
|
|
filename = ms_value_print (arg);
|
|
|
|
|
2006-06-01 00:09:07 -07:00
|
|
|
new_doc = moo_editor_open_file (editor, window, ctx->window, filename, NULL);
|
2006-04-19 14:45:56 -07:00
|
|
|
|
|
|
|
g_free (filename);
|
2006-06-01 00:09:07 -07:00
|
|
|
return ms_value_bool (new_doc != NULL);
|
2006-04-19 14:45:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_editor_api (void)
|
|
|
|
{
|
|
|
|
if (builtin_editor_funcs[0])
|
|
|
|
return;
|
|
|
|
|
|
|
|
builtin_editor_funcs[FUNC_OPEN] = ms_cfunc_new_1 (cfunc_open);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2006-08-15 22:18:16 -07:00
|
|
|
add_editor_api (MSContext *ctx)
|
2006-04-19 14:45:56 -07:00
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
init_editor_api ();
|
|
|
|
|
|
|
|
for (i = 0; i < N_BUILTIN_EDITOR_FUNCS; ++i)
|
|
|
|
ms_context_set_func (ctx, builtin_editor_func_names[i],
|
|
|
|
builtin_editor_funcs[i]);
|
|
|
|
}
|