From 8ea0384e507f988d10e3b69897d9ec77cc3611a9 Mon Sep 17 00:00:00 2001 From: Iain King Date: Thu, 26 Sep 2024 11:30:47 +0100 Subject: [PATCH] Amended creating cursor above/below to skip lines, added create_cursors_around If the current cursor is not at the start of the line, when the user creates a cursor above or below it, empty lines will be skipped. This is a lot more ergonomic; almost always this is what the user wants to do. The old behaviour is preserved when the cursor is at the start of the line (for instance, when on an empty line). create_cursors_around: Cursors will be added to every contiguous line above and below the current cursor as long as the line in question reaches to the cursor column. --- src/editors.jai | 57 +++++++++++++++++++++++++++++++++++++++++++++---- src/keymap.jai | 1 + 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/editors.jai b/src/editors.jai index 4742de74..ef6f06a4 100644 --- a/src/editors.jai +++ b/src/editors.jai @@ -215,6 +215,7 @@ active_editor_handle_event :: (editor: *Editor, buffer: *Buffer, event: Input.Ev case .select_all; select_all (editor, buffer); keep_selection = true; case .create_cursor_above; create_cursor (editor, buffer, .above); keep_selection = true; case .create_cursor_below; create_cursor (editor, buffer, .below); keep_selection = true; + case .create_cursors_around; create_cursors_around (editor, buffer); keep_selection = true; case .strip_trailing_whitespace; strip_trailing_whitespace (buffer, editor.buffer_id); keep_selection = true; case .choose_language; show_dialog (*language_dialog); @@ -1552,11 +1553,16 @@ create_cursor :: (using editor: *Editor, buffer: Buffer, where: enum { above; be cursor := *cursors[cursors.count-1]; pos_coords := offset_to_coords(editor, buffer, cursor.pos); - if where == .above then pos_coords.line -= 1; else pos_coords.line += 1; - pos_coords.col = original_pos_coords.col; + while true { + if where == .above then pos_coords.line -= 1; else pos_coords.line += 1; + pos_coords.col = original_pos_coords.col; + + cursor.pos = coords_to_offset(editor, buffer, pos_coords); + cursor.col_wanted = original_pos_coords.col; - cursor.pos = coords_to_offset(editor, buffer, pos_coords); - cursor.col_wanted = original_pos_coords.col; + if cursor.pos <= 0 || cursor.pos >= buffer.bytes.count || original_pos_coords.col == 0 || offset_to_coords(editor, buffer, cursor.pos).col > 0 + break; + } if has_selection(original) { sel_coords := pos_coords; @@ -1574,6 +1580,49 @@ create_cursor :: (using editor: *Editor, buffer: Buffer, where: enum { above; be cursor_moved = .refresh_only; } +create_cursors_around :: (using editor: *Editor, buffer: Buffer) { + organise_cursors(editor); + + top_cursor := cursors[0]; + bottom_cursor := cursors[cursors.count-1]; + original := cursors[original_cursor]; + original_pos_coords := offset_to_coords(editor, buffer, original.pos); + original_sel_coords := offset_to_coords(editor, buffer, original.sel); + + add_cursors :: (position: Coords, direction: s32, terminator_line: int) #expand { + while position.line != terminator_line { + position.line += direction; + start := get_line_start_offset(editor, buffer, position.line); + end := get_line_end_offset(editor, buffer, position.line); + if end - start < original_pos_coords.col + break; + + cursor := add(*cursors); + <