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
*, struct session
*);
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
;
173 wp
->flags
|= PANE_FREEZE
;
175 bufferevent_disable(wp
->event
, EV_READ
|EV_WRITE
);
177 data
->jumptype
= WINDOW_COPY_OFF
;
178 data
->jumpchar
= '\0';
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
;
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
);
189 mode_key_init(&data
->mdata
, &mode_key_tree_vi_copy
);
191 data
->backing
= NULL
;
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
;
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
;
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
);
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
;
233 window_copy_free(struct window_pane
*wp
)
235 struct window_copy_mode_data
*data
= wp
->modedata
;
237 wp
->flags
&= ~PANE_FREEZE
;
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
);
255 window_copy_add(struct window_pane
*wp
, const char *fmt
, ...)
260 window_copy_vadd(wp
, fmt
, ap
);
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
;
274 if (backing
== &wp
->base
)
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);
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
);
313 window_copy_pageup(struct window_pane
*wp
)
315 struct window_copy_mode_data
*data
= wp
->modedata
;
316 struct screen
*s
= &data
->screen
;
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
);
326 window_copy_update_selection(wp
);
327 window_copy_redraw_screen(wp
);
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)
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
);
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
;
365 enum mode_key_cmd cmd
;
367 np
= data
->numprefix
;
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
);
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);
388 } if (data
->inputtype
== WINDOW_COPY_NUMERICPREFIX
) {
389 if (window_copy_key_numeric_prefix(wp
, key
) == 0)
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)
399 cmd
= mode_key_lookup(&data
->mdata
, key
);
401 case MODEKEYCOPY_CANCEL
:
402 window_pane_reset_mode(wp
);
404 case MODEKEYCOPY_LEFT
:
405 for (; np
!= 0; np
--)
406 window_copy_cursor_left(wp
);
408 case MODEKEYCOPY_RIGHT
:
409 for (; np
!= 0; np
--)
410 window_copy_cursor_right(wp
);
413 for (; np
!= 0; np
--)
414 window_copy_cursor_up(wp
, 0);
416 case MODEKEYCOPY_DOWN
:
417 for (; np
!= 0; np
--)
418 window_copy_cursor_down(wp
, 0);
420 case MODEKEYCOPY_SCROLLUP
:
421 for (; np
!= 0; np
--)
422 window_copy_cursor_up(wp
, 1);
424 case MODEKEYCOPY_SCROLLDOWN
:
425 for (; np
!= 0; np
--)
426 window_copy_cursor_down(wp
, 1);
428 case MODEKEYCOPY_PREVIOUSPAGE
:
429 for (; np
!= 0; np
--)
430 window_copy_pageup(wp
);
432 case MODEKEYCOPY_NEXTPAGE
:
434 if (screen_size_y(s
) > 2)
435 n
= screen_size_y(s
) - 2;
436 for (; np
!= 0; np
--) {
442 window_copy_update_selection(wp
);
443 window_copy_redraw_screen(wp
);
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
);
453 window_copy_update_selection(wp
);
454 window_copy_redraw_screen(wp
);
456 case MODEKEYCOPY_HALFPAGEDOWN
:
457 n
= screen_size_y(s
) / 2;
458 for (; np
!= 0; np
--) {
464 window_copy_update_selection(wp
);
465 window_copy_redraw_screen(wp
);
467 case MODEKEYCOPY_TOPLINE
:
470 window_copy_update_selection(wp
);
471 window_copy_redraw_screen(wp
);
473 case MODEKEYCOPY_MIDDLELINE
:
475 data
->cy
= (screen_size_y(s
) - 1) / 2;
476 window_copy_update_selection(wp
);
477 window_copy_redraw_screen(wp
);
479 case MODEKEYCOPY_BOTTOMLINE
:
481 data
->cy
= screen_size_y(s
) - 1;
482 window_copy_update_selection(wp
);
483 window_copy_redraw_screen(wp
);
485 case MODEKEYCOPY_HISTORYTOP
:
488 data
->oy
= screen_hsize(data
->backing
);
489 window_copy_update_selection(wp
);
490 window_copy_redraw_screen(wp
);
492 case MODEKEYCOPY_HISTORYBOTTOM
:
494 data
->cy
= screen_size_y(s
) - 1;
496 window_copy_update_selection(wp
);
497 window_copy_redraw_screen(wp
);
499 case MODEKEYCOPY_STARTSELECTION
:
500 window_copy_start_selection(wp
);
501 window_copy_redraw_screen(wp
);
503 case MODEKEYCOPY_CLEARSELECTION
:
504 window_copy_clear_selection(wp
);
505 window_copy_redraw_screen(wp
);
507 case MODEKEYCOPY_COPYSELECTION
:
509 window_copy_copy_selection(wp
, sess
);
510 window_pane_reset_mode(wp
);
514 case MODEKEYCOPY_STARTOFLINE
:
515 window_copy_cursor_start_of_line(wp
);
517 case MODEKEYCOPY_BACKTOINDENTATION
:
518 window_copy_cursor_back_to_indentation(wp
);
520 case MODEKEYCOPY_ENDOFLINE
:
521 window_copy_cursor_end_of_line(wp
);
523 case MODEKEYCOPY_NEXTSPACE
:
524 for (; np
!= 0; np
--)
525 window_copy_cursor_next_word(wp
, " ");
527 case MODEKEYCOPY_NEXTSPACEEND
:
528 for (; np
!= 0; np
--)
529 window_copy_cursor_next_word_end(wp
, " ");
531 case MODEKEYCOPY_NEXTWORD
:
533 options_get_string(&wp
->window
->options
, "word-separators");
534 for (; np
!= 0; np
--)
535 window_copy_cursor_next_word(wp
, word_separators
);
537 case MODEKEYCOPY_NEXTWORDEND
:
539 options_get_string(&wp
->window
->options
, "word-separators");
540 for (; np
!= 0; np
--)
541 window_copy_cursor_next_word_end(wp
, word_separators
);
543 case MODEKEYCOPY_PREVIOUSSPACE
:
544 for (; np
!= 0; np
--)
545 window_copy_cursor_previous_word(wp
, " ");
547 case MODEKEYCOPY_PREVIOUSWORD
:
549 options_get_string(&wp
->window
->options
, "word-separators");
550 for (; np
!= 0; np
--)
551 window_copy_cursor_previous_word(wp
, word_separators
);
553 case MODEKEYCOPY_JUMP
:
554 data
->inputtype
= WINDOW_COPY_JUMPFORWARD
;
555 data
->inputprompt
= "Jump Forward";
556 *data
->inputstr
= '\0';
557 window_copy_redraw_lines(wp
, screen_size_y(s
) - 1, 1);
558 return; /* skip numprefix reset */
559 case MODEKEYCOPY_JUMPAGAIN
:
560 if (data
->jumptype
== WINDOW_COPY_JUMPFORWARD
) {
561 for (; np
!= 0; np
--)
562 window_copy_cursor_jump(wp
);
563 } else if (data
->jumptype
== WINDOW_COPY_JUMPBACK
) {
564 for (; np
!= 0; np
--)
565 window_copy_cursor_jump_back(wp
);
568 case MODEKEYCOPY_JUMPREVERSE
:
569 if (data
->jumptype
== WINDOW_COPY_JUMPFORWARD
) {
570 for (; np
!= 0; np
--)
571 window_copy_cursor_jump_back(wp
);
572 } else if (data
->jumptype
== WINDOW_COPY_JUMPBACK
) {
573 for (; np
!= 0; np
--)
574 window_copy_cursor_jump(wp
);
577 case MODEKEYCOPY_JUMPBACK
:
578 data
->inputtype
= WINDOW_COPY_JUMPBACK
;
579 data
->inputprompt
= "Jump Back";
580 *data
->inputstr
= '\0';
581 window_copy_redraw_lines(wp
, screen_size_y(s
) - 1, 1);
582 return; /* skip numprefix reset */
583 case MODEKEYCOPY_SEARCHUP
:
584 data
->inputtype
= WINDOW_COPY_SEARCHUP
;
585 data
->inputprompt
= "Search Up";
587 case MODEKEYCOPY_SEARCHDOWN
:
588 data
->inputtype
= WINDOW_COPY_SEARCHDOWN
;
589 data
->inputprompt
= "Search Down";
591 case MODEKEYCOPY_SEARCHAGAIN
:
592 case MODEKEYCOPY_SEARCHREVERSE
:
593 switch (data
->searchtype
) {
594 case WINDOW_COPY_OFF
:
595 case WINDOW_COPY_GOTOLINE
:
596 case WINDOW_COPY_JUMPFORWARD
:
597 case WINDOW_COPY_JUMPBACK
:
598 case WINDOW_COPY_NUMERICPREFIX
:
600 case WINDOW_COPY_SEARCHUP
:
601 if (cmd
== MODEKEYCOPY_SEARCHAGAIN
) {
602 for (; np
!= 0; np
--) {
603 window_copy_search_up(
604 wp
, data
->searchstr
);
607 for (; np
!= 0; np
--) {
608 window_copy_search_down(
609 wp
, data
->searchstr
);
613 case WINDOW_COPY_SEARCHDOWN
:
614 if (cmd
== MODEKEYCOPY_SEARCHAGAIN
) {
615 for (; np
!= 0; np
--) {
616 window_copy_search_down(
617 wp
, data
->searchstr
);
620 for (; np
!= 0; np
--) {
621 window_copy_search_up(
622 wp
, data
->searchstr
);
628 case MODEKEYCOPY_GOTOLINE
:
629 data
->inputtype
= WINDOW_COPY_GOTOLINE
;
630 data
->inputprompt
= "Goto Line";
631 *data
->inputstr
= '\0';
633 case MODEKEYCOPY_STARTNUMBERPREFIX
:
634 key
&= KEYC_MASK_KEY
;
635 if (key
>= '0' && key
<= '9') {
636 data
->inputtype
= WINDOW_COPY_NUMERICPREFIX
;
638 window_copy_key_numeric_prefix(wp
, key
);
642 case MODEKEYCOPY_RECTANGLETOGGLE
:
643 window_copy_rectangle_toggle(wp
);
653 keys
= options_get_number(&wp
->window
->options
, "mode-keys");
654 if (keys
== MODEKEY_EMACS
)
655 mode_key_init(&data
->mdata
, &mode_key_tree_emacs_edit
);
657 mode_key_init(&data
->mdata
, &mode_key_tree_vi_edit
);
659 window_copy_redraw_lines(wp
, screen_size_y(s
) - 1, 1);
663 keys
= options_get_number(&wp
->window
->options
, "mode-keys");
664 if (keys
== MODEKEY_EMACS
)
665 mode_key_init(&data
->mdata
, &mode_key_tree_emacs_copy
);
667 mode_key_init(&data
->mdata
, &mode_key_tree_vi_copy
);
669 data
->inputtype
= WINDOW_COPY_OFF
;
670 data
->inputprompt
= NULL
;
672 window_copy_redraw_lines(wp
, screen_size_y(s
) - 1, 1);
676 window_copy_key_input(struct window_pane
*wp
, int key
)
678 struct window_copy_mode_data
*data
= wp
->modedata
;
679 struct screen
*s
= &data
->screen
;
683 switch (mode_key_lookup(&data
->mdata
, key
)) {
684 case MODEKEYEDIT_CANCEL
:
687 case MODEKEYEDIT_BACKSPACE
:
688 inputlen
= strlen(data
->inputstr
);
690 data
->inputstr
[inputlen
- 1] = '\0';
692 case MODEKEYEDIT_DELETELINE
:
693 *data
->inputstr
= '\0';
695 case MODEKEYEDIT_ENTER
:
696 np
= data
->numprefix
;
700 switch (data
->inputtype
) {
701 case WINDOW_COPY_OFF
:
702 case WINDOW_COPY_JUMPFORWARD
:
703 case WINDOW_COPY_JUMPBACK
:
704 case WINDOW_COPY_NUMERICPREFIX
:
706 case WINDOW_COPY_SEARCHUP
:
707 for (; np
!= 0; np
--)
708 window_copy_search_up(wp
, data
->inputstr
);
709 data
->searchtype
= data
->inputtype
;
710 data
->searchstr
= xstrdup(data
->inputstr
);
712 case WINDOW_COPY_SEARCHDOWN
:
713 for (; np
!= 0; np
--)
714 window_copy_search_down(wp
, data
->inputstr
);
715 data
->searchtype
= data
->inputtype
;
716 data
->searchstr
= xstrdup(data
->inputstr
);
718 case WINDOW_COPY_GOTOLINE
:
719 window_copy_goto_line(wp
, data
->inputstr
);
720 *data
->inputstr
= '\0';
726 if (key
< 32 || key
> 126)
728 inputlen
= strlen(data
->inputstr
) + 2;
730 data
->inputstr
= xrealloc(data
->inputstr
, 1, inputlen
);
731 data
->inputstr
[inputlen
- 2] = key
;
732 data
->inputstr
[inputlen
- 1] = '\0';
738 window_copy_redraw_lines(wp
, screen_size_y(s
) - 1, 1);
743 window_copy_key_numeric_prefix(struct window_pane
*wp
, int key
)
745 struct window_copy_mode_data
*data
= wp
->modedata
;
746 struct screen
*s
= &data
->screen
;
748 key
&= KEYC_MASK_KEY
;
749 if (key
< '0' || key
> '9')
752 if (data
->numprefix
>= 100) /* no more than three digits */
754 data
->numprefix
= data
->numprefix
* 10 + key
- '0';
756 window_copy_redraw_lines(wp
, screen_size_y(s
) - 1, 1);
763 struct window_pane
*wp
, unused
struct session
*sess
, struct mouse_event
*m
)
765 struct window_copy_mode_data
*data
= wp
->modedata
;
766 struct screen
*s
= &data
->screen
;
769 if (m
->x
>= screen_size_x(s
))
771 if (m
->y
>= screen_size_y(s
))
774 /* If mouse wheel (buttons 4 and 5), scroll. */
775 if ((m
->b
& MOUSE_45
)) {
776 if ((m
->b
& MOUSE_BUTTON
) == MOUSE_1
) {
777 for (i
= 0; i
< 5; i
++)
778 window_copy_cursor_up(wp
, 0);
779 } else if ((m
->b
& MOUSE_BUTTON
) == MOUSE_2
) {
780 for (i
= 0; i
< 5; i
++)
781 window_copy_cursor_down(wp
, 0);
787 * If already reading motion, move the cursor while buttons are still
788 * pressed, or stop the selection on their release.
790 if (s
->mode
& MODE_MOUSEMOTION
) {
791 if ((m
->b
& MOUSE_BUTTON
) != MOUSE_UP
) {
792 window_copy_update_cursor(wp
, m
->x
, m
->y
);
793 if (window_copy_update_selection(wp
))
794 window_copy_redraw_screen(wp
);
796 s
->mode
&= ~MODE_MOUSEMOTION
;
798 window_copy_copy_selection(wp
, sess
);
799 window_pane_reset_mode(wp
);
805 /* Otherwise i other buttons pressed, start selection and motion. */
806 if ((m
->b
& MOUSE_BUTTON
) != MOUSE_UP
) {
807 s
->mode
|= MODE_MOUSEMOTION
;
809 window_copy_update_cursor(wp
, m
->x
, m
->y
);
810 window_copy_start_selection(wp
);
811 window_copy_redraw_screen(wp
);
816 window_copy_scroll_to(struct window_pane
*wp
, u_int px
, u_int py
)
818 struct window_copy_mode_data
*data
= wp
->modedata
;
819 struct grid
*gd
= data
->backing
->grid
;
828 } else if (py
> gd
->hsize
+ gd
->sy
- gap
) {
830 data
->cy
= py
- gd
->hsize
;
832 offset
= py
+ gap
- gd
->sy
;
833 data
->cy
= py
- offset
;
835 data
->oy
= gd
->hsize
- offset
;
837 window_copy_update_selection(wp
);
838 window_copy_redraw_screen(wp
);
842 window_copy_search_compare(
843 struct grid
*gd
, u_int px
, u_int py
, struct grid
*sgd
, u_int spx
)
845 const struct grid_cell
*gc
, *sgc
;
846 const struct grid_utf8
*gu
, *sgu
;
848 gc
= grid_peek_cell(gd
, px
, py
);
849 sgc
= grid_peek_cell(sgd
, spx
, 0);
851 if ((gc
->flags
& GRID_FLAG_UTF8
) != (sgc
->flags
& GRID_FLAG_UTF8
))
854 if (gc
->flags
& GRID_FLAG_UTF8
) {
855 gu
= grid_peek_utf8(gd
, px
, py
);
856 sgu
= grid_peek_utf8(sgd
, spx
, 0);
857 if (grid_utf8_compare(gu
, sgu
))
860 if (gc
->data
== sgc
->data
)
867 window_copy_search_lr(struct grid
*gd
,
868 struct grid
*sgd
, u_int
*ppx
, u_int py
, u_int first
, u_int last
)
872 for (ax
= first
; ax
< last
; ax
++) {
873 if (ax
+ sgd
->sx
>= gd
->sx
)
875 for (bx
= 0; bx
< sgd
->sx
; bx
++) {
877 if (!window_copy_search_compare(gd
, px
, py
, sgd
, bx
))
889 window_copy_search_rl(struct grid
*gd
,
890 struct grid
*sgd
, u_int
*ppx
, u_int py
, u_int first
, u_int last
)
894 for (ax
= last
+ 1; ax
> first
; ax
--) {
895 if (gd
->sx
- (ax
- 1) < sgd
->sx
)
897 for (bx
= 0; bx
< sgd
->sx
; bx
++) {
899 if (!window_copy_search_compare(gd
, px
, py
, sgd
, bx
))
911 window_copy_search_up(struct window_pane
*wp
, const char *searchstr
)
913 struct window_copy_mode_data
*data
= wp
->modedata
;
914 struct screen
*s
= data
->backing
, ss
;
915 struct screen_write_ctx ctx
;
916 struct grid
*gd
= s
->grid
, *sgd
;
919 u_int i
, last
, fx
, fy
, px
;
920 int utf8flag
, n
, wrapped
;
922 if (*searchstr
== '\0')
924 utf8flag
= options_get_number(&wp
->window
->options
, "utf8");
925 searchlen
= screen_write_strlen(utf8flag
, "%s", searchstr
);
927 screen_init(&ss
, searchlen
, 1, 0);
928 screen_write_start(&ctx
, NULL
, &ss
);
929 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
930 screen_write_nputs(&ctx
, -1, &gc
, utf8flag
, "%s", searchstr
);
931 screen_write_stop(&ctx
);
934 fy
= gd
->hsize
- data
->oy
+ data
->cy
;
947 for (i
= fy
+ 1; i
> 0; i
--) {
948 last
= screen_size_x(s
);
951 n
= window_copy_search_rl(gd
, sgd
, &px
, i
- 1, 0, last
);
953 window_copy_scroll_to(wp
, px
, i
- 1);
957 if (!n
&& !wrapped
) {
959 fy
= gd
->hsize
+ gd
->sy
- 1;
968 window_copy_search_down(struct window_pane
*wp
, const char *searchstr
)
970 struct window_copy_mode_data
*data
= wp
->modedata
;
971 struct screen
*s
= data
->backing
, ss
;
972 struct screen_write_ctx ctx
;
973 struct grid
*gd
= s
->grid
, *sgd
;
976 u_int i
, first
, fx
, fy
, px
;
977 int utf8flag
, n
, wrapped
;
979 if (*searchstr
== '\0')
981 utf8flag
= options_get_number(&wp
->window
->options
, "utf8");
982 searchlen
= screen_write_strlen(utf8flag
, "%s", searchstr
);
984 screen_init(&ss
, searchlen
, 1, 0);
985 screen_write_start(&ctx
, NULL
, &ss
);
986 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
987 screen_write_nputs(&ctx
, -1, &gc
, utf8flag
, "%s", searchstr
);
988 screen_write_stop(&ctx
);
991 fy
= gd
->hsize
- data
->oy
+ data
->cy
;
993 if (fx
== gd
->sx
- 1) {
994 if (fy
== gd
->hsize
+ gd
->sy
)
1004 for (i
= fy
+ 1; i
< gd
->hsize
+ gd
->sy
; i
++) {
1008 n
= window_copy_search_lr(gd
, sgd
, &px
, i
- 1, first
, gd
->sx
);
1010 window_copy_scroll_to(wp
, px
, i
- 1);
1014 if (!n
&& !wrapped
) {
1025 window_copy_goto_line(struct window_pane
*wp
, const char *linestr
)
1027 struct window_copy_mode_data
*data
= wp
->modedata
;
1031 lineno
= strtonum(linestr
, 0, screen_hsize(data
->backing
), &errstr
);
1036 window_copy_update_selection(wp
);
1037 window_copy_redraw_screen(wp
);
1041 window_copy_write_line(
1042 struct window_pane
*wp
, struct screen_write_ctx
*ctx
, u_int py
)
1044 struct window_copy_mode_data
*data
= wp
->modedata
;
1045 struct screen
*s
= &data
->screen
;
1046 struct options
*oo
= &wp
->window
->options
;
1047 struct grid_cell gc
;
1049 size_t last
, xoff
= 0, size
= 0;
1051 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
1052 colour_set_fg(&gc
, options_get_number(oo
, "mode-fg"));
1053 colour_set_bg(&gc
, options_get_number(oo
, "mode-bg"));
1054 gc
.attr
|= options_get_number(oo
, "mode-attr");
1056 last
= screen_size_y(s
) - 1;
1058 size
= xsnprintf(hdr
, sizeof hdr
,
1059 "[%u/%u]", data
->oy
, screen_hsize(data
->backing
));
1060 if (size
> screen_size_x(s
))
1061 size
= screen_size_x(s
);
1062 screen_write_cursormove(ctx
, screen_size_x(s
) - size
, 0);
1063 screen_write_puts(ctx
, &gc
, "%s", hdr
);
1064 } else if (py
== last
&& data
->inputtype
!= WINDOW_COPY_OFF
) {
1065 if (data
->inputtype
== WINDOW_COPY_NUMERICPREFIX
) {
1066 xoff
= size
= xsnprintf(hdr
, sizeof hdr
,
1067 "Repeat: %u", data
->numprefix
);
1069 xoff
= size
= xsnprintf(hdr
, sizeof hdr
,
1070 "%s: %s", data
->inputprompt
, data
->inputstr
);
1072 screen_write_cursormove(ctx
, 0, last
);
1073 screen_write_puts(ctx
, &gc
, "%s", hdr
);
1077 screen_write_cursormove(ctx
, xoff
, py
);
1078 screen_write_copy(ctx
, data
->backing
, xoff
,
1079 (screen_hsize(data
->backing
) - data
->oy
) + py
,
1080 screen_size_x(s
) - size
, 1);
1082 if (py
== data
->cy
&& data
->cx
== screen_size_x(s
)) {
1083 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
1084 screen_write_cursormove(ctx
, screen_size_x(s
) - 1, py
);
1085 screen_write_putc(ctx
, &gc
, '$');
1090 window_copy_write_lines(
1091 struct window_pane
*wp
, struct screen_write_ctx
*ctx
, u_int py
, u_int ny
)
1095 for (yy
= py
; yy
< py
+ ny
; yy
++)
1096 window_copy_write_line(wp
, ctx
, py
);
1100 window_copy_redraw_lines(struct window_pane
*wp
, u_int py
, u_int ny
)
1102 struct window_copy_mode_data
*data
= wp
->modedata
;
1103 struct screen_write_ctx ctx
;
1106 screen_write_start(&ctx
, wp
, NULL
);
1107 for (i
= py
; i
< py
+ ny
; i
++)
1108 window_copy_write_line(wp
, &ctx
, i
);
1109 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
);
1110 screen_write_stop(&ctx
);
1114 window_copy_redraw_screen(struct window_pane
*wp
)
1116 struct window_copy_mode_data
*data
= wp
->modedata
;
1118 window_copy_redraw_lines(wp
, 0, screen_size_y(&data
->screen
));
1122 window_copy_update_cursor(struct window_pane
*wp
, u_int cx
, u_int cy
)
1124 struct window_copy_mode_data
*data
= wp
->modedata
;
1125 struct screen
*s
= &data
->screen
;
1126 struct screen_write_ctx ctx
;
1127 u_int old_cx
, old_cy
;
1129 old_cx
= data
->cx
; old_cy
= data
->cy
;
1130 data
->cx
= cx
; data
->cy
= cy
;
1131 if (old_cx
== screen_size_x(s
))
1132 window_copy_redraw_lines(wp
, old_cy
, 1);
1133 if (data
->cx
== screen_size_x(s
))
1134 window_copy_redraw_lines(wp
, data
->cy
, 1);
1136 screen_write_start(&ctx
, wp
, NULL
);
1137 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
);
1138 screen_write_stop(&ctx
);
1143 window_copy_start_selection(struct window_pane
*wp
)
1145 struct window_copy_mode_data
*data
= wp
->modedata
;
1146 struct screen
*s
= &data
->screen
;
1148 data
->selx
= data
->cx
;
1149 data
->sely
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1152 window_copy_update_selection(wp
);
1156 window_copy_update_selection(struct window_pane
*wp
)
1158 struct window_copy_mode_data
*data
= wp
->modedata
;
1159 struct screen
*s
= &data
->screen
;
1160 struct options
*oo
= &wp
->window
->options
;
1161 struct grid_cell gc
;
1162 u_int sx
, sy
, ty
, cy
;
1168 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
1169 colour_set_fg(&gc
, options_get_number(oo
, "mode-fg"));
1170 colour_set_bg(&gc
, options_get_number(oo
, "mode-bg"));
1171 gc
.attr
|= options_get_number(oo
, "mode-attr");
1173 /* Find top of screen. */
1174 ty
= screen_hsize(data
->backing
) - data
->oy
;
1176 /* Adjust the selection. */
1179 if (sy
< ty
) { /* above screen */
1180 if (!data
->rectflag
)
1183 } else if (sy
> ty
+ screen_size_y(s
) - 1) { /* below screen */
1184 if (!data
->rectflag
)
1185 sx
= screen_size_x(s
) - 1;
1186 sy
= screen_size_y(s
) - 1;
1189 sy
= screen_hsize(s
) + sy
;
1191 screen_set_selection(s
,
1192 sx
, sy
, data
->cx
, screen_hsize(s
) + data
->cy
, data
->rectflag
, &gc
);
1194 if (data
->rectflag
) {
1196 * Can't rely on the caller to redraw the right lines for
1197 * rectangle selection - find the highest line and the number
1198 * of lines, and redraw just past that in both directions
1202 window_copy_redraw_lines(wp
, sy
, cy
- sy
+ 1);
1204 window_copy_redraw_lines(wp
, cy
, sy
- cy
+ 1);
1211 window_copy_copy_selection(struct window_pane
*wp
, struct session
*sess
)
1213 struct window_copy_mode_data
*data
= wp
->modedata
;
1214 struct screen
*s
= &data
->screen
;
1217 u_int i
, xx
, yy
, sx
, sy
, ex
, ey
, limit
;
1218 u_int firstsx
, lastex
, restex
, restsx
;
1229 * The selection extends from selx,sely to (adjusted) cx,cy on
1233 /* Find start and end. */
1235 yy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1236 if (yy
< data
->sely
|| (yy
== data
->sely
&& xx
< data
->selx
)) {
1238 ex
= data
->selx
; ey
= data
->sely
;
1240 sx
= data
->selx
; sy
= data
->sely
;
1244 /* Trim ex to end of line. */
1245 xx
= window_copy_find_length(wp
, ey
);
1250 * Deal with rectangle-copy if necessary; four situations: start of
1251 * first line (firstsx), end of last line (lastex), start (restsx) and
1252 * end (restex) of all other lines.
1254 xx
= screen_size_x(s
);
1255 if (data
->rectflag
) {
1257 * Need to ignore the column with the cursor in it, which for
1258 * rectangular copy means knowing which side the cursor is on.
1260 if (data
->selx
< data
->cx
) {
1261 /* Selection start is on the left. */
1264 firstsx
= data
->selx
;
1265 restsx
= data
->selx
;
1267 /* Cursor is on the left. */
1268 lastex
= data
->selx
+ 1;
1269 restex
= data
->selx
+ 1;
1270 firstsx
= data
->cx
+ 1;
1271 restsx
= data
->cx
+ 1;
1275 * Like emacs, keep the top-left-most character, and drop the
1276 * bottom-right-most, regardless of copy direction.
1284 /* Copy the lines. */
1286 window_copy_copy_line(wp
, &buf
, &off
, sy
, firstsx
, lastex
);
1288 window_copy_copy_line(wp
, &buf
, &off
, sy
, firstsx
, restex
);
1290 for (i
= sy
+ 1; i
< ey
; i
++) {
1291 window_copy_copy_line(
1292 wp
, &buf
, &off
, i
, restsx
, restex
);
1295 window_copy_copy_line(wp
, &buf
, &off
, ey
, restsx
, lastex
);
1298 /* Don't bother if no data. */
1303 off
--; /* remove final \n */
1305 /* Add the buffer to the stack. */
1306 limit
= options_get_number(&sess
->options
, "buffer-limit");
1307 paste_add(&sess
->buffers
, buf
, off
, limit
);
1311 window_copy_copy_line(struct window_pane
*wp
,
1312 char **buf
, size_t *off
, u_int sy
, u_int sx
, u_int ex
)
1314 struct window_copy_mode_data
*data
= wp
->modedata
;
1315 struct grid
*gd
= data
->backing
->grid
;
1316 const struct grid_cell
*gc
;
1317 const struct grid_utf8
*gu
;
1318 struct grid_line
*gl
;
1319 u_int i
, xx
, wrapped
= 0;
1326 * Work out if the line was wrapped at the screen edge and all of it is
1329 gl
= &gd
->linedata
[sy
];
1330 if (gl
->flags
& GRID_LINE_WRAPPED
&& gl
->cellsize
<= gd
->sx
)
1333 /* If the line was wrapped, don't strip spaces (use the full length). */
1337 xx
= window_copy_find_length(wp
, sy
);
1344 for (i
= sx
; i
< ex
; i
++) {
1345 gc
= grid_peek_cell(gd
, i
, sy
);
1346 if (gc
->flags
& GRID_FLAG_PADDING
)
1348 if (!(gc
->flags
& GRID_FLAG_UTF8
)) {
1349 *buf
= xrealloc(*buf
, 1, (*off
) + 1);
1350 (*buf
)[(*off
)++] = gc
->data
;
1352 gu
= grid_peek_utf8(gd
, i
, sy
);
1353 size
= grid_utf8_size(gu
);
1354 *buf
= xrealloc(*buf
, 1, (*off
) + size
);
1355 *off
+= grid_utf8_copy(gu
, *buf
+ *off
, size
);
1360 /* Only add a newline if the line wasn't wrapped. */
1361 if (!wrapped
|| ex
!= xx
) {
1362 *buf
= xrealloc(*buf
, 1, (*off
) + 1);
1363 (*buf
)[(*off
)++] = '\n';
1368 window_copy_clear_selection(struct window_pane
*wp
)
1370 struct window_copy_mode_data
*data
= wp
->modedata
;
1373 screen_clear_selection(&data
->screen
);
1375 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1376 px
= window_copy_find_length(wp
, py
);
1378 window_copy_update_cursor(wp
, px
, data
->cy
);
1382 window_copy_in_set(struct window_pane
*wp
, u_int px
, u_int py
, const char *set
)
1384 struct window_copy_mode_data
*data
= wp
->modedata
;
1385 const struct grid_cell
*gc
;
1387 gc
= grid_peek_cell(data
->backing
->grid
, px
, py
);
1388 if (gc
->flags
& (GRID_FLAG_PADDING
|GRID_FLAG_UTF8
))
1390 if (gc
->data
== 0x00 || gc
->data
== 0x7f)
1392 return (strchr(set
, gc
->data
) != NULL
);
1396 window_copy_find_length(struct window_pane
*wp
, u_int py
)
1398 struct window_copy_mode_data
*data
= wp
->modedata
;
1399 struct screen
*s
= data
->backing
;
1400 const struct grid_cell
*gc
;
1404 * If the pane has been resized, its grid can contain old overlong
1405 * lines. grid_peek_cell does not allow accessing cells beyond the
1406 * width of the grid, and screen_write_copy treats them as spaces, so
1407 * ignore them here too.
1409 px
= s
->grid
->linedata
[py
].cellsize
;
1410 if (px
> screen_size_x(s
))
1411 px
= screen_size_x(s
);
1413 gc
= grid_peek_cell(s
->grid
, px
- 1, py
);
1414 if (gc
->flags
& GRID_FLAG_UTF8
)
1416 if (gc
->data
!= ' ')
1424 window_copy_cursor_start_of_line(struct window_pane
*wp
)
1426 struct window_copy_mode_data
*data
= wp
->modedata
;
1427 struct screen
*back_s
= data
->backing
;
1428 struct grid
*gd
= back_s
->grid
;
1431 if (data
->cx
== 0) {
1432 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1433 while (py
> 0 && gd
->linedata
[py
-1].flags
& GRID_LINE_WRAPPED
) {
1434 window_copy_cursor_up(wp
, 0);
1435 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1438 window_copy_update_cursor(wp
, 0, data
->cy
);
1439 if (window_copy_update_selection(wp
))
1440 window_copy_redraw_lines(wp
, data
->cy
, 1);
1444 window_copy_cursor_back_to_indentation(struct window_pane
*wp
)
1446 struct window_copy_mode_data
*data
= wp
->modedata
;
1448 const struct grid_cell
*gc
;
1451 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1452 xx
= window_copy_find_length(wp
, py
);
1455 gc
= grid_peek_cell(data
->backing
->grid
, px
, py
);
1456 if (gc
->flags
& GRID_FLAG_UTF8
)
1458 if (gc
->data
!= ' ')
1463 window_copy_update_cursor(wp
, px
, data
->cy
);
1464 if (window_copy_update_selection(wp
))
1465 window_copy_redraw_lines(wp
, data
->cy
, 1);
1469 window_copy_cursor_end_of_line(struct window_pane
*wp
)
1471 struct window_copy_mode_data
*data
= wp
->modedata
;
1472 struct screen
*back_s
= data
->backing
;
1473 struct grid
*gd
= back_s
->grid
;
1476 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1477 px
= window_copy_find_length(wp
, py
);
1479 if (data
->cx
== px
) {
1480 if (data
->screen
.sel
.flag
&& data
->rectflag
)
1481 px
= screen_size_x(back_s
);
1482 if (gd
->linedata
[py
].flags
& GRID_LINE_WRAPPED
) {
1483 while (py
< gd
->sy
+ gd
->hsize
&&
1484 gd
->linedata
[py
].flags
& GRID_LINE_WRAPPED
) {
1485 window_copy_cursor_down(wp
, 0);
1486 py
= screen_hsize(back_s
)
1487 + data
->cy
- data
->oy
;
1489 px
= window_copy_find_length(wp
, py
);
1492 window_copy_update_cursor(wp
, px
, data
->cy
);
1494 if (window_copy_update_selection(wp
))
1495 window_copy_redraw_lines(wp
, data
->cy
, 1);
1499 window_copy_cursor_left(struct window_pane
*wp
)
1501 struct window_copy_mode_data
*data
= wp
->modedata
;
1503 if (data
->cx
== 0) {
1504 window_copy_cursor_up(wp
, 0);
1505 window_copy_cursor_end_of_line(wp
);
1507 window_copy_update_cursor(wp
, data
->cx
- 1, data
->cy
);
1508 if (window_copy_update_selection(wp
))
1509 window_copy_redraw_lines(wp
, data
->cy
, 1);
1514 window_copy_cursor_right(struct window_pane
*wp
)
1516 struct window_copy_mode_data
*data
= wp
->modedata
;
1519 if (data
->screen
.sel
.flag
&& data
->rectflag
)
1520 px
= screen_size_x(&data
->screen
);
1522 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1523 px
= window_copy_find_length(wp
, py
);
1526 if (data
->cx
>= px
) {
1527 window_copy_cursor_start_of_line(wp
);
1528 window_copy_cursor_down(wp
, 0);
1530 window_copy_update_cursor(wp
, data
->cx
+ 1, data
->cy
);
1531 if (window_copy_update_selection(wp
))
1532 window_copy_redraw_lines(wp
, data
->cy
, 1);
1537 window_copy_cursor_up(struct window_pane
*wp
, int scroll_only
)
1539 struct window_copy_mode_data
*data
= wp
->modedata
;
1540 struct screen
*s
= &data
->screen
;
1541 u_int ox
, oy
, px
, py
;
1543 oy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1544 ox
= window_copy_find_length(wp
, oy
);
1546 data
->lastcx
= data
->cx
;
1550 data
->cx
= data
->lastcx
;
1551 if (scroll_only
|| data
->cy
== 0) {
1552 window_copy_scroll_down(wp
, 1);
1554 if (data
->cy
== screen_size_y(s
) - 1)
1555 window_copy_redraw_lines(wp
, data
->cy
, 1);
1557 window_copy_redraw_lines(wp
, data
->cy
, 2);
1560 window_copy_update_cursor(wp
, data
->cx
, data
->cy
- 1);
1561 if (window_copy_update_selection(wp
)) {
1562 if (data
->cy
== screen_size_y(s
) - 1)
1563 window_copy_redraw_lines(wp
, data
->cy
, 1);
1565 window_copy_redraw_lines(wp
, data
->cy
, 2);
1569 if (!data
->screen
.sel
.flag
|| !data
->rectflag
) {
1570 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1571 px
= window_copy_find_length(wp
, py
);
1572 if ((data
->cx
>= data
->lastsx
&& data
->cx
!= px
) ||
1574 window_copy_cursor_end_of_line(wp
);
1579 window_copy_cursor_down(struct window_pane
*wp
, int scroll_only
)
1581 struct window_copy_mode_data
*data
= wp
->modedata
;
1582 struct screen
*s
= &data
->screen
;
1583 u_int ox
, oy
, px
, py
;
1585 oy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1586 ox
= window_copy_find_length(wp
, oy
);
1588 data
->lastcx
= data
->cx
;
1592 data
->cx
= data
->lastcx
;
1593 if (scroll_only
|| data
->cy
== screen_size_y(s
) - 1) {
1594 window_copy_scroll_up(wp
, 1);
1595 if (scroll_only
&& data
->cy
> 0)
1596 window_copy_redraw_lines(wp
, data
->cy
- 1, 2);
1598 window_copy_update_cursor(wp
, data
->cx
, data
->cy
+ 1);
1599 if (window_copy_update_selection(wp
))
1600 window_copy_redraw_lines(wp
, data
->cy
- 1, 2);
1603 if (!data
->screen
.sel
.flag
|| !data
->rectflag
) {
1604 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1605 px
= window_copy_find_length(wp
, py
);
1606 if ((data
->cx
>= data
->lastsx
&& data
->cx
!= px
) ||
1608 window_copy_cursor_end_of_line(wp
);
1613 window_copy_cursor_jump(struct window_pane
*wp
)
1615 struct window_copy_mode_data
*data
= wp
->modedata
;
1616 struct screen
*back_s
= data
->backing
;
1617 const struct grid_cell
*gc
;
1621 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1622 xx
= window_copy_find_length(wp
, py
);
1625 gc
= grid_peek_cell(back_s
->grid
, px
, py
);
1626 if ((gc
->flags
& (GRID_FLAG_PADDING
|GRID_FLAG_UTF8
)) == 0
1627 && gc
->data
== data
->jumpchar
) {
1629 window_copy_update_cursor(wp
, px
, data
->cy
);
1630 if (window_copy_update_selection(wp
))
1631 window_copy_redraw_lines(wp
, data
->cy
, 1);
1639 window_copy_cursor_jump_back(struct window_pane
*wp
)
1641 struct window_copy_mode_data
*data
= wp
->modedata
;
1642 struct screen
*back_s
= data
->backing
;
1643 const struct grid_cell
*gc
;
1647 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1653 gc
= grid_peek_cell(back_s
->grid
, px
, py
);
1654 if ((gc
->flags
& (GRID_FLAG_PADDING
|GRID_FLAG_UTF8
)) == 0
1655 && gc
->data
== data
->jumpchar
) {
1657 window_copy_update_cursor(wp
, px
, data
->cy
);
1658 if (window_copy_update_selection(wp
))
1659 window_copy_redraw_lines(wp
, data
->cy
, 1);
1669 window_copy_cursor_next_word(struct window_pane
*wp
, const char *separators
)
1671 struct window_copy_mode_data
*data
= wp
->modedata
;
1672 struct screen
*back_s
= data
->backing
;
1673 u_int px
, py
, xx
, yy
;
1677 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1678 xx
= window_copy_find_length(wp
, py
);
1679 yy
= screen_hsize(back_s
) + screen_size_y(back_s
) - 1;
1682 * First skip past any nonword characters and then any word characters.
1684 * expected is initially set to 0 for the former and then 1 for the
1689 window_copy_in_set(wp
, px
, py
, separators
) == expected
) {
1690 /* Move down if we're past the end of the line. */
1694 window_copy_cursor_down(wp
, 0);
1697 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1698 xx
= window_copy_find_length(wp
, py
);
1702 expected
= !expected
;
1703 } while (expected
== 1);
1705 window_copy_update_cursor(wp
, px
, data
->cy
);
1706 if (window_copy_update_selection(wp
))
1707 window_copy_redraw_lines(wp
, data
->cy
, 1);
1711 window_copy_cursor_next_word_end(struct window_pane
*wp
, const char *separators
)
1713 struct window_copy_mode_data
*data
= wp
->modedata
;
1714 struct screen
*back_s
= data
->backing
;
1715 u_int px
, py
, xx
, yy
;
1719 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1720 xx
= window_copy_find_length(wp
, py
);
1721 yy
= screen_hsize(back_s
) + screen_size_y(back_s
) - 1;
1724 * First skip past any word characters, then any nonword characters.
1726 * expected is initially set to 1 for the former and then 0 for the
1731 window_copy_in_set(wp
, px
, py
, separators
) == expected
) {
1732 /* Move down if we're past the end of the line. */
1736 window_copy_cursor_down(wp
, 0);
1739 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1740 xx
= window_copy_find_length(wp
, py
);
1744 expected
= !expected
;
1745 } while (expected
== 0);
1747 window_copy_update_cursor(wp
, px
, data
->cy
);
1748 if (window_copy_update_selection(wp
))
1749 window_copy_redraw_lines(wp
, data
->cy
, 1);
1752 /* Move to the previous place where a word begins. */
1754 window_copy_cursor_previous_word(struct window_pane
*wp
, const char *separators
)
1756 struct window_copy_mode_data
*data
= wp
->modedata
;
1760 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1762 /* Move back to the previous word character. */
1766 if (!window_copy_in_set(wp
, px
, py
, separators
))
1769 if (data
->cy
== 0 &&
1770 (screen_hsize(data
->backing
) == 0 ||
1771 data
->oy
>= screen_hsize(data
->backing
) - 1))
1773 window_copy_cursor_up(wp
, 0);
1775 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1776 px
= window_copy_find_length(wp
, py
);
1780 /* Move back to the beginning of this word. */
1781 while (px
> 0 && !window_copy_in_set(wp
, px
- 1, py
, separators
))
1785 window_copy_update_cursor(wp
, px
, data
->cy
);
1786 if (window_copy_update_selection(wp
))
1787 window_copy_redraw_lines(wp
, data
->cy
, 1);
1791 window_copy_scroll_up(struct window_pane
*wp
, u_int ny
)
1793 struct window_copy_mode_data
*data
= wp
->modedata
;
1794 struct screen
*s
= &data
->screen
;
1795 struct screen_write_ctx ctx
;
1803 screen_write_start(&ctx
, wp
, NULL
);
1804 screen_write_cursormove(&ctx
, 0, 0);
1805 screen_write_deleteline(&ctx
, ny
);
1806 window_copy_write_lines(wp
, &ctx
, screen_size_y(s
) - ny
, ny
);
1807 window_copy_write_line(wp
, &ctx
, 0);
1808 if (screen_size_y(s
) > 1)
1809 window_copy_write_line(wp
, &ctx
, 1);
1810 if (screen_size_y(s
) > 3)
1811 window_copy_write_line(wp
, &ctx
, screen_size_y(s
) - 2);
1812 if (s
->sel
.flag
&& screen_size_y(s
) > ny
) {
1813 window_copy_update_selection(wp
);
1814 window_copy_write_line(wp
, &ctx
, screen_size_y(s
) - ny
- 1);
1816 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
);
1817 window_copy_update_selection(wp
);
1818 screen_write_stop(&ctx
);
1822 window_copy_scroll_down(struct window_pane
*wp
, u_int ny
)
1824 struct window_copy_mode_data
*data
= wp
->modedata
;
1825 struct screen
*s
= &data
->screen
;
1826 struct screen_write_ctx ctx
;
1828 if (ny
> screen_hsize(data
->backing
))
1831 if (data
->oy
> screen_hsize(data
->backing
) - ny
)
1832 ny
= screen_hsize(data
->backing
) - data
->oy
;
1837 screen_write_start(&ctx
, wp
, NULL
);
1838 screen_write_cursormove(&ctx
, 0, 0);
1839 screen_write_insertline(&ctx
, ny
);
1840 window_copy_write_lines(wp
, &ctx
, 0, ny
);
1841 if (s
->sel
.flag
&& screen_size_y(s
) > ny
) {
1842 window_copy_update_selection(wp
);
1843 window_copy_write_line(wp
, &ctx
, ny
);
1844 } else if (ny
== 1) /* nuke position */
1845 window_copy_write_line(wp
, &ctx
, 1);
1846 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
);
1847 window_copy_update_selection(wp
);
1848 screen_write_stop(&ctx
);
1852 window_copy_rectangle_toggle(struct window_pane
*wp
)
1854 struct window_copy_mode_data
*data
= wp
->modedata
;
1857 data
->rectflag
= !data
->rectflag
;
1859 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1860 px
= window_copy_find_length(wp
, py
);
1862 window_copy_update_cursor(wp
, px
, data
->cy
);
1864 window_copy_update_selection(wp
);
1865 window_copy_redraw_screen(wp
);