/*
 * tclrtext.c --
 *
 *	This file implements a Tcl interface to the rtext (revised text) widget.
 *
 * Copyright © 1999 Scriptics Corporation.
 * Copyright © 2003 ActiveState Corporation.
 * Copyright © 2024 François Vogel.
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 */

#include <tcl.h>
#include "rtextUuid.h"
#include "tkText.h"


/*
 *----------------------------------------------------------------------
 *
 * Rtext_Init --
 *
 *	Initialize the rtext package. The string "Rtext" in the
 *	function name matches the PACKAGE declaration at the top of
 *	configure.ac.
 *
 * Results:
 *	A standard Tcl result
 *
 * Side effects:
 *	The rtext package is created.
 *	One new command "rtext" is added to the Tcl interpreter.
 *
 *----------------------------------------------------------------------
 */

#ifndef STRINGIFY
#  define STRINGIFY(x) STRINGIFY1(x)
#  define STRINGIFY1(x) #x
#endif

#ifdef __cplusplus
extern "C" {
#endif  /* __cplusplus */
DLLEXPORT int
Rtext_Init(
    Tcl_Interp* interp)		/* Tcl interpreter */
{
    Tcl_CmdInfo info;
    static char initScript[] = "\
        tcl_findLibrary rtext " PACKAGE_VERSION " " PACKAGE_VERSION " text.tcl RTEXT_LIBRARY rtext_library";

    /*
     * Require any version at least equal to 9.0 and strictly less
     * than the next (10.0) major, leaving support for Tcl 10
     * undetermined.
     * Actually we're requiring Tk 9.0.1 as minimum version, so that
     * TIP #706 (Expose three Tk "In Context" functions via stubs table)
     * is available, which allows building rtext on macOS with aqua.
     */
    if (Tcl_InitStubs(interp, "9.0-10.0", 0) == NULL) {
	return TCL_ERROR;
    }
    if (Tk_InitStubs(interp, "9.0.1-10.0", 0) == NULL) {
	return TCL_ERROR;
    }

    if (Tcl_GetCommandInfo(interp, "::tcl::build-info", &info)) {
	Tcl_CreateObjCommand(interp, "::rtext::build-info",
		info.objProc, (void *)(
		    PACKAGE_VERSION "+" STRINGIFY(RTEXT_VERSION_UUID)
#if defined(__clang__) && defined(__clang_major__)
			    ".clang-" STRINGIFY(__clang_major__)
#if __clang_minor__ < 10
			    "0"
#endif
			    STRINGIFY(__clang_minor__)
#endif
#if defined(__cplusplus) && !defined(__OBJC__)
			    ".cplusplus"
#endif
#ifndef NDEBUG
			    ".debug"
#endif
#if !defined(__clang__) && !defined(__INTEL_COMPILER) && defined(__GNUC__)
			    ".gcc-" STRINGIFY(__GNUC__)
#if __GNUC_MINOR__ < 10
			    "0"
#endif
			    STRINGIFY(__GNUC_MINOR__)
#endif
#ifdef __INTEL_COMPILER
			    ".icc-" STRINGIFY(__INTEL_COMPILER)
#endif
#ifdef TCL_MEM_DEBUG
			    ".memdebug"
#endif
#if defined(_MSC_VER)
			    ".msvc-" STRINGIFY(_MSC_VER)
#endif
#ifdef USE_NMAKE
			    ".nmake"
#endif
#ifndef TCL_CFG_OPTIMIZED
			    ".no-optimize"
#endif
#ifdef __OBJC__
			    ".objective-c"
#if defined(__cplusplus)
			    "plusplus"
#endif
#endif
#ifdef TCL_CFG_PROFILED
			    ".profile"
#endif
#ifdef PURIFY
			    ".purify"
#endif
#ifdef STATIC_BUILD
			    ".static"
#endif
		), NULL);
    }

    /* Provide the current package */

    if (Tcl_PkgProvideEx(interp, PACKAGE_NAME, PACKAGE_VERSION, NULL) != TCL_OK) {
	return TCL_ERROR;
    }

    if (Tcl_EvalEx(interp, initScript, TCL_INDEX_NONE, TCL_EVAL_GLOBAL) != TCL_OK) {
	return TCL_ERROR;
    }

    Tcl_CreateObjCommand(interp, "text", (Tcl_ObjCmdProc *)Tk_TextObjCmd, NULL, NULL);

    return TCL_OK;
}
#ifdef __cplusplus
}
#endif  /* __cplusplus */
