Size on split-window is -l not -s. Doh.
[tmux-openbsd.git] / window-copy.c
blob1cdb88d8ad0a5b81c744780ab10b9f78c878c1c8
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 wp->flags |= PANE_FREEZE;
174 if (wp->fd != -1)
175 bufferevent_disable(wp->event, EV_READ|EV_WRITE);
177 data->jumptype = WINDOW_COPY_OFF;
178 data->jumpchar = '\0';
180 s = &data->screen;
181 screen_init(s, screen_size_x(&wp->base), screen_size_y(&wp->base), 0);
182 if (options_get_number(&wp->window->options, "mode-mouse"))
183 s->mode |= MODE_MOUSE_STANDARD;
185 keys = options_get_number(&wp->window->options, "mode-keys");
186 if (keys == MODEKEY_EMACS)
187 mode_key_init(&data->mdata, &mode_key_tree_emacs_copy);
188 else
189 mode_key_init(&data->mdata, &mode_key_tree_vi_copy);
191 data->backing = NULL;
193 return (s);
196 void
197 window_copy_init_from_pane(struct window_pane *wp)
199 struct window_copy_mode_data *data = wp->modedata;
200 struct screen *s = &data->screen;
201 struct screen_write_ctx ctx;
202 u_int i;
204 if (wp->mode != &window_copy_mode)
205 fatalx("not in copy mode");
207 data->backing = &wp->base;
208 data->cx = data->backing->cx;
209 data->cy = data->backing->cy;
211 s->cx = data->cx;
212 s->cy = data->cy;
214 screen_write_start(&ctx, NULL, s);
215 for (i = 0; i < screen_size_y(s); i++)
216 window_copy_write_line(wp, &ctx, i);
217 screen_write_cursormove(&ctx, data->cx, data->cy);
218 screen_write_stop(&ctx);
221 void
222 window_copy_init_for_output(struct window_pane *wp)
224 struct window_copy_mode_data *data = wp->modedata;
226 data->backing = xmalloc(sizeof *data->backing);
227 screen_init(data->backing, screen_size_x(&wp->base),
228 screen_size_y(&wp->base), UINT_MAX);
229 data->backing->mode &= ~MODE_WRAP;
232 void
233 window_copy_free(struct window_pane *wp)
235 struct window_copy_mode_data *data = wp->modedata;
237 wp->flags &= ~PANE_FREEZE;
238 if (wp->fd != -1)
239 bufferevent_enable(wp->event, EV_READ|EV_WRITE);
241 if (data->searchstr != NULL)
242 xfree(data->searchstr);
243 xfree(data->inputstr);
245 if (data->backing != &wp->base) {
246 screen_free(data->backing);
247 xfree(data->backing);
249 screen_free(&data->screen);
251 xfree(data);
254 void
255 window_copy_add(struct window_pane *wp, const char *fmt, ...)
257 va_list ap;
259 va_start(ap, fmt);
260 window_copy_vadd(wp, fmt, ap);
261 va_end(ap);
264 void
265 window_copy_vadd(struct window_pane *wp, const char *fmt, va_list ap)
267 struct window_copy_mode_data *data = wp->modedata;
268 struct screen *backing = data->backing;
269 struct screen_write_ctx back_ctx, ctx;
270 struct grid_cell gc;
271 int utf8flag;
272 u_int old_hsize;
274 if (backing == &wp->base)
275 return;
277 utf8flag = options_get_number(&wp->window->options, "utf8");
278 memcpy(&gc, &grid_default_cell, sizeof gc);
280 old_hsize = screen_hsize(data->backing);
281 screen_write_start(&back_ctx, NULL, backing);
282 if (data->backing_written) {
284 * On the second or later line, do a CRLF before writing
285 * (so it's on a new line).
287 screen_write_carriagereturn(&back_ctx);
288 screen_write_linefeed(&back_ctx, 0);
289 } else
290 data->backing_written = 1;
291 screen_write_vnputs(&back_ctx, 0, &gc, utf8flag, fmt, ap);
292 screen_write_stop(&back_ctx);
294 data->oy += screen_hsize(data->backing) - old_hsize;
296 screen_write_start(&ctx, wp, &data->screen);
299 * If the history has changed, draw the top line.
300 * (If there's any history at all, it has changed.)
302 if (screen_hsize(data->backing))
303 window_copy_redraw_lines(wp, 0, 1);
305 /* Write the line, if it's visible. */
306 if (backing->cy + data->oy < screen_size_y(backing))
307 window_copy_redraw_lines(wp, backing->cy, 1);
309 screen_write_stop(&ctx);
312 void
313 window_copy_pageup(struct window_pane *wp)
315 struct window_copy_mode_data *data = wp->modedata;
316 struct screen *s = &data->screen;
317 u_int n;
319 n = 1;
320 if (screen_size_y(s) > 2)
321 n = screen_size_y(s) - 2;
322 if (data->oy + n > screen_hsize(data->backing))
323 data->oy = screen_hsize(data->backing);
324 else
325 data->oy += n;
326 window_copy_update_selection(wp);
327 window_copy_redraw_screen(wp);
330 void
331 window_copy_resize(struct window_pane *wp, u_int sx, u_int sy)
333 struct window_copy_mode_data *data = wp->modedata;
334 struct screen *s = &data->screen;
335 struct screen_write_ctx ctx;
337 screen_resize(s, sx, sy);
338 if (data->backing != &wp->base)
339 screen_resize(data->backing, sx, sy);
341 if (data->cy > sy - 1)
342 data->cy = sy - 1;
343 if (data->cx > sx)
344 data->cx = sx;
345 if (data->oy > screen_hsize(data->backing))
346 data->oy = screen_hsize(data->backing);
348 window_copy_clear_selection(wp);
350 screen_write_start(&ctx, NULL, s);
351 window_copy_write_lines(wp, &ctx, 0, screen_size_y(s) - 1);
352 screen_write_stop(&ctx);
354 window_copy_redraw_screen(wp);
357 void
358 window_copy_key(struct window_pane *wp, struct session *sess, int key)
360 const char *word_separators;
361 struct window_copy_mode_data *data = wp->modedata;
362 struct screen *s = &data->screen;
363 u_int n, np;
364 int keys;
365 enum mode_key_cmd cmd;
367 np = data->numprefix;
368 if (np == 0)
369 np = 1;
371 if (data->inputtype == WINDOW_COPY_JUMPFORWARD
372 || data->inputtype == WINDOW_COPY_JUMPBACK) {
373 /* Ignore keys with modifiers. */
374 if ((key & KEYC_MASK_MOD) == 0) {
375 data->jumpchar = key;
376 if (data->inputtype == WINDOW_COPY_JUMPFORWARD) {
377 for (; np != 0; np--)
378 window_copy_cursor_jump(wp);
379 } else {
380 for (; np != 0; np--)
381 window_copy_cursor_jump_back(wp);
384 data->jumptype = data->inputtype;
385 data->inputtype = WINDOW_COPY_OFF;
386 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
387 return;
388 } if (data->inputtype == WINDOW_COPY_NUMERICPREFIX) {
389 if (window_copy_key_numeric_prefix(wp, key) == 0)
390 return;
391 data->inputtype = WINDOW_COPY_OFF;
392 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
393 } else if (data->inputtype != WINDOW_COPY_OFF) {
394 if (window_copy_key_input(wp, key) != 0)
395 goto input_off;
396 return;
399 cmd = mode_key_lookup(&data->mdata, key);
400 switch (cmd) {
401 case MODEKEYCOPY_CANCEL:
402 window_pane_reset_mode(wp);
403 return;
404 case MODEKEYCOPY_LEFT:
405 for (; np != 0; np--)
406 window_copy_cursor_left(wp);
407 break;
408 case MODEKEYCOPY_RIGHT:
409 for (; np != 0; np--)
410 window_copy_cursor_right(wp);
411 break;
412 case MODEKEYCOPY_UP:
413 for (; np != 0; np--)
414 window_copy_cursor_up(wp, 0);
415 break;
416 case MODEKEYCOPY_DOWN:
417 for (; np != 0; np--)
418 window_copy_cursor_down(wp, 0);
419 break;
420 case MODEKEYCOPY_SCROLLUP:
421 for (; np != 0; np--)
422 window_copy_cursor_up(wp, 1);
423 break;
424 case MODEKEYCOPY_SCROLLDOWN:
425 for (; np != 0; np--)
426 window_copy_cursor_down(wp, 1);
427 break;
428 case MODEKEYCOPY_PREVIOUSPAGE:
429 for (; np != 0; np--)
430 window_copy_pageup(wp);
431 break;
432 case MODEKEYCOPY_NEXTPAGE:
433 n = 1;
434 if (screen_size_y(s) > 2)
435 n = screen_size_y(s) - 2;
436 for (; np != 0; np--) {
437 if (data->oy < n)
438 data->oy = 0;
439 else
440 data->oy -= n;
442 window_copy_update_selection(wp);
443 window_copy_redraw_screen(wp);
444 break;
445 case MODEKEYCOPY_HALFPAGEUP:
446 n = screen_size_y(s) / 2;
447 for (; np != 0; np--) {
448 if (data->oy + n > screen_hsize(data->backing))
449 data->oy = screen_hsize(data->backing);
450 else
451 data->oy += n;
453 window_copy_update_selection(wp);
454 window_copy_redraw_screen(wp);
455 break;
456 case MODEKEYCOPY_HALFPAGEDOWN:
457 n = screen_size_y(s) / 2;
458 for (; np != 0; np--) {
459 if (data->oy < n)
460 data->oy = 0;
461 else
462 data->oy -= n;
464 window_copy_update_selection(wp);
465 window_copy_redraw_screen(wp);
466 break;
467 case MODEKEYCOPY_TOPLINE:
468 data->cx = 0;
469 data->cy = 0;
470 window_copy_update_selection(wp);
471 window_copy_redraw_screen(wp);
472 break;
473 case MODEKEYCOPY_MIDDLELINE:
474 data->cx = 0;
475 data->cy = (screen_size_y(s) - 1) / 2;
476 window_copy_update_selection(wp);
477 window_copy_redraw_screen(wp);
478 break;
479 case MODEKEYCOPY_BOTTOMLINE:
480 data->cx = 0;
481 data->cy = screen_size_y(s) - 1;
482 window_copy_update_selection(wp);
483 window_copy_redraw_screen(wp);
484 break;
485 case MODEKEYCOPY_HISTORYTOP:
486 data->cx = 0;
487 data->cy = 0;
488 data->oy = screen_hsize(data->backing);
489 window_copy_update_selection(wp);
490 window_copy_redraw_screen(wp);
491 break;
492 case MODEKEYCOPY_HISTORYBOTTOM:
493 data->cx = 0;
494 data->cy = screen_size_y(s) - 1;
495 data->oy = 0;
496 window_copy_update_selection(wp);
497 window_copy_redraw_screen(wp);
498 break;
499 case MODEKEYCOPY_STARTSELECTION:
500 window_copy_start_selection(wp);
501 window_copy_redraw_screen(wp);
502 break;
503 case MODEKEYCOPY_CLEARSELECTION:
504 window_copy_clear_selection(wp);
505 window_copy_redraw_screen(wp);
506 break;
507 case MODEKEYCOPY_COPYSELECTION:
508 if (sess != NULL) {
509 window_copy_copy_selection(wp);
510 window_pane_reset_mode(wp);
511 return;
513 break;
514 case MODEKEYCOPY_STARTOFLINE:
515 window_copy_cursor_start_of_line(wp);
516 break;
517 case MODEKEYCOPY_BACKTOINDENTATION:
518 window_copy_cursor_back_to_indentation(wp);
519 break;
520 case MODEKEYCOPY_ENDOFLINE:
521 window_copy_cursor_end_of_line(wp);
522 break;
523 case MODEKEYCOPY_NEXTSPACE:
524 for (; np != 0; np--)
525 window_copy_cursor_next_word(wp, " ");
526 break;
527 case MODEKEYCOPY_NEXTSPACEEND:
528 for (; np != 0; np--)
529 window_copy_cursor_next_word_end(wp, " ");
530 break;
531 case MODEKEYCOPY_NEXTWORD:
532 word_separators =
533 options_get_string(&wp->window->options, "word-separators");
534 for (; np != 0; np--)
535 window_copy_cursor_next_word(wp, word_separators);
536 break;
537 case MODEKEYCOPY_NEXTWORDEND:
538 word_separators =
539 options_get_string(&wp->window->options, "word-separators");
540 for (; np != 0; np--)
541 window_copy_cursor_next_word_end(wp, word_separators);
542 break;
543 case MODEKEYCOPY_PREVIOUSSPACE:
544 for (; np != 0; np--)
545 window_copy_cursor_previous_word(wp, " ");
546 break;
547 case MODEKEYCOPY_PREVIOUSWORD:
548 word_separators =
549 options_get_string(&wp->window->options, "word-separators");
550 for (; np != 0; np--)
551 window_copy_cursor_previous_word(wp, word_separators);
552 break;
553 case MODEKEYCOPY_JUMP:
554 data->inputtype = WINDOW_COPY_JUMPFORWARD;
555 data->inputprompt = "Jump Forward";
556 *data->inputstr = '\0';
557 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
558 return; /* skip numprefix reset */
559 case MODEKEYCOPY_JUMPAGAIN:
560 if (data->jumptype == WINDOW_COPY_JUMPFORWARD) {
561 for (; np != 0; np--)
562 window_copy_cursor_jump(wp);
563 } else if (data->jumptype == WINDOW_COPY_JUMPBACK) {
564 for (; np != 0; np--)
565 window_copy_cursor_jump_back(wp);
567 break;
568 case MODEKEYCOPY_JUMPREVERSE:
569 if (data->jumptype == WINDOW_COPY_JUMPFORWARD) {
570 for (; np != 0; np--)
571 window_copy_cursor_jump_back(wp);
572 } else if (data->jumptype == WINDOW_COPY_JUMPBACK) {
573 for (; np != 0; np--)
574 window_copy_cursor_jump(wp);
576 break;
577 case MODEKEYCOPY_JUMPBACK:
578 data->inputtype = WINDOW_COPY_JUMPBACK;
579 data->inputprompt = "Jump Back";
580 *data->inputstr = '\0';
581 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
582 return; /* skip numprefix reset */
583 case MODEKEYCOPY_SEARCHUP:
584 data->inputtype = WINDOW_COPY_SEARCHUP;
585 data->inputprompt = "Search Up";
586 goto input_on;
587 case MODEKEYCOPY_SEARCHDOWN:
588 data->inputtype = WINDOW_COPY_SEARCHDOWN;
589 data->inputprompt = "Search Down";
590 goto input_on;
591 case MODEKEYCOPY_SEARCHAGAIN:
592 case MODEKEYCOPY_SEARCHREVERSE:
593 switch (data->searchtype) {
594 case WINDOW_COPY_OFF:
595 case WINDOW_COPY_GOTOLINE:
596 case WINDOW_COPY_JUMPFORWARD:
597 case WINDOW_COPY_JUMPBACK:
598 case WINDOW_COPY_NUMERICPREFIX:
599 break;
600 case WINDOW_COPY_SEARCHUP:
601 if (cmd == MODEKEYCOPY_SEARCHAGAIN) {
602 for (; np != 0; np--) {
603 window_copy_search_up(
604 wp, data->searchstr);
606 } else {
607 for (; np != 0; np--) {
608 window_copy_search_down(
609 wp, data->searchstr);
612 break;
613 case WINDOW_COPY_SEARCHDOWN:
614 if (cmd == MODEKEYCOPY_SEARCHAGAIN) {
615 for (; np != 0; np--) {
616 window_copy_search_down(
617 wp, data->searchstr);
619 } else {
620 for (; np != 0; np--) {
621 window_copy_search_up(
622 wp, data->searchstr);
625 break;
627 break;
628 case MODEKEYCOPY_GOTOLINE:
629 data->inputtype = WINDOW_COPY_GOTOLINE;
630 data->inputprompt = "Goto Line";
631 *data->inputstr = '\0';
632 goto input_on;
633 case MODEKEYCOPY_STARTNUMBERPREFIX:
634 key &= KEYC_MASK_KEY;
635 if (key >= '0' && key <= '9') {
636 data->inputtype = WINDOW_COPY_NUMERICPREFIX;
637 data->numprefix = 0;
638 window_copy_key_numeric_prefix(wp, key);
639 return;
641 break;
642 case MODEKEYCOPY_RECTANGLETOGGLE:
643 window_copy_rectangle_toggle(wp);
644 break;
645 default:
646 break;
649 data->numprefix = 0;
650 return;
652 input_on:
653 keys = options_get_number(&wp->window->options, "mode-keys");
654 if (keys == MODEKEY_EMACS)
655 mode_key_init(&data->mdata, &mode_key_tree_emacs_edit);
656 else
657 mode_key_init(&data->mdata, &mode_key_tree_vi_edit);
659 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
660 return;
662 input_off:
663 keys = options_get_number(&wp->window->options, "mode-keys");
664 if (keys == MODEKEY_EMACS)
665 mode_key_init(&data->mdata, &mode_key_tree_emacs_copy);
666 else
667 mode_key_init(&data->mdata, &mode_key_tree_vi_copy);
669 data->inputtype = WINDOW_COPY_OFF;
670 data->inputprompt = NULL;
672 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
676 window_copy_key_input(struct window_pane *wp, int key)
678 struct window_copy_mode_data *data = wp->modedata;
679 struct screen *s = &data->screen;
680 size_t inputlen;
681 u_int np;
683 switch (mode_key_lookup(&data->mdata, key)) {
684 case MODEKEYEDIT_CANCEL:
685 data->numprefix = 0;
686 return (-1);
687 case MODEKEYEDIT_BACKSPACE:
688 inputlen = strlen(data->inputstr);
689 if (inputlen > 0)
690 data->inputstr[inputlen - 1] = '\0';
691 break;
692 case MODEKEYEDIT_DELETELINE:
693 *data->inputstr = '\0';
694 break;
695 case MODEKEYEDIT_ENTER:
696 np = data->numprefix;
697 if (np == 0)
698 np = 1;
700 switch (data->inputtype) {
701 case WINDOW_COPY_OFF:
702 case WINDOW_COPY_JUMPFORWARD:
703 case WINDOW_COPY_JUMPBACK:
704 case WINDOW_COPY_NUMERICPREFIX:
705 break;
706 case WINDOW_COPY_SEARCHUP:
707 for (; np != 0; np--)
708 window_copy_search_up(wp, data->inputstr);
709 data->searchtype = data->inputtype;
710 data->searchstr = xstrdup(data->inputstr);
711 break;
712 case WINDOW_COPY_SEARCHDOWN:
713 for (; np != 0; np--)
714 window_copy_search_down(wp, data->inputstr);
715 data->searchtype = data->inputtype;
716 data->searchstr = xstrdup(data->inputstr);
717 break;
718 case WINDOW_COPY_GOTOLINE:
719 window_copy_goto_line(wp, data->inputstr);
720 *data->inputstr = '\0';
721 break;
723 data->numprefix = 0;
724 return (1);
725 case MODEKEY_OTHER:
726 if (key < 32 || key > 126)
727 break;
728 inputlen = strlen(data->inputstr) + 2;
730 data->inputstr = xrealloc(data->inputstr, 1, inputlen);
731 data->inputstr[inputlen - 2] = key;
732 data->inputstr[inputlen - 1] = '\0';
733 break;
734 default:
735 break;
738 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
739 return (0);
743 window_copy_key_numeric_prefix(struct window_pane *wp, int key)
745 struct window_copy_mode_data *data = wp->modedata;
746 struct screen *s = &data->screen;
748 key &= KEYC_MASK_KEY;
749 if (key < '0' || key > '9')
750 return 1;
752 if (data->numprefix >= 100) /* no more than three digits */
753 return 0;
754 data->numprefix = data->numprefix * 10 + key - '0';
756 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
757 return 0;
760 /* ARGSUSED */
761 void
762 window_copy_mouse(
763 struct window_pane *wp, unused struct session *sess, struct mouse_event *m)
765 struct window_copy_mode_data *data = wp->modedata;
766 struct screen *s = &data->screen;
767 u_int i;
769 if (m->x >= screen_size_x(s))
770 return;
771 if (m->y >= screen_size_y(s))
772 return;
774 /* If mouse wheel (buttons 4 and 5), scroll. */
775 if ((m->b & MOUSE_45)) {
776 if ((m->b & MOUSE_BUTTON) == MOUSE_1) {
777 for (i = 0; i < 5; i++)
778 window_copy_cursor_up(wp, 0);
779 } else if ((m->b & MOUSE_BUTTON) == MOUSE_2) {
780 for (i = 0; i < 5; i++)
781 window_copy_cursor_down(wp, 0);
783 return;
787 * If already reading motion, move the cursor while buttons are still
788 * pressed, or stop the selection on their release.
790 if (s->mode & MODE_MOUSE_ANY) {
791 if ((m->b & MOUSE_BUTTON) != MOUSE_UP) {
792 window_copy_update_cursor(wp, m->x, m->y);
793 if (window_copy_update_selection(wp))
794 window_copy_redraw_screen(wp);
795 } else {
796 s->mode &= ~MODE_MOUSE_ANY;
797 s->mode |= MODE_MOUSE_STANDARD;
798 if (sess != NULL) {
799 window_copy_copy_selection(wp);
800 window_pane_reset_mode(wp);
803 return;
806 /* Otherwise if other buttons pressed, start selection and motion. */
807 if ((m->b & MOUSE_BUTTON) != MOUSE_UP) {
808 s->mode &= ~MODE_MOUSE_STANDARD;
809 s->mode |= MODE_MOUSE_ANY;
811 window_copy_update_cursor(wp, m->x, m->y);
812 window_copy_start_selection(wp);
813 window_copy_redraw_screen(wp);
817 void
818 window_copy_scroll_to(struct window_pane *wp, u_int px, u_int py)
820 struct window_copy_mode_data *data = wp->modedata;
821 struct grid *gd = data->backing->grid;
822 u_int offset, gap;
824 data->cx = px;
826 gap = gd->sy / 4;
827 if (py < gd->sy) {
828 offset = 0;
829 data->cy = py;
830 } else if (py > gd->hsize + gd->sy - gap) {
831 offset = gd->hsize;
832 data->cy = py - gd->hsize;
833 } else {
834 offset = py + gap - gd->sy;
835 data->cy = py - offset;
837 data->oy = gd->hsize - offset;
839 window_copy_update_selection(wp);
840 window_copy_redraw_screen(wp);
844 window_copy_search_compare(
845 struct grid *gd, u_int px, u_int py, struct grid *sgd, u_int spx)
847 const struct grid_cell *gc, *sgc;
848 const struct grid_utf8 *gu, *sgu;
850 gc = grid_peek_cell(gd, px, py);
851 sgc = grid_peek_cell(sgd, spx, 0);
853 if ((gc->flags & GRID_FLAG_UTF8) != (sgc->flags & GRID_FLAG_UTF8))
854 return (0);
856 if (gc->flags & GRID_FLAG_UTF8) {
857 gu = grid_peek_utf8(gd, px, py);
858 sgu = grid_peek_utf8(sgd, spx, 0);
859 if (grid_utf8_compare(gu, sgu))
860 return (1);
861 } else {
862 if (gc->data == sgc->data)
863 return (1);
865 return (0);
869 window_copy_search_lr(struct grid *gd,
870 struct grid *sgd, u_int *ppx, u_int py, u_int first, u_int last)
872 u_int ax, bx, px;
874 for (ax = first; ax < last; ax++) {
875 if (ax + sgd->sx >= gd->sx)
876 break;
877 for (bx = 0; bx < sgd->sx; bx++) {
878 px = ax + bx;
879 if (!window_copy_search_compare(gd, px, py, sgd, bx))
880 break;
882 if (bx == sgd->sx) {
883 *ppx = ax;
884 return (1);
887 return (0);
891 window_copy_search_rl(struct grid *gd,
892 struct grid *sgd, u_int *ppx, u_int py, u_int first, u_int last)
894 u_int ax, bx, px;
896 for (ax = last + 1; ax > first; ax--) {
897 if (gd->sx - (ax - 1) < sgd->sx)
898 continue;
899 for (bx = 0; bx < sgd->sx; bx++) {
900 px = ax - 1 + bx;
901 if (!window_copy_search_compare(gd, px, py, sgd, bx))
902 break;
904 if (bx == sgd->sx) {
905 *ppx = ax - 1;
906 return (1);
909 return (0);
912 void
913 window_copy_search_up(struct window_pane *wp, const char *searchstr)
915 struct window_copy_mode_data *data = wp->modedata;
916 struct screen *s = data->backing, ss;
917 struct screen_write_ctx ctx;
918 struct grid *gd = s->grid, *sgd;
919 struct grid_cell gc;
920 size_t searchlen;
921 u_int i, last, fx, fy, px;
922 int utf8flag, n, wrapped;
924 if (*searchstr == '\0')
925 return;
926 utf8flag = options_get_number(&wp->window->options, "utf8");
927 searchlen = screen_write_strlen(utf8flag, "%s", searchstr);
929 screen_init(&ss, searchlen, 1, 0);
930 screen_write_start(&ctx, NULL, &ss);
931 memcpy(&gc, &grid_default_cell, sizeof gc);
932 screen_write_nputs(&ctx, -1, &gc, utf8flag, "%s", searchstr);
933 screen_write_stop(&ctx);
935 fx = data->cx;
936 fy = gd->hsize - data->oy + data->cy;
938 if (fx == 0) {
939 if (fy == 0)
940 return;
941 fx = gd->sx - 1;
942 fy--;
943 } else
944 fx--;
945 n = wrapped = 0;
947 retry:
948 sgd = ss.grid;
949 for (i = fy + 1; i > 0; i--) {
950 last = screen_size_x(s);
951 if (i == fy + 1)
952 last = fx;
953 n = window_copy_search_rl(gd, sgd, &px, i - 1, 0, last);
954 if (n) {
955 window_copy_scroll_to(wp, px, i - 1);
956 break;
959 if (!n && !wrapped) {
960 fx = gd->sx - 1;
961 fy = gd->hsize + gd->sy - 1;
962 wrapped = 1;
963 goto retry;
966 screen_free(&ss);
969 void
970 window_copy_search_down(struct window_pane *wp, const char *searchstr)
972 struct window_copy_mode_data *data = wp->modedata;
973 struct screen *s = data->backing, ss;
974 struct screen_write_ctx ctx;
975 struct grid *gd = s->grid, *sgd;
976 struct grid_cell gc;
977 size_t searchlen;
978 u_int i, first, fx, fy, px;
979 int utf8flag, n, wrapped;
981 if (*searchstr == '\0')
982 return;
983 utf8flag = options_get_number(&wp->window->options, "utf8");
984 searchlen = screen_write_strlen(utf8flag, "%s", searchstr);
986 screen_init(&ss, searchlen, 1, 0);
987 screen_write_start(&ctx, NULL, &ss);
988 memcpy(&gc, &grid_default_cell, sizeof gc);
989 screen_write_nputs(&ctx, -1, &gc, utf8flag, "%s", searchstr);
990 screen_write_stop(&ctx);
992 fx = data->cx;
993 fy = gd->hsize - data->oy + data->cy;
995 if (fx == gd->sx - 1) {
996 if (fy == gd->hsize + gd->sy)
997 return;
998 fx = 0;
999 fy++;
1000 } else
1001 fx++;
1002 n = wrapped = 0;
1004 retry:
1005 sgd = ss.grid;
1006 for (i = fy + 1; i < gd->hsize + gd->sy; i++) {
1007 first = 0;
1008 if (i == fy + 1)
1009 first = fx;
1010 n = window_copy_search_lr(gd, sgd, &px, i - 1, first, gd->sx);
1011 if (n) {
1012 window_copy_scroll_to(wp, px, i - 1);
1013 break;
1016 if (!n && !wrapped) {
1017 fx = 0;
1018 fy = 0;
1019 wrapped = 1;
1020 goto retry;
1023 screen_free(&ss);
1026 void
1027 window_copy_goto_line(struct window_pane *wp, const char *linestr)
1029 struct window_copy_mode_data *data = wp->modedata;
1030 const char *errstr;
1031 u_int lineno;
1033 lineno = strtonum(linestr, 0, screen_hsize(data->backing), &errstr);
1034 if (errstr != NULL)
1035 return;
1037 data->oy = lineno;
1038 window_copy_update_selection(wp);
1039 window_copy_redraw_screen(wp);
1042 void
1043 window_copy_write_line(
1044 struct window_pane *wp, struct screen_write_ctx *ctx, u_int py)
1046 struct window_copy_mode_data *data = wp->modedata;
1047 struct screen *s = &data->screen;
1048 struct options *oo = &wp->window->options;
1049 struct grid_cell gc;
1050 char hdr[32];
1051 size_t last, xoff = 0, size = 0;
1053 memcpy(&gc, &grid_default_cell, sizeof gc);
1054 colour_set_fg(&gc, options_get_number(oo, "mode-fg"));
1055 colour_set_bg(&gc, options_get_number(oo, "mode-bg"));
1056 gc.attr |= options_get_number(oo, "mode-attr");
1058 last = screen_size_y(s) - 1;
1059 if (py == 0) {
1060 size = xsnprintf(hdr, sizeof hdr,
1061 "[%u/%u]", data->oy, screen_hsize(data->backing));
1062 if (size > screen_size_x(s))
1063 size = screen_size_x(s);
1064 screen_write_cursormove(ctx, screen_size_x(s) - size, 0);
1065 screen_write_puts(ctx, &gc, "%s", hdr);
1066 } else if (py == last && data->inputtype != WINDOW_COPY_OFF) {
1067 if (data->inputtype == WINDOW_COPY_NUMERICPREFIX) {
1068 xoff = size = xsnprintf(hdr, sizeof hdr,
1069 "Repeat: %u", data->numprefix);
1070 } else {
1071 xoff = size = xsnprintf(hdr, sizeof hdr,
1072 "%s: %s", data->inputprompt, data->inputstr);
1074 screen_write_cursormove(ctx, 0, last);
1075 screen_write_puts(ctx, &gc, "%s", hdr);
1076 } else
1077 size = 0;
1079 screen_write_cursormove(ctx, xoff, py);
1080 screen_write_copy(ctx, data->backing, xoff,
1081 (screen_hsize(data->backing) - data->oy) + py,
1082 screen_size_x(s) - size, 1);
1084 if (py == data->cy && data->cx == screen_size_x(s)) {
1085 memcpy(&gc, &grid_default_cell, sizeof gc);
1086 screen_write_cursormove(ctx, screen_size_x(s) - 1, py);
1087 screen_write_putc(ctx, &gc, '$');
1091 void
1092 window_copy_write_lines(
1093 struct window_pane *wp, struct screen_write_ctx *ctx, u_int py, u_int ny)
1095 u_int yy;
1097 for (yy = py; yy < py + ny; yy++)
1098 window_copy_write_line(wp, ctx, py);
1101 void
1102 window_copy_redraw_lines(struct window_pane *wp, u_int py, u_int ny)
1104 struct window_copy_mode_data *data = wp->modedata;
1105 struct screen_write_ctx ctx;
1106 u_int i;
1108 screen_write_start(&ctx, wp, NULL);
1109 for (i = py; i < py + ny; i++)
1110 window_copy_write_line(wp, &ctx, i);
1111 screen_write_cursormove(&ctx, data->cx, data->cy);
1112 screen_write_stop(&ctx);
1115 void
1116 window_copy_redraw_screen(struct window_pane *wp)
1118 struct window_copy_mode_data *data = wp->modedata;
1120 window_copy_redraw_lines(wp, 0, screen_size_y(&data->screen));
1123 void
1124 window_copy_update_cursor(struct window_pane *wp, u_int cx, u_int cy)
1126 struct window_copy_mode_data *data = wp->modedata;
1127 struct screen *s = &data->screen;
1128 struct screen_write_ctx ctx;
1129 u_int old_cx, old_cy;
1131 old_cx = data->cx; old_cy = data->cy;
1132 data->cx = cx; data->cy = cy;
1133 if (old_cx == screen_size_x(s))
1134 window_copy_redraw_lines(wp, old_cy, 1);
1135 if (data->cx == screen_size_x(s))
1136 window_copy_redraw_lines(wp, data->cy, 1);
1137 else {
1138 screen_write_start(&ctx, wp, NULL);
1139 screen_write_cursormove(&ctx, data->cx, data->cy);
1140 screen_write_stop(&ctx);
1144 void
1145 window_copy_start_selection(struct window_pane *wp)
1147 struct window_copy_mode_data *data = wp->modedata;
1148 struct screen *s = &data->screen;
1150 data->selx = data->cx;
1151 data->sely = screen_hsize(data->backing) + data->cy - data->oy;
1153 s->sel.flag = 1;
1154 window_copy_update_selection(wp);
1158 window_copy_update_selection(struct window_pane *wp)
1160 struct window_copy_mode_data *data = wp->modedata;
1161 struct screen *s = &data->screen;
1162 struct options *oo = &wp->window->options;
1163 struct grid_cell gc;
1164 u_int sx, sy, ty, cy;
1166 if (!s->sel.flag)
1167 return (0);
1169 /* Set colours. */
1170 memcpy(&gc, &grid_default_cell, sizeof gc);
1171 colour_set_fg(&gc, options_get_number(oo, "mode-fg"));
1172 colour_set_bg(&gc, options_get_number(oo, "mode-bg"));
1173 gc.attr |= options_get_number(oo, "mode-attr");
1175 /* Find top of screen. */
1176 ty = screen_hsize(data->backing) - data->oy;
1178 /* Adjust the selection. */
1179 sx = data->selx;
1180 sy = data->sely;
1181 if (sy < ty) { /* above screen */
1182 if (!data->rectflag)
1183 sx = 0;
1184 sy = 0;
1185 } else if (sy > ty + screen_size_y(s) - 1) { /* below screen */
1186 if (!data->rectflag)
1187 sx = screen_size_x(s) - 1;
1188 sy = screen_size_y(s) - 1;
1189 } else
1190 sy -= ty;
1191 sy = screen_hsize(s) + sy;
1193 screen_set_selection(s,
1194 sx, sy, data->cx, screen_hsize(s) + data->cy, data->rectflag, &gc);
1196 if (data->rectflag) {
1198 * Can't rely on the caller to redraw the right lines for
1199 * rectangle selection - find the highest line and the number
1200 * of lines, and redraw just past that in both directions
1202 cy = data->cy;
1203 if (sy < cy)
1204 window_copy_redraw_lines(wp, sy, cy - sy + 1);
1205 else
1206 window_copy_redraw_lines(wp, cy, sy - cy + 1);
1209 return (1);
1212 void
1213 window_copy_copy_selection(struct window_pane *wp)
1215 struct window_copy_mode_data *data = wp->modedata;
1216 struct screen *s = &data->screen;
1217 char *buf;
1218 size_t off;
1219 u_int i, xx, yy, sx, sy, ex, ey, limit;
1220 u_int firstsx, lastex, restex, restsx;
1222 if (!s->sel.flag)
1223 return;
1225 buf = xmalloc(1);
1226 off = 0;
1228 *buf = '\0';
1231 * The selection extends from selx,sely to (adjusted) cx,cy on
1232 * the base screen.
1235 /* Find start and end. */
1236 xx = data->cx;
1237 yy = screen_hsize(data->backing) + data->cy - data->oy;
1238 if (yy < data->sely || (yy == data->sely && xx < data->selx)) {
1239 sx = xx; sy = yy;
1240 ex = data->selx; ey = data->sely;
1241 } else {
1242 sx = data->selx; sy = data->sely;
1243 ex = xx; ey = yy;
1246 /* Trim ex to end of line. */
1247 xx = window_copy_find_length(wp, ey);
1248 if (ex > xx)
1249 ex = xx;
1252 * Deal with rectangle-copy if necessary; four situations: start of
1253 * first line (firstsx), end of last line (lastex), start (restsx) and
1254 * end (restex) of all other lines.
1256 xx = screen_size_x(s);
1257 if (data->rectflag) {
1259 * Need to ignore the column with the cursor in it, which for
1260 * rectangular copy means knowing which side the cursor is on.
1262 if (data->selx < data->cx) {
1263 /* Selection start is on the left. */
1264 lastex = data->cx;
1265 restex = data->cx;
1266 firstsx = data->selx;
1267 restsx = data->selx;
1268 } else {
1269 /* Cursor is on the left. */
1270 lastex = data->selx + 1;
1271 restex = data->selx + 1;
1272 firstsx = data->cx;
1273 restsx = data->cx;
1275 } else {
1277 * Like emacs, keep the top-left-most character, and drop the
1278 * bottom-right-most, regardless of copy direction.
1280 lastex = ex;
1281 restex = xx;
1282 firstsx = sx;
1283 restsx = 0;
1286 /* Copy the lines. */
1287 if (sy == ey)
1288 window_copy_copy_line(wp, &buf, &off, sy, firstsx, lastex);
1289 else {
1290 window_copy_copy_line(wp, &buf, &off, sy, firstsx, restex);
1291 if (ey - sy > 1) {
1292 for (i = sy + 1; i < ey; i++) {
1293 window_copy_copy_line(
1294 wp, &buf, &off, i, restsx, restex);
1297 window_copy_copy_line(wp, &buf, &off, ey, restsx, lastex);
1300 /* Don't bother if no data. */
1301 if (off == 0) {
1302 xfree(buf);
1303 return;
1305 off--; /* remove final \n */
1307 /* Add the buffer to the stack. */
1308 limit = options_get_number(&global_options, "buffer-limit");
1309 paste_add(&global_buffers, buf, off, limit);
1312 void
1313 window_copy_copy_line(struct window_pane *wp,
1314 char **buf, size_t *off, u_int sy, u_int sx, u_int ex)
1316 struct window_copy_mode_data *data = wp->modedata;
1317 struct grid *gd = data->backing->grid;
1318 const struct grid_cell *gc;
1319 const struct grid_utf8 *gu;
1320 struct grid_line *gl;
1321 u_int i, xx, wrapped = 0;
1322 size_t size;
1324 if (sx > ex)
1325 return;
1328 * Work out if the line was wrapped at the screen edge and all of it is
1329 * on screen.
1331 gl = &gd->linedata[sy];
1332 if (gl->flags & GRID_LINE_WRAPPED && gl->cellsize <= gd->sx)
1333 wrapped = 1;
1335 /* If the line was wrapped, don't strip spaces (use the full length). */
1336 if (wrapped)
1337 xx = gl->cellsize;
1338 else
1339 xx = window_copy_find_length(wp, sy);
1340 if (ex > xx)
1341 ex = xx;
1342 if (sx > xx)
1343 sx = xx;
1345 if (sx < ex) {
1346 for (i = sx; i < ex; i++) {
1347 gc = grid_peek_cell(gd, i, sy);
1348 if (gc->flags & GRID_FLAG_PADDING)
1349 continue;
1350 if (!(gc->flags & GRID_FLAG_UTF8)) {
1351 *buf = xrealloc(*buf, 1, (*off) + 1);
1352 (*buf)[(*off)++] = gc->data;
1353 } else {
1354 gu = grid_peek_utf8(gd, i, sy);
1355 size = grid_utf8_size(gu);
1356 *buf = xrealloc(*buf, 1, (*off) + size);
1357 *off += grid_utf8_copy(gu, *buf + *off, size);
1362 /* Only add a newline if the line wasn't wrapped. */
1363 if (!wrapped || ex != xx) {
1364 *buf = xrealloc(*buf, 1, (*off) + 1);
1365 (*buf)[(*off)++] = '\n';
1369 void
1370 window_copy_clear_selection(struct window_pane *wp)
1372 struct window_copy_mode_data *data = wp->modedata;
1373 u_int px, py;
1375 screen_clear_selection(&data->screen);
1377 py = screen_hsize(data->backing) + data->cy - data->oy;
1378 px = window_copy_find_length(wp, py);
1379 if (data->cx > px)
1380 window_copy_update_cursor(wp, px, data->cy);
1384 window_copy_in_set(struct window_pane *wp, u_int px, u_int py, const char *set)
1386 struct window_copy_mode_data *data = wp->modedata;
1387 const struct grid_cell *gc;
1389 gc = grid_peek_cell(data->backing->grid, px, py);
1390 if (gc->flags & (GRID_FLAG_PADDING|GRID_FLAG_UTF8))
1391 return (0);
1392 if (gc->data == 0x00 || gc->data == 0x7f)
1393 return (0);
1394 return (strchr(set, gc->data) != NULL);
1397 u_int
1398 window_copy_find_length(struct window_pane *wp, u_int py)
1400 struct window_copy_mode_data *data = wp->modedata;
1401 struct screen *s = data->backing;
1402 const struct grid_cell *gc;
1403 u_int px;
1406 * If the pane has been resized, its grid can contain old overlong
1407 * lines. grid_peek_cell does not allow accessing cells beyond the
1408 * width of the grid, and screen_write_copy treats them as spaces, so
1409 * ignore them here too.
1411 px = s->grid->linedata[py].cellsize;
1412 if (px > screen_size_x(s))
1413 px = screen_size_x(s);
1414 while (px > 0) {
1415 gc = grid_peek_cell(s->grid, px - 1, py);
1416 if (gc->flags & GRID_FLAG_UTF8)
1417 break;
1418 if (gc->data != ' ')
1419 break;
1420 px--;
1422 return (px);
1425 void
1426 window_copy_cursor_start_of_line(struct window_pane *wp)
1428 struct window_copy_mode_data *data = wp->modedata;
1429 struct screen *back_s = data->backing;
1430 struct grid *gd = back_s->grid;
1431 u_int py;
1433 if (data->cx == 0) {
1434 py = screen_hsize(back_s) + data->cy - data->oy;
1435 while (py > 0 && gd->linedata[py-1].flags & GRID_LINE_WRAPPED) {
1436 window_copy_cursor_up(wp, 0);
1437 py = screen_hsize(back_s) + data->cy - data->oy;
1440 window_copy_update_cursor(wp, 0, data->cy);
1441 if (window_copy_update_selection(wp))
1442 window_copy_redraw_lines(wp, data->cy, 1);
1445 void
1446 window_copy_cursor_back_to_indentation(struct window_pane *wp)
1448 struct window_copy_mode_data *data = wp->modedata;
1449 u_int px, py, xx;
1450 const struct grid_cell *gc;
1452 px = 0;
1453 py = screen_hsize(data->backing) + data->cy - data->oy;
1454 xx = window_copy_find_length(wp, py);
1456 while (px < xx) {
1457 gc = grid_peek_cell(data->backing->grid, px, py);
1458 if (gc->flags & GRID_FLAG_UTF8)
1459 break;
1460 if (gc->data != ' ')
1461 break;
1462 px++;
1465 window_copy_update_cursor(wp, px, data->cy);
1466 if (window_copy_update_selection(wp))
1467 window_copy_redraw_lines(wp, data->cy, 1);
1470 void
1471 window_copy_cursor_end_of_line(struct window_pane *wp)
1473 struct window_copy_mode_data *data = wp->modedata;
1474 struct screen *back_s = data->backing;
1475 struct grid *gd = back_s->grid;
1476 u_int px, py;
1478 py = screen_hsize(back_s) + data->cy - data->oy;
1479 px = window_copy_find_length(wp, py);
1481 if (data->cx == px) {
1482 if (data->screen.sel.flag && data->rectflag)
1483 px = screen_size_x(back_s);
1484 if (gd->linedata[py].flags & GRID_LINE_WRAPPED) {
1485 while (py < gd->sy + gd->hsize &&
1486 gd->linedata[py].flags & GRID_LINE_WRAPPED) {
1487 window_copy_cursor_down(wp, 0);
1488 py = screen_hsize(back_s)
1489 + data->cy - data->oy;
1491 px = window_copy_find_length(wp, py);
1494 window_copy_update_cursor(wp, px, data->cy);
1496 if (window_copy_update_selection(wp))
1497 window_copy_redraw_lines(wp, data->cy, 1);
1500 void
1501 window_copy_cursor_left(struct window_pane *wp)
1503 struct window_copy_mode_data *data = wp->modedata;
1505 if (data->cx == 0) {
1506 window_copy_cursor_up(wp, 0);
1507 window_copy_cursor_end_of_line(wp);
1508 } else {
1509 window_copy_update_cursor(wp, data->cx - 1, data->cy);
1510 if (window_copy_update_selection(wp))
1511 window_copy_redraw_lines(wp, data->cy, 1);
1515 void
1516 window_copy_cursor_right(struct window_pane *wp)
1518 struct window_copy_mode_data *data = wp->modedata;
1519 u_int px, py;
1521 if (data->screen.sel.flag && data->rectflag)
1522 px = screen_size_x(&data->screen);
1523 else {
1524 py = screen_hsize(data->backing) + data->cy - data->oy;
1525 px = window_copy_find_length(wp, py);
1528 if (data->cx >= px) {
1529 window_copy_cursor_start_of_line(wp);
1530 window_copy_cursor_down(wp, 0);
1531 } else {
1532 window_copy_update_cursor(wp, data->cx + 1, data->cy);
1533 if (window_copy_update_selection(wp))
1534 window_copy_redraw_lines(wp, data->cy, 1);
1538 void
1539 window_copy_cursor_up(struct window_pane *wp, int scroll_only)
1541 struct window_copy_mode_data *data = wp->modedata;
1542 struct screen *s = &data->screen;
1543 u_int ox, oy, px, py;
1545 oy = screen_hsize(data->backing) + data->cy - data->oy;
1546 ox = window_copy_find_length(wp, oy);
1547 if (ox != 0) {
1548 data->lastcx = data->cx;
1549 data->lastsx = ox;
1552 data->cx = data->lastcx;
1553 if (scroll_only || data->cy == 0) {
1554 window_copy_scroll_down(wp, 1);
1555 if (scroll_only) {
1556 if (data->cy == screen_size_y(s) - 1)
1557 window_copy_redraw_lines(wp, data->cy, 1);
1558 else
1559 window_copy_redraw_lines(wp, data->cy, 2);
1561 } else {
1562 window_copy_update_cursor(wp, data->cx, data->cy - 1);
1563 if (window_copy_update_selection(wp)) {
1564 if (data->cy == screen_size_y(s) - 1)
1565 window_copy_redraw_lines(wp, data->cy, 1);
1566 else
1567 window_copy_redraw_lines(wp, data->cy, 2);
1571 if (!data->screen.sel.flag || !data->rectflag) {
1572 py = screen_hsize(data->backing) + data->cy - data->oy;
1573 px = window_copy_find_length(wp, py);
1574 if ((data->cx >= data->lastsx && data->cx != px) ||
1575 data->cx > px)
1576 window_copy_cursor_end_of_line(wp);
1580 void
1581 window_copy_cursor_down(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 == screen_size_y(s) - 1) {
1596 window_copy_scroll_up(wp, 1);
1597 if (scroll_only && data->cy > 0)
1598 window_copy_redraw_lines(wp, data->cy - 1, 2);
1599 } else {
1600 window_copy_update_cursor(wp, data->cx, data->cy + 1);
1601 if (window_copy_update_selection(wp))
1602 window_copy_redraw_lines(wp, data->cy - 1, 2);
1605 if (!data->screen.sel.flag || !data->rectflag) {
1606 py = screen_hsize(data->backing) + data->cy - data->oy;
1607 px = window_copy_find_length(wp, py);
1608 if ((data->cx >= data->lastsx && data->cx != px) ||
1609 data->cx > px)
1610 window_copy_cursor_end_of_line(wp);
1614 void
1615 window_copy_cursor_jump(struct window_pane *wp)
1617 struct window_copy_mode_data *data = wp->modedata;
1618 struct screen *back_s = data->backing;
1619 const struct grid_cell *gc;
1620 uint px, py, xx;
1622 px = data->cx + 1;
1623 py = screen_hsize(back_s) + data->cy - data->oy;
1624 xx = window_copy_find_length(wp, py);
1626 while (px < xx) {
1627 gc = grid_peek_cell(back_s->grid, px, py);
1628 if ((gc->flags & (GRID_FLAG_PADDING|GRID_FLAG_UTF8)) == 0
1629 && gc->data == data->jumpchar) {
1631 window_copy_update_cursor(wp, px, data->cy);
1632 if (window_copy_update_selection(wp))
1633 window_copy_redraw_lines(wp, data->cy, 1);
1634 return;
1636 px++;
1640 void
1641 window_copy_cursor_jump_back(struct window_pane *wp)
1643 struct window_copy_mode_data *data = wp->modedata;
1644 struct screen *back_s = data->backing;
1645 const struct grid_cell *gc;
1646 uint px, py;
1648 px = data->cx;
1649 py = screen_hsize(back_s) + data->cy - data->oy;
1651 if (px > 0)
1652 px--;
1654 for (;;) {
1655 gc = grid_peek_cell(back_s->grid, px, py);
1656 if ((gc->flags & (GRID_FLAG_PADDING|GRID_FLAG_UTF8)) == 0
1657 && gc->data == data->jumpchar) {
1659 window_copy_update_cursor(wp, px, data->cy);
1660 if (window_copy_update_selection(wp))
1661 window_copy_redraw_lines(wp, data->cy, 1);
1662 return;
1664 if (px == 0)
1665 break;
1666 px--;
1670 void
1671 window_copy_cursor_next_word(struct window_pane *wp, const char *separators)
1673 struct window_copy_mode_data *data = wp->modedata;
1674 struct screen *back_s = data->backing;
1675 u_int px, py, xx, yy;
1676 int expected = 0;
1678 px = data->cx;
1679 py = screen_hsize(back_s) + data->cy - data->oy;
1680 xx = window_copy_find_length(wp, py);
1681 yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
1684 * First skip past any nonword characters and then any word characters.
1686 * expected is initially set to 0 for the former and then 1 for the
1687 * latter.
1689 do {
1690 while (px > xx ||
1691 window_copy_in_set(wp, px, py, separators) == expected) {
1692 /* Move down if we're past the end of the line. */
1693 if (px > xx) {
1694 if (py == yy)
1695 return;
1696 window_copy_cursor_down(wp, 0);
1697 px = 0;
1699 py = screen_hsize(back_s) + data->cy - data->oy;
1700 xx = window_copy_find_length(wp, py);
1701 } else
1702 px++;
1704 expected = !expected;
1705 } while (expected == 1);
1707 window_copy_update_cursor(wp, px, data->cy);
1708 if (window_copy_update_selection(wp))
1709 window_copy_redraw_lines(wp, data->cy, 1);
1712 void
1713 window_copy_cursor_next_word_end(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 = 1;
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 word characters, then any nonword characters.
1728 * expected is initially set to 1 for the former and then 0 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 == 0);
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 /* Move to the previous place where a word begins. */
1755 void
1756 window_copy_cursor_previous_word(struct window_pane *wp, const char *separators)
1758 struct window_copy_mode_data *data = wp->modedata;
1759 u_int px, py;
1761 px = data->cx;
1762 py = screen_hsize(data->backing) + data->cy - data->oy;
1764 /* Move back to the previous word character. */
1765 for (;;) {
1766 if (px > 0) {
1767 px--;
1768 if (!window_copy_in_set(wp, px, py, separators))
1769 break;
1770 } else {
1771 if (data->cy == 0 &&
1772 (screen_hsize(data->backing) == 0 ||
1773 data->oy >= screen_hsize(data->backing) - 1))
1774 goto out;
1775 window_copy_cursor_up(wp, 0);
1777 py = screen_hsize(data->backing) + data->cy - data->oy;
1778 px = window_copy_find_length(wp, py);
1782 /* Move back to the beginning of this word. */
1783 while (px > 0 && !window_copy_in_set(wp, px - 1, py, separators))
1784 px--;
1786 out:
1787 window_copy_update_cursor(wp, px, data->cy);
1788 if (window_copy_update_selection(wp))
1789 window_copy_redraw_lines(wp, data->cy, 1);
1792 void
1793 window_copy_scroll_up(struct window_pane *wp, u_int ny)
1795 struct window_copy_mode_data *data = wp->modedata;
1796 struct screen *s = &data->screen;
1797 struct screen_write_ctx ctx;
1799 if (data->oy < ny)
1800 ny = data->oy;
1801 if (ny == 0)
1802 return;
1803 data->oy -= ny;
1805 screen_write_start(&ctx, wp, NULL);
1806 screen_write_cursormove(&ctx, 0, 0);
1807 screen_write_deleteline(&ctx, ny);
1808 window_copy_write_lines(wp, &ctx, screen_size_y(s) - ny, ny);
1809 window_copy_write_line(wp, &ctx, 0);
1810 if (screen_size_y(s) > 1)
1811 window_copy_write_line(wp, &ctx, 1);
1812 if (screen_size_y(s) > 3)
1813 window_copy_write_line(wp, &ctx, screen_size_y(s) - 2);
1814 if (s->sel.flag && screen_size_y(s) > ny) {
1815 window_copy_update_selection(wp);
1816 window_copy_write_line(wp, &ctx, screen_size_y(s) - ny - 1);
1818 screen_write_cursormove(&ctx, data->cx, data->cy);
1819 window_copy_update_selection(wp);
1820 screen_write_stop(&ctx);
1823 void
1824 window_copy_scroll_down(struct window_pane *wp, u_int ny)
1826 struct window_copy_mode_data *data = wp->modedata;
1827 struct screen *s = &data->screen;
1828 struct screen_write_ctx ctx;
1830 if (ny > screen_hsize(data->backing))
1831 return;
1833 if (data->oy > screen_hsize(data->backing) - ny)
1834 ny = screen_hsize(data->backing) - data->oy;
1835 if (ny == 0)
1836 return;
1837 data->oy += ny;
1839 screen_write_start(&ctx, wp, NULL);
1840 screen_write_cursormove(&ctx, 0, 0);
1841 screen_write_insertline(&ctx, ny);
1842 window_copy_write_lines(wp, &ctx, 0, ny);
1843 if (s->sel.flag && screen_size_y(s) > ny) {
1844 window_copy_update_selection(wp);
1845 window_copy_write_line(wp, &ctx, ny);
1846 } else if (ny == 1) /* nuke position */
1847 window_copy_write_line(wp, &ctx, 1);
1848 screen_write_cursormove(&ctx, data->cx, data->cy);
1849 window_copy_update_selection(wp);
1850 screen_write_stop(&ctx);
1853 void
1854 window_copy_rectangle_toggle(struct window_pane *wp)
1856 struct window_copy_mode_data *data = wp->modedata;
1857 u_int px, py;
1859 data->rectflag = !data->rectflag;
1861 py = screen_hsize(data->backing) + data->cy - data->oy;
1862 px = window_copy_find_length(wp, py);
1863 if (data->cx > px)
1864 window_copy_update_cursor(wp, px, data->cy);
1866 window_copy_update_selection(wp);
1867 window_copy_redraw_screen(wp);