aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormitchell <70453897+667e-11@users.noreply.github.com>2008-02-03 16:45:32 -0500
committermitchell <70453897+667e-11@users.noreply.github.com>2008-02-03 16:45:32 -0500
commit655d3222b8e242bcdb96e9458dc311aee1a658e5 (patch)
tree9f1d643b137e2ea967ffad5ce16aad7fa8f9f1bb
parentbc000dc1ea565efb81a2a3d4b7e6aa41c376c0fb (diff)
downloadtextadept-655d3222b8e242bcdb96e9458dc311aee1a658e5.tar.gz
textadept-655d3222b8e242bcdb96e9458dc311aee1a658e5.zip
Added textadept.reset() function for resetting the Lua state.
The l_init function takes an additional reinit boolean as a result. When resetting, the package.loaded and _G tables are cleared. Since the textadept.buffers, textadept.views, and arg tables were originally in _G, they were moved to the LUA_REGISTRYINDEX table so as not to be lost. They are still available in _G, but as links to the tables in LUA_REGISTRYINDEX. textadept.reset() sets a global RESETTING boolean to true when init.lua is re-run so things like reloading a session or reloading files from the command line do not occur. The boolean is set to nil afterwards.
-rw-r--r--init.lua30
-rw-r--r--src/lua_interface.c68
-rw-r--r--src/textadept.c2
-rw-r--r--src/textadept.h2
4 files changed, 75 insertions, 27 deletions
diff --git a/init.lua b/init.lua
index 44f7b13f..1a380f55 100644
--- a/init.lua
+++ b/init.lua
@@ -14,20 +14,22 @@ require 'textadept'
require 'ext/key_commands'
--- process command line arguments
-local textadept = textadept
-if #arg == 0 then
- textadept.io.load_session()
-else
- local base_dir = arg[0]:match('^.+/') or ''
- local filepath
- for _, filename in ipairs(arg) do
- if not filename:match('^~?/') then
- textadept.io.open(base_dir..filename)
- else
- textadept.io.open(filename)
+if not RESETTING then
+ -- process command line arguments
+ local textadept = textadept
+ if #arg == 0 then
+ textadept.io.load_session()
+ else
+ local base_dir = arg[0]:match('^.+/') or ''
+ local filepath
+ for _, filename in ipairs(arg) do
+ if not filename:match('^~?/') then
+ textadept.io.open(base_dir..filename)
+ else
+ textadept.io.open(filename)
+ end
end
+ textadept.pm.entry_text = 'buffers'
+ textadept.pm.activate()
end
- textadept.pm.entry_text = 'buffers'
- textadept.pm.activate()
end
diff --git a/src/lua_interface.c b/src/lua_interface.c
index ad203441..862e2b2d 100644
--- a/src/lua_interface.c
+++ b/src/lua_interface.c
@@ -23,6 +23,8 @@ 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),
@@ -42,6 +44,7 @@ LF l_cf_ta_buffer_new(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);
@@ -49,18 +52,29 @@ 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) {
- lua = lua_open();
- luaL_openlibs(lua);
- lua_newtable(lua);
- for (int i = 0; i < argc; i++) {
- lua_pushstring(lua, argv[i]); lua_rawseti(lua, -2, i);
+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);
}
- lua_setglobal(lua, "arg");
+ luaL_openlibs(lua);
lua_newtable(lua);
- lua_newtable(lua); lua_setfield(lua, -2, "buffers");
- lua_newtable(lua); lua_setfield(lua, -2, "views");
+ lua_getfield(lua, LUA_REGISTRYINDEX, "buffers");
+ lua_setfield(lua, -2, "buffers");
+ lua_getfield(lua, LUA_REGISTRYINDEX, "views");
+ lua_setfield(lua, -2, "views");
lua_newtable(lua);
l_cfunc(lua, l_cf_pm_focus, "focus");
l_cfunc(lua, l_cf_pm_clear, "clear");
@@ -77,8 +91,11 @@ void l_init(int argc, char **argv) {
l_cfunc(lua, l_cf_ta_focus_command, "focus_command");
l_cfunc(lua, l_cf_ta_gtkmenu, "gtkmenu");
l_cfunc(lua, l_cf_ta_popupmenu, "popupmenu");
+ l_cfunc(lua, l_cf_ta_reset, "reset");
l_mt(lua, "_textadept_mt", l_ta_mt_index, l_ta_mt_newindex);
lua_setglobal(lua, "textadept");
+
+ lua_getfield(lua, LUA_REGISTRYINDEX, "arg"); lua_setglobal(lua, "arg");
lua_pushstring(lua, textadept_home); lua_setglobal(lua, "_HOME");
l_load_script("core/init.lua");
@@ -104,6 +121,13 @@ void l_ta_set(LS *lua, const char *k) {
lua_pop(lua, 2); // value and textadept
}
+// value is at stack top
+void l_reg_set(LS *lua, const char *k) {
+ lua_setfield(lua, LUA_REGISTRYINDEX, k);
+ lua_getfield(lua, LUA_REGISTRYINDEX, k);
+ l_ta_set(lua, k);
+}
+
/** Checks for a view and returns the GtkWidget associated with it. */
static GtkWidget* l_checkview(LS *lua, int narg, const char *errstr=NULL) {
if (lua_type(lua, narg) == LUA_TTABLE) {
@@ -138,7 +162,7 @@ void l_remove_scintilla_window(GtkWidget *editor) {
while (lua_next(lua, -2))
editor != l_checkview(lua, -1) ? l_append(lua, -4) : lua_pop(lua, 1);
lua_pop(lua, 1); // views
- l_ta_set(lua, "views");
+ l_reg_set(lua, "views");
}
void l_goto_scintilla_window(GtkWidget *editor, int n, bool absolute) {
@@ -219,7 +243,7 @@ void l_remove_scintilla_buffer(sptr_t doc) {
while (lua_next(lua, -2))
doc != l_checkdocpointer(lua, -1) ? l_append(lua, -4) : lua_pop(lua, 1);
lua_pop(lua, 1); // buffers
- l_ta_set(lua, "buffers");
+ l_reg_set(lua, "buffers");
}
unsigned int l_get_docpointer_index(sptr_t doc) {
@@ -306,6 +330,15 @@ void l_close() {
// Utility Functions
+static void clear_table(LS *lua, int abs_index) {
+ lua_pushnil(lua);
+ while (lua_next(lua, abs_index)) {
+ lua_pop(lua, 1); // value
+ lua_pushnil(lua); lua_rawset(lua, abs_index);
+ lua_pushnil(lua); // get 'new' first key
+ }
+}
+
static void l_check_focused_buffer(LS *lua, int narg) {
ScintillaObject *sci = SCINTILLA(focused_editor);
sptr_t cur_doc = SS(sci, SCI_GETDOCPOINTER);
@@ -983,6 +1016,19 @@ LF l_cf_ta_popupmenu(LS *lua) {
return 0;
}
+LF l_cf_ta_reset(LS *lua) {
+ l_handle_event("resetting");
+ lua_getglobal(lua, "buffer"); lua_setfield(lua, LUA_REGISTRYINDEX, "buffer");
+ lua_getglobal(lua, "view"); lua_setfield(lua, LUA_REGISTRYINDEX, "view");
+ l_init(0, NULL, true);
+ lua_pushboolean(lua, true); lua_setglobal(lua, "RESETTING");
+ l_load_script("init.lua");
+ lua_pushnil(lua); lua_setglobal(lua, "RESETTING");
+ lua_getfield(lua, LUA_REGISTRYINDEX, "buffer"); lua_setglobal(lua, "buffer");
+ lua_getfield(lua, LUA_REGISTRYINDEX, "view"); lua_setglobal(lua, "view");
+ return 0;
+}
+
LF l_cf_pm_focus(LS *) { pm_toggle_focus(); return 0; }
LF l_cf_pm_clear(LS *) { gtk_tree_store_clear(pm_store); return 0; }
LF l_cf_pm_activate(LS *) {
diff --git a/src/textadept.c b/src/textadept.c
index 6a5122f9..adeb89cb 100644
--- a/src/textadept.c
+++ b/src/textadept.c
@@ -20,7 +20,7 @@ static bool w_exit(GtkWidget*, GdkEventAny*, gpointer);
int main(int argc, char **argv) {
gtk_init(&argc, &argv);
- l_init(argc, argv);
+ l_init(argc, argv, false);
create_ui();
l_load_script("init.lua");
gtk_main();
diff --git a/src/textadept.h b/src/textadept.h
index af53d0f9..5a6309a3 100644
--- a/src/textadept.h
+++ b/src/textadept.h
@@ -54,7 +54,7 @@ void set_docstatusbar_text(const char *text);
void command_toggle_focus();
// lua_interface.c
-void l_init(int argc, char **argv);
+void l_init(int argc, char **argv, bool reinit);
void l_close();
void l_load_script(const char *script_file);
void l_add_scintilla_window(GtkWidget *editor);