Various fixes to copying with select-word and select-line, including

making it consistent with keys and with the mouse, and using other-end.
From Anindya Mukherjee.
This commit is contained in:
nicm 2020-03-19 13:28:52 +00:00
parent 7b0e688a96
commit 2cd8ea7680

View File

@ -87,9 +87,10 @@ static void window_copy_update_cursor(struct window_mode_entry *, u_int,
static void window_copy_start_selection(struct window_mode_entry *); static void window_copy_start_selection(struct window_mode_entry *);
static int window_copy_adjust_selection(struct window_mode_entry *, static int window_copy_adjust_selection(struct window_mode_entry *,
u_int *, u_int *); u_int *, u_int *);
static int window_copy_set_selection(struct window_mode_entry *, int); static int window_copy_set_selection(struct window_mode_entry *, int, int);
static int window_copy_update_selection(struct window_mode_entry *, int); static int window_copy_update_selection(struct window_mode_entry *, int,
static void window_copy_synchronize_cursor(struct window_mode_entry *); int);
static void window_copy_synchronize_cursor(struct window_mode_entry *, int);
static void *window_copy_get_selection(struct window_mode_entry *, size_t *); static void *window_copy_get_selection(struct window_mode_entry *, size_t *);
static void window_copy_copy_buffer(struct window_mode_entry *, static void window_copy_copy_buffer(struct window_mode_entry *,
const char *, void *, size_t); const char *, void *, size_t);
@ -505,7 +506,7 @@ window_copy_pageup1(struct window_mode_entry *wme, int half_page)
window_copy_cursor_end_of_line(wme); window_copy_cursor_end_of_line(wme);
} }
window_copy_update_selection(wme, 1); window_copy_update_selection(wme, 1, 0);
window_copy_redraw_screen(wme); window_copy_redraw_screen(wme);
} }
@ -553,7 +554,7 @@ window_copy_pagedown(struct window_mode_entry *wme, int half_page,
if (scroll_exit && data->oy == 0) if (scroll_exit && data->oy == 0)
return (1); return (1);
window_copy_update_selection(wme, 1); window_copy_update_selection(wme, 1, 0);
window_copy_redraw_screen(wme); window_copy_redraw_screen(wme);
return (0); return (0);
} }
@ -718,9 +719,6 @@ window_copy_cmd_begin_selection(struct window_copy_cmd_state *cs)
struct client *c = cs->c; struct client *c = cs->c;
struct mouse_event *m = cs->m; struct mouse_event *m = cs->m;
struct window_copy_mode_data *data = wme->data; struct window_copy_mode_data *data = wme->data;
struct options *oo = cs->s->options;
data->ws = options_get_string(oo, "word-separators");
if (m != NULL) { if (m != NULL) {
window_copy_start_drag(c, m); window_copy_start_drag(c, m);
@ -728,6 +726,7 @@ window_copy_cmd_begin_selection(struct window_copy_cmd_state *cs)
} }
data->lineflag = LINE_SEL_NONE; data->lineflag = LINE_SEL_NONE;
data->selflag = SEL_CHAR;
window_copy_start_selection(wme); window_copy_start_selection(wme);
return (WINDOW_COPY_CMD_REDRAW); return (WINDOW_COPY_CMD_REDRAW);
} }
@ -753,7 +752,7 @@ window_copy_cmd_bottom_line(struct window_copy_cmd_state *cs)
data->cx = 0; data->cx = 0;
data->cy = screen_size_y(&data->screen) - 1; data->cy = screen_size_y(&data->screen) - 1;
window_copy_update_selection(wme, 1); window_copy_update_selection(wme, 1, 0);
return (WINDOW_COPY_CMD_REDRAW); return (WINDOW_COPY_CMD_REDRAW);
} }
@ -810,12 +809,14 @@ window_copy_cmd_copy_line(struct window_copy_cmd_state *cs)
struct session *s = cs->s; struct session *s = cs->s;
struct winlink *wl = cs->wl; struct winlink *wl = cs->wl;
struct window_pane *wp = wme->wp; struct window_pane *wp = wme->wp;
struct window_copy_mode_data *data = wme->data;
u_int np = wme->prefix; u_int np = wme->prefix;
char *prefix = NULL; char *prefix = NULL;
if (cs->args->argc == 2) if (cs->args->argc == 2)
prefix = format_single(NULL, cs->args->argv[1], c, s, wl, wp); prefix = format_single(NULL, cs->args->argv[1], c, s, wl, wp);
data->selflag = SEL_CHAR;
window_copy_cursor_start_of_line(wme); window_copy_cursor_start_of_line(wme);
window_copy_start_selection(wme); window_copy_start_selection(wme);
for (; np > 1; np--) for (; np > 1; np--)
@ -995,7 +996,7 @@ window_copy_cmd_history_bottom(struct window_copy_cmd_state *cs)
data->cx = window_copy_find_length(wme, data->cy); data->cx = window_copy_find_length(wme, data->cy);
data->oy = 0; data->oy = 0;
window_copy_update_selection(wme, 1); window_copy_update_selection(wme, 1, 0);
return (WINDOW_COPY_CMD_REDRAW); return (WINDOW_COPY_CMD_REDRAW);
} }
@ -1014,7 +1015,7 @@ window_copy_cmd_history_top(struct window_copy_cmd_state *cs)
data->cx = 0; data->cx = 0;
data->oy = screen_hsize(data->backing); data->oy = screen_hsize(data->backing);
window_copy_update_selection(wme, 1); window_copy_update_selection(wme, 1, 0);
return (WINDOW_COPY_CMD_REDRAW); return (WINDOW_COPY_CMD_REDRAW);
} }
@ -1083,7 +1084,7 @@ window_copy_cmd_middle_line(struct window_copy_cmd_state *cs)
data->cx = 0; data->cx = 0;
data->cy = (screen_size_y(&data->screen) - 1) / 2; data->cy = (screen_size_y(&data->screen) - 1) / 2;
window_copy_update_selection(wme, 1); window_copy_update_selection(wme, 1, 0);
return (WINDOW_COPY_CMD_REDRAW); return (WINDOW_COPY_CMD_REDRAW);
} }
@ -1557,7 +1558,6 @@ window_copy_cmd_select_word(struct window_copy_cmd_state *cs)
struct window_mode_entry *wme = cs->wme; struct window_mode_entry *wme = cs->wme;
struct session *s = cs->s; struct session *s = cs->s;
struct window_copy_mode_data *data = wme->data; struct window_copy_mode_data *data = wme->data;
const char *ws;
u_int px, py; u_int px, py;
data->lineflag = LINE_SEL_LEFT_RIGHT; data->lineflag = LINE_SEL_LEFT_RIGHT;
@ -1569,18 +1569,18 @@ window_copy_cmd_select_word(struct window_copy_cmd_state *cs)
px = data->cx; px = data->cx;
py = screen_hsize(data->backing) + data->cy - data->oy; py = screen_hsize(data->backing) + data->cy - data->oy;
ws = options_get_string(s->options, "word-separators"); data->ws = options_get_string(s->options, "word-separators");
window_copy_cursor_previous_word(wme, ws, 0); window_copy_cursor_previous_word(wme, data->ws, 0);
data->selrx = data->cx; data->selrx = data->cx;
data->selry = screen_hsize(data->backing) + data->cy - data->oy; data->selry = screen_hsize(data->backing) + data->cy - data->oy;
window_copy_start_selection(wme); window_copy_start_selection(wme);
if (px >= window_copy_find_length(wme, py) || if (px >= window_copy_find_length(wme, py) ||
!window_copy_in_set(wme, px + 1, py, ws)) !window_copy_in_set(wme, px + 1, py, data->ws))
window_copy_cursor_next_word_end(wme, ws); window_copy_cursor_next_word_end(wme, data->ws);
else { else {
window_copy_update_cursor(wme, px, data->cy); window_copy_update_cursor(wme, px, data->cy);
if (window_copy_update_selection(wme, 1)) if (window_copy_update_selection(wme, 1, 0))
window_copy_redraw_lines(wme, data->cy, 1); window_copy_redraw_lines(wme, data->cy, 1);
} }
data->endselrx = data->cx; data->endselrx = data->cx;
@ -1607,7 +1607,7 @@ window_copy_cmd_top_line(struct window_copy_cmd_state *cs)
data->cx = 0; data->cx = 0;
data->cy = 0; data->cy = 0;
window_copy_update_selection(wme, 1); window_copy_update_selection(wme, 1, 0);
return (WINDOW_COPY_CMD_REDRAW); return (WINDOW_COPY_CMD_REDRAW);
} }
@ -2121,7 +2121,7 @@ window_copy_scroll_to(struct window_mode_entry *wme, u_int px, u_int py)
data->oy = gd->hsize - offset; data->oy = gd->hsize - offset;
} }
window_copy_update_selection(wme, 1); window_copy_update_selection(wme, 1, 0);
window_copy_redraw_screen(wme); window_copy_redraw_screen(wme);
} }
@ -2759,7 +2759,7 @@ window_copy_goto_line(struct window_mode_entry *wme, const char *linestr)
lineno = screen_hsize(data->backing); lineno = screen_hsize(data->backing);
data->oy = lineno; data->oy = lineno;
window_copy_update_selection(wme, 1); window_copy_update_selection(wme, 1, 0);
window_copy_redraw_screen(wme); window_copy_redraw_screen(wme);
} }
@ -2877,7 +2877,8 @@ window_copy_redraw_screen(struct window_mode_entry *wme)
} }
static void static void
window_copy_synchronize_cursor_end(struct window_mode_entry *wme, int begin) window_copy_synchronize_cursor_end(struct window_mode_entry *wme, int begin,
int no_reset)
{ {
struct window_copy_mode_data *data = wme->data; struct window_copy_mode_data *data = wme->data;
u_int xx, yy; u_int xx, yy;
@ -2885,10 +2886,10 @@ window_copy_synchronize_cursor_end(struct window_mode_entry *wme, int begin)
yy = screen_hsize(data->backing) + data->cy - data->oy; yy = screen_hsize(data->backing) + data->cy - data->oy;
switch (data->selflag) { switch (data->selflag) {
case SEL_WORD: case SEL_WORD:
begin = 0;
xx = data->cx; xx = data->cx;
if (data->ws == NULL) if (no_reset)
break; break;
begin = 0;
if (data->dy > yy || (data->dy == yy && data->dx > xx)) { if (data->dy > yy || (data->dy == yy && data->dx > xx)) {
/* Right to left selection. */ /* Right to left selection. */
window_copy_cursor_previous_word_pos(wme, data->ws, 0, window_copy_cursor_previous_word_pos(wme, data->ws, 0,
@ -2911,6 +2912,10 @@ window_copy_synchronize_cursor_end(struct window_mode_entry *wme, int begin)
} }
break; break;
case SEL_LINE: case SEL_LINE:
if (no_reset) {
xx = data->cx;
break;
}
begin = 0; begin = 0;
if (data->dy > yy) { if (data->dy > yy) {
/* Right to left selection. */ /* Right to left selection. */
@ -2943,16 +2948,16 @@ window_copy_synchronize_cursor_end(struct window_mode_entry *wme, int begin)
} }
static void static void
window_copy_synchronize_cursor(struct window_mode_entry *wme) window_copy_synchronize_cursor(struct window_mode_entry *wme, int no_reset)
{ {
struct window_copy_mode_data *data = wme->data; struct window_copy_mode_data *data = wme->data;
switch (data->cursordrag) { switch (data->cursordrag) {
case CURSORDRAG_ENDSEL: case CURSORDRAG_ENDSEL:
window_copy_synchronize_cursor_end(wme, 0); window_copy_synchronize_cursor_end(wme, 0, no_reset);
break; break;
case CURSORDRAG_SEL: case CURSORDRAG_SEL:
window_copy_synchronize_cursor_end(wme, 1); window_copy_synchronize_cursor_end(wme, 1, no_reset);
break; break;
case CURSORDRAG_NONE: case CURSORDRAG_NONE:
break; break;
@ -2994,7 +2999,7 @@ window_copy_start_selection(struct window_mode_entry *wme)
data->cursordrag = CURSORDRAG_ENDSEL; data->cursordrag = CURSORDRAG_ENDSEL;
window_copy_set_selection(wme, 1); window_copy_set_selection(wme, 1, 0);
} }
static int static int
@ -3031,18 +3036,20 @@ window_copy_adjust_selection(struct window_mode_entry *wme, u_int *selx,
} }
static int static int
window_copy_update_selection(struct window_mode_entry *wme, int may_redraw) window_copy_update_selection(struct window_mode_entry *wme, int may_redraw,
int no_reset)
{ {
struct window_copy_mode_data *data = wme->data; struct window_copy_mode_data *data = wme->data;
struct screen *s = &data->screen; struct screen *s = &data->screen;
if (s->sel == NULL && data->lineflag == LINE_SEL_NONE) if (s->sel == NULL && data->lineflag == LINE_SEL_NONE)
return (0); return (0);
return (window_copy_set_selection(wme, may_redraw)); return (window_copy_set_selection(wme, may_redraw, no_reset));
} }
static int static int
window_copy_set_selection(struct window_mode_entry *wme, int may_redraw) window_copy_set_selection(struct window_mode_entry *wme, int may_redraw,
int no_reset)
{ {
struct window_pane *wp = wme->wp; struct window_pane *wp = wme->wp;
struct window_copy_mode_data *data = wme->data; struct window_copy_mode_data *data = wme->data;
@ -3052,7 +3059,7 @@ window_copy_set_selection(struct window_mode_entry *wme, int may_redraw)
u_int sx, sy, cy, endsx, endsy; u_int sx, sy, cy, endsx, endsy;
int startrelpos, endrelpos; int startrelpos, endrelpos;
window_copy_synchronize_cursor(wme); window_copy_synchronize_cursor(wme, no_reset);
/* Adjust the selection. */ /* Adjust the selection. */
sx = data->selx; sx = data->selx;
@ -3408,7 +3415,7 @@ window_copy_cursor_start_of_line(struct window_mode_entry *wme)
} }
} }
window_copy_update_cursor(wme, 0, data->cy); window_copy_update_cursor(wme, 0, data->cy);
if (window_copy_update_selection(wme, 1)) if (window_copy_update_selection(wme, 1, 0))
window_copy_redraw_lines(wme, data->cy, 1); window_copy_redraw_lines(wme, data->cy, 1);
} }
@ -3431,7 +3438,7 @@ window_copy_cursor_back_to_indentation(struct window_mode_entry *wme)
} }
window_copy_update_cursor(wme, px, data->cy); window_copy_update_cursor(wme, px, data->cy);
if (window_copy_update_selection(wme, 1)) if (window_copy_update_selection(wme, 1, 0))
window_copy_redraw_lines(wme, data->cy, 1); window_copy_redraw_lines(wme, data->cy, 1);
} }
@ -3464,7 +3471,7 @@ window_copy_cursor_end_of_line(struct window_mode_entry *wme)
} }
window_copy_update_cursor(wme, px, data->cy); window_copy_update_cursor(wme, px, data->cy);
if (window_copy_update_selection(wme, 1)) if (window_copy_update_selection(wme, 1, 0))
window_copy_redraw_lines(wme, data->cy, 1); window_copy_redraw_lines(wme, data->cy, 1);
} }
@ -3515,7 +3522,7 @@ window_copy_other_end(struct window_mode_entry *wme)
} else } else
data->cy = cy + sely - yy; data->cy = cy + sely - yy;
window_copy_update_selection(wme, 1); window_copy_update_selection(wme, 1, 1);
window_copy_redraw_screen(wme); window_copy_redraw_screen(wme);
} }
@ -3539,7 +3546,7 @@ window_copy_cursor_left(struct window_mode_entry *wme)
window_copy_cursor_end_of_line(wme); window_copy_cursor_end_of_line(wme);
} else if (cx > 0) { } else if (cx > 0) {
window_copy_update_cursor(wme, cx - 1, data->cy); window_copy_update_cursor(wme, cx - 1, data->cy);
if (window_copy_update_selection(wme, 1)) if (window_copy_update_selection(wme, 1, 0))
window_copy_redraw_lines(wme, data->cy, 1); window_copy_redraw_lines(wme, data->cy, 1);
} }
} }
@ -3571,7 +3578,7 @@ window_copy_cursor_right(struct window_mode_entry *wme)
cx++; cx++;
} }
window_copy_update_cursor(wme, cx, data->cy); window_copy_update_cursor(wme, cx, data->cy);
if (window_copy_update_selection(wme, 1)) if (window_copy_update_selection(wme, 1, 0))
window_copy_redraw_lines(wme, data->cy, 1); window_copy_redraw_lines(wme, data->cy, 1);
} }
} }
@ -3604,7 +3611,7 @@ window_copy_cursor_up(struct window_mode_entry *wme, int scroll_only)
} }
} else { } else {
window_copy_update_cursor(wme, data->lastcx, data->cy - 1); window_copy_update_cursor(wme, data->lastcx, data->cy - 1);
if (window_copy_update_selection(wme, 1)) { if (window_copy_update_selection(wme, 1, 0)) {
if (data->cy == screen_size_y(s) - 1) if (data->cy == screen_size_y(s) - 1)
window_copy_redraw_lines(wme, data->cy, 1); window_copy_redraw_lines(wme, data->cy, 1);
else else
@ -3650,7 +3657,7 @@ window_copy_cursor_down(struct window_mode_entry *wme, int scroll_only)
window_copy_redraw_lines(wme, data->cy - 1, 2); window_copy_redraw_lines(wme, data->cy - 1, 2);
} else { } else {
window_copy_update_cursor(wme, data->lastcx, data->cy + 1); window_copy_update_cursor(wme, data->lastcx, data->cy + 1);
if (window_copy_update_selection(wme, 1)) if (window_copy_update_selection(wme, 1, 0))
window_copy_redraw_lines(wme, data->cy - 1, 2); window_copy_redraw_lines(wme, data->cy - 1, 2);
} }
@ -3685,7 +3692,7 @@ window_copy_cursor_jump(struct window_mode_entry *wme)
if (!(gc.flags & GRID_FLAG_PADDING) && if (!(gc.flags & GRID_FLAG_PADDING) &&
gc.data.size == 1 && *gc.data.data == data->jumpchar) { gc.data.size == 1 && *gc.data.data == data->jumpchar) {
window_copy_update_cursor(wme, px, data->cy); window_copy_update_cursor(wme, px, data->cy);
if (window_copy_update_selection(wme, 1)) if (window_copy_update_selection(wme, 1, 0))
window_copy_redraw_lines(wme, data->cy, 1); window_copy_redraw_lines(wme, data->cy, 1);
return; return;
} }
@ -3712,7 +3719,7 @@ window_copy_cursor_jump_back(struct window_mode_entry *wme)
if (!(gc.flags & GRID_FLAG_PADDING) && if (!(gc.flags & GRID_FLAG_PADDING) &&
gc.data.size == 1 && *gc.data.data == data->jumpchar) { gc.data.size == 1 && *gc.data.data == data->jumpchar) {
window_copy_update_cursor(wme, px, data->cy); window_copy_update_cursor(wme, px, data->cy);
if (window_copy_update_selection(wme, 1)) if (window_copy_update_selection(wme, 1, 0))
window_copy_redraw_lines(wme, data->cy, 1); window_copy_redraw_lines(wme, data->cy, 1);
return; return;
} }
@ -3739,7 +3746,7 @@ window_copy_cursor_jump_to(struct window_mode_entry *wme)
if (!(gc.flags & GRID_FLAG_PADDING) && if (!(gc.flags & GRID_FLAG_PADDING) &&
gc.data.size == 1 && *gc.data.data == data->jumpchar) { gc.data.size == 1 && *gc.data.data == data->jumpchar) {
window_copy_update_cursor(wme, px - 1, data->cy); window_copy_update_cursor(wme, px - 1, data->cy);
if (window_copy_update_selection(wme, 1)) if (window_copy_update_selection(wme, 1, 0))
window_copy_redraw_lines(wme, data->cy, 1); window_copy_redraw_lines(wme, data->cy, 1);
return; return;
} }
@ -3769,7 +3776,7 @@ window_copy_cursor_jump_to_back(struct window_mode_entry *wme)
if (!(gc.flags & GRID_FLAG_PADDING) && if (!(gc.flags & GRID_FLAG_PADDING) &&
gc.data.size == 1 && *gc.data.data == data->jumpchar) { gc.data.size == 1 && *gc.data.data == data->jumpchar) {
window_copy_update_cursor(wme, px + 1, data->cy); window_copy_update_cursor(wme, px + 1, data->cy);
if (window_copy_update_selection(wme, 1)) if (window_copy_update_selection(wme, 1, 0))
window_copy_redraw_lines(wme, data->cy, 1); window_copy_redraw_lines(wme, data->cy, 1);
return; return;
} }
@ -3818,7 +3825,7 @@ window_copy_cursor_next_word(struct window_mode_entry *wme,
} while (expected == 1); } while (expected == 1);
window_copy_update_cursor(wme, px, data->cy); window_copy_update_cursor(wme, px, data->cy);
if (window_copy_update_selection(wme, 1)) if (window_copy_update_selection(wme, 1, 0))
window_copy_redraw_lines(wme, data->cy, 1); window_copy_redraw_lines(wme, data->cy, 1);
} }
@ -3919,7 +3926,7 @@ window_copy_cursor_next_word_end(struct window_mode_entry *wme,
px--; px--;
window_copy_update_cursor(wme, px, data->cy); window_copy_update_cursor(wme, px, data->cy);
if (window_copy_update_selection(wme, 1)) if (window_copy_update_selection(wme, 1, 0))
window_copy_redraw_lines(wme, data->cy, 1); window_copy_redraw_lines(wme, data->cy, 1);
} }
@ -4016,7 +4023,7 @@ window_copy_cursor_previous_word(struct window_mode_entry *wme,
out: out:
window_copy_update_cursor(wme, px, data->cy); window_copy_update_cursor(wme, px, data->cy);
if (window_copy_update_selection(wme, 1)) if (window_copy_update_selection(wme, 1, 0))
window_copy_redraw_lines(wme, data->cy, 1); window_copy_redraw_lines(wme, data->cy, 1);
} }
@ -4034,7 +4041,7 @@ window_copy_scroll_up(struct window_mode_entry *wme, u_int ny)
return; return;
data->oy -= ny; data->oy -= ny;
window_copy_update_selection(wme, 0); window_copy_update_selection(wme, 0, 0);
screen_write_start(&ctx, wp, NULL); screen_write_start(&ctx, wp, NULL);
screen_write_cursormove(&ctx, 0, 0, 0); screen_write_cursormove(&ctx, 0, 0, 0);
@ -4068,7 +4075,7 @@ window_copy_scroll_down(struct window_mode_entry *wme, u_int ny)
return; return;
data->oy += ny; data->oy += ny;
window_copy_update_selection(wme, 0); window_copy_update_selection(wme, 0, 0);
screen_write_start(&ctx, wp, NULL); screen_write_start(&ctx, wp, NULL);
screen_write_cursormove(&ctx, 0, 0, 0); screen_write_cursormove(&ctx, 0, 0, 0);
@ -4095,7 +4102,7 @@ window_copy_rectangle_toggle(struct window_mode_entry *wme)
if (data->cx > px) if (data->cx > px)
window_copy_update_cursor(wme, px, data->cy); window_copy_update_cursor(wme, px, data->cy);
window_copy_update_selection(wme, 1); window_copy_update_selection(wme, 1, 0);
window_copy_redraw_screen(wme); window_copy_redraw_screen(wme);
} }
@ -4206,7 +4213,7 @@ window_copy_drag_update(struct client *c, struct mouse_event *m)
old_cy = data->cy; old_cy = data->cy;
window_copy_update_cursor(wme, x, y); window_copy_update_cursor(wme, x, y);
if (window_copy_update_selection(wme, 1)) if (window_copy_update_selection(wme, 1, 0))
window_copy_redraw_selection(wme, old_cy); window_copy_redraw_selection(wme, old_cy);
if (old_cy != data->cy || old_cx == data->cx) { if (old_cy != data->cy || old_cx == data->cx) {
if (y == 0) { if (y == 0) {