diff --git a/build_file.cpp b/build_file.cpp index 42aaa86..72ad9e8 100644 --- a/build_file.cpp +++ b/build_file.cpp @@ -178,28 +178,55 @@ char *C(const char *value) { } S8_String LuaScript = R"==( -KEY_CTRL = 1073742048 -KEY_PAGE_DOWN = 1073741902 -KEY_PAGE_UP = 1073741899 -KEY_DOWN = 1073741905 -- 0x40000051 -KEY_UP = 1073741906 -- 0x40000052u -KEY_RIGHT = 1073741903 -KEY_LEFT = 1073741904 -KEY_Q = 113 -KEY_BACKSLASH = 0x5c +SDLK_CTRL = 1073742048 +SDLK_PAGE_DOWN = 1073741902 +SDLK_PAGE_UP = 1073741899 +SDLK_DOWN = 1073741905 -- 0x40000051 +SDLK_UP = 1073741906 -- 0x40000052u +SDLK_RIGHT = 1073741903 +SDLK_LEFT = 1073741904 +SDLK_Q = 113 +SDLK_BACKSLASH = 0x5c -KEY_F1 = 0x4000003a -KEY_F2 = 0x4000003b -KEY_F3 = 0x4000003c -KEY_F4 = 0x4000003d -KEY_F5 = 0x4000003e -KEY_F6 = 0x4000003f -KEY_F7 = 0x40000040 -KEY_F8 = 0x40000041 -KEY_F9 = 0x40000042 -KEY_F10 = 0x40000043 -KEY_F11 = 0x40000044 -KEY_F12 = 0x40000045 +SDLK_F1 = 0x4000003a +SDLK_F2 = 0x4000003b +SDLK_F3 = 0x4000003c +SDLK_F4 = 0x4000003d +SDLK_F5 = 0x4000003e +SDLK_F6 = 0x4000003f +SDLK_F7 = 0x40000040 +SDLK_F8 = 0x40000041 +SDLK_F9 = 0x40000042 +SDLK_F10 = 0x40000043 +SDLK_F11 = 0x40000044 +SDLK_F12 = 0x40000045 + +SDLK_A = 0x00000061 +SDLK_B = 0x00000062 +SDLK_C = 0x00000063 +SDLK_D = 0x00000064 +SDLK_E = 0x00000065 +SDLK_F = 0x00000066 +SDLK_G = 0x00000067 +SDLK_H = 0x00000068 +SDLK_I = 0x00000069 +SDLK_J = 0x0000006a +SDLK_K = 0x0000006b +SDLK_L = 0x0000006c +SDLK_M = 0x0000006d +SDLK_N = 0x0000006e +SDLK_O = 0x0000006f +SDLK_P = 0x00000070 +SDLK_Q = 0x00000071 +SDLK_R = 0x00000072 +SDLK_S = 0x00000073 +SDLK_T = 0x00000074 +SDLK_U = 0x00000075 +SDLK_V = 0x00000076 +SDLK_W = 0x00000077 +SDLK_X = 0x00000078 +SDLK_Y = 0x00000079 +SDLK_Z = 0x0000007a function SkipLineAndColumn(s) local line, col = "1", "1" diff --git a/src/basic/basic.h b/src/basic/basic.h index 56cea9c..cfd1fe3 100644 --- a/src/basic/basic.h +++ b/src/basic/basic.h @@ -1152,7 +1152,7 @@ UTF8Result UTF32ToUTF8(uint32_t codepoint) { return result; } -UTF32Result UTF8ToUTF32(char *c, int64_t max_advance) { +UTF32Result UTF8ToUTF32(uint8_t *c, int64_t max_advance) { UTF32Result result; MemoryZero(&result, sizeof(result)); @@ -1242,7 +1242,7 @@ int64_t CreateCharFromWidechar(char *buffer, int64_t buffer_size, wchar_t *in, i int64_t CreateWidecharFromChar(wchar_t *buffer, int64_t buffer_size, char *in, int64_t inlen) { int64_t outlen = 0; for (int64_t i = 0; i < inlen;) { - UTF32Result decode = UTF8ToUTF32(in + i, (int64_t)(inlen - i)); + UTF32Result decode = UTF8ToUTF32((uint8_t *)(in + i), (int64_t)(inlen - i)); if (!decode.error) { i += decode.advance; UTF16Result encode = UTF32ToUTF16(decode.out_str); @@ -1266,7 +1266,7 @@ bool IsValid(UTF8Iter &iter) { void Advance(UTF8Iter *iter) { iter->i += iter->utf8_codepoint_byte_size; - UTF32Result r = UTF8ToUTF32(iter->data + iter->i, iter->len - iter->i); + UTF32Result r = UTF8ToUTF32((uint8_t *)(iter->data + iter->i), iter->len - iter->i); if (r.error) { iter->item = 0; return; diff --git a/src/basic/string16.cpp b/src/basic/string16.cpp index 72cd22c..2a80471 100644 --- a/src/basic/string16.cpp +++ b/src/basic/string16.cpp @@ -30,7 +30,7 @@ bool IsWord(wchar_t w) { } bool IsLoadWord(wchar_t w) { - bool result = w == '/' || w == '\\' || w == ':' || w == '+' || w == '_' || w == '.' || w == '-'; + bool result = w == L'(' || w == L')' || w == L'/' || w == L'\\' || w == L':' || w == L'+' || w == L'_' || w == L'.' || w == L'-' || w == L','; if (!result) { result = !(IsSymbol(w) || IsWhitespace(w)); } @@ -306,7 +306,7 @@ String16 SkipWhitespace(String16 *string) { return begin; } -String16 FormatV(Allocator allocator, const wchar_t *data, va_list args1) { +String16 Format16V(Allocator allocator, const wchar_t *data, va_list args1) { va_list args2; va_copy(args2, args1); int64_t len = vswprintf(0, 0, data, args2); @@ -318,13 +318,13 @@ String16 FormatV(Allocator allocator, const wchar_t *data, va_list args1) { return res; } -#define STRING16_FORMAT(allocator, data, result) \ - va_list args1; \ - va_start(args1, data); \ - String16 result = FormatV(allocator, data, args1); \ +#define STRING16_FORMAT(allocator, data, result) \ + va_list args1; \ + va_start(args1, data); \ + String16 result = Format16V(allocator, data, args1); \ va_end(args1) -String16 Format(Allocator allocator, const wchar_t *data, ...) PrintfFormatAttribute(2, 3) { +String16 Format16(Allocator allocator, const wchar_t *data, ...) { STRING16_FORMAT(allocator, data, result); return result; } \ No newline at end of file diff --git a/src/text_editor/generated.cpp b/src/text_editor/generated.cpp index 65c902c..98fdcb2 100644 --- a/src/text_editor/generated.cpp +++ b/src/text_editor/generated.cpp @@ -67,28 +67,55 @@ Style.FontSize = 12 Style.FontFilter = 0 Style.Font = "C:/Windows/Fonts/consola.ttf" -KEY_CTRL = 1073742048 -KEY_PAGE_DOWN = 1073741902 -KEY_PAGE_UP = 1073741899 -KEY_DOWN = 1073741905 -- 0x40000051 -KEY_UP = 1073741906 -- 0x40000052u -KEY_RIGHT = 1073741903 -KEY_LEFT = 1073741904 -KEY_Q = 113 -KEY_BACKSLASH = 0x5c +SDLK_CTRL = 1073742048 +SDLK_PAGE_DOWN = 1073741902 +SDLK_PAGE_UP = 1073741899 +SDLK_DOWN = 1073741905 -- 0x40000051 +SDLK_UP = 1073741906 -- 0x40000052u +SDLK_RIGHT = 1073741903 +SDLK_LEFT = 1073741904 +SDLK_Q = 113 +SDLK_BACKSLASH = 0x5c -KEY_F1 = 0x4000003a -KEY_F2 = 0x4000003b -KEY_F3 = 0x4000003c -KEY_F4 = 0x4000003d -KEY_F5 = 0x4000003e -KEY_F6 = 0x4000003f -KEY_F7 = 0x40000040 -KEY_F8 = 0x40000041 -KEY_F9 = 0x40000042 -KEY_F10 = 0x40000043 -KEY_F11 = 0x40000044 -KEY_F12 = 0x40000045 +SDLK_F1 = 0x4000003a +SDLK_F2 = 0x4000003b +SDLK_F3 = 0x4000003c +SDLK_F4 = 0x4000003d +SDLK_F5 = 0x4000003e +SDLK_F6 = 0x4000003f +SDLK_F7 = 0x40000040 +SDLK_F8 = 0x40000041 +SDLK_F9 = 0x40000042 +SDLK_F10 = 0x40000043 +SDLK_F11 = 0x40000044 +SDLK_F12 = 0x40000045 + +SDLK_A = 0x00000061 +SDLK_B = 0x00000062 +SDLK_C = 0x00000063 +SDLK_D = 0x00000064 +SDLK_E = 0x00000065 +SDLK_F = 0x00000066 +SDLK_G = 0x00000067 +SDLK_H = 0x00000068 +SDLK_I = 0x00000069 +SDLK_J = 0x0000006a +SDLK_K = 0x0000006b +SDLK_L = 0x0000006c +SDLK_M = 0x0000006d +SDLK_N = 0x0000006e +SDLK_O = 0x0000006f +SDLK_P = 0x00000070 +SDLK_Q = 0x00000071 +SDLK_R = 0x00000072 +SDLK_S = 0x00000073 +SDLK_T = 0x00000074 +SDLK_U = 0x00000075 +SDLK_V = 0x00000076 +SDLK_W = 0x00000077 +SDLK_X = 0x00000078 +SDLK_Y = 0x00000079 +SDLK_Z = 0x0000007a function SkipLineAndColumn(s) local line, col = "1", "1" diff --git a/src/text_editor/lua_api.cpp b/src/text_editor/lua_api.cpp index 30e140d..8b3d9f1 100644 --- a/src/text_editor/lua_api.cpp +++ b/src/text_editor/lua_api.cpp @@ -14,20 +14,19 @@ String FieldString(lua_State *L, String name) { return result; } -void ExecInNewBuffer(String cmd, String working_dir) { - BSet main = GetActiveMainSet(); - CheckpointBeforeGoto(main.window); +void ExecInNewBuffer(BSet set, String cmd, String working_dir) { + CheckpointBeforeGoto(set.window); Scratch scratch; String buffer_name = GetUniqueBufferName(scratch, working_dir, "+cmd-"); - View *view = WindowOpenBufferView(main.window, buffer_name); + View *view = WindowOpenBufferView(set.window, buffer_name); Buffer *buffer = GetBuffer(view->active_buffer); buffer->gc = true; Command_SelectRangeOneCursor(view, Rng(0)); Exec(view->id, false, cmd, working_dir); - ActiveWindow = main.window->id; + ActiveWindow = set.window->id; } void Open(String path) { @@ -63,7 +62,7 @@ void Open(String path) { } else if (FieldString(LuaState, "kind") == "exec") { String cmd = FieldString(LuaState, "cmd"); String working_dir = FieldString(LuaState, "working_dir"); - ExecInNewBuffer(cmd, working_dir); + ExecInNewBuffer(main, cmd, working_dir); } else if (FieldString(LuaState, "kind") == "exec_console") { String cmd = FieldString(LuaState, "cmd"); String working_dir = FieldString(LuaState, "working_dir"); @@ -103,7 +102,43 @@ int Lua_C(lua_State *L) { lua_pop(L, 1); String working_dir = GetActiveMainWindowBufferDir(); - ExecInNewBuffer(string, working_dir); + BSet set = GetActiveMainSet(); + ExecInNewBuffer(set, string, working_dir); + return 0; +} + +int Lua_Cmd(lua_State *L) { + if (!lua_istable(L, -1)) luaL_error(L, "expected a table as argument"); + defer { lua_pop(L, 1); }; + + lua_getfield(L, -1, "working_dir"); + if (!lua_isstring(L, -1)) luaL_error(L, "expected a string for working_dir param"); + String working_dir = lua_tostring(L, -1); + lua_pop(L, 1); + + lua_getfield(L, -1, "cmd"); + if (!lua_isstring(L, -1)) luaL_error(L, "expected a string for cmd param"); + String cmd = lua_tostring(L, -1); + lua_pop(L, 1); + + lua_getfield(L, -1, "destination"); + String destination = lua_tostring(L, -1); + lua_pop(L, 1); + + BSet active = GetActiveMainSet(); + BSet set = {}; + if (destination == "next") { + Window *next_window = GetNextLayoutWindow(active.window); + set = GetBSet(next_window); + } else if (destination == "3") { + Window *next_window = GetLayoutWindow(2); + if (!next_window) next_window = active.window; + set = GetBSet(next_window); + } else { + set = active; + } + ExecInNewBuffer(set, cmd, working_dir); + return 0; } @@ -132,6 +167,19 @@ int Lua_GetLoadWord(lua_State *L) { return 1; } +int Lua_New(lua_State *L) { + String name = luaL_checkstring(L, 1); + lua_pop(L, 1); + + BSet main = GetActiveMainSet(); + String dir = GetDir(main.buffer); + Scratch scratch; + String new_dir = Format(scratch, "%.*s/%.*s", FmtString(dir), FmtString(name)); + WindowOpenBufferView(main.window, new_dir); + + return 0; +} + int Lua_Open(lua_State *L) { Scratch scratch; String path = luaL_checkstring(L, 1); diff --git a/src/text_editor/lua_api_generated.cpp b/src/text_editor/lua_api_generated.cpp index a582cc3..3501348 100644 --- a/src/text_editor/lua_api_generated.cpp +++ b/src/text_editor/lua_api_generated.cpp @@ -2,9 +2,11 @@ luaL_Reg LuaFunctions[] = { {"FuzzySort", Lua_FuzzySort}, {"AppendCmd", Lua_AppendCmd}, {"C", Lua_C}, + {"Cmd", Lua_Cmd}, {"Kill", Lua_Kill}, {"Error", Lua_Error}, {"GetLoadWord", Lua_GetLoadWord}, + {"New", Lua_New}, {"Open", Lua_Open}, {"Reopen", Lua_Reopen}, {"ToggleFullscreen", Lua_ToggleFullscreen}, diff --git a/src/text_editor/management.cpp b/src/text_editor/management.cpp index 4ecfd3d..0ba3622 100644 --- a/src/text_editor/management.cpp +++ b/src/text_editor/management.cpp @@ -269,7 +269,7 @@ Int ConvertUTF8ToUTF16UnixLine(String string, wchar_t *buffer, Int buffer_cap) { } uint32_t u32 = '?'; - UTF32Result decode = UTF8ToUTF32(string.data + i, (int64_t)(string.len - i)); + UTF32Result decode = UTF8ToUTF32((uint8_t *)(string.data + i), (int64_t)(string.len - i)); if (!decode.error) { i += decode.advance; u32 = decode.out_str; diff --git a/src/text_editor/prototype.cpp b/src/text_editor/prototype.cpp new file mode 100644 index 0000000..5807f74 --- /dev/null +++ b/src/text_editor/prototype.cpp @@ -0,0 +1,55 @@ +struct String16Builder { + Arena *arena; + size_t align; + size_t start_len; +}; + +String16Builder BeginString16(Arena *arena) { + String16Builder result = {arena, arena->align, arena->len}; + arena->align = 0; + return result; +} + +wchar_t *Add(String16Builder *sb, String16 string) { + wchar_t *r = AllocArray(*sb->arena, wchar_t, string.len); + MemoryCopy(r, string.data, string.len * sizeof(wchar_t)); + return r; +} + +String16 EndString16(String16Builder *sb) { + sb->arena->align = sb->align; + String16 result = {(wchar_t *)(sb->arena->data + sb->start_len), (Int)((sb->arena->len - sb->start_len) / sizeof(wchar_t))}; + return result; +} + +String16Builder &operator<<(String16Builder &sb, String16 string) { + Add(&sb, string); + return sb; +} + +String16Builder &operator<<(String16Builder &sb, Int num) { + Scratch scratch(sb.arena); + String16 ss = Format16(scratch, L"%lld", (long long)num); + Add(&sb, ss); + return sb; +} + +String16Builder &operator<<(String16Builder &sb, int num) { + return sb << (Int)num; +} + +String16Builder &operator<<(String16Builder &sb, double num) { + Scratch scratch(sb.arena); + String16 ss = Format16(scratch, L"%f", num); + Add(&sb, ss); + return sb; +} + +void Prototype() { + Scratch scratch; + String16Builder sb = BeginString16(scratch); + sb << 32 << 32.0 << L"\n"; + String16 result = EndString16(&sb); + + int a = 10; +} \ No newline at end of file diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index 732be37..724a303 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -53,7 +53,7 @@ int FullScreenPositionX, FullScreenPositionY; #include "coroutines.cpp" #include "test.cpp" - +#include "prototype.cpp" void FillEventWithBasicData(Event *event) { SDL_Keymod mod = SDL_GetModState(); event->shift = (mod & SDL_KMOD_SHIFT) != 0; @@ -228,6 +228,7 @@ int main(int argc, char **argv) BeginProfiler(); InitScratch(); InitArena(&Perm); + Prototype(); WorkingDir = GetWorkingDir(Perm); ExeDir = GetExeDir(Perm); @@ -248,6 +249,7 @@ int main(int argc, char **argv) return 1; } + SDL_EnableScreenSaver(); SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1"); SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0); diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index bab8e62..4d839d3 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -19,6 +19,10 @@ - ask if you want to close without saving on exit - ask if you want to create new file? +- Lua_New +- Cmd should reuse the window which already has the build +- escapeing multiple cursor after ctrl + d should put the cursor where it was (probably will need to swap secondary and primary cursor for new cursor + - open lua file with proper name it should do the work for setting up a project (double-click on desktop) - when do we regen directory buffers? - load project command which loads files and config diff --git a/src/text_editor/window.cpp b/src/text_editor/window.cpp index d06c85c..afe688f 100644 --- a/src/text_editor/window.cpp +++ b/src/text_editor/window.cpp @@ -33,6 +33,17 @@ Window *GetLayoutWindow(int n) { return NULL; } +Window *GetNextLayoutWindow(Window *input_window) { + bool found = false; + For(Windows) { + Window *window = it.o; + if (!window->is_column || !window->visible || window->absolute_position || window->is_title_bar) continue; + if (found) return window; + if (window->id == input_window->id) found = true; + } + return input_window; +} + Array GetWindowZOrder(Allocator allocator) { Array order = {allocator}; For(Windows) if (it.o->z == 2) Add(&order, GetIndex(Windows, it));