Plug memory leak, from Tiago Cunha.
[tmux-openbsd.git] / window-copy.c
bloba129118cc917283214a7fdc4c2b4d8eaf8ea2b1b
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 *);
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_next_word(struct window_pane *, const char *);
71 void window_copy_cursor_next_word_end(struct window_pane *, const char *);
72 void window_copy_cursor_previous_word(struct window_pane *, const char *);
73 void window_copy_scroll_up(struct window_pane *, u_int);
74 void window_copy_scroll_down(struct window_pane *, u_int);
75 void window_copy_rectangle_toggle(struct window_pane *);
77 const struct window_mode window_copy_mode = {
78 window_copy_init,
79 window_copy_free,
80 window_copy_resize,
81 window_copy_key,
82 window_copy_mouse,
83 NULL,
86 enum window_copy_input_type {
87 WINDOW_COPY_OFF,
88 WINDOW_COPY_NUMERICPREFIX,
89 WINDOW_COPY_SEARCHUP,
90 WINDOW_COPY_SEARCHDOWN,
91 WINDOW_COPY_JUMPFORWARD,
92 WINDOW_COPY_JUMPBACK,
93 WINDOW_COPY_GOTOLINE,
97 * Copy-mode's visible screen (the "screen" field) is filled from one of
98 * two sources: the original contents of the pane (used when we
99 * actually enter via the "copy-mode" command, to copy the contents of
100 * the current pane), or else a series of lines containing the output
101 * from an output-writing tmux command (such as any of the "show-*" or
102 * "list-*" commands).
104 * In either case, the full content of the copy-mode grid is pointed at
105 * by the "backing" field, and is copied into "screen" as needed (that
106 * is, when scrolling occurs). When copy-mode is backed by a pane,
107 * backing points directly at that pane's screen structure (&wp->base);
108 * when backed by a list of output-lines from a command, it points at
109 * a newly-allocated screen structure (which is deallocated when the
110 * mode ends).
112 struct window_copy_mode_data {
113 struct screen screen;
115 struct screen *backing;
116 int backing_written; /* backing display has started */
118 struct mode_key_data mdata;
120 u_int oy;
122 u_int selx;
123 u_int sely;
125 u_int rectflag; /* are we in rectangle copy mode? */
127 u_int cx;
128 u_int cy;
130 u_int lastcx; /* position in last line with content */
131 u_int lastsx; /* size of last line with content */
133 enum window_copy_input_type inputtype;
134 const char *inputprompt;
135 char *inputstr;
137 u_int numprefix;
139 enum window_copy_input_type searchtype;
140 char *searchstr;
142 enum window_copy_input_type jumptype;
143 char jumpchar;
146 struct screen *
147 window_copy_init(struct window_pane *wp)
149 struct window_copy_mode_data *data;
150 struct screen *s;
151 int keys;
153 wp->modedata = data = xmalloc(sizeof *data);
154 data->oy = 0;
155 data->cx = 0;
156 data->cy = 0;
158 data->lastcx = 0;
159 data->lastsx = 0;
161 data->backing_written = 0;
163 data->rectflag = 0;
165 data->inputtype = WINDOW_COPY_OFF;
166 data->inputprompt = NULL;
167 data->inputstr = xstrdup("");
168 data->numprefix = 0;
170 data->searchtype = WINDOW_COPY_OFF;
171 data->searchstr = NULL;
173 if (wp->fd != -1)
174 bufferevent_disable(wp->event, EV_READ|EV_WRITE);
176 data->jumptype = WINDOW_COPY_OFF;
177 data->jumpchar = '\0';
179 s = &data->screen;
180 screen_init(s, screen_size_x(&wp->base), screen_size_y(&wp->base), 0);
181 if (options_get_number(&wp->window->options, "mode-mouse"))
182 s->mode |= MODE_MOUSE_STANDARD;
184 keys = options_get_number(&wp->window->options, "mode-keys");
185 if (keys == MODEKEY_EMACS)
186 mode_key_init(&data->mdata, &mode_key_tree_emacs_copy);
187 else
188 mode_key_init(&data->mdata, &mode_key_tree_vi_copy);
190 data->backing = NULL;
192 return (s);
195 void
196 window_copy_init_from_pane(struct window_pane *wp)
198 struct window_copy_mode_data *data = wp->modedata;
199 struct screen *s = &data->screen;
200 struct screen_write_ctx ctx;
201 u_int i;
203 if (wp->mode != &window_copy_mode)
204 fatalx("not in copy mode");
206 data->backing = &wp->base;
207 data->cx = data->backing->cx;
208 data->cy = data->backing->cy;
210 s->cx = data->cx;
211 s->cy = data->cy;
213 screen_write_start(&ctx, NULL, s);
214 for (i = 0; i < screen_size_y(s); i++)
215 window_copy_write_line(wp, &ctx, i);
216 screen_write_cursormove(&ctx, data->cx, data->cy);
217 screen_write_stop(&ctx);
220 void
221 window_copy_init_for_output(struct window_pane *wp)
223 struct window_copy_mode_data *data = wp->modedata;
225 data->backing = xmalloc(sizeof *data->backing);
226 screen_init(data->backing, screen_size_x(&wp->base),
227 screen_size_y(&wp->base), UINT_MAX);
228 data->backing->mode &= ~MODE_WRAP;
231 void
232 window_copy_free(struct window_pane *wp)
234 struct window_copy_mode_data *data = wp->modedata;
236 if (wp->fd != -1)
237 bufferevent_enable(wp->event, EV_READ|EV_WRITE);
239 if (data->searchstr != NULL)
240 xfree(data->searchstr);
241 xfree(data->inputstr);
243 if (data->backing != &wp->base) {
244 screen_free(data->backing);
245 xfree(data->backing);
247 screen_free(&data->screen);
249 xfree(data);
252 void
253 window_copy_add(struct window_pane *wp, const char *fmt, ...)
255 va_list ap;
257 va_start(ap, fmt);
258 window_copy_vadd(wp, fmt, ap);
259 va_end(ap);
262 void
263 window_copy_vadd(struct window_pane *wp, const char *fmt, va_list ap)
265 struct window_copy_mode_data *data = wp->modedata;
266 struct screen *backing = data->backing;
267 struct screen_write_ctx back_ctx, ctx;
268 struct grid_cell gc;
269 int utf8flag;
270 u_int old_hsize;
272 if (backing == &wp->base)
273 return;
275 utf8flag = options_get_number(&wp->window->options, "utf8");
276 memcpy(&gc, &grid_default_cell, sizeof gc);
278 old_hsize = screen_hsize(data->backing);
279 screen_write_start(&back_ctx, NULL, backing);
280 if (data->backing_written) {
282 * On the second or later line, do a CRLF before writing
283 * (so it's on a new line).
285 screen_write_carriagereturn(&back_ctx);
286 screen_write_linefeed(&back_ctx, 0);
287 } else
288 data->backing_written = 1;
289 screen_write_vnputs(&back_ctx, 0, &gc, utf8flag, fmt, ap);
290 screen_write_stop(&back_ctx);
292 data->oy += screen_hsize(data->backing) - old_hsize;
294 screen_write_start(&ctx, wp, &data->screen);
297 * If the history has changed, draw the top line.
298 * (If there's any history at all, it has changed.)
300 if (screen_hsize(data->backing))
301 window_copy_redraw_lines(wp, 0, 1);
303 /* Write the line, if it's visible. */
304 if (backing->cy + data->oy < screen_size_y(backing))
305 window_copy_redraw_lines(wp, backing->cy, 1);
307 screen_write_stop(&ctx);
310 void
311 window_copy_pageup(struct window_pane *wp)
313 struct window_copy_mode_data *data = wp->modedata;
314 struct screen *s = &data->screen;
315 u_int n;
317 n = 1;
318 if (screen_size_y(s) > 2)
319 n = screen_size_y(s) - 2;
320 if (data->oy + n > screen_hsize(data->backing))
321 data->oy = screen_hsize(data->backing);
322 else
323 data->oy += n;
324 window_copy_update_selection(wp);
325 window_copy_redraw_screen(wp);
328 void
329 window_copy_resize(struct window_pane *wp, u_int sx, u_int sy)
331 struct window_copy_mode_data *data = wp->modedata;
332 struct screen *s = &data->screen;
333 struct screen_write_ctx ctx;
335 screen_resize(s, sx, sy);
336 if (data->backing != &wp->base)
337 screen_resize(data->backing, sx, sy);
339 if (data->cy > sy - 1)
340 data->cy = sy - 1;
341 if (data->cx > sx)
342 data->cx = sx;
343 if (data->oy > screen_hsize(data->backing))
344 data->oy = screen_hsize(data->backing);
346 window_copy_clear_selection(wp);
348 screen_write_start(&ctx, NULL, s);
349 window_copy_write_lines(wp, &ctx, 0, screen_size_y(s) - 1);
350 screen_write_stop(&ctx);
352 window_copy_redraw_screen(wp);
355 void
356 window_copy_key(struct window_pane *wp, struct session *sess, int key)
358 const char *word_separators;
359 struct window_copy_mode_data *data = wp->modedata;
360 struct screen *s = &data->screen;
361 u_int n, np;
362 int keys;
363 enum mode_key_cmd cmd;
365 np = data->numprefix;
366 if (np == 0)
367 np = 1;
369 if (data->inputtype == WINDOW_COPY_JUMPFORWARD ||
370 data->inputtype == WINDOW_COPY_JUMPBACK) {
371 /* Ignore keys with modifiers. */
372 if ((key & KEYC_MASK_MOD) == 0) {
373 data->jumpchar = key;
374 if (data->inputtype == WINDOW_COPY_JUMPFORWARD) {
375 for (; np != 0; np--)
376 window_copy_cursor_jump(wp);
377 } else {
378 for (; np != 0; np--)
379 window_copy_cursor_jump_back(wp);
382 data->jumptype = data->inputtype;
383 data->inputtype = WINDOW_COPY_OFF;
384 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
385 return;
386 } else if (data->inputtype == WINDOW_COPY_NUMERICPREFIX) {
387 if (window_copy_key_numeric_prefix(wp, key) == 0)
388 return;
389 data->inputtype = WINDOW_COPY_OFF;
390 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
391 } else if (data->inputtype != WINDOW_COPY_OFF) {
392 if (window_copy_key_input(wp, key) != 0)
393 goto input_off;
394 return;
397 cmd = mode_key_lookup(&data->mdata, key);
398 switch (cmd) {
399 case MODEKEYCOPY_CANCEL:
400 window_pane_reset_mode(wp);
401 return;
402 case MODEKEYCOPY_LEFT:
403 for (; np != 0; np--)
404 window_copy_cursor_left(wp);
405 break;
406 case MODEKEYCOPY_RIGHT:
407 for (; np != 0; np--)
408 window_copy_cursor_right(wp);
409 break;
410 case MODEKEYCOPY_UP:
411 for (; np != 0; np--)
412 window_copy_cursor_up(wp, 0);
413 break;
414 case MODEKEYCOPY_DOWN:
415 for (; np != 0; np--)
416 window_copy_cursor_down(wp, 0);
417 break;
418 case MODEKEYCOPY_SCROLLUP:
419 for (; np != 0; np--)
420 window_copy_cursor_up(wp, 1);
421 break;
422 case MODEKEYCOPY_SCROLLDOWN:
423 for (; np != 0; np--)
424 window_copy_cursor_down(wp, 1);
425 break;
426 case MODEKEYCOPY_PREVIOUSPAGE:
427 for (; np != 0; np--)
428 window_copy_pageup(wp);
429 break;
430 case MODEKEYCOPY_NEXTPAGE:
431 n = 1;
432 if (screen_size_y(s) > 2)
433 n = screen_size_y(s) - 2;
434 for (; np != 0; np--) {
435 if (data->oy < n)
436 data->oy = 0;
437 else
438 data->oy -= n;
440 window_copy_update_selection(wp);
441 window_copy_redraw_screen(wp);
442 break;
443 case MODEKEYCOPY_HALFPAGEUP:
444 n = screen_size_y(s) / 2;
445 for (; np != 0; np--) {
446 if (data->oy + n > screen_hsize(data->backing))
447 data->oy = screen_hsize(data->backing);
448 else
449 data->oy += n;
451 window_copy_update_selection(wp);
452 window_copy_redraw_screen(wp);
453 break;
454 case MODEKEYCOPY_HALFPAGEDOWN:
455 n = screen_size_y(s) / 2;
456 for (; np != 0; np--) {
457 if (data->oy < n)
458 data->oy = 0;
459 else
460 data->oy -= n;
462 window_copy_update_selection(wp);
463 window_copy_redraw_screen(wp);
464 break;
465 case MODEKEYCOPY_TOPLINE:
466 data->cx = 0;
467 data->cy = 0;
468 window_copy_update_selection(wp);
469 window_copy_redraw_screen(wp);
470 break;
471 case MODEKEYCOPY_MIDDLELINE:
472 data->cx = 0;
473 data->cy = (screen_size_y(s) - 1) / 2;
474 window_copy_update_selection(wp);
475 window_copy_redraw_screen(wp);
476 break;
477 case MODEKEYCOPY_BOTTOMLINE:
478 data->cx = 0;
479 data->cy = screen_size_y(s) - 1;
480 window_copy_update_selection(wp);
481 window_copy_redraw_screen(wp);
482 break;
483 case MODEKEYCOPY_HISTORYTOP:
484 data->cx = 0;
485 data->cy = 0;
486 data->oy = screen_hsize(data->backing);
487 window_copy_update_selection(wp);
488 window_copy_redraw_screen(wp);
489 break;
490 case MODEKEYCOPY_HISTORYBOTTOM:
491 data->cx = 0;
492 data->cy = screen_size_y(s) - 1;
493 data->oy = 0;
494 window_copy_update_selection(wp);
495 window_copy_redraw_screen(wp);
496 break;
497 case MODEKEYCOPY_STARTSELECTION:
498 window_copy_start_selection(wp);
499 window_copy_redraw_screen(wp);
500 break;
501 case MODEKEYCOPY_COPYLINE:
502 case MODEKEYCOPY_SELECTLINE:
503 window_copy_cursor_start_of_line(wp);
504 /* FALLTHROUGH */
505 case MODEKEYCOPY_COPYENDOFLINE:
506 window_copy_start_selection(wp);
507 for (; np > 1; np--)
508 window_copy_cursor_down(wp, 0);
509 window_copy_cursor_end_of_line(wp);
510 window_copy_redraw_screen(wp);
512 /* If a copy command then copy the selection and exit. */
513 if (sess != NULL &&
514 (cmd == MODEKEYCOPY_COPYLINE ||
515 cmd == MODEKEYCOPY_COPYENDOFLINE)) {
516 window_copy_copy_selection(wp);
517 window_pane_reset_mode(wp);
518 return;
520 break;
521 case MODEKEYCOPY_CLEARSELECTION:
522 window_copy_clear_selection(wp);
523 window_copy_redraw_screen(wp);
524 break;
525 case MODEKEYCOPY_COPYSELECTION:
526 if (sess != NULL) {
527 window_copy_copy_selection(wp);
528 window_pane_reset_mode(wp);
529 return;
531 break;
532 case MODEKEYCOPY_STARTOFLINE:
533 window_copy_cursor_start_of_line(wp);
534 break;
535 case MODEKEYCOPY_BACKTOINDENTATION:
536 window_copy_cursor_back_to_indentation(wp);
537 break;
538 case MODEKEYCOPY_ENDOFLINE:
539 window_copy_cursor_end_of_line(wp);
540 break;
541 case MODEKEYCOPY_NEXTSPACE:
542 for (; np != 0; np--)
543 window_copy_cursor_next_word(wp, " ");
544 break;
545 case MODEKEYCOPY_NEXTSPACEEND:
546 for (; np != 0; np--)
547 window_copy_cursor_next_word_end(wp, " ");
548 break;
549 case MODEKEYCOPY_NEXTWORD:
550 word_separators =
551 options_get_string(&wp->window->options, "word-separators");
552 for (; np != 0; np--)
553 window_copy_cursor_next_word(wp, word_separators);
554 break;
555 case MODEKEYCOPY_NEXTWORDEND:
556 word_separators =
557 options_get_string(&wp->window->options, "word-separators");
558 for (; np != 0; np--)
559 window_copy_cursor_next_word_end(wp, word_separators);
560 break;
561 case MODEKEYCOPY_PREVIOUSSPACE:
562 for (; np != 0; np--)
563 window_copy_cursor_previous_word(wp, " ");
564 break;
565 case MODEKEYCOPY_PREVIOUSWORD:
566 word_separators =
567 options_get_string(&wp->window->options, "word-separators");
568 for (; np != 0; np--)
569 window_copy_cursor_previous_word(wp, word_separators);
570 break;
571 case MODEKEYCOPY_JUMP:
572 data->inputtype = WINDOW_COPY_JUMPFORWARD;
573 data->inputprompt = "Jump Forward";
574 *data->inputstr = '\0';
575 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
576 return; /* skip numprefix reset */
577 case MODEKEYCOPY_JUMPAGAIN:
578 if (data->jumptype == WINDOW_COPY_JUMPFORWARD) {
579 for (; np != 0; np--)
580 window_copy_cursor_jump(wp);
581 } else if (data->jumptype == WINDOW_COPY_JUMPBACK) {
582 for (; np != 0; np--)
583 window_copy_cursor_jump_back(wp);
585 break;
586 case MODEKEYCOPY_JUMPREVERSE:
587 if (data->jumptype == WINDOW_COPY_JUMPFORWARD) {
588 for (; np != 0; np--)
589 window_copy_cursor_jump_back(wp);
590 } else if (data->jumptype == WINDOW_COPY_JUMPBACK) {
591 for (; np != 0; np--)
592 window_copy_cursor_jump(wp);
594 break;
595 case MODEKEYCOPY_JUMPBACK:
596 data->inputtype = WINDOW_COPY_JUMPBACK;
597 data->inputprompt = "Jump Back";
598 *data->inputstr = '\0';
599 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
600 return; /* skip numprefix reset */
601 case MODEKEYCOPY_SEARCHUP:
602 data->inputtype = WINDOW_COPY_SEARCHUP;
603 data->inputprompt = "Search Up";
604 goto input_on;
605 case MODEKEYCOPY_SEARCHDOWN:
606 data->inputtype = WINDOW_COPY_SEARCHDOWN;
607 data->inputprompt = "Search Down";
608 goto input_on;
609 case MODEKEYCOPY_SEARCHAGAIN:
610 case MODEKEYCOPY_SEARCHREVERSE:
611 switch (data->searchtype) {
612 case WINDOW_COPY_OFF:
613 case WINDOW_COPY_GOTOLINE:
614 case WINDOW_COPY_JUMPFORWARD:
615 case WINDOW_COPY_JUMPBACK:
616 case WINDOW_COPY_NUMERICPREFIX:
617 break;
618 case WINDOW_COPY_SEARCHUP:
619 if (cmd == MODEKEYCOPY_SEARCHAGAIN) {
620 for (; np != 0; np--) {
621 window_copy_search_up(
622 wp, data->searchstr);
624 } else {
625 for (; np != 0; np--) {
626 window_copy_search_down(
627 wp, data->searchstr);
630 break;
631 case WINDOW_COPY_SEARCHDOWN:
632 if (cmd == MODEKEYCOPY_SEARCHAGAIN) {
633 for (; np != 0; np--) {
634 window_copy_search_down(
635 wp, data->searchstr);
637 } else {
638 for (; np != 0; np--) {
639 window_copy_search_up(
640 wp, data->searchstr);
643 break;
645 break;
646 case MODEKEYCOPY_GOTOLINE:
647 data->inputtype = WINDOW_COPY_GOTOLINE;
648 data->inputprompt = "Goto Line";
649 *data->inputstr = '\0';
650 goto input_on;
651 case MODEKEYCOPY_STARTNUMBERPREFIX:
652 key &= KEYC_MASK_KEY;
653 if (key >= '0' && key <= '9') {
654 data->inputtype = WINDOW_COPY_NUMERICPREFIX;
655 data->numprefix = 0;
656 window_copy_key_numeric_prefix(wp, key);
657 return;
659 break;
660 case MODEKEYCOPY_RECTANGLETOGGLE:
661 window_copy_rectangle_toggle(wp);
662 break;
663 default:
664 break;
667 data->numprefix = 0;
668 return;
670 input_on:
671 keys = options_get_number(&wp->window->options, "mode-keys");
672 if (keys == MODEKEY_EMACS)
673 mode_key_init(&data->mdata, &mode_key_tree_emacs_edit);
674 else
675 mode_key_init(&data->mdata, &mode_key_tree_vi_edit);
677 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
678 return;
680 input_off:
681 keys = options_get_number(&wp->window->options, "mode-keys");
682 if (keys == MODEKEY_EMACS)
683 mode_key_init(&data->mdata, &mode_key_tree_emacs_copy);
684 else
685 mode_key_init(&data->mdata, &mode_key_tree_vi_copy);
687 data->inputtype = WINDOW_COPY_OFF;
688 data->inputprompt = NULL;
690 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
694 window_copy_key_input(struct window_pane *wp, int key)
696 struct window_copy_mode_data *data = wp->modedata;
697 struct screen *s = &data->screen;
698 size_t inputlen;
699 u_int np;
701 switch (mode_key_lookup(&data->mdata, key)) {
702 case MODEKEYEDIT_CANCEL:
703 data->numprefix = 0;
704 return (-1);
705 case MODEKEYEDIT_BACKSPACE:
706 inputlen = strlen(data->inputstr);
707 if (inputlen > 0)
708 data->inputstr[inputlen - 1] = '\0';
709 break;
710 case MODEKEYEDIT_DELETELINE:
711 *data->inputstr = '\0';
712 break;
713 case MODEKEYEDIT_ENTER:
714 np = data->numprefix;
715 if (np == 0)
716 np = 1;
718 switch (data->inputtype) {
719 case WINDOW_COPY_OFF:
720 case WINDOW_COPY_JUMPFORWARD:
721 case WINDOW_COPY_JUMPBACK:
722 case WINDOW_COPY_NUMERICPREFIX:
723 break;
724 case WINDOW_COPY_SEARCHUP:
725 for (; np != 0; np--)
726 window_copy_search_up(wp, data->inputstr);
727 data->searchtype = data->inputtype;
728 data->searchstr = xstrdup(data->inputstr);
729 break;
730 case WINDOW_COPY_SEARCHDOWN:
731 for (; np != 0; np--)
732 window_copy_search_down(wp, data->inputstr);
733 data->searchtype = data->inputtype;
734 data->searchstr = xstrdup(data->inputstr);
735 break;
736 case WINDOW_COPY_GOTOLINE:
737 window_copy_goto_line(wp, data->inputstr);
738 *data->inputstr = '\0';
739 break;
741 data->numprefix = 0;
742 return (1);
743 case MODEKEY_OTHER:
744 if (key < 32 || key > 126)
745 break;
746 inputlen = strlen(data->inputstr) + 2;
748 data->inputstr = xrealloc(data->inputstr, 1, inputlen);
749 data->inputstr[inputlen - 2] = key;
750 data->inputstr[inputlen - 1] = '\0';
751 break;
752 default:
753 break;
756 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
757 return (0);
761 window_copy_key_numeric_prefix(struct window_pane *wp, int key)
763 struct window_copy_mode_data *data = wp->modedata;
764 struct screen *s = &data->screen;
766 key &= KEYC_MASK_KEY;
767 if (key < '0' || key > '9')
768 return 1;
770 if (data->numprefix >= 100) /* no more than three digits */
771 return 0;
772 data->numprefix = data->numprefix * 10 + key - '0';
774 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
775 return 0;
778 /* ARGSUSED */
779 void
780 window_copy_mouse(
781 struct window_pane *wp, struct session *sess, struct mouse_event *m)
783 struct window_copy_mode_data *data = wp->modedata;
784 struct screen *s = &data->screen;
785 u_int i, old_cy;
787 if (m->x >= screen_size_x(s))
788 return;
789 if (m->y >= screen_size_y(s))
790 return;
792 /* If mouse wheel (buttons 4 and 5), scroll. */
793 if ((m->b & MOUSE_45)) {
794 if ((m->b & MOUSE_BUTTON) == MOUSE_1) {
795 for (i = 0; i < 5; i++)
796 window_copy_cursor_up(wp, 0);
797 } else if ((m->b & MOUSE_BUTTON) == MOUSE_2) {
798 old_cy = data->cy;
799 for (i = 0; i < 5; i++)
800 window_copy_cursor_down(wp, 0);
801 if (old_cy == data->cy)
802 goto reset_mode;
804 return;
808 * If already reading motion, move the cursor while buttons are still
809 * pressed, or stop the selection on their release.
811 if (s->mode & MODE_MOUSE_BUTTON) {
812 if ((m->b & MOUSE_BUTTON) != MOUSE_UP) {
813 window_copy_update_cursor(wp, m->x, m->y);
814 if (window_copy_update_selection(wp))
815 window_copy_redraw_screen(wp);
816 return;
818 goto reset_mode;
821 /* Otherwise if other buttons pressed, start selection and motion. */
822 if ((m->b & MOUSE_BUTTON) != MOUSE_UP) {
823 s->mode &= ~MODE_MOUSE_STANDARD;
824 s->mode |= MODE_MOUSE_BUTTON;
826 window_copy_update_cursor(wp, m->x, m->y);
827 window_copy_start_selection(wp);
828 window_copy_redraw_screen(wp);
831 return;
833 reset_mode:
834 s->mode &= ~MODE_MOUSE_BUTTON;
835 s->mode |= MODE_MOUSE_STANDARD;
836 if (sess != NULL) {
837 window_copy_copy_selection(wp);
838 window_pane_reset_mode(wp);
842 void
843 window_copy_scroll_to(struct window_pane *wp, u_int px, u_int py)
845 struct window_copy_mode_data *data = wp->modedata;
846 struct grid *gd = data->backing->grid;
847 u_int offset, gap;
849 data->cx = px;
851 gap = gd->sy / 4;
852 if (py < gd->sy) {
853 offset = 0;
854 data->cy = py;
855 } else if (py > gd->hsize + gd->sy - gap) {
856 offset = gd->hsize;
857 data->cy = py - gd->hsize;
858 } else {
859 offset = py + gap - gd->sy;
860 data->cy = py - offset;
862 data->oy = gd->hsize - offset;
864 window_copy_update_selection(wp);
865 window_copy_redraw_screen(wp);
869 window_copy_search_compare(
870 struct grid *gd, u_int px, u_int py, struct grid *sgd, u_int spx)
872 const struct grid_cell *gc, *sgc;
873 const struct grid_utf8 *gu, *sgu;
875 gc = grid_peek_cell(gd, px, py);
876 sgc = grid_peek_cell(sgd, spx, 0);
878 if ((gc->flags & GRID_FLAG_UTF8) != (sgc->flags & GRID_FLAG_UTF8))
879 return (0);
881 if (gc->flags & GRID_FLAG_UTF8) {
882 gu = grid_peek_utf8(gd, px, py);
883 sgu = grid_peek_utf8(sgd, spx, 0);
884 if (grid_utf8_compare(gu, sgu))
885 return (1);
886 } else {
887 if (gc->data == sgc->data)
888 return (1);
890 return (0);
894 window_copy_search_lr(struct grid *gd,
895 struct grid *sgd, u_int *ppx, u_int py, u_int first, u_int last)
897 u_int ax, bx, px;
899 for (ax = first; ax < last; ax++) {
900 if (ax + sgd->sx >= gd->sx)
901 break;
902 for (bx = 0; bx < sgd->sx; bx++) {
903 px = ax + bx;
904 if (!window_copy_search_compare(gd, px, py, sgd, bx))
905 break;
907 if (bx == sgd->sx) {
908 *ppx = ax;
909 return (1);
912 return (0);
916 window_copy_search_rl(struct grid *gd,
917 struct grid *sgd, u_int *ppx, u_int py, u_int first, u_int last)
919 u_int ax, bx, px;
921 for (ax = last + 1; ax > first; ax--) {
922 if (gd->sx - (ax - 1) < sgd->sx)
923 continue;
924 for (bx = 0; bx < sgd->sx; bx++) {
925 px = ax - 1 + bx;
926 if (!window_copy_search_compare(gd, px, py, sgd, bx))
927 break;
929 if (bx == sgd->sx) {
930 *ppx = ax - 1;
931 return (1);
934 return (0);
937 void
938 window_copy_search_up(struct window_pane *wp, const char *searchstr)
940 struct window_copy_mode_data *data = wp->modedata;
941 struct screen *s = data->backing, ss;
942 struct screen_write_ctx ctx;
943 struct grid *gd = s->grid, *sgd;
944 struct grid_cell gc;
945 size_t searchlen;
946 u_int i, last, fx, fy, px;
947 int utf8flag, n, wrapped;
949 if (*searchstr == '\0')
950 return;
951 utf8flag = options_get_number(&wp->window->options, "utf8");
952 searchlen = screen_write_strlen(utf8flag, "%s", searchstr);
954 screen_init(&ss, searchlen, 1, 0);
955 screen_write_start(&ctx, NULL, &ss);
956 memcpy(&gc, &grid_default_cell, sizeof gc);
957 screen_write_nputs(&ctx, -1, &gc, utf8flag, "%s", searchstr);
958 screen_write_stop(&ctx);
960 fx = data->cx;
961 fy = gd->hsize - data->oy + data->cy;
963 if (fx == 0) {
964 if (fy == 0)
965 return;
966 fx = gd->sx - 1;
967 fy--;
968 } else
969 fx--;
970 n = wrapped = 0;
972 retry:
973 sgd = ss.grid;
974 for (i = fy + 1; i > 0; i--) {
975 last = screen_size_x(s);
976 if (i == fy + 1)
977 last = fx;
978 n = window_copy_search_rl(gd, sgd, &px, i - 1, 0, last);
979 if (n) {
980 window_copy_scroll_to(wp, px, i - 1);
981 break;
984 if (!n && !wrapped) {
985 fx = gd->sx - 1;
986 fy = gd->hsize + gd->sy - 1;
987 wrapped = 1;
988 goto retry;
991 screen_free(&ss);
994 void
995 window_copy_search_down(struct window_pane *wp, const char *searchstr)
997 struct window_copy_mode_data *data = wp->modedata;
998 struct screen *s = data->backing, ss;
999 struct screen_write_ctx ctx;
1000 struct grid *gd = s->grid, *sgd;
1001 struct grid_cell gc;
1002 size_t searchlen;
1003 u_int i, first, fx, fy, px;
1004 int utf8flag, n, wrapped;
1006 if (*searchstr == '\0')
1007 return;
1008 utf8flag = options_get_number(&wp->window->options, "utf8");
1009 searchlen = screen_write_strlen(utf8flag, "%s", searchstr);
1011 screen_init(&ss, searchlen, 1, 0);
1012 screen_write_start(&ctx, NULL, &ss);
1013 memcpy(&gc, &grid_default_cell, sizeof gc);
1014 screen_write_nputs(&ctx, -1, &gc, utf8flag, "%s", searchstr);
1015 screen_write_stop(&ctx);
1017 fx = data->cx;
1018 fy = gd->hsize - data->oy + data->cy;
1020 if (fx == gd->sx - 1) {
1021 if (fy == gd->hsize + gd->sy)
1022 return;
1023 fx = 0;
1024 fy++;
1025 } else
1026 fx++;
1027 n = wrapped = 0;
1029 retry:
1030 sgd = ss.grid;
1031 for (i = fy + 1; i < gd->hsize + gd->sy; i++) {
1032 first = 0;
1033 if (i == fy + 1)
1034 first = fx;
1035 n = window_copy_search_lr(gd, sgd, &px, i - 1, first, gd->sx);
1036 if (n) {
1037 window_copy_scroll_to(wp, px, i - 1);
1038 break;
1041 if (!n && !wrapped) {
1042 fx = 0;
1043 fy = 0;
1044 wrapped = 1;
1045 goto retry;
1048 screen_free(&ss);
1051 void
1052 window_copy_goto_line(struct window_pane *wp, const char *linestr)
1054 struct window_copy_mode_data *data = wp->modedata;
1055 const char *errstr;
1056 u_int lineno;
1058 lineno = strtonum(linestr, 0, screen_hsize(data->backing), &errstr);
1059 if (errstr != NULL)
1060 return;
1062 data->oy = lineno;
1063 window_copy_update_selection(wp);
1064 window_copy_redraw_screen(wp);
1067 void
1068 window_copy_write_line(
1069 struct window_pane *wp, struct screen_write_ctx *ctx, u_int py)
1071 struct window_copy_mode_data *data = wp->modedata;
1072 struct screen *s = &data->screen;
1073 struct options *oo = &wp->window->options;
1074 struct grid_cell gc;
1075 char hdr[32];
1076 size_t last, xoff = 0, size = 0;
1078 memcpy(&gc, &grid_default_cell, sizeof gc);
1079 colour_set_fg(&gc, options_get_number(oo, "mode-fg"));
1080 colour_set_bg(&gc, options_get_number(oo, "mode-bg"));
1081 gc.attr |= options_get_number(oo, "mode-attr");
1083 last = screen_size_y(s) - 1;
1084 if (py == 0) {
1085 size = xsnprintf(hdr, sizeof hdr,
1086 "[%u/%u]", data->oy, screen_hsize(data->backing));
1087 if (size > screen_size_x(s))
1088 size = screen_size_x(s);
1089 screen_write_cursormove(ctx, screen_size_x(s) - size, 0);
1090 screen_write_puts(ctx, &gc, "%s", hdr);
1091 } else if (py == last && data->inputtype != WINDOW_COPY_OFF) {
1092 if (data->inputtype == WINDOW_COPY_NUMERICPREFIX) {
1093 xoff = size = xsnprintf(hdr, sizeof hdr,
1094 "Repeat: %u", data->numprefix);
1095 } else {
1096 xoff = size = xsnprintf(hdr, sizeof hdr,
1097 "%s: %s", data->inputprompt, data->inputstr);
1099 screen_write_cursormove(ctx, 0, last);
1100 screen_write_puts(ctx, &gc, "%s", hdr);
1101 } else
1102 size = 0;
1104 screen_write_cursormove(ctx, xoff, py);
1105 screen_write_copy(ctx, data->backing, xoff,
1106 (screen_hsize(data->backing) - data->oy) + py,
1107 screen_size_x(s) - size, 1);
1109 if (py == data->cy && data->cx == screen_size_x(s)) {
1110 memcpy(&gc, &grid_default_cell, sizeof gc);
1111 screen_write_cursormove(ctx, screen_size_x(s) - 1, py);
1112 screen_write_putc(ctx, &gc, '$');
1116 void
1117 window_copy_write_lines(
1118 struct window_pane *wp, struct screen_write_ctx *ctx, u_int py, u_int ny)
1120 u_int yy;
1122 for (yy = py; yy < py + ny; yy++)
1123 window_copy_write_line(wp, ctx, py);
1126 void
1127 window_copy_redraw_lines(struct window_pane *wp, u_int py, u_int ny)
1129 struct window_copy_mode_data *data = wp->modedata;
1130 struct screen_write_ctx ctx;
1131 u_int i;
1133 screen_write_start(&ctx, wp, NULL);
1134 for (i = py; i < py + ny; i++)
1135 window_copy_write_line(wp, &ctx, i);
1136 screen_write_cursormove(&ctx, data->cx, data->cy);
1137 screen_write_stop(&ctx);
1140 void
1141 window_copy_redraw_screen(struct window_pane *wp)
1143 struct window_copy_mode_data *data = wp->modedata;
1145 window_copy_redraw_lines(wp, 0, screen_size_y(&data->screen));
1148 void
1149 window_copy_update_cursor(struct window_pane *wp, u_int cx, u_int cy)
1151 struct window_copy_mode_data *data = wp->modedata;
1152 struct screen *s = &data->screen;
1153 struct screen_write_ctx ctx;
1154 u_int old_cx, old_cy;
1156 old_cx = data->cx; old_cy = data->cy;
1157 data->cx = cx; data->cy = cy;
1158 if (old_cx == screen_size_x(s))
1159 window_copy_redraw_lines(wp, old_cy, 1);
1160 if (data->cx == screen_size_x(s))
1161 window_copy_redraw_lines(wp, data->cy, 1);
1162 else {
1163 screen_write_start(&ctx, wp, NULL);
1164 screen_write_cursormove(&ctx, data->cx, data->cy);
1165 screen_write_stop(&ctx);
1169 void
1170 window_copy_start_selection(struct window_pane *wp)
1172 struct window_copy_mode_data *data = wp->modedata;
1173 struct screen *s = &data->screen;
1175 data->selx = data->cx;
1176 data->sely = screen_hsize(data->backing) + data->cy - data->oy;
1178 s->sel.flag = 1;
1179 window_copy_update_selection(wp);
1183 window_copy_update_selection(struct window_pane *wp)
1185 struct window_copy_mode_data *data = wp->modedata;
1186 struct screen *s = &data->screen;
1187 struct options *oo = &wp->window->options;
1188 struct grid_cell gc;
1189 u_int sx, sy, ty, cy;
1191 if (!s->sel.flag)
1192 return (0);
1194 /* Set colours. */
1195 memcpy(&gc, &grid_default_cell, sizeof gc);
1196 colour_set_fg(&gc, options_get_number(oo, "mode-fg"));
1197 colour_set_bg(&gc, options_get_number(oo, "mode-bg"));
1198 gc.attr |= options_get_number(oo, "mode-attr");
1200 /* Find top of screen. */
1201 ty = screen_hsize(data->backing) - data->oy;
1203 /* Adjust the selection. */
1204 sx = data->selx;
1205 sy = data->sely;
1206 if (sy < ty) { /* above screen */
1207 if (!data->rectflag)
1208 sx = 0;
1209 sy = 0;
1210 } else if (sy > ty + screen_size_y(s) - 1) { /* below screen */
1211 if (!data->rectflag)
1212 sx = screen_size_x(s) - 1;
1213 sy = screen_size_y(s) - 1;
1214 } else
1215 sy -= ty;
1216 sy = screen_hsize(s) + sy;
1218 screen_set_selection(s,
1219 sx, sy, data->cx, screen_hsize(s) + data->cy, data->rectflag, &gc);
1221 if (data->rectflag) {
1223 * Can't rely on the caller to redraw the right lines for
1224 * rectangle selection - find the highest line and the number
1225 * of lines, and redraw just past that in both directions
1227 cy = data->cy;
1228 if (sy < cy)
1229 window_copy_redraw_lines(wp, sy, cy - sy + 1);
1230 else
1231 window_copy_redraw_lines(wp, cy, sy - cy + 1);
1234 return (1);
1237 void
1238 window_copy_copy_selection(struct window_pane *wp)
1240 struct window_copy_mode_data *data = wp->modedata;
1241 struct screen *s = &data->screen;
1242 char *buf;
1243 size_t off;
1244 u_int i, xx, yy, sx, sy, ex, ey, limit;
1245 u_int firstsx, lastex, restex, restsx;
1246 int keys;
1248 if (!s->sel.flag)
1249 return;
1251 buf = xmalloc(1);
1252 off = 0;
1254 *buf = '\0';
1257 * The selection extends from selx,sely to (adjusted) cx,cy on
1258 * the base screen.
1261 /* Find start and end. */
1262 xx = data->cx;
1263 yy = screen_hsize(data->backing) + data->cy - data->oy;
1264 if (yy < data->sely || (yy == data->sely && xx < data->selx)) {
1265 sx = xx; sy = yy;
1266 ex = data->selx; ey = data->sely;
1267 } else {
1268 sx = data->selx; sy = data->sely;
1269 ex = xx; ey = yy;
1272 /* Trim ex to end of line. */
1273 xx = window_copy_find_length(wp, ey);
1274 if (ex > xx)
1275 ex = xx;
1278 * Deal with rectangle-copy if necessary; four situations: start of
1279 * first line (firstsx), end of last line (lastex), start (restsx) and
1280 * end (restex) of all other lines.
1282 xx = screen_size_x(s);
1285 * Behave according to mode-keys. If it is emacs, copy like emacs,
1286 * keeping the top-left-most character, and dropping the
1287 * bottom-right-most, regardless of copy direction. If it is vi, also
1288 * keep bottom-right-most character.
1290 keys = options_get_number(&wp->window->options, "mode-keys");
1291 if (data->rectflag) {
1293 * Need to ignore the column with the cursor in it, which for
1294 * rectangular copy means knowing which side the cursor is on.
1296 if (data->selx < data->cx) {
1297 /* Selection start is on the left. */
1298 if (keys == MODEKEY_EMACS) {
1299 lastex = data->cx;
1300 restex = data->cx;
1302 else {
1303 lastex = data->cx + 1;
1304 restex = data->cx + 1;
1306 firstsx = data->selx;
1307 restsx = data->selx;
1308 } else {
1309 /* Cursor is on the left. */
1310 lastex = data->selx + 1;
1311 restex = data->selx + 1;
1312 firstsx = data->cx;
1313 restsx = data->cx;
1315 } else {
1316 if (keys == MODEKEY_EMACS)
1317 lastex = ex;
1318 else
1319 lastex = ex + 1;
1320 restex = xx;
1321 firstsx = sx;
1322 restsx = 0;
1325 /* Copy the lines. */
1326 if (sy == ey)
1327 window_copy_copy_line(wp, &buf, &off, sy, firstsx, lastex);
1328 else {
1329 window_copy_copy_line(wp, &buf, &off, sy, firstsx, restex);
1330 if (ey - sy > 1) {
1331 for (i = sy + 1; i < ey; i++) {
1332 window_copy_copy_line(
1333 wp, &buf, &off, i, restsx, restex);
1336 window_copy_copy_line(wp, &buf, &off, ey, restsx, lastex);
1339 /* Don't bother if no data. */
1340 if (off == 0) {
1341 xfree(buf);
1342 return;
1344 off--; /* remove final \n */
1346 if (options_get_number(&global_options, "set-clipboard"))
1347 screen_write_setselection(&wp->ictx.ctx, buf, off);
1349 /* Add the buffer to the stack. */
1350 limit = options_get_number(&global_options, "buffer-limit");
1351 paste_add(&global_buffers, buf, off, limit);
1354 void
1355 window_copy_copy_line(struct window_pane *wp,
1356 char **buf, size_t *off, u_int sy, u_int sx, u_int ex)
1358 struct window_copy_mode_data *data = wp->modedata;
1359 struct grid *gd = data->backing->grid;
1360 const struct grid_cell *gc;
1361 const struct grid_utf8 *gu;
1362 struct grid_line *gl;
1363 u_int i, xx, wrapped = 0;
1364 size_t size;
1366 if (sx > ex)
1367 return;
1370 * Work out if the line was wrapped at the screen edge and all of it is
1371 * on screen.
1373 gl = &gd->linedata[sy];
1374 if (gl->flags & GRID_LINE_WRAPPED && gl->cellsize <= gd->sx)
1375 wrapped = 1;
1377 /* If the line was wrapped, don't strip spaces (use the full length). */
1378 if (wrapped)
1379 xx = gl->cellsize;
1380 else
1381 xx = window_copy_find_length(wp, sy);
1382 if (ex > xx)
1383 ex = xx;
1384 if (sx > xx)
1385 sx = xx;
1387 if (sx < ex) {
1388 for (i = sx; i < ex; i++) {
1389 gc = grid_peek_cell(gd, i, sy);
1390 if (gc->flags & GRID_FLAG_PADDING)
1391 continue;
1392 if (!(gc->flags & GRID_FLAG_UTF8)) {
1393 *buf = xrealloc(*buf, 1, (*off) + 1);
1394 (*buf)[(*off)++] = gc->data;
1395 } else {
1396 gu = grid_peek_utf8(gd, i, sy);
1397 size = grid_utf8_size(gu);
1398 *buf = xrealloc(*buf, 1, (*off) + size);
1399 *off += grid_utf8_copy(gu, *buf + *off, size);
1404 /* Only add a newline if the line wasn't wrapped. */
1405 if (!wrapped || ex != xx) {
1406 *buf = xrealloc(*buf, 1, (*off) + 1);
1407 (*buf)[(*off)++] = '\n';
1411 void
1412 window_copy_clear_selection(struct window_pane *wp)
1414 struct window_copy_mode_data *data = wp->modedata;
1415 u_int px, py;
1417 screen_clear_selection(&data->screen);
1419 py = screen_hsize(data->backing) + data->cy - data->oy;
1420 px = window_copy_find_length(wp, py);
1421 if (data->cx > px)
1422 window_copy_update_cursor(wp, px, data->cy);
1426 window_copy_in_set(struct window_pane *wp, u_int px, u_int py, const char *set)
1428 struct window_copy_mode_data *data = wp->modedata;
1429 const struct grid_cell *gc;
1431 gc = grid_peek_cell(data->backing->grid, px, py);
1432 if (gc->flags & (GRID_FLAG_PADDING|GRID_FLAG_UTF8))
1433 return (0);
1434 if (gc->data == 0x00 || gc->data == 0x7f)
1435 return (0);
1436 return (strchr(set, gc->data) != NULL);
1439 u_int
1440 window_copy_find_length(struct window_pane *wp, u_int py)
1442 struct window_copy_mode_data *data = wp->modedata;
1443 struct screen *s = data->backing;
1444 const struct grid_cell *gc;
1445 u_int px;
1448 * If the pane has been resized, its grid can contain old overlong
1449 * lines. grid_peek_cell does not allow accessing cells beyond the
1450 * width of the grid, and screen_write_copy treats them as spaces, so
1451 * ignore them here too.
1453 px = s->grid->linedata[py].cellsize;
1454 if (px > screen_size_x(s))
1455 px = screen_size_x(s);
1456 while (px > 0) {
1457 gc = grid_peek_cell(s->grid, px - 1, py);
1458 if (gc->flags & GRID_FLAG_UTF8)
1459 break;
1460 if (gc->data != ' ')
1461 break;
1462 px--;
1464 return (px);
1467 void
1468 window_copy_cursor_start_of_line(struct window_pane *wp)
1470 struct window_copy_mode_data *data = wp->modedata;
1471 struct screen *back_s = data->backing;
1472 struct grid *gd = back_s->grid;
1473 u_int py;
1475 if (data->cx == 0) {
1476 py = screen_hsize(back_s) + data->cy - data->oy;
1477 while (py > 0 && gd->linedata[py-1].flags & GRID_LINE_WRAPPED) {
1478 window_copy_cursor_up(wp, 0);
1479 py = screen_hsize(back_s) + data->cy - data->oy;
1482 window_copy_update_cursor(wp, 0, data->cy);
1483 if (window_copy_update_selection(wp))
1484 window_copy_redraw_lines(wp, data->cy, 1);
1487 void
1488 window_copy_cursor_back_to_indentation(struct window_pane *wp)
1490 struct window_copy_mode_data *data = wp->modedata;
1491 u_int px, py, xx;
1492 const struct grid_cell *gc;
1494 px = 0;
1495 py = screen_hsize(data->backing) + data->cy - data->oy;
1496 xx = window_copy_find_length(wp, py);
1498 while (px < xx) {
1499 gc = grid_peek_cell(data->backing->grid, px, py);
1500 if (gc->flags & GRID_FLAG_UTF8)
1501 break;
1502 if (gc->data != ' ')
1503 break;
1504 px++;
1507 window_copy_update_cursor(wp, px, data->cy);
1508 if (window_copy_update_selection(wp))
1509 window_copy_redraw_lines(wp, data->cy, 1);
1512 void
1513 window_copy_cursor_end_of_line(struct window_pane *wp)
1515 struct window_copy_mode_data *data = wp->modedata;
1516 struct screen *back_s = data->backing;
1517 struct grid *gd = back_s->grid;
1518 u_int px, py;
1520 py = screen_hsize(back_s) + data->cy - data->oy;
1521 px = window_copy_find_length(wp, py);
1523 if (data->cx == px) {
1524 if (data->screen.sel.flag && data->rectflag)
1525 px = screen_size_x(back_s);
1526 if (gd->linedata[py].flags & GRID_LINE_WRAPPED) {
1527 while (py < gd->sy + gd->hsize &&
1528 gd->linedata[py].flags & GRID_LINE_WRAPPED) {
1529 window_copy_cursor_down(wp, 0);
1530 py = screen_hsize(back_s)
1531 + data->cy - data->oy;
1533 px = window_copy_find_length(wp, py);
1536 window_copy_update_cursor(wp, px, data->cy);
1538 if (window_copy_update_selection(wp))
1539 window_copy_redraw_lines(wp, data->cy, 1);
1542 void
1543 window_copy_cursor_left(struct window_pane *wp)
1545 struct window_copy_mode_data *data = wp->modedata;
1547 if (data->cx == 0) {
1548 window_copy_cursor_up(wp, 0);
1549 window_copy_cursor_end_of_line(wp);
1550 } else {
1551 window_copy_update_cursor(wp, data->cx - 1, data->cy);
1552 if (window_copy_update_selection(wp))
1553 window_copy_redraw_lines(wp, data->cy, 1);
1557 void
1558 window_copy_cursor_right(struct window_pane *wp)
1560 struct window_copy_mode_data *data = wp->modedata;
1561 u_int px, py;
1563 if (data->screen.sel.flag && data->rectflag)
1564 px = screen_size_x(&data->screen);
1565 else {
1566 py = screen_hsize(data->backing) + data->cy - data->oy;
1567 px = window_copy_find_length(wp, py);
1570 if (data->cx >= px) {
1571 window_copy_cursor_start_of_line(wp);
1572 window_copy_cursor_down(wp, 0);
1573 } else {
1574 window_copy_update_cursor(wp, data->cx + 1, data->cy);
1575 if (window_copy_update_selection(wp))
1576 window_copy_redraw_lines(wp, data->cy, 1);
1580 void
1581 window_copy_cursor_up(struct window_pane *wp, int scroll_only)
1583 struct window_copy_mode_data *data = wp->modedata;
1584 struct screen *s = &data->screen;
1585 u_int ox, oy, px, py;
1587 oy = screen_hsize(data->backing) + data->cy - data->oy;
1588 ox = window_copy_find_length(wp, oy);
1589 if (ox != 0) {
1590 data->lastcx = data->cx;
1591 data->lastsx = ox;
1594 data->cx = data->lastcx;
1595 if (scroll_only || data->cy == 0) {
1596 window_copy_scroll_down(wp, 1);
1597 if (scroll_only) {
1598 if (data->cy == screen_size_y(s) - 1)
1599 window_copy_redraw_lines(wp, data->cy, 1);
1600 else
1601 window_copy_redraw_lines(wp, data->cy, 2);
1603 } else {
1604 window_copy_update_cursor(wp, data->cx, data->cy - 1);
1605 if (window_copy_update_selection(wp)) {
1606 if (data->cy == screen_size_y(s) - 1)
1607 window_copy_redraw_lines(wp, data->cy, 1);
1608 else
1609 window_copy_redraw_lines(wp, data->cy, 2);
1613 if (!data->screen.sel.flag || !data->rectflag) {
1614 py = screen_hsize(data->backing) + data->cy - data->oy;
1615 px = window_copy_find_length(wp, py);
1616 if ((data->cx >= data->lastsx && data->cx != px) ||
1617 data->cx > px)
1618 window_copy_cursor_end_of_line(wp);
1622 void
1623 window_copy_cursor_down(struct window_pane *wp, int scroll_only)
1625 struct window_copy_mode_data *data = wp->modedata;
1626 struct screen *s = &data->screen;
1627 u_int ox, oy, px, py;
1629 oy = screen_hsize(data->backing) + data->cy - data->oy;
1630 ox = window_copy_find_length(wp, oy);
1631 if (ox != 0) {
1632 data->lastcx = data->cx;
1633 data->lastsx = ox;
1636 data->cx = data->lastcx;
1637 if (scroll_only || data->cy == screen_size_y(s) - 1) {
1638 window_copy_scroll_up(wp, 1);
1639 if (scroll_only && data->cy > 0)
1640 window_copy_redraw_lines(wp, data->cy - 1, 2);
1641 } else {
1642 window_copy_update_cursor(wp, data->cx, data->cy + 1);
1643 if (window_copy_update_selection(wp))
1644 window_copy_redraw_lines(wp, data->cy - 1, 2);
1647 if (!data->screen.sel.flag || !data->rectflag) {
1648 py = screen_hsize(data->backing) + data->cy - data->oy;
1649 px = window_copy_find_length(wp, py);
1650 if ((data->cx >= data->lastsx && data->cx != px) ||
1651 data->cx > px)
1652 window_copy_cursor_end_of_line(wp);
1656 void
1657 window_copy_cursor_jump(struct window_pane *wp)
1659 struct window_copy_mode_data *data = wp->modedata;
1660 struct screen *back_s = data->backing;
1661 const struct grid_cell *gc;
1662 u_int px, py, xx;
1664 px = data->cx + 1;
1665 py = screen_hsize(back_s) + data->cy - data->oy;
1666 xx = window_copy_find_length(wp, py);
1668 while (px < xx) {
1669 gc = grid_peek_cell(back_s->grid, px, py);
1670 if ((gc->flags & (GRID_FLAG_PADDING|GRID_FLAG_UTF8)) == 0
1671 && gc->data == data->jumpchar) {
1673 window_copy_update_cursor(wp, px, data->cy);
1674 if (window_copy_update_selection(wp))
1675 window_copy_redraw_lines(wp, data->cy, 1);
1676 return;
1678 px++;
1682 void
1683 window_copy_cursor_jump_back(struct window_pane *wp)
1685 struct window_copy_mode_data *data = wp->modedata;
1686 struct screen *back_s = data->backing;
1687 const struct grid_cell *gc;
1688 u_int px, py;
1690 px = data->cx;
1691 py = screen_hsize(back_s) + data->cy - data->oy;
1693 if (px > 0)
1694 px--;
1696 for (;;) {
1697 gc = grid_peek_cell(back_s->grid, px, py);
1698 if ((gc->flags & (GRID_FLAG_PADDING|GRID_FLAG_UTF8)) == 0
1699 && gc->data == data->jumpchar) {
1701 window_copy_update_cursor(wp, px, data->cy);
1702 if (window_copy_update_selection(wp))
1703 window_copy_redraw_lines(wp, data->cy, 1);
1704 return;
1706 if (px == 0)
1707 break;
1708 px--;
1712 void
1713 window_copy_cursor_next_word(struct window_pane *wp, const char *separators)
1715 struct window_copy_mode_data *data = wp->modedata;
1716 struct screen *back_s = data->backing;
1717 u_int px, py, xx, yy;
1718 int expected = 0;
1720 px = data->cx;
1721 py = screen_hsize(back_s) + data->cy - data->oy;
1722 xx = window_copy_find_length(wp, py);
1723 yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
1726 * First skip past any nonword characters and then any word characters.
1728 * expected is initially set to 0 for the former and then 1 for the
1729 * latter.
1731 do {
1732 while (px > xx ||
1733 window_copy_in_set(wp, px, py, separators) == expected) {
1734 /* Move down if we're past the end of the line. */
1735 if (px > xx) {
1736 if (py == yy)
1737 return;
1738 window_copy_cursor_down(wp, 0);
1739 px = 0;
1741 py = screen_hsize(back_s) + data->cy - data->oy;
1742 xx = window_copy_find_length(wp, py);
1743 } else
1744 px++;
1746 expected = !expected;
1747 } while (expected == 1);
1749 window_copy_update_cursor(wp, px, data->cy);
1750 if (window_copy_update_selection(wp))
1751 window_copy_redraw_lines(wp, data->cy, 1);
1754 void
1755 window_copy_cursor_next_word_end(struct window_pane *wp, const char *separators)
1757 struct window_copy_mode_data *data = wp->modedata;
1758 struct screen *back_s = data->backing;
1759 u_int px, py, xx, yy;
1760 int expected = 1;
1762 px = data->cx;
1763 py = screen_hsize(back_s) + data->cy - data->oy;
1764 xx = window_copy_find_length(wp, py);
1765 yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
1768 * First skip past any word characters, then any nonword characters.
1770 * expected is initially set to 1 for the former and then 0 for the
1771 * latter.
1773 do {
1774 while (px > xx ||
1775 window_copy_in_set(wp, px, py, separators) == expected) {
1776 /* Move down if we're past the end of the line. */
1777 if (px > xx) {
1778 if (py == yy)
1779 return;
1780 window_copy_cursor_down(wp, 0);
1781 px = 0;
1783 py = screen_hsize(back_s) + data->cy - data->oy;
1784 xx = window_copy_find_length(wp, py);
1785 } else
1786 px++;
1788 expected = !expected;
1789 } while (expected == 0);
1791 window_copy_update_cursor(wp, px, data->cy);
1792 if (window_copy_update_selection(wp))
1793 window_copy_redraw_lines(wp, data->cy, 1);
1796 /* Move to the previous place where a word begins. */
1797 void
1798 window_copy_cursor_previous_word(struct window_pane *wp, const char *separators)
1800 struct window_copy_mode_data *data = wp->modedata;
1801 u_int px, py;
1803 px = data->cx;
1804 py = screen_hsize(data->backing) + data->cy - data->oy;
1806 /* Move back to the previous word character. */
1807 for (;;) {
1808 if (px > 0) {
1809 px--;
1810 if (!window_copy_in_set(wp, px, py, separators))
1811 break;
1812 } else {
1813 if (data->cy == 0 &&
1814 (screen_hsize(data->backing) == 0 ||
1815 data->oy >= screen_hsize(data->backing) - 1))
1816 goto out;
1817 window_copy_cursor_up(wp, 0);
1819 py = screen_hsize(data->backing) + data->cy - data->oy;
1820 px = window_copy_find_length(wp, py);
1824 /* Move back to the beginning of this word. */
1825 while (px > 0 && !window_copy_in_set(wp, px - 1, py, separators))
1826 px--;
1828 out:
1829 window_copy_update_cursor(wp, px, data->cy);
1830 if (window_copy_update_selection(wp))
1831 window_copy_redraw_lines(wp, data->cy, 1);
1834 void
1835 window_copy_scroll_up(struct window_pane *wp, u_int ny)
1837 struct window_copy_mode_data *data = wp->modedata;
1838 struct screen *s = &data->screen;
1839 struct screen_write_ctx ctx;
1841 if (data->oy < ny)
1842 ny = data->oy;
1843 if (ny == 0)
1844 return;
1845 data->oy -= ny;
1847 screen_write_start(&ctx, wp, NULL);
1848 screen_write_cursormove(&ctx, 0, 0);
1849 screen_write_deleteline(&ctx, ny);
1850 window_copy_write_lines(wp, &ctx, screen_size_y(s) - ny, ny);
1851 window_copy_write_line(wp, &ctx, 0);
1852 if (screen_size_y(s) > 1)
1853 window_copy_write_line(wp, &ctx, 1);
1854 if (screen_size_y(s) > 3)
1855 window_copy_write_line(wp, &ctx, screen_size_y(s) - 2);
1856 if (s->sel.flag && screen_size_y(s) > ny) {
1857 window_copy_update_selection(wp);
1858 window_copy_write_line(wp, &ctx, screen_size_y(s) - ny - 1);
1860 screen_write_cursormove(&ctx, data->cx, data->cy);
1861 window_copy_update_selection(wp);
1862 screen_write_stop(&ctx);
1865 void
1866 window_copy_scroll_down(struct window_pane *wp, u_int ny)
1868 struct window_copy_mode_data *data = wp->modedata;
1869 struct screen *s = &data->screen;
1870 struct screen_write_ctx ctx;
1872 if (ny > screen_hsize(data->backing))
1873 return;
1875 if (data->oy > screen_hsize(data->backing) - ny)
1876 ny = screen_hsize(data->backing) - data->oy;
1877 if (ny == 0)
1878 return;
1879 data->oy += ny;
1881 screen_write_start(&ctx, wp, NULL);
1882 screen_write_cursormove(&ctx, 0, 0);
1883 screen_write_insertline(&ctx, ny);
1884 window_copy_write_lines(wp, &ctx, 0, ny);
1885 if (s->sel.flag && screen_size_y(s) > ny) {
1886 window_copy_update_selection(wp);
1887 window_copy_write_line(wp, &ctx, ny);
1888 } else if (ny == 1) /* nuke position */
1889 window_copy_write_line(wp, &ctx, 1);
1890 screen_write_cursormove(&ctx, data->cx, data->cy);
1891 window_copy_update_selection(wp);
1892 screen_write_stop(&ctx);
1895 void
1896 window_copy_rectangle_toggle(struct window_pane *wp)
1898 struct window_copy_mode_data *data = wp->modedata;
1899 u_int px, py;
1901 data->rectflag = !data->rectflag;
1903 py = screen_hsize(data->backing) + data->cy - data->oy;
1904 px = window_copy_find_length(wp, py);
1905 if (data->cx > px)
1906 window_copy_update_cursor(wp, px, data->cy);
1908 window_copy_update_selection(wp);
1909 window_copy_redraw_screen(wp);