From 4d8bb4cb3c4549145a9c76f960168bdee12d9a04 Mon Sep 17 00:00:00 2001 From: nicm Date: Fri, 22 Jan 2021 10:21:24 +0000 Subject: [PATCH] Fix some cursor movement commands, from Anindya Mukherjee. --- usr.bin/tmux/window-copy.c | 100 +++++++++++++++++++++---------------- 1 file changed, 57 insertions(+), 43 deletions(-) diff --git a/usr.bin/tmux/window-copy.c b/usr.bin/tmux/window-copy.c index 54cd1b0f377..5ae90e0ad48 100644 --- a/usr.bin/tmux/window-copy.c +++ b/usr.bin/tmux/window-copy.c @@ -1,4 +1,4 @@ -/* $OpenBSD: window-copy.c,v 1.311 2021/01/18 11:14:24 nicm Exp $ */ +/* $OpenBSD: window-copy.c,v 1.312 2021/01/22 10:21:24 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -3981,11 +3981,12 @@ window_copy_cursor_start_of_line(struct window_mode_entry *wme) struct window_copy_mode_data *data = wme->data; struct screen *back_s = data->backing; struct grid_reader gr; - u_int px, py, cy, yy, ny, hsize; + u_int px, py, cy, oldy, yy, ny, nd, hsize; px = data->cx; hsize = screen_hsize(back_s); py = hsize + data->cy - data->oy; + oldy = data->cy; grid_reader_start(&gr, back_s->grid, px, py); grid_reader_cursor_start_of_line(&gr, 1); @@ -3996,9 +3997,11 @@ window_copy_cursor_start_of_line(struct window_mode_entry *wme) if (py < yy) { ny = yy - py; cy = 0; + nd = 1; } else { ny = 0; cy = py - yy; + nd = oldy - cy + 1; } while (ny > 0) { window_copy_cursor_up(wme, 1); @@ -4006,7 +4009,7 @@ window_copy_cursor_start_of_line(struct window_mode_entry *wme) } window_copy_update_cursor(wme, px, cy); if (window_copy_update_selection(wme, 1, 0)) - window_copy_redraw_lines(wme, data->cy, 1); + window_copy_redraw_lines(wme, data->cy, nd); } static void @@ -4038,11 +4041,12 @@ window_copy_cursor_end_of_line(struct window_mode_entry *wme) struct window_copy_mode_data *data = wme->data; struct screen *back_s = data->backing; struct grid_reader gr; - u_int px, py, cy, yy, ny, hsize; + u_int px, py, cy, oldy, yy, ny, nd, hsize; px = data->cx; hsize = screen_hsize(back_s); py = hsize + data->cy - data->oy; + oldy = data->cy; grid_reader_start(&gr, back_s->grid, px, py); if (data->screen.sel != NULL && data->rectflag) @@ -4054,10 +4058,14 @@ window_copy_cursor_end_of_line(struct window_mode_entry *wme) /* Scroll down if we went off the visible screen. */ cy = py - hsize + data->oy; yy = screen_size_y(back_s) - 1; - if (cy > yy) + if (cy > yy) { ny = cy - yy; - else + oldy = yy; + nd = 1; + } else { ny = 0; + nd = cy - oldy + 1; + } while (ny > 0) { window_copy_cursor_down(wme, 1); ny--; @@ -4067,7 +4075,7 @@ window_copy_cursor_end_of_line(struct window_mode_entry *wme) else window_copy_update_cursor(wme, px, cy); if (window_copy_update_selection(wme, 1, 0)) - window_copy_redraw_lines(wme, data->cy, 1); + window_copy_redraw_lines(wme, oldy, nd); } static void @@ -4433,41 +4441,39 @@ window_copy_cursor_next_word(struct window_mode_entry *wme, { struct window_copy_mode_data *data = wme->data; struct screen *back_s = data->backing; - u_int px, py, xx, yy; - int expected = 0; + struct grid_reader gr; + u_int px, py, cy, oldy, yy, ny, nd, hsize; px = data->cx; - py = screen_hsize(back_s) + data->cy - data->oy; - xx = window_copy_find_length(wme, py); - yy = screen_hsize(back_s) + screen_size_y(back_s) - 1; - - /* - * First skip past any nonword characters and then any word characters. - * - * expected is initially set to 0 for the former and then 1 for the - * latter. - */ - do { - while (px > xx || - window_copy_in_set(wme, px, py, separators) == expected) { - /* Move down if we're past the end of the line. */ - if (px > xx) { - if (py == yy) - return; - window_copy_cursor_down(wme, 0); - px = 0; + hsize = screen_hsize(back_s); + py = hsize + data->cy - data->oy; + oldy = data->cy; - py = screen_hsize(back_s) + data->cy - data->oy; - xx = window_copy_find_length(wme, py); - } else - px++; - } - expected = !expected; - } while (expected == 1); + grid_reader_start(&gr, back_s->grid, px, py); + grid_reader_cursor_next_word(&gr, separators); + grid_reader_get_cursor(&gr, &px, &py); - window_copy_update_cursor(wme, px, data->cy); + /* Scroll down if we went off the visible screen. */ + cy = py - hsize + data->oy; + yy = screen_size_y(back_s) - 1; + if (cy > yy) { + ny = cy - yy; + oldy = yy; + nd = 1; + } else { + ny = 0; + nd = cy - oldy + 1; + } + while (ny > 0) { + window_copy_cursor_down(wme, 1); + ny--; + } + if (cy > yy) + window_copy_update_cursor(wme, px, yy); + else + window_copy_update_cursor(wme, px, cy); if (window_copy_update_selection(wme, 1, 0)) - window_copy_redraw_lines(wme, data->cy, 1); + window_copy_redraw_lines(wme, oldy, nd); } static void @@ -4528,12 +4534,13 @@ window_copy_cursor_next_word_end(struct window_mode_entry *wme, struct options *oo = wp->window->options; struct screen *back_s = data->backing; struct grid_reader gr; - u_int px, py, cy, yy, ny, hsize; + u_int px, py, cy, oldy, yy, ny, nd, hsize; int keys; px = data->cx; hsize = screen_hsize(back_s); py = hsize + data->cy - data->oy; + oldy = data->cy; grid_reader_start(&gr, back_s->grid, px, py); keys = options_get_number(oo, "mode-keys"); @@ -4547,10 +4554,14 @@ window_copy_cursor_next_word_end(struct window_mode_entry *wme, /* Scroll down if we went off the visible screen. */ cy = py - hsize + data->oy; yy = screen_size_y(back_s) - 1; - if (cy > yy) + if (cy > yy) { ny = cy - yy; - else + oldy = yy; + nd = 1; + } else { ny = 0; + nd = cy - oldy + 1; + } while (ny > 0) { window_copy_cursor_down(wme, 1); ny--; @@ -4560,7 +4571,7 @@ window_copy_cursor_next_word_end(struct window_mode_entry *wme, else window_copy_update_cursor(wme, px, cy); if (window_copy_update_selection(wme, 1, no_reset)) - window_copy_redraw_lines(wme, data->cy, 1); + window_copy_redraw_lines(wme, oldy, nd); } /* Compute the previous place where a word begins. */ @@ -4617,11 +4628,12 @@ window_copy_cursor_previous_word(struct window_mode_entry *wme, struct window_copy_mode_data *data = wme->data; struct screen *back_s = data->backing; struct grid_reader gr; - u_int px, py, cy, yy, ny, hsize; + u_int px, py, cy, oldy, yy, ny, nd, hsize; px = data->cx; hsize = screen_hsize(back_s); py = hsize + data->cy - data->oy; + oldy = data->cy; grid_reader_start(&gr, back_s->grid, px, py); grid_reader_cursor_previous_word(&gr, separators, already); @@ -4632,9 +4644,11 @@ window_copy_cursor_previous_word(struct window_mode_entry *wme, if (py < yy) { ny = yy - py; cy = 0; + nd = 1; } else { ny = 0; cy = py - yy; + nd = oldy - cy + 1; } while (ny > 0) { window_copy_cursor_up(wme, 1); @@ -4642,7 +4656,7 @@ window_copy_cursor_previous_word(struct window_mode_entry *wme, } window_copy_update_cursor(wme, px, cy); if (window_copy_update_selection(wme, 1, 0)) - window_copy_redraw_lines(wme, data->cy, 1); + window_copy_redraw_lines(wme, data->cy, nd); } static void -- 2.20.1