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

/*
* Copyright (c) 2017 Felipe Escoto (https://github.com/Philip-Scott/Notes-up)
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY 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, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authored by: Felipe Escoto <felescoto95@hotmail.com>
*/

#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <gee.h>
#include <glib/gi18n-lib.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
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define ENOTES_TYPE_PLUGIN (enotes_plugin_get_type ())
#define ENOTES_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), ENOTES_TYPE_PLUGIN, ENotesPlugin))
#define ENOTES_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), ENOTES_TYPE_PLUGIN, ENotesPluginClass))
#define ENOTES_IS_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), ENOTES_TYPE_PLUGIN))
#define ENOTES_IS_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), ENOTES_TYPE_PLUGIN))
#define ENOTES_PLUGIN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), ENOTES_TYPE_PLUGIN, ENotesPluginClass))

typedef struct _ENotesPlugin ENotesPlugin;
typedef struct _ENotesPluginClass ENotesPluginClass;
typedef struct _ENotesPluginPrivate ENotesPluginPrivate;

#define TYPE_BL_MEMBER (bl_member_get_type ())
#define BL_MEMBER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_BL_MEMBER, BLMember))
#define BL_MEMBER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_BL_MEMBER, BLMemberClass))
#define IS_BL_MEMBER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_BL_MEMBER))
#define IS_BL_MEMBER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_BL_MEMBER))
#define BL_MEMBER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_BL_MEMBER, BLMemberClass))

typedef struct _BLMember BLMember;
typedef struct _BLMemberClass BLMemberClass;

#define ENOTES_TYPE_HIGHLIGHT (enotes_highlight_get_type ())
#define ENOTES_HIGHLIGHT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), ENOTES_TYPE_HIGHLIGHT, ENotesHighlight))
#define ENOTES_HIGHLIGHT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), ENOTES_TYPE_HIGHLIGHT, ENotesHighlightClass))
#define ENOTES_IS_HIGHLIGHT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), ENOTES_TYPE_HIGHLIGHT))
#define ENOTES_IS_HIGHLIGHT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), ENOTES_TYPE_HIGHLIGHT))
#define ENOTES_HIGHLIGHT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), ENOTES_TYPE_HIGHLIGHT, ENotesHighlightClass))

typedef struct _ENotesHighlight ENotesHighlight;
typedef struct _ENotesHighlightClass ENotesHighlightClass;
typedef struct _ENotesHighlightPrivate ENotesHighlightPrivate;
enum  {
	ENOTES_HIGHLIGHT_0_PROPERTY,
	ENOTES_HIGHLIGHT_NUM_PROPERTIES
};
static GParamSpec* enotes_highlight_properties[ENOTES_HIGHLIGHT_NUM_PROPERTIES];
#define _g_pattern_spec_free0(var) ((var == NULL) ? NULL : (var = (g_pattern_spec_free (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))
#define _bl_member_unref0(var) ((var == NULL) ? NULL : (var = (bl_member_unref (var), NULL)))

struct _ENotesPlugin {
	GObject parent_instance;
	ENotesPluginPrivate * priv;
	gboolean state;
	gchar* code_name;
};

struct _ENotesPluginClass {
	GObjectClass parent_class;
	gchar* (*get_desctiption) (ENotesPlugin* self);
	gchar* (*get_name) (ENotesPlugin* self);
	gboolean (*has_match) (ENotesPlugin* self, const gchar* text);
	gchar* (*convert) (ENotesPlugin* self, const gchar* line);
	GtkWidget* (*editor_button) (ENotesPlugin* self);
	gchar* (*get_button_desctiption) (ENotesPlugin* self);
	gchar* (*request_string) (ENotesPlugin* self, const gchar* selection);
	GeeList* (*get_blacklist_members) (ENotesPlugin* self);
};

struct _ENotesHighlight {
	ENotesPlugin parent_instance;
	ENotesHighlightPrivate * priv;
};

struct _ENotesHighlightClass {
	ENotesPluginClass parent_class;
};

struct _ENotesHighlightPrivate {
	GPatternSpec* spec;
};

static gint ENotesHighlight_private_offset;
static gpointer enotes_highlight_parent_class = NULL;

VALA_EXTERN GType enotes_plugin_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ENotesPlugin, g_object_unref)
VALA_EXTERN gpointer bl_member_ref (gpointer instance);
VALA_EXTERN void bl_member_unref (gpointer instance);
VALA_EXTERN GParamSpec* param_spec_bl_member (const gchar* name,
                                  const gchar* nick,
                                  const gchar* blurb,
                                  GType object_type,
                                  GParamFlags flags);
VALA_EXTERN void value_set_bl_member (GValue* value,
                          gpointer v_object);
VALA_EXTERN void value_take_bl_member (GValue* value,
                           gpointer v_object);
VALA_EXTERN gpointer value_get_bl_member (const GValue* value);
VALA_EXTERN GType bl_member_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (BLMember, bl_member_unref)
VALA_EXTERN GType enotes_highlight_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ENotesHighlight, g_object_unref)
VALA_EXTERN gchar* enotes_plugin_get_desctiption (ENotesPlugin* self);
VALA_EXTERN gchar* enotes_plugin_get_name (ENotesPlugin* self);
VALA_EXTERN GtkWidget* enotes_plugin_editor_button (ENotesPlugin* self);
VALA_EXTERN gchar* enotes_plugin_request_string (ENotesPlugin* self,
                                     const gchar* selection);
VALA_EXTERN gchar* enotes_plugin_get_button_desctiption (ENotesPlugin* self);
VALA_EXTERN gboolean enotes_plugin_has_match (ENotesPlugin* self,
                                  const gchar* text);
VALA_EXTERN gchar* enotes_plugin_convert (ENotesPlugin* self,
                              const gchar* line);
VALA_EXTERN GeeList* enotes_plugin_get_blacklist_members (ENotesPlugin* self);
static gchar* enotes_highlight_real_get_desctiption (ENotesPlugin* base);
static gchar* enotes_highlight_real_get_name (ENotesPlugin* base);
static GtkWidget* enotes_highlight_real_editor_button (ENotesPlugin* base);
static gchar* enotes_highlight_real_request_string (ENotesPlugin* base,
                                             const gchar* selection);
static gchar* enotes_highlight_real_get_button_desctiption (ENotesPlugin* base);
static gboolean enotes_highlight_real_has_match (ENotesPlugin* base,
                                          const gchar* text);
static gchar* enotes_highlight_real_convert (ENotesPlugin* base,
                                      const gchar* line_);
static GeeList* enotes_highlight_real_get_blacklist_members (ENotesPlugin* base);
VALA_EXTERN BLMember* bl_member_new (GRegex* reg,
                         const gchar* replace);
VALA_EXTERN BLMember* bl_member_construct (GType object_type,
                               GRegex* reg,
                               const gchar* replace);
VALA_EXTERN ENotesHighlight* enotes_highlight_new (void);
VALA_EXTERN ENotesHighlight* enotes_highlight_construct (GType object_type);
VALA_EXTERN ENotesPlugin* enotes_plugin_construct (GType object_type);
static GObject * enotes_highlight_constructor (GType type,
                                        guint n_construct_properties,
                                        GObjectConstructParam * construct_properties);
static void enotes_highlight_finalize (GObject * obj);
static GType enotes_highlight_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static GRegex* _tmp_regex_0 = NULL;

static inline gpointer
enotes_highlight_get_instance_private (ENotesHighlight* self)
{
	return G_STRUCT_MEMBER_P (self, ENotesHighlight_private_offset);
}

static gchar*
enotes_highlight_real_get_desctiption (ENotesPlugin* base)
{
	ENotesHighlight * self;
	gchar* _tmp0_;
	gchar* result;
	self = (ENotesHighlight*) base;
	_tmp0_ = g_strdup (_ ("Enable Syntax Highlighting"));
	result = _tmp0_;
	return result;
}

static gchar*
enotes_highlight_real_get_name (ENotesPlugin* base)
{
	ENotesHighlight * self;
	gchar* _tmp0_;
	gchar* result;
	self = (ENotesHighlight*) base;
	_tmp0_ = g_strdup (_ ("Syntax Highlighing"));
	result = _tmp0_;
	return result;
}

static GtkWidget*
enotes_highlight_real_editor_button (ENotesPlugin* base)
{
	ENotesHighlight * self;
	GtkWidget* result;
	self = (ENotesHighlight*) base;
	result = NULL;
	return result;
}

static gchar*
enotes_highlight_real_request_string (ENotesPlugin* base,
                                      const gchar* selection)
{
	ENotesHighlight * self;
	gchar* _tmp0_;
	gchar* result;
	self = (ENotesHighlight*) base;
	g_return_val_if_fail (selection != NULL, NULL);
	g_signal_emit_by_name ((ENotesPlugin*) self, "string-cooked", "<highlight>");
	_tmp0_ = g_strdup (selection);
	result = _tmp0_;
	return result;
}

static gchar*
enotes_highlight_real_get_button_desctiption (ENotesPlugin* base)
{
	ENotesHighlight * self;
	gchar* _tmp0_;
	gchar* result;
	self = (ENotesHighlight*) base;
	_tmp0_ = g_strdup ("");
	result = _tmp0_;
	return result;
}

static gboolean
enotes_highlight_real_has_match (ENotesPlugin* base,
                                 const gchar* text)
{
	ENotesHighlight * self;
	GPatternSpec* _tmp0_;
	gboolean result;
	self = (ENotesHighlight*) base;
	g_return_val_if_fail (text != NULL, FALSE);
	_tmp0_ = self->priv->spec;
	result = g_pattern_match_string (_tmp0_, text);
	return result;
}

static gchar*
string_replace (const gchar* self,
                const gchar* old,
                const gchar* replacement)
{
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_ = FALSE;
	GError* _inner_error0_ = NULL;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (old != NULL, NULL);
	g_return_val_if_fail (replacement != NULL, NULL);
	if ((*((gchar*) self)) == '\0') {
		_tmp1_ = TRUE;
	} else {
		_tmp1_ = (*((gchar*) old)) == '\0';
	}
	if (_tmp1_) {
		_tmp0_ = TRUE;
	} else {
		_tmp0_ = g_strcmp0 (old, replacement) == 0;
	}
	if (_tmp0_) {
		gchar* _tmp2_;
		_tmp2_ = g_strdup (self);
		result = _tmp2_;
		return result;
	}
	{
		GRegex* regex = NULL;
		gchar* _tmp3_;
		gchar* _tmp4_;
		GRegex* _tmp5_;
		GRegex* _tmp6_;
		gchar* _tmp7_ = NULL;
		GRegex* _tmp8_;
		gchar* _tmp9_;
		gchar* _tmp10_;
		_tmp3_ = g_regex_escape_string (old, -1);
		_tmp4_ = _tmp3_;
		_tmp5_ = g_regex_new (_tmp4_, 0, 0, &_inner_error0_);
		_tmp6_ = _tmp5_;
		_g_free0 (_tmp4_);
		regex = _tmp6_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return NULL;
		}
		_tmp8_ = regex;
		_tmp9_ = g_regex_replace_literal (_tmp8_, self, (gssize) -1, 0, replacement, 0, &_inner_error0_);
		_tmp7_ = _tmp9_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_regex_unref0 (regex);
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return NULL;
		}
		_tmp10_ = _tmp7_;
		_tmp7_ = NULL;
		result = _tmp10_;
		_g_free0 (_tmp7_);
		_g_regex_unref0 (regex);
		return result;
	}
	goto __finally0;
	__catch0_g_regex_error:
	{
		g_clear_error (&_inner_error0_);
		g_assert_not_reached ();
	}
	__finally0:
	g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
	g_clear_error (&_inner_error0_);
	return NULL;
}

static gchar*
enotes_highlight_real_convert (ENotesPlugin* base,
                               const gchar* line_)
{
	ENotesHighlight * self;
	gchar* _tmp0_;
	gchar* result;
	self = (ENotesHighlight*) base;
	g_return_val_if_fail (line_ != NULL, NULL);
	_tmp0_ = string_replace (line_, "<highlight>", "<link rel=\"stylesheet\" href=\"/usr/share/notes-up/solarized-light.cs" \
"s\">\n" \
"<script src=\"/usr/share/notes-up/highlight.pack.js\"></script>\n" \
"<script>hljs.initHighlightingOnLoad();</script>");
	result = _tmp0_;
	return result;
}

static inline GRegex*
_thread_safe_regex_init (GRegex** re,
                         const gchar * pattern,
                         GRegexCompileFlags compile_flags)
{
	if (g_once_init_enter ((volatile gsize*) re)) {
		GRegex* val = g_regex_new (pattern, compile_flags, 0, NULL);
		g_once_init_leave ((volatile gsize*) re, (gsize) val);
	}
	return *re;
}

static GeeList*
enotes_highlight_real_get_blacklist_members (ENotesPlugin* base)
{
	ENotesHighlight * self;
	GeeLinkedList* list = NULL;
	GeeLinkedList* _tmp0_;
	BLMember* _tmp1_;
	BLMember* _tmp2_;
	GeeList* result;
	self = (ENotesHighlight*) base;
	_tmp0_ = gee_linked_list_new (TYPE_BL_MEMBER, (GBoxedCopyFunc) bl_member_ref, (GDestroyNotify) bl_member_unref, NULL, NULL, NULL);
	list = _tmp0_;
	_tmp1_ = bl_member_new (_thread_safe_regex_init (&_tmp_regex_0, "<highlight>", 0), "");
	_tmp2_ = _tmp1_;
	gee_abstract_collection_add ((GeeAbstractCollection*) list, _tmp2_);
	_bl_member_unref0 (_tmp2_);
	result = (GeeList*) list;
	return result;
}

ENotesHighlight*
enotes_highlight_construct (GType object_type)
{
	ENotesHighlight * self = NULL;
	self = (ENotesHighlight*) enotes_plugin_construct (object_type);
	return self;
}

ENotesHighlight*
enotes_highlight_new (void)
{
	return enotes_highlight_construct (ENOTES_TYPE_HIGHLIGHT);
}

static GObject *
enotes_highlight_constructor (GType type,
                              guint n_construct_properties,
                              GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	ENotesHighlight * self;
	parent_class = G_OBJECT_CLASS (enotes_highlight_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, ENOTES_TYPE_HIGHLIGHT, ENotesHighlight);
	return obj;
}

static void
enotes_highlight_class_init (ENotesHighlightClass * klass,
                             gpointer klass_data)
{
	enotes_highlight_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ENotesHighlight_private_offset);
	((ENotesPluginClass *) klass)->get_desctiption = (gchar* (*) (ENotesPlugin*)) enotes_highlight_real_get_desctiption;
	((ENotesPluginClass *) klass)->get_name = (gchar* (*) (ENotesPlugin*)) enotes_highlight_real_get_name;
	((ENotesPluginClass *) klass)->editor_button = (GtkWidget* (*) (ENotesPlugin*)) enotes_highlight_real_editor_button;
	((ENotesPluginClass *) klass)->request_string = (gchar* (*) (ENotesPlugin*, const gchar*)) enotes_highlight_real_request_string;
	((ENotesPluginClass *) klass)->get_button_desctiption = (gchar* (*) (ENotesPlugin*)) enotes_highlight_real_get_button_desctiption;
	((ENotesPluginClass *) klass)->has_match = (gboolean (*) (ENotesPlugin*, const gchar*)) enotes_highlight_real_has_match;
	((ENotesPluginClass *) klass)->convert = (gchar* (*) (ENotesPlugin*, const gchar*)) enotes_highlight_real_convert;
	((ENotesPluginClass *) klass)->get_blacklist_members = (GeeList* (*) (ENotesPlugin*)) enotes_highlight_real_get_blacklist_members;
	G_OBJECT_CLASS (klass)->constructor = enotes_highlight_constructor;
	G_OBJECT_CLASS (klass)->finalize = enotes_highlight_finalize;
}

static void
enotes_highlight_instance_init (ENotesHighlight * self,
                                gpointer klass)
{
	GPatternSpec* _tmp0_;
	self->priv = enotes_highlight_get_instance_private (self);
	_tmp0_ = g_pattern_spec_new ("*<highlight>*");
	self->priv->spec = _tmp0_;
}

static void
enotes_highlight_finalize (GObject * obj)
{
	ENotesHighlight * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, ENOTES_TYPE_HIGHLIGHT, ENotesHighlight);
	_g_pattern_spec_free0 (self->priv->spec);
	G_OBJECT_CLASS (enotes_highlight_parent_class)->finalize (obj);
}

static GType
enotes_highlight_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ENotesHighlightClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) enotes_highlight_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ENotesHighlight), 0, (GInstanceInitFunc) enotes_highlight_instance_init, NULL };
	GType enotes_highlight_type_id;
	enotes_highlight_type_id = g_type_register_static (ENOTES_TYPE_PLUGIN, "ENotesHighlight", &g_define_type_info, 0);
	ENotesHighlight_private_offset = g_type_add_instance_private (enotes_highlight_type_id, sizeof (ENotesHighlightPrivate));
	return enotes_highlight_type_id;
}

GType
enotes_highlight_get_type (void)
{
	static volatile gsize enotes_highlight_type_id__once = 0;
	if (g_once_init_enter (&enotes_highlight_type_id__once)) {
		GType enotes_highlight_type_id;
		enotes_highlight_type_id = enotes_highlight_get_type_once ();
		g_once_init_leave (&enotes_highlight_type_id__once, enotes_highlight_type_id);
	}
	return enotes_highlight_type_id__once;
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

