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>
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
= {
86 enum window_copy_input_type
{
88 WINDOW_COPY_NUMERICPREFIX
,
90 WINDOW_COPY_SEARCHDOWN
,
91 WINDOW_COPY_JUMPFORWARD
,
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
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
;
125 u_int rectflag
; /* are we in rectangle copy mode? */
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
;
139 enum window_copy_input_type searchtype
;
142 enum window_copy_input_type jumptype
;
147 window_copy_init(struct window_pane
*wp
)
149 struct window_copy_mode_data
*data
;
153 wp
->modedata
= data
= xmalloc(sizeof *data
);
161 data
->backing_written
= 0;
165 data
->inputtype
= WINDOW_COPY_OFF
;
166 data
->inputprompt
= NULL
;
167 data
->inputstr
= xstrdup("");
170 data
->searchtype
= WINDOW_COPY_OFF
;
171 data
->searchstr
= NULL
;
174 bufferevent_disable(wp
->event
, EV_READ
|EV_WRITE
);
176 data
->jumptype
= WINDOW_COPY_OFF
;
177 data
->jumpchar
= '\0';
180 screen_init(s
, screen_size_x(&wp
->base
), screen_size_y(&wp
->base
), 0);
181 if (options_get_number(&wp
->window
->options
, "mode-mouse"))
182 s
->mode
|= MODE_MOUSE_STANDARD
;
184 keys
= options_get_number(&wp
->window
->options
, "mode-keys");
185 if (keys
== MODEKEY_EMACS
)
186 mode_key_init(&data
->mdata
, &mode_key_tree_emacs_copy
);
188 mode_key_init(&data
->mdata
, &mode_key_tree_vi_copy
);
190 data
->backing
= NULL
;
196 window_copy_init_from_pane(struct window_pane
*wp
)
198 struct window_copy_mode_data
*data
= wp
->modedata
;
199 struct screen
*s
= &data
->screen
;
200 struct screen_write_ctx ctx
;
203 if (wp
->mode
!= &window_copy_mode
)
204 fatalx("not in copy mode");
206 data
->backing
= &wp
->base
;
207 data
->cx
= data
->backing
->cx
;
208 data
->cy
= data
->backing
->cy
;
213 screen_write_start(&ctx
, NULL
, s
);
214 for (i
= 0; i
< screen_size_y(s
); i
++)
215 window_copy_write_line(wp
, &ctx
, i
);
216 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
);
217 screen_write_stop(&ctx
);
221 window_copy_init_for_output(struct window_pane
*wp
)
223 struct window_copy_mode_data
*data
= wp
->modedata
;
225 data
->backing
= xmalloc(sizeof *data
->backing
);
226 screen_init(data
->backing
, screen_size_x(&wp
->base
),
227 screen_size_y(&wp
->base
), UINT_MAX
);
228 data
->backing
->mode
&= ~MODE_WRAP
;
232 window_copy_free(struct window_pane
*wp
)
234 struct window_copy_mode_data
*data
= wp
->modedata
;
237 bufferevent_enable(wp
->event
, EV_READ
|EV_WRITE
);
239 if (data
->searchstr
!= NULL
)
240 xfree(data
->searchstr
);
241 xfree(data
->inputstr
);
243 if (data
->backing
!= &wp
->base
) {
244 screen_free(data
->backing
);
245 xfree(data
->backing
);
247 screen_free(&data
->screen
);
253 window_copy_add(struct window_pane
*wp
, const char *fmt
, ...)
258 window_copy_vadd(wp
, fmt
, ap
);
263 window_copy_vadd(struct window_pane
*wp
, const char *fmt
, va_list ap
)
265 struct window_copy_mode_data
*data
= wp
->modedata
;
266 struct screen
*backing
= data
->backing
;
267 struct screen_write_ctx back_ctx
, ctx
;
272 if (backing
== &wp
->base
)
275 utf8flag
= options_get_number(&wp
->window
->options
, "utf8");
276 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
278 old_hsize
= screen_hsize(data
->backing
);
279 screen_write_start(&back_ctx
, NULL
, backing
);
280 if (data
->backing_written
) {
282 * On the second or later line, do a CRLF before writing
283 * (so it's on a new line).
285 screen_write_carriagereturn(&back_ctx
);
286 screen_write_linefeed(&back_ctx
, 0);
288 data
->backing_written
= 1;
289 screen_write_vnputs(&back_ctx
, 0, &gc
, utf8flag
, fmt
, ap
);
290 screen_write_stop(&back_ctx
);
292 data
->oy
+= screen_hsize(data
->backing
) - old_hsize
;
294 screen_write_start(&ctx
, wp
, &data
->screen
);
297 * If the history has changed, draw the top line.
298 * (If there's any history at all, it has changed.)
300 if (screen_hsize(data
->backing
))
301 window_copy_redraw_lines(wp
, 0, 1);
303 /* Write the line, if it's visible. */
304 if (backing
->cy
+ data
->oy
< screen_size_y(backing
))
305 window_copy_redraw_lines(wp
, backing
->cy
, 1);
307 screen_write_stop(&ctx
);
311 window_copy_pageup(struct window_pane
*wp
)
313 struct window_copy_mode_data
*data
= wp
->modedata
;
314 struct screen
*s
= &data
->screen
;
318 if (screen_size_y(s
) > 2)
319 n
= screen_size_y(s
) - 2;
320 if (data
->oy
+ n
> screen_hsize(data
->backing
))
321 data
->oy
= screen_hsize(data
->backing
);
324 window_copy_update_selection(wp
);
325 window_copy_redraw_screen(wp
);
329 window_copy_resize(struct window_pane
*wp
, u_int sx
, u_int sy
)
331 struct window_copy_mode_data
*data
= wp
->modedata
;
332 struct screen
*s
= &data
->screen
;
333 struct screen_write_ctx ctx
;
335 screen_resize(s
, sx
, sy
);
336 if (data
->backing
!= &wp
->base
)
337 screen_resize(data
->backing
, sx
, sy
);
339 if (data
->cy
> sy
- 1)
343 if (data
->oy
> screen_hsize(data
->backing
))
344 data
->oy
= screen_hsize(data
->backing
);
346 window_copy_clear_selection(wp
);
348 screen_write_start(&ctx
, NULL
, s
);
349 window_copy_write_lines(wp
, &ctx
, 0, screen_size_y(s
) - 1);
350 screen_write_stop(&ctx
);
352 window_copy_redraw_screen(wp
);
356 window_copy_key(struct window_pane
*wp
, struct session
*sess
, int key
)
358 const char *word_separators
;
359 struct window_copy_mode_data
*data
= wp
->modedata
;
360 struct screen
*s
= &data
->screen
;
363 enum mode_key_cmd cmd
;
365 np
= data
->numprefix
;
369 if (data
->inputtype
== WINDOW_COPY_JUMPFORWARD
||
370 data
->inputtype
== WINDOW_COPY_JUMPBACK
) {
371 /* Ignore keys with modifiers. */
372 if ((key
& KEYC_MASK_MOD
) == 0) {
373 data
->jumpchar
= key
;
374 if (data
->inputtype
== WINDOW_COPY_JUMPFORWARD
) {
375 for (; np
!= 0; np
--)
376 window_copy_cursor_jump(wp
);
378 for (; np
!= 0; np
--)
379 window_copy_cursor_jump_back(wp
);
382 data
->jumptype
= data
->inputtype
;
383 data
->inputtype
= WINDOW_COPY_OFF
;
384 window_copy_redraw_lines(wp
, screen_size_y(s
) - 1, 1);
386 } else if (data
->inputtype
== WINDOW_COPY_NUMERICPREFIX
) {
387 if (window_copy_key_numeric_prefix(wp
, key
) == 0)
389 data
->inputtype
= WINDOW_COPY_OFF
;
390 window_copy_redraw_lines(wp
, screen_size_y(s
) - 1, 1);
391 } else if (data
->inputtype
!= WINDOW_COPY_OFF
) {
392 if (window_copy_key_input(wp
, key
) != 0)
397 cmd
= mode_key_lookup(&data
->mdata
, key
);
399 case MODEKEYCOPY_CANCEL
:
400 window_pane_reset_mode(wp
);
402 case MODEKEYCOPY_LEFT
:
403 for (; np
!= 0; np
--)
404 window_copy_cursor_left(wp
);
406 case MODEKEYCOPY_RIGHT
:
407 for (; np
!= 0; np
--)
408 window_copy_cursor_right(wp
);
411 for (; np
!= 0; np
--)
412 window_copy_cursor_up(wp
, 0);
414 case MODEKEYCOPY_DOWN
:
415 for (; np
!= 0; np
--)
416 window_copy_cursor_down(wp
, 0);
418 case MODEKEYCOPY_SCROLLUP
:
419 for (; np
!= 0; np
--)
420 window_copy_cursor_up(wp
, 1);
422 case MODEKEYCOPY_SCROLLDOWN
:
423 for (; np
!= 0; np
--)
424 window_copy_cursor_down(wp
, 1);
426 case MODEKEYCOPY_PREVIOUSPAGE
:
427 for (; np
!= 0; np
--)
428 window_copy_pageup(wp
);
430 case MODEKEYCOPY_NEXTPAGE
:
432 if (screen_size_y(s
) > 2)
433 n
= screen_size_y(s
) - 2;
434 for (; np
!= 0; np
--) {
440 window_copy_update_selection(wp
);
441 window_copy_redraw_screen(wp
);
443 case MODEKEYCOPY_HALFPAGEUP
:
444 n
= screen_size_y(s
) / 2;
445 for (; np
!= 0; np
--) {
446 if (data
->oy
+ n
> screen_hsize(data
->backing
))
447 data
->oy
= screen_hsize(data
->backing
);
451 window_copy_update_selection(wp
);
452 window_copy_redraw_screen(wp
);
454 case MODEKEYCOPY_HALFPAGEDOWN
:
455 n
= screen_size_y(s
) / 2;
456 for (; np
!= 0; np
--) {
462 window_copy_update_selection(wp
);
463 window_copy_redraw_screen(wp
);
465 case MODEKEYCOPY_TOPLINE
:
468 window_copy_update_selection(wp
);
469 window_copy_redraw_screen(wp
);
471 case MODEKEYCOPY_MIDDLELINE
:
473 data
->cy
= (screen_size_y(s
) - 1) / 2;
474 window_copy_update_selection(wp
);
475 window_copy_redraw_screen(wp
);
477 case MODEKEYCOPY_BOTTOMLINE
:
479 data
->cy
= screen_size_y(s
) - 1;
480 window_copy_update_selection(wp
);
481 window_copy_redraw_screen(wp
);
483 case MODEKEYCOPY_HISTORYTOP
:
486 data
->oy
= screen_hsize(data
->backing
);
487 window_copy_update_selection(wp
);
488 window_copy_redraw_screen(wp
);
490 case MODEKEYCOPY_HISTORYBOTTOM
:
492 data
->cy
= screen_size_y(s
) - 1;
494 window_copy_update_selection(wp
);
495 window_copy_redraw_screen(wp
);
497 case MODEKEYCOPY_STARTSELECTION
:
498 window_copy_start_selection(wp
);
499 window_copy_redraw_screen(wp
);
501 case MODEKEYCOPY_COPYLINE
:
502 case MODEKEYCOPY_SELECTLINE
:
503 window_copy_cursor_start_of_line(wp
);
505 case MODEKEYCOPY_COPYENDOFLINE
:
506 window_copy_start_selection(wp
);
508 window_copy_cursor_down(wp
, 0);
509 window_copy_cursor_end_of_line(wp
);
510 window_copy_redraw_screen(wp
);
512 /* If a copy command then copy the selection and exit. */
514 (cmd
== MODEKEYCOPY_COPYLINE
||
515 cmd
== MODEKEYCOPY_COPYENDOFLINE
)) {
516 window_copy_copy_selection(wp
);
517 window_pane_reset_mode(wp
);
521 case MODEKEYCOPY_CLEARSELECTION
:
522 window_copy_clear_selection(wp
);
523 window_copy_redraw_screen(wp
);
525 case MODEKEYCOPY_COPYSELECTION
:
527 window_copy_copy_selection(wp
);
528 window_pane_reset_mode(wp
);
532 case MODEKEYCOPY_STARTOFLINE
:
533 window_copy_cursor_start_of_line(wp
);
535 case MODEKEYCOPY_BACKTOINDENTATION
:
536 window_copy_cursor_back_to_indentation(wp
);
538 case MODEKEYCOPY_ENDOFLINE
:
539 window_copy_cursor_end_of_line(wp
);
541 case MODEKEYCOPY_NEXTSPACE
:
542 for (; np
!= 0; np
--)
543 window_copy_cursor_next_word(wp
, " ");
545 case MODEKEYCOPY_NEXTSPACEEND
:
546 for (; np
!= 0; np
--)
547 window_copy_cursor_next_word_end(wp
, " ");
549 case MODEKEYCOPY_NEXTWORD
:
551 options_get_string(&wp
->window
->options
, "word-separators");
552 for (; np
!= 0; np
--)
553 window_copy_cursor_next_word(wp
, word_separators
);
555 case MODEKEYCOPY_NEXTWORDEND
:
557 options_get_string(&wp
->window
->options
, "word-separators");
558 for (; np
!= 0; np
--)
559 window_copy_cursor_next_word_end(wp
, word_separators
);
561 case MODEKEYCOPY_PREVIOUSSPACE
:
562 for (; np
!= 0; np
--)
563 window_copy_cursor_previous_word(wp
, " ");
565 case MODEKEYCOPY_PREVIOUSWORD
:
567 options_get_string(&wp
->window
->options
, "word-separators");
568 for (; np
!= 0; np
--)
569 window_copy_cursor_previous_word(wp
, word_separators
);
571 case MODEKEYCOPY_JUMP
:
572 data
->inputtype
= WINDOW_COPY_JUMPFORWARD
;
573 data
->inputprompt
= "Jump Forward";
574 *data
->inputstr
= '\0';
575 window_copy_redraw_lines(wp
, screen_size_y(s
) - 1, 1);
576 return; /* skip numprefix reset */
577 case MODEKEYCOPY_JUMPAGAIN
:
578 if (data
->jumptype
== WINDOW_COPY_JUMPFORWARD
) {
579 for (; np
!= 0; np
--)
580 window_copy_cursor_jump(wp
);
581 } else if (data
->jumptype
== WINDOW_COPY_JUMPBACK
) {
582 for (; np
!= 0; np
--)
583 window_copy_cursor_jump_back(wp
);
586 case MODEKEYCOPY_JUMPREVERSE
:
587 if (data
->jumptype
== WINDOW_COPY_JUMPFORWARD
) {
588 for (; np
!= 0; np
--)
589 window_copy_cursor_jump_back(wp
);
590 } else if (data
->jumptype
== WINDOW_COPY_JUMPBACK
) {
591 for (; np
!= 0; np
--)
592 window_copy_cursor_jump(wp
);
595 case MODEKEYCOPY_JUMPBACK
:
596 data
->inputtype
= WINDOW_COPY_JUMPBACK
;
597 data
->inputprompt
= "Jump Back";
598 *data
->inputstr
= '\0';
599 window_copy_redraw_lines(wp
, screen_size_y(s
) - 1, 1);
600 return; /* skip numprefix reset */
601 case MODEKEYCOPY_SEARCHUP
:
602 data
->inputtype
= WINDOW_COPY_SEARCHUP
;
603 data
->inputprompt
= "Search Up";
605 case MODEKEYCOPY_SEARCHDOWN
:
606 data
->inputtype
= WINDOW_COPY_SEARCHDOWN
;
607 data
->inputprompt
= "Search Down";
609 case MODEKEYCOPY_SEARCHAGAIN
:
610 case MODEKEYCOPY_SEARCHREVERSE
:
611 switch (data
->searchtype
) {
612 case WINDOW_COPY_OFF
:
613 case WINDOW_COPY_GOTOLINE
:
614 case WINDOW_COPY_JUMPFORWARD
:
615 case WINDOW_COPY_JUMPBACK
:
616 case WINDOW_COPY_NUMERICPREFIX
:
618 case WINDOW_COPY_SEARCHUP
:
619 if (cmd
== MODEKEYCOPY_SEARCHAGAIN
) {
620 for (; np
!= 0; np
--) {
621 window_copy_search_up(
622 wp
, data
->searchstr
);
625 for (; np
!= 0; np
--) {
626 window_copy_search_down(
627 wp
, data
->searchstr
);
631 case WINDOW_COPY_SEARCHDOWN
:
632 if (cmd
== MODEKEYCOPY_SEARCHAGAIN
) {
633 for (; np
!= 0; np
--) {
634 window_copy_search_down(
635 wp
, data
->searchstr
);
638 for (; np
!= 0; np
--) {
639 window_copy_search_up(
640 wp
, data
->searchstr
);
646 case MODEKEYCOPY_GOTOLINE
:
647 data
->inputtype
= WINDOW_COPY_GOTOLINE
;
648 data
->inputprompt
= "Goto Line";
649 *data
->inputstr
= '\0';
651 case MODEKEYCOPY_STARTNUMBERPREFIX
:
652 key
&= KEYC_MASK_KEY
;
653 if (key
>= '0' && key
<= '9') {
654 data
->inputtype
= WINDOW_COPY_NUMERICPREFIX
;
656 window_copy_key_numeric_prefix(wp
, key
);
660 case MODEKEYCOPY_RECTANGLETOGGLE
:
661 window_copy_rectangle_toggle(wp
);
671 keys
= options_get_number(&wp
->window
->options
, "mode-keys");
672 if (keys
== MODEKEY_EMACS
)
673 mode_key_init(&data
->mdata
, &mode_key_tree_emacs_edit
);
675 mode_key_init(&data
->mdata
, &mode_key_tree_vi_edit
);
677 window_copy_redraw_lines(wp
, screen_size_y(s
) - 1, 1);
681 keys
= options_get_number(&wp
->window
->options
, "mode-keys");
682 if (keys
== MODEKEY_EMACS
)
683 mode_key_init(&data
->mdata
, &mode_key_tree_emacs_copy
);
685 mode_key_init(&data
->mdata
, &mode_key_tree_vi_copy
);
687 data
->inputtype
= WINDOW_COPY_OFF
;
688 data
->inputprompt
= NULL
;
690 window_copy_redraw_lines(wp
, screen_size_y(s
) - 1, 1);
694 window_copy_key_input(struct window_pane
*wp
, int key
)
696 struct window_copy_mode_data
*data
= wp
->modedata
;
697 struct screen
*s
= &data
->screen
;
701 switch (mode_key_lookup(&data
->mdata
, key
)) {
702 case MODEKEYEDIT_CANCEL
:
705 case MODEKEYEDIT_BACKSPACE
:
706 inputlen
= strlen(data
->inputstr
);
708 data
->inputstr
[inputlen
- 1] = '\0';
710 case MODEKEYEDIT_DELETELINE
:
711 *data
->inputstr
= '\0';
713 case MODEKEYEDIT_ENTER
:
714 np
= data
->numprefix
;
718 switch (data
->inputtype
) {
719 case WINDOW_COPY_OFF
:
720 case WINDOW_COPY_JUMPFORWARD
:
721 case WINDOW_COPY_JUMPBACK
:
722 case WINDOW_COPY_NUMERICPREFIX
:
724 case WINDOW_COPY_SEARCHUP
:
725 for (; np
!= 0; np
--)
726 window_copy_search_up(wp
, data
->inputstr
);
727 data
->searchtype
= data
->inputtype
;
728 data
->searchstr
= xstrdup(data
->inputstr
);
730 case WINDOW_COPY_SEARCHDOWN
:
731 for (; np
!= 0; np
--)
732 window_copy_search_down(wp
, data
->inputstr
);
733 data
->searchtype
= data
->inputtype
;
734 data
->searchstr
= xstrdup(data
->inputstr
);
736 case WINDOW_COPY_GOTOLINE
:
737 window_copy_goto_line(wp
, data
->inputstr
);
738 *data
->inputstr
= '\0';
744 if (key
< 32 || key
> 126)
746 inputlen
= strlen(data
->inputstr
) + 2;
748 data
->inputstr
= xrealloc(data
->inputstr
, 1, inputlen
);
749 data
->inputstr
[inputlen
- 2] = key
;
750 data
->inputstr
[inputlen
- 1] = '\0';
756 window_copy_redraw_lines(wp
, screen_size_y(s
) - 1, 1);
761 window_copy_key_numeric_prefix(struct window_pane
*wp
, int key
)
763 struct window_copy_mode_data
*data
= wp
->modedata
;
764 struct screen
*s
= &data
->screen
;
766 key
&= KEYC_MASK_KEY
;
767 if (key
< '0' || key
> '9')
770 if (data
->numprefix
>= 100) /* no more than three digits */
772 data
->numprefix
= data
->numprefix
* 10 + key
- '0';
774 window_copy_redraw_lines(wp
, screen_size_y(s
) - 1, 1);
781 struct window_pane
*wp
, struct session
*sess
, struct mouse_event
*m
)
783 struct window_copy_mode_data
*data
= wp
->modedata
;
784 struct screen
*s
= &data
->screen
;
787 if (m
->x
>= screen_size_x(s
))
789 if (m
->y
>= screen_size_y(s
))
792 /* If mouse wheel (buttons 4 and 5), scroll. */
793 if ((m
->b
& MOUSE_45
)) {
794 if ((m
->b
& MOUSE_BUTTON
) == MOUSE_1
) {
795 for (i
= 0; i
< 5; i
++)
796 window_copy_cursor_up(wp
, 0);
797 } else if ((m
->b
& MOUSE_BUTTON
) == MOUSE_2
) {
799 for (i
= 0; i
< 5; i
++)
800 window_copy_cursor_down(wp
, 0);
801 if (old_cy
== data
->cy
)
808 * If already reading motion, move the cursor while buttons are still
809 * pressed, or stop the selection on their release.
811 if (s
->mode
& MODE_MOUSE_BUTTON
) {
812 if ((m
->b
& MOUSE_BUTTON
) != MOUSE_UP
) {
813 window_copy_update_cursor(wp
, m
->x
, m
->y
);
814 if (window_copy_update_selection(wp
))
815 window_copy_redraw_screen(wp
);
821 /* Otherwise if other buttons pressed, start selection and motion. */
822 if ((m
->b
& MOUSE_BUTTON
) != MOUSE_UP
) {
823 s
->mode
&= ~MODE_MOUSE_STANDARD
;
824 s
->mode
|= MODE_MOUSE_BUTTON
;
826 window_copy_update_cursor(wp
, m
->x
, m
->y
);
827 window_copy_start_selection(wp
);
828 window_copy_redraw_screen(wp
);
834 s
->mode
&= ~MODE_MOUSE_BUTTON
;
835 s
->mode
|= MODE_MOUSE_STANDARD
;
837 window_copy_copy_selection(wp
);
838 window_pane_reset_mode(wp
);
843 window_copy_scroll_to(struct window_pane
*wp
, u_int px
, u_int py
)
845 struct window_copy_mode_data
*data
= wp
->modedata
;
846 struct grid
*gd
= data
->backing
->grid
;
855 } else if (py
> gd
->hsize
+ gd
->sy
- gap
) {
857 data
->cy
= py
- gd
->hsize
;
859 offset
= py
+ gap
- gd
->sy
;
860 data
->cy
= py
- offset
;
862 data
->oy
= gd
->hsize
- offset
;
864 window_copy_update_selection(wp
);
865 window_copy_redraw_screen(wp
);
869 window_copy_search_compare(
870 struct grid
*gd
, u_int px
, u_int py
, struct grid
*sgd
, u_int spx
)
872 const struct grid_cell
*gc
, *sgc
;
873 const struct grid_utf8
*gu
, *sgu
;
875 gc
= grid_peek_cell(gd
, px
, py
);
876 sgc
= grid_peek_cell(sgd
, spx
, 0);
878 if ((gc
->flags
& GRID_FLAG_UTF8
) != (sgc
->flags
& GRID_FLAG_UTF8
))
881 if (gc
->flags
& GRID_FLAG_UTF8
) {
882 gu
= grid_peek_utf8(gd
, px
, py
);
883 sgu
= grid_peek_utf8(sgd
, spx
, 0);
884 if (grid_utf8_compare(gu
, sgu
))
887 if (gc
->data
== sgc
->data
)
894 window_copy_search_lr(struct grid
*gd
,
895 struct grid
*sgd
, u_int
*ppx
, u_int py
, u_int first
, u_int last
)
899 for (ax
= first
; ax
< last
; ax
++) {
900 if (ax
+ sgd
->sx
>= gd
->sx
)
902 for (bx
= 0; bx
< sgd
->sx
; bx
++) {
904 if (!window_copy_search_compare(gd
, px
, py
, sgd
, bx
))
916 window_copy_search_rl(struct grid
*gd
,
917 struct grid
*sgd
, u_int
*ppx
, u_int py
, u_int first
, u_int last
)
921 for (ax
= last
+ 1; ax
> first
; ax
--) {
922 if (gd
->sx
- (ax
- 1) < sgd
->sx
)
924 for (bx
= 0; bx
< sgd
->sx
; bx
++) {
926 if (!window_copy_search_compare(gd
, px
, py
, sgd
, bx
))
938 window_copy_search_up(struct window_pane
*wp
, const char *searchstr
)
940 struct window_copy_mode_data
*data
= wp
->modedata
;
941 struct screen
*s
= data
->backing
, ss
;
942 struct screen_write_ctx ctx
;
943 struct grid
*gd
= s
->grid
, *sgd
;
946 u_int i
, last
, fx
, fy
, px
;
947 int utf8flag
, n
, wrapped
;
949 if (*searchstr
== '\0')
951 utf8flag
= options_get_number(&wp
->window
->options
, "utf8");
952 searchlen
= screen_write_strlen(utf8flag
, "%s", searchstr
);
954 screen_init(&ss
, searchlen
, 1, 0);
955 screen_write_start(&ctx
, NULL
, &ss
);
956 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
957 screen_write_nputs(&ctx
, -1, &gc
, utf8flag
, "%s", searchstr
);
958 screen_write_stop(&ctx
);
961 fy
= gd
->hsize
- data
->oy
+ data
->cy
;
974 for (i
= fy
+ 1; i
> 0; i
--) {
975 last
= screen_size_x(s
);
978 n
= window_copy_search_rl(gd
, sgd
, &px
, i
- 1, 0, last
);
980 window_copy_scroll_to(wp
, px
, i
- 1);
984 if (!n
&& !wrapped
) {
986 fy
= gd
->hsize
+ gd
->sy
- 1;
995 window_copy_search_down(struct window_pane
*wp
, const char *searchstr
)
997 struct window_copy_mode_data
*data
= wp
->modedata
;
998 struct screen
*s
= data
->backing
, ss
;
999 struct screen_write_ctx ctx
;
1000 struct grid
*gd
= s
->grid
, *sgd
;
1001 struct grid_cell gc
;
1003 u_int i
, first
, fx
, fy
, px
;
1004 int utf8flag
, n
, wrapped
;
1006 if (*searchstr
== '\0')
1008 utf8flag
= options_get_number(&wp
->window
->options
, "utf8");
1009 searchlen
= screen_write_strlen(utf8flag
, "%s", searchstr
);
1011 screen_init(&ss
, searchlen
, 1, 0);
1012 screen_write_start(&ctx
, NULL
, &ss
);
1013 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
1014 screen_write_nputs(&ctx
, -1, &gc
, utf8flag
, "%s", searchstr
);
1015 screen_write_stop(&ctx
);
1018 fy
= gd
->hsize
- data
->oy
+ data
->cy
;
1020 if (fx
== gd
->sx
- 1) {
1021 if (fy
== gd
->hsize
+ gd
->sy
)
1031 for (i
= fy
+ 1; i
< gd
->hsize
+ gd
->sy
; i
++) {
1035 n
= window_copy_search_lr(gd
, sgd
, &px
, i
- 1, first
, gd
->sx
);
1037 window_copy_scroll_to(wp
, px
, i
- 1);
1041 if (!n
&& !wrapped
) {
1052 window_copy_goto_line(struct window_pane
*wp
, const char *linestr
)
1054 struct window_copy_mode_data
*data
= wp
->modedata
;
1058 lineno
= strtonum(linestr
, 0, screen_hsize(data
->backing
), &errstr
);
1063 window_copy_update_selection(wp
);
1064 window_copy_redraw_screen(wp
);
1068 window_copy_write_line(
1069 struct window_pane
*wp
, struct screen_write_ctx
*ctx
, u_int py
)
1071 struct window_copy_mode_data
*data
= wp
->modedata
;
1072 struct screen
*s
= &data
->screen
;
1073 struct options
*oo
= &wp
->window
->options
;
1074 struct grid_cell gc
;
1076 size_t last
, xoff
= 0, size
= 0;
1078 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
1079 colour_set_fg(&gc
, options_get_number(oo
, "mode-fg"));
1080 colour_set_bg(&gc
, options_get_number(oo
, "mode-bg"));
1081 gc
.attr
|= options_get_number(oo
, "mode-attr");
1083 last
= screen_size_y(s
) - 1;
1085 size
= xsnprintf(hdr
, sizeof hdr
,
1086 "[%u/%u]", data
->oy
, screen_hsize(data
->backing
));
1087 if (size
> screen_size_x(s
))
1088 size
= screen_size_x(s
);
1089 screen_write_cursormove(ctx
, screen_size_x(s
) - size
, 0);
1090 screen_write_puts(ctx
, &gc
, "%s", hdr
);
1091 } else if (py
== last
&& data
->inputtype
!= WINDOW_COPY_OFF
) {
1092 if (data
->inputtype
== WINDOW_COPY_NUMERICPREFIX
) {
1093 xoff
= size
= xsnprintf(hdr
, sizeof hdr
,
1094 "Repeat: %u", data
->numprefix
);
1096 xoff
= size
= xsnprintf(hdr
, sizeof hdr
,
1097 "%s: %s", data
->inputprompt
, data
->inputstr
);
1099 screen_write_cursormove(ctx
, 0, last
);
1100 screen_write_puts(ctx
, &gc
, "%s", hdr
);
1104 screen_write_cursormove(ctx
, xoff
, py
);
1105 screen_write_copy(ctx
, data
->backing
, xoff
,
1106 (screen_hsize(data
->backing
) - data
->oy
) + py
,
1107 screen_size_x(s
) - size
, 1);
1109 if (py
== data
->cy
&& data
->cx
== screen_size_x(s
)) {
1110 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
1111 screen_write_cursormove(ctx
, screen_size_x(s
) - 1, py
);
1112 screen_write_putc(ctx
, &gc
, '$');
1117 window_copy_write_lines(
1118 struct window_pane
*wp
, struct screen_write_ctx
*ctx
, u_int py
, u_int ny
)
1122 for (yy
= py
; yy
< py
+ ny
; yy
++)
1123 window_copy_write_line(wp
, ctx
, py
);
1127 window_copy_redraw_lines(struct window_pane
*wp
, u_int py
, u_int ny
)
1129 struct window_copy_mode_data
*data
= wp
->modedata
;
1130 struct screen_write_ctx ctx
;
1133 screen_write_start(&ctx
, wp
, NULL
);
1134 for (i
= py
; i
< py
+ ny
; i
++)
1135 window_copy_write_line(wp
, &ctx
, i
);
1136 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
);
1137 screen_write_stop(&ctx
);
1141 window_copy_redraw_screen(struct window_pane
*wp
)
1143 struct window_copy_mode_data
*data
= wp
->modedata
;
1145 window_copy_redraw_lines(wp
, 0, screen_size_y(&data
->screen
));
1149 window_copy_update_cursor(struct window_pane
*wp
, u_int cx
, u_int cy
)
1151 struct window_copy_mode_data
*data
= wp
->modedata
;
1152 struct screen
*s
= &data
->screen
;
1153 struct screen_write_ctx ctx
;
1154 u_int old_cx
, old_cy
;
1156 old_cx
= data
->cx
; old_cy
= data
->cy
;
1157 data
->cx
= cx
; data
->cy
= cy
;
1158 if (old_cx
== screen_size_x(s
))
1159 window_copy_redraw_lines(wp
, old_cy
, 1);
1160 if (data
->cx
== screen_size_x(s
))
1161 window_copy_redraw_lines(wp
, data
->cy
, 1);
1163 screen_write_start(&ctx
, wp
, NULL
);
1164 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
);
1165 screen_write_stop(&ctx
);
1170 window_copy_start_selection(struct window_pane
*wp
)
1172 struct window_copy_mode_data
*data
= wp
->modedata
;
1173 struct screen
*s
= &data
->screen
;
1175 data
->selx
= data
->cx
;
1176 data
->sely
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1179 window_copy_update_selection(wp
);
1183 window_copy_update_selection(struct window_pane
*wp
)
1185 struct window_copy_mode_data
*data
= wp
->modedata
;
1186 struct screen
*s
= &data
->screen
;
1187 struct options
*oo
= &wp
->window
->options
;
1188 struct grid_cell gc
;
1189 u_int sx
, sy
, ty
, cy
;
1195 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
1196 colour_set_fg(&gc
, options_get_number(oo
, "mode-fg"));
1197 colour_set_bg(&gc
, options_get_number(oo
, "mode-bg"));
1198 gc
.attr
|= options_get_number(oo
, "mode-attr");
1200 /* Find top of screen. */
1201 ty
= screen_hsize(data
->backing
) - data
->oy
;
1203 /* Adjust the selection. */
1206 if (sy
< ty
) { /* above screen */
1207 if (!data
->rectflag
)
1210 } else if (sy
> ty
+ screen_size_y(s
) - 1) { /* below screen */
1211 if (!data
->rectflag
)
1212 sx
= screen_size_x(s
) - 1;
1213 sy
= screen_size_y(s
) - 1;
1216 sy
= screen_hsize(s
) + sy
;
1218 screen_set_selection(s
,
1219 sx
, sy
, data
->cx
, screen_hsize(s
) + data
->cy
, data
->rectflag
, &gc
);
1221 if (data
->rectflag
) {
1223 * Can't rely on the caller to redraw the right lines for
1224 * rectangle selection - find the highest line and the number
1225 * of lines, and redraw just past that in both directions
1229 window_copy_redraw_lines(wp
, sy
, cy
- sy
+ 1);
1231 window_copy_redraw_lines(wp
, cy
, sy
- cy
+ 1);
1238 window_copy_copy_selection(struct window_pane
*wp
)
1240 struct window_copy_mode_data
*data
= wp
->modedata
;
1241 struct screen
*s
= &data
->screen
;
1244 u_int i
, xx
, yy
, sx
, sy
, ex
, ey
, limit
;
1245 u_int firstsx
, lastex
, restex
, restsx
;
1257 * The selection extends from selx,sely to (adjusted) cx,cy on
1261 /* Find start and end. */
1263 yy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1264 if (yy
< data
->sely
|| (yy
== data
->sely
&& xx
< data
->selx
)) {
1266 ex
= data
->selx
; ey
= data
->sely
;
1268 sx
= data
->selx
; sy
= data
->sely
;
1272 /* Trim ex to end of line. */
1273 xx
= window_copy_find_length(wp
, ey
);
1278 * Deal with rectangle-copy if necessary; four situations: start of
1279 * first line (firstsx), end of last line (lastex), start (restsx) and
1280 * end (restex) of all other lines.
1282 xx
= screen_size_x(s
);
1285 * Behave according to mode-keys. If it is emacs, copy like emacs,
1286 * keeping the top-left-most character, and dropping the
1287 * bottom-right-most, regardless of copy direction. If it is vi, also
1288 * keep bottom-right-most character.
1290 keys
= options_get_number(&wp
->window
->options
, "mode-keys");
1291 if (data
->rectflag
) {
1293 * Need to ignore the column with the cursor in it, which for
1294 * rectangular copy means knowing which side the cursor is on.
1296 if (data
->selx
< data
->cx
) {
1297 /* Selection start is on the left. */
1298 if (keys
== MODEKEY_EMACS
) {
1303 lastex
= data
->cx
+ 1;
1304 restex
= data
->cx
+ 1;
1306 firstsx
= data
->selx
;
1307 restsx
= data
->selx
;
1309 /* Cursor is on the left. */
1310 lastex
= data
->selx
+ 1;
1311 restex
= data
->selx
+ 1;
1316 if (keys
== MODEKEY_EMACS
)
1325 /* Copy the lines. */
1327 window_copy_copy_line(wp
, &buf
, &off
, sy
, firstsx
, lastex
);
1329 window_copy_copy_line(wp
, &buf
, &off
, sy
, firstsx
, restex
);
1331 for (i
= sy
+ 1; i
< ey
; i
++) {
1332 window_copy_copy_line(
1333 wp
, &buf
, &off
, i
, restsx
, restex
);
1336 window_copy_copy_line(wp
, &buf
, &off
, ey
, restsx
, lastex
);
1339 /* Don't bother if no data. */
1344 off
--; /* remove final \n */
1346 if (options_get_number(&global_options
, "set-clipboard"))
1347 screen_write_setselection(&wp
->ictx
.ctx
, buf
, off
);
1349 /* Add the buffer to the stack. */
1350 limit
= options_get_number(&global_options
, "buffer-limit");
1351 paste_add(&global_buffers
, buf
, off
, limit
);
1355 window_copy_copy_line(struct window_pane
*wp
,
1356 char **buf
, size_t *off
, u_int sy
, u_int sx
, u_int ex
)
1358 struct window_copy_mode_data
*data
= wp
->modedata
;
1359 struct grid
*gd
= data
->backing
->grid
;
1360 const struct grid_cell
*gc
;
1361 const struct grid_utf8
*gu
;
1362 struct grid_line
*gl
;
1363 u_int i
, xx
, wrapped
= 0;
1370 * Work out if the line was wrapped at the screen edge and all of it is
1373 gl
= &gd
->linedata
[sy
];
1374 if (gl
->flags
& GRID_LINE_WRAPPED
&& gl
->cellsize
<= gd
->sx
)
1377 /* If the line was wrapped, don't strip spaces (use the full length). */
1381 xx
= window_copy_find_length(wp
, sy
);
1388 for (i
= sx
; i
< ex
; i
++) {
1389 gc
= grid_peek_cell(gd
, i
, sy
);
1390 if (gc
->flags
& GRID_FLAG_PADDING
)
1392 if (!(gc
->flags
& GRID_FLAG_UTF8
)) {
1393 *buf
= xrealloc(*buf
, 1, (*off
) + 1);
1394 (*buf
)[(*off
)++] = gc
->data
;
1396 gu
= grid_peek_utf8(gd
, i
, sy
);
1397 size
= grid_utf8_size(gu
);
1398 *buf
= xrealloc(*buf
, 1, (*off
) + size
);
1399 *off
+= grid_utf8_copy(gu
, *buf
+ *off
, size
);
1404 /* Only add a newline if the line wasn't wrapped. */
1405 if (!wrapped
|| ex
!= xx
) {
1406 *buf
= xrealloc(*buf
, 1, (*off
) + 1);
1407 (*buf
)[(*off
)++] = '\n';
1412 window_copy_clear_selection(struct window_pane
*wp
)
1414 struct window_copy_mode_data
*data
= wp
->modedata
;
1417 screen_clear_selection(&data
->screen
);
1419 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1420 px
= window_copy_find_length(wp
, py
);
1422 window_copy_update_cursor(wp
, px
, data
->cy
);
1426 window_copy_in_set(struct window_pane
*wp
, u_int px
, u_int py
, const char *set
)
1428 struct window_copy_mode_data
*data
= wp
->modedata
;
1429 const struct grid_cell
*gc
;
1431 gc
= grid_peek_cell(data
->backing
->grid
, px
, py
);
1432 if (gc
->flags
& (GRID_FLAG_PADDING
|GRID_FLAG_UTF8
))
1434 if (gc
->data
== 0x00 || gc
->data
== 0x7f)
1436 return (strchr(set
, gc
->data
) != NULL
);
1440 window_copy_find_length(struct window_pane
*wp
, u_int py
)
1442 struct window_copy_mode_data
*data
= wp
->modedata
;
1443 struct screen
*s
= data
->backing
;
1444 const struct grid_cell
*gc
;
1448 * If the pane has been resized, its grid can contain old overlong
1449 * lines. grid_peek_cell does not allow accessing cells beyond the
1450 * width of the grid, and screen_write_copy treats them as spaces, so
1451 * ignore them here too.
1453 px
= s
->grid
->linedata
[py
].cellsize
;
1454 if (px
> screen_size_x(s
))
1455 px
= screen_size_x(s
);
1457 gc
= grid_peek_cell(s
->grid
, px
- 1, py
);
1458 if (gc
->flags
& GRID_FLAG_UTF8
)
1460 if (gc
->data
!= ' ')
1468 window_copy_cursor_start_of_line(struct window_pane
*wp
)
1470 struct window_copy_mode_data
*data
= wp
->modedata
;
1471 struct screen
*back_s
= data
->backing
;
1472 struct grid
*gd
= back_s
->grid
;
1475 if (data
->cx
== 0) {
1476 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1477 while (py
> 0 && gd
->linedata
[py
-1].flags
& GRID_LINE_WRAPPED
) {
1478 window_copy_cursor_up(wp
, 0);
1479 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1482 window_copy_update_cursor(wp
, 0, data
->cy
);
1483 if (window_copy_update_selection(wp
))
1484 window_copy_redraw_lines(wp
, data
->cy
, 1);
1488 window_copy_cursor_back_to_indentation(struct window_pane
*wp
)
1490 struct window_copy_mode_data
*data
= wp
->modedata
;
1492 const struct grid_cell
*gc
;
1495 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1496 xx
= window_copy_find_length(wp
, py
);
1499 gc
= grid_peek_cell(data
->backing
->grid
, px
, py
);
1500 if (gc
->flags
& GRID_FLAG_UTF8
)
1502 if (gc
->data
!= ' ')
1507 window_copy_update_cursor(wp
, px
, data
->cy
);
1508 if (window_copy_update_selection(wp
))
1509 window_copy_redraw_lines(wp
, data
->cy
, 1);
1513 window_copy_cursor_end_of_line(struct window_pane
*wp
)
1515 struct window_copy_mode_data
*data
= wp
->modedata
;
1516 struct screen
*back_s
= data
->backing
;
1517 struct grid
*gd
= back_s
->grid
;
1520 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1521 px
= window_copy_find_length(wp
, py
);
1523 if (data
->cx
== px
) {
1524 if (data
->screen
.sel
.flag
&& data
->rectflag
)
1525 px
= screen_size_x(back_s
);
1526 if (gd
->linedata
[py
].flags
& GRID_LINE_WRAPPED
) {
1527 while (py
< gd
->sy
+ gd
->hsize
&&
1528 gd
->linedata
[py
].flags
& GRID_LINE_WRAPPED
) {
1529 window_copy_cursor_down(wp
, 0);
1530 py
= screen_hsize(back_s
)
1531 + data
->cy
- data
->oy
;
1533 px
= window_copy_find_length(wp
, py
);
1536 window_copy_update_cursor(wp
, px
, data
->cy
);
1538 if (window_copy_update_selection(wp
))
1539 window_copy_redraw_lines(wp
, data
->cy
, 1);
1543 window_copy_cursor_left(struct window_pane
*wp
)
1545 struct window_copy_mode_data
*data
= wp
->modedata
;
1547 if (data
->cx
== 0) {
1548 window_copy_cursor_up(wp
, 0);
1549 window_copy_cursor_end_of_line(wp
);
1551 window_copy_update_cursor(wp
, data
->cx
- 1, data
->cy
);
1552 if (window_copy_update_selection(wp
))
1553 window_copy_redraw_lines(wp
, data
->cy
, 1);
1558 window_copy_cursor_right(struct window_pane
*wp
)
1560 struct window_copy_mode_data
*data
= wp
->modedata
;
1563 if (data
->screen
.sel
.flag
&& data
->rectflag
)
1564 px
= screen_size_x(&data
->screen
);
1566 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1567 px
= window_copy_find_length(wp
, py
);
1570 if (data
->cx
>= px
) {
1571 window_copy_cursor_start_of_line(wp
);
1572 window_copy_cursor_down(wp
, 0);
1574 window_copy_update_cursor(wp
, data
->cx
+ 1, data
->cy
);
1575 if (window_copy_update_selection(wp
))
1576 window_copy_redraw_lines(wp
, data
->cy
, 1);
1581 window_copy_cursor_up(struct window_pane
*wp
, int scroll_only
)
1583 struct window_copy_mode_data
*data
= wp
->modedata
;
1584 struct screen
*s
= &data
->screen
;
1585 u_int ox
, oy
, px
, py
;
1587 oy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1588 ox
= window_copy_find_length(wp
, oy
);
1590 data
->lastcx
= data
->cx
;
1594 data
->cx
= data
->lastcx
;
1595 if (scroll_only
|| data
->cy
== 0) {
1596 window_copy_scroll_down(wp
, 1);
1598 if (data
->cy
== screen_size_y(s
) - 1)
1599 window_copy_redraw_lines(wp
, data
->cy
, 1);
1601 window_copy_redraw_lines(wp
, data
->cy
, 2);
1604 window_copy_update_cursor(wp
, data
->cx
, data
->cy
- 1);
1605 if (window_copy_update_selection(wp
)) {
1606 if (data
->cy
== screen_size_y(s
) - 1)
1607 window_copy_redraw_lines(wp
, data
->cy
, 1);
1609 window_copy_redraw_lines(wp
, data
->cy
, 2);
1613 if (!data
->screen
.sel
.flag
|| !data
->rectflag
) {
1614 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1615 px
= window_copy_find_length(wp
, py
);
1616 if ((data
->cx
>= data
->lastsx
&& data
->cx
!= px
) ||
1618 window_copy_cursor_end_of_line(wp
);
1623 window_copy_cursor_down(struct window_pane
*wp
, int scroll_only
)
1625 struct window_copy_mode_data
*data
= wp
->modedata
;
1626 struct screen
*s
= &data
->screen
;
1627 u_int ox
, oy
, px
, py
;
1629 oy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1630 ox
= window_copy_find_length(wp
, oy
);
1632 data
->lastcx
= data
->cx
;
1636 data
->cx
= data
->lastcx
;
1637 if (scroll_only
|| data
->cy
== screen_size_y(s
) - 1) {
1638 window_copy_scroll_up(wp
, 1);
1639 if (scroll_only
&& data
->cy
> 0)
1640 window_copy_redraw_lines(wp
, data
->cy
- 1, 2);
1642 window_copy_update_cursor(wp
, data
->cx
, data
->cy
+ 1);
1643 if (window_copy_update_selection(wp
))
1644 window_copy_redraw_lines(wp
, data
->cy
- 1, 2);
1647 if (!data
->screen
.sel
.flag
|| !data
->rectflag
) {
1648 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1649 px
= window_copy_find_length(wp
, py
);
1650 if ((data
->cx
>= data
->lastsx
&& data
->cx
!= px
) ||
1652 window_copy_cursor_end_of_line(wp
);
1657 window_copy_cursor_jump(struct window_pane
*wp
)
1659 struct window_copy_mode_data
*data
= wp
->modedata
;
1660 struct screen
*back_s
= data
->backing
;
1661 const struct grid_cell
*gc
;
1665 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1666 xx
= window_copy_find_length(wp
, py
);
1669 gc
= grid_peek_cell(back_s
->grid
, px
, py
);
1670 if ((gc
->flags
& (GRID_FLAG_PADDING
|GRID_FLAG_UTF8
)) == 0
1671 && gc
->data
== data
->jumpchar
) {
1673 window_copy_update_cursor(wp
, px
, data
->cy
);
1674 if (window_copy_update_selection(wp
))
1675 window_copy_redraw_lines(wp
, data
->cy
, 1);
1683 window_copy_cursor_jump_back(struct window_pane
*wp
)
1685 struct window_copy_mode_data
*data
= wp
->modedata
;
1686 struct screen
*back_s
= data
->backing
;
1687 const struct grid_cell
*gc
;
1691 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1697 gc
= grid_peek_cell(back_s
->grid
, px
, py
);
1698 if ((gc
->flags
& (GRID_FLAG_PADDING
|GRID_FLAG_UTF8
)) == 0
1699 && gc
->data
== data
->jumpchar
) {
1701 window_copy_update_cursor(wp
, px
, data
->cy
);
1702 if (window_copy_update_selection(wp
))
1703 window_copy_redraw_lines(wp
, data
->cy
, 1);
1713 window_copy_cursor_next_word(struct window_pane
*wp
, const char *separators
)
1715 struct window_copy_mode_data
*data
= wp
->modedata
;
1716 struct screen
*back_s
= data
->backing
;
1717 u_int px
, py
, xx
, yy
;
1721 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1722 xx
= window_copy_find_length(wp
, py
);
1723 yy
= screen_hsize(back_s
) + screen_size_y(back_s
) - 1;
1726 * First skip past any nonword characters and then any word characters.
1728 * expected is initially set to 0 for the former and then 1 for the
1733 window_copy_in_set(wp
, px
, py
, separators
) == expected
) {
1734 /* Move down if we're past the end of the line. */
1738 window_copy_cursor_down(wp
, 0);
1741 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1742 xx
= window_copy_find_length(wp
, py
);
1746 expected
= !expected
;
1747 } while (expected
== 1);
1749 window_copy_update_cursor(wp
, px
, data
->cy
);
1750 if (window_copy_update_selection(wp
))
1751 window_copy_redraw_lines(wp
, data
->cy
, 1);
1755 window_copy_cursor_next_word_end(struct window_pane
*wp
, const char *separators
)
1757 struct window_copy_mode_data
*data
= wp
->modedata
;
1758 struct screen
*back_s
= data
->backing
;
1759 u_int px
, py
, xx
, yy
;
1763 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1764 xx
= window_copy_find_length(wp
, py
);
1765 yy
= screen_hsize(back_s
) + screen_size_y(back_s
) - 1;
1768 * First skip past any word characters, then any nonword characters.
1770 * expected is initially set to 1 for the former and then 0 for the
1775 window_copy_in_set(wp
, px
, py
, separators
) == expected
) {
1776 /* Move down if we're past the end of the line. */
1780 window_copy_cursor_down(wp
, 0);
1783 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1784 xx
= window_copy_find_length(wp
, py
);
1788 expected
= !expected
;
1789 } while (expected
== 0);
1791 window_copy_update_cursor(wp
, px
, data
->cy
);
1792 if (window_copy_update_selection(wp
))
1793 window_copy_redraw_lines(wp
, data
->cy
, 1);
1796 /* Move to the previous place where a word begins. */
1798 window_copy_cursor_previous_word(struct window_pane
*wp
, const char *separators
)
1800 struct window_copy_mode_data
*data
= wp
->modedata
;
1804 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1806 /* Move back to the previous word character. */
1810 if (!window_copy_in_set(wp
, px
, py
, separators
))
1813 if (data
->cy
== 0 &&
1814 (screen_hsize(data
->backing
) == 0 ||
1815 data
->oy
>= screen_hsize(data
->backing
) - 1))
1817 window_copy_cursor_up(wp
, 0);
1819 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1820 px
= window_copy_find_length(wp
, py
);
1824 /* Move back to the beginning of this word. */
1825 while (px
> 0 && !window_copy_in_set(wp
, px
- 1, py
, separators
))
1829 window_copy_update_cursor(wp
, px
, data
->cy
);
1830 if (window_copy_update_selection(wp
))
1831 window_copy_redraw_lines(wp
, data
->cy
, 1);
1835 window_copy_scroll_up(struct window_pane
*wp
, u_int ny
)
1837 struct window_copy_mode_data
*data
= wp
->modedata
;
1838 struct screen
*s
= &data
->screen
;
1839 struct screen_write_ctx ctx
;
1847 screen_write_start(&ctx
, wp
, NULL
);
1848 screen_write_cursormove(&ctx
, 0, 0);
1849 screen_write_deleteline(&ctx
, ny
);
1850 window_copy_write_lines(wp
, &ctx
, screen_size_y(s
) - ny
, ny
);
1851 window_copy_write_line(wp
, &ctx
, 0);
1852 if (screen_size_y(s
) > 1)
1853 window_copy_write_line(wp
, &ctx
, 1);
1854 if (screen_size_y(s
) > 3)
1855 window_copy_write_line(wp
, &ctx
, screen_size_y(s
) - 2);
1856 if (s
->sel
.flag
&& screen_size_y(s
) > ny
) {
1857 window_copy_update_selection(wp
);
1858 window_copy_write_line(wp
, &ctx
, screen_size_y(s
) - ny
- 1);
1860 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
);
1861 window_copy_update_selection(wp
);
1862 screen_write_stop(&ctx
);
1866 window_copy_scroll_down(struct window_pane
*wp
, u_int ny
)
1868 struct window_copy_mode_data
*data
= wp
->modedata
;
1869 struct screen
*s
= &data
->screen
;
1870 struct screen_write_ctx ctx
;
1872 if (ny
> screen_hsize(data
->backing
))
1875 if (data
->oy
> screen_hsize(data
->backing
) - ny
)
1876 ny
= screen_hsize(data
->backing
) - data
->oy
;
1881 screen_write_start(&ctx
, wp
, NULL
);
1882 screen_write_cursormove(&ctx
, 0, 0);
1883 screen_write_insertline(&ctx
, ny
);
1884 window_copy_write_lines(wp
, &ctx
, 0, ny
);
1885 if (s
->sel
.flag
&& screen_size_y(s
) > ny
) {
1886 window_copy_update_selection(wp
);
1887 window_copy_write_line(wp
, &ctx
, ny
);
1888 } else if (ny
== 1) /* nuke position */
1889 window_copy_write_line(wp
, &ctx
, 1);
1890 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
);
1891 window_copy_update_selection(wp
);
1892 screen_write_stop(&ctx
);
1896 window_copy_rectangle_toggle(struct window_pane
*wp
)
1898 struct window_copy_mode_data
*data
= wp
->modedata
;
1901 data
->rectflag
= !data
->rectflag
;
1903 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1904 px
= window_copy_find_length(wp
, py
);
1906 window_copy_update_cursor(wp
, px
, data
->cy
);
1908 window_copy_update_selection(wp
);
1909 window_copy_redraw_screen(wp
);