summaryrefslogtreecommitdiff
path: root/buffer.c
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2021-01-02 18:05:02 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2021-01-02 18:05:02 -0500
commit189bbdf3e1d5285fc0508d1a190036d6f4295558 (patch)
treef7316b668b75085a5afdb880fe6712c4310e512f /buffer.c
parent496c8aeb321be70292837d15a14a0dfb0f17e33c (diff)
double/triple click to select word/line
Diffstat (limited to 'buffer.c')
-rw-r--r--buffer.c23
1 files changed, 21 insertions, 2 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);