Check for the right return value from sscanf.
[tmux-openbsd.git] / window-copy.c
blob3ba11f2a00d7baf9b18182c91bed1804667ff612
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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 <stdlib.h>
22 #include <string.h>
24 #include "tmux.h"
26 struct screen *window_copy_init(struct window_pane *);
27 void window_copy_free(struct window_pane *);
28 void window_copy_resize(struct window_pane *, u_int, u_int);
29 void window_copy_key(struct window_pane *, struct session *, int);
30 int window_copy_key_input(struct window_pane *, int);
31 int window_copy_key_numeric_prefix(struct window_pane *, int);
32 void window_copy_mouse(
33 struct window_pane *, struct session *, struct mouse_event *);
35 void window_copy_redraw_lines(struct window_pane *, u_int, u_int);
36 void window_copy_redraw_screen(struct window_pane *);
37 void window_copy_write_line(
38 struct window_pane *, struct screen_write_ctx *, u_int);
39 void window_copy_write_lines(
40 struct window_pane *, struct screen_write_ctx *, u_int, u_int);
42 void window_copy_scroll_to(struct window_pane *, u_int, u_int);
43 int window_copy_search_compare(
44 struct grid *, u_int, u_int, struct grid *, u_int);
45 int window_copy_search_lr(
46 struct grid *, struct grid *, u_int *, u_int, u_int, u_int);
47 int window_copy_search_rl(
48 struct grid *, struct grid *, u_int *, u_int, u_int, u_int);
49 void window_copy_search_up(struct window_pane *, const char *);
50 void window_copy_search_down(struct window_pane *, const char *);
51 void window_copy_goto_line(struct window_pane *, const char *);
52 void window_copy_update_cursor(struct window_pane *, u_int, u_int);
53 void window_copy_start_selection(struct window_pane *);
54 int window_copy_update_selection(struct window_pane *);
55 void window_copy_copy_selection(struct window_pane *, int);
56 void window_copy_clear_selection(struct window_pane *);
57 void window_copy_copy_line(
58 struct window_pane *, char **, size_t *, u_int, u_int, u_int);
59 int window_copy_in_set(struct window_pane *, u_int, u_int, const char *);
60 u_int window_copy_find_length(struct window_pane *, u_int);
61 void window_copy_cursor_start_of_line(struct window_pane *);
62 void window_copy_cursor_back_to_indentation(struct window_pane *);
63 void window_copy_cursor_end_of_line(struct window_pane *);
64 void window_copy_cursor_left(struct window_pane *);
65 void window_copy_cursor_right(struct window_pane *);
66 void window_copy_cursor_up(struct window_pane *, int);
67 void window_copy_cursor_down(struct window_pane *, int);
68 void window_copy_cursor_jump(struct window_pane *);
69 void window_copy_cursor_jump_back(struct window_pane *);
70 void window_copy_cursor_jump_to(struct window_pane *);
71 void window_copy_cursor_jump_to_back(struct window_pane *);
72 void window_copy_cursor_next_word(struct window_pane *, const char *);
73 void window_copy_cursor_next_word_end(struct window_pane *, const char *);
74 void window_copy_cursor_previous_word(struct window_pane *, const char *);
75 void window_copy_scroll_up(struct window_pane *, u_int);
76 void window_copy_scroll_down(struct window_pane *, u_int);
77 void window_copy_rectangle_toggle(struct window_pane *);
79 const struct window_mode window_copy_mode = {
80 window_copy_init,
81 window_copy_free,
82 window_copy_resize,
83 window_copy_key,
84 window_copy_mouse,
85 NULL,
88 enum window_copy_input_type {
89 WINDOW_COPY_OFF,
90 WINDOW_COPY_NUMERICPREFIX,
91 WINDOW_COPY_SEARCHUP,
92 WINDOW_COPY_SEARCHDOWN,
93 WINDOW_COPY_JUMPFORWARD,
94 WINDOW_COPY_JUMPBACK,
95 WINDOW_COPY_JUMPTOFORWARD,
96 WINDOW_COPY_JUMPTOBACK,
97 WINDOW_COPY_GOTOLINE,
101 * Copy-mode's visible screen (the "screen" field) is filled from one of
102 * two sources: the original contents of the pane (used when we
103 * actually enter via the "copy-mode" command, to copy the contents of
104 * the current pane), or else a series of lines containing the output
105 * from an output-writing tmux command (such as any of the "show-*" or
106 * "list-*" commands).
108 * In either case, the full content of the copy-mode grid is pointed at
109 * by the "backing" field, and is copied into "screen" as needed (that
110 * is, when scrolling occurs). When copy-mode is backed by a pane,
111 * backing points directly at that pane's screen structure (&wp->base);
112 * when backed by a list of output-lines from a command, it points at
113 * a newly-allocated screen structure (which is deallocated when the
114 * mode ends).
116 struct window_copy_mode_data {
117 struct screen screen;
119 struct screen *backing;
120 int backing_written; /* backing display has started */
122 struct mode_key_data mdata;
124 u_int oy;
126 u_int selx;
127 u_int sely;
129 u_int rectflag; /* are we in rectangle copy mode? */
131 u_int cx;
132 u_int cy;
134 u_int lastcx; /* position in last line with content */
135 u_int lastsx; /* size of last line with content */
137 enum window_copy_input_type inputtype;
138 const char *inputprompt;
139 char *inputstr;
141 int numprefix;
143 enum window_copy_input_type searchtype;
144 char *searchstr;
146 enum window_copy_input_type jumptype;
147 char jumpchar;
150 struct screen *
151 window_copy_init(struct window_pane *wp)
153 struct window_copy_mode_data *data;
154 struct screen *s;
155 int keys;
157 wp->modedata = data = xmalloc(sizeof *data);
158 data->oy = 0;
159 data->cx = 0;
160 data->cy = 0;
162 data->lastcx = 0;
163 data->lastsx = 0;
165 data->backing_written = 0;
167 data->rectflag = 0;
169 data->inputtype = WINDOW_COPY_OFF;
170 data->inputprompt = NULL;
171 data->inputstr = xstrdup("");
172 data->numprefix = -1;
174 data->searchtype = WINDOW_COPY_OFF;
175 data->searchstr = NULL;
177 if (wp->fd != -1)
178 bufferevent_disable(wp->event, EV_READ|EV_WRITE);
180 data->jumptype = WINDOW_COPY_OFF;
181 data->jumpchar = '\0';
183 s = &data->screen;
184 screen_init(s, screen_size_x(&wp->base), screen_size_y(&wp->base), 0);
185 if (options_get_number(&wp->window->options, "mode-mouse"))
186 s->mode |= MODE_MOUSE_STANDARD;
188 keys = options_get_number(&wp->window->options, "mode-keys");
189 if (keys == MODEKEY_EMACS)
190 mode_key_init(&data->mdata, &mode_key_tree_emacs_copy);
191 else
192 mode_key_init(&data->mdata, &mode_key_tree_vi_copy);
194 data->backing = NULL;
196 return (s);
199 void
200 window_copy_init_from_pane(struct window_pane *wp)
202 struct window_copy_mode_data *data = wp->modedata;
203 struct screen *s = &data->screen;
204 struct screen_write_ctx ctx;
205 u_int i;
207 if (wp->mode != &window_copy_mode)
208 fatalx("not in copy mode");
210 data->backing = &wp->base;
211 data->cx = data->backing->cx;
212 data->cy = data->backing->cy;
214 s->cx = data->cx;
215 s->cy = data->cy;
217 screen_write_start(&ctx, NULL, s);
218 for (i = 0; i < screen_size_y(s); i++)
219 window_copy_write_line(wp, &ctx, i);
220 screen_write_cursormove(&ctx, data->cx, data->cy);
221 screen_write_stop(&ctx);
224 void
225 window_copy_init_for_output(struct window_pane *wp)
227 struct window_copy_mode_data *data = wp->modedata;
229 data->backing = xmalloc(sizeof *data->backing);
230 screen_init(data->backing, screen_size_x(&wp->base),
231 screen_size_y(&wp->base), UINT_MAX);
232 data->backing->mode &= ~MODE_WRAP;
235 void
236 window_copy_free(struct window_pane *wp)
238 struct window_copy_mode_data *data = wp->modedata;
240 if (wp->fd != -1)
241 bufferevent_enable(wp->event, EV_READ|EV_WRITE);
243 if (data->searchstr != NULL)
244 xfree(data->searchstr);
245 xfree(data->inputstr);
247 if (data->backing != &wp->base) {
248 screen_free(data->backing);
249 xfree(data->backing);
251 screen_free(&data->screen);
253 xfree(data);
256 void
257 window_copy_add(struct window_pane *wp, const char *fmt, ...)
259 va_list ap;
261 va_start(ap, fmt);
262 window_copy_vadd(wp, fmt, ap);
263 va_end(ap);
266 void
267 window_copy_vadd(struct window_pane *wp, const char *fmt, va_list ap)
269 struct window_copy_mode_data *data = wp->modedata;
270 struct screen *backing = data->backing;
271 struct screen_write_ctx back_ctx, ctx;
272 struct grid_cell gc;
273 int utf8flag;
274 u_int old_hsize;
276 if (backing == &wp->base)
277 return;
279 utf8flag = options_get_number(&wp->window->options, "utf8");
280 memcpy(&gc, &grid_default_cell, sizeof gc);
282 old_hsize = screen_hsize(data->backing);
283 screen_write_start(&back_ctx, NULL, backing);
284 if (data->backing_written) {
286 * On the second or later line, do a CRLF before writing
287 * (so it's on a new line).
289 screen_write_carriagereturn(&back_ctx);
290 screen_write_linefeed(&back_ctx, 0);
291 } else
292 data->backing_written = 1;
293 screen_write_vnputs(&back_ctx, 0, &gc, utf8flag, fmt, ap);
294 screen_write_stop(&back_ctx);
296 data->oy += screen_hsize(data->backing) - old_hsize;
298 screen_write_start(&ctx, wp, &data->screen);
301 * If the history has changed, draw the top line.
302 * (If there's any history at all, it has changed.)
304 if (screen_hsize(data->backing))
305 window_copy_redraw_lines(wp, 0, 1);
307 /* Write the line, if it's visible. */
308 if (backing->cy + data->oy < screen_size_y(backing))
309 window_copy_redraw_lines(wp, backing->cy, 1);
311 screen_write_stop(&ctx);
314 void
315 window_copy_pageup(struct window_pane *wp)
317 struct window_copy_mode_data *data = wp->modedata;
318 struct screen *s = &data->screen;
319 u_int n;
321 n = 1;
322 if (screen_size_y(s) > 2)
323 n = screen_size_y(s) - 2;
324 if (data->oy + n > screen_hsize(data->backing))
325 data->oy = screen_hsize(data->backing);
326 else
327 data->oy += n;
328 window_copy_update_selection(wp);
329 window_copy_redraw_screen(wp);
332 void
333 window_copy_resize(struct window_pane *wp, u_int sx, u_int sy)
335 struct window_copy_mode_data *data = wp->modedata;
336 struct screen *s = &data->screen;
337 struct screen_write_ctx ctx;
339 screen_resize(s, sx, sy);
340 if (data->backing != &wp->base)
341 screen_resize(data->backing, sx, sy);
343 if (data->cy > sy - 1)
344 data->cy = sy - 1;
345 if (data->cx > sx)
346 data->cx = sx;
347 if (data->oy > screen_hsize(data->backing))
348 data->oy = screen_hsize(data->backing);
350 window_copy_clear_selection(wp);
352 screen_write_start(&ctx, NULL, s);
353 window_copy_write_lines(wp, &ctx, 0, screen_size_y(s) - 1);
354 screen_write_stop(&ctx);
356 window_copy_redraw_screen(wp);
359 void
360 window_copy_key(struct window_pane *wp, struct session *sess, int key)
362 const char *word_separators;
363 struct window_copy_mode_data *data = wp->modedata;
364 struct screen *s = &data->screen;
365 u_int n;
366 int np, keys;
367 enum mode_key_cmd cmd;
369 np = data->numprefix;
370 if (np <= 0)
371 np = 1;
373 if (data->inputtype == WINDOW_COPY_JUMPFORWARD ||
374 data->inputtype == WINDOW_COPY_JUMPBACK ||
375 data->inputtype == WINDOW_COPY_JUMPTOFORWARD ||
376 data->inputtype == WINDOW_COPY_JUMPTOBACK) {
377 /* Ignore keys with modifiers. */
378 if ((key & KEYC_MASK_MOD) == 0) {
379 data->jumpchar = key;
380 if (data->inputtype == WINDOW_COPY_JUMPFORWARD) {
381 for (; np != 0; np--)
382 window_copy_cursor_jump(wp);
383 } else if (data->inputtype == WINDOW_COPY_JUMPBACK) {
384 for (; np != 0; np--)
385 window_copy_cursor_jump_back(wp);
386 } else if (data->inputtype == WINDOW_COPY_JUMPTOFORWARD) {
387 for (; np != 0; np--)
388 window_copy_cursor_jump_to(wp);
389 } else if (data->inputtype == WINDOW_COPY_JUMPTOBACK) {
390 for (; np != 0; np--)
391 window_copy_cursor_jump_to_back(wp);
394 data->jumptype = data->inputtype;
395 data->inputtype = WINDOW_COPY_OFF;
396 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
397 return;
398 } else if (data->inputtype == WINDOW_COPY_NUMERICPREFIX) {
399 if (window_copy_key_numeric_prefix(wp, key) == 0)
400 return;
401 data->inputtype = WINDOW_COPY_OFF;
402 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
403 } else if (data->inputtype != WINDOW_COPY_OFF) {
404 if (window_copy_key_input(wp, key) != 0)
405 goto input_off;
406 return;
409 cmd = mode_key_lookup(&data->mdata, key);
410 switch (cmd) {
411 case MODEKEYCOPY_CANCEL:
412 window_pane_reset_mode(wp);
413 return;
414 case MODEKEYCOPY_LEFT:
415 for (; np != 0; np--)
416 window_copy_cursor_left(wp);
417 break;
418 case MODEKEYCOPY_RIGHT:
419 for (; np != 0; np--)
420 window_copy_cursor_right(wp);
421 break;
422 case MODEKEYCOPY_UP:
423 for (; np != 0; np--)
424 window_copy_cursor_up(wp, 0);
425 break;
426 case MODEKEYCOPY_DOWN:
427 for (; np != 0; np--)
428 window_copy_cursor_down(wp, 0);
429 break;
430 case MODEKEYCOPY_SCROLLUP:
431 for (; np != 0; np--)
432 window_copy_cursor_up(wp, 1);
433 break;
434 case MODEKEYCOPY_SCROLLDOWN:
435 for (; np != 0; np--)
436 window_copy_cursor_down(wp, 1);
437 break;
438 case MODEKEYCOPY_PREVIOUSPAGE:
439 for (; np != 0; np--)
440 window_copy_pageup(wp);
441 break;
442 case MODEKEYCOPY_NEXTPAGE:
443 n = 1;
444 if (screen_size_y(s) > 2)
445 n = screen_size_y(s) - 2;
446 for (; np != 0; np--) {
447 if (data->oy < n)
448 data->oy = 0;
449 else
450 data->oy -= n;
452 window_copy_update_selection(wp);
453 window_copy_redraw_screen(wp);
454 break;
455 case MODEKEYCOPY_HALFPAGEUP:
456 n = screen_size_y(s) / 2;
457 for (; np != 0; np--) {
458 if (data->oy + n > screen_hsize(data->backing))
459 data->oy = screen_hsize(data->backing);
460 else
461 data->oy += n;
463 window_copy_update_selection(wp);
464 window_copy_redraw_screen(wp);
465 break;
466 case MODEKEYCOPY_HALFPAGEDOWN:
467 n = screen_size_y(s) / 2;
468 for (; np != 0; np--) {
469 if (data->oy < n)
470 data->oy = 0;
471 else
472 data->oy -= n;
474 window_copy_update_selection(wp);
475 window_copy_redraw_screen(wp);
476 break;
477 case MODEKEYCOPY_TOPLINE:
478 data->cx = 0;
479 data->cy = 0;
480 window_copy_update_selection(wp);
481 window_copy_redraw_screen(wp);
482 break;
483 case MODEKEYCOPY_MIDDLELINE:
484 data->cx = 0;
485 data->cy = (screen_size_y(s) - 1) / 2;
486 window_copy_update_selection(wp);
487 window_copy_redraw_screen(wp);
488 break;
489 case MODEKEYCOPY_BOTTOMLINE:
490 data->cx = 0;
491 data->cy = screen_size_y(s) - 1;
492 window_copy_update_selection(wp);
493 window_copy_redraw_screen(wp);
494 break;
495 case MODEKEYCOPY_HISTORYTOP:
496 data->cx = 0;
497 data->cy = 0;
498 data->oy = screen_hsize(data->backing);
499 window_copy_update_selection(wp);
500 window_copy_redraw_screen(wp);
501 break;
502 case MODEKEYCOPY_HISTORYBOTTOM:
503 data->cx = 0;
504 data->cy = screen_size_y(s) - 1;
505 data->oy = 0;
506 window_copy_update_selection(wp);
507 window_copy_redraw_screen(wp);
508 break;
509 case MODEKEYCOPY_STARTSELECTION:
510 window_copy_start_selection(wp);
511 window_copy_redraw_screen(wp);
512 break;
513 case MODEKEYCOPY_COPYLINE:
514 case MODEKEYCOPY_SELECTLINE:
515 window_copy_cursor_start_of_line(wp);
516 /* FALLTHROUGH */
517 case MODEKEYCOPY_COPYENDOFLINE:
518 window_copy_start_selection(wp);
519 for (; np > 1; np--)
520 window_copy_cursor_down(wp, 0);
521 window_copy_cursor_end_of_line(wp);
522 window_copy_redraw_screen(wp);
524 /* If a copy command then copy the selection and exit. */
525 if (sess != NULL &&
526 (cmd == MODEKEYCOPY_COPYLINE ||
527 cmd == MODEKEYCOPY_COPYENDOFLINE)) {
528 window_copy_copy_selection(wp, -1);
529 window_pane_reset_mode(wp);
530 return;
532 break;
533 case MODEKEYCOPY_CLEARSELECTION:
534 window_copy_clear_selection(wp);
535 window_copy_redraw_screen(wp);
536 break;
537 case MODEKEYCOPY_COPYSELECTION:
538 if (sess != NULL) {
539 window_copy_copy_selection(wp, data->numprefix);
540 window_pane_reset_mode(wp);
541 return;
543 break;
544 case MODEKEYCOPY_STARTOFLINE:
545 window_copy_cursor_start_of_line(wp);
546 break;
547 case MODEKEYCOPY_BACKTOINDENTATION:
548 window_copy_cursor_back_to_indentation(wp);
549 break;
550 case MODEKEYCOPY_ENDOFLINE:
551 window_copy_cursor_end_of_line(wp);
552 break;
553 case MODEKEYCOPY_NEXTSPACE:
554 for (; np != 0; np--)
555 window_copy_cursor_next_word(wp, " ");
556 break;
557 case MODEKEYCOPY_NEXTSPACEEND:
558 for (; np != 0; np--)
559 window_copy_cursor_next_word_end(wp, " ");
560 break;
561 case MODEKEYCOPY_NEXTWORD:
562 word_separators =
563 options_get_string(&sess->options, "word-separators");
564 for (; np != 0; np--)
565 window_copy_cursor_next_word(wp, word_separators);
566 break;
567 case MODEKEYCOPY_NEXTWORDEND:
568 word_separators =
569 options_get_string(&sess->options, "word-separators");
570 for (; np != 0; np--)
571 window_copy_cursor_next_word_end(wp, word_separators);
572 break;
573 case MODEKEYCOPY_PREVIOUSSPACE:
574 for (; np != 0; np--)
575 window_copy_cursor_previous_word(wp, " ");
576 break;
577 case MODEKEYCOPY_PREVIOUSWORD:
578 word_separators =
579 options_get_string(&sess->options, "word-separators");
580 for (; np != 0; np--)
581 window_copy_cursor_previous_word(wp, word_separators);
582 break;
583 case MODEKEYCOPY_JUMP:
584 data->inputtype = WINDOW_COPY_JUMPFORWARD;
585 data->inputprompt = "Jump Forward";
586 *data->inputstr = '\0';
587 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
588 return; /* skip numprefix reset */
589 case MODEKEYCOPY_JUMPAGAIN:
590 if (data->jumptype == WINDOW_COPY_JUMPFORWARD) {
591 for (; np != 0; np--)
592 window_copy_cursor_jump(wp);
593 } else if (data->jumptype == WINDOW_COPY_JUMPBACK) {
594 for (; np != 0; np--)
595 window_copy_cursor_jump_back(wp);
596 } else if (data->jumptype == WINDOW_COPY_JUMPTOFORWARD) {
597 for (; np != 0; np--)
598 window_copy_cursor_jump_to(wp);
599 } else if (data->jumptype == WINDOW_COPY_JUMPTOBACK) {
600 for (; np != 0; np--)
601 window_copy_cursor_jump_to_back(wp);
603 break;
604 case MODEKEYCOPY_JUMPREVERSE:
605 if (data->jumptype == WINDOW_COPY_JUMPFORWARD) {
606 for (; np != 0; np--)
607 window_copy_cursor_jump_back(wp);
608 } else if (data->jumptype == WINDOW_COPY_JUMPBACK) {
609 for (; np != 0; np--)
610 window_copy_cursor_jump(wp);
611 } else if (data->jumptype == WINDOW_COPY_JUMPTOFORWARD) {
612 for (; np != 0; np--)
613 window_copy_cursor_jump_to_back(wp);
614 } else if (data->jumptype == WINDOW_COPY_JUMPTOBACK) {
615 for (; np != 0; np--)
616 window_copy_cursor_jump_to(wp);
618 break;
619 case MODEKEYCOPY_JUMPBACK:
620 data->inputtype = WINDOW_COPY_JUMPBACK;
621 data->inputprompt = "Jump Back";
622 *data->inputstr = '\0';
623 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
624 return; /* skip numprefix reset */
625 case MODEKEYCOPY_JUMPTO:
626 data->inputtype = WINDOW_COPY_JUMPTOFORWARD;
627 data->inputprompt = "Jump To";
628 *data->inputstr = '\0';
629 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
630 return; /* skip numprefix reset */
631 case MODEKEYCOPY_JUMPTOBACK:
632 data->inputtype = WINDOW_COPY_JUMPTOBACK;
633 data->inputprompt = "Jump To Back";
634 *data->inputstr = '\0';
635 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
636 return; /* skip numprefix reset */
637 case MODEKEYCOPY_SEARCHUP:
638 data->inputtype = WINDOW_COPY_SEARCHUP;
639 data->inputprompt = "Search Up";
640 goto input_on;
641 case MODEKEYCOPY_SEARCHDOWN:
642 data->inputtype = WINDOW_COPY_SEARCHDOWN;
643 data->inputprompt = "Search Down";
644 goto input_on;
645 case MODEKEYCOPY_SEARCHAGAIN:
646 case MODEKEYCOPY_SEARCHREVERSE:
647 switch (data->searchtype) {
648 case WINDOW_COPY_OFF:
649 case WINDOW_COPY_GOTOLINE:
650 case WINDOW_COPY_JUMPFORWARD:
651 case WINDOW_COPY_JUMPBACK:
652 case WINDOW_COPY_JUMPTOFORWARD:
653 case WINDOW_COPY_JUMPTOBACK:
654 case WINDOW_COPY_NUMERICPREFIX:
655 break;
656 case WINDOW_COPY_SEARCHUP:
657 if (cmd == MODEKEYCOPY_SEARCHAGAIN) {
658 for (; np != 0; np--) {
659 window_copy_search_up(
660 wp, data->searchstr);
662 } else {
663 for (; np != 0; np--) {
664 window_copy_search_down(
665 wp, data->searchstr);
668 break;
669 case WINDOW_COPY_SEARCHDOWN:
670 if (cmd == MODEKEYCOPY_SEARCHAGAIN) {
671 for (; np != 0; np--) {
672 window_copy_search_down(
673 wp, data->searchstr);
675 } else {
676 for (; np != 0; np--) {
677 window_copy_search_up(
678 wp, data->searchstr);
681 break;
683 break;
684 case MODEKEYCOPY_GOTOLINE:
685 data->inputtype = WINDOW_COPY_GOTOLINE;
686 data->inputprompt = "Goto Line";
687 *data->inputstr = '\0';
688 goto input_on;
689 case MODEKEYCOPY_STARTNUMBERPREFIX:
690 key &= KEYC_MASK_KEY;
691 if (key >= '0' && key <= '9') {
692 data->inputtype = WINDOW_COPY_NUMERICPREFIX;
693 data->numprefix = 0;
694 window_copy_key_numeric_prefix(wp, key);
695 return;
697 break;
698 case MODEKEYCOPY_RECTANGLETOGGLE:
699 window_copy_rectangle_toggle(wp);
700 break;
701 default:
702 break;
705 data->numprefix = -1;
706 return;
708 input_on:
709 keys = options_get_number(&wp->window->options, "mode-keys");
710 if (keys == MODEKEY_EMACS)
711 mode_key_init(&data->mdata, &mode_key_tree_emacs_edit);
712 else
713 mode_key_init(&data->mdata, &mode_key_tree_vi_edit);
715 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
716 return;
718 input_off:
719 keys = options_get_number(&wp->window->options, "mode-keys");
720 if (keys == MODEKEY_EMACS)
721 mode_key_init(&data->mdata, &mode_key_tree_emacs_copy);
722 else
723 mode_key_init(&data->mdata, &mode_key_tree_vi_copy);
725 data->inputtype = WINDOW_COPY_OFF;
726 data->inputprompt = NULL;
728 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
732 window_copy_key_input(struct window_pane *wp, int key)
734 struct window_copy_mode_data *data = wp->modedata;
735 struct screen *s = &data->screen;
736 size_t inputlen;
737 int np;
739 switch (mode_key_lookup(&data->mdata, key)) {
740 case MODEKEYEDIT_CANCEL:
741 data->numprefix = -1;
742 return (-1);
743 case MODEKEYEDIT_BACKSPACE:
744 inputlen = strlen(data->inputstr);
745 if (inputlen > 0)
746 data->inputstr[inputlen - 1] = '\0';
747 break;
748 case MODEKEYEDIT_DELETELINE:
749 *data->inputstr = '\0';
750 break;
751 case MODEKEYEDIT_ENTER:
752 np = data->numprefix;
753 if (np <= 0)
754 np = 1;
756 switch (data->inputtype) {
757 case WINDOW_COPY_OFF:
758 case WINDOW_COPY_JUMPFORWARD:
759 case WINDOW_COPY_JUMPBACK:
760 case WINDOW_COPY_JUMPTOFORWARD:
761 case WINDOW_COPY_JUMPTOBACK:
762 case WINDOW_COPY_NUMERICPREFIX:
763 break;
764 case WINDOW_COPY_SEARCHUP:
765 for (; np != 0; np--)
766 window_copy_search_up(wp, data->inputstr);
767 data->searchtype = data->inputtype;
768 data->searchstr = xstrdup(data->inputstr);
769 break;
770 case WINDOW_COPY_SEARCHDOWN:
771 for (; np != 0; np--)
772 window_copy_search_down(wp, data->inputstr);
773 data->searchtype = data->inputtype;
774 data->searchstr = xstrdup(data->inputstr);
775 break;
776 case WINDOW_COPY_GOTOLINE:
777 window_copy_goto_line(wp, data->inputstr);
778 *data->inputstr = '\0';
779 break;
781 data->numprefix = -1;
782 return (1);
783 case MODEKEY_OTHER:
784 if (key < 32 || key > 126)
785 break;
786 inputlen = strlen(data->inputstr) + 2;
788 data->inputstr = xrealloc(data->inputstr, 1, inputlen);
789 data->inputstr[inputlen - 2] = key;
790 data->inputstr[inputlen - 1] = '\0';
791 break;
792 default:
793 break;
796 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
797 return (0);
801 window_copy_key_numeric_prefix(struct window_pane *wp, int key)
803 struct window_copy_mode_data *data = wp->modedata;
804 struct screen *s = &data->screen;
806 key &= KEYC_MASK_KEY;
807 if (key < '0' || key > '9')
808 return 1;
810 if (data->numprefix >= 100) /* no more than three digits */
811 return 0;
812 data->numprefix = data->numprefix * 10 + key - '0';
814 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
815 return 0;
818 /* ARGSUSED */
819 void
820 window_copy_mouse(
821 struct window_pane *wp, struct session *sess, struct mouse_event *m)
823 struct window_copy_mode_data *data = wp->modedata;
824 struct screen *s = &data->screen;
825 u_int i, old_cy;
827 if (m->x >= screen_size_x(s))
828 return;
829 if (m->y >= screen_size_y(s))
830 return;
832 /* If mouse wheel (buttons 4 and 5), scroll. */
833 if ((m->b & MOUSE_45)) {
834 if ((m->b & MOUSE_BUTTON) == MOUSE_1) {
835 for (i = 0; i < 5; i++)
836 window_copy_cursor_up(wp, 0);
837 } else if ((m->b & MOUSE_BUTTON) == MOUSE_2) {
838 old_cy = data->cy;
839 for (i = 0; i < 5; i++)
840 window_copy_cursor_down(wp, 0);
841 if (old_cy == data->cy)
842 goto reset_mode;
844 return;
848 * If already reading motion, move the cursor while buttons are still
849 * pressed, or stop the selection on their release.
851 if (s->mode & MODE_MOUSE_BUTTON) {
852 if ((m->b & MOUSE_BUTTON) != MOUSE_UP) {
853 window_copy_update_cursor(wp, m->x, m->y);
854 if (window_copy_update_selection(wp))
855 window_copy_redraw_screen(wp);
856 return;
858 goto reset_mode;
861 /* Otherwise if other buttons pressed, start selection and motion. */
862 if ((m->b & MOUSE_BUTTON) != MOUSE_UP) {
863 s->mode &= ~MODE_MOUSE_STANDARD;
864 s->mode |= MODE_MOUSE_BUTTON;
866 window_copy_update_cursor(wp, m->x, m->y);
867 window_copy_start_selection(wp);
868 window_copy_redraw_screen(wp);
871 return;
873 reset_mode:
874 s->mode &= ~MODE_MOUSE_BUTTON;
875 s->mode |= MODE_MOUSE_STANDARD;
876 if (sess != NULL) {
877 window_copy_copy_selection(wp, -1);
878 window_pane_reset_mode(wp);
882 void
883 window_copy_scroll_to(struct window_pane *wp, u_int px, u_int py)
885 struct window_copy_mode_data *data = wp->modedata;
886 struct grid *gd = data->backing->grid;
887 u_int offset, gap;
889 data->cx = px;
891 gap = gd->sy / 4;
892 if (py < gd->sy) {
893 offset = 0;
894 data->cy = py;
895 } else if (py > gd->hsize + gd->sy - gap) {
896 offset = gd->hsize;
897 data->cy = py - gd->hsize;
898 } else {
899 offset = py + gap - gd->sy;
900 data->cy = py - offset;
902 data->oy = gd->hsize - offset;
904 window_copy_update_selection(wp);
905 window_copy_redraw_screen(wp);
909 window_copy_search_compare(
910 struct grid *gd, u_int px, u_int py, struct grid *sgd, u_int spx)
912 const struct grid_cell *gc, *sgc;
913 const struct grid_utf8 *gu, *sgu;
915 gc = grid_peek_cell(gd, px, py);
916 sgc = grid_peek_cell(sgd, spx, 0);
918 if ((gc->flags & GRID_FLAG_UTF8) != (sgc->flags & GRID_FLAG_UTF8))
919 return (0);
921 if (gc->flags & GRID_FLAG_UTF8) {
922 gu = grid_peek_utf8(gd, px, py);
923 sgu = grid_peek_utf8(sgd, spx, 0);
924 if (grid_utf8_compare(gu, sgu))
925 return (1);
926 } else {
927 if (gc->data == sgc->data)
928 return (1);
930 return (0);
934 window_copy_search_lr(struct grid *gd,
935 struct grid *sgd, u_int *ppx, u_int py, u_int first, u_int last)
937 u_int ax, bx, px;
939 for (ax = first; ax < last; ax++) {
940 if (ax + sgd->sx >= gd->sx)
941 break;
942 for (bx = 0; bx < sgd->sx; bx++) {
943 px = ax + bx;
944 if (!window_copy_search_compare(gd, px, py, sgd, bx))
945 break;
947 if (bx == sgd->sx) {
948 *ppx = ax;
949 return (1);
952 return (0);
956 window_copy_search_rl(struct grid *gd,
957 struct grid *sgd, u_int *ppx, u_int py, u_int first, u_int last)
959 u_int ax, bx, px;
961 for (ax = last + 1; ax > first; ax--) {
962 if (gd->sx - (ax - 1) < sgd->sx)
963 continue;
964 for (bx = 0; bx < sgd->sx; bx++) {
965 px = ax - 1 + bx;
966 if (!window_copy_search_compare(gd, px, py, sgd, bx))
967 break;
969 if (bx == sgd->sx) {
970 *ppx = ax - 1;
971 return (1);
974 return (0);
977 void
978 window_copy_search_up(struct window_pane *wp, const char *searchstr)
980 struct window_copy_mode_data *data = wp->modedata;
981 struct screen *s = data->backing, ss;
982 struct screen_write_ctx ctx;
983 struct grid *gd = s->grid, *sgd;
984 struct grid_cell gc;
985 size_t searchlen;
986 u_int i, last, fx, fy, px;
987 int utf8flag, n, wrapped;
989 if (*searchstr == '\0')
990 return;
991 utf8flag = options_get_number(&wp->window->options, "utf8");
992 searchlen = screen_write_strlen(utf8flag, "%s", searchstr);
994 screen_init(&ss, searchlen, 1, 0);
995 screen_write_start(&ctx, NULL, &ss);
996 memcpy(&gc, &grid_default_cell, sizeof gc);
997 screen_write_nputs(&ctx, -1, &gc, utf8flag, "%s", searchstr);
998 screen_write_stop(&ctx);
1000 fx = data->cx;
1001 fy = gd->hsize - data->oy + data->cy;
1003 if (fx == 0) {
1004 if (fy == 0)
1005 return;
1006 fx = gd->sx - 1;
1007 fy--;
1008 } else
1009 fx--;
1010 n = wrapped = 0;
1012 retry:
1013 sgd = ss.grid;
1014 for (i = fy + 1; i > 0; i--) {
1015 last = screen_size_x(s);
1016 if (i == fy + 1)
1017 last = fx;
1018 n = window_copy_search_rl(gd, sgd, &px, i - 1, 0, last);
1019 if (n) {
1020 window_copy_scroll_to(wp, px, i - 1);
1021 break;
1024 if (!n && !wrapped) {
1025 fx = gd->sx - 1;
1026 fy = gd->hsize + gd->sy - 1;
1027 wrapped = 1;
1028 goto retry;
1031 screen_free(&ss);
1034 void
1035 window_copy_search_down(struct window_pane *wp, const char *searchstr)
1037 struct window_copy_mode_data *data = wp->modedata;
1038 struct screen *s = data->backing, ss;
1039 struct screen_write_ctx ctx;
1040 struct grid *gd = s->grid, *sgd;
1041 struct grid_cell gc;
1042 size_t searchlen;
1043 u_int i, first, fx, fy, px;
1044 int utf8flag, n, wrapped;
1046 if (*searchstr == '\0')
1047 return;
1048 utf8flag = options_get_number(&wp->window->options, "utf8");
1049 searchlen = screen_write_strlen(utf8flag, "%s", searchstr);
1051 screen_init(&ss, searchlen, 1, 0);
1052 screen_write_start(&ctx, NULL, &ss);
1053 memcpy(&gc, &grid_default_cell, sizeof gc);
1054 screen_write_nputs(&ctx, -1, &gc, utf8flag, "%s", searchstr);
1055 screen_write_stop(&ctx);
1057 fx = data->cx;
1058 fy = gd->hsize - data->oy + data->cy;
1060 if (fx == gd->sx - 1) {
1061 if (fy == gd->hsize + gd->sy)
1062 return;
1063 fx = 0;
1064 fy++;
1065 } else
1066 fx++;
1067 n = wrapped = 0;
1069 retry:
1070 sgd = ss.grid;
1071 for (i = fy + 1; i < gd->hsize + gd->sy; i++) {
1072 first = 0;
1073 if (i == fy + 1)
1074 first = fx;
1075 n = window_copy_search_lr(gd, sgd, &px, i - 1, first, gd->sx);
1076 if (n) {
1077 window_copy_scroll_to(wp, px, i - 1);
1078 break;
1081 if (!n && !wrapped) {
1082 fx = 0;
1083 fy = 0;
1084 wrapped = 1;
1085 goto retry;
1088 screen_free(&ss);
1091 void
1092 window_copy_goto_line(struct window_pane *wp, const char *linestr)
1094 struct window_copy_mode_data *data = wp->modedata;
1095 const char *errstr;
1096 u_int lineno;
1098 lineno = strtonum(linestr, 0, screen_hsize(data->backing), &errstr);
1099 if (errstr != NULL)
1100 return;
1102 data->oy = lineno;
1103 window_copy_update_selection(wp);
1104 window_copy_redraw_screen(wp);
1107 void
1108 window_copy_write_line(
1109 struct window_pane *wp, struct screen_write_ctx *ctx, u_int py)
1111 struct window_copy_mode_data *data = wp->modedata;
1112 struct screen *s = &data->screen;
1113 struct options *oo = &wp->window->options;
1114 struct grid_cell gc;
1115 char hdr[32];
1116 size_t last, xoff = 0, size = 0;
1118 memcpy(&gc, &grid_default_cell, sizeof gc);
1119 colour_set_fg(&gc, options_get_number(oo, "mode-fg"));
1120 colour_set_bg(&gc, options_get_number(oo, "mode-bg"));
1121 gc.attr |= options_get_number(oo, "mode-attr");
1123 last = screen_size_y(s) - 1;
1124 if (py == 0) {
1125 size = xsnprintf(hdr, sizeof hdr,
1126 "[%u/%u]", data->oy, screen_hsize(data->backing));
1127 if (size > screen_size_x(s))
1128 size = screen_size_x(s);
1129 screen_write_cursormove(ctx, screen_size_x(s) - size, 0);
1130 screen_write_puts(ctx, &gc, "%s", hdr);
1131 } else if (py == last && data->inputtype != WINDOW_COPY_OFF) {
1132 if (data->inputtype == WINDOW_COPY_NUMERICPREFIX) {
1133 xoff = size = xsnprintf(hdr, sizeof hdr,
1134 "Repeat: %u", data->numprefix);
1135 } else {
1136 xoff = size = xsnprintf(hdr, sizeof hdr,
1137 "%s: %s", data->inputprompt, data->inputstr);
1139 screen_write_cursormove(ctx, 0, last);
1140 screen_write_puts(ctx, &gc, "%s", hdr);
1141 } else
1142 size = 0;
1144 screen_write_cursormove(ctx, xoff, py);
1145 screen_write_copy(ctx, data->backing, xoff,
1146 (screen_hsize(data->backing) - data->oy) + py,
1147 screen_size_x(s) - size, 1);
1149 if (py == data->cy && data->cx == screen_size_x(s)) {
1150 memcpy(&gc, &grid_default_cell, sizeof gc);
1151 screen_write_cursormove(ctx, screen_size_x(s) - 1, py);
1152 screen_write_putc(ctx, &gc, '$');
1156 void
1157 window_copy_write_lines(
1158 struct window_pane *wp, struct screen_write_ctx *ctx, u_int py, u_int ny)
1160 u_int yy;
1162 for (yy = py; yy < py + ny; yy++)
1163 window_copy_write_line(wp, ctx, py);
1166 void
1167 window_copy_redraw_lines(struct window_pane *wp, u_int py, u_int ny)
1169 struct window_copy_mode_data *data = wp->modedata;
1170 struct screen_write_ctx ctx;
1171 u_int i;
1173 screen_write_start(&ctx, wp, NULL);
1174 for (i = py; i < py + ny; i++)
1175 window_copy_write_line(wp, &ctx, i);
1176 screen_write_cursormove(&ctx, data->cx, data->cy);
1177 screen_write_stop(&ctx);
1180 void
1181 window_copy_redraw_screen(struct window_pane *wp)
1183 struct window_copy_mode_data *data = wp->modedata;
1185 window_copy_redraw_lines(wp, 0, screen_size_y(&data->screen));
1188 void
1189 window_copy_update_cursor(struct window_pane *wp, u_int cx, u_int cy)
1191 struct window_copy_mode_data *data = wp->modedata;
1192 struct screen *s = &data->screen;
1193 struct screen_write_ctx ctx;
1194 u_int old_cx, old_cy;
1196 old_cx = data->cx; old_cy = data->cy;
1197 data->cx = cx; data->cy = cy;
1198 if (old_cx == screen_size_x(s))
1199 window_copy_redraw_lines(wp, old_cy, 1);
1200 if (data->cx == screen_size_x(s))
1201 window_copy_redraw_lines(wp, data->cy, 1);
1202 else {
1203 screen_write_start(&ctx, wp, NULL);
1204 screen_write_cursormove(&ctx, data->cx, data->cy);
1205 screen_write_stop(&ctx);
1209 void
1210 window_copy_start_selection(struct window_pane *wp)
1212 struct window_copy_mode_data *data = wp->modedata;
1213 struct screen *s = &data->screen;
1215 data->selx = data->cx;
1216 data->sely = screen_hsize(data->backing) + data->cy - data->oy;
1218 s->sel.flag = 1;
1219 window_copy_update_selection(wp);
1223 window_copy_update_selection(struct window_pane *wp)
1225 struct window_copy_mode_data *data = wp->modedata;
1226 struct screen *s = &data->screen;
1227 struct options *oo = &wp->window->options;
1228 struct grid_cell gc;
1229 u_int sx, sy, ty, cy;
1231 if (!s->sel.flag)
1232 return (0);
1234 /* Set colours. */
1235 memcpy(&gc, &grid_default_cell, sizeof gc);
1236 colour_set_fg(&gc, options_get_number(oo, "mode-fg"));
1237 colour_set_bg(&gc, options_get_number(oo, "mode-bg"));
1238 gc.attr |= options_get_number(oo, "mode-attr");
1240 /* Find top of screen. */
1241 ty = screen_hsize(data->backing) - data->oy;
1243 /* Adjust the selection. */
1244 sx = data->selx;
1245 sy = data->sely;
1246 if (sy < ty) { /* above screen */
1247 if (!data->rectflag)
1248 sx = 0;
1249 sy = 0;
1250 } else if (sy > ty + screen_size_y(s) - 1) { /* below screen */
1251 if (!data->rectflag)
1252 sx = screen_size_x(s) - 1;
1253 sy = screen_size_y(s) - 1;
1254 } else
1255 sy -= ty;
1256 sy = screen_hsize(s) + sy;
1258 screen_set_selection(s,
1259 sx, sy, data->cx, screen_hsize(s) + data->cy, data->rectflag, &gc);
1261 if (data->rectflag) {
1263 * Can't rely on the caller to redraw the right lines for
1264 * rectangle selection - find the highest line and the number
1265 * of lines, and redraw just past that in both directions
1267 cy = data->cy;
1268 if (sy < cy)
1269 window_copy_redraw_lines(wp, sy, cy - sy + 1);
1270 else
1271 window_copy_redraw_lines(wp, cy, sy - cy + 1);
1274 return (1);
1277 void
1278 window_copy_copy_selection(struct window_pane *wp, int idx)
1280 struct window_copy_mode_data *data = wp->modedata;
1281 struct screen *s = &data->screen;
1282 char *buf;
1283 size_t off;
1284 u_int i, xx, yy, sx, sy, ex, ey, limit;
1285 u_int firstsx, lastex, restex, restsx;
1286 int keys;
1288 if (!s->sel.flag)
1289 return;
1291 buf = xmalloc(1);
1292 off = 0;
1294 *buf = '\0';
1297 * The selection extends from selx,sely to (adjusted) cx,cy on
1298 * the base screen.
1301 /* Find start and end. */
1302 xx = data->cx;
1303 yy = screen_hsize(data->backing) + data->cy - data->oy;
1304 if (yy < data->sely || (yy == data->sely && xx < data->selx)) {
1305 sx = xx; sy = yy;
1306 ex = data->selx; ey = data->sely;
1307 } else {
1308 sx = data->selx; sy = data->sely;
1309 ex = xx; ey = yy;
1312 /* Trim ex to end of line. */
1313 xx = window_copy_find_length(wp, ey);
1314 if (ex > xx)
1315 ex = xx;
1318 * Deal with rectangle-copy if necessary; four situations: start of
1319 * first line (firstsx), end of last line (lastex), start (restsx) and
1320 * end (restex) of all other lines.
1322 xx = screen_size_x(s);
1325 * Behave according to mode-keys. If it is emacs, copy like emacs,
1326 * keeping the top-left-most character, and dropping the
1327 * bottom-right-most, regardless of copy direction. If it is vi, also
1328 * keep bottom-right-most character.
1330 keys = options_get_number(&wp->window->options, "mode-keys");
1331 if (data->rectflag) {
1333 * Need to ignore the column with the cursor in it, which for
1334 * rectangular copy means knowing which side the cursor is on.
1336 if (data->selx < data->cx) {
1337 /* Selection start is on the left. */
1338 if (keys == MODEKEY_EMACS) {
1339 lastex = data->cx;
1340 restex = data->cx;
1342 else {
1343 lastex = data->cx + 1;
1344 restex = data->cx + 1;
1346 firstsx = data->selx;
1347 restsx = data->selx;
1348 } else {
1349 /* Cursor is on the left. */
1350 lastex = data->selx + 1;
1351 restex = data->selx + 1;
1352 firstsx = data->cx;
1353 restsx = data->cx;
1355 } else {
1356 if (keys == MODEKEY_EMACS)
1357 lastex = ex;
1358 else
1359 lastex = ex + 1;
1360 restex = xx;
1361 firstsx = sx;
1362 restsx = 0;
1365 /* Copy the lines. */
1366 if (sy == ey)
1367 window_copy_copy_line(wp, &buf, &off, sy, firstsx, lastex);
1368 else {
1369 window_copy_copy_line(wp, &buf, &off, sy, firstsx, restex);
1370 if (ey - sy > 1) {
1371 for (i = sy + 1; i < ey; i++) {
1372 window_copy_copy_line(
1373 wp, &buf, &off, i, restsx, restex);
1376 window_copy_copy_line(wp, &buf, &off, ey, restsx, lastex);
1379 /* Don't bother if no data. */
1380 if (off == 0) {
1381 xfree(buf);
1382 return;
1384 off--; /* remove final \n */
1386 if (options_get_number(&global_options, "set-clipboard"))
1387 screen_write_setselection(&wp->ictx.ctx, buf, off);
1389 /* Add the buffer to the stack. */
1390 if (idx == -1) {
1391 limit = options_get_number(&global_options, "buffer-limit");
1392 paste_add(&global_buffers, buf, off, limit);
1393 } else
1394 paste_replace(&global_buffers, idx, buf, off);
1397 void
1398 window_copy_copy_line(struct window_pane *wp,
1399 char **buf, size_t *off, u_int sy, u_int sx, u_int ex)
1401 struct window_copy_mode_data *data = wp->modedata;
1402 struct grid *gd = data->backing->grid;
1403 const struct grid_cell *gc;
1404 const struct grid_utf8 *gu;
1405 struct grid_line *gl;
1406 u_int i, xx, wrapped = 0;
1407 size_t size;
1409 if (sx > ex)
1410 return;
1413 * Work out if the line was wrapped at the screen edge and all of it is
1414 * on screen.
1416 gl = &gd->linedata[sy];
1417 if (gl->flags & GRID_LINE_WRAPPED && gl->cellsize <= gd->sx)
1418 wrapped = 1;
1420 /* If the line was wrapped, don't strip spaces (use the full length). */
1421 if (wrapped)
1422 xx = gl->cellsize;
1423 else
1424 xx = window_copy_find_length(wp, sy);
1425 if (ex > xx)
1426 ex = xx;
1427 if (sx > xx)
1428 sx = xx;
1430 if (sx < ex) {
1431 for (i = sx; i < ex; i++) {
1432 gc = grid_peek_cell(gd, i, sy);
1433 if (gc->flags & GRID_FLAG_PADDING)
1434 continue;
1435 if (!(gc->flags & GRID_FLAG_UTF8)) {
1436 *buf = xrealloc(*buf, 1, (*off) + 1);
1437 (*buf)[(*off)++] = gc->data;
1438 } else {
1439 gu = grid_peek_utf8(gd, i, sy);
1440 size = grid_utf8_size(gu);
1441 *buf = xrealloc(*buf, 1, (*off) + size);
1442 *off += grid_utf8_copy(gu, *buf + *off, size);
1447 /* Only add a newline if the line wasn't wrapped. */
1448 if (!wrapped || ex != xx) {
1449 *buf = xrealloc(*buf, 1, (*off) + 1);
1450 (*buf)[(*off)++] = '\n';
1454 void
1455 window_copy_clear_selection(struct window_pane *wp)
1457 struct window_copy_mode_data *data = wp->modedata;
1458 u_int px, py;
1460 screen_clear_selection(&data->screen);
1462 py = screen_hsize(data->backing) + data->cy - data->oy;
1463 px = window_copy_find_length(wp, py);
1464 if (data->cx > px)
1465 window_copy_update_cursor(wp, px, data->cy);
1469 window_copy_in_set(struct window_pane *wp, u_int px, u_int py, const char *set)
1471 struct window_copy_mode_data *data = wp->modedata;
1472 const struct grid_cell *gc;
1474 gc = grid_peek_cell(data->backing->grid, px, py);
1475 if (gc->flags & (GRID_FLAG_PADDING|GRID_FLAG_UTF8))
1476 return (0);
1477 if (gc->data == 0x00 || gc->data == 0x7f)
1478 return (0);
1479 return (strchr(set, gc->data) != NULL);
1482 u_int
1483 window_copy_find_length(struct window_pane *wp, u_int py)
1485 struct window_copy_mode_data *data = wp->modedata;
1486 struct screen *s = data->backing;
1487 const struct grid_cell *gc;
1488 u_int px;
1491 * If the pane has been resized, its grid can contain old overlong
1492 * lines. grid_peek_cell does not allow accessing cells beyond the
1493 * width of the grid, and screen_write_copy treats them as spaces, so
1494 * ignore them here too.
1496 px = s->grid->linedata[py].cellsize;
1497 if (px > screen_size_x(s))
1498 px = screen_size_x(s);
1499 while (px > 0) {
1500 gc = grid_peek_cell(s->grid, px - 1, py);
1501 if (gc->flags & GRID_FLAG_UTF8)
1502 break;
1503 if (gc->data != ' ')
1504 break;
1505 px--;
1507 return (px);
1510 void
1511 window_copy_cursor_start_of_line(struct window_pane *wp)
1513 struct window_copy_mode_data *data = wp->modedata;
1514 struct screen *back_s = data->backing;
1515 struct grid *gd = back_s->grid;
1516 u_int py;
1518 if (data->cx == 0) {
1519 py = screen_hsize(back_s) + data->cy - data->oy;
1520 while (py > 0 && gd->linedata[py-1].flags & GRID_LINE_WRAPPED) {
1521 window_copy_cursor_up(wp, 0);
1522 py = screen_hsize(back_s) + data->cy - data->oy;
1525 window_copy_update_cursor(wp, 0, data->cy);
1526 if (window_copy_update_selection(wp))
1527 window_copy_redraw_lines(wp, data->cy, 1);
1530 void
1531 window_copy_cursor_back_to_indentation(struct window_pane *wp)
1533 struct window_copy_mode_data *data = wp->modedata;
1534 u_int px, py, xx;
1535 const struct grid_cell *gc;
1537 px = 0;
1538 py = screen_hsize(data->backing) + data->cy - data->oy;
1539 xx = window_copy_find_length(wp, py);
1541 while (px < xx) {
1542 gc = grid_peek_cell(data->backing->grid, px, py);
1543 if (gc->flags & GRID_FLAG_UTF8)
1544 break;
1545 if (gc->data != ' ')
1546 break;
1547 px++;
1550 window_copy_update_cursor(wp, px, data->cy);
1551 if (window_copy_update_selection(wp))
1552 window_copy_redraw_lines(wp, data->cy, 1);
1555 void
1556 window_copy_cursor_end_of_line(struct window_pane *wp)
1558 struct window_copy_mode_data *data = wp->modedata;
1559 struct screen *back_s = data->backing;
1560 struct grid *gd = back_s->grid;
1561 u_int px, py;
1563 py = screen_hsize(back_s) + data->cy - data->oy;
1564 px = window_copy_find_length(wp, py);
1566 if (data->cx == px) {
1567 if (data->screen.sel.flag && data->rectflag)
1568 px = screen_size_x(back_s);
1569 if (gd->linedata[py].flags & GRID_LINE_WRAPPED) {
1570 while (py < gd->sy + gd->hsize &&
1571 gd->linedata[py].flags & GRID_LINE_WRAPPED) {
1572 window_copy_cursor_down(wp, 0);
1573 py = screen_hsize(back_s)
1574 + data->cy - data->oy;
1576 px = window_copy_find_length(wp, py);
1579 window_copy_update_cursor(wp, px, data->cy);
1581 if (window_copy_update_selection(wp))
1582 window_copy_redraw_lines(wp, data->cy, 1);
1585 void
1586 window_copy_cursor_left(struct window_pane *wp)
1588 struct window_copy_mode_data *data = wp->modedata;
1590 if (data->cx == 0) {
1591 window_copy_cursor_up(wp, 0);
1592 window_copy_cursor_end_of_line(wp);
1593 } else {
1594 window_copy_update_cursor(wp, data->cx - 1, data->cy);
1595 if (window_copy_update_selection(wp))
1596 window_copy_redraw_lines(wp, data->cy, 1);
1600 void
1601 window_copy_cursor_right(struct window_pane *wp)
1603 struct window_copy_mode_data *data = wp->modedata;
1604 u_int px, py;
1606 if (data->screen.sel.flag && data->rectflag)
1607 px = screen_size_x(&data->screen);
1608 else {
1609 py = screen_hsize(data->backing) + data->cy - data->oy;
1610 px = window_copy_find_length(wp, py);
1613 if (data->cx >= px) {
1614 window_copy_cursor_start_of_line(wp);
1615 window_copy_cursor_down(wp, 0);
1616 } else {
1617 window_copy_update_cursor(wp, data->cx + 1, data->cy);
1618 if (window_copy_update_selection(wp))
1619 window_copy_redraw_lines(wp, data->cy, 1);
1623 void
1624 window_copy_cursor_up(struct window_pane *wp, int scroll_only)
1626 struct window_copy_mode_data *data = wp->modedata;
1627 struct screen *s = &data->screen;
1628 u_int ox, oy, px, py;
1630 oy = screen_hsize(data->backing) + data->cy - data->oy;
1631 ox = window_copy_find_length(wp, oy);
1632 if (ox != 0) {
1633 data->lastcx = data->cx;
1634 data->lastsx = ox;
1637 data->cx = data->lastcx;
1638 if (scroll_only || data->cy == 0) {
1639 window_copy_scroll_down(wp, 1);
1640 if (scroll_only) {
1641 if (data->cy == screen_size_y(s) - 1)
1642 window_copy_redraw_lines(wp, data->cy, 1);
1643 else
1644 window_copy_redraw_lines(wp, data->cy, 2);
1646 } else {
1647 window_copy_update_cursor(wp, data->cx, data->cy - 1);
1648 if (window_copy_update_selection(wp)) {
1649 if (data->cy == screen_size_y(s) - 1)
1650 window_copy_redraw_lines(wp, data->cy, 1);
1651 else
1652 window_copy_redraw_lines(wp, data->cy, 2);
1656 if (!data->screen.sel.flag || !data->rectflag) {
1657 py = screen_hsize(data->backing) + data->cy - data->oy;
1658 px = window_copy_find_length(wp, py);
1659 if ((data->cx >= data->lastsx && data->cx != px) ||
1660 data->cx > px)
1661 window_copy_cursor_end_of_line(wp);
1665 void
1666 window_copy_cursor_down(struct window_pane *wp, int scroll_only)
1668 struct window_copy_mode_data *data = wp->modedata;
1669 struct screen *s = &data->screen;
1670 u_int ox, oy, px, py;
1672 oy = screen_hsize(data->backing) + data->cy - data->oy;
1673 ox = window_copy_find_length(wp, oy);
1674 if (ox != 0) {
1675 data->lastcx = data->cx;
1676 data->lastsx = ox;
1679 data->cx = data->lastcx;
1680 if (scroll_only || data->cy == screen_size_y(s) - 1) {
1681 window_copy_scroll_up(wp, 1);
1682 if (scroll_only && data->cy > 0)
1683 window_copy_redraw_lines(wp, data->cy - 1, 2);
1684 } else {
1685 window_copy_update_cursor(wp, data->cx, data->cy + 1);
1686 if (window_copy_update_selection(wp))
1687 window_copy_redraw_lines(wp, data->cy - 1, 2);
1690 if (!data->screen.sel.flag || !data->rectflag) {
1691 py = screen_hsize(data->backing) + data->cy - data->oy;
1692 px = window_copy_find_length(wp, py);
1693 if ((data->cx >= data->lastsx && data->cx != px) ||
1694 data->cx > px)
1695 window_copy_cursor_end_of_line(wp);
1699 void
1700 window_copy_cursor_jump(struct window_pane *wp)
1702 struct window_copy_mode_data *data = wp->modedata;
1703 struct screen *back_s = data->backing;
1704 const struct grid_cell *gc;
1705 u_int px, py, xx;
1707 px = data->cx + 1;
1708 py = screen_hsize(back_s) + data->cy - data->oy;
1709 xx = window_copy_find_length(wp, py);
1711 while (px < xx) {
1712 gc = grid_peek_cell(back_s->grid, px, py);
1713 if ((gc->flags & (GRID_FLAG_PADDING|GRID_FLAG_UTF8)) == 0
1714 && gc->data == data->jumpchar) {
1716 window_copy_update_cursor(wp, px, data->cy);
1717 if (window_copy_update_selection(wp))
1718 window_copy_redraw_lines(wp, data->cy, 1);
1719 return;
1721 px++;
1725 void
1726 window_copy_cursor_jump_back(struct window_pane *wp)
1728 struct window_copy_mode_data *data = wp->modedata;
1729 struct screen *back_s = data->backing;
1730 const struct grid_cell *gc;
1731 u_int px, py;
1733 px = data->cx;
1734 py = screen_hsize(back_s) + data->cy - data->oy;
1736 if (px > 0)
1737 px--;
1739 for (;;) {
1740 gc = grid_peek_cell(back_s->grid, px, py);
1741 if ((gc->flags & (GRID_FLAG_PADDING|GRID_FLAG_UTF8)) == 0
1742 && gc->data == data->jumpchar) {
1744 window_copy_update_cursor(wp, px, data->cy);
1745 if (window_copy_update_selection(wp))
1746 window_copy_redraw_lines(wp, data->cy, 1);
1747 return;
1749 if (px == 0)
1750 break;
1751 px--;
1755 void
1756 window_copy_cursor_jump_to(struct window_pane *wp)
1758 struct window_copy_mode_data *data = wp->modedata;
1759 struct screen *back_s = data->backing;
1760 const struct grid_cell *gc;
1761 u_int px, py, xx;
1763 px = data->cx + 1;
1764 py = screen_hsize(back_s) + data->cy - data->oy;
1765 xx = window_copy_find_length(wp, py);
1767 while (px < xx) {
1768 gc = grid_peek_cell(back_s->grid, px, py);
1769 if ((gc->flags & (GRID_FLAG_PADDING|GRID_FLAG_UTF8)) == 0
1770 && gc->data == data->jumpchar) {
1772 window_copy_update_cursor(wp, px - 1, data->cy);
1773 if (window_copy_update_selection(wp))
1774 window_copy_redraw_lines(wp, data->cy, 1);
1775 return;
1777 px++;
1781 void
1782 window_copy_cursor_jump_to_back(struct window_pane *wp)
1784 struct window_copy_mode_data *data = wp->modedata;
1785 struct screen *back_s = data->backing;
1786 const struct grid_cell *gc;
1787 u_int px, py;
1789 px = data->cx;
1790 py = screen_hsize(back_s) + data->cy - data->oy;
1792 if (px > 0)
1793 px--;
1795 for (;;) {
1796 gc = grid_peek_cell(back_s->grid, px, py);
1797 if ((gc->flags & (GRID_FLAG_PADDING|GRID_FLAG_UTF8)) == 0
1798 && gc->data == data->jumpchar) {
1800 window_copy_update_cursor(wp, px + 1, data->cy);
1801 if (window_copy_update_selection(wp))
1802 window_copy_redraw_lines(wp, data->cy, 1);
1803 return;
1805 if (px == 0)
1806 break;
1807 px--;
1811 void
1812 window_copy_cursor_next_word(struct window_pane *wp, const char *separators)
1814 struct window_copy_mode_data *data = wp->modedata;
1815 struct screen *back_s = data->backing;
1816 u_int px, py, xx, yy;
1817 int expected = 0;
1819 px = data->cx;
1820 py = screen_hsize(back_s) + data->cy - data->oy;
1821 xx = window_copy_find_length(wp, py);
1822 yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
1825 * First skip past any nonword characters and then any word characters.
1827 * expected is initially set to 0 for the former and then 1 for the
1828 * latter.
1830 do {
1831 while (px > xx ||
1832 window_copy_in_set(wp, px, py, separators) == expected) {
1833 /* Move down if we're past the end of the line. */
1834 if (px > xx) {
1835 if (py == yy)
1836 return;
1837 window_copy_cursor_down(wp, 0);
1838 px = 0;
1840 py = screen_hsize(back_s) + data->cy - data->oy;
1841 xx = window_copy_find_length(wp, py);
1842 } else
1843 px++;
1845 expected = !expected;
1846 } while (expected == 1);
1848 window_copy_update_cursor(wp, px, data->cy);
1849 if (window_copy_update_selection(wp))
1850 window_copy_redraw_lines(wp, data->cy, 1);
1853 void
1854 window_copy_cursor_next_word_end(struct window_pane *wp, const char *separators)
1856 struct window_copy_mode_data *data = wp->modedata;
1857 struct screen *back_s = data->backing;
1858 u_int px, py, xx, yy;
1859 int expected = 1;
1861 px = data->cx;
1862 py = screen_hsize(back_s) + data->cy - data->oy;
1863 xx = window_copy_find_length(wp, py);
1864 yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
1867 * First skip past any word characters, then any nonword characters.
1869 * expected is initially set to 1 for the former and then 0 for the
1870 * latter.
1872 do {
1873 while (px > xx ||
1874 window_copy_in_set(wp, px, py, separators) == expected) {
1875 /* Move down if we're past the end of the line. */
1876 if (px > xx) {
1877 if (py == yy)
1878 return;
1879 window_copy_cursor_down(wp, 0);
1880 px = 0;
1882 py = screen_hsize(back_s) + data->cy - data->oy;
1883 xx = window_copy_find_length(wp, py);
1884 } else
1885 px++;
1887 expected = !expected;
1888 } while (expected == 0);
1890 window_copy_update_cursor(wp, px, data->cy);
1891 if (window_copy_update_selection(wp))
1892 window_copy_redraw_lines(wp, data->cy, 1);
1895 /* Move to the previous place where a word begins. */
1896 void
1897 window_copy_cursor_previous_word(struct window_pane *wp, const char *separators)
1899 struct window_copy_mode_data *data = wp->modedata;
1900 u_int px, py;
1902 px = data->cx;
1903 py = screen_hsize(data->backing) + data->cy - data->oy;
1905 /* Move back to the previous word character. */
1906 for (;;) {
1907 if (px > 0) {
1908 px--;
1909 if (!window_copy_in_set(wp, px, py, separators))
1910 break;
1911 } else {
1912 if (data->cy == 0 &&
1913 (screen_hsize(data->backing) == 0 ||
1914 data->oy >= screen_hsize(data->backing) - 1))
1915 goto out;
1916 window_copy_cursor_up(wp, 0);
1918 py = screen_hsize(data->backing) + data->cy - data->oy;
1919 px = window_copy_find_length(wp, py);
1923 /* Move back to the beginning of this word. */
1924 while (px > 0 && !window_copy_in_set(wp, px - 1, py, separators))
1925 px--;
1927 out:
1928 window_copy_update_cursor(wp, px, data->cy);
1929 if (window_copy_update_selection(wp))
1930 window_copy_redraw_lines(wp, data->cy, 1);
1933 void
1934 window_copy_scroll_up(struct window_pane *wp, u_int ny)
1936 struct window_copy_mode_data *data = wp->modedata;
1937 struct screen *s = &data->screen;
1938 struct screen_write_ctx ctx;
1940 if (data->oy < ny)
1941 ny = data->oy;
1942 if (ny == 0)
1943 return;
1944 data->oy -= ny;
1946 screen_write_start(&ctx, wp, NULL);
1947 screen_write_cursormove(&ctx, 0, 0);
1948 screen_write_deleteline(&ctx, ny);
1949 window_copy_write_lines(wp, &ctx, screen_size_y(s) - ny, ny);
1950 window_copy_write_line(wp, &ctx, 0);
1951 if (screen_size_y(s) > 1)
1952 window_copy_write_line(wp, &ctx, 1);
1953 if (screen_size_y(s) > 3)
1954 window_copy_write_line(wp, &ctx, screen_size_y(s) - 2);
1955 if (s->sel.flag && screen_size_y(s) > ny) {
1956 window_copy_update_selection(wp);
1957 window_copy_write_line(wp, &ctx, screen_size_y(s) - ny - 1);
1959 screen_write_cursormove(&ctx, data->cx, data->cy);
1960 window_copy_update_selection(wp);
1961 screen_write_stop(&ctx);
1964 void
1965 window_copy_scroll_down(struct window_pane *wp, u_int ny)
1967 struct window_copy_mode_data *data = wp->modedata;
1968 struct screen *s = &data->screen;
1969 struct screen_write_ctx ctx;
1971 if (ny > screen_hsize(data->backing))
1972 return;
1974 if (data->oy > screen_hsize(data->backing) - ny)
1975 ny = screen_hsize(data->backing) - data->oy;
1976 if (ny == 0)
1977 return;
1978 data->oy += ny;
1980 screen_write_start(&ctx, wp, NULL);
1981 screen_write_cursormove(&ctx, 0, 0);
1982 screen_write_insertline(&ctx, ny);
1983 window_copy_write_lines(wp, &ctx, 0, ny);
1984 if (s->sel.flag && screen_size_y(s) > ny) {
1985 window_copy_update_selection(wp);
1986 window_copy_write_line(wp, &ctx, ny);
1987 } else if (ny == 1) /* nuke position */
1988 window_copy_write_line(wp, &ctx, 1);
1989 screen_write_cursormove(&ctx, data->cx, data->cy);
1990 window_copy_update_selection(wp);
1991 screen_write_stop(&ctx);
1994 void
1995 window_copy_rectangle_toggle(struct window_pane *wp)
1997 struct window_copy_mode_data *data = wp->modedata;
1998 u_int px, py;
2000 data->rectflag = !data->rectflag;
2002 py = screen_hsize(data->backing) + data->cy - data->oy;
2003 px = window_copy_find_length(wp, py);
2004 if (data->cx > px)
2005 window_copy_update_cursor(wp, px, data->cy);
2007 window_copy_update_selection(wp);
2008 window_copy_redraw_screen(wp);