Moving Open code to C
This commit is contained in:
@@ -333,4 +333,34 @@ API Int GetSize(Array<String> array) {
|
||||
Int result = 0;
|
||||
For (array) result += it.len;
|
||||
return result;
|
||||
}
|
||||
|
||||
API bool Chop(String *string, String ending) {
|
||||
if (EndsWith(*string, ending)) {
|
||||
*string = Chop(*string, ending.len);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
API String ChopNumberEx(String *string) {
|
||||
String col = {};
|
||||
for (int64_t i = string->len - 1; i >= 0; i -= 1) {
|
||||
if (IsDigit(string->data[i])) {
|
||||
col.data = string->data + i;
|
||||
col.len += 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
*string = Chop(*string, col.len);
|
||||
return col;
|
||||
}
|
||||
|
||||
API Int ChopNumber(String *string) {
|
||||
Scratch scratch;
|
||||
String col = ChopNumberEx(string);
|
||||
if (col.len == 0) return -1;
|
||||
Int result = strtoll(col.data, NULL, 10) - 1;
|
||||
return result;
|
||||
}
|
||||
@@ -394,20 +394,18 @@ API String16 SkipWhitespace(String16 *string) {
|
||||
return begin;
|
||||
}
|
||||
|
||||
// chop this - :324
|
||||
// chop this - 324
|
||||
API String16 ChopNumberEx(String16 *string) {
|
||||
String16 col = {};
|
||||
for (int64_t i = string->len - 1; i >= 0; i -= 1) {
|
||||
if (IsDigit(string->data[i])) {
|
||||
col.data = string->data + i;
|
||||
col.len += 1;
|
||||
} else if (string->data[i] == L':') {
|
||||
break;
|
||||
} else {
|
||||
return {};
|
||||
break;
|
||||
}
|
||||
}
|
||||
*string = Chop(*string, col.len + 1);
|
||||
*string = Chop(*string, col.len);
|
||||
return col;
|
||||
}
|
||||
|
||||
@@ -416,7 +414,6 @@ API Int ChopNumber(String16 *string) {
|
||||
String16 col = ChopNumberEx(string);
|
||||
if (col.len == 0) return -1;
|
||||
String num_string = ToString(scratch, col);
|
||||
Int result = strtoll(num_string.data, NULL, 10) - 1;
|
||||
|
||||
Int result = strtoll(num_string.data, NULL, 10) - 1;
|
||||
return result;
|
||||
}
|
||||
@@ -1414,39 +1414,25 @@ void RunBufferTest() {
|
||||
|
||||
///////////////////////////////
|
||||
// Management
|
||||
inline BufferID AllocBufferID(Buffer *buffer) {
|
||||
BufferID AllocBufferID(Buffer *buffer) {
|
||||
return {BufferIDs++, buffer};
|
||||
}
|
||||
|
||||
inline Buffer *GetBuffer(BufferID id) {
|
||||
Buffer *GetBuffer(BufferID id, Buffer *default_buffer = Buffers[0]) {
|
||||
For(Buffers) {
|
||||
if (it->id == id) return it;
|
||||
}
|
||||
return Buffers[0];
|
||||
return default_buffer;
|
||||
}
|
||||
|
||||
inline Buffer *FindBuffer(BufferID id) {
|
||||
For(Buffers) {
|
||||
if (it->id == id) return it;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
inline Buffer *GetBuffer(String name) {
|
||||
Buffer *GetBuffer(String name, Buffer *default_buffer = Buffers[0]) {
|
||||
For(Buffers) {
|
||||
if (it->name == name) return it;
|
||||
}
|
||||
return Buffers[0];
|
||||
return default_buffer;
|
||||
}
|
||||
|
||||
inline Buffer *FindBuffer(String name) {
|
||||
For(Buffers) {
|
||||
if (it->name == name) return it;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
inline bool IsNull(Buffer *buffer) {
|
||||
bool IsNull(Buffer *buffer) {
|
||||
return buffer->id.id == 0;
|
||||
}
|
||||
|
||||
@@ -1459,10 +1445,10 @@ Buffer *CreateBuffer(Allocator allocator, String name, Int size) {
|
||||
String GetUniqueBufferName(String working_dir, String prepend_name, String extension = ".log") {
|
||||
Scratch scratch;
|
||||
String buffer_name = {};
|
||||
for (int i = 1; i < INT_MAX; i += 1) {
|
||||
for (int i = 1; i < 1000; i += 1) {
|
||||
buffer_name = Format(scratch, "%S/%S%d%S", working_dir, prepend_name, i, extension);
|
||||
buffer_name = GetAbsolutePath(scratch, buffer_name);
|
||||
Buffer *exists = FindBuffer(buffer_name);
|
||||
Buffer *exists = GetBuffer(buffer_name, NULL);
|
||||
if (!exists && !FileExists(buffer_name)) {
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -161,4 +161,3 @@ API void ResetHistory(Buffer *buffer);
|
||||
|
||||
API void DeallocHistoryArray(Array<HistoryEntry> *entries);
|
||||
API void DeallocHistoryEntries(Array<HistoryEntry> *entries);
|
||||
|
||||
|
||||
@@ -35,65 +35,6 @@ String GetMainDir() {
|
||||
return name;
|
||||
}
|
||||
|
||||
void CheckpointBeforeGoto(Window *window, View *view) {
|
||||
if (window->jump_history == false) return;
|
||||
Add(&window->goto_history, {view->id, view->carets[0], GetTimeSeconds()});
|
||||
window->goto_redo.len = 0;
|
||||
}
|
||||
|
||||
void CheckpointBeforeGoto(Window *window) {
|
||||
CheckpointBeforeGoto(window, GetView(window->active_view));
|
||||
}
|
||||
|
||||
GotoCrumb GetCrumb(Array<GotoCrumb> *cr) {
|
||||
for (; cr->len;) {
|
||||
GotoCrumb c = Pop(cr);
|
||||
View *view = FindView(c.view_id);
|
||||
if (view) return c;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
void GotoBackward(Window *window) {
|
||||
if (window->jump_history == false) return;
|
||||
if (window->goto_history.len <= 0) return;
|
||||
BSet set = GetBSet(window);
|
||||
Add(&window->goto_redo, {set.view->id, set.view->carets[0], GetTimeSeconds()});
|
||||
|
||||
GotoCrumb c = GetCrumb(&window->goto_history);
|
||||
window->active_view = c.view_id;
|
||||
View *view = GetView(c.view_id);
|
||||
view->carets[0] = c.caret;
|
||||
UpdateScroll(window, true);
|
||||
|
||||
if (window->goto_history.len) {
|
||||
GotoCrumb *next = GetLast(window->goto_history);
|
||||
if (c.time - next->time <= StyleUndoMergeTimeout) {
|
||||
GotoBackward(window);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GotoForward(Window *window) {
|
||||
if (window->goto_redo.len <= 0) return;
|
||||
if (window->jump_history == false) return;
|
||||
BSet set = GetBSet(window);
|
||||
Add(&window->goto_history, {set.view->id, set.view->carets[0], GetTimeSeconds()});
|
||||
|
||||
GotoCrumb c = GetCrumb(&window->goto_redo);
|
||||
window->active_view = c.view_id;
|
||||
View *view = GetView(c.view_id);
|
||||
view->carets[0] = c.caret;
|
||||
UpdateScroll(window, true);
|
||||
|
||||
if (window->goto_redo.len) {
|
||||
GotoCrumb *next = GetLast(window->goto_redo);
|
||||
if (c.time - next->time <= StyleUndoMergeTimeout) {
|
||||
GotoForward(window);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void JumpGarbageBuffer(BSet *set, String buffer_name = "") {
|
||||
CheckpointBeforeGoto(set->window);
|
||||
if (buffer_name.len == 0) {
|
||||
@@ -530,6 +471,7 @@ enum OpenKind {
|
||||
OpenKind_Invalid,
|
||||
OpenKind_Skip,
|
||||
OpenKind_Exec,
|
||||
OpenKind_BackgroundExec,
|
||||
OpenKind_Goto,
|
||||
OpenKind_Command,
|
||||
};
|
||||
@@ -540,24 +482,94 @@ struct ResolvedOpen {
|
||||
Int line, col;
|
||||
};
|
||||
|
||||
ResolvedOpen ResolveOpen(String path, String meta) {
|
||||
ResolvedOpen ResolveOpen(Allocator scratch, String path, String meta) {
|
||||
ResolvedOpen result = {};
|
||||
|
||||
if (StartsWith(path, ":")) {
|
||||
result = OpenKind_Command;
|
||||
result.path = Skip(path, 1);
|
||||
} else if (StartsWith(path, "!")) {
|
||||
result = OpenKind_Exec;
|
||||
result.path = Skip(path, 1);
|
||||
} else if () {
|
||||
// Editor command
|
||||
{
|
||||
if (StartsWith(path, ":")) {
|
||||
result.kind = OpenKind_Command;
|
||||
result.path = Skip(path, 1);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// Shell
|
||||
{
|
||||
if (StartsWith(path, "!")) {
|
||||
result.kind = OpenKind_Exec;
|
||||
result.path = Skip(path, 1);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// Web
|
||||
{
|
||||
if (StartsWith(path, "https://") || StartsWith(path, "http://")) {
|
||||
result.path = Format(scratch, "%S %S", ConfigInternetBrowser, path);
|
||||
result.kind = OpenKind_BackgroundExec;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// Commit
|
||||
{
|
||||
if (StartsWith(path, "commit ")) {
|
||||
path = Skip(path, 7);
|
||||
result.path = Format(scratch, "git --no-pager show %S", path);
|
||||
result.kind = OpenKind_Exec;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// Parse ":line:column"
|
||||
{
|
||||
path = NormalizePath(scratch, path);
|
||||
String p = path;
|
||||
Int a = ChopNumber(&p);
|
||||
if (a != -1 && Chop(&p, ":")) {
|
||||
path = p;
|
||||
Int b = ChopNumber(&p);
|
||||
if (b != -1 && Chop(&p, ":")) {
|
||||
path = p;
|
||||
result.col = a;
|
||||
result.line = b;
|
||||
} else {
|
||||
result.line = a;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Buffer *existing_buffer = GetBuffer(path, NULL);
|
||||
if (existing_buffer != NULL) {
|
||||
result.path = path;
|
||||
result.kind = OpenKind_Goto;
|
||||
return result;
|
||||
}
|
||||
|
||||
if (IsAbsolute(path)) {
|
||||
if (FileExists(path)) {
|
||||
result.path = path;
|
||||
result.kind = OpenKind_Goto;
|
||||
return result;
|
||||
}
|
||||
} else {
|
||||
String rel_path = Format(scratch, "%S/%S", GetMainDir(), path);
|
||||
if (GetBuffer(rel_path, NULL) || FileExists(rel_path)) {
|
||||
result.path = rel_path;
|
||||
result.kind = OpenKind_Goto;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
BSet Open(Window *window, String path, String meta, bool set_active = true) {
|
||||
Scratch scratch;
|
||||
BSet set = GetBSet(window);
|
||||
path = Trim(path);
|
||||
ResolvedOpen o = ResolveOpen(path, meta);
|
||||
ResolvedOpen o = ResolveOpen(scratch, path, meta);
|
||||
if (o.kind == OpenKind_Goto) {
|
||||
if (set_active) {
|
||||
ActiveWindowID = set.window->id;
|
||||
@@ -584,11 +596,14 @@ BSet Open(Window *window, String path, String meta, bool set_active = true) {
|
||||
ActiveWindowID = set.window->id;
|
||||
}
|
||||
JumpGarbageBuffer(&set);
|
||||
Exec(set.view->id, true, o.path, WorkDir);
|
||||
Exec(set.view->id, true, o.path, GetMainDir());
|
||||
} else if (o.kind == OpenKind_BackgroundExec) {
|
||||
// this shouldn't change the focus/window/view
|
||||
Exec(NullViewID, true, o.path, GetMainDir());
|
||||
} else if (o.kind == OpenKind_Skip) {
|
||||
return {};
|
||||
} else {
|
||||
ReportWarningf("Failed to match any of OnOpen results!");
|
||||
ReportWarningf("Failed to open: %S", path);
|
||||
}
|
||||
|
||||
return GetBSet(window);
|
||||
@@ -606,12 +621,6 @@ BSet Open(String16 path, String meta) {
|
||||
return Open(string, meta);
|
||||
}
|
||||
|
||||
void SetProjectFile(Buffer *buffer) {
|
||||
WorkDir = ChopLastSlash(buffer->name);
|
||||
LuaProjectBuffer = buffer;
|
||||
LuaProjectBuffer->user_change_id = -1;
|
||||
}
|
||||
|
||||
void Command_Save() {
|
||||
BSet active = GetBSet(LastActiveLayoutWindowID);
|
||||
SaveBuffer(active.buffer);
|
||||
@@ -653,21 +662,11 @@ void Command_ToggleFullscreen() {
|
||||
IsInFullscreen = !IsInFullscreen;
|
||||
} RegisterCommand(Command_ToggleFullscreen, "f11");
|
||||
|
||||
void Command_SetProjectFile() {
|
||||
BSet main = GetBSet(LastActiveLayoutWindowID);
|
||||
SetProjectFile(main.buffer);
|
||||
} RegisterCommand(Command_SetProjectFile, "");
|
||||
|
||||
void Command_SetWorkDir() {
|
||||
BSet main = GetBSet(LastActiveLayoutWindowID);
|
||||
WorkDir = ChopLastSlash(main.buffer->name);
|
||||
} RegisterCommand(Command_SetWorkDir, "");
|
||||
|
||||
void Command_SetProject() {
|
||||
Command_SetWorkDir();
|
||||
Command_SetProjectFile();
|
||||
} RegisterCommand(Command_SetProject, "");
|
||||
|
||||
void Command_KillProcess() {
|
||||
BSet main = GetBSet(LastActiveLayoutWindowID);
|
||||
KillProcess(main.view);
|
||||
@@ -735,7 +734,6 @@ void Command_Open() {
|
||||
if (active.window->id == CommandBarWindowID) {
|
||||
CommandWindowOpen(active);
|
||||
} else {
|
||||
BSet active = GetBSet(LastActiveLayoutWindowID);
|
||||
Open(FetchLoadWord(active.view));
|
||||
}
|
||||
} RegisterCommand(Command_Open, "ctrl-q");
|
||||
|
||||
@@ -454,6 +454,16 @@ Event TranslateSDLEvent(SDL_Event *input_event) {
|
||||
return event;
|
||||
}
|
||||
|
||||
inline void AddEvent(Array<Event> *events, Event ev) {
|
||||
if (ev.kind == EVENT_TEXT_INPUT && (ev.ctrl || ev.alt || ev.super)) {
|
||||
return;
|
||||
}
|
||||
if (ev.kind == EVENT_NONE) {
|
||||
return;
|
||||
}
|
||||
Add(events, ev);
|
||||
}
|
||||
|
||||
Array<Event> GetEventsForFrame(Allocator allocator) {
|
||||
Array<Event> result = {allocator};
|
||||
if (EventPlayback.len) {
|
||||
@@ -465,12 +475,12 @@ Array<Event> GetEventsForFrame(Allocator allocator) {
|
||||
if (WaitForEvents) {
|
||||
SDL_WaitEvent(&event);
|
||||
Event ev = TranslateSDLEvent(&event);
|
||||
if (ev.kind != EVENT_NONE) Add(&result, ev);
|
||||
AddEvent(&result, ev);
|
||||
}
|
||||
|
||||
while (SDL_PollEvent(&event)) {
|
||||
Event ev = TranslateSDLEvent(&event);
|
||||
if (ev.kind != EVENT_NONE) Add(&result, ev);
|
||||
AddEvent(&result, ev);
|
||||
}
|
||||
|
||||
if (result.len == 0) {
|
||||
|
||||
@@ -38,8 +38,6 @@ WindowID ResizerSelected = {-1};
|
||||
WindowID ResizerHover = {-1};
|
||||
Caret DocumentAnchor;
|
||||
|
||||
Buffer *LuaProjectBuffer;
|
||||
Buffer *LuaConfigBuffer;
|
||||
Buffer *GCInfoBuffer;
|
||||
Buffer *EventBuffer;
|
||||
Buffer *ScratchBuffer;
|
||||
@@ -154,3 +152,4 @@ RegisterVariable(Int, StyleFontFilter, 0);
|
||||
RegisterVariable(String, StyleFont, "/home/krz/text_editor/package/CascadiaMono.ttf");
|
||||
RegisterVariable(String, StyleVCVarsall, "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Auxiliary/Build/vcvars64.bat");
|
||||
RegisterVariable(Float, StyleUndoMergeTimeout, 0.3);
|
||||
RegisterVariable(String, ConfigInternetBrowser, "firefox");
|
||||
|
||||
@@ -721,13 +721,11 @@ int main(int argc, char **argv)
|
||||
|
||||
for (int i = 1; i < argc; i += 1) {
|
||||
String it = argv[i];
|
||||
if (!FileExists(it)) continue;
|
||||
|
||||
if (it == "--testing") {
|
||||
Testing = true;
|
||||
} else if (EndsWith(it, ".project.lua")) {
|
||||
SetProjectFile(BufferOpenFile(it));
|
||||
} else {
|
||||
if (!FileExists(it)) continue;
|
||||
Window *window = GetWindow({0});
|
||||
WindowOpenBufferView(window, it);
|
||||
}
|
||||
|
||||
@@ -102,7 +102,6 @@ void Appendf(View *view, const char *fmt, ...);
|
||||
Buffer *CreateBuffer(Allocator allocator, String name, Int size = 4096);
|
||||
View *CreateView(BufferID active_buffer);
|
||||
void ReopenBuffer(Buffer *buffer);
|
||||
inline Buffer *FindBuffer(String name);
|
||||
bool ProcessIsActive(ViewID view);
|
||||
|
||||
inline bool operator==(BufferID a, BufferID b) { return a.id == b.id; }
|
||||
|
||||
Reference in New Issue
Block a user