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
;
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_STANDARD
;
185 keys
= options_get_number(&wp
->window
->options
, "mode-keys");
186 if (keys
== MODEKEY_EMACS
)
187 mode_key_init(&data
->mdata
, &mode_key_tree_emacs_copy
);
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
);
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_MOUSE_ANY
) {
791 if ((m
->b
& MOUSE_BUTTON
) != MOUSE_UP
) {
792 window_copy_update_cursor(wp
, m
->x
, m
->y
);
793 if (window_copy_update_selection(wp
))
794 window_copy_redraw_screen(wp
);
796 s
->mode
&= ~MODE_MOUSE_ANY
;
797 s
->mode
|= MODE_MOUSE_STANDARD
;
799 window_copy_copy_selection(wp
);
800 window_pane_reset_mode(wp
);
806 /* Otherwise if other buttons pressed, start selection and motion. */
807 if ((m
->b
& MOUSE_BUTTON
) != MOUSE_UP
) {
808 s
->mode
&= ~MODE_MOUSE_STANDARD
;
809 s
->mode
|= MODE_MOUSE_ANY
;
811 window_copy_update_cursor(wp
, m
->x
, m
->y
);
812 window_copy_start_selection(wp
);
813 window_copy_redraw_screen(wp
);
818 window_copy_scroll_to(struct window_pane
*wp
, u_int px
, u_int py
)
820 struct window_copy_mode_data
*data
= wp
->modedata
;
821 struct grid
*gd
= data
->backing
->grid
;
830 } else if (py
> gd
->hsize
+ gd
->sy
- gap
) {
832 data
->cy
= py
- gd
->hsize
;
834 offset
= py
+ gap
- gd
->sy
;
835 data
->cy
= py
- offset
;
837 data
->oy
= gd
->hsize
- offset
;
839 window_copy_update_selection(wp
);
840 window_copy_redraw_screen(wp
);
844 window_copy_search_compare(
845 struct grid
*gd
, u_int px
, u_int py
, struct grid
*sgd
, u_int spx
)
847 const struct grid_cell
*gc
, *sgc
;
848 const struct grid_utf8
*gu
, *sgu
;
850 gc
= grid_peek_cell(gd
, px
, py
);
851 sgc
= grid_peek_cell(sgd
, spx
, 0);
853 if ((gc
->flags
& GRID_FLAG_UTF8
) != (sgc
->flags
& GRID_FLAG_UTF8
))
856 if (gc
->flags
& GRID_FLAG_UTF8
) {
857 gu
= grid_peek_utf8(gd
, px
, py
);
858 sgu
= grid_peek_utf8(sgd
, spx
, 0);
859 if (grid_utf8_compare(gu
, sgu
))
862 if (gc
->data
== sgc
->data
)
869 window_copy_search_lr(struct grid
*gd
,
870 struct grid
*sgd
, u_int
*ppx
, u_int py
, u_int first
, u_int last
)
874 for (ax
= first
; ax
< last
; ax
++) {
875 if (ax
+ sgd
->sx
>= gd
->sx
)
877 for (bx
= 0; bx
< sgd
->sx
; bx
++) {
879 if (!window_copy_search_compare(gd
, px
, py
, sgd
, bx
))
891 window_copy_search_rl(struct grid
*gd
,
892 struct grid
*sgd
, u_int
*ppx
, u_int py
, u_int first
, u_int last
)
896 for (ax
= last
+ 1; ax
> first
; ax
--) {
897 if (gd
->sx
- (ax
- 1) < sgd
->sx
)
899 for (bx
= 0; bx
< sgd
->sx
; bx
++) {
901 if (!window_copy_search_compare(gd
, px
, py
, sgd
, bx
))
913 window_copy_search_up(struct window_pane
*wp
, const char *searchstr
)
915 struct window_copy_mode_data
*data
= wp
->modedata
;
916 struct screen
*s
= data
->backing
, ss
;
917 struct screen_write_ctx ctx
;
918 struct grid
*gd
= s
->grid
, *sgd
;
921 u_int i
, last
, fx
, fy
, px
;
922 int utf8flag
, n
, wrapped
;
924 if (*searchstr
== '\0')
926 utf8flag
= options_get_number(&wp
->window
->options
, "utf8");
927 searchlen
= screen_write_strlen(utf8flag
, "%s", searchstr
);
929 screen_init(&ss
, searchlen
, 1, 0);
930 screen_write_start(&ctx
, NULL
, &ss
);
931 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
932 screen_write_nputs(&ctx
, -1, &gc
, utf8flag
, "%s", searchstr
);
933 screen_write_stop(&ctx
);
936 fy
= gd
->hsize
- data
->oy
+ data
->cy
;
949 for (i
= fy
+ 1; i
> 0; i
--) {
950 last
= screen_size_x(s
);
953 n
= window_copy_search_rl(gd
, sgd
, &px
, i
- 1, 0, last
);
955 window_copy_scroll_to(wp
, px
, i
- 1);
959 if (!n
&& !wrapped
) {
961 fy
= gd
->hsize
+ gd
->sy
- 1;
970 window_copy_search_down(struct window_pane
*wp
, const char *searchstr
)
972 struct window_copy_mode_data
*data
= wp
->modedata
;
973 struct screen
*s
= data
->backing
, ss
;
974 struct screen_write_ctx ctx
;
975 struct grid
*gd
= s
->grid
, *sgd
;
978 u_int i
, first
, fx
, fy
, px
;
979 int utf8flag
, n
, wrapped
;
981 if (*searchstr
== '\0')
983 utf8flag
= options_get_number(&wp
->window
->options
, "utf8");
984 searchlen
= screen_write_strlen(utf8flag
, "%s", searchstr
);
986 screen_init(&ss
, searchlen
, 1, 0);
987 screen_write_start(&ctx
, NULL
, &ss
);
988 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
989 screen_write_nputs(&ctx
, -1, &gc
, utf8flag
, "%s", searchstr
);
990 screen_write_stop(&ctx
);
993 fy
= gd
->hsize
- data
->oy
+ data
->cy
;
995 if (fx
== gd
->sx
- 1) {
996 if (fy
== gd
->hsize
+ gd
->sy
)
1006 for (i
= fy
+ 1; i
< gd
->hsize
+ gd
->sy
; i
++) {
1010 n
= window_copy_search_lr(gd
, sgd
, &px
, i
- 1, first
, gd
->sx
);
1012 window_copy_scroll_to(wp
, px
, i
- 1);
1016 if (!n
&& !wrapped
) {
1027 window_copy_goto_line(struct window_pane
*wp
, const char *linestr
)
1029 struct window_copy_mode_data
*data
= wp
->modedata
;
1033 lineno
= strtonum(linestr
, 0, screen_hsize(data
->backing
), &errstr
);
1038 window_copy_update_selection(wp
);
1039 window_copy_redraw_screen(wp
);
1043 window_copy_write_line(
1044 struct window_pane
*wp
, struct screen_write_ctx
*ctx
, u_int py
)
1046 struct window_copy_mode_data
*data
= wp
->modedata
;
1047 struct screen
*s
= &data
->screen
;
1048 struct options
*oo
= &wp
->window
->options
;
1049 struct grid_cell gc
;
1051 size_t last
, xoff
= 0, size
= 0;
1053 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
1054 colour_set_fg(&gc
, options_get_number(oo
, "mode-fg"));
1055 colour_set_bg(&gc
, options_get_number(oo
, "mode-bg"));
1056 gc
.attr
|= options_get_number(oo
, "mode-attr");
1058 last
= screen_size_y(s
) - 1;
1060 size
= xsnprintf(hdr
, sizeof hdr
,
1061 "[%u/%u]", data
->oy
, screen_hsize(data
->backing
));
1062 if (size
> screen_size_x(s
))
1063 size
= screen_size_x(s
);
1064 screen_write_cursormove(ctx
, screen_size_x(s
) - size
, 0);
1065 screen_write_puts(ctx
, &gc
, "%s", hdr
);
1066 } else if (py
== last
&& data
->inputtype
!= WINDOW_COPY_OFF
) {
1067 if (data
->inputtype
== WINDOW_COPY_NUMERICPREFIX
) {
1068 xoff
= size
= xsnprintf(hdr
, sizeof hdr
,
1069 "Repeat: %u", data
->numprefix
);
1071 xoff
= size
= xsnprintf(hdr
, sizeof hdr
,
1072 "%s: %s", data
->inputprompt
, data
->inputstr
);
1074 screen_write_cursormove(ctx
, 0, last
);
1075 screen_write_puts(ctx
, &gc
, "%s", hdr
);
1079 screen_write_cursormove(ctx
, xoff
, py
);
1080 screen_write_copy(ctx
, data
->backing
, xoff
,
1081 (screen_hsize(data
->backing
) - data
->oy
) + py
,
1082 screen_size_x(s
) - size
, 1);
1084 if (py
== data
->cy
&& data
->cx
== screen_size_x(s
)) {
1085 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
1086 screen_write_cursormove(ctx
, screen_size_x(s
) - 1, py
);
1087 screen_write_putc(ctx
, &gc
, '$');
1092 window_copy_write_lines(
1093 struct window_pane
*wp
, struct screen_write_ctx
*ctx
, u_int py
, u_int ny
)
1097 for (yy
= py
; yy
< py
+ ny
; yy
++)
1098 window_copy_write_line(wp
, ctx
, py
);
1102 window_copy_redraw_lines(struct window_pane
*wp
, u_int py
, u_int ny
)
1104 struct window_copy_mode_data
*data
= wp
->modedata
;
1105 struct screen_write_ctx ctx
;
1108 screen_write_start(&ctx
, wp
, NULL
);
1109 for (i
= py
; i
< py
+ ny
; i
++)
1110 window_copy_write_line(wp
, &ctx
, i
);
1111 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
);
1112 screen_write_stop(&ctx
);
1116 window_copy_redraw_screen(struct window_pane
*wp
)
1118 struct window_copy_mode_data
*data
= wp
->modedata
;
1120 window_copy_redraw_lines(wp
, 0, screen_size_y(&data
->screen
));
1124 window_copy_update_cursor(struct window_pane
*wp
, u_int cx
, u_int cy
)
1126 struct window_copy_mode_data
*data
= wp
->modedata
;
1127 struct screen
*s
= &data
->screen
;
1128 struct screen_write_ctx ctx
;
1129 u_int old_cx
, old_cy
;
1131 old_cx
= data
->cx
; old_cy
= data
->cy
;
1132 data
->cx
= cx
; data
->cy
= cy
;
1133 if (old_cx
== screen_size_x(s
))
1134 window_copy_redraw_lines(wp
, old_cy
, 1);
1135 if (data
->cx
== screen_size_x(s
))
1136 window_copy_redraw_lines(wp
, data
->cy
, 1);
1138 screen_write_start(&ctx
, wp
, NULL
);
1139 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
);
1140 screen_write_stop(&ctx
);
1145 window_copy_start_selection(struct window_pane
*wp
)
1147 struct window_copy_mode_data
*data
= wp
->modedata
;
1148 struct screen
*s
= &data
->screen
;
1150 data
->selx
= data
->cx
;
1151 data
->sely
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1154 window_copy_update_selection(wp
);
1158 window_copy_update_selection(struct window_pane
*wp
)
1160 struct window_copy_mode_data
*data
= wp
->modedata
;
1161 struct screen
*s
= &data
->screen
;
1162 struct options
*oo
= &wp
->window
->options
;
1163 struct grid_cell gc
;
1164 u_int sx
, sy
, ty
, cy
;
1170 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
1171 colour_set_fg(&gc
, options_get_number(oo
, "mode-fg"));
1172 colour_set_bg(&gc
, options_get_number(oo
, "mode-bg"));
1173 gc
.attr
|= options_get_number(oo
, "mode-attr");
1175 /* Find top of screen. */
1176 ty
= screen_hsize(data
->backing
) - data
->oy
;
1178 /* Adjust the selection. */
1181 if (sy
< ty
) { /* above screen */
1182 if (!data
->rectflag
)
1185 } else if (sy
> ty
+ screen_size_y(s
) - 1) { /* below screen */
1186 if (!data
->rectflag
)
1187 sx
= screen_size_x(s
) - 1;
1188 sy
= screen_size_y(s
) - 1;
1191 sy
= screen_hsize(s
) + sy
;
1193 screen_set_selection(s
,
1194 sx
, sy
, data
->cx
, screen_hsize(s
) + data
->cy
, data
->rectflag
, &gc
);
1196 if (data
->rectflag
) {
1198 * Can't rely on the caller to redraw the right lines for
1199 * rectangle selection - find the highest line and the number
1200 * of lines, and redraw just past that in both directions
1204 window_copy_redraw_lines(wp
, sy
, cy
- sy
+ 1);
1206 window_copy_redraw_lines(wp
, cy
, sy
- cy
+ 1);
1213 window_copy_copy_selection(struct window_pane
*wp
)
1215 struct window_copy_mode_data
*data
= wp
->modedata
;
1216 struct screen
*s
= &data
->screen
;
1219 u_int i
, xx
, yy
, sx
, sy
, ex
, ey
, limit
;
1220 u_int firstsx
, lastex
, restex
, restsx
;
1231 * The selection extends from selx,sely to (adjusted) cx,cy on
1235 /* Find start and end. */
1237 yy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1238 if (yy
< data
->sely
|| (yy
== data
->sely
&& xx
< data
->selx
)) {
1240 ex
= data
->selx
; ey
= data
->sely
;
1242 sx
= data
->selx
; sy
= data
->sely
;
1246 /* Trim ex to end of line. */
1247 xx
= window_copy_find_length(wp
, ey
);
1252 * Deal with rectangle-copy if necessary; four situations: start of
1253 * first line (firstsx), end of last line (lastex), start (restsx) and
1254 * end (restex) of all other lines.
1256 xx
= screen_size_x(s
);
1257 if (data
->rectflag
) {
1259 * Need to ignore the column with the cursor in it, which for
1260 * rectangular copy means knowing which side the cursor is on.
1262 if (data
->selx
< data
->cx
) {
1263 /* Selection start is on the left. */
1266 firstsx
= data
->selx
;
1267 restsx
= data
->selx
;
1269 /* Cursor is on the left. */
1270 lastex
= data
->selx
+ 1;
1271 restex
= data
->selx
+ 1;
1277 * Like emacs, keep the top-left-most character, and drop the
1278 * bottom-right-most, regardless of copy direction.
1286 /* Copy the lines. */
1288 window_copy_copy_line(wp
, &buf
, &off
, sy
, firstsx
, lastex
);
1290 window_copy_copy_line(wp
, &buf
, &off
, sy
, firstsx
, restex
);
1292 for (i
= sy
+ 1; i
< ey
; i
++) {
1293 window_copy_copy_line(
1294 wp
, &buf
, &off
, i
, restsx
, restex
);
1297 window_copy_copy_line(wp
, &buf
, &off
, ey
, restsx
, lastex
);
1300 /* Don't bother if no data. */
1305 off
--; /* remove final \n */
1307 /* Add the buffer to the stack. */
1308 limit
= options_get_number(&global_options
, "buffer-limit");
1309 paste_add(&global_buffers
, buf
, off
, limit
);
1313 window_copy_copy_line(struct window_pane
*wp
,
1314 char **buf
, size_t *off
, u_int sy
, u_int sx
, u_int ex
)
1316 struct window_copy_mode_data
*data
= wp
->modedata
;
1317 struct grid
*gd
= data
->backing
->grid
;
1318 const struct grid_cell
*gc
;
1319 const struct grid_utf8
*gu
;
1320 struct grid_line
*gl
;
1321 u_int i
, xx
, wrapped
= 0;
1328 * Work out if the line was wrapped at the screen edge and all of it is
1331 gl
= &gd
->linedata
[sy
];
1332 if (gl
->flags
& GRID_LINE_WRAPPED
&& gl
->cellsize
<= gd
->sx
)
1335 /* If the line was wrapped, don't strip spaces (use the full length). */
1339 xx
= window_copy_find_length(wp
, sy
);
1346 for (i
= sx
; i
< ex
; i
++) {
1347 gc
= grid_peek_cell(gd
, i
, sy
);
1348 if (gc
->flags
& GRID_FLAG_PADDING
)
1350 if (!(gc
->flags
& GRID_FLAG_UTF8
)) {
1351 *buf
= xrealloc(*buf
, 1, (*off
) + 1);
1352 (*buf
)[(*off
)++] = gc
->data
;
1354 gu
= grid_peek_utf8(gd
, i
, sy
);
1355 size
= grid_utf8_size(gu
);
1356 *buf
= xrealloc(*buf
, 1, (*off
) + size
);
1357 *off
+= grid_utf8_copy(gu
, *buf
+ *off
, size
);
1362 /* Only add a newline if the line wasn't wrapped. */
1363 if (!wrapped
|| ex
!= xx
) {
1364 *buf
= xrealloc(*buf
, 1, (*off
) + 1);
1365 (*buf
)[(*off
)++] = '\n';
1370 window_copy_clear_selection(struct window_pane
*wp
)
1372 struct window_copy_mode_data
*data
= wp
->modedata
;
1375 screen_clear_selection(&data
->screen
);
1377 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1378 px
= window_copy_find_length(wp
, py
);
1380 window_copy_update_cursor(wp
, px
, data
->cy
);
1384 window_copy_in_set(struct window_pane
*wp
, u_int px
, u_int py
, const char *set
)
1386 struct window_copy_mode_data
*data
= wp
->modedata
;
1387 const struct grid_cell
*gc
;
1389 gc
= grid_peek_cell(data
->backing
->grid
, px
, py
);
1390 if (gc
->flags
& (GRID_FLAG_PADDING
|GRID_FLAG_UTF8
))
1392 if (gc
->data
== 0x00 || gc
->data
== 0x7f)
1394 return (strchr(set
, gc
->data
) != NULL
);
1398 window_copy_find_length(struct window_pane
*wp
, u_int py
)
1400 struct window_copy_mode_data
*data
= wp
->modedata
;
1401 struct screen
*s
= data
->backing
;
1402 const struct grid_cell
*gc
;
1406 * If the pane has been resized, its grid can contain old overlong
1407 * lines. grid_peek_cell does not allow accessing cells beyond the
1408 * width of the grid, and screen_write_copy treats them as spaces, so
1409 * ignore them here too.
1411 px
= s
->grid
->linedata
[py
].cellsize
;
1412 if (px
> screen_size_x(s
))
1413 px
= screen_size_x(s
);
1415 gc
= grid_peek_cell(s
->grid
, px
- 1, py
);
1416 if (gc
->flags
& GRID_FLAG_UTF8
)
1418 if (gc
->data
!= ' ')
1426 window_copy_cursor_start_of_line(struct window_pane
*wp
)
1428 struct window_copy_mode_data
*data
= wp
->modedata
;
1429 struct screen
*back_s
= data
->backing
;
1430 struct grid
*gd
= back_s
->grid
;
1433 if (data
->cx
== 0) {
1434 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1435 while (py
> 0 && gd
->linedata
[py
-1].flags
& GRID_LINE_WRAPPED
) {
1436 window_copy_cursor_up(wp
, 0);
1437 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1440 window_copy_update_cursor(wp
, 0, data
->cy
);
1441 if (window_copy_update_selection(wp
))
1442 window_copy_redraw_lines(wp
, data
->cy
, 1);
1446 window_copy_cursor_back_to_indentation(struct window_pane
*wp
)
1448 struct window_copy_mode_data
*data
= wp
->modedata
;
1450 const struct grid_cell
*gc
;
1453 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1454 xx
= window_copy_find_length(wp
, py
);
1457 gc
= grid_peek_cell(data
->backing
->grid
, px
, py
);
1458 if (gc
->flags
& GRID_FLAG_UTF8
)
1460 if (gc
->data
!= ' ')
1465 window_copy_update_cursor(wp
, px
, data
->cy
);
1466 if (window_copy_update_selection(wp
))
1467 window_copy_redraw_lines(wp
, data
->cy
, 1);
1471 window_copy_cursor_end_of_line(struct window_pane
*wp
)
1473 struct window_copy_mode_data
*data
= wp
->modedata
;
1474 struct screen
*back_s
= data
->backing
;
1475 struct grid
*gd
= back_s
->grid
;
1478 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1479 px
= window_copy_find_length(wp
, py
);
1481 if (data
->cx
== px
) {
1482 if (data
->screen
.sel
.flag
&& data
->rectflag
)
1483 px
= screen_size_x(back_s
);
1484 if (gd
->linedata
[py
].flags
& GRID_LINE_WRAPPED
) {
1485 while (py
< gd
->sy
+ gd
->hsize
&&
1486 gd
->linedata
[py
].flags
& GRID_LINE_WRAPPED
) {
1487 window_copy_cursor_down(wp
, 0);
1488 py
= screen_hsize(back_s
)
1489 + data
->cy
- data
->oy
;
1491 px
= window_copy_find_length(wp
, py
);
1494 window_copy_update_cursor(wp
, px
, data
->cy
);
1496 if (window_copy_update_selection(wp
))
1497 window_copy_redraw_lines(wp
, data
->cy
, 1);
1501 window_copy_cursor_left(struct window_pane
*wp
)
1503 struct window_copy_mode_data
*data
= wp
->modedata
;
1505 if (data
->cx
== 0) {
1506 window_copy_cursor_up(wp
, 0);
1507 window_copy_cursor_end_of_line(wp
);
1509 window_copy_update_cursor(wp
, data
->cx
- 1, data
->cy
);
1510 if (window_copy_update_selection(wp
))
1511 window_copy_redraw_lines(wp
, data
->cy
, 1);
1516 window_copy_cursor_right(struct window_pane
*wp
)
1518 struct window_copy_mode_data
*data
= wp
->modedata
;
1521 if (data
->screen
.sel
.flag
&& data
->rectflag
)
1522 px
= screen_size_x(&data
->screen
);
1524 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1525 px
= window_copy_find_length(wp
, py
);
1528 if (data
->cx
>= px
) {
1529 window_copy_cursor_start_of_line(wp
);
1530 window_copy_cursor_down(wp
, 0);
1532 window_copy_update_cursor(wp
, data
->cx
+ 1, data
->cy
);
1533 if (window_copy_update_selection(wp
))
1534 window_copy_redraw_lines(wp
, data
->cy
, 1);
1539 window_copy_cursor_up(struct window_pane
*wp
, int scroll_only
)
1541 struct window_copy_mode_data
*data
= wp
->modedata
;
1542 struct screen
*s
= &data
->screen
;
1543 u_int ox
, oy
, px
, py
;
1545 oy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1546 ox
= window_copy_find_length(wp
, oy
);
1548 data
->lastcx
= data
->cx
;
1552 data
->cx
= data
->lastcx
;
1553 if (scroll_only
|| data
->cy
== 0) {
1554 window_copy_scroll_down(wp
, 1);
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);
1562 window_copy_update_cursor(wp
, data
->cx
, data
->cy
- 1);
1563 if (window_copy_update_selection(wp
)) {
1564 if (data
->cy
== screen_size_y(s
) - 1)
1565 window_copy_redraw_lines(wp
, data
->cy
, 1);
1567 window_copy_redraw_lines(wp
, data
->cy
, 2);
1571 if (!data
->screen
.sel
.flag
|| !data
->rectflag
) {
1572 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1573 px
= window_copy_find_length(wp
, py
);
1574 if ((data
->cx
>= data
->lastsx
&& data
->cx
!= px
) ||
1576 window_copy_cursor_end_of_line(wp
);
1581 window_copy_cursor_down(struct window_pane
*wp
, int scroll_only
)
1583 struct window_copy_mode_data
*data
= wp
->modedata
;
1584 struct screen
*s
= &data
->screen
;
1585 u_int ox
, oy
, px
, py
;
1587 oy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1588 ox
= window_copy_find_length(wp
, oy
);
1590 data
->lastcx
= data
->cx
;
1594 data
->cx
= data
->lastcx
;
1595 if (scroll_only
|| data
->cy
== screen_size_y(s
) - 1) {
1596 window_copy_scroll_up(wp
, 1);
1597 if (scroll_only
&& data
->cy
> 0)
1598 window_copy_redraw_lines(wp
, data
->cy
- 1, 2);
1600 window_copy_update_cursor(wp
, data
->cx
, data
->cy
+ 1);
1601 if (window_copy_update_selection(wp
))
1602 window_copy_redraw_lines(wp
, data
->cy
- 1, 2);
1605 if (!data
->screen
.sel
.flag
|| !data
->rectflag
) {
1606 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1607 px
= window_copy_find_length(wp
, py
);
1608 if ((data
->cx
>= data
->lastsx
&& data
->cx
!= px
) ||
1610 window_copy_cursor_end_of_line(wp
);
1615 window_copy_cursor_jump(struct window_pane
*wp
)
1617 struct window_copy_mode_data
*data
= wp
->modedata
;
1618 struct screen
*back_s
= data
->backing
;
1619 const struct grid_cell
*gc
;
1623 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1624 xx
= window_copy_find_length(wp
, py
);
1627 gc
= grid_peek_cell(back_s
->grid
, px
, py
);
1628 if ((gc
->flags
& (GRID_FLAG_PADDING
|GRID_FLAG_UTF8
)) == 0
1629 && gc
->data
== data
->jumpchar
) {
1631 window_copy_update_cursor(wp
, px
, data
->cy
);
1632 if (window_copy_update_selection(wp
))
1633 window_copy_redraw_lines(wp
, data
->cy
, 1);
1641 window_copy_cursor_jump_back(struct window_pane
*wp
)
1643 struct window_copy_mode_data
*data
= wp
->modedata
;
1644 struct screen
*back_s
= data
->backing
;
1645 const struct grid_cell
*gc
;
1649 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1655 gc
= grid_peek_cell(back_s
->grid
, px
, py
);
1656 if ((gc
->flags
& (GRID_FLAG_PADDING
|GRID_FLAG_UTF8
)) == 0
1657 && gc
->data
== data
->jumpchar
) {
1659 window_copy_update_cursor(wp
, px
, data
->cy
);
1660 if (window_copy_update_selection(wp
))
1661 window_copy_redraw_lines(wp
, data
->cy
, 1);
1671 window_copy_cursor_next_word(struct window_pane
*wp
, const char *separators
)
1673 struct window_copy_mode_data
*data
= wp
->modedata
;
1674 struct screen
*back_s
= data
->backing
;
1675 u_int px
, py
, xx
, yy
;
1679 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1680 xx
= window_copy_find_length(wp
, py
);
1681 yy
= screen_hsize(back_s
) + screen_size_y(back_s
) - 1;
1684 * First skip past any nonword characters and then any word characters.
1686 * expected is initially set to 0 for the former and then 1 for the
1691 window_copy_in_set(wp
, px
, py
, separators
) == expected
) {
1692 /* Move down if we're past the end of the line. */
1696 window_copy_cursor_down(wp
, 0);
1699 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1700 xx
= window_copy_find_length(wp
, py
);
1704 expected
= !expected
;
1705 } while (expected
== 1);
1707 window_copy_update_cursor(wp
, px
, data
->cy
);
1708 if (window_copy_update_selection(wp
))
1709 window_copy_redraw_lines(wp
, data
->cy
, 1);
1713 window_copy_cursor_next_word_end(struct window_pane
*wp
, const char *separators
)
1715 struct window_copy_mode_data
*data
= wp
->modedata
;
1716 struct screen
*back_s
= data
->backing
;
1717 u_int px
, py
, xx
, yy
;
1721 py
= screen_hsize(back_s
) + data
->cy
- data
->oy
;
1722 xx
= window_copy_find_length(wp
, py
);
1723 yy
= screen_hsize(back_s
) + screen_size_y(back_s
) - 1;
1726 * First skip past any word characters, then any nonword characters.
1728 * expected is initially set to 1 for the former and then 0 for the
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
== 0);
1749 window_copy_update_cursor(wp
, px
, data
->cy
);
1750 if (window_copy_update_selection(wp
))
1751 window_copy_redraw_lines(wp
, data
->cy
, 1);
1754 /* Move to the previous place where a word begins. */
1756 window_copy_cursor_previous_word(struct window_pane
*wp
, const char *separators
)
1758 struct window_copy_mode_data
*data
= wp
->modedata
;
1762 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1764 /* Move back to the previous word character. */
1768 if (!window_copy_in_set(wp
, px
, py
, separators
))
1771 if (data
->cy
== 0 &&
1772 (screen_hsize(data
->backing
) == 0 ||
1773 data
->oy
>= screen_hsize(data
->backing
) - 1))
1775 window_copy_cursor_up(wp
, 0);
1777 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1778 px
= window_copy_find_length(wp
, py
);
1782 /* Move back to the beginning of this word. */
1783 while (px
> 0 && !window_copy_in_set(wp
, px
- 1, py
, separators
))
1787 window_copy_update_cursor(wp
, px
, data
->cy
);
1788 if (window_copy_update_selection(wp
))
1789 window_copy_redraw_lines(wp
, data
->cy
, 1);
1793 window_copy_scroll_up(struct window_pane
*wp
, u_int ny
)
1795 struct window_copy_mode_data
*data
= wp
->modedata
;
1796 struct screen
*s
= &data
->screen
;
1797 struct screen_write_ctx ctx
;
1805 screen_write_start(&ctx
, wp
, NULL
);
1806 screen_write_cursormove(&ctx
, 0, 0);
1807 screen_write_deleteline(&ctx
, ny
);
1808 window_copy_write_lines(wp
, &ctx
, screen_size_y(s
) - ny
, ny
);
1809 window_copy_write_line(wp
, &ctx
, 0);
1810 if (screen_size_y(s
) > 1)
1811 window_copy_write_line(wp
, &ctx
, 1);
1812 if (screen_size_y(s
) > 3)
1813 window_copy_write_line(wp
, &ctx
, screen_size_y(s
) - 2);
1814 if (s
->sel
.flag
&& screen_size_y(s
) > ny
) {
1815 window_copy_update_selection(wp
);
1816 window_copy_write_line(wp
, &ctx
, screen_size_y(s
) - ny
- 1);
1818 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
);
1819 window_copy_update_selection(wp
);
1820 screen_write_stop(&ctx
);
1824 window_copy_scroll_down(struct window_pane
*wp
, u_int ny
)
1826 struct window_copy_mode_data
*data
= wp
->modedata
;
1827 struct screen
*s
= &data
->screen
;
1828 struct screen_write_ctx ctx
;
1830 if (ny
> screen_hsize(data
->backing
))
1833 if (data
->oy
> screen_hsize(data
->backing
) - ny
)
1834 ny
= screen_hsize(data
->backing
) - data
->oy
;
1839 screen_write_start(&ctx
, wp
, NULL
);
1840 screen_write_cursormove(&ctx
, 0, 0);
1841 screen_write_insertline(&ctx
, ny
);
1842 window_copy_write_lines(wp
, &ctx
, 0, ny
);
1843 if (s
->sel
.flag
&& screen_size_y(s
) > ny
) {
1844 window_copy_update_selection(wp
);
1845 window_copy_write_line(wp
, &ctx
, ny
);
1846 } else if (ny
== 1) /* nuke position */
1847 window_copy_write_line(wp
, &ctx
, 1);
1848 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
);
1849 window_copy_update_selection(wp
);
1850 screen_write_stop(&ctx
);
1854 window_copy_rectangle_toggle(struct window_pane
*wp
)
1856 struct window_copy_mode_data
*data
= wp
->modedata
;
1859 data
->rectflag
= !data
->rectflag
;
1861 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1862 px
= window_copy_find_length(wp
, py
);
1864 window_copy_update_cursor(wp
, px
, data
->cy
);
1866 window_copy_update_selection(wp
);
1867 window_copy_redraw_screen(wp
);