Refactor
This commit is contained in:
@@ -132,21 +132,19 @@ Int ScreenSpaceToBufferPosErrorOutOfBounds(Window *window, View *view, Buffer *b
|
||||
}
|
||||
|
||||
void MouseExecWord(Event event) {
|
||||
Vec2I mouse = MouseVec2I();
|
||||
Window *window = GetActiveWindow();
|
||||
Vec2I mouse = MouseVec2I();
|
||||
BSet active = GetActiveSet();
|
||||
|
||||
bool mouse_in_document = CheckCollisionPointRec(mouse, window->document_rect);
|
||||
bool mouse_in_document = CheckCollisionPointRec(mouse, active.window->document_rect);
|
||||
if (mouse_in_document) {
|
||||
View *view = GetView(window->active_view);
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
Int p = ScreenSpaceToBufferPosErrorOutOfBounds(window, view, buffer, mouse);
|
||||
Int p = ScreenSpaceToBufferPosErrorOutOfBounds(active.window, active.view, active.buffer, mouse);
|
||||
if (p != -1) {
|
||||
Range enclose = EncloseExecWord(buffer, p);
|
||||
if (InBounds(view->carets[0].range, p)) {
|
||||
enclose = view->carets[0].range;
|
||||
Range enclose = EncloseExecWord(active.buffer, p);
|
||||
if (InBounds(active.view->carets[0].range, p)) {
|
||||
enclose = active.view->carets[0].range;
|
||||
}
|
||||
String16 string = GetString(*buffer, enclose);
|
||||
Command_EvalLua(view, string);
|
||||
String16 string = GetString(*active.buffer, enclose);
|
||||
Command_EvalLua(active.view, string);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -173,21 +171,19 @@ void Command_ListBuffers() {
|
||||
}
|
||||
|
||||
void MouseLoadWord(Event event) {
|
||||
Vec2I mouse = MouseVec2I();
|
||||
Window *window = GetActiveWindow();
|
||||
Vec2I mouse = MouseVec2I();
|
||||
BSet active = GetActiveSet();
|
||||
|
||||
bool mouse_in_document = CheckCollisionPointRec(mouse, window->document_rect);
|
||||
bool mouse_in_document = CheckCollisionPointRec(mouse, active.window->document_rect);
|
||||
if (mouse_in_document) {
|
||||
View *view = GetView(window->active_view);
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
Int p = ScreenSpaceToBufferPosErrorOutOfBounds(window, view, buffer, mouse);
|
||||
Int p = ScreenSpaceToBufferPosErrorOutOfBounds(active.window, active.view, active.buffer, mouse);
|
||||
if (p != -1) {
|
||||
Range enclose = EncloseLoadWord(buffer, p);
|
||||
if (InBounds(view->carets[0].range, p)) enclose = view->carets[0].range;
|
||||
String16 string = GetString(*buffer, enclose);
|
||||
Range enclose = EncloseLoadWord(active.buffer, p);
|
||||
if (InBounds(active.view->carets[0].range, p)) enclose = active.view->carets[0].range;
|
||||
String16 string = GetString(*active.buffer, enclose);
|
||||
|
||||
view->carets.len = 1;
|
||||
view->carets[0] = MakeCaret(p);
|
||||
active.view->carets.len = 1;
|
||||
active.view->carets[0] = MakeCaret(p);
|
||||
Open(string);
|
||||
}
|
||||
}
|
||||
@@ -281,15 +277,13 @@ bool GlobalCommand(Event event) {
|
||||
DocumentSelected.id = -1;
|
||||
} else if (IsDocumentSelectionValid()) {
|
||||
Assert(ScrollbarSelected.id == -1);
|
||||
Window *window = GetWindow(DocumentSelected);
|
||||
View *view = GetView(window->active_view);
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
BSet selected = GetBSet(DocumentSelected);
|
||||
|
||||
Vec2I mouse = MouseVec2I();
|
||||
// Special case for full-screen where we can have document
|
||||
// aligned with monitor screen in which case mouse cursor cannot
|
||||
// be smaller then 0 which means we cannot scroll
|
||||
if (mouse.y == 0 && window->document_rect.min.y == 0) {
|
||||
if (mouse.y == 0 && selected.window->document_rect.min.y == 0) {
|
||||
float x, y;
|
||||
SDL_GetGlobalMouseState(&x, &y);
|
||||
if (y == 0) {
|
||||
@@ -297,8 +291,8 @@ bool GlobalCommand(Event event) {
|
||||
}
|
||||
}
|
||||
|
||||
Int p = ScreenSpaceToBufferPos(window, view, buffer, mouse);
|
||||
Caret &caret = view->carets[0];
|
||||
Int p = ScreenSpaceToBufferPos(selected.window, selected.view, selected.buffer, mouse);
|
||||
Caret &caret = selected.view->carets[0];
|
||||
|
||||
caret = SetFrontWithAnchor(caret, DocumentRangeAnchor, p);
|
||||
}
|
||||
@@ -328,32 +322,29 @@ bool GlobalCommand(Event event) {
|
||||
GotoBackward(GetActiveMainSet().window);
|
||||
} else if (event.alt && Mouse(RIGHT)) {
|
||||
} else if (Mouse(RIGHT)) {
|
||||
Vec2I mouse = MouseVec2I();
|
||||
Window *window = GetActiveWindow();
|
||||
bool mouse_in_document = CheckCollisionPointRec(mouse, window->document_rect);
|
||||
Vec2I mouse = MouseVec2I();
|
||||
BSet active = GetActiveSet();
|
||||
bool mouse_in_document = CheckCollisionPointRec(mouse, active.window->document_rect);
|
||||
if (mouse_in_document) {
|
||||
View *view = GetView(window->active_view);
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
|
||||
Int p = ScreenSpaceToBufferPos(window, view, buffer, mouse);
|
||||
Int p = ScreenSpaceToBufferPos(active.window, active.view, active.buffer, mouse);
|
||||
Int saved_front = -1;
|
||||
|
||||
IterRemove(view->carets) {
|
||||
IterRemovePrepare(view->carets);
|
||||
IterRemove(active.view->carets) {
|
||||
IterRemovePrepare(active.view->carets);
|
||||
if (InBounds(it.range, p)) {
|
||||
String16 string = GetString(*buffer, it.range);
|
||||
String16 string = GetString(*active.buffer, it.range);
|
||||
SaveStringInClipboard(string);
|
||||
|
||||
remove_item = true;
|
||||
saved_front = GetFront(it);
|
||||
}
|
||||
}
|
||||
if (view->carets.len == 0) Add(&view->carets, MakeCaret(saved_front));
|
||||
if (active.view->carets.len == 0) Add(&active.view->carets, MakeCaret(saved_front));
|
||||
|
||||
if (saved_front == -1) {
|
||||
Int line = PosToLine(*buffer, p);
|
||||
Range line_range = GetLineRangeWithoutNL(*buffer, line);
|
||||
String16 string = GetString(*buffer, line_range);
|
||||
Int line = PosToLine(*active.buffer, p);
|
||||
Range line_range = GetLineRangeWithoutNL(*active.buffer, line);
|
||||
String16 string = GetString(*active.buffer, line_range);
|
||||
SaveStringInClipboard(string);
|
||||
}
|
||||
}
|
||||
@@ -373,19 +364,17 @@ bool GlobalCommand(Event event) {
|
||||
Assert(ScrollbarSelected.id == -1);
|
||||
Assert(DocumentSelected.id == -1);
|
||||
|
||||
Window *window = GetActiveWindow();
|
||||
bool mouse_in_document = CheckCollisionPointRec(mouse, window->document_rect);
|
||||
bool mouse_in_line_numbers = CheckCollisionPointRec(mouse, window->line_numbers_rect);
|
||||
BSet active = GetActiveSet();
|
||||
bool mouse_in_document = CheckCollisionPointRec(mouse, active.window->document_rect);
|
||||
bool mouse_in_line_numbers = CheckCollisionPointRec(mouse, active.window->line_numbers_rect);
|
||||
if (mouse_in_document || mouse_in_line_numbers) {
|
||||
View *view = GetView(window->active_view);
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
DocumentSelected = window->id;
|
||||
DocumentSelected = active.window->id;
|
||||
|
||||
Int p = ScreenSpaceToBufferPos(window, view, buffer, mouse);
|
||||
if (event.alt) Insert(&view->carets, MakeCaret(p, p), 0);
|
||||
if (!event.alt && !event.shift) view->carets.len = 1;
|
||||
Int p = ScreenSpaceToBufferPos(active.window, active.view, active.buffer, mouse);
|
||||
if (event.alt) Insert(&active.view->carets, MakeCaret(p, p), 0);
|
||||
if (!event.alt && !event.shift) active.view->carets.len = 1;
|
||||
|
||||
Caret &caret = view->carets[0];
|
||||
Caret &caret = active.view->carets[0];
|
||||
if (event.shift) {
|
||||
if (p <= caret.range.min) {
|
||||
caret.range.min = p;
|
||||
@@ -395,13 +384,13 @@ bool GlobalCommand(Event event) {
|
||||
caret.ifront = 1;
|
||||
}
|
||||
} else if (event.clicks >= 2 && InBounds({caret.range.min - 1, caret.range.max + 1}, p)) {
|
||||
Range range = EncloseWord(buffer, p);
|
||||
if (event.clicks >= 3) range = EncloseLoadWord(buffer, p);
|
||||
Range range = EncloseWord(active.buffer, p);
|
||||
if (event.clicks >= 3) range = EncloseLoadWord(active.buffer, p);
|
||||
caret = MakeCaret(range.max, range.min);
|
||||
} else {
|
||||
caret = MakeCaret(p);
|
||||
}
|
||||
MergeCarets(view);
|
||||
MergeCarets(active.view);
|
||||
DocumentRangeAnchor = caret.range;
|
||||
}
|
||||
}
|
||||
@@ -490,8 +479,7 @@ void Command_ReplaceWithoutMovingCarets(View *view, Range range, String16 string
|
||||
AdjustCarets(edits, &caret_copy);
|
||||
}
|
||||
|
||||
void Command_Append(ViewID view_id, String16 string, bool scroll_to_end_if_cursor_on_last_line) {
|
||||
View *view = GetView(view_id);
|
||||
void Command_Append(View *view, String16 string, bool scroll_to_end_if_cursor_on_last_line) {
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
|
||||
bool scroll_to_end = false;
|
||||
@@ -518,32 +506,36 @@ void Command_Append(ViewID view_id, String16 string, bool scroll_to_end_if_curso
|
||||
}
|
||||
}
|
||||
|
||||
void Command_Append(ViewID view_id, String string, bool scroll_to_end_if_cursor_on_last_line) {
|
||||
void Command_Append(View *view, String string, bool scroll_to_end_if_cursor_on_last_line) {
|
||||
Scratch scratch;
|
||||
String16 string16 = ToString16(scratch, string);
|
||||
Command_Append(view_id, string16, scroll_to_end_if_cursor_on_last_line);
|
||||
Command_Append(view, string16, scroll_to_end_if_cursor_on_last_line);
|
||||
}
|
||||
|
||||
void ReportErrorf(const char *fmt, ...) {
|
||||
Scratch scratch;
|
||||
STRING_FORMAT(scratch, fmt, string);
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error!", string.data, NULL);
|
||||
Command_Append(NullViewID, string, true);
|
||||
View *view = GetView(NullViewID);
|
||||
Command_Append(view, string, true);
|
||||
}
|
||||
|
||||
void ReportConsolef(const char *fmt, ...) {
|
||||
Scratch scratch;
|
||||
STRING_FORMAT(scratch, fmt, string);
|
||||
Command_Append(NullViewID, string, true);
|
||||
View *view = GetView(NullViewID);
|
||||
Command_Append(view, string, true);
|
||||
}
|
||||
|
||||
void ReportWarningf(const char *fmt, ...) {
|
||||
Scratch scratch;
|
||||
STRING_FORMAT(scratch, fmt, string);
|
||||
Command_Append(NullViewID, string, true);
|
||||
Window *window = GetWindowWithView(NullViewID);
|
||||
View *null_view = GetView(NullViewID);
|
||||
Command_Append(null_view, string, true);
|
||||
|
||||
Window *window = GetWindowWithView(null_view->id);
|
||||
if (!window) window = GetActiveMainSet().window;
|
||||
CheckpointBeforeGoto(window);
|
||||
window->active_view = NullViewID;
|
||||
window->active_view = null_view->id;
|
||||
ActiveWindow = window->id;
|
||||
}
|
||||
@@ -1,24 +1,23 @@
|
||||
|
||||
void Command_MoveCursorsByPageSize(Window *window, int direction, bool shift = false) {
|
||||
Assert(direction == DIR_UP || direction == DIR_DOWN);
|
||||
View *view = GetView(window->active_view);
|
||||
Buffer *buffer = GetBuffer(view->active_buffer);
|
||||
BSet set = GetBSet(window);
|
||||
|
||||
Rect2I visible_cells_rect = GetVisibleCells(window);
|
||||
Int y = GetSize(visible_cells_rect).y - 2;
|
||||
if (direction == DIR_UP) y = -y;
|
||||
|
||||
For(view->carets) {
|
||||
XY xy = PosToXY(*buffer, GetFront(it));
|
||||
if (direction == DIR_DOWN && xy.line == buffer->line_starts.len - 1) {
|
||||
Range line_range = GetLineRange(*buffer, xy.line);
|
||||
For(set.view->carets) {
|
||||
XY xy = PosToXY(*set.buffer, GetFront(it));
|
||||
if (direction == DIR_DOWN && xy.line == set.buffer->line_starts.len - 1) {
|
||||
Range line_range = GetLineRange(*set.buffer, xy.line);
|
||||
xy.col = line_range.max - line_range.min;
|
||||
} else if (direction == DIR_UP && xy.line == 0) {
|
||||
xy.col = 0;
|
||||
}
|
||||
xy.line += y;
|
||||
|
||||
Int pos = XYToPos(*buffer, xy);
|
||||
Int pos = XYToPos(*set.buffer, xy);
|
||||
if (shift) {
|
||||
it = SetFront(it, pos);
|
||||
} else {
|
||||
@@ -880,28 +879,8 @@ void WindowCommand(Event event, Window *window, View *view) {
|
||||
}
|
||||
|
||||
if (CtrlShift(SDLK_G)) {
|
||||
Window *titlebar = GetTitlebarWindow(window->id);
|
||||
View *titlebar_view = GetView(titlebar->active_view);
|
||||
Buffer *titlebar_buffer = GetBuffer(titlebar_view->active_buffer);
|
||||
ActiveWindow = titlebar->id;
|
||||
|
||||
String16 buffer_string = GetString(*titlebar_buffer);
|
||||
if (Seek(buffer_string, L" |", &buffer_string.len)) {
|
||||
buffer_string = Trim(buffer_string);
|
||||
String16 col = ChopNumberEx(&buffer_string);
|
||||
String16 line = ChopNumberEx(&buffer_string);
|
||||
if (line.len == 0) line = col;
|
||||
if (line.len) {
|
||||
Int min = line.data - buffer_string.data;
|
||||
titlebar_view->carets[0] = MakeCaret(min + line.len, min);
|
||||
titlebar_view->update_scroll = false;
|
||||
}
|
||||
}
|
||||
} else if (Ctrl(SDLK_G)) {
|
||||
Window *titlebar = GetTitlebarWindow(window->id);
|
||||
View *titlebar_view = GetView(titlebar->active_view);
|
||||
Buffer *titlebar_buffer = GetBuffer(titlebar_view->active_buffer);
|
||||
ActiveWindow = titlebar->id;
|
||||
ActiveWindow = GetTitleSet(window).window->id;
|
||||
}
|
||||
|
||||
if (CtrlShift(SDLK_W)) {
|
||||
|
||||
@@ -117,7 +117,7 @@ int Lua_NewCmd(lua_State *L) {
|
||||
|
||||
int Lua_Kill(lua_State *L) {
|
||||
BSet main = GetActiveMainSet();
|
||||
KillProcess(main.window->active_view);
|
||||
KillProcess(main.view);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -133,7 +133,9 @@ int Lua_Print(lua_State *L) {
|
||||
Scratch scratch;
|
||||
String string = lua_tostring(L, 1);
|
||||
lua_pop(L, 1);
|
||||
Command_Append(NullViewID, string, true);
|
||||
|
||||
View *null_view = GetView(NullViewID);
|
||||
Command_Append(null_view, string, true);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -179,6 +179,12 @@ BSet GetBSet(Window *window) {
|
||||
return set;
|
||||
}
|
||||
|
||||
BSet GetBSet(WindowID window_id) {
|
||||
Window *window = GetWindow(window_id);
|
||||
BSet result = GetBSet(window);
|
||||
return result;
|
||||
}
|
||||
|
||||
BSet GetTitleSet(Window *window) {
|
||||
if (!window->is_title_bar) window = GetWindow(window->title_bar_window);
|
||||
BSet result = GetBSet(window);
|
||||
|
||||
@@ -11,7 +11,9 @@ void UpdateProcesses() {
|
||||
IterRemovePrepare(ActiveProcesses);
|
||||
Scratch scratch;
|
||||
String poll = PollStdout(scratch, &it);
|
||||
if (poll.len) Command_Append({it.view_id}, poll, it.scroll_to_end);
|
||||
View *view = GetView({it.view_id});
|
||||
|
||||
if (poll.len) Command_Append(view, poll, it.scroll_to_end);
|
||||
if (!IsValid(&it)) remove_item = true;
|
||||
}
|
||||
}
|
||||
@@ -40,11 +42,12 @@ Buffer *ExecAndWait(Allocator allocator, String cmd, String working_dir, String
|
||||
return temp_buffer;
|
||||
}
|
||||
|
||||
void KillProcess(ViewID view) {
|
||||
void KillProcess(View *view) {
|
||||
IterRemove(ActiveProcesses) {
|
||||
IterRemovePrepare(ActiveProcesses);
|
||||
|
||||
if (it.view_id == view.id) {
|
||||
ViewID view_id = {it.view_id};
|
||||
if (view_id == view->id) {
|
||||
KillProcess(&it);
|
||||
remove_item = true;
|
||||
String string = "process was killed by user";
|
||||
|
||||
@@ -131,8 +131,8 @@ void UpdateScroll(Window *window, bool update_caret_scrolling);
|
||||
void Command_SelectEntireBuffer(View *view);
|
||||
void Command_Replace(View *view, String16 string);
|
||||
void Command_SelectRangeOneCursor(View *view, Range range);
|
||||
void Command_Append(ViewID view_id, String16 string, bool scroll_to_end_if_cursor_on_last_line);
|
||||
void Command_Append(ViewID view_id, String string, bool scroll_to_end_if_cursor_on_last_line);
|
||||
void Command_Append(View *view, String16 string, bool scroll_to_end_if_cursor_on_last_line);
|
||||
void Command_Append(View *view, String string, bool scroll_to_end_if_cursor_on_last_line);
|
||||
Array<Edit> Command_ReplaceEx(Allocator scratch, View *view, String16 string);
|
||||
void Command_ReplaceWithoutMovingCarets(View *view, Range range, String16 string);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user