lua improvements
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -11,44 +11,47 @@ int LuaOpenFile(lua_State *L) {
|
||||
|
||||
int LuaListOpenBuffers(lua_State *L) {
|
||||
Scratch scratch;
|
||||
Array<String16> strings = {scratch};
|
||||
Array<String> 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<String16> strings = {scratch};
|
||||
Array<String> 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<String16> strings = {scratch};
|
||||
Array<String> 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 = {};
|
||||
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 {};
|
||||
}
|
||||
}
|
||||
@@ -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<Edit> edits);
|
||||
Scroller ComputeScrollerRect(Window &window);
|
||||
|
||||
Reference in New Issue
Block a user