Add support for marking lines with a shell prompt based on the OSC 133
[tmux-openbsd.git] / window-copy.c
blobb0f140984c41b9d55bf49f4ee9647c680046709d
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
21 #include <ctype.h>
22 #include <regex.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
27 #include "tmux.h"
29 struct window_copy_mode_data;
31 static const char *window_copy_key_table(struct window_mode_entry *);
32 static void window_copy_command(struct window_mode_entry *, struct client *,
33 struct session *, struct winlink *, struct args *,
34 struct mouse_event *);
35 static struct screen *window_copy_init(struct window_mode_entry *,
36 struct cmd_find_state *, struct args *);
37 static struct screen *window_copy_view_init(struct window_mode_entry *,
38 struct cmd_find_state *, struct args *);
39 static void window_copy_free(struct window_mode_entry *);
40 static void window_copy_resize(struct window_mode_entry *, u_int, u_int);
41 static void window_copy_formats(struct window_mode_entry *,
42 struct format_tree *);
43 static void window_copy_pageup1(struct window_mode_entry *, int);
44 static int window_copy_pagedown(struct window_mode_entry *, int, int);
45 static void window_copy_next_paragraph(struct window_mode_entry *);
46 static void window_copy_previous_paragraph(struct window_mode_entry *);
47 static void window_copy_redraw_selection(struct window_mode_entry *, u_int);
48 static void window_copy_redraw_lines(struct window_mode_entry *, u_int,
49 u_int);
50 static void window_copy_redraw_screen(struct window_mode_entry *);
51 static void window_copy_write_line(struct window_mode_entry *,
52 struct screen_write_ctx *, u_int);
53 static void window_copy_write_lines(struct window_mode_entry *,
54 struct screen_write_ctx *, u_int, u_int);
55 static char *window_copy_match_at_cursor(struct window_copy_mode_data *);
56 static void window_copy_scroll_to(struct window_mode_entry *, u_int, u_int,
57 int);
58 static int window_copy_search_compare(struct grid *, u_int, u_int,
59 struct grid *, u_int, int);
60 static int window_copy_search_lr(struct grid *, struct grid *, u_int *,
61 u_int, u_int, u_int, int);
62 static int window_copy_search_rl(struct grid *, struct grid *, u_int *,
63 u_int, u_int, u_int, int);
64 static int window_copy_last_regex(struct grid *, u_int, u_int, u_int,
65 u_int, u_int *, u_int *, const char *, const regex_t *,
66 int);
67 static int window_copy_search_mark_at(struct window_copy_mode_data *,
68 u_int, u_int, u_int *);
69 static char *window_copy_stringify(struct grid *, u_int, u_int, u_int,
70 char *, u_int *);
71 static void window_copy_cstrtocellpos(struct grid *, u_int, u_int *,
72 u_int *, const char *);
73 static int window_copy_search_marks(struct window_mode_entry *,
74 struct screen *, int, int);
75 static void window_copy_clear_marks(struct window_mode_entry *);
76 static int window_copy_is_lowercase(const char *);
77 static void window_copy_search_back_overlap(struct grid *, regex_t *,
78 u_int *, u_int *, u_int *, u_int);
79 static int window_copy_search_jump(struct window_mode_entry *,
80 struct grid *, struct grid *, u_int, u_int, u_int, int, int,
81 int, int);
82 static int window_copy_search(struct window_mode_entry *, int, int);
83 static int window_copy_search_up(struct window_mode_entry *, int);
84 static int window_copy_search_down(struct window_mode_entry *, int);
85 static void window_copy_goto_line(struct window_mode_entry *, const char *);
86 static void window_copy_update_cursor(struct window_mode_entry *, u_int,
87 u_int);
88 static void window_copy_start_selection(struct window_mode_entry *);
89 static int window_copy_adjust_selection(struct window_mode_entry *,
90 u_int *, u_int *);
91 static int window_copy_set_selection(struct window_mode_entry *, int, int);
92 static int window_copy_update_selection(struct window_mode_entry *, int,
93 int);
94 static void window_copy_synchronize_cursor(struct window_mode_entry *, int);
95 static void *window_copy_get_selection(struct window_mode_entry *, size_t *);
96 static void window_copy_copy_buffer(struct window_mode_entry *,
97 const char *, void *, size_t);
98 static void window_copy_pipe(struct window_mode_entry *,
99 struct session *, const char *);
100 static void window_copy_copy_pipe(struct window_mode_entry *,
101 struct session *, const char *, const char *);
102 static void window_copy_copy_selection(struct window_mode_entry *,
103 const char *);
104 static void window_copy_append_selection(struct window_mode_entry *);
105 static void window_copy_clear_selection(struct window_mode_entry *);
106 static void window_copy_copy_line(struct window_mode_entry *, char **,
107 size_t *, u_int, u_int, u_int);
108 static int window_copy_in_set(struct window_mode_entry *, u_int, u_int,
109 const char *);
110 static u_int window_copy_find_length(struct window_mode_entry *, u_int);
111 static void window_copy_cursor_start_of_line(struct window_mode_entry *);
112 static void window_copy_cursor_back_to_indentation(
113 struct window_mode_entry *);
114 static void window_copy_cursor_end_of_line(struct window_mode_entry *);
115 static void window_copy_other_end(struct window_mode_entry *);
116 static void window_copy_cursor_left(struct window_mode_entry *);
117 static void window_copy_cursor_right(struct window_mode_entry *, int);
118 static void window_copy_cursor_up(struct window_mode_entry *, int);
119 static void window_copy_cursor_down(struct window_mode_entry *, int);
120 static void window_copy_cursor_jump(struct window_mode_entry *);
121 static void window_copy_cursor_jump_back(struct window_mode_entry *);
122 static void window_copy_cursor_jump_to(struct window_mode_entry *);
123 static void window_copy_cursor_jump_to_back(struct window_mode_entry *);
124 static void window_copy_cursor_next_word(struct window_mode_entry *,
125 const char *);
126 static void window_copy_cursor_next_word_end_pos(struct window_mode_entry *,
127 const char *, u_int *, u_int *);
128 static void window_copy_cursor_next_word_end(struct window_mode_entry *,
129 const char *, int);
130 static void window_copy_cursor_previous_word_pos(struct window_mode_entry *,
131 const char *, u_int *, u_int *);
132 static void window_copy_cursor_previous_word(struct window_mode_entry *,
133 const char *, int);
134 static void window_copy_cursor_prompt(struct window_mode_entry *, int);
135 static void window_copy_scroll_up(struct window_mode_entry *, u_int);
136 static void window_copy_scroll_down(struct window_mode_entry *, u_int);
137 static void window_copy_rectangle_set(struct window_mode_entry *, int);
138 static void window_copy_move_mouse(struct mouse_event *);
139 static void window_copy_drag_update(struct client *, struct mouse_event *);
140 static void window_copy_drag_release(struct client *, struct mouse_event *);
141 static void window_copy_jump_to_mark(struct window_mode_entry *);
142 static void window_copy_acquire_cursor_up(struct window_mode_entry *,
143 u_int, u_int, u_int, u_int, u_int);
144 static void window_copy_acquire_cursor_down(struct window_mode_entry *,
145 u_int, u_int, u_int, u_int, u_int, u_int, int);
147 const struct window_mode window_copy_mode = {
148 .name = "copy-mode",
150 .init = window_copy_init,
151 .free = window_copy_free,
152 .resize = window_copy_resize,
153 .key_table = window_copy_key_table,
154 .command = window_copy_command,
155 .formats = window_copy_formats,
158 const struct window_mode window_view_mode = {
159 .name = "view-mode",
161 .init = window_copy_view_init,
162 .free = window_copy_free,
163 .resize = window_copy_resize,
164 .key_table = window_copy_key_table,
165 .command = window_copy_command,
166 .formats = window_copy_formats,
169 enum {
170 WINDOW_COPY_OFF,
171 WINDOW_COPY_SEARCHUP,
172 WINDOW_COPY_SEARCHDOWN,
173 WINDOW_COPY_JUMPFORWARD,
174 WINDOW_COPY_JUMPBACKWARD,
175 WINDOW_COPY_JUMPTOFORWARD,
176 WINDOW_COPY_JUMPTOBACKWARD,
179 enum {
180 WINDOW_COPY_REL_POS_ABOVE,
181 WINDOW_COPY_REL_POS_ON_SCREEN,
182 WINDOW_COPY_REL_POS_BELOW,
185 enum window_copy_cmd_action {
186 WINDOW_COPY_CMD_NOTHING,
187 WINDOW_COPY_CMD_REDRAW,
188 WINDOW_COPY_CMD_CANCEL,
191 enum window_copy_cmd_clear {
192 WINDOW_COPY_CMD_CLEAR_ALWAYS,
193 WINDOW_COPY_CMD_CLEAR_NEVER,
194 WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
197 struct window_copy_cmd_state {
198 struct window_mode_entry *wme;
199 struct args *args;
200 struct mouse_event *m;
202 struct client *c;
203 struct session *s;
204 struct winlink *wl;
208 * Copy mode's visible screen (the "screen" field) is filled from one of two
209 * sources: the original contents of the pane (used when we actually enter via
210 * the "copy-mode" command, to copy the contents of the current pane), or else
211 * a series of lines containing the output from an output-writing tmux command
212 * (such as any of the "show-*" or "list-*" commands).
214 * In either case, the full content of the copy-mode grid is pointed at by the
215 * "backing" field, and is copied into "screen" as needed (that is, when
216 * scrolling occurs). When copy-mode is backed by a pane, backing points
217 * directly at that pane's screen structure (&wp->base); when backed by a list
218 * of output-lines from a command, it points at a newly-allocated screen
219 * structure (which is deallocated when the mode ends).
221 struct window_copy_mode_data {
222 struct screen screen;
224 struct screen *backing;
225 int backing_written; /* backing display started */
226 struct screen *writing;
227 struct input_ctx *ictx;
229 int viewmode; /* view mode entered */
231 u_int oy; /* number of lines scrolled up */
233 u_int selx; /* beginning of selection */
234 u_int sely;
236 u_int endselx; /* end of selection */
237 u_int endsely;
239 enum {
240 CURSORDRAG_NONE, /* selection is independent of cursor */
241 CURSORDRAG_ENDSEL, /* end is synchronized with cursor */
242 CURSORDRAG_SEL, /* start is synchronized with cursor */
243 } cursordrag;
245 int modekeys;
246 enum {
247 LINE_SEL_NONE,
248 LINE_SEL_LEFT_RIGHT,
249 LINE_SEL_RIGHT_LEFT,
250 } lineflag; /* line selection mode */
251 int rectflag; /* in rectangle copy mode? */
252 int scroll_exit; /* exit on scroll to end? */
253 int hide_position; /* hide position marker */
255 enum {
256 SEL_CHAR, /* select one char at a time */
257 SEL_WORD, /* select one word at a time */
258 SEL_LINE, /* select one line at a time */
259 } selflag;
261 const char *separators; /* word separators */
263 u_int dx; /* drag start position */
264 u_int dy;
266 u_int selrx; /* selection reset positions */
267 u_int selry;
268 u_int endselrx;
269 u_int endselry;
271 u_int cx;
272 u_int cy;
274 u_int lastcx; /* position in last line w/ content */
275 u_int lastsx; /* size of last line w/ content */
277 u_int mx; /* mark position */
278 u_int my;
279 int showmark;
281 int searchtype;
282 int searchdirection;
283 int searchregex;
284 char *searchstr;
285 u_char *searchmark;
286 int searchcount;
287 int searchmore;
288 int searchall;
289 int searchx;
290 int searchy;
291 int searcho;
292 u_char searchgen;
294 int timeout; /* search has timed out */
295 #define WINDOW_COPY_SEARCH_TIMEOUT 10000
296 #define WINDOW_COPY_SEARCH_ALL_TIMEOUT 200
298 int jumptype;
299 struct utf8_data *jumpchar;
301 struct event dragtimer;
302 #define WINDOW_COPY_DRAG_REPEAT_TIME 50000
305 static void
306 window_copy_scroll_timer(__unused int fd, __unused short events, void *arg)
308 struct window_mode_entry *wme = arg;
309 struct window_pane *wp = wme->wp;
310 struct window_copy_mode_data *data = wme->data;
311 struct timeval tv = {
312 .tv_usec = WINDOW_COPY_DRAG_REPEAT_TIME
315 evtimer_del(&data->dragtimer);
317 if (TAILQ_FIRST(&wp->modes) != wme)
318 return;
320 if (data->cy == 0) {
321 evtimer_add(&data->dragtimer, &tv);
322 window_copy_cursor_up(wme, 1);
323 } else if (data->cy == screen_size_y(&data->screen) - 1) {
324 evtimer_add(&data->dragtimer, &tv);
325 window_copy_cursor_down(wme, 1);
329 static struct screen *
330 window_copy_clone_screen(struct screen *src, struct screen *hint, u_int *cx,
331 u_int *cy, int trim)
333 struct screen *dst;
334 const struct grid_line *gl;
335 u_int sy, wx, wy;
336 int reflow;
338 dst = xcalloc(1, sizeof *dst);
340 sy = screen_hsize(src) + screen_size_y(src);
341 if (trim) {
342 while (sy > screen_hsize(src)) {
343 gl = grid_peek_line(src->grid, sy - 1);
344 if (gl->cellused != 0)
345 break;
346 sy--;
349 log_debug("%s: target screen is %ux%u, source %ux%u", __func__,
350 screen_size_x(src), sy, screen_size_x(hint),
351 screen_hsize(src) + screen_size_y(src));
352 screen_init(dst, screen_size_x(src), sy, screen_hlimit(src));
355 * Ensure history is on for the backing grid so lines are not deleted
356 * during resizing.
358 dst->grid->flags |= GRID_HISTORY;
359 grid_duplicate_lines(dst->grid, 0, src->grid, 0, sy);
361 dst->grid->sy = sy - screen_hsize(src);
362 dst->grid->hsize = screen_hsize(src);
363 dst->grid->hscrolled = src->grid->hscrolled;
364 if (src->cy > dst->grid->sy - 1) {
365 dst->cx = 0;
366 dst->cy = dst->grid->sy - 1;
367 } else {
368 dst->cx = src->cx;
369 dst->cy = src->cy;
372 if (cx != NULL && cy != NULL) {
373 *cx = dst->cx;
374 *cy = screen_hsize(dst) + dst->cy;
375 reflow = (screen_size_x(hint) != screen_size_x(dst));
377 else
378 reflow = 0;
379 if (reflow)
380 grid_wrap_position(dst->grid, *cx, *cy, &wx, &wy);
381 screen_resize_cursor(dst, screen_size_x(hint), screen_size_y(hint), 1,
382 0, 0);
383 if (reflow)
384 grid_unwrap_position(dst->grid, cx, cy, wx, wy);
386 return (dst);
389 static struct window_copy_mode_data *
390 window_copy_common_init(struct window_mode_entry *wme)
392 struct window_pane *wp = wme->wp;
393 struct window_copy_mode_data *data;
394 struct screen *base = &wp->base;
396 wme->data = data = xcalloc(1, sizeof *data);
398 data->cursordrag = CURSORDRAG_NONE;
399 data->lineflag = LINE_SEL_NONE;
400 data->selflag = SEL_CHAR;
402 if (wp->searchstr != NULL) {
403 data->searchtype = WINDOW_COPY_SEARCHUP;
404 data->searchregex = wp->searchregex;
405 data->searchstr = xstrdup(wp->searchstr);
406 } else {
407 data->searchtype = WINDOW_COPY_OFF;
408 data->searchregex = 0;
409 data->searchstr = NULL;
411 data->searchx = data->searchy = data->searcho = -1;
412 data->searchall = 1;
414 data->jumptype = WINDOW_COPY_OFF;
415 data->jumpchar = NULL;
417 screen_init(&data->screen, screen_size_x(base), screen_size_y(base), 0);
418 data->modekeys = options_get_number(wp->window->options, "mode-keys");
420 evtimer_set(&data->dragtimer, window_copy_scroll_timer, wme);
422 return (data);
425 static struct screen *
426 window_copy_init(struct window_mode_entry *wme,
427 __unused struct cmd_find_state *fs, struct args *args)
429 struct window_pane *wp = wme->swp;
430 struct window_copy_mode_data *data;
431 struct screen *base = &wp->base;
432 struct screen_write_ctx ctx;
433 u_int i, cx, cy;
435 data = window_copy_common_init(wme);
436 data->backing = window_copy_clone_screen(base, &data->screen, &cx, &cy,
437 wme->swp != wme->wp);
439 data->cx = cx;
440 if (cy < screen_hsize(data->backing)) {
441 data->cy = 0;
442 data->oy = screen_hsize(data->backing) - cy;
443 } else {
444 data->cy = cy - screen_hsize(data->backing);
445 data->oy = 0;
448 data->scroll_exit = args_has(args, 'e');
449 data->hide_position = args_has(args, 'H');
451 data->screen.cx = data->cx;
452 data->screen.cy = data->cy;
453 data->mx = data->cx;
454 data->my = screen_hsize(data->backing) + data->cy - data->oy;
455 data->showmark = 0;
457 screen_write_start(&ctx, &data->screen);
458 for (i = 0; i < screen_size_y(&data->screen); i++)
459 window_copy_write_line(wme, &ctx, i);
460 screen_write_cursormove(&ctx, data->cx, data->cy, 0);
461 screen_write_stop(&ctx);
463 return (&data->screen);
466 static struct screen *
467 window_copy_view_init(struct window_mode_entry *wme,
468 __unused struct cmd_find_state *fs, __unused struct args *args)
470 struct window_pane *wp = wme->wp;
471 struct window_copy_mode_data *data;
472 struct screen *base = &wp->base;
473 u_int sx = screen_size_x(base);
475 data = window_copy_common_init(wme);
476 data->viewmode = 1;
478 data->backing = xmalloc(sizeof *data->backing);
479 screen_init(data->backing, sx, screen_size_y(base), UINT_MAX);
480 data->writing = xmalloc(sizeof *data->writing);
481 screen_init(data->writing, sx, screen_size_y(base), 0);
482 data->ictx = input_init(NULL, NULL, NULL);
483 data->mx = data->cx;
484 data->my = screen_hsize(data->backing) + data->cy - data->oy;
485 data->showmark = 0;
487 return (&data->screen);
490 static void
491 window_copy_free(struct window_mode_entry *wme)
493 struct window_copy_mode_data *data = wme->data;
495 evtimer_del(&data->dragtimer);
497 free(data->searchmark);
498 free(data->searchstr);
499 free(data->jumpchar);
501 if (data->writing != NULL) {
502 screen_free(data->writing);
503 free(data->writing);
505 if (data->ictx != NULL)
506 input_free(data->ictx);
507 screen_free(data->backing);
508 free(data->backing);
510 screen_free(&data->screen);
511 free(data);
514 void
515 window_copy_add(struct window_pane *wp, int parse, const char *fmt, ...)
517 va_list ap;
519 va_start(ap, fmt);
520 window_copy_vadd(wp, parse, fmt, ap);
521 va_end(ap);
524 static void
525 window_copy_init_ctx_cb(__unused struct screen_write_ctx *ctx,
526 struct tty_ctx *ttyctx)
528 memcpy(&ttyctx->defaults, &grid_default_cell, sizeof ttyctx->defaults);
529 ttyctx->palette = NULL;
530 ttyctx->redraw_cb = NULL;
531 ttyctx->set_client_cb = NULL;
532 ttyctx->arg = NULL;
535 void
536 window_copy_vadd(struct window_pane *wp, int parse, const char *fmt, va_list ap)
538 struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
539 struct window_copy_mode_data *data = wme->data;
540 struct screen *backing = data->backing;
541 struct screen *writing = data->writing;
542 struct screen_write_ctx writing_ctx, backing_ctx, ctx;
543 struct grid_cell gc;
544 u_int old_hsize, old_cy;
545 u_int sx = screen_size_x(backing);
546 char *text;
548 if (parse) {
549 vasprintf(&text, fmt, ap);
550 screen_write_start(&writing_ctx, writing);
551 screen_write_reset(&writing_ctx);
552 input_parse_screen(data->ictx, writing, window_copy_init_ctx_cb,
553 data, text, strlen(text));
554 free(text);
557 old_hsize = screen_hsize(data->backing);
558 screen_write_start(&backing_ctx, backing);
559 if (data->backing_written) {
561 * On the second or later line, do a CRLF before writing
562 * (so it's on a new line).
564 screen_write_carriagereturn(&backing_ctx);
565 screen_write_linefeed(&backing_ctx, 0, 8);
566 } else
567 data->backing_written = 1;
568 old_cy = backing->cy;
569 if (parse)
570 screen_write_fast_copy(&backing_ctx, writing, 0, 0, sx, 1);
571 else {
572 memcpy(&gc, &grid_default_cell, sizeof gc);
573 screen_write_vnputs(&backing_ctx, 0, &gc, fmt, ap);
575 screen_write_stop(&backing_ctx);
577 data->oy += screen_hsize(data->backing) - old_hsize;
579 screen_write_start_pane(&ctx, wp, &data->screen);
582 * If the history has changed, draw the top line.
583 * (If there's any history at all, it has changed.)
585 if (screen_hsize(data->backing))
586 window_copy_redraw_lines(wme, 0, 1);
588 /* Write the new lines. */
589 window_copy_redraw_lines(wme, old_cy, backing->cy - old_cy + 1);
591 screen_write_stop(&ctx);
594 void
595 window_copy_pageup(struct window_pane *wp, int half_page)
597 window_copy_pageup1(TAILQ_FIRST(&wp->modes), half_page);
600 static void
601 window_copy_pageup1(struct window_mode_entry *wme, int half_page)
603 struct window_copy_mode_data *data = wme->data;
604 struct screen *s = &data->screen;
605 u_int n, ox, oy, px, py;
607 oy = screen_hsize(data->backing) + data->cy - data->oy;
608 ox = window_copy_find_length(wme, oy);
610 if (data->cx != ox) {
611 data->lastcx = data->cx;
612 data->lastsx = ox;
614 data->cx = data->lastcx;
616 n = 1;
617 if (screen_size_y(s) > 2) {
618 if (half_page)
619 n = screen_size_y(s) / 2;
620 else
621 n = screen_size_y(s) - 2;
624 if (data->oy + n > screen_hsize(data->backing)) {
625 data->oy = screen_hsize(data->backing);
626 if (data->cy < n)
627 data->cy = 0;
628 else
629 data->cy -= n;
630 } else
631 data->oy += n;
633 if (data->screen.sel == NULL || !data->rectflag) {
634 py = screen_hsize(data->backing) + data->cy - data->oy;
635 px = window_copy_find_length(wme, py);
636 if ((data->cx >= data->lastsx && data->cx != px) ||
637 data->cx > px)
638 window_copy_cursor_end_of_line(wme);
641 if (data->searchmark != NULL && !data->timeout)
642 window_copy_search_marks(wme, NULL, data->searchregex, 1);
643 window_copy_update_selection(wme, 1, 0);
644 window_copy_redraw_screen(wme);
647 static int
648 window_copy_pagedown(struct window_mode_entry *wme, int half_page,
649 int scroll_exit)
651 struct window_copy_mode_data *data = wme->data;
652 struct screen *s = &data->screen;
653 u_int n, ox, oy, px, py;
655 oy = screen_hsize(data->backing) + data->cy - data->oy;
656 ox = window_copy_find_length(wme, oy);
658 if (data->cx != ox) {
659 data->lastcx = data->cx;
660 data->lastsx = ox;
662 data->cx = data->lastcx;
664 n = 1;
665 if (screen_size_y(s) > 2) {
666 if (half_page)
667 n = screen_size_y(s) / 2;
668 else
669 n = screen_size_y(s) - 2;
672 if (data->oy < n) {
673 data->oy = 0;
674 if (data->cy + (n - data->oy) >= screen_size_y(data->backing))
675 data->cy = screen_size_y(data->backing) - 1;
676 else
677 data->cy += n - data->oy;
678 } else
679 data->oy -= n;
681 if (data->screen.sel == NULL || !data->rectflag) {
682 py = screen_hsize(data->backing) + data->cy - data->oy;
683 px = window_copy_find_length(wme, py);
684 if ((data->cx >= data->lastsx && data->cx != px) ||
685 data->cx > px)
686 window_copy_cursor_end_of_line(wme);
689 if (scroll_exit && data->oy == 0)
690 return (1);
691 if (data->searchmark != NULL && !data->timeout)
692 window_copy_search_marks(wme, NULL, data->searchregex, 1);
693 window_copy_update_selection(wme, 1, 0);
694 window_copy_redraw_screen(wme);
695 return (0);
698 static void
699 window_copy_previous_paragraph(struct window_mode_entry *wme)
701 struct window_copy_mode_data *data = wme->data;
702 u_int oy;
704 oy = screen_hsize(data->backing) + data->cy - data->oy;
706 while (oy > 0 && window_copy_find_length(wme, oy) == 0)
707 oy--;
709 while (oy > 0 && window_copy_find_length(wme, oy) > 0)
710 oy--;
712 window_copy_scroll_to(wme, 0, oy, 0);
715 static void
716 window_copy_next_paragraph(struct window_mode_entry *wme)
718 struct window_copy_mode_data *data = wme->data;
719 struct screen *s = &data->screen;
720 u_int maxy, ox, oy;
722 oy = screen_hsize(data->backing) + data->cy - data->oy;
723 maxy = screen_hsize(data->backing) + screen_size_y(s) - 1;
725 while (oy < maxy && window_copy_find_length(wme, oy) == 0)
726 oy++;
728 while (oy < maxy && window_copy_find_length(wme, oy) > 0)
729 oy++;
731 ox = window_copy_find_length(wme, oy);
732 window_copy_scroll_to(wme, ox, oy, 0);
735 char *
736 window_copy_get_word(struct window_pane *wp, u_int x, u_int y)
738 struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
739 struct window_copy_mode_data *data = wme->data;
740 struct grid *gd = data->screen.grid;
742 return (format_grid_word(gd, x, gd->hsize + y));
745 char *
746 window_copy_get_line(struct window_pane *wp, u_int y)
748 struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
749 struct window_copy_mode_data *data = wme->data;
750 struct grid *gd = data->screen.grid;
752 return (format_grid_line(gd, gd->hsize + y));
755 static void *
756 window_copy_cursor_word_cb(struct format_tree *ft)
758 struct window_pane *wp = format_get_pane(ft);
759 struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
760 struct window_copy_mode_data *data = wme->data;
762 return (window_copy_get_word(wp, data->cx, data->cy));
765 static void *
766 window_copy_cursor_line_cb(struct format_tree *ft)
768 struct window_pane *wp = format_get_pane(ft);
769 struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
770 struct window_copy_mode_data *data = wme->data;
772 return (window_copy_get_line(wp, data->cy));
775 static void *
776 window_copy_search_match_cb(struct format_tree *ft)
778 struct window_pane *wp = format_get_pane(ft);
779 struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
780 struct window_copy_mode_data *data = wme->data;
782 return (window_copy_match_at_cursor(data));
785 static void
786 window_copy_formats(struct window_mode_entry *wme, struct format_tree *ft)
788 struct window_copy_mode_data *data = wme->data;
790 format_add(ft, "scroll_position", "%d", data->oy);
791 format_add(ft, "rectangle_toggle", "%d", data->rectflag);
793 format_add(ft, "copy_cursor_x", "%d", data->cx);
794 format_add(ft, "copy_cursor_y", "%d", data->cy);
796 format_add(ft, "selection_present", "%d", data->screen.sel != NULL);
797 if (data->screen.sel != NULL) {
798 format_add(ft, "selection_start_x", "%d", data->selx);
799 format_add(ft, "selection_start_y", "%d", data->sely);
800 format_add(ft, "selection_end_x", "%d", data->endselx);
801 format_add(ft, "selection_end_y", "%d", data->endsely);
802 format_add(ft, "selection_active", "%d",
803 data->cursordrag != CURSORDRAG_NONE);
804 } else
805 format_add(ft, "selection_active", "%d", 0);
807 format_add(ft, "search_present", "%d", data->searchmark != NULL);
808 format_add_cb(ft, "search_match", window_copy_search_match_cb);
810 format_add_cb(ft, "copy_cursor_word", window_copy_cursor_word_cb);
811 format_add_cb(ft, "copy_cursor_line", window_copy_cursor_line_cb);
814 static void
815 window_copy_size_changed(struct window_mode_entry *wme)
817 struct window_copy_mode_data *data = wme->data;
818 struct screen *s = &data->screen;
819 struct screen_write_ctx ctx;
820 int search = (data->searchmark != NULL);
822 window_copy_clear_selection(wme);
823 window_copy_clear_marks(wme);
825 screen_write_start(&ctx, s);
826 window_copy_write_lines(wme, &ctx, 0, screen_size_y(s));
827 screen_write_stop(&ctx);
829 if (search && !data->timeout)
830 window_copy_search_marks(wme, NULL, data->searchregex, 0);
831 data->searchx = data->cx;
832 data->searchy = data->cy;
833 data->searcho = data->oy;
836 static void
837 window_copy_resize(struct window_mode_entry *wme, u_int sx, u_int sy)
839 struct window_copy_mode_data *data = wme->data;
840 struct screen *s = &data->screen;
841 struct grid *gd = data->backing->grid;
842 u_int cx, cy, wx, wy;
843 int reflow;
845 screen_resize(s, sx, sy, 0);
846 cx = data->cx;
847 cy = gd->hsize + data->cy - data->oy;
848 reflow = (gd->sx != sx);
849 if (reflow)
850 grid_wrap_position(gd, cx, cy, &wx, &wy);
851 screen_resize_cursor(data->backing, sx, sy, 1, 0, 0);
852 if (reflow)
853 grid_unwrap_position(gd, &cx, &cy, wx, wy);
855 data->cx = cx;
856 if (cy < gd->hsize) {
857 data->cy = 0;
858 data->oy = gd->hsize - cy;
859 } else {
860 data->cy = cy - gd->hsize;
861 data->oy = 0;
864 window_copy_size_changed(wme);
865 window_copy_redraw_screen(wme);
868 static const char *
869 window_copy_key_table(struct window_mode_entry *wme)
871 struct window_pane *wp = wme->wp;
873 if (options_get_number(wp->window->options, "mode-keys") == MODEKEY_VI)
874 return ("copy-mode-vi");
875 return ("copy-mode");
878 static int
879 window_copy_expand_search_string(struct window_copy_cmd_state *cs)
881 struct window_mode_entry *wme = cs->wme;
882 struct window_copy_mode_data *data = wme->data;
883 const char *ss = args_string(cs->args, 1);
884 char *expanded;
886 if (ss == NULL || *ss == '\0')
887 return (0);
889 if (args_has(cs->args, 'F')) {
890 expanded = format_single(NULL, ss, NULL, NULL, NULL, wme->wp);
891 if (*expanded == '\0') {
892 free(expanded);
893 return (0);
895 free(data->searchstr);
896 data->searchstr = expanded;
897 } else {
898 free(data->searchstr);
899 data->searchstr = xstrdup(ss);
901 return (1);
904 static enum window_copy_cmd_action
905 window_copy_cmd_append_selection(struct window_copy_cmd_state *cs)
907 struct window_mode_entry *wme = cs->wme;
908 struct session *s = cs->s;
910 if (s != NULL)
911 window_copy_append_selection(wme);
912 window_copy_clear_selection(wme);
913 return (WINDOW_COPY_CMD_REDRAW);
916 static enum window_copy_cmd_action
917 window_copy_cmd_append_selection_and_cancel(struct window_copy_cmd_state *cs)
919 struct window_mode_entry *wme = cs->wme;
920 struct session *s = cs->s;
922 if (s != NULL)
923 window_copy_append_selection(wme);
924 window_copy_clear_selection(wme);
925 return (WINDOW_COPY_CMD_CANCEL);
928 static enum window_copy_cmd_action
929 window_copy_cmd_back_to_indentation(struct window_copy_cmd_state *cs)
931 struct window_mode_entry *wme = cs->wme;
933 window_copy_cursor_back_to_indentation(wme);
934 return (WINDOW_COPY_CMD_NOTHING);
937 static enum window_copy_cmd_action
938 window_copy_cmd_begin_selection(struct window_copy_cmd_state *cs)
940 struct window_mode_entry *wme = cs->wme;
941 struct client *c = cs->c;
942 struct mouse_event *m = cs->m;
943 struct window_copy_mode_data *data = wme->data;
945 if (m != NULL) {
946 window_copy_start_drag(c, m);
947 return (WINDOW_COPY_CMD_NOTHING);
950 data->lineflag = LINE_SEL_NONE;
951 data->selflag = SEL_CHAR;
952 window_copy_start_selection(wme);
953 return (WINDOW_COPY_CMD_REDRAW);
956 static enum window_copy_cmd_action
957 window_copy_cmd_stop_selection(struct window_copy_cmd_state *cs)
959 struct window_mode_entry *wme = cs->wme;
960 struct window_copy_mode_data *data = wme->data;
962 data->cursordrag = CURSORDRAG_NONE;
963 data->lineflag = LINE_SEL_NONE;
964 data->selflag = SEL_CHAR;
965 return (WINDOW_COPY_CMD_NOTHING);
968 static enum window_copy_cmd_action
969 window_copy_cmd_bottom_line(struct window_copy_cmd_state *cs)
971 struct window_mode_entry *wme = cs->wme;
972 struct window_copy_mode_data *data = wme->data;
974 data->cx = 0;
975 data->cy = screen_size_y(&data->screen) - 1;
977 window_copy_update_selection(wme, 1, 0);
978 return (WINDOW_COPY_CMD_REDRAW);
981 static enum window_copy_cmd_action
982 window_copy_cmd_cancel(__unused struct window_copy_cmd_state *cs)
984 return (WINDOW_COPY_CMD_CANCEL);
987 static enum window_copy_cmd_action
988 window_copy_cmd_clear_selection(struct window_copy_cmd_state *cs)
990 struct window_mode_entry *wme = cs->wme;
992 window_copy_clear_selection(wme);
993 return (WINDOW_COPY_CMD_REDRAW);
996 static enum window_copy_cmd_action
997 window_copy_do_copy_end_of_line(struct window_copy_cmd_state *cs, int pipe,
998 int cancel)
1000 struct window_mode_entry *wme = cs->wme;
1001 struct client *c = cs->c;
1002 struct session *s = cs->s;
1003 struct winlink *wl = cs->wl;
1004 struct window_pane *wp = wme->wp;
1005 u_int count = args_count(cs->args);
1006 u_int np = wme->prefix, ocx, ocy, ooy;
1007 struct window_copy_mode_data *data = wme->data;
1008 char *prefix = NULL, *command = NULL;
1009 const char *arg1 = args_string(cs->args, 1);
1010 const char *arg2 = args_string(cs->args, 2);
1012 if (pipe) {
1013 if (count == 3)
1014 prefix = format_single(NULL, arg2, c, s, wl, wp);
1015 if (s != NULL && count > 1 && *arg1 != '\0')
1016 command = format_single(NULL, arg1, c, s, wl, wp);
1017 } else {
1018 if (count == 2)
1019 prefix = format_single(NULL, arg1, c, s, wl, wp);
1022 ocx = data->cx;
1023 ocy = data->cy;
1024 ooy = data->oy;
1026 window_copy_start_selection(wme);
1027 for (; np > 1; np--)
1028 window_copy_cursor_down(wme, 0);
1029 window_copy_cursor_end_of_line(wme);
1031 if (s != NULL) {
1032 if (pipe)
1033 window_copy_copy_pipe(wme, s, prefix, command);
1034 else
1035 window_copy_copy_selection(wme, prefix);
1037 if (cancel) {
1038 free(prefix);
1039 free(command);
1040 return (WINDOW_COPY_CMD_CANCEL);
1043 window_copy_clear_selection(wme);
1045 data->cx = ocx;
1046 data->cy = ocy;
1047 data->oy = ooy;
1049 free(prefix);
1050 free(command);
1051 return (WINDOW_COPY_CMD_REDRAW);
1054 static enum window_copy_cmd_action
1055 window_copy_cmd_copy_end_of_line(struct window_copy_cmd_state *cs)
1057 return (window_copy_do_copy_end_of_line(cs, 0, 0));
1060 static enum window_copy_cmd_action
1061 window_copy_cmd_copy_end_of_line_and_cancel(struct window_copy_cmd_state *cs)
1063 return (window_copy_do_copy_end_of_line(cs, 0, 1));
1066 static enum window_copy_cmd_action
1067 window_copy_cmd_copy_pipe_end_of_line(struct window_copy_cmd_state *cs)
1069 return (window_copy_do_copy_end_of_line(cs, 1, 0));
1072 static enum window_copy_cmd_action
1073 window_copy_cmd_copy_pipe_end_of_line_and_cancel(
1074 struct window_copy_cmd_state *cs)
1076 return (window_copy_do_copy_end_of_line(cs, 1, 1));
1079 static enum window_copy_cmd_action
1080 window_copy_do_copy_line(struct window_copy_cmd_state *cs, int pipe, int cancel)
1082 struct window_mode_entry *wme = cs->wme;
1083 struct client *c = cs->c;
1084 struct session *s = cs->s;
1085 struct winlink *wl = cs->wl;
1086 struct window_pane *wp = wme->wp;
1087 struct window_copy_mode_data *data = wme->data;
1088 u_int count = args_count(cs->args);
1089 u_int np = wme->prefix, ocx, ocy, ooy;
1090 char *prefix = NULL, *command = NULL;
1091 const char *arg1 = args_string(cs->args, 1);
1092 const char *arg2 = args_string(cs->args, 2);
1094 if (pipe) {
1095 if (count == 3)
1096 prefix = format_single(NULL, arg2, c, s, wl, wp);
1097 if (s != NULL && count > 1 && *arg1 != '\0')
1098 command = format_single(NULL, arg1, c, s, wl, wp);
1099 } else {
1100 if (count == 2)
1101 prefix = format_single(NULL, arg1, c, s, wl, wp);
1104 ocx = data->cx;
1105 ocy = data->cy;
1106 ooy = data->oy;
1108 data->selflag = SEL_CHAR;
1109 window_copy_cursor_start_of_line(wme);
1110 window_copy_start_selection(wme);
1111 for (; np > 1; np--)
1112 window_copy_cursor_down(wme, 0);
1113 window_copy_cursor_end_of_line(wme);
1115 if (s != NULL) {
1116 if (pipe)
1117 window_copy_copy_pipe(wme, s, prefix, command);
1118 else
1119 window_copy_copy_selection(wme, prefix);
1121 if (cancel) {
1122 free(prefix);
1123 free(command);
1124 return (WINDOW_COPY_CMD_CANCEL);
1127 window_copy_clear_selection(wme);
1129 data->cx = ocx;
1130 data->cy = ocy;
1131 data->oy = ooy;
1133 free(prefix);
1134 free(command);
1135 return (WINDOW_COPY_CMD_REDRAW);
1138 static enum window_copy_cmd_action
1139 window_copy_cmd_copy_line(struct window_copy_cmd_state *cs)
1141 return (window_copy_do_copy_line(cs, 0, 0));
1144 static enum window_copy_cmd_action
1145 window_copy_cmd_copy_line_and_cancel(struct window_copy_cmd_state *cs)
1147 return (window_copy_do_copy_line(cs, 0, 1));
1150 static enum window_copy_cmd_action
1151 window_copy_cmd_copy_pipe_line(struct window_copy_cmd_state *cs)
1153 return (window_copy_do_copy_line(cs, 1, 0));
1156 static enum window_copy_cmd_action
1157 window_copy_cmd_copy_pipe_line_and_cancel(struct window_copy_cmd_state *cs)
1159 return (window_copy_do_copy_line(cs, 1, 1));
1162 static enum window_copy_cmd_action
1163 window_copy_cmd_copy_selection_no_clear(struct window_copy_cmd_state *cs)
1165 struct window_mode_entry *wme = cs->wme;
1166 struct client *c = cs->c;
1167 struct session *s = cs->s;
1168 struct winlink *wl = cs->wl;
1169 struct window_pane *wp = wme->wp;
1170 char *prefix = NULL;
1171 const char *arg1 = args_string(cs->args, 1);
1173 if (arg1 != NULL)
1174 prefix = format_single(NULL, arg1, c, s, wl, wp);
1176 if (s != NULL)
1177 window_copy_copy_selection(wme, prefix);
1179 free(prefix);
1180 return (WINDOW_COPY_CMD_NOTHING);
1183 static enum window_copy_cmd_action
1184 window_copy_cmd_copy_selection(struct window_copy_cmd_state *cs)
1186 struct window_mode_entry *wme = cs->wme;
1188 window_copy_cmd_copy_selection_no_clear(cs);
1189 window_copy_clear_selection(wme);
1190 return (WINDOW_COPY_CMD_REDRAW);
1193 static enum window_copy_cmd_action
1194 window_copy_cmd_copy_selection_and_cancel(struct window_copy_cmd_state *cs)
1196 struct window_mode_entry *wme = cs->wme;
1198 window_copy_cmd_copy_selection_no_clear(cs);
1199 window_copy_clear_selection(wme);
1200 return (WINDOW_COPY_CMD_CANCEL);
1203 static enum window_copy_cmd_action
1204 window_copy_cmd_cursor_down(struct window_copy_cmd_state *cs)
1206 struct window_mode_entry *wme = cs->wme;
1207 u_int np = wme->prefix;
1209 for (; np != 0; np--)
1210 window_copy_cursor_down(wme, 0);
1211 return (WINDOW_COPY_CMD_NOTHING);
1214 static enum window_copy_cmd_action
1215 window_copy_cmd_cursor_down_and_cancel(struct window_copy_cmd_state *cs)
1217 struct window_mode_entry *wme = cs->wme;
1218 struct window_copy_mode_data *data = wme->data;
1219 u_int np = wme->prefix, cy;
1221 cy = data->cy;
1222 for (; np != 0; np--)
1223 window_copy_cursor_down(wme, 0);
1224 if (cy == data->cy && data->oy == 0)
1225 return (WINDOW_COPY_CMD_CANCEL);
1226 return (WINDOW_COPY_CMD_NOTHING);
1229 static enum window_copy_cmd_action
1230 window_copy_cmd_cursor_left(struct window_copy_cmd_state *cs)
1232 struct window_mode_entry *wme = cs->wme;
1233 u_int np = wme->prefix;
1235 for (; np != 0; np--)
1236 window_copy_cursor_left(wme);
1237 return (WINDOW_COPY_CMD_NOTHING);
1240 static enum window_copy_cmd_action
1241 window_copy_cmd_cursor_right(struct window_copy_cmd_state *cs)
1243 struct window_mode_entry *wme = cs->wme;
1244 struct window_copy_mode_data *data = wme->data;
1245 u_int np = wme->prefix;
1247 for (; np != 0; np--) {
1248 window_copy_cursor_right(wme, data->screen.sel != NULL &&
1249 data->rectflag);
1251 return (WINDOW_COPY_CMD_NOTHING);
1254 /* Scroll line containing the cursor to the given position. */
1255 static enum window_copy_cmd_action
1256 window_copy_cmd_scroll_to(struct window_copy_cmd_state *cs, u_int to)
1258 struct window_mode_entry *wme = cs->wme;
1259 struct window_copy_mode_data *data = wme->data;
1260 u_int oy, delta;
1261 int scroll_up; /* >0 up, <0 down */
1263 scroll_up = data->cy - to;
1264 delta = abs(scroll_up);
1265 oy = screen_hsize(data->backing) - data->oy;
1268 * oy is the maximum scroll down amount, while data->oy is the maximum
1269 * scroll up amount.
1271 if (scroll_up > 0 && data->oy >= delta) {
1272 window_copy_scroll_up(wme, delta);
1273 data->cy -= delta;
1274 } else if (scroll_up < 0 && oy >= delta) {
1275 window_copy_scroll_down(wme, delta);
1276 data->cy += delta;
1279 window_copy_update_selection(wme, 0, 0);
1280 return (WINDOW_COPY_CMD_REDRAW);
1283 /* Scroll line containing the cursor to the bottom. */
1284 static enum window_copy_cmd_action
1285 window_copy_cmd_scroll_bottom(struct window_copy_cmd_state *cs)
1287 struct window_copy_mode_data *data = cs->wme->data;
1288 u_int bottom;
1290 bottom = screen_size_y(&data->screen) - 1;
1291 return (window_copy_cmd_scroll_to(cs, bottom));
1294 /* Scroll line containing the cursor to the middle. */
1295 static enum window_copy_cmd_action
1296 window_copy_cmd_scroll_middle(struct window_copy_cmd_state *cs)
1298 struct window_copy_mode_data *data = cs->wme->data;
1299 u_int mid_value;
1301 mid_value = (screen_size_y(&data->screen) - 1) / 2;
1302 return (window_copy_cmd_scroll_to(cs, mid_value));
1305 /* Scroll line containing the cursor to the top. */
1306 static enum window_copy_cmd_action
1307 window_copy_cmd_scroll_top(struct window_copy_cmd_state *cs)
1309 return (window_copy_cmd_scroll_to(cs, 0));
1312 static enum window_copy_cmd_action
1313 window_copy_cmd_cursor_up(struct window_copy_cmd_state *cs)
1315 struct window_mode_entry *wme = cs->wme;
1316 u_int np = wme->prefix;
1318 for (; np != 0; np--)
1319 window_copy_cursor_up(wme, 0);
1320 return (WINDOW_COPY_CMD_NOTHING);
1323 static enum window_copy_cmd_action
1324 window_copy_cmd_end_of_line(struct window_copy_cmd_state *cs)
1326 struct window_mode_entry *wme = cs->wme;
1328 window_copy_cursor_end_of_line(wme);
1329 return (WINDOW_COPY_CMD_NOTHING);
1332 static enum window_copy_cmd_action
1333 window_copy_cmd_halfpage_down(struct window_copy_cmd_state *cs)
1335 struct window_mode_entry *wme = cs->wme;
1336 struct window_copy_mode_data *data = wme->data;
1337 u_int np = wme->prefix;
1339 for (; np != 0; np--) {
1340 if (window_copy_pagedown(wme, 1, data->scroll_exit))
1341 return (WINDOW_COPY_CMD_CANCEL);
1343 return (WINDOW_COPY_CMD_NOTHING);
1346 static enum window_copy_cmd_action
1347 window_copy_cmd_halfpage_down_and_cancel(struct window_copy_cmd_state *cs)
1350 struct window_mode_entry *wme = cs->wme;
1351 u_int np = wme->prefix;
1353 for (; np != 0; np--) {
1354 if (window_copy_pagedown(wme, 1, 1))
1355 return (WINDOW_COPY_CMD_CANCEL);
1357 return (WINDOW_COPY_CMD_NOTHING);
1360 static enum window_copy_cmd_action
1361 window_copy_cmd_halfpage_up(struct window_copy_cmd_state *cs)
1363 struct window_mode_entry *wme = cs->wme;
1364 u_int np = wme->prefix;
1366 for (; np != 0; np--)
1367 window_copy_pageup1(wme, 1);
1368 return (WINDOW_COPY_CMD_NOTHING);
1371 static enum window_copy_cmd_action
1372 window_copy_cmd_toggle_position(struct window_copy_cmd_state *cs)
1374 struct window_mode_entry *wme = cs->wme;
1375 struct window_copy_mode_data *data = wme->data;
1377 data->hide_position = !data->hide_position;
1378 return (WINDOW_COPY_CMD_REDRAW);
1381 static enum window_copy_cmd_action
1382 window_copy_cmd_history_bottom(struct window_copy_cmd_state *cs)
1384 struct window_mode_entry *wme = cs->wme;
1385 struct window_copy_mode_data *data = wme->data;
1386 struct screen *s = data->backing;
1387 u_int oy;
1389 oy = screen_hsize(s) + data->cy - data->oy;
1390 if (data->lineflag == LINE_SEL_RIGHT_LEFT && oy == data->endsely)
1391 window_copy_other_end(wme);
1393 data->cy = screen_size_y(&data->screen) - 1;
1394 data->cx = window_copy_find_length(wme, screen_hsize(s) + data->cy);
1395 data->oy = 0;
1397 if (data->searchmark != NULL && !data->timeout)
1398 window_copy_search_marks(wme, NULL, data->searchregex, 1);
1399 window_copy_update_selection(wme, 1, 0);
1400 return (WINDOW_COPY_CMD_REDRAW);
1403 static enum window_copy_cmd_action
1404 window_copy_cmd_history_top(struct window_copy_cmd_state *cs)
1406 struct window_mode_entry *wme = cs->wme;
1407 struct window_copy_mode_data *data = wme->data;
1408 u_int oy;
1410 oy = screen_hsize(data->backing) + data->cy - data->oy;
1411 if (data->lineflag == LINE_SEL_LEFT_RIGHT && oy == data->sely)
1412 window_copy_other_end(wme);
1414 data->cy = 0;
1415 data->cx = 0;
1416 data->oy = screen_hsize(data->backing);
1418 if (data->searchmark != NULL && !data->timeout)
1419 window_copy_search_marks(wme, NULL, data->searchregex, 1);
1420 window_copy_update_selection(wme, 1, 0);
1421 return (WINDOW_COPY_CMD_REDRAW);
1424 static enum window_copy_cmd_action
1425 window_copy_cmd_jump_again(struct window_copy_cmd_state *cs)
1427 struct window_mode_entry *wme = cs->wme;
1428 struct window_copy_mode_data *data = wme->data;
1429 u_int np = wme->prefix;
1431 switch (data->jumptype) {
1432 case WINDOW_COPY_JUMPFORWARD:
1433 for (; np != 0; np--)
1434 window_copy_cursor_jump(wme);
1435 break;
1436 case WINDOW_COPY_JUMPBACKWARD:
1437 for (; np != 0; np--)
1438 window_copy_cursor_jump_back(wme);
1439 break;
1440 case WINDOW_COPY_JUMPTOFORWARD:
1441 for (; np != 0; np--)
1442 window_copy_cursor_jump_to(wme);
1443 break;
1444 case WINDOW_COPY_JUMPTOBACKWARD:
1445 for (; np != 0; np--)
1446 window_copy_cursor_jump_to_back(wme);
1447 break;
1449 return (WINDOW_COPY_CMD_NOTHING);
1452 static enum window_copy_cmd_action
1453 window_copy_cmd_jump_reverse(struct window_copy_cmd_state *cs)
1455 struct window_mode_entry *wme = cs->wme;
1456 struct window_copy_mode_data *data = wme->data;
1457 u_int np = wme->prefix;
1459 switch (data->jumptype) {
1460 case WINDOW_COPY_JUMPFORWARD:
1461 for (; np != 0; np--)
1462 window_copy_cursor_jump_back(wme);
1463 break;
1464 case WINDOW_COPY_JUMPBACKWARD:
1465 for (; np != 0; np--)
1466 window_copy_cursor_jump(wme);
1467 break;
1468 case WINDOW_COPY_JUMPTOFORWARD:
1469 for (; np != 0; np--)
1470 window_copy_cursor_jump_to_back(wme);
1471 break;
1472 case WINDOW_COPY_JUMPTOBACKWARD:
1473 for (; np != 0; np--)
1474 window_copy_cursor_jump_to(wme);
1475 break;
1477 return (WINDOW_COPY_CMD_NOTHING);
1480 static enum window_copy_cmd_action
1481 window_copy_cmd_middle_line(struct window_copy_cmd_state *cs)
1483 struct window_mode_entry *wme = cs->wme;
1484 struct window_copy_mode_data *data = wme->data;
1486 data->cx = 0;
1487 data->cy = (screen_size_y(&data->screen) - 1) / 2;
1489 window_copy_update_selection(wme, 1, 0);
1490 return (WINDOW_COPY_CMD_REDRAW);
1493 static enum window_copy_cmd_action
1494 window_copy_cmd_previous_matching_bracket(struct window_copy_cmd_state *cs)
1496 struct window_mode_entry *wme = cs->wme;
1497 u_int np = wme->prefix;
1498 struct window_copy_mode_data *data = wme->data;
1499 struct screen *s = data->backing;
1500 char open[] = "{[(", close[] = "}])";
1501 char tried, found, start, *cp;
1502 u_int px, py, xx, n;
1503 struct grid_cell gc;
1504 int failed;
1506 for (; np != 0; np--) {
1507 /* Get cursor position and line length. */
1508 px = data->cx;
1509 py = screen_hsize(s) + data->cy - data->oy;
1510 xx = window_copy_find_length(wme, py);
1511 if (xx == 0)
1512 break;
1515 * Get the current character. If not on a bracket, try the
1516 * previous. If still not, then behave like previous-word.
1518 tried = 0;
1519 retry:
1520 grid_get_cell(s->grid, px, py, &gc);
1521 if (gc.data.size != 1 || (gc.flags & GRID_FLAG_PADDING))
1522 cp = NULL;
1523 else {
1524 found = *gc.data.data;
1525 cp = strchr(close, found);
1527 if (cp == NULL) {
1528 if (data->modekeys == MODEKEY_EMACS) {
1529 if (!tried && px > 0) {
1530 px--;
1531 tried = 1;
1532 goto retry;
1534 window_copy_cursor_previous_word(wme, close, 1);
1536 continue;
1538 start = open[cp - close];
1540 /* Walk backward until the matching bracket is reached. */
1541 n = 1;
1542 failed = 0;
1543 do {
1544 if (px == 0) {
1545 if (py == 0) {
1546 failed = 1;
1547 break;
1549 do {
1550 py--;
1551 xx = window_copy_find_length(wme, py);
1552 } while (xx == 0 && py > 0);
1553 if (xx == 0 && py == 0) {
1554 failed = 1;
1555 break;
1557 px = xx - 1;
1558 } else
1559 px--;
1561 grid_get_cell(s->grid, px, py, &gc);
1562 if (gc.data.size == 1 &&
1563 (~gc.flags & GRID_FLAG_PADDING)) {
1564 if (*gc.data.data == found)
1565 n++;
1566 else if (*gc.data.data == start)
1567 n--;
1569 } while (n != 0);
1571 /* Move the cursor to the found location if any. */
1572 if (!failed)
1573 window_copy_scroll_to(wme, px, py, 0);
1576 return (WINDOW_COPY_CMD_NOTHING);
1579 static enum window_copy_cmd_action
1580 window_copy_cmd_next_matching_bracket(struct window_copy_cmd_state *cs)
1582 struct window_mode_entry *wme = cs->wme;
1583 u_int np = wme->prefix;
1584 struct window_copy_mode_data *data = wme->data;
1585 struct screen *s = data->backing;
1586 char open[] = "{[(", close[] = "}])";
1587 char tried, found, end, *cp;
1588 u_int px, py, xx, yy, sx, sy, n;
1589 struct grid_cell gc;
1590 int failed;
1591 struct grid_line *gl;
1593 for (; np != 0; np--) {
1594 /* Get cursor position and line length. */
1595 px = data->cx;
1596 py = screen_hsize(s) + data->cy - data->oy;
1597 xx = window_copy_find_length(wme, py);
1598 yy = screen_hsize(s) + screen_size_y(s) - 1;
1599 if (xx == 0)
1600 break;
1603 * Get the current character. If not on a bracket, try the
1604 * next. If still not, then behave like next-word.
1606 tried = 0;
1607 retry:
1608 grid_get_cell(s->grid, px, py, &gc);
1609 if (gc.data.size != 1 || (gc.flags & GRID_FLAG_PADDING))
1610 cp = NULL;
1611 else {
1612 found = *gc.data.data;
1615 * In vi mode, attempt to move to previous bracket if a
1616 * closing bracket is found first. If this fails,
1617 * return to the original cursor position.
1619 cp = strchr(close, found);
1620 if (cp != NULL && data->modekeys == MODEKEY_VI) {
1621 sx = data->cx;
1622 sy = screen_hsize(s) + data->cy - data->oy;
1624 window_copy_scroll_to(wme, px, py, 0);
1625 window_copy_cmd_previous_matching_bracket(cs);
1627 px = data->cx;
1628 py = screen_hsize(s) + data->cy - data->oy;
1629 grid_get_cell(s->grid, px, py, &gc);
1630 if (gc.data.size == 1 &&
1631 (~gc.flags & GRID_FLAG_PADDING) &&
1632 strchr(close, *gc.data.data) != NULL)
1633 window_copy_scroll_to(wme, sx, sy, 0);
1634 break;
1637 cp = strchr(open, found);
1639 if (cp == NULL) {
1640 if (data->modekeys == MODEKEY_EMACS) {
1641 if (!tried && px <= xx) {
1642 px++;
1643 tried = 1;
1644 goto retry;
1646 window_copy_cursor_next_word_end(wme, open, 0);
1647 continue;
1649 /* For vi, continue searching for bracket until EOL. */
1650 if (px > xx) {
1651 if (py == yy)
1652 continue;
1653 gl = grid_get_line(s->grid, py);
1654 if (~gl->flags & GRID_LINE_WRAPPED)
1655 continue;
1656 if (gl->cellsize > s->grid->sx)
1657 continue;
1658 px = 0;
1659 py++;
1660 xx = window_copy_find_length(wme, py);
1661 } else
1662 px++;
1663 goto retry;
1665 end = close[cp - open];
1667 /* Walk forward until the matching bracket is reached. */
1668 n = 1;
1669 failed = 0;
1670 do {
1671 if (px > xx) {
1672 if (py == yy) {
1673 failed = 1;
1674 break;
1676 px = 0;
1677 py++;
1678 xx = window_copy_find_length(wme, py);
1679 } else
1680 px++;
1682 grid_get_cell(s->grid, px, py, &gc);
1683 if (gc.data.size == 1 &&
1684 (~gc.flags & GRID_FLAG_PADDING)) {
1685 if (*gc.data.data == found)
1686 n++;
1687 else if (*gc.data.data == end)
1688 n--;
1690 } while (n != 0);
1692 /* Move the cursor to the found location if any. */
1693 if (!failed)
1694 window_copy_scroll_to(wme, px, py, 0);
1697 return (WINDOW_COPY_CMD_NOTHING);
1700 static enum window_copy_cmd_action
1701 window_copy_cmd_next_paragraph(struct window_copy_cmd_state *cs)
1703 struct window_mode_entry *wme = cs->wme;
1704 u_int np = wme->prefix;
1706 for (; np != 0; np--)
1707 window_copy_next_paragraph(wme);
1708 return (WINDOW_COPY_CMD_NOTHING);
1711 static enum window_copy_cmd_action
1712 window_copy_cmd_next_space(struct window_copy_cmd_state *cs)
1714 struct window_mode_entry *wme = cs->wme;
1715 u_int np = wme->prefix;
1717 for (; np != 0; np--)
1718 window_copy_cursor_next_word(wme, "");
1719 return (WINDOW_COPY_CMD_NOTHING);
1722 static enum window_copy_cmd_action
1723 window_copy_cmd_next_space_end(struct window_copy_cmd_state *cs)
1725 struct window_mode_entry *wme = cs->wme;
1726 u_int np = wme->prefix;
1728 for (; np != 0; np--)
1729 window_copy_cursor_next_word_end(wme, "", 0);
1730 return (WINDOW_COPY_CMD_NOTHING);
1733 static enum window_copy_cmd_action
1734 window_copy_cmd_next_word(struct window_copy_cmd_state *cs)
1736 struct window_mode_entry *wme = cs->wme;
1737 u_int np = wme->prefix;
1738 const char *separators;
1740 separators = options_get_string(cs->s->options, "word-separators");
1742 for (; np != 0; np--)
1743 window_copy_cursor_next_word(wme, separators);
1744 return (WINDOW_COPY_CMD_NOTHING);
1747 static enum window_copy_cmd_action
1748 window_copy_cmd_next_word_end(struct window_copy_cmd_state *cs)
1750 struct window_mode_entry *wme = cs->wme;
1751 u_int np = wme->prefix;
1752 const char *separators;
1754 separators = options_get_string(cs->s->options, "word-separators");
1756 for (; np != 0; np--)
1757 window_copy_cursor_next_word_end(wme, separators, 0);
1758 return (WINDOW_COPY_CMD_NOTHING);
1761 static enum window_copy_cmd_action
1762 window_copy_cmd_other_end(struct window_copy_cmd_state *cs)
1764 struct window_mode_entry *wme = cs->wme;
1765 u_int np = wme->prefix;
1766 struct window_copy_mode_data *data = wme->data;
1768 data->selflag = SEL_CHAR;
1769 if ((np % 2) != 0)
1770 window_copy_other_end(wme);
1771 return (WINDOW_COPY_CMD_NOTHING);
1774 static enum window_copy_cmd_action
1775 window_copy_cmd_page_down(struct window_copy_cmd_state *cs)
1777 struct window_mode_entry *wme = cs->wme;
1778 struct window_copy_mode_data *data = wme->data;
1779 u_int np = wme->prefix;
1781 for (; np != 0; np--) {
1782 if (window_copy_pagedown(wme, 0, data->scroll_exit))
1783 return (WINDOW_COPY_CMD_CANCEL);
1785 return (WINDOW_COPY_CMD_NOTHING);
1788 static enum window_copy_cmd_action
1789 window_copy_cmd_page_down_and_cancel(struct window_copy_cmd_state *cs)
1791 struct window_mode_entry *wme = cs->wme;
1792 u_int np = wme->prefix;
1794 for (; np != 0; np--) {
1795 if (window_copy_pagedown(wme, 0, 1))
1796 return (WINDOW_COPY_CMD_CANCEL);
1798 return (WINDOW_COPY_CMD_NOTHING);
1801 static enum window_copy_cmd_action
1802 window_copy_cmd_page_up(struct window_copy_cmd_state *cs)
1804 struct window_mode_entry *wme = cs->wme;
1805 u_int np = wme->prefix;
1807 for (; np != 0; np--)
1808 window_copy_pageup1(wme, 0);
1809 return (WINDOW_COPY_CMD_NOTHING);
1812 static enum window_copy_cmd_action
1813 window_copy_cmd_previous_paragraph(struct window_copy_cmd_state *cs)
1815 struct window_mode_entry *wme = cs->wme;
1816 u_int np = wme->prefix;
1818 for (; np != 0; np--)
1819 window_copy_previous_paragraph(wme);
1820 return (WINDOW_COPY_CMD_NOTHING);
1823 static enum window_copy_cmd_action
1824 window_copy_cmd_previous_space(struct window_copy_cmd_state *cs)
1826 struct window_mode_entry *wme = cs->wme;
1827 u_int np = wme->prefix;
1829 for (; np != 0; np--)
1830 window_copy_cursor_previous_word(wme, "", 1);
1831 return (WINDOW_COPY_CMD_NOTHING);
1834 static enum window_copy_cmd_action
1835 window_copy_cmd_previous_word(struct window_copy_cmd_state *cs)
1837 struct window_mode_entry *wme = cs->wme;
1838 u_int np = wme->prefix;
1839 const char *separators;
1841 separators = options_get_string(cs->s->options, "word-separators");
1843 for (; np != 0; np--)
1844 window_copy_cursor_previous_word(wme, separators, 1);
1845 return (WINDOW_COPY_CMD_NOTHING);
1848 static enum window_copy_cmd_action
1849 window_copy_cmd_rectangle_on(struct window_copy_cmd_state *cs)
1851 struct window_mode_entry *wme = cs->wme;
1852 struct window_copy_mode_data *data = wme->data;
1854 data->lineflag = LINE_SEL_NONE;
1855 window_copy_rectangle_set(wme, 1);
1857 return (WINDOW_COPY_CMD_NOTHING);
1860 static enum window_copy_cmd_action
1861 window_copy_cmd_rectangle_off(struct window_copy_cmd_state *cs)
1863 struct window_mode_entry *wme = cs->wme;
1864 struct window_copy_mode_data *data = wme->data;
1866 data->lineflag = LINE_SEL_NONE;
1867 window_copy_rectangle_set(wme, 0);
1869 return (WINDOW_COPY_CMD_NOTHING);
1872 static enum window_copy_cmd_action
1873 window_copy_cmd_rectangle_toggle(struct window_copy_cmd_state *cs)
1875 struct window_mode_entry *wme = cs->wme;
1876 struct window_copy_mode_data *data = wme->data;
1878 data->lineflag = LINE_SEL_NONE;
1879 window_copy_rectangle_set(wme, !data->rectflag);
1881 return (WINDOW_COPY_CMD_NOTHING);
1884 static enum window_copy_cmd_action
1885 window_copy_cmd_scroll_down(struct window_copy_cmd_state *cs)
1887 struct window_mode_entry *wme = cs->wme;
1888 struct window_copy_mode_data *data = wme->data;
1889 u_int np = wme->prefix;
1891 for (; np != 0; np--)
1892 window_copy_cursor_down(wme, 1);
1893 if (data->scroll_exit && data->oy == 0)
1894 return (WINDOW_COPY_CMD_CANCEL);
1895 return (WINDOW_COPY_CMD_NOTHING);
1898 static enum window_copy_cmd_action
1899 window_copy_cmd_scroll_down_and_cancel(struct window_copy_cmd_state *cs)
1901 struct window_mode_entry *wme = cs->wme;
1902 struct window_copy_mode_data *data = wme->data;
1903 u_int np = wme->prefix;
1905 for (; np != 0; np--)
1906 window_copy_cursor_down(wme, 1);
1907 if (data->oy == 0)
1908 return (WINDOW_COPY_CMD_CANCEL);
1909 return (WINDOW_COPY_CMD_NOTHING);
1912 static enum window_copy_cmd_action
1913 window_copy_cmd_scroll_up(struct window_copy_cmd_state *cs)
1915 struct window_mode_entry *wme = cs->wme;
1916 u_int np = wme->prefix;
1918 for (; np != 0; np--)
1919 window_copy_cursor_up(wme, 1);
1920 return (WINDOW_COPY_CMD_NOTHING);
1923 static enum window_copy_cmd_action
1924 window_copy_cmd_search_again(struct window_copy_cmd_state *cs)
1926 struct window_mode_entry *wme = cs->wme;
1927 struct window_copy_mode_data *data = wme->data;
1928 u_int np = wme->prefix;
1930 if (data->searchtype == WINDOW_COPY_SEARCHUP) {
1931 for (; np != 0; np--)
1932 window_copy_search_up(wme, data->searchregex);
1933 } else if (data->searchtype == WINDOW_COPY_SEARCHDOWN) {
1934 for (; np != 0; np--)
1935 window_copy_search_down(wme, data->searchregex);
1937 return (WINDOW_COPY_CMD_NOTHING);
1940 static enum window_copy_cmd_action
1941 window_copy_cmd_search_reverse(struct window_copy_cmd_state *cs)
1943 struct window_mode_entry *wme = cs->wme;
1944 struct window_copy_mode_data *data = wme->data;
1945 u_int np = wme->prefix;
1947 if (data->searchtype == WINDOW_COPY_SEARCHUP) {
1948 for (; np != 0; np--)
1949 window_copy_search_down(wme, data->searchregex);
1950 } else if (data->searchtype == WINDOW_COPY_SEARCHDOWN) {
1951 for (; np != 0; np--)
1952 window_copy_search_up(wme, data->searchregex);
1954 return (WINDOW_COPY_CMD_NOTHING);
1957 static enum window_copy_cmd_action
1958 window_copy_cmd_select_line(struct window_copy_cmd_state *cs)
1960 struct window_mode_entry *wme = cs->wme;
1961 struct window_copy_mode_data *data = wme->data;
1962 u_int np = wme->prefix;
1964 data->lineflag = LINE_SEL_LEFT_RIGHT;
1965 data->rectflag = 0;
1966 data->selflag = SEL_LINE;
1967 data->dx = data->cx;
1968 data->dy = screen_hsize(data->backing) + data->cy - data->oy;
1970 window_copy_cursor_start_of_line(wme);
1971 data->selrx = data->cx;
1972 data->selry = screen_hsize(data->backing) + data->cy - data->oy;
1973 data->endselry = data->selry;
1974 window_copy_start_selection(wme);
1975 window_copy_cursor_end_of_line(wme);
1976 data->endselry = screen_hsize(data->backing) + data->cy - data->oy;
1977 data->endselrx = window_copy_find_length(wme, data->endselry);
1978 for (; np > 1; np--) {
1979 window_copy_cursor_down(wme, 0);
1980 window_copy_cursor_end_of_line(wme);
1983 return (WINDOW_COPY_CMD_REDRAW);
1986 static enum window_copy_cmd_action
1987 window_copy_cmd_select_word(struct window_copy_cmd_state *cs)
1989 struct window_mode_entry *wme = cs->wme;
1990 struct options *session_options = cs->s->options;
1991 struct window_copy_mode_data *data = wme->data;
1992 u_int px, py, nextx, nexty;
1994 data->lineflag = LINE_SEL_LEFT_RIGHT;
1995 data->rectflag = 0;
1996 data->selflag = SEL_WORD;
1997 data->dx = data->cx;
1998 data->dy = screen_hsize(data->backing) + data->cy - data->oy;
2000 data->separators = options_get_string(session_options,
2001 "word-separators");
2002 window_copy_cursor_previous_word(wme, data->separators, 0);
2003 px = data->cx;
2004 py = screen_hsize(data->backing) + data->cy - data->oy;
2005 data->selrx = px;
2006 data->selry = py;
2007 window_copy_start_selection(wme);
2009 /* Handle single character words. */
2010 nextx = px + 1;
2011 nexty = py;
2012 if (grid_get_line(data->backing->grid, nexty)->flags &
2013 GRID_LINE_WRAPPED && nextx > screen_size_x(data->backing) - 1) {
2014 nextx = 0;
2015 nexty++;
2017 if (px >= window_copy_find_length(wme, py) ||
2018 !window_copy_in_set(wme, nextx, nexty, WHITESPACE))
2019 window_copy_cursor_next_word_end(wme, data->separators, 1);
2020 else {
2021 window_copy_update_cursor(wme, px, data->cy);
2022 if (window_copy_update_selection(wme, 1, 1))
2023 window_copy_redraw_lines(wme, data->cy, 1);
2025 data->endselrx = data->cx;
2026 data->endselry = screen_hsize(data->backing) + data->cy - data->oy;
2027 if (data->dy > data->endselry) {
2028 data->dy = data->endselry;
2029 data->dx = data->endselrx;
2030 } else if (data->dx > data->endselrx)
2031 data->dx = data->endselrx;
2033 return (WINDOW_COPY_CMD_REDRAW);
2036 static enum window_copy_cmd_action
2037 window_copy_cmd_set_mark(struct window_copy_cmd_state *cs)
2039 struct window_copy_mode_data *data = cs->wme->data;
2041 data->mx = data->cx;
2042 data->my = screen_hsize(data->backing) + data->cy - data->oy;
2043 data->showmark = 1;
2044 return (WINDOW_COPY_CMD_REDRAW);
2047 static enum window_copy_cmd_action
2048 window_copy_cmd_start_of_line(struct window_copy_cmd_state *cs)
2050 struct window_mode_entry *wme = cs->wme;
2052 window_copy_cursor_start_of_line(wme);
2053 return (WINDOW_COPY_CMD_NOTHING);
2056 static enum window_copy_cmd_action
2057 window_copy_cmd_top_line(struct window_copy_cmd_state *cs)
2059 struct window_mode_entry *wme = cs->wme;
2060 struct window_copy_mode_data *data = wme->data;
2062 data->cx = 0;
2063 data->cy = 0;
2065 window_copy_update_selection(wme, 1, 0);
2066 return (WINDOW_COPY_CMD_REDRAW);
2069 static enum window_copy_cmd_action
2070 window_copy_cmd_copy_pipe_no_clear(struct window_copy_cmd_state *cs)
2072 struct window_mode_entry *wme = cs->wme;
2073 struct client *c = cs->c;
2074 struct session *s = cs->s;
2075 struct winlink *wl = cs->wl;
2076 struct window_pane *wp = wme->wp;
2077 char *command = NULL, *prefix = NULL;
2078 const char *arg1 = args_string(cs->args, 1);
2079 const char *arg2 = args_string(cs->args, 2);
2081 if (arg2 != NULL)
2082 prefix = format_single(NULL, arg2, c, s, wl, wp);
2084 if (s != NULL && arg1 != NULL && *arg1 != '\0')
2085 command = format_single(NULL, arg1, c, s, wl, wp);
2086 window_copy_copy_pipe(wme, s, prefix, command);
2087 free(command);
2089 free(prefix);
2090 return (WINDOW_COPY_CMD_NOTHING);
2093 static enum window_copy_cmd_action
2094 window_copy_cmd_copy_pipe(struct window_copy_cmd_state *cs)
2096 struct window_mode_entry *wme = cs->wme;
2098 window_copy_cmd_copy_pipe_no_clear(cs);
2099 window_copy_clear_selection(wme);
2100 return (WINDOW_COPY_CMD_REDRAW);
2103 static enum window_copy_cmd_action
2104 window_copy_cmd_copy_pipe_and_cancel(struct window_copy_cmd_state *cs)
2106 struct window_mode_entry *wme = cs->wme;
2108 window_copy_cmd_copy_pipe_no_clear(cs);
2109 window_copy_clear_selection(wme);
2110 return (WINDOW_COPY_CMD_CANCEL);
2113 static enum window_copy_cmd_action
2114 window_copy_cmd_pipe_no_clear(struct window_copy_cmd_state *cs)
2116 struct window_mode_entry *wme = cs->wme;
2117 struct client *c = cs->c;
2118 struct session *s = cs->s;
2119 struct winlink *wl = cs->wl;
2120 struct window_pane *wp = wme->wp;
2121 char *command = NULL;
2122 const char *arg1 = args_string(cs->args, 1);
2124 if (s != NULL && arg1 != NULL && *arg1 != '\0')
2125 command = format_single(NULL, arg1, c, s, wl, wp);
2126 window_copy_pipe(wme, s, command);
2127 free(command);
2129 return (WINDOW_COPY_CMD_NOTHING);
2132 static enum window_copy_cmd_action
2133 window_copy_cmd_pipe(struct window_copy_cmd_state *cs)
2135 struct window_mode_entry *wme = cs->wme;
2137 window_copy_cmd_pipe_no_clear(cs);
2138 window_copy_clear_selection(wme);
2139 return (WINDOW_COPY_CMD_REDRAW);
2142 static enum window_copy_cmd_action
2143 window_copy_cmd_pipe_and_cancel(struct window_copy_cmd_state *cs)
2145 struct window_mode_entry *wme = cs->wme;
2147 window_copy_cmd_pipe_no_clear(cs);
2148 window_copy_clear_selection(wme);
2149 return (WINDOW_COPY_CMD_CANCEL);
2152 static enum window_copy_cmd_action
2153 window_copy_cmd_goto_line(struct window_copy_cmd_state *cs)
2155 struct window_mode_entry *wme = cs->wme;
2156 const char *arg1 = args_string(cs->args, 1);
2158 if (*arg1 != '\0')
2159 window_copy_goto_line(wme, arg1);
2160 return (WINDOW_COPY_CMD_NOTHING);
2163 static enum window_copy_cmd_action
2164 window_copy_cmd_jump_backward(struct window_copy_cmd_state *cs)
2166 struct window_mode_entry *wme = cs->wme;
2167 struct window_copy_mode_data *data = wme->data;
2168 u_int np = wme->prefix;
2169 const char *arg1 = args_string(cs->args, 1);
2171 if (*arg1 != '\0') {
2172 data->jumptype = WINDOW_COPY_JUMPBACKWARD;
2173 free(data->jumpchar);
2174 data->jumpchar = utf8_fromcstr(arg1);
2175 for (; np != 0; np--)
2176 window_copy_cursor_jump_back(wme);
2178 return (WINDOW_COPY_CMD_NOTHING);
2181 static enum window_copy_cmd_action
2182 window_copy_cmd_jump_forward(struct window_copy_cmd_state *cs)
2184 struct window_mode_entry *wme = cs->wme;
2185 struct window_copy_mode_data *data = wme->data;
2186 u_int np = wme->prefix;
2187 const char *arg1 = args_string(cs->args, 1);
2189 if (*arg1 != '\0') {
2190 data->jumptype = WINDOW_COPY_JUMPFORWARD;
2191 free(data->jumpchar);
2192 data->jumpchar = utf8_fromcstr(arg1);
2193 for (; np != 0; np--)
2194 window_copy_cursor_jump(wme);
2196 return (WINDOW_COPY_CMD_NOTHING);
2199 static enum window_copy_cmd_action
2200 window_copy_cmd_jump_to_backward(struct window_copy_cmd_state *cs)
2202 struct window_mode_entry *wme = cs->wme;
2203 struct window_copy_mode_data *data = wme->data;
2204 u_int np = wme->prefix;
2205 const char *arg1 = args_string(cs->args, 1);
2207 if (*arg1 != '\0') {
2208 data->jumptype = WINDOW_COPY_JUMPTOBACKWARD;
2209 free(data->jumpchar);
2210 data->jumpchar = utf8_fromcstr(arg1);
2211 for (; np != 0; np--)
2212 window_copy_cursor_jump_to_back(wme);
2214 return (WINDOW_COPY_CMD_NOTHING);
2217 static enum window_copy_cmd_action
2218 window_copy_cmd_jump_to_forward(struct window_copy_cmd_state *cs)
2220 struct window_mode_entry *wme = cs->wme;
2221 struct window_copy_mode_data *data = wme->data;
2222 u_int np = wme->prefix;
2223 const char *arg1 = args_string(cs->args, 1);
2225 if (*arg1 != '\0') {
2226 data->jumptype = WINDOW_COPY_JUMPTOFORWARD;
2227 free(data->jumpchar);
2228 data->jumpchar = utf8_fromcstr(arg1);
2229 for (; np != 0; np--)
2230 window_copy_cursor_jump_to(wme);
2232 return (WINDOW_COPY_CMD_NOTHING);
2235 static enum window_copy_cmd_action
2236 window_copy_cmd_jump_to_mark(struct window_copy_cmd_state *cs)
2238 struct window_mode_entry *wme = cs->wme;
2240 window_copy_jump_to_mark(wme);
2241 return (WINDOW_COPY_CMD_NOTHING);
2244 static enum window_copy_cmd_action
2245 window_copy_cmd_next_prompt(struct window_copy_cmd_state *cs)
2247 struct window_mode_entry *wme = cs->wme;
2249 window_copy_cursor_prompt(wme, 1);
2250 return (WINDOW_COPY_CMD_NOTHING);
2253 static enum window_copy_cmd_action
2254 window_copy_cmd_previous_prompt(struct window_copy_cmd_state *cs)
2256 struct window_mode_entry *wme = cs->wme;
2258 window_copy_cursor_prompt(wme, 0);
2259 return (WINDOW_COPY_CMD_NOTHING);
2262 static enum window_copy_cmd_action
2263 window_copy_cmd_search_backward(struct window_copy_cmd_state *cs)
2265 struct window_mode_entry *wme = cs->wme;
2266 struct window_copy_mode_data *data = wme->data;
2267 u_int np = wme->prefix;
2269 if (!window_copy_expand_search_string(cs))
2270 return (WINDOW_COPY_CMD_NOTHING);
2272 if (data->searchstr != NULL) {
2273 data->searchtype = WINDOW_COPY_SEARCHUP;
2274 data->searchregex = 1;
2275 data->timeout = 0;
2276 for (; np != 0; np--)
2277 window_copy_search_up(wme, 1);
2279 return (WINDOW_COPY_CMD_NOTHING);
2282 static enum window_copy_cmd_action
2283 window_copy_cmd_search_backward_text(struct window_copy_cmd_state *cs)
2285 struct window_mode_entry *wme = cs->wme;
2286 struct window_copy_mode_data *data = wme->data;
2287 u_int np = wme->prefix;
2289 if (!window_copy_expand_search_string(cs))
2290 return (WINDOW_COPY_CMD_NOTHING);
2292 if (data->searchstr != NULL) {
2293 data->searchtype = WINDOW_COPY_SEARCHUP;
2294 data->searchregex = 0;
2295 data->timeout = 0;
2296 for (; np != 0; np--)
2297 window_copy_search_up(wme, 0);
2299 return (WINDOW_COPY_CMD_NOTHING);
2302 static enum window_copy_cmd_action
2303 window_copy_cmd_search_forward(struct window_copy_cmd_state *cs)
2305 struct window_mode_entry *wme = cs->wme;
2306 struct window_copy_mode_data *data = wme->data;
2307 u_int np = wme->prefix;
2309 if (!window_copy_expand_search_string(cs))
2310 return (WINDOW_COPY_CMD_NOTHING);
2312 if (data->searchstr != NULL) {
2313 data->searchtype = WINDOW_COPY_SEARCHDOWN;
2314 data->searchregex = 1;
2315 data->timeout = 0;
2316 for (; np != 0; np--)
2317 window_copy_search_down(wme, 1);
2319 return (WINDOW_COPY_CMD_NOTHING);
2322 static enum window_copy_cmd_action
2323 window_copy_cmd_search_forward_text(struct window_copy_cmd_state *cs)
2325 struct window_mode_entry *wme = cs->wme;
2326 struct window_copy_mode_data *data = wme->data;
2327 u_int np = wme->prefix;
2329 if (!window_copy_expand_search_string(cs))
2330 return (WINDOW_COPY_CMD_NOTHING);
2332 if (data->searchstr != NULL) {
2333 data->searchtype = WINDOW_COPY_SEARCHDOWN;
2334 data->searchregex = 0;
2335 data->timeout = 0;
2336 for (; np != 0; np--)
2337 window_copy_search_down(wme, 0);
2339 return (WINDOW_COPY_CMD_NOTHING);
2342 static enum window_copy_cmd_action
2343 window_copy_cmd_search_backward_incremental(struct window_copy_cmd_state *cs)
2345 struct window_mode_entry *wme = cs->wme;
2346 struct window_copy_mode_data *data = wme->data;
2347 const char *arg1 = args_string(cs->args, 1);
2348 const char *ss = data->searchstr;
2349 char prefix;
2350 enum window_copy_cmd_action action = WINDOW_COPY_CMD_NOTHING;
2352 data->timeout = 0;
2354 log_debug("%s: %s", __func__, arg1);
2356 prefix = *arg1++;
2357 if (data->searchx == -1 || data->searchy == -1) {
2358 data->searchx = data->cx;
2359 data->searchy = data->cy;
2360 data->searcho = data->oy;
2361 } else if (ss != NULL && strcmp(arg1, ss) != 0) {
2362 data->cx = data->searchx;
2363 data->cy = data->searchy;
2364 data->oy = data->searcho;
2365 action = WINDOW_COPY_CMD_REDRAW;
2367 if (*arg1 == '\0') {
2368 window_copy_clear_marks(wme);
2369 return (WINDOW_COPY_CMD_REDRAW);
2371 switch (prefix) {
2372 case '=':
2373 case '-':
2374 data->searchtype = WINDOW_COPY_SEARCHUP;
2375 data->searchregex = 0;
2376 free(data->searchstr);
2377 data->searchstr = xstrdup(arg1);
2378 if (!window_copy_search_up(wme, 0)) {
2379 window_copy_clear_marks(wme);
2380 return (WINDOW_COPY_CMD_REDRAW);
2382 break;
2383 case '+':
2384 data->searchtype = WINDOW_COPY_SEARCHDOWN;
2385 data->searchregex = 0;
2386 free(data->searchstr);
2387 data->searchstr = xstrdup(arg1);
2388 if (!window_copy_search_down(wme, 0)) {
2389 window_copy_clear_marks(wme);
2390 return (WINDOW_COPY_CMD_REDRAW);
2392 break;
2394 return (action);
2397 static enum window_copy_cmd_action
2398 window_copy_cmd_search_forward_incremental(struct window_copy_cmd_state *cs)
2400 struct window_mode_entry *wme = cs->wme;
2401 struct window_copy_mode_data *data = wme->data;
2402 const char *arg1 = args_string(cs->args, 1);
2403 const char *ss = data->searchstr;
2404 char prefix;
2405 enum window_copy_cmd_action action = WINDOW_COPY_CMD_NOTHING;
2407 data->timeout = 0;
2409 log_debug("%s: %s", __func__, arg1);
2411 prefix = *arg1++;
2412 if (data->searchx == -1 || data->searchy == -1) {
2413 data->searchx = data->cx;
2414 data->searchy = data->cy;
2415 data->searcho = data->oy;
2416 } else if (ss != NULL && strcmp(arg1, ss) != 0) {
2417 data->cx = data->searchx;
2418 data->cy = data->searchy;
2419 data->oy = data->searcho;
2420 action = WINDOW_COPY_CMD_REDRAW;
2422 if (*arg1 == '\0') {
2423 window_copy_clear_marks(wme);
2424 return (WINDOW_COPY_CMD_REDRAW);
2426 switch (prefix) {
2427 case '=':
2428 case '+':
2429 data->searchtype = WINDOW_COPY_SEARCHDOWN;
2430 data->searchregex = 0;
2431 free(data->searchstr);
2432 data->searchstr = xstrdup(arg1);
2433 if (!window_copy_search_down(wme, 0)) {
2434 window_copy_clear_marks(wme);
2435 return (WINDOW_COPY_CMD_REDRAW);
2437 break;
2438 case '-':
2439 data->searchtype = WINDOW_COPY_SEARCHUP;
2440 data->searchregex = 0;
2441 free(data->searchstr);
2442 data->searchstr = xstrdup(arg1);
2443 if (!window_copy_search_up(wme, 0)) {
2444 window_copy_clear_marks(wme);
2445 return (WINDOW_COPY_CMD_REDRAW);
2448 return (action);
2451 static enum window_copy_cmd_action
2452 window_copy_cmd_refresh_from_pane(struct window_copy_cmd_state *cs)
2454 struct window_mode_entry *wme = cs->wme;
2455 struct window_pane *wp = wme->swp;
2456 struct window_copy_mode_data *data = wme->data;
2458 if (data->viewmode)
2459 return (WINDOW_COPY_CMD_NOTHING);
2461 screen_free(data->backing);
2462 free(data->backing);
2463 data->backing = window_copy_clone_screen(&wp->base, &data->screen, NULL, NULL, wme->swp != wme->wp);
2465 window_copy_size_changed(wme);
2466 return (WINDOW_COPY_CMD_REDRAW);
2469 static const struct {
2470 const char *command;
2471 u_int minargs;
2472 u_int maxargs;
2473 enum window_copy_cmd_clear clear;
2474 enum window_copy_cmd_action (*f)(struct window_copy_cmd_state *);
2475 } window_copy_cmd_table[] = {
2476 { .command = "append-selection",
2477 .minargs = 0,
2478 .maxargs = 0,
2479 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2480 .f = window_copy_cmd_append_selection
2482 { .command = "append-selection-and-cancel",
2483 .minargs = 0,
2484 .maxargs = 0,
2485 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2486 .f = window_copy_cmd_append_selection_and_cancel
2488 { .command = "back-to-indentation",
2489 .minargs = 0,
2490 .maxargs = 0,
2491 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2492 .f = window_copy_cmd_back_to_indentation
2494 { .command = "begin-selection",
2495 .minargs = 0,
2496 .maxargs = 0,
2497 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2498 .f = window_copy_cmd_begin_selection
2500 { .command = "bottom-line",
2501 .minargs = 0,
2502 .maxargs = 0,
2503 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2504 .f = window_copy_cmd_bottom_line
2506 { .command = "cancel",
2507 .minargs = 0,
2508 .maxargs = 0,
2509 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2510 .f = window_copy_cmd_cancel
2512 { .command = "clear-selection",
2513 .minargs = 0,
2514 .maxargs = 0,
2515 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2516 .f = window_copy_cmd_clear_selection
2518 { .command = "copy-end-of-line",
2519 .minargs = 0,
2520 .maxargs = 1,
2521 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2522 .f = window_copy_cmd_copy_end_of_line
2524 { .command = "copy-end-of-line-and-cancel",
2525 .minargs = 0,
2526 .maxargs = 1,
2527 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2528 .f = window_copy_cmd_copy_end_of_line_and_cancel
2530 { .command = "copy-pipe-end-of-line",
2531 .minargs = 0,
2532 .maxargs = 2,
2533 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2534 .f = window_copy_cmd_copy_pipe_end_of_line
2536 { .command = "copy-pipe-end-of-line-and-cancel",
2537 .minargs = 0,
2538 .maxargs = 2,
2539 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2540 .f = window_copy_cmd_copy_pipe_end_of_line_and_cancel
2542 { .command = "copy-line",
2543 .minargs = 0,
2544 .maxargs = 1,
2545 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2546 .f = window_copy_cmd_copy_line
2548 { .command = "copy-line-and-cancel",
2549 .minargs = 0,
2550 .maxargs = 1,
2551 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2552 .f = window_copy_cmd_copy_line_and_cancel
2554 { .command = "copy-pipe-line",
2555 .minargs = 0,
2556 .maxargs = 2,
2557 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2558 .f = window_copy_cmd_copy_pipe_line
2560 { .command = "copy-pipe-line-and-cancel",
2561 .minargs = 0,
2562 .maxargs = 2,
2563 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2564 .f = window_copy_cmd_copy_pipe_line_and_cancel
2566 { .command = "copy-pipe-no-clear",
2567 .minargs = 0,
2568 .maxargs = 2,
2569 .clear = WINDOW_COPY_CMD_CLEAR_NEVER,
2570 .f = window_copy_cmd_copy_pipe_no_clear
2572 { .command = "copy-pipe",
2573 .minargs = 0,
2574 .maxargs = 2,
2575 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2576 .f = window_copy_cmd_copy_pipe
2578 { .command = "copy-pipe-and-cancel",
2579 .minargs = 0,
2580 .maxargs = 2,
2581 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2582 .f = window_copy_cmd_copy_pipe_and_cancel
2584 { .command = "copy-selection-no-clear",
2585 .minargs = 0,
2586 .maxargs = 1,
2587 .clear = WINDOW_COPY_CMD_CLEAR_NEVER,
2588 .f = window_copy_cmd_copy_selection_no_clear
2590 { .command = "copy-selection",
2591 .minargs = 0,
2592 .maxargs = 1,
2593 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2594 .f = window_copy_cmd_copy_selection
2596 { .command = "copy-selection-and-cancel",
2597 .minargs = 0,
2598 .maxargs = 1,
2599 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2600 .f = window_copy_cmd_copy_selection_and_cancel
2602 { .command = "cursor-down",
2603 .minargs = 0,
2604 .maxargs = 0,
2605 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2606 .f = window_copy_cmd_cursor_down
2608 { .command = "cursor-down-and-cancel",
2609 .minargs = 0,
2610 .maxargs = 0,
2611 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2612 .f = window_copy_cmd_cursor_down_and_cancel
2614 { .command = "cursor-left",
2615 .minargs = 0,
2616 .maxargs = 0,
2617 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2618 .f = window_copy_cmd_cursor_left
2620 { .command = "cursor-right",
2621 .minargs = 0,
2622 .maxargs = 0,
2623 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2624 .f = window_copy_cmd_cursor_right
2626 { .command = "cursor-up",
2627 .minargs = 0,
2628 .maxargs = 0,
2629 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2630 .f = window_copy_cmd_cursor_up
2632 { .command = "end-of-line",
2633 .minargs = 0,
2634 .maxargs = 0,
2635 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2636 .f = window_copy_cmd_end_of_line
2638 { .command = "goto-line",
2639 .minargs = 1,
2640 .maxargs = 1,
2641 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2642 .f = window_copy_cmd_goto_line
2644 { .command = "halfpage-down",
2645 .minargs = 0,
2646 .maxargs = 0,
2647 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2648 .f = window_copy_cmd_halfpage_down
2650 { .command = "halfpage-down-and-cancel",
2651 .minargs = 0,
2652 .maxargs = 0,
2653 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2654 .f = window_copy_cmd_halfpage_down_and_cancel
2656 { .command = "halfpage-up",
2657 .minargs = 0,
2658 .maxargs = 0,
2659 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2660 .f = window_copy_cmd_halfpage_up
2662 { .command = "history-bottom",
2663 .minargs = 0,
2664 .maxargs = 0,
2665 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2666 .f = window_copy_cmd_history_bottom
2668 { .command = "history-top",
2669 .minargs = 0,
2670 .maxargs = 0,
2671 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2672 .f = window_copy_cmd_history_top
2674 { .command = "jump-again",
2675 .minargs = 0,
2676 .maxargs = 0,
2677 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2678 .f = window_copy_cmd_jump_again
2680 { .command = "jump-backward",
2681 .minargs = 1,
2682 .maxargs = 1,
2683 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2684 .f = window_copy_cmd_jump_backward
2686 { .command = "jump-forward",
2687 .minargs = 1,
2688 .maxargs = 1,
2689 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2690 .f = window_copy_cmd_jump_forward
2692 { .command = "jump-reverse",
2693 .minargs = 0,
2694 .maxargs = 0,
2695 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2696 .f = window_copy_cmd_jump_reverse
2698 { .command = "jump-to-backward",
2699 .minargs = 1,
2700 .maxargs = 1,
2701 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2702 .f = window_copy_cmd_jump_to_backward
2704 { .command = "jump-to-forward",
2705 .minargs = 1,
2706 .maxargs = 1,
2707 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2708 .f = window_copy_cmd_jump_to_forward
2710 { .command = "jump-to-mark",
2711 .minargs = 0,
2712 .maxargs = 0,
2713 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2714 .f = window_copy_cmd_jump_to_mark
2716 { .command = "next-prompt",
2717 .minargs = 0,
2718 .maxargs = 0,
2719 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2720 .f = window_copy_cmd_next_prompt
2722 { .command = "previous-prompt",
2723 .minargs = 0,
2724 .maxargs = 0,
2725 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2726 .f = window_copy_cmd_previous_prompt
2728 { .command = "middle-line",
2729 .minargs = 0,
2730 .maxargs = 0,
2731 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2732 .f = window_copy_cmd_middle_line
2734 { .command = "next-matching-bracket",
2735 .minargs = 0,
2736 .maxargs = 0,
2737 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2738 .f = window_copy_cmd_next_matching_bracket
2740 { .command = "next-paragraph",
2741 .minargs = 0,
2742 .maxargs = 0,
2743 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2744 .f = window_copy_cmd_next_paragraph
2746 { .command = "next-space",
2747 .minargs = 0,
2748 .maxargs = 0,
2749 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2750 .f = window_copy_cmd_next_space
2752 { .command = "next-space-end",
2753 .minargs = 0,
2754 .maxargs = 0,
2755 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2756 .f = window_copy_cmd_next_space_end
2758 { .command = "next-word",
2759 .minargs = 0,
2760 .maxargs = 0,
2761 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2762 .f = window_copy_cmd_next_word
2764 { .command = "next-word-end",
2765 .minargs = 0,
2766 .maxargs = 0,
2767 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2768 .f = window_copy_cmd_next_word_end
2770 { .command = "other-end",
2771 .minargs = 0,
2772 .maxargs = 0,
2773 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2774 .f = window_copy_cmd_other_end
2776 { .command = "page-down",
2777 .minargs = 0,
2778 .maxargs = 0,
2779 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2780 .f = window_copy_cmd_page_down
2782 { .command = "page-down-and-cancel",
2783 .minargs = 0,
2784 .maxargs = 0,
2785 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2786 .f = window_copy_cmd_page_down_and_cancel
2788 { .command = "page-up",
2789 .minargs = 0,
2790 .maxargs = 0,
2791 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2792 .f = window_copy_cmd_page_up
2794 { .command = "pipe-no-clear",
2795 .minargs = 0,
2796 .maxargs = 1,
2797 .clear = WINDOW_COPY_CMD_CLEAR_NEVER,
2798 .f = window_copy_cmd_pipe_no_clear
2800 { .command = "pipe",
2801 .minargs = 0,
2802 .maxargs = 1,
2803 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2804 .f = window_copy_cmd_pipe
2806 { .command = "pipe-and-cancel",
2807 .minargs = 0,
2808 .maxargs = 1,
2809 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2810 .f = window_copy_cmd_pipe_and_cancel
2812 { .command = "previous-matching-bracket",
2813 .minargs = 0,
2814 .maxargs = 0,
2815 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2816 .f = window_copy_cmd_previous_matching_bracket
2818 { .command = "previous-paragraph",
2819 .minargs = 0,
2820 .maxargs = 0,
2821 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2822 .f = window_copy_cmd_previous_paragraph
2824 { .command = "previous-space",
2825 .minargs = 0,
2826 .maxargs = 0,
2827 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2828 .f = window_copy_cmd_previous_space
2830 { .command = "previous-word",
2831 .minargs = 0,
2832 .maxargs = 0,
2833 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2834 .f = window_copy_cmd_previous_word
2836 { .command = "rectangle-on",
2837 .minargs = 0,
2838 .maxargs = 0,
2839 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2840 .f = window_copy_cmd_rectangle_on
2842 { .command = "rectangle-off",
2843 .minargs = 0,
2844 .maxargs = 0,
2845 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2846 .f = window_copy_cmd_rectangle_off
2848 { .command = "rectangle-toggle",
2849 .minargs = 0,
2850 .maxargs = 0,
2851 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2852 .f = window_copy_cmd_rectangle_toggle
2854 { .command = "refresh-from-pane",
2855 .minargs = 0,
2856 .maxargs = 0,
2857 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2858 .f = window_copy_cmd_refresh_from_pane
2860 { .command = "scroll-bottom",
2861 .minargs = 0,
2862 .maxargs = 0,
2863 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2864 .f = window_copy_cmd_scroll_bottom
2866 { .command = "scroll-down",
2867 .minargs = 0,
2868 .maxargs = 0,
2869 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2870 .f = window_copy_cmd_scroll_down
2872 { .command = "scroll-down-and-cancel",
2873 .minargs = 0,
2874 .maxargs = 0,
2875 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2876 .f = window_copy_cmd_scroll_down_and_cancel
2878 { .command = "scroll-middle",
2879 .minargs = 0,
2880 .maxargs = 0,
2881 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2882 .f = window_copy_cmd_scroll_middle
2884 { .command = "scroll-top",
2885 .minargs = 0,
2886 .maxargs = 0,
2887 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2888 .f = window_copy_cmd_scroll_top
2890 { .command = "scroll-up",
2891 .minargs = 0,
2892 .maxargs = 0,
2893 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2894 .f = window_copy_cmd_scroll_up
2896 { .command = "search-again",
2897 .minargs = 0,
2898 .maxargs = 0,
2899 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2900 .f = window_copy_cmd_search_again
2902 { .command = "search-backward",
2903 .minargs = 0,
2904 .maxargs = 1,
2905 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2906 .f = window_copy_cmd_search_backward
2908 { .command = "search-backward-text",
2909 .minargs = 0,
2910 .maxargs = 1,
2911 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2912 .f = window_copy_cmd_search_backward_text
2914 { .command = "search-backward-incremental",
2915 .minargs = 1,
2916 .maxargs = 1,
2917 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2918 .f = window_copy_cmd_search_backward_incremental
2920 { .command = "search-forward",
2921 .minargs = 0,
2922 .maxargs = 1,
2923 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2924 .f = window_copy_cmd_search_forward
2926 { .command = "search-forward-text",
2927 .minargs = 0,
2928 .maxargs = 1,
2929 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2930 .f = window_copy_cmd_search_forward_text
2932 { .command = "search-forward-incremental",
2933 .minargs = 1,
2934 .maxargs = 1,
2935 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2936 .f = window_copy_cmd_search_forward_incremental
2938 { .command = "search-reverse",
2939 .minargs = 0,
2940 .maxargs = 0,
2941 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2942 .f = window_copy_cmd_search_reverse
2944 { .command = "select-line",
2945 .minargs = 0,
2946 .maxargs = 0,
2947 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2948 .f = window_copy_cmd_select_line
2950 { .command = "select-word",
2951 .minargs = 0,
2952 .maxargs = 0,
2953 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2954 .f = window_copy_cmd_select_word
2956 { .command = "set-mark",
2957 .minargs = 0,
2958 .maxargs = 0,
2959 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2960 .f = window_copy_cmd_set_mark
2962 { .command = "start-of-line",
2963 .minargs = 0,
2964 .maxargs = 0,
2965 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2966 .f = window_copy_cmd_start_of_line
2968 { .command = "stop-selection",
2969 .minargs = 0,
2970 .maxargs = 0,
2971 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2972 .f = window_copy_cmd_stop_selection
2974 { .command = "toggle-position",
2975 .minargs = 0,
2976 .maxargs = 0,
2977 .clear = WINDOW_COPY_CMD_CLEAR_NEVER,
2978 .f = window_copy_cmd_toggle_position
2980 { .command = "top-line",
2981 .minargs = 0,
2982 .maxargs = 0,
2983 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2984 .f = window_copy_cmd_top_line
2988 static void
2989 window_copy_command(struct window_mode_entry *wme, struct client *c,
2990 struct session *s, struct winlink *wl, struct args *args,
2991 struct mouse_event *m)
2993 struct window_copy_mode_data *data = wme->data;
2994 struct window_copy_cmd_state cs;
2995 enum window_copy_cmd_action action;
2996 enum window_copy_cmd_clear clear = WINDOW_COPY_CMD_CLEAR_NEVER;
2997 const char *command;
2998 u_int i, count = args_count(args);
2999 int keys;
3001 if (count == 0)
3002 return;
3003 command = args_string(args, 0);
3005 if (m != NULL && m->valid && !MOUSE_WHEEL(m->b))
3006 window_copy_move_mouse(m);
3008 cs.wme = wme;
3009 cs.args = args;
3010 cs.m = m;
3012 cs.c = c;
3013 cs.s = s;
3014 cs.wl = wl;
3016 action = WINDOW_COPY_CMD_NOTHING;
3017 for (i = 0; i < nitems(window_copy_cmd_table); i++) {
3018 if (strcmp(window_copy_cmd_table[i].command, command) == 0) {
3019 if (count - 1 < window_copy_cmd_table[i].minargs ||
3020 count - 1 > window_copy_cmd_table[i].maxargs)
3021 break;
3022 clear = window_copy_cmd_table[i].clear;
3023 action = window_copy_cmd_table[i].f(&cs);
3024 break;
3028 if (strncmp(command, "search-", 7) != 0 && data->searchmark != NULL) {
3029 keys = options_get_number(wme->wp->window->options, "mode-keys");
3030 if (clear == WINDOW_COPY_CMD_CLEAR_EMACS_ONLY &&
3031 keys == MODEKEY_VI)
3032 clear = WINDOW_COPY_CMD_CLEAR_NEVER;
3033 if (clear != WINDOW_COPY_CMD_CLEAR_NEVER) {
3034 window_copy_clear_marks(wme);
3035 data->searchx = data->searchy = -1;
3037 if (action == WINDOW_COPY_CMD_NOTHING)
3038 action = WINDOW_COPY_CMD_REDRAW;
3040 wme->prefix = 1;
3042 if (action == WINDOW_COPY_CMD_CANCEL)
3043 window_pane_reset_mode(wme->wp);
3044 else if (action == WINDOW_COPY_CMD_REDRAW)
3045 window_copy_redraw_screen(wme);
3048 static void
3049 window_copy_scroll_to(struct window_mode_entry *wme, u_int px, u_int py,
3050 int no_redraw)
3052 struct window_copy_mode_data *data = wme->data;
3053 struct grid *gd = data->backing->grid;
3054 u_int offset, gap;
3056 data->cx = px;
3058 if (py >= gd->hsize - data->oy && py < gd->hsize - data->oy + gd->sy)
3059 data->cy = py - (gd->hsize - data->oy);
3060 else {
3061 gap = gd->sy / 4;
3062 if (py < gd->sy) {
3063 offset = 0;
3064 data->cy = py;
3065 } else if (py > gd->hsize + gd->sy - gap) {
3066 offset = gd->hsize;
3067 data->cy = py - gd->hsize;
3068 } else {
3069 offset = py + gap - gd->sy;
3070 data->cy = py - offset;
3072 data->oy = gd->hsize - offset;
3075 if (!no_redraw && data->searchmark != NULL && !data->timeout)
3076 window_copy_search_marks(wme, NULL, data->searchregex, 1);
3077 window_copy_update_selection(wme, 1, 0);
3078 if (!no_redraw)
3079 window_copy_redraw_screen(wme);
3082 static int
3083 window_copy_search_compare(struct grid *gd, u_int px, u_int py,
3084 struct grid *sgd, u_int spx, int cis)
3086 struct grid_cell gc, sgc;
3087 const struct utf8_data *ud, *sud;
3089 grid_get_cell(gd, px, py, &gc);
3090 ud = &gc.data;
3091 grid_get_cell(sgd, spx, 0, &sgc);
3092 sud = &sgc.data;
3094 if (ud->size != sud->size || ud->width != sud->width)
3095 return (0);
3097 if (cis && ud->size == 1)
3098 return (tolower(ud->data[0]) == sud->data[0]);
3100 return (memcmp(ud->data, sud->data, ud->size) == 0);
3103 static int
3104 window_copy_search_lr(struct grid *gd, struct grid *sgd, u_int *ppx, u_int py,
3105 u_int first, u_int last, int cis)
3107 u_int ax, bx, px, pywrap, endline;
3108 int matched;
3109 struct grid_line *gl;
3111 endline = gd->hsize + gd->sy - 1;
3112 for (ax = first; ax < last; ax++) {
3113 for (bx = 0; bx < sgd->sx; bx++) {
3114 px = ax + bx;
3115 pywrap = py;
3116 /* Wrap line. */
3117 while (px >= gd->sx && pywrap < endline) {
3118 gl = grid_get_line(gd, pywrap);
3119 if (~gl->flags & GRID_LINE_WRAPPED)
3120 break;
3121 px -= gd->sx;
3122 pywrap++;
3124 /* We have run off the end of the grid. */
3125 if (px >= gd->sx)
3126 break;
3127 matched = window_copy_search_compare(gd, px, pywrap,
3128 sgd, bx, cis);
3129 if (!matched)
3130 break;
3132 if (bx == sgd->sx) {
3133 *ppx = ax;
3134 return (1);
3137 return (0);
3140 static int
3141 window_copy_search_rl(struct grid *gd,
3142 struct grid *sgd, u_int *ppx, u_int py, u_int first, u_int last, int cis)
3144 u_int ax, bx, px, pywrap, endline;
3145 int matched;
3146 struct grid_line *gl;
3148 endline = gd->hsize + gd->sy - 1;
3149 for (ax = last; ax > first; ax--) {
3150 for (bx = 0; bx < sgd->sx; bx++) {
3151 px = ax - 1 + bx;
3152 pywrap = py;
3153 /* Wrap line. */
3154 while (px >= gd->sx && pywrap < endline) {
3155 gl = grid_get_line(gd, pywrap);
3156 if (~gl->flags & GRID_LINE_WRAPPED)
3157 break;
3158 px -= gd->sx;
3159 pywrap++;
3161 /* We have run off the end of the grid. */
3162 if (px >= gd->sx)
3163 break;
3164 matched = window_copy_search_compare(gd, px, pywrap,
3165 sgd, bx, cis);
3166 if (!matched)
3167 break;
3169 if (bx == sgd->sx) {
3170 *ppx = ax - 1;
3171 return (1);
3174 return (0);
3177 static int
3178 window_copy_search_lr_regex(struct grid *gd, u_int *ppx, u_int *psx, u_int py,
3179 u_int first, u_int last, regex_t *reg)
3181 int eflags = 0;
3182 u_int endline, foundx, foundy, len, pywrap, size = 1;
3183 char *buf;
3184 regmatch_t regmatch;
3185 struct grid_line *gl;
3188 * This can happen during search if the last match was the last
3189 * character on a line.
3191 if (first >= last)
3192 return (0);
3194 /* Set flags for regex search. */
3195 if (first != 0)
3196 eflags |= REG_NOTBOL;
3198 /* Need to look at the entire string. */
3199 buf = xmalloc(size);
3200 buf[0] = '\0';
3201 buf = window_copy_stringify(gd, py, first, gd->sx, buf, &size);
3202 len = gd->sx - first;
3203 endline = gd->hsize + gd->sy - 1;
3204 pywrap = py;
3205 while (buf != NULL && pywrap <= endline) {
3206 gl = grid_get_line(gd, pywrap);
3207 if (~gl->flags & GRID_LINE_WRAPPED)
3208 break;
3209 pywrap++;
3210 buf = window_copy_stringify(gd, pywrap, 0, gd->sx, buf, &size);
3211 len += gd->sx;
3214 if (regexec(reg, buf, 1, &regmatch, eflags) == 0 &&
3215 regmatch.rm_so != regmatch.rm_eo) {
3216 foundx = first;
3217 foundy = py;
3218 window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
3219 buf + regmatch.rm_so);
3220 if (foundy == py && foundx < last) {
3221 *ppx = foundx;
3222 len -= foundx - first;
3223 window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
3224 buf + regmatch.rm_eo);
3225 *psx = foundx;
3226 while (foundy > py) {
3227 *psx += gd->sx;
3228 foundy--;
3230 *psx -= *ppx;
3231 free(buf);
3232 return (1);
3236 free(buf);
3237 *ppx = 0;
3238 *psx = 0;
3239 return (0);
3242 static int
3243 window_copy_search_rl_regex(struct grid *gd, u_int *ppx, u_int *psx, u_int py,
3244 u_int first, u_int last, regex_t *reg)
3246 int eflags = 0;
3247 u_int endline, len, pywrap, size = 1;
3248 char *buf;
3249 struct grid_line *gl;
3251 /* Set flags for regex search. */
3252 if (first != 0)
3253 eflags |= REG_NOTBOL;
3255 /* Need to look at the entire string. */
3256 buf = xmalloc(size);
3257 buf[0] = '\0';
3258 buf = window_copy_stringify(gd, py, first, gd->sx, buf, &size);
3259 len = gd->sx - first;
3260 endline = gd->hsize + gd->sy - 1;
3261 pywrap = py;
3262 while (buf != NULL && (pywrap <= endline)) {
3263 gl = grid_get_line(gd, pywrap);
3264 if (~gl->flags & GRID_LINE_WRAPPED)
3265 break;
3266 pywrap++;
3267 buf = window_copy_stringify(gd, pywrap, 0, gd->sx, buf, &size);
3268 len += gd->sx;
3271 if (window_copy_last_regex(gd, py, first, last, len, ppx, psx, buf,
3272 reg, eflags))
3274 free(buf);
3275 return (1);
3278 free(buf);
3279 *ppx = 0;
3280 *psx = 0;
3281 return (0);
3284 static const char *
3285 window_copy_cellstring(const struct grid_line *gl, u_int px, size_t *size,
3286 int *allocated)
3288 static struct utf8_data ud;
3289 struct grid_cell_entry *gce;
3290 char *copy;
3292 if (px >= gl->cellsize) {
3293 *size = 1;
3294 *allocated = 0;
3295 return (" ");
3298 gce = &gl->celldata[px];
3299 if (gce->flags & GRID_FLAG_PADDING) {
3300 *size = 0;
3301 *allocated = 0;
3302 return (NULL);
3304 if (~gce->flags & GRID_FLAG_EXTENDED) {
3305 *size = 1;
3306 *allocated = 0;
3307 return (&gce->data.data);
3310 utf8_to_data(gl->extddata[gce->offset].data, &ud);
3311 if (ud.size == 0) {
3312 *size = 0;
3313 *allocated = 0;
3314 return (NULL);
3316 *size = ud.size;
3317 *allocated = 1;
3319 copy = xmalloc(ud.size);
3320 memcpy(copy, ud.data, ud.size);
3321 return (copy);
3324 /* Find last match in given range. */
3325 static int
3326 window_copy_last_regex(struct grid *gd, u_int py, u_int first, u_int last,
3327 u_int len, u_int *ppx, u_int *psx, const char *buf, const regex_t *preg,
3328 int eflags)
3330 u_int foundx, foundy, oldx, px = 0, savepx, savesx = 0;
3331 regmatch_t regmatch;
3333 foundx = first;
3334 foundy = py;
3335 oldx = first;
3336 while (regexec(preg, buf + px, 1, &regmatch, eflags) == 0) {
3337 if (regmatch.rm_so == regmatch.rm_eo)
3338 break;
3339 window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
3340 buf + px + regmatch.rm_so);
3341 if (foundy > py || foundx >= last)
3342 break;
3343 len -= foundx - oldx;
3344 savepx = foundx;
3345 window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
3346 buf + px + regmatch.rm_eo);
3347 if (foundy > py || foundx >= last) {
3348 *ppx = savepx;
3349 *psx = foundx;
3350 while (foundy > py) {
3351 *psx += gd->sx;
3352 foundy--;
3354 *psx -= *ppx;
3355 return (1);
3356 } else {
3357 savesx = foundx - savepx;
3358 len -= savesx;
3359 oldx = foundx;
3361 px += regmatch.rm_eo;
3364 if (savesx > 0) {
3365 *ppx = savepx;
3366 *psx = savesx;
3367 return (1);
3368 } else {
3369 *ppx = 0;
3370 *psx = 0;
3371 return (0);
3375 /* Stringify line and append to input buffer. Caller frees. */
3376 static char *
3377 window_copy_stringify(struct grid *gd, u_int py, u_int first, u_int last,
3378 char *buf, u_int *size)
3380 u_int ax, bx, newsize = *size;
3381 const struct grid_line *gl;
3382 const char *d;
3383 size_t bufsize = 1024, dlen;
3384 int allocated;
3386 while (bufsize < newsize)
3387 bufsize *= 2;
3388 buf = xrealloc(buf, bufsize);
3390 gl = grid_peek_line(gd, py);
3391 bx = *size - 1;
3392 for (ax = first; ax < last; ax++) {
3393 d = window_copy_cellstring(gl, ax, &dlen, &allocated);
3394 newsize += dlen;
3395 while (bufsize < newsize) {
3396 bufsize *= 2;
3397 buf = xrealloc(buf, bufsize);
3399 if (dlen == 1)
3400 buf[bx++] = *d;
3401 else {
3402 memcpy(buf + bx, d, dlen);
3403 bx += dlen;
3405 if (allocated)
3406 free((void *)d);
3408 buf[newsize - 1] = '\0';
3410 *size = newsize;
3411 return (buf);
3414 /* Map start of C string containing UTF-8 data to grid cell position. */
3415 static void
3416 window_copy_cstrtocellpos(struct grid *gd, u_int ncells, u_int *ppx, u_int *ppy,
3417 const char *str)
3419 u_int cell, ccell, px, pywrap, pos, len;
3420 int match;
3421 const struct grid_line *gl;
3422 const char *d;
3423 size_t dlen;
3424 struct {
3425 const char *d;
3426 size_t dlen;
3427 int allocated;
3428 } *cells;
3430 /* Populate the array of cell data. */
3431 cells = xreallocarray(NULL, ncells, sizeof cells[0]);
3432 cell = 0;
3433 px = *ppx;
3434 pywrap = *ppy;
3435 gl = grid_peek_line(gd, pywrap);
3436 while (cell < ncells) {
3437 cells[cell].d = window_copy_cellstring(gl, px,
3438 &cells[cell].dlen, &cells[cell].allocated);
3439 cell++;
3440 px++;
3441 if (px == gd->sx) {
3442 px = 0;
3443 pywrap++;
3444 gl = grid_peek_line(gd, pywrap);
3448 /* Locate starting cell. */
3449 cell = 0;
3450 len = strlen(str);
3451 while (cell < ncells) {
3452 ccell = cell;
3453 pos = 0;
3454 match = 1;
3455 while (ccell < ncells) {
3456 if (str[pos] == '\0') {
3457 match = 0;
3458 break;
3460 d = cells[ccell].d;
3461 dlen = cells[ccell].dlen;
3462 if (dlen == 1) {
3463 if (str[pos] != *d) {
3464 match = 0;
3465 break;
3467 pos++;
3468 } else {
3469 if (dlen > len - pos)
3470 dlen = len - pos;
3471 if (memcmp(str + pos, d, dlen) != 0) {
3472 match = 0;
3473 break;
3475 pos += dlen;
3477 ccell++;
3479 if (match)
3480 break;
3481 cell++;
3484 /* If not found this will be one past the end. */
3485 px = *ppx + cell;
3486 pywrap = *ppy;
3487 while (px >= gd->sx) {
3488 px -= gd->sx;
3489 pywrap++;
3492 *ppx = px;
3493 *ppy = pywrap;
3495 /* Free cell data. */
3496 for (cell = 0; cell < ncells; cell++) {
3497 if (cells[cell].allocated)
3498 free((void *)cells[cell].d);
3500 free(cells);
3503 static void
3504 window_copy_move_left(struct screen *s, u_int *fx, u_int *fy, int wrapflag)
3506 if (*fx == 0) { /* left */
3507 if (*fy == 0) { /* top */
3508 if (wrapflag) {
3509 *fx = screen_size_x(s) - 1;
3510 *fy = screen_hsize(s) + screen_size_y(s) - 1;
3512 return;
3514 *fx = screen_size_x(s) - 1;
3515 *fy = *fy - 1;
3516 } else
3517 *fx = *fx - 1;
3520 static void
3521 window_copy_move_right(struct screen *s, u_int *fx, u_int *fy, int wrapflag)
3523 if (*fx == screen_size_x(s) - 1) { /* right */
3524 if (*fy == screen_hsize(s) + screen_size_y(s) - 1) { /* bottom */
3525 if (wrapflag) {
3526 *fx = 0;
3527 *fy = 0;
3529 return;
3531 *fx = 0;
3532 *fy = *fy + 1;
3533 } else
3534 *fx = *fx + 1;
3537 static int
3538 window_copy_is_lowercase(const char *ptr)
3540 while (*ptr != '\0') {
3541 if (*ptr != tolower((u_char)*ptr))
3542 return (0);
3543 ++ptr;
3545 return (1);
3549 * Handle backward wrapped regex searches with overlapping matches. In this case
3550 * find the longest overlapping match from previous wrapped lines.
3552 static void
3553 window_copy_search_back_overlap(struct grid *gd, regex_t *preg, u_int *ppx,
3554 u_int *psx, u_int *ppy, u_int endline)
3556 u_int endx, endy, oldendx, oldendy, px, py, sx;
3557 int found = 1;
3559 oldendx = *ppx + *psx;
3560 oldendy = *ppy - 1;
3561 while (oldendx > gd->sx - 1) {
3562 oldendx -= gd->sx;
3563 oldendy++;
3565 endx = oldendx;
3566 endy = oldendy;
3567 px = *ppx;
3568 py = *ppy;
3569 while (found && px == 0 && py - 1 > endline &&
3570 grid_get_line(gd, py - 2)->flags & GRID_LINE_WRAPPED &&
3571 endx == oldendx && endy == oldendy) {
3572 py--;
3573 found = window_copy_search_rl_regex(gd, &px, &sx, py - 1, 0,
3574 gd->sx, preg);
3575 if (found) {
3576 endx = px + sx;
3577 endy = py - 1;
3578 while (endx > gd->sx - 1) {
3579 endx -= gd->sx;
3580 endy++;
3582 if (endx == oldendx && endy == oldendy) {
3583 *ppx = px;
3584 *ppy = py;
3591 * Search for text stored in sgd starting from position fx,fy up to endline. If
3592 * found, jump to it. If cis then ignore case. The direction is 0 for searching
3593 * up, down otherwise. If wrap then go to begin/end of grid and try again if
3594 * not found.
3596 static int
3597 window_copy_search_jump(struct window_mode_entry *wme, struct grid *gd,
3598 struct grid *sgd, u_int fx, u_int fy, u_int endline, int cis, int wrap,
3599 int direction, int regex)
3601 u_int i, px, sx, ssize = 1;
3602 int found = 0, cflags = REG_EXTENDED;
3603 char *sbuf;
3604 regex_t reg;
3606 if (regex) {
3607 sbuf = xmalloc(ssize);
3608 sbuf[0] = '\0';
3609 sbuf = window_copy_stringify(sgd, 0, 0, sgd->sx, sbuf, &ssize);
3610 if (cis)
3611 cflags |= REG_ICASE;
3612 if (regcomp(&reg, sbuf, cflags) != 0) {
3613 free(sbuf);
3614 return (0);
3616 free(sbuf);
3619 if (direction) {
3620 for (i = fy; i <= endline; i++) {
3621 if (regex) {
3622 found = window_copy_search_lr_regex(gd,
3623 &px, &sx, i, fx, gd->sx, &reg);
3624 } else {
3625 found = window_copy_search_lr(gd, sgd,
3626 &px, i, fx, gd->sx, cis);
3628 if (found)
3629 break;
3630 fx = 0;
3632 } else {
3633 for (i = fy + 1; endline < i; i--) {
3634 if (regex) {
3635 found = window_copy_search_rl_regex(gd,
3636 &px, &sx, i - 1, 0, fx + 1, &reg);
3637 if (found) {
3638 window_copy_search_back_overlap(gd,
3639 &reg, &px, &sx, &i, endline);
3641 } else {
3642 found = window_copy_search_rl(gd, sgd,
3643 &px, i - 1, 0, fx + 1, cis);
3645 if (found) {
3646 i--;
3647 break;
3649 fx = gd->sx - 1;
3652 if (regex)
3653 regfree(&reg);
3655 if (found) {
3656 window_copy_scroll_to(wme, px, i, 1);
3657 return (1);
3659 if (wrap) {
3660 return (window_copy_search_jump(wme, gd, sgd,
3661 direction ? 0 : gd->sx - 1,
3662 direction ? 0 : gd->hsize + gd->sy - 1, fy, cis, 0,
3663 direction, regex));
3665 return (0);
3668 static void
3669 window_copy_move_after_search_mark(struct window_copy_mode_data *data,
3670 u_int *fx, u_int *fy, int wrapflag)
3672 struct screen *s = data->backing;
3673 u_int at, start;
3675 if (window_copy_search_mark_at(data, *fx, *fy, &start) == 0 &&
3676 data->searchmark[start] != 0) {
3677 while (window_copy_search_mark_at(data, *fx, *fy, &at) == 0) {
3678 if (data->searchmark[at] != data->searchmark[start])
3679 break;
3680 /* Stop if not wrapping and at the end of the grid. */
3681 if (!wrapflag &&
3682 *fx == screen_size_x(s) - 1 &&
3683 *fy == screen_hsize(s) + screen_size_y(s) - 1)
3684 break;
3686 window_copy_move_right(s, fx, fy, wrapflag);
3692 * Search in for text searchstr. If direction is 0 then search up, otherwise
3693 * down.
3695 static int
3696 window_copy_search(struct window_mode_entry *wme, int direction, int regex)
3698 struct window_pane *wp = wme->wp;
3699 struct window_copy_mode_data *data = wme->data;
3700 struct screen *s = data->backing, ss;
3701 struct screen_write_ctx ctx;
3702 struct grid *gd = s->grid;
3703 const char *str = data->searchstr;
3704 u_int at, endline, fx, fy, start;
3705 int cis, found, keys, visible_only;
3706 int wrapflag;
3708 if (regex && str[strcspn(str, "^$*+()?[].\\")] == '\0')
3709 regex = 0;
3711 data->searchdirection = direction;
3713 if (data->timeout)
3714 return (0);
3716 if (data->searchall || wp->searchstr == NULL ||
3717 wp->searchregex != regex) {
3718 visible_only = 0;
3719 data->searchall = 0;
3720 } else
3721 visible_only = (strcmp(wp->searchstr, str) == 0);
3722 if (visible_only == 0 && data->searchmark != NULL)
3723 window_copy_clear_marks(wme);
3724 free(wp->searchstr);
3725 wp->searchstr = xstrdup(str);
3726 wp->searchregex = regex;
3728 fx = data->cx;
3729 fy = screen_hsize(data->backing) - data->oy + data->cy;
3731 screen_init(&ss, screen_write_strlen("%s", str), 1, 0);
3732 screen_write_start(&ctx, &ss);
3733 screen_write_nputs(&ctx, -1, &grid_default_cell, "%s", str);
3734 screen_write_stop(&ctx);
3736 wrapflag = options_get_number(wp->window->options, "wrap-search");
3737 cis = window_copy_is_lowercase(str);
3739 keys = options_get_number(wp->window->options, "mode-keys");
3741 if (direction) {
3743 * Behave according to mode-keys. If it is emacs, search forward
3744 * leaves the cursor after the match. If it is vi, the cursor
3745 * remains at the beginning of the match, regardless of
3746 * direction, which means that we need to start the next search
3747 * after the term the cursor is currently on when searching
3748 * forward.
3750 if (keys == MODEKEY_VI) {
3751 if (data->searchmark != NULL)
3752 window_copy_move_after_search_mark(data, &fx,
3753 &fy, wrapflag);
3754 else {
3756 * When there are no search marks, start the
3757 * search after the current cursor position.
3759 window_copy_move_right(s, &fx, &fy, wrapflag);
3762 endline = gd->hsize + gd->sy - 1;
3764 else {
3765 window_copy_move_left(s, &fx, &fy, wrapflag);
3766 endline = 0;
3769 found = window_copy_search_jump(wme, gd, ss.grid, fx, fy, endline, cis,
3770 wrapflag, direction, regex);
3771 if (found) {
3772 window_copy_search_marks(wme, &ss, regex, visible_only);
3773 fx = data->cx;
3774 fy = screen_hsize(data->backing) - data->oy + data->cy;
3777 * When searching forward, if the cursor is not at the beginning
3778 * of the mark, search again.
3780 if (direction &&
3781 window_copy_search_mark_at(data, fx, fy, &at) == 0 &&
3782 at > 0 &&
3783 data->searchmark != NULL &&
3784 data->searchmark[at] == data->searchmark[at - 1]) {
3785 window_copy_move_after_search_mark(data, &fx, &fy,
3786 wrapflag);
3787 window_copy_search_jump(wme, gd, ss.grid, fx,
3788 fy, endline, cis, wrapflag, direction,
3789 regex);
3790 fx = data->cx;
3791 fy = screen_hsize(data->backing) - data->oy + data->cy;
3794 if (direction) {
3796 * When in Emacs mode, position the cursor just after
3797 * the mark.
3799 if (keys == MODEKEY_EMACS) {
3800 window_copy_move_after_search_mark(data, &fx,
3801 &fy, wrapflag);
3802 data->cx = fx;
3803 data->cy = fy - screen_hsize(data->backing) +
3804 data-> oy;
3807 else {
3809 * When searching backward, position the cursor at the
3810 * beginning of the mark.
3812 if (window_copy_search_mark_at(data, fx, fy,
3813 &start) == 0) {
3814 while (window_copy_search_mark_at(data, fx, fy,
3815 &at) == 0 &&
3816 data->searchmark != NULL &&
3817 data->searchmark[at] ==
3818 data->searchmark[start]) {
3819 data->cx = fx;
3820 data->cy = fy -
3821 screen_hsize(data->backing) +
3822 data-> oy;
3823 if (at == 0)
3824 break;
3826 window_copy_move_left(s, &fx, &fy, 0);
3831 window_copy_redraw_screen(wme);
3833 screen_free(&ss);
3834 return (found);
3837 static void
3838 window_copy_visible_lines(struct window_copy_mode_data *data, u_int *start,
3839 u_int *end)
3841 struct grid *gd = data->backing->grid;
3842 const struct grid_line *gl;
3844 for (*start = gd->hsize - data->oy; *start > 0; (*start)--) {
3845 gl = grid_peek_line(gd, (*start) - 1);
3846 if (~gl->flags & GRID_LINE_WRAPPED)
3847 break;
3849 *end = gd->hsize - data->oy + gd->sy;
3852 static int
3853 window_copy_search_mark_at(struct window_copy_mode_data *data, u_int px,
3854 u_int py, u_int *at)
3856 struct screen *s = data->backing;
3857 struct grid *gd = s->grid;
3859 if (py < gd->hsize - data->oy)
3860 return (-1);
3861 if (py > gd->hsize - data->oy + gd->sy - 1)
3862 return (-1);
3863 *at = ((py - (gd->hsize - data->oy)) * gd->sx) + px;
3864 return (0);
3867 static int
3868 window_copy_search_marks(struct window_mode_entry *wme, struct screen *ssp,
3869 int regex, int visible_only)
3871 struct window_copy_mode_data *data = wme->data;
3872 struct screen *s = data->backing, ss;
3873 struct screen_write_ctx ctx;
3874 struct grid *gd = s->grid;
3875 int found, cis, stopped = 0;
3876 int cflags = REG_EXTENDED;
3877 u_int px, py, i, b, nfound = 0, width;
3878 u_int ssize = 1, start, end;
3879 char *sbuf;
3880 regex_t reg;
3881 uint64_t stop = 0, tstart, t;
3883 if (ssp == NULL) {
3884 width = screen_write_strlen("%s", data->searchstr);
3885 screen_init(&ss, width, 1, 0);
3886 screen_write_start(&ctx, &ss);
3887 screen_write_nputs(&ctx, -1, &grid_default_cell, "%s",
3888 data->searchstr);
3889 screen_write_stop(&ctx);
3890 ssp = &ss;
3891 } else
3892 width = screen_size_x(ssp);
3894 cis = window_copy_is_lowercase(data->searchstr);
3896 if (regex) {
3897 sbuf = xmalloc(ssize);
3898 sbuf[0] = '\0';
3899 sbuf = window_copy_stringify(ssp->grid, 0, 0, ssp->grid->sx,
3900 sbuf, &ssize);
3901 if (cis)
3902 cflags |= REG_ICASE;
3903 if (regcomp(&reg, sbuf, cflags) != 0) {
3904 free(sbuf);
3905 return (0);
3907 free(sbuf);
3909 tstart = get_timer();
3911 if (visible_only)
3912 window_copy_visible_lines(data, &start, &end);
3913 else {
3914 start = 0;
3915 end = gd->hsize + gd->sy;
3916 stop = get_timer() + WINDOW_COPY_SEARCH_ALL_TIMEOUT;
3919 again:
3920 free(data->searchmark);
3921 data->searchmark = xcalloc(gd->sx, gd->sy);
3922 data->searchgen = 1;
3924 for (py = start; py < end; py++) {
3925 px = 0;
3926 for (;;) {
3927 if (regex) {
3928 found = window_copy_search_lr_regex(gd,
3929 &px, &width, py, px, gd->sx, &reg);
3930 if (!found)
3931 break;
3932 } else {
3933 found = window_copy_search_lr(gd, ssp->grid,
3934 &px, py, px, gd->sx, cis);
3935 if (!found)
3936 break;
3938 nfound++;
3940 if (window_copy_search_mark_at(data, px, py, &b) == 0) {
3941 if (b + width > gd->sx * gd->sy)
3942 width = (gd->sx * gd->sy) - b;
3943 for (i = b; i < b + width; i++) {
3944 if (data->searchmark[i] != 0)
3945 continue;
3946 data->searchmark[i] = data->searchgen;
3948 if (data->searchgen == UCHAR_MAX)
3949 data->searchgen = 1;
3950 else
3951 data->searchgen++;
3953 px += width;
3956 t = get_timer();
3957 if (t - tstart > WINDOW_COPY_SEARCH_TIMEOUT) {
3958 data->timeout = 1;
3959 break;
3961 if (stop != 0 && t > stop) {
3962 stopped = 1;
3963 break;
3966 if (data->timeout) {
3967 window_copy_clear_marks(wme);
3968 goto out;
3971 if (stopped && stop != 0) {
3972 /* Try again but just the visible context. */
3973 window_copy_visible_lines(data, &start, &end);
3974 stop = 0;
3975 goto again;
3978 if (!visible_only) {
3979 if (stopped) {
3980 if (nfound > 1000)
3981 data->searchcount = 1000;
3982 else if (nfound > 100)
3983 data->searchcount = 100;
3984 else if (nfound > 10)
3985 data->searchcount = 10;
3986 else
3987 data->searchcount = -1;
3988 data->searchmore = 1;
3989 } else {
3990 data->searchcount = nfound;
3991 data->searchmore = 0;
3995 out:
3996 if (ssp == &ss)
3997 screen_free(&ss);
3998 if (regex)
3999 regfree(&reg);
4000 return (1);
4003 static void
4004 window_copy_clear_marks(struct window_mode_entry *wme)
4006 struct window_copy_mode_data *data = wme->data;
4008 free(data->searchmark);
4009 data->searchmark = NULL;
4012 static int
4013 window_copy_search_up(struct window_mode_entry *wme, int regex)
4015 return (window_copy_search(wme, 0, regex));
4018 static int
4019 window_copy_search_down(struct window_mode_entry *wme, int regex)
4021 return (window_copy_search(wme, 1, regex));
4024 static void
4025 window_copy_goto_line(struct window_mode_entry *wme, const char *linestr)
4027 struct window_copy_mode_data *data = wme->data;
4028 const char *errstr;
4029 int lineno;
4031 lineno = strtonum(linestr, -1, INT_MAX, &errstr);
4032 if (errstr != NULL)
4033 return;
4034 if (lineno < 0 || (u_int)lineno > screen_hsize(data->backing))
4035 lineno = screen_hsize(data->backing);
4037 data->oy = lineno;
4038 window_copy_update_selection(wme, 1, 0);
4039 window_copy_redraw_screen(wme);
4042 static void
4043 window_copy_match_start_end(struct window_copy_mode_data *data, u_int at,
4044 u_int *start, u_int *end)
4046 struct grid *gd = data->backing->grid;
4047 u_int last = (gd->sy * gd->sx) - 1;
4048 u_char mark = data->searchmark[at];
4050 *start = *end = at;
4051 while (*start != 0 && data->searchmark[*start] == mark)
4052 (*start)--;
4053 if (data->searchmark[*start] != mark)
4054 (*start)++;
4055 while (*end != last && data->searchmark[*end] == mark)
4056 (*end)++;
4057 if (data->searchmark[*end] != mark)
4058 (*end)--;
4061 static char *
4062 window_copy_match_at_cursor(struct window_copy_mode_data *data)
4064 struct grid *gd = data->backing->grid;
4065 struct grid_cell gc;
4066 u_int at, start, end, cy, px, py;
4067 u_int sx = screen_size_x(data->backing);
4068 char *buf = NULL;
4069 size_t len = 0;
4071 if (data->searchmark == NULL)
4072 return (NULL);
4074 cy = screen_hsize(data->backing) - data->oy + data->cy;
4075 if (window_copy_search_mark_at(data, data->cx, cy, &at) != 0)
4076 return (NULL);
4077 if (data->searchmark[at] == 0) {
4078 /* Allow one position after the match. */
4079 if (at == 0 || data->searchmark[--at] == 0)
4080 return (NULL);
4082 window_copy_match_start_end(data, at, &start, &end);
4085 * Cells will not be set in the marked array unless they are valid text
4086 * and wrapping will be taken care of, so we can just copy.
4088 for (at = start; at <= end; at++) {
4089 py = at / sx;
4090 px = at - (py * sx);
4092 grid_get_cell(gd, px, gd->hsize + py - data->oy, &gc);
4093 buf = xrealloc(buf, len + gc.data.size + 1);
4094 memcpy(buf + len, gc.data.data, gc.data.size);
4095 len += gc.data.size;
4097 if (len != 0)
4098 buf[len] = '\0';
4099 return (buf);
4102 static void
4103 window_copy_update_style(struct window_mode_entry *wme, u_int fx, u_int fy,
4104 struct grid_cell *gc, const struct grid_cell *mgc,
4105 const struct grid_cell *cgc, const struct grid_cell *mkgc)
4107 struct window_pane *wp = wme->wp;
4108 struct window_copy_mode_data *data = wme->data;
4109 u_int mark, start, end, cy, cursor, current;
4110 int inv = 0, found = 0;
4111 int keys;
4113 if (data->showmark && fy == data->my) {
4114 gc->attr = mkgc->attr;
4115 if (fx == data->mx)
4116 inv = 1;
4117 if (inv) {
4118 gc->fg = mkgc->bg;
4119 gc->bg = mkgc->fg;
4121 else {
4122 gc->fg = mkgc->fg;
4123 gc->bg = mkgc->bg;
4127 if (data->searchmark == NULL)
4128 return;
4130 if (window_copy_search_mark_at(data, fx, fy, &current) != 0)
4131 return;
4132 mark = data->searchmark[current];
4133 if (mark == 0)
4134 return;
4136 cy = screen_hsize(data->backing) - data->oy + data->cy;
4137 if (window_copy_search_mark_at(data, data->cx, cy, &cursor) == 0) {
4138 keys = options_get_number(wp->window->options, "mode-keys");
4139 if (cursor != 0 &&
4140 keys == MODEKEY_EMACS &&
4141 data->searchdirection) {
4142 if (data->searchmark[cursor - 1] == mark) {
4143 cursor--;
4144 found = 1;
4146 } else if (data->searchmark[cursor] == mark)
4147 found = 1;
4148 if (found) {
4149 window_copy_match_start_end(data, cursor, &start, &end);
4150 if (current >= start && current <= end) {
4151 gc->attr = cgc->attr;
4152 if (inv) {
4153 gc->fg = cgc->bg;
4154 gc->bg = cgc->fg;
4156 else {
4157 gc->fg = cgc->fg;
4158 gc->bg = cgc->bg;
4160 return;
4165 gc->attr = mgc->attr;
4166 if (inv) {
4167 gc->fg = mgc->bg;
4168 gc->bg = mgc->fg;
4170 else {
4171 gc->fg = mgc->fg;
4172 gc->bg = mgc->bg;
4176 static void
4177 window_copy_write_one(struct window_mode_entry *wme,
4178 struct screen_write_ctx *ctx, u_int py, u_int fy, u_int nx,
4179 const struct grid_cell *mgc, const struct grid_cell *cgc,
4180 const struct grid_cell *mkgc)
4182 struct window_copy_mode_data *data = wme->data;
4183 struct grid *gd = data->backing->grid;
4184 struct grid_cell gc;
4185 u_int fx;
4187 screen_write_cursormove(ctx, 0, py, 0);
4188 for (fx = 0; fx < nx; fx++) {
4189 grid_get_cell(gd, fx, fy, &gc);
4190 if (fx + gc.data.width <= nx) {
4191 window_copy_update_style(wme, fx, fy, &gc, mgc, cgc,
4192 mkgc);
4193 screen_write_cell(ctx, &gc);
4198 static void
4199 window_copy_write_line(struct window_mode_entry *wme,
4200 struct screen_write_ctx *ctx, u_int py)
4202 struct window_pane *wp = wme->wp;
4203 struct window_copy_mode_data *data = wme->data;
4204 struct screen *s = &data->screen;
4205 struct options *oo = wp->window->options;
4206 struct grid_line *gl;
4207 struct grid_cell gc, mgc, cgc, mkgc;
4208 char hdr[512], tmp[256], *t;
4209 size_t size = 0;
4210 u_int hsize = screen_hsize(data->backing);
4212 style_apply(&gc, oo, "mode-style", NULL);
4213 gc.flags |= GRID_FLAG_NOPALETTE;
4214 style_apply(&mgc, oo, "copy-mode-match-style", NULL);
4215 mgc.flags |= GRID_FLAG_NOPALETTE;
4216 style_apply(&cgc, oo, "copy-mode-current-match-style", NULL);
4217 cgc.flags |= GRID_FLAG_NOPALETTE;
4218 style_apply(&mkgc, oo, "copy-mode-mark-style", NULL);
4219 mkgc.flags |= GRID_FLAG_NOPALETTE;
4221 if (py == 0 && s->rupper < s->rlower && !data->hide_position) {
4222 gl = grid_get_line(data->backing->grid, hsize - data->oy);
4223 if (gl->time == 0)
4224 xsnprintf(tmp, sizeof tmp, "[%u/%u]", data->oy, hsize);
4225 else {
4226 t = format_pretty_time(gl->time, 1);
4227 xsnprintf(tmp, sizeof tmp, "%s [%u/%u]", t, data->oy,
4228 hsize);
4229 free(t);
4232 if (data->searchmark == NULL) {
4233 if (data->timeout) {
4234 size = xsnprintf(hdr, sizeof hdr,
4235 "(timed out) %s", tmp);
4236 } else
4237 size = xsnprintf(hdr, sizeof hdr, "%s", tmp);
4238 } else {
4239 if (data->searchcount == -1)
4240 size = xsnprintf(hdr, sizeof hdr, "%s", tmp);
4241 else {
4242 size = xsnprintf(hdr, sizeof hdr,
4243 "(%d%s results) %s", data->searchcount,
4244 data->searchmore ? "+" : "", tmp);
4247 if (size > screen_size_x(s))
4248 size = screen_size_x(s);
4249 screen_write_cursormove(ctx, screen_size_x(s) - size, 0, 0);
4250 screen_write_puts(ctx, &gc, "%s", hdr);
4251 } else
4252 size = 0;
4254 if (size < screen_size_x(s)) {
4255 window_copy_write_one(wme, ctx, py, hsize - data->oy + py,
4256 screen_size_x(s) - size, &mgc, &cgc, &mkgc);
4259 if (py == data->cy && data->cx == screen_size_x(s)) {
4260 screen_write_cursormove(ctx, screen_size_x(s) - 1, py, 0);
4261 screen_write_putc(ctx, &grid_default_cell, '$');
4265 static void
4266 window_copy_write_lines(struct window_mode_entry *wme,
4267 struct screen_write_ctx *ctx, u_int py, u_int ny)
4269 u_int yy;
4271 for (yy = py; yy < py + ny; yy++)
4272 window_copy_write_line(wme, ctx, py);
4275 static void
4276 window_copy_redraw_selection(struct window_mode_entry *wme, u_int old_y)
4278 struct window_copy_mode_data *data = wme->data;
4279 struct grid *gd = data->backing->grid;
4280 u_int new_y, start, end;
4282 new_y = data->cy;
4283 if (old_y <= new_y) {
4284 start = old_y;
4285 end = new_y;
4286 } else {
4287 start = new_y;
4288 end = old_y;
4292 * In word selection mode the first word on the line below the cursor
4293 * might be selected, so add this line to the redraw area.
4295 if (data->selflag == SEL_WORD) {
4296 /* Last grid line in data coordinates. */
4297 if (end < gd->sy + data->oy - 1)
4298 end++;
4300 window_copy_redraw_lines(wme, start, end - start + 1);
4303 static void
4304 window_copy_redraw_lines(struct window_mode_entry *wme, u_int py, u_int ny)
4306 struct window_pane *wp = wme->wp;
4307 struct window_copy_mode_data *data = wme->data;
4308 struct screen_write_ctx ctx;
4309 u_int i;
4311 screen_write_start_pane(&ctx, wp, NULL);
4312 for (i = py; i < py + ny; i++)
4313 window_copy_write_line(wme, &ctx, i);
4314 screen_write_cursormove(&ctx, data->cx, data->cy, 0);
4315 screen_write_stop(&ctx);
4318 static void
4319 window_copy_redraw_screen(struct window_mode_entry *wme)
4321 struct window_copy_mode_data *data = wme->data;
4323 window_copy_redraw_lines(wme, 0, screen_size_y(&data->screen));
4326 static void
4327 window_copy_synchronize_cursor_end(struct window_mode_entry *wme, int begin,
4328 int no_reset)
4330 struct window_copy_mode_data *data = wme->data;
4331 u_int xx, yy;
4333 xx = data->cx;
4334 yy = screen_hsize(data->backing) + data->cy - data->oy;
4335 switch (data->selflag) {
4336 case SEL_WORD:
4337 if (no_reset)
4338 break;
4339 begin = 0;
4340 if (data->dy > yy || (data->dy == yy && data->dx > xx)) {
4341 /* Right to left selection. */
4342 window_copy_cursor_previous_word_pos(wme,
4343 data->separators, &xx, &yy);
4344 begin = 1;
4346 /* Reset the end. */
4347 data->endselx = data->endselrx;
4348 data->endsely = data->endselry;
4349 } else {
4350 /* Left to right selection. */
4351 if (xx >= window_copy_find_length(wme, yy) ||
4352 !window_copy_in_set(wme, xx + 1, yy, WHITESPACE)) {
4353 window_copy_cursor_next_word_end_pos(wme,
4354 data->separators, &xx, &yy);
4357 /* Reset the start. */
4358 data->selx = data->selrx;
4359 data->sely = data->selry;
4361 break;
4362 case SEL_LINE:
4363 if (no_reset)
4364 break;
4365 begin = 0;
4366 if (data->dy > yy) {
4367 /* Right to left selection. */
4368 xx = 0;
4369 begin = 1;
4371 /* Reset the end. */
4372 data->endselx = data->endselrx;
4373 data->endsely = data->endselry;
4374 } else {
4375 /* Left to right selection. */
4376 if (yy < data->endselry)
4377 yy = data->endselry;
4378 xx = window_copy_find_length(wme, yy);
4380 /* Reset the start. */
4381 data->selx = data->selrx;
4382 data->sely = data->selry;
4384 break;
4385 case SEL_CHAR:
4386 break;
4388 if (begin) {
4389 data->selx = xx;
4390 data->sely = yy;
4391 } else {
4392 data->endselx = xx;
4393 data->endsely = yy;
4397 static void
4398 window_copy_synchronize_cursor(struct window_mode_entry *wme, int no_reset)
4400 struct window_copy_mode_data *data = wme->data;
4402 switch (data->cursordrag) {
4403 case CURSORDRAG_ENDSEL:
4404 window_copy_synchronize_cursor_end(wme, 0, no_reset);
4405 break;
4406 case CURSORDRAG_SEL:
4407 window_copy_synchronize_cursor_end(wme, 1, no_reset);
4408 break;
4409 case CURSORDRAG_NONE:
4410 break;
4414 static void
4415 window_copy_update_cursor(struct window_mode_entry *wme, u_int cx, u_int cy)
4417 struct window_pane *wp = wme->wp;
4418 struct window_copy_mode_data *data = wme->data;
4419 struct screen *s = &data->screen;
4420 struct screen_write_ctx ctx;
4421 u_int old_cx, old_cy;
4423 old_cx = data->cx; old_cy = data->cy;
4424 data->cx = cx; data->cy = cy;
4425 if (old_cx == screen_size_x(s))
4426 window_copy_redraw_lines(wme, old_cy, 1);
4427 if (data->cx == screen_size_x(s))
4428 window_copy_redraw_lines(wme, data->cy, 1);
4429 else {
4430 screen_write_start_pane(&ctx, wp, NULL);
4431 screen_write_cursormove(&ctx, data->cx, data->cy, 0);
4432 screen_write_stop(&ctx);
4436 static void
4437 window_copy_start_selection(struct window_mode_entry *wme)
4439 struct window_copy_mode_data *data = wme->data;
4441 data->selx = data->cx;
4442 data->sely = screen_hsize(data->backing) + data->cy - data->oy;
4444 data->endselx = data->selx;
4445 data->endsely = data->sely;
4447 data->cursordrag = CURSORDRAG_ENDSEL;
4449 window_copy_set_selection(wme, 1, 0);
4452 static int
4453 window_copy_adjust_selection(struct window_mode_entry *wme, u_int *selx,
4454 u_int *sely)
4456 struct window_copy_mode_data *data = wme->data;
4457 struct screen *s = &data->screen;
4458 u_int sx, sy, ty;
4459 int relpos;
4461 sx = *selx;
4462 sy = *sely;
4464 ty = screen_hsize(data->backing) - data->oy;
4465 if (sy < ty) {
4466 relpos = WINDOW_COPY_REL_POS_ABOVE;
4467 if (!data->rectflag)
4468 sx = 0;
4469 sy = 0;
4470 } else if (sy > ty + screen_size_y(s) - 1) {
4471 relpos = WINDOW_COPY_REL_POS_BELOW;
4472 if (!data->rectflag)
4473 sx = screen_size_x(s) - 1;
4474 sy = screen_size_y(s) - 1;
4475 } else {
4476 relpos = WINDOW_COPY_REL_POS_ON_SCREEN;
4477 sy -= ty;
4480 *selx = sx;
4481 *sely = sy;
4482 return (relpos);
4485 static int
4486 window_copy_update_selection(struct window_mode_entry *wme, int may_redraw,
4487 int no_reset)
4489 struct window_copy_mode_data *data = wme->data;
4490 struct screen *s = &data->screen;
4492 if (s->sel == NULL && data->lineflag == LINE_SEL_NONE)
4493 return (0);
4494 return (window_copy_set_selection(wme, may_redraw, no_reset));
4497 static int
4498 window_copy_set_selection(struct window_mode_entry *wme, int may_redraw,
4499 int no_reset)
4501 struct window_pane *wp = wme->wp;
4502 struct window_copy_mode_data *data = wme->data;
4503 struct screen *s = &data->screen;
4504 struct options *oo = wp->window->options;
4505 struct grid_cell gc;
4506 u_int sx, sy, cy, endsx, endsy;
4507 int startrelpos, endrelpos;
4509 window_copy_synchronize_cursor(wme, no_reset);
4511 /* Adjust the selection. */
4512 sx = data->selx;
4513 sy = data->sely;
4514 startrelpos = window_copy_adjust_selection(wme, &sx, &sy);
4516 /* Adjust the end of selection. */
4517 endsx = data->endselx;
4518 endsy = data->endsely;
4519 endrelpos = window_copy_adjust_selection(wme, &endsx, &endsy);
4521 /* Selection is outside of the current screen */
4522 if (startrelpos == endrelpos &&
4523 startrelpos != WINDOW_COPY_REL_POS_ON_SCREEN) {
4524 screen_hide_selection(s);
4525 return (0);
4528 /* Set colours and selection. */
4529 style_apply(&gc, oo, "mode-style", NULL);
4530 gc.flags |= GRID_FLAG_NOPALETTE;
4531 screen_set_selection(s, sx, sy, endsx, endsy, data->rectflag,
4532 data->modekeys, &gc);
4534 if (data->rectflag && may_redraw) {
4536 * Can't rely on the caller to redraw the right lines for
4537 * rectangle selection - find the highest line and the number
4538 * of lines, and redraw just past that in both directions
4540 cy = data->cy;
4541 if (data->cursordrag == CURSORDRAG_ENDSEL) {
4542 if (sy < cy)
4543 window_copy_redraw_lines(wme, sy, cy - sy + 1);
4544 else
4545 window_copy_redraw_lines(wme, cy, sy - cy + 1);
4546 } else {
4547 if (endsy < cy) {
4548 window_copy_redraw_lines(wme, endsy,
4549 cy - endsy + 1);
4550 } else {
4551 window_copy_redraw_lines(wme, cy,
4552 endsy - cy + 1);
4557 return (1);
4560 static void *
4561 window_copy_get_selection(struct window_mode_entry *wme, size_t *len)
4563 struct window_pane *wp = wme->wp;
4564 struct window_copy_mode_data *data = wme->data;
4565 struct screen *s = &data->screen;
4566 char *buf;
4567 size_t off;
4568 u_int i, xx, yy, sx, sy, ex, ey, ey_last;
4569 u_int firstsx, lastex, restex, restsx, selx;
4570 int keys;
4572 if (data->screen.sel == NULL && data->lineflag == LINE_SEL_NONE) {
4573 buf = window_copy_match_at_cursor(data);
4574 if (buf != NULL)
4575 *len = strlen(buf);
4576 else
4577 *len = 0;
4578 return (buf);
4581 buf = xmalloc(1);
4582 off = 0;
4584 *buf = '\0';
4587 * The selection extends from selx,sely to (adjusted) cx,cy on
4588 * the base screen.
4591 /* Find start and end. */
4592 xx = data->endselx;
4593 yy = data->endsely;
4594 if (yy < data->sely || (yy == data->sely && xx < data->selx)) {
4595 sx = xx; sy = yy;
4596 ex = data->selx; ey = data->sely;
4597 } else {
4598 sx = data->selx; sy = data->sely;
4599 ex = xx; ey = yy;
4602 /* Trim ex to end of line. */
4603 ey_last = window_copy_find_length(wme, ey);
4604 if (ex > ey_last)
4605 ex = ey_last;
4608 * Deal with rectangle-copy if necessary; four situations: start of
4609 * first line (firstsx), end of last line (lastex), start (restsx) and
4610 * end (restex) of all other lines.
4612 xx = screen_size_x(s);
4615 * Behave according to mode-keys. If it is emacs, copy like emacs,
4616 * keeping the top-left-most character, and dropping the
4617 * bottom-right-most, regardless of copy direction. If it is vi, also
4618 * keep bottom-right-most character.
4620 keys = options_get_number(wp->window->options, "mode-keys");
4621 if (data->rectflag) {
4623 * Need to ignore the column with the cursor in it, which for
4624 * rectangular copy means knowing which side the cursor is on.
4626 if (data->cursordrag == CURSORDRAG_ENDSEL)
4627 selx = data->selx;
4628 else
4629 selx = data->endselx;
4630 if (selx < data->cx) {
4631 /* Selection start is on the left. */
4632 if (keys == MODEKEY_EMACS) {
4633 lastex = data->cx;
4634 restex = data->cx;
4636 else {
4637 lastex = data->cx + 1;
4638 restex = data->cx + 1;
4640 firstsx = selx;
4641 restsx = selx;
4642 } else {
4643 /* Cursor is on the left. */
4644 lastex = selx + 1;
4645 restex = selx + 1;
4646 firstsx = data->cx;
4647 restsx = data->cx;
4649 } else {
4650 if (keys == MODEKEY_EMACS)
4651 lastex = ex;
4652 else
4653 lastex = ex + 1;
4654 restex = xx;
4655 firstsx = sx;
4656 restsx = 0;
4659 /* Copy the lines. */
4660 for (i = sy; i <= ey; i++) {
4661 window_copy_copy_line(wme, &buf, &off, i,
4662 (i == sy ? firstsx : restsx),
4663 (i == ey ? lastex : restex));
4666 /* Don't bother if no data. */
4667 if (off == 0) {
4668 free(buf);
4669 *len = 0;
4670 return (NULL);
4672 /* Remove final \n (unless at end in vi mode). */
4673 if (keys == MODEKEY_EMACS || lastex <= ey_last) {
4674 if (~grid_get_line(data->backing->grid, ey)->flags &
4675 GRID_LINE_WRAPPED || lastex != ey_last)
4676 off -= 1;
4678 *len = off;
4679 return (buf);
4682 static void
4683 window_copy_copy_buffer(struct window_mode_entry *wme, const char *prefix,
4684 void *buf, size_t len)
4686 struct window_pane *wp = wme->wp;
4687 struct screen_write_ctx ctx;
4689 if (options_get_number(global_options, "set-clipboard") != 0) {
4690 screen_write_start_pane(&ctx, wp, NULL);
4691 screen_write_setselection(&ctx, "", buf, len);
4692 screen_write_stop(&ctx);
4693 notify_pane("pane-set-clipboard", wp);
4696 paste_add(prefix, buf, len);
4699 static void *
4700 window_copy_pipe_run(struct window_mode_entry *wme, struct session *s,
4701 const char *cmd, size_t *len)
4703 void *buf;
4704 struct job *job;
4706 buf = window_copy_get_selection(wme, len);
4707 if (cmd == NULL || *cmd == '\0')
4708 cmd = options_get_string(global_options, "copy-command");
4709 if (cmd != NULL && *cmd != '\0') {
4710 job = job_run(cmd, 0, NULL, NULL, s, NULL, NULL, NULL, NULL,
4711 NULL, JOB_NOWAIT, -1, -1);
4712 bufferevent_write(job_get_event(job), buf, *len);
4714 return (buf);
4717 static void
4718 window_copy_pipe(struct window_mode_entry *wme, struct session *s,
4719 const char *cmd)
4721 size_t len;
4723 window_copy_pipe_run(wme, s, cmd, &len);
4726 static void
4727 window_copy_copy_pipe(struct window_mode_entry *wme, struct session *s,
4728 const char *prefix, const char *cmd)
4730 void *buf;
4731 size_t len;
4733 buf = window_copy_pipe_run(wme, s, cmd, &len);
4734 if (buf != NULL)
4735 window_copy_copy_buffer(wme, prefix, buf, len);
4738 static void
4739 window_copy_copy_selection(struct window_mode_entry *wme, const char *prefix)
4741 char *buf;
4742 size_t len;
4744 buf = window_copy_get_selection(wme, &len);
4745 if (buf != NULL)
4746 window_copy_copy_buffer(wme, prefix, buf, len);
4749 static void
4750 window_copy_append_selection(struct window_mode_entry *wme)
4752 struct window_pane *wp = wme->wp;
4753 char *buf;
4754 struct paste_buffer *pb;
4755 const char *bufdata, *bufname = NULL;
4756 size_t len, bufsize;
4757 struct screen_write_ctx ctx;
4759 buf = window_copy_get_selection(wme, &len);
4760 if (buf == NULL)
4761 return;
4763 if (options_get_number(global_options, "set-clipboard") != 0) {
4764 screen_write_start_pane(&ctx, wp, NULL);
4765 screen_write_setselection(&ctx, "", buf, len);
4766 screen_write_stop(&ctx);
4767 notify_pane("pane-set-clipboard", wp);
4770 pb = paste_get_top(&bufname);
4771 if (pb != NULL) {
4772 bufdata = paste_buffer_data(pb, &bufsize);
4773 buf = xrealloc(buf, len + bufsize);
4774 memmove(buf + bufsize, buf, len);
4775 memcpy(buf, bufdata, bufsize);
4776 len += bufsize;
4778 if (paste_set(buf, len, bufname, NULL) != 0)
4779 free(buf);
4782 static void
4783 window_copy_copy_line(struct window_mode_entry *wme, char **buf, size_t *off,
4784 u_int sy, u_int sx, u_int ex)
4786 struct window_copy_mode_data *data = wme->data;
4787 struct grid *gd = data->backing->grid;
4788 struct grid_cell gc;
4789 struct grid_line *gl;
4790 struct utf8_data ud;
4791 u_int i, xx, wrapped = 0;
4792 const char *s;
4794 if (sx > ex)
4795 return;
4798 * Work out if the line was wrapped at the screen edge and all of it is
4799 * on screen.
4801 gl = grid_get_line(gd, sy);
4802 if (gl->flags & GRID_LINE_WRAPPED && gl->cellsize <= gd->sx)
4803 wrapped = 1;
4805 /* If the line was wrapped, don't strip spaces (use the full length). */
4806 if (wrapped)
4807 xx = gl->cellsize;
4808 else
4809 xx = window_copy_find_length(wme, sy);
4810 if (ex > xx)
4811 ex = xx;
4812 if (sx > xx)
4813 sx = xx;
4815 if (sx < ex) {
4816 for (i = sx; i < ex; i++) {
4817 grid_get_cell(gd, i, sy, &gc);
4818 if (gc.flags & GRID_FLAG_PADDING)
4819 continue;
4820 utf8_copy(&ud, &gc.data);
4821 if (ud.size == 1 && (gc.attr & GRID_ATTR_CHARSET)) {
4822 s = tty_acs_get(NULL, ud.data[0]);
4823 if (s != NULL && strlen(s) <= sizeof ud.data) {
4824 ud.size = strlen(s);
4825 memcpy(ud.data, s, ud.size);
4829 *buf = xrealloc(*buf, (*off) + ud.size);
4830 memcpy(*buf + *off, ud.data, ud.size);
4831 *off += ud.size;
4835 /* Only add a newline if the line wasn't wrapped. */
4836 if (!wrapped || ex != xx) {
4837 *buf = xrealloc(*buf, (*off) + 1);
4838 (*buf)[(*off)++] = '\n';
4842 static void
4843 window_copy_clear_selection(struct window_mode_entry *wme)
4845 struct window_copy_mode_data *data = wme->data;
4846 u_int px, py;
4848 screen_clear_selection(&data->screen);
4850 data->cursordrag = CURSORDRAG_NONE;
4851 data->lineflag = LINE_SEL_NONE;
4852 data->selflag = SEL_CHAR;
4854 py = screen_hsize(data->backing) + data->cy - data->oy;
4855 px = window_copy_find_length(wme, py);
4856 if (data->cx > px)
4857 window_copy_update_cursor(wme, px, data->cy);
4860 static int
4861 window_copy_in_set(struct window_mode_entry *wme, u_int px, u_int py,
4862 const char *set)
4864 struct window_copy_mode_data *data = wme->data;
4865 struct grid_cell gc;
4867 grid_get_cell(data->backing->grid, px, py, &gc);
4868 if (gc.flags & GRID_FLAG_PADDING)
4869 return (0);
4870 return (utf8_cstrhas(set, &gc.data));
4873 static u_int
4874 window_copy_find_length(struct window_mode_entry *wme, u_int py)
4876 struct window_copy_mode_data *data = wme->data;
4878 return (grid_line_length(data->backing->grid, py));
4881 static void
4882 window_copy_cursor_start_of_line(struct window_mode_entry *wme)
4884 struct window_copy_mode_data *data = wme->data;
4885 struct screen *back_s = data->backing;
4886 struct grid_reader gr;
4887 u_int px, py, oldy, hsize;
4889 px = data->cx;
4890 hsize = screen_hsize(back_s);
4891 py = hsize + data->cy - data->oy;
4892 oldy = data->cy;
4894 grid_reader_start(&gr, back_s->grid, px, py);
4895 grid_reader_cursor_start_of_line(&gr, 1);
4896 grid_reader_get_cursor(&gr, &px, &py);
4897 window_copy_acquire_cursor_up(wme, hsize, data->oy, oldy, px, py);
4900 static void
4901 window_copy_cursor_back_to_indentation(struct window_mode_entry *wme)
4903 struct window_copy_mode_data *data = wme->data;
4904 struct screen *back_s = data->backing;
4905 struct grid_reader gr;
4906 u_int px, py, oldy, hsize;
4908 px = data->cx;
4909 hsize = screen_hsize(back_s);
4910 py = hsize + data->cy - data->oy;
4911 oldy = data->cy;
4913 grid_reader_start(&gr, back_s->grid, px, py);
4914 grid_reader_cursor_back_to_indentation(&gr);
4915 grid_reader_get_cursor(&gr, &px, &py);
4916 window_copy_acquire_cursor_up(wme, hsize, data->oy, oldy, px, py);
4919 static void
4920 window_copy_cursor_end_of_line(struct window_mode_entry *wme)
4922 struct window_copy_mode_data *data = wme->data;
4923 struct screen *back_s = data->backing;
4924 struct grid_reader gr;
4925 u_int px, py, oldy, hsize;
4927 px = data->cx;
4928 hsize = screen_hsize(back_s);
4929 py = hsize + data->cy - data->oy;
4930 oldy = data->cy;
4932 grid_reader_start(&gr, back_s->grid, px, py);
4933 if (data->screen.sel != NULL && data->rectflag)
4934 grid_reader_cursor_end_of_line(&gr, 1, 1);
4935 else
4936 grid_reader_cursor_end_of_line(&gr, 1, 0);
4937 grid_reader_get_cursor(&gr, &px, &py);
4938 window_copy_acquire_cursor_down(wme, hsize, screen_size_y(back_s),
4939 data->oy, oldy, px, py, 0);
4942 static void
4943 window_copy_other_end(struct window_mode_entry *wme)
4945 struct window_copy_mode_data *data = wme->data;
4946 struct screen *s = &data->screen;
4947 u_int selx, sely, cy, yy, hsize;
4949 if (s->sel == NULL && data->lineflag == LINE_SEL_NONE)
4950 return;
4952 if (data->lineflag == LINE_SEL_LEFT_RIGHT)
4953 data->lineflag = LINE_SEL_RIGHT_LEFT;
4954 else if (data->lineflag == LINE_SEL_RIGHT_LEFT)
4955 data->lineflag = LINE_SEL_LEFT_RIGHT;
4957 switch (data->cursordrag) {
4958 case CURSORDRAG_NONE:
4959 case CURSORDRAG_SEL:
4960 data->cursordrag = CURSORDRAG_ENDSEL;
4961 break;
4962 case CURSORDRAG_ENDSEL:
4963 data->cursordrag = CURSORDRAG_SEL;
4964 break;
4967 selx = data->endselx;
4968 sely = data->endsely;
4969 if (data->cursordrag == CURSORDRAG_SEL) {
4970 selx = data->selx;
4971 sely = data->sely;
4974 cy = data->cy;
4975 yy = screen_hsize(data->backing) + data->cy - data->oy;
4977 data->cx = selx;
4979 hsize = screen_hsize(data->backing);
4980 if (sely < hsize - data->oy) { /* above */
4981 data->oy = hsize - sely;
4982 data->cy = 0;
4983 } else if (sely > hsize - data->oy + screen_size_y(s)) { /* below */
4984 data->oy = hsize - sely + screen_size_y(s) - 1;
4985 data->cy = screen_size_y(s) - 1;
4986 } else
4987 data->cy = cy + sely - yy;
4989 window_copy_update_selection(wme, 1, 1);
4990 window_copy_redraw_screen(wme);
4993 static void
4994 window_copy_cursor_left(struct window_mode_entry *wme)
4996 struct window_copy_mode_data *data = wme->data;
4997 struct screen *back_s = data->backing;
4998 struct grid_reader gr;
4999 u_int px, py, oldy, hsize;
5001 px = data->cx;
5002 hsize = screen_hsize(back_s);
5003 py = hsize + data->cy - data->oy;
5004 oldy = data->cy;
5006 grid_reader_start(&gr, back_s->grid, px, py);
5007 grid_reader_cursor_left(&gr, 1);
5008 grid_reader_get_cursor(&gr, &px, &py);
5009 window_copy_acquire_cursor_up(wme, hsize, data->oy, oldy, px, py);
5012 static void
5013 window_copy_cursor_right(struct window_mode_entry *wme, int all)
5015 struct window_copy_mode_data *data = wme->data;
5016 struct screen *back_s = data->backing;
5017 struct grid_reader gr;
5018 u_int px, py, oldy, hsize;
5020 px = data->cx;
5021 hsize = screen_hsize(back_s);
5022 py = hsize + data->cy - data->oy;
5023 oldy = data->cy;
5025 grid_reader_start(&gr, back_s->grid, px, py);
5026 grid_reader_cursor_right(&gr, 1, all);
5027 grid_reader_get_cursor(&gr, &px, &py);
5028 window_copy_acquire_cursor_down(wme, hsize, screen_size_y(back_s),
5029 data->oy, oldy, px, py, 0);
5032 static void
5033 window_copy_cursor_up(struct window_mode_entry *wme, int scroll_only)
5035 struct window_copy_mode_data *data = wme->data;
5036 struct screen *s = &data->screen;
5037 u_int ox, oy, px, py;
5038 int norectsel;
5040 norectsel = data->screen.sel == NULL || !data->rectflag;
5041 oy = screen_hsize(data->backing) + data->cy - data->oy;
5042 ox = window_copy_find_length(wme, oy);
5043 if (norectsel && data->cx != ox) {
5044 data->lastcx = data->cx;
5045 data->lastsx = ox;
5048 if (data->lineflag == LINE_SEL_LEFT_RIGHT && oy == data->sely)
5049 window_copy_other_end(wme);
5051 if (scroll_only || data->cy == 0) {
5052 if (norectsel)
5053 data->cx = data->lastcx;
5054 window_copy_scroll_down(wme, 1);
5055 if (scroll_only) {
5056 if (data->cy == screen_size_y(s) - 1)
5057 window_copy_redraw_lines(wme, data->cy, 1);
5058 else
5059 window_copy_redraw_lines(wme, data->cy, 2);
5061 } else {
5062 if (norectsel) {
5063 window_copy_update_cursor(wme, data->lastcx,
5064 data->cy - 1);
5065 } else
5066 window_copy_update_cursor(wme, data->cx, data->cy - 1);
5067 if (window_copy_update_selection(wme, 1, 0)) {
5068 if (data->cy == screen_size_y(s) - 1)
5069 window_copy_redraw_lines(wme, data->cy, 1);
5070 else
5071 window_copy_redraw_lines(wme, data->cy, 2);
5075 if (norectsel) {
5076 py = screen_hsize(data->backing) + data->cy - data->oy;
5077 px = window_copy_find_length(wme, py);
5078 if ((data->cx >= data->lastsx && data->cx != px) ||
5079 data->cx > px)
5081 window_copy_update_cursor(wme, px, data->cy);
5082 if (window_copy_update_selection(wme, 1, 0))
5083 window_copy_redraw_lines(wme, data->cy, 1);
5087 if (data->lineflag == LINE_SEL_LEFT_RIGHT)
5089 py = screen_hsize(data->backing) + data->cy - data->oy;
5090 if (data->rectflag)
5091 px = screen_size_x(data->backing);
5092 else
5093 px = window_copy_find_length(wme, py);
5094 window_copy_update_cursor(wme, px, data->cy);
5095 if (window_copy_update_selection(wme, 1, 0))
5096 window_copy_redraw_lines(wme, data->cy, 1);
5098 else if (data->lineflag == LINE_SEL_RIGHT_LEFT)
5100 window_copy_update_cursor(wme, 0, data->cy);
5101 if (window_copy_update_selection(wme, 1, 0))
5102 window_copy_redraw_lines(wme, data->cy, 1);
5106 static void
5107 window_copy_cursor_down(struct window_mode_entry *wme, int scroll_only)
5109 struct window_copy_mode_data *data = wme->data;
5110 struct screen *s = &data->screen;
5111 u_int ox, oy, px, py;
5112 int norectsel;
5114 norectsel = data->screen.sel == NULL || !data->rectflag;
5115 oy = screen_hsize(data->backing) + data->cy - data->oy;
5116 ox = window_copy_find_length(wme, oy);
5117 if (norectsel && data->cx != ox) {
5118 data->lastcx = data->cx;
5119 data->lastsx = ox;
5122 if (data->lineflag == LINE_SEL_RIGHT_LEFT && oy == data->endsely)
5123 window_copy_other_end(wme);
5125 if (scroll_only || data->cy == screen_size_y(s) - 1) {
5126 if (norectsel)
5127 data->cx = data->lastcx;
5128 window_copy_scroll_up(wme, 1);
5129 if (scroll_only && data->cy > 0)
5130 window_copy_redraw_lines(wme, data->cy - 1, 2);
5131 } else {
5132 if (norectsel) {
5133 window_copy_update_cursor(wme, data->lastcx,
5134 data->cy + 1);
5135 } else
5136 window_copy_update_cursor(wme, data->cx, data->cy + 1);
5137 if (window_copy_update_selection(wme, 1, 0))
5138 window_copy_redraw_lines(wme, data->cy - 1, 2);
5141 if (norectsel) {
5142 py = screen_hsize(data->backing) + data->cy - data->oy;
5143 px = window_copy_find_length(wme, py);
5144 if ((data->cx >= data->lastsx && data->cx != px) ||
5145 data->cx > px)
5147 window_copy_update_cursor(wme, px, data->cy);
5148 if (window_copy_update_selection(wme, 1, 0))
5149 window_copy_redraw_lines(wme, data->cy, 1);
5153 if (data->lineflag == LINE_SEL_LEFT_RIGHT)
5155 py = screen_hsize(data->backing) + data->cy - data->oy;
5156 if (data->rectflag)
5157 px = screen_size_x(data->backing);
5158 else
5159 px = window_copy_find_length(wme, py);
5160 window_copy_update_cursor(wme, px, data->cy);
5161 if (window_copy_update_selection(wme, 1, 0))
5162 window_copy_redraw_lines(wme, data->cy, 1);
5164 else if (data->lineflag == LINE_SEL_RIGHT_LEFT)
5166 window_copy_update_cursor(wme, 0, data->cy);
5167 if (window_copy_update_selection(wme, 1, 0))
5168 window_copy_redraw_lines(wme, data->cy, 1);
5172 static void
5173 window_copy_cursor_jump(struct window_mode_entry *wme)
5175 struct window_copy_mode_data *data = wme->data;
5176 struct screen *back_s = data->backing;
5177 struct grid_reader gr;
5178 u_int px, py, oldy, hsize;
5180 px = data->cx + 1;
5181 hsize = screen_hsize(back_s);
5182 py = hsize + data->cy - data->oy;
5183 oldy = data->cy;
5185 grid_reader_start(&gr, back_s->grid, px, py);
5186 if (grid_reader_cursor_jump(&gr, data->jumpchar)) {
5187 grid_reader_get_cursor(&gr, &px, &py);
5188 window_copy_acquire_cursor_down(wme, hsize,
5189 screen_size_y(back_s), data->oy, oldy, px, py, 0);
5193 static void
5194 window_copy_cursor_jump_back(struct window_mode_entry *wme)
5196 struct window_copy_mode_data *data = wme->data;
5197 struct screen *back_s = data->backing;
5198 struct grid_reader gr;
5199 u_int px, py, oldy, hsize;
5201 px = data->cx;
5202 hsize = screen_hsize(back_s);
5203 py = hsize + data->cy - data->oy;
5204 oldy = data->cy;
5206 grid_reader_start(&gr, back_s->grid, px, py);
5207 grid_reader_cursor_left(&gr, 0);
5208 if (grid_reader_cursor_jump_back(&gr, data->jumpchar)) {
5209 grid_reader_get_cursor(&gr, &px, &py);
5210 window_copy_acquire_cursor_up(wme, hsize, data->oy, oldy, px,
5211 py);
5215 static void
5216 window_copy_cursor_jump_to(struct window_mode_entry *wme)
5218 struct window_copy_mode_data *data = wme->data;
5219 struct screen *back_s = data->backing;
5220 struct grid_reader gr;
5221 u_int px, py, oldy, hsize;
5223 px = data->cx + 2;
5224 hsize = screen_hsize(back_s);
5225 py = hsize + data->cy - data->oy;
5226 oldy = data->cy;
5228 grid_reader_start(&gr, back_s->grid, px, py);
5229 if (grid_reader_cursor_jump(&gr, data->jumpchar)) {
5230 grid_reader_cursor_left(&gr, 1);
5231 grid_reader_get_cursor(&gr, &px, &py);
5232 window_copy_acquire_cursor_down(wme, hsize,
5233 screen_size_y(back_s), data->oy, oldy, px, py, 0);
5237 static void
5238 window_copy_cursor_jump_to_back(struct window_mode_entry *wme)
5240 struct window_copy_mode_data *data = wme->data;
5241 struct screen *back_s = data->backing;
5242 struct grid_reader gr;
5243 u_int px, py, oldy, hsize;
5245 px = data->cx;
5246 hsize = screen_hsize(back_s);
5247 py = hsize + data->cy - data->oy;
5248 oldy = data->cy;
5250 grid_reader_start(&gr, back_s->grid, px, py);
5251 grid_reader_cursor_left(&gr, 0);
5252 grid_reader_cursor_left(&gr, 0);
5253 if (grid_reader_cursor_jump_back(&gr, data->jumpchar)) {
5254 grid_reader_cursor_right(&gr, 1, 0);
5255 grid_reader_get_cursor(&gr, &px, &py);
5256 window_copy_acquire_cursor_up(wme, hsize, data->oy, oldy, px,
5257 py);
5261 static void
5262 window_copy_cursor_next_word(struct window_mode_entry *wme,
5263 const char *separators)
5265 struct window_copy_mode_data *data = wme->data;
5266 struct screen *back_s = data->backing;
5267 struct grid_reader gr;
5268 u_int px, py, oldy, hsize;
5270 px = data->cx;
5271 hsize = screen_hsize(back_s);
5272 py = hsize + data->cy - data->oy;
5273 oldy = data->cy;
5275 grid_reader_start(&gr, back_s->grid, px, py);
5276 grid_reader_cursor_next_word(&gr, separators);
5277 grid_reader_get_cursor(&gr, &px, &py);
5278 window_copy_acquire_cursor_down(wme, hsize, screen_size_y(back_s),
5279 data->oy, oldy, px, py, 0);
5282 /* Compute the next place where a word ends. */
5283 static void
5284 window_copy_cursor_next_word_end_pos(struct window_mode_entry *wme,
5285 const char *separators, u_int *ppx, u_int *ppy)
5287 struct window_pane *wp = wme->wp;
5288 struct window_copy_mode_data *data = wme->data;
5289 struct options *oo = wp->window->options;
5290 struct screen *back_s = data->backing;
5291 struct grid_reader gr;
5292 u_int px, py, hsize;
5294 px = data->cx;
5295 hsize = screen_hsize(back_s);
5296 py = hsize + data->cy - data->oy;
5298 grid_reader_start(&gr, back_s->grid, px, py);
5299 if (options_get_number(oo, "mode-keys") == MODEKEY_VI) {
5300 if (!grid_reader_in_set(&gr, WHITESPACE))
5301 grid_reader_cursor_right(&gr, 0, 0);
5302 grid_reader_cursor_next_word_end(&gr, separators);
5303 grid_reader_cursor_left(&gr, 1);
5304 } else
5305 grid_reader_cursor_next_word_end(&gr, separators);
5306 grid_reader_get_cursor(&gr, &px, &py);
5307 *ppx = px;
5308 *ppy = py;
5311 /* Move to the next place where a word ends. */
5312 static void
5313 window_copy_cursor_next_word_end(struct window_mode_entry *wme,
5314 const char *separators, int no_reset)
5316 struct window_pane *wp = wme->wp;
5317 struct window_copy_mode_data *data = wme->data;
5318 struct options *oo = wp->window->options;
5319 struct screen *back_s = data->backing;
5320 struct grid_reader gr;
5321 u_int px, py, oldy, hsize;
5323 px = data->cx;
5324 hsize = screen_hsize(back_s);
5325 py = hsize + data->cy - data->oy;
5326 oldy = data->cy;
5328 grid_reader_start(&gr, back_s->grid, px, py);
5329 if (options_get_number(oo, "mode-keys") == MODEKEY_VI) {
5330 if (!grid_reader_in_set(&gr, WHITESPACE))
5331 grid_reader_cursor_right(&gr, 0, 0);
5332 grid_reader_cursor_next_word_end(&gr, separators);
5333 grid_reader_cursor_left(&gr, 1);
5334 } else
5335 grid_reader_cursor_next_word_end(&gr, separators);
5336 grid_reader_get_cursor(&gr, &px, &py);
5337 window_copy_acquire_cursor_down(wme, hsize, screen_size_y(back_s),
5338 data->oy, oldy, px, py, no_reset);
5341 /* Compute the previous place where a word begins. */
5342 static void
5343 window_copy_cursor_previous_word_pos(struct window_mode_entry *wme,
5344 const char *separators, u_int *ppx, u_int *ppy)
5346 struct window_copy_mode_data *data = wme->data;
5347 struct screen *back_s = data->backing;
5348 struct grid_reader gr;
5349 u_int px, py, hsize;
5351 px = data->cx;
5352 hsize = screen_hsize(back_s);
5353 py = hsize + data->cy - data->oy;
5355 grid_reader_start(&gr, back_s->grid, px, py);
5356 grid_reader_cursor_previous_word(&gr, separators, /* already= */ 0,
5357 /* stop_at_eol= */ 1);
5358 grid_reader_get_cursor(&gr, &px, &py);
5359 *ppx = px;
5360 *ppy = py;
5363 /* Move to the previous place where a word begins. */
5364 static void
5365 window_copy_cursor_previous_word(struct window_mode_entry *wme,
5366 const char *separators, int already)
5368 struct window_copy_mode_data *data = wme->data;
5369 struct window *w = wme->wp->window;
5370 struct screen *back_s = data->backing;
5371 struct grid_reader gr;
5372 u_int px, py, oldy, hsize;
5373 int stop_at_eol;
5375 if (options_get_number(w->options, "mode-keys") == MODEKEY_EMACS)
5376 stop_at_eol = 1;
5377 else
5378 stop_at_eol = 0;
5380 px = data->cx;
5381 hsize = screen_hsize(back_s);
5382 py = hsize + data->cy - data->oy;
5383 oldy = data->cy;
5385 grid_reader_start(&gr, back_s->grid, px, py);
5386 grid_reader_cursor_previous_word(&gr, separators, already, stop_at_eol);
5387 grid_reader_get_cursor(&gr, &px, &py);
5388 window_copy_acquire_cursor_up(wme, hsize, data->oy, oldy, px, py);
5391 static void
5392 window_copy_cursor_prompt(struct window_mode_entry *wme, int direction)
5394 struct window_copy_mode_data *data = wme->data;
5395 struct screen *s = data->backing;
5396 struct grid *gd = s->grid;
5397 u_int end_line;
5398 u_int line = gd->hsize - data->oy + data->cy;
5399 int add;
5401 if (direction == 0) { /* up */
5402 add = -1;
5403 end_line = 0;
5404 } else { /* down */
5405 add = 1;
5406 end_line = gd->hsize + gd->sy - 1;
5409 if (line == end_line)
5410 return;
5411 for (;;) {
5412 if (line == end_line)
5413 return;
5414 line += add;
5416 if (grid_get_line(gd, line)->flags & GRID_LINE_START_PROMPT)
5417 break;
5420 data->cx = 0;
5421 if (line > gd->hsize) {
5422 data->cy = line - gd->hsize;
5423 data->oy = 0;
5424 } else {
5425 data->cy = 0;
5426 data->oy = gd->hsize - line;
5429 window_copy_update_selection(wme, 1, 0);
5430 window_copy_redraw_screen(wme);
5433 static void
5434 window_copy_scroll_up(struct window_mode_entry *wme, u_int ny)
5436 struct window_pane *wp = wme->wp;
5437 struct window_copy_mode_data *data = wme->data;
5438 struct screen *s = &data->screen;
5439 struct screen_write_ctx ctx;
5441 if (data->oy < ny)
5442 ny = data->oy;
5443 if (ny == 0)
5444 return;
5445 data->oy -= ny;
5447 if (data->searchmark != NULL && !data->timeout)
5448 window_copy_search_marks(wme, NULL, data->searchregex, 1);
5449 window_copy_update_selection(wme, 0, 0);
5451 screen_write_start_pane(&ctx, wp, NULL);
5452 screen_write_cursormove(&ctx, 0, 0, 0);
5453 screen_write_deleteline(&ctx, ny, 8);
5454 window_copy_write_lines(wme, &ctx, screen_size_y(s) - ny, ny);
5455 window_copy_write_line(wme, &ctx, 0);
5456 if (screen_size_y(s) > 1)
5457 window_copy_write_line(wme, &ctx, 1);
5458 if (screen_size_y(s) > 3)
5459 window_copy_write_line(wme, &ctx, screen_size_y(s) - 2);
5460 if (s->sel != NULL && screen_size_y(s) > ny)
5461 window_copy_write_line(wme, &ctx, screen_size_y(s) - ny - 1);
5462 screen_write_cursormove(&ctx, data->cx, data->cy, 0);
5463 screen_write_stop(&ctx);
5466 static void
5467 window_copy_scroll_down(struct window_mode_entry *wme, u_int ny)
5469 struct window_pane *wp = wme->wp;
5470 struct window_copy_mode_data *data = wme->data;
5471 struct screen *s = &data->screen;
5472 struct screen_write_ctx ctx;
5474 if (ny > screen_hsize(data->backing))
5475 return;
5477 if (data->oy > screen_hsize(data->backing) - ny)
5478 ny = screen_hsize(data->backing) - data->oy;
5479 if (ny == 0)
5480 return;
5481 data->oy += ny;
5483 if (data->searchmark != NULL && !data->timeout)
5484 window_copy_search_marks(wme, NULL, data->searchregex, 1);
5485 window_copy_update_selection(wme, 0, 0);
5487 screen_write_start_pane(&ctx, wp, NULL);
5488 screen_write_cursormove(&ctx, 0, 0, 0);
5489 screen_write_insertline(&ctx, ny, 8);
5490 window_copy_write_lines(wme, &ctx, 0, ny);
5491 if (s->sel != NULL && screen_size_y(s) > ny)
5492 window_copy_write_line(wme, &ctx, ny);
5493 else if (ny == 1) /* nuke position */
5494 window_copy_write_line(wme, &ctx, 1);
5495 screen_write_cursormove(&ctx, data->cx, data->cy, 0);
5496 screen_write_stop(&ctx);
5499 static void
5500 window_copy_rectangle_set(struct window_mode_entry *wme, int rectflag)
5502 struct window_copy_mode_data *data = wme->data;
5503 u_int px, py;
5505 data->rectflag = rectflag;
5507 py = screen_hsize(data->backing) + data->cy - data->oy;
5508 px = window_copy_find_length(wme, py);
5509 if (data->cx > px)
5510 window_copy_update_cursor(wme, px, data->cy);
5512 window_copy_update_selection(wme, 1, 0);
5513 window_copy_redraw_screen(wme);
5516 static void
5517 window_copy_move_mouse(struct mouse_event *m)
5519 struct window_pane *wp;
5520 struct window_mode_entry *wme;
5521 u_int x, y;
5523 wp = cmd_mouse_pane(m, NULL, NULL);
5524 if (wp == NULL)
5525 return;
5526 wme = TAILQ_FIRST(&wp->modes);
5527 if (wme == NULL)
5528 return;
5529 if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
5530 return;
5532 if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
5533 return;
5535 window_copy_update_cursor(wme, x, y);
5538 void
5539 window_copy_start_drag(struct client *c, struct mouse_event *m)
5541 struct window_pane *wp;
5542 struct window_mode_entry *wme;
5543 struct window_copy_mode_data *data;
5544 u_int x, y, yg;
5546 if (c == NULL)
5547 return;
5549 wp = cmd_mouse_pane(m, NULL, NULL);
5550 if (wp == NULL)
5551 return;
5552 wme = TAILQ_FIRST(&wp->modes);
5553 if (wme == NULL)
5554 return;
5555 if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
5556 return;
5558 if (cmd_mouse_at(wp, m, &x, &y, 1) != 0)
5559 return;
5561 c->tty.mouse_drag_update = window_copy_drag_update;
5562 c->tty.mouse_drag_release = window_copy_drag_release;
5564 data = wme->data;
5565 yg = screen_hsize(data->backing) + y - data->oy;
5566 if (x < data->selrx || x > data->endselrx || yg != data->selry)
5567 data->selflag = SEL_CHAR;
5568 switch (data->selflag) {
5569 case SEL_WORD:
5570 if (data->separators != NULL) {
5571 window_copy_update_cursor(wme, x, y);
5572 window_copy_cursor_previous_word_pos(wme,
5573 data->separators, &x, &y);
5574 y -= screen_hsize(data->backing) - data->oy;
5576 window_copy_update_cursor(wme, x, y);
5577 break;
5578 case SEL_LINE:
5579 window_copy_update_cursor(wme, 0, y);
5580 break;
5581 case SEL_CHAR:
5582 window_copy_update_cursor(wme, x, y);
5583 window_copy_start_selection(wme);
5584 break;
5587 window_copy_redraw_screen(wme);
5588 window_copy_drag_update(c, m);
5591 static void
5592 window_copy_drag_update(struct client *c, struct mouse_event *m)
5594 struct window_pane *wp;
5595 struct window_mode_entry *wme;
5596 struct window_copy_mode_data *data;
5597 u_int x, y, old_cx, old_cy;
5598 struct timeval tv = {
5599 .tv_usec = WINDOW_COPY_DRAG_REPEAT_TIME
5602 if (c == NULL)
5603 return;
5605 wp = cmd_mouse_pane(m, NULL, NULL);
5606 if (wp == NULL)
5607 return;
5608 wme = TAILQ_FIRST(&wp->modes);
5609 if (wme == NULL)
5610 return;
5611 if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
5612 return;
5614 data = wme->data;
5615 evtimer_del(&data->dragtimer);
5617 if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
5618 return;
5619 old_cx = data->cx;
5620 old_cy = data->cy;
5622 window_copy_update_cursor(wme, x, y);
5623 if (window_copy_update_selection(wme, 1, 0))
5624 window_copy_redraw_selection(wme, old_cy);
5625 if (old_cy != data->cy || old_cx == data->cx) {
5626 if (y == 0) {
5627 evtimer_add(&data->dragtimer, &tv);
5628 window_copy_cursor_up(wme, 1);
5629 } else if (y == screen_size_y(&data->screen) - 1) {
5630 evtimer_add(&data->dragtimer, &tv);
5631 window_copy_cursor_down(wme, 1);
5636 static void
5637 window_copy_drag_release(struct client *c, struct mouse_event *m)
5639 struct window_pane *wp;
5640 struct window_mode_entry *wme;
5641 struct window_copy_mode_data *data;
5643 if (c == NULL)
5644 return;
5646 wp = cmd_mouse_pane(m, NULL, NULL);
5647 if (wp == NULL)
5648 return;
5649 wme = TAILQ_FIRST(&wp->modes);
5650 if (wme == NULL)
5651 return;
5652 if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
5653 return;
5655 data = wme->data;
5656 evtimer_del(&data->dragtimer);
5659 static void
5660 window_copy_jump_to_mark(struct window_mode_entry *wme)
5662 struct window_copy_mode_data *data = wme->data;
5663 u_int tmx, tmy;
5665 tmx = data->cx;
5666 tmy = screen_hsize(data->backing) + data->cy - data->oy;
5667 data->cx = data->mx;
5668 if (data->my < screen_hsize(data->backing)) {
5669 data->cy = 0;
5670 data->oy = screen_hsize(data->backing) - data->my;
5671 } else {
5672 data->cy = data->my - screen_hsize(data->backing);
5673 data->oy = 0;
5675 data->mx = tmx;
5676 data->my = tmy;
5677 data->showmark = 1;
5678 window_copy_update_selection(wme, 0, 0);
5679 window_copy_redraw_screen(wme);
5682 /* Scroll up if the cursor went off the visible screen. */
5683 static void
5684 window_copy_acquire_cursor_up(struct window_mode_entry *wme, u_int hsize,
5685 u_int oy, u_int oldy, u_int px, u_int py)
5687 u_int cy, yy, ny, nd;
5689 yy = hsize - oy;
5690 if (py < yy) {
5691 ny = yy - py;
5692 cy = 0;
5693 nd = 1;
5694 } else {
5695 ny = 0;
5696 cy = py - yy;
5697 nd = oldy - cy + 1;
5699 while (ny > 0) {
5700 window_copy_cursor_up(wme, 1);
5701 ny--;
5703 window_copy_update_cursor(wme, px, cy);
5704 if (window_copy_update_selection(wme, 1, 0))
5705 window_copy_redraw_lines(wme, cy, nd);
5708 /* Scroll down if the cursor went off the visible screen. */
5709 static void
5710 window_copy_acquire_cursor_down(struct window_mode_entry *wme, u_int hsize,
5711 u_int sy, u_int oy, u_int oldy, u_int px, u_int py, int no_reset)
5713 u_int cy, yy, ny, nd;
5715 cy = py - hsize + oy;
5716 yy = sy - 1;
5717 if (cy > yy) {
5718 ny = cy - yy;
5719 oldy = yy;
5720 nd = 1;
5721 } else {
5722 ny = 0;
5723 nd = cy - oldy + 1;
5725 while (ny > 0) {
5726 window_copy_cursor_down(wme, 1);
5727 ny--;
5729 if (cy > yy)
5730 window_copy_update_cursor(wme, px, yy);
5731 else
5732 window_copy_update_cursor(wme, px, cy);
5733 if (window_copy_update_selection(wme, 1, no_reset))
5734 window_copy_redraw_lines(wme, oldy, nd);