diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index 524ff67..066b1f3 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -378,10 +378,11 @@ void HandleActiveWindowBindings(Window *window) { if (window->execute_line) { if (Press(KEY_ENTER)) { + Scratch scratch; Int line = PosToLine(*buffer, GetFront(view.carets[0])); Range range = GetLineRange(*buffer, line); String16 string = GetString(*buffer, range); - String16 eval_result = EvalString(string); + String16 eval_result = EvalString(scratch, string); if (Ctrl()) { Command_SelectEntireBuffer(&view); diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index 28c8f95..5a99232 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -10,45 +10,48 @@ int LuaOpenFile(lua_State *L) { } int LuaListOpenBuffers(lua_State *L) { - Scratch scratch; - Array strings = {scratch}; + Scratch scratch; + Array strings = {scratch}; For(Buffers) { String string = Format(scratch, "open \"%.*s\"", FmtString(it.name)); - Add(&strings, ToString16(scratch, string)); + Add(&strings, string); } - LuaCommandResult = Merge(scratch, strings, L"\n"); - return 0; + String result = Merge(scratch, strings, "\n"); + lua_pushlstring(LuaState, result.data, result.len); + return 1; } int LuaListViews(lua_State *L) { - Scratch scratch; - Array strings = {scratch}; + Scratch scratch; + Array strings = {scratch}; For(Views) { Buffer *buffer = GetBuffer(it.buffer_id); String string = Format(scratch, "view = %lld buffer = %lld name = %.*s", (long long)it.id.id, (long long)buffer->id.id, FmtString(buffer->name)); - Add(&strings, ToString16(scratch, string)); + Add(&strings, string); } - LuaCommandResult = Merge(scratch, strings, L"\n"); - return 0; + String result = Merge(scratch, strings, "\n"); + lua_pushlstring(LuaState, result.data, result.len); + return 1; } int LuaListWindows(lua_State *L) { - Scratch scratch; - Array strings = {scratch}; + Scratch scratch; + Array strings = {scratch}; For(Windows) { View *view = GetActiveView(&it); Buffer *buffer = GetBuffer(view->buffer_id); String string = Format(scratch, "window = %lld active_view = %lld buffer_name = %.*s", (long long)it.id.id, (long long)it.active_view.id, FmtString(buffer->name)); - Add(&strings, ToString16(scratch, string)); + Add(&strings, string); ForItem(child_view_id, it.views) { View *child_view = GetView(child_view_id); Buffer *child_buffer = GetBuffer(child_view->buffer_id); String child_string = Format(scratch, " view = %lld buffer = %lld name = %.*s", (long long)child_view->id.id, (long long)child_buffer->id.id, FmtString(child_buffer->name)); - Add(&strings, ToString16(scratch, child_string)); + Add(&strings, child_string); } } - LuaCommandResult = Merge(scratch, strings, L"\n"); - return 0; + String result = Merge(scratch, strings, "\n"); + lua_pushlstring(LuaState, result.data, result.len); + return 1; } void InitLua() { @@ -75,14 +78,19 @@ void SetInfoBarErrorMessage(String string) { if (string.len) InfoBarErrorMessage = Copy(sys, string); } -// @todo: get result from lua? -String16 EvalString(String16 string16) { - Scratch scratch; - LuaCommandResult = {}; - String string = ToString(scratch, string16); +String16 EvalString(Allocator allocator, String16 string16) { + Scratch scratch((Arena *)allocator.object); + String string = ToString(scratch, string16); + string = Format(scratch, "return %.*s", FmtString(string)); if (luaL_dostring(LuaState, string.data) != LUA_OK) { - const char *text = lua_tostring(LuaState, -1); - SetInfoBarErrorMessage(text); } - return LuaCommandResult; + const char *text = lua_tostring(LuaState, -1); + if (text) { + String s = text; + String16 result = ToString16(allocator, s); + lua_pop(LuaState, 1); + return result; + } else { + return {}; + } } \ No newline at end of file diff --git a/src/text_editor/text_editor.h b/src/text_editor/text_editor.h index a1b4ea6..2a919e6 100644 --- a/src/text_editor/text_editor.h +++ b/src/text_editor/text_editor.h @@ -109,7 +109,7 @@ WindowID LastActiveWindow; Int LastFrameIDWhenSwitchedActiveWindow; String InfoBarErrorMessage; -String16 EvalString(String16 string16); +String16 EvalString(Allocator allocator, String16 string16); Rect2I GetVisibleCells(Window &window); void AfterEdit(View *view, Array edits); Scroller ComputeScrollerRect(Window &window);