From 376d732e48b89b0a0a392e2582826775e2d4a2d1 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Tue, 23 Feb 2021 10:44:09 -0500 Subject: improve word movement --- buffer.c | 21 +++++++-------------- main.c | 1 - 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/buffer.c b/buffer.c index 08d40df..378d5c7 100644 --- a/buffer.c +++ b/buffer.c @@ -1051,12 +1051,8 @@ i64 buffer_pos_move_words(TextBuffer *buffer, BufferPos *pos, i64 nwords) { pos->index = 0; } } else { - // move past any whitespace before the word - while (index < line->len && is_space(str[index])) - ++index; - bool starting_isword = is_word(str[index]) != 0; - for (; index < line->len && !is_space(str[index]); ++index) { + for (; index < line->len; ++index) { bool this_isword = is_word(str[index]) != 0; if (this_isword != starting_isword) { // either the position *was* on an alphanumeric character and now it's not @@ -1065,9 +1061,6 @@ i64 buffer_pos_move_words(TextBuffer *buffer, BufferPos *pos, i64 nwords) { } } - // move past any whitespace after the word - while (index < line->len && is_space(str[index])) - ++index; pos->index = index; } } @@ -1089,14 +1082,11 @@ i64 buffer_pos_move_words(TextBuffer *buffer, BufferPos *pos, i64 nwords) { } } else { --index; - - while (index > 0 && is_space(str[index])) // skip whitespace after word - --index; if (index > 0) { bool starting_isword = is_word(str[index]) != 0; while (true) { bool this_isword = is_word(str[index]) != 0; - if (is_space(str[index]) || this_isword != starting_isword) { + if (this_isword != starting_isword) { ++index; break; } @@ -1388,8 +1378,11 @@ void buffer_select_to_end_of_file(TextBuffer *buffer) { // select the word the cursor is inside of void buffer_select_word(TextBuffer *buffer) { BufferPos start_pos = buffer->cursor_pos, end_pos = buffer->cursor_pos; - buffer_pos_move_left_words(buffer, &start_pos, 1); - buffer_pos_move_right_words(buffer, &end_pos, 1); + if (start_pos.index > 0) + buffer_pos_move_left_words(buffer, &start_pos, 1); + if (end_pos.index < buffer->lines[end_pos.line].len) + buffer_pos_move_right_words(buffer, &end_pos, 1); + buffer_cursor_move_to_pos(buffer, end_pos); buffer_select_to_pos(buffer, start_pos); } diff --git a/main.c b/main.c index aeef6c1..72e3ff1 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,4 @@ // @TODO: -// - improve word movement // - goto line // - :run -- if .html file, open in browser, otherwise figure out a way of sending a command to shell -- cgit v1.2.3