/* vim-emulation.c generated by valac 0.56.19, the Vala compiler
 * generated from vim-emulation.vala, do not modify */

/* -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-*/

#include "vim-emulation.h"
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gee.h>
#include <glib-object.h>
#include "codecore.h"
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gtksourceview/gtksource.h>
#include <float.h>
#include <math.h>
#include <libpeas.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

enum  {
	SCRATCH_PLUGINS_VIM_EMULATION_0_PROPERTY,
	SCRATCH_PLUGINS_VIM_EMULATION_OBJECT_PROPERTY,
	SCRATCH_PLUGINS_VIM_EMULATION_NUM_PROPERTIES
};
static GParamSpec* scratch_plugins_vim_emulation_properties[SCRATCH_PLUGINS_VIM_EMULATION_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

struct _ScratchPluginsVimEmulationPrivate {
	ScratchPluginsVimEmulationMode mode;
	gchar* number;
	gchar* action;
	gboolean g;
	GeeTreeSet* views;
	ScratchWidgetsSourceView* view;
	ScratchServicesInterface* plugins;
	GObject* _object;
};

static gint ScratchPluginsVimEmulation_private_offset;
static gpointer scratch_plugins_vim_emulation_parent_class = NULL;
static ScratchServicesActivatablePluginIface * scratch_plugins_vim_emulation_scratch_services_activatable_plugin_parent_iface = NULL;
static GType scratch_plugins_vim_emulation_type_id = 0;

static void scratch_plugins_vim_emulation_real_update_state (ScratchServicesActivatablePlugin* base);
static void scratch_plugins_vim_emulation_real_activate (ScratchServicesActivatablePlugin* base);
static void __lambda4_ (ScratchPluginsVimEmulation* self,
                 ScratchServicesDocument* doc);
static gboolean scratch_plugins_vim_emulation_handle_key_press (ScratchPluginsVimEmulation* self,
                                                         GdkEventKey* event);
static gboolean _scratch_plugins_vim_emulation_handle_key_press_gtk_widget_key_press_event (GtkWidget* _sender,
                                                                                     GdkEventKey* event,
                                                                                     gpointer self);
static void ___lambda4__scratch_services_interface_hook_document (ScratchServicesInterface* _sender,
                                                           ScratchServicesDocument* doc,
                                                           gpointer self);
static void scratch_plugins_vim_emulation_real_deactivate (ScratchServicesActivatablePlugin* base);
static void scratch_plugins_vim_emulation_move_paragraph (ScratchPluginsVimEmulation* self,
                                                   gboolean up,
                                                   gboolean select);
static GObject * scratch_plugins_vim_emulation_constructor (GType type,
                                                     guint n_construct_properties,
                                                     GObjectConstructParam * construct_properties);
static void scratch_plugins_vim_emulation_finalize (GObject * obj);
static void _vala_scratch_plugins_vim_emulation_get_property (GObject * object,
                                                       guint property_id,
                                                       GValue * value,
                                                       GParamSpec * pspec);
static void _vala_scratch_plugins_vim_emulation_set_property (GObject * object,
                                                       guint property_id,
                                                       const GValue * value,
                                                       GParamSpec * pspec);

static inline gpointer
scratch_plugins_vim_emulation_get_instance_private (ScratchPluginsVimEmulation* self)
{
	return G_STRUCT_MEMBER_P (self, ScratchPluginsVimEmulation_private_offset);
}

 G_GNUC_NO_INLINE static GType
scratch_plugins_vim_emulation_mode_get_type_once (void)
{
	static const GEnumValue values[] = {{SCRATCH_PLUGINS_VIM_EMULATION_MODE_COMMAND, "SCRATCH_PLUGINS_VIM_EMULATION_MODE_COMMAND", "command"}, {SCRATCH_PLUGINS_VIM_EMULATION_MODE_INSERT, "SCRATCH_PLUGINS_VIM_EMULATION_MODE_INSERT", "insert"}, {SCRATCH_PLUGINS_VIM_EMULATION_MODE_VISUAL, "SCRATCH_PLUGINS_VIM_EMULATION_MODE_VISUAL", "visual"}, {0, NULL, NULL}};
	GType scratch_plugins_vim_emulation_mode_type_id;
	scratch_plugins_vim_emulation_mode_type_id = g_enum_register_static ("ScratchPluginsVimEmulationMode", values);
	return scratch_plugins_vim_emulation_mode_type_id;
}

GType
scratch_plugins_vim_emulation_mode_get_type (void)
{
	static gsize scratch_plugins_vim_emulation_mode_type_id__once = 0;
	if (g_once_init_enter (&scratch_plugins_vim_emulation_mode_type_id__once)) {
		GType scratch_plugins_vim_emulation_mode_type_id;
		scratch_plugins_vim_emulation_mode_type_id = scratch_plugins_vim_emulation_mode_get_type_once ();
		g_once_init_leave (&scratch_plugins_vim_emulation_mode_type_id__once, scratch_plugins_vim_emulation_mode_type_id);
	}
	return scratch_plugins_vim_emulation_mode_type_id__once;
}

static void
scratch_plugins_vim_emulation_real_update_state (ScratchServicesActivatablePlugin* base)
{
	ScratchPluginsVimEmulation * self;
	self = (ScratchPluginsVimEmulation*) base;
}

static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

static gboolean
_scratch_plugins_vim_emulation_handle_key_press_gtk_widget_key_press_event (GtkWidget* _sender,
                                                                            GdkEventKey* event,
                                                                            gpointer self)
{
	gboolean result;
	result = scratch_plugins_vim_emulation_handle_key_press ((ScratchPluginsVimEmulation*) self, event);
	return result;
}

static void
__lambda4_ (ScratchPluginsVimEmulation* self,
            ScratchServicesDocument* doc)
{
	ScratchWidgetsSourceView* _tmp0_;
	ScratchWidgetsSourceView* _tmp1_;
	ScratchWidgetsSourceView* _tmp2_;
	ScratchWidgetsSourceView* _tmp3_;
	guint _tmp4_;
	ScratchWidgetsSourceView* _tmp5_;
	GeeTreeSet* _tmp6_;
	ScratchWidgetsSourceView* _tmp7_;
	g_return_if_fail (doc != NULL);
	_tmp0_ = scratch_services_document_get_source_view (doc);
	_tmp1_ = _tmp0_;
	_tmp2_ = _g_object_ref0 (_tmp1_);
	_g_object_unref0 (self->priv->view);
	self->priv->view = _tmp2_;
	_tmp3_ = self->priv->view;
	g_signal_parse_name ("key-press-event", gtk_widget_get_type (), &_tmp4_, NULL, FALSE);
	g_signal_handlers_disconnect_matched ((GtkWidget*) _tmp3_, G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA, _tmp4_, 0, NULL, (GCallback) _scratch_plugins_vim_emulation_handle_key_press_gtk_widget_key_press_event, self);
	_tmp5_ = self->priv->view;
	g_signal_connect_object ((GtkWidget*) _tmp5_, "key-press-event", (GCallback) _scratch_plugins_vim_emulation_handle_key_press_gtk_widget_key_press_event, self, 0);
	_tmp6_ = self->priv->views;
	_tmp7_ = self->priv->view;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp6_, _tmp7_);
}

static void
___lambda4__scratch_services_interface_hook_document (ScratchServicesInterface* _sender,
                                                      ScratchServicesDocument* doc,
                                                      gpointer self)
{
	__lambda4_ ((ScratchPluginsVimEmulation*) self, doc);
}

static void
scratch_plugins_vim_emulation_real_activate (ScratchServicesActivatablePlugin* base)
{
	ScratchPluginsVimEmulation * self;
	GObject* _tmp0_;
	GObject* _tmp1_;
	ScratchServicesInterface* _tmp2_;
	self = (ScratchPluginsVimEmulation*) base;
	_tmp0_ = scratch_services_activatable_plugin_get_object ((ScratchServicesActivatablePlugin*) self);
	_tmp1_ = _tmp0_;
	_g_object_unref0 (self->priv->plugins);
	self->priv->plugins = G_TYPE_CHECK_INSTANCE_CAST (_tmp1_, SCRATCH_SERVICES_TYPE_INTERFACE, ScratchServicesInterface);
	_tmp2_ = self->priv->plugins;
	g_signal_connect_object (_tmp2_, "hook-document", (GCallback) ___lambda4__scratch_services_interface_hook_document, self, 0);
}

static void
scratch_plugins_vim_emulation_real_deactivate (ScratchServicesActivatablePlugin* base)
{
	ScratchPluginsVimEmulation * self;
	self = (ScratchPluginsVimEmulation*) base;
	{
		GeeIterator* _v_it = NULL;
		GeeTreeSet* _tmp0_;
		GeeIterator* _tmp1_;
		_tmp0_ = self->priv->views;
		_tmp1_ = gee_abstract_collection_iterator ((GeeAbstractCollection*) _tmp0_);
		_v_it = _tmp1_;
		while (TRUE) {
			GeeIterator* _tmp2_;
			ScratchWidgetsSourceView* v = NULL;
			GeeIterator* _tmp3_;
			gpointer _tmp4_;
			ScratchWidgetsSourceView* _tmp5_;
			guint _tmp6_;
			_tmp2_ = _v_it;
			if (!gee_iterator_next (_tmp2_)) {
				break;
			}
			_tmp3_ = _v_it;
			_tmp4_ = gee_iterator_get (_tmp3_);
			v = (ScratchWidgetsSourceView*) _tmp4_;
			_tmp5_ = v;
			g_signal_parse_name ("key-press-event", gtk_widget_get_type (), &_tmp6_, NULL, FALSE);
			g_signal_handlers_disconnect_matched ((GtkWidget*) _tmp5_, G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA, _tmp6_, 0, NULL, (GCallback) _scratch_plugins_vim_emulation_handle_key_press_gtk_widget_key_press_event, self);
			_g_object_unref0 (v);
		}
		_g_object_unref0 (_v_it);
	}
}

static gboolean
scratch_plugins_vim_emulation_handle_key_press (ScratchPluginsVimEmulation* self,
                                                GdkEventKey* event)
{
	gboolean ctrl = FALSE;
	gboolean shift = FALSE;
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_ = FALSE;
	gint old_len = 0;
	const gchar* _tmp2_;
	gint _tmp3_;
	gint _tmp4_;
	const gchar* _tmp76_;
	gint _tmp77_;
	gint _tmp78_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (event != NULL, FALSE);
	ctrl = (event->state & GDK_CONTROL_MASK) != 0;
	shift = (event->state & GDK_SHIFT_MASK) != 0;
	if (ctrl) {
		_tmp0_ = event->keyval == GDK_KEY_Up;
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		scratch_plugins_vim_emulation_move_paragraph (self, TRUE, shift);
		result = TRUE;
		return result;
	}
	if (ctrl) {
		_tmp1_ = event->keyval == GDK_KEY_Down;
	} else {
		_tmp1_ = FALSE;
	}
	if (_tmp1_) {
		scratch_plugins_vim_emulation_move_paragraph (self, FALSE, shift);
		result = TRUE;
		return result;
	}
	_tmp2_ = self->priv->number;
	_tmp3_ = strlen (_tmp2_);
	_tmp4_ = _tmp3_;
	old_len = _tmp4_;
	switch (event->keyval) {
		case GDK_KEY_i:
		{
			if (self->priv->mode == SCRATCH_PLUGINS_VIM_EMULATION_MODE_INSERT) {
				result = FALSE;
				return result;
			} else {
				gchar* _tmp5_;
				_tmp5_ = g_strdup ("");
				_g_free0 (self->priv->action);
				self->priv->action = _tmp5_;
			}
			self->priv->mode = SCRATCH_PLUGINS_VIM_EMULATION_MODE_INSERT;
			g_debug ("vim-emulation.vala:91: Vim Emulation: INSERT Mode!");
			result = TRUE;
			return result;
		}
		case GDK_KEY_Escape:
		{
			self->priv->mode = SCRATCH_PLUGINS_VIM_EMULATION_MODE_COMMAND;
			g_debug ("vim-emulation.vala:95: Vim Emulation: COMMAND Mode!");
			break;
		}
		default:
		break;
	}
	if (self->priv->mode == SCRATCH_PLUGINS_VIM_EMULATION_MODE_INSERT) {
		const gchar* _tmp6_;
		const gchar* _tmp7_;
		gchar* _tmp8_;
		_tmp6_ = self->priv->action;
		_tmp7_ = event->string;
		_tmp8_ = g_strconcat (_tmp6_, _tmp7_, NULL);
		_g_free0 (self->priv->action);
		self->priv->action = _tmp8_;
		result = FALSE;
		return result;
	}
	switch (event->keyval) {
		case GDK_KEY_1:
		{
			const gchar* _tmp9_;
			gchar* _tmp10_;
			_tmp9_ = self->priv->number;
			_tmp10_ = g_strconcat (_tmp9_, "1", NULL);
			_g_free0 (self->priv->number);
			self->priv->number = _tmp10_;
			break;
		}
		case GDK_KEY_2:
		{
			const gchar* _tmp11_;
			gchar* _tmp12_;
			_tmp11_ = self->priv->number;
			_tmp12_ = g_strconcat (_tmp11_, "2", NULL);
			_g_free0 (self->priv->number);
			self->priv->number = _tmp12_;
			break;
		}
		case GDK_KEY_3:
		{
			const gchar* _tmp13_;
			gchar* _tmp14_;
			_tmp13_ = self->priv->number;
			_tmp14_ = g_strconcat (_tmp13_, "3", NULL);
			_g_free0 (self->priv->number);
			self->priv->number = _tmp14_;
			break;
		}
		case GDK_KEY_4:
		{
			const gchar* _tmp15_;
			gchar* _tmp16_;
			_tmp15_ = self->priv->number;
			_tmp16_ = g_strconcat (_tmp15_, "4", NULL);
			_g_free0 (self->priv->number);
			self->priv->number = _tmp16_;
			break;
		}
		case GDK_KEY_5:
		{
			const gchar* _tmp17_;
			gchar* _tmp18_;
			_tmp17_ = self->priv->number;
			_tmp18_ = g_strconcat (_tmp17_, "5", NULL);
			_g_free0 (self->priv->number);
			self->priv->number = _tmp18_;
			break;
		}
		case GDK_KEY_6:
		{
			const gchar* _tmp19_;
			gchar* _tmp20_;
			_tmp19_ = self->priv->number;
			_tmp20_ = g_strconcat (_tmp19_, "6", NULL);
			_g_free0 (self->priv->number);
			self->priv->number = _tmp20_;
			break;
		}
		case GDK_KEY_7:
		{
			const gchar* _tmp21_;
			gchar* _tmp22_;
			_tmp21_ = self->priv->number;
			_tmp22_ = g_strconcat (_tmp21_, "7", NULL);
			_g_free0 (self->priv->number);
			self->priv->number = _tmp22_;
			break;
		}
		case GDK_KEY_8:
		{
			const gchar* _tmp23_;
			gchar* _tmp24_;
			_tmp23_ = self->priv->number;
			_tmp24_ = g_strconcat (_tmp23_, "8", NULL);
			_g_free0 (self->priv->number);
			self->priv->number = _tmp24_;
			break;
		}
		case GDK_KEY_9:
		{
			const gchar* _tmp25_;
			gchar* _tmp26_;
			_tmp25_ = self->priv->number;
			_tmp26_ = g_strconcat (_tmp25_, "9", NULL);
			_g_free0 (self->priv->number);
			self->priv->number = _tmp26_;
			break;
		}
		case GDK_KEY_Left:
		case GDK_KEY_h:
		{
			ScratchWidgetsSourceView* _tmp27_;
			_tmp27_ = self->priv->view;
			g_signal_emit_by_name ((GtkTextView*) _tmp27_, "move-cursor", GTK_MOVEMENT_VISUAL_POSITIONS, -1, FALSE);
			break;
		}
		case GDK_KEY_Down:
		case GDK_KEY_j:
		case GDK_KEY_plus:
		{
			ScratchWidgetsSourceView* _tmp28_;
			_tmp28_ = self->priv->view;
			g_signal_emit_by_name ((GtkTextView*) _tmp28_, "move-cursor", GTK_MOVEMENT_DISPLAY_LINES, 1, FALSE);
			break;
		}
		case GDK_KEY_Up:
		case GDK_KEY_k:
		case GDK_KEY_minus:
		{
			ScratchWidgetsSourceView* _tmp29_;
			_tmp29_ = self->priv->view;
			g_signal_emit_by_name ((GtkTextView*) _tmp29_, "move-cursor", GTK_MOVEMENT_DISPLAY_LINES, -1, FALSE);
			break;
		}
		case GDK_KEY_Right:
		case GDK_KEY_l:
		{
			ScratchWidgetsSourceView* _tmp30_;
			_tmp30_ = self->priv->view;
			g_signal_emit_by_name ((GtkTextView*) _tmp30_, "move-cursor", GTK_MOVEMENT_VISUAL_POSITIONS, 1, FALSE);
			break;
		}
		case GDK_KEY_End:
		case GDK_KEY_dollar:
		{
			ScratchWidgetsSourceView* _tmp31_;
			_tmp31_ = self->priv->view;
			g_signal_emit_by_name ((GtkTextView*) _tmp31_, "move-cursor", GTK_MOVEMENT_PARAGRAPH_ENDS, 1, FALSE);
			break;
		}
		case GDK_KEY_u:
		{
			ScratchWidgetsSourceView* _tmp32_;
			_tmp32_ = self->priv->view;
			g_signal_emit_by_name ((GtkSourceView*) _tmp32_, "undo");
			break;
		}
		case GDK_KEY_H:
		{
			ScratchWidgetsSourceView* _tmp33_;
			_tmp33_ = self->priv->view;
			g_signal_emit_by_name ((GtkTextView*) _tmp33_, "move-cursor", GTK_MOVEMENT_BUFFER_ENDS, -1, FALSE);
			break;
		}
		case GDK_KEY_L:
		{
			ScratchWidgetsSourceView* _tmp34_;
			_tmp34_ = self->priv->view;
			g_signal_emit_by_name ((GtkTextView*) _tmp34_, "move-cursor", GTK_MOVEMENT_BUFFER_ENDS, 1, FALSE);
			break;
		}
		case GDK_KEY_w:
		{
			ScratchWidgetsSourceView* _tmp35_;
			_tmp35_ = self->priv->view;
			g_signal_emit_by_name ((GtkTextView*) _tmp35_, "move-cursor", GTK_MOVEMENT_WORDS, 1, FALSE);
			break;
		}
		case GDK_KEY_b:
		{
			ScratchWidgetsSourceView* _tmp36_;
			_tmp36_ = self->priv->view;
			g_signal_emit_by_name ((GtkTextView*) _tmp36_, "move-cursor", GTK_MOVEMENT_WORDS, -1, FALSE);
			break;
		}
		case GDK_KEY_I:
		{
			GtkTextBuffer* buffer = NULL;
			ScratchWidgetsSourceView* _tmp37_;
			GtkTextBuffer* _tmp38_;
			GtkTextBuffer* _tmp39_;
			GtkTextBuffer* _tmp40_;
			GtkTextIter start = {0};
			GtkTextIter end = {0};
			GtkTextBuffer* _tmp41_;
			GtkTextIter _tmp42_ = {0};
			GtkTextIter _tmp43_ = {0};
			GtkTextBuffer* _tmp44_;
			GtkTextBuffer* _tmp45_;
			GtkTextMark* _tmp46_;
			GtkTextIter _tmp47_ = {0};
			GtkTextBuffer* _tmp48_;
			GtkTextIter _tmp49_;
			if (self->priv->mode == SCRATCH_PLUGINS_VIM_EMULATION_MODE_INSERT) {
				result = FALSE;
				return result;
			}
			self->priv->mode = SCRATCH_PLUGINS_VIM_EMULATION_MODE_INSERT;
			_tmp37_ = self->priv->view;
			_tmp38_ = gtk_text_view_get_buffer ((GtkTextView*) _tmp37_);
			_tmp39_ = _tmp38_;
			_tmp40_ = _g_object_ref0 (_tmp39_);
			buffer = _tmp40_;
			_tmp41_ = buffer;
			gtk_text_buffer_get_selection_bounds (_tmp41_, &_tmp42_, &_tmp43_);
			start = _tmp42_;
			end = _tmp43_;
			_tmp44_ = buffer;
			_tmp45_ = buffer;
			_tmp46_ = gtk_text_buffer_get_insert (_tmp45_);
			gtk_text_buffer_get_iter_at_mark (_tmp44_, &_tmp47_, _tmp46_);
			start = _tmp47_;
			gtk_text_iter_backward_sentence_start (&start);
			_tmp48_ = buffer;
			_tmp49_ = start;
			gtk_text_buffer_place_cursor (_tmp48_, &_tmp49_);
			g_debug ("vim-emulation.vala:186: Vim Emulation: INSERT Mode!");
			_g_object_unref0 (buffer);
			break;
		}
		case GDK_KEY_a:
		{
			gchar* _tmp50_;
			gint _tmp51_ = 0;
			const gchar* _tmp52_;
			ScratchWidgetsSourceView* _tmp54_;
			if (self->priv->mode == SCRATCH_PLUGINS_VIM_EMULATION_MODE_INSERT) {
				result = FALSE;
				return result;
			}
			_tmp50_ = g_strdup ("");
			_g_free0 (self->priv->action);
			self->priv->action = _tmp50_;
			self->priv->mode = SCRATCH_PLUGINS_VIM_EMULATION_MODE_INSERT;
			_tmp52_ = self->priv->number;
			if (g_strcmp0 (_tmp52_, "") == 0) {
				_tmp51_ = 1;
			} else {
				const gchar* _tmp53_;
				_tmp53_ = self->priv->number;
				_tmp51_ = atoi (_tmp53_);
			}
			_tmp54_ = self->priv->view;
			g_signal_emit_by_name ((GtkTextView*) _tmp54_, "move-cursor", GTK_MOVEMENT_VISUAL_POSITIONS, _tmp51_, FALSE);
			g_debug ("vim-emulation.vala:197: Vim Emulation: INSERT Mode!");
			result = TRUE;
			return result;
		}
		case GDK_KEY_A:
		{
			ScratchWidgetsSourceView* _tmp55_;
			if (self->priv->mode == SCRATCH_PLUGINS_VIM_EMULATION_MODE_INSERT) {
				result = FALSE;
				return result;
			}
			self->priv->mode = SCRATCH_PLUGINS_VIM_EMULATION_MODE_INSERT;
			_tmp55_ = self->priv->view;
			g_signal_emit_by_name ((GtkTextView*) _tmp55_, "move-cursor", GTK_MOVEMENT_PARAGRAPH_ENDS, 1, FALSE);
			g_debug ("vim-emulation.vala:206: Vim Emulation: INSERT Mode!");
			break;
		}
		case GDK_KEY_o:
		{
			ScratchWidgetsSourceView* _tmp56_;
			ScratchWidgetsSourceView* _tmp57_;
			if (self->priv->mode == SCRATCH_PLUGINS_VIM_EMULATION_MODE_INSERT) {
				result = FALSE;
				return result;
			}
			self->priv->mode = SCRATCH_PLUGINS_VIM_EMULATION_MODE_INSERT;
			g_debug ("vim-emulation.vala:213: Vim Emulation: INSERT Mode!");
			_tmp56_ = self->priv->view;
			g_signal_emit_by_name ((GtkTextView*) _tmp56_, "move-cursor", GTK_MOVEMENT_PARAGRAPH_ENDS, 1, FALSE);
			_tmp57_ = self->priv->view;
			g_signal_emit_by_name ((GtkTextView*) _tmp57_, "insert-at-cursor", "\n");
			break;
		}
		case GDK_KEY_O:
		{
			ScratchWidgetsSourceView* _tmp58_;
			ScratchWidgetsSourceView* _tmp59_;
			ScratchWidgetsSourceView* _tmp60_;
			ScratchWidgetsSourceView* _tmp61_;
			if (self->priv->mode == SCRATCH_PLUGINS_VIM_EMULATION_MODE_INSERT) {
				result = FALSE;
				return result;
			}
			self->priv->mode = SCRATCH_PLUGINS_VIM_EMULATION_MODE_INSERT;
			g_debug ("vim-emulation.vala:223: Vim Emulation: INSERT Mode!");
			_tmp58_ = self->priv->view;
			g_signal_emit_by_name ((GtkTextView*) _tmp58_, "move-cursor", GTK_MOVEMENT_PARAGRAPH_ENDS, -1, FALSE);
			_tmp59_ = self->priv->view;
			g_signal_emit_by_name ((GtkTextView*) _tmp59_, "move-cursor", GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1, FALSE);
			_tmp60_ = self->priv->view;
			g_signal_emit_by_name ((GtkTextView*) _tmp60_, "insert-at-cursor", "\n");
			_tmp61_ = self->priv->view;
			g_signal_emit_by_name ((GtkTextView*) _tmp61_, "move-cursor", GTK_MOVEMENT_PARAGRAPHS, -1, FALSE);
			break;
		}
		case 46:
		{
			const gchar* _tmp62_;
			ScratchWidgetsSourceView* _tmp63_;
			const gchar* _tmp64_;
			_tmp62_ = self->priv->action;
			g_debug ("vim-emulation.vala:234: %s", _tmp62_);
			_tmp63_ = self->priv->view;
			_tmp64_ = self->priv->action;
			g_signal_emit_by_name ((GtkTextView*) _tmp63_, "insert-at-cursor", _tmp64_);
			break;
		}
		case GDK_KEY_Home:
		case GDK_KEY_0:
		{
			const gchar* _tmp65_;
			_tmp65_ = self->priv->number;
			if (g_strcmp0 (_tmp65_, "") == 0) {
				ScratchWidgetsSourceView* _tmp66_;
				ScratchWidgetsSourceView* _tmp67_;
				_tmp66_ = self->priv->view;
				g_signal_emit_by_name ((GtkTextView*) _tmp66_, "move-cursor", GTK_MOVEMENT_PARAGRAPH_ENDS, -1, FALSE);
				_tmp67_ = self->priv->view;
				g_signal_emit_by_name ((GtkTextView*) _tmp67_, "move-cursor", GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1, FALSE);
			} else {
				const gchar* _tmp68_;
				gchar* _tmp69_;
				_tmp68_ = self->priv->number;
				_tmp69_ = g_strconcat (_tmp68_, "0", NULL);
				_g_free0 (self->priv->number);
				self->priv->number = _tmp69_;
			}
			break;
		}
		case GDK_KEY_e:
		{
			gint _tmp70_ = 0;
			const gchar* _tmp71_;
			ScratchWidgetsSourceView* _tmp73_;
			_tmp71_ = self->priv->number;
			if (g_strcmp0 (_tmp71_, "") == 0) {
				_tmp70_ = 1;
			} else {
				const gchar* _tmp72_;
				_tmp72_ = self->priv->number;
				_tmp70_ = atoi (_tmp72_);
			}
			_tmp73_ = self->priv->view;
			g_signal_emit_by_name ((GtkTextView*) _tmp73_, "move-cursor", GTK_MOVEMENT_WORDS, _tmp70_, FALSE);
			break;
		}
		case GDK_KEY_g:
		{
			ScratchWidgetsSourceView* _tmp74_;
			const gchar* _tmp75_;
			self->priv->g = TRUE;
			_tmp74_ = self->priv->view;
			_tmp75_ = self->priv->number;
			scratch_widgets_source_view_go_to_line (_tmp74_, atoi (_tmp75_), 0);
			break;
		}
		default:
		break;
	}
	_tmp76_ = self->priv->number;
	_tmp77_ = strlen (_tmp76_);
	_tmp78_ = _tmp77_;
	if (old_len == _tmp78_) {
		gchar* _tmp79_;
		_tmp79_ = g_strdup ("");
		_g_free0 (self->priv->number);
		self->priv->number = _tmp79_;
	}
	result = TRUE;
	return result;
}

static void
scratch_plugins_vim_emulation_move_paragraph (ScratchPluginsVimEmulation* self,
                                              gboolean up,
                                              gboolean select)
{
	GtkTextBuffer* buffer = NULL;
	ScratchWidgetsSourceView* _tmp0_;
	GtkTextBuffer* _tmp1_;
	GtkTextBuffer* _tmp2_;
	GtkTextBuffer* _tmp3_;
	GtkTextIter iter = {0};
	GtkTextIter start = {0};
	GtkTextIter end = {0};
	GtkTextBuffer* _tmp4_;
	GtkTextBuffer* _tmp5_;
	gint _tmp6_;
	gint _tmp7_;
	GtkTextIter _tmp8_ = {0};
	gchar* search = NULL;
	gchar* _tmp9_;
	gboolean success = FALSE;
	ScratchWidgetsSourceView* _tmp27_;
	GtkTextIter _tmp28_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->view;
	_tmp1_ = gtk_text_view_get_buffer ((GtkTextView*) _tmp0_);
	_tmp2_ = _tmp1_;
	_tmp3_ = _g_object_ref0 (_tmp2_);
	buffer = _tmp3_;
	_tmp4_ = buffer;
	_tmp5_ = buffer;
	g_object_get (_tmp5_, "cursor-position", &_tmp6_, NULL);
	_tmp7_ = _tmp6_;
	gtk_text_buffer_get_iter_at_offset (_tmp4_, &_tmp8_, _tmp7_);
	iter = _tmp8_;
	_tmp9_ = g_strdup ("\n\n");
	search = _tmp9_;
	success = FALSE;
	if (up) {
		const gchar* _tmp10_;
		GtkTextIter _tmp11_ = {0};
		GtkTextIter _tmp12_ = {0};
		gboolean _tmp13_;
		_tmp10_ = search;
		_tmp13_ = gtk_text_iter_backward_search (&iter, _tmp10_, 0, &_tmp11_, &_tmp12_, NULL);
		start = _tmp11_;
		end = _tmp12_;
		success = _tmp13_;
	} else {
		const gchar* _tmp14_;
		GtkTextIter _tmp15_ = {0};
		GtkTextIter _tmp16_ = {0};
		gboolean _tmp17_;
		_tmp14_ = search;
		_tmp17_ = gtk_text_iter_forward_search (&iter, _tmp14_, 0, &_tmp15_, &_tmp16_, NULL);
		start = _tmp15_;
		end = _tmp16_;
		success = _tmp17_;
	}
	if (!success) {
		if (up) {
			GtkTextBuffer* _tmp18_;
			GtkTextIter _tmp19_ = {0};
			_tmp18_ = buffer;
			gtk_text_buffer_get_start_iter (_tmp18_, &_tmp19_);
			start = _tmp19_;
		} else {
			GtkTextBuffer* _tmp20_;
			GtkTextIter _tmp21_ = {0};
			_tmp20_ = buffer;
			gtk_text_buffer_get_end_iter (_tmp20_, &_tmp21_);
			start = _tmp21_;
		}
	} else {
		gtk_text_iter_forward_char (&start);
	}
	if (select) {
		GtkTextBuffer* _tmp22_;
		GtkTextIter _tmp23_;
		GtkTextIter _tmp24_;
		_tmp22_ = buffer;
		_tmp23_ = start;
		_tmp24_ = iter;
		gtk_text_buffer_select_range (_tmp22_, &_tmp23_, &_tmp24_);
	} else {
		GtkTextBuffer* _tmp25_;
		GtkTextIter _tmp26_;
		_tmp25_ = buffer;
		_tmp26_ = start;
		gtk_text_buffer_place_cursor (_tmp25_, &_tmp26_);
	}
	_tmp27_ = self->priv->view;
	_tmp28_ = start;
	gtk_text_view_scroll_to_iter ((GtkTextView*) _tmp27_, &_tmp28_, (gdouble) 0, FALSE, (gdouble) 0, (gdouble) 0);
	_g_free0 (search);
	_g_object_unref0 (buffer);
}

ScratchPluginsVimEmulation*
scratch_plugins_vim_emulation_construct (GType object_type)
{
	ScratchPluginsVimEmulation * self = NULL;
	self = (ScratchPluginsVimEmulation*) g_object_new (object_type, NULL);
	return self;
}

ScratchPluginsVimEmulation*
scratch_plugins_vim_emulation_new (void)
{
	return scratch_plugins_vim_emulation_construct (SCRATCH_PLUGINS_TYPE_VIM_EMULATION);
}

static GObject*
scratch_plugins_vim_emulation_real_get_object (ScratchServicesActivatablePlugin* base)
{
	GObject* result;
	ScratchPluginsVimEmulation* self;
	GObject* _tmp0_;
	GObject* _tmp1_;
	self = (ScratchPluginsVimEmulation*) base;
	_tmp0_ = self->priv->_object;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	result = _tmp1_;
	return result;
}

static void
scratch_plugins_vim_emulation_real_set_object (ScratchServicesActivatablePlugin* base,
                                               GObject* value)
{
	ScratchPluginsVimEmulation* self;
	GObject* old_value;
	self = (ScratchPluginsVimEmulation*) base;
	old_value = scratch_plugins_vim_emulation_real_get_object (base);
	if (old_value != value) {
		GObject* _tmp0_;
		_tmp0_ = _g_object_ref0 (value);
		_g_object_unref0 (self->priv->_object);
		self->priv->_object = _tmp0_;
		g_object_notify_by_pspec ((GObject *) self, scratch_plugins_vim_emulation_properties[SCRATCH_PLUGINS_VIM_EMULATION_OBJECT_PROPERTY]);
	}
	_g_object_unref0 (old_value);
}

static GObject *
scratch_plugins_vim_emulation_constructor (GType type,
                                           guint n_construct_properties,
                                           GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	ScratchPluginsVimEmulation * self;
	GeeTreeSet* _tmp0_;
	parent_class = G_OBJECT_CLASS (scratch_plugins_vim_emulation_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SCRATCH_PLUGINS_TYPE_VIM_EMULATION, ScratchPluginsVimEmulation);
	_tmp0_ = gee_tree_set_new (SCRATCH_WIDGETS_TYPE_SOURCE_VIEW, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, NULL, NULL, NULL);
	_g_object_unref0 (self->priv->views);
	self->priv->views = _tmp0_;
	return obj;
}

static void
scratch_plugins_vim_emulation_class_init (ScratchPluginsVimEmulationClass * klass,
                                          gpointer klass_data)
{
	scratch_plugins_vim_emulation_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ScratchPluginsVimEmulation_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_scratch_plugins_vim_emulation_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_scratch_plugins_vim_emulation_set_property;
	G_OBJECT_CLASS (klass)->constructor = scratch_plugins_vim_emulation_constructor;
	G_OBJECT_CLASS (klass)->finalize = scratch_plugins_vim_emulation_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), SCRATCH_PLUGINS_VIM_EMULATION_OBJECT_PROPERTY, scratch_plugins_vim_emulation_properties[SCRATCH_PLUGINS_VIM_EMULATION_OBJECT_PROPERTY] = g_param_spec_object ("object", "object", "object", G_TYPE_OBJECT, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
}

static void
scratch_plugins_vim_emulation_scratch_services_activatable_plugin_interface_init (ScratchServicesActivatablePluginIface * iface,
                                                                                  gpointer iface_data)
{
	scratch_plugins_vim_emulation_scratch_services_activatable_plugin_parent_iface = g_type_interface_peek_parent (iface);
	iface->update_state = (void (*) (ScratchServicesActivatablePlugin*)) scratch_plugins_vim_emulation_real_update_state;
	iface->activate = (void (*) (ScratchServicesActivatablePlugin*)) scratch_plugins_vim_emulation_real_activate;
	iface->deactivate = (void (*) (ScratchServicesActivatablePlugin*)) scratch_plugins_vim_emulation_real_deactivate;
	iface->get_object = scratch_plugins_vim_emulation_real_get_object;
	iface->set_object = scratch_plugins_vim_emulation_real_set_object;
}

static void
scratch_plugins_vim_emulation_instance_init (ScratchPluginsVimEmulation * self,
                                             gpointer klass)
{
	gchar* _tmp0_;
	gchar* _tmp1_;
	self->priv = scratch_plugins_vim_emulation_get_instance_private (self);
	self->priv->mode = SCRATCH_PLUGINS_VIM_EMULATION_MODE_COMMAND;
	_tmp0_ = g_strdup ("");
	self->priv->number = _tmp0_;
	_tmp1_ = g_strdup ("");
	self->priv->action = _tmp1_;
	self->priv->g = FALSE;
	self->priv->view = NULL;
}

static void
scratch_plugins_vim_emulation_finalize (GObject * obj)
{
	ScratchPluginsVimEmulation * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SCRATCH_PLUGINS_TYPE_VIM_EMULATION, ScratchPluginsVimEmulation);
	_g_free0 (self->priv->number);
	_g_free0 (self->priv->action);
	_g_object_unref0 (self->priv->views);
	_g_object_unref0 (self->priv->view);
	_g_object_unref0 (self->priv->plugins);
	_g_object_unref0 (self->priv->_object);
	G_OBJECT_CLASS (scratch_plugins_vim_emulation_parent_class)->finalize (obj);
}

/***
  BEGIN LICENSE

  Copyright (C) 2013 Mario Guerriero <mario@elementaryos.org>
  This program is free software: you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License version 3, as published
  by the Free Software Foundation.

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranties of
  MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
  PURPOSE.  See the GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program.  If not, see <http://www.gnu.org/licenses/>

  END LICENSE
***/
GType
scratch_plugins_vim_emulation_get_type (void)
{
	return scratch_plugins_vim_emulation_type_id;
}

GType
scratch_plugins_vim_emulation_register_type (GTypeModule * module)
{
	static const GTypeInfo g_define_type_info = { sizeof (ScratchPluginsVimEmulationClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) scratch_plugins_vim_emulation_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ScratchPluginsVimEmulation), 0, (GInstanceInitFunc) scratch_plugins_vim_emulation_instance_init, NULL };
	static const GInterfaceInfo scratch_services_activatable_plugin_info = { (GInterfaceInitFunc) scratch_plugins_vim_emulation_scratch_services_activatable_plugin_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	scratch_plugins_vim_emulation_type_id = g_type_module_register_type (module, peas_extension_base_get_type (), "ScratchPluginsVimEmulation", &g_define_type_info, G_TYPE_FLAG_NONE);
	g_type_module_add_interface (module, scratch_plugins_vim_emulation_type_id, SCRATCH_SERVICES_TYPE_ACTIVATABLE_PLUGIN, &scratch_services_activatable_plugin_info);
	ScratchPluginsVimEmulation_private_offset = sizeof (ScratchPluginsVimEmulationPrivate);
	return scratch_plugins_vim_emulation_type_id;
}

static void
_vala_scratch_plugins_vim_emulation_get_property (GObject * object,
                                                  guint property_id,
                                                  GValue * value,
                                                  GParamSpec * pspec)
{
	ScratchPluginsVimEmulation * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, SCRATCH_PLUGINS_TYPE_VIM_EMULATION, ScratchPluginsVimEmulation);
	switch (property_id) {
		case SCRATCH_PLUGINS_VIM_EMULATION_OBJECT_PROPERTY:
		g_value_take_object (value, scratch_services_activatable_plugin_get_object ((ScratchServicesActivatablePlugin*) self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_scratch_plugins_vim_emulation_set_property (GObject * object,
                                                  guint property_id,
                                                  const GValue * value,
                                                  GParamSpec * pspec)
{
	ScratchPluginsVimEmulation * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, SCRATCH_PLUGINS_TYPE_VIM_EMULATION, ScratchPluginsVimEmulation);
	switch (property_id) {
		case SCRATCH_PLUGINS_VIM_EMULATION_OBJECT_PROPERTY:
		scratch_services_activatable_plugin_set_object ((ScratchServicesActivatablePlugin*) self, g_value_get_object (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

void
peas_register_types (GTypeModule* module)
{
	PeasObjectModule* objmodule = NULL;
	PeasObjectModule* _tmp0_;
	g_return_if_fail (module != NULL);
	scratch_plugins_vim_emulation_register_type (module);
	_tmp0_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_TYPE (module, peas_object_module_get_type ()) ? ((PeasObjectModule*) module) : NULL);
	objmodule = _tmp0_;
	peas_object_module_register_extension_type (objmodule, SCRATCH_SERVICES_TYPE_ACTIVATABLE_PLUGIN, SCRATCH_PLUGINS_TYPE_VIM_EMULATION);
	_g_object_unref0 (objmodule);
}

