Support DECSCUSR sequence to set the cursor style with two new
[tmux-openbsd.git] / window-copy.c
blob1c060dc01d07d7cf93a3f9dfa6b6e75185b222ee
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 } else 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_COPYLINE:
504 case MODEKEYCOPY_SELECTLINE:
505 window_copy_cursor_start_of_line(wp);
506 /* FALLTHROUGH */
507 case MODEKEYCOPY_COPYENDOFLINE:
508 window_copy_start_selection(wp);
509 for (; np > 1; np--)
510 window_copy_cursor_down(wp, 0);
511 window_copy_cursor_end_of_line(wp);
512 window_copy_redraw_screen(wp);
514 /* If a copy command then copy the selection and exit. */
515 if (sess != NULL &&
516 (cmd == MODEKEYCOPY_COPYLINE ||
517 cmd == MODEKEYCOPY_COPYENDOFLINE)) {
518 window_copy_copy_selection(wp);
519 window_pane_reset_mode(wp);
520 return;
522 break;
523 case MODEKEYCOPY_CLEARSELECTION:
524 window_copy_clear_selection(wp);
525 window_copy_redraw_screen(wp);
526 break;
527 case MODEKEYCOPY_COPYSELECTION:
528 if (sess != NULL) {
529 window_copy_copy_selection(wp);
530 window_pane_reset_mode(wp);
531 return;
533 break;
534 case MODEKEYCOPY_STARTOFLINE:
535 window_copy_cursor_start_of_line(wp);
536 break;
537 case MODEKEYCOPY_BACKTOINDENTATION:
538 window_copy_cursor_back_to_indentation(wp);
539 break;
540 case MODEKEYCOPY_ENDOFLINE:
541 window_copy_cursor_end_of_line(wp);
542 break;
543 case MODEKEYCOPY_NEXTSPACE:
544 for (; np != 0; np--)
545 window_copy_cursor_next_word(wp, " ");
546 break;
547 case MODEKEYCOPY_NEXTSPACEEND:
548 for (; np != 0; np--)
549 window_copy_cursor_next_word_end(wp, " ");
550 break;
551 case MODEKEYCOPY_NEXTWORD:
552 word_separators =
553 options_get_string(&wp->window->options, "word-separators");
554 for (; np != 0; np--)
555 window_copy_cursor_next_word(wp, word_separators);
556 break;
557 case MODEKEYCOPY_NEXTWORDEND:
558 word_separators =
559 options_get_string(&wp->window->options, "word-separators");
560 for (; np != 0; np--)
561 window_copy_cursor_next_word_end(wp, word_separators);
562 break;
563 case MODEKEYCOPY_PREVIOUSSPACE:
564 for (; np != 0; np--)
565 window_copy_cursor_previous_word(wp, " ");
566 break;
567 case MODEKEYCOPY_PREVIOUSWORD:
568 word_separators =
569 options_get_string(&wp->window->options, "word-separators");
570 for (; np != 0; np--)
571 window_copy_cursor_previous_word(wp, word_separators);
572 break;
573 case MODEKEYCOPY_JUMP:
574 data->inputtype = WINDOW_COPY_JUMPFORWARD;
575 data->inputprompt = "Jump Forward";
576 *data->inputstr = '\0';
577 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
578 return; /* skip numprefix reset */
579 case MODEKEYCOPY_JUMPAGAIN:
580 if (data->jumptype == WINDOW_COPY_JUMPFORWARD) {
581 for (; np != 0; np--)
582 window_copy_cursor_jump(wp);
583 } else if (data->jumptype == WINDOW_COPY_JUMPBACK) {
584 for (; np != 0; np--)
585 window_copy_cursor_jump_back(wp);
587 break;
588 case MODEKEYCOPY_JUMPREVERSE:
589 if (data->jumptype == WINDOW_COPY_JUMPFORWARD) {
590 for (; np != 0; np--)
591 window_copy_cursor_jump_back(wp);
592 } else if (data->jumptype == WINDOW_COPY_JUMPBACK) {
593 for (; np != 0; np--)
594 window_copy_cursor_jump(wp);
596 break;
597 case MODEKEYCOPY_JUMPBACK:
598 data->inputtype = WINDOW_COPY_JUMPBACK;
599 data->inputprompt = "Jump Back";
600 *data->inputstr = '\0';
601 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
602 return; /* skip numprefix reset */
603 case MODEKEYCOPY_SEARCHUP:
604 data->inputtype = WINDOW_COPY_SEARCHUP;
605 data->inputprompt = "Search Up";
606 goto input_on;
607 case MODEKEYCOPY_SEARCHDOWN:
608 data->inputtype = WINDOW_COPY_SEARCHDOWN;
609 data->inputprompt = "Search Down";
610 goto input_on;
611 case MODEKEYCOPY_SEARCHAGAIN:
612 case MODEKEYCOPY_SEARCHREVERSE:
613 switch (data->searchtype) {
614 case WINDOW_COPY_OFF:
615 case WINDOW_COPY_GOTOLINE:
616 case WINDOW_COPY_JUMPFORWARD:
617 case WINDOW_COPY_JUMPBACK:
618 case WINDOW_COPY_NUMERICPREFIX:
619 break;
620 case WINDOW_COPY_SEARCHUP:
621 if (cmd == MODEKEYCOPY_SEARCHAGAIN) {
622 for (; np != 0; np--) {
623 window_copy_search_up(
624 wp, data->searchstr);
626 } else {
627 for (; np != 0; np--) {
628 window_copy_search_down(
629 wp, data->searchstr);
632 break;
633 case WINDOW_COPY_SEARCHDOWN:
634 if (cmd == MODEKEYCOPY_SEARCHAGAIN) {
635 for (; np != 0; np--) {
636 window_copy_search_down(
637 wp, data->searchstr);
639 } else {
640 for (; np != 0; np--) {
641 window_copy_search_up(
642 wp, data->searchstr);
645 break;
647 break;
648 case MODEKEYCOPY_GOTOLINE:
649 data->inputtype = WINDOW_COPY_GOTOLINE;
650 data->inputprompt = "Goto Line";
651 *data->inputstr = '\0';
652 goto input_on;
653 case MODEKEYCOPY_STARTNUMBERPREFIX:
654 key &= KEYC_MASK_KEY;
655 if (key >= '0' && key <= '9') {
656 data->inputtype = WINDOW_COPY_NUMERICPREFIX;
657 data->numprefix = 0;
658 window_copy_key_numeric_prefix(wp, key);
659 return;
661 break;
662 case MODEKEYCOPY_RECTANGLETOGGLE:
663 window_copy_rectangle_toggle(wp);
664 break;
665 default:
666 break;
669 data->numprefix = 0;
670 return;
672 input_on:
673 keys = options_get_number(&wp->window->options, "mode-keys");
674 if (keys == MODEKEY_EMACS)
675 mode_key_init(&data->mdata, &mode_key_tree_emacs_edit);
676 else
677 mode_key_init(&data->mdata, &mode_key_tree_vi_edit);
679 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
680 return;
682 input_off:
683 keys = options_get_number(&wp->window->options, "mode-keys");
684 if (keys == MODEKEY_EMACS)
685 mode_key_init(&data->mdata, &mode_key_tree_emacs_copy);
686 else
687 mode_key_init(&data->mdata, &mode_key_tree_vi_copy);
689 data->inputtype = WINDOW_COPY_OFF;
690 data->inputprompt = NULL;
692 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
696 window_copy_key_input(struct window_pane *wp, int key)
698 struct window_copy_mode_data *data = wp->modedata;
699 struct screen *s = &data->screen;
700 size_t inputlen;
701 u_int np;
703 switch (mode_key_lookup(&data->mdata, key)) {
704 case MODEKEYEDIT_CANCEL:
705 data->numprefix = 0;
706 return (-1);
707 case MODEKEYEDIT_BACKSPACE:
708 inputlen = strlen(data->inputstr);
709 if (inputlen > 0)
710 data->inputstr[inputlen - 1] = '\0';
711 break;
712 case MODEKEYEDIT_DELETELINE:
713 *data->inputstr = '\0';
714 break;
715 case MODEKEYEDIT_ENTER:
716 np = data->numprefix;
717 if (np == 0)
718 np = 1;
720 switch (data->inputtype) {
721 case WINDOW_COPY_OFF:
722 case WINDOW_COPY_JUMPFORWARD:
723 case WINDOW_COPY_JUMPBACK:
724 case WINDOW_COPY_NUMERICPREFIX:
725 break;
726 case WINDOW_COPY_SEARCHUP:
727 for (; np != 0; np--)
728 window_copy_search_up(wp, data->inputstr);
729 data->searchtype = data->inputtype;
730 data->searchstr = xstrdup(data->inputstr);
731 break;
732 case WINDOW_COPY_SEARCHDOWN:
733 for (; np != 0; np--)
734 window_copy_search_down(wp, data->inputstr);
735 data->searchtype = data->inputtype;
736 data->searchstr = xstrdup(data->inputstr);
737 break;
738 case WINDOW_COPY_GOTOLINE:
739 window_copy_goto_line(wp, data->inputstr);
740 *data->inputstr = '\0';
741 break;
743 data->numprefix = 0;
744 return (1);
745 case MODEKEY_OTHER:
746 if (key < 32 || key > 126)
747 break;
748 inputlen = strlen(data->inputstr) + 2;
750 data->inputstr = xrealloc(data->inputstr, 1, inputlen);
751 data->inputstr[inputlen - 2] = key;
752 data->inputstr[inputlen - 1] = '\0';
753 break;
754 default:
755 break;
758 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
759 return (0);
763 window_copy_key_numeric_prefix(struct window_pane *wp, int key)
765 struct window_copy_mode_data *data = wp->modedata;
766 struct screen *s = &data->screen;
768 key &= KEYC_MASK_KEY;
769 if (key < '0' || key > '9')
770 return 1;
772 if (data->numprefix >= 100) /* no more than three digits */
773 return 0;
774 data->numprefix = data->numprefix * 10 + key - '0';
776 window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1);
777 return 0;
780 /* ARGSUSED */
781 void
782 window_copy_mouse(
783 struct window_pane *wp, struct session *sess, struct mouse_event *m)
785 struct window_copy_mode_data *data = wp->modedata;
786 struct screen *s = &data->screen;
787 u_int i, old_cy;
789 if (m->x >= screen_size_x(s))
790 return;
791 if (m->y >= screen_size_y(s))
792 return;
794 /* If mouse wheel (buttons 4 and 5), scroll. */
795 if ((m->b & MOUSE_45)) {
796 if ((m->b & MOUSE_BUTTON) == MOUSE_1) {
797 for (i = 0; i < 5; i++)
798 window_copy_cursor_up(wp, 0);
799 } else if ((m->b & MOUSE_BUTTON) == MOUSE_2) {
800 old_cy = data->cy;
801 for (i = 0; i < 5; i++)
802 window_copy_cursor_down(wp, 0);
803 if (old_cy == data->cy)
804 goto reset_mode;
806 return;
810 * If already reading motion, move the cursor while buttons are still
811 * pressed, or stop the selection on their release.
813 if (s->mode & MODE_MOUSE_BUTTON) {
814 if ((m->b & MOUSE_BUTTON) != MOUSE_UP) {
815 window_copy_update_cursor(wp, m->x, m->y);
816 if (window_copy_update_selection(wp))
817 window_copy_redraw_screen(wp);
818 return;
820 goto reset_mode;
823 /* Otherwise if other buttons pressed, start selection and motion. */
824 if ((m->b & MOUSE_BUTTON) != MOUSE_UP) {
825 s->mode &= ~MODE_MOUSE_STANDARD;
826 s->mode |= MODE_MOUSE_BUTTON;
828 window_copy_update_cursor(wp, m->x, m->y);
829 window_copy_start_selection(wp);
830 window_copy_redraw_screen(wp);
833 return;
835 reset_mode:
836 s->mode &= ~MODE_MOUSE_BUTTON;
837 s->mode |= MODE_MOUSE_STANDARD;
838 if (sess != NULL) {
839 window_copy_copy_selection(wp);
840 window_pane_reset_mode(wp);
844 void
845 window_copy_scroll_to(struct window_pane *wp, u_int px, u_int py)
847 struct window_copy_mode_data *data = wp->modedata;
848 struct grid *gd = data->backing->grid;
849 u_int offset, gap;
851 data->cx = px;
853 gap = gd->sy / 4;
854 if (py < gd->sy) {
855 offset = 0;
856 data->cy = py;
857 } else if (py > gd->hsize + gd->sy - gap) {
858 offset = gd->hsize;
859 data->cy = py - gd->hsize;
860 } else {
861 offset = py + gap - gd->sy;
862 data->cy = py - offset;
864 data->oy = gd->hsize - offset;
866 window_copy_update_selection(wp);
867 window_copy_redraw_screen(wp);
871 window_copy_search_compare(
872 struct grid *gd, u_int px, u_int py, struct grid *sgd, u_int spx)
874 const struct grid_cell *gc, *sgc;
875 const struct grid_utf8 *gu, *sgu;
877 gc = grid_peek_cell(gd, px, py);
878 sgc = grid_peek_cell(sgd, spx, 0);
880 if ((gc->flags & GRID_FLAG_UTF8) != (sgc->flags & GRID_FLAG_UTF8))
881 return (0);
883 if (gc->flags & GRID_FLAG_UTF8) {
884 gu = grid_peek_utf8(gd, px, py);
885 sgu = grid_peek_utf8(sgd, spx, 0);
886 if (grid_utf8_compare(gu, sgu))
887 return (1);
888 } else {
889 if (gc->data == sgc->data)
890 return (1);
892 return (0);
896 window_copy_search_lr(struct grid *gd,
897 struct grid *sgd, u_int *ppx, u_int py, u_int first, u_int last)
899 u_int ax, bx, px;
901 for (ax = first; ax < last; ax++) {
902 if (ax + sgd->sx >= gd->sx)
903 break;
904 for (bx = 0; bx < sgd->sx; bx++) {
905 px = ax + bx;
906 if (!window_copy_search_compare(gd, px, py, sgd, bx))
907 break;
909 if (bx == sgd->sx) {
910 *ppx = ax;
911 return (1);
914 return (0);
918 window_copy_search_rl(struct grid *gd,
919 struct grid *sgd, u_int *ppx, u_int py, u_int first, u_int last)
921 u_int ax, bx, px;
923 for (ax = last + 1; ax > first; ax--) {
924 if (gd->sx - (ax - 1) < sgd->sx)
925 continue;
926 for (bx = 0; bx < sgd->sx; bx++) {
927 px = ax - 1 + bx;
928 if (!window_copy_search_compare(gd, px, py, sgd, bx))
929 break;
931 if (bx == sgd->sx) {
932 *ppx = ax - 1;
933 return (1);
936 return (0);
939 void
940 window_copy_search_up(struct window_pane *wp, const char *searchstr)
942 struct window_copy_mode_data *data = wp->modedata;
943 struct screen *s = data->backing, ss;
944 struct screen_write_ctx ctx;
945 struct grid *gd = s->grid, *sgd;
946 struct grid_cell gc;
947 size_t searchlen;
948 u_int i, last, fx, fy, px;
949 int utf8flag, n, wrapped;
951 if (*searchstr == '\0')
952 return;
953 utf8flag = options_get_number(&wp->window->options, "utf8");
954 searchlen = screen_write_strlen(utf8flag, "%s", searchstr);
956 screen_init(&ss, searchlen, 1, 0);
957 screen_write_start(&ctx, NULL, &ss);
958 memcpy(&gc, &grid_default_cell, sizeof gc);
959 screen_write_nputs(&ctx, -1, &gc, utf8flag, "%s", searchstr);
960 screen_write_stop(&ctx);
962 fx = data->cx;
963 fy = gd->hsize - data->oy + data->cy;
965 if (fx == 0) {
966 if (fy == 0)
967 return;
968 fx = gd->sx - 1;
969 fy--;
970 } else
971 fx--;
972 n = wrapped = 0;
974 retry:
975 sgd = ss.grid;
976 for (i = fy + 1; i > 0; i--) {
977 last = screen_size_x(s);
978 if (i == fy + 1)
979 last = fx;
980 n = window_copy_search_rl(gd, sgd, &px, i - 1, 0, last);
981 if (n) {
982 window_copy_scroll_to(wp, px, i - 1);
983 break;
986 if (!n && !wrapped) {
987 fx = gd->sx - 1;
988 fy = gd->hsize + gd->sy - 1;
989 wrapped = 1;
990 goto retry;
993 screen_free(&ss);
996 void
997 window_copy_search_down(struct window_pane *wp, const char *searchstr)
999 struct window_copy_mode_data *data = wp->modedata;
1000 struct screen *s = data->backing, ss;
1001 struct screen_write_ctx ctx;
1002 struct grid *gd = s->grid, *sgd;
1003 struct grid_cell gc;
1004 size_t searchlen;
1005 u_int i, first, fx, fy, px;
1006 int utf8flag, n, wrapped;
1008 if (*searchstr == '\0')
1009 return;
1010 utf8flag = options_get_number(&wp->window->options, "utf8");
1011 searchlen = screen_write_strlen(utf8flag, "%s", searchstr);
1013 screen_init(&ss, searchlen, 1, 0);
1014 screen_write_start(&ctx, NULL, &ss);
1015 memcpy(&gc, &grid_default_cell, sizeof gc);
1016 screen_write_nputs(&ctx, -1, &gc, utf8flag, "%s", searchstr);
1017 screen_write_stop(&ctx);
1019 fx = data->cx;
1020 fy = gd->hsize - data->oy + data->cy;
1022 if (fx == gd->sx - 1) {
1023 if (fy == gd->hsize + gd->sy)
1024 return;
1025 fx = 0;
1026 fy++;
1027 } else
1028 fx++;
1029 n = wrapped = 0;
1031 retry:
1032 sgd = ss.grid;
1033 for (i = fy + 1; i < gd->hsize + gd->sy; i++) {
1034 first = 0;
1035 if (i == fy + 1)
1036 first = fx;
1037 n = window_copy_search_lr(gd, sgd, &px, i - 1, first, gd->sx);
1038 if (n) {
1039 window_copy_scroll_to(wp, px, i - 1);
1040 break;
1043 if (!n && !wrapped) {
1044 fx = 0;
1045 fy = 0;
1046 wrapped = 1;
1047 goto retry;
1050 screen_free(&ss);
1053 void
1054 window_copy_goto_line(struct window_pane *wp, const char *linestr)
1056 struct window_copy_mode_data *data = wp->modedata;
1057 const char *errstr;
1058 u_int lineno;
1060 lineno = strtonum(linestr, 0, screen_hsize(data->backing), &errstr);
1061 if (errstr != NULL)
1062 return;
1064 data->oy = lineno;
1065 window_copy_update_selection(wp);
1066 window_copy_redraw_screen(wp);
1069 void
1070 window_copy_write_line(
1071 struct window_pane *wp, struct screen_write_ctx *ctx, u_int py)
1073 struct window_copy_mode_data *data = wp->modedata;
1074 struct screen *s = &data->screen;
1075 struct options *oo = &wp->window->options;
1076 struct grid_cell gc;
1077 char hdr[32];
1078 size_t last, xoff = 0, size = 0;
1080 memcpy(&gc, &grid_default_cell, sizeof gc);
1081 colour_set_fg(&gc, options_get_number(oo, "mode-fg"));
1082 colour_set_bg(&gc, options_get_number(oo, "mode-bg"));
1083 gc.attr |= options_get_number(oo, "mode-attr");
1085 last = screen_size_y(s) - 1;
1086 if (py == 0) {
1087 size = xsnprintf(hdr, sizeof hdr,
1088 "[%u/%u]", data->oy, screen_hsize(data->backing));
1089 if (size > screen_size_x(s))
1090 size = screen_size_x(s);
1091 screen_write_cursormove(ctx, screen_size_x(s) - size, 0);
1092 screen_write_puts(ctx, &gc, "%s", hdr);
1093 } else if (py == last && data->inputtype != WINDOW_COPY_OFF) {
1094 if (data->inputtype == WINDOW_COPY_NUMERICPREFIX) {
1095 xoff = size = xsnprintf(hdr, sizeof hdr,
1096 "Repeat: %u", data->numprefix);
1097 } else {
1098 xoff = size = xsnprintf(hdr, sizeof hdr,
1099 "%s: %s", data->inputprompt, data->inputstr);
1101 screen_write_cursormove(ctx, 0, last);
1102 screen_write_puts(ctx, &gc, "%s", hdr);
1103 } else
1104 size = 0;
1106 screen_write_cursormove(ctx, xoff, py);
1107 screen_write_copy(ctx, data->backing, xoff,
1108 (screen_hsize(data->backing) - data->oy) + py,
1109 screen_size_x(s) - size, 1);
1111 if (py == data->cy && data->cx == screen_size_x(s)) {
1112 memcpy(&gc, &grid_default_cell, sizeof gc);
1113 screen_write_cursormove(ctx, screen_size_x(s) - 1, py);
1114 screen_write_putc(ctx, &gc, '$');
1118 void
1119 window_copy_write_lines(
1120 struct window_pane *wp, struct screen_write_ctx *ctx, u_int py, u_int ny)
1122 u_int yy;
1124 for (yy = py; yy < py + ny; yy++)
1125 window_copy_write_line(wp, ctx, py);
1128 void
1129 window_copy_redraw_lines(struct window_pane *wp, u_int py, u_int ny)
1131 struct window_copy_mode_data *data = wp->modedata;
1132 struct screen_write_ctx ctx;
1133 u_int i;
1135 screen_write_start(&ctx, wp, NULL);
1136 for (i = py; i < py + ny; i++)
1137 window_copy_write_line(wp, &ctx, i);
1138 screen_write_cursormove(&ctx, data->cx, data->cy);
1139 screen_write_stop(&ctx);
1142 void
1143 window_copy_redraw_screen(struct window_pane *wp)
1145 struct window_copy_mode_data *data = wp->modedata;
1147 window_copy_redraw_lines(wp, 0, screen_size_y(&data->screen));
1150 void
1151 window_copy_update_cursor(struct window_pane *wp, u_int cx, u_int cy)
1153 struct window_copy_mode_data *data = wp->modedata;
1154 struct screen *s = &data->screen;
1155 struct screen_write_ctx ctx;
1156 u_int old_cx, old_cy;
1158 old_cx = data->cx; old_cy = data->cy;
1159 data->cx = cx; data->cy = cy;
1160 if (old_cx == screen_size_x(s))
1161 window_copy_redraw_lines(wp, old_cy, 1);
1162 if (data->cx == screen_size_x(s))
1163 window_copy_redraw_lines(wp, data->cy, 1);
1164 else {
1165 screen_write_start(&ctx, wp, NULL);
1166 screen_write_cursormove(&ctx, data->cx, data->cy);
1167 screen_write_stop(&ctx);
1171 void
1172 window_copy_start_selection(struct window_pane *wp)
1174 struct window_copy_mode_data *data = wp->modedata;
1175 struct screen *s = &data->screen;
1177 data->selx = data->cx;
1178 data->sely = screen_hsize(data->backing) + data->cy - data->oy;
1180 s->sel.flag = 1;
1181 window_copy_update_selection(wp);
1185 window_copy_update_selection(struct window_pane *wp)
1187 struct window_copy_mode_data *data = wp->modedata;
1188 struct screen *s = &data->screen;
1189 struct options *oo = &wp->window->options;
1190 struct grid_cell gc;
1191 u_int sx, sy, ty, cy;
1193 if (!s->sel.flag)
1194 return (0);
1196 /* Set colours. */
1197 memcpy(&gc, &grid_default_cell, sizeof gc);
1198 colour_set_fg(&gc, options_get_number(oo, "mode-fg"));
1199 colour_set_bg(&gc, options_get_number(oo, "mode-bg"));
1200 gc.attr |= options_get_number(oo, "mode-attr");
1202 /* Find top of screen. */
1203 ty = screen_hsize(data->backing) - data->oy;
1205 /* Adjust the selection. */
1206 sx = data->selx;
1207 sy = data->sely;
1208 if (sy < ty) { /* above screen */
1209 if (!data->rectflag)
1210 sx = 0;
1211 sy = 0;
1212 } else if (sy > ty + screen_size_y(s) - 1) { /* below screen */
1213 if (!data->rectflag)
1214 sx = screen_size_x(s) - 1;
1215 sy = screen_size_y(s) - 1;
1216 } else
1217 sy -= ty;
1218 sy = screen_hsize(s) + sy;
1220 screen_set_selection(s,
1221 sx, sy, data->cx, screen_hsize(s) + data->cy, data->rectflag, &gc);
1223 if (data->rectflag) {
1225 * Can't rely on the caller to redraw the right lines for
1226 * rectangle selection - find the highest line and the number
1227 * of lines, and redraw just past that in both directions
1229 cy = data->cy;
1230 if (sy < cy)
1231 window_copy_redraw_lines(wp, sy, cy - sy + 1);
1232 else
1233 window_copy_redraw_lines(wp, cy, sy - cy + 1);
1236 return (1);
1239 void
1240 window_copy_copy_selection(struct window_pane *wp)
1242 struct window_copy_mode_data *data = wp->modedata;
1243 struct screen *s = &data->screen;
1244 char *buf;
1245 size_t off;
1246 u_int i, xx, yy, sx, sy, ex, ey, limit;
1247 u_int firstsx, lastex, restex, restsx;
1248 int keys;
1250 if (!s->sel.flag)
1251 return;
1253 buf = xmalloc(1);
1254 off = 0;
1256 *buf = '\0';
1259 * The selection extends from selx,sely to (adjusted) cx,cy on
1260 * the base screen.
1263 /* Find start and end. */
1264 xx = data->cx;
1265 yy = screen_hsize(data->backing) + data->cy - data->oy;
1266 if (yy < data->sely || (yy == data->sely && xx < data->selx)) {
1267 sx = xx; sy = yy;
1268 ex = data->selx; ey = data->sely;
1269 } else {
1270 sx = data->selx; sy = data->sely;
1271 ex = xx; ey = yy;
1274 /* Trim ex to end of line. */
1275 xx = window_copy_find_length(wp, ey);
1276 if (ex > xx)
1277 ex = xx;
1280 * Deal with rectangle-copy if necessary; four situations: start of
1281 * first line (firstsx), end of last line (lastex), start (restsx) and
1282 * end (restex) of all other lines.
1284 xx = screen_size_x(s);
1287 * Behave according to mode-keys. If it is emacs, copy like emacs,
1288 * keeping the top-left-most character, and dropping the
1289 * bottom-right-most, regardless of copy direction. If it is vi, also
1290 * keep bottom-right-most character.
1292 keys = options_get_number(&wp->window->options, "mode-keys");
1293 if (data->rectflag) {
1295 * Need to ignore the column with the cursor in it, which for
1296 * rectangular copy means knowing which side the cursor is on.
1298 if (data->selx < data->cx) {
1299 /* Selection start is on the left. */
1300 if (keys == MODEKEY_EMACS) {
1301 lastex = data->cx;
1302 restex = data->cx;
1304 else {
1305 lastex = data->cx + 1;
1306 restex = data->cx + 1;
1308 firstsx = data->selx;
1309 restsx = data->selx;
1310 } else {
1311 /* Cursor is on the left. */
1312 lastex = data->selx + 1;
1313 restex = data->selx + 1;
1314 firstsx = data->cx;
1315 restsx = data->cx;
1317 } else {
1318 if (keys == MODEKEY_EMACS)
1319 lastex = ex;
1320 else
1321 lastex = ex + 1;
1322 restex = xx;
1323 firstsx = sx;
1324 restsx = 0;
1327 /* Copy the lines. */
1328 if (sy == ey)
1329 window_copy_copy_line(wp, &buf, &off, sy, firstsx, lastex);
1330 else {
1331 window_copy_copy_line(wp, &buf, &off, sy, firstsx, restex);
1332 if (ey - sy > 1) {
1333 for (i = sy + 1; i < ey; i++) {
1334 window_copy_copy_line(
1335 wp, &buf, &off, i, restsx, restex);
1338 window_copy_copy_line(wp, &buf, &off, ey, restsx, lastex);
1341 /* Don't bother if no data. */
1342 if (off == 0) {
1343 xfree(buf);
1344 return;
1346 off--; /* remove final \n */
1348 if (options_get_number(&global_options, "set-clipboard"))
1349 screen_write_setselection(&wp->ictx.ctx, buf, off);
1351 /* Add the buffer to the stack. */
1352 limit = options_get_number(&global_options, "buffer-limit");
1353 paste_add(&global_buffers, buf, off, limit);
1356 void
1357 window_copy_copy_line(struct window_pane *wp,
1358 char **buf, size_t *off, u_int sy, u_int sx, u_int ex)
1360 struct window_copy_mode_data *data = wp->modedata;
1361 struct grid *gd = data->backing->grid;
1362 const struct grid_cell *gc;
1363 const struct grid_utf8 *gu;
1364 struct grid_line *gl;
1365 u_int i, xx, wrapped = 0;
1366 size_t size;
1368 if (sx > ex)
1369 return;
1372 * Work out if the line was wrapped at the screen edge and all of it is
1373 * on screen.
1375 gl = &gd->linedata[sy];
1376 if (gl->flags & GRID_LINE_WRAPPED && gl->cellsize <= gd->sx)
1377 wrapped = 1;
1379 /* If the line was wrapped, don't strip spaces (use the full length). */
1380 if (wrapped)
1381 xx = gl->cellsize;
1382 else
1383 xx = window_copy_find_length(wp, sy);
1384 if (ex > xx)
1385 ex = xx;
1386 if (sx > xx)
1387 sx = xx;
1389 if (sx < ex) {
1390 for (i = sx; i < ex; i++) {
1391 gc = grid_peek_cell(gd, i, sy);
1392 if (gc->flags & GRID_FLAG_PADDING)
1393 continue;
1394 if (!(gc->flags & GRID_FLAG_UTF8)) {
1395 *buf = xrealloc(*buf, 1, (*off) + 1);
1396 (*buf)[(*off)++] = gc->data;
1397 } else {
1398 gu = grid_peek_utf8(gd, i, sy);
1399 size = grid_utf8_size(gu);
1400 *buf = xrealloc(*buf, 1, (*off) + size);
1401 *off += grid_utf8_copy(gu, *buf + *off, size);
1406 /* Only add a newline if the line wasn't wrapped. */
1407 if (!wrapped || ex != xx) {
1408 *buf = xrealloc(*buf, 1, (*off) + 1);
1409 (*buf)[(*off)++] = '\n';
1413 void
1414 window_copy_clear_selection(struct window_pane *wp)
1416 struct window_copy_mode_data *data = wp->modedata;
1417 u_int px, py;
1419 screen_clear_selection(&data->screen);
1421 py = screen_hsize(data->backing) + data->cy - data->oy;
1422 px = window_copy_find_length(wp, py);
1423 if (data->cx > px)
1424 window_copy_update_cursor(wp, px, data->cy);
1428 window_copy_in_set(struct window_pane *wp, u_int px, u_int py, const char *set)
1430 struct window_copy_mode_data *data = wp->modedata;
1431 const struct grid_cell *gc;
1433 gc = grid_peek_cell(data->backing->grid, px, py);
1434 if (gc->flags & (GRID_FLAG_PADDING|GRID_FLAG_UTF8))
1435 return (0);
1436 if (gc->data == 0x00 || gc->data == 0x7f)
1437 return (0);
1438 return (strchr(set, gc->data) != NULL);
1441 u_int
1442 window_copy_find_length(struct window_pane *wp, u_int py)
1444 struct window_copy_mode_data *data = wp->modedata;
1445 struct screen *s = data->backing;
1446 const struct grid_cell *gc;
1447 u_int px;
1450 * If the pane has been resized, its grid can contain old overlong
1451 * lines. grid_peek_cell does not allow accessing cells beyond the
1452 * width of the grid, and screen_write_copy treats them as spaces, so
1453 * ignore them here too.
1455 px = s->grid->linedata[py].cellsize;
1456 if (px > screen_size_x(s))
1457 px = screen_size_x(s);
1458 while (px > 0) {
1459 gc = grid_peek_cell(s->grid, px - 1, py);
1460 if (gc->flags & GRID_FLAG_UTF8)
1461 break;
1462 if (gc->data != ' ')
1463 break;
1464 px--;
1466 return (px);
1469 void
1470 window_copy_cursor_start_of_line(struct window_pane *wp)
1472 struct window_copy_mode_data *data = wp->modedata;
1473 struct screen *back_s = data->backing;
1474 struct grid *gd = back_s->grid;
1475 u_int py;
1477 if (data->cx == 0) {
1478 py = screen_hsize(back_s) + data->cy - data->oy;
1479 while (py > 0 && gd->linedata[py-1].flags & GRID_LINE_WRAPPED) {
1480 window_copy_cursor_up(wp, 0);
1481 py = screen_hsize(back_s) + data->cy - data->oy;
1484 window_copy_update_cursor(wp, 0, data->cy);
1485 if (window_copy_update_selection(wp))
1486 window_copy_redraw_lines(wp, data->cy, 1);
1489 void
1490 window_copy_cursor_back_to_indentation(struct window_pane *wp)
1492 struct window_copy_mode_data *data = wp->modedata;
1493 u_int px, py, xx;
1494 const struct grid_cell *gc;
1496 px = 0;
1497 py = screen_hsize(data->backing) + data->cy - data->oy;
1498 xx = window_copy_find_length(wp, py);
1500 while (px < xx) {
1501 gc = grid_peek_cell(data->backing->grid, px, py);
1502 if (gc->flags & GRID_FLAG_UTF8)
1503 break;
1504 if (gc->data != ' ')
1505 break;
1506 px++;
1509 window_copy_update_cursor(wp, px, data->cy);
1510 if (window_copy_update_selection(wp))
1511 window_copy_redraw_lines(wp, data->cy, 1);
1514 void
1515 window_copy_cursor_end_of_line(struct window_pane *wp)
1517 struct window_copy_mode_data *data = wp->modedata;
1518 struct screen *back_s = data->backing;
1519 struct grid *gd = back_s->grid;
1520 u_int px, py;
1522 py = screen_hsize(back_s) + data->cy - data->oy;
1523 px = window_copy_find_length(wp, py);
1525 if (data->cx == px) {
1526 if (data->screen.sel.flag && data->rectflag)
1527 px = screen_size_x(back_s);
1528 if (gd->linedata[py].flags & GRID_LINE_WRAPPED) {
1529 while (py < gd->sy + gd->hsize &&
1530 gd->linedata[py].flags & GRID_LINE_WRAPPED) {
1531 window_copy_cursor_down(wp, 0);
1532 py = screen_hsize(back_s)
1533 + data->cy - data->oy;
1535 px = window_copy_find_length(wp, py);
1538 window_copy_update_cursor(wp, px, data->cy);
1540 if (window_copy_update_selection(wp))
1541 window_copy_redraw_lines(wp, data->cy, 1);
1544 void
1545 window_copy_cursor_left(struct window_pane *wp)
1547 struct window_copy_mode_data *data = wp->modedata;
1549 if (data->cx == 0) {
1550 window_copy_cursor_up(wp, 0);
1551 window_copy_cursor_end_of_line(wp);
1552 } else {
1553 window_copy_update_cursor(wp, data->cx - 1, data->cy);
1554 if (window_copy_update_selection(wp))
1555 window_copy_redraw_lines(wp, data->cy, 1);
1559 void
1560 window_copy_cursor_right(struct window_pane *wp)
1562 struct window_copy_mode_data *data = wp->modedata;
1563 u_int px, py;
1565 if (data->screen.sel.flag && data->rectflag)
1566 px = screen_size_x(&data->screen);
1567 else {
1568 py = screen_hsize(data->backing) + data->cy - data->oy;
1569 px = window_copy_find_length(wp, py);
1572 if (data->cx >= px) {
1573 window_copy_cursor_start_of_line(wp);
1574 window_copy_cursor_down(wp, 0);
1575 } else {
1576 window_copy_update_cursor(wp, data->cx + 1, data->cy);
1577 if (window_copy_update_selection(wp))
1578 window_copy_redraw_lines(wp, data->cy, 1);
1582 void
1583 window_copy_cursor_up(struct window_pane *wp, int scroll_only)
1585 struct window_copy_mode_data *data = wp->modedata;
1586 struct screen *s = &data->screen;
1587 u_int ox, oy, px, py;
1589 oy = screen_hsize(data->backing) + data->cy - data->oy;
1590 ox = window_copy_find_length(wp, oy);
1591 if (ox != 0) {
1592 data->lastcx = data->cx;
1593 data->lastsx = ox;
1596 data->cx = data->lastcx;
1597 if (scroll_only || data->cy == 0) {
1598 window_copy_scroll_down(wp, 1);
1599 if (scroll_only) {
1600 if (data->cy == screen_size_y(s) - 1)
1601 window_copy_redraw_lines(wp, data->cy, 1);
1602 else
1603 window_copy_redraw_lines(wp, data->cy, 2);
1605 } else {
1606 window_copy_update_cursor(wp, data->cx, data->cy - 1);
1607 if (window_copy_update_selection(wp)) {
1608 if (data->cy == screen_size_y(s) - 1)
1609 window_copy_redraw_lines(wp, data->cy, 1);
1610 else
1611 window_copy_redraw_lines(wp, data->cy, 2);
1615 if (!data->screen.sel.flag || !data->rectflag) {
1616 py = screen_hsize(data->backing) + data->cy - data->oy;
1617 px = window_copy_find_length(wp, py);
1618 if ((data->cx >= data->lastsx && data->cx != px) ||
1619 data->cx > px)
1620 window_copy_cursor_end_of_line(wp);
1624 void
1625 window_copy_cursor_down(struct window_pane *wp, int scroll_only)
1627 struct window_copy_mode_data *data = wp->modedata;
1628 struct screen *s = &data->screen;
1629 u_int ox, oy, px, py;
1631 oy = screen_hsize(data->backing) + data->cy - data->oy;
1632 ox = window_copy_find_length(wp, oy);
1633 if (ox != 0) {
1634 data->lastcx = data->cx;
1635 data->lastsx = ox;
1638 data->cx = data->lastcx;
1639 if (scroll_only || data->cy == screen_size_y(s) - 1) {
1640 window_copy_scroll_up(wp, 1);
1641 if (scroll_only && data->cy > 0)
1642 window_copy_redraw_lines(wp, data->cy - 1, 2);
1643 } else {
1644 window_copy_update_cursor(wp, data->cx, data->cy + 1);
1645 if (window_copy_update_selection(wp))
1646 window_copy_redraw_lines(wp, data->cy - 1, 2);
1649 if (!data->screen.sel.flag || !data->rectflag) {
1650 py = screen_hsize(data->backing) + data->cy - data->oy;
1651 px = window_copy_find_length(wp, py);
1652 if ((data->cx >= data->lastsx && data->cx != px) ||
1653 data->cx > px)
1654 window_copy_cursor_end_of_line(wp);
1658 void
1659 window_copy_cursor_jump(struct window_pane *wp)
1661 struct window_copy_mode_data *data = wp->modedata;
1662 struct screen *back_s = data->backing;
1663 const struct grid_cell *gc;
1664 u_int px, py, xx;
1666 px = data->cx + 1;
1667 py = screen_hsize(back_s) + data->cy - data->oy;
1668 xx = window_copy_find_length(wp, py);
1670 while (px < xx) {
1671 gc = grid_peek_cell(back_s->grid, px, py);
1672 if ((gc->flags & (GRID_FLAG_PADDING|GRID_FLAG_UTF8)) == 0
1673 && gc->data == data->jumpchar) {
1675 window_copy_update_cursor(wp, px, data->cy);
1676 if (window_copy_update_selection(wp))
1677 window_copy_redraw_lines(wp, data->cy, 1);
1678 return;
1680 px++;
1684 void
1685 window_copy_cursor_jump_back(struct window_pane *wp)
1687 struct window_copy_mode_data *data = wp->modedata;
1688 struct screen *back_s = data->backing;
1689 const struct grid_cell *gc;
1690 u_int px, py;
1692 px = data->cx;
1693 py = screen_hsize(back_s) + data->cy - data->oy;
1695 if (px > 0)
1696 px--;
1698 for (;;) {
1699 gc = grid_peek_cell(back_s->grid, px, py);
1700 if ((gc->flags & (GRID_FLAG_PADDING|GRID_FLAG_UTF8)) == 0
1701 && gc->data == data->jumpchar) {
1703 window_copy_update_cursor(wp, px, data->cy);
1704 if (window_copy_update_selection(wp))
1705 window_copy_redraw_lines(wp, data->cy, 1);
1706 return;
1708 if (px == 0)
1709 break;
1710 px--;
1714 void
1715 window_copy_cursor_next_word(struct window_pane *wp, const char *separators)
1717 struct window_copy_mode_data *data = wp->modedata;
1718 struct screen *back_s = data->backing;
1719 u_int px, py, xx, yy;
1720 int expected = 0;
1722 px = data->cx;
1723 py = screen_hsize(back_s) + data->cy - data->oy;
1724 xx = window_copy_find_length(wp, py);
1725 yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
1728 * First skip past any nonword characters and then any word characters.
1730 * expected is initially set to 0 for the former and then 1 for the
1731 * latter.
1733 do {
1734 while (px > xx ||
1735 window_copy_in_set(wp, px, py, separators) == expected) {
1736 /* Move down if we're past the end of the line. */
1737 if (px > xx) {
1738 if (py == yy)
1739 return;
1740 window_copy_cursor_down(wp, 0);
1741 px = 0;
1743 py = screen_hsize(back_s) + data->cy - data->oy;
1744 xx = window_copy_find_length(wp, py);
1745 } else
1746 px++;
1748 expected = !expected;
1749 } while (expected == 1);
1751 window_copy_update_cursor(wp, px, data->cy);
1752 if (window_copy_update_selection(wp))
1753 window_copy_redraw_lines(wp, data->cy, 1);
1756 void
1757 window_copy_cursor_next_word_end(struct window_pane *wp, const char *separators)
1759 struct window_copy_mode_data *data = wp->modedata;
1760 struct screen *back_s = data->backing;
1761 u_int px, py, xx, yy;
1762 int expected = 1;
1764 px = data->cx;
1765 py = screen_hsize(back_s) + data->cy - data->oy;
1766 xx = window_copy_find_length(wp, py);
1767 yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
1770 * First skip past any word characters, then any nonword characters.
1772 * expected is initially set to 1 for the former and then 0 for the
1773 * latter.
1775 do {
1776 while (px > xx ||
1777 window_copy_in_set(wp, px, py, separators) == expected) {
1778 /* Move down if we're past the end of the line. */
1779 if (px > xx) {
1780 if (py == yy)
1781 return;
1782 window_copy_cursor_down(wp, 0);
1783 px = 0;
1785 py = screen_hsize(back_s) + data->cy - data->oy;
1786 xx = window_copy_find_length(wp, py);
1787 } else
1788 px++;
1790 expected = !expected;
1791 } while (expected == 0);
1793 window_copy_update_cursor(wp, px, data->cy);
1794 if (window_copy_update_selection(wp))
1795 window_copy_redraw_lines(wp, data->cy, 1);
1798 /* Move to the previous place where a word begins. */
1799 void
1800 window_copy_cursor_previous_word(struct window_pane *wp, const char *separators)
1802 struct window_copy_mode_data *data = wp->modedata;
1803 u_int px, py;
1805 px = data->cx;
1806 py = screen_hsize(data->backing) + data->cy - data->oy;
1808 /* Move back to the previous word character. */
1809 for (;;) {
1810 if (px > 0) {
1811 px--;
1812 if (!window_copy_in_set(wp, px, py, separators))
1813 break;
1814 } else {
1815 if (data->cy == 0 &&
1816 (screen_hsize(data->backing) == 0 ||
1817 data->oy >= screen_hsize(data->backing) - 1))
1818 goto out;
1819 window_copy_cursor_up(wp, 0);
1821 py = screen_hsize(data->backing) + data->cy - data->oy;
1822 px = window_copy_find_length(wp, py);
1826 /* Move back to the beginning of this word. */
1827 while (px > 0 && !window_copy_in_set(wp, px - 1, py, separators))
1828 px--;
1830 out:
1831 window_copy_update_cursor(wp, px, data->cy);
1832 if (window_copy_update_selection(wp))
1833 window_copy_redraw_lines(wp, data->cy, 1);
1836 void
1837 window_copy_scroll_up(struct window_pane *wp, u_int ny)
1839 struct window_copy_mode_data *data = wp->modedata;
1840 struct screen *s = &data->screen;
1841 struct screen_write_ctx ctx;
1843 if (data->oy < ny)
1844 ny = data->oy;
1845 if (ny == 0)
1846 return;
1847 data->oy -= ny;
1849 screen_write_start(&ctx, wp, NULL);
1850 screen_write_cursormove(&ctx, 0, 0);
1851 screen_write_deleteline(&ctx, ny);
1852 window_copy_write_lines(wp, &ctx, screen_size_y(s) - ny, ny);
1853 window_copy_write_line(wp, &ctx, 0);
1854 if (screen_size_y(s) > 1)
1855 window_copy_write_line(wp, &ctx, 1);
1856 if (screen_size_y(s) > 3)
1857 window_copy_write_line(wp, &ctx, screen_size_y(s) - 2);
1858 if (s->sel.flag && screen_size_y(s) > ny) {
1859 window_copy_update_selection(wp);
1860 window_copy_write_line(wp, &ctx, screen_size_y(s) - ny - 1);
1862 screen_write_cursormove(&ctx, data->cx, data->cy);
1863 window_copy_update_selection(wp);
1864 screen_write_stop(&ctx);
1867 void
1868 window_copy_scroll_down(struct window_pane *wp, u_int ny)
1870 struct window_copy_mode_data *data = wp->modedata;
1871 struct screen *s = &data->screen;
1872 struct screen_write_ctx ctx;
1874 if (ny > screen_hsize(data->backing))
1875 return;
1877 if (data->oy > screen_hsize(data->backing) - ny)
1878 ny = screen_hsize(data->backing) - data->oy;
1879 if (ny == 0)
1880 return;
1881 data->oy += ny;
1883 screen_write_start(&ctx, wp, NULL);
1884 screen_write_cursormove(&ctx, 0, 0);
1885 screen_write_insertline(&ctx, ny);
1886 window_copy_write_lines(wp, &ctx, 0, ny);
1887 if (s->sel.flag && screen_size_y(s) > ny) {
1888 window_copy_update_selection(wp);
1889 window_copy_write_line(wp, &ctx, ny);
1890 } else if (ny == 1) /* nuke position */
1891 window_copy_write_line(wp, &ctx, 1);
1892 screen_write_cursormove(&ctx, data->cx, data->cy);
1893 window_copy_update_selection(wp);
1894 screen_write_stop(&ctx);
1897 void
1898 window_copy_rectangle_toggle(struct window_pane *wp)
1900 struct window_copy_mode_data *data = wp->modedata;
1901 u_int px, py;
1903 data->rectflag = !data->rectflag;
1905 py = screen_hsize(data->backing) + data->cy - data->oy;
1906 px = window_copy_find_length(wp, py);
1907 if (data->cx > px)
1908 window_copy_update_cursor(wp, px, data->cy);
1910 window_copy_update_selection(wp);
1911 window_copy_redraw_screen(wp);