summaryrefslogtreecommitdiff
path: root/buffer.c
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2021-03-03 13:49:40 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2021-03-03 13:49:40 -0500
commitc59429d28718c1aae581f023d33466580118b0fc (patch)
tree3fc7abde6ab8a1d22f2856c490e9aaf3d8b06944 /buffer.c
parentb061785c4a18e668e4328680d75fde9412381b0e (diff)
speed up startup, improve left/right while selecting
Diffstat (limited to 'buffer.c')
-rw-r--r--buffer.c28
1 files changed, 23 insertions, 5 deletions
diff --git a/buffer.c b/buffer.c
index e45abeb..7489d04 100644
--- a/buffer.c
+++ b/buffer.c
@@ -1066,15 +1066,34 @@ void buffer_cursor_move_to_pos(TextBuffer *buffer, BufferPos pos) {
i64 buffer_cursor_move_left(TextBuffer *buffer, i64 by) {
BufferPos cur_pos = buffer->cursor_pos;
- i64 ret = buffer_pos_move_left(buffer, &cur_pos, by);
- buffer_cursor_move_to_pos(buffer, cur_pos);
+ i64 ret = 0;
+ // if use is selecting something, then moves left, the cursor should move to the left of the selection
+ if (buffer->selection) {
+ if (buffer_pos_cmp(buffer->selection_pos, buffer->cursor_pos) < 0) {
+ ret = buffer_pos_diff(buffer, buffer->selection_pos, buffer->cursor_pos);
+ buffer_cursor_move_to_pos(buffer, buffer->selection_pos);
+ }
+ buffer->selection = false;
+ } else {
+ ret = buffer_pos_move_left(buffer, &cur_pos, by);
+ buffer_cursor_move_to_pos(buffer, cur_pos);
+ }
return ret;
}
i64 buffer_cursor_move_right(TextBuffer *buffer, i64 by) {
BufferPos cur_pos = buffer->cursor_pos;
- i64 ret = buffer_pos_move_right(buffer, &cur_pos, by);
- buffer_cursor_move_to_pos(buffer, cur_pos);
+ i64 ret = 0;
+ if (buffer->selection) {
+ if (buffer_pos_cmp(buffer->selection_pos, buffer->cursor_pos) > 0) {
+ ret = buffer_pos_diff(buffer, buffer->cursor_pos, buffer->selection_pos);
+ buffer_cursor_move_to_pos(buffer, buffer->selection_pos);
+ }
+ buffer->selection = false;
+ } else {
+ ret = buffer_pos_move_right(buffer, &cur_pos, by);
+ buffer_cursor_move_to_pos(buffer, cur_pos);
+ }
return ret;
}
@@ -1785,7 +1804,6 @@ static void buffer_cursor_to_edit(TextBuffer *buffer, BufferEdit *edit) {
buffer->selection = false;
buffer_cursor_move_to_pos(buffer,
buffer_pos_advance(buffer, edit->pos, edit->prev_len));
- buffer_center_cursor(buffer); // whenever we undo an edit, put the cursor in the center, to make it clear where the undo happened
}
// a <-b <-c