Files
text_editor/src/text_editor/commands.cpp
2025-12-16 23:07:16 +01:00

1611 lines
54 KiB
C++

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) {
String current_dir = ChopLastSlash(set->buffer->name);
buffer_name = GetUniqueBufferName(current_dir, "temp");
}
set->view = WindowOpenBufferView(set->window, buffer_name);
set->buffer = GetBuffer(set->view->active_buffer);
set->buffer->garbage = true;
}
void BeginJump(BSet *set, BufferID buffer_id = NullBufferID) {
CheckpointBeforeGoto(set->window);
set->buffer = GetBuffer(buffer_id);
set->view = WindowOpenBufferView(set->window, set->buffer->name);
}
void EndJump(BSet set) {
Int pos = XYToPos(set.buffer, {0, set.buffer->line_starts.len - 1});
set.view->carets[0] = MakeCaret(pos);
UpdateScroll(set.window, true);
}
Int ScreenSpaceToBufferPos(Window *window, View *view, Buffer *buffer, Vec2I mouse) {
Vec2I mworld = mouse - window->document_rect.min + view->scroll;
double px = (double)mworld.x / (double)window->font->char_spacing;
double py = (double)mworld.y / (double)window->font->line_spacing;
XY xy = {(Int)round(px), (Int)floor(py)};
Int result = XYToPosWithoutNL(buffer, xy);
return result;
}
Int ScreenSpaceToBufferPosErrorOutOfBounds(Window *window, View *view, Buffer *buffer, Vec2I mouse) {
Vec2I mworld = mouse - window->document_rect.min + view->scroll;
double px = (double)mworld.x / (double)window->font->char_spacing;
double py = (double)mworld.y / (double)window->font->line_spacing;
XY xy = {(Int)round(px), (Int)floor(py)};
Int result = XYToPosErrorOutOfBounds(buffer, xy);
return result;
}
void MouseLoadWord(Event event, String meta = "") {
Vec2I mouse = MouseVec2I();
BSet active = GetBSet(ActiveWindowID);
bool mouse_in_document = AreOverlapping(mouse, active.window->document_rect);
if (mouse_in_document) {
Int p = ScreenSpaceToBufferPosErrorOutOfBounds(active.window, active.view, active.buffer, mouse);
if (p != -1) {
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);
active.view->carets.len = 1;
active.view->carets[0] = MakeCaret(p);
Open(string, meta);
}
}
}
View *GetViewForFixingWhenBufferCommand(Buffer *buffer, bool *is_active = NULL) {
View *view = NULL;
if (is_active) {
*is_active = false;
}
BSet active = GetBSet(ActiveWindowID);
if (active.buffer->id == buffer->id) {
if (is_active) {
*is_active = true;
}
return active.view;
}
For(Views) {
if (it->active_buffer != buffer->id) {
continue;
}
view = it;
break;
}
if (!view) {
view = CreateView(buffer->id);
}
return view;
}
void ReplaceWithoutMovingCarets(Buffer *buffer, Range range, String16 string) {
View *view = GetViewForFixingWhenBufferCommand(buffer);
Array<Caret> carets = Copy(GetSystemAllocator(), view->carets);
Scratch scratch;
SelectRange(view, range);
ReplaceEx(scratch, view, string);
Dealloc(&view->carets);
view->carets = carets;
}
// @todo: revamp interface since it scrolls ALL VIEWS??? or maybe not??
void Append(View *view, String16 string, bool scroll_to_end_if_cursor_on_last_line) {
Scratch scratch;
Buffer *buffer = GetBuffer(view->active_buffer);
struct ViewInfo {
View *view;
Array<Caret> carets;
bool scroll_to_end;
};
Array<ViewInfo> view_info = {scratch};
ForItem(it_view, Views) {
if (it_view->active_buffer != buffer->id) {
continue;
}
ViewInfo vi = {it_view};
if (scroll_to_end_if_cursor_on_last_line) {
Int line = PosToLine(buffer, GetFront(it_view->carets[0]));
if (line == buffer->line_starts.len - 1) {
vi.scroll_to_end = true;
}
}
if (!vi.scroll_to_end) {
vi.carets = Copy(GetSystemAllocator(), it_view->carets);
}
Add(&view_info, vi);
}
SelectRange(view, GetBufferEndAsRange(buffer));
Replace(view, string);
For (view_info) {
if (it.scroll_to_end) {
it.view->carets[0] = MakeCaret(GetBufferEndAsRange(buffer).min);
} else {
Dealloc(&it.view->carets);
it.view->carets = it.carets;
}
}
}
void Append(View *view, String string, bool scroll_to_end_if_cursor_on_last_line) {
Scratch scratch;
String16 string16 = ToString16(scratch, string);
Append(view, string16, scroll_to_end_if_cursor_on_last_line);
}
void Appendf(View *view, const char *fmt, ...) {
Scratch scratch;
STRING_FORMAT(scratch, fmt, string);
Append(view, string, true);
}
void ReportErrorf(const char *fmt, ...) {
Scratch scratch;
STRING_FORMAT(scratch, fmt, string);
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error!", string.data, NULL);
View *view = GetView(NullViewID);
if (view) {
Appendf(view, "%S\n", string);
ActiveWindowID = NullWindowID;
}
}
void ReportConsolef(const char *fmt, ...) {
Scratch scratch;
STRING_FORMAT(scratch, fmt, string);
View *view = GetView(NullViewID);
Appendf(view, "%S\n", string);
}
void ReportWarningf(const char *fmt, ...) {
Scratch scratch;
STRING_FORMAT(scratch, fmt, string);
View *null_view = GetView(NullViewID);
Appendf(null_view, "%S\n", string);
}
void ReportDebugf(const char *fmt, ...) {
Scratch scratch;
STRING_FORMAT(scratch, fmt, string);
Appendf(TraceView, "%S\n", string);
}
void MoveCursorByPageSize(Window *window, int direction, bool shift = false) {
Assert(direction == DIR_UP || direction == DIR_DOWN);
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(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(set.buffer, xy);
if (shift) {
it = SetFront(it, pos);
} else {
it = MakeCaret(pos);
}
}
}
void MoveCursorToSide(View *view, int direction, bool shift = false) {
Assert(direction == DIR_LEFT || direction == DIR_RIGHT);
Buffer *buffer = GetBuffer(view->active_buffer);
For(view->carets) {
Int pos = GetFront(it);
if (direction == DIR_RIGHT) {
pos = GetLineEnd(buffer, pos);
} else {
Int indent = GetIndentAtPos(buffer, pos);
Int new_pos = GetLineStart(buffer, pos);
if (new_pos + indent != pos) {
pos = new_pos + indent;
} else {
pos = new_pos;
}
}
if (shift) {
it = SetFront(it, pos);
} else {
it.range.max = it.range.min = pos;
}
}
}
Caret MoveCaret(Buffer *buffer, Caret it, int direction, bool ctrl = false, bool shift = false) {
Int front = GetFront(it);
Int range_size = GetSize(it.range);
switch (direction) {
case DIR_UP: {
if (ctrl && shift) {
Int pos = GetPrevEmptyLineStart(buffer, front);
it = SetFront(it, pos);
} else if (ctrl) {
Int pos = GetPrevEmptyLineStart(buffer, it.range.min);
it = MakeCaret(pos);
} else if (shift) {
if (PosToLine(buffer, front) == 0) {
it = SetFront(it, 0);
} else {
Int pos = OffsetByLine(buffer, front, -1);
it = SetFront(it, pos);
}
} else {
if (range_size == 0) {
Int pos = OffsetByLine(buffer, it.range.min, -1);
it = MakeCaret(pos);
} else {
it = MakeCaret(it.range.min);
}
}
} break;
case DIR_DOWN: {
if (ctrl && shift) {
Int pos = GetNextEmptyLineStart(buffer, front);
it = SetFront(it, pos);
} else if (ctrl) {
Int pos = GetNextEmptyLineStart(buffer, it.range.max);
it = MakeCaret(pos);
} else if (shift) {
if (LastLine(buffer) == PosToLine(buffer, front)) {
it = SetFront(it, buffer->len);
} else {
Int pos = OffsetByLine(buffer, front, 1);
it = SetFront(it, pos);
}
} else {
if (range_size == 0) {
Int pos = OffsetByLine(buffer, it.range.max, 1);
it = MakeCaret(pos);
} else {
it = MakeCaret(it.range.max);
}
}
} break;
case DIR_LEFT: {
if (ctrl && shift) {
Int pos = GetPrevWordStart(buffer, front);
it = SetFront(it, pos);
} else if (ctrl) {
if (range_size != 0 && front != it.range.min) {
it = MakeCaret(it.range.min);
} else {
Int pos = GetPrevWordStart(buffer, it.range.min);
it = MakeCaret(pos);
}
} else if (shift) {
Int pos = GetPrevChar(buffer, front);
it = SetFront(it, pos);
} else {
if (range_size == 0) {
Int pos = GetPrevChar(buffer, it.range.min);
it = MakeCaret(pos);
} else {
it = MakeCaret(it.range.min);
}
}
} break;
case DIR_RIGHT: {
if (ctrl && shift) {
Int pos = GetNextWordEnd(buffer, front);
it = SetFront(it, pos);
} else if (ctrl) {
if (range_size != 0 && front != it.range.max) {
it = MakeCaret(it.range.max);
} else {
Int pos = GetNextWordEnd(buffer, it.range.max);
it = MakeCaret(pos);
}
} else if (shift) {
Int pos = GetNextChar(buffer, front);
it = SetFront(it, pos);
} else {
if (range_size == 0) {
Int pos = GetNextChar(buffer, it.range.max);
it = MakeCaret(pos);
} else {
it = MakeCaret(it.range.max);
}
}
} break;
}
return it;
}
void MoveCarets(View *view, int direction, bool ctrl = false, bool shift = false) {
Assert(direction < DIR_COUNT);
Buffer *buffer = GetBuffer(view->active_buffer);
For(view->carets) {
it = MoveCaret(buffer, it, direction, ctrl, shift);
}
}
void MoveCaretsLine(View *view, int direction) {
Assert(direction == DIR_DOWN || direction == DIR_UP);
Scratch scratch;
// @todo: this doesn't work well at the end of buffer
struct XYPair {
XY front;
XY back;
};
Buffer *buffer = GetBuffer(view->active_buffer);
Array<Edit> edits = BeginEdit(scratch, buffer, view->carets);
MergeCarets(buffer, &view->carets);
Array<XYPair> saved_xy = {scratch};
For(view->carets) {
Int eof_current = 0;
Range lines_to_move_range = {GetFullLineStart(buffer, it.range.min), GetFullLineEnd(buffer, it.range.max, &eof_current)};
if (lines_to_move_range.min == 0 && direction == DIR_UP) {
continue;
}
Int eof = 0;
Int next_line_start = lines_to_move_range.max;
Int next_line_end = GetFullLineEnd(buffer, next_line_start, &eof);
Int prev_line_end = lines_to_move_range.min - 1;
Int prev_line_start = GetFullLineStart(buffer, prev_line_end);
if (direction == DIR_DOWN && eof) {
continue;
} else if (direction == DIR_UP && eof_current) {
continue;
}
String16 string = Copy16(scratch, GetString(buffer, lines_to_move_range));
AddEdit(&edits, lines_to_move_range, {});
if (direction == DIR_DOWN) {
AddEdit(&edits, MakeRange(next_line_end), string);
} else {
AddEdit(&edits, MakeRange(prev_line_start), string);
}
Add(&saved_xy, {PosToXY(buffer, GetFront(it)), PosToXY(buffer, GetBack(it))});
}
EndEdit(buffer, &edits, &view->carets, !KILL_SELECTION);
Int line_offset = direction == DIR_UP ? -1 : +1;
for (Int i = 0; i < saved_xy.len; i += 1) {
Caret &caret = view->carets[i];
XYPair &xypair = saved_xy[i];
xypair.front.line += line_offset;
xypair.back.line += line_offset;
Int front = XYToPos(buffer, xypair.front);
Int back = XYToPos(buffer, xypair.back);
caret = MakeCaret(front, back);
}
}
Array<Edit> ReplaceEx(Allocator scratch, View *view, String16 string) {
Buffer *buffer = GetBuffer(view->active_buffer);
Array<Edit> edits = BeginEdit(scratch, buffer, view->carets);
MergeCarets(buffer, &view->carets);
For(view->carets) AddEdit(&edits, it.range, string);
EndEdit(buffer, &edits, &view->carets, KILL_SELECTION);
return edits;
}
void Replace(View *view, String16 string) {
Scratch scratch;
ReplaceEx(scratch, view, string);
}
void DuplicateLine(View *view, int direction) {
Assert(direction == DIR_UP || direction == DIR_DOWN);
Scratch scratch;
Buffer *buffer = GetBuffer(view->active_buffer);
BeginEdit(scratch, buffer, view->carets);
MergeCarets(buffer, &view->carets);
Array<Edit> edits = {scratch};
For(view->carets) {
Int eof = 0;
Range range = {};
range.max = GetFullLineEnd(buffer, it.range.max, &eof);
range.min = GetFullLineStart(buffer, it.range.min);
range.min -= Clamp(eof, (Int)0, buffer->len);
String16 string = Copy16(scratch, GetString(buffer, range));
Int pos = direction == DIR_UP ? range.min : range.max;
AddEdit(&edits, MakeRange(pos), string);
}
EndEdit(buffer, &edits, &view->carets, !KILL_SELECTION);
Int coef = direction == DIR_UP ? -1 : 1;
for (Int i = 0; i < edits.len; i += 1) {
Caret *caret = view->carets.data + i;
XY xymin = PosToXY(buffer, caret->range.min);
XY xymax = PosToXY(buffer, caret->range.max);
Int line_count = xymax.line - xymin.line + 1;
xymin.line += coef * line_count;
xymax.line += coef * line_count;
caret->range.min = XYToPos(buffer, xymin);
caret->range.max = XYToPos(buffer, xymax);
}
}
Array<Range> GetSelectedLinesSorted(Allocator allocator, View *view) {
Scratch scratch(allocator);
Buffer *buffer = GetBuffer(view->active_buffer);
Array<Caret> caret_copy = TightCopy(scratch, view->carets);
Array<Caret> temp = TightCopy(scratch, view->carets);
if (view->carets.len > 1) MergeSort(view->carets.len, caret_copy.data, temp.data);
Array<Range> result = {allocator};
For(caret_copy) {
Int min_line = PosToLine(buffer, it.range.min);
Int max_line = PosToLine(buffer, it.range.max);
Range line_range = {min_line, max_line + 1};
if (result.len == 0) {
Add(&result, line_range);
continue;
}
Range *last = GetLast(result);
if (AreOverlapping(*last, line_range)) {
last->max = Max(last->max, line_range.max);
} else {
Add(&result, line_range);
}
}
return result;
}
void IndentSelectedLines(View *view, bool shift = false) {
Scratch scratch;
Buffer *buffer = GetBuffer(view->active_buffer);
Array<Edit> edits = BeginEdit(scratch, buffer, view->carets);
MergeCarets(buffer, &view->carets);
Array<Range> line_ranges_to_indent = GetSelectedLinesSorted(scratch, view);
For(line_ranges_to_indent) {
for (Int i = it.min; i < it.max; i += 1) {
Range pos_range_of_line = GetLineRange(buffer, i);
if (!shift) {
String16 whitespace_string = {u" ", StyleIndentSize};
AddEdit(&edits, {pos_range_of_line.min, pos_range_of_line.min}, whitespace_string);
} else {
String16 string = GetString(buffer, pos_range_of_line);
Int whitespace_len = 0;
for (Int i = 0; i < StyleIndentSize && i < string.len && string.data[i] == ' '; i += 1) {
whitespace_len += 1;
}
AddEdit(&edits, {pos_range_of_line.min, pos_range_of_line.min + whitespace_len}, u"");
}
}
}
EndEdit(buffer, &edits, &view->carets, !KILL_SELECTION);
view->update_scroll = false;
}
void TrimTrailingWhitespace(Buffer *buffer, bool trim_lines_with_caret = false) {
Scratch scratch;
bool is_active_view = false;
View *view = GetViewForFixingWhenBufferCommand(buffer, &is_active_view);
if (!is_active_view && !trim_lines_with_caret) {
trim_lines_with_caret = true;
}
Array<Edit> edits = BeginEdit(scratch, buffer, view->carets);
MergeCarets(buffer, &view->carets);
Array<Range> lines_to_skip_triming = {};
if (!trim_lines_with_caret) {
lines_to_skip_triming = GetSelectedLinesSorted(scratch, view);
}
for (Int i = 0; i < buffer->line_starts.len; i += 1) {
Int range_index = FindRangeByPos(&lines_to_skip_triming, i);
if (range_index != -1) continue;
Range range = GetLineRangeWithoutNL(buffer, i);
Int whitespace_end = range.max;
for (; whitespace_end > range.min; whitespace_end -= 1) {
U16 w = buffer->data[whitespace_end - 1];
bool is_whitespace = w == ' ' || w == '\t' || w == '\v' || w == '\r';
if (!is_whitespace) break;
}
Range whitespace_range = {whitespace_end, range.max};
AddEdit(&edits, whitespace_range, u"");
}
EndEdit(buffer, &edits, &view->carets, !KILL_SELECTION);
view->update_scroll = false;
}
void ConvertLineEndingsToLF(Buffer *buffer, bool trim_lines_with_caret = false) {
Scratch scratch;
bool is_active_view = false;
View *view = GetViewForFixingWhenBufferCommand(buffer, &is_active_view);
if (!is_active_view && !trim_lines_with_caret) {
trim_lines_with_caret = true;
}
Array<Edit> edits = BeginEdit(scratch, buffer, view->carets);
MergeCarets(buffer, &view->carets);
Array<Range> lines_to_skip_triming = {};
if (!trim_lines_with_caret) {
lines_to_skip_triming = GetSelectedLinesSorted(scratch, view);
}
for (Int i = 0; i < buffer->line_starts.len; i += 1) {
Int range_index = FindRangeByPos(&lines_to_skip_triming, i);
if (range_index != -1) continue;
Range range = GetLineRangeWithoutNL(buffer, i);
char16_t cr = GetChar(buffer, range.max - 1);
char16_t lf = GetChar(buffer, range.max);
if (cr == u'\r' && lf == u'\n') {
AddEdit(&edits, {range.max - 1, range.max}, u"");
}
}
EndEdit(buffer, &edits, &view->carets, !KILL_SELECTION);
view->update_scroll = false;
}
void ApplyClangFormat(Buffer *buffer) {
Scratch scratch;
String string = AllocCharString(scratch, buffer);
Buffer *temp_buffer = ExecAndWait(scratch, "clang-format", GetDir(buffer), string);
ReplaceWithoutMovingCarets(buffer, GetRange(buffer), {temp_buffer->str, temp_buffer->len});
}
void SaveBuffer(Buffer *buffer) {
CallOnSave(buffer->id);
Scratch scratch;
String string = AllocCharString(scratch, buffer);
bool success = WriteFile(buffer->name, string);
if (success) {
buffer->file_mod_time = GetFileModTime(buffer->name);
buffer->dirty = false;
buffer->garbage = false;
} else {
ReportWarningf("Failed to save file with name: %S", buffer->name);
}
}
void KillSelectedLines(View *view) {
Scratch scratch;
Buffer *buffer = GetBuffer(view->active_buffer);
SaveCaretHistoryBeforeBeginEdit(buffer, view->carets);
For (view->carets) {
Int eof = 0;
it.range.max = GetFullLineEnd(buffer, it.range.max, &eof);
it.range.min = GetFullLineStart(buffer, it.range.min);
it.range.min -= Clamp(eof, (Int)0, buffer->len);
}
Replace(view, u"");
}
void EncloseLine(View *view) {
Buffer *buffer = GetBuffer(view->active_buffer);
For (view->carets) {
Int eof = 0;
it.range.max = GetFullLineEnd(buffer, it.range.max, &eof);
it.range.min = GetFullLineStart(buffer, it.range.min);
}
}
void EncloseScope(View *view) {
Buffer *buffer = GetBuffer(view->active_buffer);
For (view->carets) {
it.range = EncloseScope(buffer, it.range.min - 1, it.range.max + 1, u'(', u')');
it.range.min = Clamp(buffer, it.range.min + 1);
it.range.max = Clamp(buffer, it.range.max - 1);
}
}
void EncloseSpace(View *view) {
Buffer *buffer = GetBuffer(view->active_buffer);
For (view->carets) {
it.range.min = GetPrevEmptyLineStart(buffer, it.range.min);
it.range.max = GetNextEmptyLineStart(buffer, it.range.max);
}
}
void Delete(View *view, int direction, bool ctrl = false) {
Assert(direction == DIR_LEFT || direction == DIR_RIGHT);
Scratch scratch;
Buffer *buffer = GetBuffer(view->active_buffer);
Array<Edit> edits = BeginEdit(scratch, buffer, view->carets);
// Select things to delete
For(view->carets) {
if (GetSize(it.range)) continue;
if (direction == DIR_LEFT) {
// Delete indent in multiple of IndentSize
Range indent_range = GetIndentRangeAtPos(buffer, it.range.min);
if (ctrl == false && it.range.min > indent_range.min && it.range.max <= indent_range.max) {
Int offset = it.range.min - indent_range.min;
Int to_delete = (offset % (StyleIndentSize));
if (to_delete == 0) to_delete = StyleIndentSize;
to_delete = Clamp(to_delete, (Int)1, StyleIndentSize);
for (Int i = 0; i < to_delete; i += 1) {
it = MoveCaret(buffer, it, direction, false, SHIFT_PRESS);
}
} else {
it = MoveCaret(buffer, it, direction, ctrl, SHIFT_PRESS);
}
} else {
it = MoveCaret(buffer, it, direction, ctrl, SHIFT_PRESS);
}
}
MergeCarets(buffer, &view->carets);
For(view->carets) AddEdit(&edits, it.range, {});
EndEdit(buffer, &edits, &view->carets, true);
}
void SelectAll(View *view, String16 needle) {
Buffer *buffer = GetBuffer(view->active_buffer);
String16 string_buffer = GetString(buffer);
for (Int pos = 0;;) {
Int index = 0;
String16 medium = Skip(string_buffer, pos);
if (!Seek(medium, needle, &index)) {
break;
}
Add(&view->carets, MakeCaret(pos + index + needle.len, pos + index));
pos += needle.len;
}
MergeCarets(buffer, &view->carets);
}
void CreateCursorVertical(View *view, int direction) {
Assert(direction == DIR_UP || direction == DIR_DOWN);
Buffer *buffer = GetBuffer(view->active_buffer);
Int line_offset = direction == DIR_UP ? -1 : 1;
Scratch scratch;
Array<Caret> arr = {scratch};
For(view->carets) {
if (PosToLine(buffer, it.range.min) == PosToLine(buffer, it.range.max)) {
Int f = OffsetByLine(buffer, GetFront(it), line_offset);
Int b = OffsetByLine(buffer, GetBack(it), line_offset);
Add(&arr, MakeCaret(f, b));
} else {
Int pos = direction == DIR_UP ? it.range.min : it.range.max;
Caret caret = MakeCaret(pos);
caret = MoveCaret(buffer, caret, direction);
Add(&arr, caret);
}
}
For(arr) Add(&view->carets, it);
MergeCarets(buffer, &view->carets);
}
void SelectRange(View *view, Caret caret) {
view->carets.len = 1;
view->carets[0] = caret;
}
void SelectRange(View *view, Range range) {
SelectRange(view, MakeCaret(range.min, range.max));
}
void SelectEntireBuffer(View *view) {
Buffer *buffer = GetBuffer(view->active_buffer);
SelectRange(view, GetRange(buffer));
}
Caret FindPrev(Buffer *buffer, String16 needle, Caret caret) {
Int pos = GetFront(caret);
String16 medium = GetString(buffer, {0, pos});
Caret result = caret;
Int index = 0;
if (Seek(medium, needle, &index, SeekFlag_MatchFindLast)) {
result = MakeCaret(index, index + needle.len);
} else {
medium = GetString(buffer);
if (Seek(medium, needle, &index, SeekFlag_MatchFindLast)) {
result = MakeCaret(index, index + needle.len);
}
}
return result;
}
Caret FindNext(Buffer *buffer, String16 needle, Caret caret) {
Int pos = GetMax(caret);
String16 medium = GetString(buffer, {pos, INT64_MAX});
Caret result = caret;
Int index = 0;
if (Seek(medium, needle, &index)) {
result = MakeCaret(pos + index + needle.len, pos + index);
} else {
medium = GetString(buffer);
if (Seek(medium, needle, &index)) {
result = MakeCaret(index + needle.len, index);
}
}
return result;
}
void IdentedNewLine(View *view) {
Buffer *buffer = GetBuffer(view->active_buffer);
Scratch scratch;
Array<Edit> edits = BeginEdit(scratch, buffer, view->carets);
MergeCarets(buffer, &view->carets);
For(view->carets) {
Int front = GetFront(it);
Int line = PosToLine(buffer, front);
Int indent = GetLineIndent(buffer, line);
String string = Format(scratch, "\n%.*s", (int)indent, " ");
String16 string16 = ToString16(scratch, string);
AddEdit(&edits, it.range, string16);
}
EndEdit(buffer, &edits, &view->carets, KILL_SELECTION);
}
void Find(View *seek_view, String16 needle, bool forward = true) {
Buffer *seek_buffer = GetBuffer(seek_view->active_buffer);
Caret caret = seek_view->carets[0];
if (forward) caret = FindNext(seek_buffer, needle, caret);
if (!forward) caret = FindPrev(seek_buffer, needle, caret);
SelectRange(seek_view, caret);
IF_DEBUG(AssertRanges(seek_view->carets));
}
void GotoNextInList(Window *window, Int line_offset = 1) {
Assert(line_offset == 1 || line_offset == -1);
View *active_view = GetView(window->active_view);
View *view_goto = GetView(window->active_goto_list);
window->active_view = view_goto->id;
Buffer *buffer_goto = GetBuffer(view_goto->active_buffer);
int64_t pos = window->goto_list_pos;
Int line = PosToLine(buffer_goto, pos);
bool opened = false;
for (Int i = line + line_offset; i >= 0 && i < buffer_goto->line_starts.len; i += line_offset) {
Range line_range = GetLineRangeWithoutNL(buffer_goto, i);
String16 line = GetString(buffer_goto, line_range);
view_goto->carets[0] = MakeCaret(line_range.min);
window->goto_list_pos = line_range.min;
line = Trim(line);
MergeCarets(buffer_goto, &view_goto->carets);
IF_DEBUG(AssertRanges(view_goto->carets));
if (line.len == 0) {
continue;
}
BSet set = Open(line, "goto_build");
if (set.window == NULL) {
continue;
}
opened = true;
break;
}
if (!opened) window->active_view = active_view->id;
}
void FuzzySortView(View *view, String16 needle) {
Buffer *buffer = GetBuffer(view->active_buffer);
Scratch scratch;
Array<FuzzyPair> ratings = FuzzySearchLines(scratch, buffer, 0, buffer->line_starts.len, needle);
Buffer *temp_buffer = CreateTempBuffer(scratch, buffer->cap);
For(ratings) {
String16 s = GetLineStringWithoutNL(buffer, it.index);
if (s.len == 0) continue;
RawReplaceText(temp_buffer, GetBufferEndAsRange(temp_buffer), s);
RawReplaceText(temp_buffer, GetBufferEndAsRange(temp_buffer), u"\n");
}
SelectEntireBuffer(view);
Replace(view, GetString(temp_buffer));
SelectRange(view, MakeRange(0));
}
void ReopenBuffer(Buffer *buffer) {
Scratch scratch;
String string = ReadFile(scratch, buffer->name);
if (string.len == 0) {
return;
}
String16 string16 = ToUnixString16(scratch, string);
ReplaceWithoutMovingCarets(buffer, GetRange(buffer), string16);
buffer->file_mod_time = GetFileModTime(buffer->name);
buffer->changed_on_disk = false;
buffer->dirty = false;
}
void New(Window *window, String name = "") {
View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer);
Scratch scratch;
String dir = GetDir(buffer);
if (name != "") {
if (!IsAbsolute(name)) {
name = Format(scratch, "%S/%S", dir, name);
}
name = GetAbsolutePath(scratch, name);
} else {
name = GetUniqueBufferName(dir, "new");
}
WindowOpenBufferView(window, name);
}
void NewDir(Window *window, String name = "") {
View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer);
Scratch scratch;
String dir = GetDir(buffer);
if (name != "") {
if (!IsAbsolute(name)) {
name = Format(scratch, "%S/%S", dir, name);
}
name = GetAbsolutePath(scratch, name);
} else {
name = GetUniqueBufferName(dir, "new");
}
MakeDir(name);
Open(name);
}
void ListFilesRecursive(Buffer *buffer, String dir) {
Scratch scratch(buffer->line_starts.allocator);
for (FileIter it = IterateFiles(scratch, dir); IsValid(it); Advance(&it)) {
if (it.filename == ".git") {
continue;
}
if (it.is_directory) {
ListFilesRecursive(buffer, it.absolute_path);
} else {
bool good = CallIsCode(it.absolute_path);
if (!good) {
continue;
}
RawAppendf(buffer, "%-80S || %S\n", it.filename, it.absolute_path);
}
}
}
View *ExecHidden(String buffer_name, String cmd, String working_dir) {
View *view = OpenBufferView(buffer_name);
Exec(view->id, true, cmd, working_dir);
return view;
}
BSet Exec(String cmd, String working_dir, bool set_active = true) {
BSet main = GetBSet(LastActiveLayoutWindowID);
if (set_active) {
ActiveWindowID = main.window->id;
}
JumpGarbageBuffer(&main);
Exec(main.view->id, true, cmd, working_dir);
return main;
}
BSet Open(Window *window, String path, String meta, bool set_active = true) {
Scratch scratch;
BSet set = GetBSet(window);
path = Trim(path);
OnOpenResult ores = CallOnOpen(scratch, path, meta);
if (ores.kind == "text") {
if (set_active) {
ActiveWindowID = set.window->id;
}
if (IsDir(ores.file_path)) {
JumpGarbageBuffer(&set, GetUniqueBufferName(ores.file_path, "temp", ".dirlisting"));
Appendf(set.view, "..\n");
for (FileIter it = IterateFiles(scratch, ores.file_path); IsValid(it); Advance(&it)) {
Appendf(set.view, "%S\n", it.filename);
}
} else {
CheckpointBeforeGoto(set.window);
View *view = WindowOpenBufferView(set.window, ores.file_path);
Buffer *buffer = GetBuffer(view->active_buffer);
if (ores.line != -1) {
if (ores.col == -1) ores.col = 1;
Int pos = XYToPos(buffer, {ores.col - 1, ores.line - 1});
view->carets[0] = MakeCaret(pos);
}
}
UpdateScroll(set.window, true);
} else if (ores.kind == "exec") {
if (set_active) {
ActiveWindowID = set.window->id;
}
JumpGarbageBuffer(&set);
Exec(set.view->id, true, ores.cmd, ores.working_dir);
} else if (ores.kind == "exec_console") {
// this shouldn't change the focus/window/view
Exec(NullViewID, true, ores.cmd, ores.working_dir);
} else if (ores.kind == "skip") {
return {};
} else {
ReportWarningf("Failed to match any of OnOpen results!");
}
set = GetBSet(window);
return set;
}
BSet Open(String path, String meta) {
BSet main = GetBSet(LastActiveLayoutWindowID);
main = Open(main.window, path, meta);
return main;
}
BSet Open(String16 path, String meta) {
Scratch scratch;
String string = ToString(scratch, path);
return Open(string, meta);
}
void Eval(String string) {
if (luaL_dostring(LuaState, string.data) != LUA_OK) {
const char *error_message = lua_tostring(LuaState, -1);
ReportWarningf("Execution error! %s", error_message);
lua_pop(LuaState, 1);
}
}
void Eval(String16 string) {
Scratch scratch;
Eval(ToString(scratch, string));
}
void SetProjectFile(Buffer *buffer) {
WorkDir = ChopLastSlash(buffer->name);
LuaProjectBuffer = buffer;
LuaProjectBuffer->user_change_id = -1;
}
String16 FetchLoadWord(BSet set) {
Caret caret = set.view->carets[0];
Range range = caret.range;
if (GetSize(caret.range) == 0) range = EncloseLoadWord(set.buffer, GetFront(caret));
String16 string = GetString(set.buffer, range);
return string;
}
void Command_Save(CommandContext *ctx) {
BSet active = GetBSet(LastActiveLayoutWindowID);
SaveBuffer(active.buffer);
} RegisterCommand(Command_Save, "ctrl-s");
void Command_SaveAll(CommandContext *ctx) {
For(Buffers) {
if (it->file_mod_time) {
SaveBuffer(it);
}
}
} RegisterCommand(Command_SaveAll, "ctrl-shift-s");
void Command_Reopen(CommandContext *ctx) {
BSet main = GetBSet(LastActiveLayoutWindowID);
ReopenBuffer(main.buffer);
ActiveWindowID = main.window->id;
} RegisterCommand(Command_Reopen, "");
void Command_New(CommandContext *ctx) {
BSet main = GetBSet(LastActiveLayoutWindowID);
New(main.window, "");
} RegisterCommand(Command_New, "ctrl-n");
void Command_ToggleFullscreen(CommandContext *ctx) {
if (IsInFullscreen) {
SDL_SetWindowSize(SDLWindow, FullScreenSizeX, FullScreenSizeY);
SDL_SetWindowPosition(SDLWindow, FullScreenPositionX, FullScreenPositionY);
} else {
SDL_GetWindowSize(SDLWindow, &FullScreenSizeX, &FullScreenSizeY);
SDL_GetWindowPosition(SDLWindow, &FullScreenPositionX, &FullScreenPositionY);
SDL_DisplayID display = SDL_GetDisplayForWindow(SDLWindow);
const SDL_DisplayMode *dm = SDL_GetCurrentDisplayMode(display);
SDL_SetWindowSize(SDLWindow, dm->w, dm->h);
SDL_SetWindowPosition(SDLWindow, 0, 0);
}
IsInFullscreen = !IsInFullscreen;
} RegisterCommand(Command_ToggleFullscreen, "f11");
void Command_ListCode(CommandContext *ctx) {
BSet main = GetBSet(LastActiveLayoutWindowID);
JumpGarbageBuffer(&main);
ListFilesRecursive(main.buffer, WorkDir);
main.view->fuzzy_search = true;
main.view->update_scroll = true;
SelectRange(main.view, GetBufferEndAsRange(main.buffer));
} RegisterCommand(Command_ListCode, "");
void Command_ShowBufferList(CommandContext *ctx) {
BSet command_bar = GetBSet(CommandBarWindowID);
command_bar.window->visible = true;
command_bar.window->eval_command = false;
ActiveWindowID = command_bar.window->id;
ResetBuffer(command_bar.buffer);
For(Buffers) {
RawAppendf(command_bar.buffer, "%-80S || %S\n", SkipToLastSlash(it->name), it->name);
}
command_bar.view->update_scroll = true;
SelectRange(command_bar.view, GetBufferEndAsRange(command_bar.buffer));
} RegisterCommand(Command_ShowBufferList, "ctrl-p");
void Command_ListViews(CommandContext *ctx) {
BSet command_bar = GetBSet(CommandBarWindowID);
command_bar.window->visible = true;
command_bar.window->eval_command = false;
ActiveWindowID = command_bar.window->id;
ResetBuffer(command_bar.buffer);
For(Views) {
Buffer *buffer = GetBuffer(it->active_buffer);
Appendf(command_bar.view, "%d %S\n", (int)it->id.id, buffer->name);
}
command_bar.view->fuzzy_search = true;
command_bar.view->update_scroll = true;
SelectRange(command_bar.view, GetBufferEndAsRange(command_bar.buffer));
} RegisterCommand(Command_ListViews, "");
void Command_SetProjectFile(CommandContext *ctx) {
BSet main = GetBSet(LastActiveLayoutWindowID);
SetProjectFile(main.buffer);
} RegisterCommand(Command_SetProjectFile, "");
void Command_SetWorkDir(CommandContext *ctx) {
String dir = lua_tostring(LuaState, -1);
if (dir.len == 0) {
BSet main = GetBSet(LastActiveLayoutWindowID);
WorkDir = ChopLastSlash(main.buffer->name);
} else {
WorkDir = dir;
}
} RegisterCommand(Command_SetWorkDir, "");
void Command_SetProject(CommandContext *ctx) {
Command_SetWorkDir(ctx);
Command_SetProjectFile(ctx);
} RegisterCommand(Command_SetProject, "");
void Command_ToggleDebug(CommandContext *ctx) {
Window *window = GetWindow(DebugWindowID);
window->visible = !window->visible;
} RegisterCommand(Command_ToggleDebug, "ctrl-0");
void Command_KillProcess(CommandContext *ctx) {
BSet main = GetBSet(LastActiveLayoutWindowID);
KillProcess(main.view);
} RegisterCommand(Command_KillProcess, "");
void Command_KillWindow(CommandContext *ctx) {
BSet main = GetBSet(LastActiveLayoutWindowID);
main.window->kill = true;
} RegisterCommand(Command_KillWindow, "ctrl-w");
void Command_ShowCommands(CommandContext *ctx) {
BSet command_bar = GetBSet(CommandBarWindowID);
command_bar.window->visible = true;
command_bar.window->eval_command = true;
ActiveWindowID = command_bar.window->id;
ResetBuffer(command_bar.buffer);
For(CommandFunctions) {
Appendf(command_bar.view, "%S\n", it.name);
}
command_bar.view->update_scroll = true;
SelectRange(command_bar.view, GetBufferEndAsRange(command_bar.buffer));
} RegisterCommand(Command_ShowCommands, "ctrl-shift-p");
void Command_ShowLuaFunctions(CommandContext *ctx) {
BSet command_bar = GetBSet(CommandBarWindowID);
command_bar.window->visible = true;
command_bar.window->eval_command = false;
ActiveWindowID = command_bar.window->id;
ResetBuffer(command_bar.buffer);
For(LuaFunctions) {
Appendf(command_bar.view, "%S()\n ", it.name);
}
command_bar.view->update_scroll = true;
SelectRange(command_bar.view, GetBufferEndAsRange(command_bar.buffer));
} RegisterCommand(Command_ShowLuaFunctions, "");
void Command_GotoBackward(CommandContext *ctx) {
BSet main = GetBSet(LastActiveLayoutWindowID);
GotoBackward(main.window);
} RegisterCommand(Command_GotoBackward, "alt-q | mousex1");
void Command_GotoForward(CommandContext *ctx) {
BSet main = GetBSet(LastActiveLayoutWindowID);
GotoForward(main.window);
} RegisterCommand(Command_GotoForward, "mousex2");
void Command_OpenUpFolder(CommandContext *ctx) {
BSet main = GetBSet(LastActiveLayoutWindowID);
String name = ChopLastSlash(main.buffer->name);
if (EndsWith(main.buffer->name, "dirlisting")) {
name = ChopLastSlash(name);
}
Open(name);
} RegisterCommand(Command_OpenUpFolder, "ctrl-period");
void Command_EncloseLine(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
EncloseLine(active.view);
} RegisterCommand(Command_EncloseLine, "ctrl-l");
void Command_SelectAll(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
SelectEntireBuffer(active.view);
active.view->update_scroll = false;
} RegisterCommand(Command_SelectAll, "ctrl-a");
void Command_Redo(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
RedoEdit(active.buffer, &active.view->carets);
} RegisterCommand(Command_Redo, "ctrl-shift-z");
void Command_Undo(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
UndoEdit(active.buffer, &active.view->carets);
} RegisterCommand(Command_Undo, "ctrl-z");
void Command_MakeFontLarger(CommandContext *ctx) {
StyleFontSize += 1;
ReloadFont(StyleFont, (U32)StyleFontSize);
} RegisterCommand(Command_MakeFontLarger, "ctrl-equals");
void Command_MakeFontSmaller(CommandContext *ctx) {
if (StyleFontSize > 4) {
StyleFontSize -= 1;
ReloadFont(StyleFont, (U32)StyleFontSize);
}
} RegisterCommand(Command_MakeFontSmaller, "ctrl-minus");
void Command_Search(CommandContext *ctx) {
Window *window = GetWindow(SearchBarWindowID);
window->visible = !window->visible;
} RegisterCommand(Command_Search, "ctrl-f");
void EvalCommand(CommandContext *ctx, String command) {
For (CommandFunctions) {
if (it.name == command) {
it.function(ctx);
break;
}
}
}
void EvalCommand(CommandContext *ctx, String16 command) {
Scratch scratch;
EvalCommand(ctx, ToString(scratch, command));
}
void FuzzySearchOpen(CommandContext *ctx, BSet active) {
Range range = active.view->carets[0].range;
String16 string = FetchLoadWord(active);
if (GetSize(range) == 0) {
Int line = PosToLine(active.buffer, range.min);
if ((active.buffer->line_starts.len - 1) == line) {
line = ClampBottom(0ll, line - 1ll);
}
string = GetLineStringWithoutNL(active.buffer, line);
Int idx = 0;
if (Seek(string, u"||", &idx)) {
string = Skip(string, idx + 3);
}
}
if (active.window->eval_command) {
BSet main = GetBSet(LastActiveLayoutWindowID);
ActiveWindowID = main.window->id;
EvalCommand(ctx, string);
} else {
Open(string);
}
}
void Command_Open(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
if (active.view->fuzzy_search) {
FuzzySearchOpen(ctx, active);
} else {
BSet active = GetBSet(LastActiveLayoutWindowID);
Open(FetchLoadWord(active));
}
} RegisterCommand(Command_Open, "ctrl-q");
void Command_KillSelectedLines(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
KillSelectedLines(active.view);
} RegisterCommand(Command_KillSelectedLines, "ctrl-shift-k");
void Command_IndentSelectedLines(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
Event event = *OnCommandEvent;
bool left = false;
if (Press(SDLK_LEFTBRACKET) || ShiftPress(SDLK_TAB)) {
left = true;
}
IndentSelectedLines(active.view, left);
} RegisterCommand(Command_IndentSelectedLines, "ctrl-leftbracket | ctrl-rightbracket | tab | shift-tab");
void Command_DuplicateLineDown(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
DuplicateLine(active.view, DIR_DOWN);
} RegisterCommand(Command_DuplicateLineDown, "ctrl-alt-down");
void Command_CreateCursorDown(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
CreateCursorVertical(active.view, DIR_DOWN);
} RegisterCommand(Command_CreateCursorDown, "alt-shift-down");
void Command_SelectDownToEmptyLine(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCarets(active.view, DIR_DOWN, CTRL_PRESSED, SHIFT_PRESS);
} RegisterCommand(Command_SelectDownToEmptyLine, "ctrl-shift-down");
void Command_MoveLineDown(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCaretsLine(active.view, DIR_DOWN);
} RegisterCommand(Command_MoveLineDown, "alt-down");
void Command_MoveDownToEmptyLine(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCarets(active.view, DIR_DOWN, CTRL_PRESSED);
} RegisterCommand(Command_MoveDownToEmptyLine, "ctrl-down");
void Command_SelectDown(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCarets(active.view, DIR_DOWN, false, SHIFT_PRESS);
} RegisterCommand(Command_SelectDown, "shift-down");
void Command_MoveDown(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCarets(active.view, DIR_DOWN);
} RegisterCommand(Command_MoveDown, "down");
void Command_DuplicateLineUp(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
DuplicateLine(active.view, DIR_UP);
} RegisterCommand(Command_DuplicateLineUp, "ctrl-alt-up");
void Command_CreateCursorUp(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
CreateCursorVertical(active.view, DIR_UP);
} RegisterCommand(Command_CreateCursorUp, "alt-shift-up");
void Command_SelectUpToEmptyLine(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCarets(active.view, DIR_UP, CTRL_PRESSED, SHIFT_PRESS);
} RegisterCommand(Command_SelectUpToEmptyLine, "ctrl-shift-up");
void Command_MoveLineUp(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCaretsLine(active.view, DIR_UP);
} RegisterCommand(Command_MoveLineUp, "alt-up");
void Command_MoveUpToEmptyLine(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCarets(active.view, DIR_UP, CTRL_PRESSED);
} RegisterCommand(Command_MoveUpToEmptyLine, "ctrl-up");
void Command_SelectUp(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCarets(active.view, DIR_UP, false, SHIFT_PRESS);
} RegisterCommand(Command_SelectUp, "shift-up");
void Command_MoveUp(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCarets(active.view, DIR_UP);
} RegisterCommand(Command_MoveUp, "up");
void Command_BoundarySelectLeft(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCarets(active.view, DIR_LEFT, CTRL_PRESSED, SHIFT_PRESS);
} RegisterCommand(Command_BoundarySelectLeft, "ctrl-shift-left");
void Command_BoundaryMoveLeft(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCarets(active.view, DIR_LEFT, CTRL_PRESSED);
} RegisterCommand(Command_BoundaryMoveLeft, "ctrl-left");
void Command_SelectLeft(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCarets(active.view, DIR_LEFT, false, SHIFT_PRESS);
} RegisterCommand(Command_SelectLeft, "shift-left");
void Command_FocusLeftWindow(CommandContext *ctx) {
ActiveWindowID = SwitchWindow(DIR_LEFT)->id;
} RegisterCommand(Command_FocusLeftWindow, "alt-left");
void Command_MoveLeft(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCarets(active.view, DIR_LEFT);
} RegisterCommand(Command_MoveLeft, "left");
void Command_BoundarySelectRight(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCarets(active.view, DIR_RIGHT, CTRL_PRESSED, SHIFT_PRESS);
} RegisterCommand(Command_BoundarySelectRight, "ctrl-shift-right");
void Command_BoundaryMoveRight(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCarets(active.view, DIR_RIGHT, CTRL_PRESSED);
} RegisterCommand(Command_BoundaryMoveRight, "ctrl-right");
void Command_SelectRight(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCarets(active.view, DIR_RIGHT, false, SHIFT_PRESS);
} RegisterCommand(Command_SelectRight, "shift-right");
void Command_FocusRightWindow(CommandContext *ctx) {
ActiveWindowID = SwitchWindow(DIR_RIGHT)->id;
} RegisterCommand(Command_FocusRightWindow, "alt-right");
void Command_MoveRight(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCarets(active.view, DIR_RIGHT);
} RegisterCommand(Command_MoveRight, "right");
void Command_MoveUpAPage(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCursorByPageSize(active.window, DIR_UP, SHIFT_PRESS);
} RegisterCommand(Command_MoveUpAPage, "pageup");
void Command_SelectDownPage(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCursorByPageSize(active.window, DIR_DOWN, SHIFT_PRESS);
} RegisterCommand(Command_SelectDownPage, "shift-pagedown");
void Command_MoveToEnd(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
SelectRange(active.view, MakeRange(active.buffer->len));
} RegisterCommand(Command_MoveToEnd, "pagedown");
void Command_MoveDownPage(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
SelectRange(active.view, MakeRange(active.buffer->len));
} RegisterCommand(Command_MoveDownPage, "ctrl-pagedown");
void Command_SelectUpPage(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCursorByPageSize(active.window, DIR_UP, SHIFT_PRESS);
} RegisterCommand(Command_SelectUpPage, "shift-pageup");
void Command_MoveToStart(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
SelectRange(active.view, MakeRange(0));
} RegisterCommand(Command_MoveToStart, "ctrl-pageup");
void Command_MoveUpPage(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCursorByPageSize(active.window, DIR_UP);
} RegisterCommand(Command_MoveUpPage, "pageup");
void Command_SelectToLineStart(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCursorToSide(active.view, DIR_LEFT, SHIFT_PRESS);
} RegisterCommand(Command_SelectToLineStart, "shift-home");
void Command_MoveToLineStart(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCursorToSide(active.view, DIR_LEFT);
} RegisterCommand(Command_MoveToLineStart, "home");
void Command_MoveToLineEnd(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCursorToSide(active.view, DIR_RIGHT);
} RegisterCommand(Command_MoveToLineEnd, "end");
void Command_SelectToLineEnd(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCursorToSide(active.view, DIR_RIGHT, SHIFT_PRESS);
} RegisterCommand(Command_SelectToLineEnd, "shift-end");
void Command_Delete(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
Delete(active.view, DIR_LEFT);
} RegisterCommand(Command_Delete, "backspace");
void Command_DeleteBoundary(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
Delete(active.view, DIR_LEFT, SHIFT_PRESS);
} RegisterCommand(Command_DeleteBoundary, "ctrl-backspace");
void Command_DeleteForward(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
Delete(active.view, DIR_RIGHT);
} RegisterCommand(Command_DeleteForward, "delete");
void Command_DeleteForwardBoundary(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
Delete(active.view, DIR_RIGHT, SHIFT_PRESS);
} RegisterCommand(Command_DeleteForwardBoundary, "ctrl-delete");
void Command_InsertNewLineUp(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCursorToSide(active.view, DIR_LEFT);
IdentedNewLine(active.view);
MoveCarets(active.view, DIR_UP);
} RegisterCommand(Command_InsertNewLineUp, "ctrl-shift-enter");
void Command_InsertNewLineDown(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
MoveCursorToSide(active.view, DIR_RIGHT);
IdentedNewLine(active.view);
} RegisterCommand(Command_InsertNewLineDown, "ctrl-enter");
void Command_SelectFuzzySearchEntry(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
if (active.view->fuzzy_search) {
FuzzySearchOpen(ctx, active);
ctx->out_skip_rem_cmds = true;
}
} RegisterCommand(Command_SelectFuzzySearchEntry, "enter");
void Command_NewLine(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
IdentedNewLine(active.view);
} RegisterCommand(Command_NewLine, "enter");
void Command_CreateCaretOnNextFind(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
String16 string = GetString(active.buffer, active.view->carets[0].range);
Caret caret = FindNext(active.buffer, string, active.view->carets[0]);
Insert(&active.view->carets, caret, 0);
MergeCarets(active.buffer, &active.view->carets);
} RegisterCommand(Command_CreateCaretOnNextFind, "ctrl-d");
void Command_FocusWindow1(CommandContext *ctx) {
ActiveWindowID = GetOverlappingWindow({0,0}, GetWindow(ActiveWindowID))->id;
} RegisterCommand(Command_FocusWindow1, "ctrl-1");
void Command_FocusWindow2(CommandContext *ctx) {
Window *first = GetOverlappingWindow({0,0}, GetWindow(ActiveWindowID));
Vec2I p = GetSideOfWindow(first, DIR_RIGHT);
ActiveWindowID = GetOverlappingWindow(p, GetWindow(ActiveWindowID))->id;
} RegisterCommand(Command_FocusWindow2, "ctrl-2");
void Command_FocusWindow3(CommandContext *ctx) {
Window *first = GetOverlappingWindow({0,0});
if (first) {
Window *second = GetOverlappingWindow(GetSideOfWindow(first, DIR_RIGHT));
if (second) {
Window *third = GetOverlappingWindow(GetSideOfWindow(second, DIR_RIGHT));
if (third) {
ActiveWindowID = third->id;
}
}
}
} RegisterCommand(Command_FocusWindow3, "ctrl-3");
void Command_ClearCarets(CommandContext *ctx) {
BSet active = GetBSet(ActiveWindowID);
active.view->carets.len = 1;
active.view->carets[0] = MakeCaret(GetFront(active.view->carets[0]));
// @todo: move to hook post command???
if (active.window->lose_focus_on_escape && active.window->id == ActiveWindowID) {
if (active.window->layout) {
//
} else {
ActiveWindowID = LastActiveLayoutWindowID;
}
}
} RegisterCommand(Command_ClearCarets, "escape");