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);
135 static void window_copy_scroll_up(struct window_mode_entry
*, u_int
);
136 static void window_copy_scroll_down(struct window_mode_entry
*, u_int
);
137 static void window_copy_rectangle_set(struct window_mode_entry
*, int);
138 static void window_copy_move_mouse(struct mouse_event
*);
139 static void window_copy_drag_update(struct client
*, struct mouse_event
*);
140 static void window_copy_drag_release(struct client
*, struct mouse_event
*);
141 static void window_copy_jump_to_mark(struct window_mode_entry
*);
142 static void window_copy_acquire_cursor_up(struct window_mode_entry
*,
143 u_int
, u_int
, u_int
, u_int
, u_int
);
144 static void window_copy_acquire_cursor_down(struct window_mode_entry
*,
145 u_int
, u_int
, u_int
, u_int
, u_int
, u_int
, int);
147 const struct window_mode window_copy_mode
= {
150 .init
= window_copy_init
,
151 .free
= window_copy_free
,
152 .resize
= window_copy_resize
,
153 .key_table
= window_copy_key_table
,
154 .command
= window_copy_command
,
155 .formats
= window_copy_formats
,
158 const struct window_mode window_view_mode
= {
161 .init
= window_copy_view_init
,
162 .free
= window_copy_free
,
163 .resize
= window_copy_resize
,
164 .key_table
= window_copy_key_table
,
165 .command
= window_copy_command
,
166 .formats
= window_copy_formats
,
171 WINDOW_COPY_SEARCHUP
,
172 WINDOW_COPY_SEARCHDOWN
,
173 WINDOW_COPY_JUMPFORWARD
,
174 WINDOW_COPY_JUMPBACKWARD
,
175 WINDOW_COPY_JUMPTOFORWARD
,
176 WINDOW_COPY_JUMPTOBACKWARD
,
180 WINDOW_COPY_REL_POS_ABOVE
,
181 WINDOW_COPY_REL_POS_ON_SCREEN
,
182 WINDOW_COPY_REL_POS_BELOW
,
185 enum window_copy_cmd_action
{
186 WINDOW_COPY_CMD_NOTHING
,
187 WINDOW_COPY_CMD_REDRAW
,
188 WINDOW_COPY_CMD_CANCEL
,
191 enum window_copy_cmd_clear
{
192 WINDOW_COPY_CMD_CLEAR_ALWAYS
,
193 WINDOW_COPY_CMD_CLEAR_NEVER
,
194 WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
197 struct window_copy_cmd_state
{
198 struct window_mode_entry
*wme
;
200 struct mouse_event
*m
;
208 * Copy mode's visible screen (the "screen" field) is filled from one of two
209 * sources: the original contents of the pane (used when we actually enter via
210 * the "copy-mode" command, to copy the contents of the current pane), or else
211 * a series of lines containing the output from an output-writing tmux command
212 * (such as any of the "show-*" or "list-*" commands).
214 * In either case, the full content of the copy-mode grid is pointed at by the
215 * "backing" field, and is copied into "screen" as needed (that is, when
216 * scrolling occurs). When copy-mode is backed by a pane, backing points
217 * directly at that pane's screen structure (&wp->base); when backed by a list
218 * of output-lines from a command, it points at a newly-allocated screen
219 * structure (which is deallocated when the mode ends).
221 struct window_copy_mode_data
{
222 struct screen screen
;
224 struct screen
*backing
;
225 int backing_written
; /* backing display started */
226 struct screen
*writing
;
227 struct input_ctx
*ictx
;
229 int viewmode
; /* view mode entered */
231 u_int oy
; /* number of lines scrolled up */
233 u_int selx
; /* beginning of selection */
236 u_int endselx
; /* end of selection */
240 CURSORDRAG_NONE
, /* selection is independent of cursor */
241 CURSORDRAG_ENDSEL
, /* end is synchronized with cursor */
242 CURSORDRAG_SEL
, /* start is synchronized with cursor */
250 } lineflag
; /* line selection mode */
251 int rectflag
; /* in rectangle copy mode? */
252 int scroll_exit
; /* exit on scroll to end? */
253 int hide_position
; /* hide position marker */
256 SEL_CHAR
, /* select one char at a time */
257 SEL_WORD
, /* select one word at a time */
258 SEL_LINE
, /* select one line at a time */
261 const char *separators
; /* word separators */
263 u_int dx
; /* drag start position */
266 u_int selrx
; /* selection reset positions */
274 u_int lastcx
; /* position in last line w/ content */
275 u_int lastsx
; /* size of last line w/ content */
277 u_int mx
; /* mark position */
294 int timeout
; /* search has timed out */
295 #define WINDOW_COPY_SEARCH_TIMEOUT 10000
296 #define WINDOW_COPY_SEARCH_ALL_TIMEOUT 200
299 struct utf8_data
*jumpchar
;
301 struct event dragtimer
;
302 #define WINDOW_COPY_DRAG_REPEAT_TIME 50000
306 window_copy_scroll_timer(__unused
int fd
, __unused
short events
, void *arg
)
308 struct window_mode_entry
*wme
= arg
;
309 struct window_pane
*wp
= wme
->wp
;
310 struct window_copy_mode_data
*data
= wme
->data
;
311 struct timeval tv
= {
312 .tv_usec
= WINDOW_COPY_DRAG_REPEAT_TIME
315 evtimer_del(&data
->dragtimer
);
317 if (TAILQ_FIRST(&wp
->modes
) != wme
)
321 evtimer_add(&data
->dragtimer
, &tv
);
322 window_copy_cursor_up(wme
, 1);
323 } else if (data
->cy
== screen_size_y(&data
->screen
) - 1) {
324 evtimer_add(&data
->dragtimer
, &tv
);
325 window_copy_cursor_down(wme
, 1);
329 static struct screen
*
330 window_copy_clone_screen(struct screen
*src
, struct screen
*hint
, u_int
*cx
,
334 const struct grid_line
*gl
;
338 dst
= xcalloc(1, sizeof *dst
);
340 sy
= screen_hsize(src
) + screen_size_y(src
);
342 while (sy
> screen_hsize(src
)) {
343 gl
= grid_peek_line(src
->grid
, sy
- 1);
344 if (gl
->cellused
!= 0)
349 log_debug("%s: target screen is %ux%u, source %ux%u", __func__
,
350 screen_size_x(src
), sy
, screen_size_x(hint
),
351 screen_hsize(src
) + screen_size_y(src
));
352 screen_init(dst
, screen_size_x(src
), sy
, screen_hlimit(src
));
355 * Ensure history is on for the backing grid so lines are not deleted
358 dst
->grid
->flags
|= GRID_HISTORY
;
359 grid_duplicate_lines(dst
->grid
, 0, src
->grid
, 0, sy
);
361 dst
->grid
->sy
= sy
- screen_hsize(src
);
362 dst
->grid
->hsize
= screen_hsize(src
);
363 dst
->grid
->hscrolled
= src
->grid
->hscrolled
;
364 if (src
->cy
> dst
->grid
->sy
- 1) {
366 dst
->cy
= dst
->grid
->sy
- 1;
372 if (cx
!= NULL
&& cy
!= NULL
) {
374 *cy
= screen_hsize(dst
) + dst
->cy
;
375 reflow
= (screen_size_x(hint
) != screen_size_x(dst
));
380 grid_wrap_position(dst
->grid
, *cx
, *cy
, &wx
, &wy
);
381 screen_resize_cursor(dst
, screen_size_x(hint
), screen_size_y(hint
), 1,
384 grid_unwrap_position(dst
->grid
, cx
, cy
, wx
, wy
);
389 static struct window_copy_mode_data
*
390 window_copy_common_init(struct window_mode_entry
*wme
)
392 struct window_pane
*wp
= wme
->wp
;
393 struct window_copy_mode_data
*data
;
394 struct screen
*base
= &wp
->base
;
396 wme
->data
= data
= xcalloc(1, sizeof *data
);
398 data
->cursordrag
= CURSORDRAG_NONE
;
399 data
->lineflag
= LINE_SEL_NONE
;
400 data
->selflag
= SEL_CHAR
;
402 if (wp
->searchstr
!= NULL
) {
403 data
->searchtype
= WINDOW_COPY_SEARCHUP
;
404 data
->searchregex
= wp
->searchregex
;
405 data
->searchstr
= xstrdup(wp
->searchstr
);
407 data
->searchtype
= WINDOW_COPY_OFF
;
408 data
->searchregex
= 0;
409 data
->searchstr
= NULL
;
411 data
->searchx
= data
->searchy
= data
->searcho
= -1;
414 data
->jumptype
= WINDOW_COPY_OFF
;
415 data
->jumpchar
= NULL
;
417 screen_init(&data
->screen
, screen_size_x(base
), screen_size_y(base
), 0);
418 data
->modekeys
= options_get_number(wp
->window
->options
, "mode-keys");
420 evtimer_set(&data
->dragtimer
, window_copy_scroll_timer
, wme
);
425 static struct screen
*
426 window_copy_init(struct window_mode_entry
*wme
,
427 __unused
struct cmd_find_state
*fs
, struct args
*args
)
429 struct window_pane
*wp
= wme
->swp
;
430 struct window_copy_mode_data
*data
;
431 struct screen
*base
= &wp
->base
;
432 struct screen_write_ctx ctx
;
435 data
= window_copy_common_init(wme
);
436 data
->backing
= window_copy_clone_screen(base
, &data
->screen
, &cx
, &cy
,
437 wme
->swp
!= wme
->wp
);
440 if (cy
< screen_hsize(data
->backing
)) {
442 data
->oy
= screen_hsize(data
->backing
) - cy
;
444 data
->cy
= cy
- screen_hsize(data
->backing
);
448 data
->scroll_exit
= args_has(args
, 'e');
449 data
->hide_position
= args_has(args
, 'H');
451 data
->screen
.cx
= data
->cx
;
452 data
->screen
.cy
= data
->cy
;
454 data
->my
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
457 screen_write_start(&ctx
, &data
->screen
);
458 for (i
= 0; i
< screen_size_y(&data
->screen
); i
++)
459 window_copy_write_line(wme
, &ctx
, i
);
460 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
, 0);
461 screen_write_stop(&ctx
);
463 return (&data
->screen
);
466 static struct screen
*
467 window_copy_view_init(struct window_mode_entry
*wme
,
468 __unused
struct cmd_find_state
*fs
, __unused
struct args
*args
)
470 struct window_pane
*wp
= wme
->wp
;
471 struct window_copy_mode_data
*data
;
472 struct screen
*base
= &wp
->base
;
473 u_int sx
= screen_size_x(base
);
475 data
= window_copy_common_init(wme
);
478 data
->backing
= xmalloc(sizeof *data
->backing
);
479 screen_init(data
->backing
, sx
, screen_size_y(base
), UINT_MAX
);
480 data
->writing
= xmalloc(sizeof *data
->writing
);
481 screen_init(data
->writing
, sx
, screen_size_y(base
), 0);
482 data
->ictx
= input_init(NULL
, NULL
, NULL
);
484 data
->my
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
487 return (&data
->screen
);
491 window_copy_free(struct window_mode_entry
*wme
)
493 struct window_copy_mode_data
*data
= wme
->data
;
495 evtimer_del(&data
->dragtimer
);
497 free(data
->searchmark
);
498 free(data
->searchstr
);
499 free(data
->jumpchar
);
501 if (data
->writing
!= NULL
) {
502 screen_free(data
->writing
);
505 if (data
->ictx
!= NULL
)
506 input_free(data
->ictx
);
507 screen_free(data
->backing
);
510 screen_free(&data
->screen
);
515 window_copy_add(struct window_pane
*wp
, int parse
, const char *fmt
, ...)
520 window_copy_vadd(wp
, parse
, fmt
, ap
);
525 window_copy_init_ctx_cb(__unused
struct screen_write_ctx
*ctx
,
526 struct tty_ctx
*ttyctx
)
528 memcpy(&ttyctx
->defaults
, &grid_default_cell
, sizeof ttyctx
->defaults
);
529 ttyctx
->palette
= NULL
;
530 ttyctx
->redraw_cb
= NULL
;
531 ttyctx
->set_client_cb
= NULL
;
536 window_copy_vadd(struct window_pane
*wp
, int parse
, const char *fmt
, va_list ap
)
538 struct window_mode_entry
*wme
= TAILQ_FIRST(&wp
->modes
);
539 struct window_copy_mode_data
*data
= wme
->data
;
540 struct screen
*backing
= data
->backing
;
541 struct screen
*writing
= data
->writing
;
542 struct screen_write_ctx writing_ctx
, backing_ctx
, ctx
;
544 u_int old_hsize
, old_cy
;
545 u_int sx
= screen_size_x(backing
);
549 vasprintf(&text
, fmt
, ap
);
550 screen_write_start(&writing_ctx
, writing
);
551 screen_write_reset(&writing_ctx
);
552 input_parse_screen(data
->ictx
, writing
, window_copy_init_ctx_cb
,
553 data
, text
, strlen(text
));
557 old_hsize
= screen_hsize(data
->backing
);
558 screen_write_start(&backing_ctx
, backing
);
559 if (data
->backing_written
) {
561 * On the second or later line, do a CRLF before writing
562 * (so it's on a new line).
564 screen_write_carriagereturn(&backing_ctx
);
565 screen_write_linefeed(&backing_ctx
, 0, 8);
567 data
->backing_written
= 1;
568 old_cy
= backing
->cy
;
570 screen_write_fast_copy(&backing_ctx
, writing
, 0, 0, sx
, 1);
572 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
573 screen_write_vnputs(&backing_ctx
, 0, &gc
, fmt
, ap
);
575 screen_write_stop(&backing_ctx
);
577 data
->oy
+= screen_hsize(data
->backing
) - old_hsize
;
579 screen_write_start_pane(&ctx
, wp
, &data
->screen
);
582 * If the history has changed, draw the top line.
583 * (If there's any history at all, it has changed.)
585 if (screen_hsize(data
->backing
))
586 window_copy_redraw_lines(wme
, 0, 1);
588 /* Write the new lines. */
589 window_copy_redraw_lines(wme
, old_cy
, backing
->cy
- old_cy
+ 1);
591 screen_write_stop(&ctx
);
595 window_copy_pageup(struct window_pane
*wp
, int half_page
)
597 window_copy_pageup1(TAILQ_FIRST(&wp
->modes
), half_page
);
601 window_copy_pageup1(struct window_mode_entry
*wme
, int half_page
)
603 struct window_copy_mode_data
*data
= wme
->data
;
604 struct screen
*s
= &data
->screen
;
605 u_int n
, ox
, oy
, px
, py
;
607 oy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
608 ox
= window_copy_find_length(wme
, oy
);
610 if (data
->cx
!= ox
) {
611 data
->lastcx
= data
->cx
;
614 data
->cx
= data
->lastcx
;
617 if (screen_size_y(s
) > 2) {
619 n
= screen_size_y(s
) / 2;
621 n
= screen_size_y(s
) - 2;
624 if (data
->oy
+ n
> screen_hsize(data
->backing
)) {
625 data
->oy
= screen_hsize(data
->backing
);
633 if (data
->screen
.sel
== NULL
|| !data
->rectflag
) {
634 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
635 px
= window_copy_find_length(wme
, py
);
636 if ((data
->cx
>= data
->lastsx
&& data
->cx
!= px
) ||
638 window_copy_cursor_end_of_line(wme
);
641 if (data
->searchmark
!= NULL
&& !data
->timeout
)
642 window_copy_search_marks(wme
, NULL
, data
->searchregex
, 1);
643 window_copy_update_selection(wme
, 1, 0);
644 window_copy_redraw_screen(wme
);
648 window_copy_pagedown(struct window_mode_entry
*wme
, int half_page
,
651 struct window_copy_mode_data
*data
= wme
->data
;
652 struct screen
*s
= &data
->screen
;
653 u_int n
, ox
, oy
, px
, py
;
655 oy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
656 ox
= window_copy_find_length(wme
, oy
);
658 if (data
->cx
!= ox
) {
659 data
->lastcx
= data
->cx
;
662 data
->cx
= data
->lastcx
;
665 if (screen_size_y(s
) > 2) {
667 n
= screen_size_y(s
) / 2;
669 n
= screen_size_y(s
) - 2;
674 if (data
->cy
+ (n
- data
->oy
) >= screen_size_y(data
->backing
))
675 data
->cy
= screen_size_y(data
->backing
) - 1;
677 data
->cy
+= n
- data
->oy
;
681 if (data
->screen
.sel
== NULL
|| !data
->rectflag
) {
682 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
683 px
= window_copy_find_length(wme
, py
);
684 if ((data
->cx
>= data
->lastsx
&& data
->cx
!= px
) ||
686 window_copy_cursor_end_of_line(wme
);
689 if (scroll_exit
&& data
->oy
== 0)
691 if (data
->searchmark
!= NULL
&& !data
->timeout
)
692 window_copy_search_marks(wme
, NULL
, data
->searchregex
, 1);
693 window_copy_update_selection(wme
, 1, 0);
694 window_copy_redraw_screen(wme
);
699 window_copy_previous_paragraph(struct window_mode_entry
*wme
)
701 struct window_copy_mode_data
*data
= wme
->data
;
704 oy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
706 while (oy
> 0 && window_copy_find_length(wme
, oy
) == 0)
709 while (oy
> 0 && window_copy_find_length(wme
, oy
) > 0)
712 window_copy_scroll_to(wme
, 0, oy
, 0);
716 window_copy_next_paragraph(struct window_mode_entry
*wme
)
718 struct window_copy_mode_data
*data
= wme
->data
;
719 struct screen
*s
= &data
->screen
;
722 oy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
723 maxy
= screen_hsize(data
->backing
) + screen_size_y(s
) - 1;
725 while (oy
< maxy
&& window_copy_find_length(wme
, oy
) == 0)
728 while (oy
< maxy
&& window_copy_find_length(wme
, oy
) > 0)
731 ox
= window_copy_find_length(wme
, oy
);
732 window_copy_scroll_to(wme
, ox
, oy
, 0);
736 window_copy_get_word(struct window_pane
*wp
, u_int x
, u_int y
)
738 struct window_mode_entry
*wme
= TAILQ_FIRST(&wp
->modes
);
739 struct window_copy_mode_data
*data
= wme
->data
;
740 struct grid
*gd
= data
->screen
.grid
;
742 return (format_grid_word(gd
, x
, gd
->hsize
+ y
));
746 window_copy_get_line(struct window_pane
*wp
, u_int y
)
748 struct window_mode_entry
*wme
= TAILQ_FIRST(&wp
->modes
);
749 struct window_copy_mode_data
*data
= wme
->data
;
750 struct grid
*gd
= data
->screen
.grid
;
752 return (format_grid_line(gd
, gd
->hsize
+ y
));
756 window_copy_cursor_word_cb(struct format_tree
*ft
)
758 struct window_pane
*wp
= format_get_pane(ft
);
759 struct window_mode_entry
*wme
= TAILQ_FIRST(&wp
->modes
);
760 struct window_copy_mode_data
*data
= wme
->data
;
762 return (window_copy_get_word(wp
, data
->cx
, data
->cy
));
766 window_copy_cursor_line_cb(struct format_tree
*ft
)
768 struct window_pane
*wp
= format_get_pane(ft
);
769 struct window_mode_entry
*wme
= TAILQ_FIRST(&wp
->modes
);
770 struct window_copy_mode_data
*data
= wme
->data
;
772 return (window_copy_get_line(wp
, data
->cy
));
776 window_copy_search_match_cb(struct format_tree
*ft
)
778 struct window_pane
*wp
= format_get_pane(ft
);
779 struct window_mode_entry
*wme
= TAILQ_FIRST(&wp
->modes
);
780 struct window_copy_mode_data
*data
= wme
->data
;
782 return (window_copy_match_at_cursor(data
));
786 window_copy_formats(struct window_mode_entry
*wme
, struct format_tree
*ft
)
788 struct window_copy_mode_data
*data
= wme
->data
;
790 format_add(ft
, "scroll_position", "%d", data
->oy
);
791 format_add(ft
, "rectangle_toggle", "%d", data
->rectflag
);
793 format_add(ft
, "copy_cursor_x", "%d", data
->cx
);
794 format_add(ft
, "copy_cursor_y", "%d", data
->cy
);
796 format_add(ft
, "selection_present", "%d", data
->screen
.sel
!= NULL
);
797 if (data
->screen
.sel
!= NULL
) {
798 format_add(ft
, "selection_start_x", "%d", data
->selx
);
799 format_add(ft
, "selection_start_y", "%d", data
->sely
);
800 format_add(ft
, "selection_end_x", "%d", data
->endselx
);
801 format_add(ft
, "selection_end_y", "%d", data
->endsely
);
802 format_add(ft
, "selection_active", "%d",
803 data
->cursordrag
!= CURSORDRAG_NONE
);
805 format_add(ft
, "selection_active", "%d", 0);
807 format_add(ft
, "search_present", "%d", data
->searchmark
!= NULL
);
808 format_add_cb(ft
, "search_match", window_copy_search_match_cb
);
810 format_add_cb(ft
, "copy_cursor_word", window_copy_cursor_word_cb
);
811 format_add_cb(ft
, "copy_cursor_line", window_copy_cursor_line_cb
);
815 window_copy_size_changed(struct window_mode_entry
*wme
)
817 struct window_copy_mode_data
*data
= wme
->data
;
818 struct screen
*s
= &data
->screen
;
819 struct screen_write_ctx ctx
;
820 int search
= (data
->searchmark
!= NULL
);
822 window_copy_clear_selection(wme
);
823 window_copy_clear_marks(wme
);
825 screen_write_start(&ctx
, s
);
826 window_copy_write_lines(wme
, &ctx
, 0, screen_size_y(s
));
827 screen_write_stop(&ctx
);
829 if (search
&& !data
->timeout
)
830 window_copy_search_marks(wme
, NULL
, data
->searchregex
, 0);
831 data
->searchx
= data
->cx
;
832 data
->searchy
= data
->cy
;
833 data
->searcho
= data
->oy
;
837 window_copy_resize(struct window_mode_entry
*wme
, u_int sx
, u_int sy
)
839 struct window_copy_mode_data
*data
= wme
->data
;
840 struct screen
*s
= &data
->screen
;
841 struct grid
*gd
= data
->backing
->grid
;
842 u_int cx
, cy
, wx
, wy
;
845 screen_resize(s
, sx
, sy
, 0);
847 cy
= gd
->hsize
+ data
->cy
- data
->oy
;
848 reflow
= (gd
->sx
!= sx
);
850 grid_wrap_position(gd
, cx
, cy
, &wx
, &wy
);
851 screen_resize_cursor(data
->backing
, sx
, sy
, 1, 0, 0);
853 grid_unwrap_position(gd
, &cx
, &cy
, wx
, wy
);
856 if (cy
< gd
->hsize
) {
858 data
->oy
= gd
->hsize
- cy
;
860 data
->cy
= cy
- gd
->hsize
;
864 window_copy_size_changed(wme
);
865 window_copy_redraw_screen(wme
);
869 window_copy_key_table(struct window_mode_entry
*wme
)
871 struct window_pane
*wp
= wme
->wp
;
873 if (options_get_number(wp
->window
->options
, "mode-keys") == MODEKEY_VI
)
874 return ("copy-mode-vi");
875 return ("copy-mode");
879 window_copy_expand_search_string(struct window_copy_cmd_state
*cs
)
881 struct window_mode_entry
*wme
= cs
->wme
;
882 struct window_copy_mode_data
*data
= wme
->data
;
883 const char *ss
= args_string(cs
->args
, 1);
886 if (ss
== NULL
|| *ss
== '\0')
889 if (args_has(cs
->args
, 'F')) {
890 expanded
= format_single(NULL
, ss
, NULL
, NULL
, NULL
, wme
->wp
);
891 if (*expanded
== '\0') {
895 free(data
->searchstr
);
896 data
->searchstr
= expanded
;
898 free(data
->searchstr
);
899 data
->searchstr
= xstrdup(ss
);
904 static enum window_copy_cmd_action
905 window_copy_cmd_append_selection(struct window_copy_cmd_state
*cs
)
907 struct window_mode_entry
*wme
= cs
->wme
;
908 struct session
*s
= cs
->s
;
911 window_copy_append_selection(wme
);
912 window_copy_clear_selection(wme
);
913 return (WINDOW_COPY_CMD_REDRAW
);
916 static enum window_copy_cmd_action
917 window_copy_cmd_append_selection_and_cancel(struct window_copy_cmd_state
*cs
)
919 struct window_mode_entry
*wme
= cs
->wme
;
920 struct session
*s
= cs
->s
;
923 window_copy_append_selection(wme
);
924 window_copy_clear_selection(wme
);
925 return (WINDOW_COPY_CMD_CANCEL
);
928 static enum window_copy_cmd_action
929 window_copy_cmd_back_to_indentation(struct window_copy_cmd_state
*cs
)
931 struct window_mode_entry
*wme
= cs
->wme
;
933 window_copy_cursor_back_to_indentation(wme
);
934 return (WINDOW_COPY_CMD_NOTHING
);
937 static enum window_copy_cmd_action
938 window_copy_cmd_begin_selection(struct window_copy_cmd_state
*cs
)
940 struct window_mode_entry
*wme
= cs
->wme
;
941 struct client
*c
= cs
->c
;
942 struct mouse_event
*m
= cs
->m
;
943 struct window_copy_mode_data
*data
= wme
->data
;
946 window_copy_start_drag(c
, m
);
947 return (WINDOW_COPY_CMD_NOTHING
);
950 data
->lineflag
= LINE_SEL_NONE
;
951 data
->selflag
= SEL_CHAR
;
952 window_copy_start_selection(wme
);
953 return (WINDOW_COPY_CMD_REDRAW
);
956 static enum window_copy_cmd_action
957 window_copy_cmd_stop_selection(struct window_copy_cmd_state
*cs
)
959 struct window_mode_entry
*wme
= cs
->wme
;
960 struct window_copy_mode_data
*data
= wme
->data
;
962 data
->cursordrag
= CURSORDRAG_NONE
;
963 data
->lineflag
= LINE_SEL_NONE
;
964 data
->selflag
= SEL_CHAR
;
965 return (WINDOW_COPY_CMD_NOTHING
);
968 static enum window_copy_cmd_action
969 window_copy_cmd_bottom_line(struct window_copy_cmd_state
*cs
)
971 struct window_mode_entry
*wme
= cs
->wme
;
972 struct window_copy_mode_data
*data
= wme
->data
;
975 data
->cy
= screen_size_y(&data
->screen
) - 1;
977 window_copy_update_selection(wme
, 1, 0);
978 return (WINDOW_COPY_CMD_REDRAW
);
981 static enum window_copy_cmd_action
982 window_copy_cmd_cancel(__unused
struct window_copy_cmd_state
*cs
)
984 return (WINDOW_COPY_CMD_CANCEL
);
987 static enum window_copy_cmd_action
988 window_copy_cmd_clear_selection(struct window_copy_cmd_state
*cs
)
990 struct window_mode_entry
*wme
= cs
->wme
;
992 window_copy_clear_selection(wme
);
993 return (WINDOW_COPY_CMD_REDRAW
);
996 static enum window_copy_cmd_action
997 window_copy_do_copy_end_of_line(struct window_copy_cmd_state
*cs
, int pipe
,
1000 struct window_mode_entry
*wme
= cs
->wme
;
1001 struct client
*c
= cs
->c
;
1002 struct session
*s
= cs
->s
;
1003 struct winlink
*wl
= cs
->wl
;
1004 struct window_pane
*wp
= wme
->wp
;
1005 u_int count
= args_count(cs
->args
);
1006 u_int np
= wme
->prefix
, ocx
, ocy
, ooy
;
1007 struct window_copy_mode_data
*data
= wme
->data
;
1008 char *prefix
= NULL
, *command
= NULL
;
1009 const char *arg1
= args_string(cs
->args
, 1);
1010 const char *arg2
= args_string(cs
->args
, 2);
1014 prefix
= format_single(NULL
, arg2
, c
, s
, wl
, wp
);
1015 if (s
!= NULL
&& count
> 1 && *arg1
!= '\0')
1016 command
= format_single(NULL
, arg1
, c
, s
, wl
, wp
);
1019 prefix
= format_single(NULL
, arg1
, c
, s
, wl
, wp
);
1026 window_copy_start_selection(wme
);
1027 for (; np
> 1; np
--)
1028 window_copy_cursor_down(wme
, 0);
1029 window_copy_cursor_end_of_line(wme
);
1033 window_copy_copy_pipe(wme
, s
, prefix
, command
);
1035 window_copy_copy_selection(wme
, prefix
);
1040 return (WINDOW_COPY_CMD_CANCEL
);
1043 window_copy_clear_selection(wme
);
1051 return (WINDOW_COPY_CMD_REDRAW
);
1054 static enum window_copy_cmd_action
1055 window_copy_cmd_copy_end_of_line(struct window_copy_cmd_state
*cs
)
1057 return (window_copy_do_copy_end_of_line(cs
, 0, 0));
1060 static enum window_copy_cmd_action
1061 window_copy_cmd_copy_end_of_line_and_cancel(struct window_copy_cmd_state
*cs
)
1063 return (window_copy_do_copy_end_of_line(cs
, 0, 1));
1066 static enum window_copy_cmd_action
1067 window_copy_cmd_copy_pipe_end_of_line(struct window_copy_cmd_state
*cs
)
1069 return (window_copy_do_copy_end_of_line(cs
, 1, 0));
1072 static enum window_copy_cmd_action
1073 window_copy_cmd_copy_pipe_end_of_line_and_cancel(
1074 struct window_copy_cmd_state
*cs
)
1076 return (window_copy_do_copy_end_of_line(cs
, 1, 1));
1079 static enum window_copy_cmd_action
1080 window_copy_do_copy_line(struct window_copy_cmd_state
*cs
, int pipe
, int cancel
)
1082 struct window_mode_entry
*wme
= cs
->wme
;
1083 struct client
*c
= cs
->c
;
1084 struct session
*s
= cs
->s
;
1085 struct winlink
*wl
= cs
->wl
;
1086 struct window_pane
*wp
= wme
->wp
;
1087 struct window_copy_mode_data
*data
= wme
->data
;
1088 u_int count
= args_count(cs
->args
);
1089 u_int np
= wme
->prefix
, ocx
, ocy
, ooy
;
1090 char *prefix
= NULL
, *command
= NULL
;
1091 const char *arg1
= args_string(cs
->args
, 1);
1092 const char *arg2
= args_string(cs
->args
, 2);
1096 prefix
= format_single(NULL
, arg2
, c
, s
, wl
, wp
);
1097 if (s
!= NULL
&& count
> 1 && *arg1
!= '\0')
1098 command
= format_single(NULL
, arg1
, c
, s
, wl
, wp
);
1101 prefix
= format_single(NULL
, arg1
, c
, s
, wl
, wp
);
1108 data
->selflag
= SEL_CHAR
;
1109 window_copy_cursor_start_of_line(wme
);
1110 window_copy_start_selection(wme
);
1111 for (; np
> 1; np
--)
1112 window_copy_cursor_down(wme
, 0);
1113 window_copy_cursor_end_of_line(wme
);
1117 window_copy_copy_pipe(wme
, s
, prefix
, command
);
1119 window_copy_copy_selection(wme
, prefix
);
1124 return (WINDOW_COPY_CMD_CANCEL
);
1127 window_copy_clear_selection(wme
);
1135 return (WINDOW_COPY_CMD_REDRAW
);
1138 static enum window_copy_cmd_action
1139 window_copy_cmd_copy_line(struct window_copy_cmd_state
*cs
)
1141 return (window_copy_do_copy_line(cs
, 0, 0));
1144 static enum window_copy_cmd_action
1145 window_copy_cmd_copy_line_and_cancel(struct window_copy_cmd_state
*cs
)
1147 return (window_copy_do_copy_line(cs
, 0, 1));
1150 static enum window_copy_cmd_action
1151 window_copy_cmd_copy_pipe_line(struct window_copy_cmd_state
*cs
)
1153 return (window_copy_do_copy_line(cs
, 1, 0));
1156 static enum window_copy_cmd_action
1157 window_copy_cmd_copy_pipe_line_and_cancel(struct window_copy_cmd_state
*cs
)
1159 return (window_copy_do_copy_line(cs
, 1, 1));
1162 static enum window_copy_cmd_action
1163 window_copy_cmd_copy_selection_no_clear(struct window_copy_cmd_state
*cs
)
1165 struct window_mode_entry
*wme
= cs
->wme
;
1166 struct client
*c
= cs
->c
;
1167 struct session
*s
= cs
->s
;
1168 struct winlink
*wl
= cs
->wl
;
1169 struct window_pane
*wp
= wme
->wp
;
1170 char *prefix
= NULL
;
1171 const char *arg1
= args_string(cs
->args
, 1);
1174 prefix
= format_single(NULL
, arg1
, c
, s
, wl
, wp
);
1177 window_copy_copy_selection(wme
, prefix
);
1180 return (WINDOW_COPY_CMD_NOTHING
);
1183 static enum window_copy_cmd_action
1184 window_copy_cmd_copy_selection(struct window_copy_cmd_state
*cs
)
1186 struct window_mode_entry
*wme
= cs
->wme
;
1188 window_copy_cmd_copy_selection_no_clear(cs
);
1189 window_copy_clear_selection(wme
);
1190 return (WINDOW_COPY_CMD_REDRAW
);
1193 static enum window_copy_cmd_action
1194 window_copy_cmd_copy_selection_and_cancel(struct window_copy_cmd_state
*cs
)
1196 struct window_mode_entry
*wme
= cs
->wme
;
1198 window_copy_cmd_copy_selection_no_clear(cs
);
1199 window_copy_clear_selection(wme
);
1200 return (WINDOW_COPY_CMD_CANCEL
);
1203 static enum window_copy_cmd_action
1204 window_copy_cmd_cursor_down(struct window_copy_cmd_state
*cs
)
1206 struct window_mode_entry
*wme
= cs
->wme
;
1207 u_int np
= wme
->prefix
;
1209 for (; np
!= 0; np
--)
1210 window_copy_cursor_down(wme
, 0);
1211 return (WINDOW_COPY_CMD_NOTHING
);
1214 static enum window_copy_cmd_action
1215 window_copy_cmd_cursor_down_and_cancel(struct window_copy_cmd_state
*cs
)
1217 struct window_mode_entry
*wme
= cs
->wme
;
1218 struct window_copy_mode_data
*data
= wme
->data
;
1219 u_int np
= wme
->prefix
, cy
;
1222 for (; np
!= 0; np
--)
1223 window_copy_cursor_down(wme
, 0);
1224 if (cy
== data
->cy
&& data
->oy
== 0)
1225 return (WINDOW_COPY_CMD_CANCEL
);
1226 return (WINDOW_COPY_CMD_NOTHING
);
1229 static enum window_copy_cmd_action
1230 window_copy_cmd_cursor_left(struct window_copy_cmd_state
*cs
)
1232 struct window_mode_entry
*wme
= cs
->wme
;
1233 u_int np
= wme
->prefix
;
1235 for (; np
!= 0; np
--)
1236 window_copy_cursor_left(wme
);
1237 return (WINDOW_COPY_CMD_NOTHING
);
1240 static enum window_copy_cmd_action
1241 window_copy_cmd_cursor_right(struct window_copy_cmd_state
*cs
)
1243 struct window_mode_entry
*wme
= cs
->wme
;
1244 struct window_copy_mode_data
*data
= wme
->data
;
1245 u_int np
= wme
->prefix
;
1247 for (; np
!= 0; np
--) {
1248 window_copy_cursor_right(wme
, data
->screen
.sel
!= NULL
&&
1251 return (WINDOW_COPY_CMD_NOTHING
);
1254 /* Scroll line containing the cursor to the given position. */
1255 static enum window_copy_cmd_action
1256 window_copy_cmd_scroll_to(struct window_copy_cmd_state
*cs
, u_int to
)
1258 struct window_mode_entry
*wme
= cs
->wme
;
1259 struct window_copy_mode_data
*data
= wme
->data
;
1261 int scroll_up
; /* >0 up, <0 down */
1263 scroll_up
= data
->cy
- to
;
1264 delta
= abs(scroll_up
);
1265 oy
= screen_hsize(data
->backing
) - data
->oy
;
1268 * oy is the maximum scroll down amount, while data->oy is the maximum
1271 if (scroll_up
> 0 && data
->oy
>= delta
) {
1272 window_copy_scroll_up(wme
, delta
);
1274 } else if (scroll_up
< 0 && oy
>= delta
) {
1275 window_copy_scroll_down(wme
, delta
);
1279 window_copy_update_selection(wme
, 0, 0);
1280 return (WINDOW_COPY_CMD_REDRAW
);
1283 /* Scroll line containing the cursor to the bottom. */
1284 static enum window_copy_cmd_action
1285 window_copy_cmd_scroll_bottom(struct window_copy_cmd_state
*cs
)
1287 struct window_copy_mode_data
*data
= cs
->wme
->data
;
1290 bottom
= screen_size_y(&data
->screen
) - 1;
1291 return (window_copy_cmd_scroll_to(cs
, bottom
));
1294 /* Scroll line containing the cursor to the middle. */
1295 static enum window_copy_cmd_action
1296 window_copy_cmd_scroll_middle(struct window_copy_cmd_state
*cs
)
1298 struct window_copy_mode_data
*data
= cs
->wme
->data
;
1301 mid_value
= (screen_size_y(&data
->screen
) - 1) / 2;
1302 return (window_copy_cmd_scroll_to(cs
, mid_value
));
1305 /* Scroll line containing the cursor to the top. */
1306 static enum window_copy_cmd_action
1307 window_copy_cmd_scroll_top(struct window_copy_cmd_state
*cs
)
1309 return (window_copy_cmd_scroll_to(cs
, 0));
1312 static enum window_copy_cmd_action
1313 window_copy_cmd_cursor_up(struct window_copy_cmd_state
*cs
)
1315 struct window_mode_entry
*wme
= cs
->wme
;
1316 u_int np
= wme
->prefix
;
1318 for (; np
!= 0; np
--)
1319 window_copy_cursor_up(wme
, 0);
1320 return (WINDOW_COPY_CMD_NOTHING
);
1323 static enum window_copy_cmd_action
1324 window_copy_cmd_end_of_line(struct window_copy_cmd_state
*cs
)
1326 struct window_mode_entry
*wme
= cs
->wme
;
1328 window_copy_cursor_end_of_line(wme
);
1329 return (WINDOW_COPY_CMD_NOTHING
);
1332 static enum window_copy_cmd_action
1333 window_copy_cmd_halfpage_down(struct window_copy_cmd_state
*cs
)
1335 struct window_mode_entry
*wme
= cs
->wme
;
1336 struct window_copy_mode_data
*data
= wme
->data
;
1337 u_int np
= wme
->prefix
;
1339 for (; np
!= 0; np
--) {
1340 if (window_copy_pagedown(wme
, 1, data
->scroll_exit
))
1341 return (WINDOW_COPY_CMD_CANCEL
);
1343 return (WINDOW_COPY_CMD_NOTHING
);
1346 static enum window_copy_cmd_action
1347 window_copy_cmd_halfpage_down_and_cancel(struct window_copy_cmd_state
*cs
)
1350 struct window_mode_entry
*wme
= cs
->wme
;
1351 u_int np
= wme
->prefix
;
1353 for (; np
!= 0; np
--) {
1354 if (window_copy_pagedown(wme
, 1, 1))
1355 return (WINDOW_COPY_CMD_CANCEL
);
1357 return (WINDOW_COPY_CMD_NOTHING
);
1360 static enum window_copy_cmd_action
1361 window_copy_cmd_halfpage_up(struct window_copy_cmd_state
*cs
)
1363 struct window_mode_entry
*wme
= cs
->wme
;
1364 u_int np
= wme
->prefix
;
1366 for (; np
!= 0; np
--)
1367 window_copy_pageup1(wme
, 1);
1368 return (WINDOW_COPY_CMD_NOTHING
);
1371 static enum window_copy_cmd_action
1372 window_copy_cmd_toggle_position(struct window_copy_cmd_state
*cs
)
1374 struct window_mode_entry
*wme
= cs
->wme
;
1375 struct window_copy_mode_data
*data
= wme
->data
;
1377 data
->hide_position
= !data
->hide_position
;
1378 return (WINDOW_COPY_CMD_REDRAW
);
1381 static enum window_copy_cmd_action
1382 window_copy_cmd_history_bottom(struct window_copy_cmd_state
*cs
)
1384 struct window_mode_entry
*wme
= cs
->wme
;
1385 struct window_copy_mode_data
*data
= wme
->data
;
1386 struct screen
*s
= data
->backing
;
1389 oy
= screen_hsize(s
) + data
->cy
- data
->oy
;
1390 if (data
->lineflag
== LINE_SEL_RIGHT_LEFT
&& oy
== data
->endsely
)
1391 window_copy_other_end(wme
);
1393 data
->cy
= screen_size_y(&data
->screen
) - 1;
1394 data
->cx
= window_copy_find_length(wme
, screen_hsize(s
) + data
->cy
);
1397 if (data
->searchmark
!= NULL
&& !data
->timeout
)
1398 window_copy_search_marks(wme
, NULL
, data
->searchregex
, 1);
1399 window_copy_update_selection(wme
, 1, 0);
1400 return (WINDOW_COPY_CMD_REDRAW
);
1403 static enum window_copy_cmd_action
1404 window_copy_cmd_history_top(struct window_copy_cmd_state
*cs
)
1406 struct window_mode_entry
*wme
= cs
->wme
;
1407 struct window_copy_mode_data
*data
= wme
->data
;
1410 oy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1411 if (data
->lineflag
== LINE_SEL_LEFT_RIGHT
&& oy
== data
->sely
)
1412 window_copy_other_end(wme
);
1416 data
->oy
= screen_hsize(data
->backing
);
1418 if (data
->searchmark
!= NULL
&& !data
->timeout
)
1419 window_copy_search_marks(wme
, NULL
, data
->searchregex
, 1);
1420 window_copy_update_selection(wme
, 1, 0);
1421 return (WINDOW_COPY_CMD_REDRAW
);
1424 static enum window_copy_cmd_action
1425 window_copy_cmd_jump_again(struct window_copy_cmd_state
*cs
)
1427 struct window_mode_entry
*wme
= cs
->wme
;
1428 struct window_copy_mode_data
*data
= wme
->data
;
1429 u_int np
= wme
->prefix
;
1431 switch (data
->jumptype
) {
1432 case WINDOW_COPY_JUMPFORWARD
:
1433 for (; np
!= 0; np
--)
1434 window_copy_cursor_jump(wme
);
1436 case WINDOW_COPY_JUMPBACKWARD
:
1437 for (; np
!= 0; np
--)
1438 window_copy_cursor_jump_back(wme
);
1440 case WINDOW_COPY_JUMPTOFORWARD
:
1441 for (; np
!= 0; np
--)
1442 window_copy_cursor_jump_to(wme
);
1444 case WINDOW_COPY_JUMPTOBACKWARD
:
1445 for (; np
!= 0; np
--)
1446 window_copy_cursor_jump_to_back(wme
);
1449 return (WINDOW_COPY_CMD_NOTHING
);
1452 static enum window_copy_cmd_action
1453 window_copy_cmd_jump_reverse(struct window_copy_cmd_state
*cs
)
1455 struct window_mode_entry
*wme
= cs
->wme
;
1456 struct window_copy_mode_data
*data
= wme
->data
;
1457 u_int np
= wme
->prefix
;
1459 switch (data
->jumptype
) {
1460 case WINDOW_COPY_JUMPFORWARD
:
1461 for (; np
!= 0; np
--)
1462 window_copy_cursor_jump_back(wme
);
1464 case WINDOW_COPY_JUMPBACKWARD
:
1465 for (; np
!= 0; np
--)
1466 window_copy_cursor_jump(wme
);
1468 case WINDOW_COPY_JUMPTOFORWARD
:
1469 for (; np
!= 0; np
--)
1470 window_copy_cursor_jump_to_back(wme
);
1472 case WINDOW_COPY_JUMPTOBACKWARD
:
1473 for (; np
!= 0; np
--)
1474 window_copy_cursor_jump_to(wme
);
1477 return (WINDOW_COPY_CMD_NOTHING
);
1480 static enum window_copy_cmd_action
1481 window_copy_cmd_middle_line(struct window_copy_cmd_state
*cs
)
1483 struct window_mode_entry
*wme
= cs
->wme
;
1484 struct window_copy_mode_data
*data
= wme
->data
;
1487 data
->cy
= (screen_size_y(&data
->screen
) - 1) / 2;
1489 window_copy_update_selection(wme
, 1, 0);
1490 return (WINDOW_COPY_CMD_REDRAW
);
1493 static enum window_copy_cmd_action
1494 window_copy_cmd_previous_matching_bracket(struct window_copy_cmd_state
*cs
)
1496 struct window_mode_entry
*wme
= cs
->wme
;
1497 u_int np
= wme
->prefix
;
1498 struct window_copy_mode_data
*data
= wme
->data
;
1499 struct screen
*s
= data
->backing
;
1500 char open
[] = "{[(", close
[] = "}])";
1501 char tried
, found
, start
, *cp
;
1502 u_int px
, py
, xx
, n
;
1503 struct grid_cell gc
;
1506 for (; np
!= 0; np
--) {
1507 /* Get cursor position and line length. */
1509 py
= screen_hsize(s
) + data
->cy
- data
->oy
;
1510 xx
= window_copy_find_length(wme
, py
);
1515 * Get the current character. If not on a bracket, try the
1516 * previous. If still not, then behave like previous-word.
1520 grid_get_cell(s
->grid
, px
, py
, &gc
);
1521 if (gc
.data
.size
!= 1 || (gc
.flags
& GRID_FLAG_PADDING
))
1524 found
= *gc
.data
.data
;
1525 cp
= strchr(close
, found
);
1528 if (data
->modekeys
== MODEKEY_EMACS
) {
1529 if (!tried
&& px
> 0) {
1534 window_copy_cursor_previous_word(wme
, close
, 1);
1538 start
= open
[cp
- close
];
1540 /* Walk backward until the matching bracket is reached. */
1551 xx
= window_copy_find_length(wme
, py
);
1552 } while (xx
== 0 && py
> 0);
1553 if (xx
== 0 && py
== 0) {
1561 grid_get_cell(s
->grid
, px
, py
, &gc
);
1562 if (gc
.data
.size
== 1 &&
1563 (~gc
.flags
& GRID_FLAG_PADDING
)) {
1564 if (*gc
.data
.data
== found
)
1566 else if (*gc
.data
.data
== start
)
1571 /* Move the cursor to the found location if any. */
1573 window_copy_scroll_to(wme
, px
, py
, 0);
1576 return (WINDOW_COPY_CMD_NOTHING
);
1579 static enum window_copy_cmd_action
1580 window_copy_cmd_next_matching_bracket(struct window_copy_cmd_state
*cs
)
1582 struct window_mode_entry
*wme
= cs
->wme
;
1583 u_int np
= wme
->prefix
;
1584 struct window_copy_mode_data
*data
= wme
->data
;
1585 struct screen
*s
= data
->backing
;
1586 char open
[] = "{[(", close
[] = "}])";
1587 char tried
, found
, end
, *cp
;
1588 u_int px
, py
, xx
, yy
, sx
, sy
, n
;
1589 struct grid_cell gc
;
1591 struct grid_line
*gl
;
1593 for (; np
!= 0; np
--) {
1594 /* Get cursor position and line length. */
1596 py
= screen_hsize(s
) + data
->cy
- data
->oy
;
1597 xx
= window_copy_find_length(wme
, py
);
1598 yy
= screen_hsize(s
) + screen_size_y(s
) - 1;
1603 * Get the current character. If not on a bracket, try the
1604 * next. If still not, then behave like next-word.
1608 grid_get_cell(s
->grid
, px
, py
, &gc
);
1609 if (gc
.data
.size
!= 1 || (gc
.flags
& GRID_FLAG_PADDING
))
1612 found
= *gc
.data
.data
;
1615 * In vi mode, attempt to move to previous bracket if a
1616 * closing bracket is found first. If this fails,
1617 * return to the original cursor position.
1619 cp
= strchr(close
, found
);
1620 if (cp
!= NULL
&& data
->modekeys
== MODEKEY_VI
) {
1622 sy
= screen_hsize(s
) + data
->cy
- data
->oy
;
1624 window_copy_scroll_to(wme
, px
, py
, 0);
1625 window_copy_cmd_previous_matching_bracket(cs
);
1628 py
= screen_hsize(s
) + data
->cy
- data
->oy
;
1629 grid_get_cell(s
->grid
, px
, py
, &gc
);
1630 if (gc
.data
.size
== 1 &&
1631 (~gc
.flags
& GRID_FLAG_PADDING
) &&
1632 strchr(close
, *gc
.data
.data
) != NULL
)
1633 window_copy_scroll_to(wme
, sx
, sy
, 0);
1637 cp
= strchr(open
, found
);
1640 if (data
->modekeys
== MODEKEY_EMACS
) {
1641 if (!tried
&& px
<= xx
) {
1646 window_copy_cursor_next_word_end(wme
, open
, 0);
1649 /* For vi, continue searching for bracket until EOL. */
1653 gl
= grid_get_line(s
->grid
, py
);
1654 if (~gl
->flags
& GRID_LINE_WRAPPED
)
1656 if (gl
->cellsize
> s
->grid
->sx
)
1660 xx
= window_copy_find_length(wme
, py
);
1665 end
= close
[cp
- open
];
1667 /* Walk forward until the matching bracket is reached. */
1678 xx
= window_copy_find_length(wme
, py
);
1682 grid_get_cell(s
->grid
, px
, py
, &gc
);
1683 if (gc
.data
.size
== 1 &&
1684 (~gc
.flags
& GRID_FLAG_PADDING
)) {
1685 if (*gc
.data
.data
== found
)
1687 else if (*gc
.data
.data
== end
)
1692 /* Move the cursor to the found location if any. */
1694 window_copy_scroll_to(wme
, px
, py
, 0);
1697 return (WINDOW_COPY_CMD_NOTHING
);
1700 static enum window_copy_cmd_action
1701 window_copy_cmd_next_paragraph(struct window_copy_cmd_state
*cs
)
1703 struct window_mode_entry
*wme
= cs
->wme
;
1704 u_int np
= wme
->prefix
;
1706 for (; np
!= 0; np
--)
1707 window_copy_next_paragraph(wme
);
1708 return (WINDOW_COPY_CMD_NOTHING
);
1711 static enum window_copy_cmd_action
1712 window_copy_cmd_next_space(struct window_copy_cmd_state
*cs
)
1714 struct window_mode_entry
*wme
= cs
->wme
;
1715 u_int np
= wme
->prefix
;
1717 for (; np
!= 0; np
--)
1718 window_copy_cursor_next_word(wme
, "");
1719 return (WINDOW_COPY_CMD_NOTHING
);
1722 static enum window_copy_cmd_action
1723 window_copy_cmd_next_space_end(struct window_copy_cmd_state
*cs
)
1725 struct window_mode_entry
*wme
= cs
->wme
;
1726 u_int np
= wme
->prefix
;
1728 for (; np
!= 0; np
--)
1729 window_copy_cursor_next_word_end(wme
, "", 0);
1730 return (WINDOW_COPY_CMD_NOTHING
);
1733 static enum window_copy_cmd_action
1734 window_copy_cmd_next_word(struct window_copy_cmd_state
*cs
)
1736 struct window_mode_entry
*wme
= cs
->wme
;
1737 u_int np
= wme
->prefix
;
1738 const char *separators
;
1740 separators
= options_get_string(cs
->s
->options
, "word-separators");
1742 for (; np
!= 0; np
--)
1743 window_copy_cursor_next_word(wme
, separators
);
1744 return (WINDOW_COPY_CMD_NOTHING
);
1747 static enum window_copy_cmd_action
1748 window_copy_cmd_next_word_end(struct window_copy_cmd_state
*cs
)
1750 struct window_mode_entry
*wme
= cs
->wme
;
1751 u_int np
= wme
->prefix
;
1752 const char *separators
;
1754 separators
= options_get_string(cs
->s
->options
, "word-separators");
1756 for (; np
!= 0; np
--)
1757 window_copy_cursor_next_word_end(wme
, separators
, 0);
1758 return (WINDOW_COPY_CMD_NOTHING
);
1761 static enum window_copy_cmd_action
1762 window_copy_cmd_other_end(struct window_copy_cmd_state
*cs
)
1764 struct window_mode_entry
*wme
= cs
->wme
;
1765 u_int np
= wme
->prefix
;
1766 struct window_copy_mode_data
*data
= wme
->data
;
1768 data
->selflag
= SEL_CHAR
;
1770 window_copy_other_end(wme
);
1771 return (WINDOW_COPY_CMD_NOTHING
);
1774 static enum window_copy_cmd_action
1775 window_copy_cmd_page_down(struct window_copy_cmd_state
*cs
)
1777 struct window_mode_entry
*wme
= cs
->wme
;
1778 struct window_copy_mode_data
*data
= wme
->data
;
1779 u_int np
= wme
->prefix
;
1781 for (; np
!= 0; np
--) {
1782 if (window_copy_pagedown(wme
, 0, data
->scroll_exit
))
1783 return (WINDOW_COPY_CMD_CANCEL
);
1785 return (WINDOW_COPY_CMD_NOTHING
);
1788 static enum window_copy_cmd_action
1789 window_copy_cmd_page_down_and_cancel(struct window_copy_cmd_state
*cs
)
1791 struct window_mode_entry
*wme
= cs
->wme
;
1792 u_int np
= wme
->prefix
;
1794 for (; np
!= 0; np
--) {
1795 if (window_copy_pagedown(wme
, 0, 1))
1796 return (WINDOW_COPY_CMD_CANCEL
);
1798 return (WINDOW_COPY_CMD_NOTHING
);
1801 static enum window_copy_cmd_action
1802 window_copy_cmd_page_up(struct window_copy_cmd_state
*cs
)
1804 struct window_mode_entry
*wme
= cs
->wme
;
1805 u_int np
= wme
->prefix
;
1807 for (; np
!= 0; np
--)
1808 window_copy_pageup1(wme
, 0);
1809 return (WINDOW_COPY_CMD_NOTHING
);
1812 static enum window_copy_cmd_action
1813 window_copy_cmd_previous_paragraph(struct window_copy_cmd_state
*cs
)
1815 struct window_mode_entry
*wme
= cs
->wme
;
1816 u_int np
= wme
->prefix
;
1818 for (; np
!= 0; np
--)
1819 window_copy_previous_paragraph(wme
);
1820 return (WINDOW_COPY_CMD_NOTHING
);
1823 static enum window_copy_cmd_action
1824 window_copy_cmd_previous_space(struct window_copy_cmd_state
*cs
)
1826 struct window_mode_entry
*wme
= cs
->wme
;
1827 u_int np
= wme
->prefix
;
1829 for (; np
!= 0; np
--)
1830 window_copy_cursor_previous_word(wme
, "", 1);
1831 return (WINDOW_COPY_CMD_NOTHING
);
1834 static enum window_copy_cmd_action
1835 window_copy_cmd_previous_word(struct window_copy_cmd_state
*cs
)
1837 struct window_mode_entry
*wme
= cs
->wme
;
1838 u_int np
= wme
->prefix
;
1839 const char *separators
;
1841 separators
= options_get_string(cs
->s
->options
, "word-separators");
1843 for (; np
!= 0; np
--)
1844 window_copy_cursor_previous_word(wme
, separators
, 1);
1845 return (WINDOW_COPY_CMD_NOTHING
);
1848 static enum window_copy_cmd_action
1849 window_copy_cmd_rectangle_on(struct window_copy_cmd_state
*cs
)
1851 struct window_mode_entry
*wme
= cs
->wme
;
1852 struct window_copy_mode_data
*data
= wme
->data
;
1854 data
->lineflag
= LINE_SEL_NONE
;
1855 window_copy_rectangle_set(wme
, 1);
1857 return (WINDOW_COPY_CMD_NOTHING
);
1860 static enum window_copy_cmd_action
1861 window_copy_cmd_rectangle_off(struct window_copy_cmd_state
*cs
)
1863 struct window_mode_entry
*wme
= cs
->wme
;
1864 struct window_copy_mode_data
*data
= wme
->data
;
1866 data
->lineflag
= LINE_SEL_NONE
;
1867 window_copy_rectangle_set(wme
, 0);
1869 return (WINDOW_COPY_CMD_NOTHING
);
1872 static enum window_copy_cmd_action
1873 window_copy_cmd_rectangle_toggle(struct window_copy_cmd_state
*cs
)
1875 struct window_mode_entry
*wme
= cs
->wme
;
1876 struct window_copy_mode_data
*data
= wme
->data
;
1878 data
->lineflag
= LINE_SEL_NONE
;
1879 window_copy_rectangle_set(wme
, !data
->rectflag
);
1881 return (WINDOW_COPY_CMD_NOTHING
);
1884 static enum window_copy_cmd_action
1885 window_copy_cmd_scroll_down(struct window_copy_cmd_state
*cs
)
1887 struct window_mode_entry
*wme
= cs
->wme
;
1888 struct window_copy_mode_data
*data
= wme
->data
;
1889 u_int np
= wme
->prefix
;
1891 for (; np
!= 0; np
--)
1892 window_copy_cursor_down(wme
, 1);
1893 if (data
->scroll_exit
&& data
->oy
== 0)
1894 return (WINDOW_COPY_CMD_CANCEL
);
1895 return (WINDOW_COPY_CMD_NOTHING
);
1898 static enum window_copy_cmd_action
1899 window_copy_cmd_scroll_down_and_cancel(struct window_copy_cmd_state
*cs
)
1901 struct window_mode_entry
*wme
= cs
->wme
;
1902 struct window_copy_mode_data
*data
= wme
->data
;
1903 u_int np
= wme
->prefix
;
1905 for (; np
!= 0; np
--)
1906 window_copy_cursor_down(wme
, 1);
1908 return (WINDOW_COPY_CMD_CANCEL
);
1909 return (WINDOW_COPY_CMD_NOTHING
);
1912 static enum window_copy_cmd_action
1913 window_copy_cmd_scroll_up(struct window_copy_cmd_state
*cs
)
1915 struct window_mode_entry
*wme
= cs
->wme
;
1916 u_int np
= wme
->prefix
;
1918 for (; np
!= 0; np
--)
1919 window_copy_cursor_up(wme
, 1);
1920 return (WINDOW_COPY_CMD_NOTHING
);
1923 static enum window_copy_cmd_action
1924 window_copy_cmd_search_again(struct window_copy_cmd_state
*cs
)
1926 struct window_mode_entry
*wme
= cs
->wme
;
1927 struct window_copy_mode_data
*data
= wme
->data
;
1928 u_int np
= wme
->prefix
;
1930 if (data
->searchtype
== WINDOW_COPY_SEARCHUP
) {
1931 for (; np
!= 0; np
--)
1932 window_copy_search_up(wme
, data
->searchregex
);
1933 } else if (data
->searchtype
== WINDOW_COPY_SEARCHDOWN
) {
1934 for (; np
!= 0; np
--)
1935 window_copy_search_down(wme
, data
->searchregex
);
1937 return (WINDOW_COPY_CMD_NOTHING
);
1940 static enum window_copy_cmd_action
1941 window_copy_cmd_search_reverse(struct window_copy_cmd_state
*cs
)
1943 struct window_mode_entry
*wme
= cs
->wme
;
1944 struct window_copy_mode_data
*data
= wme
->data
;
1945 u_int np
= wme
->prefix
;
1947 if (data
->searchtype
== WINDOW_COPY_SEARCHUP
) {
1948 for (; np
!= 0; np
--)
1949 window_copy_search_down(wme
, data
->searchregex
);
1950 } else if (data
->searchtype
== WINDOW_COPY_SEARCHDOWN
) {
1951 for (; np
!= 0; np
--)
1952 window_copy_search_up(wme
, data
->searchregex
);
1954 return (WINDOW_COPY_CMD_NOTHING
);
1957 static enum window_copy_cmd_action
1958 window_copy_cmd_select_line(struct window_copy_cmd_state
*cs
)
1960 struct window_mode_entry
*wme
= cs
->wme
;
1961 struct window_copy_mode_data
*data
= wme
->data
;
1962 u_int np
= wme
->prefix
;
1964 data
->lineflag
= LINE_SEL_LEFT_RIGHT
;
1966 data
->selflag
= SEL_LINE
;
1967 data
->dx
= data
->cx
;
1968 data
->dy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1970 window_copy_cursor_start_of_line(wme
);
1971 data
->selrx
= data
->cx
;
1972 data
->selry
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1973 data
->endselry
= data
->selry
;
1974 window_copy_start_selection(wme
);
1975 window_copy_cursor_end_of_line(wme
);
1976 data
->endselry
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
1977 data
->endselrx
= window_copy_find_length(wme
, data
->endselry
);
1978 for (; np
> 1; np
--) {
1979 window_copy_cursor_down(wme
, 0);
1980 window_copy_cursor_end_of_line(wme
);
1983 return (WINDOW_COPY_CMD_REDRAW
);
1986 static enum window_copy_cmd_action
1987 window_copy_cmd_select_word(struct window_copy_cmd_state
*cs
)
1989 struct window_mode_entry
*wme
= cs
->wme
;
1990 struct options
*session_options
= cs
->s
->options
;
1991 struct window_copy_mode_data
*data
= wme
->data
;
1992 u_int px
, py
, nextx
, nexty
;
1994 data
->lineflag
= LINE_SEL_LEFT_RIGHT
;
1996 data
->selflag
= SEL_WORD
;
1997 data
->dx
= data
->cx
;
1998 data
->dy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
2000 data
->separators
= options_get_string(session_options
,
2002 window_copy_cursor_previous_word(wme
, data
->separators
, 0);
2004 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
2007 window_copy_start_selection(wme
);
2009 /* Handle single character words. */
2012 if (grid_get_line(data
->backing
->grid
, nexty
)->flags
&
2013 GRID_LINE_WRAPPED
&& nextx
> screen_size_x(data
->backing
) - 1) {
2017 if (px
>= window_copy_find_length(wme
, py
) ||
2018 !window_copy_in_set(wme
, nextx
, nexty
, WHITESPACE
))
2019 window_copy_cursor_next_word_end(wme
, data
->separators
, 1);
2021 window_copy_update_cursor(wme
, px
, data
->cy
);
2022 if (window_copy_update_selection(wme
, 1, 1))
2023 window_copy_redraw_lines(wme
, data
->cy
, 1);
2025 data
->endselrx
= data
->cx
;
2026 data
->endselry
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
2027 if (data
->dy
> data
->endselry
) {
2028 data
->dy
= data
->endselry
;
2029 data
->dx
= data
->endselrx
;
2030 } else if (data
->dx
> data
->endselrx
)
2031 data
->dx
= data
->endselrx
;
2033 return (WINDOW_COPY_CMD_REDRAW
);
2036 static enum window_copy_cmd_action
2037 window_copy_cmd_set_mark(struct window_copy_cmd_state
*cs
)
2039 struct window_copy_mode_data
*data
= cs
->wme
->data
;
2041 data
->mx
= data
->cx
;
2042 data
->my
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
2044 return (WINDOW_COPY_CMD_REDRAW
);
2047 static enum window_copy_cmd_action
2048 window_copy_cmd_start_of_line(struct window_copy_cmd_state
*cs
)
2050 struct window_mode_entry
*wme
= cs
->wme
;
2052 window_copy_cursor_start_of_line(wme
);
2053 return (WINDOW_COPY_CMD_NOTHING
);
2056 static enum window_copy_cmd_action
2057 window_copy_cmd_top_line(struct window_copy_cmd_state
*cs
)
2059 struct window_mode_entry
*wme
= cs
->wme
;
2060 struct window_copy_mode_data
*data
= wme
->data
;
2065 window_copy_update_selection(wme
, 1, 0);
2066 return (WINDOW_COPY_CMD_REDRAW
);
2069 static enum window_copy_cmd_action
2070 window_copy_cmd_copy_pipe_no_clear(struct window_copy_cmd_state
*cs
)
2072 struct window_mode_entry
*wme
= cs
->wme
;
2073 struct client
*c
= cs
->c
;
2074 struct session
*s
= cs
->s
;
2075 struct winlink
*wl
= cs
->wl
;
2076 struct window_pane
*wp
= wme
->wp
;
2077 char *command
= NULL
, *prefix
= NULL
;
2078 const char *arg1
= args_string(cs
->args
, 1);
2079 const char *arg2
= args_string(cs
->args
, 2);
2082 prefix
= format_single(NULL
, arg2
, c
, s
, wl
, wp
);
2084 if (s
!= NULL
&& arg1
!= NULL
&& *arg1
!= '\0')
2085 command
= format_single(NULL
, arg1
, c
, s
, wl
, wp
);
2086 window_copy_copy_pipe(wme
, s
, prefix
, command
);
2090 return (WINDOW_COPY_CMD_NOTHING
);
2093 static enum window_copy_cmd_action
2094 window_copy_cmd_copy_pipe(struct window_copy_cmd_state
*cs
)
2096 struct window_mode_entry
*wme
= cs
->wme
;
2098 window_copy_cmd_copy_pipe_no_clear(cs
);
2099 window_copy_clear_selection(wme
);
2100 return (WINDOW_COPY_CMD_REDRAW
);
2103 static enum window_copy_cmd_action
2104 window_copy_cmd_copy_pipe_and_cancel(struct window_copy_cmd_state
*cs
)
2106 struct window_mode_entry
*wme
= cs
->wme
;
2108 window_copy_cmd_copy_pipe_no_clear(cs
);
2109 window_copy_clear_selection(wme
);
2110 return (WINDOW_COPY_CMD_CANCEL
);
2113 static enum window_copy_cmd_action
2114 window_copy_cmd_pipe_no_clear(struct window_copy_cmd_state
*cs
)
2116 struct window_mode_entry
*wme
= cs
->wme
;
2117 struct client
*c
= cs
->c
;
2118 struct session
*s
= cs
->s
;
2119 struct winlink
*wl
= cs
->wl
;
2120 struct window_pane
*wp
= wme
->wp
;
2121 char *command
= NULL
;
2122 const char *arg1
= args_string(cs
->args
, 1);
2124 if (s
!= NULL
&& arg1
!= NULL
&& *arg1
!= '\0')
2125 command
= format_single(NULL
, arg1
, c
, s
, wl
, wp
);
2126 window_copy_pipe(wme
, s
, command
);
2129 return (WINDOW_COPY_CMD_NOTHING
);
2132 static enum window_copy_cmd_action
2133 window_copy_cmd_pipe(struct window_copy_cmd_state
*cs
)
2135 struct window_mode_entry
*wme
= cs
->wme
;
2137 window_copy_cmd_pipe_no_clear(cs
);
2138 window_copy_clear_selection(wme
);
2139 return (WINDOW_COPY_CMD_REDRAW
);
2142 static enum window_copy_cmd_action
2143 window_copy_cmd_pipe_and_cancel(struct window_copy_cmd_state
*cs
)
2145 struct window_mode_entry
*wme
= cs
->wme
;
2147 window_copy_cmd_pipe_no_clear(cs
);
2148 window_copy_clear_selection(wme
);
2149 return (WINDOW_COPY_CMD_CANCEL
);
2152 static enum window_copy_cmd_action
2153 window_copy_cmd_goto_line(struct window_copy_cmd_state
*cs
)
2155 struct window_mode_entry
*wme
= cs
->wme
;
2156 const char *arg1
= args_string(cs
->args
, 1);
2159 window_copy_goto_line(wme
, arg1
);
2160 return (WINDOW_COPY_CMD_NOTHING
);
2163 static enum window_copy_cmd_action
2164 window_copy_cmd_jump_backward(struct window_copy_cmd_state
*cs
)
2166 struct window_mode_entry
*wme
= cs
->wme
;
2167 struct window_copy_mode_data
*data
= wme
->data
;
2168 u_int np
= wme
->prefix
;
2169 const char *arg1
= args_string(cs
->args
, 1);
2171 if (*arg1
!= '\0') {
2172 data
->jumptype
= WINDOW_COPY_JUMPBACKWARD
;
2173 free(data
->jumpchar
);
2174 data
->jumpchar
= utf8_fromcstr(arg1
);
2175 for (; np
!= 0; np
--)
2176 window_copy_cursor_jump_back(wme
);
2178 return (WINDOW_COPY_CMD_NOTHING
);
2181 static enum window_copy_cmd_action
2182 window_copy_cmd_jump_forward(struct window_copy_cmd_state
*cs
)
2184 struct window_mode_entry
*wme
= cs
->wme
;
2185 struct window_copy_mode_data
*data
= wme
->data
;
2186 u_int np
= wme
->prefix
;
2187 const char *arg1
= args_string(cs
->args
, 1);
2189 if (*arg1
!= '\0') {
2190 data
->jumptype
= WINDOW_COPY_JUMPFORWARD
;
2191 free(data
->jumpchar
);
2192 data
->jumpchar
= utf8_fromcstr(arg1
);
2193 for (; np
!= 0; np
--)
2194 window_copy_cursor_jump(wme
);
2196 return (WINDOW_COPY_CMD_NOTHING
);
2199 static enum window_copy_cmd_action
2200 window_copy_cmd_jump_to_backward(struct window_copy_cmd_state
*cs
)
2202 struct window_mode_entry
*wme
= cs
->wme
;
2203 struct window_copy_mode_data
*data
= wme
->data
;
2204 u_int np
= wme
->prefix
;
2205 const char *arg1
= args_string(cs
->args
, 1);
2207 if (*arg1
!= '\0') {
2208 data
->jumptype
= WINDOW_COPY_JUMPTOBACKWARD
;
2209 free(data
->jumpchar
);
2210 data
->jumpchar
= utf8_fromcstr(arg1
);
2211 for (; np
!= 0; np
--)
2212 window_copy_cursor_jump_to_back(wme
);
2214 return (WINDOW_COPY_CMD_NOTHING
);
2217 static enum window_copy_cmd_action
2218 window_copy_cmd_jump_to_forward(struct window_copy_cmd_state
*cs
)
2220 struct window_mode_entry
*wme
= cs
->wme
;
2221 struct window_copy_mode_data
*data
= wme
->data
;
2222 u_int np
= wme
->prefix
;
2223 const char *arg1
= args_string(cs
->args
, 1);
2225 if (*arg1
!= '\0') {
2226 data
->jumptype
= WINDOW_COPY_JUMPTOFORWARD
;
2227 free(data
->jumpchar
);
2228 data
->jumpchar
= utf8_fromcstr(arg1
);
2229 for (; np
!= 0; np
--)
2230 window_copy_cursor_jump_to(wme
);
2232 return (WINDOW_COPY_CMD_NOTHING
);
2235 static enum window_copy_cmd_action
2236 window_copy_cmd_jump_to_mark(struct window_copy_cmd_state
*cs
)
2238 struct window_mode_entry
*wme
= cs
->wme
;
2240 window_copy_jump_to_mark(wme
);
2241 return (WINDOW_COPY_CMD_NOTHING
);
2244 static enum window_copy_cmd_action
2245 window_copy_cmd_next_prompt(struct window_copy_cmd_state
*cs
)
2247 struct window_mode_entry
*wme
= cs
->wme
;
2249 window_copy_cursor_prompt(wme
, 1);
2250 return (WINDOW_COPY_CMD_NOTHING
);
2253 static enum window_copy_cmd_action
2254 window_copy_cmd_previous_prompt(struct window_copy_cmd_state
*cs
)
2256 struct window_mode_entry
*wme
= cs
->wme
;
2258 window_copy_cursor_prompt(wme
, 0);
2259 return (WINDOW_COPY_CMD_NOTHING
);
2262 static enum window_copy_cmd_action
2263 window_copy_cmd_search_backward(struct window_copy_cmd_state
*cs
)
2265 struct window_mode_entry
*wme
= cs
->wme
;
2266 struct window_copy_mode_data
*data
= wme
->data
;
2267 u_int np
= wme
->prefix
;
2269 if (!window_copy_expand_search_string(cs
))
2270 return (WINDOW_COPY_CMD_NOTHING
);
2272 if (data
->searchstr
!= NULL
) {
2273 data
->searchtype
= WINDOW_COPY_SEARCHUP
;
2274 data
->searchregex
= 1;
2276 for (; np
!= 0; np
--)
2277 window_copy_search_up(wme
, 1);
2279 return (WINDOW_COPY_CMD_NOTHING
);
2282 static enum window_copy_cmd_action
2283 window_copy_cmd_search_backward_text(struct window_copy_cmd_state
*cs
)
2285 struct window_mode_entry
*wme
= cs
->wme
;
2286 struct window_copy_mode_data
*data
= wme
->data
;
2287 u_int np
= wme
->prefix
;
2289 if (!window_copy_expand_search_string(cs
))
2290 return (WINDOW_COPY_CMD_NOTHING
);
2292 if (data
->searchstr
!= NULL
) {
2293 data
->searchtype
= WINDOW_COPY_SEARCHUP
;
2294 data
->searchregex
= 0;
2296 for (; np
!= 0; np
--)
2297 window_copy_search_up(wme
, 0);
2299 return (WINDOW_COPY_CMD_NOTHING
);
2302 static enum window_copy_cmd_action
2303 window_copy_cmd_search_forward(struct window_copy_cmd_state
*cs
)
2305 struct window_mode_entry
*wme
= cs
->wme
;
2306 struct window_copy_mode_data
*data
= wme
->data
;
2307 u_int np
= wme
->prefix
;
2309 if (!window_copy_expand_search_string(cs
))
2310 return (WINDOW_COPY_CMD_NOTHING
);
2312 if (data
->searchstr
!= NULL
) {
2313 data
->searchtype
= WINDOW_COPY_SEARCHDOWN
;
2314 data
->searchregex
= 1;
2316 for (; np
!= 0; np
--)
2317 window_copy_search_down(wme
, 1);
2319 return (WINDOW_COPY_CMD_NOTHING
);
2322 static enum window_copy_cmd_action
2323 window_copy_cmd_search_forward_text(struct window_copy_cmd_state
*cs
)
2325 struct window_mode_entry
*wme
= cs
->wme
;
2326 struct window_copy_mode_data
*data
= wme
->data
;
2327 u_int np
= wme
->prefix
;
2329 if (!window_copy_expand_search_string(cs
))
2330 return (WINDOW_COPY_CMD_NOTHING
);
2332 if (data
->searchstr
!= NULL
) {
2333 data
->searchtype
= WINDOW_COPY_SEARCHDOWN
;
2334 data
->searchregex
= 0;
2336 for (; np
!= 0; np
--)
2337 window_copy_search_down(wme
, 0);
2339 return (WINDOW_COPY_CMD_NOTHING
);
2342 static enum window_copy_cmd_action
2343 window_copy_cmd_search_backward_incremental(struct window_copy_cmd_state
*cs
)
2345 struct window_mode_entry
*wme
= cs
->wme
;
2346 struct window_copy_mode_data
*data
= wme
->data
;
2347 const char *arg1
= args_string(cs
->args
, 1);
2348 const char *ss
= data
->searchstr
;
2350 enum window_copy_cmd_action action
= WINDOW_COPY_CMD_NOTHING
;
2354 log_debug("%s: %s", __func__
, arg1
);
2357 if (data
->searchx
== -1 || data
->searchy
== -1) {
2358 data
->searchx
= data
->cx
;
2359 data
->searchy
= data
->cy
;
2360 data
->searcho
= data
->oy
;
2361 } else if (ss
!= NULL
&& strcmp(arg1
, ss
) != 0) {
2362 data
->cx
= data
->searchx
;
2363 data
->cy
= data
->searchy
;
2364 data
->oy
= data
->searcho
;
2365 action
= WINDOW_COPY_CMD_REDRAW
;
2367 if (*arg1
== '\0') {
2368 window_copy_clear_marks(wme
);
2369 return (WINDOW_COPY_CMD_REDRAW
);
2374 data
->searchtype
= WINDOW_COPY_SEARCHUP
;
2375 data
->searchregex
= 0;
2376 free(data
->searchstr
);
2377 data
->searchstr
= xstrdup(arg1
);
2378 if (!window_copy_search_up(wme
, 0)) {
2379 window_copy_clear_marks(wme
);
2380 return (WINDOW_COPY_CMD_REDRAW
);
2384 data
->searchtype
= WINDOW_COPY_SEARCHDOWN
;
2385 data
->searchregex
= 0;
2386 free(data
->searchstr
);
2387 data
->searchstr
= xstrdup(arg1
);
2388 if (!window_copy_search_down(wme
, 0)) {
2389 window_copy_clear_marks(wme
);
2390 return (WINDOW_COPY_CMD_REDRAW
);
2397 static enum window_copy_cmd_action
2398 window_copy_cmd_search_forward_incremental(struct window_copy_cmd_state
*cs
)
2400 struct window_mode_entry
*wme
= cs
->wme
;
2401 struct window_copy_mode_data
*data
= wme
->data
;
2402 const char *arg1
= args_string(cs
->args
, 1);
2403 const char *ss
= data
->searchstr
;
2405 enum window_copy_cmd_action action
= WINDOW_COPY_CMD_NOTHING
;
2409 log_debug("%s: %s", __func__
, arg1
);
2412 if (data
->searchx
== -1 || data
->searchy
== -1) {
2413 data
->searchx
= data
->cx
;
2414 data
->searchy
= data
->cy
;
2415 data
->searcho
= data
->oy
;
2416 } else if (ss
!= NULL
&& strcmp(arg1
, ss
) != 0) {
2417 data
->cx
= data
->searchx
;
2418 data
->cy
= data
->searchy
;
2419 data
->oy
= data
->searcho
;
2420 action
= WINDOW_COPY_CMD_REDRAW
;
2422 if (*arg1
== '\0') {
2423 window_copy_clear_marks(wme
);
2424 return (WINDOW_COPY_CMD_REDRAW
);
2429 data
->searchtype
= WINDOW_COPY_SEARCHDOWN
;
2430 data
->searchregex
= 0;
2431 free(data
->searchstr
);
2432 data
->searchstr
= xstrdup(arg1
);
2433 if (!window_copy_search_down(wme
, 0)) {
2434 window_copy_clear_marks(wme
);
2435 return (WINDOW_COPY_CMD_REDRAW
);
2439 data
->searchtype
= WINDOW_COPY_SEARCHUP
;
2440 data
->searchregex
= 0;
2441 free(data
->searchstr
);
2442 data
->searchstr
= xstrdup(arg1
);
2443 if (!window_copy_search_up(wme
, 0)) {
2444 window_copy_clear_marks(wme
);
2445 return (WINDOW_COPY_CMD_REDRAW
);
2451 static enum window_copy_cmd_action
2452 window_copy_cmd_refresh_from_pane(struct window_copy_cmd_state
*cs
)
2454 struct window_mode_entry
*wme
= cs
->wme
;
2455 struct window_pane
*wp
= wme
->swp
;
2456 struct window_copy_mode_data
*data
= wme
->data
;
2459 return (WINDOW_COPY_CMD_NOTHING
);
2461 screen_free(data
->backing
);
2462 free(data
->backing
);
2463 data
->backing
= window_copy_clone_screen(&wp
->base
, &data
->screen
, NULL
, NULL
, wme
->swp
!= wme
->wp
);
2465 window_copy_size_changed(wme
);
2466 return (WINDOW_COPY_CMD_REDRAW
);
2469 static const struct {
2470 const char *command
;
2473 enum window_copy_cmd_clear clear
;
2474 enum window_copy_cmd_action (*f
)(struct window_copy_cmd_state
*);
2475 } window_copy_cmd_table
[] = {
2476 { .command
= "append-selection",
2479 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2480 .f
= window_copy_cmd_append_selection
2482 { .command
= "append-selection-and-cancel",
2485 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2486 .f
= window_copy_cmd_append_selection_and_cancel
2488 { .command
= "back-to-indentation",
2491 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2492 .f
= window_copy_cmd_back_to_indentation
2494 { .command
= "begin-selection",
2497 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2498 .f
= window_copy_cmd_begin_selection
2500 { .command
= "bottom-line",
2503 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2504 .f
= window_copy_cmd_bottom_line
2506 { .command
= "cancel",
2509 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2510 .f
= window_copy_cmd_cancel
2512 { .command
= "clear-selection",
2515 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2516 .f
= window_copy_cmd_clear_selection
2518 { .command
= "copy-end-of-line",
2521 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2522 .f
= window_copy_cmd_copy_end_of_line
2524 { .command
= "copy-end-of-line-and-cancel",
2527 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2528 .f
= window_copy_cmd_copy_end_of_line_and_cancel
2530 { .command
= "copy-pipe-end-of-line",
2533 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2534 .f
= window_copy_cmd_copy_pipe_end_of_line
2536 { .command
= "copy-pipe-end-of-line-and-cancel",
2539 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2540 .f
= window_copy_cmd_copy_pipe_end_of_line_and_cancel
2542 { .command
= "copy-line",
2545 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2546 .f
= window_copy_cmd_copy_line
2548 { .command
= "copy-line-and-cancel",
2551 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2552 .f
= window_copy_cmd_copy_line_and_cancel
2554 { .command
= "copy-pipe-line",
2557 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2558 .f
= window_copy_cmd_copy_pipe_line
2560 { .command
= "copy-pipe-line-and-cancel",
2563 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2564 .f
= window_copy_cmd_copy_pipe_line_and_cancel
2566 { .command
= "copy-pipe-no-clear",
2569 .clear
= WINDOW_COPY_CMD_CLEAR_NEVER
,
2570 .f
= window_copy_cmd_copy_pipe_no_clear
2572 { .command
= "copy-pipe",
2575 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2576 .f
= window_copy_cmd_copy_pipe
2578 { .command
= "copy-pipe-and-cancel",
2581 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2582 .f
= window_copy_cmd_copy_pipe_and_cancel
2584 { .command
= "copy-selection-no-clear",
2587 .clear
= WINDOW_COPY_CMD_CLEAR_NEVER
,
2588 .f
= window_copy_cmd_copy_selection_no_clear
2590 { .command
= "copy-selection",
2593 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2594 .f
= window_copy_cmd_copy_selection
2596 { .command
= "copy-selection-and-cancel",
2599 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2600 .f
= window_copy_cmd_copy_selection_and_cancel
2602 { .command
= "cursor-down",
2605 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2606 .f
= window_copy_cmd_cursor_down
2608 { .command
= "cursor-down-and-cancel",
2611 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2612 .f
= window_copy_cmd_cursor_down_and_cancel
2614 { .command
= "cursor-left",
2617 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2618 .f
= window_copy_cmd_cursor_left
2620 { .command
= "cursor-right",
2623 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2624 .f
= window_copy_cmd_cursor_right
2626 { .command
= "cursor-up",
2629 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2630 .f
= window_copy_cmd_cursor_up
2632 { .command
= "end-of-line",
2635 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2636 .f
= window_copy_cmd_end_of_line
2638 { .command
= "goto-line",
2641 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2642 .f
= window_copy_cmd_goto_line
2644 { .command
= "halfpage-down",
2647 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2648 .f
= window_copy_cmd_halfpage_down
2650 { .command
= "halfpage-down-and-cancel",
2653 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2654 .f
= window_copy_cmd_halfpage_down_and_cancel
2656 { .command
= "halfpage-up",
2659 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2660 .f
= window_copy_cmd_halfpage_up
2662 { .command
= "history-bottom",
2665 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2666 .f
= window_copy_cmd_history_bottom
2668 { .command
= "history-top",
2671 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2672 .f
= window_copy_cmd_history_top
2674 { .command
= "jump-again",
2677 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2678 .f
= window_copy_cmd_jump_again
2680 { .command
= "jump-backward",
2683 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2684 .f
= window_copy_cmd_jump_backward
2686 { .command
= "jump-forward",
2689 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2690 .f
= window_copy_cmd_jump_forward
2692 { .command
= "jump-reverse",
2695 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2696 .f
= window_copy_cmd_jump_reverse
2698 { .command
= "jump-to-backward",
2701 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2702 .f
= window_copy_cmd_jump_to_backward
2704 { .command
= "jump-to-forward",
2707 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2708 .f
= window_copy_cmd_jump_to_forward
2710 { .command
= "jump-to-mark",
2713 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2714 .f
= window_copy_cmd_jump_to_mark
2716 { .command
= "next-prompt",
2719 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2720 .f
= window_copy_cmd_next_prompt
2722 { .command
= "previous-prompt",
2725 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2726 .f
= window_copy_cmd_previous_prompt
2728 { .command
= "middle-line",
2731 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2732 .f
= window_copy_cmd_middle_line
2734 { .command
= "next-matching-bracket",
2737 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2738 .f
= window_copy_cmd_next_matching_bracket
2740 { .command
= "next-paragraph",
2743 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2744 .f
= window_copy_cmd_next_paragraph
2746 { .command
= "next-space",
2749 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2750 .f
= window_copy_cmd_next_space
2752 { .command
= "next-space-end",
2755 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2756 .f
= window_copy_cmd_next_space_end
2758 { .command
= "next-word",
2761 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2762 .f
= window_copy_cmd_next_word
2764 { .command
= "next-word-end",
2767 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2768 .f
= window_copy_cmd_next_word_end
2770 { .command
= "other-end",
2773 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2774 .f
= window_copy_cmd_other_end
2776 { .command
= "page-down",
2779 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2780 .f
= window_copy_cmd_page_down
2782 { .command
= "page-down-and-cancel",
2785 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2786 .f
= window_copy_cmd_page_down_and_cancel
2788 { .command
= "page-up",
2791 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2792 .f
= window_copy_cmd_page_up
2794 { .command
= "pipe-no-clear",
2797 .clear
= WINDOW_COPY_CMD_CLEAR_NEVER
,
2798 .f
= window_copy_cmd_pipe_no_clear
2800 { .command
= "pipe",
2803 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2804 .f
= window_copy_cmd_pipe
2806 { .command
= "pipe-and-cancel",
2809 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2810 .f
= window_copy_cmd_pipe_and_cancel
2812 { .command
= "previous-matching-bracket",
2815 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2816 .f
= window_copy_cmd_previous_matching_bracket
2818 { .command
= "previous-paragraph",
2821 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2822 .f
= window_copy_cmd_previous_paragraph
2824 { .command
= "previous-space",
2827 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2828 .f
= window_copy_cmd_previous_space
2830 { .command
= "previous-word",
2833 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2834 .f
= window_copy_cmd_previous_word
2836 { .command
= "rectangle-on",
2839 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2840 .f
= window_copy_cmd_rectangle_on
2842 { .command
= "rectangle-off",
2845 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2846 .f
= window_copy_cmd_rectangle_off
2848 { .command
= "rectangle-toggle",
2851 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2852 .f
= window_copy_cmd_rectangle_toggle
2854 { .command
= "refresh-from-pane",
2857 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2858 .f
= window_copy_cmd_refresh_from_pane
2860 { .command
= "scroll-bottom",
2863 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2864 .f
= window_copy_cmd_scroll_bottom
2866 { .command
= "scroll-down",
2869 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2870 .f
= window_copy_cmd_scroll_down
2872 { .command
= "scroll-down-and-cancel",
2875 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2876 .f
= window_copy_cmd_scroll_down_and_cancel
2878 { .command
= "scroll-middle",
2881 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2882 .f
= window_copy_cmd_scroll_middle
2884 { .command
= "scroll-top",
2887 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2888 .f
= window_copy_cmd_scroll_top
2890 { .command
= "scroll-up",
2893 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2894 .f
= window_copy_cmd_scroll_up
2896 { .command
= "search-again",
2899 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2900 .f
= window_copy_cmd_search_again
2902 { .command
= "search-backward",
2905 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2906 .f
= window_copy_cmd_search_backward
2908 { .command
= "search-backward-text",
2911 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2912 .f
= window_copy_cmd_search_backward_text
2914 { .command
= "search-backward-incremental",
2917 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2918 .f
= window_copy_cmd_search_backward_incremental
2920 { .command
= "search-forward",
2923 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2924 .f
= window_copy_cmd_search_forward
2926 { .command
= "search-forward-text",
2929 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2930 .f
= window_copy_cmd_search_forward_text
2932 { .command
= "search-forward-incremental",
2935 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2936 .f
= window_copy_cmd_search_forward_incremental
2938 { .command
= "search-reverse",
2941 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2942 .f
= window_copy_cmd_search_reverse
2944 { .command
= "select-line",
2947 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2948 .f
= window_copy_cmd_select_line
2950 { .command
= "select-word",
2953 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2954 .f
= window_copy_cmd_select_word
2956 { .command
= "set-mark",
2959 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2960 .f
= window_copy_cmd_set_mark
2962 { .command
= "start-of-line",
2965 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2966 .f
= window_copy_cmd_start_of_line
2968 { .command
= "stop-selection",
2971 .clear
= WINDOW_COPY_CMD_CLEAR_ALWAYS
,
2972 .f
= window_copy_cmd_stop_selection
2974 { .command
= "toggle-position",
2977 .clear
= WINDOW_COPY_CMD_CLEAR_NEVER
,
2978 .f
= window_copy_cmd_toggle_position
2980 { .command
= "top-line",
2983 .clear
= WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
,
2984 .f
= window_copy_cmd_top_line
2989 window_copy_command(struct window_mode_entry
*wme
, struct client
*c
,
2990 struct session
*s
, struct winlink
*wl
, struct args
*args
,
2991 struct mouse_event
*m
)
2993 struct window_copy_mode_data
*data
= wme
->data
;
2994 struct window_copy_cmd_state cs
;
2995 enum window_copy_cmd_action action
;
2996 enum window_copy_cmd_clear clear
= WINDOW_COPY_CMD_CLEAR_NEVER
;
2997 const char *command
;
2998 u_int i
, count
= args_count(args
);
3003 command
= args_string(args
, 0);
3005 if (m
!= NULL
&& m
->valid
&& !MOUSE_WHEEL(m
->b
))
3006 window_copy_move_mouse(m
);
3016 action
= WINDOW_COPY_CMD_NOTHING
;
3017 for (i
= 0; i
< nitems(window_copy_cmd_table
); i
++) {
3018 if (strcmp(window_copy_cmd_table
[i
].command
, command
) == 0) {
3019 if (count
- 1 < window_copy_cmd_table
[i
].minargs
||
3020 count
- 1 > window_copy_cmd_table
[i
].maxargs
)
3022 clear
= window_copy_cmd_table
[i
].clear
;
3023 action
= window_copy_cmd_table
[i
].f(&cs
);
3028 if (strncmp(command
, "search-", 7) != 0 && data
->searchmark
!= NULL
) {
3029 keys
= options_get_number(wme
->wp
->window
->options
, "mode-keys");
3030 if (clear
== WINDOW_COPY_CMD_CLEAR_EMACS_ONLY
&&
3032 clear
= WINDOW_COPY_CMD_CLEAR_NEVER
;
3033 if (clear
!= WINDOW_COPY_CMD_CLEAR_NEVER
) {
3034 window_copy_clear_marks(wme
);
3035 data
->searchx
= data
->searchy
= -1;
3037 if (action
== WINDOW_COPY_CMD_NOTHING
)
3038 action
= WINDOW_COPY_CMD_REDRAW
;
3042 if (action
== WINDOW_COPY_CMD_CANCEL
)
3043 window_pane_reset_mode(wme
->wp
);
3044 else if (action
== WINDOW_COPY_CMD_REDRAW
)
3045 window_copy_redraw_screen(wme
);
3049 window_copy_scroll_to(struct window_mode_entry
*wme
, u_int px
, u_int py
,
3052 struct window_copy_mode_data
*data
= wme
->data
;
3053 struct grid
*gd
= data
->backing
->grid
;
3058 if (py
>= gd
->hsize
- data
->oy
&& py
< gd
->hsize
- data
->oy
+ gd
->sy
)
3059 data
->cy
= py
- (gd
->hsize
- data
->oy
);
3065 } else if (py
> gd
->hsize
+ gd
->sy
- gap
) {
3067 data
->cy
= py
- gd
->hsize
;
3069 offset
= py
+ gap
- gd
->sy
;
3070 data
->cy
= py
- offset
;
3072 data
->oy
= gd
->hsize
- offset
;
3075 if (!no_redraw
&& data
->searchmark
!= NULL
&& !data
->timeout
)
3076 window_copy_search_marks(wme
, NULL
, data
->searchregex
, 1);
3077 window_copy_update_selection(wme
, 1, 0);
3079 window_copy_redraw_screen(wme
);
3083 window_copy_search_compare(struct grid
*gd
, u_int px
, u_int py
,
3084 struct grid
*sgd
, u_int spx
, int cis
)
3086 struct grid_cell gc
, sgc
;
3087 const struct utf8_data
*ud
, *sud
;
3089 grid_get_cell(gd
, px
, py
, &gc
);
3091 grid_get_cell(sgd
, spx
, 0, &sgc
);
3094 if (ud
->size
!= sud
->size
|| ud
->width
!= sud
->width
)
3097 if (cis
&& ud
->size
== 1)
3098 return (tolower(ud
->data
[0]) == sud
->data
[0]);
3100 return (memcmp(ud
->data
, sud
->data
, ud
->size
) == 0);
3104 window_copy_search_lr(struct grid
*gd
, struct grid
*sgd
, u_int
*ppx
, u_int py
,
3105 u_int first
, u_int last
, int cis
)
3107 u_int ax
, bx
, px
, pywrap
, endline
;
3109 struct grid_line
*gl
;
3111 endline
= gd
->hsize
+ gd
->sy
- 1;
3112 for (ax
= first
; ax
< last
; ax
++) {
3113 for (bx
= 0; bx
< sgd
->sx
; bx
++) {
3117 while (px
>= gd
->sx
&& pywrap
< endline
) {
3118 gl
= grid_get_line(gd
, pywrap
);
3119 if (~gl
->flags
& GRID_LINE_WRAPPED
)
3124 /* We have run off the end of the grid. */
3127 matched
= window_copy_search_compare(gd
, px
, pywrap
,
3132 if (bx
== sgd
->sx
) {
3141 window_copy_search_rl(struct grid
*gd
,
3142 struct grid
*sgd
, u_int
*ppx
, u_int py
, u_int first
, u_int last
, int cis
)
3144 u_int ax
, bx
, px
, pywrap
, endline
;
3146 struct grid_line
*gl
;
3148 endline
= gd
->hsize
+ gd
->sy
- 1;
3149 for (ax
= last
; ax
> first
; ax
--) {
3150 for (bx
= 0; bx
< sgd
->sx
; bx
++) {
3154 while (px
>= gd
->sx
&& pywrap
< endline
) {
3155 gl
= grid_get_line(gd
, pywrap
);
3156 if (~gl
->flags
& GRID_LINE_WRAPPED
)
3161 /* We have run off the end of the grid. */
3164 matched
= window_copy_search_compare(gd
, px
, pywrap
,
3169 if (bx
== sgd
->sx
) {
3178 window_copy_search_lr_regex(struct grid
*gd
, u_int
*ppx
, u_int
*psx
, u_int py
,
3179 u_int first
, u_int last
, regex_t
*reg
)
3182 u_int endline
, foundx
, foundy
, len
, pywrap
, size
= 1;
3184 regmatch_t regmatch
;
3185 struct grid_line
*gl
;
3188 * This can happen during search if the last match was the last
3189 * character on a line.
3194 /* Set flags for regex search. */
3196 eflags
|= REG_NOTBOL
;
3198 /* Need to look at the entire string. */
3199 buf
= xmalloc(size
);
3201 buf
= window_copy_stringify(gd
, py
, first
, gd
->sx
, buf
, &size
);
3202 len
= gd
->sx
- first
;
3203 endline
= gd
->hsize
+ gd
->sy
- 1;
3205 while (buf
!= NULL
&& pywrap
<= endline
) {
3206 gl
= grid_get_line(gd
, pywrap
);
3207 if (~gl
->flags
& GRID_LINE_WRAPPED
)
3210 buf
= window_copy_stringify(gd
, pywrap
, 0, gd
->sx
, buf
, &size
);
3214 if (regexec(reg
, buf
, 1, ®match
, eflags
) == 0 &&
3215 regmatch
.rm_so
!= regmatch
.rm_eo
) {
3218 window_copy_cstrtocellpos(gd
, len
, &foundx
, &foundy
,
3219 buf
+ regmatch
.rm_so
);
3220 if (foundy
== py
&& foundx
< last
) {
3222 len
-= foundx
- first
;
3223 window_copy_cstrtocellpos(gd
, len
, &foundx
, &foundy
,
3224 buf
+ regmatch
.rm_eo
);
3226 while (foundy
> py
) {
3243 window_copy_search_rl_regex(struct grid
*gd
, u_int
*ppx
, u_int
*psx
, u_int py
,
3244 u_int first
, u_int last
, regex_t
*reg
)
3247 u_int endline
, len
, pywrap
, size
= 1;
3249 struct grid_line
*gl
;
3251 /* Set flags for regex search. */
3253 eflags
|= REG_NOTBOL
;
3255 /* Need to look at the entire string. */
3256 buf
= xmalloc(size
);
3258 buf
= window_copy_stringify(gd
, py
, first
, gd
->sx
, buf
, &size
);
3259 len
= gd
->sx
- first
;
3260 endline
= gd
->hsize
+ gd
->sy
- 1;
3262 while (buf
!= NULL
&& (pywrap
<= endline
)) {
3263 gl
= grid_get_line(gd
, pywrap
);
3264 if (~gl
->flags
& GRID_LINE_WRAPPED
)
3267 buf
= window_copy_stringify(gd
, pywrap
, 0, gd
->sx
, buf
, &size
);
3271 if (window_copy_last_regex(gd
, py
, first
, last
, len
, ppx
, psx
, buf
,
3285 window_copy_cellstring(const struct grid_line
*gl
, u_int px
, size_t *size
,
3288 static struct utf8_data ud
;
3289 struct grid_cell_entry
*gce
;
3292 if (px
>= gl
->cellsize
) {
3298 gce
= &gl
->celldata
[px
];
3299 if (gce
->flags
& GRID_FLAG_PADDING
) {
3304 if (~gce
->flags
& GRID_FLAG_EXTENDED
) {
3307 return (&gce
->data
.data
);
3310 utf8_to_data(gl
->extddata
[gce
->offset
].data
, &ud
);
3319 copy
= xmalloc(ud
.size
);
3320 memcpy(copy
, ud
.data
, ud
.size
);
3324 /* Find last match in given range. */
3326 window_copy_last_regex(struct grid
*gd
, u_int py
, u_int first
, u_int last
,
3327 u_int len
, u_int
*ppx
, u_int
*psx
, const char *buf
, const regex_t
*preg
,
3330 u_int foundx
, foundy
, oldx
, px
= 0, savepx
, savesx
= 0;
3331 regmatch_t regmatch
;
3336 while (regexec(preg
, buf
+ px
, 1, ®match
, eflags
) == 0) {
3337 if (regmatch
.rm_so
== regmatch
.rm_eo
)
3339 window_copy_cstrtocellpos(gd
, len
, &foundx
, &foundy
,
3340 buf
+ px
+ regmatch
.rm_so
);
3341 if (foundy
> py
|| foundx
>= last
)
3343 len
-= foundx
- oldx
;
3345 window_copy_cstrtocellpos(gd
, len
, &foundx
, &foundy
,
3346 buf
+ px
+ regmatch
.rm_eo
);
3347 if (foundy
> py
|| foundx
>= last
) {
3350 while (foundy
> py
) {
3357 savesx
= foundx
- savepx
;
3361 px
+= regmatch
.rm_eo
;
3375 /* Stringify line and append to input buffer. Caller frees. */
3377 window_copy_stringify(struct grid
*gd
, u_int py
, u_int first
, u_int last
,
3378 char *buf
, u_int
*size
)
3380 u_int ax
, bx
, newsize
= *size
;
3381 const struct grid_line
*gl
;
3383 size_t bufsize
= 1024, dlen
;
3386 while (bufsize
< newsize
)
3388 buf
= xrealloc(buf
, bufsize
);
3390 gl
= grid_peek_line(gd
, py
);
3392 for (ax
= first
; ax
< last
; ax
++) {
3393 d
= window_copy_cellstring(gl
, ax
, &dlen
, &allocated
);
3395 while (bufsize
< newsize
) {
3397 buf
= xrealloc(buf
, bufsize
);
3402 memcpy(buf
+ bx
, d
, dlen
);
3408 buf
[newsize
- 1] = '\0';
3414 /* Map start of C string containing UTF-8 data to grid cell position. */
3416 window_copy_cstrtocellpos(struct grid
*gd
, u_int ncells
, u_int
*ppx
, u_int
*ppy
,
3419 u_int cell
, ccell
, px
, pywrap
, pos
, len
;
3421 const struct grid_line
*gl
;
3430 /* Populate the array of cell data. */
3431 cells
= xreallocarray(NULL
, ncells
, sizeof cells
[0]);
3435 gl
= grid_peek_line(gd
, pywrap
);
3436 while (cell
< ncells
) {
3437 cells
[cell
].d
= window_copy_cellstring(gl
, px
,
3438 &cells
[cell
].dlen
, &cells
[cell
].allocated
);
3444 gl
= grid_peek_line(gd
, pywrap
);
3448 /* Locate starting cell. */
3451 while (cell
< ncells
) {
3455 while (ccell
< ncells
) {
3456 if (str
[pos
] == '\0') {
3461 dlen
= cells
[ccell
].dlen
;
3463 if (str
[pos
] != *d
) {
3469 if (dlen
> len
- pos
)
3471 if (memcmp(str
+ pos
, d
, dlen
) != 0) {
3484 /* If not found this will be one past the end. */
3487 while (px
>= gd
->sx
) {
3495 /* Free cell data. */
3496 for (cell
= 0; cell
< ncells
; cell
++) {
3497 if (cells
[cell
].allocated
)
3498 free((void *)cells
[cell
].d
);
3504 window_copy_move_left(struct screen
*s
, u_int
*fx
, u_int
*fy
, int wrapflag
)
3506 if (*fx
== 0) { /* left */
3507 if (*fy
== 0) { /* top */
3509 *fx
= screen_size_x(s
) - 1;
3510 *fy
= screen_hsize(s
) + screen_size_y(s
) - 1;
3514 *fx
= screen_size_x(s
) - 1;
3521 window_copy_move_right(struct screen
*s
, u_int
*fx
, u_int
*fy
, int wrapflag
)
3523 if (*fx
== screen_size_x(s
) - 1) { /* right */
3524 if (*fy
== screen_hsize(s
) + screen_size_y(s
) - 1) { /* bottom */
3538 window_copy_is_lowercase(const char *ptr
)
3540 while (*ptr
!= '\0') {
3541 if (*ptr
!= tolower((u_char
)*ptr
))
3549 * Handle backward wrapped regex searches with overlapping matches. In this case
3550 * find the longest overlapping match from previous wrapped lines.
3553 window_copy_search_back_overlap(struct grid
*gd
, regex_t
*preg
, u_int
*ppx
,
3554 u_int
*psx
, u_int
*ppy
, u_int endline
)
3556 u_int endx
, endy
, oldendx
, oldendy
, px
, py
, sx
;
3559 oldendx
= *ppx
+ *psx
;
3561 while (oldendx
> gd
->sx
- 1) {
3569 while (found
&& px
== 0 && py
- 1 > endline
&&
3570 grid_get_line(gd
, py
- 2)->flags
& GRID_LINE_WRAPPED
&&
3571 endx
== oldendx
&& endy
== oldendy
) {
3573 found
= window_copy_search_rl_regex(gd
, &px
, &sx
, py
- 1, 0,
3578 while (endx
> gd
->sx
- 1) {
3582 if (endx
== oldendx
&& endy
== oldendy
) {
3591 * Search for text stored in sgd starting from position fx,fy up to endline. If
3592 * found, jump to it. If cis then ignore case. The direction is 0 for searching
3593 * up, down otherwise. If wrap then go to begin/end of grid and try again if
3597 window_copy_search_jump(struct window_mode_entry
*wme
, struct grid
*gd
,
3598 struct grid
*sgd
, u_int fx
, u_int fy
, u_int endline
, int cis
, int wrap
,
3599 int direction
, int regex
)
3601 u_int i
, px
, sx
, ssize
= 1;
3602 int found
= 0, cflags
= REG_EXTENDED
;
3607 sbuf
= xmalloc(ssize
);
3609 sbuf
= window_copy_stringify(sgd
, 0, 0, sgd
->sx
, sbuf
, &ssize
);
3611 cflags
|= REG_ICASE
;
3612 if (regcomp(®
, sbuf
, cflags
) != 0) {
3620 for (i
= fy
; i
<= endline
; i
++) {
3622 found
= window_copy_search_lr_regex(gd
,
3623 &px
, &sx
, i
, fx
, gd
->sx
, ®
);
3625 found
= window_copy_search_lr(gd
, sgd
,
3626 &px
, i
, fx
, gd
->sx
, cis
);
3633 for (i
= fy
+ 1; endline
< i
; i
--) {
3635 found
= window_copy_search_rl_regex(gd
,
3636 &px
, &sx
, i
- 1, 0, fx
+ 1, ®
);
3638 window_copy_search_back_overlap(gd
,
3639 ®
, &px
, &sx
, &i
, endline
);
3642 found
= window_copy_search_rl(gd
, sgd
,
3643 &px
, i
- 1, 0, fx
+ 1, cis
);
3656 window_copy_scroll_to(wme
, px
, i
, 1);
3660 return (window_copy_search_jump(wme
, gd
, sgd
,
3661 direction
? 0 : gd
->sx
- 1,
3662 direction
? 0 : gd
->hsize
+ gd
->sy
- 1, fy
, cis
, 0,
3669 window_copy_move_after_search_mark(struct window_copy_mode_data
*data
,
3670 u_int
*fx
, u_int
*fy
, int wrapflag
)
3672 struct screen
*s
= data
->backing
;
3675 if (window_copy_search_mark_at(data
, *fx
, *fy
, &start
) == 0 &&
3676 data
->searchmark
[start
] != 0) {
3677 while (window_copy_search_mark_at(data
, *fx
, *fy
, &at
) == 0) {
3678 if (data
->searchmark
[at
] != data
->searchmark
[start
])
3680 /* Stop if not wrapping and at the end of the grid. */
3682 *fx
== screen_size_x(s
) - 1 &&
3683 *fy
== screen_hsize(s
) + screen_size_y(s
) - 1)
3686 window_copy_move_right(s
, fx
, fy
, wrapflag
);
3692 * Search in for text searchstr. If direction is 0 then search up, otherwise
3696 window_copy_search(struct window_mode_entry
*wme
, int direction
, int regex
)
3698 struct window_pane
*wp
= wme
->wp
;
3699 struct window_copy_mode_data
*data
= wme
->data
;
3700 struct screen
*s
= data
->backing
, ss
;
3701 struct screen_write_ctx ctx
;
3702 struct grid
*gd
= s
->grid
;
3703 const char *str
= data
->searchstr
;
3704 u_int at
, endline
, fx
, fy
, start
;
3705 int cis
, found
, keys
, visible_only
;
3708 if (regex
&& str
[strcspn(str
, "^$*+()?[].\\")] == '\0')
3711 data
->searchdirection
= direction
;
3716 if (data
->searchall
|| wp
->searchstr
== NULL
||
3717 wp
->searchregex
!= regex
) {
3719 data
->searchall
= 0;
3721 visible_only
= (strcmp(wp
->searchstr
, str
) == 0);
3722 if (visible_only
== 0 && data
->searchmark
!= NULL
)
3723 window_copy_clear_marks(wme
);
3724 free(wp
->searchstr
);
3725 wp
->searchstr
= xstrdup(str
);
3726 wp
->searchregex
= regex
;
3729 fy
= screen_hsize(data
->backing
) - data
->oy
+ data
->cy
;
3731 screen_init(&ss
, screen_write_strlen("%s", str
), 1, 0);
3732 screen_write_start(&ctx
, &ss
);
3733 screen_write_nputs(&ctx
, -1, &grid_default_cell
, "%s", str
);
3734 screen_write_stop(&ctx
);
3736 wrapflag
= options_get_number(wp
->window
->options
, "wrap-search");
3737 cis
= window_copy_is_lowercase(str
);
3739 keys
= options_get_number(wp
->window
->options
, "mode-keys");
3743 * Behave according to mode-keys. If it is emacs, search forward
3744 * leaves the cursor after the match. If it is vi, the cursor
3745 * remains at the beginning of the match, regardless of
3746 * direction, which means that we need to start the next search
3747 * after the term the cursor is currently on when searching
3750 if (keys
== MODEKEY_VI
) {
3751 if (data
->searchmark
!= NULL
)
3752 window_copy_move_after_search_mark(data
, &fx
,
3756 * When there are no search marks, start the
3757 * search after the current cursor position.
3759 window_copy_move_right(s
, &fx
, &fy
, wrapflag
);
3762 endline
= gd
->hsize
+ gd
->sy
- 1;
3765 window_copy_move_left(s
, &fx
, &fy
, wrapflag
);
3769 found
= window_copy_search_jump(wme
, gd
, ss
.grid
, fx
, fy
, endline
, cis
,
3770 wrapflag
, direction
, regex
);
3772 window_copy_search_marks(wme
, &ss
, regex
, visible_only
);
3774 fy
= screen_hsize(data
->backing
) - data
->oy
+ data
->cy
;
3777 * When searching forward, if the cursor is not at the beginning
3778 * of the mark, search again.
3781 window_copy_search_mark_at(data
, fx
, fy
, &at
) == 0 &&
3783 data
->searchmark
!= NULL
&&
3784 data
->searchmark
[at
] == data
->searchmark
[at
- 1]) {
3785 window_copy_move_after_search_mark(data
, &fx
, &fy
,
3787 window_copy_search_jump(wme
, gd
, ss
.grid
, fx
,
3788 fy
, endline
, cis
, wrapflag
, direction
,
3791 fy
= screen_hsize(data
->backing
) - data
->oy
+ data
->cy
;
3796 * When in Emacs mode, position the cursor just after
3799 if (keys
== MODEKEY_EMACS
) {
3800 window_copy_move_after_search_mark(data
, &fx
,
3803 data
->cy
= fy
- screen_hsize(data
->backing
) +
3809 * When searching backward, position the cursor at the
3810 * beginning of the mark.
3812 if (window_copy_search_mark_at(data
, fx
, fy
,
3814 while (window_copy_search_mark_at(data
, fx
, fy
,
3816 data
->searchmark
!= NULL
&&
3817 data
->searchmark
[at
] ==
3818 data
->searchmark
[start
]) {
3821 screen_hsize(data
->backing
) +
3826 window_copy_move_left(s
, &fx
, &fy
, 0);
3831 window_copy_redraw_screen(wme
);
3838 window_copy_visible_lines(struct window_copy_mode_data
*data
, u_int
*start
,
3841 struct grid
*gd
= data
->backing
->grid
;
3842 const struct grid_line
*gl
;
3844 for (*start
= gd
->hsize
- data
->oy
; *start
> 0; (*start
)--) {
3845 gl
= grid_peek_line(gd
, (*start
) - 1);
3846 if (~gl
->flags
& GRID_LINE_WRAPPED
)
3849 *end
= gd
->hsize
- data
->oy
+ gd
->sy
;
3853 window_copy_search_mark_at(struct window_copy_mode_data
*data
, u_int px
,
3854 u_int py
, u_int
*at
)
3856 struct screen
*s
= data
->backing
;
3857 struct grid
*gd
= s
->grid
;
3859 if (py
< gd
->hsize
- data
->oy
)
3861 if (py
> gd
->hsize
- data
->oy
+ gd
->sy
- 1)
3863 *at
= ((py
- (gd
->hsize
- data
->oy
)) * gd
->sx
) + px
;
3868 window_copy_search_marks(struct window_mode_entry
*wme
, struct screen
*ssp
,
3869 int regex
, int visible_only
)
3871 struct window_copy_mode_data
*data
= wme
->data
;
3872 struct screen
*s
= data
->backing
, ss
;
3873 struct screen_write_ctx ctx
;
3874 struct grid
*gd
= s
->grid
;
3875 int found
, cis
, stopped
= 0;
3876 int cflags
= REG_EXTENDED
;
3877 u_int px
, py
, i
, b
, nfound
= 0, width
;
3878 u_int ssize
= 1, start
, end
;
3881 uint64_t stop
= 0, tstart
, t
;
3884 width
= screen_write_strlen("%s", data
->searchstr
);
3885 screen_init(&ss
, width
, 1, 0);
3886 screen_write_start(&ctx
, &ss
);
3887 screen_write_nputs(&ctx
, -1, &grid_default_cell
, "%s",
3889 screen_write_stop(&ctx
);
3892 width
= screen_size_x(ssp
);
3894 cis
= window_copy_is_lowercase(data
->searchstr
);
3897 sbuf
= xmalloc(ssize
);
3899 sbuf
= window_copy_stringify(ssp
->grid
, 0, 0, ssp
->grid
->sx
,
3902 cflags
|= REG_ICASE
;
3903 if (regcomp(®
, sbuf
, cflags
) != 0) {
3909 tstart
= get_timer();
3912 window_copy_visible_lines(data
, &start
, &end
);
3915 end
= gd
->hsize
+ gd
->sy
;
3916 stop
= get_timer() + WINDOW_COPY_SEARCH_ALL_TIMEOUT
;
3920 free(data
->searchmark
);
3921 data
->searchmark
= xcalloc(gd
->sx
, gd
->sy
);
3922 data
->searchgen
= 1;
3924 for (py
= start
; py
< end
; py
++) {
3928 found
= window_copy_search_lr_regex(gd
,
3929 &px
, &width
, py
, px
, gd
->sx
, ®
);
3933 found
= window_copy_search_lr(gd
, ssp
->grid
,
3934 &px
, py
, px
, gd
->sx
, cis
);
3940 if (window_copy_search_mark_at(data
, px
, py
, &b
) == 0) {
3941 if (b
+ width
> gd
->sx
* gd
->sy
)
3942 width
= (gd
->sx
* gd
->sy
) - b
;
3943 for (i
= b
; i
< b
+ width
; i
++) {
3944 if (data
->searchmark
[i
] != 0)
3946 data
->searchmark
[i
] = data
->searchgen
;
3948 if (data
->searchgen
== UCHAR_MAX
)
3949 data
->searchgen
= 1;
3957 if (t
- tstart
> WINDOW_COPY_SEARCH_TIMEOUT
) {
3961 if (stop
!= 0 && t
> stop
) {
3966 if (data
->timeout
) {
3967 window_copy_clear_marks(wme
);
3971 if (stopped
&& stop
!= 0) {
3972 /* Try again but just the visible context. */
3973 window_copy_visible_lines(data
, &start
, &end
);
3978 if (!visible_only
) {
3981 data
->searchcount
= 1000;
3982 else if (nfound
> 100)
3983 data
->searchcount
= 100;
3984 else if (nfound
> 10)
3985 data
->searchcount
= 10;
3987 data
->searchcount
= -1;
3988 data
->searchmore
= 1;
3990 data
->searchcount
= nfound
;
3991 data
->searchmore
= 0;
4004 window_copy_clear_marks(struct window_mode_entry
*wme
)
4006 struct window_copy_mode_data
*data
= wme
->data
;
4008 free(data
->searchmark
);
4009 data
->searchmark
= NULL
;
4013 window_copy_search_up(struct window_mode_entry
*wme
, int regex
)
4015 return (window_copy_search(wme
, 0, regex
));
4019 window_copy_search_down(struct window_mode_entry
*wme
, int regex
)
4021 return (window_copy_search(wme
, 1, regex
));
4025 window_copy_goto_line(struct window_mode_entry
*wme
, const char *linestr
)
4027 struct window_copy_mode_data
*data
= wme
->data
;
4031 lineno
= strtonum(linestr
, -1, INT_MAX
, &errstr
);
4034 if (lineno
< 0 || (u_int
)lineno
> screen_hsize(data
->backing
))
4035 lineno
= screen_hsize(data
->backing
);
4038 window_copy_update_selection(wme
, 1, 0);
4039 window_copy_redraw_screen(wme
);
4043 window_copy_match_start_end(struct window_copy_mode_data
*data
, u_int at
,
4044 u_int
*start
, u_int
*end
)
4046 struct grid
*gd
= data
->backing
->grid
;
4047 u_int last
= (gd
->sy
* gd
->sx
) - 1;
4048 u_char mark
= data
->searchmark
[at
];
4051 while (*start
!= 0 && data
->searchmark
[*start
] == mark
)
4053 if (data
->searchmark
[*start
] != mark
)
4055 while (*end
!= last
&& data
->searchmark
[*end
] == mark
)
4057 if (data
->searchmark
[*end
] != mark
)
4062 window_copy_match_at_cursor(struct window_copy_mode_data
*data
)
4064 struct grid
*gd
= data
->backing
->grid
;
4065 struct grid_cell gc
;
4066 u_int at
, start
, end
, cy
, px
, py
;
4067 u_int sx
= screen_size_x(data
->backing
);
4071 if (data
->searchmark
== NULL
)
4074 cy
= screen_hsize(data
->backing
) - data
->oy
+ data
->cy
;
4075 if (window_copy_search_mark_at(data
, data
->cx
, cy
, &at
) != 0)
4077 if (data
->searchmark
[at
] == 0) {
4078 /* Allow one position after the match. */
4079 if (at
== 0 || data
->searchmark
[--at
] == 0)
4082 window_copy_match_start_end(data
, at
, &start
, &end
);
4085 * Cells will not be set in the marked array unless they are valid text
4086 * and wrapping will be taken care of, so we can just copy.
4088 for (at
= start
; at
<= end
; at
++) {
4090 px
= at
- (py
* sx
);
4092 grid_get_cell(gd
, px
, gd
->hsize
+ py
- data
->oy
, &gc
);
4093 buf
= xrealloc(buf
, len
+ gc
.data
.size
+ 1);
4094 memcpy(buf
+ len
, gc
.data
.data
, gc
.data
.size
);
4095 len
+= gc
.data
.size
;
4103 window_copy_update_style(struct window_mode_entry
*wme
, u_int fx
, u_int fy
,
4104 struct grid_cell
*gc
, const struct grid_cell
*mgc
,
4105 const struct grid_cell
*cgc
, const struct grid_cell
*mkgc
)
4107 struct window_pane
*wp
= wme
->wp
;
4108 struct window_copy_mode_data
*data
= wme
->data
;
4109 u_int mark
, start
, end
, cy
, cursor
, current
;
4110 int inv
= 0, found
= 0;
4113 if (data
->showmark
&& fy
== data
->my
) {
4114 gc
->attr
= mkgc
->attr
;
4127 if (data
->searchmark
== NULL
)
4130 if (window_copy_search_mark_at(data
, fx
, fy
, ¤t
) != 0)
4132 mark
= data
->searchmark
[current
];
4136 cy
= screen_hsize(data
->backing
) - data
->oy
+ data
->cy
;
4137 if (window_copy_search_mark_at(data
, data
->cx
, cy
, &cursor
) == 0) {
4138 keys
= options_get_number(wp
->window
->options
, "mode-keys");
4140 keys
== MODEKEY_EMACS
&&
4141 data
->searchdirection
) {
4142 if (data
->searchmark
[cursor
- 1] == mark
) {
4146 } else if (data
->searchmark
[cursor
] == mark
)
4149 window_copy_match_start_end(data
, cursor
, &start
, &end
);
4150 if (current
>= start
&& current
<= end
) {
4151 gc
->attr
= cgc
->attr
;
4165 gc
->attr
= mgc
->attr
;
4177 window_copy_write_one(struct window_mode_entry
*wme
,
4178 struct screen_write_ctx
*ctx
, u_int py
, u_int fy
, u_int nx
,
4179 const struct grid_cell
*mgc
, const struct grid_cell
*cgc
,
4180 const struct grid_cell
*mkgc
)
4182 struct window_copy_mode_data
*data
= wme
->data
;
4183 struct grid
*gd
= data
->backing
->grid
;
4184 struct grid_cell gc
;
4187 screen_write_cursormove(ctx
, 0, py
, 0);
4188 for (fx
= 0; fx
< nx
; fx
++) {
4189 grid_get_cell(gd
, fx
, fy
, &gc
);
4190 if (fx
+ gc
.data
.width
<= nx
) {
4191 window_copy_update_style(wme
, fx
, fy
, &gc
, mgc
, cgc
,
4193 screen_write_cell(ctx
, &gc
);
4199 window_copy_write_line(struct window_mode_entry
*wme
,
4200 struct screen_write_ctx
*ctx
, u_int py
)
4202 struct window_pane
*wp
= wme
->wp
;
4203 struct window_copy_mode_data
*data
= wme
->data
;
4204 struct screen
*s
= &data
->screen
;
4205 struct options
*oo
= wp
->window
->options
;
4206 struct grid_line
*gl
;
4207 struct grid_cell gc
, mgc
, cgc
, mkgc
;
4208 char hdr
[512], tmp
[256], *t
;
4210 u_int hsize
= screen_hsize(data
->backing
);
4212 style_apply(&gc
, oo
, "mode-style", NULL
);
4213 gc
.flags
|= GRID_FLAG_NOPALETTE
;
4214 style_apply(&mgc
, oo
, "copy-mode-match-style", NULL
);
4215 mgc
.flags
|= GRID_FLAG_NOPALETTE
;
4216 style_apply(&cgc
, oo
, "copy-mode-current-match-style", NULL
);
4217 cgc
.flags
|= GRID_FLAG_NOPALETTE
;
4218 style_apply(&mkgc
, oo
, "copy-mode-mark-style", NULL
);
4219 mkgc
.flags
|= GRID_FLAG_NOPALETTE
;
4221 if (py
== 0 && s
->rupper
< s
->rlower
&& !data
->hide_position
) {
4222 gl
= grid_get_line(data
->backing
->grid
, hsize
- data
->oy
);
4224 xsnprintf(tmp
, sizeof tmp
, "[%u/%u]", data
->oy
, hsize
);
4226 t
= format_pretty_time(gl
->time
, 1);
4227 xsnprintf(tmp
, sizeof tmp
, "%s [%u/%u]", t
, data
->oy
,
4232 if (data
->searchmark
== NULL
) {
4233 if (data
->timeout
) {
4234 size
= xsnprintf(hdr
, sizeof hdr
,
4235 "(timed out) %s", tmp
);
4237 size
= xsnprintf(hdr
, sizeof hdr
, "%s", tmp
);
4239 if (data
->searchcount
== -1)
4240 size
= xsnprintf(hdr
, sizeof hdr
, "%s", tmp
);
4242 size
= xsnprintf(hdr
, sizeof hdr
,
4243 "(%d%s results) %s", data
->searchcount
,
4244 data
->searchmore
? "+" : "", tmp
);
4247 if (size
> screen_size_x(s
))
4248 size
= screen_size_x(s
);
4249 screen_write_cursormove(ctx
, screen_size_x(s
) - size
, 0, 0);
4250 screen_write_puts(ctx
, &gc
, "%s", hdr
);
4254 if (size
< screen_size_x(s
)) {
4255 window_copy_write_one(wme
, ctx
, py
, hsize
- data
->oy
+ py
,
4256 screen_size_x(s
) - size
, &mgc
, &cgc
, &mkgc
);
4259 if (py
== data
->cy
&& data
->cx
== screen_size_x(s
)) {
4260 screen_write_cursormove(ctx
, screen_size_x(s
) - 1, py
, 0);
4261 screen_write_putc(ctx
, &grid_default_cell
, '$');
4266 window_copy_write_lines(struct window_mode_entry
*wme
,
4267 struct screen_write_ctx
*ctx
, u_int py
, u_int ny
)
4271 for (yy
= py
; yy
< py
+ ny
; yy
++)
4272 window_copy_write_line(wme
, ctx
, py
);
4276 window_copy_redraw_selection(struct window_mode_entry
*wme
, u_int old_y
)
4278 struct window_copy_mode_data
*data
= wme
->data
;
4279 struct grid
*gd
= data
->backing
->grid
;
4280 u_int new_y
, start
, end
;
4283 if (old_y
<= new_y
) {
4292 * In word selection mode the first word on the line below the cursor
4293 * might be selected, so add this line to the redraw area.
4295 if (data
->selflag
== SEL_WORD
) {
4296 /* Last grid line in data coordinates. */
4297 if (end
< gd
->sy
+ data
->oy
- 1)
4300 window_copy_redraw_lines(wme
, start
, end
- start
+ 1);
4304 window_copy_redraw_lines(struct window_mode_entry
*wme
, u_int py
, u_int ny
)
4306 struct window_pane
*wp
= wme
->wp
;
4307 struct window_copy_mode_data
*data
= wme
->data
;
4308 struct screen_write_ctx ctx
;
4311 screen_write_start_pane(&ctx
, wp
, NULL
);
4312 for (i
= py
; i
< py
+ ny
; i
++)
4313 window_copy_write_line(wme
, &ctx
, i
);
4314 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
, 0);
4315 screen_write_stop(&ctx
);
4319 window_copy_redraw_screen(struct window_mode_entry
*wme
)
4321 struct window_copy_mode_data
*data
= wme
->data
;
4323 window_copy_redraw_lines(wme
, 0, screen_size_y(&data
->screen
));
4327 window_copy_synchronize_cursor_end(struct window_mode_entry
*wme
, int begin
,
4330 struct window_copy_mode_data
*data
= wme
->data
;
4334 yy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
4335 switch (data
->selflag
) {
4340 if (data
->dy
> yy
|| (data
->dy
== yy
&& data
->dx
> xx
)) {
4341 /* Right to left selection. */
4342 window_copy_cursor_previous_word_pos(wme
,
4343 data
->separators
, &xx
, &yy
);
4346 /* Reset the end. */
4347 data
->endselx
= data
->endselrx
;
4348 data
->endsely
= data
->endselry
;
4350 /* Left to right selection. */
4351 if (xx
>= window_copy_find_length(wme
, yy
) ||
4352 !window_copy_in_set(wme
, xx
+ 1, yy
, WHITESPACE
)) {
4353 window_copy_cursor_next_word_end_pos(wme
,
4354 data
->separators
, &xx
, &yy
);
4357 /* Reset the start. */
4358 data
->selx
= data
->selrx
;
4359 data
->sely
= data
->selry
;
4366 if (data
->dy
> yy
) {
4367 /* Right to left selection. */
4371 /* Reset the end. */
4372 data
->endselx
= data
->endselrx
;
4373 data
->endsely
= data
->endselry
;
4375 /* Left to right selection. */
4376 if (yy
< data
->endselry
)
4377 yy
= data
->endselry
;
4378 xx
= window_copy_find_length(wme
, yy
);
4380 /* Reset the start. */
4381 data
->selx
= data
->selrx
;
4382 data
->sely
= data
->selry
;
4398 window_copy_synchronize_cursor(struct window_mode_entry
*wme
, int no_reset
)
4400 struct window_copy_mode_data
*data
= wme
->data
;
4402 switch (data
->cursordrag
) {
4403 case CURSORDRAG_ENDSEL
:
4404 window_copy_synchronize_cursor_end(wme
, 0, no_reset
);
4406 case CURSORDRAG_SEL
:
4407 window_copy_synchronize_cursor_end(wme
, 1, no_reset
);
4409 case CURSORDRAG_NONE
:
4415 window_copy_update_cursor(struct window_mode_entry
*wme
, u_int cx
, u_int cy
)
4417 struct window_pane
*wp
= wme
->wp
;
4418 struct window_copy_mode_data
*data
= wme
->data
;
4419 struct screen
*s
= &data
->screen
;
4420 struct screen_write_ctx ctx
;
4421 u_int old_cx
, old_cy
;
4423 old_cx
= data
->cx
; old_cy
= data
->cy
;
4424 data
->cx
= cx
; data
->cy
= cy
;
4425 if (old_cx
== screen_size_x(s
))
4426 window_copy_redraw_lines(wme
, old_cy
, 1);
4427 if (data
->cx
== screen_size_x(s
))
4428 window_copy_redraw_lines(wme
, data
->cy
, 1);
4430 screen_write_start_pane(&ctx
, wp
, NULL
);
4431 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
, 0);
4432 screen_write_stop(&ctx
);
4437 window_copy_start_selection(struct window_mode_entry
*wme
)
4439 struct window_copy_mode_data
*data
= wme
->data
;
4441 data
->selx
= data
->cx
;
4442 data
->sely
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
4444 data
->endselx
= data
->selx
;
4445 data
->endsely
= data
->sely
;
4447 data
->cursordrag
= CURSORDRAG_ENDSEL
;
4449 window_copy_set_selection(wme
, 1, 0);
4453 window_copy_adjust_selection(struct window_mode_entry
*wme
, u_int
*selx
,
4456 struct window_copy_mode_data
*data
= wme
->data
;
4457 struct screen
*s
= &data
->screen
;
4464 ty
= screen_hsize(data
->backing
) - data
->oy
;
4466 relpos
= WINDOW_COPY_REL_POS_ABOVE
;
4467 if (!data
->rectflag
)
4470 } else if (sy
> ty
+ screen_size_y(s
) - 1) {
4471 relpos
= WINDOW_COPY_REL_POS_BELOW
;
4472 if (!data
->rectflag
)
4473 sx
= screen_size_x(s
) - 1;
4474 sy
= screen_size_y(s
) - 1;
4476 relpos
= WINDOW_COPY_REL_POS_ON_SCREEN
;
4486 window_copy_update_selection(struct window_mode_entry
*wme
, int may_redraw
,
4489 struct window_copy_mode_data
*data
= wme
->data
;
4490 struct screen
*s
= &data
->screen
;
4492 if (s
->sel
== NULL
&& data
->lineflag
== LINE_SEL_NONE
)
4494 return (window_copy_set_selection(wme
, may_redraw
, no_reset
));
4498 window_copy_set_selection(struct window_mode_entry
*wme
, int may_redraw
,
4501 struct window_pane
*wp
= wme
->wp
;
4502 struct window_copy_mode_data
*data
= wme
->data
;
4503 struct screen
*s
= &data
->screen
;
4504 struct options
*oo
= wp
->window
->options
;
4505 struct grid_cell gc
;
4506 u_int sx
, sy
, cy
, endsx
, endsy
;
4507 int startrelpos
, endrelpos
;
4509 window_copy_synchronize_cursor(wme
, no_reset
);
4511 /* Adjust the selection. */
4514 startrelpos
= window_copy_adjust_selection(wme
, &sx
, &sy
);
4516 /* Adjust the end of selection. */
4517 endsx
= data
->endselx
;
4518 endsy
= data
->endsely
;
4519 endrelpos
= window_copy_adjust_selection(wme
, &endsx
, &endsy
);
4521 /* Selection is outside of the current screen */
4522 if (startrelpos
== endrelpos
&&
4523 startrelpos
!= WINDOW_COPY_REL_POS_ON_SCREEN
) {
4524 screen_hide_selection(s
);
4528 /* Set colours and selection. */
4529 style_apply(&gc
, oo
, "mode-style", NULL
);
4530 gc
.flags
|= GRID_FLAG_NOPALETTE
;
4531 screen_set_selection(s
, sx
, sy
, endsx
, endsy
, data
->rectflag
,
4532 data
->modekeys
, &gc
);
4534 if (data
->rectflag
&& may_redraw
) {
4536 * Can't rely on the caller to redraw the right lines for
4537 * rectangle selection - find the highest line and the number
4538 * of lines, and redraw just past that in both directions
4541 if (data
->cursordrag
== CURSORDRAG_ENDSEL
) {
4543 window_copy_redraw_lines(wme
, sy
, cy
- sy
+ 1);
4545 window_copy_redraw_lines(wme
, cy
, sy
- cy
+ 1);
4548 window_copy_redraw_lines(wme
, endsy
,
4551 window_copy_redraw_lines(wme
, cy
,
4561 window_copy_get_selection(struct window_mode_entry
*wme
, size_t *len
)
4563 struct window_pane
*wp
= wme
->wp
;
4564 struct window_copy_mode_data
*data
= wme
->data
;
4565 struct screen
*s
= &data
->screen
;
4568 u_int i
, xx
, yy
, sx
, sy
, ex
, ey
, ey_last
;
4569 u_int firstsx
, lastex
, restex
, restsx
, selx
;
4572 if (data
->screen
.sel
== NULL
&& data
->lineflag
== LINE_SEL_NONE
) {
4573 buf
= window_copy_match_at_cursor(data
);
4587 * The selection extends from selx,sely to (adjusted) cx,cy on
4591 /* Find start and end. */
4594 if (yy
< data
->sely
|| (yy
== data
->sely
&& xx
< data
->selx
)) {
4596 ex
= data
->selx
; ey
= data
->sely
;
4598 sx
= data
->selx
; sy
= data
->sely
;
4602 /* Trim ex to end of line. */
4603 ey_last
= window_copy_find_length(wme
, ey
);
4608 * Deal with rectangle-copy if necessary; four situations: start of
4609 * first line (firstsx), end of last line (lastex), start (restsx) and
4610 * end (restex) of all other lines.
4612 xx
= screen_size_x(s
);
4615 * Behave according to mode-keys. If it is emacs, copy like emacs,
4616 * keeping the top-left-most character, and dropping the
4617 * bottom-right-most, regardless of copy direction. If it is vi, also
4618 * keep bottom-right-most character.
4620 keys
= options_get_number(wp
->window
->options
, "mode-keys");
4621 if (data
->rectflag
) {
4623 * Need to ignore the column with the cursor in it, which for
4624 * rectangular copy means knowing which side the cursor is on.
4626 if (data
->cursordrag
== CURSORDRAG_ENDSEL
)
4629 selx
= data
->endselx
;
4630 if (selx
< data
->cx
) {
4631 /* Selection start is on the left. */
4632 if (keys
== MODEKEY_EMACS
) {
4637 lastex
= data
->cx
+ 1;
4638 restex
= data
->cx
+ 1;
4643 /* Cursor is on the left. */
4650 if (keys
== MODEKEY_EMACS
)
4659 /* Copy the lines. */
4660 for (i
= sy
; i
<= ey
; i
++) {
4661 window_copy_copy_line(wme
, &buf
, &off
, i
,
4662 (i
== sy
? firstsx
: restsx
),
4663 (i
== ey
? lastex
: restex
));
4666 /* Don't bother if no data. */
4672 /* Remove final \n (unless at end in vi mode). */
4673 if (keys
== MODEKEY_EMACS
|| lastex
<= ey_last
) {
4674 if (~grid_get_line(data
->backing
->grid
, ey
)->flags
&
4675 GRID_LINE_WRAPPED
|| lastex
!= ey_last
)
4683 window_copy_copy_buffer(struct window_mode_entry
*wme
, const char *prefix
,
4684 void *buf
, size_t len
)
4686 struct window_pane
*wp
= wme
->wp
;
4687 struct screen_write_ctx ctx
;
4689 if (options_get_number(global_options
, "set-clipboard") != 0) {
4690 screen_write_start_pane(&ctx
, wp
, NULL
);
4691 screen_write_setselection(&ctx
, "", buf
, len
);
4692 screen_write_stop(&ctx
);
4693 notify_pane("pane-set-clipboard", wp
);
4696 paste_add(prefix
, buf
, len
);
4700 window_copy_pipe_run(struct window_mode_entry
*wme
, struct session
*s
,
4701 const char *cmd
, size_t *len
)
4706 buf
= window_copy_get_selection(wme
, len
);
4707 if (cmd
== NULL
|| *cmd
== '\0')
4708 cmd
= options_get_string(global_options
, "copy-command");
4709 if (cmd
!= NULL
&& *cmd
!= '\0') {
4710 job
= job_run(cmd
, 0, NULL
, NULL
, s
, NULL
, NULL
, NULL
, NULL
,
4711 NULL
, JOB_NOWAIT
, -1, -1);
4712 bufferevent_write(job_get_event(job
), buf
, *len
);
4718 window_copy_pipe(struct window_mode_entry
*wme
, struct session
*s
,
4723 window_copy_pipe_run(wme
, s
, cmd
, &len
);
4727 window_copy_copy_pipe(struct window_mode_entry
*wme
, struct session
*s
,
4728 const char *prefix
, const char *cmd
)
4733 buf
= window_copy_pipe_run(wme
, s
, cmd
, &len
);
4735 window_copy_copy_buffer(wme
, prefix
, buf
, len
);
4739 window_copy_copy_selection(struct window_mode_entry
*wme
, const char *prefix
)
4744 buf
= window_copy_get_selection(wme
, &len
);
4746 window_copy_copy_buffer(wme
, prefix
, buf
, len
);
4750 window_copy_append_selection(struct window_mode_entry
*wme
)
4752 struct window_pane
*wp
= wme
->wp
;
4754 struct paste_buffer
*pb
;
4755 const char *bufdata
, *bufname
= NULL
;
4756 size_t len
, bufsize
;
4757 struct screen_write_ctx ctx
;
4759 buf
= window_copy_get_selection(wme
, &len
);
4763 if (options_get_number(global_options
, "set-clipboard") != 0) {
4764 screen_write_start_pane(&ctx
, wp
, NULL
);
4765 screen_write_setselection(&ctx
, "", buf
, len
);
4766 screen_write_stop(&ctx
);
4767 notify_pane("pane-set-clipboard", wp
);
4770 pb
= paste_get_top(&bufname
);
4772 bufdata
= paste_buffer_data(pb
, &bufsize
);
4773 buf
= xrealloc(buf
, len
+ bufsize
);
4774 memmove(buf
+ bufsize
, buf
, len
);
4775 memcpy(buf
, bufdata
, bufsize
);
4778 if (paste_set(buf
, len
, bufname
, NULL
) != 0)
4783 window_copy_copy_line(struct window_mode_entry
*wme
, char **buf
, size_t *off
,
4784 u_int sy
, u_int sx
, u_int ex
)
4786 struct window_copy_mode_data
*data
= wme
->data
;
4787 struct grid
*gd
= data
->backing
->grid
;
4788 struct grid_cell gc
;
4789 struct grid_line
*gl
;
4790 struct utf8_data ud
;
4791 u_int i
, xx
, wrapped
= 0;
4798 * Work out if the line was wrapped at the screen edge and all of it is
4801 gl
= grid_get_line(gd
, sy
);
4802 if (gl
->flags
& GRID_LINE_WRAPPED
&& gl
->cellsize
<= gd
->sx
)
4805 /* If the line was wrapped, don't strip spaces (use the full length). */
4809 xx
= window_copy_find_length(wme
, sy
);
4816 for (i
= sx
; i
< ex
; i
++) {
4817 grid_get_cell(gd
, i
, sy
, &gc
);
4818 if (gc
.flags
& GRID_FLAG_PADDING
)
4820 utf8_copy(&ud
, &gc
.data
);
4821 if (ud
.size
== 1 && (gc
.attr
& GRID_ATTR_CHARSET
)) {
4822 s
= tty_acs_get(NULL
, ud
.data
[0]);
4823 if (s
!= NULL
&& strlen(s
) <= sizeof ud
.data
) {
4824 ud
.size
= strlen(s
);
4825 memcpy(ud
.data
, s
, ud
.size
);
4829 *buf
= xrealloc(*buf
, (*off
) + ud
.size
);
4830 memcpy(*buf
+ *off
, ud
.data
, ud
.size
);
4835 /* Only add a newline if the line wasn't wrapped. */
4836 if (!wrapped
|| ex
!= xx
) {
4837 *buf
= xrealloc(*buf
, (*off
) + 1);
4838 (*buf
)[(*off
)++] = '\n';
4843 window_copy_clear_selection(struct window_mode_entry
*wme
)
4845 struct window_copy_mode_data
*data
= wme
->data
;
4848 screen_clear_selection(&data
->screen
);
4850 data
->cursordrag
= CURSORDRAG_NONE
;
4851 data
->lineflag
= LINE_SEL_NONE
;
4852 data
->selflag
= SEL_CHAR
;
4854 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
4855 px
= window_copy_find_length(wme
, py
);
4857 window_copy_update_cursor(wme
, px
, data
->cy
);
4861 window_copy_in_set(struct window_mode_entry
*wme
, u_int px
, u_int py
,
4864 struct window_copy_mode_data
*data
= wme
->data
;
4865 struct grid_cell gc
;
4867 grid_get_cell(data
->backing
->grid
, px
, py
, &gc
);
4868 if (gc
.flags
& GRID_FLAG_PADDING
)
4870 return (utf8_cstrhas(set
, &gc
.data
));
4874 window_copy_find_length(struct window_mode_entry
*wme
, u_int py
)
4876 struct window_copy_mode_data
*data
= wme
->data
;
4878 return (grid_line_length(data
->backing
->grid
, py
));
4882 window_copy_cursor_start_of_line(struct window_mode_entry
*wme
)
4884 struct window_copy_mode_data
*data
= wme
->data
;
4885 struct screen
*back_s
= data
->backing
;
4886 struct grid_reader gr
;
4887 u_int px
, py
, oldy
, hsize
;
4890 hsize
= screen_hsize(back_s
);
4891 py
= hsize
+ data
->cy
- data
->oy
;
4894 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
4895 grid_reader_cursor_start_of_line(&gr
, 1);
4896 grid_reader_get_cursor(&gr
, &px
, &py
);
4897 window_copy_acquire_cursor_up(wme
, hsize
, data
->oy
, oldy
, px
, py
);
4901 window_copy_cursor_back_to_indentation(struct window_mode_entry
*wme
)
4903 struct window_copy_mode_data
*data
= wme
->data
;
4904 struct screen
*back_s
= data
->backing
;
4905 struct grid_reader gr
;
4906 u_int px
, py
, oldy
, hsize
;
4909 hsize
= screen_hsize(back_s
);
4910 py
= hsize
+ data
->cy
- data
->oy
;
4913 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
4914 grid_reader_cursor_back_to_indentation(&gr
);
4915 grid_reader_get_cursor(&gr
, &px
, &py
);
4916 window_copy_acquire_cursor_up(wme
, hsize
, data
->oy
, oldy
, px
, py
);
4920 window_copy_cursor_end_of_line(struct window_mode_entry
*wme
)
4922 struct window_copy_mode_data
*data
= wme
->data
;
4923 struct screen
*back_s
= data
->backing
;
4924 struct grid_reader gr
;
4925 u_int px
, py
, oldy
, hsize
;
4928 hsize
= screen_hsize(back_s
);
4929 py
= hsize
+ data
->cy
- data
->oy
;
4932 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
4933 if (data
->screen
.sel
!= NULL
&& data
->rectflag
)
4934 grid_reader_cursor_end_of_line(&gr
, 1, 1);
4936 grid_reader_cursor_end_of_line(&gr
, 1, 0);
4937 grid_reader_get_cursor(&gr
, &px
, &py
);
4938 window_copy_acquire_cursor_down(wme
, hsize
, screen_size_y(back_s
),
4939 data
->oy
, oldy
, px
, py
, 0);
4943 window_copy_other_end(struct window_mode_entry
*wme
)
4945 struct window_copy_mode_data
*data
= wme
->data
;
4946 struct screen
*s
= &data
->screen
;
4947 u_int selx
, sely
, cy
, yy
, hsize
;
4949 if (s
->sel
== NULL
&& data
->lineflag
== LINE_SEL_NONE
)
4952 if (data
->lineflag
== LINE_SEL_LEFT_RIGHT
)
4953 data
->lineflag
= LINE_SEL_RIGHT_LEFT
;
4954 else if (data
->lineflag
== LINE_SEL_RIGHT_LEFT
)
4955 data
->lineflag
= LINE_SEL_LEFT_RIGHT
;
4957 switch (data
->cursordrag
) {
4958 case CURSORDRAG_NONE
:
4959 case CURSORDRAG_SEL
:
4960 data
->cursordrag
= CURSORDRAG_ENDSEL
;
4962 case CURSORDRAG_ENDSEL
:
4963 data
->cursordrag
= CURSORDRAG_SEL
;
4967 selx
= data
->endselx
;
4968 sely
= data
->endsely
;
4969 if (data
->cursordrag
== CURSORDRAG_SEL
) {
4975 yy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
4979 hsize
= screen_hsize(data
->backing
);
4980 if (sely
< hsize
- data
->oy
) { /* above */
4981 data
->oy
= hsize
- sely
;
4983 } else if (sely
> hsize
- data
->oy
+ screen_size_y(s
)) { /* below */
4984 data
->oy
= hsize
- sely
+ screen_size_y(s
) - 1;
4985 data
->cy
= screen_size_y(s
) - 1;
4987 data
->cy
= cy
+ sely
- yy
;
4989 window_copy_update_selection(wme
, 1, 1);
4990 window_copy_redraw_screen(wme
);
4994 window_copy_cursor_left(struct window_mode_entry
*wme
)
4996 struct window_copy_mode_data
*data
= wme
->data
;
4997 struct screen
*back_s
= data
->backing
;
4998 struct grid_reader gr
;
4999 u_int px
, py
, oldy
, hsize
;
5002 hsize
= screen_hsize(back_s
);
5003 py
= hsize
+ data
->cy
- data
->oy
;
5006 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
5007 grid_reader_cursor_left(&gr
, 1);
5008 grid_reader_get_cursor(&gr
, &px
, &py
);
5009 window_copy_acquire_cursor_up(wme
, hsize
, data
->oy
, oldy
, px
, py
);
5013 window_copy_cursor_right(struct window_mode_entry
*wme
, int all
)
5015 struct window_copy_mode_data
*data
= wme
->data
;
5016 struct screen
*back_s
= data
->backing
;
5017 struct grid_reader gr
;
5018 u_int px
, py
, oldy
, hsize
;
5021 hsize
= screen_hsize(back_s
);
5022 py
= hsize
+ data
->cy
- data
->oy
;
5025 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
5026 grid_reader_cursor_right(&gr
, 1, all
);
5027 grid_reader_get_cursor(&gr
, &px
, &py
);
5028 window_copy_acquire_cursor_down(wme
, hsize
, screen_size_y(back_s
),
5029 data
->oy
, oldy
, px
, py
, 0);
5033 window_copy_cursor_up(struct window_mode_entry
*wme
, int scroll_only
)
5035 struct window_copy_mode_data
*data
= wme
->data
;
5036 struct screen
*s
= &data
->screen
;
5037 u_int ox
, oy
, px
, py
;
5040 norectsel
= data
->screen
.sel
== NULL
|| !data
->rectflag
;
5041 oy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
5042 ox
= window_copy_find_length(wme
, oy
);
5043 if (norectsel
&& data
->cx
!= ox
) {
5044 data
->lastcx
= data
->cx
;
5048 if (data
->lineflag
== LINE_SEL_LEFT_RIGHT
&& oy
== data
->sely
)
5049 window_copy_other_end(wme
);
5051 if (scroll_only
|| data
->cy
== 0) {
5053 data
->cx
= data
->lastcx
;
5054 window_copy_scroll_down(wme
, 1);
5056 if (data
->cy
== screen_size_y(s
) - 1)
5057 window_copy_redraw_lines(wme
, data
->cy
, 1);
5059 window_copy_redraw_lines(wme
, data
->cy
, 2);
5063 window_copy_update_cursor(wme
, data
->lastcx
,
5066 window_copy_update_cursor(wme
, data
->cx
, data
->cy
- 1);
5067 if (window_copy_update_selection(wme
, 1, 0)) {
5068 if (data
->cy
== screen_size_y(s
) - 1)
5069 window_copy_redraw_lines(wme
, data
->cy
, 1);
5071 window_copy_redraw_lines(wme
, data
->cy
, 2);
5076 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
5077 px
= window_copy_find_length(wme
, py
);
5078 if ((data
->cx
>= data
->lastsx
&& data
->cx
!= px
) ||
5081 window_copy_update_cursor(wme
, px
, data
->cy
);
5082 if (window_copy_update_selection(wme
, 1, 0))
5083 window_copy_redraw_lines(wme
, data
->cy
, 1);
5087 if (data
->lineflag
== LINE_SEL_LEFT_RIGHT
)
5089 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
5091 px
= screen_size_x(data
->backing
);
5093 px
= window_copy_find_length(wme
, py
);
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);
5098 else if (data
->lineflag
== LINE_SEL_RIGHT_LEFT
)
5100 window_copy_update_cursor(wme
, 0, data
->cy
);
5101 if (window_copy_update_selection(wme
, 1, 0))
5102 window_copy_redraw_lines(wme
, data
->cy
, 1);
5107 window_copy_cursor_down(struct window_mode_entry
*wme
, int scroll_only
)
5109 struct window_copy_mode_data
*data
= wme
->data
;
5110 struct screen
*s
= &data
->screen
;
5111 u_int ox
, oy
, px
, py
;
5114 norectsel
= data
->screen
.sel
== NULL
|| !data
->rectflag
;
5115 oy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
5116 ox
= window_copy_find_length(wme
, oy
);
5117 if (norectsel
&& data
->cx
!= ox
) {
5118 data
->lastcx
= data
->cx
;
5122 if (data
->lineflag
== LINE_SEL_RIGHT_LEFT
&& oy
== data
->endsely
)
5123 window_copy_other_end(wme
);
5125 if (scroll_only
|| data
->cy
== screen_size_y(s
) - 1) {
5127 data
->cx
= data
->lastcx
;
5128 window_copy_scroll_up(wme
, 1);
5129 if (scroll_only
&& data
->cy
> 0)
5130 window_copy_redraw_lines(wme
, data
->cy
- 1, 2);
5133 window_copy_update_cursor(wme
, data
->lastcx
,
5136 window_copy_update_cursor(wme
, data
->cx
, data
->cy
+ 1);
5137 if (window_copy_update_selection(wme
, 1, 0))
5138 window_copy_redraw_lines(wme
, data
->cy
- 1, 2);
5142 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
5143 px
= window_copy_find_length(wme
, py
);
5144 if ((data
->cx
>= data
->lastsx
&& data
->cx
!= px
) ||
5147 window_copy_update_cursor(wme
, px
, data
->cy
);
5148 if (window_copy_update_selection(wme
, 1, 0))
5149 window_copy_redraw_lines(wme
, data
->cy
, 1);
5153 if (data
->lineflag
== LINE_SEL_LEFT_RIGHT
)
5155 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
5157 px
= screen_size_x(data
->backing
);
5159 px
= window_copy_find_length(wme
, py
);
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);
5164 else if (data
->lineflag
== LINE_SEL_RIGHT_LEFT
)
5166 window_copy_update_cursor(wme
, 0, data
->cy
);
5167 if (window_copy_update_selection(wme
, 1, 0))
5168 window_copy_redraw_lines(wme
, data
->cy
, 1);
5173 window_copy_cursor_jump(struct window_mode_entry
*wme
)
5175 struct window_copy_mode_data
*data
= wme
->data
;
5176 struct screen
*back_s
= data
->backing
;
5177 struct grid_reader gr
;
5178 u_int px
, py
, oldy
, hsize
;
5181 hsize
= screen_hsize(back_s
);
5182 py
= hsize
+ data
->cy
- data
->oy
;
5185 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
5186 if (grid_reader_cursor_jump(&gr
, data
->jumpchar
)) {
5187 grid_reader_get_cursor(&gr
, &px
, &py
);
5188 window_copy_acquire_cursor_down(wme
, hsize
,
5189 screen_size_y(back_s
), data
->oy
, oldy
, px
, py
, 0);
5194 window_copy_cursor_jump_back(struct window_mode_entry
*wme
)
5196 struct window_copy_mode_data
*data
= wme
->data
;
5197 struct screen
*back_s
= data
->backing
;
5198 struct grid_reader gr
;
5199 u_int px
, py
, oldy
, hsize
;
5202 hsize
= screen_hsize(back_s
);
5203 py
= hsize
+ data
->cy
- data
->oy
;
5206 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
5207 grid_reader_cursor_left(&gr
, 0);
5208 if (grid_reader_cursor_jump_back(&gr
, data
->jumpchar
)) {
5209 grid_reader_get_cursor(&gr
, &px
, &py
);
5210 window_copy_acquire_cursor_up(wme
, hsize
, data
->oy
, oldy
, px
,
5216 window_copy_cursor_jump_to(struct window_mode_entry
*wme
)
5218 struct window_copy_mode_data
*data
= wme
->data
;
5219 struct screen
*back_s
= data
->backing
;
5220 struct grid_reader gr
;
5221 u_int px
, py
, oldy
, hsize
;
5224 hsize
= screen_hsize(back_s
);
5225 py
= hsize
+ data
->cy
- data
->oy
;
5228 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
5229 if (grid_reader_cursor_jump(&gr
, data
->jumpchar
)) {
5230 grid_reader_cursor_left(&gr
, 1);
5231 grid_reader_get_cursor(&gr
, &px
, &py
);
5232 window_copy_acquire_cursor_down(wme
, hsize
,
5233 screen_size_y(back_s
), data
->oy
, oldy
, px
, py
, 0);
5238 window_copy_cursor_jump_to_back(struct window_mode_entry
*wme
)
5240 struct window_copy_mode_data
*data
= wme
->data
;
5241 struct screen
*back_s
= data
->backing
;
5242 struct grid_reader gr
;
5243 u_int px
, py
, oldy
, hsize
;
5246 hsize
= screen_hsize(back_s
);
5247 py
= hsize
+ data
->cy
- data
->oy
;
5250 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
5251 grid_reader_cursor_left(&gr
, 0);
5252 grid_reader_cursor_left(&gr
, 0);
5253 if (grid_reader_cursor_jump_back(&gr
, data
->jumpchar
)) {
5254 grid_reader_cursor_right(&gr
, 1, 0);
5255 grid_reader_get_cursor(&gr
, &px
, &py
);
5256 window_copy_acquire_cursor_up(wme
, hsize
, data
->oy
, oldy
, px
,
5262 window_copy_cursor_next_word(struct window_mode_entry
*wme
,
5263 const char *separators
)
5265 struct window_copy_mode_data
*data
= wme
->data
;
5266 struct screen
*back_s
= data
->backing
;
5267 struct grid_reader gr
;
5268 u_int px
, py
, oldy
, hsize
;
5271 hsize
= screen_hsize(back_s
);
5272 py
= hsize
+ data
->cy
- data
->oy
;
5275 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
5276 grid_reader_cursor_next_word(&gr
, separators
);
5277 grid_reader_get_cursor(&gr
, &px
, &py
);
5278 window_copy_acquire_cursor_down(wme
, hsize
, screen_size_y(back_s
),
5279 data
->oy
, oldy
, px
, py
, 0);
5282 /* Compute the next place where a word ends. */
5284 window_copy_cursor_next_word_end_pos(struct window_mode_entry
*wme
,
5285 const char *separators
, u_int
*ppx
, u_int
*ppy
)
5287 struct window_pane
*wp
= wme
->wp
;
5288 struct window_copy_mode_data
*data
= wme
->data
;
5289 struct options
*oo
= wp
->window
->options
;
5290 struct screen
*back_s
= data
->backing
;
5291 struct grid_reader gr
;
5292 u_int px
, py
, hsize
;
5295 hsize
= screen_hsize(back_s
);
5296 py
= hsize
+ data
->cy
- data
->oy
;
5298 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
5299 if (options_get_number(oo
, "mode-keys") == MODEKEY_VI
) {
5300 if (!grid_reader_in_set(&gr
, WHITESPACE
))
5301 grid_reader_cursor_right(&gr
, 0, 0);
5302 grid_reader_cursor_next_word_end(&gr
, separators
);
5303 grid_reader_cursor_left(&gr
, 1);
5305 grid_reader_cursor_next_word_end(&gr
, separators
);
5306 grid_reader_get_cursor(&gr
, &px
, &py
);
5311 /* Move to the next place where a word ends. */
5313 window_copy_cursor_next_word_end(struct window_mode_entry
*wme
,
5314 const char *separators
, int no_reset
)
5316 struct window_pane
*wp
= wme
->wp
;
5317 struct window_copy_mode_data
*data
= wme
->data
;
5318 struct options
*oo
= wp
->window
->options
;
5319 struct screen
*back_s
= data
->backing
;
5320 struct grid_reader gr
;
5321 u_int px
, py
, oldy
, hsize
;
5324 hsize
= screen_hsize(back_s
);
5325 py
= hsize
+ data
->cy
- data
->oy
;
5328 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
5329 if (options_get_number(oo
, "mode-keys") == MODEKEY_VI
) {
5330 if (!grid_reader_in_set(&gr
, WHITESPACE
))
5331 grid_reader_cursor_right(&gr
, 0, 0);
5332 grid_reader_cursor_next_word_end(&gr
, separators
);
5333 grid_reader_cursor_left(&gr
, 1);
5335 grid_reader_cursor_next_word_end(&gr
, separators
);
5336 grid_reader_get_cursor(&gr
, &px
, &py
);
5337 window_copy_acquire_cursor_down(wme
, hsize
, screen_size_y(back_s
),
5338 data
->oy
, oldy
, px
, py
, no_reset
);
5341 /* Compute the previous place where a word begins. */
5343 window_copy_cursor_previous_word_pos(struct window_mode_entry
*wme
,
5344 const char *separators
, u_int
*ppx
, u_int
*ppy
)
5346 struct window_copy_mode_data
*data
= wme
->data
;
5347 struct screen
*back_s
= data
->backing
;
5348 struct grid_reader gr
;
5349 u_int px
, py
, hsize
;
5352 hsize
= screen_hsize(back_s
);
5353 py
= hsize
+ data
->cy
- data
->oy
;
5355 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
5356 grid_reader_cursor_previous_word(&gr
, separators
, /* already= */ 0,
5357 /* stop_at_eol= */ 1);
5358 grid_reader_get_cursor(&gr
, &px
, &py
);
5363 /* Move to the previous place where a word begins. */
5365 window_copy_cursor_previous_word(struct window_mode_entry
*wme
,
5366 const char *separators
, int already
)
5368 struct window_copy_mode_data
*data
= wme
->data
;
5369 struct window
*w
= wme
->wp
->window
;
5370 struct screen
*back_s
= data
->backing
;
5371 struct grid_reader gr
;
5372 u_int px
, py
, oldy
, hsize
;
5375 if (options_get_number(w
->options
, "mode-keys") == MODEKEY_EMACS
)
5381 hsize
= screen_hsize(back_s
);
5382 py
= hsize
+ data
->cy
- data
->oy
;
5385 grid_reader_start(&gr
, back_s
->grid
, px
, py
);
5386 grid_reader_cursor_previous_word(&gr
, separators
, already
, stop_at_eol
);
5387 grid_reader_get_cursor(&gr
, &px
, &py
);
5388 window_copy_acquire_cursor_up(wme
, hsize
, data
->oy
, oldy
, px
, py
);
5392 window_copy_cursor_prompt(struct window_mode_entry
*wme
, int direction
)
5394 struct window_copy_mode_data
*data
= wme
->data
;
5395 struct screen
*s
= data
->backing
;
5396 struct grid
*gd
= s
->grid
;
5398 u_int line
= gd
->hsize
- data
->oy
+ data
->cy
;
5401 if (direction
== 0) { /* up */
5406 end_line
= gd
->hsize
+ gd
->sy
- 1;
5409 if (line
== end_line
)
5412 if (line
== end_line
)
5416 if (grid_get_line(gd
, line
)->flags
& GRID_LINE_START_PROMPT
)
5421 if (line
> gd
->hsize
) {
5422 data
->cy
= line
- gd
->hsize
;
5426 data
->oy
= gd
->hsize
- line
;
5429 window_copy_update_selection(wme
, 1, 0);
5430 window_copy_redraw_screen(wme
);
5434 window_copy_scroll_up(struct window_mode_entry
*wme
, u_int ny
)
5436 struct window_pane
*wp
= wme
->wp
;
5437 struct window_copy_mode_data
*data
= wme
->data
;
5438 struct screen
*s
= &data
->screen
;
5439 struct screen_write_ctx ctx
;
5447 if (data
->searchmark
!= NULL
&& !data
->timeout
)
5448 window_copy_search_marks(wme
, NULL
, data
->searchregex
, 1);
5449 window_copy_update_selection(wme
, 0, 0);
5451 screen_write_start_pane(&ctx
, wp
, NULL
);
5452 screen_write_cursormove(&ctx
, 0, 0, 0);
5453 screen_write_deleteline(&ctx
, ny
, 8);
5454 window_copy_write_lines(wme
, &ctx
, screen_size_y(s
) - ny
, ny
);
5455 window_copy_write_line(wme
, &ctx
, 0);
5456 if (screen_size_y(s
) > 1)
5457 window_copy_write_line(wme
, &ctx
, 1);
5458 if (screen_size_y(s
) > 3)
5459 window_copy_write_line(wme
, &ctx
, screen_size_y(s
) - 2);
5460 if (s
->sel
!= NULL
&& screen_size_y(s
) > ny
)
5461 window_copy_write_line(wme
, &ctx
, screen_size_y(s
) - ny
- 1);
5462 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
, 0);
5463 screen_write_stop(&ctx
);
5467 window_copy_scroll_down(struct window_mode_entry
*wme
, u_int ny
)
5469 struct window_pane
*wp
= wme
->wp
;
5470 struct window_copy_mode_data
*data
= wme
->data
;
5471 struct screen
*s
= &data
->screen
;
5472 struct screen_write_ctx ctx
;
5474 if (ny
> screen_hsize(data
->backing
))
5477 if (data
->oy
> screen_hsize(data
->backing
) - ny
)
5478 ny
= screen_hsize(data
->backing
) - data
->oy
;
5483 if (data
->searchmark
!= NULL
&& !data
->timeout
)
5484 window_copy_search_marks(wme
, NULL
, data
->searchregex
, 1);
5485 window_copy_update_selection(wme
, 0, 0);
5487 screen_write_start_pane(&ctx
, wp
, NULL
);
5488 screen_write_cursormove(&ctx
, 0, 0, 0);
5489 screen_write_insertline(&ctx
, ny
, 8);
5490 window_copy_write_lines(wme
, &ctx
, 0, ny
);
5491 if (s
->sel
!= NULL
&& screen_size_y(s
) > ny
)
5492 window_copy_write_line(wme
, &ctx
, ny
);
5493 else if (ny
== 1) /* nuke position */
5494 window_copy_write_line(wme
, &ctx
, 1);
5495 screen_write_cursormove(&ctx
, data
->cx
, data
->cy
, 0);
5496 screen_write_stop(&ctx
);
5500 window_copy_rectangle_set(struct window_mode_entry
*wme
, int rectflag
)
5502 struct window_copy_mode_data
*data
= wme
->data
;
5505 data
->rectflag
= rectflag
;
5507 py
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
5508 px
= window_copy_find_length(wme
, py
);
5510 window_copy_update_cursor(wme
, px
, data
->cy
);
5512 window_copy_update_selection(wme
, 1, 0);
5513 window_copy_redraw_screen(wme
);
5517 window_copy_move_mouse(struct mouse_event
*m
)
5519 struct window_pane
*wp
;
5520 struct window_mode_entry
*wme
;
5523 wp
= cmd_mouse_pane(m
, NULL
, NULL
);
5526 wme
= TAILQ_FIRST(&wp
->modes
);
5529 if (wme
->mode
!= &window_copy_mode
&& wme
->mode
!= &window_view_mode
)
5532 if (cmd_mouse_at(wp
, m
, &x
, &y
, 0) != 0)
5535 window_copy_update_cursor(wme
, x
, y
);
5539 window_copy_start_drag(struct client
*c
, struct mouse_event
*m
)
5541 struct window_pane
*wp
;
5542 struct window_mode_entry
*wme
;
5543 struct window_copy_mode_data
*data
;
5549 wp
= cmd_mouse_pane(m
, NULL
, NULL
);
5552 wme
= TAILQ_FIRST(&wp
->modes
);
5555 if (wme
->mode
!= &window_copy_mode
&& wme
->mode
!= &window_view_mode
)
5558 if (cmd_mouse_at(wp
, m
, &x
, &y
, 1) != 0)
5561 c
->tty
.mouse_drag_update
= window_copy_drag_update
;
5562 c
->tty
.mouse_drag_release
= window_copy_drag_release
;
5565 yg
= screen_hsize(data
->backing
) + y
- data
->oy
;
5566 if (x
< data
->selrx
|| x
> data
->endselrx
|| yg
!= data
->selry
)
5567 data
->selflag
= SEL_CHAR
;
5568 switch (data
->selflag
) {
5570 if (data
->separators
!= NULL
) {
5571 window_copy_update_cursor(wme
, x
, y
);
5572 window_copy_cursor_previous_word_pos(wme
,
5573 data
->separators
, &x
, &y
);
5574 y
-= screen_hsize(data
->backing
) - data
->oy
;
5576 window_copy_update_cursor(wme
, x
, y
);
5579 window_copy_update_cursor(wme
, 0, y
);
5582 window_copy_update_cursor(wme
, x
, y
);
5583 window_copy_start_selection(wme
);
5587 window_copy_redraw_screen(wme
);
5588 window_copy_drag_update(c
, m
);
5592 window_copy_drag_update(struct client
*c
, struct mouse_event
*m
)
5594 struct window_pane
*wp
;
5595 struct window_mode_entry
*wme
;
5596 struct window_copy_mode_data
*data
;
5597 u_int x
, y
, old_cx
, old_cy
;
5598 struct timeval tv
= {
5599 .tv_usec
= WINDOW_COPY_DRAG_REPEAT_TIME
5605 wp
= cmd_mouse_pane(m
, NULL
, NULL
);
5608 wme
= TAILQ_FIRST(&wp
->modes
);
5611 if (wme
->mode
!= &window_copy_mode
&& wme
->mode
!= &window_view_mode
)
5615 evtimer_del(&data
->dragtimer
);
5617 if (cmd_mouse_at(wp
, m
, &x
, &y
, 0) != 0)
5622 window_copy_update_cursor(wme
, x
, y
);
5623 if (window_copy_update_selection(wme
, 1, 0))
5624 window_copy_redraw_selection(wme
, old_cy
);
5625 if (old_cy
!= data
->cy
|| old_cx
== data
->cx
) {
5627 evtimer_add(&data
->dragtimer
, &tv
);
5628 window_copy_cursor_up(wme
, 1);
5629 } else if (y
== screen_size_y(&data
->screen
) - 1) {
5630 evtimer_add(&data
->dragtimer
, &tv
);
5631 window_copy_cursor_down(wme
, 1);
5637 window_copy_drag_release(struct client
*c
, struct mouse_event
*m
)
5639 struct window_pane
*wp
;
5640 struct window_mode_entry
*wme
;
5641 struct window_copy_mode_data
*data
;
5646 wp
= cmd_mouse_pane(m
, NULL
, NULL
);
5649 wme
= TAILQ_FIRST(&wp
->modes
);
5652 if (wme
->mode
!= &window_copy_mode
&& wme
->mode
!= &window_view_mode
)
5656 evtimer_del(&data
->dragtimer
);
5660 window_copy_jump_to_mark(struct window_mode_entry
*wme
)
5662 struct window_copy_mode_data
*data
= wme
->data
;
5666 tmy
= screen_hsize(data
->backing
) + data
->cy
- data
->oy
;
5667 data
->cx
= data
->mx
;
5668 if (data
->my
< screen_hsize(data
->backing
)) {
5670 data
->oy
= screen_hsize(data
->backing
) - data
->my
;
5672 data
->cy
= data
->my
- screen_hsize(data
->backing
);
5678 window_copy_update_selection(wme
, 0, 0);
5679 window_copy_redraw_screen(wme
);
5682 /* Scroll up if the cursor went off the visible screen. */
5684 window_copy_acquire_cursor_up(struct window_mode_entry
*wme
, u_int hsize
,
5685 u_int oy
, u_int oldy
, u_int px
, u_int py
)
5687 u_int cy
, yy
, ny
, nd
;
5700 window_copy_cursor_up(wme
, 1);
5703 window_copy_update_cursor(wme
, px
, cy
);
5704 if (window_copy_update_selection(wme
, 1, 0))
5705 window_copy_redraw_lines(wme
, cy
, nd
);
5708 /* Scroll down if the cursor went off the visible screen. */
5710 window_copy_acquire_cursor_down(struct window_mode_entry
*wme
, u_int hsize
,
5711 u_int sy
, u_int oy
, u_int oldy
, u_int px
, u_int py
, int no_reset
)
5713 u_int cy
, yy
, ny
, nd
;
5715 cy
= py
- hsize
+ oy
;
5726 window_copy_cursor_down(wme
, 1);
5730 window_copy_update_cursor(wme
, px
, yy
);
5732 window_copy_update_cursor(wme
, px
, cy
);
5733 if (window_copy_update_selection(wme
, 1, no_reset
))
5734 window_copy_redraw_lines(wme
, oldy
, nd
);