makefile: fixup bad merge
[tmux.git] / window-copy.c
blob1c8a326d35ce9a7e1f944c020ab1a5983d052b94
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 const char *);
136 static void window_copy_scroll_up(struct window_mode_entry *, u_int);
137 static void window_copy_scroll_down(struct window_mode_entry *, u_int);
138 static void window_copy_rectangle_set(struct window_mode_entry *, int);
139 static void window_copy_move_mouse(struct mouse_event *);
140 static void window_copy_drag_update(struct client *, struct mouse_event *);
141 static void window_copy_drag_release(struct client *, struct mouse_event *);
142 static void window_copy_jump_to_mark(struct window_mode_entry *);
143 static void window_copy_acquire_cursor_up(struct window_mode_entry *,
144 u_int, u_int, u_int, u_int, u_int);
145 static void window_copy_acquire_cursor_down(struct window_mode_entry *,
146 u_int, u_int, u_int, u_int, u_int, u_int, int);
148 const struct window_mode window_copy_mode = {
149 .name = "copy-mode",
151 .init = window_copy_init,
152 .free = window_copy_free,
153 .resize = window_copy_resize,
154 .key_table = window_copy_key_table,
155 .command = window_copy_command,
156 .formats = window_copy_formats,
159 const struct window_mode window_view_mode = {
160 .name = "view-mode",
162 .init = window_copy_view_init,
163 .free = window_copy_free,
164 .resize = window_copy_resize,
165 .key_table = window_copy_key_table,
166 .command = window_copy_command,
167 .formats = window_copy_formats,
170 enum {
171 WINDOW_COPY_OFF,
172 WINDOW_COPY_SEARCHUP,
173 WINDOW_COPY_SEARCHDOWN,
174 WINDOW_COPY_JUMPFORWARD,
175 WINDOW_COPY_JUMPBACKWARD,
176 WINDOW_COPY_JUMPTOFORWARD,
177 WINDOW_COPY_JUMPTOBACKWARD,
180 enum {
181 WINDOW_COPY_REL_POS_ABOVE,
182 WINDOW_COPY_REL_POS_ON_SCREEN,
183 WINDOW_COPY_REL_POS_BELOW,
186 enum window_copy_cmd_action {
187 WINDOW_COPY_CMD_NOTHING,
188 WINDOW_COPY_CMD_REDRAW,
189 WINDOW_COPY_CMD_CANCEL,
192 enum window_copy_cmd_clear {
193 WINDOW_COPY_CMD_CLEAR_ALWAYS,
194 WINDOW_COPY_CMD_CLEAR_NEVER,
195 WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
198 struct window_copy_cmd_state {
199 struct window_mode_entry *wme;
200 struct args *args;
201 struct mouse_event *m;
203 struct client *c;
204 struct session *s;
205 struct winlink *wl;
209 * Copy mode's visible screen (the "screen" field) is filled from one of two
210 * sources: the original contents of the pane (used when we actually enter via
211 * the "copy-mode" command, to copy the contents of the current pane), or else
212 * a series of lines containing the output from an output-writing tmux command
213 * (such as any of the "show-*" or "list-*" commands).
215 * In either case, the full content of the copy-mode grid is pointed at by the
216 * "backing" field, and is copied into "screen" as needed (that is, when
217 * scrolling occurs). When copy-mode is backed by a pane, backing points
218 * directly at that pane's screen structure (&wp->base); when backed by a list
219 * of output-lines from a command, it points at a newly-allocated screen
220 * structure (which is deallocated when the mode ends).
222 struct window_copy_mode_data {
223 struct screen screen;
225 struct screen *backing;
226 int backing_written; /* backing display started */
227 struct screen *writing;
228 struct input_ctx *ictx;
230 int viewmode; /* view mode entered */
232 u_int oy; /* number of lines scrolled up */
234 u_int selx; /* beginning of selection */
235 u_int sely;
237 u_int endselx; /* end of selection */
238 u_int endsely;
240 enum {
241 CURSORDRAG_NONE, /* selection is independent of cursor */
242 CURSORDRAG_ENDSEL, /* end is synchronized with cursor */
243 CURSORDRAG_SEL, /* start is synchronized with cursor */
244 } cursordrag;
246 int modekeys;
247 enum {
248 LINE_SEL_NONE,
249 LINE_SEL_LEFT_RIGHT,
250 LINE_SEL_RIGHT_LEFT,
251 } lineflag; /* line selection mode */
252 int rectflag; /* in rectangle copy mode? */
253 int scroll_exit; /* exit on scroll to end? */
254 int hide_position; /* hide position marker */
256 enum {
257 SEL_CHAR, /* select one char at a time */
258 SEL_WORD, /* select one word at a time */
259 SEL_LINE, /* select one line at a time */
260 } selflag;
262 const char *separators; /* word separators */
264 u_int dx; /* drag start position */
265 u_int dy;
267 u_int selrx; /* selection reset positions */
268 u_int selry;
269 u_int endselrx;
270 u_int endselry;
272 u_int cx;
273 u_int cy;
275 u_int lastcx; /* position in last line w/ content */
276 u_int lastsx; /* size of last line w/ content */
278 u_int mx; /* mark position */
279 u_int my;
280 int showmark;
282 int searchtype;
283 int searchdirection;
284 int searchregex;
285 char *searchstr;
286 u_char *searchmark;
287 int searchcount;
288 int searchmore;
289 int searchall;
290 int searchx;
291 int searchy;
292 int searcho;
293 u_char searchgen;
295 int timeout; /* search has timed out */
296 #define WINDOW_COPY_SEARCH_TIMEOUT 10000
297 #define WINDOW_COPY_SEARCH_ALL_TIMEOUT 200
299 int jumptype;
300 struct utf8_data *jumpchar;
302 struct event dragtimer;
303 #define WINDOW_COPY_DRAG_REPEAT_TIME 50000
306 static void
307 window_copy_scroll_timer(__unused int fd, __unused short events, void *arg)
309 struct window_mode_entry *wme = arg;
310 struct window_pane *wp = wme->wp;
311 struct window_copy_mode_data *data = wme->data;
312 struct timeval tv = {
313 .tv_usec = WINDOW_COPY_DRAG_REPEAT_TIME
316 evtimer_del(&data->dragtimer);
318 if (TAILQ_FIRST(&wp->modes) != wme)
319 return;
321 if (data->cy == 0) {
322 evtimer_add(&data->dragtimer, &tv);
323 window_copy_cursor_up(wme, 1);
324 } else if (data->cy == screen_size_y(&data->screen) - 1) {
325 evtimer_add(&data->dragtimer, &tv);
326 window_copy_cursor_down(wme, 1);
330 static struct screen *
331 window_copy_clone_screen(struct screen *src, struct screen *hint, u_int *cx,
332 u_int *cy, int trim)
334 struct screen *dst;
335 const struct grid_line *gl;
336 u_int sy, wx, wy;
337 int reflow;
339 dst = xcalloc(1, sizeof *dst);
341 sy = screen_hsize(src) + screen_size_y(src);
342 if (trim) {
343 while (sy > screen_hsize(src)) {
344 gl = grid_peek_line(src->grid, sy - 1);
345 if (gl->cellused != 0)
346 break;
347 sy--;
350 log_debug("%s: target screen is %ux%u, source %ux%u", __func__,
351 screen_size_x(src), sy, screen_size_x(hint),
352 screen_hsize(src) + screen_size_y(src));
353 screen_init(dst, screen_size_x(src), sy, screen_hlimit(src));
356 * Ensure history is on for the backing grid so lines are not deleted
357 * during resizing.
359 dst->grid->flags |= GRID_HISTORY;
360 grid_duplicate_lines(dst->grid, 0, src->grid, 0, sy);
362 dst->grid->sy = sy - screen_hsize(src);
363 dst->grid->hsize = screen_hsize(src);
364 dst->grid->hscrolled = src->grid->hscrolled;
365 if (src->cy > dst->grid->sy - 1) {
366 dst->cx = 0;
367 dst->cy = dst->grid->sy - 1;
368 } else {
369 dst->cx = src->cx;
370 dst->cy = src->cy;
373 if (cx != NULL && cy != NULL) {
374 *cx = dst->cx;
375 *cy = screen_hsize(dst) + dst->cy;
376 reflow = (screen_size_x(hint) != screen_size_x(dst));
378 else
379 reflow = 0;
380 if (reflow)
381 grid_wrap_position(dst->grid, *cx, *cy, &wx, &wy);
382 screen_resize_cursor(dst, screen_size_x(hint), screen_size_y(hint), 1,
383 0, 0);
384 if (reflow)
385 grid_unwrap_position(dst->grid, cx, cy, wx, wy);
387 return (dst);
390 static struct window_copy_mode_data *
391 window_copy_common_init(struct window_mode_entry *wme)
393 struct window_pane *wp = wme->wp;
394 struct window_copy_mode_data *data;
395 struct screen *base = &wp->base;
397 wme->data = data = xcalloc(1, sizeof *data);
399 data->cursordrag = CURSORDRAG_NONE;
400 data->lineflag = LINE_SEL_NONE;
401 data->selflag = SEL_CHAR;
403 if (wp->searchstr != NULL) {
404 data->searchtype = WINDOW_COPY_SEARCHUP;
405 data->searchregex = wp->searchregex;
406 data->searchstr = xstrdup(wp->searchstr);
407 } else {
408 data->searchtype = WINDOW_COPY_OFF;
409 data->searchregex = 0;
410 data->searchstr = NULL;
412 data->searchx = data->searchy = data->searcho = -1;
413 data->searchall = 1;
415 data->jumptype = WINDOW_COPY_OFF;
416 data->jumpchar = NULL;
418 screen_init(&data->screen, screen_size_x(base), screen_size_y(base), 0);
419 data->modekeys = options_get_number(wp->window->options, "mode-keys");
421 evtimer_set(&data->dragtimer, window_copy_scroll_timer, wme);
423 return (data);
426 static struct screen *
427 window_copy_init(struct window_mode_entry *wme,
428 __unused struct cmd_find_state *fs, struct args *args)
430 struct window_pane *wp = wme->swp;
431 struct window_copy_mode_data *data;
432 struct screen *base = &wp->base;
433 struct screen_write_ctx ctx;
434 u_int i, cx, cy;
436 data = window_copy_common_init(wme);
437 data->backing = window_copy_clone_screen(base, &data->screen, &cx, &cy,
438 wme->swp != wme->wp);
440 data->cx = cx;
441 if (cy < screen_hsize(data->backing)) {
442 data->cy = 0;
443 data->oy = screen_hsize(data->backing) - cy;
444 } else {
445 data->cy = cy - screen_hsize(data->backing);
446 data->oy = 0;
449 data->scroll_exit = args_has(args, 'e');
450 data->hide_position = args_has(args, 'H');
452 data->screen.cx = data->cx;
453 data->screen.cy = data->cy;
454 data->mx = data->cx;
455 data->my = screen_hsize(data->backing) + data->cy - data->oy;
456 data->showmark = 0;
458 screen_write_start(&ctx, &data->screen);
459 for (i = 0; i < screen_size_y(&data->screen); i++)
460 window_copy_write_line(wme, &ctx, i);
461 screen_write_cursormove(&ctx, data->cx, data->cy, 0);
462 screen_write_stop(&ctx);
464 return (&data->screen);
467 static struct screen *
468 window_copy_view_init(struct window_mode_entry *wme,
469 __unused struct cmd_find_state *fs, __unused struct args *args)
471 struct window_pane *wp = wme->wp;
472 struct window_copy_mode_data *data;
473 struct screen *base = &wp->base;
474 u_int sx = screen_size_x(base);
476 data = window_copy_common_init(wme);
477 data->viewmode = 1;
479 data->backing = xmalloc(sizeof *data->backing);
480 screen_init(data->backing, sx, screen_size_y(base), UINT_MAX);
481 data->writing = xmalloc(sizeof *data->writing);
482 screen_init(data->writing, sx, screen_size_y(base), 0);
483 data->ictx = input_init(NULL, NULL, NULL);
484 data->mx = data->cx;
485 data->my = screen_hsize(data->backing) + data->cy - data->oy;
486 data->showmark = 0;
488 return (&data->screen);
491 static void
492 window_copy_free(struct window_mode_entry *wme)
494 struct window_copy_mode_data *data = wme->data;
496 evtimer_del(&data->dragtimer);
498 free(data->searchmark);
499 free(data->searchstr);
500 free(data->jumpchar);
502 if (data->writing != NULL) {
503 screen_free(data->writing);
504 free(data->writing);
506 if (data->ictx != NULL)
507 input_free(data->ictx);
508 screen_free(data->backing);
509 free(data->backing);
511 screen_free(&data->screen);
512 free(data);
515 void
516 window_copy_add(struct window_pane *wp, int parse, const char *fmt, ...)
518 va_list ap;
520 va_start(ap, fmt);
521 window_copy_vadd(wp, parse, fmt, ap);
522 va_end(ap);
525 static void
526 window_copy_init_ctx_cb(__unused struct screen_write_ctx *ctx,
527 struct tty_ctx *ttyctx)
529 memcpy(&ttyctx->defaults, &grid_default_cell, sizeof ttyctx->defaults);
530 ttyctx->palette = NULL;
531 ttyctx->redraw_cb = NULL;
532 ttyctx->set_client_cb = NULL;
533 ttyctx->arg = NULL;
536 void
537 window_copy_vadd(struct window_pane *wp, int parse, const char *fmt, va_list ap)
539 struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
540 struct window_copy_mode_data *data = wme->data;
541 struct screen *backing = data->backing;
542 struct screen *writing = data->writing;
543 struct screen_write_ctx writing_ctx, backing_ctx, ctx;
544 struct grid_cell gc;
545 u_int old_hsize, old_cy;
546 u_int sx = screen_size_x(backing);
547 char *text;
549 if (parse) {
550 vasprintf(&text, fmt, ap);
551 screen_write_start(&writing_ctx, writing);
552 screen_write_reset(&writing_ctx);
553 input_parse_screen(data->ictx, writing, window_copy_init_ctx_cb,
554 data, text, strlen(text));
555 free(text);
558 old_hsize = screen_hsize(data->backing);
559 screen_write_start(&backing_ctx, backing);
560 if (data->backing_written) {
562 * On the second or later line, do a CRLF before writing
563 * (so it's on a new line).
565 screen_write_carriagereturn(&backing_ctx);
566 screen_write_linefeed(&backing_ctx, 0, 8);
567 } else
568 data->backing_written = 1;
569 old_cy = backing->cy;
570 if (parse)
571 screen_write_fast_copy(&backing_ctx, writing, 0, 0, sx, 1);
572 else {
573 memcpy(&gc, &grid_default_cell, sizeof gc);
574 screen_write_vnputs(&backing_ctx, 0, &gc, fmt, ap);
576 screen_write_stop(&backing_ctx);
578 data->oy += screen_hsize(data->backing) - old_hsize;
580 screen_write_start_pane(&ctx, wp, &data->screen);
583 * If the history has changed, draw the top line.
584 * (If there's any history at all, it has changed.)
586 if (screen_hsize(data->backing))
587 window_copy_redraw_lines(wme, 0, 1);
589 /* Write the new lines. */
590 window_copy_redraw_lines(wme, old_cy, backing->cy - old_cy + 1);
592 screen_write_stop(&ctx);
595 void
596 window_copy_pageup(struct window_pane *wp, int half_page)
598 window_copy_pageup1(TAILQ_FIRST(&wp->modes), half_page);
601 static void
602 window_copy_pageup1(struct window_mode_entry *wme, int half_page)
604 struct window_copy_mode_data *data = wme->data;
605 struct screen *s = &data->screen;
606 u_int n, ox, oy, px, py;
608 oy = screen_hsize(data->backing) + data->cy - data->oy;
609 ox = window_copy_find_length(wme, oy);
611 if (data->cx != ox) {
612 data->lastcx = data->cx;
613 data->lastsx = ox;
615 data->cx = data->lastcx;
617 n = 1;
618 if (screen_size_y(s) > 2) {
619 if (half_page)
620 n = screen_size_y(s) / 2;
621 else
622 n = screen_size_y(s) - 2;
625 if (data->oy + n > screen_hsize(data->backing)) {
626 data->oy = screen_hsize(data->backing);
627 if (data->cy < n)
628 data->cy = 0;
629 else
630 data->cy -= n;
631 } else
632 data->oy += n;
634 if (data->screen.sel == NULL || !data->rectflag) {
635 py = screen_hsize(data->backing) + data->cy - data->oy;
636 px = window_copy_find_length(wme, py);
637 if ((data->cx >= data->lastsx && data->cx != px) ||
638 data->cx > px)
639 window_copy_cursor_end_of_line(wme);
642 if (data->searchmark != NULL && !data->timeout)
643 window_copy_search_marks(wme, NULL, data->searchregex, 1);
644 window_copy_update_selection(wme, 1, 0);
645 window_copy_redraw_screen(wme);
648 static int
649 window_copy_pagedown(struct window_mode_entry *wme, int half_page,
650 int scroll_exit)
652 struct window_copy_mode_data *data = wme->data;
653 struct screen *s = &data->screen;
654 u_int n, ox, oy, px, py;
656 oy = screen_hsize(data->backing) + data->cy - data->oy;
657 ox = window_copy_find_length(wme, oy);
659 if (data->cx != ox) {
660 data->lastcx = data->cx;
661 data->lastsx = ox;
663 data->cx = data->lastcx;
665 n = 1;
666 if (screen_size_y(s) > 2) {
667 if (half_page)
668 n = screen_size_y(s) / 2;
669 else
670 n = screen_size_y(s) - 2;
673 if (data->oy < n) {
674 data->oy = 0;
675 if (data->cy + (n - data->oy) >= screen_size_y(data->backing))
676 data->cy = screen_size_y(data->backing) - 1;
677 else
678 data->cy += n - data->oy;
679 } else
680 data->oy -= n;
682 if (data->screen.sel == NULL || !data->rectflag) {
683 py = screen_hsize(data->backing) + data->cy - data->oy;
684 px = window_copy_find_length(wme, py);
685 if ((data->cx >= data->lastsx && data->cx != px) ||
686 data->cx > px)
687 window_copy_cursor_end_of_line(wme);
690 if (scroll_exit && data->oy == 0)
691 return (1);
692 if (data->searchmark != NULL && !data->timeout)
693 window_copy_search_marks(wme, NULL, data->searchregex, 1);
694 window_copy_update_selection(wme, 1, 0);
695 window_copy_redraw_screen(wme);
696 return (0);
699 static void
700 window_copy_previous_paragraph(struct window_mode_entry *wme)
702 struct window_copy_mode_data *data = wme->data;
703 u_int oy;
705 oy = screen_hsize(data->backing) + data->cy - data->oy;
707 while (oy > 0 && window_copy_find_length(wme, oy) == 0)
708 oy--;
710 while (oy > 0 && window_copy_find_length(wme, oy) > 0)
711 oy--;
713 window_copy_scroll_to(wme, 0, oy, 0);
716 static void
717 window_copy_next_paragraph(struct window_mode_entry *wme)
719 struct window_copy_mode_data *data = wme->data;
720 struct screen *s = &data->screen;
721 u_int maxy, ox, oy;
723 oy = screen_hsize(data->backing) + data->cy - data->oy;
724 maxy = screen_hsize(data->backing) + screen_size_y(s) - 1;
726 while (oy < maxy && window_copy_find_length(wme, oy) == 0)
727 oy++;
729 while (oy < maxy && window_copy_find_length(wme, oy) > 0)
730 oy++;
732 ox = window_copy_find_length(wme, oy);
733 window_copy_scroll_to(wme, ox, oy, 0);
736 char *
737 window_copy_get_word(struct window_pane *wp, u_int x, u_int y)
739 struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
740 struct window_copy_mode_data *data = wme->data;
741 struct grid *gd = data->screen.grid;
743 return (format_grid_word(gd, x, gd->hsize + y));
746 char *
747 window_copy_get_line(struct window_pane *wp, u_int y)
749 struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
750 struct window_copy_mode_data *data = wme->data;
751 struct grid *gd = data->screen.grid;
753 return (format_grid_line(gd, gd->hsize + y));
756 static void *
757 window_copy_cursor_word_cb(struct format_tree *ft)
759 struct window_pane *wp = format_get_pane(ft);
760 struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
761 struct window_copy_mode_data *data = wme->data;
763 return (window_copy_get_word(wp, data->cx, data->cy));
766 static void *
767 window_copy_cursor_line_cb(struct format_tree *ft)
769 struct window_pane *wp = format_get_pane(ft);
770 struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
771 struct window_copy_mode_data *data = wme->data;
773 return (window_copy_get_line(wp, data->cy));
776 static void *
777 window_copy_search_match_cb(struct format_tree *ft)
779 struct window_pane *wp = format_get_pane(ft);
780 struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
781 struct window_copy_mode_data *data = wme->data;
783 return (window_copy_match_at_cursor(data));
786 static void
787 window_copy_formats(struct window_mode_entry *wme, struct format_tree *ft)
789 struct window_copy_mode_data *data = wme->data;
791 format_add(ft, "scroll_position", "%d", data->oy);
792 format_add(ft, "rectangle_toggle", "%d", data->rectflag);
794 format_add(ft, "copy_cursor_x", "%d", data->cx);
795 format_add(ft, "copy_cursor_y", "%d", data->cy);
797 format_add(ft, "selection_present", "%d", data->screen.sel != NULL);
798 if (data->screen.sel != NULL) {
799 format_add(ft, "selection_start_x", "%d", data->selx);
800 format_add(ft, "selection_start_y", "%d", data->sely);
801 format_add(ft, "selection_end_x", "%d", data->endselx);
802 format_add(ft, "selection_end_y", "%d", data->endsely);
803 format_add(ft, "selection_active", "%d",
804 data->cursordrag != CURSORDRAG_NONE);
805 } else
806 format_add(ft, "selection_active", "%d", 0);
808 format_add(ft, "search_present", "%d", data->searchmark != NULL);
809 format_add_cb(ft, "search_match", window_copy_search_match_cb);
811 format_add_cb(ft, "copy_cursor_word", window_copy_cursor_word_cb);
812 format_add_cb(ft, "copy_cursor_line", window_copy_cursor_line_cb);
815 static void
816 window_copy_size_changed(struct window_mode_entry *wme)
818 struct window_copy_mode_data *data = wme->data;
819 struct screen *s = &data->screen;
820 struct screen_write_ctx ctx;
821 int search = (data->searchmark != NULL);
823 window_copy_clear_selection(wme);
824 window_copy_clear_marks(wme);
826 screen_write_start(&ctx, s);
827 window_copy_write_lines(wme, &ctx, 0, screen_size_y(s));
828 screen_write_stop(&ctx);
830 if (search && !data->timeout)
831 window_copy_search_marks(wme, NULL, data->searchregex, 0);
832 data->searchx = data->cx;
833 data->searchy = data->cy;
834 data->searcho = data->oy;
837 static void
838 window_copy_resize(struct window_mode_entry *wme, u_int sx, u_int sy)
840 struct window_copy_mode_data *data = wme->data;
841 struct screen *s = &data->screen;
842 struct grid *gd = data->backing->grid;
843 u_int cx, cy, wx, wy;
844 int reflow;
846 screen_resize(s, sx, sy, 0);
847 cx = data->cx;
848 cy = gd->hsize + data->cy - data->oy;
849 reflow = (gd->sx != sx);
850 if (reflow)
851 grid_wrap_position(gd, cx, cy, &wx, &wy);
852 screen_resize_cursor(data->backing, sx, sy, 1, 0, 0);
853 if (reflow)
854 grid_unwrap_position(gd, &cx, &cy, wx, wy);
856 data->cx = cx;
857 if (cy < gd->hsize) {
858 data->cy = 0;
859 data->oy = gd->hsize - cy;
860 } else {
861 data->cy = cy - gd->hsize;
862 data->oy = 0;
865 window_copy_size_changed(wme);
866 window_copy_redraw_screen(wme);
869 static const char *
870 window_copy_key_table(struct window_mode_entry *wme)
872 struct window_pane *wp = wme->wp;
874 if (options_get_number(wp->window->options, "mode-keys") == MODEKEY_VI)
875 return ("copy-mode-vi");
876 return ("copy-mode");
879 static int
880 window_copy_expand_search_string(struct window_copy_cmd_state *cs)
882 struct window_mode_entry *wme = cs->wme;
883 struct window_copy_mode_data *data = wme->data;
884 const char *ss = args_string(cs->args, 1);
885 char *expanded;
887 if (ss == NULL || *ss == '\0')
888 return (0);
890 if (args_has(cs->args, 'F')) {
891 expanded = format_single(NULL, ss, NULL, NULL, NULL, wme->wp);
892 if (*expanded == '\0') {
893 free(expanded);
894 return (0);
896 free(data->searchstr);
897 data->searchstr = expanded;
898 } else {
899 free(data->searchstr);
900 data->searchstr = xstrdup(ss);
902 return (1);
905 static enum window_copy_cmd_action
906 window_copy_cmd_append_selection(struct window_copy_cmd_state *cs)
908 struct window_mode_entry *wme = cs->wme;
909 struct session *s = cs->s;
911 if (s != NULL)
912 window_copy_append_selection(wme);
913 window_copy_clear_selection(wme);
914 return (WINDOW_COPY_CMD_REDRAW);
917 static enum window_copy_cmd_action
918 window_copy_cmd_append_selection_and_cancel(struct window_copy_cmd_state *cs)
920 struct window_mode_entry *wme = cs->wme;
921 struct session *s = cs->s;
923 if (s != NULL)
924 window_copy_append_selection(wme);
925 window_copy_clear_selection(wme);
926 return (WINDOW_COPY_CMD_CANCEL);
929 static enum window_copy_cmd_action
930 window_copy_cmd_back_to_indentation(struct window_copy_cmd_state *cs)
932 struct window_mode_entry *wme = cs->wme;
934 window_copy_cursor_back_to_indentation(wme);
935 return (WINDOW_COPY_CMD_NOTHING);
938 static enum window_copy_cmd_action
939 window_copy_cmd_begin_selection(struct window_copy_cmd_state *cs)
941 struct window_mode_entry *wme = cs->wme;
942 struct client *c = cs->c;
943 struct mouse_event *m = cs->m;
944 struct window_copy_mode_data *data = wme->data;
946 if (m != NULL) {
947 window_copy_start_drag(c, m);
948 return (WINDOW_COPY_CMD_NOTHING);
951 data->lineflag = LINE_SEL_NONE;
952 data->selflag = SEL_CHAR;
953 window_copy_start_selection(wme);
954 return (WINDOW_COPY_CMD_REDRAW);
957 static enum window_copy_cmd_action
958 window_copy_cmd_stop_selection(struct window_copy_cmd_state *cs)
960 struct window_mode_entry *wme = cs->wme;
961 struct window_copy_mode_data *data = wme->data;
963 data->cursordrag = CURSORDRAG_NONE;
964 data->lineflag = LINE_SEL_NONE;
965 data->selflag = SEL_CHAR;
966 return (WINDOW_COPY_CMD_NOTHING);
969 static enum window_copy_cmd_action
970 window_copy_cmd_bottom_line(struct window_copy_cmd_state *cs)
972 struct window_mode_entry *wme = cs->wme;
973 struct window_copy_mode_data *data = wme->data;
975 data->cx = 0;
976 data->cy = screen_size_y(&data->screen) - 1;
978 window_copy_update_selection(wme, 1, 0);
979 return (WINDOW_COPY_CMD_REDRAW);
982 static enum window_copy_cmd_action
983 window_copy_cmd_cancel(__unused struct window_copy_cmd_state *cs)
985 return (WINDOW_COPY_CMD_CANCEL);
988 static enum window_copy_cmd_action
989 window_copy_cmd_clear_selection(struct window_copy_cmd_state *cs)
991 struct window_mode_entry *wme = cs->wme;
993 window_copy_clear_selection(wme);
994 return (WINDOW_COPY_CMD_REDRAW);
997 static enum window_copy_cmd_action
998 window_copy_do_copy_end_of_line(struct window_copy_cmd_state *cs, int pipe,
999 int cancel)
1001 struct window_mode_entry *wme = cs->wme;
1002 struct client *c = cs->c;
1003 struct session *s = cs->s;
1004 struct winlink *wl = cs->wl;
1005 struct window_pane *wp = wme->wp;
1006 u_int count = args_count(cs->args);
1007 u_int np = wme->prefix, ocx, ocy, ooy;
1008 struct window_copy_mode_data *data = wme->data;
1009 char *prefix = NULL, *command = NULL;
1010 const char *arg1 = args_string(cs->args, 1);
1011 const char *arg2 = args_string(cs->args, 2);
1013 if (pipe) {
1014 if (count == 3)
1015 prefix = format_single(NULL, arg2, c, s, wl, wp);
1016 if (s != NULL && count > 1 && *arg1 != '\0')
1017 command = format_single(NULL, arg1, c, s, wl, wp);
1018 } else {
1019 if (count == 2)
1020 prefix = format_single(NULL, arg1, c, s, wl, wp);
1023 ocx = data->cx;
1024 ocy = data->cy;
1025 ooy = data->oy;
1027 window_copy_start_selection(wme);
1028 for (; np > 1; np--)
1029 window_copy_cursor_down(wme, 0);
1030 window_copy_cursor_end_of_line(wme);
1032 if (s != NULL) {
1033 if (pipe)
1034 window_copy_copy_pipe(wme, s, prefix, command);
1035 else
1036 window_copy_copy_selection(wme, prefix);
1038 if (cancel) {
1039 free(prefix);
1040 free(command);
1041 return (WINDOW_COPY_CMD_CANCEL);
1044 window_copy_clear_selection(wme);
1046 data->cx = ocx;
1047 data->cy = ocy;
1048 data->oy = ooy;
1050 free(prefix);
1051 free(command);
1052 return (WINDOW_COPY_CMD_REDRAW);
1055 static enum window_copy_cmd_action
1056 window_copy_cmd_copy_end_of_line(struct window_copy_cmd_state *cs)
1058 return (window_copy_do_copy_end_of_line(cs, 0, 0));
1061 static enum window_copy_cmd_action
1062 window_copy_cmd_copy_end_of_line_and_cancel(struct window_copy_cmd_state *cs)
1064 return (window_copy_do_copy_end_of_line(cs, 0, 1));
1067 static enum window_copy_cmd_action
1068 window_copy_cmd_copy_pipe_end_of_line(struct window_copy_cmd_state *cs)
1070 return (window_copy_do_copy_end_of_line(cs, 1, 0));
1073 static enum window_copy_cmd_action
1074 window_copy_cmd_copy_pipe_end_of_line_and_cancel(
1075 struct window_copy_cmd_state *cs)
1077 return (window_copy_do_copy_end_of_line(cs, 1, 1));
1080 static enum window_copy_cmd_action
1081 window_copy_do_copy_line(struct window_copy_cmd_state *cs, int pipe, int cancel)
1083 struct window_mode_entry *wme = cs->wme;
1084 struct client *c = cs->c;
1085 struct session *s = cs->s;
1086 struct winlink *wl = cs->wl;
1087 struct window_pane *wp = wme->wp;
1088 struct window_copy_mode_data *data = wme->data;
1089 u_int count = args_count(cs->args);
1090 u_int np = wme->prefix, ocx, ocy, ooy;
1091 char *prefix = NULL, *command = NULL;
1092 const char *arg1 = args_string(cs->args, 1);
1093 const char *arg2 = args_string(cs->args, 2);
1095 if (pipe) {
1096 if (count == 3)
1097 prefix = format_single(NULL, arg2, c, s, wl, wp);
1098 if (s != NULL && count > 1 && *arg1 != '\0')
1099 command = format_single(NULL, arg1, c, s, wl, wp);
1100 } else {
1101 if (count == 2)
1102 prefix = format_single(NULL, arg1, c, s, wl, wp);
1105 ocx = data->cx;
1106 ocy = data->cy;
1107 ooy = data->oy;
1109 data->selflag = SEL_CHAR;
1110 window_copy_cursor_start_of_line(wme);
1111 window_copy_start_selection(wme);
1112 for (; np > 1; np--)
1113 window_copy_cursor_down(wme, 0);
1114 window_copy_cursor_end_of_line(wme);
1116 if (s != NULL) {
1117 if (pipe)
1118 window_copy_copy_pipe(wme, s, prefix, command);
1119 else
1120 window_copy_copy_selection(wme, prefix);
1122 if (cancel) {
1123 free(prefix);
1124 free(command);
1125 return (WINDOW_COPY_CMD_CANCEL);
1128 window_copy_clear_selection(wme);
1130 data->cx = ocx;
1131 data->cy = ocy;
1132 data->oy = ooy;
1134 free(prefix);
1135 free(command);
1136 return (WINDOW_COPY_CMD_REDRAW);
1139 static enum window_copy_cmd_action
1140 window_copy_cmd_copy_line(struct window_copy_cmd_state *cs)
1142 return (window_copy_do_copy_line(cs, 0, 0));
1145 static enum window_copy_cmd_action
1146 window_copy_cmd_copy_line_and_cancel(struct window_copy_cmd_state *cs)
1148 return (window_copy_do_copy_line(cs, 0, 1));
1151 static enum window_copy_cmd_action
1152 window_copy_cmd_copy_pipe_line(struct window_copy_cmd_state *cs)
1154 return (window_copy_do_copy_line(cs, 1, 0));
1157 static enum window_copy_cmd_action
1158 window_copy_cmd_copy_pipe_line_and_cancel(struct window_copy_cmd_state *cs)
1160 return (window_copy_do_copy_line(cs, 1, 1));
1163 static enum window_copy_cmd_action
1164 window_copy_cmd_copy_selection_no_clear(struct window_copy_cmd_state *cs)
1166 struct window_mode_entry *wme = cs->wme;
1167 struct client *c = cs->c;
1168 struct session *s = cs->s;
1169 struct winlink *wl = cs->wl;
1170 struct window_pane *wp = wme->wp;
1171 char *prefix = NULL;
1172 const char *arg1 = args_string(cs->args, 1);
1174 if (arg1 != NULL)
1175 prefix = format_single(NULL, arg1, c, s, wl, wp);
1177 if (s != NULL)
1178 window_copy_copy_selection(wme, prefix);
1180 free(prefix);
1181 return (WINDOW_COPY_CMD_NOTHING);
1184 static enum window_copy_cmd_action
1185 window_copy_cmd_copy_selection(struct window_copy_cmd_state *cs)
1187 struct window_mode_entry *wme = cs->wme;
1189 window_copy_cmd_copy_selection_no_clear(cs);
1190 window_copy_clear_selection(wme);
1191 return (WINDOW_COPY_CMD_REDRAW);
1194 static enum window_copy_cmd_action
1195 window_copy_cmd_copy_selection_and_cancel(struct window_copy_cmd_state *cs)
1197 struct window_mode_entry *wme = cs->wme;
1199 window_copy_cmd_copy_selection_no_clear(cs);
1200 window_copy_clear_selection(wme);
1201 return (WINDOW_COPY_CMD_CANCEL);
1204 static enum window_copy_cmd_action
1205 window_copy_cmd_cursor_down(struct window_copy_cmd_state *cs)
1207 struct window_mode_entry *wme = cs->wme;
1208 u_int np = wme->prefix;
1210 for (; np != 0; np--)
1211 window_copy_cursor_down(wme, 0);
1212 return (WINDOW_COPY_CMD_NOTHING);
1215 static enum window_copy_cmd_action
1216 window_copy_cmd_cursor_down_and_cancel(struct window_copy_cmd_state *cs)
1218 struct window_mode_entry *wme = cs->wme;
1219 struct window_copy_mode_data *data = wme->data;
1220 u_int np = wme->prefix, cy;
1222 cy = data->cy;
1223 for (; np != 0; np--)
1224 window_copy_cursor_down(wme, 0);
1225 if (cy == data->cy && data->oy == 0)
1226 return (WINDOW_COPY_CMD_CANCEL);
1227 return (WINDOW_COPY_CMD_NOTHING);
1230 static enum window_copy_cmd_action
1231 window_copy_cmd_cursor_left(struct window_copy_cmd_state *cs)
1233 struct window_mode_entry *wme = cs->wme;
1234 u_int np = wme->prefix;
1236 for (; np != 0; np--)
1237 window_copy_cursor_left(wme);
1238 return (WINDOW_COPY_CMD_NOTHING);
1241 static enum window_copy_cmd_action
1242 window_copy_cmd_cursor_right(struct window_copy_cmd_state *cs)
1244 struct window_mode_entry *wme = cs->wme;
1245 struct window_copy_mode_data *data = wme->data;
1246 u_int np = wme->prefix;
1248 for (; np != 0; np--) {
1249 window_copy_cursor_right(wme, data->screen.sel != NULL &&
1250 data->rectflag);
1252 return (WINDOW_COPY_CMD_NOTHING);
1255 /* Scroll line containing the cursor to the given position. */
1256 static enum window_copy_cmd_action
1257 window_copy_cmd_scroll_to(struct window_copy_cmd_state *cs, u_int to)
1259 struct window_mode_entry *wme = cs->wme;
1260 struct window_copy_mode_data *data = wme->data;
1261 u_int oy, delta;
1262 int scroll_up; /* >0 up, <0 down */
1264 scroll_up = data->cy - to;
1265 delta = abs(scroll_up);
1266 oy = screen_hsize(data->backing) - data->oy;
1269 * oy is the maximum scroll down amount, while data->oy is the maximum
1270 * scroll up amount.
1272 if (scroll_up > 0 && data->oy >= delta) {
1273 window_copy_scroll_up(wme, delta);
1274 data->cy -= delta;
1275 } else if (scroll_up < 0 && oy >= delta) {
1276 window_copy_scroll_down(wme, delta);
1277 data->cy += delta;
1280 window_copy_update_selection(wme, 0, 0);
1281 return (WINDOW_COPY_CMD_REDRAW);
1284 /* Scroll line containing the cursor to the bottom. */
1285 static enum window_copy_cmd_action
1286 window_copy_cmd_scroll_bottom(struct window_copy_cmd_state *cs)
1288 struct window_copy_mode_data *data = cs->wme->data;
1289 u_int bottom;
1291 bottom = screen_size_y(&data->screen) - 1;
1292 return (window_copy_cmd_scroll_to(cs, bottom));
1295 /* Scroll line containing the cursor to the middle. */
1296 static enum window_copy_cmd_action
1297 window_copy_cmd_scroll_middle(struct window_copy_cmd_state *cs)
1299 struct window_copy_mode_data *data = cs->wme->data;
1300 u_int mid_value;
1302 mid_value = (screen_size_y(&data->screen) - 1) / 2;
1303 return (window_copy_cmd_scroll_to(cs, mid_value));
1306 /* Scroll line containing the cursor to the top. */
1307 static enum window_copy_cmd_action
1308 window_copy_cmd_scroll_top(struct window_copy_cmd_state *cs)
1310 return (window_copy_cmd_scroll_to(cs, 0));
1313 static enum window_copy_cmd_action
1314 window_copy_cmd_cursor_up(struct window_copy_cmd_state *cs)
1316 struct window_mode_entry *wme = cs->wme;
1317 u_int np = wme->prefix;
1319 for (; np != 0; np--)
1320 window_copy_cursor_up(wme, 0);
1321 return (WINDOW_COPY_CMD_NOTHING);
1324 static enum window_copy_cmd_action
1325 window_copy_cmd_end_of_line(struct window_copy_cmd_state *cs)
1327 struct window_mode_entry *wme = cs->wme;
1329 window_copy_cursor_end_of_line(wme);
1330 return (WINDOW_COPY_CMD_NOTHING);
1333 static enum window_copy_cmd_action
1334 window_copy_cmd_halfpage_down(struct window_copy_cmd_state *cs)
1336 struct window_mode_entry *wme = cs->wme;
1337 struct window_copy_mode_data *data = wme->data;
1338 u_int np = wme->prefix;
1340 for (; np != 0; np--) {
1341 if (window_copy_pagedown(wme, 1, data->scroll_exit))
1342 return (WINDOW_COPY_CMD_CANCEL);
1344 return (WINDOW_COPY_CMD_NOTHING);
1347 static enum window_copy_cmd_action
1348 window_copy_cmd_halfpage_down_and_cancel(struct window_copy_cmd_state *cs)
1351 struct window_mode_entry *wme = cs->wme;
1352 u_int np = wme->prefix;
1354 for (; np != 0; np--) {
1355 if (window_copy_pagedown(wme, 1, 1))
1356 return (WINDOW_COPY_CMD_CANCEL);
1358 return (WINDOW_COPY_CMD_NOTHING);
1361 static enum window_copy_cmd_action
1362 window_copy_cmd_halfpage_up(struct window_copy_cmd_state *cs)
1364 struct window_mode_entry *wme = cs->wme;
1365 u_int np = wme->prefix;
1367 for (; np != 0; np--)
1368 window_copy_pageup1(wme, 1);
1369 return (WINDOW_COPY_CMD_NOTHING);
1372 static enum window_copy_cmd_action
1373 window_copy_cmd_toggle_position(struct window_copy_cmd_state *cs)
1375 struct window_mode_entry *wme = cs->wme;
1376 struct window_copy_mode_data *data = wme->data;
1378 data->hide_position = !data->hide_position;
1379 return (WINDOW_COPY_CMD_REDRAW);
1382 static enum window_copy_cmd_action
1383 window_copy_cmd_history_bottom(struct window_copy_cmd_state *cs)
1385 struct window_mode_entry *wme = cs->wme;
1386 struct window_copy_mode_data *data = wme->data;
1387 struct screen *s = data->backing;
1388 u_int oy;
1390 oy = screen_hsize(s) + data->cy - data->oy;
1391 if (data->lineflag == LINE_SEL_RIGHT_LEFT && oy == data->endsely)
1392 window_copy_other_end(wme);
1394 data->cy = screen_size_y(&data->screen) - 1;
1395 data->cx = window_copy_find_length(wme, screen_hsize(s) + data->cy);
1396 data->oy = 0;
1398 if (data->searchmark != NULL && !data->timeout)
1399 window_copy_search_marks(wme, NULL, data->searchregex, 1);
1400 window_copy_update_selection(wme, 1, 0);
1401 return (WINDOW_COPY_CMD_REDRAW);
1404 static enum window_copy_cmd_action
1405 window_copy_cmd_history_top(struct window_copy_cmd_state *cs)
1407 struct window_mode_entry *wme = cs->wme;
1408 struct window_copy_mode_data *data = wme->data;
1409 u_int oy;
1411 oy = screen_hsize(data->backing) + data->cy - data->oy;
1412 if (data->lineflag == LINE_SEL_LEFT_RIGHT && oy == data->sely)
1413 window_copy_other_end(wme);
1415 data->cy = 0;
1416 data->cx = 0;
1417 data->oy = screen_hsize(data->backing);
1419 if (data->searchmark != NULL && !data->timeout)
1420 window_copy_search_marks(wme, NULL, data->searchregex, 1);
1421 window_copy_update_selection(wme, 1, 0);
1422 return (WINDOW_COPY_CMD_REDRAW);
1425 static enum window_copy_cmd_action
1426 window_copy_cmd_jump_again(struct window_copy_cmd_state *cs)
1428 struct window_mode_entry *wme = cs->wme;
1429 struct window_copy_mode_data *data = wme->data;
1430 u_int np = wme->prefix;
1432 switch (data->jumptype) {
1433 case WINDOW_COPY_JUMPFORWARD:
1434 for (; np != 0; np--)
1435 window_copy_cursor_jump(wme);
1436 break;
1437 case WINDOW_COPY_JUMPBACKWARD:
1438 for (; np != 0; np--)
1439 window_copy_cursor_jump_back(wme);
1440 break;
1441 case WINDOW_COPY_JUMPTOFORWARD:
1442 for (; np != 0; np--)
1443 window_copy_cursor_jump_to(wme);
1444 break;
1445 case WINDOW_COPY_JUMPTOBACKWARD:
1446 for (; np != 0; np--)
1447 window_copy_cursor_jump_to_back(wme);
1448 break;
1450 return (WINDOW_COPY_CMD_NOTHING);
1453 static enum window_copy_cmd_action
1454 window_copy_cmd_jump_reverse(struct window_copy_cmd_state *cs)
1456 struct window_mode_entry *wme = cs->wme;
1457 struct window_copy_mode_data *data = wme->data;
1458 u_int np = wme->prefix;
1460 switch (data->jumptype) {
1461 case WINDOW_COPY_JUMPFORWARD:
1462 for (; np != 0; np--)
1463 window_copy_cursor_jump_back(wme);
1464 break;
1465 case WINDOW_COPY_JUMPBACKWARD:
1466 for (; np != 0; np--)
1467 window_copy_cursor_jump(wme);
1468 break;
1469 case WINDOW_COPY_JUMPTOFORWARD:
1470 for (; np != 0; np--)
1471 window_copy_cursor_jump_to_back(wme);
1472 break;
1473 case WINDOW_COPY_JUMPTOBACKWARD:
1474 for (; np != 0; np--)
1475 window_copy_cursor_jump_to(wme);
1476 break;
1478 return (WINDOW_COPY_CMD_NOTHING);
1481 static enum window_copy_cmd_action
1482 window_copy_cmd_middle_line(struct window_copy_cmd_state *cs)
1484 struct window_mode_entry *wme = cs->wme;
1485 struct window_copy_mode_data *data = wme->data;
1487 data->cx = 0;
1488 data->cy = (screen_size_y(&data->screen) - 1) / 2;
1490 window_copy_update_selection(wme, 1, 0);
1491 return (WINDOW_COPY_CMD_REDRAW);
1494 static enum window_copy_cmd_action
1495 window_copy_cmd_previous_matching_bracket(struct window_copy_cmd_state *cs)
1497 struct window_mode_entry *wme = cs->wme;
1498 u_int np = wme->prefix;
1499 struct window_copy_mode_data *data = wme->data;
1500 struct screen *s = data->backing;
1501 char open[] = "{[(", close[] = "}])";
1502 char tried, found, start, *cp;
1503 u_int px, py, xx, n;
1504 struct grid_cell gc;
1505 int failed;
1507 for (; np != 0; np--) {
1508 /* Get cursor position and line length. */
1509 px = data->cx;
1510 py = screen_hsize(s) + data->cy - data->oy;
1511 xx = window_copy_find_length(wme, py);
1512 if (xx == 0)
1513 break;
1516 * Get the current character. If not on a bracket, try the
1517 * previous. If still not, then behave like previous-word.
1519 tried = 0;
1520 retry:
1521 grid_get_cell(s->grid, px, py, &gc);
1522 if (gc.data.size != 1 || (gc.flags & GRID_FLAG_PADDING))
1523 cp = NULL;
1524 else {
1525 found = *gc.data.data;
1526 cp = strchr(close, found);
1528 if (cp == NULL) {
1529 if (data->modekeys == MODEKEY_EMACS) {
1530 if (!tried && px > 0) {
1531 px--;
1532 tried = 1;
1533 goto retry;
1535 window_copy_cursor_previous_word(wme, close, 1);
1537 continue;
1539 start = open[cp - close];
1541 /* Walk backward until the matching bracket is reached. */
1542 n = 1;
1543 failed = 0;
1544 do {
1545 if (px == 0) {
1546 if (py == 0) {
1547 failed = 1;
1548 break;
1550 do {
1551 py--;
1552 xx = window_copy_find_length(wme, py);
1553 } while (xx == 0 && py > 0);
1554 if (xx == 0 && py == 0) {
1555 failed = 1;
1556 break;
1558 px = xx - 1;
1559 } else
1560 px--;
1562 grid_get_cell(s->grid, px, py, &gc);
1563 if (gc.data.size == 1 &&
1564 (~gc.flags & GRID_FLAG_PADDING)) {
1565 if (*gc.data.data == found)
1566 n++;
1567 else if (*gc.data.data == start)
1568 n--;
1570 } while (n != 0);
1572 /* Move the cursor to the found location if any. */
1573 if (!failed)
1574 window_copy_scroll_to(wme, px, py, 0);
1577 return (WINDOW_COPY_CMD_NOTHING);
1580 static enum window_copy_cmd_action
1581 window_copy_cmd_next_matching_bracket(struct window_copy_cmd_state *cs)
1583 struct window_mode_entry *wme = cs->wme;
1584 u_int np = wme->prefix;
1585 struct window_copy_mode_data *data = wme->data;
1586 struct screen *s = data->backing;
1587 char open[] = "{[(", close[] = "}])";
1588 char tried, found, end, *cp;
1589 u_int px, py, xx, yy, sx, sy, n;
1590 struct grid_cell gc;
1591 int failed;
1592 struct grid_line *gl;
1594 for (; np != 0; np--) {
1595 /* Get cursor position and line length. */
1596 px = data->cx;
1597 py = screen_hsize(s) + data->cy - data->oy;
1598 xx = window_copy_find_length(wme, py);
1599 yy = screen_hsize(s) + screen_size_y(s) - 1;
1600 if (xx == 0)
1601 break;
1604 * Get the current character. If not on a bracket, try the
1605 * next. If still not, then behave like next-word.
1607 tried = 0;
1608 retry:
1609 grid_get_cell(s->grid, px, py, &gc);
1610 if (gc.data.size != 1 || (gc.flags & GRID_FLAG_PADDING))
1611 cp = NULL;
1612 else {
1613 found = *gc.data.data;
1616 * In vi mode, attempt to move to previous bracket if a
1617 * closing bracket is found first. If this fails,
1618 * return to the original cursor position.
1620 cp = strchr(close, found);
1621 if (cp != NULL && data->modekeys == MODEKEY_VI) {
1622 sx = data->cx;
1623 sy = screen_hsize(s) + data->cy - data->oy;
1625 window_copy_scroll_to(wme, px, py, 0);
1626 window_copy_cmd_previous_matching_bracket(cs);
1628 px = data->cx;
1629 py = screen_hsize(s) + data->cy - data->oy;
1630 grid_get_cell(s->grid, px, py, &gc);
1631 if (gc.data.size == 1 &&
1632 (~gc.flags & GRID_FLAG_PADDING) &&
1633 strchr(close, *gc.data.data) != NULL)
1634 window_copy_scroll_to(wme, sx, sy, 0);
1635 break;
1638 cp = strchr(open, found);
1640 if (cp == NULL) {
1641 if (data->modekeys == MODEKEY_EMACS) {
1642 if (!tried && px <= xx) {
1643 px++;
1644 tried = 1;
1645 goto retry;
1647 window_copy_cursor_next_word_end(wme, open, 0);
1648 continue;
1650 /* For vi, continue searching for bracket until EOL. */
1651 if (px > xx) {
1652 if (py == yy)
1653 continue;
1654 gl = grid_get_line(s->grid, py);
1655 if (~gl->flags & GRID_LINE_WRAPPED)
1656 continue;
1657 if (gl->cellsize > s->grid->sx)
1658 continue;
1659 px = 0;
1660 py++;
1661 xx = window_copy_find_length(wme, py);
1662 } else
1663 px++;
1664 goto retry;
1666 end = close[cp - open];
1668 /* Walk forward until the matching bracket is reached. */
1669 n = 1;
1670 failed = 0;
1671 do {
1672 if (px > xx) {
1673 if (py == yy) {
1674 failed = 1;
1675 break;
1677 px = 0;
1678 py++;
1679 xx = window_copy_find_length(wme, py);
1680 } else
1681 px++;
1683 grid_get_cell(s->grid, px, py, &gc);
1684 if (gc.data.size == 1 &&
1685 (~gc.flags & GRID_FLAG_PADDING)) {
1686 if (*gc.data.data == found)
1687 n++;
1688 else if (*gc.data.data == end)
1689 n--;
1691 } while (n != 0);
1693 /* Move the cursor to the found location if any. */
1694 if (!failed)
1695 window_copy_scroll_to(wme, px, py, 0);
1698 return (WINDOW_COPY_CMD_NOTHING);
1701 static enum window_copy_cmd_action
1702 window_copy_cmd_next_paragraph(struct window_copy_cmd_state *cs)
1704 struct window_mode_entry *wme = cs->wme;
1705 u_int np = wme->prefix;
1707 for (; np != 0; np--)
1708 window_copy_next_paragraph(wme);
1709 return (WINDOW_COPY_CMD_NOTHING);
1712 static enum window_copy_cmd_action
1713 window_copy_cmd_next_space(struct window_copy_cmd_state *cs)
1715 struct window_mode_entry *wme = cs->wme;
1716 u_int np = wme->prefix;
1718 for (; np != 0; np--)
1719 window_copy_cursor_next_word(wme, "");
1720 return (WINDOW_COPY_CMD_NOTHING);
1723 static enum window_copy_cmd_action
1724 window_copy_cmd_next_space_end(struct window_copy_cmd_state *cs)
1726 struct window_mode_entry *wme = cs->wme;
1727 u_int np = wme->prefix;
1729 for (; np != 0; np--)
1730 window_copy_cursor_next_word_end(wme, "", 0);
1731 return (WINDOW_COPY_CMD_NOTHING);
1734 static enum window_copy_cmd_action
1735 window_copy_cmd_next_word(struct window_copy_cmd_state *cs)
1737 struct window_mode_entry *wme = cs->wme;
1738 u_int np = wme->prefix;
1739 const char *separators;
1741 separators = options_get_string(cs->s->options, "word-separators");
1743 for (; np != 0; np--)
1744 window_copy_cursor_next_word(wme, separators);
1745 return (WINDOW_COPY_CMD_NOTHING);
1748 static enum window_copy_cmd_action
1749 window_copy_cmd_next_word_end(struct window_copy_cmd_state *cs)
1751 struct window_mode_entry *wme = cs->wme;
1752 u_int np = wme->prefix;
1753 const char *separators;
1755 separators = options_get_string(cs->s->options, "word-separators");
1757 for (; np != 0; np--)
1758 window_copy_cursor_next_word_end(wme, separators, 0);
1759 return (WINDOW_COPY_CMD_NOTHING);
1762 static enum window_copy_cmd_action
1763 window_copy_cmd_other_end(struct window_copy_cmd_state *cs)
1765 struct window_mode_entry *wme = cs->wme;
1766 u_int np = wme->prefix;
1767 struct window_copy_mode_data *data = wme->data;
1769 data->selflag = SEL_CHAR;
1770 if ((np % 2) != 0)
1771 window_copy_other_end(wme);
1772 return (WINDOW_COPY_CMD_NOTHING);
1775 static enum window_copy_cmd_action
1776 window_copy_cmd_page_down(struct window_copy_cmd_state *cs)
1778 struct window_mode_entry *wme = cs->wme;
1779 struct window_copy_mode_data *data = wme->data;
1780 u_int np = wme->prefix;
1782 for (; np != 0; np--) {
1783 if (window_copy_pagedown(wme, 0, data->scroll_exit))
1784 return (WINDOW_COPY_CMD_CANCEL);
1786 return (WINDOW_COPY_CMD_NOTHING);
1789 static enum window_copy_cmd_action
1790 window_copy_cmd_page_down_and_cancel(struct window_copy_cmd_state *cs)
1792 struct window_mode_entry *wme = cs->wme;
1793 u_int np = wme->prefix;
1795 for (; np != 0; np--) {
1796 if (window_copy_pagedown(wme, 0, 1))
1797 return (WINDOW_COPY_CMD_CANCEL);
1799 return (WINDOW_COPY_CMD_NOTHING);
1802 static enum window_copy_cmd_action
1803 window_copy_cmd_page_up(struct window_copy_cmd_state *cs)
1805 struct window_mode_entry *wme = cs->wme;
1806 u_int np = wme->prefix;
1808 for (; np != 0; np--)
1809 window_copy_pageup1(wme, 0);
1810 return (WINDOW_COPY_CMD_NOTHING);
1813 static enum window_copy_cmd_action
1814 window_copy_cmd_previous_paragraph(struct window_copy_cmd_state *cs)
1816 struct window_mode_entry *wme = cs->wme;
1817 u_int np = wme->prefix;
1819 for (; np != 0; np--)
1820 window_copy_previous_paragraph(wme);
1821 return (WINDOW_COPY_CMD_NOTHING);
1824 static enum window_copy_cmd_action
1825 window_copy_cmd_previous_space(struct window_copy_cmd_state *cs)
1827 struct window_mode_entry *wme = cs->wme;
1828 u_int np = wme->prefix;
1830 for (; np != 0; np--)
1831 window_copy_cursor_previous_word(wme, "", 1);
1832 return (WINDOW_COPY_CMD_NOTHING);
1835 static enum window_copy_cmd_action
1836 window_copy_cmd_previous_word(struct window_copy_cmd_state *cs)
1838 struct window_mode_entry *wme = cs->wme;
1839 u_int np = wme->prefix;
1840 const char *separators;
1842 separators = options_get_string(cs->s->options, "word-separators");
1844 for (; np != 0; np--)
1845 window_copy_cursor_previous_word(wme, separators, 1);
1846 return (WINDOW_COPY_CMD_NOTHING);
1849 static enum window_copy_cmd_action
1850 window_copy_cmd_rectangle_on(struct window_copy_cmd_state *cs)
1852 struct window_mode_entry *wme = cs->wme;
1853 struct window_copy_mode_data *data = wme->data;
1855 data->lineflag = LINE_SEL_NONE;
1856 window_copy_rectangle_set(wme, 1);
1858 return (WINDOW_COPY_CMD_NOTHING);
1861 static enum window_copy_cmd_action
1862 window_copy_cmd_rectangle_off(struct window_copy_cmd_state *cs)
1864 struct window_mode_entry *wme = cs->wme;
1865 struct window_copy_mode_data *data = wme->data;
1867 data->lineflag = LINE_SEL_NONE;
1868 window_copy_rectangle_set(wme, 0);
1870 return (WINDOW_COPY_CMD_NOTHING);
1873 static enum window_copy_cmd_action
1874 window_copy_cmd_rectangle_toggle(struct window_copy_cmd_state *cs)
1876 struct window_mode_entry *wme = cs->wme;
1877 struct window_copy_mode_data *data = wme->data;
1879 data->lineflag = LINE_SEL_NONE;
1880 window_copy_rectangle_set(wme, !data->rectflag);
1882 return (WINDOW_COPY_CMD_NOTHING);
1885 static enum window_copy_cmd_action
1886 window_copy_cmd_scroll_down(struct window_copy_cmd_state *cs)
1888 struct window_mode_entry *wme = cs->wme;
1889 struct window_copy_mode_data *data = wme->data;
1890 u_int np = wme->prefix;
1892 for (; np != 0; np--)
1893 window_copy_cursor_down(wme, 1);
1894 if (data->scroll_exit && data->oy == 0)
1895 return (WINDOW_COPY_CMD_CANCEL);
1896 return (WINDOW_COPY_CMD_NOTHING);
1899 static enum window_copy_cmd_action
1900 window_copy_cmd_scroll_down_and_cancel(struct window_copy_cmd_state *cs)
1902 struct window_mode_entry *wme = cs->wme;
1903 struct window_copy_mode_data *data = wme->data;
1904 u_int np = wme->prefix;
1906 for (; np != 0; np--)
1907 window_copy_cursor_down(wme, 1);
1908 if (data->oy == 0)
1909 return (WINDOW_COPY_CMD_CANCEL);
1910 return (WINDOW_COPY_CMD_NOTHING);
1913 static enum window_copy_cmd_action
1914 window_copy_cmd_scroll_up(struct window_copy_cmd_state *cs)
1916 struct window_mode_entry *wme = cs->wme;
1917 u_int np = wme->prefix;
1919 for (; np != 0; np--)
1920 window_copy_cursor_up(wme, 1);
1921 return (WINDOW_COPY_CMD_NOTHING);
1924 static enum window_copy_cmd_action
1925 window_copy_cmd_search_again(struct window_copy_cmd_state *cs)
1927 struct window_mode_entry *wme = cs->wme;
1928 struct window_copy_mode_data *data = wme->data;
1929 u_int np = wme->prefix;
1931 if (data->searchtype == WINDOW_COPY_SEARCHUP) {
1932 for (; np != 0; np--)
1933 window_copy_search_up(wme, data->searchregex);
1934 } else if (data->searchtype == WINDOW_COPY_SEARCHDOWN) {
1935 for (; np != 0; np--)
1936 window_copy_search_down(wme, data->searchregex);
1938 return (WINDOW_COPY_CMD_NOTHING);
1941 static enum window_copy_cmd_action
1942 window_copy_cmd_search_reverse(struct window_copy_cmd_state *cs)
1944 struct window_mode_entry *wme = cs->wme;
1945 struct window_copy_mode_data *data = wme->data;
1946 u_int np = wme->prefix;
1948 if (data->searchtype == WINDOW_COPY_SEARCHUP) {
1949 for (; np != 0; np--)
1950 window_copy_search_down(wme, data->searchregex);
1951 } else if (data->searchtype == WINDOW_COPY_SEARCHDOWN) {
1952 for (; np != 0; np--)
1953 window_copy_search_up(wme, data->searchregex);
1955 return (WINDOW_COPY_CMD_NOTHING);
1958 static enum window_copy_cmd_action
1959 window_copy_cmd_select_line(struct window_copy_cmd_state *cs)
1961 struct window_mode_entry *wme = cs->wme;
1962 struct window_copy_mode_data *data = wme->data;
1963 u_int np = wme->prefix;
1965 data->lineflag = LINE_SEL_LEFT_RIGHT;
1966 data->rectflag = 0;
1967 data->selflag = SEL_LINE;
1968 data->dx = data->cx;
1969 data->dy = screen_hsize(data->backing) + data->cy - data->oy;
1971 window_copy_cursor_start_of_line(wme);
1972 data->selrx = data->cx;
1973 data->selry = screen_hsize(data->backing) + data->cy - data->oy;
1974 data->endselry = data->selry;
1975 window_copy_start_selection(wme);
1976 window_copy_cursor_end_of_line(wme);
1977 data->endselry = screen_hsize(data->backing) + data->cy - data->oy;
1978 data->endselrx = window_copy_find_length(wme, data->endselry);
1979 for (; np > 1; np--) {
1980 window_copy_cursor_down(wme, 0);
1981 window_copy_cursor_end_of_line(wme);
1984 return (WINDOW_COPY_CMD_REDRAW);
1987 static enum window_copy_cmd_action
1988 window_copy_cmd_select_word(struct window_copy_cmd_state *cs)
1990 struct window_mode_entry *wme = cs->wme;
1991 struct options *session_options = cs->s->options;
1992 struct window_copy_mode_data *data = wme->data;
1993 u_int px, py, nextx, nexty;
1995 data->lineflag = LINE_SEL_LEFT_RIGHT;
1996 data->rectflag = 0;
1997 data->selflag = SEL_WORD;
1998 data->dx = data->cx;
1999 data->dy = screen_hsize(data->backing) + data->cy - data->oy;
2001 data->separators = options_get_string(session_options,
2002 "word-separators");
2003 window_copy_cursor_previous_word(wme, data->separators, 0);
2004 px = data->cx;
2005 py = screen_hsize(data->backing) + data->cy - data->oy;
2006 data->selrx = px;
2007 data->selry = py;
2008 window_copy_start_selection(wme);
2010 /* Handle single character words. */
2011 nextx = px + 1;
2012 nexty = py;
2013 if (grid_get_line(data->backing->grid, nexty)->flags &
2014 GRID_LINE_WRAPPED && nextx > screen_size_x(data->backing) - 1) {
2015 nextx = 0;
2016 nexty++;
2018 if (px >= window_copy_find_length(wme, py) ||
2019 !window_copy_in_set(wme, nextx, nexty, WHITESPACE))
2020 window_copy_cursor_next_word_end(wme, data->separators, 1);
2021 else {
2022 window_copy_update_cursor(wme, px, data->cy);
2023 if (window_copy_update_selection(wme, 1, 1))
2024 window_copy_redraw_lines(wme, data->cy, 1);
2026 data->endselrx = data->cx;
2027 data->endselry = screen_hsize(data->backing) + data->cy - data->oy;
2028 if (data->dy > data->endselry) {
2029 data->dy = data->endselry;
2030 data->dx = data->endselrx;
2031 } else if (data->dx > data->endselrx)
2032 data->dx = data->endselrx;
2034 return (WINDOW_COPY_CMD_REDRAW);
2037 static enum window_copy_cmd_action
2038 window_copy_cmd_set_mark(struct window_copy_cmd_state *cs)
2040 struct window_copy_mode_data *data = cs->wme->data;
2042 data->mx = data->cx;
2043 data->my = screen_hsize(data->backing) + data->cy - data->oy;
2044 data->showmark = 1;
2045 return (WINDOW_COPY_CMD_REDRAW);
2048 static enum window_copy_cmd_action
2049 window_copy_cmd_start_of_line(struct window_copy_cmd_state *cs)
2051 struct window_mode_entry *wme = cs->wme;
2053 window_copy_cursor_start_of_line(wme);
2054 return (WINDOW_COPY_CMD_NOTHING);
2057 static enum window_copy_cmd_action
2058 window_copy_cmd_top_line(struct window_copy_cmd_state *cs)
2060 struct window_mode_entry *wme = cs->wme;
2061 struct window_copy_mode_data *data = wme->data;
2063 data->cx = 0;
2064 data->cy = 0;
2066 window_copy_update_selection(wme, 1, 0);
2067 return (WINDOW_COPY_CMD_REDRAW);
2070 static enum window_copy_cmd_action
2071 window_copy_cmd_copy_pipe_no_clear(struct window_copy_cmd_state *cs)
2073 struct window_mode_entry *wme = cs->wme;
2074 struct client *c = cs->c;
2075 struct session *s = cs->s;
2076 struct winlink *wl = cs->wl;
2077 struct window_pane *wp = wme->wp;
2078 char *command = NULL, *prefix = NULL;
2079 const char *arg1 = args_string(cs->args, 1);
2080 const char *arg2 = args_string(cs->args, 2);
2082 if (arg2 != NULL)
2083 prefix = format_single(NULL, arg2, c, s, wl, wp);
2085 if (s != NULL && arg1 != NULL && *arg1 != '\0')
2086 command = format_single(NULL, arg1, c, s, wl, wp);
2087 window_copy_copy_pipe(wme, s, prefix, command);
2088 free(command);
2090 free(prefix);
2091 return (WINDOW_COPY_CMD_NOTHING);
2094 static enum window_copy_cmd_action
2095 window_copy_cmd_copy_pipe(struct window_copy_cmd_state *cs)
2097 struct window_mode_entry *wme = cs->wme;
2099 window_copy_cmd_copy_pipe_no_clear(cs);
2100 window_copy_clear_selection(wme);
2101 return (WINDOW_COPY_CMD_REDRAW);
2104 static enum window_copy_cmd_action
2105 window_copy_cmd_copy_pipe_and_cancel(struct window_copy_cmd_state *cs)
2107 struct window_mode_entry *wme = cs->wme;
2109 window_copy_cmd_copy_pipe_no_clear(cs);
2110 window_copy_clear_selection(wme);
2111 return (WINDOW_COPY_CMD_CANCEL);
2114 static enum window_copy_cmd_action
2115 window_copy_cmd_pipe_no_clear(struct window_copy_cmd_state *cs)
2117 struct window_mode_entry *wme = cs->wme;
2118 struct client *c = cs->c;
2119 struct session *s = cs->s;
2120 struct winlink *wl = cs->wl;
2121 struct window_pane *wp = wme->wp;
2122 char *command = NULL;
2123 const char *arg1 = args_string(cs->args, 1);
2125 if (s != NULL && arg1 != NULL && *arg1 != '\0')
2126 command = format_single(NULL, arg1, c, s, wl, wp);
2127 window_copy_pipe(wme, s, command);
2128 free(command);
2130 return (WINDOW_COPY_CMD_NOTHING);
2133 static enum window_copy_cmd_action
2134 window_copy_cmd_pipe(struct window_copy_cmd_state *cs)
2136 struct window_mode_entry *wme = cs->wme;
2138 window_copy_cmd_pipe_no_clear(cs);
2139 window_copy_clear_selection(wme);
2140 return (WINDOW_COPY_CMD_REDRAW);
2143 static enum window_copy_cmd_action
2144 window_copy_cmd_pipe_and_cancel(struct window_copy_cmd_state *cs)
2146 struct window_mode_entry *wme = cs->wme;
2148 window_copy_cmd_pipe_no_clear(cs);
2149 window_copy_clear_selection(wme);
2150 return (WINDOW_COPY_CMD_CANCEL);
2153 static enum window_copy_cmd_action
2154 window_copy_cmd_goto_line(struct window_copy_cmd_state *cs)
2156 struct window_mode_entry *wme = cs->wme;
2157 const char *arg1 = args_string(cs->args, 1);
2159 if (*arg1 != '\0')
2160 window_copy_goto_line(wme, arg1);
2161 return (WINDOW_COPY_CMD_NOTHING);
2164 static enum window_copy_cmd_action
2165 window_copy_cmd_jump_backward(struct window_copy_cmd_state *cs)
2167 struct window_mode_entry *wme = cs->wme;
2168 struct window_copy_mode_data *data = wme->data;
2169 u_int np = wme->prefix;
2170 const char *arg1 = args_string(cs->args, 1);
2172 if (*arg1 != '\0') {
2173 data->jumptype = WINDOW_COPY_JUMPBACKWARD;
2174 free(data->jumpchar);
2175 data->jumpchar = utf8_fromcstr(arg1);
2176 for (; np != 0; np--)
2177 window_copy_cursor_jump_back(wme);
2179 return (WINDOW_COPY_CMD_NOTHING);
2182 static enum window_copy_cmd_action
2183 window_copy_cmd_jump_forward(struct window_copy_cmd_state *cs)
2185 struct window_mode_entry *wme = cs->wme;
2186 struct window_copy_mode_data *data = wme->data;
2187 u_int np = wme->prefix;
2188 const char *arg1 = args_string(cs->args, 1);
2190 if (*arg1 != '\0') {
2191 data->jumptype = WINDOW_COPY_JUMPFORWARD;
2192 free(data->jumpchar);
2193 data->jumpchar = utf8_fromcstr(arg1);
2194 for (; np != 0; np--)
2195 window_copy_cursor_jump(wme);
2197 return (WINDOW_COPY_CMD_NOTHING);
2200 static enum window_copy_cmd_action
2201 window_copy_cmd_jump_to_backward(struct window_copy_cmd_state *cs)
2203 struct window_mode_entry *wme = cs->wme;
2204 struct window_copy_mode_data *data = wme->data;
2205 u_int np = wme->prefix;
2206 const char *arg1 = args_string(cs->args, 1);
2208 if (*arg1 != '\0') {
2209 data->jumptype = WINDOW_COPY_JUMPTOBACKWARD;
2210 free(data->jumpchar);
2211 data->jumpchar = utf8_fromcstr(arg1);
2212 for (; np != 0; np--)
2213 window_copy_cursor_jump_to_back(wme);
2215 return (WINDOW_COPY_CMD_NOTHING);
2218 static enum window_copy_cmd_action
2219 window_copy_cmd_jump_to_forward(struct window_copy_cmd_state *cs)
2221 struct window_mode_entry *wme = cs->wme;
2222 struct window_copy_mode_data *data = wme->data;
2223 u_int np = wme->prefix;
2224 const char *arg1 = args_string(cs->args, 1);
2226 if (*arg1 != '\0') {
2227 data->jumptype = WINDOW_COPY_JUMPTOFORWARD;
2228 free(data->jumpchar);
2229 data->jumpchar = utf8_fromcstr(arg1);
2230 for (; np != 0; np--)
2231 window_copy_cursor_jump_to(wme);
2233 return (WINDOW_COPY_CMD_NOTHING);
2236 static enum window_copy_cmd_action
2237 window_copy_cmd_jump_to_mark(struct window_copy_cmd_state *cs)
2239 struct window_mode_entry *wme = cs->wme;
2241 window_copy_jump_to_mark(wme);
2242 return (WINDOW_COPY_CMD_NOTHING);
2245 static enum window_copy_cmd_action
2246 window_copy_cmd_next_prompt(struct window_copy_cmd_state *cs)
2248 struct window_mode_entry *wme = cs->wme;
2249 const char *arg1 = args_string(cs->args, 1);
2251 window_copy_cursor_prompt(wme, 1, arg1);
2252 return (WINDOW_COPY_CMD_NOTHING);
2255 static enum window_copy_cmd_action
2256 window_copy_cmd_previous_prompt(struct window_copy_cmd_state *cs)
2258 struct window_mode_entry *wme = cs->wme;
2259 const char *arg1 = args_string(cs->args, 1);
2261 window_copy_cursor_prompt(wme, 0, arg1);
2262 return (WINDOW_COPY_CMD_NOTHING);
2265 static enum window_copy_cmd_action
2266 window_copy_cmd_search_backward(struct window_copy_cmd_state *cs)
2268 struct window_mode_entry *wme = cs->wme;
2269 struct window_copy_mode_data *data = wme->data;
2270 u_int np = wme->prefix;
2272 if (!window_copy_expand_search_string(cs))
2273 return (WINDOW_COPY_CMD_NOTHING);
2275 if (data->searchstr != NULL) {
2276 data->searchtype = WINDOW_COPY_SEARCHUP;
2277 data->searchregex = 1;
2278 data->timeout = 0;
2279 for (; np != 0; np--)
2280 window_copy_search_up(wme, 1);
2282 return (WINDOW_COPY_CMD_NOTHING);
2285 static enum window_copy_cmd_action
2286 window_copy_cmd_search_backward_text(struct window_copy_cmd_state *cs)
2288 struct window_mode_entry *wme = cs->wme;
2289 struct window_copy_mode_data *data = wme->data;
2290 u_int np = wme->prefix;
2292 if (!window_copy_expand_search_string(cs))
2293 return (WINDOW_COPY_CMD_NOTHING);
2295 if (data->searchstr != NULL) {
2296 data->searchtype = WINDOW_COPY_SEARCHUP;
2297 data->searchregex = 0;
2298 data->timeout = 0;
2299 for (; np != 0; np--)
2300 window_copy_search_up(wme, 0);
2302 return (WINDOW_COPY_CMD_NOTHING);
2305 static enum window_copy_cmd_action
2306 window_copy_cmd_search_forward(struct window_copy_cmd_state *cs)
2308 struct window_mode_entry *wme = cs->wme;
2309 struct window_copy_mode_data *data = wme->data;
2310 u_int np = wme->prefix;
2312 if (!window_copy_expand_search_string(cs))
2313 return (WINDOW_COPY_CMD_NOTHING);
2315 if (data->searchstr != NULL) {
2316 data->searchtype = WINDOW_COPY_SEARCHDOWN;
2317 data->searchregex = 1;
2318 data->timeout = 0;
2319 for (; np != 0; np--)
2320 window_copy_search_down(wme, 1);
2322 return (WINDOW_COPY_CMD_NOTHING);
2325 static enum window_copy_cmd_action
2326 window_copy_cmd_search_forward_text(struct window_copy_cmd_state *cs)
2328 struct window_mode_entry *wme = cs->wme;
2329 struct window_copy_mode_data *data = wme->data;
2330 u_int np = wme->prefix;
2332 if (!window_copy_expand_search_string(cs))
2333 return (WINDOW_COPY_CMD_NOTHING);
2335 if (data->searchstr != NULL) {
2336 data->searchtype = WINDOW_COPY_SEARCHDOWN;
2337 data->searchregex = 0;
2338 data->timeout = 0;
2339 for (; np != 0; np--)
2340 window_copy_search_down(wme, 0);
2342 return (WINDOW_COPY_CMD_NOTHING);
2345 static enum window_copy_cmd_action
2346 window_copy_cmd_search_backward_incremental(struct window_copy_cmd_state *cs)
2348 struct window_mode_entry *wme = cs->wme;
2349 struct window_copy_mode_data *data = wme->data;
2350 const char *arg1 = args_string(cs->args, 1);
2351 const char *ss = data->searchstr;
2352 char prefix;
2353 enum window_copy_cmd_action action = WINDOW_COPY_CMD_NOTHING;
2355 data->timeout = 0;
2357 log_debug("%s: %s", __func__, arg1);
2359 prefix = *arg1++;
2360 if (data->searchx == -1 || data->searchy == -1) {
2361 data->searchx = data->cx;
2362 data->searchy = data->cy;
2363 data->searcho = data->oy;
2364 } else if (ss != NULL && strcmp(arg1, ss) != 0) {
2365 data->cx = data->searchx;
2366 data->cy = data->searchy;
2367 data->oy = data->searcho;
2368 action = WINDOW_COPY_CMD_REDRAW;
2370 if (*arg1 == '\0') {
2371 window_copy_clear_marks(wme);
2372 return (WINDOW_COPY_CMD_REDRAW);
2374 switch (prefix) {
2375 case '=':
2376 case '-':
2377 data->searchtype = WINDOW_COPY_SEARCHUP;
2378 data->searchregex = 0;
2379 free(data->searchstr);
2380 data->searchstr = xstrdup(arg1);
2381 if (!window_copy_search_up(wme, 0)) {
2382 window_copy_clear_marks(wme);
2383 return (WINDOW_COPY_CMD_REDRAW);
2385 break;
2386 case '+':
2387 data->searchtype = WINDOW_COPY_SEARCHDOWN;
2388 data->searchregex = 0;
2389 free(data->searchstr);
2390 data->searchstr = xstrdup(arg1);
2391 if (!window_copy_search_down(wme, 0)) {
2392 window_copy_clear_marks(wme);
2393 return (WINDOW_COPY_CMD_REDRAW);
2395 break;
2397 return (action);
2400 static enum window_copy_cmd_action
2401 window_copy_cmd_search_forward_incremental(struct window_copy_cmd_state *cs)
2403 struct window_mode_entry *wme = cs->wme;
2404 struct window_copy_mode_data *data = wme->data;
2405 const char *arg1 = args_string(cs->args, 1);
2406 const char *ss = data->searchstr;
2407 char prefix;
2408 enum window_copy_cmd_action action = WINDOW_COPY_CMD_NOTHING;
2410 data->timeout = 0;
2412 log_debug("%s: %s", __func__, arg1);
2414 prefix = *arg1++;
2415 if (data->searchx == -1 || data->searchy == -1) {
2416 data->searchx = data->cx;
2417 data->searchy = data->cy;
2418 data->searcho = data->oy;
2419 } else if (ss != NULL && strcmp(arg1, ss) != 0) {
2420 data->cx = data->searchx;
2421 data->cy = data->searchy;
2422 data->oy = data->searcho;
2423 action = WINDOW_COPY_CMD_REDRAW;
2425 if (*arg1 == '\0') {
2426 window_copy_clear_marks(wme);
2427 return (WINDOW_COPY_CMD_REDRAW);
2429 switch (prefix) {
2430 case '=':
2431 case '+':
2432 data->searchtype = WINDOW_COPY_SEARCHDOWN;
2433 data->searchregex = 0;
2434 free(data->searchstr);
2435 data->searchstr = xstrdup(arg1);
2436 if (!window_copy_search_down(wme, 0)) {
2437 window_copy_clear_marks(wme);
2438 return (WINDOW_COPY_CMD_REDRAW);
2440 break;
2441 case '-':
2442 data->searchtype = WINDOW_COPY_SEARCHUP;
2443 data->searchregex = 0;
2444 free(data->searchstr);
2445 data->searchstr = xstrdup(arg1);
2446 if (!window_copy_search_up(wme, 0)) {
2447 window_copy_clear_marks(wme);
2448 return (WINDOW_COPY_CMD_REDRAW);
2451 return (action);
2454 static enum window_copy_cmd_action
2455 window_copy_cmd_refresh_from_pane(struct window_copy_cmd_state *cs)
2457 struct window_mode_entry *wme = cs->wme;
2458 struct window_pane *wp = wme->swp;
2459 struct window_copy_mode_data *data = wme->data;
2461 if (data->viewmode)
2462 return (WINDOW_COPY_CMD_NOTHING);
2464 screen_free(data->backing);
2465 free(data->backing);
2466 data->backing = window_copy_clone_screen(&wp->base, &data->screen, NULL, NULL, wme->swp != wme->wp);
2468 window_copy_size_changed(wme);
2469 return (WINDOW_COPY_CMD_REDRAW);
2472 static const struct {
2473 const char *command;
2474 u_int minargs;
2475 u_int maxargs;
2476 enum window_copy_cmd_clear clear;
2477 enum window_copy_cmd_action (*f)(struct window_copy_cmd_state *);
2478 } window_copy_cmd_table[] = {
2479 { .command = "append-selection",
2480 .minargs = 0,
2481 .maxargs = 0,
2482 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2483 .f = window_copy_cmd_append_selection
2485 { .command = "append-selection-and-cancel",
2486 .minargs = 0,
2487 .maxargs = 0,
2488 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2489 .f = window_copy_cmd_append_selection_and_cancel
2491 { .command = "back-to-indentation",
2492 .minargs = 0,
2493 .maxargs = 0,
2494 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2495 .f = window_copy_cmd_back_to_indentation
2497 { .command = "begin-selection",
2498 .minargs = 0,
2499 .maxargs = 0,
2500 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2501 .f = window_copy_cmd_begin_selection
2503 { .command = "bottom-line",
2504 .minargs = 0,
2505 .maxargs = 0,
2506 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2507 .f = window_copy_cmd_bottom_line
2509 { .command = "cancel",
2510 .minargs = 0,
2511 .maxargs = 0,
2512 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2513 .f = window_copy_cmd_cancel
2515 { .command = "clear-selection",
2516 .minargs = 0,
2517 .maxargs = 0,
2518 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2519 .f = window_copy_cmd_clear_selection
2521 { .command = "copy-end-of-line",
2522 .minargs = 0,
2523 .maxargs = 1,
2524 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2525 .f = window_copy_cmd_copy_end_of_line
2527 { .command = "copy-end-of-line-and-cancel",
2528 .minargs = 0,
2529 .maxargs = 1,
2530 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2531 .f = window_copy_cmd_copy_end_of_line_and_cancel
2533 { .command = "copy-pipe-end-of-line",
2534 .minargs = 0,
2535 .maxargs = 2,
2536 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2537 .f = window_copy_cmd_copy_pipe_end_of_line
2539 { .command = "copy-pipe-end-of-line-and-cancel",
2540 .minargs = 0,
2541 .maxargs = 2,
2542 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2543 .f = window_copy_cmd_copy_pipe_end_of_line_and_cancel
2545 { .command = "copy-line",
2546 .minargs = 0,
2547 .maxargs = 1,
2548 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2549 .f = window_copy_cmd_copy_line
2551 { .command = "copy-line-and-cancel",
2552 .minargs = 0,
2553 .maxargs = 1,
2554 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2555 .f = window_copy_cmd_copy_line_and_cancel
2557 { .command = "copy-pipe-line",
2558 .minargs = 0,
2559 .maxargs = 2,
2560 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2561 .f = window_copy_cmd_copy_pipe_line
2563 { .command = "copy-pipe-line-and-cancel",
2564 .minargs = 0,
2565 .maxargs = 2,
2566 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2567 .f = window_copy_cmd_copy_pipe_line_and_cancel
2569 { .command = "copy-pipe-no-clear",
2570 .minargs = 0,
2571 .maxargs = 2,
2572 .clear = WINDOW_COPY_CMD_CLEAR_NEVER,
2573 .f = window_copy_cmd_copy_pipe_no_clear
2575 { .command = "copy-pipe",
2576 .minargs = 0,
2577 .maxargs = 2,
2578 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2579 .f = window_copy_cmd_copy_pipe
2581 { .command = "copy-pipe-and-cancel",
2582 .minargs = 0,
2583 .maxargs = 2,
2584 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2585 .f = window_copy_cmd_copy_pipe_and_cancel
2587 { .command = "copy-selection-no-clear",
2588 .minargs = 0,
2589 .maxargs = 1,
2590 .clear = WINDOW_COPY_CMD_CLEAR_NEVER,
2591 .f = window_copy_cmd_copy_selection_no_clear
2593 { .command = "copy-selection",
2594 .minargs = 0,
2595 .maxargs = 1,
2596 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2597 .f = window_copy_cmd_copy_selection
2599 { .command = "copy-selection-and-cancel",
2600 .minargs = 0,
2601 .maxargs = 1,
2602 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2603 .f = window_copy_cmd_copy_selection_and_cancel
2605 { .command = "cursor-down",
2606 .minargs = 0,
2607 .maxargs = 0,
2608 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2609 .f = window_copy_cmd_cursor_down
2611 { .command = "cursor-down-and-cancel",
2612 .minargs = 0,
2613 .maxargs = 0,
2614 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2615 .f = window_copy_cmd_cursor_down_and_cancel
2617 { .command = "cursor-left",
2618 .minargs = 0,
2619 .maxargs = 0,
2620 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2621 .f = window_copy_cmd_cursor_left
2623 { .command = "cursor-right",
2624 .minargs = 0,
2625 .maxargs = 0,
2626 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2627 .f = window_copy_cmd_cursor_right
2629 { .command = "cursor-up",
2630 .minargs = 0,
2631 .maxargs = 0,
2632 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2633 .f = window_copy_cmd_cursor_up
2635 { .command = "end-of-line",
2636 .minargs = 0,
2637 .maxargs = 0,
2638 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2639 .f = window_copy_cmd_end_of_line
2641 { .command = "goto-line",
2642 .minargs = 1,
2643 .maxargs = 1,
2644 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2645 .f = window_copy_cmd_goto_line
2647 { .command = "halfpage-down",
2648 .minargs = 0,
2649 .maxargs = 0,
2650 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2651 .f = window_copy_cmd_halfpage_down
2653 { .command = "halfpage-down-and-cancel",
2654 .minargs = 0,
2655 .maxargs = 0,
2656 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2657 .f = window_copy_cmd_halfpage_down_and_cancel
2659 { .command = "halfpage-up",
2660 .minargs = 0,
2661 .maxargs = 0,
2662 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2663 .f = window_copy_cmd_halfpage_up
2665 { .command = "history-bottom",
2666 .minargs = 0,
2667 .maxargs = 0,
2668 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2669 .f = window_copy_cmd_history_bottom
2671 { .command = "history-top",
2672 .minargs = 0,
2673 .maxargs = 0,
2674 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2675 .f = window_copy_cmd_history_top
2677 { .command = "jump-again",
2678 .minargs = 0,
2679 .maxargs = 0,
2680 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2681 .f = window_copy_cmd_jump_again
2683 { .command = "jump-backward",
2684 .minargs = 1,
2685 .maxargs = 1,
2686 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2687 .f = window_copy_cmd_jump_backward
2689 { .command = "jump-forward",
2690 .minargs = 1,
2691 .maxargs = 1,
2692 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2693 .f = window_copy_cmd_jump_forward
2695 { .command = "jump-reverse",
2696 .minargs = 0,
2697 .maxargs = 0,
2698 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2699 .f = window_copy_cmd_jump_reverse
2701 { .command = "jump-to-backward",
2702 .minargs = 1,
2703 .maxargs = 1,
2704 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2705 .f = window_copy_cmd_jump_to_backward
2707 { .command = "jump-to-forward",
2708 .minargs = 1,
2709 .maxargs = 1,
2710 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2711 .f = window_copy_cmd_jump_to_forward
2713 { .command = "jump-to-mark",
2714 .minargs = 0,
2715 .maxargs = 0,
2716 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2717 .f = window_copy_cmd_jump_to_mark
2719 { .command = "next-prompt",
2720 .minargs = 0,
2721 .maxargs = 0,
2722 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2723 .f = window_copy_cmd_next_prompt
2725 { .command = "previous-prompt",
2726 .minargs = 0,
2727 .maxargs = 1,
2728 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2729 .f = window_copy_cmd_previous_prompt
2731 { .command = "middle-line",
2732 .minargs = 0,
2733 .maxargs = 0,
2734 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2735 .f = window_copy_cmd_middle_line
2737 { .command = "next-matching-bracket",
2738 .minargs = 0,
2739 .maxargs = 0,
2740 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2741 .f = window_copy_cmd_next_matching_bracket
2743 { .command = "next-paragraph",
2744 .minargs = 0,
2745 .maxargs = 0,
2746 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2747 .f = window_copy_cmd_next_paragraph
2749 { .command = "next-space",
2750 .minargs = 0,
2751 .maxargs = 0,
2752 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2753 .f = window_copy_cmd_next_space
2755 { .command = "next-space-end",
2756 .minargs = 0,
2757 .maxargs = 0,
2758 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2759 .f = window_copy_cmd_next_space_end
2761 { .command = "next-word",
2762 .minargs = 0,
2763 .maxargs = 0,
2764 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2765 .f = window_copy_cmd_next_word
2767 { .command = "next-word-end",
2768 .minargs = 0,
2769 .maxargs = 0,
2770 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2771 .f = window_copy_cmd_next_word_end
2773 { .command = "other-end",
2774 .minargs = 0,
2775 .maxargs = 0,
2776 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2777 .f = window_copy_cmd_other_end
2779 { .command = "page-down",
2780 .minargs = 0,
2781 .maxargs = 0,
2782 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2783 .f = window_copy_cmd_page_down
2785 { .command = "page-down-and-cancel",
2786 .minargs = 0,
2787 .maxargs = 0,
2788 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2789 .f = window_copy_cmd_page_down_and_cancel
2791 { .command = "page-up",
2792 .minargs = 0,
2793 .maxargs = 0,
2794 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2795 .f = window_copy_cmd_page_up
2797 { .command = "pipe-no-clear",
2798 .minargs = 0,
2799 .maxargs = 1,
2800 .clear = WINDOW_COPY_CMD_CLEAR_NEVER,
2801 .f = window_copy_cmd_pipe_no_clear
2803 { .command = "pipe",
2804 .minargs = 0,
2805 .maxargs = 1,
2806 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2807 .f = window_copy_cmd_pipe
2809 { .command = "pipe-and-cancel",
2810 .minargs = 0,
2811 .maxargs = 1,
2812 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2813 .f = window_copy_cmd_pipe_and_cancel
2815 { .command = "previous-matching-bracket",
2816 .minargs = 0,
2817 .maxargs = 0,
2818 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2819 .f = window_copy_cmd_previous_matching_bracket
2821 { .command = "previous-paragraph",
2822 .minargs = 0,
2823 .maxargs = 0,
2824 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2825 .f = window_copy_cmd_previous_paragraph
2827 { .command = "previous-space",
2828 .minargs = 0,
2829 .maxargs = 0,
2830 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2831 .f = window_copy_cmd_previous_space
2833 { .command = "previous-word",
2834 .minargs = 0,
2835 .maxargs = 0,
2836 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2837 .f = window_copy_cmd_previous_word
2839 { .command = "rectangle-on",
2840 .minargs = 0,
2841 .maxargs = 0,
2842 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2843 .f = window_copy_cmd_rectangle_on
2845 { .command = "rectangle-off",
2846 .minargs = 0,
2847 .maxargs = 0,
2848 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2849 .f = window_copy_cmd_rectangle_off
2851 { .command = "rectangle-toggle",
2852 .minargs = 0,
2853 .maxargs = 0,
2854 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2855 .f = window_copy_cmd_rectangle_toggle
2857 { .command = "refresh-from-pane",
2858 .minargs = 0,
2859 .maxargs = 0,
2860 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2861 .f = window_copy_cmd_refresh_from_pane
2863 { .command = "scroll-bottom",
2864 .minargs = 0,
2865 .maxargs = 0,
2866 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2867 .f = window_copy_cmd_scroll_bottom
2869 { .command = "scroll-down",
2870 .minargs = 0,
2871 .maxargs = 0,
2872 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2873 .f = window_copy_cmd_scroll_down
2875 { .command = "scroll-down-and-cancel",
2876 .minargs = 0,
2877 .maxargs = 0,
2878 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2879 .f = window_copy_cmd_scroll_down_and_cancel
2881 { .command = "scroll-middle",
2882 .minargs = 0,
2883 .maxargs = 0,
2884 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2885 .f = window_copy_cmd_scroll_middle
2887 { .command = "scroll-top",
2888 .minargs = 0,
2889 .maxargs = 0,
2890 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2891 .f = window_copy_cmd_scroll_top
2893 { .command = "scroll-up",
2894 .minargs = 0,
2895 .maxargs = 0,
2896 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2897 .f = window_copy_cmd_scroll_up
2899 { .command = "search-again",
2900 .minargs = 0,
2901 .maxargs = 0,
2902 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2903 .f = window_copy_cmd_search_again
2905 { .command = "search-backward",
2906 .minargs = 0,
2907 .maxargs = 1,
2908 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2909 .f = window_copy_cmd_search_backward
2911 { .command = "search-backward-text",
2912 .minargs = 0,
2913 .maxargs = 1,
2914 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2915 .f = window_copy_cmd_search_backward_text
2917 { .command = "search-backward-incremental",
2918 .minargs = 1,
2919 .maxargs = 1,
2920 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2921 .f = window_copy_cmd_search_backward_incremental
2923 { .command = "search-forward",
2924 .minargs = 0,
2925 .maxargs = 1,
2926 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2927 .f = window_copy_cmd_search_forward
2929 { .command = "search-forward-text",
2930 .minargs = 0,
2931 .maxargs = 1,
2932 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2933 .f = window_copy_cmd_search_forward_text
2935 { .command = "search-forward-incremental",
2936 .minargs = 1,
2937 .maxargs = 1,
2938 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2939 .f = window_copy_cmd_search_forward_incremental
2941 { .command = "search-reverse",
2942 .minargs = 0,
2943 .maxargs = 0,
2944 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2945 .f = window_copy_cmd_search_reverse
2947 { .command = "select-line",
2948 .minargs = 0,
2949 .maxargs = 0,
2950 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2951 .f = window_copy_cmd_select_line
2953 { .command = "select-word",
2954 .minargs = 0,
2955 .maxargs = 0,
2956 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2957 .f = window_copy_cmd_select_word
2959 { .command = "set-mark",
2960 .minargs = 0,
2961 .maxargs = 0,
2962 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2963 .f = window_copy_cmd_set_mark
2965 { .command = "start-of-line",
2966 .minargs = 0,
2967 .maxargs = 0,
2968 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2969 .f = window_copy_cmd_start_of_line
2971 { .command = "stop-selection",
2972 .minargs = 0,
2973 .maxargs = 0,
2974 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2975 .f = window_copy_cmd_stop_selection
2977 { .command = "toggle-position",
2978 .minargs = 0,
2979 .maxargs = 0,
2980 .clear = WINDOW_COPY_CMD_CLEAR_NEVER,
2981 .f = window_copy_cmd_toggle_position
2983 { .command = "top-line",
2984 .minargs = 0,
2985 .maxargs = 0,
2986 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2987 .f = window_copy_cmd_top_line
2991 static void
2992 window_copy_command(struct window_mode_entry *wme, struct client *c,
2993 struct session *s, struct winlink *wl, struct args *args,
2994 struct mouse_event *m)
2996 struct window_copy_mode_data *data = wme->data;
2997 struct window_copy_cmd_state cs;
2998 enum window_copy_cmd_action action;
2999 enum window_copy_cmd_clear clear = WINDOW_COPY_CMD_CLEAR_NEVER;
3000 const char *command;
3001 u_int i, count = args_count(args);
3002 int keys;
3004 if (count == 0)
3005 return;
3006 command = args_string(args, 0);
3008 if (m != NULL && m->valid && !MOUSE_WHEEL(m->b))
3009 window_copy_move_mouse(m);
3011 cs.wme = wme;
3012 cs.args = args;
3013 cs.m = m;
3015 cs.c = c;
3016 cs.s = s;
3017 cs.wl = wl;
3019 action = WINDOW_COPY_CMD_NOTHING;
3020 for (i = 0; i < nitems(window_copy_cmd_table); i++) {
3021 if (strcmp(window_copy_cmd_table[i].command, command) == 0) {
3022 if (count - 1 < window_copy_cmd_table[i].minargs ||
3023 count - 1 > window_copy_cmd_table[i].maxargs)
3024 break;
3025 clear = window_copy_cmd_table[i].clear;
3026 action = window_copy_cmd_table[i].f(&cs);
3027 break;
3031 if (strncmp(command, "search-", 7) != 0 && data->searchmark != NULL) {
3032 keys = options_get_number(wme->wp->window->options, "mode-keys");
3033 if (clear == WINDOW_COPY_CMD_CLEAR_EMACS_ONLY &&
3034 keys == MODEKEY_VI)
3035 clear = WINDOW_COPY_CMD_CLEAR_NEVER;
3036 if (clear != WINDOW_COPY_CMD_CLEAR_NEVER) {
3037 window_copy_clear_marks(wme);
3038 data->searchx = data->searchy = -1;
3040 if (action == WINDOW_COPY_CMD_NOTHING)
3041 action = WINDOW_COPY_CMD_REDRAW;
3043 wme->prefix = 1;
3045 if (action == WINDOW_COPY_CMD_CANCEL)
3046 window_pane_reset_mode(wme->wp);
3047 else if (action == WINDOW_COPY_CMD_REDRAW)
3048 window_copy_redraw_screen(wme);
3051 static void
3052 window_copy_scroll_to(struct window_mode_entry *wme, u_int px, u_int py,
3053 int no_redraw)
3055 struct window_copy_mode_data *data = wme->data;
3056 struct grid *gd = data->backing->grid;
3057 u_int offset, gap;
3059 data->cx = px;
3061 if (py >= gd->hsize - data->oy && py < gd->hsize - data->oy + gd->sy)
3062 data->cy = py - (gd->hsize - data->oy);
3063 else {
3064 gap = gd->sy / 4;
3065 if (py < gd->sy) {
3066 offset = 0;
3067 data->cy = py;
3068 } else if (py > gd->hsize + gd->sy - gap) {
3069 offset = gd->hsize;
3070 data->cy = py - gd->hsize;
3071 } else {
3072 offset = py + gap - gd->sy;
3073 data->cy = py - offset;
3075 data->oy = gd->hsize - offset;
3078 if (!no_redraw && data->searchmark != NULL && !data->timeout)
3079 window_copy_search_marks(wme, NULL, data->searchregex, 1);
3080 window_copy_update_selection(wme, 1, 0);
3081 if (!no_redraw)
3082 window_copy_redraw_screen(wme);
3085 static int
3086 window_copy_search_compare(struct grid *gd, u_int px, u_int py,
3087 struct grid *sgd, u_int spx, int cis)
3089 struct grid_cell gc, sgc;
3090 const struct utf8_data *ud, *sud;
3092 grid_get_cell(gd, px, py, &gc);
3093 ud = &gc.data;
3094 grid_get_cell(sgd, spx, 0, &sgc);
3095 sud = &sgc.data;
3097 if (ud->size != sud->size || ud->width != sud->width)
3098 return (0);
3100 if (cis && ud->size == 1)
3101 return (tolower(ud->data[0]) == sud->data[0]);
3103 return (memcmp(ud->data, sud->data, ud->size) == 0);
3106 static int
3107 window_copy_search_lr(struct grid *gd, struct grid *sgd, u_int *ppx, u_int py,
3108 u_int first, u_int last, int cis)
3110 u_int ax, bx, px, pywrap, endline;
3111 int matched;
3112 struct grid_line *gl;
3114 endline = gd->hsize + gd->sy - 1;
3115 for (ax = first; ax < last; ax++) {
3116 for (bx = 0; bx < sgd->sx; bx++) {
3117 px = ax + bx;
3118 pywrap = py;
3119 /* Wrap line. */
3120 while (px >= gd->sx && pywrap < endline) {
3121 gl = grid_get_line(gd, pywrap);
3122 if (~gl->flags & GRID_LINE_WRAPPED)
3123 break;
3124 px -= gd->sx;
3125 pywrap++;
3127 /* We have run off the end of the grid. */
3128 if (px >= gd->sx)
3129 break;
3130 matched = window_copy_search_compare(gd, px, pywrap,
3131 sgd, bx, cis);
3132 if (!matched)
3133 break;
3135 if (bx == sgd->sx) {
3136 *ppx = ax;
3137 return (1);
3140 return (0);
3143 static int
3144 window_copy_search_rl(struct grid *gd,
3145 struct grid *sgd, u_int *ppx, u_int py, u_int first, u_int last, int cis)
3147 u_int ax, bx, px, pywrap, endline;
3148 int matched;
3149 struct grid_line *gl;
3151 endline = gd->hsize + gd->sy - 1;
3152 for (ax = last; ax > first; ax--) {
3153 for (bx = 0; bx < sgd->sx; bx++) {
3154 px = ax - 1 + bx;
3155 pywrap = py;
3156 /* Wrap line. */
3157 while (px >= gd->sx && pywrap < endline) {
3158 gl = grid_get_line(gd, pywrap);
3159 if (~gl->flags & GRID_LINE_WRAPPED)
3160 break;
3161 px -= gd->sx;
3162 pywrap++;
3164 /* We have run off the end of the grid. */
3165 if (px >= gd->sx)
3166 break;
3167 matched = window_copy_search_compare(gd, px, pywrap,
3168 sgd, bx, cis);
3169 if (!matched)
3170 break;
3172 if (bx == sgd->sx) {
3173 *ppx = ax - 1;
3174 return (1);
3177 return (0);
3180 static int
3181 window_copy_search_lr_regex(struct grid *gd, u_int *ppx, u_int *psx, u_int py,
3182 u_int first, u_int last, regex_t *reg)
3184 int eflags = 0;
3185 u_int endline, foundx, foundy, len, pywrap, size = 1;
3186 char *buf;
3187 regmatch_t regmatch;
3188 struct grid_line *gl;
3191 * This can happen during search if the last match was the last
3192 * character on a line.
3194 if (first >= last)
3195 return (0);
3197 /* Set flags for regex search. */
3198 if (first != 0)
3199 eflags |= REG_NOTBOL;
3201 /* Need to look at the entire string. */
3202 buf = xmalloc(size);
3203 buf[0] = '\0';
3204 buf = window_copy_stringify(gd, py, first, gd->sx, buf, &size);
3205 len = gd->sx - first;
3206 endline = gd->hsize + gd->sy - 1;
3207 pywrap = py;
3208 while (buf != NULL && pywrap <= endline) {
3209 gl = grid_get_line(gd, pywrap);
3210 if (~gl->flags & GRID_LINE_WRAPPED)
3211 break;
3212 pywrap++;
3213 buf = window_copy_stringify(gd, pywrap, 0, gd->sx, buf, &size);
3214 len += gd->sx;
3217 if (regexec(reg, buf, 1, &regmatch, eflags) == 0 &&
3218 regmatch.rm_so != regmatch.rm_eo) {
3219 foundx = first;
3220 foundy = py;
3221 window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
3222 buf + regmatch.rm_so);
3223 if (foundy == py && foundx < last) {
3224 *ppx = foundx;
3225 len -= foundx - first;
3226 window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
3227 buf + regmatch.rm_eo);
3228 *psx = foundx;
3229 while (foundy > py) {
3230 *psx += gd->sx;
3231 foundy--;
3233 *psx -= *ppx;
3234 free(buf);
3235 return (1);
3239 free(buf);
3240 *ppx = 0;
3241 *psx = 0;
3242 return (0);
3245 static int
3246 window_copy_search_rl_regex(struct grid *gd, u_int *ppx, u_int *psx, u_int py,
3247 u_int first, u_int last, regex_t *reg)
3249 int eflags = 0;
3250 u_int endline, len, pywrap, size = 1;
3251 char *buf;
3252 struct grid_line *gl;
3254 /* Set flags for regex search. */
3255 if (first != 0)
3256 eflags |= REG_NOTBOL;
3258 /* Need to look at the entire string. */
3259 buf = xmalloc(size);
3260 buf[0] = '\0';
3261 buf = window_copy_stringify(gd, py, first, gd->sx, buf, &size);
3262 len = gd->sx - first;
3263 endline = gd->hsize + gd->sy - 1;
3264 pywrap = py;
3265 while (buf != NULL && (pywrap <= endline)) {
3266 gl = grid_get_line(gd, pywrap);
3267 if (~gl->flags & GRID_LINE_WRAPPED)
3268 break;
3269 pywrap++;
3270 buf = window_copy_stringify(gd, pywrap, 0, gd->sx, buf, &size);
3271 len += gd->sx;
3274 if (window_copy_last_regex(gd, py, first, last, len, ppx, psx, buf,
3275 reg, eflags))
3277 free(buf);
3278 return (1);
3281 free(buf);
3282 *ppx = 0;
3283 *psx = 0;
3284 return (0);
3287 static const char *
3288 window_copy_cellstring(const struct grid_line *gl, u_int px, size_t *size,
3289 int *allocated)
3291 static struct utf8_data ud;
3292 struct grid_cell_entry *gce;
3293 char *copy;
3295 if (px >= gl->cellsize) {
3296 *size = 1;
3297 *allocated = 0;
3298 return (" ");
3301 gce = &gl->celldata[px];
3302 if (gce->flags & GRID_FLAG_PADDING) {
3303 *size = 0;
3304 *allocated = 0;
3305 return (NULL);
3307 if (~gce->flags & GRID_FLAG_EXTENDED) {
3308 *size = 1;
3309 *allocated = 0;
3310 return (&gce->data.data);
3313 utf8_to_data(gl->extddata[gce->offset].data, &ud);
3314 if (ud.size == 0) {
3315 *size = 0;
3316 *allocated = 0;
3317 return (NULL);
3319 *size = ud.size;
3320 *allocated = 1;
3322 copy = xmalloc(ud.size);
3323 memcpy(copy, ud.data, ud.size);
3324 return (copy);
3327 /* Find last match in given range. */
3328 static int
3329 window_copy_last_regex(struct grid *gd, u_int py, u_int first, u_int last,
3330 u_int len, u_int *ppx, u_int *psx, const char *buf, const regex_t *preg,
3331 int eflags)
3333 u_int foundx, foundy, oldx, px = 0, savepx, savesx = 0;
3334 regmatch_t regmatch;
3336 foundx = first;
3337 foundy = py;
3338 oldx = first;
3339 while (regexec(preg, buf + px, 1, &regmatch, eflags) == 0) {
3340 if (regmatch.rm_so == regmatch.rm_eo)
3341 break;
3342 window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
3343 buf + px + regmatch.rm_so);
3344 if (foundy > py || foundx >= last)
3345 break;
3346 len -= foundx - oldx;
3347 savepx = foundx;
3348 window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
3349 buf + px + regmatch.rm_eo);
3350 if (foundy > py || foundx >= last) {
3351 *ppx = savepx;
3352 *psx = foundx;
3353 while (foundy > py) {
3354 *psx += gd->sx;
3355 foundy--;
3357 *psx -= *ppx;
3358 return (1);
3359 } else {
3360 savesx = foundx - savepx;
3361 len -= savesx;
3362 oldx = foundx;
3364 px += regmatch.rm_eo;
3367 if (savesx > 0) {
3368 *ppx = savepx;
3369 *psx = savesx;
3370 return (1);
3371 } else {
3372 *ppx = 0;
3373 *psx = 0;
3374 return (0);
3378 /* Stringify line and append to input buffer. Caller frees. */
3379 static char *
3380 window_copy_stringify(struct grid *gd, u_int py, u_int first, u_int last,
3381 char *buf, u_int *size)
3383 u_int ax, bx, newsize = *size;
3384 const struct grid_line *gl;
3385 const char *d;
3386 size_t bufsize = 1024, dlen;
3387 int allocated;
3389 while (bufsize < newsize)
3390 bufsize *= 2;
3391 buf = xrealloc(buf, bufsize);
3393 gl = grid_peek_line(gd, py);
3394 bx = *size - 1;
3395 for (ax = first; ax < last; ax++) {
3396 d = window_copy_cellstring(gl, ax, &dlen, &allocated);
3397 newsize += dlen;
3398 while (bufsize < newsize) {
3399 bufsize *= 2;
3400 buf = xrealloc(buf, bufsize);
3402 if (dlen == 1)
3403 buf[bx++] = *d;
3404 else {
3405 memcpy(buf + bx, d, dlen);
3406 bx += dlen;
3408 if (allocated)
3409 free((void *)d);
3411 buf[newsize - 1] = '\0';
3413 *size = newsize;
3414 return (buf);
3417 /* Map start of C string containing UTF-8 data to grid cell position. */
3418 static void
3419 window_copy_cstrtocellpos(struct grid *gd, u_int ncells, u_int *ppx, u_int *ppy,
3420 const char *str)
3422 u_int cell, ccell, px, pywrap, pos, len;
3423 int match;
3424 const struct grid_line *gl;
3425 const char *d;
3426 size_t dlen;
3427 struct {
3428 const char *d;
3429 size_t dlen;
3430 int allocated;
3431 } *cells;
3433 /* Populate the array of cell data. */
3434 cells = xreallocarray(NULL, ncells, sizeof cells[0]);
3435 cell = 0;
3436 px = *ppx;
3437 pywrap = *ppy;
3438 gl = grid_peek_line(gd, pywrap);
3439 while (cell < ncells) {
3440 cells[cell].d = window_copy_cellstring(gl, px,
3441 &cells[cell].dlen, &cells[cell].allocated);
3442 cell++;
3443 px++;
3444 if (px == gd->sx) {
3445 px = 0;
3446 pywrap++;
3447 gl = grid_peek_line(gd, pywrap);
3451 /* Locate starting cell. */
3452 cell = 0;
3453 len = strlen(str);
3454 while (cell < ncells) {
3455 ccell = cell;
3456 pos = 0;
3457 match = 1;
3458 while (ccell < ncells) {
3459 if (str[pos] == '\0') {
3460 match = 0;
3461 break;
3463 d = cells[ccell].d;
3464 dlen = cells[ccell].dlen;
3465 if (dlen == 1) {
3466 if (str[pos] != *d) {
3467 match = 0;
3468 break;
3470 pos++;
3471 } else {
3472 if (dlen > len - pos)
3473 dlen = len - pos;
3474 if (memcmp(str + pos, d, dlen) != 0) {
3475 match = 0;
3476 break;
3478 pos += dlen;
3480 ccell++;
3482 if (match)
3483 break;
3484 cell++;
3487 /* If not found this will be one past the end. */
3488 px = *ppx + cell;
3489 pywrap = *ppy;
3490 while (px >= gd->sx) {
3491 px -= gd->sx;
3492 pywrap++;
3495 *ppx = px;
3496 *ppy = pywrap;
3498 /* Free cell data. */
3499 for (cell = 0; cell < ncells; cell++) {
3500 if (cells[cell].allocated)
3501 free((void *)cells[cell].d);
3503 free(cells);
3506 static void
3507 window_copy_move_left(struct screen *s, u_int *fx, u_int *fy, int wrapflag)
3509 if (*fx == 0) { /* left */
3510 if (*fy == 0) { /* top */
3511 if (wrapflag) {
3512 *fx = screen_size_x(s) - 1;
3513 *fy = screen_hsize(s) + screen_size_y(s) - 1;
3515 return;
3517 *fx = screen_size_x(s) - 1;
3518 *fy = *fy - 1;
3519 } else
3520 *fx = *fx - 1;
3523 static void
3524 window_copy_move_right(struct screen *s, u_int *fx, u_int *fy, int wrapflag)
3526 if (*fx == screen_size_x(s) - 1) { /* right */
3527 if (*fy == screen_hsize(s) + screen_size_y(s) - 1) { /* bottom */
3528 if (wrapflag) {
3529 *fx = 0;
3530 *fy = 0;
3532 return;
3534 *fx = 0;
3535 *fy = *fy + 1;
3536 } else
3537 *fx = *fx + 1;
3540 static int
3541 window_copy_is_lowercase(const char *ptr)
3543 while (*ptr != '\0') {
3544 if (*ptr != tolower((u_char)*ptr))
3545 return (0);
3546 ++ptr;
3548 return (1);
3552 * Handle backward wrapped regex searches with overlapping matches. In this case
3553 * find the longest overlapping match from previous wrapped lines.
3555 static void
3556 window_copy_search_back_overlap(struct grid *gd, regex_t *preg, u_int *ppx,
3557 u_int *psx, u_int *ppy, u_int endline)
3559 u_int endx, endy, oldendx, oldendy, px, py, sx;
3560 int found = 1;
3562 oldendx = *ppx + *psx;
3563 oldendy = *ppy - 1;
3564 while (oldendx > gd->sx - 1) {
3565 oldendx -= gd->sx;
3566 oldendy++;
3568 endx = oldendx;
3569 endy = oldendy;
3570 px = *ppx;
3571 py = *ppy;
3572 while (found && px == 0 && py - 1 > endline &&
3573 grid_get_line(gd, py - 2)->flags & GRID_LINE_WRAPPED &&
3574 endx == oldendx && endy == oldendy) {
3575 py--;
3576 found = window_copy_search_rl_regex(gd, &px, &sx, py - 1, 0,
3577 gd->sx, preg);
3578 if (found) {
3579 endx = px + sx;
3580 endy = py - 1;
3581 while (endx > gd->sx - 1) {
3582 endx -= gd->sx;
3583 endy++;
3585 if (endx == oldendx && endy == oldendy) {
3586 *ppx = px;
3587 *ppy = py;
3594 * Search for text stored in sgd starting from position fx,fy up to endline. If
3595 * found, jump to it. If cis then ignore case. The direction is 0 for searching
3596 * up, down otherwise. If wrap then go to begin/end of grid and try again if
3597 * not found.
3599 static int
3600 window_copy_search_jump(struct window_mode_entry *wme, struct grid *gd,
3601 struct grid *sgd, u_int fx, u_int fy, u_int endline, int cis, int wrap,
3602 int direction, int regex)
3604 u_int i, px, sx, ssize = 1;
3605 int found = 0, cflags = REG_EXTENDED;
3606 char *sbuf;
3607 regex_t reg;
3609 if (regex) {
3610 sbuf = xmalloc(ssize);
3611 sbuf[0] = '\0';
3612 sbuf = window_copy_stringify(sgd, 0, 0, sgd->sx, sbuf, &ssize);
3613 if (cis)
3614 cflags |= REG_ICASE;
3615 if (regcomp(&reg, sbuf, cflags) != 0) {
3616 free(sbuf);
3617 return (0);
3619 free(sbuf);
3622 if (direction) {
3623 for (i = fy; i <= endline; i++) {
3624 if (regex) {
3625 found = window_copy_search_lr_regex(gd,
3626 &px, &sx, i, fx, gd->sx, &reg);
3627 } else {
3628 found = window_copy_search_lr(gd, sgd,
3629 &px, i, fx, gd->sx, cis);
3631 if (found)
3632 break;
3633 fx = 0;
3635 } else {
3636 for (i = fy + 1; endline < i; i--) {
3637 if (regex) {
3638 found = window_copy_search_rl_regex(gd,
3639 &px, &sx, i - 1, 0, fx + 1, &reg);
3640 if (found) {
3641 window_copy_search_back_overlap(gd,
3642 &reg, &px, &sx, &i, endline);
3644 } else {
3645 found = window_copy_search_rl(gd, sgd,
3646 &px, i - 1, 0, fx + 1, cis);
3648 if (found) {
3649 i--;
3650 break;
3652 fx = gd->sx - 1;
3655 if (regex)
3656 regfree(&reg);
3658 if (found) {
3659 window_copy_scroll_to(wme, px, i, 1);
3660 return (1);
3662 if (wrap) {
3663 return (window_copy_search_jump(wme, gd, sgd,
3664 direction ? 0 : gd->sx - 1,
3665 direction ? 0 : gd->hsize + gd->sy - 1, fy, cis, 0,
3666 direction, regex));
3668 return (0);
3671 static void
3672 window_copy_move_after_search_mark(struct window_copy_mode_data *data,
3673 u_int *fx, u_int *fy, int wrapflag)
3675 struct screen *s = data->backing;
3676 u_int at, start;
3678 if (window_copy_search_mark_at(data, *fx, *fy, &start) == 0 &&
3679 data->searchmark[start] != 0) {
3680 while (window_copy_search_mark_at(data, *fx, *fy, &at) == 0) {
3681 if (data->searchmark[at] != data->searchmark[start])
3682 break;
3683 /* Stop if not wrapping and at the end of the grid. */
3684 if (!wrapflag &&
3685 *fx == screen_size_x(s) - 1 &&
3686 *fy == screen_hsize(s) + screen_size_y(s) - 1)
3687 break;
3689 window_copy_move_right(s, fx, fy, wrapflag);
3695 * Search in for text searchstr. If direction is 0 then search up, otherwise
3696 * down.
3698 static int
3699 window_copy_search(struct window_mode_entry *wme, int direction, int regex)
3701 struct window_pane *wp = wme->wp;
3702 struct window_copy_mode_data *data = wme->data;
3703 struct screen *s = data->backing, ss;
3704 struct screen_write_ctx ctx;
3705 struct grid *gd = s->grid;
3706 const char *str = data->searchstr;
3707 u_int at, endline, fx, fy, start;
3708 int cis, found, keys, visible_only;
3709 int wrapflag;
3711 if (regex && str[strcspn(str, "^$*+()?[].\\")] == '\0')
3712 regex = 0;
3714 data->searchdirection = direction;
3716 if (data->timeout)
3717 return (0);
3719 if (data->searchall || wp->searchstr == NULL ||
3720 wp->searchregex != regex) {
3721 visible_only = 0;
3722 data->searchall = 0;
3723 } else
3724 visible_only = (strcmp(wp->searchstr, str) == 0);
3725 if (visible_only == 0 && data->searchmark != NULL)
3726 window_copy_clear_marks(wme);
3727 free(wp->searchstr);
3728 wp->searchstr = xstrdup(str);
3729 wp->searchregex = regex;
3731 fx = data->cx;
3732 fy = screen_hsize(data->backing) - data->oy + data->cy;
3734 screen_init(&ss, screen_write_strlen("%s", str), 1, 0);
3735 screen_write_start(&ctx, &ss);
3736 screen_write_nputs(&ctx, -1, &grid_default_cell, "%s", str);
3737 screen_write_stop(&ctx);
3739 wrapflag = options_get_number(wp->window->options, "wrap-search");
3740 cis = window_copy_is_lowercase(str);
3742 keys = options_get_number(wp->window->options, "mode-keys");
3744 if (direction) {
3746 * Behave according to mode-keys. If it is emacs, search forward
3747 * leaves the cursor after the match. If it is vi, the cursor
3748 * remains at the beginning of the match, regardless of
3749 * direction, which means that we need to start the next search
3750 * after the term the cursor is currently on when searching
3751 * forward.
3753 if (keys == MODEKEY_VI) {
3754 if (data->searchmark != NULL)
3755 window_copy_move_after_search_mark(data, &fx,
3756 &fy, wrapflag);
3757 else {
3759 * When there are no search marks, start the
3760 * search after the current cursor position.
3762 window_copy_move_right(s, &fx, &fy, wrapflag);
3765 endline = gd->hsize + gd->sy - 1;
3766 } else {
3767 window_copy_move_left(s, &fx, &fy, wrapflag);
3768 endline = 0;
3771 found = window_copy_search_jump(wme, gd, ss.grid, fx, fy, endline, cis,
3772 wrapflag, direction, regex);
3773 if (found) {
3774 window_copy_search_marks(wme, &ss, regex, visible_only);
3775 fx = data->cx;
3776 fy = screen_hsize(data->backing) - data->oy + data->cy;
3779 * When searching forward, if the cursor is not at the beginning
3780 * of the mark, search again.
3782 if (direction &&
3783 window_copy_search_mark_at(data, fx, fy, &at) == 0 &&
3784 at > 0 &&
3785 data->searchmark != NULL &&
3786 data->searchmark[at] == data->searchmark[at - 1]) {
3787 window_copy_move_after_search_mark(data, &fx, &fy,
3788 wrapflag);
3789 window_copy_search_jump(wme, gd, ss.grid, fx,
3790 fy, endline, cis, wrapflag, direction,
3791 regex);
3792 fx = data->cx;
3793 fy = screen_hsize(data->backing) - data->oy + data->cy;
3796 if (direction) {
3798 * When in Emacs mode, position the cursor just after
3799 * the mark.
3801 if (keys == MODEKEY_EMACS) {
3802 window_copy_move_after_search_mark(data, &fx,
3803 &fy, wrapflag);
3804 data->cx = fx;
3805 data->cy = fy - screen_hsize(data->backing) +
3806 data-> oy;
3808 } else {
3810 * When searching backward, position the cursor at the
3811 * beginning of the mark.
3813 if (window_copy_search_mark_at(data, fx, fy,
3814 &start) == 0) {
3815 while (window_copy_search_mark_at(data, fx, fy,
3816 &at) == 0 &&
3817 data->searchmark != NULL &&
3818 data->searchmark[at] ==
3819 data->searchmark[start]) {
3820 data->cx = fx;
3821 data->cy = fy -
3822 screen_hsize(data->backing) +
3823 data-> oy;
3824 if (at == 0)
3825 break;
3827 window_copy_move_left(s, &fx, &fy, 0);
3832 window_copy_redraw_screen(wme);
3834 screen_free(&ss);
3835 return (found);
3838 static void
3839 window_copy_visible_lines(struct window_copy_mode_data *data, u_int *start,
3840 u_int *end)
3842 struct grid *gd = data->backing->grid;
3843 const struct grid_line *gl;
3845 for (*start = gd->hsize - data->oy; *start > 0; (*start)--) {
3846 gl = grid_peek_line(gd, (*start) - 1);
3847 if (~gl->flags & GRID_LINE_WRAPPED)
3848 break;
3850 *end = gd->hsize - data->oy + gd->sy;
3853 static int
3854 window_copy_search_mark_at(struct window_copy_mode_data *data, u_int px,
3855 u_int py, u_int *at)
3857 struct screen *s = data->backing;
3858 struct grid *gd = s->grid;
3860 if (py < gd->hsize - data->oy)
3861 return (-1);
3862 if (py > gd->hsize - data->oy + gd->sy - 1)
3863 return (-1);
3864 *at = ((py - (gd->hsize - data->oy)) * gd->sx) + px;
3865 return (0);
3868 static int
3869 window_copy_search_marks(struct window_mode_entry *wme, struct screen *ssp,
3870 int regex, int visible_only)
3872 struct window_copy_mode_data *data = wme->data;
3873 struct screen *s = data->backing, ss;
3874 struct screen_write_ctx ctx;
3875 struct grid *gd = s->grid;
3876 int found, cis, stopped = 0;
3877 int cflags = REG_EXTENDED;
3878 u_int px, py, i, b, nfound = 0, width;
3879 u_int ssize = 1, start, end;
3880 char *sbuf;
3881 regex_t reg;
3882 uint64_t stop = 0, tstart, t;
3884 if (ssp == NULL) {
3885 width = screen_write_strlen("%s", data->searchstr);
3886 screen_init(&ss, width, 1, 0);
3887 screen_write_start(&ctx, &ss);
3888 screen_write_nputs(&ctx, -1, &grid_default_cell, "%s",
3889 data->searchstr);
3890 screen_write_stop(&ctx);
3891 ssp = &ss;
3892 } else
3893 width = screen_size_x(ssp);
3895 cis = window_copy_is_lowercase(data->searchstr);
3897 if (regex) {
3898 sbuf = xmalloc(ssize);
3899 sbuf[0] = '\0';
3900 sbuf = window_copy_stringify(ssp->grid, 0, 0, ssp->grid->sx,
3901 sbuf, &ssize);
3902 if (cis)
3903 cflags |= REG_ICASE;
3904 if (regcomp(&reg, sbuf, cflags) != 0) {
3905 free(sbuf);
3906 return (0);
3908 free(sbuf);
3910 tstart = get_timer();
3912 if (visible_only)
3913 window_copy_visible_lines(data, &start, &end);
3914 else {
3915 start = 0;
3916 end = gd->hsize + gd->sy;
3917 stop = get_timer() + WINDOW_COPY_SEARCH_ALL_TIMEOUT;
3920 again:
3921 free(data->searchmark);
3922 data->searchmark = xcalloc(gd->sx, gd->sy);
3923 data->searchgen = 1;
3925 for (py = start; py < end; py++) {
3926 px = 0;
3927 for (;;) {
3928 if (regex) {
3929 found = window_copy_search_lr_regex(gd,
3930 &px, &width, py, px, gd->sx, &reg);
3931 if (!found)
3932 break;
3933 } else {
3934 found = window_copy_search_lr(gd, ssp->grid,
3935 &px, py, px, gd->sx, cis);
3936 if (!found)
3937 break;
3939 nfound++;
3941 if (window_copy_search_mark_at(data, px, py, &b) == 0) {
3942 if (b + width > gd->sx * gd->sy)
3943 width = (gd->sx * gd->sy) - b;
3944 for (i = b; i < b + width; i++) {
3945 if (data->searchmark[i] != 0)
3946 continue;
3947 data->searchmark[i] = data->searchgen;
3949 if (data->searchgen == UCHAR_MAX)
3950 data->searchgen = 1;
3951 else
3952 data->searchgen++;
3954 px += width;
3957 t = get_timer();
3958 if (t - tstart > WINDOW_COPY_SEARCH_TIMEOUT) {
3959 data->timeout = 1;
3960 break;
3962 if (stop != 0 && t > stop) {
3963 stopped = 1;
3964 break;
3967 if (data->timeout) {
3968 window_copy_clear_marks(wme);
3969 goto out;
3972 if (stopped && stop != 0) {
3973 /* Try again but just the visible context. */
3974 window_copy_visible_lines(data, &start, &end);
3975 stop = 0;
3976 goto again;
3979 if (!visible_only) {
3980 if (stopped) {
3981 if (nfound > 1000)
3982 data->searchcount = 1000;
3983 else if (nfound > 100)
3984 data->searchcount = 100;
3985 else if (nfound > 10)
3986 data->searchcount = 10;
3987 else
3988 data->searchcount = -1;
3989 data->searchmore = 1;
3990 } else {
3991 data->searchcount = nfound;
3992 data->searchmore = 0;
3996 out:
3997 if (ssp == &ss)
3998 screen_free(&ss);
3999 if (regex)
4000 regfree(&reg);
4001 return (1);
4004 static void
4005 window_copy_clear_marks(struct window_mode_entry *wme)
4007 struct window_copy_mode_data *data = wme->data;
4009 free(data->searchmark);
4010 data->searchmark = NULL;
4013 static int
4014 window_copy_search_up(struct window_mode_entry *wme, int regex)
4016 return (window_copy_search(wme, 0, regex));
4019 static int
4020 window_copy_search_down(struct window_mode_entry *wme, int regex)
4022 return (window_copy_search(wme, 1, regex));
4025 static void
4026 window_copy_goto_line(struct window_mode_entry *wme, const char *linestr)
4028 struct window_copy_mode_data *data = wme->data;
4029 const char *errstr;
4030 int lineno;
4032 lineno = strtonum(linestr, -1, INT_MAX, &errstr);
4033 if (errstr != NULL)
4034 return;
4035 if (lineno < 0 || (u_int)lineno > screen_hsize(data->backing))
4036 lineno = screen_hsize(data->backing);
4038 data->oy = lineno;
4039 window_copy_update_selection(wme, 1, 0);
4040 window_copy_redraw_screen(wme);
4043 static void
4044 window_copy_match_start_end(struct window_copy_mode_data *data, u_int at,
4045 u_int *start, u_int *end)
4047 struct grid *gd = data->backing->grid;
4048 u_int last = (gd->sy * gd->sx) - 1;
4049 u_char mark = data->searchmark[at];
4051 *start = *end = at;
4052 while (*start != 0 && data->searchmark[*start] == mark)
4053 (*start)--;
4054 if (data->searchmark[*start] != mark)
4055 (*start)++;
4056 while (*end != last && data->searchmark[*end] == mark)
4057 (*end)++;
4058 if (data->searchmark[*end] != mark)
4059 (*end)--;
4062 static char *
4063 window_copy_match_at_cursor(struct window_copy_mode_data *data)
4065 struct grid *gd = data->backing->grid;
4066 struct grid_cell gc;
4067 u_int at, start, end, cy, px, py;
4068 u_int sx = screen_size_x(data->backing);
4069 char *buf = NULL;
4070 size_t len = 0;
4072 if (data->searchmark == NULL)
4073 return (NULL);
4075 cy = screen_hsize(data->backing) - data->oy + data->cy;
4076 if (window_copy_search_mark_at(data, data->cx, cy, &at) != 0)
4077 return (NULL);
4078 if (data->searchmark[at] == 0) {
4079 /* Allow one position after the match. */
4080 if (at == 0 || data->searchmark[--at] == 0)
4081 return (NULL);
4083 window_copy_match_start_end(data, at, &start, &end);
4086 * Cells will not be set in the marked array unless they are valid text
4087 * and wrapping will be taken care of, so we can just copy.
4089 for (at = start; at <= end; at++) {
4090 py = at / sx;
4091 px = at - (py * sx);
4093 grid_get_cell(gd, px, gd->hsize + py - data->oy, &gc);
4094 buf = xrealloc(buf, len + gc.data.size + 1);
4095 memcpy(buf + len, gc.data.data, gc.data.size);
4096 len += gc.data.size;
4098 if (len != 0)
4099 buf[len] = '\0';
4100 return (buf);
4103 static void
4104 window_copy_update_style(struct window_mode_entry *wme, u_int fx, u_int fy,
4105 struct grid_cell *gc, const struct grid_cell *mgc,
4106 const struct grid_cell *cgc, const struct grid_cell *mkgc)
4108 struct window_pane *wp = wme->wp;
4109 struct window_copy_mode_data *data = wme->data;
4110 u_int mark, start, end, cy, cursor, current;
4111 int inv = 0, found = 0;
4112 int keys;
4114 if (data->showmark && fy == data->my) {
4115 gc->attr = mkgc->attr;
4116 if (fx == data->mx)
4117 inv = 1;
4118 if (inv) {
4119 gc->fg = mkgc->bg;
4120 gc->bg = mkgc->fg;
4122 else {
4123 gc->fg = mkgc->fg;
4124 gc->bg = mkgc->bg;
4128 if (data->searchmark == NULL)
4129 return;
4131 if (window_copy_search_mark_at(data, fx, fy, &current) != 0)
4132 return;
4133 mark = data->searchmark[current];
4134 if (mark == 0)
4135 return;
4137 cy = screen_hsize(data->backing) - data->oy + data->cy;
4138 if (window_copy_search_mark_at(data, data->cx, cy, &cursor) == 0) {
4139 keys = options_get_number(wp->window->options, "mode-keys");
4140 if (cursor != 0 &&
4141 keys == MODEKEY_EMACS &&
4142 data->searchdirection) {
4143 if (data->searchmark[cursor - 1] == mark) {
4144 cursor--;
4145 found = 1;
4147 } else if (data->searchmark[cursor] == mark)
4148 found = 1;
4149 if (found) {
4150 window_copy_match_start_end(data, cursor, &start, &end);
4151 if (current >= start && current <= end) {
4152 gc->attr = cgc->attr;
4153 if (inv) {
4154 gc->fg = cgc->bg;
4155 gc->bg = cgc->fg;
4157 else {
4158 gc->fg = cgc->fg;
4159 gc->bg = cgc->bg;
4161 return;
4166 gc->attr = mgc->attr;
4167 if (inv) {
4168 gc->fg = mgc->bg;
4169 gc->bg = mgc->fg;
4171 else {
4172 gc->fg = mgc->fg;
4173 gc->bg = mgc->bg;
4177 static void
4178 window_copy_write_one(struct window_mode_entry *wme,
4179 struct screen_write_ctx *ctx, u_int py, u_int fy, u_int nx,
4180 const struct grid_cell *mgc, const struct grid_cell *cgc,
4181 const struct grid_cell *mkgc)
4183 struct window_copy_mode_data *data = wme->data;
4184 struct grid *gd = data->backing->grid;
4185 struct grid_cell gc;
4186 u_int fx;
4188 screen_write_cursormove(ctx, 0, py, 0);
4189 for (fx = 0; fx < nx; fx++) {
4190 grid_get_cell(gd, fx, fy, &gc);
4191 if (fx + gc.data.width <= nx) {
4192 window_copy_update_style(wme, fx, fy, &gc, mgc, cgc,
4193 mkgc);
4194 screen_write_cell(ctx, &gc);
4199 static void
4200 window_copy_write_line(struct window_mode_entry *wme,
4201 struct screen_write_ctx *ctx, u_int py)
4203 struct window_pane *wp = wme->wp;
4204 struct window_copy_mode_data *data = wme->data;
4205 struct screen *s = &data->screen;
4206 struct options *oo = wp->window->options;
4207 struct grid_line *gl;
4208 struct grid_cell gc, mgc, cgc, mkgc;
4209 char hdr[512], tmp[256], *t;
4210 size_t size = 0;
4211 u_int hsize = screen_hsize(data->backing);
4213 style_apply(&gc, oo, "mode-style", NULL);
4214 gc.flags |= GRID_FLAG_NOPALETTE;
4215 style_apply(&mgc, oo, "copy-mode-match-style", NULL);
4216 mgc.flags |= GRID_FLAG_NOPALETTE;
4217 style_apply(&cgc, oo, "copy-mode-current-match-style", NULL);
4218 cgc.flags |= GRID_FLAG_NOPALETTE;
4219 style_apply(&mkgc, oo, "copy-mode-mark-style", NULL);
4220 mkgc.flags |= GRID_FLAG_NOPALETTE;
4222 if (py == 0 && s->rupper < s->rlower && !data->hide_position) {
4223 gl = grid_get_line(data->backing->grid, hsize - data->oy);
4224 if (gl->time == 0)
4225 xsnprintf(tmp, sizeof tmp, "[%u/%u]", data->oy, hsize);
4226 else {
4227 t = format_pretty_time(gl->time, 1);
4228 xsnprintf(tmp, sizeof tmp, "%s [%u/%u]", t, data->oy,
4229 hsize);
4230 free(t);
4233 if (data->searchmark == NULL) {
4234 if (data->timeout) {
4235 size = xsnprintf(hdr, sizeof hdr,
4236 "(timed out) %s", tmp);
4237 } else
4238 size = xsnprintf(hdr, sizeof hdr, "%s", tmp);
4239 } else {
4240 if (data->searchcount == -1)
4241 size = xsnprintf(hdr, sizeof hdr, "%s", tmp);
4242 else {
4243 size = xsnprintf(hdr, sizeof hdr,
4244 "(%d%s results) %s", data->searchcount,
4245 data->searchmore ? "+" : "", tmp);
4248 if (size > screen_size_x(s))
4249 size = screen_size_x(s);
4250 screen_write_cursormove(ctx, screen_size_x(s) - size, 0, 0);
4251 screen_write_puts(ctx, &gc, "%s", hdr);
4252 } else
4253 size = 0;
4255 if (size < screen_size_x(s)) {
4256 window_copy_write_one(wme, ctx, py, hsize - data->oy + py,
4257 screen_size_x(s) - size, &mgc, &cgc, &mkgc);
4260 if (py == data->cy && data->cx == screen_size_x(s)) {
4261 screen_write_cursormove(ctx, screen_size_x(s) - 1, py, 0);
4262 screen_write_putc(ctx, &grid_default_cell, '$');
4266 static void
4267 window_copy_write_lines(struct window_mode_entry *wme,
4268 struct screen_write_ctx *ctx, u_int py, u_int ny)
4270 u_int yy;
4272 for (yy = py; yy < py + ny; yy++)
4273 window_copy_write_line(wme, ctx, py);
4276 static void
4277 window_copy_redraw_selection(struct window_mode_entry *wme, u_int old_y)
4279 struct window_copy_mode_data *data = wme->data;
4280 struct grid *gd = data->backing->grid;
4281 u_int new_y, start, end;
4283 new_y = data->cy;
4284 if (old_y <= new_y) {
4285 start = old_y;
4286 end = new_y;
4287 } else {
4288 start = new_y;
4289 end = old_y;
4293 * In word selection mode the first word on the line below the cursor
4294 * might be selected, so add this line to the redraw area.
4296 if (data->selflag == SEL_WORD) {
4297 /* Last grid line in data coordinates. */
4298 if (end < gd->sy + data->oy - 1)
4299 end++;
4301 window_copy_redraw_lines(wme, start, end - start + 1);
4304 static void
4305 window_copy_redraw_lines(struct window_mode_entry *wme, u_int py, u_int ny)
4307 struct window_pane *wp = wme->wp;
4308 struct window_copy_mode_data *data = wme->data;
4309 struct screen_write_ctx ctx;
4310 u_int i;
4312 screen_write_start_pane(&ctx, wp, NULL);
4313 for (i = py; i < py + ny; i++)
4314 window_copy_write_line(wme, &ctx, i);
4315 screen_write_cursormove(&ctx, data->cx, data->cy, 0);
4316 screen_write_stop(&ctx);
4319 static void
4320 window_copy_redraw_screen(struct window_mode_entry *wme)
4322 struct window_copy_mode_data *data = wme->data;
4324 window_copy_redraw_lines(wme, 0, screen_size_y(&data->screen));
4327 static void
4328 window_copy_synchronize_cursor_end(struct window_mode_entry *wme, int begin,
4329 int no_reset)
4331 struct window_copy_mode_data *data = wme->data;
4332 u_int xx, yy;
4334 xx = data->cx;
4335 yy = screen_hsize(data->backing) + data->cy - data->oy;
4336 switch (data->selflag) {
4337 case SEL_WORD:
4338 if (no_reset)
4339 break;
4340 begin = 0;
4341 if (data->dy > yy || (data->dy == yy && data->dx > xx)) {
4342 /* Right to left selection. */
4343 window_copy_cursor_previous_word_pos(wme,
4344 data->separators, &xx, &yy);
4345 begin = 1;
4347 /* Reset the end. */
4348 data->endselx = data->endselrx;
4349 data->endsely = data->endselry;
4350 } else {
4351 /* Left to right selection. */
4352 if (xx >= window_copy_find_length(wme, yy) ||
4353 !window_copy_in_set(wme, xx + 1, yy, WHITESPACE)) {
4354 window_copy_cursor_next_word_end_pos(wme,
4355 data->separators, &xx, &yy);
4358 /* Reset the start. */
4359 data->selx = data->selrx;
4360 data->sely = data->selry;
4362 break;
4363 case SEL_LINE:
4364 if (no_reset)
4365 break;
4366 begin = 0;
4367 if (data->dy > yy) {
4368 /* Right to left selection. */
4369 xx = 0;
4370 begin = 1;
4372 /* Reset the end. */
4373 data->endselx = data->endselrx;
4374 data->endsely = data->endselry;
4375 } else {
4376 /* Left to right selection. */
4377 if (yy < data->endselry)
4378 yy = data->endselry;
4379 xx = window_copy_find_length(wme, yy);
4381 /* Reset the start. */
4382 data->selx = data->selrx;
4383 data->sely = data->selry;
4385 break;
4386 case SEL_CHAR:
4387 break;
4389 if (begin) {
4390 data->selx = xx;
4391 data->sely = yy;
4392 } else {
4393 data->endselx = xx;
4394 data->endsely = yy;
4398 static void
4399 window_copy_synchronize_cursor(struct window_mode_entry *wme, int no_reset)
4401 struct window_copy_mode_data *data = wme->data;
4403 switch (data->cursordrag) {
4404 case CURSORDRAG_ENDSEL:
4405 window_copy_synchronize_cursor_end(wme, 0, no_reset);
4406 break;
4407 case CURSORDRAG_SEL:
4408 window_copy_synchronize_cursor_end(wme, 1, no_reset);
4409 break;
4410 case CURSORDRAG_NONE:
4411 break;
4415 static void
4416 window_copy_update_cursor(struct window_mode_entry *wme, u_int cx, u_int cy)
4418 struct window_pane *wp = wme->wp;
4419 struct window_copy_mode_data *data = wme->data;
4420 struct screen *s = &data->screen;
4421 struct screen_write_ctx ctx;
4422 u_int old_cx, old_cy;
4424 old_cx = data->cx; old_cy = data->cy;
4425 data->cx = cx; data->cy = cy;
4426 if (old_cx == screen_size_x(s))
4427 window_copy_redraw_lines(wme, old_cy, 1);
4428 if (data->cx == screen_size_x(s))
4429 window_copy_redraw_lines(wme, data->cy, 1);
4430 else {
4431 screen_write_start_pane(&ctx, wp, NULL);
4432 screen_write_cursormove(&ctx, data->cx, data->cy, 0);
4433 screen_write_stop(&ctx);
4437 static void
4438 window_copy_start_selection(struct window_mode_entry *wme)
4440 struct window_copy_mode_data *data = wme->data;
4442 data->selx = data->cx;
4443 data->sely = screen_hsize(data->backing) + data->cy - data->oy;
4445 data->endselx = data->selx;
4446 data->endsely = data->sely;
4448 data->cursordrag = CURSORDRAG_ENDSEL;
4450 window_copy_set_selection(wme, 1, 0);
4453 static int
4454 window_copy_adjust_selection(struct window_mode_entry *wme, u_int *selx,
4455 u_int *sely)
4457 struct window_copy_mode_data *data = wme->data;
4458 struct screen *s = &data->screen;
4459 u_int sx, sy, ty;
4460 int relpos;
4462 sx = *selx;
4463 sy = *sely;
4465 ty = screen_hsize(data->backing) - data->oy;
4466 if (sy < ty) {
4467 relpos = WINDOW_COPY_REL_POS_ABOVE;
4468 if (!data->rectflag)
4469 sx = 0;
4470 sy = 0;
4471 } else if (sy > ty + screen_size_y(s) - 1) {
4472 relpos = WINDOW_COPY_REL_POS_BELOW;
4473 if (!data->rectflag)
4474 sx = screen_size_x(s) - 1;
4475 sy = screen_size_y(s) - 1;
4476 } else {
4477 relpos = WINDOW_COPY_REL_POS_ON_SCREEN;
4478 sy -= ty;
4481 *selx = sx;
4482 *sely = sy;
4483 return (relpos);
4486 static int
4487 window_copy_update_selection(struct window_mode_entry *wme, int may_redraw,
4488 int no_reset)
4490 struct window_copy_mode_data *data = wme->data;
4491 struct screen *s = &data->screen;
4493 if (s->sel == NULL && data->lineflag == LINE_SEL_NONE)
4494 return (0);
4495 return (window_copy_set_selection(wme, may_redraw, no_reset));
4498 static int
4499 window_copy_set_selection(struct window_mode_entry *wme, int may_redraw,
4500 int no_reset)
4502 struct window_pane *wp = wme->wp;
4503 struct window_copy_mode_data *data = wme->data;
4504 struct screen *s = &data->screen;
4505 struct options *oo = wp->window->options;
4506 struct grid_cell gc;
4507 u_int sx, sy, cy, endsx, endsy;
4508 int startrelpos, endrelpos;
4510 window_copy_synchronize_cursor(wme, no_reset);
4512 /* Adjust the selection. */
4513 sx = data->selx;
4514 sy = data->sely;
4515 startrelpos = window_copy_adjust_selection(wme, &sx, &sy);
4517 /* Adjust the end of selection. */
4518 endsx = data->endselx;
4519 endsy = data->endsely;
4520 endrelpos = window_copy_adjust_selection(wme, &endsx, &endsy);
4522 /* Selection is outside of the current screen */
4523 if (startrelpos == endrelpos &&
4524 startrelpos != WINDOW_COPY_REL_POS_ON_SCREEN) {
4525 screen_hide_selection(s);
4526 return (0);
4529 /* Set colours and selection. */
4530 style_apply(&gc, oo, "mode-style", NULL);
4531 gc.flags |= GRID_FLAG_NOPALETTE;
4532 screen_set_selection(s, sx, sy, endsx, endsy, data->rectflag,
4533 data->modekeys, &gc);
4535 if (data->rectflag && may_redraw) {
4537 * Can't rely on the caller to redraw the right lines for
4538 * rectangle selection - find the highest line and the number
4539 * of lines, and redraw just past that in both directions
4541 cy = data->cy;
4542 if (data->cursordrag == CURSORDRAG_ENDSEL) {
4543 if (sy < cy)
4544 window_copy_redraw_lines(wme, sy, cy - sy + 1);
4545 else
4546 window_copy_redraw_lines(wme, cy, sy - cy + 1);
4547 } else {
4548 if (endsy < cy) {
4549 window_copy_redraw_lines(wme, endsy,
4550 cy - endsy + 1);
4551 } else {
4552 window_copy_redraw_lines(wme, cy,
4553 endsy - cy + 1);
4558 return (1);
4561 static void *
4562 window_copy_get_selection(struct window_mode_entry *wme, size_t *len)
4564 struct window_pane *wp = wme->wp;
4565 struct window_copy_mode_data *data = wme->data;
4566 struct screen *s = &data->screen;
4567 char *buf;
4568 size_t off;
4569 u_int i, xx, yy, sx, sy, ex, ey, ey_last;
4570 u_int firstsx, lastex, restex, restsx, selx;
4571 int keys;
4573 if (data->screen.sel == NULL && data->lineflag == LINE_SEL_NONE) {
4574 buf = window_copy_match_at_cursor(data);
4575 if (buf != NULL)
4576 *len = strlen(buf);
4577 else
4578 *len = 0;
4579 return (buf);
4582 buf = xmalloc(1);
4583 off = 0;
4585 *buf = '\0';
4588 * The selection extends from selx,sely to (adjusted) cx,cy on
4589 * the base screen.
4592 /* Find start and end. */
4593 xx = data->endselx;
4594 yy = data->endsely;
4595 if (yy < data->sely || (yy == data->sely && xx < data->selx)) {
4596 sx = xx; sy = yy;
4597 ex = data->selx; ey = data->sely;
4598 } else {
4599 sx = data->selx; sy = data->sely;
4600 ex = xx; ey = yy;
4603 /* Trim ex to end of line. */
4604 ey_last = window_copy_find_length(wme, ey);
4605 if (ex > ey_last)
4606 ex = ey_last;
4609 * Deal with rectangle-copy if necessary; four situations: start of
4610 * first line (firstsx), end of last line (lastex), start (restsx) and
4611 * end (restex) of all other lines.
4613 xx = screen_size_x(s);
4616 * Behave according to mode-keys. If it is emacs, copy like emacs,
4617 * keeping the top-left-most character, and dropping the
4618 * bottom-right-most, regardless of copy direction. If it is vi, also
4619 * keep bottom-right-most character.
4621 keys = options_get_number(wp->window->options, "mode-keys");
4622 if (data->rectflag) {
4624 * Need to ignore the column with the cursor in it, which for
4625 * rectangular copy means knowing which side the cursor is on.
4627 if (data->cursordrag == CURSORDRAG_ENDSEL)
4628 selx = data->selx;
4629 else
4630 selx = data->endselx;
4631 if (selx < data->cx) {
4632 /* Selection start is on the left. */
4633 if (keys == MODEKEY_EMACS) {
4634 lastex = data->cx;
4635 restex = data->cx;
4637 else {
4638 lastex = data->cx + 1;
4639 restex = data->cx + 1;
4641 firstsx = selx;
4642 restsx = selx;
4643 } else {
4644 /* Cursor is on the left. */
4645 lastex = selx + 1;
4646 restex = selx + 1;
4647 firstsx = data->cx;
4648 restsx = data->cx;
4650 } else {
4651 if (keys == MODEKEY_EMACS)
4652 lastex = ex;
4653 else
4654 lastex = ex + 1;
4655 restex = xx;
4656 firstsx = sx;
4657 restsx = 0;
4660 /* Copy the lines. */
4661 for (i = sy; i <= ey; i++) {
4662 window_copy_copy_line(wme, &buf, &off, i,
4663 (i == sy ? firstsx : restsx),
4664 (i == ey ? lastex : restex));
4667 /* Don't bother if no data. */
4668 if (off == 0) {
4669 free(buf);
4670 *len = 0;
4671 return (NULL);
4673 /* Remove final \n (unless at end in vi mode). */
4674 if (keys == MODEKEY_EMACS || lastex <= ey_last) {
4675 if (~grid_get_line(data->backing->grid, ey)->flags &
4676 GRID_LINE_WRAPPED || lastex != ey_last)
4677 off -= 1;
4679 *len = off;
4680 return (buf);
4683 static void
4684 window_copy_copy_buffer(struct window_mode_entry *wme, const char *prefix,
4685 void *buf, size_t len)
4687 struct window_pane *wp = wme->wp;
4688 struct screen_write_ctx ctx;
4690 if (options_get_number(global_options, "set-clipboard") != 0) {
4691 screen_write_start_pane(&ctx, wp, NULL);
4692 screen_write_setselection(&ctx, "", buf, len);
4693 screen_write_stop(&ctx);
4694 notify_pane("pane-set-clipboard", wp);
4697 paste_add(prefix, buf, len);
4700 static void *
4701 window_copy_pipe_run(struct window_mode_entry *wme, struct session *s,
4702 const char *cmd, size_t *len)
4704 void *buf;
4705 struct job *job;
4707 buf = window_copy_get_selection(wme, len);
4708 if (cmd == NULL || *cmd == '\0')
4709 cmd = options_get_string(global_options, "copy-command");
4710 if (cmd != NULL && *cmd != '\0') {
4711 job = job_run(cmd, 0, NULL, NULL, s, NULL, NULL, NULL, NULL,
4712 NULL, JOB_NOWAIT, -1, -1);
4713 bufferevent_write(job_get_event(job), buf, *len);
4715 return (buf);
4718 static void
4719 window_copy_pipe(struct window_mode_entry *wme, struct session *s,
4720 const char *cmd)
4722 size_t len;
4724 window_copy_pipe_run(wme, s, cmd, &len);
4727 static void
4728 window_copy_copy_pipe(struct window_mode_entry *wme, struct session *s,
4729 const char *prefix, const char *cmd)
4731 void *buf;
4732 size_t len;
4734 buf = window_copy_pipe_run(wme, s, cmd, &len);
4735 if (buf != NULL)
4736 window_copy_copy_buffer(wme, prefix, buf, len);
4739 static void
4740 window_copy_copy_selection(struct window_mode_entry *wme, const char *prefix)
4742 char *buf;
4743 size_t len;
4745 buf = window_copy_get_selection(wme, &len);
4746 if (buf != NULL)
4747 window_copy_copy_buffer(wme, prefix, buf, len);
4750 static void
4751 window_copy_append_selection(struct window_mode_entry *wme)
4753 struct window_pane *wp = wme->wp;
4754 char *buf;
4755 struct paste_buffer *pb;
4756 const char *bufdata, *bufname = NULL;
4757 size_t len, bufsize;
4758 struct screen_write_ctx ctx;
4760 buf = window_copy_get_selection(wme, &len);
4761 if (buf == NULL)
4762 return;
4764 if (options_get_number(global_options, "set-clipboard") != 0) {
4765 screen_write_start_pane(&ctx, wp, NULL);
4766 screen_write_setselection(&ctx, "", buf, len);
4767 screen_write_stop(&ctx);
4768 notify_pane("pane-set-clipboard", wp);
4771 pb = paste_get_top(&bufname);
4772 if (pb != NULL) {
4773 bufdata = paste_buffer_data(pb, &bufsize);
4774 buf = xrealloc(buf, len + bufsize);
4775 memmove(buf + bufsize, buf, len);
4776 memcpy(buf, bufdata, bufsize);
4777 len += bufsize;
4779 if (paste_set(buf, len, bufname, NULL) != 0)
4780 free(buf);
4783 static void
4784 window_copy_copy_line(struct window_mode_entry *wme, char **buf, size_t *off,
4785 u_int sy, u_int sx, u_int ex)
4787 struct window_copy_mode_data *data = wme->data;
4788 struct grid *gd = data->backing->grid;
4789 struct grid_cell gc;
4790 struct grid_line *gl;
4791 struct utf8_data ud;
4792 u_int i, xx, wrapped = 0;
4793 const char *s;
4795 if (sx > ex)
4796 return;
4799 * Work out if the line was wrapped at the screen edge and all of it is
4800 * on screen.
4802 gl = grid_get_line(gd, sy);
4803 if (gl->flags & GRID_LINE_WRAPPED && gl->cellsize <= gd->sx)
4804 wrapped = 1;
4806 /* If the line was wrapped, don't strip spaces (use the full length). */
4807 if (wrapped)
4808 xx = gl->cellsize;
4809 else
4810 xx = window_copy_find_length(wme, sy);
4811 if (ex > xx)
4812 ex = xx;
4813 if (sx > xx)
4814 sx = xx;
4816 if (sx < ex) {
4817 for (i = sx; i < ex; i++) {
4818 grid_get_cell(gd, i, sy, &gc);
4819 if (gc.flags & GRID_FLAG_PADDING)
4820 continue;
4821 utf8_copy(&ud, &gc.data);
4822 if (ud.size == 1 && (gc.attr & GRID_ATTR_CHARSET)) {
4823 s = tty_acs_get(NULL, ud.data[0]);
4824 if (s != NULL && strlen(s) <= sizeof ud.data) {
4825 ud.size = strlen(s);
4826 memcpy(ud.data, s, ud.size);
4830 *buf = xrealloc(*buf, (*off) + ud.size);
4831 memcpy(*buf + *off, ud.data, ud.size);
4832 *off += ud.size;
4836 /* Only add a newline if the line wasn't wrapped. */
4837 if (!wrapped || ex != xx) {
4838 *buf = xrealloc(*buf, (*off) + 1);
4839 (*buf)[(*off)++] = '\n';
4843 static void
4844 window_copy_clear_selection(struct window_mode_entry *wme)
4846 struct window_copy_mode_data *data = wme->data;
4847 u_int px, py;
4849 screen_clear_selection(&data->screen);
4851 data->cursordrag = CURSORDRAG_NONE;
4852 data->lineflag = LINE_SEL_NONE;
4853 data->selflag = SEL_CHAR;
4855 py = screen_hsize(data->backing) + data->cy - data->oy;
4856 px = window_copy_find_length(wme, py);
4857 if (data->cx > px)
4858 window_copy_update_cursor(wme, px, data->cy);
4861 static int
4862 window_copy_in_set(struct window_mode_entry *wme, u_int px, u_int py,
4863 const char *set)
4865 struct window_copy_mode_data *data = wme->data;
4866 struct grid_cell gc;
4868 grid_get_cell(data->backing->grid, px, py, &gc);
4869 if (gc.flags & GRID_FLAG_PADDING)
4870 return (0);
4871 return (utf8_cstrhas(set, &gc.data));
4874 static u_int
4875 window_copy_find_length(struct window_mode_entry *wme, u_int py)
4877 struct window_copy_mode_data *data = wme->data;
4879 return (grid_line_length(data->backing->grid, py));
4882 static void
4883 window_copy_cursor_start_of_line(struct window_mode_entry *wme)
4885 struct window_copy_mode_data *data = wme->data;
4886 struct screen *back_s = data->backing;
4887 struct grid_reader gr;
4888 u_int px, py, oldy, hsize;
4890 px = data->cx;
4891 hsize = screen_hsize(back_s);
4892 py = hsize + data->cy - data->oy;
4893 oldy = data->cy;
4895 grid_reader_start(&gr, back_s->grid, px, py);
4896 grid_reader_cursor_start_of_line(&gr, 1);
4897 grid_reader_get_cursor(&gr, &px, &py);
4898 window_copy_acquire_cursor_up(wme, hsize, data->oy, oldy, px, py);
4901 static void
4902 window_copy_cursor_back_to_indentation(struct window_mode_entry *wme)
4904 struct window_copy_mode_data *data = wme->data;
4905 struct screen *back_s = data->backing;
4906 struct grid_reader gr;
4907 u_int px, py, oldy, hsize;
4909 px = data->cx;
4910 hsize = screen_hsize(back_s);
4911 py = hsize + data->cy - data->oy;
4912 oldy = data->cy;
4914 grid_reader_start(&gr, back_s->grid, px, py);
4915 grid_reader_cursor_back_to_indentation(&gr);
4916 grid_reader_get_cursor(&gr, &px, &py);
4917 window_copy_acquire_cursor_up(wme, hsize, data->oy, oldy, px, py);
4920 static void
4921 window_copy_cursor_end_of_line(struct window_mode_entry *wme)
4923 struct window_copy_mode_data *data = wme->data;
4924 struct screen *back_s = data->backing;
4925 struct grid_reader gr;
4926 u_int px, py, oldy, hsize;
4928 px = data->cx;
4929 hsize = screen_hsize(back_s);
4930 py = hsize + data->cy - data->oy;
4931 oldy = data->cy;
4933 grid_reader_start(&gr, back_s->grid, px, py);
4934 if (data->screen.sel != NULL && data->rectflag)
4935 grid_reader_cursor_end_of_line(&gr, 1, 1);
4936 else
4937 grid_reader_cursor_end_of_line(&gr, 1, 0);
4938 grid_reader_get_cursor(&gr, &px, &py);
4939 window_copy_acquire_cursor_down(wme, hsize, screen_size_y(back_s),
4940 data->oy, oldy, px, py, 0);
4943 static void
4944 window_copy_other_end(struct window_mode_entry *wme)
4946 struct window_copy_mode_data *data = wme->data;
4947 struct screen *s = &data->screen;
4948 u_int selx, sely, cy, yy, hsize;
4950 if (s->sel == NULL && data->lineflag == LINE_SEL_NONE)
4951 return;
4953 if (data->lineflag == LINE_SEL_LEFT_RIGHT)
4954 data->lineflag = LINE_SEL_RIGHT_LEFT;
4955 else if (data->lineflag == LINE_SEL_RIGHT_LEFT)
4956 data->lineflag = LINE_SEL_LEFT_RIGHT;
4958 switch (data->cursordrag) {
4959 case CURSORDRAG_NONE:
4960 case CURSORDRAG_SEL:
4961 data->cursordrag = CURSORDRAG_ENDSEL;
4962 break;
4963 case CURSORDRAG_ENDSEL:
4964 data->cursordrag = CURSORDRAG_SEL;
4965 break;
4968 selx = data->endselx;
4969 sely = data->endsely;
4970 if (data->cursordrag == CURSORDRAG_SEL) {
4971 selx = data->selx;
4972 sely = data->sely;
4975 cy = data->cy;
4976 yy = screen_hsize(data->backing) + data->cy - data->oy;
4978 data->cx = selx;
4980 hsize = screen_hsize(data->backing);
4981 if (sely < hsize - data->oy) { /* above */
4982 data->oy = hsize - sely;
4983 data->cy = 0;
4984 } else if (sely > hsize - data->oy + screen_size_y(s)) { /* below */
4985 data->oy = hsize - sely + screen_size_y(s) - 1;
4986 data->cy = screen_size_y(s) - 1;
4987 } else
4988 data->cy = cy + sely - yy;
4990 window_copy_update_selection(wme, 1, 1);
4991 window_copy_redraw_screen(wme);
4994 static void
4995 window_copy_cursor_left(struct window_mode_entry *wme)
4997 struct window_copy_mode_data *data = wme->data;
4998 struct screen *back_s = data->backing;
4999 struct grid_reader gr;
5000 u_int px, py, oldy, hsize;
5002 px = data->cx;
5003 hsize = screen_hsize(back_s);
5004 py = hsize + data->cy - data->oy;
5005 oldy = data->cy;
5007 grid_reader_start(&gr, back_s->grid, px, py);
5008 grid_reader_cursor_left(&gr, 1);
5009 grid_reader_get_cursor(&gr, &px, &py);
5010 window_copy_acquire_cursor_up(wme, hsize, data->oy, oldy, px, py);
5013 static void
5014 window_copy_cursor_right(struct window_mode_entry *wme, int all)
5016 struct window_copy_mode_data *data = wme->data;
5017 struct screen *back_s = data->backing;
5018 struct grid_reader gr;
5019 u_int px, py, oldy, hsize;
5021 px = data->cx;
5022 hsize = screen_hsize(back_s);
5023 py = hsize + data->cy - data->oy;
5024 oldy = data->cy;
5026 grid_reader_start(&gr, back_s->grid, px, py);
5027 grid_reader_cursor_right(&gr, 1, all);
5028 grid_reader_get_cursor(&gr, &px, &py);
5029 window_copy_acquire_cursor_down(wme, hsize, screen_size_y(back_s),
5030 data->oy, oldy, px, py, 0);
5033 static void
5034 window_copy_cursor_up(struct window_mode_entry *wme, int scroll_only)
5036 struct window_copy_mode_data *data = wme->data;
5037 struct screen *s = &data->screen;
5038 u_int ox, oy, px, py;
5039 int norectsel;
5041 norectsel = data->screen.sel == NULL || !data->rectflag;
5042 oy = screen_hsize(data->backing) + data->cy - data->oy;
5043 ox = window_copy_find_length(wme, oy);
5044 if (norectsel && data->cx != ox) {
5045 data->lastcx = data->cx;
5046 data->lastsx = ox;
5049 if (data->lineflag == LINE_SEL_LEFT_RIGHT && oy == data->sely)
5050 window_copy_other_end(wme);
5052 if (scroll_only || data->cy == 0) {
5053 if (norectsel)
5054 data->cx = data->lastcx;
5055 window_copy_scroll_down(wme, 1);
5056 if (scroll_only) {
5057 if (data->cy == screen_size_y(s) - 1)
5058 window_copy_redraw_lines(wme, data->cy, 1);
5059 else
5060 window_copy_redraw_lines(wme, data->cy, 2);
5062 } else {
5063 if (norectsel) {
5064 window_copy_update_cursor(wme, data->lastcx,
5065 data->cy - 1);
5066 } else
5067 window_copy_update_cursor(wme, data->cx, data->cy - 1);
5068 if (window_copy_update_selection(wme, 1, 0)) {
5069 if (data->cy == screen_size_y(s) - 1)
5070 window_copy_redraw_lines(wme, data->cy, 1);
5071 else
5072 window_copy_redraw_lines(wme, data->cy, 2);
5076 if (norectsel) {
5077 py = screen_hsize(data->backing) + data->cy - data->oy;
5078 px = window_copy_find_length(wme, py);
5079 if ((data->cx >= data->lastsx && data->cx != px) ||
5080 data->cx > px)
5082 window_copy_update_cursor(wme, px, data->cy);
5083 if (window_copy_update_selection(wme, 1, 0))
5084 window_copy_redraw_lines(wme, data->cy, 1);
5088 if (data->lineflag == LINE_SEL_LEFT_RIGHT)
5090 py = screen_hsize(data->backing) + data->cy - data->oy;
5091 if (data->rectflag)
5092 px = screen_size_x(data->backing);
5093 else
5094 px = window_copy_find_length(wme, py);
5095 window_copy_update_cursor(wme, px, data->cy);
5096 if (window_copy_update_selection(wme, 1, 0))
5097 window_copy_redraw_lines(wme, data->cy, 1);
5099 else if (data->lineflag == LINE_SEL_RIGHT_LEFT)
5101 window_copy_update_cursor(wme, 0, data->cy);
5102 if (window_copy_update_selection(wme, 1, 0))
5103 window_copy_redraw_lines(wme, data->cy, 1);
5107 static void
5108 window_copy_cursor_down(struct window_mode_entry *wme, int scroll_only)
5110 struct window_copy_mode_data *data = wme->data;
5111 struct screen *s = &data->screen;
5112 u_int ox, oy, px, py;
5113 int norectsel;
5115 norectsel = data->screen.sel == NULL || !data->rectflag;
5116 oy = screen_hsize(data->backing) + data->cy - data->oy;
5117 ox = window_copy_find_length(wme, oy);
5118 if (norectsel && data->cx != ox) {
5119 data->lastcx = data->cx;
5120 data->lastsx = ox;
5123 if (data->lineflag == LINE_SEL_RIGHT_LEFT && oy == data->endsely)
5124 window_copy_other_end(wme);
5126 if (scroll_only || data->cy == screen_size_y(s) - 1) {
5127 if (norectsel)
5128 data->cx = data->lastcx;
5129 window_copy_scroll_up(wme, 1);
5130 if (scroll_only && data->cy > 0)
5131 window_copy_redraw_lines(wme, data->cy - 1, 2);
5132 } else {
5133 if (norectsel) {
5134 window_copy_update_cursor(wme, data->lastcx,
5135 data->cy + 1);
5136 } else
5137 window_copy_update_cursor(wme, data->cx, data->cy + 1);
5138 if (window_copy_update_selection(wme, 1, 0))
5139 window_copy_redraw_lines(wme, data->cy - 1, 2);
5142 if (norectsel) {
5143 py = screen_hsize(data->backing) + data->cy - data->oy;
5144 px = window_copy_find_length(wme, py);
5145 if ((data->cx >= data->lastsx && data->cx != px) ||
5146 data->cx > px)
5148 window_copy_update_cursor(wme, px, data->cy);
5149 if (window_copy_update_selection(wme, 1, 0))
5150 window_copy_redraw_lines(wme, data->cy, 1);
5154 if (data->lineflag == LINE_SEL_LEFT_RIGHT)
5156 py = screen_hsize(data->backing) + data->cy - data->oy;
5157 if (data->rectflag)
5158 px = screen_size_x(data->backing);
5159 else
5160 px = window_copy_find_length(wme, py);
5161 window_copy_update_cursor(wme, px, data->cy);
5162 if (window_copy_update_selection(wme, 1, 0))
5163 window_copy_redraw_lines(wme, data->cy, 1);
5165 else if (data->lineflag == LINE_SEL_RIGHT_LEFT)
5167 window_copy_update_cursor(wme, 0, data->cy);
5168 if (window_copy_update_selection(wme, 1, 0))
5169 window_copy_redraw_lines(wme, data->cy, 1);
5173 static void
5174 window_copy_cursor_jump(struct window_mode_entry *wme)
5176 struct window_copy_mode_data *data = wme->data;
5177 struct screen *back_s = data->backing;
5178 struct grid_reader gr;
5179 u_int px, py, oldy, hsize;
5181 px = data->cx + 1;
5182 hsize = screen_hsize(back_s);
5183 py = hsize + data->cy - data->oy;
5184 oldy = data->cy;
5186 grid_reader_start(&gr, back_s->grid, px, py);
5187 if (grid_reader_cursor_jump(&gr, data->jumpchar)) {
5188 grid_reader_get_cursor(&gr, &px, &py);
5189 window_copy_acquire_cursor_down(wme, hsize,
5190 screen_size_y(back_s), data->oy, oldy, px, py, 0);
5194 static void
5195 window_copy_cursor_jump_back(struct window_mode_entry *wme)
5197 struct window_copy_mode_data *data = wme->data;
5198 struct screen *back_s = data->backing;
5199 struct grid_reader gr;
5200 u_int px, py, oldy, hsize;
5202 px = data->cx;
5203 hsize = screen_hsize(back_s);
5204 py = hsize + data->cy - data->oy;
5205 oldy = data->cy;
5207 grid_reader_start(&gr, back_s->grid, px, py);
5208 grid_reader_cursor_left(&gr, 0);
5209 if (grid_reader_cursor_jump_back(&gr, data->jumpchar)) {
5210 grid_reader_get_cursor(&gr, &px, &py);
5211 window_copy_acquire_cursor_up(wme, hsize, data->oy, oldy, px,
5212 py);
5216 static void
5217 window_copy_cursor_jump_to(struct window_mode_entry *wme)
5219 struct window_copy_mode_data *data = wme->data;
5220 struct screen *back_s = data->backing;
5221 struct grid_reader gr;
5222 u_int px, py, oldy, hsize;
5224 px = data->cx + 2;
5225 hsize = screen_hsize(back_s);
5226 py = hsize + data->cy - data->oy;
5227 oldy = data->cy;
5229 grid_reader_start(&gr, back_s->grid, px, py);
5230 if (grid_reader_cursor_jump(&gr, data->jumpchar)) {
5231 grid_reader_cursor_left(&gr, 1);
5232 grid_reader_get_cursor(&gr, &px, &py);
5233 window_copy_acquire_cursor_down(wme, hsize,
5234 screen_size_y(back_s), data->oy, oldy, px, py, 0);
5238 static void
5239 window_copy_cursor_jump_to_back(struct window_mode_entry *wme)
5241 struct window_copy_mode_data *data = wme->data;
5242 struct screen *back_s = data->backing;
5243 struct grid_reader gr;
5244 u_int px, py, oldy, hsize;
5246 px = data->cx;
5247 hsize = screen_hsize(back_s);
5248 py = hsize + data->cy - data->oy;
5249 oldy = data->cy;
5251 grid_reader_start(&gr, back_s->grid, px, py);
5252 grid_reader_cursor_left(&gr, 0);
5253 grid_reader_cursor_left(&gr, 0);
5254 if (grid_reader_cursor_jump_back(&gr, data->jumpchar)) {
5255 grid_reader_cursor_right(&gr, 1, 0);
5256 grid_reader_get_cursor(&gr, &px, &py);
5257 window_copy_acquire_cursor_up(wme, hsize, data->oy, oldy, px,
5258 py);
5262 static void
5263 window_copy_cursor_next_word(struct window_mode_entry *wme,
5264 const char *separators)
5266 struct window_copy_mode_data *data = wme->data;
5267 struct screen *back_s = data->backing;
5268 struct grid_reader gr;
5269 u_int px, py, oldy, hsize;
5271 px = data->cx;
5272 hsize = screen_hsize(back_s);
5273 py = hsize + data->cy - data->oy;
5274 oldy = data->cy;
5276 grid_reader_start(&gr, back_s->grid, px, py);
5277 grid_reader_cursor_next_word(&gr, separators);
5278 grid_reader_get_cursor(&gr, &px, &py);
5279 window_copy_acquire_cursor_down(wme, hsize, screen_size_y(back_s),
5280 data->oy, oldy, px, py, 0);
5283 /* Compute the next place where a word ends. */
5284 static void
5285 window_copy_cursor_next_word_end_pos(struct window_mode_entry *wme,
5286 const char *separators, u_int *ppx, u_int *ppy)
5288 struct window_pane *wp = wme->wp;
5289 struct window_copy_mode_data *data = wme->data;
5290 struct options *oo = wp->window->options;
5291 struct screen *back_s = data->backing;
5292 struct grid_reader gr;
5293 u_int px, py, hsize;
5295 px = data->cx;
5296 hsize = screen_hsize(back_s);
5297 py = hsize + data->cy - data->oy;
5299 grid_reader_start(&gr, back_s->grid, px, py);
5300 if (options_get_number(oo, "mode-keys") == MODEKEY_VI) {
5301 if (!grid_reader_in_set(&gr, WHITESPACE))
5302 grid_reader_cursor_right(&gr, 0, 0);
5303 grid_reader_cursor_next_word_end(&gr, separators);
5304 grid_reader_cursor_left(&gr, 1);
5305 } else
5306 grid_reader_cursor_next_word_end(&gr, separators);
5307 grid_reader_get_cursor(&gr, &px, &py);
5308 *ppx = px;
5309 *ppy = py;
5312 /* Move to the next place where a word ends. */
5313 static void
5314 window_copy_cursor_next_word_end(struct window_mode_entry *wme,
5315 const char *separators, int no_reset)
5317 struct window_pane *wp = wme->wp;
5318 struct window_copy_mode_data *data = wme->data;
5319 struct options *oo = wp->window->options;
5320 struct screen *back_s = data->backing;
5321 struct grid_reader gr;
5322 u_int px, py, oldy, hsize;
5324 px = data->cx;
5325 hsize = screen_hsize(back_s);
5326 py = hsize + data->cy - data->oy;
5327 oldy = data->cy;
5329 grid_reader_start(&gr, back_s->grid, px, py);
5330 if (options_get_number(oo, "mode-keys") == MODEKEY_VI) {
5331 if (!grid_reader_in_set(&gr, WHITESPACE))
5332 grid_reader_cursor_right(&gr, 0, 0);
5333 grid_reader_cursor_next_word_end(&gr, separators);
5334 grid_reader_cursor_left(&gr, 1);
5335 } else
5336 grid_reader_cursor_next_word_end(&gr, separators);
5337 grid_reader_get_cursor(&gr, &px, &py);
5338 window_copy_acquire_cursor_down(wme, hsize, screen_size_y(back_s),
5339 data->oy, oldy, px, py, no_reset);
5342 /* Compute the previous place where a word begins. */
5343 static void
5344 window_copy_cursor_previous_word_pos(struct window_mode_entry *wme,
5345 const char *separators, u_int *ppx, u_int *ppy)
5347 struct window_copy_mode_data *data = wme->data;
5348 struct screen *back_s = data->backing;
5349 struct grid_reader gr;
5350 u_int px, py, hsize;
5352 px = data->cx;
5353 hsize = screen_hsize(back_s);
5354 py = hsize + data->cy - data->oy;
5356 grid_reader_start(&gr, back_s->grid, px, py);
5357 grid_reader_cursor_previous_word(&gr, separators, /* already= */ 0,
5358 /* stop_at_eol= */ 1);
5359 grid_reader_get_cursor(&gr, &px, &py);
5360 *ppx = px;
5361 *ppy = py;
5364 /* Move to the previous place where a word begins. */
5365 static void
5366 window_copy_cursor_previous_word(struct window_mode_entry *wme,
5367 const char *separators, int already)
5369 struct window_copy_mode_data *data = wme->data;
5370 struct window *w = wme->wp->window;
5371 struct screen *back_s = data->backing;
5372 struct grid_reader gr;
5373 u_int px, py, oldy, hsize;
5374 int stop_at_eol;
5376 if (options_get_number(w->options, "mode-keys") == MODEKEY_EMACS)
5377 stop_at_eol = 1;
5378 else
5379 stop_at_eol = 0;
5381 px = data->cx;
5382 hsize = screen_hsize(back_s);
5383 py = hsize + data->cy - data->oy;
5384 oldy = data->cy;
5386 grid_reader_start(&gr, back_s->grid, px, py);
5387 grid_reader_cursor_previous_word(&gr, separators, already, stop_at_eol);
5388 grid_reader_get_cursor(&gr, &px, &py);
5389 window_copy_acquire_cursor_up(wme, hsize, data->oy, oldy, px, py);
5392 static void
5393 window_copy_cursor_prompt(struct window_mode_entry *wme, int direction,
5394 const char *args)
5396 struct window_copy_mode_data *data = wme->data;
5397 struct screen *s = data->backing;
5398 struct grid *gd = s->grid;
5399 u_int end_line;
5400 u_int line = gd->hsize - data->oy + data->cy;
5401 int add, line_flag;
5403 if (args != NULL && strcmp(args, "-o") == 0)
5404 line_flag = GRID_LINE_START_OUTPUT;
5405 else
5406 line_flag = GRID_LINE_START_PROMPT;
5408 if (direction == 0) { /* up */
5409 add = -1;
5410 end_line = 0;
5411 } else { /* down */
5412 add = 1;
5413 end_line = gd->hsize + gd->sy - 1;
5416 if (line == end_line)
5417 return;
5418 for (;;) {
5419 if (line == end_line)
5420 return;
5421 line += add;
5423 if (grid_get_line(gd, line)->flags & line_flag)
5424 break;
5427 data->cx = 0;
5428 if (line > gd->hsize) {
5429 data->cy = line - gd->hsize;
5430 data->oy = 0;
5431 } else {
5432 data->cy = 0;
5433 data->oy = gd->hsize - line;
5436 window_copy_update_selection(wme, 1, 0);
5437 window_copy_redraw_screen(wme);
5440 static void
5441 window_copy_scroll_up(struct window_mode_entry *wme, u_int ny)
5443 struct window_pane *wp = wme->wp;
5444 struct window_copy_mode_data *data = wme->data;
5445 struct screen *s = &data->screen;
5446 struct screen_write_ctx ctx;
5448 if (data->oy < ny)
5449 ny = data->oy;
5450 if (ny == 0)
5451 return;
5452 data->oy -= ny;
5454 if (data->searchmark != NULL && !data->timeout)
5455 window_copy_search_marks(wme, NULL, data->searchregex, 1);
5456 window_copy_update_selection(wme, 0, 0);
5458 screen_write_start_pane(&ctx, wp, NULL);
5459 screen_write_cursormove(&ctx, 0, 0, 0);
5460 screen_write_deleteline(&ctx, ny, 8);
5461 window_copy_write_lines(wme, &ctx, screen_size_y(s) - ny, ny);
5462 window_copy_write_line(wme, &ctx, 0);
5463 if (screen_size_y(s) > 1)
5464 window_copy_write_line(wme, &ctx, 1);
5465 if (screen_size_y(s) > 3)
5466 window_copy_write_line(wme, &ctx, screen_size_y(s) - 2);
5467 if (s->sel != NULL && screen_size_y(s) > ny)
5468 window_copy_write_line(wme, &ctx, screen_size_y(s) - ny - 1);
5469 screen_write_cursormove(&ctx, data->cx, data->cy, 0);
5470 screen_write_stop(&ctx);
5473 static void
5474 window_copy_scroll_down(struct window_mode_entry *wme, u_int ny)
5476 struct window_pane *wp = wme->wp;
5477 struct window_copy_mode_data *data = wme->data;
5478 struct screen *s = &data->screen;
5479 struct screen_write_ctx ctx;
5481 if (ny > screen_hsize(data->backing))
5482 return;
5484 if (data->oy > screen_hsize(data->backing) - ny)
5485 ny = screen_hsize(data->backing) - data->oy;
5486 if (ny == 0)
5487 return;
5488 data->oy += ny;
5490 if (data->searchmark != NULL && !data->timeout)
5491 window_copy_search_marks(wme, NULL, data->searchregex, 1);
5492 window_copy_update_selection(wme, 0, 0);
5494 screen_write_start_pane(&ctx, wp, NULL);
5495 screen_write_cursormove(&ctx, 0, 0, 0);
5496 screen_write_insertline(&ctx, ny, 8);
5497 window_copy_write_lines(wme, &ctx, 0, ny);
5498 if (s->sel != NULL && screen_size_y(s) > ny)
5499 window_copy_write_line(wme, &ctx, ny);
5500 else if (ny == 1) /* nuke position */
5501 window_copy_write_line(wme, &ctx, 1);
5502 screen_write_cursormove(&ctx, data->cx, data->cy, 0);
5503 screen_write_stop(&ctx);
5506 static void
5507 window_copy_rectangle_set(struct window_mode_entry *wme, int rectflag)
5509 struct window_copy_mode_data *data = wme->data;
5510 u_int px, py;
5512 data->rectflag = rectflag;
5514 py = screen_hsize(data->backing) + data->cy - data->oy;
5515 px = window_copy_find_length(wme, py);
5516 if (data->cx > px)
5517 window_copy_update_cursor(wme, px, data->cy);
5519 window_copy_update_selection(wme, 1, 0);
5520 window_copy_redraw_screen(wme);
5523 static void
5524 window_copy_move_mouse(struct mouse_event *m)
5526 struct window_pane *wp;
5527 struct window_mode_entry *wme;
5528 u_int x, y;
5530 wp = cmd_mouse_pane(m, NULL, NULL);
5531 if (wp == NULL)
5532 return;
5533 wme = TAILQ_FIRST(&wp->modes);
5534 if (wme == NULL)
5535 return;
5536 if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
5537 return;
5539 if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
5540 return;
5542 window_copy_update_cursor(wme, x, y);
5545 void
5546 window_copy_start_drag(struct client *c, struct mouse_event *m)
5548 struct window_pane *wp;
5549 struct window_mode_entry *wme;
5550 struct window_copy_mode_data *data;
5551 u_int x, y, yg;
5553 if (c == NULL)
5554 return;
5556 wp = cmd_mouse_pane(m, NULL, NULL);
5557 if (wp == NULL)
5558 return;
5559 wme = TAILQ_FIRST(&wp->modes);
5560 if (wme == NULL)
5561 return;
5562 if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
5563 return;
5565 if (cmd_mouse_at(wp, m, &x, &y, 1) != 0)
5566 return;
5568 c->tty.mouse_drag_update = window_copy_drag_update;
5569 c->tty.mouse_drag_release = window_copy_drag_release;
5571 data = wme->data;
5572 yg = screen_hsize(data->backing) + y - data->oy;
5573 if (x < data->selrx || x > data->endselrx || yg != data->selry)
5574 data->selflag = SEL_CHAR;
5575 switch (data->selflag) {
5576 case SEL_WORD:
5577 if (data->separators != NULL) {
5578 window_copy_update_cursor(wme, x, y);
5579 window_copy_cursor_previous_word_pos(wme,
5580 data->separators, &x, &y);
5581 y -= screen_hsize(data->backing) - data->oy;
5583 window_copy_update_cursor(wme, x, y);
5584 break;
5585 case SEL_LINE:
5586 window_copy_update_cursor(wme, 0, y);
5587 break;
5588 case SEL_CHAR:
5589 window_copy_update_cursor(wme, x, y);
5590 window_copy_start_selection(wme);
5591 break;
5594 window_copy_redraw_screen(wme);
5595 window_copy_drag_update(c, m);
5598 static void
5599 window_copy_drag_update(struct client *c, struct mouse_event *m)
5601 struct window_pane *wp;
5602 struct window_mode_entry *wme;
5603 struct window_copy_mode_data *data;
5604 u_int x, y, old_cx, old_cy;
5605 struct timeval tv = {
5606 .tv_usec = WINDOW_COPY_DRAG_REPEAT_TIME
5609 if (c == NULL)
5610 return;
5612 wp = cmd_mouse_pane(m, NULL, NULL);
5613 if (wp == NULL)
5614 return;
5615 wme = TAILQ_FIRST(&wp->modes);
5616 if (wme == NULL)
5617 return;
5618 if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
5619 return;
5621 data = wme->data;
5622 evtimer_del(&data->dragtimer);
5624 if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
5625 return;
5626 old_cx = data->cx;
5627 old_cy = data->cy;
5629 window_copy_update_cursor(wme, x, y);
5630 if (window_copy_update_selection(wme, 1, 0))
5631 window_copy_redraw_selection(wme, old_cy);
5632 if (old_cy != data->cy || old_cx == data->cx) {
5633 if (y == 0) {
5634 evtimer_add(&data->dragtimer, &tv);
5635 window_copy_cursor_up(wme, 1);
5636 } else if (y == screen_size_y(&data->screen) - 1) {
5637 evtimer_add(&data->dragtimer, &tv);
5638 window_copy_cursor_down(wme, 1);
5643 static void
5644 window_copy_drag_release(struct client *c, struct mouse_event *m)
5646 struct window_pane *wp;
5647 struct window_mode_entry *wme;
5648 struct window_copy_mode_data *data;
5650 if (c == NULL)
5651 return;
5653 wp = cmd_mouse_pane(m, NULL, NULL);
5654 if (wp == NULL)
5655 return;
5656 wme = TAILQ_FIRST(&wp->modes);
5657 if (wme == NULL)
5658 return;
5659 if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
5660 return;
5662 data = wme->data;
5663 evtimer_del(&data->dragtimer);
5666 static void
5667 window_copy_jump_to_mark(struct window_mode_entry *wme)
5669 struct window_copy_mode_data *data = wme->data;
5670 u_int tmx, tmy;
5672 tmx = data->cx;
5673 tmy = screen_hsize(data->backing) + data->cy - data->oy;
5674 data->cx = data->mx;
5675 if (data->my < screen_hsize(data->backing)) {
5676 data->cy = 0;
5677 data->oy = screen_hsize(data->backing) - data->my;
5678 } else {
5679 data->cy = data->my - screen_hsize(data->backing);
5680 data->oy = 0;
5682 data->mx = tmx;
5683 data->my = tmy;
5684 data->showmark = 1;
5685 window_copy_update_selection(wme, 0, 0);
5686 window_copy_redraw_screen(wme);
5689 /* Scroll up if the cursor went off the visible screen. */
5690 static void
5691 window_copy_acquire_cursor_up(struct window_mode_entry *wme, u_int hsize,
5692 u_int oy, u_int oldy, u_int px, u_int py)
5694 u_int cy, yy, ny, nd;
5696 yy = hsize - oy;
5697 if (py < yy) {
5698 ny = yy - py;
5699 cy = 0;
5700 nd = 1;
5701 } else {
5702 ny = 0;
5703 cy = py - yy;
5704 nd = oldy - cy + 1;
5706 while (ny > 0) {
5707 window_copy_cursor_up(wme, 1);
5708 ny--;
5710 window_copy_update_cursor(wme, px, cy);
5711 if (window_copy_update_selection(wme, 1, 0))
5712 window_copy_redraw_lines(wme, cy, nd);
5715 /* Scroll down if the cursor went off the visible screen. */
5716 static void
5717 window_copy_acquire_cursor_down(struct window_mode_entry *wme, u_int hsize,
5718 u_int sy, u_int oy, u_int oldy, u_int px, u_int py, int no_reset)
5720 u_int cy, yy, ny, nd;
5722 cy = py - hsize + oy;
5723 yy = sy - 1;
5724 if (cy > yy) {
5725 ny = cy - yy;
5726 oldy = yy;
5727 nd = 1;
5728 } else {
5729 ny = 0;
5730 nd = cy - oldy + 1;
5732 while (ny > 0) {
5733 window_copy_cursor_down(wme, 1);
5734 ny--;
5736 if (cy > yy)
5737 window_copy_update_cursor(wme, px, yy);
5738 else
5739 window_copy_update_cursor(wme, px, cy);
5740 if (window_copy_update_selection(wme, 1, no_reset))
5741 window_copy_redraw_lines(wme, oldy, nd);