4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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>
29 struct window_copy_mode_data
;
31 static const char *window_copy_key_table(struct window_mode_entry
*);
32 static void window_copy_command(struct window_mode_entry
*, struct client
*,
33 struct session
*, struct winlink
*, struct args
*,
34 struct mouse_event
*);
35 static struct screen
*window_copy_init(struct window_mode_entry
*,
36 struct cmd_find_state
*, struct args
*);
37 static struct screen
*window_copy_view_init(struct window_mode_entry
*,
38 struct cmd_find_state
*, struct args
*);
39 static void window_copy_free(struct window_mode_entry
*);
40 static void window_copy_resize(struct window_mode_entry
*, u_int
, u_int
);
41 static void window_copy_formats(struct window_mode_entry
*,
42 struct format_tree
*);
43 static void window_copy_pageup1(struct window_mode_entry
*, int);
44 static int window_copy_pagedown(struct window_mode_entry
*, int, int);
45 static void window_copy_next_paragraph(struct window_mode_entry
*);
46 static void window_copy_previous_paragraph(struct window_mode_entry
*);
47 static void window_copy_redraw_selection(struct window_mode_entry
*, u_int
);
48 static void window_copy_redraw_lines(struct window_mode_entry
*, u_int
,
50 static void window_copy_redraw_screen(struct window_mode_entry
*);
51 static void window_copy_write_line(struct window_mode_entry
*,
52 struct screen_write_ctx
*, u_int
);
53 static void window_copy_write_lines(struct window_mode_entry
*,
54 struct screen_write_ctx
*, u_int
, u_int
);
55 static char *window_copy_match_at_cursor(struct window_copy_mode_data
*);
56 static void window_copy_scroll_to(struct window_mode_entry
*, u_int
, u_int
,
58 static int window_copy_search_compare(struct grid
*, u_int
, u_int
,
59 struct grid
*, u_int
, int);
60 static int window_copy_search_lr(struct grid
*, struct grid
*, u_int
*,
61 u_int
, u_int
, u_int
, int);
62 static int window_copy_search_rl(struct grid
*, struct grid
*, u_int
*,
63 u_int
, u_int
, u_int
, int);
64 static int window_copy_last_regex(struct grid
*, u_int
, u_int
, u_int
,
65 u_int
, u_int
*, u_int
*, const char *, const regex_t
*,
67 static int window_copy_search_mark_at(struct window_copy_mode_data
*,
68 u_int
, u_int
, u_int
*);
69 static char *window_copy_stringify(struct grid
*, u_int
, u_int
, u_int
,
71 static void window_copy_cstrtocellpos(struct grid
*, u_int
, u_int
*,
72 u_int
*, const char *);
73 static int window_copy_search_marks(struct window_mode_entry
*,
74 struct screen
*, int, int);
75 static void window_copy_clear_marks(struct window_mode_entry
*);
76 static int window_copy_is_lowercase(const char *);
77 static void window_copy_search_back_overlap(struct grid
*, regex_t
*,
78 u_int
*, u_int
*, u_int
*, u_int
);
79 static int window_copy_search_jump(struct window_mode_entry
*,
80 struct grid
*, struct grid
*, u_int
, u_int
, u_int
, int, int,
82 static int window_copy_search(struct window_mode_entry
*, int, int);
83 static int window_copy_search_up(struct window_mode_entry
*, int);
84 static int window_copy_search_down(struct window_mode_entry
*, int);
85 static void window_copy_goto_line(struct window_mode_entry
*, const char *);
86 static void window_copy_update_cursor(struct window_mode_entry
*, u_int
,
88 static void window_copy_start_selection(struct window_mode_entry
*);
89 static int window_copy_adjust_selection(struct window_mode_entry
*,
91 static int window_copy_set_selection(struct window_mode_entry
*, int, int);
92 static int window_copy_update_selection(struct window_mode_entry
*, int,
94 static void window_copy_synchronize_cursor(struct window_mode_entry
*, int);
95 static void *window_copy_get_selection(struct window_mode_entry
*, size_t *);
96 static void window_copy_copy_buffer(struct window_mode_entry
*,
97 const char *, void *, size_t);
98 static void window_copy_pipe(struct window_mode_entry
*,
99 struct session
*, const char *);
100 static void window_copy_copy_pipe(struct window_mode_entry
*,
101 struct session
*, const char *, const char *);
102 static void window_copy_copy_selection(struct window_mode_entry
*,
104 static void window_copy_append_selection(struct window_mode_entry
*);
105 static void window_copy_clear_selection(struct window_mode_entry
*);
106 static void window_copy_copy_line(struct window_mode_entry
*, char **,
107 size_t *, u_int
, u_int
, u_int
);
108 static int window_copy_in_set(struct window_mode_entry
*, u_int
, u_int
,
110 static u_int
window_copy_find_length(struct window_mode_entry
*, u_int
);
111 static void window_copy_cursor_start_of_line(struct window_mode_entry
*);
112 static void window_copy_cursor_back_to_indentation(
113 struct window_mode_entry
*);
114 static void window_copy_cursor_end_of_line(struct window_mode_entry
*);
115 static void window_copy_other_end(struct window_mode_entry
*);
116 static void window_copy_cursor_left(struct window_mode_entry
*);
117 static void window_copy_cursor_right(struct window_mode_entry
*, int);
118 static void window_copy_cursor_up(struct window_mode_entry
*, int);
119 static void window_copy_cursor_down(struct window_mode_entry
*, int);
120 static void window_copy_cursor_jump(struct window_mode_entry
*);
121 static void window_copy_cursor_jump_back(struct window_mode_entry
*);
122 static void window_copy_cursor_jump_to(struct window_mode_entry
*);
123 static void window_copy_cursor_jump_to_back(struct window_mode_entry
*);
124 static void window_copy_cursor_next_word(struct window_mode_entry
*,
126 static void window_copy_cursor_next_word_end_pos(struct window_mode_entry
*,
127 const char *, u_int
*, u_int
*);
128 static void window_copy_cursor_next_word_end(struct window_mode_entry
*,
130 static void window_copy_cursor_previous_word_pos(struct window_mode_entry
*,
131 const char *, u_int
*, u_int
*);
132 static void window_copy_cursor_previous_word(struct window_mode_entry
*,
134 static void window_copy_cursor_prompt(struct window_mode_entry
*, int,
136 static void window_copy_scroll_up(struct window_mode_entry
*, u_int
);
137 static void window_copy_scroll_down(struct window_mode_entry
*, u_int
);
138 static void window_copy_rectangle_set(struct window_mode_entry
*, int);
139 static void window_copy_move_mouse(struct mouse_event
*);
140 static void window_copy_drag_update(struct client
*, struct mouse_event
*);
141 static void window_copy_drag_release(struct client
*, struct mouse_event
*);
142 static void window_copy_jump_to_mark(struct window_mode_entry
*);
143 static void window_copy_acquire_cursor_up(struct window_mode_entry
*,
144 u_int
, u_int
, u_int
, u_int
, u_int
);
145 static void window_copy_acquire_cursor_down(struct window_mode_entry
*,
146 u_int
, u_int
, u_int
, u_int
, u_int
, u_int
, int);
148 const struct window_mode window_copy_mode
= {
151 .init
= window_copy_init
,
152 .free
= window_copy_free
,
153 .resize
= window_copy_resize
,
154 .key_table
= window_copy_key_table
,
155 .command
= window_copy_command
,
156 .formats
= window_copy_formats
,
159 const struct window_mode window_view_mode
= {
162 .init
= window_copy_view_init
,
163 .free
= window_copy_free
,
164 .resize
= window_copy_resize
,
165 .key_table
= window_copy_key_table
,
166 .command
= window_copy_command
,
167 .formats
= window_copy_formats
,
172 WINDOW_COPY_SEARCHUP
,
173 WINDOW_COPY_SEARCHDOWN
,
174 WINDOW_COPY_JUMPFORWARD
,
175 WINDOW_COPY_JUMPBACKWARD
,
176 WINDOW_COPY_JUMPTOFORWARD
,
177 WINDOW_COPY_JUMPTOBACKWARD
,
181 WINDOW_COPY_REL_POS_ABOVE
,
182 WINDOW_COPY_REL_POS_ON_SCREEN
,
183 WINDOW_COPY_REL_POS_BELOW
,
186 enum window_copy_cmd_action
{
187 WINDOW_COPY_CMD_NOTHING
,
188 WINDOW_COPY_CMD_REDRAW
,
189 WINDOW_COPY_CMD_CANCEL
,
192 enum window_copy_cmd_clear
{
193 WINDOW_COPY_CMD_CLEAR_ALWAYS
,
194 WINDOW_COPY_CMD_CLEAR_NEVER
,
195 WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
198 struct window_copy_cmd_state
{
199 struct window_mode_entry
*wme
;
201 struct mouse_event
*m
;
209 * Copy mode's visible screen (the "screen" field) is filled from one of two
210 * sources: the original contents of the pane (used when we actually enter via
211 * the "copy-mode" command, to copy the contents of the current pane), or else
212 * a series of lines containing the output from an output-writing tmux command
213 * (such as any of the "show-*" or "list-*" commands).
215 * In either case, the full content of the copy-mode grid is pointed at by the
216 * "backing" field, and is copied into "screen" as needed (that is, when
217 * scrolling occurs). When copy-mode is backed by a pane, backing points
218 * directly at that pane's screen structure (&wp->base); when backed by a list
219 * of output-lines from a command, it points at a newly-allocated screen
220 * structure (which is deallocated when the mode ends).
222 struct window_copy_mode_data
{
223 struct screen screen
;
225 struct screen
*backing
;
226 int backing_written
; /* backing display started */
227 struct screen
*writing
;
228 struct input_ctx
*ictx
;
230 int viewmode
; /* view mode entered */
232 u_int oy
; /* number of lines scrolled up */
234 u_int selx
; /* beginning of selection */
237 u_int endselx
; /* end of selection */
241 CURSORDRAG_NONE
, /* selection is independent of cursor */
242 CURSORDRAG_ENDSEL
, /* end is synchronized with cursor */
243 CURSORDRAG_SEL
, /* start is synchronized with cursor */
251 } lineflag
; /* line selection mode */
252 int rectflag
; /* in rectangle copy mode? */
253 int scroll_exit
; /* exit on scroll to end? */
254 int hide_position
; /* hide position marker */
257 SEL_CHAR
, /* select one char at a time */
258 SEL_WORD
, /* select one word at a time */
259 SEL_LINE
, /* select one line at a time */
262 const char *separators
; /* word separators */
264 u_int dx
; /* drag start position */
267 u_int selrx
; /* selection reset positions */
275 u_int lastcx
; /* position in last line w/ content */
276 u_int lastsx
; /* size of last line w/ content */
278 u_int mx
; /* mark position */
295 int timeout
; /* search has timed out */
296 #define WINDOW_COPY_SEARCH_TIMEOUT 10000
297 #define WINDOW_COPY_SEARCH_ALL_TIMEOUT 200
298 #define WINDOW_COPY_SEARCH_MAX_LINE 2000
301 struct utf8_data
*jumpchar
;
303 struct event dragtimer
;
304 #define WINDOW_COPY_DRAG_REPEAT_TIME 50000
308 window_copy_scroll_timer(__unused
int fd
, __unused
short events
, void *arg
)
310 struct window_mode_entry
*wme
= arg
;
311 struct window_pane
*wp
= wme
->wp
;
312 struct window_copy_mode_data
*data
= wme
->data
;
313 struct timeval tv
= {
314 .tv_usec
= WINDOW_COPY_DRAG_REPEAT_TIME
317 evtimer_del(&data
->dragtimer
);
319 if (TAILQ_FIRST(&wp
->modes
) != wme
)
323 evtimer_add(&data
->dragtimer
, &tv
);
324 window_copy_cursor_up(wme
, 1);
325 } else if (data
->cy
== screen_size_y(&data
->screen
) - 1) {
326 evtimer_add(&data
->dragtimer
, &tv
);
327 window_copy_cursor_down(wme
, 1);
331 static struct screen
*
332 window_copy_clone_screen(struct screen
*src
, struct screen
*hint
, u_int
*cx
,
336 const struct grid_line
*gl
;
340 dst
= xcalloc(1, sizeof *dst
);
342 sy
= screen_hsize(src
) + screen_size_y(src
);
344 while (sy
> screen_hsize(src
)) {
345 gl
= grid_peek_line(src
->grid
, sy
- 1);
346 if (gl
->cellused
!= 0)
351 log_debug("%s: target screen is %ux%u, source %ux%u", __func__
,
352 screen_size_x(src
), sy
, screen_size_x(hint
),
353 screen_hsize(src
) + screen_size_y(src
));
354 screen_init(dst
, screen_size_x(src
), sy
, screen_hlimit(src
));
357 * Ensure history is on for the backing grid so lines are not deleted
360 dst
->grid
->flags
|= GRID_HISTORY
;
361 grid_duplicate_lines(dst
->grid
, 0, src
->grid
, 0, sy
);
363 dst
->grid
->sy
= sy
- screen_hsize(src
);
364 dst
->grid
->hsize
= screen_hsize(src
);
365 dst
->grid
->hscrolled
= src
->grid
->hscrolled
;
366 if (src
->cy
> dst
->grid
->sy
- 1) {
368 dst
->cy
= dst
->grid
->sy
- 1;
374 if (cx
!= NULL
&& cy
!= NULL
) {
376 *cy
= screen_hsize(dst
) + dst
->cy
;
377 reflow
= (screen_size_x(hint
) != screen_size_x(dst
));
382 grid_wrap_position(dst
->grid
, *cx
, *cy
, &wx
, &wy
);
383 screen_resize_cursor(dst
, screen_size_x(hint
), screen_size_y(hint
), 1,
386 grid_unwrap_position(dst
->grid
, cx
, cy
, wx
, wy
);
391 static struct window_copy_mode_data
*
392 window_copy_common_init(struct window_mode_entry
*wme
)
394 struct window_pane
*wp
= wme
->wp
;
395 struct window_copy_mode_data
*data
;
396 struct screen
*base
= &wp
->base
;
398 wme
->data
= data
= xcalloc(1, sizeof *data
);
400 data
->cursordrag
= CURSORDRAG_NONE
;
401 data
->lineflag
= LINE_SEL_NONE
;
402 data
->selflag
= SEL_CHAR
;
404 if (wp
->searchstr
!= NULL
) {
405 data
->searchtype
= WINDOW_COPY_SEARCHUP
;
406 data
->searchregex
= wp
->searchregex
;
407 data
->searchstr
= xstrdup(wp
->searchstr
);
409 data
->searchtype
= WINDOW_COPY_OFF
;
410 data
->searchregex
= 0;
411 data
->searchstr
= NULL
;
413 data
->searchx
= data
->searchy
= data
->searcho
= -1;
416 data
->jumptype
= WINDOW_COPY_OFF
;
417 data
->jumpchar
= NULL
;
419 screen_init(&data
->screen
, screen_size_x(base
), screen_size_y(base
), 0);
420 data
->modekeys
= options_get_number(wp
->window
->options
, "mode-keys");
422 evtimer_set(&data
->dragtimer
, window_copy_scroll_timer
, wme
);
427 static struct screen
*
428 window_copy_init(struct window_mode_entry
*wme
,
429 __unused
struct cmd_find_state
*fs
, struct args
*args
)
431 struct window_pane
*wp
= wme
->swp
;
432 struct window_copy_mode_data
*data
;
433 struct screen
*base
= &wp
->base
;
434 struct screen_write_ctx ctx
;
437 data
= window_copy_common_init(wme
);
438 data
->backing
= window_copy_clone_screen(base
, &data
->screen
, &cx
, &cy
,
439 wme
->swp
!= wme
->wp
);
442 if (cy
< screen_hsize(data
->backing
)) {
444 data
->oy
= screen_hsize(data
->backing
) - cy
;
446 data
->cy
= cy
- screen_hsize(data
->backing
);
450 data
->scroll_exit
= args_has(args
, 'e');
451 data
->hide_position
= args_has(args
, 'H');
453 data
->screen
.cx
= data
->cx
;
454 data
->screen
.cy
= data
->cy
;
456 data
->my
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
459 screen_write_start(&ctx
, &data
->screen
);
460 for (i
= 0; i
< screen_size_y(&data
->screen
); i
++)
461 window_copy_write_line(wme
, &ctx
, i
);
462 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
, 0);
463 screen_write_stop(&ctx
);
465 return (&data
->screen
);
468 static struct screen
*
469 window_copy_view_init(struct window_mode_entry
*wme
,
470 __unused
struct cmd_find_state
*fs
, __unused
struct args
*args
)
472 struct window_pane
*wp
= wme
->wp
;
473 struct window_copy_mode_data
*data
;
474 struct screen
*base
= &wp
->base
;
475 u_int sx
= screen_size_x(base
);
477 data
= window_copy_common_init(wme
);
480 data
->backing
= xmalloc(sizeof *data
->backing
);
481 screen_init(data
->backing
, sx
, screen_size_y(base
), UINT_MAX
);
482 data
->writing
= xmalloc(sizeof *data
->writing
);
483 screen_init(data
->writing
, sx
, screen_size_y(base
), 0);
484 data
->ictx
= input_init(NULL
, NULL
, NULL
);
486 data
->my
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
489 return (&data
->screen
);
493 window_copy_free(struct window_mode_entry
*wme
)
495 struct window_copy_mode_data
*data
= wme
->data
;
497 evtimer_del(&data
->dragtimer
);
499 free(data
->searchmark
);
500 free(data
->searchstr
);
501 free(data
->jumpchar
);
503 if (data
->writing
!= NULL
) {
504 screen_free(data
->writing
);
507 if (data
->ictx
!= NULL
)
508 input_free(data
->ictx
);
509 screen_free(data
->backing
);
512 screen_free(&data
->screen
);
517 window_copy_add(struct window_pane
*wp
, int parse
, const char *fmt
, ...)
522 window_copy_vadd(wp
, parse
, fmt
, ap
);
527 window_copy_init_ctx_cb(__unused
struct screen_write_ctx
*ctx
,
528 struct tty_ctx
*ttyctx
)
530 memcpy(&ttyctx
->defaults
, &grid_default_cell
, sizeof ttyctx
->defaults
);
531 ttyctx
->palette
= NULL
;
532 ttyctx
->redraw_cb
= NULL
;
533 ttyctx
->set_client_cb
= NULL
;
538 window_copy_vadd(struct window_pane
*wp
, int parse
, const char *fmt
, va_list ap
)
540 struct window_mode_entry
*wme
= TAILQ_FIRST(&wp
->modes
);
541 struct window_copy_mode_data
*data
= wme
->data
;
542 struct screen
*backing
= data
->backing
;
543 struct screen
*writing
= data
->writing
;
544 struct screen_write_ctx writing_ctx
, backing_ctx
, ctx
;
546 u_int old_hsize
, old_cy
;
547 u_int sx
= screen_size_x(backing
);
551 vasprintf(&text
, fmt
, ap
);
552 screen_write_start(&writing_ctx
, writing
);
553 screen_write_reset(&writing_ctx
);
554 input_parse_screen(data
->ictx
, writing
, window_copy_init_ctx_cb
,
555 data
, text
, strlen(text
));
559 old_hsize
= screen_hsize(data
->backing
);
560 screen_write_start(&backing_ctx
, backing
);
561 if (data
->backing_written
) {
563 * On the second or later line, do a CRLF before writing
564 * (so it's on a new line).
566 screen_write_carriagereturn(&backing_ctx
);
567 screen_write_linefeed(&backing_ctx
, 0, 8);
569 data
->backing_written
= 1;
570 old_cy
= backing
->cy
;
572 screen_write_fast_copy(&backing_ctx
, writing
, 0, 0, sx
, 1);
574 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
575 screen_write_vnputs(&backing_ctx
, 0, &gc
, fmt
, ap
);
577 screen_write_stop(&backing_ctx
);
579 data
->oy
+= screen_hsize(data
->backing
) - old_hsize
;
581 screen_write_start_pane(&ctx
, wp
, &data
->screen
);
584 * If the history has changed, draw the top line.
585 * (If there's any history at all, it has changed.)
587 if (screen_hsize(data
->backing
))
588 window_copy_redraw_lines(wme
, 0, 1);
590 /* Write the new lines. */
591 window_copy_redraw_lines(wme
, old_cy
, backing
->cy
- old_cy
+ 1);
593 screen_write_stop(&ctx
);
597 window_copy_pageup(struct window_pane
*wp
, int half_page
)
599 window_copy_pageup1(TAILQ_FIRST(&wp
->modes
), half_page
);
603 window_copy_pageup1(struct window_mode_entry
*wme
, int half_page
)
605 struct window_copy_mode_data
*data
= wme
->data
;
606 struct screen
*s
= &data
->screen
;
607 u_int n
, ox
, oy
, px
, py
;
609 oy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
610 ox
= window_copy_find_length(wme
, oy
);
612 if (data
->cx
!= ox
) {
613 data
->lastcx
= data
->cx
;
616 data
->cx
= data
->lastcx
;
619 if (screen_size_y(s
) > 2) {
621 n
= screen_size_y(s
) / 2;
623 n
= screen_size_y(s
) - 2;
626 if (data
->oy
+ n
> screen_hsize(data
->backing
)) {
627 data
->oy
= screen_hsize(data
->backing
);
635 if (data
->screen
.sel
== NULL
|| !data
->rectflag
) {
636 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
637 px
= window_copy_find_length(wme
, py
);
638 if ((data
->cx
>= data
->lastsx
&& data
->cx
!= px
) ||
640 window_copy_cursor_end_of_line(wme
);
643 if (data
->searchmark
!= NULL
&& !data
->timeout
)
644 window_copy_search_marks(wme
, NULL
, data
->searchregex
, 1);
645 window_copy_update_selection(wme
, 1, 0);
646 window_copy_redraw_screen(wme
);
650 window_copy_pagedown(struct window_mode_entry
*wme
, int half_page
,
653 struct window_copy_mode_data
*data
= wme
->data
;
654 struct screen
*s
= &data
->screen
;
655 u_int n
, ox
, oy
, px
, py
;
657 oy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
658 ox
= window_copy_find_length(wme
, oy
);
660 if (data
->cx
!= ox
) {
661 data
->lastcx
= data
->cx
;
664 data
->cx
= data
->lastcx
;
667 if (screen_size_y(s
) > 2) {
669 n
= screen_size_y(s
) / 2;
671 n
= screen_size_y(s
) - 2;
676 if (data
->cy
+ (n
- data
->oy
) >= screen_size_y(data
->backing
))
677 data
->cy
= screen_size_y(data
->backing
) - 1;
679 data
->cy
+= n
- data
->oy
;
683 if (data
->screen
.sel
== NULL
|| !data
->rectflag
) {
684 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
685 px
= window_copy_find_length(wme
, py
);
686 if ((data
->cx
>= data
->lastsx
&& data
->cx
!= px
) ||
688 window_copy_cursor_end_of_line(wme
);
691 if (scroll_exit
&& data
->oy
== 0)
693 if (data
->searchmark
!= NULL
&& !data
->timeout
)
694 window_copy_search_marks(wme
, NULL
, data
->searchregex
, 1);
695 window_copy_update_selection(wme
, 1, 0);
696 window_copy_redraw_screen(wme
);
701 window_copy_previous_paragraph(struct window_mode_entry
*wme
)
703 struct window_copy_mode_data
*data
= wme
->data
;
706 oy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
708 while (oy
> 0 && window_copy_find_length(wme
, oy
) == 0)
711 while (oy
> 0 && window_copy_find_length(wme
, oy
) > 0)
714 window_copy_scroll_to(wme
, 0, oy
, 0);
718 window_copy_next_paragraph(struct window_mode_entry
*wme
)
720 struct window_copy_mode_data
*data
= wme
->data
;
721 struct screen
*s
= &data
->screen
;
724 oy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
725 maxy
= screen_hsize(data
->backing
) + screen_size_y(s
) - 1;
727 while (oy
< maxy
&& window_copy_find_length(wme
, oy
) == 0)
730 while (oy
< maxy
&& window_copy_find_length(wme
, oy
) > 0)
733 ox
= window_copy_find_length(wme
, oy
);
734 window_copy_scroll_to(wme
, ox
, oy
, 0);
738 window_copy_get_word(struct window_pane
*wp
, u_int x
, u_int y
)
740 struct window_mode_entry
*wme
= TAILQ_FIRST(&wp
->modes
);
741 struct window_copy_mode_data
*data
= wme
->data
;
742 struct grid
*gd
= data
->screen
.grid
;
744 return (format_grid_word(gd
, x
, gd
->hsize
+ y
));
748 window_copy_get_line(struct window_pane
*wp
, u_int y
)
750 struct window_mode_entry
*wme
= TAILQ_FIRST(&wp
->modes
);
751 struct window_copy_mode_data
*data
= wme
->data
;
752 struct grid
*gd
= data
->screen
.grid
;
754 return (format_grid_line(gd
, gd
->hsize
+ y
));
758 window_copy_cursor_word_cb(struct format_tree
*ft
)
760 struct window_pane
*wp
= format_get_pane(ft
);
761 struct window_mode_entry
*wme
= TAILQ_FIRST(&wp
->modes
);
762 struct window_copy_mode_data
*data
= wme
->data
;
764 return (window_copy_get_word(wp
, data
->cx
, data
->cy
));
768 window_copy_cursor_line_cb(struct format_tree
*ft
)
770 struct window_pane
*wp
= format_get_pane(ft
);
771 struct window_mode_entry
*wme
= TAILQ_FIRST(&wp
->modes
);
772 struct window_copy_mode_data
*data
= wme
->data
;
774 return (window_copy_get_line(wp
, data
->cy
));
778 window_copy_search_match_cb(struct format_tree
*ft
)
780 struct window_pane
*wp
= format_get_pane(ft
);
781 struct window_mode_entry
*wme
= TAILQ_FIRST(&wp
->modes
);
782 struct window_copy_mode_data
*data
= wme
->data
;
784 return (window_copy_match_at_cursor(data
));
788 window_copy_formats(struct window_mode_entry
*wme
, struct format_tree
*ft
)
790 struct window_copy_mode_data
*data
= wme
->data
;
792 format_add(ft
, "scroll_position", "%d", data
->oy
);
793 format_add(ft
, "rectangle_toggle", "%d", data
->rectflag
);
795 format_add(ft
, "copy_cursor_x", "%d", data
->cx
);
796 format_add(ft
, "copy_cursor_y", "%d", data
->cy
);
798 format_add(ft
, "selection_present", "%d", data
->screen
.sel
!= NULL
);
799 if (data
->screen
.sel
!= NULL
) {
800 format_add(ft
, "selection_start_x", "%d", data
->selx
);
801 format_add(ft
, "selection_start_y", "%d", data
->sely
);
802 format_add(ft
, "selection_end_x", "%d", data
->endselx
);
803 format_add(ft
, "selection_end_y", "%d", data
->endsely
);
804 format_add(ft
, "selection_active", "%d",
805 data
->cursordrag
!= CURSORDRAG_NONE
);
807 format_add(ft
, "selection_active", "%d", 0);
809 format_add(ft
, "search_present", "%d", data
->searchmark
!= NULL
);
810 format_add_cb(ft
, "search_match", window_copy_search_match_cb
);
812 format_add_cb(ft
, "copy_cursor_word", window_copy_cursor_word_cb
);
813 format_add_cb(ft
, "copy_cursor_line", window_copy_cursor_line_cb
);
817 window_copy_size_changed(struct window_mode_entry
*wme
)
819 struct window_copy_mode_data
*data
= wme
->data
;
820 struct screen
*s
= &data
->screen
;
821 struct screen_write_ctx ctx
;
822 int search
= (data
->searchmark
!= NULL
);
824 window_copy_clear_selection(wme
);
825 window_copy_clear_marks(wme
);
827 screen_write_start(&ctx
, s
);
828 window_copy_write_lines(wme
, &ctx
, 0, screen_size_y(s
));
829 screen_write_stop(&ctx
);
831 if (search
&& !data
->timeout
)
832 window_copy_search_marks(wme
, NULL
, data
->searchregex
, 0);
833 data
->searchx
= data
->cx
;
834 data
->searchy
= data
->cy
;
835 data
->searcho
= data
->oy
;
839 window_copy_resize(struct window_mode_entry
*wme
, u_int sx
, u_int sy
)
841 struct window_copy_mode_data
*data
= wme
->data
;
842 struct screen
*s
= &data
->screen
;
843 struct grid
*gd
= data
->backing
->grid
;
844 u_int cx
, cy
, wx
, wy
;
847 screen_resize(s
, sx
, sy
, 0);
849 cy
= gd
->hsize
+ data
->cy
- data
->oy
;
850 reflow
= (gd
->sx
!= sx
);
852 grid_wrap_position(gd
, cx
, cy
, &wx
, &wy
);
853 screen_resize_cursor(data
->backing
, sx
, sy
, 1, 0, 0);
855 grid_unwrap_position(gd
, &cx
, &cy
, wx
, wy
);
858 if (cy
< gd
->hsize
) {
860 data
->oy
= gd
->hsize
- cy
;
862 data
->cy
= cy
- gd
->hsize
;
866 window_copy_size_changed(wme
);
867 window_copy_redraw_screen(wme
);
871 window_copy_key_table(struct window_mode_entry
*wme
)
873 struct window_pane
*wp
= wme
->wp
;
875 if (options_get_number(wp
->window
->options
, "mode-keys") == MODEKEY_VI
)
876 return ("copy-mode-vi");
877 return ("copy-mode");
881 window_copy_expand_search_string(struct window_copy_cmd_state
*cs
)
883 struct window_mode_entry
*wme
= cs
->wme
;
884 struct window_copy_mode_data
*data
= wme
->data
;
885 const char *ss
= args_string(cs
->args
, 1);
888 if (ss
== NULL
|| *ss
== '\0')
891 if (args_has(cs
->args
, 'F')) {
892 expanded
= format_single(NULL
, ss
, NULL
, NULL
, NULL
, wme
->wp
);
893 if (*expanded
== '\0') {
897 free(data
->searchstr
);
898 data
->searchstr
= expanded
;
900 free(data
->searchstr
);
901 data
->searchstr
= xstrdup(ss
);
906 static enum window_copy_cmd_action
907 window_copy_cmd_append_selection(struct window_copy_cmd_state
*cs
)
909 struct window_mode_entry
*wme
= cs
->wme
;
910 struct session
*s
= cs
->s
;
913 window_copy_append_selection(wme
);
914 window_copy_clear_selection(wme
);
915 return (WINDOW_COPY_CMD_REDRAW
);
918 static enum window_copy_cmd_action
919 window_copy_cmd_append_selection_and_cancel(struct window_copy_cmd_state
*cs
)
921 struct window_mode_entry
*wme
= cs
->wme
;
922 struct session
*s
= cs
->s
;
925 window_copy_append_selection(wme
);
926 window_copy_clear_selection(wme
);
927 return (WINDOW_COPY_CMD_CANCEL
);
930 static enum window_copy_cmd_action
931 window_copy_cmd_back_to_indentation(struct window_copy_cmd_state
*cs
)
933 struct window_mode_entry
*wme
= cs
->wme
;
935 window_copy_cursor_back_to_indentation(wme
);
936 return (WINDOW_COPY_CMD_NOTHING
);
939 static enum window_copy_cmd_action
940 window_copy_cmd_begin_selection(struct window_copy_cmd_state
*cs
)
942 struct window_mode_entry
*wme
= cs
->wme
;
943 struct client
*c
= cs
->c
;
944 struct mouse_event
*m
= cs
->m
;
945 struct window_copy_mode_data
*data
= wme
->data
;
948 window_copy_start_drag(c
, m
);
949 return (WINDOW_COPY_CMD_NOTHING
);
952 data
->lineflag
= LINE_SEL_NONE
;
953 data
->selflag
= SEL_CHAR
;
954 window_copy_start_selection(wme
);
955 return (WINDOW_COPY_CMD_REDRAW
);
958 static enum window_copy_cmd_action
959 window_copy_cmd_stop_selection(struct window_copy_cmd_state
*cs
)
961 struct window_mode_entry
*wme
= cs
->wme
;
962 struct window_copy_mode_data
*data
= wme
->data
;
964 data
->cursordrag
= CURSORDRAG_NONE
;
965 data
->lineflag
= LINE_SEL_NONE
;
966 data
->selflag
= SEL_CHAR
;
967 return (WINDOW_COPY_CMD_NOTHING
);
970 static enum window_copy_cmd_action
971 window_copy_cmd_bottom_line(struct window_copy_cmd_state
*cs
)
973 struct window_mode_entry
*wme
= cs
->wme
;
974 struct window_copy_mode_data
*data
= wme
->data
;
977 data
->cy
= screen_size_y(&data
->screen
) - 1;
979 window_copy_update_selection(wme
, 1, 0);
980 return (WINDOW_COPY_CMD_REDRAW
);
983 static enum window_copy_cmd_action
984 window_copy_cmd_cancel(__unused
struct window_copy_cmd_state
*cs
)
986 return (WINDOW_COPY_CMD_CANCEL
);
989 static enum window_copy_cmd_action
990 window_copy_cmd_clear_selection(struct window_copy_cmd_state
*cs
)
992 struct window_mode_entry
*wme
= cs
->wme
;
994 window_copy_clear_selection(wme
);
995 return (WINDOW_COPY_CMD_REDRAW
);
998 static enum window_copy_cmd_action
999 window_copy_do_copy_end_of_line(struct window_copy_cmd_state
*cs
, int pipe
,
1002 struct window_mode_entry
*wme
= cs
->wme
;
1003 struct client
*c
= cs
->c
;
1004 struct session
*s
= cs
->s
;
1005 struct winlink
*wl
= cs
->wl
;
1006 struct window_pane
*wp
= wme
->wp
;
1007 u_int count
= args_count(cs
->args
);
1008 u_int np
= wme
->prefix
, ocx
, ocy
, ooy
;
1009 struct window_copy_mode_data
*data
= wme
->data
;
1010 char *prefix
= NULL
, *command
= NULL
;
1011 const char *arg1
= args_string(cs
->args
, 1);
1012 const char *arg2
= args_string(cs
->args
, 2);
1016 prefix
= format_single(NULL
, arg2
, c
, s
, wl
, wp
);
1017 if (s
!= NULL
&& count
> 1 && *arg1
!= '\0')
1018 command
= format_single(NULL
, arg1
, c
, s
, wl
, wp
);
1021 prefix
= format_single(NULL
, arg1
, c
, s
, wl
, wp
);
1028 window_copy_start_selection(wme
);
1029 for (; np
> 1; np
--)
1030 window_copy_cursor_down(wme
, 0);
1031 window_copy_cursor_end_of_line(wme
);
1035 window_copy_copy_pipe(wme
, s
, prefix
, command
);
1037 window_copy_copy_selection(wme
, prefix
);
1042 return (WINDOW_COPY_CMD_CANCEL
);
1045 window_copy_clear_selection(wme
);
1053 return (WINDOW_COPY_CMD_REDRAW
);
1056 static enum window_copy_cmd_action
1057 window_copy_cmd_copy_end_of_line(struct window_copy_cmd_state
*cs
)
1059 return (window_copy_do_copy_end_of_line(cs
, 0, 0));
1062 static enum window_copy_cmd_action
1063 window_copy_cmd_copy_end_of_line_and_cancel(struct window_copy_cmd_state
*cs
)
1065 return (window_copy_do_copy_end_of_line(cs
, 0, 1));
1068 static enum window_copy_cmd_action
1069 window_copy_cmd_copy_pipe_end_of_line(struct window_copy_cmd_state
*cs
)
1071 return (window_copy_do_copy_end_of_line(cs
, 1, 0));
1074 static enum window_copy_cmd_action
1075 window_copy_cmd_copy_pipe_end_of_line_and_cancel(
1076 struct window_copy_cmd_state
*cs
)
1078 return (window_copy_do_copy_end_of_line(cs
, 1, 1));
1081 static enum window_copy_cmd_action
1082 window_copy_do_copy_line(struct window_copy_cmd_state
*cs
, int pipe
, int cancel
)
1084 struct window_mode_entry
*wme
= cs
->wme
;
1085 struct client
*c
= cs
->c
;
1086 struct session
*s
= cs
->s
;
1087 struct winlink
*wl
= cs
->wl
;
1088 struct window_pane
*wp
= wme
->wp
;
1089 struct window_copy_mode_data
*data
= wme
->data
;
1090 u_int count
= args_count(cs
->args
);
1091 u_int np
= wme
->prefix
, ocx
, ocy
, ooy
;
1092 char *prefix
= NULL
, *command
= NULL
;
1093 const char *arg1
= args_string(cs
->args
, 1);
1094 const char *arg2
= args_string(cs
->args
, 2);
1098 prefix
= format_single(NULL
, arg2
, c
, s
, wl
, wp
);
1099 if (s
!= NULL
&& count
> 1 && *arg1
!= '\0')
1100 command
= format_single(NULL
, arg1
, c
, s
, wl
, wp
);
1103 prefix
= format_single(NULL
, arg1
, c
, s
, wl
, wp
);
1110 data
->selflag
= SEL_CHAR
;
1111 window_copy_cursor_start_of_line(wme
);
1112 window_copy_start_selection(wme
);
1113 for (; np
> 1; np
--)
1114 window_copy_cursor_down(wme
, 0);
1115 window_copy_cursor_end_of_line(wme
);
1119 window_copy_copy_pipe(wme
, s
, prefix
, command
);
1121 window_copy_copy_selection(wme
, prefix
);
1126 return (WINDOW_COPY_CMD_CANCEL
);
1129 window_copy_clear_selection(wme
);
1137 return (WINDOW_COPY_CMD_REDRAW
);
1140 static enum window_copy_cmd_action
1141 window_copy_cmd_copy_line(struct window_copy_cmd_state
*cs
)
1143 return (window_copy_do_copy_line(cs
, 0, 0));
1146 static enum window_copy_cmd_action
1147 window_copy_cmd_copy_line_and_cancel(struct window_copy_cmd_state
*cs
)
1149 return (window_copy_do_copy_line(cs
, 0, 1));
1152 static enum window_copy_cmd_action
1153 window_copy_cmd_copy_pipe_line(struct window_copy_cmd_state
*cs
)
1155 return (window_copy_do_copy_line(cs
, 1, 0));
1158 static enum window_copy_cmd_action
1159 window_copy_cmd_copy_pipe_line_and_cancel(struct window_copy_cmd_state
*cs
)
1161 return (window_copy_do_copy_line(cs
, 1, 1));
1164 static enum window_copy_cmd_action
1165 window_copy_cmd_copy_selection_no_clear(struct window_copy_cmd_state
*cs
)
1167 struct window_mode_entry
*wme
= cs
->wme
;
1168 struct client
*c
= cs
->c
;
1169 struct session
*s
= cs
->s
;
1170 struct winlink
*wl
= cs
->wl
;
1171 struct window_pane
*wp
= wme
->wp
;
1172 char *prefix
= NULL
;
1173 const char *arg1
= args_string(cs
->args
, 1);
1176 prefix
= format_single(NULL
, arg1
, c
, s
, wl
, wp
);
1179 window_copy_copy_selection(wme
, prefix
);
1182 return (WINDOW_COPY_CMD_NOTHING
);
1185 static enum window_copy_cmd_action
1186 window_copy_cmd_copy_selection(struct window_copy_cmd_state
*cs
)
1188 struct window_mode_entry
*wme
= cs
->wme
;
1190 window_copy_cmd_copy_selection_no_clear(cs
);
1191 window_copy_clear_selection(wme
);
1192 return (WINDOW_COPY_CMD_REDRAW
);
1195 static enum window_copy_cmd_action
1196 window_copy_cmd_copy_selection_and_cancel(struct window_copy_cmd_state
*cs
)
1198 struct window_mode_entry
*wme
= cs
->wme
;
1200 window_copy_cmd_copy_selection_no_clear(cs
);
1201 window_copy_clear_selection(wme
);
1202 return (WINDOW_COPY_CMD_CANCEL
);
1205 static enum window_copy_cmd_action
1206 window_copy_cmd_cursor_down(struct window_copy_cmd_state
*cs
)
1208 struct window_mode_entry
*wme
= cs
->wme
;
1209 u_int np
= wme
->prefix
;
1211 for (; np
!= 0; np
--)
1212 window_copy_cursor_down(wme
, 0);
1213 return (WINDOW_COPY_CMD_NOTHING
);
1216 static enum window_copy_cmd_action
1217 window_copy_cmd_cursor_down_and_cancel(struct window_copy_cmd_state
*cs
)
1219 struct window_mode_entry
*wme
= cs
->wme
;
1220 struct window_copy_mode_data
*data
= wme
->data
;
1221 u_int np
= wme
->prefix
, cy
;
1224 for (; np
!= 0; np
--)
1225 window_copy_cursor_down(wme
, 0);
1226 if (cy
== data
->cy
&& data
->oy
== 0)
1227 return (WINDOW_COPY_CMD_CANCEL
);
1228 return (WINDOW_COPY_CMD_NOTHING
);
1231 static enum window_copy_cmd_action
1232 window_copy_cmd_cursor_left(struct window_copy_cmd_state
*cs
)
1234 struct window_mode_entry
*wme
= cs
->wme
;
1235 u_int np
= wme
->prefix
;
1237 for (; np
!= 0; np
--)
1238 window_copy_cursor_left(wme
);
1239 return (WINDOW_COPY_CMD_NOTHING
);
1242 static enum window_copy_cmd_action
1243 window_copy_cmd_cursor_right(struct window_copy_cmd_state
*cs
)
1245 struct window_mode_entry
*wme
= cs
->wme
;
1246 struct window_copy_mode_data
*data
= wme
->data
;
1247 u_int np
= wme
->prefix
;
1249 for (; np
!= 0; np
--) {
1250 window_copy_cursor_right(wme
, data
->screen
.sel
!= NULL
&&
1253 return (WINDOW_COPY_CMD_NOTHING
);
1256 /* Scroll line containing the cursor to the given position. */
1257 static enum window_copy_cmd_action
1258 window_copy_cmd_scroll_to(struct window_copy_cmd_state
*cs
, u_int to
)
1260 struct window_mode_entry
*wme
= cs
->wme
;
1261 struct window_copy_mode_data
*data
= wme
->data
;
1263 int scroll_up
; /* >0 up, <0 down */
1265 scroll_up
= data
->cy
- to
;
1266 delta
= abs(scroll_up
);
1267 oy
= screen_hsize(data
->backing
) - data
->oy
;
1270 * oy is the maximum scroll down amount, while data->oy is the maximum
1273 if (scroll_up
> 0 && data
->oy
>= delta
) {
1274 window_copy_scroll_up(wme
, delta
);
1276 } else if (scroll_up
< 0 && oy
>= delta
) {
1277 window_copy_scroll_down(wme
, delta
);
1281 window_copy_update_selection(wme
, 0, 0);
1282 return (WINDOW_COPY_CMD_REDRAW
);
1285 /* Scroll line containing the cursor to the bottom. */
1286 static enum window_copy_cmd_action
1287 window_copy_cmd_scroll_bottom(struct window_copy_cmd_state
*cs
)
1289 struct window_copy_mode_data
*data
= cs
->wme
->data
;
1292 bottom
= screen_size_y(&data
->screen
) - 1;
1293 return (window_copy_cmd_scroll_to(cs
, bottom
));
1296 /* Scroll line containing the cursor to the middle. */
1297 static enum window_copy_cmd_action
1298 window_copy_cmd_scroll_middle(struct window_copy_cmd_state
*cs
)
1300 struct window_copy_mode_data
*data
= cs
->wme
->data
;
1303 mid_value
= (screen_size_y(&data
->screen
) - 1) / 2;
1304 return (window_copy_cmd_scroll_to(cs
, mid_value
));
1307 /* Scroll line containing the cursor to the top. */
1308 static enum window_copy_cmd_action
1309 window_copy_cmd_scroll_top(struct window_copy_cmd_state
*cs
)
1311 return (window_copy_cmd_scroll_to(cs
, 0));
1314 static enum window_copy_cmd_action
1315 window_copy_cmd_cursor_up(struct window_copy_cmd_state
*cs
)
1317 struct window_mode_entry
*wme
= cs
->wme
;
1318 u_int np
= wme
->prefix
;
1320 for (; np
!= 0; np
--)
1321 window_copy_cursor_up(wme
, 0);
1322 return (WINDOW_COPY_CMD_NOTHING
);
1325 static enum window_copy_cmd_action
1326 window_copy_cmd_end_of_line(struct window_copy_cmd_state
*cs
)
1328 struct window_mode_entry
*wme
= cs
->wme
;
1330 window_copy_cursor_end_of_line(wme
);
1331 return (WINDOW_COPY_CMD_NOTHING
);
1334 static enum window_copy_cmd_action
1335 window_copy_cmd_halfpage_down(struct window_copy_cmd_state
*cs
)
1337 struct window_mode_entry
*wme
= cs
->wme
;
1338 struct window_copy_mode_data
*data
= wme
->data
;
1339 u_int np
= wme
->prefix
;
1341 for (; np
!= 0; np
--) {
1342 if (window_copy_pagedown(wme
, 1, data
->scroll_exit
))
1343 return (WINDOW_COPY_CMD_CANCEL
);
1345 return (WINDOW_COPY_CMD_NOTHING
);
1348 static enum window_copy_cmd_action
1349 window_copy_cmd_halfpage_down_and_cancel(struct window_copy_cmd_state
*cs
)
1352 struct window_mode_entry
*wme
= cs
->wme
;
1353 u_int np
= wme
->prefix
;
1355 for (; np
!= 0; np
--) {
1356 if (window_copy_pagedown(wme
, 1, 1))
1357 return (WINDOW_COPY_CMD_CANCEL
);
1359 return (WINDOW_COPY_CMD_NOTHING
);
1362 static enum window_copy_cmd_action
1363 window_copy_cmd_halfpage_up(struct window_copy_cmd_state
*cs
)
1365 struct window_mode_entry
*wme
= cs
->wme
;
1366 u_int np
= wme
->prefix
;
1368 for (; np
!= 0; np
--)
1369 window_copy_pageup1(wme
, 1);
1370 return (WINDOW_COPY_CMD_NOTHING
);
1373 static enum window_copy_cmd_action
1374 window_copy_cmd_toggle_position(struct window_copy_cmd_state
*cs
)
1376 struct window_mode_entry
*wme
= cs
->wme
;
1377 struct window_copy_mode_data
*data
= wme
->data
;
1379 data
->hide_position
= !data
->hide_position
;
1380 return (WINDOW_COPY_CMD_REDRAW
);
1383 static enum window_copy_cmd_action
1384 window_copy_cmd_history_bottom(struct window_copy_cmd_state
*cs
)
1386 struct window_mode_entry
*wme
= cs
->wme
;
1387 struct window_copy_mode_data
*data
= wme
->data
;
1388 struct screen
*s
= data
->backing
;
1391 oy
= screen_hsize(s
) + data
->cy
- data
->oy
;
1392 if (data
->lineflag
== LINE_SEL_RIGHT_LEFT
&& oy
== data
->endsely
)
1393 window_copy_other_end(wme
);
1395 data
->cy
= screen_size_y(&data
->screen
) - 1;
1396 data
->cx
= window_copy_find_length(wme
, screen_hsize(s
) + data
->cy
);
1399 if (data
->searchmark
!= NULL
&& !data
->timeout
)
1400 window_copy_search_marks(wme
, NULL
, data
->searchregex
, 1);
1401 window_copy_update_selection(wme
, 1, 0);
1402 return (WINDOW_COPY_CMD_REDRAW
);
1405 static enum window_copy_cmd_action
1406 window_copy_cmd_history_top(struct window_copy_cmd_state
*cs
)
1408 struct window_mode_entry
*wme
= cs
->wme
;
1409 struct window_copy_mode_data
*data
= wme
->data
;
1412 oy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1413 if (data
->lineflag
== LINE_SEL_LEFT_RIGHT
&& oy
== data
->sely
)
1414 window_copy_other_end(wme
);
1418 data
->oy
= screen_hsize(data
->backing
);
1420 if (data
->searchmark
!= NULL
&& !data
->timeout
)
1421 window_copy_search_marks(wme
, NULL
, data
->searchregex
, 1);
1422 window_copy_update_selection(wme
, 1, 0);
1423 return (WINDOW_COPY_CMD_REDRAW
);
1426 static enum window_copy_cmd_action
1427 window_copy_cmd_jump_again(struct window_copy_cmd_state
*cs
)
1429 struct window_mode_entry
*wme
= cs
->wme
;
1430 struct window_copy_mode_data
*data
= wme
->data
;
1431 u_int np
= wme
->prefix
;
1433 switch (data
->jumptype
) {
1434 case WINDOW_COPY_JUMPFORWARD
:
1435 for (; np
!= 0; np
--)
1436 window_copy_cursor_jump(wme
);
1438 case WINDOW_COPY_JUMPBACKWARD
:
1439 for (; np
!= 0; np
--)
1440 window_copy_cursor_jump_back(wme
);
1442 case WINDOW_COPY_JUMPTOFORWARD
:
1443 for (; np
!= 0; np
--)
1444 window_copy_cursor_jump_to(wme
);
1446 case WINDOW_COPY_JUMPTOBACKWARD
:
1447 for (; np
!= 0; np
--)
1448 window_copy_cursor_jump_to_back(wme
);
1451 return (WINDOW_COPY_CMD_NOTHING
);
1454 static enum window_copy_cmd_action
1455 window_copy_cmd_jump_reverse(struct window_copy_cmd_state
*cs
)
1457 struct window_mode_entry
*wme
= cs
->wme
;
1458 struct window_copy_mode_data
*data
= wme
->data
;
1459 u_int np
= wme
->prefix
;
1461 switch (data
->jumptype
) {
1462 case WINDOW_COPY_JUMPFORWARD
:
1463 for (; np
!= 0; np
--)
1464 window_copy_cursor_jump_back(wme
);
1466 case WINDOW_COPY_JUMPBACKWARD
:
1467 for (; np
!= 0; np
--)
1468 window_copy_cursor_jump(wme
);
1470 case WINDOW_COPY_JUMPTOFORWARD
:
1471 for (; np
!= 0; np
--)
1472 window_copy_cursor_jump_to_back(wme
);
1474 case WINDOW_COPY_JUMPTOBACKWARD
:
1475 for (; np
!= 0; np
--)
1476 window_copy_cursor_jump_to(wme
);
1479 return (WINDOW_COPY_CMD_NOTHING
);
1482 static enum window_copy_cmd_action
1483 window_copy_cmd_middle_line(struct window_copy_cmd_state
*cs
)
1485 struct window_mode_entry
*wme
= cs
->wme
;
1486 struct window_copy_mode_data
*data
= wme
->data
;
1489 data
->cy
= (screen_size_y(&data
->screen
) - 1) / 2;
1491 window_copy_update_selection(wme
, 1, 0);
1492 return (WINDOW_COPY_CMD_REDRAW
);
1495 static enum window_copy_cmd_action
1496 window_copy_cmd_previous_matching_bracket(struct window_copy_cmd_state
*cs
)
1498 struct window_mode_entry
*wme
= cs
->wme
;
1499 u_int np
= wme
->prefix
;
1500 struct window_copy_mode_data
*data
= wme
->data
;
1501 struct screen
*s
= data
->backing
;
1502 char open
[] = "{[(", close
[] = "}])";
1503 char tried
, found
, start
, *cp
;
1504 u_int px
, py
, xx
, n
;
1505 struct grid_cell gc
;
1508 for (; np
!= 0; np
--) {
1509 /* Get cursor position and line length. */
1511 py
= screen_hsize(s
) + data
->cy
- data
->oy
;
1512 xx
= window_copy_find_length(wme
, py
);
1517 * Get the current character. If not on a bracket, try the
1518 * previous. If still not, then behave like previous-word.
1522 grid_get_cell(s
->grid
, px
, py
, &gc
);
1523 if (gc
.data
.size
!= 1 || (gc
.flags
& GRID_FLAG_PADDING
))
1526 found
= *gc
.data
.data
;
1527 cp
= strchr(close
, found
);
1530 if (data
->modekeys
== MODEKEY_EMACS
) {
1531 if (!tried
&& px
> 0) {
1536 window_copy_cursor_previous_word(wme
, close
, 1);
1540 start
= open
[cp
- close
];
1542 /* Walk backward until the matching bracket is reached. */
1553 xx
= window_copy_find_length(wme
, py
);
1554 } while (xx
== 0 && py
> 0);
1555 if (xx
== 0 && py
== 0) {
1563 grid_get_cell(s
->grid
, px
, py
, &gc
);
1564 if (gc
.data
.size
== 1 &&
1565 (~gc
.flags
& GRID_FLAG_PADDING
)) {
1566 if (*gc
.data
.data
== found
)
1568 else if (*gc
.data
.data
== start
)
1573 /* Move the cursor to the found location if any. */
1575 window_copy_scroll_to(wme
, px
, py
, 0);
1578 return (WINDOW_COPY_CMD_NOTHING
);
1581 static enum window_copy_cmd_action
1582 window_copy_cmd_next_matching_bracket(struct window_copy_cmd_state
*cs
)
1584 struct window_mode_entry
*wme
= cs
->wme
;
1585 u_int np
= wme
->prefix
;
1586 struct window_copy_mode_data
*data
= wme
->data
;
1587 struct screen
*s
= data
->backing
;
1588 char open
[] = "{[(", close
[] = "}])";
1589 char tried
, found
, end
, *cp
;
1590 u_int px
, py
, xx
, yy
, sx
, sy
, n
;
1591 struct grid_cell gc
;
1593 struct grid_line
*gl
;
1595 for (; np
!= 0; np
--) {
1596 /* Get cursor position and line length. */
1598 py
= screen_hsize(s
) + data
->cy
- data
->oy
;
1599 xx
= window_copy_find_length(wme
, py
);
1600 yy
= screen_hsize(s
) + screen_size_y(s
) - 1;
1605 * Get the current character. If not on a bracket, try the
1606 * next. If still not, then behave like next-word.
1610 grid_get_cell(s
->grid
, px
, py
, &gc
);
1611 if (gc
.data
.size
!= 1 || (gc
.flags
& GRID_FLAG_PADDING
))
1614 found
= *gc
.data
.data
;
1617 * In vi mode, attempt to move to previous bracket if a
1618 * closing bracket is found first. If this fails,
1619 * return to the original cursor position.
1621 cp
= strchr(close
, found
);
1622 if (cp
!= NULL
&& data
->modekeys
== MODEKEY_VI
) {
1624 sy
= screen_hsize(s
) + data
->cy
- data
->oy
;
1626 window_copy_scroll_to(wme
, px
, py
, 0);
1627 window_copy_cmd_previous_matching_bracket(cs
);
1630 py
= screen_hsize(s
) + data
->cy
- data
->oy
;
1631 grid_get_cell(s
->grid
, px
, py
, &gc
);
1632 if (gc
.data
.size
== 1 &&
1633 (~gc
.flags
& GRID_FLAG_PADDING
) &&
1634 strchr(close
, *gc
.data
.data
) != NULL
)
1635 window_copy_scroll_to(wme
, sx
, sy
, 0);
1639 cp
= strchr(open
, found
);
1642 if (data
->modekeys
== MODEKEY_EMACS
) {
1643 if (!tried
&& px
<= xx
) {
1648 window_copy_cursor_next_word_end(wme
, open
, 0);
1651 /* For vi, continue searching for bracket until EOL. */
1655 gl
= grid_get_line(s
->grid
, py
);
1656 if (~gl
->flags
& GRID_LINE_WRAPPED
)
1658 if (gl
->cellsize
> s
->grid
->sx
)
1662 xx
= window_copy_find_length(wme
, py
);
1667 end
= close
[cp
- open
];
1669 /* Walk forward until the matching bracket is reached. */
1680 xx
= window_copy_find_length(wme
, py
);
1684 grid_get_cell(s
->grid
, px
, py
, &gc
);
1685 if (gc
.data
.size
== 1 &&
1686 (~gc
.flags
& GRID_FLAG_PADDING
)) {
1687 if (*gc
.data
.data
== found
)
1689 else if (*gc
.data
.data
== end
)
1694 /* Move the cursor to the found location if any. */
1696 window_copy_scroll_to(wme
, px
, py
, 0);
1699 return (WINDOW_COPY_CMD_NOTHING
);
1702 static enum window_copy_cmd_action
1703 window_copy_cmd_next_paragraph(struct window_copy_cmd_state
*cs
)
1705 struct window_mode_entry
*wme
= cs
->wme
;
1706 u_int np
= wme
->prefix
;
1708 for (; np
!= 0; np
--)
1709 window_copy_next_paragraph(wme
);
1710 return (WINDOW_COPY_CMD_NOTHING
);
1713 static enum window_copy_cmd_action
1714 window_copy_cmd_next_space(struct window_copy_cmd_state
*cs
)
1716 struct window_mode_entry
*wme
= cs
->wme
;
1717 u_int np
= wme
->prefix
;
1719 for (; np
!= 0; np
--)
1720 window_copy_cursor_next_word(wme
, "");
1721 return (WINDOW_COPY_CMD_NOTHING
);
1724 static enum window_copy_cmd_action
1725 window_copy_cmd_next_space_end(struct window_copy_cmd_state
*cs
)
1727 struct window_mode_entry
*wme
= cs
->wme
;
1728 u_int np
= wme
->prefix
;
1730 for (; np
!= 0; np
--)
1731 window_copy_cursor_next_word_end(wme
, "", 0);
1732 return (WINDOW_COPY_CMD_NOTHING
);
1735 static enum window_copy_cmd_action
1736 window_copy_cmd_next_word(struct window_copy_cmd_state
*cs
)
1738 struct window_mode_entry
*wme
= cs
->wme
;
1739 u_int np
= wme
->prefix
;
1740 const char *separators
;
1742 separators
= options_get_string(cs
->s
->options
, "word-separators");
1744 for (; np
!= 0; np
--)
1745 window_copy_cursor_next_word(wme
, separators
);
1746 return (WINDOW_COPY_CMD_NOTHING
);
1749 static enum window_copy_cmd_action
1750 window_copy_cmd_next_word_end(struct window_copy_cmd_state
*cs
)
1752 struct window_mode_entry
*wme
= cs
->wme
;
1753 u_int np
= wme
->prefix
;
1754 const char *separators
;
1756 separators
= options_get_string(cs
->s
->options
, "word-separators");
1758 for (; np
!= 0; np
--)
1759 window_copy_cursor_next_word_end(wme
, separators
, 0);
1760 return (WINDOW_COPY_CMD_NOTHING
);
1763 static enum window_copy_cmd_action
1764 window_copy_cmd_other_end(struct window_copy_cmd_state
*cs
)
1766 struct window_mode_entry
*wme
= cs
->wme
;
1767 u_int np
= wme
->prefix
;
1768 struct window_copy_mode_data
*data
= wme
->data
;
1770 data
->selflag
= SEL_CHAR
;
1772 window_copy_other_end(wme
);
1773 return (WINDOW_COPY_CMD_NOTHING
);
1776 static enum window_copy_cmd_action
1777 window_copy_cmd_page_down(struct window_copy_cmd_state
*cs
)
1779 struct window_mode_entry
*wme
= cs
->wme
;
1780 struct window_copy_mode_data
*data
= wme
->data
;
1781 u_int np
= wme
->prefix
;
1783 for (; np
!= 0; np
--) {
1784 if (window_copy_pagedown(wme
, 0, data
->scroll_exit
))
1785 return (WINDOW_COPY_CMD_CANCEL
);
1787 return (WINDOW_COPY_CMD_NOTHING
);
1790 static enum window_copy_cmd_action
1791 window_copy_cmd_page_down_and_cancel(struct window_copy_cmd_state
*cs
)
1793 struct window_mode_entry
*wme
= cs
->wme
;
1794 u_int np
= wme
->prefix
;
1796 for (; np
!= 0; np
--) {
1797 if (window_copy_pagedown(wme
, 0, 1))
1798 return (WINDOW_COPY_CMD_CANCEL
);
1800 return (WINDOW_COPY_CMD_NOTHING
);
1803 static enum window_copy_cmd_action
1804 window_copy_cmd_page_up(struct window_copy_cmd_state
*cs
)
1806 struct window_mode_entry
*wme
= cs
->wme
;
1807 u_int np
= wme
->prefix
;
1809 for (; np
!= 0; np
--)
1810 window_copy_pageup1(wme
, 0);
1811 return (WINDOW_COPY_CMD_NOTHING
);
1814 static enum window_copy_cmd_action
1815 window_copy_cmd_previous_paragraph(struct window_copy_cmd_state
*cs
)
1817 struct window_mode_entry
*wme
= cs
->wme
;
1818 u_int np
= wme
->prefix
;
1820 for (; np
!= 0; np
--)
1821 window_copy_previous_paragraph(wme
);
1822 return (WINDOW_COPY_CMD_NOTHING
);
1825 static enum window_copy_cmd_action
1826 window_copy_cmd_previous_space(struct window_copy_cmd_state
*cs
)
1828 struct window_mode_entry
*wme
= cs
->wme
;
1829 u_int np
= wme
->prefix
;
1831 for (; np
!= 0; np
--)
1832 window_copy_cursor_previous_word(wme
, "", 1);
1833 return (WINDOW_COPY_CMD_NOTHING
);
1836 static enum window_copy_cmd_action
1837 window_copy_cmd_previous_word(struct window_copy_cmd_state
*cs
)
1839 struct window_mode_entry
*wme
= cs
->wme
;
1840 u_int np
= wme
->prefix
;
1841 const char *separators
;
1843 separators
= options_get_string(cs
->s
->options
, "word-separators");
1845 for (; np
!= 0; np
--)
1846 window_copy_cursor_previous_word(wme
, separators
, 1);
1847 return (WINDOW_COPY_CMD_NOTHING
);
1850 static enum window_copy_cmd_action
1851 window_copy_cmd_rectangle_on(struct window_copy_cmd_state
*cs
)
1853 struct window_mode_entry
*wme
= cs
->wme
;
1854 struct window_copy_mode_data
*data
= wme
->data
;
1856 data
->lineflag
= LINE_SEL_NONE
;
1857 window_copy_rectangle_set(wme
, 1);
1859 return (WINDOW_COPY_CMD_NOTHING
);
1862 static enum window_copy_cmd_action
1863 window_copy_cmd_rectangle_off(struct window_copy_cmd_state
*cs
)
1865 struct window_mode_entry
*wme
= cs
->wme
;
1866 struct window_copy_mode_data
*data
= wme
->data
;
1868 data
->lineflag
= LINE_SEL_NONE
;
1869 window_copy_rectangle_set(wme
, 0);
1871 return (WINDOW_COPY_CMD_NOTHING
);
1874 static enum window_copy_cmd_action
1875 window_copy_cmd_rectangle_toggle(struct window_copy_cmd_state
*cs
)
1877 struct window_mode_entry
*wme
= cs
->wme
;
1878 struct window_copy_mode_data
*data
= wme
->data
;
1880 data
->lineflag
= LINE_SEL_NONE
;
1881 window_copy_rectangle_set(wme
, !data
->rectflag
);
1883 return (WINDOW_COPY_CMD_NOTHING
);
1886 static enum window_copy_cmd_action
1887 window_copy_cmd_scroll_down(struct window_copy_cmd_state
*cs
)
1889 struct window_mode_entry
*wme
= cs
->wme
;
1890 struct window_copy_mode_data
*data
= wme
->data
;
1891 u_int np
= wme
->prefix
;
1893 for (; np
!= 0; np
--)
1894 window_copy_cursor_down(wme
, 1);
1895 if (data
->scroll_exit
&& data
->oy
== 0)
1896 return (WINDOW_COPY_CMD_CANCEL
);
1897 return (WINDOW_COPY_CMD_NOTHING
);
1900 static enum window_copy_cmd_action
1901 window_copy_cmd_scroll_down_and_cancel(struct window_copy_cmd_state
*cs
)
1903 struct window_mode_entry
*wme
= cs
->wme
;
1904 struct window_copy_mode_data
*data
= wme
->data
;
1905 u_int np
= wme
->prefix
;
1907 for (; np
!= 0; np
--)
1908 window_copy_cursor_down(wme
, 1);
1910 return (WINDOW_COPY_CMD_CANCEL
);
1911 return (WINDOW_COPY_CMD_NOTHING
);
1914 static enum window_copy_cmd_action
1915 window_copy_cmd_scroll_up(struct window_copy_cmd_state
*cs
)
1917 struct window_mode_entry
*wme
= cs
->wme
;
1918 u_int np
= wme
->prefix
;
1920 for (; np
!= 0; np
--)
1921 window_copy_cursor_up(wme
, 1);
1922 return (WINDOW_COPY_CMD_NOTHING
);
1925 static enum window_copy_cmd_action
1926 window_copy_cmd_search_again(struct window_copy_cmd_state
*cs
)
1928 struct window_mode_entry
*wme
= cs
->wme
;
1929 struct window_copy_mode_data
*data
= wme
->data
;
1930 u_int np
= wme
->prefix
;
1932 if (data
->searchtype
== WINDOW_COPY_SEARCHUP
) {
1933 for (; np
!= 0; np
--)
1934 window_copy_search_up(wme
, data
->searchregex
);
1935 } else if (data
->searchtype
== WINDOW_COPY_SEARCHDOWN
) {
1936 for (; np
!= 0; np
--)
1937 window_copy_search_down(wme
, data
->searchregex
);
1939 return (WINDOW_COPY_CMD_NOTHING
);
1942 static enum window_copy_cmd_action
1943 window_copy_cmd_search_reverse(struct window_copy_cmd_state
*cs
)
1945 struct window_mode_entry
*wme
= cs
->wme
;
1946 struct window_copy_mode_data
*data
= wme
->data
;
1947 u_int np
= wme
->prefix
;
1949 if (data
->searchtype
== WINDOW_COPY_SEARCHUP
) {
1950 for (; np
!= 0; np
--)
1951 window_copy_search_down(wme
, data
->searchregex
);
1952 } else if (data
->searchtype
== WINDOW_COPY_SEARCHDOWN
) {
1953 for (; np
!= 0; np
--)
1954 window_copy_search_up(wme
, data
->searchregex
);
1956 return (WINDOW_COPY_CMD_NOTHING
);
1959 static enum window_copy_cmd_action
1960 window_copy_cmd_select_line(struct window_copy_cmd_state
*cs
)
1962 struct window_mode_entry
*wme
= cs
->wme
;
1963 struct window_copy_mode_data
*data
= wme
->data
;
1964 u_int np
= wme
->prefix
;
1966 data
->lineflag
= LINE_SEL_LEFT_RIGHT
;
1968 data
->selflag
= SEL_LINE
;
1969 data
->dx
= data
->cx
;
1970 data
->dy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1972 window_copy_cursor_start_of_line(wme
);
1973 data
->selrx
= data
->cx
;
1974 data
->selry
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1975 data
->endselry
= data
->selry
;
1976 window_copy_start_selection(wme
);
1977 window_copy_cursor_end_of_line(wme
);
1978 data
->endselry
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1979 data
->endselrx
= window_copy_find_length(wme
, data
->endselry
);
1980 for (; np
> 1; np
--) {
1981 window_copy_cursor_down(wme
, 0);
1982 window_copy_cursor_end_of_line(wme
);
1985 return (WINDOW_COPY_CMD_REDRAW
);
1988 static enum window_copy_cmd_action
1989 window_copy_cmd_select_word(struct window_copy_cmd_state
*cs
)
1991 struct window_mode_entry
*wme
= cs
->wme
;
1992 struct options
*session_options
= cs
->s
->options
;
1993 struct window_copy_mode_data
*data
= wme
->data
;
1994 u_int px
, py
, nextx
, nexty
;
1996 data
->lineflag
= LINE_SEL_LEFT_RIGHT
;
1998 data
->selflag
= SEL_WORD
;
1999 data
->dx
= data
->cx
;
2000 data
->dy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
2002 data
->separators
= options_get_string(session_options
,
2004 window_copy_cursor_previous_word(wme
, data
->separators
, 0);
2006 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
2009 window_copy_start_selection(wme
);
2011 /* Handle single character words. */
2014 if (grid_get_line(data
->backing
->grid
, nexty
)->flags
&
2015 GRID_LINE_WRAPPED
&& nextx
> screen_size_x(data
->backing
) - 1) {
2019 if (px
>= window_copy_find_length(wme
, py
) ||
2020 !window_copy_in_set(wme
, nextx
, nexty
, WHITESPACE
))
2021 window_copy_cursor_next_word_end(wme
, data
->separators
, 1);
2023 window_copy_update_cursor(wme
, px
, data
->cy
);
2024 if (window_copy_update_selection(wme
, 1, 1))
2025 window_copy_redraw_lines(wme
, data
->cy
, 1);
2027 data
->endselrx
= data
->cx
;
2028 data
->endselry
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
2029 if (data
->dy
> data
->endselry
) {
2030 data
->dy
= data
->endselry
;
2031 data
->dx
= data
->endselrx
;
2032 } else if (data
->dx
> data
->endselrx
)
2033 data
->dx
= data
->endselrx
;
2035 return (WINDOW_COPY_CMD_REDRAW
);
2038 static enum window_copy_cmd_action
2039 window_copy_cmd_set_mark(struct window_copy_cmd_state
*cs
)
2041 struct window_copy_mode_data
*data
= cs
->wme
->data
;
2043 data
->mx
= data
->cx
;
2044 data
->my
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
2046 return (WINDOW_COPY_CMD_REDRAW
);
2049 static enum window_copy_cmd_action
2050 window_copy_cmd_start_of_line(struct window_copy_cmd_state
*cs
)
2052 struct window_mode_entry
*wme
= cs
->wme
;
2054 window_copy_cursor_start_of_line(wme
);
2055 return (WINDOW_COPY_CMD_NOTHING
);
2058 static enum window_copy_cmd_action
2059 window_copy_cmd_top_line(struct window_copy_cmd_state
*cs
)
2061 struct window_mode_entry
*wme
= cs
->wme
;
2062 struct window_copy_mode_data
*data
= wme
->data
;
2067 window_copy_update_selection(wme
, 1, 0);
2068 return (WINDOW_COPY_CMD_REDRAW
);
2071 static enum window_copy_cmd_action
2072 window_copy_cmd_copy_pipe_no_clear(struct window_copy_cmd_state
*cs
)
2074 struct window_mode_entry
*wme
= cs
->wme
;
2075 struct client
*c
= cs
->c
;
2076 struct session
*s
= cs
->s
;
2077 struct winlink
*wl
= cs
->wl
;
2078 struct window_pane
*wp
= wme
->wp
;
2079 char *command
= NULL
, *prefix
= NULL
;
2080 const char *arg1
= args_string(cs
->args
, 1);
2081 const char *arg2
= args_string(cs
->args
, 2);
2084 prefix
= format_single(NULL
, arg2
, c
, s
, wl
, wp
);
2086 if (s
!= NULL
&& arg1
!= NULL
&& *arg1
!= '\0')
2087 command
= format_single(NULL
, arg1
, c
, s
, wl
, wp
);
2088 window_copy_copy_pipe(wme
, s
, prefix
, command
);
2092 return (WINDOW_COPY_CMD_NOTHING
);
2095 static enum window_copy_cmd_action
2096 window_copy_cmd_copy_pipe(struct window_copy_cmd_state
*cs
)
2098 struct window_mode_entry
*wme
= cs
->wme
;
2100 window_copy_cmd_copy_pipe_no_clear(cs
);
2101 window_copy_clear_selection(wme
);
2102 return (WINDOW_COPY_CMD_REDRAW
);
2105 static enum window_copy_cmd_action
2106 window_copy_cmd_copy_pipe_and_cancel(struct window_copy_cmd_state
*cs
)
2108 struct window_mode_entry
*wme
= cs
->wme
;
2110 window_copy_cmd_copy_pipe_no_clear(cs
);
2111 window_copy_clear_selection(wme
);
2112 return (WINDOW_COPY_CMD_CANCEL
);
2115 static enum window_copy_cmd_action
2116 window_copy_cmd_pipe_no_clear(struct window_copy_cmd_state
*cs
)
2118 struct window_mode_entry
*wme
= cs
->wme
;
2119 struct client
*c
= cs
->c
;
2120 struct session
*s
= cs
->s
;
2121 struct winlink
*wl
= cs
->wl
;
2122 struct window_pane
*wp
= wme
->wp
;
2123 char *command
= NULL
;
2124 const char *arg1
= args_string(cs
->args
, 1);
2126 if (s
!= NULL
&& arg1
!= NULL
&& *arg1
!= '\0')
2127 command
= format_single(NULL
, arg1
, c
, s
, wl
, wp
);
2128 window_copy_pipe(wme
, s
, command
);
2131 return (WINDOW_COPY_CMD_NOTHING
);
2134 static enum window_copy_cmd_action
2135 window_copy_cmd_pipe(struct window_copy_cmd_state
*cs
)
2137 struct window_mode_entry
*wme
= cs
->wme
;
2139 window_copy_cmd_pipe_no_clear(cs
);
2140 window_copy_clear_selection(wme
);
2141 return (WINDOW_COPY_CMD_REDRAW
);
2144 static enum window_copy_cmd_action
2145 window_copy_cmd_pipe_and_cancel(struct window_copy_cmd_state
*cs
)
2147 struct window_mode_entry
*wme
= cs
->wme
;
2149 window_copy_cmd_pipe_no_clear(cs
);
2150 window_copy_clear_selection(wme
);
2151 return (WINDOW_COPY_CMD_CANCEL
);
2154 static enum window_copy_cmd_action
2155 window_copy_cmd_goto_line(struct window_copy_cmd_state
*cs
)
2157 struct window_mode_entry
*wme
= cs
->wme
;
2158 const char *arg1
= args_string(cs
->args
, 1);
2161 window_copy_goto_line(wme
, arg1
);
2162 return (WINDOW_COPY_CMD_NOTHING
);
2165 static enum window_copy_cmd_action
2166 window_copy_cmd_jump_backward(struct window_copy_cmd_state
*cs
)
2168 struct window_mode_entry
*wme
= cs
->wme
;
2169 struct window_copy_mode_data
*data
= wme
->data
;
2170 u_int np
= wme
->prefix
;
2171 const char *arg1
= args_string(cs
->args
, 1);
2173 if (*arg1
!= '\0') {
2174 data
->jumptype
= WINDOW_COPY_JUMPBACKWARD
;
2175 free(data
->jumpchar
);
2176 data
->jumpchar
= utf8_fromcstr(arg1
);
2177 for (; np
!= 0; np
--)
2178 window_copy_cursor_jump_back(wme
);
2180 return (WINDOW_COPY_CMD_NOTHING
);
2183 static enum window_copy_cmd_action
2184 window_copy_cmd_jump_forward(struct window_copy_cmd_state
*cs
)
2186 struct window_mode_entry
*wme
= cs
->wme
;
2187 struct window_copy_mode_data
*data
= wme
->data
;
2188 u_int np
= wme
->prefix
;
2189 const char *arg1
= args_string(cs
->args
, 1);
2191 if (*arg1
!= '\0') {
2192 data
->jumptype
= WINDOW_COPY_JUMPFORWARD
;
2193 free(data
->jumpchar
);
2194 data
->jumpchar
= utf8_fromcstr(arg1
);
2195 for (; np
!= 0; np
--)
2196 window_copy_cursor_jump(wme
);
2198 return (WINDOW_COPY_CMD_NOTHING
);
2201 static enum window_copy_cmd_action
2202 window_copy_cmd_jump_to_backward(struct window_copy_cmd_state
*cs
)
2204 struct window_mode_entry
*wme
= cs
->wme
;
2205 struct window_copy_mode_data
*data
= wme
->data
;
2206 u_int np
= wme
->prefix
;
2207 const char *arg1
= args_string(cs
->args
, 1);
2209 if (*arg1
!= '\0') {
2210 data
->jumptype
= WINDOW_COPY_JUMPTOBACKWARD
;
2211 free(data
->jumpchar
);
2212 data
->jumpchar
= utf8_fromcstr(arg1
);
2213 for (; np
!= 0; np
--)
2214 window_copy_cursor_jump_to_back(wme
);
2216 return (WINDOW_COPY_CMD_NOTHING
);
2219 static enum window_copy_cmd_action
2220 window_copy_cmd_jump_to_forward(struct window_copy_cmd_state
*cs
)
2222 struct window_mode_entry
*wme
= cs
->wme
;
2223 struct window_copy_mode_data
*data
= wme
->data
;
2224 u_int np
= wme
->prefix
;
2225 const char *arg1
= args_string(cs
->args
, 1);
2227 if (*arg1
!= '\0') {
2228 data
->jumptype
= WINDOW_COPY_JUMPTOFORWARD
;
2229 free(data
->jumpchar
);
2230 data
->jumpchar
= utf8_fromcstr(arg1
);
2231 for (; np
!= 0; np
--)
2232 window_copy_cursor_jump_to(wme
);
2234 return (WINDOW_COPY_CMD_NOTHING
);
2237 static enum window_copy_cmd_action
2238 window_copy_cmd_jump_to_mark(struct window_copy_cmd_state
*cs
)
2240 struct window_mode_entry
*wme
= cs
->wme
;
2242 window_copy_jump_to_mark(wme
);
2243 return (WINDOW_COPY_CMD_NOTHING
);
2246 static enum window_copy_cmd_action
2247 window_copy_cmd_next_prompt(struct window_copy_cmd_state
*cs
)
2249 struct window_mode_entry
*wme
= cs
->wme
;
2250 const char *arg1
= args_string(cs
->args
, 1);
2252 window_copy_cursor_prompt(wme
, 1, arg1
);
2253 return (WINDOW_COPY_CMD_NOTHING
);
2256 static enum window_copy_cmd_action
2257 window_copy_cmd_previous_prompt(struct window_copy_cmd_state
*cs
)
2259 struct window_mode_entry
*wme
= cs
->wme
;
2260 const char *arg1
= args_string(cs
->args
, 1);
2262 window_copy_cursor_prompt(wme
, 0, arg1
);
2263 return (WINDOW_COPY_CMD_NOTHING
);
2266 static enum window_copy_cmd_action
2267 window_copy_cmd_search_backward(struct window_copy_cmd_state
*cs
)
2269 struct window_mode_entry
*wme
= cs
->wme
;
2270 struct window_copy_mode_data
*data
= wme
->data
;
2271 u_int np
= wme
->prefix
;
2273 if (!window_copy_expand_search_string(cs
))
2274 return (WINDOW_COPY_CMD_NOTHING
);
2276 if (data
->searchstr
!= NULL
) {
2277 data
->searchtype
= WINDOW_COPY_SEARCHUP
;
2278 data
->searchregex
= 1;
2280 for (; np
!= 0; np
--)
2281 window_copy_search_up(wme
, 1);
2283 return (WINDOW_COPY_CMD_NOTHING
);
2286 static enum window_copy_cmd_action
2287 window_copy_cmd_search_backward_text(struct window_copy_cmd_state
*cs
)
2289 struct window_mode_entry
*wme
= cs
->wme
;
2290 struct window_copy_mode_data
*data
= wme
->data
;
2291 u_int np
= wme
->prefix
;
2293 if (!window_copy_expand_search_string(cs
))
2294 return (WINDOW_COPY_CMD_NOTHING
);
2296 if (data
->searchstr
!= NULL
) {
2297 data
->searchtype
= WINDOW_COPY_SEARCHUP
;
2298 data
->searchregex
= 0;
2300 for (; np
!= 0; np
--)
2301 window_copy_search_up(wme
, 0);
2303 return (WINDOW_COPY_CMD_NOTHING
);
2306 static enum window_copy_cmd_action
2307 window_copy_cmd_search_forward(struct window_copy_cmd_state
*cs
)
2309 struct window_mode_entry
*wme
= cs
->wme
;
2310 struct window_copy_mode_data
*data
= wme
->data
;
2311 u_int np
= wme
->prefix
;
2313 if (!window_copy_expand_search_string(cs
))
2314 return (WINDOW_COPY_CMD_NOTHING
);
2316 if (data
->searchstr
!= NULL
) {
2317 data
->searchtype
= WINDOW_COPY_SEARCHDOWN
;
2318 data
->searchregex
= 1;
2320 for (; np
!= 0; np
--)
2321 window_copy_search_down(wme
, 1);
2323 return (WINDOW_COPY_CMD_NOTHING
);
2326 static enum window_copy_cmd_action
2327 window_copy_cmd_search_forward_text(struct window_copy_cmd_state
*cs
)
2329 struct window_mode_entry
*wme
= cs
->wme
;
2330 struct window_copy_mode_data
*data
= wme
->data
;
2331 u_int np
= wme
->prefix
;
2333 if (!window_copy_expand_search_string(cs
))
2334 return (WINDOW_COPY_CMD_NOTHING
);
2336 if (data
->searchstr
!= NULL
) {
2337 data
->searchtype
= WINDOW_COPY_SEARCHDOWN
;
2338 data
->searchregex
= 0;
2340 for (; np
!= 0; np
--)
2341 window_copy_search_down(wme
, 0);
2343 return (WINDOW_COPY_CMD_NOTHING
);
2346 static enum window_copy_cmd_action
2347 window_copy_cmd_search_backward_incremental(struct window_copy_cmd_state
*cs
)
2349 struct window_mode_entry
*wme
= cs
->wme
;
2350 struct window_copy_mode_data
*data
= wme
->data
;
2351 const char *arg1
= args_string(cs
->args
, 1);
2352 const char *ss
= data
->searchstr
;
2354 enum window_copy_cmd_action action
= WINDOW_COPY_CMD_NOTHING
;
2358 log_debug("%s: %s", __func__
, arg1
);
2361 if (data
->searchx
== -1 || data
->searchy
== -1) {
2362 data
->searchx
= data
->cx
;
2363 data
->searchy
= data
->cy
;
2364 data
->searcho
= data
->oy
;
2365 } else if (ss
!= NULL
&& strcmp(arg1
, ss
) != 0) {
2366 data
->cx
= data
->searchx
;
2367 data
->cy
= data
->searchy
;
2368 data
->oy
= data
->searcho
;
2369 action
= WINDOW_COPY_CMD_REDRAW
;
2371 if (*arg1
== '\0') {
2372 window_copy_clear_marks(wme
);
2373 return (WINDOW_COPY_CMD_REDRAW
);
2378 data
->searchtype
= WINDOW_COPY_SEARCHUP
;
2379 data
->searchregex
= 0;
2380 free(data
->searchstr
);
2381 data
->searchstr
= xstrdup(arg1
);
2382 if (!window_copy_search_up(wme
, 0)) {
2383 window_copy_clear_marks(wme
);
2384 return (WINDOW_COPY_CMD_REDRAW
);
2388 data
->searchtype
= WINDOW_COPY_SEARCHDOWN
;
2389 data
->searchregex
= 0;
2390 free(data
->searchstr
);
2391 data
->searchstr
= xstrdup(arg1
);
2392 if (!window_copy_search_down(wme
, 0)) {
2393 window_copy_clear_marks(wme
);
2394 return (WINDOW_COPY_CMD_REDRAW
);
2401 static enum window_copy_cmd_action
2402 window_copy_cmd_search_forward_incremental(struct window_copy_cmd_state
*cs
)
2404 struct window_mode_entry
*wme
= cs
->wme
;
2405 struct window_copy_mode_data
*data
= wme
->data
;
2406 const char *arg1
= args_string(cs
->args
, 1);
2407 const char *ss
= data
->searchstr
;
2409 enum window_copy_cmd_action action
= WINDOW_COPY_CMD_NOTHING
;
2413 log_debug("%s: %s", __func__
, arg1
);
2416 if (data
->searchx
== -1 || data
->searchy
== -1) {
2417 data
->searchx
= data
->cx
;
2418 data
->searchy
= data
->cy
;
2419 data
->searcho
= data
->oy
;
2420 } else if (ss
!= NULL
&& strcmp(arg1
, ss
) != 0) {
2421 data
->cx
= data
->searchx
;
2422 data
->cy
= data
->searchy
;
2423 data
->oy
= data
->searcho
;
2424 action
= WINDOW_COPY_CMD_REDRAW
;
2426 if (*arg1
== '\0') {
2427 window_copy_clear_marks(wme
);
2428 return (WINDOW_COPY_CMD_REDRAW
);
2433 data
->searchtype
= WINDOW_COPY_SEARCHDOWN
;
2434 data
->searchregex
= 0;
2435 free(data
->searchstr
);
2436 data
->searchstr
= xstrdup(arg1
);
2437 if (!window_copy_search_down(wme
, 0)) {
2438 window_copy_clear_marks(wme
);
2439 return (WINDOW_COPY_CMD_REDRAW
);
2443 data
->searchtype
= WINDOW_COPY_SEARCHUP
;
2444 data
->searchregex
= 0;
2445 free(data
->searchstr
);
2446 data
->searchstr
= xstrdup(arg1
);
2447 if (!window_copy_search_up(wme
, 0)) {
2448 window_copy_clear_marks(wme
);
2449 return (WINDOW_COPY_CMD_REDRAW
);
2455 static enum window_copy_cmd_action
2456 window_copy_cmd_refresh_from_pane(struct window_copy_cmd_state
*cs
)
2458 struct window_mode_entry
*wme
= cs
->wme
;
2459 struct window_pane
*wp
= wme
->swp
;
2460 struct window_copy_mode_data
*data
= wme
->data
;
2463 return (WINDOW_COPY_CMD_NOTHING
);
2465 screen_free(data
->backing
);
2466 free(data
->backing
);
2467 data
->backing
= window_copy_clone_screen(&wp
->base
, &data
->screen
, NULL
, NULL
, wme
->swp
!= wme
->wp
);
2469 window_copy_size_changed(wme
);
2470 return (WINDOW_COPY_CMD_REDRAW
);
2473 static const struct {
2474 const char *command
;
2477 enum window_copy_cmd_clear clear
;
2478 enum window_copy_cmd_action (*f
)(struct window_copy_cmd_state
*);
2479 } window_copy_cmd_table
[] = {
2480 { .command
= "append-selection",
2483 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2484 .f
= window_copy_cmd_append_selection
2486 { .command
= "append-selection-and-cancel",
2489 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2490 .f
= window_copy_cmd_append_selection_and_cancel
2492 { .command
= "back-to-indentation",
2495 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2496 .f
= window_copy_cmd_back_to_indentation
2498 { .command
= "begin-selection",
2501 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2502 .f
= window_copy_cmd_begin_selection
2504 { .command
= "bottom-line",
2507 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2508 .f
= window_copy_cmd_bottom_line
2510 { .command
= "cancel",
2513 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2514 .f
= window_copy_cmd_cancel
2516 { .command
= "clear-selection",
2519 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2520 .f
= window_copy_cmd_clear_selection
2522 { .command
= "copy-end-of-line",
2525 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2526 .f
= window_copy_cmd_copy_end_of_line
2528 { .command
= "copy-end-of-line-and-cancel",
2531 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2532 .f
= window_copy_cmd_copy_end_of_line_and_cancel
2534 { .command
= "copy-pipe-end-of-line",
2537 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2538 .f
= window_copy_cmd_copy_pipe_end_of_line
2540 { .command
= "copy-pipe-end-of-line-and-cancel",
2543 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2544 .f
= window_copy_cmd_copy_pipe_end_of_line_and_cancel
2546 { .command
= "copy-line",
2549 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2550 .f
= window_copy_cmd_copy_line
2552 { .command
= "copy-line-and-cancel",
2555 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2556 .f
= window_copy_cmd_copy_line_and_cancel
2558 { .command
= "copy-pipe-line",
2561 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2562 .f
= window_copy_cmd_copy_pipe_line
2564 { .command
= "copy-pipe-line-and-cancel",
2567 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2568 .f
= window_copy_cmd_copy_pipe_line_and_cancel
2570 { .command
= "copy-pipe-no-clear",
2573 .clear
= WINDOW_COPY_CMD_CLEAR_NEVER
,
2574 .f
= window_copy_cmd_copy_pipe_no_clear
2576 { .command
= "copy-pipe",
2579 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2580 .f
= window_copy_cmd_copy_pipe
2582 { .command
= "copy-pipe-and-cancel",
2585 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2586 .f
= window_copy_cmd_copy_pipe_and_cancel
2588 { .command
= "copy-selection-no-clear",
2591 .clear
= WINDOW_COPY_CMD_CLEAR_NEVER
,
2592 .f
= window_copy_cmd_copy_selection_no_clear
2594 { .command
= "copy-selection",
2597 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2598 .f
= window_copy_cmd_copy_selection
2600 { .command
= "copy-selection-and-cancel",
2603 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2604 .f
= window_copy_cmd_copy_selection_and_cancel
2606 { .command
= "cursor-down",
2609 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2610 .f
= window_copy_cmd_cursor_down
2612 { .command
= "cursor-down-and-cancel",
2615 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2616 .f
= window_copy_cmd_cursor_down_and_cancel
2618 { .command
= "cursor-left",
2621 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2622 .f
= window_copy_cmd_cursor_left
2624 { .command
= "cursor-right",
2627 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2628 .f
= window_copy_cmd_cursor_right
2630 { .command
= "cursor-up",
2633 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2634 .f
= window_copy_cmd_cursor_up
2636 { .command
= "end-of-line",
2639 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2640 .f
= window_copy_cmd_end_of_line
2642 { .command
= "goto-line",
2645 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2646 .f
= window_copy_cmd_goto_line
2648 { .command
= "halfpage-down",
2651 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2652 .f
= window_copy_cmd_halfpage_down
2654 { .command
= "halfpage-down-and-cancel",
2657 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2658 .f
= window_copy_cmd_halfpage_down_and_cancel
2660 { .command
= "halfpage-up",
2663 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2664 .f
= window_copy_cmd_halfpage_up
2666 { .command
= "history-bottom",
2669 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2670 .f
= window_copy_cmd_history_bottom
2672 { .command
= "history-top",
2675 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2676 .f
= window_copy_cmd_history_top
2678 { .command
= "jump-again",
2681 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2682 .f
= window_copy_cmd_jump_again
2684 { .command
= "jump-backward",
2687 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2688 .f
= window_copy_cmd_jump_backward
2690 { .command
= "jump-forward",
2693 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2694 .f
= window_copy_cmd_jump_forward
2696 { .command
= "jump-reverse",
2699 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2700 .f
= window_copy_cmd_jump_reverse
2702 { .command
= "jump-to-backward",
2705 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2706 .f
= window_copy_cmd_jump_to_backward
2708 { .command
= "jump-to-forward",
2711 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2712 .f
= window_copy_cmd_jump_to_forward
2714 { .command
= "jump-to-mark",
2717 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2718 .f
= window_copy_cmd_jump_to_mark
2720 { .command
= "next-prompt",
2723 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2724 .f
= window_copy_cmd_next_prompt
2726 { .command
= "previous-prompt",
2729 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2730 .f
= window_copy_cmd_previous_prompt
2732 { .command
= "middle-line",
2735 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2736 .f
= window_copy_cmd_middle_line
2738 { .command
= "next-matching-bracket",
2741 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2742 .f
= window_copy_cmd_next_matching_bracket
2744 { .command
= "next-paragraph",
2747 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2748 .f
= window_copy_cmd_next_paragraph
2750 { .command
= "next-space",
2753 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2754 .f
= window_copy_cmd_next_space
2756 { .command
= "next-space-end",
2759 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2760 .f
= window_copy_cmd_next_space_end
2762 { .command
= "next-word",
2765 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2766 .f
= window_copy_cmd_next_word
2768 { .command
= "next-word-end",
2771 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2772 .f
= window_copy_cmd_next_word_end
2774 { .command
= "other-end",
2777 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2778 .f
= window_copy_cmd_other_end
2780 { .command
= "page-down",
2783 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2784 .f
= window_copy_cmd_page_down
2786 { .command
= "page-down-and-cancel",
2789 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2790 .f
= window_copy_cmd_page_down_and_cancel
2792 { .command
= "page-up",
2795 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2796 .f
= window_copy_cmd_page_up
2798 { .command
= "pipe-no-clear",
2801 .clear
= WINDOW_COPY_CMD_CLEAR_NEVER
,
2802 .f
= window_copy_cmd_pipe_no_clear
2804 { .command
= "pipe",
2807 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2808 .f
= window_copy_cmd_pipe
2810 { .command
= "pipe-and-cancel",
2813 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2814 .f
= window_copy_cmd_pipe_and_cancel
2816 { .command
= "previous-matching-bracket",
2819 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2820 .f
= window_copy_cmd_previous_matching_bracket
2822 { .command
= "previous-paragraph",
2825 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2826 .f
= window_copy_cmd_previous_paragraph
2828 { .command
= "previous-space",
2831 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2832 .f
= window_copy_cmd_previous_space
2834 { .command
= "previous-word",
2837 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2838 .f
= window_copy_cmd_previous_word
2840 { .command
= "rectangle-on",
2843 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2844 .f
= window_copy_cmd_rectangle_on
2846 { .command
= "rectangle-off",
2849 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2850 .f
= window_copy_cmd_rectangle_off
2852 { .command
= "rectangle-toggle",
2855 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2856 .f
= window_copy_cmd_rectangle_toggle
2858 { .command
= "refresh-from-pane",
2861 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2862 .f
= window_copy_cmd_refresh_from_pane
2864 { .command
= "scroll-bottom",
2867 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2868 .f
= window_copy_cmd_scroll_bottom
2870 { .command
= "scroll-down",
2873 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2874 .f
= window_copy_cmd_scroll_down
2876 { .command
= "scroll-down-and-cancel",
2879 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2880 .f
= window_copy_cmd_scroll_down_and_cancel
2882 { .command
= "scroll-middle",
2885 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2886 .f
= window_copy_cmd_scroll_middle
2888 { .command
= "scroll-top",
2891 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2892 .f
= window_copy_cmd_scroll_top
2894 { .command
= "scroll-up",
2897 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2898 .f
= window_copy_cmd_scroll_up
2900 { .command
= "search-again",
2903 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2904 .f
= window_copy_cmd_search_again
2906 { .command
= "search-backward",
2909 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2910 .f
= window_copy_cmd_search_backward
2912 { .command
= "search-backward-text",
2915 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2916 .f
= window_copy_cmd_search_backward_text
2918 { .command
= "search-backward-incremental",
2921 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2922 .f
= window_copy_cmd_search_backward_incremental
2924 { .command
= "search-forward",
2927 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2928 .f
= window_copy_cmd_search_forward
2930 { .command
= "search-forward-text",
2933 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2934 .f
= window_copy_cmd_search_forward_text
2936 { .command
= "search-forward-incremental",
2939 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2940 .f
= window_copy_cmd_search_forward_incremental
2942 { .command
= "search-reverse",
2945 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2946 .f
= window_copy_cmd_search_reverse
2948 { .command
= "select-line",
2951 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2952 .f
= window_copy_cmd_select_line
2954 { .command
= "select-word",
2957 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2958 .f
= window_copy_cmd_select_word
2960 { .command
= "set-mark",
2963 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2964 .f
= window_copy_cmd_set_mark
2966 { .command
= "start-of-line",
2969 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2970 .f
= window_copy_cmd_start_of_line
2972 { .command
= "stop-selection",
2975 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2976 .f
= window_copy_cmd_stop_selection
2978 { .command
= "toggle-position",
2981 .clear
= WINDOW_COPY_CMD_CLEAR_NEVER
,
2982 .f
= window_copy_cmd_toggle_position
2984 { .command
= "top-line",
2987 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2988 .f
= window_copy_cmd_top_line
2993 window_copy_command(struct window_mode_entry
*wme
, struct client
*c
,
2994 struct session
*s
, struct winlink
*wl
, struct args
*args
,
2995 struct mouse_event
*m
)
2997 struct window_copy_mode_data
*data
= wme
->data
;
2998 struct window_copy_cmd_state cs
;
2999 enum window_copy_cmd_action action
;
3000 enum window_copy_cmd_clear clear
= WINDOW_COPY_CMD_CLEAR_NEVER
;
3001 const char *command
;
3002 u_int i
, count
= args_count(args
);
3007 command
= args_string(args
, 0);
3009 if (m
!= NULL
&& m
->valid
&& !MOUSE_WHEEL(m
->b
))
3010 window_copy_move_mouse(m
);
3020 action
= WINDOW_COPY_CMD_NOTHING
;
3021 for (i
= 0; i
< nitems(window_copy_cmd_table
); i
++) {
3022 if (strcmp(window_copy_cmd_table
[i
].command
, command
) == 0) {
3023 if (count
- 1 < window_copy_cmd_table
[i
].minargs
||
3024 count
- 1 > window_copy_cmd_table
[i
].maxargs
)
3026 clear
= window_copy_cmd_table
[i
].clear
;
3027 action
= window_copy_cmd_table
[i
].f(&cs
);
3032 if (strncmp(command
, "search-", 7) != 0 && data
->searchmark
!= NULL
) {
3033 keys
= options_get_number(wme
->wp
->window
->options
, "mode-keys");
3034 if (clear
== WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
&&
3036 clear
= WINDOW_COPY_CMD_CLEAR_NEVER
;
3037 if (clear
!= WINDOW_COPY_CMD_CLEAR_NEVER
) {
3038 window_copy_clear_marks(wme
);
3039 data
->searchx
= data
->searchy
= -1;
3041 if (action
== WINDOW_COPY_CMD_NOTHING
)
3042 action
= WINDOW_COPY_CMD_REDRAW
;
3046 if (action
== WINDOW_COPY_CMD_CANCEL
)
3047 window_pane_reset_mode(wme
->wp
);
3048 else if (action
== WINDOW_COPY_CMD_REDRAW
)
3049 window_copy_redraw_screen(wme
);
3053 window_copy_scroll_to(struct window_mode_entry
*wme
, u_int px
, u_int py
,
3056 struct window_copy_mode_data
*data
= wme
->data
;
3057 struct grid
*gd
= data
->backing
->grid
;
3062 if (py
>= gd
->hsize
- data
->oy
&& py
< gd
->hsize
- data
->oy
+ gd
->sy
)
3063 data
->cy
= py
- (gd
->hsize
- data
->oy
);
3069 } else if (py
> gd
->hsize
+ gd
->sy
- gap
) {
3071 data
->cy
= py
- gd
->hsize
;
3073 offset
= py
+ gap
- gd
->sy
;
3074 data
->cy
= py
- offset
;
3076 data
->oy
= gd
->hsize
- offset
;
3079 if (!no_redraw
&& data
->searchmark
!= NULL
&& !data
->timeout
)
3080 window_copy_search_marks(wme
, NULL
, data
->searchregex
, 1);
3081 window_copy_update_selection(wme
, 1, 0);
3083 window_copy_redraw_screen(wme
);
3087 window_copy_search_compare(struct grid
*gd
, u_int px
, u_int py
,
3088 struct grid
*sgd
, u_int spx
, int cis
)
3090 struct grid_cell gc
, sgc
;
3091 const struct utf8_data
*ud
, *sud
;
3093 grid_get_cell(gd
, px
, py
, &gc
);
3095 grid_get_cell(sgd
, spx
, 0, &sgc
);
3098 if (ud
->size
!= sud
->size
|| ud
->width
!= sud
->width
)
3101 if (cis
&& ud
->size
== 1)
3102 return (tolower(ud
->data
[0]) == sud
->data
[0]);
3104 return (memcmp(ud
->data
, sud
->data
, ud
->size
) == 0);
3108 window_copy_search_lr(struct grid
*gd
, struct grid
*sgd
, u_int
*ppx
, u_int py
,
3109 u_int first
, u_int last
, int cis
)
3111 u_int ax
, bx
, px
, pywrap
, endline
;
3113 struct grid_line
*gl
;
3115 endline
= gd
->hsize
+ gd
->sy
- 1;
3116 for (ax
= first
; ax
< last
; ax
++) {
3117 for (bx
= 0; bx
< sgd
->sx
; bx
++) {
3121 while (px
>= gd
->sx
&& pywrap
< endline
) {
3122 gl
= grid_get_line(gd
, pywrap
);
3123 if (~gl
->flags
& GRID_LINE_WRAPPED
)
3128 /* We have run off the end of the grid. */
3131 matched
= window_copy_search_compare(gd
, px
, pywrap
,
3136 if (bx
== sgd
->sx
) {
3145 window_copy_search_rl(struct grid
*gd
,
3146 struct grid
*sgd
, u_int
*ppx
, u_int py
, u_int first
, u_int last
, int cis
)
3148 u_int ax
, bx
, px
, pywrap
, endline
;
3150 struct grid_line
*gl
;
3152 endline
= gd
->hsize
+ gd
->sy
- 1;
3153 for (ax
= last
; ax
> first
; ax
--) {
3154 for (bx
= 0; bx
< sgd
->sx
; bx
++) {
3158 while (px
>= gd
->sx
&& pywrap
< endline
) {
3159 gl
= grid_get_line(gd
, pywrap
);
3160 if (~gl
->flags
& GRID_LINE_WRAPPED
)
3165 /* We have run off the end of the grid. */
3168 matched
= window_copy_search_compare(gd
, px
, pywrap
,
3173 if (bx
== sgd
->sx
) {
3182 window_copy_search_lr_regex(struct grid
*gd
, u_int
*ppx
, u_int
*psx
, u_int py
,
3183 u_int first
, u_int last
, regex_t
*reg
)
3186 u_int endline
, foundx
, foundy
, len
, pywrap
, size
= 1;
3188 regmatch_t regmatch
;
3189 struct grid_line
*gl
;
3192 * This can happen during search if the last match was the last
3193 * character on a line.
3198 /* Set flags for regex search. */
3200 eflags
|= REG_NOTBOL
;
3202 /* Need to look at the entire string. */
3203 buf
= xmalloc(size
);
3205 buf
= window_copy_stringify(gd
, py
, first
, gd
->sx
, buf
, &size
);
3206 len
= gd
->sx
- first
;
3207 endline
= gd
->hsize
+ gd
->sy
- 1;
3209 while (buf
!= NULL
&&
3210 pywrap
<= endline
&&
3211 len
< WINDOW_COPY_SEARCH_MAX_LINE
) {
3212 gl
= grid_get_line(gd
, pywrap
);
3213 if (~gl
->flags
& GRID_LINE_WRAPPED
)
3216 buf
= window_copy_stringify(gd
, pywrap
, 0, gd
->sx
, buf
, &size
);
3220 if (regexec(reg
, buf
, 1, ®match
, eflags
) == 0 &&
3221 regmatch
.rm_so
!= regmatch
.rm_eo
) {
3224 window_copy_cstrtocellpos(gd
, len
, &foundx
, &foundy
,
3225 buf
+ regmatch
.rm_so
);
3226 if (foundy
== py
&& foundx
< last
) {
3228 len
-= foundx
- first
;
3229 window_copy_cstrtocellpos(gd
, len
, &foundx
, &foundy
,
3230 buf
+ regmatch
.rm_eo
);
3232 while (foundy
> py
) {
3249 window_copy_search_rl_regex(struct grid
*gd
, u_int
*ppx
, u_int
*psx
, u_int py
,
3250 u_int first
, u_int last
, regex_t
*reg
)
3253 u_int endline
, len
, pywrap
, size
= 1;
3255 struct grid_line
*gl
;
3257 /* Set flags for regex search. */
3259 eflags
|= REG_NOTBOL
;
3261 /* Need to look at the entire string. */
3262 buf
= xmalloc(size
);
3264 buf
= window_copy_stringify(gd
, py
, first
, gd
->sx
, buf
, &size
);
3265 len
= gd
->sx
- first
;
3266 endline
= gd
->hsize
+ gd
->sy
- 1;
3268 while (buf
!= NULL
&&
3269 pywrap
<= endline
&&
3270 len
< WINDOW_COPY_SEARCH_MAX_LINE
) {
3271 gl
= grid_get_line(gd
, pywrap
);
3272 if (~gl
->flags
& GRID_LINE_WRAPPED
)
3275 buf
= window_copy_stringify(gd
, pywrap
, 0, gd
->sx
, buf
, &size
);
3279 if (window_copy_last_regex(gd
, py
, first
, last
, len
, ppx
, psx
, buf
,
3293 window_copy_cellstring(const struct grid_line
*gl
, u_int px
, size_t *size
,
3296 static struct utf8_data ud
;
3297 struct grid_cell_entry
*gce
;
3300 if (px
>= gl
->cellsize
) {
3306 gce
= &gl
->celldata
[px
];
3307 if (gce
->flags
& GRID_FLAG_PADDING
) {
3312 if (~gce
->flags
& GRID_FLAG_EXTENDED
) {
3315 return (&gce
->data
.data
);
3318 utf8_to_data(gl
->extddata
[gce
->offset
].data
, &ud
);
3327 copy
= xmalloc(ud
.size
);
3328 memcpy(copy
, ud
.data
, ud
.size
);
3332 /* Find last match in given range. */
3334 window_copy_last_regex(struct grid
*gd
, u_int py
, u_int first
, u_int last
,
3335 u_int len
, u_int
*ppx
, u_int
*psx
, const char *buf
, const regex_t
*preg
,
3338 u_int foundx
, foundy
, oldx
, px
= 0, savepx
, savesx
= 0;
3339 regmatch_t regmatch
;
3344 while (regexec(preg
, buf
+ px
, 1, ®match
, eflags
) == 0) {
3345 if (regmatch
.rm_so
== regmatch
.rm_eo
)
3347 window_copy_cstrtocellpos(gd
, len
, &foundx
, &foundy
,
3348 buf
+ px
+ regmatch
.rm_so
);
3349 if (foundy
> py
|| foundx
>= last
)
3351 len
-= foundx
- oldx
;
3353 window_copy_cstrtocellpos(gd
, len
, &foundx
, &foundy
,
3354 buf
+ px
+ regmatch
.rm_eo
);
3355 if (foundy
> py
|| foundx
>= last
) {
3358 while (foundy
> py
) {
3365 savesx
= foundx
- savepx
;
3369 px
+= regmatch
.rm_eo
;
3383 /* Stringify line and append to input buffer. Caller frees. */
3385 window_copy_stringify(struct grid
*gd
, u_int py
, u_int first
, u_int last
,
3386 char *buf
, u_int
*size
)
3388 u_int ax
, bx
, newsize
= *size
;
3389 const struct grid_line
*gl
;
3391 size_t bufsize
= 1024, dlen
;
3394 while (bufsize
< newsize
)
3396 buf
= xrealloc(buf
, bufsize
);
3398 gl
= grid_peek_line(gd
, py
);
3400 for (ax
= first
; ax
< last
; ax
++) {
3401 d
= window_copy_cellstring(gl
, ax
, &dlen
, &allocated
);
3403 while (bufsize
< newsize
) {
3405 buf
= xrealloc(buf
, bufsize
);
3410 memcpy(buf
+ bx
, d
, dlen
);
3416 buf
[newsize
- 1] = '\0';
3422 /* Map start of C string containing UTF-8 data to grid cell position. */
3424 window_copy_cstrtocellpos(struct grid
*gd
, u_int ncells
, u_int
*ppx
, u_int
*ppy
,
3427 u_int cell
, ccell
, px
, pywrap
, pos
, len
;
3429 const struct grid_line
*gl
;
3438 /* Populate the array of cell data. */
3439 cells
= xreallocarray(NULL
, ncells
, sizeof cells
[0]);
3443 gl
= grid_peek_line(gd
, pywrap
);
3444 while (cell
< ncells
) {
3445 cells
[cell
].d
= window_copy_cellstring(gl
, px
,
3446 &cells
[cell
].dlen
, &cells
[cell
].allocated
);
3452 gl
= grid_peek_line(gd
, pywrap
);
3456 /* Locate starting cell. */
3459 while (cell
< ncells
) {
3463 while (ccell
< ncells
) {
3464 if (str
[pos
] == '\0') {
3469 dlen
= cells
[ccell
].dlen
;
3471 if (str
[pos
] != *d
) {
3477 if (dlen
> len
- pos
)
3479 if (memcmp(str
+ pos
, d
, dlen
) != 0) {
3492 /* If not found this will be one past the end. */
3495 while (px
>= gd
->sx
) {
3503 /* Free cell data. */
3504 for (cell
= 0; cell
< ncells
; cell
++) {
3505 if (cells
[cell
].allocated
)
3506 free((void *)cells
[cell
].d
);
3512 window_copy_move_left(struct screen
*s
, u_int
*fx
, u_int
*fy
, int wrapflag
)
3514 if (*fx
== 0) { /* left */
3515 if (*fy
== 0) { /* top */
3517 *fx
= screen_size_x(s
) - 1;
3518 *fy
= screen_hsize(s
) + screen_size_y(s
) - 1;
3522 *fx
= screen_size_x(s
) - 1;
3529 window_copy_move_right(struct screen
*s
, u_int
*fx
, u_int
*fy
, int wrapflag
)
3531 if (*fx
== screen_size_x(s
) - 1) { /* right */
3532 if (*fy
== screen_hsize(s
) + screen_size_y(s
) - 1) { /* bottom */
3546 window_copy_is_lowercase(const char *ptr
)
3548 while (*ptr
!= '\0') {
3549 if (*ptr
!= tolower((u_char
)*ptr
))
3557 * Handle backward wrapped regex searches with overlapping matches. In this case
3558 * find the longest overlapping match from previous wrapped lines.
3561 window_copy_search_back_overlap(struct grid
*gd
, regex_t
*preg
, u_int
*ppx
,
3562 u_int
*psx
, u_int
*ppy
, u_int endline
)
3564 u_int endx
, endy
, oldendx
, oldendy
, px
, py
, sx
;
3567 oldendx
= *ppx
+ *psx
;
3569 while (oldendx
> gd
->sx
- 1) {
3577 while (found
&& px
== 0 && py
- 1 > endline
&&
3578 grid_get_line(gd
, py
- 2)->flags
& GRID_LINE_WRAPPED
&&
3579 endx
== oldendx
&& endy
== oldendy
) {
3581 found
= window_copy_search_rl_regex(gd
, &px
, &sx
, py
- 1, 0,
3586 while (endx
> gd
->sx
- 1) {
3590 if (endx
== oldendx
&& endy
== oldendy
) {
3599 * Search for text stored in sgd starting from position fx,fy up to endline. If
3600 * found, jump to it. If cis then ignore case. The direction is 0 for searching
3601 * up, down otherwise. If wrap then go to begin/end of grid and try again if
3605 window_copy_search_jump(struct window_mode_entry
*wme
, struct grid
*gd
,
3606 struct grid
*sgd
, u_int fx
, u_int fy
, u_int endline
, int cis
, int wrap
,
3607 int direction
, int regex
)
3609 u_int i
, px
, sx
, ssize
= 1;
3610 int found
= 0, cflags
= REG_EXTENDED
;
3613 struct grid_line
*gl
;
3616 sbuf
= xmalloc(ssize
);
3618 sbuf
= window_copy_stringify(sgd
, 0, 0, sgd
->sx
, sbuf
, &ssize
);
3620 cflags
|= REG_ICASE
;
3621 if (regcomp(®
, sbuf
, cflags
) != 0) {
3629 for (i
= fy
; i
<= endline
; i
++) {
3630 gl
= grid_get_line(gd
, i
);
3631 if (i
!= endline
&& gl
->flags
& GRID_LINE_WRAPPED
)
3634 found
= window_copy_search_lr_regex(gd
,
3635 &px
, &sx
, i
, fx
, gd
->sx
, ®
);
3637 found
= window_copy_search_lr(gd
, sgd
,
3638 &px
, i
, fx
, gd
->sx
, cis
);
3645 for (i
= fy
+ 1; endline
< i
; i
--) {
3646 gl
= grid_get_line(gd
, i
- 1);
3647 if (i
!= endline
&& gl
->flags
& GRID_LINE_WRAPPED
)
3650 found
= window_copy_search_rl_regex(gd
,
3651 &px
, &sx
, i
- 1, 0, fx
+ 1, ®
);
3653 window_copy_search_back_overlap(gd
,
3654 ®
, &px
, &sx
, &i
, endline
);
3657 found
= window_copy_search_rl(gd
, sgd
,
3658 &px
, i
- 1, 0, fx
+ 1, cis
);
3671 window_copy_scroll_to(wme
, px
, i
, 1);
3675 return (window_copy_search_jump(wme
, gd
, sgd
,
3676 direction
? 0 : gd
->sx
- 1,
3677 direction
? 0 : gd
->hsize
+ gd
->sy
- 1, fy
, cis
, 0,
3684 window_copy_move_after_search_mark(struct window_copy_mode_data
*data
,
3685 u_int
*fx
, u_int
*fy
, int wrapflag
)
3687 struct screen
*s
= data
->backing
;
3690 if (window_copy_search_mark_at(data
, *fx
, *fy
, &start
) == 0 &&
3691 data
->searchmark
[start
] != 0) {
3692 while (window_copy_search_mark_at(data
, *fx
, *fy
, &at
) == 0) {
3693 if (data
->searchmark
[at
] != data
->searchmark
[start
])
3695 /* Stop if not wrapping and at the end of the grid. */
3697 *fx
== screen_size_x(s
) - 1 &&
3698 *fy
== screen_hsize(s
) + screen_size_y(s
) - 1)
3701 window_copy_move_right(s
, fx
, fy
, wrapflag
);
3707 * Search in for text searchstr. If direction is 0 then search up, otherwise
3711 window_copy_search(struct window_mode_entry
*wme
, int direction
, int regex
)
3713 struct window_pane
*wp
= wme
->wp
;
3714 struct window_copy_mode_data
*data
= wme
->data
;
3715 struct screen
*s
= data
->backing
, ss
;
3716 struct screen_write_ctx ctx
;
3717 struct grid
*gd
= s
->grid
;
3718 const char *str
= data
->searchstr
;
3719 u_int at
, endline
, fx
, fy
, start
;
3720 int cis
, found
, keys
, visible_only
;
3723 if (regex
&& str
[strcspn(str
, "^$*+()?[].\\")] == '\0')
3726 data
->searchdirection
= direction
;
3731 if (data
->searchall
|| wp
->searchstr
== NULL
||
3732 wp
->searchregex
!= regex
) {
3734 data
->searchall
= 0;
3736 visible_only
= (strcmp(wp
->searchstr
, str
) == 0);
3737 if (visible_only
== 0 && data
->searchmark
!= NULL
)
3738 window_copy_clear_marks(wme
);
3739 free(wp
->searchstr
);
3740 wp
->searchstr
= xstrdup(str
);
3741 wp
->searchregex
= regex
;
3744 fy
= screen_hsize(data
->backing
) - data
->oy
+ data
->cy
;
3746 screen_init(&ss
, screen_write_strlen("%s", str
), 1, 0);
3747 screen_write_start(&ctx
, &ss
);
3748 screen_write_nputs(&ctx
, -1, &grid_default_cell
, "%s", str
);
3749 screen_write_stop(&ctx
);
3751 wrapflag
= options_get_number(wp
->window
->options
, "wrap-search");
3752 cis
= window_copy_is_lowercase(str
);
3754 keys
= options_get_number(wp
->window
->options
, "mode-keys");
3758 * Behave according to mode-keys. If it is emacs, search forward
3759 * leaves the cursor after the match. If it is vi, the cursor
3760 * remains at the beginning of the match, regardless of
3761 * direction, which means that we need to start the next search
3762 * after the term the cursor is currently on when searching
3765 if (keys
== MODEKEY_VI
) {
3766 if (data
->searchmark
!= NULL
)
3767 window_copy_move_after_search_mark(data
, &fx
,
3771 * When there are no search marks, start the
3772 * search after the current cursor position.
3774 window_copy_move_right(s
, &fx
, &fy
, wrapflag
);
3777 endline
= gd
->hsize
+ gd
->sy
- 1;
3779 window_copy_move_left(s
, &fx
, &fy
, wrapflag
);
3783 found
= window_copy_search_jump(wme
, gd
, ss
.grid
, fx
, fy
, endline
, cis
,
3784 wrapflag
, direction
, regex
);
3786 window_copy_search_marks(wme
, &ss
, regex
, visible_only
);
3788 fy
= screen_hsize(data
->backing
) - data
->oy
+ data
->cy
;
3791 * When searching forward, if the cursor is not at the beginning
3792 * of the mark, search again.
3795 window_copy_search_mark_at(data
, fx
, fy
, &at
) == 0 &&
3797 data
->searchmark
!= NULL
&&
3798 data
->searchmark
[at
] == data
->searchmark
[at
- 1]) {
3799 window_copy_move_after_search_mark(data
, &fx
, &fy
,
3801 window_copy_search_jump(wme
, gd
, ss
.grid
, fx
,
3802 fy
, endline
, cis
, wrapflag
, direction
,
3805 fy
= screen_hsize(data
->backing
) - data
->oy
+ data
->cy
;
3810 * When in Emacs mode, position the cursor just after
3813 if (keys
== MODEKEY_EMACS
) {
3814 window_copy_move_after_search_mark(data
, &fx
,
3817 data
->cy
= fy
- screen_hsize(data
->backing
) +
3822 * When searching backward, position the cursor at the
3823 * beginning of the mark.
3825 if (window_copy_search_mark_at(data
, fx
, fy
,
3827 while (window_copy_search_mark_at(data
, fx
, fy
,
3829 data
->searchmark
!= NULL
&&
3830 data
->searchmark
[at
] ==
3831 data
->searchmark
[start
]) {
3834 screen_hsize(data
->backing
) +
3839 window_copy_move_left(s
, &fx
, &fy
, 0);
3844 window_copy_redraw_screen(wme
);
3851 window_copy_visible_lines(struct window_copy_mode_data
*data
, u_int
*start
,
3854 struct grid
*gd
= data
->backing
->grid
;
3855 const struct grid_line
*gl
;
3857 for (*start
= gd
->hsize
- data
->oy
; *start
> 0; (*start
)--) {
3858 gl
= grid_peek_line(gd
, (*start
) - 1);
3859 if (~gl
->flags
& GRID_LINE_WRAPPED
)
3862 *end
= gd
->hsize
- data
->oy
+ gd
->sy
;
3866 window_copy_search_mark_at(struct window_copy_mode_data
*data
, u_int px
,
3867 u_int py
, u_int
*at
)
3869 struct screen
*s
= data
->backing
;
3870 struct grid
*gd
= s
->grid
;
3872 if (py
< gd
->hsize
- data
->oy
)
3874 if (py
> gd
->hsize
- data
->oy
+ gd
->sy
- 1)
3876 *at
= ((py
- (gd
->hsize
- data
->oy
)) * gd
->sx
) + px
;
3881 window_copy_search_marks(struct window_mode_entry
*wme
, struct screen
*ssp
,
3882 int regex
, int visible_only
)
3884 struct window_copy_mode_data
*data
= wme
->data
;
3885 struct screen
*s
= data
->backing
, ss
;
3886 struct screen_write_ctx ctx
;
3887 struct grid
*gd
= s
->grid
;
3888 int found
, cis
, stopped
= 0;
3889 int cflags
= REG_EXTENDED
;
3890 u_int px
, py
, i
, b
, nfound
= 0, width
;
3891 u_int ssize
= 1, start
, end
;
3894 uint64_t stop
= 0, tstart
, t
;
3897 width
= screen_write_strlen("%s", data
->searchstr
);
3898 screen_init(&ss
, width
, 1, 0);
3899 screen_write_start(&ctx
, &ss
);
3900 screen_write_nputs(&ctx
, -1, &grid_default_cell
, "%s",
3902 screen_write_stop(&ctx
);
3905 width
= screen_size_x(ssp
);
3907 cis
= window_copy_is_lowercase(data
->searchstr
);
3910 sbuf
= xmalloc(ssize
);
3912 sbuf
= window_copy_stringify(ssp
->grid
, 0, 0, ssp
->grid
->sx
,
3915 cflags
|= REG_ICASE
;
3916 if (regcomp(®
, sbuf
, cflags
) != 0) {
3922 tstart
= get_timer();
3925 window_copy_visible_lines(data
, &start
, &end
);
3928 end
= gd
->hsize
+ gd
->sy
;
3929 stop
= get_timer() + WINDOW_COPY_SEARCH_ALL_TIMEOUT
;
3933 free(data
->searchmark
);
3934 data
->searchmark
= xcalloc(gd
->sx
, gd
->sy
);
3935 data
->searchgen
= 1;
3937 for (py
= start
; py
< end
; py
++) {
3941 found
= window_copy_search_lr_regex(gd
,
3942 &px
, &width
, py
, px
, gd
->sx
, ®
);
3946 found
= window_copy_search_lr(gd
, ssp
->grid
,
3947 &px
, py
, px
, gd
->sx
, cis
);
3953 if (window_copy_search_mark_at(data
, px
, py
, &b
) == 0) {
3954 if (b
+ width
> gd
->sx
* gd
->sy
)
3955 width
= (gd
->sx
* gd
->sy
) - b
;
3956 for (i
= b
; i
< b
+ width
; i
++) {
3957 if (data
->searchmark
[i
] != 0)
3959 data
->searchmark
[i
] = data
->searchgen
;
3961 if (data
->searchgen
== UCHAR_MAX
)
3962 data
->searchgen
= 1;
3970 if (t
- tstart
> WINDOW_COPY_SEARCH_TIMEOUT
) {
3974 if (stop
!= 0 && t
> stop
) {
3979 if (data
->timeout
) {
3980 window_copy_clear_marks(wme
);
3984 if (stopped
&& stop
!= 0) {
3985 /* Try again but just the visible context. */
3986 window_copy_visible_lines(data
, &start
, &end
);
3991 if (!visible_only
) {
3994 data
->searchcount
= 1000;
3995 else if (nfound
> 100)
3996 data
->searchcount
= 100;
3997 else if (nfound
> 10)
3998 data
->searchcount
= 10;
4000 data
->searchcount
= -1;
4001 data
->searchmore
= 1;
4003 data
->searchcount
= nfound
;
4004 data
->searchmore
= 0;
4017 window_copy_clear_marks(struct window_mode_entry
*wme
)
4019 struct window_copy_mode_data
*data
= wme
->data
;
4021 free(data
->searchmark
);
4022 data
->searchmark
= NULL
;
4026 window_copy_search_up(struct window_mode_entry
*wme
, int regex
)
4028 return (window_copy_search(wme
, 0, regex
));
4032 window_copy_search_down(struct window_mode_entry
*wme
, int regex
)
4034 return (window_copy_search(wme
, 1, regex
));
4038 window_copy_goto_line(struct window_mode_entry
*wme
, const char *linestr
)
4040 struct window_copy_mode_data
*data
= wme
->data
;
4044 lineno
= strtonum(linestr
, -1, INT_MAX
, &errstr
);
4047 if (lineno
< 0 || (u_int
)lineno
> screen_hsize(data
->backing
))
4048 lineno
= screen_hsize(data
->backing
);
4051 window_copy_update_selection(wme
, 1, 0);
4052 window_copy_redraw_screen(wme
);
4056 window_copy_match_start_end(struct window_copy_mode_data
*data
, u_int at
,
4057 u_int
*start
, u_int
*end
)
4059 struct grid
*gd
= data
->backing
->grid
;
4060 u_int last
= (gd
->sy
* gd
->sx
) - 1;
4061 u_char mark
= data
->searchmark
[at
];
4064 while (*start
!= 0 && data
->searchmark
[*start
] == mark
)
4066 if (data
->searchmark
[*start
] != mark
)
4068 while (*end
!= last
&& data
->searchmark
[*end
] == mark
)
4070 if (data
->searchmark
[*end
] != mark
)
4075 window_copy_match_at_cursor(struct window_copy_mode_data
*data
)
4077 struct grid
*gd
= data
->backing
->grid
;
4078 struct grid_cell gc
;
4079 u_int at
, start
, end
, cy
, px
, py
;
4080 u_int sx
= screen_size_x(data
->backing
);
4084 if (data
->searchmark
== NULL
)
4087 cy
= screen_hsize(data
->backing
) - data
->oy
+ data
->cy
;
4088 if (window_copy_search_mark_at(data
, data
->cx
, cy
, &at
) != 0)
4090 if (data
->searchmark
[at
] == 0) {
4091 /* Allow one position after the match. */
4092 if (at
== 0 || data
->searchmark
[--at
] == 0)
4095 window_copy_match_start_end(data
, at
, &start
, &end
);
4098 * Cells will not be set in the marked array unless they are valid text
4099 * and wrapping will be taken care of, so we can just copy.
4101 for (at
= start
; at
<= end
; at
++) {
4103 px
= at
- (py
* sx
);
4105 grid_get_cell(gd
, px
, gd
->hsize
+ py
- data
->oy
, &gc
);
4106 buf
= xrealloc(buf
, len
+ gc
.data
.size
+ 1);
4107 memcpy(buf
+ len
, gc
.data
.data
, gc
.data
.size
);
4108 len
+= gc
.data
.size
;
4116 window_copy_update_style(struct window_mode_entry
*wme
, u_int fx
, u_int fy
,
4117 struct grid_cell
*gc
, const struct grid_cell
*mgc
,
4118 const struct grid_cell
*cgc
, const struct grid_cell
*mkgc
)
4120 struct window_pane
*wp
= wme
->wp
;
4121 struct window_copy_mode_data
*data
= wme
->data
;
4122 u_int mark
, start
, end
, cy
, cursor
, current
;
4123 int inv
= 0, found
= 0;
4126 if (data
->showmark
&& fy
== data
->my
) {
4127 gc
->attr
= mkgc
->attr
;
4140 if (data
->searchmark
== NULL
)
4143 if (window_copy_search_mark_at(data
, fx
, fy
, ¤t
) != 0)
4145 mark
= data
->searchmark
[current
];
4149 cy
= screen_hsize(data
->backing
) - data
->oy
+ data
->cy
;
4150 if (window_copy_search_mark_at(data
, data
->cx
, cy
, &cursor
) == 0) {
4151 keys
= options_get_number(wp
->window
->options
, "mode-keys");
4153 keys
== MODEKEY_EMACS
&&
4154 data
->searchdirection
) {
4155 if (data
->searchmark
[cursor
- 1] == mark
) {
4159 } else if (data
->searchmark
[cursor
] == mark
)
4162 window_copy_match_start_end(data
, cursor
, &start
, &end
);
4163 if (current
>= start
&& current
<= end
) {
4164 gc
->attr
= cgc
->attr
;
4178 gc
->attr
= mgc
->attr
;
4190 window_copy_write_one(struct window_mode_entry
*wme
,
4191 struct screen_write_ctx
*ctx
, u_int py
, u_int fy
, u_int nx
,
4192 const struct grid_cell
*mgc
, const struct grid_cell
*cgc
,
4193 const struct grid_cell
*mkgc
)
4195 struct window_copy_mode_data
*data
= wme
->data
;
4196 struct grid
*gd
= data
->backing
->grid
;
4197 struct grid_cell gc
;
4200 screen_write_cursormove(ctx
, 0, py
, 0);
4201 for (fx
= 0; fx
< nx
; fx
++) {
4202 grid_get_cell(gd
, fx
, fy
, &gc
);
4203 if (fx
+ gc
.data
.width
<= nx
) {
4204 window_copy_update_style(wme
, fx
, fy
, &gc
, mgc
, cgc
,
4206 screen_write_cell(ctx
, &gc
);
4212 window_copy_write_line(struct window_mode_entry
*wme
,
4213 struct screen_write_ctx
*ctx
, u_int py
)
4215 struct window_pane
*wp
= wme
->wp
;
4216 struct window_copy_mode_data
*data
= wme
->data
;
4217 struct screen
*s
= &data
->screen
;
4218 struct options
*oo
= wp
->window
->options
;
4219 struct grid_line
*gl
;
4220 struct grid_cell gc
, mgc
, cgc
, mkgc
;
4221 char hdr
[512], tmp
[256], *t
;
4223 u_int hsize
= screen_hsize(data
->backing
);
4225 style_apply(&gc
, oo
, "mode-style", NULL
);
4226 gc
.flags
|= GRID_FLAG_NOPALETTE
;
4227 style_apply(&mgc
, oo
, "copy-mode-match-style", NULL
);
4228 mgc
.flags
|= GRID_FLAG_NOPALETTE
;
4229 style_apply(&cgc
, oo
, "copy-mode-current-match-style", NULL
);
4230 cgc
.flags
|= GRID_FLAG_NOPALETTE
;
4231 style_apply(&mkgc
, oo
, "copy-mode-mark-style", NULL
);
4232 mkgc
.flags
|= GRID_FLAG_NOPALETTE
;
4234 if (py
== 0 && s
->rupper
< s
->rlower
&& !data
->hide_position
) {
4235 gl
= grid_get_line(data
->backing
->grid
, hsize
- data
->oy
);
4237 xsnprintf(tmp
, sizeof tmp
, "[%u/%u]", data
->oy
, hsize
);
4239 t
= format_pretty_time(gl
->time
, 1);
4240 xsnprintf(tmp
, sizeof tmp
, "%s [%u/%u]", t
, data
->oy
,
4245 if (data
->searchmark
== NULL
) {
4246 if (data
->timeout
) {
4247 size
= xsnprintf(hdr
, sizeof hdr
,
4248 "(timed out) %s", tmp
);
4250 size
= xsnprintf(hdr
, sizeof hdr
, "%s", tmp
);
4252 if (data
->searchcount
== -1)
4253 size
= xsnprintf(hdr
, sizeof hdr
, "%s", tmp
);
4255 size
= xsnprintf(hdr
, sizeof hdr
,
4256 "(%d%s results) %s", data
->searchcount
,
4257 data
->searchmore
? "+" : "", tmp
);
4260 if (size
> screen_size_x(s
))
4261 size
= screen_size_x(s
);
4262 screen_write_cursormove(ctx
, screen_size_x(s
) - size
, 0, 0);
4263 screen_write_puts(ctx
, &gc
, "%s", hdr
);
4267 if (size
< screen_size_x(s
)) {
4268 window_copy_write_one(wme
, ctx
, py
, hsize
- data
->oy
+ py
,
4269 screen_size_x(s
) - size
, &mgc
, &cgc
, &mkgc
);
4272 if (py
== data
->cy
&& data
->cx
== screen_size_x(s
)) {
4273 screen_write_cursormove(ctx
, screen_size_x(s
) - 1, py
, 0);
4274 screen_write_putc(ctx
, &grid_default_cell
, '$');
4279 window_copy_write_lines(struct window_mode_entry
*wme
,
4280 struct screen_write_ctx
*ctx
, u_int py
, u_int ny
)
4284 for (yy
= py
; yy
< py
+ ny
; yy
++)
4285 window_copy_write_line(wme
, ctx
, py
);
4289 window_copy_redraw_selection(struct window_mode_entry
*wme
, u_int old_y
)
4291 struct window_copy_mode_data
*data
= wme
->data
;
4292 struct grid
*gd
= data
->backing
->grid
;
4293 u_int new_y
, start
, end
;
4296 if (old_y
<= new_y
) {
4305 * In word selection mode the first word on the line below the cursor
4306 * might be selected, so add this line to the redraw area.
4308 if (data
->selflag
== SEL_WORD
) {
4309 /* Last grid line in data coordinates. */
4310 if (end
< gd
->sy
+ data
->oy
- 1)
4313 window_copy_redraw_lines(wme
, start
, end
- start
+ 1);
4317 window_copy_redraw_lines(struct window_mode_entry
*wme
, u_int py
, u_int ny
)
4319 struct window_pane
*wp
= wme
->wp
;
4320 struct window_copy_mode_data
*data
= wme
->data
;
4321 struct screen_write_ctx ctx
;
4324 screen_write_start_pane(&ctx
, wp
, NULL
);
4325 for (i
= py
; i
< py
+ ny
; i
++)
4326 window_copy_write_line(wme
, &ctx
, i
);
4327 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
, 0);
4328 screen_write_stop(&ctx
);
4332 window_copy_redraw_screen(struct window_mode_entry
*wme
)
4334 struct window_copy_mode_data
*data
= wme
->data
;
4336 window_copy_redraw_lines(wme
, 0, screen_size_y(&data
->screen
));
4340 window_copy_synchronize_cursor_end(struct window_mode_entry
*wme
, int begin
,
4343 struct window_copy_mode_data
*data
= wme
->data
;
4347 yy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
4348 switch (data
->selflag
) {
4353 if (data
->dy
> yy
|| (data
->dy
== yy
&& data
->dx
> xx
)) {
4354 /* Right to left selection. */
4355 window_copy_cursor_previous_word_pos(wme
,
4356 data
->separators
, &xx
, &yy
);
4359 /* Reset the end. */
4360 data
->endselx
= data
->endselrx
;
4361 data
->endsely
= data
->endselry
;
4363 /* Left to right selection. */
4364 if (xx
>= window_copy_find_length(wme
, yy
) ||
4365 !window_copy_in_set(wme
, xx
+ 1, yy
, WHITESPACE
)) {
4366 window_copy_cursor_next_word_end_pos(wme
,
4367 data
->separators
, &xx
, &yy
);
4370 /* Reset the start. */
4371 data
->selx
= data
->selrx
;
4372 data
->sely
= data
->selry
;
4379 if (data
->dy
> yy
) {
4380 /* Right to left selection. */
4384 /* Reset the end. */
4385 data
->endselx
= data
->endselrx
;
4386 data
->endsely
= data
->endselry
;
4388 /* Left to right selection. */
4389 if (yy
< data
->endselry
)
4390 yy
= data
->endselry
;
4391 xx
= window_copy_find_length(wme
, yy
);
4393 /* Reset the start. */
4394 data
->selx
= data
->selrx
;
4395 data
->sely
= data
->selry
;
4411 window_copy_synchronize_cursor(struct window_mode_entry
*wme
, int no_reset
)
4413 struct window_copy_mode_data
*data
= wme
->data
;
4415 switch (data
->cursordrag
) {
4416 case CURSORDRAG_ENDSEL
:
4417 window_copy_synchronize_cursor_end(wme
, 0, no_reset
);
4419 case CURSORDRAG_SEL
:
4420 window_copy_synchronize_cursor_end(wme
, 1, no_reset
);
4422 case CURSORDRAG_NONE
:
4428 window_copy_update_cursor(struct window_mode_entry
*wme
, u_int cx
, u_int cy
)
4430 struct window_pane
*wp
= wme
->wp
;
4431 struct window_copy_mode_data
*data
= wme
->data
;
4432 struct screen
*s
= &data
->screen
;
4433 struct screen_write_ctx ctx
;
4434 u_int old_cx
, old_cy
;
4436 old_cx
= data
->cx
; old_cy
= data
->cy
;
4437 data
->cx
= cx
; data
->cy
= cy
;
4438 if (old_cx
== screen_size_x(s
))
4439 window_copy_redraw_lines(wme
, old_cy
, 1);
4440 if (data
->cx
== screen_size_x(s
))
4441 window_copy_redraw_lines(wme
, data
->cy
, 1);
4443 screen_write_start_pane(&ctx
, wp
, NULL
);
4444 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
, 0);
4445 screen_write_stop(&ctx
);
4450 window_copy_start_selection(struct window_mode_entry
*wme
)
4452 struct window_copy_mode_data
*data
= wme
->data
;
4454 data
->selx
= data
->cx
;
4455 data
->sely
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
4457 data
->endselx
= data
->selx
;
4458 data
->endsely
= data
->sely
;
4460 data
->cursordrag
= CURSORDRAG_ENDSEL
;
4462 window_copy_set_selection(wme
, 1, 0);
4466 window_copy_adjust_selection(struct window_mode_entry
*wme
, u_int
*selx
,
4469 struct window_copy_mode_data
*data
= wme
->data
;
4470 struct screen
*s
= &data
->screen
;
4477 ty
= screen_hsize(data
->backing
) - data
->oy
;
4479 relpos
= WINDOW_COPY_REL_POS_ABOVE
;
4480 if (!data
->rectflag
)
4483 } else if (sy
> ty
+ screen_size_y(s
) - 1) {
4484 relpos
= WINDOW_COPY_REL_POS_BELOW
;
4485 if (!data
->rectflag
)
4486 sx
= screen_size_x(s
) - 1;
4487 sy
= screen_size_y(s
) - 1;
4489 relpos
= WINDOW_COPY_REL_POS_ON_SCREEN
;
4499 window_copy_update_selection(struct window_mode_entry
*wme
, int may_redraw
,
4502 struct window_copy_mode_data
*data
= wme
->data
;
4503 struct screen
*s
= &data
->screen
;
4505 if (s
->sel
== NULL
&& data
->lineflag
== LINE_SEL_NONE
)
4507 return (window_copy_set_selection(wme
, may_redraw
, no_reset
));
4511 window_copy_set_selection(struct window_mode_entry
*wme
, int may_redraw
,
4514 struct window_pane
*wp
= wme
->wp
;
4515 struct window_copy_mode_data
*data
= wme
->data
;
4516 struct screen
*s
= &data
->screen
;
4517 struct options
*oo
= wp
->window
->options
;
4518 struct grid_cell gc
;
4519 u_int sx
, sy
, cy
, endsx
, endsy
;
4520 int startrelpos
, endrelpos
;
4522 window_copy_synchronize_cursor(wme
, no_reset
);
4524 /* Adjust the selection. */
4527 startrelpos
= window_copy_adjust_selection(wme
, &sx
, &sy
);
4529 /* Adjust the end of selection. */
4530 endsx
= data
->endselx
;
4531 endsy
= data
->endsely
;
4532 endrelpos
= window_copy_adjust_selection(wme
, &endsx
, &endsy
);
4534 /* Selection is outside of the current screen */
4535 if (startrelpos
== endrelpos
&&
4536 startrelpos
!= WINDOW_COPY_REL_POS_ON_SCREEN
) {
4537 screen_hide_selection(s
);
4541 /* Set colours and selection. */
4542 style_apply(&gc
, oo
, "mode-style", NULL
);
4543 gc
.flags
|= GRID_FLAG_NOPALETTE
;
4544 screen_set_selection(s
, sx
, sy
, endsx
, endsy
, data
->rectflag
,
4545 data
->modekeys
, &gc
);
4547 if (data
->rectflag
&& may_redraw
) {
4549 * Can't rely on the caller to redraw the right lines for
4550 * rectangle selection - find the highest line and the number
4551 * of lines, and redraw just past that in both directions
4554 if (data
->cursordrag
== CURSORDRAG_ENDSEL
) {
4556 window_copy_redraw_lines(wme
, sy
, cy
- sy
+ 1);
4558 window_copy_redraw_lines(wme
, cy
, sy
- cy
+ 1);
4561 window_copy_redraw_lines(wme
, endsy
,
4564 window_copy_redraw_lines(wme
, cy
,
4574 window_copy_get_selection(struct window_mode_entry
*wme
, size_t *len
)
4576 struct window_pane
*wp
= wme
->wp
;
4577 struct window_copy_mode_data
*data
= wme
->data
;
4578 struct screen
*s
= &data
->screen
;
4581 u_int i
, xx
, yy
, sx
, sy
, ex
, ey
, ey_last
;
4582 u_int firstsx
, lastex
, restex
, restsx
, selx
;
4585 if (data
->screen
.sel
== NULL
&& data
->lineflag
== LINE_SEL_NONE
) {
4586 buf
= window_copy_match_at_cursor(data
);
4600 * The selection extends from selx,sely to (adjusted) cx,cy on
4604 /* Find start and end. */
4607 if (yy
< data
->sely
|| (yy
== data
->sely
&& xx
< data
->selx
)) {
4609 ex
= data
->selx
; ey
= data
->sely
;
4611 sx
= data
->selx
; sy
= data
->sely
;
4615 /* Trim ex to end of line. */
4616 ey_last
= window_copy_find_length(wme
, ey
);
4621 * Deal with rectangle-copy if necessary; four situations: start of
4622 * first line (firstsx), end of last line (lastex), start (restsx) and
4623 * end (restex) of all other lines.
4625 xx
= screen_size_x(s
);
4628 * Behave according to mode-keys. If it is emacs, copy like emacs,
4629 * keeping the top-left-most character, and dropping the
4630 * bottom-right-most, regardless of copy direction. If it is vi, also
4631 * keep bottom-right-most character.
4633 keys
= options_get_number(wp
->window
->options
, "mode-keys");
4634 if (data
->rectflag
) {
4636 * Need to ignore the column with the cursor in it, which for
4637 * rectangular copy means knowing which side the cursor is on.
4639 if (data
->cursordrag
== CURSORDRAG_ENDSEL
)
4642 selx
= data
->endselx
;
4643 if (selx
< data
->cx
) {
4644 /* Selection start is on the left. */
4645 if (keys
== MODEKEY_EMACS
) {
4650 lastex
= data
->cx
+ 1;
4651 restex
= data
->cx
+ 1;
4656 /* Cursor is on the left. */
4663 if (keys
== MODEKEY_EMACS
)
4672 /* Copy the lines. */
4673 for (i
= sy
; i
<= ey
; i
++) {
4674 window_copy_copy_line(wme
, &buf
, &off
, i
,
4675 (i
== sy
? firstsx
: restsx
),
4676 (i
== ey
? lastex
: restex
));
4679 /* Don't bother if no data. */
4685 /* Remove final \n (unless at end in vi mode). */
4686 if (keys
== MODEKEY_EMACS
|| lastex
<= ey_last
) {
4687 if (~grid_get_line(data
->backing
->grid
, ey
)->flags
&
4688 GRID_LINE_WRAPPED
|| lastex
!= ey_last
)
4696 window_copy_copy_buffer(struct window_mode_entry
*wme
, const char *prefix
,
4697 void *buf
, size_t len
)
4699 struct window_pane
*wp
= wme
->wp
;
4700 struct screen_write_ctx ctx
;
4702 if (options_get_number(global_options
, "set-clipboard") != 0) {
4703 screen_write_start_pane(&ctx
, wp
, NULL
);
4704 screen_write_setselection(&ctx
, "", buf
, len
);
4705 screen_write_stop(&ctx
);
4706 notify_pane("pane-set-clipboard", wp
);
4709 paste_add(prefix
, buf
, len
);
4713 window_copy_pipe_run(struct window_mode_entry
*wme
, struct session
*s
,
4714 const char *cmd
, size_t *len
)
4719 buf
= window_copy_get_selection(wme
, len
);
4720 if (cmd
== NULL
|| *cmd
== '\0')
4721 cmd
= options_get_string(global_options
, "copy-command");
4722 if (cmd
!= NULL
&& *cmd
!= '\0') {
4723 job
= job_run(cmd
, 0, NULL
, NULL
, s
, NULL
, NULL
, NULL
, NULL
,
4724 NULL
, JOB_NOWAIT
, -1, -1);
4725 bufferevent_write(job_get_event(job
), buf
, *len
);
4731 window_copy_pipe(struct window_mode_entry
*wme
, struct session
*s
,
4736 window_copy_pipe_run(wme
, s
, cmd
, &len
);
4740 window_copy_copy_pipe(struct window_mode_entry
*wme
, struct session
*s
,
4741 const char *prefix
, const char *cmd
)
4746 buf
= window_copy_pipe_run(wme
, s
, cmd
, &len
);
4748 window_copy_copy_buffer(wme
, prefix
, buf
, len
);
4752 window_copy_copy_selection(struct window_mode_entry
*wme
, const char *prefix
)
4757 buf
= window_copy_get_selection(wme
, &len
);
4759 window_copy_copy_buffer(wme
, prefix
, buf
, len
);
4763 window_copy_append_selection(struct window_mode_entry
*wme
)
4765 struct window_pane
*wp
= wme
->wp
;
4767 struct paste_buffer
*pb
;
4768 const char *bufdata
, *bufname
= NULL
;
4769 size_t len
, bufsize
;
4770 struct screen_write_ctx ctx
;
4772 buf
= window_copy_get_selection(wme
, &len
);
4776 if (options_get_number(global_options
, "set-clipboard") != 0) {
4777 screen_write_start_pane(&ctx
, wp
, NULL
);
4778 screen_write_setselection(&ctx
, "", buf
, len
);
4779 screen_write_stop(&ctx
);
4780 notify_pane("pane-set-clipboard", wp
);
4783 pb
= paste_get_top(&bufname
);
4785 bufdata
= paste_buffer_data(pb
, &bufsize
);
4786 buf
= xrealloc(buf
, len
+ bufsize
);
4787 memmove(buf
+ bufsize
, buf
, len
);
4788 memcpy(buf
, bufdata
, bufsize
);
4791 if (paste_set(buf
, len
, bufname
, NULL
) != 0)
4796 window_copy_copy_line(struct window_mode_entry
*wme
, char **buf
, size_t *off
,
4797 u_int sy
, u_int sx
, u_int ex
)
4799 struct window_copy_mode_data
*data
= wme
->data
;
4800 struct grid
*gd
= data
->backing
->grid
;
4801 struct grid_cell gc
;
4802 struct grid_line
*gl
;
4803 struct utf8_data ud
;
4804 u_int i
, xx
, wrapped
= 0;
4811 * Work out if the line was wrapped at the screen edge and all of it is
4814 gl
= grid_get_line(gd
, sy
);
4815 if (gl
->flags
& GRID_LINE_WRAPPED
&& gl
->cellsize
<= gd
->sx
)
4818 /* If the line was wrapped, don't strip spaces (use the full length). */
4822 xx
= window_copy_find_length(wme
, sy
);
4829 for (i
= sx
; i
< ex
; i
++) {
4830 grid_get_cell(gd
, i
, sy
, &gc
);
4831 if (gc
.flags
& GRID_FLAG_PADDING
)
4833 utf8_copy(&ud
, &gc
.data
);
4834 if (ud
.size
== 1 && (gc
.attr
& GRID_ATTR_CHARSET
)) {
4835 s
= tty_acs_get(NULL
, ud
.data
[0]);
4836 if (s
!= NULL
&& strlen(s
) <= sizeof ud
.data
) {
4837 ud
.size
= strlen(s
);
4838 memcpy(ud
.data
, s
, ud
.size
);
4842 *buf
= xrealloc(*buf
, (*off
) + ud
.size
);
4843 memcpy(*buf
+ *off
, ud
.data
, ud
.size
);
4848 /* Only add a newline if the line wasn't wrapped. */
4849 if (!wrapped
|| ex
!= xx
) {
4850 *buf
= xrealloc(*buf
, (*off
) + 1);
4851 (*buf
)[(*off
)++] = '\n';
4856 window_copy_clear_selection(struct window_mode_entry
*wme
)
4858 struct window_copy_mode_data
*data
= wme
->data
;
4861 screen_clear_selection(&data
->screen
);
4863 data
->cursordrag
= CURSORDRAG_NONE
;
4864 data
->lineflag
= LINE_SEL_NONE
;
4865 data
->selflag
= SEL_CHAR
;
4867 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
4868 px
= window_copy_find_length(wme
, py
);
4870 window_copy_update_cursor(wme
, px
, data
->cy
);
4874 window_copy_in_set(struct window_mode_entry
*wme
, u_int px
, u_int py
,
4877 struct window_copy_mode_data
*data
= wme
->data
;
4878 struct grid_cell gc
;
4880 grid_get_cell(data
->backing
->grid
, px
, py
, &gc
);
4881 if (gc
.flags
& GRID_FLAG_PADDING
)
4883 return (utf8_cstrhas(set
, &gc
.data
));
4887 window_copy_find_length(struct window_mode_entry
*wme
, u_int py
)
4889 struct window_copy_mode_data
*data
= wme
->data
;
4891 return (grid_line_length(data
->backing
->grid
, py
));
4895 window_copy_cursor_start_of_line(struct window_mode_entry
*wme
)
4897 struct window_copy_mode_data
*data
= wme
->data
;
4898 struct screen
*back_s
= data
->backing
;
4899 struct grid_reader gr
;
4900 u_int px
, py
, oldy
, hsize
;
4903 hsize
= screen_hsize(back_s
);
4904 py
= hsize
+ data
->cy
- data
->oy
;
4907 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
4908 grid_reader_cursor_start_of_line(&gr
, 1);
4909 grid_reader_get_cursor(&gr
, &px
, &py
);
4910 window_copy_acquire_cursor_up(wme
, hsize
, data
->oy
, oldy
, px
, py
);
4914 window_copy_cursor_back_to_indentation(struct window_mode_entry
*wme
)
4916 struct window_copy_mode_data
*data
= wme
->data
;
4917 struct screen
*back_s
= data
->backing
;
4918 struct grid_reader gr
;
4919 u_int px
, py
, oldy
, hsize
;
4922 hsize
= screen_hsize(back_s
);
4923 py
= hsize
+ data
->cy
- data
->oy
;
4926 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
4927 grid_reader_cursor_back_to_indentation(&gr
);
4928 grid_reader_get_cursor(&gr
, &px
, &py
);
4929 window_copy_acquire_cursor_up(wme
, hsize
, data
->oy
, oldy
, px
, py
);
4933 window_copy_cursor_end_of_line(struct window_mode_entry
*wme
)
4935 struct window_copy_mode_data
*data
= wme
->data
;
4936 struct screen
*back_s
= data
->backing
;
4937 struct grid_reader gr
;
4938 u_int px
, py
, oldy
, hsize
;
4941 hsize
= screen_hsize(back_s
);
4942 py
= hsize
+ data
->cy
- data
->oy
;
4945 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
4946 if (data
->screen
.sel
!= NULL
&& data
->rectflag
)
4947 grid_reader_cursor_end_of_line(&gr
, 1, 1);
4949 grid_reader_cursor_end_of_line(&gr
, 1, 0);
4950 grid_reader_get_cursor(&gr
, &px
, &py
);
4951 window_copy_acquire_cursor_down(wme
, hsize
, screen_size_y(back_s
),
4952 data
->oy
, oldy
, px
, py
, 0);
4956 window_copy_other_end(struct window_mode_entry
*wme
)
4958 struct window_copy_mode_data
*data
= wme
->data
;
4959 struct screen
*s
= &data
->screen
;
4960 u_int selx
, sely
, cy
, yy
, hsize
;
4962 if (s
->sel
== NULL
&& data
->lineflag
== LINE_SEL_NONE
)
4965 if (data
->lineflag
== LINE_SEL_LEFT_RIGHT
)
4966 data
->lineflag
= LINE_SEL_RIGHT_LEFT
;
4967 else if (data
->lineflag
== LINE_SEL_RIGHT_LEFT
)
4968 data
->lineflag
= LINE_SEL_LEFT_RIGHT
;
4970 switch (data
->cursordrag
) {
4971 case CURSORDRAG_NONE
:
4972 case CURSORDRAG_SEL
:
4973 data
->cursordrag
= CURSORDRAG_ENDSEL
;
4975 case CURSORDRAG_ENDSEL
:
4976 data
->cursordrag
= CURSORDRAG_SEL
;
4980 selx
= data
->endselx
;
4981 sely
= data
->endsely
;
4982 if (data
->cursordrag
== CURSORDRAG_SEL
) {
4988 yy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
4992 hsize
= screen_hsize(data
->backing
);
4993 if (sely
< hsize
- data
->oy
) { /* above */
4994 data
->oy
= hsize
- sely
;
4996 } else if (sely
> hsize
- data
->oy
+ screen_size_y(s
)) { /* below */
4997 data
->oy
= hsize
- sely
+ screen_size_y(s
) - 1;
4998 data
->cy
= screen_size_y(s
) - 1;
5000 data
->cy
= cy
+ sely
- yy
;
5002 window_copy_update_selection(wme
, 1, 1);
5003 window_copy_redraw_screen(wme
);
5007 window_copy_cursor_left(struct window_mode_entry
*wme
)
5009 struct window_copy_mode_data
*data
= wme
->data
;
5010 struct screen
*back_s
= data
->backing
;
5011 struct grid_reader gr
;
5012 u_int px
, py
, oldy
, hsize
;
5015 hsize
= screen_hsize(back_s
);
5016 py
= hsize
+ data
->cy
- data
->oy
;
5019 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
5020 grid_reader_cursor_left(&gr
, 1);
5021 grid_reader_get_cursor(&gr
, &px
, &py
);
5022 window_copy_acquire_cursor_up(wme
, hsize
, data
->oy
, oldy
, px
, py
);
5026 window_copy_cursor_right(struct window_mode_entry
*wme
, int all
)
5028 struct window_copy_mode_data
*data
= wme
->data
;
5029 struct screen
*back_s
= data
->backing
;
5030 struct grid_reader gr
;
5031 u_int px
, py
, oldy
, hsize
;
5034 hsize
= screen_hsize(back_s
);
5035 py
= hsize
+ data
->cy
- data
->oy
;
5038 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
5039 grid_reader_cursor_right(&gr
, 1, all
);
5040 grid_reader_get_cursor(&gr
, &px
, &py
);
5041 window_copy_acquire_cursor_down(wme
, hsize
, screen_size_y(back_s
),
5042 data
->oy
, oldy
, px
, py
, 0);
5046 window_copy_cursor_up(struct window_mode_entry
*wme
, int scroll_only
)
5048 struct window_copy_mode_data
*data
= wme
->data
;
5049 struct screen
*s
= &data
->screen
;
5050 u_int ox
, oy
, px
, py
;
5053 norectsel
= data
->screen
.sel
== NULL
|| !data
->rectflag
;
5054 oy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
5055 ox
= window_copy_find_length(wme
, oy
);
5056 if (norectsel
&& data
->cx
!= ox
) {
5057 data
->lastcx
= data
->cx
;
5061 if (data
->lineflag
== LINE_SEL_LEFT_RIGHT
&& oy
== data
->sely
)
5062 window_copy_other_end(wme
);
5064 if (scroll_only
|| data
->cy
== 0) {
5066 data
->cx
= data
->lastcx
;
5067 window_copy_scroll_down(wme
, 1);
5069 if (data
->cy
== screen_size_y(s
) - 1)
5070 window_copy_redraw_lines(wme
, data
->cy
, 1);
5072 window_copy_redraw_lines(wme
, data
->cy
, 2);
5076 window_copy_update_cursor(wme
, data
->lastcx
,
5079 window_copy_update_cursor(wme
, data
->cx
, data
->cy
- 1);
5080 if (window_copy_update_selection(wme
, 1, 0)) {
5081 if (data
->cy
== screen_size_y(s
) - 1)
5082 window_copy_redraw_lines(wme
, data
->cy
, 1);
5084 window_copy_redraw_lines(wme
, data
->cy
, 2);
5089 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
5090 px
= window_copy_find_length(wme
, py
);
5091 if ((data
->cx
>= data
->lastsx
&& data
->cx
!= px
) ||
5094 window_copy_update_cursor(wme
, px
, data
->cy
);
5095 if (window_copy_update_selection(wme
, 1, 0))
5096 window_copy_redraw_lines(wme
, data
->cy
, 1);
5100 if (data
->lineflag
== LINE_SEL_LEFT_RIGHT
)
5102 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
5104 px
= screen_size_x(data
->backing
);
5106 px
= window_copy_find_length(wme
, py
);
5107 window_copy_update_cursor(wme
, px
, data
->cy
);
5108 if (window_copy_update_selection(wme
, 1, 0))
5109 window_copy_redraw_lines(wme
, data
->cy
, 1);
5111 else if (data
->lineflag
== LINE_SEL_RIGHT_LEFT
)
5113 window_copy_update_cursor(wme
, 0, data
->cy
);
5114 if (window_copy_update_selection(wme
, 1, 0))
5115 window_copy_redraw_lines(wme
, data
->cy
, 1);
5120 window_copy_cursor_down(struct window_mode_entry
*wme
, int scroll_only
)
5122 struct window_copy_mode_data
*data
= wme
->data
;
5123 struct screen
*s
= &data
->screen
;
5124 u_int ox
, oy
, px
, py
;
5127 norectsel
= data
->screen
.sel
== NULL
|| !data
->rectflag
;
5128 oy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
5129 ox
= window_copy_find_length(wme
, oy
);
5130 if (norectsel
&& data
->cx
!= ox
) {
5131 data
->lastcx
= data
->cx
;
5135 if (data
->lineflag
== LINE_SEL_RIGHT_LEFT
&& oy
== data
->endsely
)
5136 window_copy_other_end(wme
);
5138 if (scroll_only
|| data
->cy
== screen_size_y(s
) - 1) {
5140 data
->cx
= data
->lastcx
;
5141 window_copy_scroll_up(wme
, 1);
5142 if (scroll_only
&& data
->cy
> 0)
5143 window_copy_redraw_lines(wme
, data
->cy
- 1, 2);
5146 window_copy_update_cursor(wme
, data
->lastcx
,
5149 window_copy_update_cursor(wme
, data
->cx
, data
->cy
+ 1);
5150 if (window_copy_update_selection(wme
, 1, 0))
5151 window_copy_redraw_lines(wme
, data
->cy
- 1, 2);
5155 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
5156 px
= window_copy_find_length(wme
, py
);
5157 if ((data
->cx
>= data
->lastsx
&& data
->cx
!= px
) ||
5160 window_copy_update_cursor(wme
, px
, data
->cy
);
5161 if (window_copy_update_selection(wme
, 1, 0))
5162 window_copy_redraw_lines(wme
, data
->cy
, 1);
5166 if (data
->lineflag
== LINE_SEL_LEFT_RIGHT
)
5168 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
5170 px
= screen_size_x(data
->backing
);
5172 px
= window_copy_find_length(wme
, py
);
5173 window_copy_update_cursor(wme
, px
, data
->cy
);
5174 if (window_copy_update_selection(wme
, 1, 0))
5175 window_copy_redraw_lines(wme
, data
->cy
, 1);
5177 else if (data
->lineflag
== LINE_SEL_RIGHT_LEFT
)
5179 window_copy_update_cursor(wme
, 0, data
->cy
);
5180 if (window_copy_update_selection(wme
, 1, 0))
5181 window_copy_redraw_lines(wme
, data
->cy
, 1);
5186 window_copy_cursor_jump(struct window_mode_entry
*wme
)
5188 struct window_copy_mode_data
*data
= wme
->data
;
5189 struct screen
*back_s
= data
->backing
;
5190 struct grid_reader gr
;
5191 u_int px
, py
, oldy
, hsize
;
5194 hsize
= screen_hsize(back_s
);
5195 py
= hsize
+ data
->cy
- data
->oy
;
5198 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
5199 if (grid_reader_cursor_jump(&gr
, data
->jumpchar
)) {
5200 grid_reader_get_cursor(&gr
, &px
, &py
);
5201 window_copy_acquire_cursor_down(wme
, hsize
,
5202 screen_size_y(back_s
), data
->oy
, oldy
, px
, py
, 0);
5207 window_copy_cursor_jump_back(struct window_mode_entry
*wme
)
5209 struct window_copy_mode_data
*data
= wme
->data
;
5210 struct screen
*back_s
= data
->backing
;
5211 struct grid_reader gr
;
5212 u_int px
, py
, oldy
, hsize
;
5215 hsize
= screen_hsize(back_s
);
5216 py
= hsize
+ data
->cy
- data
->oy
;
5219 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
5220 grid_reader_cursor_left(&gr
, 0);
5221 if (grid_reader_cursor_jump_back(&gr
, data
->jumpchar
)) {
5222 grid_reader_get_cursor(&gr
, &px
, &py
);
5223 window_copy_acquire_cursor_up(wme
, hsize
, data
->oy
, oldy
, px
,
5229 window_copy_cursor_jump_to(struct window_mode_entry
*wme
)
5231 struct window_copy_mode_data
*data
= wme
->data
;
5232 struct screen
*back_s
= data
->backing
;
5233 struct grid_reader gr
;
5234 u_int px
, py
, oldy
, hsize
;
5237 hsize
= screen_hsize(back_s
);
5238 py
= hsize
+ data
->cy
- data
->oy
;
5241 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
5242 if (grid_reader_cursor_jump(&gr
, data
->jumpchar
)) {
5243 grid_reader_cursor_left(&gr
, 1);
5244 grid_reader_get_cursor(&gr
, &px
, &py
);
5245 window_copy_acquire_cursor_down(wme
, hsize
,
5246 screen_size_y(back_s
), data
->oy
, oldy
, px
, py
, 0);
5251 window_copy_cursor_jump_to_back(struct window_mode_entry
*wme
)
5253 struct window_copy_mode_data
*data
= wme
->data
;
5254 struct screen
*back_s
= data
->backing
;
5255 struct grid_reader gr
;
5256 u_int px
, py
, oldy
, hsize
;
5259 hsize
= screen_hsize(back_s
);
5260 py
= hsize
+ data
->cy
- data
->oy
;
5263 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
5264 grid_reader_cursor_left(&gr
, 0);
5265 grid_reader_cursor_left(&gr
, 0);
5266 if (grid_reader_cursor_jump_back(&gr
, data
->jumpchar
)) {
5267 grid_reader_cursor_right(&gr
, 1, 0);
5268 grid_reader_get_cursor(&gr
, &px
, &py
);
5269 window_copy_acquire_cursor_up(wme
, hsize
, data
->oy
, oldy
, px
,
5275 window_copy_cursor_next_word(struct window_mode_entry
*wme
,
5276 const char *separators
)
5278 struct window_copy_mode_data
*data
= wme
->data
;
5279 struct screen
*back_s
= data
->backing
;
5280 struct grid_reader gr
;
5281 u_int px
, py
, oldy
, hsize
;
5284 hsize
= screen_hsize(back_s
);
5285 py
= hsize
+ data
->cy
- data
->oy
;
5288 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
5289 grid_reader_cursor_next_word(&gr
, separators
);
5290 grid_reader_get_cursor(&gr
, &px
, &py
);
5291 window_copy_acquire_cursor_down(wme
, hsize
, screen_size_y(back_s
),
5292 data
->oy
, oldy
, px
, py
, 0);
5295 /* Compute the next place where a word ends. */
5297 window_copy_cursor_next_word_end_pos(struct window_mode_entry
*wme
,
5298 const char *separators
, u_int
*ppx
, u_int
*ppy
)
5300 struct window_pane
*wp
= wme
->wp
;
5301 struct window_copy_mode_data
*data
= wme
->data
;
5302 struct options
*oo
= wp
->window
->options
;
5303 struct screen
*back_s
= data
->backing
;
5304 struct grid_reader gr
;
5305 u_int px
, py
, hsize
;
5308 hsize
= screen_hsize(back_s
);
5309 py
= hsize
+ data
->cy
- data
->oy
;
5311 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
5312 if (options_get_number(oo
, "mode-keys") == MODEKEY_VI
) {
5313 if (!grid_reader_in_set(&gr
, WHITESPACE
))
5314 grid_reader_cursor_right(&gr
, 0, 0);
5315 grid_reader_cursor_next_word_end(&gr
, separators
);
5316 grid_reader_cursor_left(&gr
, 1);
5318 grid_reader_cursor_next_word_end(&gr
, separators
);
5319 grid_reader_get_cursor(&gr
, &px
, &py
);
5324 /* Move to the next place where a word ends. */
5326 window_copy_cursor_next_word_end(struct window_mode_entry
*wme
,
5327 const char *separators
, int no_reset
)
5329 struct window_pane
*wp
= wme
->wp
;
5330 struct window_copy_mode_data
*data
= wme
->data
;
5331 struct options
*oo
= wp
->window
->options
;
5332 struct screen
*back_s
= data
->backing
;
5333 struct grid_reader gr
;
5334 u_int px
, py
, oldy
, hsize
;
5337 hsize
= screen_hsize(back_s
);
5338 py
= hsize
+ data
->cy
- data
->oy
;
5341 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
5342 if (options_get_number(oo
, "mode-keys") == MODEKEY_VI
) {
5343 if (!grid_reader_in_set(&gr
, WHITESPACE
))
5344 grid_reader_cursor_right(&gr
, 0, 0);
5345 grid_reader_cursor_next_word_end(&gr
, separators
);
5346 grid_reader_cursor_left(&gr
, 1);
5348 grid_reader_cursor_next_word_end(&gr
, separators
);
5349 grid_reader_get_cursor(&gr
, &px
, &py
);
5350 window_copy_acquire_cursor_down(wme
, hsize
, screen_size_y(back_s
),
5351 data
->oy
, oldy
, px
, py
, no_reset
);
5354 /* Compute the previous place where a word begins. */
5356 window_copy_cursor_previous_word_pos(struct window_mode_entry
*wme
,
5357 const char *separators
, u_int
*ppx
, u_int
*ppy
)
5359 struct window_copy_mode_data
*data
= wme
->data
;
5360 struct screen
*back_s
= data
->backing
;
5361 struct grid_reader gr
;
5362 u_int px
, py
, hsize
;
5365 hsize
= screen_hsize(back_s
);
5366 py
= hsize
+ data
->cy
- data
->oy
;
5368 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
5369 grid_reader_cursor_previous_word(&gr
, separators
, /* already= */ 0,
5370 /* stop_at_eol= */ 1);
5371 grid_reader_get_cursor(&gr
, &px
, &py
);
5376 /* Move to the previous place where a word begins. */
5378 window_copy_cursor_previous_word(struct window_mode_entry
*wme
,
5379 const char *separators
, int already
)
5381 struct window_copy_mode_data
*data
= wme
->data
;
5382 struct window
*w
= wme
->wp
->window
;
5383 struct screen
*back_s
= data
->backing
;
5384 struct grid_reader gr
;
5385 u_int px
, py
, oldy
, hsize
;
5388 if (options_get_number(w
->options
, "mode-keys") == MODEKEY_EMACS
)
5394 hsize
= screen_hsize(back_s
);
5395 py
= hsize
+ data
->cy
- data
->oy
;
5398 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
5399 grid_reader_cursor_previous_word(&gr
, separators
, already
, stop_at_eol
);
5400 grid_reader_get_cursor(&gr
, &px
, &py
);
5401 window_copy_acquire_cursor_up(wme
, hsize
, data
->oy
, oldy
, px
, py
);
5405 window_copy_cursor_prompt(struct window_mode_entry
*wme
, int direction
,
5408 struct window_copy_mode_data
*data
= wme
->data
;
5409 struct screen
*s
= data
->backing
;
5410 struct grid
*gd
= s
->grid
;
5412 u_int line
= gd
->hsize
- data
->oy
+ data
->cy
;
5415 if (args
!= NULL
&& strcmp(args
, "-o") == 0)
5416 line_flag
= GRID_LINE_START_OUTPUT
;
5418 line_flag
= GRID_LINE_START_PROMPT
;
5420 if (direction
== 0) { /* up */
5425 end_line
= gd
->hsize
+ gd
->sy
- 1;
5428 if (line
== end_line
)
5431 if (line
== end_line
)
5435 if (grid_get_line(gd
, line
)->flags
& line_flag
)
5440 if (line
> gd
->hsize
) {
5441 data
->cy
= line
- gd
->hsize
;
5445 data
->oy
= gd
->hsize
- line
;
5448 window_copy_update_selection(wme
, 1, 0);
5449 window_copy_redraw_screen(wme
);
5453 window_copy_scroll_up(struct window_mode_entry
*wme
, u_int ny
)
5455 struct window_pane
*wp
= wme
->wp
;
5456 struct window_copy_mode_data
*data
= wme
->data
;
5457 struct screen
*s
= &data
->screen
;
5458 struct screen_write_ctx ctx
;
5466 if (data
->searchmark
!= NULL
&& !data
->timeout
)
5467 window_copy_search_marks(wme
, NULL
, data
->searchregex
, 1);
5468 window_copy_update_selection(wme
, 0, 0);
5470 screen_write_start_pane(&ctx
, wp
, NULL
);
5471 screen_write_cursormove(&ctx
, 0, 0, 0);
5472 screen_write_deleteline(&ctx
, ny
, 8);
5473 window_copy_write_lines(wme
, &ctx
, screen_size_y(s
) - ny
, ny
);
5474 window_copy_write_line(wme
, &ctx
, 0);
5475 if (screen_size_y(s
) > 1)
5476 window_copy_write_line(wme
, &ctx
, 1);
5477 if (screen_size_y(s
) > 3)
5478 window_copy_write_line(wme
, &ctx
, screen_size_y(s
) - 2);
5479 if (s
->sel
!= NULL
&& screen_size_y(s
) > ny
)
5480 window_copy_write_line(wme
, &ctx
, screen_size_y(s
) - ny
- 1);
5481 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
, 0);
5482 screen_write_stop(&ctx
);
5486 window_copy_scroll_down(struct window_mode_entry
*wme
, u_int ny
)
5488 struct window_pane
*wp
= wme
->wp
;
5489 struct window_copy_mode_data
*data
= wme
->data
;
5490 struct screen
*s
= &data
->screen
;
5491 struct screen_write_ctx ctx
;
5493 if (ny
> screen_hsize(data
->backing
))
5496 if (data
->oy
> screen_hsize(data
->backing
) - ny
)
5497 ny
= screen_hsize(data
->backing
) - data
->oy
;
5502 if (data
->searchmark
!= NULL
&& !data
->timeout
)
5503 window_copy_search_marks(wme
, NULL
, data
->searchregex
, 1);
5504 window_copy_update_selection(wme
, 0, 0);
5506 screen_write_start_pane(&ctx
, wp
, NULL
);
5507 screen_write_cursormove(&ctx
, 0, 0, 0);
5508 screen_write_insertline(&ctx
, ny
, 8);
5509 window_copy_write_lines(wme
, &ctx
, 0, ny
);
5510 if (s
->sel
!= NULL
&& screen_size_y(s
) > ny
)
5511 window_copy_write_line(wme
, &ctx
, ny
);
5512 else if (ny
== 1) /* nuke position */
5513 window_copy_write_line(wme
, &ctx
, 1);
5514 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
, 0);
5515 screen_write_stop(&ctx
);
5519 window_copy_rectangle_set(struct window_mode_entry
*wme
, int rectflag
)
5521 struct window_copy_mode_data
*data
= wme
->data
;
5524 data
->rectflag
= rectflag
;
5526 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
5527 px
= window_copy_find_length(wme
, py
);
5529 window_copy_update_cursor(wme
, px
, data
->cy
);
5531 window_copy_update_selection(wme
, 1, 0);
5532 window_copy_redraw_screen(wme
);
5536 window_copy_move_mouse(struct mouse_event
*m
)
5538 struct window_pane
*wp
;
5539 struct window_mode_entry
*wme
;
5542 wp
= cmd_mouse_pane(m
, NULL
, NULL
);
5545 wme
= TAILQ_FIRST(&wp
->modes
);
5548 if (wme
->mode
!= &window_copy_mode
&& wme
->mode
!= &window_view_mode
)
5551 if (cmd_mouse_at(wp
, m
, &x
, &y
, 0) != 0)
5554 window_copy_update_cursor(wme
, x
, y
);
5558 window_copy_start_drag(struct client
*c
, struct mouse_event
*m
)
5560 struct window_pane
*wp
;
5561 struct window_mode_entry
*wme
;
5562 struct window_copy_mode_data
*data
;
5568 wp
= cmd_mouse_pane(m
, NULL
, NULL
);
5571 wme
= TAILQ_FIRST(&wp
->modes
);
5574 if (wme
->mode
!= &window_copy_mode
&& wme
->mode
!= &window_view_mode
)
5577 if (cmd_mouse_at(wp
, m
, &x
, &y
, 1) != 0)
5580 c
->tty
.mouse_drag_update
= window_copy_drag_update
;
5581 c
->tty
.mouse_drag_release
= window_copy_drag_release
;
5584 yg
= screen_hsize(data
->backing
) + y
- data
->oy
;
5585 if (x
< data
->selrx
|| x
> data
->endselrx
|| yg
!= data
->selry
)
5586 data
->selflag
= SEL_CHAR
;
5587 switch (data
->selflag
) {
5589 if (data
->separators
!= NULL
) {
5590 window_copy_update_cursor(wme
, x
, y
);
5591 window_copy_cursor_previous_word_pos(wme
,
5592 data
->separators
, &x
, &y
);
5593 y
-= screen_hsize(data
->backing
) - data
->oy
;
5595 window_copy_update_cursor(wme
, x
, y
);
5598 window_copy_update_cursor(wme
, 0, y
);
5601 window_copy_update_cursor(wme
, x
, y
);
5602 window_copy_start_selection(wme
);
5606 window_copy_redraw_screen(wme
);
5607 window_copy_drag_update(c
, m
);
5611 window_copy_drag_update(struct client
*c
, struct mouse_event
*m
)
5613 struct window_pane
*wp
;
5614 struct window_mode_entry
*wme
;
5615 struct window_copy_mode_data
*data
;
5616 u_int x
, y
, old_cx
, old_cy
;
5617 struct timeval tv
= {
5618 .tv_usec
= WINDOW_COPY_DRAG_REPEAT_TIME
5624 wp
= cmd_mouse_pane(m
, NULL
, NULL
);
5627 wme
= TAILQ_FIRST(&wp
->modes
);
5630 if (wme
->mode
!= &window_copy_mode
&& wme
->mode
!= &window_view_mode
)
5634 evtimer_del(&data
->dragtimer
);
5636 if (cmd_mouse_at(wp
, m
, &x
, &y
, 0) != 0)
5641 window_copy_update_cursor(wme
, x
, y
);
5642 if (window_copy_update_selection(wme
, 1, 0))
5643 window_copy_redraw_selection(wme
, old_cy
);
5644 if (old_cy
!= data
->cy
|| old_cx
== data
->cx
) {
5646 evtimer_add(&data
->dragtimer
, &tv
);
5647 window_copy_cursor_up(wme
, 1);
5648 } else if (y
== screen_size_y(&data
->screen
) - 1) {
5649 evtimer_add(&data
->dragtimer
, &tv
);
5650 window_copy_cursor_down(wme
, 1);
5656 window_copy_drag_release(struct client
*c
, struct mouse_event
*m
)
5658 struct window_pane
*wp
;
5659 struct window_mode_entry
*wme
;
5660 struct window_copy_mode_data
*data
;
5665 wp
= cmd_mouse_pane(m
, NULL
, NULL
);
5668 wme
= TAILQ_FIRST(&wp
->modes
);
5671 if (wme
->mode
!= &window_copy_mode
&& wme
->mode
!= &window_view_mode
)
5675 evtimer_del(&data
->dragtimer
);
5679 window_copy_jump_to_mark(struct window_mode_entry
*wme
)
5681 struct window_copy_mode_data
*data
= wme
->data
;
5685 tmy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
5686 data
->cx
= data
->mx
;
5687 if (data
->my
< screen_hsize(data
->backing
)) {
5689 data
->oy
= screen_hsize(data
->backing
) - data
->my
;
5691 data
->cy
= data
->my
- screen_hsize(data
->backing
);
5697 window_copy_update_selection(wme
, 0, 0);
5698 window_copy_redraw_screen(wme
);
5701 /* Scroll up if the cursor went off the visible screen. */
5703 window_copy_acquire_cursor_up(struct window_mode_entry
*wme
, u_int hsize
,
5704 u_int oy
, u_int oldy
, u_int px
, u_int py
)
5706 u_int cy
, yy
, ny
, nd
;
5719 window_copy_cursor_up(wme
, 1);
5722 window_copy_update_cursor(wme
, px
, cy
);
5723 if (window_copy_update_selection(wme
, 1, 0))
5724 window_copy_redraw_lines(wme
, cy
, nd
);
5727 /* Scroll down if the cursor went off the visible screen. */
5729 window_copy_acquire_cursor_down(struct window_mode_entry
*wme
, u_int hsize
,
5730 u_int sy
, u_int oy
, u_int oldy
, u_int px
, u_int py
, int no_reset
)
5732 u_int cy
, yy
, ny
, nd
;
5734 cy
= py
- hsize
+ oy
;
5745 window_copy_cursor_down(wme
, 1);
5749 window_copy_update_cursor(wme
, px
, yy
);
5751 window_copy_update_cursor(wme
, px
, cy
);
5752 if (window_copy_update_selection(wme
, 1, no_reset
))
5753 window_copy_redraw_lines(wme
, oldy
, nd
);