/*
 * This file was generated automatically by ExtUtils::ParseXS version 3.40 from the
 * contents of Themes.xs. Do not edit this file, edit Themes.xs instead.
 *
 *    ANY CHANGES MADE HERE WILL BE LOST!
 *
 */

#line 1 "Themes.xs"
#define PERL_NO_GET_CONTEXT
#include "module.h"

static void printformat_perl(TEXT_DEST_REC *dest, char *format, char **arglist)
{
	char *module;
	int formatnum;

	module = g_strdup(perl_get_package());
	formatnum = format_find_tag(module, format);
	if (formatnum < 0) {
		die("printformat(): unregistered format '%s'", format);
                g_free(module);
		return;
	}

	printformat_module_dest_charargs(module, dest, formatnum, arglist);
	g_free(module);
}

static void perl_unregister_theme(const char *package)
{
	FORMAT_REC *formats;
	int n;

	formats = g_hash_table_lookup(default_formats, package);
	if (formats == NULL) return;

	for (n = 0; formats[n].def != NULL; n++) {
		g_free(formats[n].tag);
		g_free(formats[n].def);
	}
	g_free(formats);
	theme_unregister_module(package);
}

static void sig_script_destroyed(PERL_SCRIPT_REC *script)
{
	perl_unregister_theme(script->package);
}

void perl_themes_init(void)
{
	signal_add("script destroyed", (SIGNAL_FUNC) sig_script_destroyed);
}

void perl_themes_deinit(void)
{
	signal_remove("script destroyed", (SIGNAL_FUNC) sig_script_destroyed);
}

#line 62 "Themes.c"
#ifndef PERL_UNUSED_VAR
#  define PERL_UNUSED_VAR(var) if (0) var = var
#endif

#ifndef dVAR
#  define dVAR		dNOOP
#endif


/* This stuff is not part of the API! You have been warned. */
#ifndef PERL_VERSION_DECIMAL
#  define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
#endif
#ifndef PERL_DECIMAL_VERSION
#  define PERL_DECIMAL_VERSION \
	  PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
#endif
#ifndef PERL_VERSION_GE
#  define PERL_VERSION_GE(r,v,s) \
	  (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))
#endif
#ifndef PERL_VERSION_LE
#  define PERL_VERSION_LE(r,v,s) \
	  (PERL_DECIMAL_VERSION <= PERL_VERSION_DECIMAL(r,v,s))
#endif

/* XS_INTERNAL is the explicit static-linkage variant of the default
 * XS macro.
 *
 * XS_EXTERNAL is the same as XS_INTERNAL except it does not include
 * "STATIC", ie. it exports XSUB symbols. You probably don't want that
 * for anything but the BOOT XSUB.
 *
 * See XSUB.h in core!
 */


/* TODO: This might be compatible further back than 5.10.0. */
#if PERL_VERSION_GE(5, 10, 0) && PERL_VERSION_LE(5, 15, 1)
#  undef XS_EXTERNAL
#  undef XS_INTERNAL
#  if defined(__CYGWIN__) && defined(USE_DYNAMIC_LOADING)
#    define XS_EXTERNAL(name) __declspec(dllexport) XSPROTO(name)
#    define XS_INTERNAL(name) STATIC XSPROTO(name)
#  endif
#  if defined(__SYMBIAN32__)
#    define XS_EXTERNAL(name) EXPORT_C XSPROTO(name)
#    define XS_INTERNAL(name) EXPORT_C STATIC XSPROTO(name)
#  endif
#  ifndef XS_EXTERNAL
#    if defined(HASATTRIBUTE_UNUSED) && !defined(__cplusplus)
#      define XS_EXTERNAL(name) void name(pTHX_ CV* cv __attribute__unused__)
#      define XS_INTERNAL(name) STATIC void name(pTHX_ CV* cv __attribute__unused__)
#    else
#      ifdef __cplusplus
#        define XS_EXTERNAL(name) extern "C" XSPROTO(name)
#        define XS_INTERNAL(name) static XSPROTO(name)
#      else
#        define XS_EXTERNAL(name) XSPROTO(name)
#        define XS_INTERNAL(name) STATIC XSPROTO(name)
#      endif
#    endif
#  endif
#endif

/* perl >= 5.10.0 && perl <= 5.15.1 */


/* The XS_EXTERNAL macro is used for functions that must not be static
 * like the boot XSUB of a module. If perl didn't have an XS_EXTERNAL
 * macro defined, the best we can do is assume XS is the same.
 * Dito for XS_INTERNAL.
 */
#ifndef XS_EXTERNAL
#  define XS_EXTERNAL(name) XS(name)
#endif
#ifndef XS_INTERNAL
#  define XS_INTERNAL(name) XS(name)
#endif

/* Now, finally, after all this mess, we want an ExtUtils::ParseXS
 * internal macro that we're free to redefine for varying linkage due
 * to the EXPORT_XSUB_SYMBOLS XS keyword. This is internal, use
 * XS_EXTERNAL(name) or XS_INTERNAL(name) in your code if you need to!
 */

#undef XS_EUPXS
#if defined(PERL_EUPXS_ALWAYS_EXPORT)
#  define XS_EUPXS(name) XS_EXTERNAL(name)
#else
   /* default to internal */
#  define XS_EUPXS(name) XS_INTERNAL(name)
#endif

#ifndef PERL_ARGS_ASSERT_CROAK_XS_USAGE
#define PERL_ARGS_ASSERT_CROAK_XS_USAGE assert(cv); assert(params)

/* prototype to pass -Wmissing-prototypes */
STATIC void
S_croak_xs_usage(const CV *const cv, const char *const params);

STATIC void
S_croak_xs_usage(const CV *const cv, const char *const params)
{
    const GV *const gv = CvGV(cv);

    PERL_ARGS_ASSERT_CROAK_XS_USAGE;

    if (gv) {
        const char *const gvname = GvNAME(gv);
        const HV *const stash = GvSTASH(gv);
        const char *const hvname = stash ? HvNAME(stash) : NULL;

        if (hvname)
	    Perl_croak_nocontext("Usage: %s::%s(%s)", hvname, gvname, params);
        else
	    Perl_croak_nocontext("Usage: %s(%s)", gvname, params);
    } else {
        /* Pants. I don't think that it should be possible to get here. */
	Perl_croak_nocontext("Usage: CODE(0x%" UVxf ")(%s)", PTR2UV(cv), params);
    }
}
#undef  PERL_ARGS_ASSERT_CROAK_XS_USAGE

#define croak_xs_usage        S_croak_xs_usage

#endif

/* NOTE: the prototype of newXSproto() is different in versions of perls,
 * so we define a portable version of newXSproto()
 */
#ifdef newXS_flags
#define newXSproto_portable(name, c_impl, file, proto) newXS_flags(name, c_impl, file, proto, 0)
#else
#define newXSproto_portable(name, c_impl, file, proto) (PL_Sv=(SV*)newXS(name, c_impl, file), sv_setpv(PL_Sv, proto), (CV*)PL_Sv)
#endif /* !defined(newXS_flags) */

#if PERL_VERSION_LE(5, 21, 5)
#  define newXS_deffile(a,b) Perl_newXS(aTHX_ a,b,file)
#else
#  define newXS_deffile(a,b) Perl_newXS_deffile(aTHX_ a,b)
#endif

#line 206 "Themes.c"

XS_EUPXS(XS_Irssi_current_theme); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi_current_theme)
{
    dVAR; dXSARGS;
    if (items != 0)
       croak_xs_usage(cv,  "");
    {
	Irssi__UI__Theme	RETVAL;
#line 58 "Themes.xs"
	RETVAL = current_theme;
#line 218 "Themes.c"
	{
	    SV * RETVALSV;
	    RETVALSV = plain_bless(RETVAL, "Irssi::UI::Theme");
	    RETVALSV = sv_2mortal(RETVALSV);
	    ST(0) = RETVALSV;
	}
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Irssi_EXPAND_FLAG_IGNORE_REPLACES); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi_EXPAND_FLAG_IGNORE_REPLACES)
{
    dVAR; dXSARGS;
    if (items != 0)
       croak_xs_usage(cv,  "");
    {
	int	RETVAL;
	dXSTARG;
#line 65 "Themes.xs"
	RETVAL = EXPAND_FLAG_IGNORE_REPLACES;
#line 241 "Themes.c"
	XSprePUSH; PUSHi((IV)RETVAL);
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Irssi_EXPAND_FLAG_IGNORE_EMPTY); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi_EXPAND_FLAG_IGNORE_EMPTY)
{
    dVAR; dXSARGS;
    if (items != 0)
       croak_xs_usage(cv,  "");
    {
	int	RETVAL;
	dXSTARG;
#line 72 "Themes.xs"
	RETVAL = EXPAND_FLAG_IGNORE_EMPTY;
#line 259 "Themes.c"
	XSprePUSH; PUSHi((IV)RETVAL);
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Irssi_EXPAND_FLAG_RECURSIVE_MASK); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi_EXPAND_FLAG_RECURSIVE_MASK)
{
    dVAR; dXSARGS;
    if (items != 0)
       croak_xs_usage(cv,  "");
    {
	int	RETVAL;
	dXSTARG;
#line 79 "Themes.xs"
	RETVAL = EXPAND_FLAG_RECURSIVE_MASK;
#line 277 "Themes.c"
	XSprePUSH; PUSHi((IV)RETVAL);
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Irssi_theme_register); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi_theme_register)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "formats");
    {
	SV *	formats = ST(0)
;
#line 87 "Themes.xs"
	AV *av;
	FORMAT_REC *formatrecs;
	char *key, *value;
	int len, n, fpos;
#line 298 "Themes.c"
#line 93 "Themes.xs"
        if (!SvROK(formats))
        	croak("formats is not a reference");

	av = (AV *) SvRV(formats);
	if (SvTYPE(av) != SVt_PVAV)
        	croak("formats is not a reference to a list");

	len = av_len(av)+1;
	if (len == 0 || (len & 1) != 0)
        	croak("formats list is invalid - not divisible by 2 (%d)", len);

	formatrecs = g_new0(FORMAT_REC, len/2+2);
	formatrecs[0].tag = g_strdup(perl_get_package());
	formatrecs[0].def = g_strdup("Perl script");

        for (fpos = 1, n = 0; n < len; n++, fpos++) {
		key = SvPV_nolen(*av_fetch(av, n, 0)); n++;
		value = SvPV_nolen(*av_fetch(av, n, 0));

		formatrecs[fpos].tag = g_strdup(key);
		formatrecs[fpos].def = g_strdup(value);
		formatrecs[fpos].params = MAX_FORMAT_PARAMS;
	}

	theme_register_module(perl_get_package(), formatrecs);
#line 325 "Themes.c"
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_Irssi_printformat); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi_printformat)
{
    dVAR; dXSARGS;
    if (items < 2)
       croak_xs_usage(cv,  "level, format, ...");
    {
	int	level = (int)SvIV(ST(0))
;
	char *	format = (char *)SvPV_nolen(ST(1))
;
#line 124 "Themes.xs"
	TEXT_DEST_REC dest;
	char *arglist[MAX_FORMAT_PARAMS+1];
	int n;
#line 346 "Themes.c"
#line 128 "Themes.xs"
	format_create_dest(&dest, NULL, NULL, level, NULL);
	memset(arglist, 0, sizeof(arglist));
	for (n = 2; n < items && n < MAX_FORMAT_PARAMS+2; n++) {
		arglist[n-2] = SvPV_nolen(ST(n));
	}

        printformat_perl(&dest, format, arglist);
#line 355 "Themes.c"
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_Irssi_abstracts_register); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi_abstracts_register)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "abstracts");
    {
	SV *	abstracts = ST(0)
;
#line 140 "Themes.xs"
	AV *av;
	char *key, *value;
	int i, len;
#line 374 "Themes.c"
#line 144 "Themes.xs"
        if (!SvROK(abstracts))
        	croak("abstracts is not a reference to list");
	av = (AV *) SvRV(abstracts);
	len = av_len(av)+1;
	if (len == 0 || (len & 1) != 0)
        	croak("abstracts list is invalid - not divisible by 2 (%d)", len);

        for (i = 0; i < len; i++) {
		key = SvPV_nolen(*av_fetch(av, i, 0)); i++;
		value = SvPV_nolen(*av_fetch(av, i, 0));

		theme_set_default_abstract(key, value);
	}
	themes_reload();
#line 390 "Themes.c"
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_Irssi_themes_reload); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi_themes_reload)
{
    dVAR; dXSARGS;
    if (items != 0)
       croak_xs_usage(cv,  "");
    {

	themes_reload();
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_Irssi__Server_printformat); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi__Server_printformat)
{
    dVAR; dXSARGS;
    if (items < 4)
       croak_xs_usage(cv,  "server, target, level, format, ...");
    {
	Irssi__Server	server = irssi_ref_object(ST(0))
;
	char *	target = (char *)SvPV_nolen(ST(1))
;
	int	level = (int)SvIV(ST(2))
;
	char *	format = (char *)SvPV_nolen(ST(3))
;
#line 173 "Themes.xs"
	TEXT_DEST_REC dest;
	char *arglist[MAX_FORMAT_PARAMS+1];
	int n;
#line 429 "Themes.c"
#line 177 "Themes.xs"
	format_create_dest(&dest, server, target, level, NULL);
	memset(arglist, 0, sizeof(arglist));
	for (n = 4; n < items && n < MAX_FORMAT_PARAMS+4; n++) {
		arglist[n-4] = SvPV_nolen(ST(n));
	}

        printformat_perl(&dest, format, arglist);
#line 438 "Themes.c"
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_Irssi__UI__Window_printformat); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi__UI__Window_printformat)
{
    dVAR; dXSARGS;
    if (items < 3)
       croak_xs_usage(cv,  "window, level, format, ...");
    {
	Irssi__UI__Window	window = irssi_ref_object(ST(0))
;
	int	level = (int)SvIV(ST(1))
;
	char *	format = (char *)SvPV_nolen(ST(2))
;
#line 195 "Themes.xs"
	TEXT_DEST_REC dest;
	char *arglist[MAX_FORMAT_PARAMS+1];
	int n;
#line 461 "Themes.c"
#line 199 "Themes.xs"
	format_create_dest(&dest, NULL, NULL, level, window);
	memset(arglist, 0, sizeof(arglist));
	for (n = 3; n < items && n < MAX_FORMAT_PARAMS+3; n++) {
		arglist[n-3] = SvPV_nolen(ST(n));
	}

        printformat_perl(&dest, format, arglist);
#line 470 "Themes.c"
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_Irssi__Windowitem_printformat); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi__Windowitem_printformat)
{
    dVAR; dXSARGS;
    if (items < 3)
       croak_xs_usage(cv,  "item, level, format, ...");
    {
	Irssi__Windowitem	item = irssi_ref_object(ST(0))
;
	int	level = (int)SvIV(ST(1))
;
	char *	format = (char *)SvPV_nolen(ST(2))
;
#line 217 "Themes.xs"
	TEXT_DEST_REC dest;
	char *arglist[MAX_FORMAT_PARAMS+1];
	int n;
#line 493 "Themes.c"
#line 221 "Themes.xs"
	format_create_dest(&dest, item->server, item->visible_name, level, NULL);
	memset(arglist, 0, sizeof(arglist));
	for (n = 3; n < items && n < MAX_FORMAT_PARAMS+3; n++) {
		arglist[n-3] = SvPV_nolen(ST(n));
	}

        printformat_perl(&dest, format, arglist);
#line 502 "Themes.c"
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_Irssi__UI__Theme_format_expand); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi__UI__Theme_format_expand)
{
    dVAR; dXSARGS;
    if (items < 2 || items > 3)
       croak_xs_usage(cv,  "theme, format, flags=0");
    PERL_UNUSED_VAR(ax); /* -Wall */
    SP -= items;
    {
	Irssi__UI__Theme	theme = irssi_ref_object(ST(0))
;
	char *	format = (char *)SvPV_nolen(ST(1))
;
	int	flags;
#line 239 "Themes.xs"
	char *ret;
#line 524 "Themes.c"

	if (items < 3)
	    flags = 0;
	else {
	    flags = (int)SvIV(ST(2))
;
	}
#line 241 "Themes.xs"
	if (flags == 0) {
		ret = theme_format_expand(theme, format);
	} else {
		theme_rm_col reset;
		strcpy(reset.m, "n");
		ret = theme_format_expand_data(theme, (const char **) &format,
					       reset, reset, NULL, NULL,
					       EXPAND_FLAG_ROOT | flags);
	}
	XPUSHs(sv_2mortal(new_pv(ret)));
	g_free_not_null(ret);
#line 544 "Themes.c"
	PUTBACK;
	return;
    }
}


XS_EUPXS(XS_Irssi__UI__Theme_get_format); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi__UI__Theme_get_format)
{
    dVAR; dXSARGS;
    if (items != 3)
       croak_xs_usage(cv,  "theme, module, tag");
    {
	Irssi__UI__Theme	theme = irssi_ref_object(ST(0))
;
	char *	module = (char *)SvPV_nolen(ST(1))
;
	char *	tag = (char *)SvPV_nolen(ST(2))
;
#line 259 "Themes.xs"
	MODULE_THEME_REC *modtheme;
	FORMAT_REC *formats;
	int i;
#line 568 "Themes.c"
	char *	RETVAL;
	dXSTARG;
#line 263 "Themes.xs"
	formats = g_hash_table_lookup(default_formats, module);
	if (formats == NULL)
		croak("Unknown module: %s", module);

	for (i = 0; formats[i].def != NULL; i++) {
		if (formats[i].tag != NULL &&
		    g_ascii_strcasecmp(formats[i].tag, tag) == 0)
			break;
	}

	if (formats[i].def == NULL)
		croak("Unknown format tag: %s", tag);

	modtheme = g_hash_table_lookup(theme->modules, module);
	RETVAL = modtheme == NULL ? NULL : modtheme->formats[i];
	if (RETVAL == NULL)
		RETVAL = formats[i].def;
#line 589 "Themes.c"
	sv_setpv(TARG, RETVAL); XSprePUSH; PUSHTARG;
    }
    XSRETURN(1);
}

#ifdef __cplusplus
extern "C"
#endif
XS_EXTERNAL(boot_Irssi__UI__Themes); /* prototype to pass -Wmissing-prototypes */
XS_EXTERNAL(boot_Irssi__UI__Themes)
{
#if PERL_VERSION_LE(5, 21, 5)
    dVAR; dXSARGS;
#else
    dVAR; dXSBOOTARGSXSAPIVERCHK;
#endif
#if (PERL_REVISION == 5 && PERL_VERSION < 9)
    char* file = __FILE__;
#else
    const char* file = __FILE__;
#endif

    PERL_UNUSED_VAR(file);

    PERL_UNUSED_VAR(cv); /* -W */
    PERL_UNUSED_VAR(items); /* -W */
#if PERL_VERSION_LE(5, 21, 5)
    XS_VERSION_BOOTCHECK;
#  ifdef XS_APIVERSION_BOOTCHECK
    XS_APIVERSION_BOOTCHECK;
#  endif
#endif

        (void)newXSproto_portable("Irssi::current_theme", XS_Irssi_current_theme, file, "");
        (void)newXSproto_portable("Irssi::EXPAND_FLAG_IGNORE_REPLACES", XS_Irssi_EXPAND_FLAG_IGNORE_REPLACES, file, "");
        (void)newXSproto_portable("Irssi::EXPAND_FLAG_IGNORE_EMPTY", XS_Irssi_EXPAND_FLAG_IGNORE_EMPTY, file, "");
        (void)newXSproto_portable("Irssi::EXPAND_FLAG_RECURSIVE_MASK", XS_Irssi_EXPAND_FLAG_RECURSIVE_MASK, file, "");
        (void)newXSproto_portable("Irssi::theme_register", XS_Irssi_theme_register, file, "$");
        (void)newXSproto_portable("Irssi::printformat", XS_Irssi_printformat, file, "$$;@");
        (void)newXSproto_portable("Irssi::abstracts_register", XS_Irssi_abstracts_register, file, "$");
        (void)newXSproto_portable("Irssi::themes_reload", XS_Irssi_themes_reload, file, "");
        (void)newXSproto_portable("Irssi::Server::printformat", XS_Irssi__Server_printformat, file, "$$$$;@");
        (void)newXSproto_portable("Irssi::UI::Window::printformat", XS_Irssi__UI__Window_printformat, file, "$$$;@");
        (void)newXSproto_portable("Irssi::Windowitem::printformat", XS_Irssi__Windowitem_printformat, file, "$$$;@");
        (void)newXSproto_portable("Irssi::UI::Theme::format_expand", XS_Irssi__UI__Theme_format_expand, file, "$$;$");
        (void)newXSproto_portable("Irssi::UI::Theme::get_format", XS_Irssi__UI__Theme_get_format, file, "$$$");
#if PERL_VERSION_LE(5, 21, 5)
#  if PERL_VERSION_GE(5, 9, 0)
    if (PL_unitcheckav)
        call_list(PL_scopestack_ix, PL_unitcheckav);
#  endif
    XSRETURN_YES;
#else
    Perl_xs_boot_epilog(aTHX_ ax);
#endif
}

