From 189bbdf3e1d5285fc0508d1a190036d6f4295558 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Sat, 2 Jan 2021 18:05:02 -0500 Subject: double/triple click to select word/line --- buffer.c | 23 +++++++++++++++++++++-- main.c | 21 ++++++++++++++++----- ted.cfg | 2 +- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/buffer.c b/buffer.c index 2e72e54..d964f50 100644 --- a/buffer.c +++ b/buffer.c @@ -737,8 +737,9 @@ bool buffer_pixels_to_pos(TextBuffer *buffer, v2 pixel_coords, BufferPos *pos) { if (display_line < 0 || display_line >= buffer_display_lines(buffer)) return false; - u32 column = (u32)round(display_col + buffer->scroll_x); u32 line = (u32)floor(display_line + buffer->scroll_y); + if (line >= buffer->nlines) line = buffer->nlines - 1; + u32 column = (u32)round(display_col + buffer->scroll_x); u32 index = buffer_column_to_index(buffer, line, column); pos->line = line; pos->index = index; @@ -1087,7 +1088,6 @@ void buffer_cursor_move_to_end_of_file(TextBuffer *buffer) { buffer_cursor_move_to_pos(buffer, buffer_pos_end_of_file(buffer)); } - // insert `number` empty lines starting at index `where`. static void buffer_insert_lines(TextBuffer *buffer, u32 where, u32 number) { u32 old_nlines = buffer->nlines; @@ -1250,6 +1250,25 @@ void buffer_select_to_end_of_file(TextBuffer *buffer) { buffer_select_to_pos(buffer, buffer_pos_end_of_file(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); + buffer_cursor_move_to_pos(buffer, end_pos); + buffer_select_to_pos(buffer, start_pos); +} + +// select the line the cursor is currently on +void buffer_select_line(TextBuffer *buffer) { + u32 line = buffer->cursor_pos.line; + if (line == buffer->nlines - 1) + buffer_cursor_move_to_pos(buffer, buffer_pos_end_of_line(buffer, line)); + else + buffer_cursor_move_to_pos(buffer, buffer_pos_start_of_line(buffer, line + 1)); + buffer_select_to_pos(buffer, buffer_pos_start_of_line(buffer, line)); +} + static void buffer_shorten_line(Line *line, u32 new_len) { assert(line->len >= new_len); diff --git a/main.c b/main.c index eca095b..9ece8ac 100644 --- a/main.c +++ b/main.c @@ -1,3 +1,7 @@ +// @TODO: +// - select all (:select-all) +// - blinking cursor (cursor-blink-time) +// - text size (text-size, :increase-text-size, :decrease-text-size) #include "base.h" no_warn_start #if _WIN32 @@ -13,9 +17,6 @@ no_warn_end #include "command.h" #include "util.c" #include "colors.c" -// @TODO: -// - blinking cursor (cursor-blink-time) -// - text size typedef struct { u32 colors[COLOR_COUNT]; u8 tab_width; @@ -142,10 +143,20 @@ int main(void) { case SDL_BUTTON_LEFT: { BufferPos pos = {0}; if (buffer_pixels_to_pos(buffer, V2((float)event.button.x, (float)event.button.y), &pos)) { - if (key_modifier == KEY_MODIFIER_SHIFT) + if (key_modifier == KEY_MODIFIER_SHIFT) { buffer_select_to_pos(buffer, pos); - else if (key_modifier == 0) + } else if (key_modifier == 0) { buffer_cursor_move_to_pos(buffer, pos); + switch ((event.button.clicks - 1) % 3) { + case 0: break; // single-click + case 1: // double-click: select word + buffer_select_word(buffer); + break; + case 2: // triple-click: select line + buffer_select_line(buffer); + break; + } + } } } break; } diff --git a/ted.cfg b/ted.cfg index 219ce43..aecf14e 100644 --- a/ted.cfg +++ b/ted.cfg @@ -52,6 +52,6 @@ Ctrl+Shift+z = :redo border = #a77 cursor-line-bg = #222 cursor = #3ff -selection-bg = #36a8 +selection-bg = #36aa text = #fff bg = #001 -- cgit v1.2.3