<commit_before>// Copyright 2007 <NAME> mitchell<att>cal<EMAIL>. See LICENSE.

#include "textadept.h"

#define LS lua_State
#define LF static int
#define streq(s1, s2) strcmp(s1, s2) == 0
#define l_insert(l, i) lua_insert(l, i < 0 ? lua_gettop(l) + i : i)
#define l_append(l, i) lua_rawseti(l, i, lua_objlen(l, i) + 1)
#define l_cfunc(l, f, k) { lua_pushcfunction(l, f); lua_setfield(l, -2, k); }
#define l_todocpointer(l, i) static_cast<sptr_t>(lua_tonumber(l, i))
#define l_togtkwidget(l, i) reinterpret_cast<GtkWidget*>(lua_touserdata(l, i))
#define l_mt(l, k, i, ni) { \
  if (luaL_newmetatable(l, k)) { \
    l_cfunc(l, i, "__index"); \
    l_cfunc(l, ni, "__newindex"); \
  } lua_setmetatable(l, -2); }

LS *lua;
bool closing = false;

static int // parameter/return types
  tVOID = 0, /*tINT = 1,*/ tLENGTH = 2, /*tPOSITION = 3,*/ /*tCOLOUR = 4,*/
  tBOOL = 5, tKEYMOD = 6, tSTRING = 7, tSTRINGRESULT = 8;

static void clear_table(LS *lua, int index);

LF l_buffer_mt_index(LS *lua), l_buffer_mt_newindex(LS *lua),
   l_bufferp_mt_index(LS *lua), l_bufferp_mt_newindex(LS *lua),
   l_view_mt_index(LS *lua), l_view_mt_newindex(LS *lua),
   l_ta_mt_index(LS *lua), l_ta_mt_newindex(LS *lua),
   l_pm_mt_index(LS *lua), l_pm_mt_newindex(LS *lua),
   l_find_mt_index(LS *lua), l_find_mt_newindex(LS *lua);

LF l_cf_ta_buffer_new(LS *lua),
   l_cf_buffer_delete(LS *lua),
   l_cf_buffer_find(LS *lua),
   l_cf_buffer_text_range(LS *lua),
   l_cf_view_focus(LS *lua),
   l_cf_view_split(LS *lua), l_cf_view_unsplit(LS *lua),
   l_cf_ta_get_split_table(LS *lua),
   l_cf_ta_focus_command(LS *lua),
   l_cf_ta_goto_window(LS *lua),
   l_cf_view_goto_buffer(LS *lua),
   l_cf_ta_gtkmenu(LS *lua),
   l_cf_ta_popupmenu(LS *lua),
   l_cf_ta_reset(LS *lua),
   l_cf_pm_focus(LS *lua), l_cf_pm_clear(LS *lua), l_cf_pm_activate(LS *lua),
   l_cf_find_focus(LS *lua);

const char
  *views_dne = "textadept.views doesn't exist or was overridden.",
  *buffers_dne = "textadept.buffers doesn't exist or was overridden.";

void l_init(int argc, char **argv, bool reinit) {
  if (!reinit) {
    lua = lua_open();
    lua_newtable(lua);
    for (int i = 0; i < argc; i++) {
      lua_pushstring(lua, argv[i]); lua_rawseti(lua, -2, i);
    }
    lua_setfield(lua, LUA_REGISTRYINDEX, "arg");
    lua_newtable(lua); lua_setfield(lua, LUA_REGISTRYINDEX, "buffers");
    lua_newtable(lua); lua_setfield(lua, LUA_REGISTRYINDEX, "views");
  } else { // clear package.loaded and _G
    lua_getglobal(lua, "package"); lua_getfield(lua, -1, "loaded");
    clear_table(lua, lua_gettop(lua));
    lua_pop(lua, 2); // package and package.loaded
    clear_table(lua, LUA_GLOBALSINDEX);
  }
  luaL_openlibs(lua);<commit_msg>Fixed typo; src/lua_interface.c<commit_after>// Copyright 2007 <NAME> mitchell<att>caladbolg.net. See LICENSE.

#include "textadept.h"

#define LS lua_State
#define LF static int
#define streq(s1, s2) strcmp(s1, s2) == 0
#define l_insert(l, i) lua_insert(l, i < 0 ? lua_gettop(l) + i : i)
#define l_append(l, i) lua_rawseti(l, i, lua_objlen(l, i) + 1)
#define l_cfunc(l, f, k) { lua_pushcfunction(l, f); lua_setfield(l, -2, k); }
#define l_todocpointer(l, i) static_cast<sptr_t>(lua_tonumber(l, i))
#define l_togtkwidget(l, i) reinterpret_cast<GtkWidget*>(lua_touserdata(l, i))
#define l_mt(l, k, i, ni) { \
  if (luaL_newmetatable(l, k)) { \
    l_cfunc(l, i, "__index"); \
    l_cfunc(l, ni, "__newindex"); \
  } lua_setmetatable(l, -2); }

LS *lua;
bool closing = false;

static int // parameter/return types
  tVOID = 0, /*tINT = 1,*/ tLENGTH = 2, /*tPOSITION = 3,*/ /*tCOLOUR = 4,*/
  tBOOL = 5, tKEYMOD = 6, tSTRING = 7, tSTRINGRESULT = 8;

static void clear_table(LS *lua, int index);

LF l_buffer_mt_index(LS *lua), l_buffer_mt_newindex(LS *lua),
   l_bufferp_mt_index(LS *lua), l_bufferp_mt_newindex(LS *lua),
   l_view_mt_index(LS *lua), l_view_mt_newindex(LS *lua),
   l_ta_mt_index(LS *lua), l_ta_mt_newindex(LS *lua),
   l_pm_mt_index(LS *lua), l_pm_mt_newindex(LS *lua),
   l_find_mt_index(LS *lua), l_find_mt_newindex(LS *lua);

LF l_cf_ta_buffer_new(LS *lua),
   l_cf_buffer_delete(LS *lua),
   l_cf_buffer_find(LS *lua),
   l_cf_buffer_text_range(LS *lua),
   l_cf_view_focus(LS *lua),
   l_cf_view_split(LS *lua), l_cf_view_unsplit(LS *lua),
   l_cf_ta_get_split_table(LS *lua),
   l_cf_ta_focus_command(LS *lua),
   l_cf_ta_goto_window(LS *lua),
   l_cf_view_goto_buffer(LS *lua),
   l_cf_ta_gtkmenu(LS *lua),
   l_cf_ta_popupmenu(LS *lua),
   l_cf_ta_reset(LS *lua),
   l_cf_pm_focus(LS *lua), l_cf_pm_clear(LS *lua), l_cf_pm_activate(LS *lua),
   l_cf_find_focus(LS *lua);

const char
  *views_dne = "textadept.views doesn't exist or was overwritten.",
  *buffers_dne = "textadept.buffers doesn't exist or was overwritten.";

void l_init(int argc, char **argv, bool reinit) {
  if (!reinit) {
    lua = lua_open();
    lua_newtable(lua);
    for (int i = 0; i < argc; i++) {
      lua_pushstring(lua, argv[i]); lua_rawseti(lua, -2, i);
    }
    lua_setfield(lua, LUA_REGISTRYINDEX, "arg");
    lua_newtable(lua); lua_setfield(lua, LUA_REGISTRYINDEX, "buffers");
    lua_newtable(lua); lua_setfield(lua, LUA_REGISTRYINDEX, "views");
  } else { // clear package.loaded and _G
    lua_getglobal(lua, "package"); lua_getfield(lua, -1, "loaded");
    clear_table(lua, lua_gettop(lua));
    lua_pop(lua, 2); // package and package.loaded
    clear_table(lua, LUA_GLOBALSINDEX);
  }
  luaL_openlibs(lua);
{'fraction_non_alphanumeric': 0.15401662049861495, 'fraction_numerical': 0.009602954755309326, 'mean_word_length': 4.8997821350762525, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1415325', 'n_tokens_mistral': 2443, 'n_tokens_neox': 2338, 'n_words': 481}