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
;
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
;
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
;
236 wp
->flags
&= ~PANE_FREEZE
;
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)
344 window_copy_clear_selection(wp
);
346 screen_write_start(&ctx
, NULL
, s
);
347 window_copy_write_lines(wp
, &ctx
, 0, screen_size_y(s
) - 1);
348 screen_write_stop(&ctx
);
350 window_copy_redraw_screen(wp
);
354 window_copy_key(struct window_pane
*wp
, struct session
*sess
, int key
)
356 const char *word_separators
;
357 struct window_copy_mode_data
*data
= wp
->modedata
;
358 struct screen
*s
= &data
->screen
;
361 enum mode_key_cmd cmd
;
363 np
= data
->numprefix
;
367 if (data
->inputtype
== WINDOW_COPY_JUMPFORWARD
368 || data
->inputtype
== WINDOW_COPY_JUMPBACK
) {
369 /* Ignore keys with modifiers. */
370 if ((key
& KEYC_MASK_MOD
) == 0) {
371 data
->jumpchar
= key
;
372 if (data
->inputtype
== WINDOW_COPY_JUMPFORWARD
) {
373 for (; np
!= 0; np
--)
374 window_copy_cursor_jump(wp
);
376 for (; np
!= 0; np
--)
377 window_copy_cursor_jump_back(wp
);
380 data
->jumptype
= data
->inputtype
;
381 data
->inputtype
= WINDOW_COPY_OFF
;
382 window_copy_redraw_lines(wp
, screen_size_y(s
) - 1, 1);
384 } if (data
->inputtype
== WINDOW_COPY_NUMERICPREFIX
) {
385 if (window_copy_key_numeric_prefix(wp
, key
) == 0)
387 data
->inputtype
= WINDOW_COPY_OFF
;
388 window_copy_redraw_lines(wp
, screen_size_y(s
) - 1, 1);
389 } else if (data
->inputtype
!= WINDOW_COPY_OFF
) {
390 if (window_copy_key_input(wp
, key
) != 0)
395 cmd
= mode_key_lookup(&data
->mdata
, key
);
397 case MODEKEYCOPY_CANCEL
:
398 window_pane_reset_mode(wp
);
400 case MODEKEYCOPY_LEFT
:
401 for (; np
!= 0; np
--)
402 window_copy_cursor_left(wp
);
404 case MODEKEYCOPY_RIGHT
:
405 for (; np
!= 0; np
--)
406 window_copy_cursor_right(wp
);
409 for (; np
!= 0; np
--)
410 window_copy_cursor_up(wp
, 0);
412 case MODEKEYCOPY_DOWN
:
413 for (; np
!= 0; np
--)
414 window_copy_cursor_down(wp
, 0);
416 case MODEKEYCOPY_SCROLLUP
:
417 for (; np
!= 0; np
--)
418 window_copy_cursor_up(wp
, 1);
420 case MODEKEYCOPY_SCROLLDOWN
:
421 for (; np
!= 0; np
--)
422 window_copy_cursor_down(wp
, 1);
424 case MODEKEYCOPY_PREVIOUSPAGE
:
425 for (; np
!= 0; np
--)
426 window_copy_pageup(wp
);
428 case MODEKEYCOPY_NEXTPAGE
:
430 if (screen_size_y(s
) > 2)
431 n
= screen_size_y(s
) - 2;
432 for (; np
!= 0; np
--) {
438 window_copy_update_selection(wp
);
439 window_copy_redraw_screen(wp
);
441 case MODEKEYCOPY_HALFPAGEUP
:
442 n
= screen_size_y(s
) / 2;
443 for (; np
!= 0; np
--) {
444 if (data
->oy
+ n
> screen_hsize(data
->backing
))
445 data
->oy
= screen_hsize(data
->backing
);
449 window_copy_update_selection(wp
);
450 window_copy_redraw_screen(wp
);
452 case MODEKEYCOPY_HALFPAGEDOWN
:
453 n
= screen_size_y(s
) / 2;
454 for (; np
!= 0; np
--) {
460 window_copy_update_selection(wp
);
461 window_copy_redraw_screen(wp
);
463 case MODEKEYCOPY_TOPLINE
:
466 window_copy_update_selection(wp
);
467 window_copy_redraw_screen(wp
);
469 case MODEKEYCOPY_MIDDLELINE
:
471 data
->cy
= (screen_size_y(s
) - 1) / 2;
472 window_copy_update_selection(wp
);
473 window_copy_redraw_screen(wp
);
475 case MODEKEYCOPY_BOTTOMLINE
:
477 data
->cy
= screen_size_y(s
) - 1;
478 window_copy_update_selection(wp
);
479 window_copy_redraw_screen(wp
);
481 case MODEKEYCOPY_HISTORYTOP
:
484 data
->oy
= screen_hsize(data
->backing
);
485 window_copy_update_selection(wp
);
486 window_copy_redraw_screen(wp
);
488 case MODEKEYCOPY_HISTORYBOTTOM
:
490 data
->cy
= screen_size_y(s
) - 1;
492 window_copy_update_selection(wp
);
493 window_copy_redraw_screen(wp
);
495 case MODEKEYCOPY_STARTSELECTION
:
496 window_copy_start_selection(wp
);
497 window_copy_redraw_screen(wp
);
499 case MODEKEYCOPY_CLEARSELECTION
:
500 window_copy_clear_selection(wp
);
501 window_copy_redraw_screen(wp
);
503 case MODEKEYCOPY_COPYSELECTION
:
505 window_copy_copy_selection(wp
, sess
);
506 window_pane_reset_mode(wp
);
510 case MODEKEYCOPY_STARTOFLINE
:
511 window_copy_cursor_start_of_line(wp
);
513 case MODEKEYCOPY_BACKTOINDENTATION
:
514 window_copy_cursor_back_to_indentation(wp
);
516 case MODEKEYCOPY_ENDOFLINE
:
517 window_copy_cursor_end_of_line(wp
);
519 case MODEKEYCOPY_NEXTSPACE
:
520 for (; np
!= 0; np
--)
521 window_copy_cursor_next_word(wp
, " ");
523 case MODEKEYCOPY_NEXTSPACEEND
:
524 for (; np
!= 0; np
--)
525 window_copy_cursor_next_word_end(wp
, " ");
527 case MODEKEYCOPY_NEXTWORD
:
529 options_get_string(&wp
->window
->options
, "word-separators");
530 for (; np
!= 0; np
--)
531 window_copy_cursor_next_word(wp
, word_separators
);
533 case MODEKEYCOPY_NEXTWORDEND
:
535 options_get_string(&wp
->window
->options
, "word-separators");
536 for (; np
!= 0; np
--)
537 window_copy_cursor_next_word_end(wp
, word_separators
);
539 case MODEKEYCOPY_PREVIOUSSPACE
:
540 for (; np
!= 0; np
--)
541 window_copy_cursor_previous_word(wp
, " ");
543 case MODEKEYCOPY_PREVIOUSWORD
:
545 options_get_string(&wp
->window
->options
, "word-separators");
546 for (; np
!= 0; np
--)
547 window_copy_cursor_previous_word(wp
, word_separators
);
549 case MODEKEYCOPY_JUMP
:
550 data
->inputtype
= WINDOW_COPY_JUMPFORWARD
;
551 data
->inputprompt
= "Jump Forward";
552 *data
->inputstr
= '\0';
553 window_copy_redraw_lines(wp
, screen_size_y(s
) - 1, 1);
554 return; /* skip numprefix reset */
555 case MODEKEYCOPY_JUMPAGAIN
:
556 if (data
->jumptype
== WINDOW_COPY_JUMPFORWARD
) {
557 for (; np
!= 0; np
--)
558 window_copy_cursor_jump(wp
);
559 } else if (data
->jumptype
== WINDOW_COPY_JUMPBACK
) {
560 for (; np
!= 0; np
--)
561 window_copy_cursor_jump_back(wp
);
564 case MODEKEYCOPY_JUMPREVERSE
:
565 if (data
->jumptype
== WINDOW_COPY_JUMPFORWARD
) {
566 for (; np
!= 0; np
--)
567 window_copy_cursor_jump_back(wp
);
568 } else if (data
->jumptype
== WINDOW_COPY_JUMPBACK
) {
569 for (; np
!= 0; np
--)
570 window_copy_cursor_jump(wp
);
573 case MODEKEYCOPY_JUMPBACK
:
574 data
->inputtype
= WINDOW_COPY_JUMPBACK
;
575 data
->inputprompt
= "Jump Back";
576 *data
->inputstr
= '\0';
577 window_copy_redraw_lines(wp
, screen_size_y(s
) - 1, 1);
578 return; /* skip numprefix reset */
579 case MODEKEYCOPY_SEARCHUP
:
580 data
->inputtype
= WINDOW_COPY_SEARCHUP
;
581 data
->inputprompt
= "Search Up";
583 case MODEKEYCOPY_SEARCHDOWN
:
584 data
->inputtype
= WINDOW_COPY_SEARCHDOWN
;
585 data
->inputprompt
= "Search Down";
587 case MODEKEYCOPY_SEARCHAGAIN
:
588 case MODEKEYCOPY_SEARCHREVERSE
:
589 switch (data
->searchtype
) {
590 case WINDOW_COPY_OFF
:
591 case WINDOW_COPY_GOTOLINE
:
592 case WINDOW_COPY_JUMPFORWARD
:
593 case WINDOW_COPY_JUMPBACK
:
594 case WINDOW_COPY_NUMERICPREFIX
:
596 case WINDOW_COPY_SEARCHUP
:
597 if (cmd
== MODEKEYCOPY_SEARCHAGAIN
) {
598 for (; np
!= 0; np
--) {
599 window_copy_search_up(
600 wp
, data
->searchstr
);
603 for (; np
!= 0; np
--) {
604 window_copy_search_down(
605 wp
, data
->searchstr
);
609 case WINDOW_COPY_SEARCHDOWN
:
610 if (cmd
== MODEKEYCOPY_SEARCHAGAIN
) {
611 for (; np
!= 0; np
--) {
612 window_copy_search_down(
613 wp
, data
->searchstr
);
616 for (; np
!= 0; np
--) {
617 window_copy_search_up(
618 wp
, data
->searchstr
);
624 case MODEKEYCOPY_GOTOLINE
:
625 data
->inputtype
= WINDOW_COPY_GOTOLINE
;
626 data
->inputprompt
= "Goto Line";
627 *data
->inputstr
= '\0';
629 case MODEKEYCOPY_STARTNUMBERPREFIX
:
630 key
&= KEYC_MASK_KEY
;
631 if (key
>= '0' && key
<= '9') {
632 data
->inputtype
= WINDOW_COPY_NUMERICPREFIX
;
634 window_copy_key_numeric_prefix(wp
, key
);
638 case MODEKEYCOPY_RECTANGLETOGGLE
:
639 window_copy_rectangle_toggle(wp
);
649 keys
= options_get_number(&wp
->window
->options
, "mode-keys");
650 if (keys
== MODEKEY_EMACS
)
651 mode_key_init(&data
->mdata
, &mode_key_tree_emacs_edit
);
653 mode_key_init(&data
->mdata
, &mode_key_tree_vi_edit
);
655 window_copy_redraw_lines(wp
, screen_size_y(s
) - 1, 1);
659 keys
= options_get_number(&wp
->window
->options
, "mode-keys");
660 if (keys
== MODEKEY_EMACS
)
661 mode_key_init(&data
->mdata
, &mode_key_tree_emacs_copy
);
663 mode_key_init(&data
->mdata
, &mode_key_tree_vi_copy
);
665 data
->inputtype
= WINDOW_COPY_OFF
;
666 data
->inputprompt
= NULL
;
668 window_copy_redraw_lines(wp
, screen_size_y(s
) - 1, 1);
672 window_copy_key_input(struct window_pane
*wp
, int key
)
674 struct window_copy_mode_data
*data
= wp
->modedata
;
675 struct screen
*s
= &data
->screen
;
679 switch (mode_key_lookup(&data
->mdata
, key
)) {
680 case MODEKEYEDIT_CANCEL
:
683 case MODEKEYEDIT_BACKSPACE
:
684 inputlen
= strlen(data
->inputstr
);
686 data
->inputstr
[inputlen
- 1] = '\0';
688 case MODEKEYEDIT_DELETELINE
:
689 *data
->inputstr
= '\0';
691 case MODEKEYEDIT_ENTER
:
692 np
= data
->numprefix
;
696 switch (data
->inputtype
) {
697 case WINDOW_COPY_OFF
:
698 case WINDOW_COPY_JUMPFORWARD
:
699 case WINDOW_COPY_JUMPBACK
:
700 case WINDOW_COPY_NUMERICPREFIX
:
702 case WINDOW_COPY_SEARCHUP
:
703 for (; np
!= 0; np
--)
704 window_copy_search_up(wp
, data
->inputstr
);
705 data
->searchtype
= data
->inputtype
;
706 data
->searchstr
= xstrdup(data
->inputstr
);
708 case WINDOW_COPY_SEARCHDOWN
:
709 for (; np
!= 0; np
--)
710 window_copy_search_down(wp
, data
->inputstr
);
711 data
->searchtype
= data
->inputtype
;
712 data
->searchstr
= xstrdup(data
->inputstr
);
714 case WINDOW_COPY_GOTOLINE
:
715 window_copy_goto_line(wp
, data
->inputstr
);
716 *data
->inputstr
= '\0';
722 if (key
< 32 || key
> 126)
724 inputlen
= strlen(data
->inputstr
) + 2;
726 data
->inputstr
= xrealloc(data
->inputstr
, 1, inputlen
);
727 data
->inputstr
[inputlen
- 2] = key
;
728 data
->inputstr
[inputlen
- 1] = '\0';
734 window_copy_redraw_lines(wp
, screen_size_y(s
) - 1, 1);
739 window_copy_key_numeric_prefix(struct window_pane
*wp
, int key
)
741 struct window_copy_mode_data
*data
= wp
->modedata
;
742 struct screen
*s
= &data
->screen
;
744 key
&= KEYC_MASK_KEY
;
745 if (key
< '0' || key
> '9')
748 if (data
->numprefix
>= 100) /* no more than three digits */
750 data
->numprefix
= data
->numprefix
* 10 + key
- '0';
752 window_copy_redraw_lines(wp
, screen_size_y(s
) - 1, 1);
759 struct window_pane
*wp
, unused
struct session
*sess
, struct mouse_event
*m
)
761 struct window_copy_mode_data
*data
= wp
->modedata
;
762 struct screen
*s
= &data
->screen
;
765 if (m
->x
>= screen_size_x(s
))
767 if (m
->y
>= screen_size_y(s
))
770 /* If mouse wheel (buttons 4 and 5), scroll. */
771 if ((m
->b
& MOUSE_45
)) {
772 if ((m
->b
& MOUSE_BUTTON
) == MOUSE_1
) {
773 for (i
= 0; i
< 5; i
++)
774 window_copy_cursor_up(wp
, 0);
775 } else if ((m
->b
& MOUSE_BUTTON
) == MOUSE_2
) {
776 for (i
= 0; i
< 5; i
++)
777 window_copy_cursor_down(wp
, 0);
783 * If already reading motion, move the cursor while buttons are still
784 * pressed, or stop the selection on their release.
786 if (s
->mode
& MODE_MOUSEMOTION
) {
787 if ((m
->b
& MOUSE_BUTTON
) != MOUSE_UP
) {
788 window_copy_update_cursor(wp
, m
->x
, m
->y
);
789 if (window_copy_update_selection(wp
))
790 window_copy_redraw_screen(wp
);
792 s
->mode
&= ~MODE_MOUSEMOTION
;
794 window_copy_copy_selection(wp
, sess
);
795 window_pane_reset_mode(wp
);
801 /* Otherwise i other buttons pressed, start selection and motion. */
802 if ((m
->b
& MOUSE_BUTTON
) != MOUSE_UP
) {
803 s
->mode
|= MODE_MOUSEMOTION
;
805 window_copy_update_cursor(wp
, m
->x
, m
->y
);
806 window_copy_start_selection(wp
);
807 window_copy_redraw_screen(wp
);
812 window_copy_scroll_to(struct window_pane
*wp
, u_int px
, u_int py
)
814 struct window_copy_mode_data
*data
= wp
->modedata
;
815 struct grid
*gd
= data
->backing
->grid
;
824 } else if (py
> gd
->hsize
+ gd
->sy
- gap
) {
826 data
->cy
= py
- gd
->hsize
;
828 offset
= py
+ gap
- gd
->sy
;
829 data
->cy
= py
- offset
;
831 data
->oy
= gd
->hsize
- offset
;
833 window_copy_update_selection(wp
);
834 window_copy_redraw_screen(wp
);
838 window_copy_search_compare(
839 struct grid
*gd
, u_int px
, u_int py
, struct grid
*sgd
, u_int spx
)
841 const struct grid_cell
*gc
, *sgc
;
842 const struct grid_utf8
*gu
, *sgu
;
844 gc
= grid_peek_cell(gd
, px
, py
);
845 sgc
= grid_peek_cell(sgd
, spx
, 0);
847 if ((gc
->flags
& GRID_FLAG_UTF8
) != (sgc
->flags
& GRID_FLAG_UTF8
))
850 if (gc
->flags
& GRID_FLAG_UTF8
) {
851 gu
= grid_peek_utf8(gd
, px
, py
);
852 sgu
= grid_peek_utf8(sgd
, spx
, 0);
853 if (grid_utf8_compare(gu
, sgu
))
856 if (gc
->data
== sgc
->data
)
863 window_copy_search_lr(struct grid
*gd
,
864 struct grid
*sgd
, u_int
*ppx
, u_int py
, u_int first
, u_int last
)
868 for (ax
= first
; ax
< last
; ax
++) {
869 if (ax
+ sgd
->sx
>= gd
->sx
)
871 for (bx
= 0; bx
< sgd
->sx
; bx
++) {
873 if (!window_copy_search_compare(gd
, px
, py
, sgd
, bx
))
885 window_copy_search_rl(struct grid
*gd
,
886 struct grid
*sgd
, u_int
*ppx
, u_int py
, u_int first
, u_int last
)
890 for (ax
= last
+ 1; ax
> first
; ax
--) {
891 if (gd
->sx
- (ax
- 1) < sgd
->sx
)
893 for (bx
= 0; bx
< sgd
->sx
; bx
++) {
895 if (!window_copy_search_compare(gd
, px
, py
, sgd
, bx
))
907 window_copy_search_up(struct window_pane
*wp
, const char *searchstr
)
909 struct window_copy_mode_data
*data
= wp
->modedata
;
910 struct screen
*s
= data
->backing
, ss
;
911 struct screen_write_ctx ctx
;
912 struct grid
*gd
= s
->grid
, *sgd
;
915 u_int i
, last
, fx
, fy
, px
;
916 int utf8flag
, n
, wrapped
;
918 if (*searchstr
== '\0')
920 utf8flag
= options_get_number(&wp
->window
->options
, "utf8");
921 searchlen
= screen_write_strlen(utf8flag
, "%s", searchstr
);
923 screen_init(&ss
, searchlen
, 1, 0);
924 screen_write_start(&ctx
, NULL
, &ss
);
925 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
926 screen_write_nputs(&ctx
, -1, &gc
, utf8flag
, "%s", searchstr
);
927 screen_write_stop(&ctx
);
930 fy
= gd
->hsize
- data
->oy
+ data
->cy
;
943 for (i
= fy
+ 1; i
> 0; i
--) {
944 last
= screen_size_x(s
);
947 n
= window_copy_search_rl(gd
, sgd
, &px
, i
- 1, 0, last
);
949 window_copy_scroll_to(wp
, px
, i
- 1);
953 if (!n
&& !wrapped
) {
955 fy
= gd
->hsize
+ gd
->sy
- 1;
964 window_copy_search_down(struct window_pane
*wp
, const char *searchstr
)
966 struct window_copy_mode_data
*data
= wp
->modedata
;
967 struct screen
*s
= data
->backing
, ss
;
968 struct screen_write_ctx ctx
;
969 struct grid
*gd
= s
->grid
, *sgd
;
972 u_int i
, first
, fx
, fy
, px
;
973 int utf8flag
, n
, wrapped
;
975 if (*searchstr
== '\0')
977 utf8flag
= options_get_number(&wp
->window
->options
, "utf8");
978 searchlen
= screen_write_strlen(utf8flag
, "%s", searchstr
);
980 screen_init(&ss
, searchlen
, 1, 0);
981 screen_write_start(&ctx
, NULL
, &ss
);
982 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
983 screen_write_nputs(&ctx
, -1, &gc
, utf8flag
, "%s", searchstr
);
984 screen_write_stop(&ctx
);
987 fy
= gd
->hsize
- data
->oy
+ data
->cy
;
989 if (fx
== gd
->sx
- 1) {
990 if (fy
== gd
->hsize
+ gd
->sy
)
1000 for (i
= fy
+ 1; i
< gd
->hsize
+ gd
->sy
; i
++) {
1004 n
= window_copy_search_lr(gd
, sgd
, &px
, i
- 1, first
, gd
->sx
);
1006 window_copy_scroll_to(wp
, px
, i
- 1);
1010 if (!n
&& !wrapped
) {
1021 window_copy_goto_line(struct window_pane
*wp
, const char *linestr
)
1023 struct window_copy_mode_data
*data
= wp
->modedata
;
1027 lineno
= strtonum(linestr
, 0, screen_hsize(data
->backing
), &errstr
);
1032 window_copy_update_selection(wp
);
1033 window_copy_redraw_screen(wp
);
1037 window_copy_write_line(
1038 struct window_pane
*wp
, struct screen_write_ctx
*ctx
, u_int py
)
1040 struct window_copy_mode_data
*data
= wp
->modedata
;
1041 struct screen
*s
= &data
->screen
;
1042 struct options
*oo
= &wp
->window
->options
;
1043 struct grid_cell gc
;
1045 size_t last
, xoff
= 0, size
= 0;
1047 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
1048 colour_set_fg(&gc
, options_get_number(oo
, "mode-fg"));
1049 colour_set_bg(&gc
, options_get_number(oo
, "mode-bg"));
1050 gc
.attr
|= options_get_number(oo
, "mode-attr");
1052 last
= screen_size_y(s
) - 1;
1054 size
= xsnprintf(hdr
, sizeof hdr
,
1055 "[%u/%u]", data
->oy
, screen_hsize(data
->backing
));
1056 screen_write_cursormove(ctx
, screen_size_x(s
) - size
, 0);
1057 screen_write_puts(ctx
, &gc
, "%s", hdr
);
1058 } else if (py
== last
&& data
->inputtype
!= WINDOW_COPY_OFF
) {
1059 if (data
->inputtype
== WINDOW_COPY_NUMERICPREFIX
) {
1060 xoff
= size
= xsnprintf(hdr
, sizeof hdr
,
1061 "Repeat: %u", data
->numprefix
);
1063 xoff
= size
= xsnprintf(hdr
, sizeof hdr
,
1064 "%s: %s", data
->inputprompt
, data
->inputstr
);
1066 screen_write_cursormove(ctx
, 0, last
);
1067 screen_write_puts(ctx
, &gc
, "%s", hdr
);
1071 screen_write_cursormove(ctx
, xoff
, py
);
1072 screen_write_copy(ctx
, data
->backing
, xoff
,
1073 (screen_hsize(data
->backing
) - data
->oy
) + py
,
1074 screen_size_x(s
) - size
, 1);
1076 if (py
== data
->cy
&& data
->cx
== screen_size_x(s
)) {
1077 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
1078 screen_write_cursormove(ctx
, screen_size_x(s
) - 1, py
);
1079 screen_write_putc(ctx
, &gc
, '$');
1084 window_copy_write_lines(
1085 struct window_pane
*wp
, struct screen_write_ctx
*ctx
, u_int py
, u_int ny
)
1089 for (yy
= py
; yy
< py
+ ny
; yy
++)
1090 window_copy_write_line(wp
, ctx
, py
);
1094 window_copy_redraw_lines(struct window_pane
*wp
, u_int py
, u_int ny
)
1096 struct window_copy_mode_data
*data
= wp
->modedata
;
1097 struct screen_write_ctx ctx
;
1100 screen_write_start(&ctx
, wp
, NULL
);
1101 for (i
= py
; i
< py
+ ny
; i
++)
1102 window_copy_write_line(wp
, &ctx
, i
);
1103 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
);
1104 screen_write_stop(&ctx
);
1108 window_copy_redraw_screen(struct window_pane
*wp
)
1110 struct window_copy_mode_data
*data
= wp
->modedata
;
1112 window_copy_redraw_lines(wp
, 0, screen_size_y(&data
->screen
));
1116 window_copy_update_cursor(struct window_pane
*wp
, u_int cx
, u_int cy
)
1118 struct window_copy_mode_data
*data
= wp
->modedata
;
1119 struct screen
*s
= &data
->screen
;
1120 struct screen_write_ctx ctx
;
1121 u_int old_cx
, old_cy
;
1123 old_cx
= data
->cx
; old_cy
= data
->cy
;
1124 data
->cx
= cx
; data
->cy
= cy
;
1125 if (old_cx
== screen_size_x(s
))
1126 window_copy_redraw_lines(wp
, old_cy
, 1);
1127 if (data
->cx
== screen_size_x(s
))
1128 window_copy_redraw_lines(wp
, data
->cy
, 1);
1130 screen_write_start(&ctx
, wp
, NULL
);
1131 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
);
1132 screen_write_stop(&ctx
);
1137 window_copy_start_selection(struct window_pane
*wp
)
1139 struct window_copy_mode_data
*data
= wp
->modedata
;
1140 struct screen
*s
= &data
->screen
;
1142 data
->selx
= data
->cx
;
1143 data
->sely
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1146 window_copy_update_selection(wp
);
1150 window_copy_update_selection(struct window_pane
*wp
)
1152 struct window_copy_mode_data
*data
= wp
->modedata
;
1153 struct screen
*s
= &data
->screen
;
1154 struct options
*oo
= &wp
->window
->options
;
1155 struct grid_cell gc
;
1156 u_int sx
, sy
, ty
, cy
;
1162 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
1163 colour_set_fg(&gc
, options_get_number(oo
, "mode-fg"));
1164 colour_set_bg(&gc
, options_get_number(oo
, "mode-bg"));
1165 gc
.attr
|= options_get_number(oo
, "mode-attr");
1167 /* Find top of screen. */
1168 ty
= screen_hsize(data
->backing
) - data
->oy
;
1170 /* Adjust the selection. */
1173 if (sy
< ty
) { /* above screen */
1174 if (!data
->rectflag
)
1177 } else if (sy
> ty
+ screen_size_y(s
) - 1) { /* below screen */
1178 if (!data
->rectflag
)
1179 sx
= screen_size_x(s
) - 1;
1180 sy
= screen_size_y(s
) - 1;
1183 sy
= screen_hsize(s
) + sy
;
1185 screen_set_selection(s
,
1186 sx
, sy
, data
->cx
, screen_hsize(s
) + data
->cy
, data
->rectflag
, &gc
);
1188 if (data
->rectflag
) {
1190 * Can't rely on the caller to redraw the right lines for
1191 * rectangle selection - find the highest line and the number
1192 * of lines, and redraw just past that in both directions
1196 window_copy_redraw_lines(wp
, sy
, cy
- sy
+ 1);
1198 window_copy_redraw_lines(wp
, cy
, sy
- cy
+ 1);
1205 window_copy_copy_selection(struct window_pane
*wp
, struct session
*sess
)
1207 struct window_copy_mode_data
*data
= wp
->modedata
;
1208 struct screen
*s
= &data
->screen
;
1211 u_int i
, xx
, yy
, sx
, sy
, ex
, ey
, limit
;
1212 u_int firstsx
, lastex
, restex
, restsx
;
1223 * The selection extends from selx,sely to (adjusted) cx,cy on
1227 /* Find start and end. */
1229 yy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1230 if (yy
< data
->sely
|| (yy
== data
->sely
&& xx
< data
->selx
)) {
1232 ex
= data
->selx
; ey
= data
->sely
;
1234 sx
= data
->selx
; sy
= data
->sely
;
1238 /* Trim ex to end of line. */
1239 xx
= window_copy_find_length(wp
, ey
);
1244 * Deal with rectangle-copy if necessary; four situations: start of
1245 * first line (firstsx), end of last line (lastex), start (restsx) and
1246 * end (restex) of all other lines.
1248 xx
= screen_size_x(s
);
1249 if (data
->rectflag
) {
1251 * Need to ignore the column with the cursor in it, which for
1252 * rectangular copy means knowing which side the cursor is on.
1254 if (data
->selx
< data
->cx
) {
1255 /* Selection start is on the left. */
1258 firstsx
= data
->selx
;
1259 restsx
= data
->selx
;
1261 /* Cursor is on the left. */
1262 lastex
= data
->selx
+ 1;
1263 restex
= data
->selx
+ 1;
1264 firstsx
= data
->cx
+ 1;
1265 restsx
= data
->cx
+ 1;
1269 * Like emacs, keep the top-left-most character, and drop the
1270 * bottom-right-most, regardless of copy direction.
1278 /* Copy the lines. */
1280 window_copy_copy_line(wp
, &buf
, &off
, sy
, firstsx
, lastex
);
1282 window_copy_copy_line(wp
, &buf
, &off
, sy
, firstsx
, restex
);
1284 for (i
= sy
+ 1; i
< ey
; i
++) {
1285 window_copy_copy_line(
1286 wp
, &buf
, &off
, i
, restsx
, restex
);
1289 window_copy_copy_line(wp
, &buf
, &off
, ey
, restsx
, lastex
);
1292 /* Don't bother if no data. */
1297 off
--; /* remove final \n */
1299 /* Add the buffer to the stack. */
1300 limit
= options_get_number(&sess
->options
, "buffer-limit");
1301 paste_add(&sess
->buffers
, buf
, off
, limit
);
1305 window_copy_copy_line(struct window_pane
*wp
,
1306 char **buf
, size_t *off
, u_int sy
, u_int sx
, u_int ex
)
1308 struct window_copy_mode_data
*data
= wp
->modedata
;
1309 struct grid
*gd
= data
->backing
->grid
;
1310 const struct grid_cell
*gc
;
1311 const struct grid_utf8
*gu
;
1312 struct grid_line
*gl
;
1313 u_int i
, xx
, wrapped
= 0;
1320 * Work out if the line was wrapped at the screen edge and all of it is
1323 gl
= &gd
->linedata
[sy
];
1324 if (gl
->flags
& GRID_LINE_WRAPPED
&& gl
->cellsize
<= gd
->sx
)
1327 /* If the line was wrapped, don't strip spaces (use the full length). */
1331 xx
= window_copy_find_length(wp
, sy
);
1338 for (i
= sx
; i
< ex
; i
++) {
1339 gc
= grid_peek_cell(gd
, i
, sy
);
1340 if (gc
->flags
& GRID_FLAG_PADDING
)
1342 if (!(gc
->flags
& GRID_FLAG_UTF8
)) {
1343 *buf
= xrealloc(*buf
, 1, (*off
) + 1);
1344 (*buf
)[(*off
)++] = gc
->data
;
1346 gu
= grid_peek_utf8(gd
, i
, sy
);
1347 size
= grid_utf8_size(gu
);
1348 *buf
= xrealloc(*buf
, 1, (*off
) + size
);
1349 *off
+= grid_utf8_copy(gu
, *buf
+ *off
, size
);
1354 /* Only add a newline if the line wasn't wrapped. */
1355 if (!wrapped
|| ex
!= xx
) {
1356 *buf
= xrealloc(*buf
, 1, (*off
) + 1);
1357 (*buf
)[(*off
)++] = '\n';
1362 window_copy_clear_selection(struct window_pane
*wp
)
1364 struct window_copy_mode_data
*data
= wp
->modedata
;
1367 screen_clear_selection(&data
->screen
);
1369 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1370 px
= window_copy_find_length(wp
, py
);
1372 window_copy_update_cursor(wp
, px
, data
->cy
);
1376 window_copy_in_set(struct window_pane
*wp
, u_int px
, u_int py
, const char *set
)
1378 struct window_copy_mode_data
*data
= wp
->modedata
;
1379 const struct grid_cell
*gc
;
1381 gc
= grid_peek_cell(data
->backing
->grid
, px
, py
);
1382 if (gc
->flags
& (GRID_FLAG_PADDING
|GRID_FLAG_UTF8
))
1384 if (gc
->data
== 0x00 || gc
->data
== 0x7f)
1386 return (strchr(set
, gc
->data
) != NULL
);
1390 window_copy_find_length(struct window_pane
*wp
, u_int py
)
1392 struct window_copy_mode_data
*data
= wp
->modedata
;
1393 struct screen
*s
= data
->backing
;
1394 const struct grid_cell
*gc
;
1398 * If the pane has been resized, its grid can contain old overlong
1399 * lines. grid_peek_cell does not allow accessing cells beyond the
1400 * width of the grid, and screen_write_copy treats them as spaces, so
1401 * ignore them here too.
1403 px
= s
->grid
->linedata
[py
].cellsize
;
1404 if (px
> screen_size_x(s
))
1405 px
= screen_size_x(s
);
1407 gc
= grid_peek_cell(s
->grid
, px
- 1, py
);
1408 if (gc
->flags
& GRID_FLAG_UTF8
)
1410 if (gc
->data
!= ' ')
1418 window_copy_cursor_start_of_line(struct window_pane
*wp
)
1420 struct window_copy_mode_data
*data
= wp
->modedata
;
1421 struct screen
*back_s
= data
->backing
;
1422 struct grid
*gd
= back_s
->grid
;
1425 if (data
->cx
== 0) {
1426 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1427 while (py
> 0 && gd
->linedata
[py
-1].flags
& GRID_LINE_WRAPPED
) {
1428 window_copy_cursor_up(wp
, 0);
1429 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1432 window_copy_update_cursor(wp
, 0, data
->cy
);
1433 if (window_copy_update_selection(wp
))
1434 window_copy_redraw_lines(wp
, data
->cy
, 1);
1438 window_copy_cursor_back_to_indentation(struct window_pane
*wp
)
1440 struct window_copy_mode_data
*data
= wp
->modedata
;
1442 const struct grid_cell
*gc
;
1445 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1446 xx
= window_copy_find_length(wp
, py
);
1449 gc
= grid_peek_cell(data
->backing
->grid
, px
, py
);
1450 if (gc
->flags
& GRID_FLAG_UTF8
)
1452 if (gc
->data
!= ' ')
1457 window_copy_update_cursor(wp
, px
, data
->cy
);
1458 if (window_copy_update_selection(wp
))
1459 window_copy_redraw_lines(wp
, data
->cy
, 1);
1463 window_copy_cursor_end_of_line(struct window_pane
*wp
)
1465 struct window_copy_mode_data
*data
= wp
->modedata
;
1466 struct screen
*back_s
= data
->backing
;
1467 struct grid
*gd
= back_s
->grid
;
1470 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1471 px
= window_copy_find_length(wp
, py
);
1473 if (data
->cx
== px
) {
1474 if (data
->screen
.sel
.flag
&& data
->rectflag
)
1475 px
= screen_size_x(back_s
);
1476 if (gd
->linedata
[py
].flags
& GRID_LINE_WRAPPED
) {
1477 while (py
< gd
->sy
+ gd
->hsize
&&
1478 gd
->linedata
[py
].flags
& GRID_LINE_WRAPPED
) {
1479 window_copy_cursor_down(wp
, 0);
1480 py
= screen_hsize(back_s
)
1481 + data
->cy
- data
->oy
;
1483 px
= window_copy_find_length(wp
, py
);
1486 window_copy_update_cursor(wp
, px
, data
->cy
);
1488 if (window_copy_update_selection(wp
))
1489 window_copy_redraw_lines(wp
, data
->cy
, 1);
1493 window_copy_cursor_left(struct window_pane
*wp
)
1495 struct window_copy_mode_data
*data
= wp
->modedata
;
1497 if (data
->cx
== 0) {
1498 window_copy_cursor_up(wp
, 0);
1499 window_copy_cursor_end_of_line(wp
);
1501 window_copy_update_cursor(wp
, data
->cx
- 1, data
->cy
);
1502 if (window_copy_update_selection(wp
))
1503 window_copy_redraw_lines(wp
, data
->cy
, 1);
1508 window_copy_cursor_right(struct window_pane
*wp
)
1510 struct window_copy_mode_data
*data
= wp
->modedata
;
1513 if (data
->screen
.sel
.flag
&& data
->rectflag
)
1514 px
= screen_size_x(&data
->screen
);
1516 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1517 px
= window_copy_find_length(wp
, py
);
1520 if (data
->cx
>= px
) {
1521 window_copy_cursor_start_of_line(wp
);
1522 window_copy_cursor_down(wp
, 0);
1524 window_copy_update_cursor(wp
, data
->cx
+ 1, data
->cy
);
1525 if (window_copy_update_selection(wp
))
1526 window_copy_redraw_lines(wp
, data
->cy
, 1);
1531 window_copy_cursor_up(struct window_pane
*wp
, int scroll_only
)
1533 struct window_copy_mode_data
*data
= wp
->modedata
;
1534 struct screen
*s
= &data
->screen
;
1535 u_int ox
, oy
, px
, py
;
1537 oy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1538 ox
= window_copy_find_length(wp
, oy
);
1540 data
->lastcx
= data
->cx
;
1544 data
->cx
= data
->lastcx
;
1545 if (scroll_only
|| data
->cy
== 0) {
1546 window_copy_scroll_down(wp
, 1);
1548 if (data
->cy
== screen_size_y(s
) - 1)
1549 window_copy_redraw_lines(wp
, data
->cy
, 1);
1551 window_copy_redraw_lines(wp
, data
->cy
, 2);
1554 window_copy_update_cursor(wp
, data
->cx
, data
->cy
- 1);
1555 if (window_copy_update_selection(wp
)) {
1556 if (data
->cy
== screen_size_y(s
) - 1)
1557 window_copy_redraw_lines(wp
, data
->cy
, 1);
1559 window_copy_redraw_lines(wp
, data
->cy
, 2);
1563 if (!data
->screen
.sel
.flag
|| !data
->rectflag
) {
1564 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1565 px
= window_copy_find_length(wp
, py
);
1566 if ((data
->cx
>= data
->lastsx
&& data
->cx
!= px
) ||
1568 window_copy_cursor_end_of_line(wp
);
1573 window_copy_cursor_down(struct window_pane
*wp
, int scroll_only
)
1575 struct window_copy_mode_data
*data
= wp
->modedata
;
1576 struct screen
*s
= &data
->screen
;
1577 u_int ox
, oy
, px
, py
;
1579 oy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1580 ox
= window_copy_find_length(wp
, oy
);
1582 data
->lastcx
= data
->cx
;
1586 data
->cx
= data
->lastcx
;
1587 if (scroll_only
|| data
->cy
== screen_size_y(s
) - 1) {
1588 window_copy_scroll_up(wp
, 1);
1589 if (scroll_only
&& data
->cy
> 0)
1590 window_copy_redraw_lines(wp
, data
->cy
- 1, 2);
1592 window_copy_update_cursor(wp
, data
->cx
, data
->cy
+ 1);
1593 if (window_copy_update_selection(wp
))
1594 window_copy_redraw_lines(wp
, data
->cy
- 1, 2);
1597 if (!data
->screen
.sel
.flag
|| !data
->rectflag
) {
1598 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1599 px
= window_copy_find_length(wp
, py
);
1600 if ((data
->cx
>= data
->lastsx
&& data
->cx
!= px
) ||
1602 window_copy_cursor_end_of_line(wp
);
1607 window_copy_cursor_jump(struct window_pane
*wp
)
1609 struct window_copy_mode_data
*data
= wp
->modedata
;
1610 struct screen
*back_s
= data
->backing
;
1611 const struct grid_cell
*gc
;
1615 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1616 xx
= window_copy_find_length(wp
, py
);
1619 gc
= grid_peek_cell(back_s
->grid
, px
, py
);
1620 if ((gc
->flags
& (GRID_FLAG_PADDING
|GRID_FLAG_UTF8
)) == 0
1621 && gc
->data
== data
->jumpchar
) {
1623 window_copy_update_cursor(wp
, px
, data
->cy
);
1624 if (window_copy_update_selection(wp
))
1625 window_copy_redraw_lines(wp
, data
->cy
, 1);
1633 window_copy_cursor_jump_back(struct window_pane
*wp
)
1635 struct window_copy_mode_data
*data
= wp
->modedata
;
1636 struct screen
*back_s
= data
->backing
;
1637 const struct grid_cell
*gc
;
1641 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1647 gc
= grid_peek_cell(back_s
->grid
, px
, py
);
1648 if ((gc
->flags
& (GRID_FLAG_PADDING
|GRID_FLAG_UTF8
)) == 0
1649 && gc
->data
== data
->jumpchar
) {
1651 window_copy_update_cursor(wp
, px
, data
->cy
);
1652 if (window_copy_update_selection(wp
))
1653 window_copy_redraw_lines(wp
, data
->cy
, 1);
1663 window_copy_cursor_next_word(struct window_pane
*wp
, const char *separators
)
1665 struct window_copy_mode_data
*data
= wp
->modedata
;
1666 struct screen
*back_s
= data
->backing
;
1667 u_int px
, py
, xx
, yy
;
1671 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1672 xx
= window_copy_find_length(wp
, py
);
1673 yy
= screen_hsize(back_s
) + screen_size_y(back_s
) - 1;
1676 * First skip past any nonword characters and then any word characters.
1678 * expected is initially set to 0 for the former and then 1 for the
1683 window_copy_in_set(wp
, px
, py
, separators
) == expected
) {
1684 /* Move down if we're past the end of the line. */
1688 window_copy_cursor_down(wp
, 0);
1691 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1692 xx
= window_copy_find_length(wp
, py
);
1696 expected
= !expected
;
1697 } while (expected
== 1);
1699 window_copy_update_cursor(wp
, px
, data
->cy
);
1700 if (window_copy_update_selection(wp
))
1701 window_copy_redraw_lines(wp
, data
->cy
, 1);
1705 window_copy_cursor_next_word_end(struct window_pane
*wp
, const char *separators
)
1707 struct window_copy_mode_data
*data
= wp
->modedata
;
1708 struct screen
*back_s
= data
->backing
;
1709 u_int px
, py
, xx
, yy
;
1713 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1714 xx
= window_copy_find_length(wp
, py
);
1715 yy
= screen_hsize(back_s
) + screen_size_y(back_s
) - 1;
1718 * First skip past any word characters, then any nonword characters.
1720 * expected is initially set to 1 for the former and then 0 for the
1725 window_copy_in_set(wp
, px
, py
, separators
) == expected
) {
1726 /* Move down if we're past the end of the line. */
1730 window_copy_cursor_down(wp
, 0);
1733 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1734 xx
= window_copy_find_length(wp
, py
);
1738 expected
= !expected
;
1739 } while (expected
== 0);
1741 window_copy_update_cursor(wp
, px
, data
->cy
);
1742 if (window_copy_update_selection(wp
))
1743 window_copy_redraw_lines(wp
, data
->cy
, 1);
1746 /* Move to the previous place where a word begins. */
1748 window_copy_cursor_previous_word(struct window_pane
*wp
, const char *separators
)
1750 struct window_copy_mode_data
*data
= wp
->modedata
;
1754 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1756 /* Move back to the previous word character. */
1760 if (!window_copy_in_set(wp
, px
, py
, separators
))
1763 if (data
->cy
== 0 &&
1764 (screen_hsize(data
->backing
) == 0 ||
1765 data
->oy
>= screen_hsize(data
->backing
) - 1))
1767 window_copy_cursor_up(wp
, 0);
1769 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1770 px
= window_copy_find_length(wp
, py
);
1774 /* Move back to the beginning of this word. */
1775 while (px
> 0 && !window_copy_in_set(wp
, px
- 1, py
, separators
))
1779 window_copy_update_cursor(wp
, px
, data
->cy
);
1780 if (window_copy_update_selection(wp
))
1781 window_copy_redraw_lines(wp
, data
->cy
, 1);
1785 window_copy_scroll_up(struct window_pane
*wp
, u_int ny
)
1787 struct window_copy_mode_data
*data
= wp
->modedata
;
1788 struct screen
*s
= &data
->screen
;
1789 struct screen_write_ctx ctx
;
1797 screen_write_start(&ctx
, wp
, NULL
);
1798 screen_write_cursormove(&ctx
, 0, 0);
1799 screen_write_deleteline(&ctx
, ny
);
1800 window_copy_write_lines(wp
, &ctx
, screen_size_y(s
) - ny
, ny
);
1801 window_copy_write_line(wp
, &ctx
, 0);
1802 if (screen_size_y(s
) > 1)
1803 window_copy_write_line(wp
, &ctx
, 1);
1804 if (screen_size_y(s
) > 3)
1805 window_copy_write_line(wp
, &ctx
, screen_size_y(s
) - 2);
1806 if (s
->sel
.flag
&& screen_size_y(s
) > ny
) {
1807 window_copy_update_selection(wp
);
1808 window_copy_write_line(wp
, &ctx
, screen_size_y(s
) - ny
- 1);
1810 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
);
1811 window_copy_update_selection(wp
);
1812 screen_write_stop(&ctx
);
1816 window_copy_scroll_down(struct window_pane
*wp
, u_int ny
)
1818 struct window_copy_mode_data
*data
= wp
->modedata
;
1819 struct screen
*s
= &data
->screen
;
1820 struct screen_write_ctx ctx
;
1822 if (ny
> screen_hsize(data
->backing
))
1825 if (data
->oy
> screen_hsize(data
->backing
) - ny
)
1826 ny
= screen_hsize(data
->backing
) - data
->oy
;
1831 screen_write_start(&ctx
, wp
, NULL
);
1832 screen_write_cursormove(&ctx
, 0, 0);
1833 screen_write_insertline(&ctx
, ny
);
1834 window_copy_write_lines(wp
, &ctx
, 0, ny
);
1835 if (s
->sel
.flag
&& screen_size_y(s
) > ny
) {
1836 window_copy_update_selection(wp
);
1837 window_copy_write_line(wp
, &ctx
, ny
);
1838 } else if (ny
== 1) /* nuke position */
1839 window_copy_write_line(wp
, &ctx
, 1);
1840 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
);
1841 window_copy_update_selection(wp
);
1842 screen_write_stop(&ctx
);
1846 window_copy_rectangle_toggle(struct window_pane
*wp
)
1848 struct window_copy_mode_data
*data
= wp
->modedata
;
1851 data
->rectflag
= !data
->rectflag
;
1853 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1854 px
= window_copy_find_length(wp
, py
);
1856 window_copy_update_cursor(wp
, px
, data
->cy
);
1858 window_copy_update_selection(wp
);
1859 window_copy_redraw_screen(wp
);