add 'o' vim command

memory-refactor
Patrick Cleavelin 2025-01-04 17:28:56 -05:00
parent e4af6d3403
commit 55f6005d58
2 changed files with 25 additions and 9 deletions

View File

@ -428,13 +428,16 @@ move_cursor_start_of_line :: proc(buffer: ^FileBuffer) {
} }
} }
move_cursor_end_of_line :: proc(buffer: ^FileBuffer) { move_cursor_end_of_line :: proc(buffer: ^FileBuffer, stop_at_end: bool = true) {
it := new_file_buffer_iter_with_cursor(buffer, buffer.cursor); it := new_file_buffer_iter_with_cursor(buffer, buffer.cursor);
line_length := file_buffer_line_length(buffer, it.cursor.index); line_length := file_buffer_line_length(buffer, it.cursor.index);
if stop_at_end {
line_length -= 1;
}
if buffer.cursor.col < line_length-1 { if buffer.cursor.col < line_length {
for _ in iterate_file_buffer(&it) { for _ in iterate_file_buffer(&it) {
if it.cursor.col >= line_length-1 { if it.cursor.col >= line_length {
break; break;
} }
} }
@ -497,17 +500,19 @@ move_cursor_down :: proc(buffer: ^FileBuffer, amount: int = 1) {
move_cursor_left :: proc(buffer: ^FileBuffer) { move_cursor_left :: proc(buffer: ^FileBuffer) {
if buffer.cursor.col > 0 { if buffer.cursor.col > 0 {
buffer.cursor.col -= 1; it := new_file_buffer_iter_with_cursor(buffer, buffer.cursor);
update_file_buffer_index_from_cursor(buffer); iterate_file_buffer_reverse(&it);
buffer.cursor = it.cursor;
} }
} }
move_cursor_right :: proc(buffer: ^FileBuffer, stop_at_end: bool = true) { move_cursor_right :: proc(buffer: ^FileBuffer, stop_at_end: bool = true) {
line_length := file_buffer_line_length(buffer, buffer.cursor.index); it := new_file_buffer_iter_with_cursor(buffer, buffer.cursor);
line_length := file_buffer_line_length(buffer, it.cursor.index);
if !stop_at_end || (line_length > 0 && buffer.cursor.col < line_length-1) { if !stop_at_end || buffer.cursor.col < line_length-1 {
buffer.cursor.col += 1; iterate_file_buffer(&it);
update_file_buffer_index_from_cursor(buffer); buffer.cursor = it.cursor;
} }
} }

View File

@ -161,6 +161,17 @@ register_default_input_actions :: proc(input_map: ^core.InputMap) {
state.mode = .Insert; state.mode = .Insert;
sdl2.StartTextInput(); sdl2.StartTextInput();
}, "enter insert mode after character (append)"); }, "enter insert mode after character (append)");
// TODO: add shift+o to insert newline above current one
core.register_key_action(input_map, .O, proc(state: ^State) {
core.move_cursor_end_of_line(&state.buffers[state.current_buffer], false);
core.insert_content(&state.buffers[state.current_buffer], []u8{'\n'});
core.move_cursor_down(&state.buffers[state.current_buffer]);
state.mode = .Insert;
sdl2.StartTextInput();
}, "insert mode on newline");
} }
core.register_key_action(input_map, .SPACE, core.new_input_map(), "leader commands"); core.register_key_action(input_map, .SPACE, core.new_input_map(), "leader commands");