commit 32ba7c8e9dbb2f7f4327b2a989e07d02e84ee2e1
parent e76057dd60a70a13d5ede8274d504494d2ee70a8
Author: lumidify <nobody@lumidify.org>
Date: Wed, 17 Nov 2021 23:38:54 +0100
Respect cursor x position when scrolling (Ctrl-d/Ctrl-u)
Diffstat:
2 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/buffer.c b/buffer.c
@@ -1456,6 +1456,7 @@ ledit_pos_to_x_softline(ledit_line *line, int pos, int *x_ret, int *softline_ret
/* if in normal mode, change position to the middle of the
current rectangle so that moving around won't jump weirdly */
/* FIXME: also in visual? */
+ /* FIXME: this is too much magic for my taste */
if (line->parent_buffer->common->mode == NORMAL) {
PangoRectangle rect;
pango_layout_index_to_pos(line->layout, pos, &rect);
diff --git a/keys_basic.c b/keys_basic.c
@@ -642,14 +642,15 @@ move_to_line(ledit_buffer *buffer, char *text, int len) {
}
/* FIXME: should these scrolling functions change behavior when hard_line_based == 1? */
-/* FIXME: preserve x position on these scrolling functions */
static void
scroll_lines(ledit_buffer *buffer, int lines, int dir) {
int final_lines;
int text_w, text_h;
ledit_line *ll = ledit_buffer_get_line(buffer, buffer->cur_line);
- int x, y, h;
+ int x, y, h, sli;
ledit_buffer_get_cursor_pixel_pos(buffer, buffer->cur_line, buffer->cur_index, &x, &y, &h);
+ /* get the middle position of char */
+ ledit_pos_to_x_softline(ll, buffer->cur_index, &x, &sli);
long abs_pos = ll->y_offset + y;
ledit_window_get_textview_size(buffer->window, &text_w, &text_h);
if (lines > 0)
@@ -659,7 +660,6 @@ scroll_lines(ledit_buffer *buffer, int lines, int dir) {
else
final_lines = text_h / h / 2;
ledit_buffer_wipe_line_cursor_attrs(buffer, buffer->cur_line);
- int sli; /* FIXME: set proper cursor position */
get_new_line_softline(
buffer, buffer->cur_line, buffer->cur_index,
dir < 0 ? -final_lines : final_lines,
@@ -667,7 +667,8 @@ scroll_lines(ledit_buffer *buffer, int lines, int dir) {
);
int start, end;
ledit_buffer_get_softline_bounds(buffer, buffer->cur_line, sli, &start, &end);
- buffer->cur_index = start;
+ ll = ledit_buffer_get_line(buffer, buffer->cur_line);
+ ledit_x_softline_to_pos(ll, x, sli, &buffer->cur_index);
ledit_buffer_get_cursor_pixel_pos(buffer, buffer->cur_line, buffer->cur_index, &x, &y, &h);
long new_abs_pos = ll->y_offset + y;
ledit_buffer_scroll(buffer, buffer->display_offset + (new_abs_pos - abs_pos));