1 /* $OpenBSD: window-copy.c,v 1.340 2022/09/28 07:59:50 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> 5 * 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. 9 * 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. 17 */ 18 19 #include <sys/types.h> 20 21 #include <ctype.h> 22 #include <regex.h> 23 #include <stdlib.h> 24 #include <string.h> 25 #include <time.h> 26 27 #include "tmux.h" 28 29 struct window_copy_mode_data; 30 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, 49 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, 57 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 *, 66 int); 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, 70 char *, 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, 81 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, 87 u_int); 88 static void window_copy_start_selection(struct window_mode_entry *); 89 static int window_copy_adjust_selection(struct window_mode_entry *, 90 u_int *, u_int *); 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, 93 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 *, 103 const char *); 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, 109 const char *); 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 *, 125 const char *); 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 *, 129 const char *, int); 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 *, 133 const char *, int); 134 static void window_copy_scroll_up(struct window_mode_entry *, u_int); 135 static void window_copy_scroll_down(struct window_mode_entry *, u_int); 136 static void window_copy_rectangle_set(struct window_mode_entry *, int); 137 static void window_copy_move_mouse(struct mouse_event *); 138 static void window_copy_drag_update(struct client *, struct mouse_event *); 139 static void window_copy_drag_release(struct client *, struct mouse_event *); 140 static void window_copy_jump_to_mark(struct window_mode_entry *); 141 static void window_copy_acquire_cursor_up(struct window_mode_entry *, 142 u_int, u_int, u_int, u_int, u_int); 143 static void window_copy_acquire_cursor_down(struct window_mode_entry *, 144 u_int, u_int, u_int, u_int, u_int, u_int, int); 145 146 const struct window_mode window_copy_mode = { 147 .name = "copy-mode", 148 149 .init = window_copy_init, 150 .free = window_copy_free, 151 .resize = window_copy_resize, 152 .key_table = window_copy_key_table, 153 .command = window_copy_command, 154 .formats = window_copy_formats, 155 }; 156 157 const struct window_mode window_view_mode = { 158 .name = "view-mode", 159 160 .init = window_copy_view_init, 161 .free = window_copy_free, 162 .resize = window_copy_resize, 163 .key_table = window_copy_key_table, 164 .command = window_copy_command, 165 .formats = window_copy_formats, 166 }; 167 168 enum { 169 WINDOW_COPY_OFF, 170 WINDOW_COPY_SEARCHUP, 171 WINDOW_COPY_SEARCHDOWN, 172 WINDOW_COPY_JUMPFORWARD, 173 WINDOW_COPY_JUMPBACKWARD, 174 WINDOW_COPY_JUMPTOFORWARD, 175 WINDOW_COPY_JUMPTOBACKWARD, 176 }; 177 178 enum { 179 WINDOW_COPY_REL_POS_ABOVE, 180 WINDOW_COPY_REL_POS_ON_SCREEN, 181 WINDOW_COPY_REL_POS_BELOW, 182 }; 183 184 enum window_copy_cmd_action { 185 WINDOW_COPY_CMD_NOTHING, 186 WINDOW_COPY_CMD_REDRAW, 187 WINDOW_COPY_CMD_CANCEL, 188 }; 189 190 enum window_copy_cmd_clear { 191 WINDOW_COPY_CMD_CLEAR_ALWAYS, 192 WINDOW_COPY_CMD_CLEAR_NEVER, 193 WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 194 }; 195 196 struct window_copy_cmd_state { 197 struct window_mode_entry *wme; 198 struct args *args; 199 struct mouse_event *m; 200 201 struct client *c; 202 struct session *s; 203 struct winlink *wl; 204 }; 205 206 /* 207 * Copy mode's visible screen (the "screen" field) is filled from one of two 208 * sources: the original contents of the pane (used when we actually enter via 209 * the "copy-mode" command, to copy the contents of the current pane), or else 210 * a series of lines containing the output from an output-writing tmux command 211 * (such as any of the "show-*" or "list-*" commands). 212 * 213 * In either case, the full content of the copy-mode grid is pointed at by the 214 * "backing" field, and is copied into "screen" as needed (that is, when 215 * scrolling occurs). When copy-mode is backed by a pane, backing points 216 * directly at that pane's screen structure (&wp->base); when backed by a list 217 * of output-lines from a command, it points at a newly-allocated screen 218 * structure (which is deallocated when the mode ends). 219 */ 220 struct window_copy_mode_data { 221 struct screen screen; 222 223 struct screen *backing; 224 int backing_written; /* backing display started */ 225 struct screen *writing; 226 struct input_ctx *ictx; 227 228 int viewmode; /* view mode entered */ 229 230 u_int oy; /* number of lines scrolled up */ 231 232 u_int selx; /* beginning of selection */ 233 u_int sely; 234 235 u_int endselx; /* end of selection */ 236 u_int endsely; 237 238 enum { 239 CURSORDRAG_NONE, /* selection is independent of cursor */ 240 CURSORDRAG_ENDSEL, /* end is synchronized with cursor */ 241 CURSORDRAG_SEL, /* start is synchronized with cursor */ 242 } cursordrag; 243 244 int modekeys; 245 enum { 246 LINE_SEL_NONE, 247 LINE_SEL_LEFT_RIGHT, 248 LINE_SEL_RIGHT_LEFT, 249 } lineflag; /* line selection mode */ 250 int rectflag; /* in rectangle copy mode? */ 251 int scroll_exit; /* exit on scroll to end? */ 252 int hide_position; /* hide position marker */ 253 254 enum { 255 SEL_CHAR, /* select one char at a time */ 256 SEL_WORD, /* select one word at a time */ 257 SEL_LINE, /* select one line at a time */ 258 } selflag; 259 260 const char *separators; /* word separators */ 261 262 u_int dx; /* drag start position */ 263 u_int dy; 264 265 u_int selrx; /* selection reset positions */ 266 u_int selry; 267 u_int endselrx; 268 u_int endselry; 269 270 u_int cx; 271 u_int cy; 272 273 u_int lastcx; /* position in last line w/ content */ 274 u_int lastsx; /* size of last line w/ content */ 275 276 u_int mx; /* mark position */ 277 u_int my; 278 int showmark; 279 280 int searchtype; 281 int searchdirection; 282 int searchregex; 283 char *searchstr; 284 u_char *searchmark; 285 int searchcount; 286 int searchmore; 287 int searchall; 288 int searchx; 289 int searchy; 290 int searcho; 291 u_char searchgen; 292 293 int timeout; /* search has timed out */ 294 #define WINDOW_COPY_SEARCH_TIMEOUT 10000 295 #define WINDOW_COPY_SEARCH_ALL_TIMEOUT 200 296 297 int jumptype; 298 struct utf8_data *jumpchar; 299 300 struct event dragtimer; 301 #define WINDOW_COPY_DRAG_REPEAT_TIME 50000 302 }; 303 304 static void 305 window_copy_scroll_timer(__unused int fd, __unused short events, void *arg) 306 { 307 struct window_mode_entry *wme = arg; 308 struct window_pane *wp = wme->wp; 309 struct window_copy_mode_data *data = wme->data; 310 struct timeval tv = { 311 .tv_usec = WINDOW_COPY_DRAG_REPEAT_TIME 312 }; 313 314 evtimer_del(&data->dragtimer); 315 316 if (TAILQ_FIRST(&wp->modes) != wme) 317 return; 318 319 if (data->cy == 0) { 320 evtimer_add(&data->dragtimer, &tv); 321 window_copy_cursor_up(wme, 1); 322 } else if (data->cy == screen_size_y(&data->screen) - 1) { 323 evtimer_add(&data->dragtimer, &tv); 324 window_copy_cursor_down(wme, 1); 325 } 326 } 327 328 static struct screen * 329 window_copy_clone_screen(struct screen *src, struct screen *hint, u_int *cx, 330 u_int *cy, int trim) 331 { 332 struct screen *dst; 333 const struct grid_line *gl; 334 u_int sy, wx, wy; 335 int reflow; 336 337 dst = xcalloc(1, sizeof *dst); 338 339 sy = screen_hsize(src) + screen_size_y(src); 340 if (trim) { 341 while (sy > screen_hsize(src)) { 342 gl = grid_peek_line(src->grid, sy - 1); 343 if (gl->cellused != 0) 344 break; 345 sy--; 346 } 347 } 348 log_debug("%s: target screen is %ux%u, source %ux%u", __func__, 349 screen_size_x(src), sy, screen_size_x(hint), 350 screen_hsize(src) + screen_size_y(src)); 351 screen_init(dst, screen_size_x(src), sy, screen_hlimit(src)); 352 353 /* 354 * Ensure history is on for the backing grid so lines are not deleted 355 * during resizing. 356 */ 357 dst->grid->flags |= GRID_HISTORY; 358 grid_duplicate_lines(dst->grid, 0, src->grid, 0, sy); 359 360 dst->grid->sy = sy - screen_hsize(src); 361 dst->grid->hsize = screen_hsize(src); 362 dst->grid->hscrolled = src->grid->hscrolled; 363 if (src->cy > dst->grid->sy - 1) { 364 dst->cx = 0; 365 dst->cy = dst->grid->sy - 1; 366 } else { 367 dst->cx = src->cx; 368 dst->cy = src->cy; 369 } 370 371 if (cx != NULL && cy != NULL) { 372 *cx = dst->cx; 373 *cy = screen_hsize(dst) + dst->cy; 374 reflow = (screen_size_x(hint) != screen_size_x(dst)); 375 } 376 else 377 reflow = 0; 378 if (reflow) 379 grid_wrap_position(dst->grid, *cx, *cy, &wx, &wy); 380 screen_resize_cursor(dst, screen_size_x(hint), screen_size_y(hint), 1, 381 0, 0); 382 if (reflow) 383 grid_unwrap_position(dst->grid, cx, cy, wx, wy); 384 385 return (dst); 386 } 387 388 static struct window_copy_mode_data * 389 window_copy_common_init(struct window_mode_entry *wme) 390 { 391 struct window_pane *wp = wme->wp; 392 struct window_copy_mode_data *data; 393 struct screen *base = &wp->base; 394 395 wme->data = data = xcalloc(1, sizeof *data); 396 397 data->cursordrag = CURSORDRAG_NONE; 398 data->lineflag = LINE_SEL_NONE; 399 data->selflag = SEL_CHAR; 400 401 if (wp->searchstr != NULL) { 402 data->searchtype = WINDOW_COPY_SEARCHUP; 403 data->searchregex = wp->searchregex; 404 data->searchstr = xstrdup(wp->searchstr); 405 } else { 406 data->searchtype = WINDOW_COPY_OFF; 407 data->searchregex = 0; 408 data->searchstr = NULL; 409 } 410 data->searchx = data->searchy = data->searcho = -1; 411 data->searchall = 1; 412 413 data->jumptype = WINDOW_COPY_OFF; 414 data->jumpchar = NULL; 415 416 screen_init(&data->screen, screen_size_x(base), screen_size_y(base), 0); 417 data->modekeys = options_get_number(wp->window->options, "mode-keys"); 418 419 evtimer_set(&data->dragtimer, window_copy_scroll_timer, wme); 420 421 return (data); 422 } 423 424 static struct screen * 425 window_copy_init(struct window_mode_entry *wme, 426 __unused struct cmd_find_state *fs, struct args *args) 427 { 428 struct window_pane *wp = wme->swp; 429 struct window_copy_mode_data *data; 430 struct screen *base = &wp->base; 431 struct screen_write_ctx ctx; 432 u_int i, cx, cy; 433 434 data = window_copy_common_init(wme); 435 data->backing = window_copy_clone_screen(base, &data->screen, &cx, &cy, 436 wme->swp != wme->wp); 437 438 data->cx = cx; 439 if (cy < screen_hsize(data->backing)) { 440 data->cy = 0; 441 data->oy = screen_hsize(data->backing) - cy; 442 } else { 443 data->cy = cy - screen_hsize(data->backing); 444 data->oy = 0; 445 } 446 447 data->scroll_exit = args_has(args, 'e'); 448 data->hide_position = args_has(args, 'H'); 449 450 data->screen.cx = data->cx; 451 data->screen.cy = data->cy; 452 data->mx = data->cx; 453 data->my = screen_hsize(data->backing) + data->cy - data->oy; 454 data->showmark = 0; 455 456 screen_write_start(&ctx, &data->screen); 457 for (i = 0; i < screen_size_y(&data->screen); i++) 458 window_copy_write_line(wme, &ctx, i); 459 screen_write_cursormove(&ctx, data->cx, data->cy, 0); 460 screen_write_stop(&ctx); 461 462 return (&data->screen); 463 } 464 465 static struct screen * 466 window_copy_view_init(struct window_mode_entry *wme, 467 __unused struct cmd_find_state *fs, __unused struct args *args) 468 { 469 struct window_pane *wp = wme->wp; 470 struct window_copy_mode_data *data; 471 struct screen *base = &wp->base; 472 u_int sx = screen_size_x(base); 473 474 data = window_copy_common_init(wme); 475 data->viewmode = 1; 476 477 data->backing = xmalloc(sizeof *data->backing); 478 screen_init(data->backing, sx, screen_size_y(base), UINT_MAX); 479 data->writing = xmalloc(sizeof *data->writing); 480 screen_init(data->writing, sx, screen_size_y(base), 0); 481 data->ictx = input_init(NULL, NULL, NULL); 482 data->mx = data->cx; 483 data->my = screen_hsize(data->backing) + data->cy - data->oy; 484 data->showmark = 0; 485 486 return (&data->screen); 487 } 488 489 static void 490 window_copy_free(struct window_mode_entry *wme) 491 { 492 struct window_copy_mode_data *data = wme->data; 493 494 evtimer_del(&data->dragtimer); 495 496 free(data->searchmark); 497 free(data->searchstr); 498 free(data->jumpchar); 499 500 if (data->writing != NULL) { 501 screen_free(data->writing); 502 free(data->writing); 503 } 504 if (data->ictx != NULL) 505 input_free(data->ictx); 506 screen_free(data->backing); 507 free(data->backing); 508 509 screen_free(&data->screen); 510 free(data); 511 } 512 513 void 514 window_copy_add(struct window_pane *wp, int parse, const char *fmt, ...) 515 { 516 va_list ap; 517 518 va_start(ap, fmt); 519 window_copy_vadd(wp, parse, fmt, ap); 520 va_end(ap); 521 } 522 523 static void 524 window_copy_init_ctx_cb(__unused struct screen_write_ctx *ctx, 525 struct tty_ctx *ttyctx) 526 { 527 memcpy(&ttyctx->defaults, &grid_default_cell, sizeof ttyctx->defaults); 528 ttyctx->palette = NULL; 529 ttyctx->redraw_cb = NULL; 530 ttyctx->set_client_cb = NULL; 531 ttyctx->arg = NULL; 532 } 533 534 void 535 window_copy_vadd(struct window_pane *wp, int parse, const char *fmt, va_list ap) 536 { 537 struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes); 538 struct window_copy_mode_data *data = wme->data; 539 struct screen *backing = data->backing; 540 struct screen *writing = data->writing; 541 struct screen_write_ctx writing_ctx, backing_ctx, ctx; 542 struct grid_cell gc; 543 u_int old_hsize, old_cy; 544 u_int sx = screen_size_x(backing); 545 char *text; 546 547 if (parse) { 548 vasprintf(&text, fmt, ap); 549 screen_write_start(&writing_ctx, writing); 550 screen_write_reset(&writing_ctx); 551 input_parse_screen(data->ictx, writing, window_copy_init_ctx_cb, 552 data, text, strlen(text)); 553 free(text); 554 } 555 556 old_hsize = screen_hsize(data->backing); 557 screen_write_start(&backing_ctx, backing); 558 if (data->backing_written) { 559 /* 560 * On the second or later line, do a CRLF before writing 561 * (so it's on a new line). 562 */ 563 screen_write_carriagereturn(&backing_ctx); 564 screen_write_linefeed(&backing_ctx, 0, 8); 565 } else 566 data->backing_written = 1; 567 old_cy = backing->cy; 568 if (parse) 569 screen_write_fast_copy(&backing_ctx, writing, 0, 0, sx, 1); 570 else { 571 memcpy(&gc, &grid_default_cell, sizeof gc); 572 screen_write_vnputs(&backing_ctx, 0, &gc, fmt, ap); 573 } 574 screen_write_stop(&backing_ctx); 575 576 data->oy += screen_hsize(data->backing) - old_hsize; 577 578 screen_write_start_pane(&ctx, wp, &data->screen); 579 580 /* 581 * If the history has changed, draw the top line. 582 * (If there's any history at all, it has changed.) 583 */ 584 if (screen_hsize(data->backing)) 585 window_copy_redraw_lines(wme, 0, 1); 586 587 /* Write the new lines. */ 588 window_copy_redraw_lines(wme, old_cy, backing->cy - old_cy + 1); 589 590 screen_write_stop(&ctx); 591 } 592 593 void 594 window_copy_pageup(struct window_pane *wp, int half_page) 595 { 596 window_copy_pageup1(TAILQ_FIRST(&wp->modes), half_page); 597 } 598 599 static void 600 window_copy_pageup1(struct window_mode_entry *wme, int half_page) 601 { 602 struct window_copy_mode_data *data = wme->data; 603 struct screen *s = &data->screen; 604 u_int n, ox, oy, px, py; 605 606 oy = screen_hsize(data->backing) + data->cy - data->oy; 607 ox = window_copy_find_length(wme, oy); 608 609 if (data->cx != ox) { 610 data->lastcx = data->cx; 611 data->lastsx = ox; 612 } 613 data->cx = data->lastcx; 614 615 n = 1; 616 if (screen_size_y(s) > 2) { 617 if (half_page) 618 n = screen_size_y(s) / 2; 619 else 620 n = screen_size_y(s) - 2; 621 } 622 623 if (data->oy + n > screen_hsize(data->backing)) { 624 data->oy = screen_hsize(data->backing); 625 if (data->cy < n) 626 data->cy = 0; 627 else 628 data->cy -= n; 629 } else 630 data->oy += n; 631 632 if (data->screen.sel == NULL || !data->rectflag) { 633 py = screen_hsize(data->backing) + data->cy - data->oy; 634 px = window_copy_find_length(wme, py); 635 if ((data->cx >= data->lastsx && data->cx != px) || 636 data->cx > px) 637 window_copy_cursor_end_of_line(wme); 638 } 639 640 if (data->searchmark != NULL && !data->timeout) 641 window_copy_search_marks(wme, NULL, data->searchregex, 1); 642 window_copy_update_selection(wme, 1, 0); 643 window_copy_redraw_screen(wme); 644 } 645 646 static int 647 window_copy_pagedown(struct window_mode_entry *wme, int half_page, 648 int scroll_exit) 649 { 650 struct window_copy_mode_data *data = wme->data; 651 struct screen *s = &data->screen; 652 u_int n, ox, oy, px, py; 653 654 oy = screen_hsize(data->backing) + data->cy - data->oy; 655 ox = window_copy_find_length(wme, oy); 656 657 if (data->cx != ox) { 658 data->lastcx = data->cx; 659 data->lastsx = ox; 660 } 661 data->cx = data->lastcx; 662 663 n = 1; 664 if (screen_size_y(s) > 2) { 665 if (half_page) 666 n = screen_size_y(s) / 2; 667 else 668 n = screen_size_y(s) - 2; 669 } 670 671 if (data->oy < n) { 672 data->oy = 0; 673 if (data->cy + (n - data->oy) >= screen_size_y(data->backing)) 674 data->cy = screen_size_y(data->backing) - 1; 675 else 676 data->cy += n - data->oy; 677 } else 678 data->oy -= n; 679 680 if (data->screen.sel == NULL || !data->rectflag) { 681 py = screen_hsize(data->backing) + data->cy - data->oy; 682 px = window_copy_find_length(wme, py); 683 if ((data->cx >= data->lastsx && data->cx != px) || 684 data->cx > px) 685 window_copy_cursor_end_of_line(wme); 686 } 687 688 if (scroll_exit && data->oy == 0) 689 return (1); 690 if (data->searchmark != NULL && !data->timeout) 691 window_copy_search_marks(wme, NULL, data->searchregex, 1); 692 window_copy_update_selection(wme, 1, 0); 693 window_copy_redraw_screen(wme); 694 return (0); 695 } 696 697 static void 698 window_copy_previous_paragraph(struct window_mode_entry *wme) 699 { 700 struct window_copy_mode_data *data = wme->data; 701 u_int oy; 702 703 oy = screen_hsize(data->backing) + data->cy - data->oy; 704 705 while (oy > 0 && window_copy_find_length(wme, oy) == 0) 706 oy--; 707 708 while (oy > 0 && window_copy_find_length(wme, oy) > 0) 709 oy--; 710 711 window_copy_scroll_to(wme, 0, oy, 0); 712 } 713 714 static void 715 window_copy_next_paragraph(struct window_mode_entry *wme) 716 { 717 struct window_copy_mode_data *data = wme->data; 718 struct screen *s = &data->screen; 719 u_int maxy, ox, oy; 720 721 oy = screen_hsize(data->backing) + data->cy - data->oy; 722 maxy = screen_hsize(data->backing) + screen_size_y(s) - 1; 723 724 while (oy < maxy && window_copy_find_length(wme, oy) == 0) 725 oy++; 726 727 while (oy < maxy && window_copy_find_length(wme, oy) > 0) 728 oy++; 729 730 ox = window_copy_find_length(wme, oy); 731 window_copy_scroll_to(wme, ox, oy, 0); 732 } 733 734 char * 735 window_copy_get_word(struct window_pane *wp, u_int x, u_int y) 736 { 737 struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes); 738 struct window_copy_mode_data *data = wme->data; 739 struct grid *gd = data->screen.grid; 740 741 return (format_grid_word(gd, x, gd->hsize + y)); 742 } 743 744 char * 745 window_copy_get_line(struct window_pane *wp, u_int y) 746 { 747 struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes); 748 struct window_copy_mode_data *data = wme->data; 749 struct grid *gd = data->screen.grid; 750 751 return (format_grid_line(gd, gd->hsize + y)); 752 } 753 754 static void * 755 window_copy_cursor_word_cb(struct format_tree *ft) 756 { 757 struct window_pane *wp = format_get_pane(ft); 758 struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes); 759 struct window_copy_mode_data *data = wme->data; 760 761 return (window_copy_get_word(wp, data->cx, data->cy)); 762 } 763 764 static void * 765 window_copy_cursor_line_cb(struct format_tree *ft) 766 { 767 struct window_pane *wp = format_get_pane(ft); 768 struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes); 769 struct window_copy_mode_data *data = wme->data; 770 771 return (window_copy_get_line(wp, data->cy)); 772 } 773 774 static void * 775 window_copy_search_match_cb(struct format_tree *ft) 776 { 777 struct window_pane *wp = format_get_pane(ft); 778 struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes); 779 struct window_copy_mode_data *data = wme->data; 780 781 return (window_copy_match_at_cursor(data)); 782 } 783 784 static void 785 window_copy_formats(struct window_mode_entry *wme, struct format_tree *ft) 786 { 787 struct window_copy_mode_data *data = wme->data; 788 789 format_add(ft, "scroll_position", "%d", data->oy); 790 format_add(ft, "rectangle_toggle", "%d", data->rectflag); 791 792 format_add(ft, "copy_cursor_x", "%d", data->cx); 793 format_add(ft, "copy_cursor_y", "%d", data->cy); 794 795 format_add(ft, "selection_present", "%d", data->screen.sel != NULL); 796 if (data->screen.sel != NULL) { 797 format_add(ft, "selection_start_x", "%d", data->selx); 798 format_add(ft, "selection_start_y", "%d", data->sely); 799 format_add(ft, "selection_end_x", "%d", data->endselx); 800 format_add(ft, "selection_end_y", "%d", data->endsely); 801 format_add(ft, "selection_active", "%d", 802 data->cursordrag != CURSORDRAG_NONE); 803 } else 804 format_add(ft, "selection_active", "%d", 0); 805 806 format_add(ft, "search_present", "%d", data->searchmark != NULL); 807 format_add_cb(ft, "search_match", window_copy_search_match_cb); 808 809 format_add_cb(ft, "copy_cursor_word", window_copy_cursor_word_cb); 810 format_add_cb(ft, "copy_cursor_line", window_copy_cursor_line_cb); 811 } 812 813 static void 814 window_copy_size_changed(struct window_mode_entry *wme) 815 { 816 struct window_copy_mode_data *data = wme->data; 817 struct screen *s = &data->screen; 818 struct screen_write_ctx ctx; 819 int search = (data->searchmark != NULL); 820 821 window_copy_clear_selection(wme); 822 window_copy_clear_marks(wme); 823 824 screen_write_start(&ctx, s); 825 window_copy_write_lines(wme, &ctx, 0, screen_size_y(s)); 826 screen_write_stop(&ctx); 827 828 if (search && !data->timeout) 829 window_copy_search_marks(wme, NULL, data->searchregex, 0); 830 data->searchx = data->cx; 831 data->searchy = data->cy; 832 data->searcho = data->oy; 833 } 834 835 static void 836 window_copy_resize(struct window_mode_entry *wme, u_int sx, u_int sy) 837 { 838 struct window_copy_mode_data *data = wme->data; 839 struct screen *s = &data->screen; 840 struct grid *gd = data->backing->grid; 841 u_int cx, cy, wx, wy; 842 int reflow; 843 844 screen_resize(s, sx, sy, 0); 845 cx = data->cx; 846 cy = gd->hsize + data->cy - data->oy; 847 reflow = (gd->sx != sx); 848 if (reflow) 849 grid_wrap_position(gd, cx, cy, &wx, &wy); 850 screen_resize_cursor(data->backing, sx, sy, 1, 0, 0); 851 if (reflow) 852 grid_unwrap_position(gd, &cx, &cy, wx, wy); 853 854 data->cx = cx; 855 if (cy < gd->hsize) { 856 data->cy = 0; 857 data->oy = gd->hsize - cy; 858 } else { 859 data->cy = cy - gd->hsize; 860 data->oy = 0; 861 } 862 863 window_copy_size_changed(wme); 864 window_copy_redraw_screen(wme); 865 } 866 867 static const char * 868 window_copy_key_table(struct window_mode_entry *wme) 869 { 870 struct window_pane *wp = wme->wp; 871 872 if (options_get_number(wp->window->options, "mode-keys") == MODEKEY_VI) 873 return ("copy-mode-vi"); 874 return ("copy-mode"); 875 } 876 877 static int 878 window_copy_expand_search_string(struct window_copy_cmd_state *cs) 879 { 880 struct window_mode_entry *wme = cs->wme; 881 struct window_copy_mode_data *data = wme->data; 882 const char *ss = args_string(cs->args, 1); 883 char *expanded; 884 885 if (ss == NULL || *ss == '\0') 886 return (0); 887 888 if (args_has(cs->args, 'F')) { 889 expanded = format_single(NULL, ss, NULL, NULL, NULL, wme->wp); 890 if (*expanded == '\0') { 891 free(expanded); 892 return (0); 893 } 894 free(data->searchstr); 895 data->searchstr = expanded; 896 } else { 897 free(data->searchstr); 898 data->searchstr = xstrdup(ss); 899 } 900 return (1); 901 } 902 903 static enum window_copy_cmd_action 904 window_copy_cmd_append_selection(struct window_copy_cmd_state *cs) 905 { 906 struct window_mode_entry *wme = cs->wme; 907 struct session *s = cs->s; 908 909 if (s != NULL) 910 window_copy_append_selection(wme); 911 window_copy_clear_selection(wme); 912 return (WINDOW_COPY_CMD_REDRAW); 913 } 914 915 static enum window_copy_cmd_action 916 window_copy_cmd_append_selection_and_cancel(struct window_copy_cmd_state *cs) 917 { 918 struct window_mode_entry *wme = cs->wme; 919 struct session *s = cs->s; 920 921 if (s != NULL) 922 window_copy_append_selection(wme); 923 window_copy_clear_selection(wme); 924 return (WINDOW_COPY_CMD_CANCEL); 925 } 926 927 static enum window_copy_cmd_action 928 window_copy_cmd_back_to_indentation(struct window_copy_cmd_state *cs) 929 { 930 struct window_mode_entry *wme = cs->wme; 931 932 window_copy_cursor_back_to_indentation(wme); 933 return (WINDOW_COPY_CMD_NOTHING); 934 } 935 936 static enum window_copy_cmd_action 937 window_copy_cmd_begin_selection(struct window_copy_cmd_state *cs) 938 { 939 struct window_mode_entry *wme = cs->wme; 940 struct client *c = cs->c; 941 struct mouse_event *m = cs->m; 942 struct window_copy_mode_data *data = wme->data; 943 944 if (m != NULL) { 945 window_copy_start_drag(c, m); 946 return (WINDOW_COPY_CMD_NOTHING); 947 } 948 949 data->lineflag = LINE_SEL_NONE; 950 data->selflag = SEL_CHAR; 951 window_copy_start_selection(wme); 952 return (WINDOW_COPY_CMD_REDRAW); 953 } 954 955 static enum window_copy_cmd_action 956 window_copy_cmd_stop_selection(struct window_copy_cmd_state *cs) 957 { 958 struct window_mode_entry *wme = cs->wme; 959 struct window_copy_mode_data *data = wme->data; 960 961 data->cursordrag = CURSORDRAG_NONE; 962 data->lineflag = LINE_SEL_NONE; 963 data->selflag = SEL_CHAR; 964 return (WINDOW_COPY_CMD_NOTHING); 965 } 966 967 static enum window_copy_cmd_action 968 window_copy_cmd_bottom_line(struct window_copy_cmd_state *cs) 969 { 970 struct window_mode_entry *wme = cs->wme; 971 struct window_copy_mode_data *data = wme->data; 972 973 data->cx = 0; 974 data->cy = screen_size_y(&data->screen) - 1; 975 976 window_copy_update_selection(wme, 1, 0); 977 return (WINDOW_COPY_CMD_REDRAW); 978 } 979 980 static enum window_copy_cmd_action 981 window_copy_cmd_cancel(__unused struct window_copy_cmd_state *cs) 982 { 983 return (WINDOW_COPY_CMD_CANCEL); 984 } 985 986 static enum window_copy_cmd_action 987 window_copy_cmd_clear_selection(struct window_copy_cmd_state *cs) 988 { 989 struct window_mode_entry *wme = cs->wme; 990 991 window_copy_clear_selection(wme); 992 return (WINDOW_COPY_CMD_REDRAW); 993 } 994 995 static enum window_copy_cmd_action 996 window_copy_do_copy_end_of_line(struct window_copy_cmd_state *cs, int pipe, 997 int cancel) 998 { 999 struct window_mode_entry *wme = cs->wme; 1000 struct client *c = cs->c; 1001 struct session *s = cs->s; 1002 struct winlink *wl = cs->wl; 1003 struct window_pane *wp = wme->wp; 1004 u_int count = args_count(cs->args); 1005 u_int np = wme->prefix, ocx, ocy, ooy; 1006 struct window_copy_mode_data *data = wme->data; 1007 char *prefix = NULL, *command = NULL; 1008 const char *arg1 = args_string(cs->args, 1); 1009 const char *arg2 = args_string(cs->args, 2); 1010 1011 if (pipe) { 1012 if (count == 3) 1013 prefix = format_single(NULL, arg2, c, s, wl, wp); 1014 if (s != NULL && count > 1 && *arg1 != '\0') 1015 command = format_single(NULL, arg1, c, s, wl, wp); 1016 } else { 1017 if (count == 2) 1018 prefix = format_single(NULL, arg1, c, s, wl, wp); 1019 } 1020 1021 ocx = data->cx; 1022 ocy = data->cy; 1023 ooy = data->oy; 1024 1025 window_copy_start_selection(wme); 1026 for (; np > 1; np--) 1027 window_copy_cursor_down(wme, 0); 1028 window_copy_cursor_end_of_line(wme); 1029 1030 if (s != NULL) { 1031 if (pipe) 1032 window_copy_copy_pipe(wme, s, prefix, command); 1033 else 1034 window_copy_copy_selection(wme, prefix); 1035 1036 if (cancel) { 1037 free(prefix); 1038 free(command); 1039 return (WINDOW_COPY_CMD_CANCEL); 1040 } 1041 } 1042 window_copy_clear_selection(wme); 1043 1044 data->cx = ocx; 1045 data->cy = ocy; 1046 data->oy = ooy; 1047 1048 free(prefix); 1049 free(command); 1050 return (WINDOW_COPY_CMD_REDRAW); 1051 } 1052 1053 static enum window_copy_cmd_action 1054 window_copy_cmd_copy_end_of_line(struct window_copy_cmd_state *cs) 1055 { 1056 return (window_copy_do_copy_end_of_line(cs, 0, 0)); 1057 } 1058 1059 static enum window_copy_cmd_action 1060 window_copy_cmd_copy_end_of_line_and_cancel(struct window_copy_cmd_state *cs) 1061 { 1062 return (window_copy_do_copy_end_of_line(cs, 0, 1)); 1063 } 1064 1065 static enum window_copy_cmd_action 1066 window_copy_cmd_copy_pipe_end_of_line(struct window_copy_cmd_state *cs) 1067 { 1068 return (window_copy_do_copy_end_of_line(cs, 1, 0)); 1069 } 1070 1071 static enum window_copy_cmd_action 1072 window_copy_cmd_copy_pipe_end_of_line_and_cancel( 1073 struct window_copy_cmd_state *cs) 1074 { 1075 return (window_copy_do_copy_end_of_line(cs, 1, 1)); 1076 } 1077 1078 static enum window_copy_cmd_action 1079 window_copy_do_copy_line(struct window_copy_cmd_state *cs, int pipe, int cancel) 1080 { 1081 struct window_mode_entry *wme = cs->wme; 1082 struct client *c = cs->c; 1083 struct session *s = cs->s; 1084 struct winlink *wl = cs->wl; 1085 struct window_pane *wp = wme->wp; 1086 struct window_copy_mode_data *data = wme->data; 1087 u_int count = args_count(cs->args); 1088 u_int np = wme->prefix, ocx, ocy, ooy; 1089 char *prefix = NULL, *command = NULL; 1090 const char *arg1 = args_string(cs->args, 1); 1091 const char *arg2 = args_string(cs->args, 2); 1092 1093 if (pipe) { 1094 if (count == 3) 1095 prefix = format_single(NULL, arg2, c, s, wl, wp); 1096 if (s != NULL && count > 1 && *arg1 != '\0') 1097 command = format_single(NULL, arg1, c, s, wl, wp); 1098 } else { 1099 if (count == 2) 1100 prefix = format_single(NULL, arg1, c, s, wl, wp); 1101 } 1102 1103 ocx = data->cx; 1104 ocy = data->cy; 1105 ooy = data->oy; 1106 1107 data->selflag = SEL_CHAR; 1108 window_copy_cursor_start_of_line(wme); 1109 window_copy_start_selection(wme); 1110 for (; np > 1; np--) 1111 window_copy_cursor_down(wme, 0); 1112 window_copy_cursor_end_of_line(wme); 1113 1114 if (s != NULL) { 1115 if (pipe) 1116 window_copy_copy_pipe(wme, s, prefix, command); 1117 else 1118 window_copy_copy_selection(wme, prefix); 1119 1120 if (cancel) { 1121 free(prefix); 1122 free(command); 1123 return (WINDOW_COPY_CMD_CANCEL); 1124 } 1125 } 1126 window_copy_clear_selection(wme); 1127 1128 data->cx = ocx; 1129 data->cy = ocy; 1130 data->oy = ooy; 1131 1132 free(prefix); 1133 free(command); 1134 return (WINDOW_COPY_CMD_REDRAW); 1135 } 1136 1137 static enum window_copy_cmd_action 1138 window_copy_cmd_copy_line(struct window_copy_cmd_state *cs) 1139 { 1140 return (window_copy_do_copy_line(cs, 0, 0)); 1141 } 1142 1143 static enum window_copy_cmd_action 1144 window_copy_cmd_copy_line_and_cancel(struct window_copy_cmd_state *cs) 1145 { 1146 return (window_copy_do_copy_line(cs, 0, 1)); 1147 } 1148 1149 static enum window_copy_cmd_action 1150 window_copy_cmd_copy_pipe_line(struct window_copy_cmd_state *cs) 1151 { 1152 return (window_copy_do_copy_line(cs, 1, 0)); 1153 } 1154 1155 static enum window_copy_cmd_action 1156 window_copy_cmd_copy_pipe_line_and_cancel(struct window_copy_cmd_state *cs) 1157 { 1158 return (window_copy_do_copy_line(cs, 1, 1)); 1159 } 1160 1161 static enum window_copy_cmd_action 1162 window_copy_cmd_copy_selection_no_clear(struct window_copy_cmd_state *cs) 1163 { 1164 struct window_mode_entry *wme = cs->wme; 1165 struct client *c = cs->c; 1166 struct session *s = cs->s; 1167 struct winlink *wl = cs->wl; 1168 struct window_pane *wp = wme->wp; 1169 char *prefix = NULL; 1170 const char *arg1 = args_string(cs->args, 1); 1171 1172 if (arg1 != NULL) 1173 prefix = format_single(NULL, arg1, c, s, wl, wp); 1174 1175 if (s != NULL) 1176 window_copy_copy_selection(wme, prefix); 1177 1178 free(prefix); 1179 return (WINDOW_COPY_CMD_NOTHING); 1180 } 1181 1182 static enum window_copy_cmd_action 1183 window_copy_cmd_copy_selection(struct window_copy_cmd_state *cs) 1184 { 1185 struct window_mode_entry *wme = cs->wme; 1186 1187 window_copy_cmd_copy_selection_no_clear(cs); 1188 window_copy_clear_selection(wme); 1189 return (WINDOW_COPY_CMD_REDRAW); 1190 } 1191 1192 static enum window_copy_cmd_action 1193 window_copy_cmd_copy_selection_and_cancel(struct window_copy_cmd_state *cs) 1194 { 1195 struct window_mode_entry *wme = cs->wme; 1196 1197 window_copy_cmd_copy_selection_no_clear(cs); 1198 window_copy_clear_selection(wme); 1199 return (WINDOW_COPY_CMD_CANCEL); 1200 } 1201 1202 static enum window_copy_cmd_action 1203 window_copy_cmd_cursor_down(struct window_copy_cmd_state *cs) 1204 { 1205 struct window_mode_entry *wme = cs->wme; 1206 u_int np = wme->prefix; 1207 1208 for (; np != 0; np--) 1209 window_copy_cursor_down(wme, 0); 1210 return (WINDOW_COPY_CMD_NOTHING); 1211 } 1212 1213 static enum window_copy_cmd_action 1214 window_copy_cmd_cursor_down_and_cancel(struct window_copy_cmd_state *cs) 1215 { 1216 struct window_mode_entry *wme = cs->wme; 1217 struct window_copy_mode_data *data = wme->data; 1218 u_int np = wme->prefix, cy; 1219 1220 cy = data->cy; 1221 for (; np != 0; np--) 1222 window_copy_cursor_down(wme, 0); 1223 if (cy == data->cy && data->oy == 0) 1224 return (WINDOW_COPY_CMD_CANCEL); 1225 return (WINDOW_COPY_CMD_NOTHING); 1226 } 1227 1228 static enum window_copy_cmd_action 1229 window_copy_cmd_cursor_left(struct window_copy_cmd_state *cs) 1230 { 1231 struct window_mode_entry *wme = cs->wme; 1232 u_int np = wme->prefix; 1233 1234 for (; np != 0; np--) 1235 window_copy_cursor_left(wme); 1236 return (WINDOW_COPY_CMD_NOTHING); 1237 } 1238 1239 static enum window_copy_cmd_action 1240 window_copy_cmd_cursor_right(struct window_copy_cmd_state *cs) 1241 { 1242 struct window_mode_entry *wme = cs->wme; 1243 struct window_copy_mode_data *data = wme->data; 1244 u_int np = wme->prefix; 1245 1246 for (; np != 0; np--) { 1247 window_copy_cursor_right(wme, data->screen.sel != NULL && 1248 data->rectflag); 1249 } 1250 return (WINDOW_COPY_CMD_NOTHING); 1251 } 1252 1253 /* Scroll line containing the cursor to the given position. */ 1254 static enum window_copy_cmd_action 1255 window_copy_cmd_scroll_to(struct window_copy_cmd_state *cs, u_int to) 1256 { 1257 struct window_mode_entry *wme = cs->wme; 1258 struct window_copy_mode_data *data = wme->data; 1259 u_int oy, delta; 1260 int scroll_up; /* >0 up, <0 down */ 1261 1262 scroll_up = data->cy - to; 1263 delta = abs(scroll_up); 1264 oy = screen_hsize(data->backing) - data->oy; 1265 1266 /* 1267 * oy is the maximum scroll down amount, while data->oy is the maximum 1268 * scroll up amount. 1269 */ 1270 if (scroll_up > 0 && data->oy >= delta) { 1271 window_copy_scroll_up(wme, delta); 1272 data->cy -= delta; 1273 } else if (scroll_up < 0 && oy >= delta) { 1274 window_copy_scroll_down(wme, delta); 1275 data->cy += delta; 1276 } 1277 1278 window_copy_update_selection(wme, 0, 0); 1279 return (WINDOW_COPY_CMD_REDRAW); 1280 } 1281 1282 /* Scroll line containing the cursor to the bottom. */ 1283 static enum window_copy_cmd_action 1284 window_copy_cmd_scroll_bottom(struct window_copy_cmd_state *cs) 1285 { 1286 struct window_copy_mode_data *data = cs->wme->data; 1287 u_int bottom; 1288 1289 bottom = screen_size_y(&data->screen) - 1; 1290 return (window_copy_cmd_scroll_to(cs, bottom)); 1291 } 1292 1293 /* Scroll line containing the cursor to the middle. */ 1294 static enum window_copy_cmd_action 1295 window_copy_cmd_scroll_middle(struct window_copy_cmd_state *cs) 1296 { 1297 struct window_copy_mode_data *data = cs->wme->data; 1298 u_int mid_value; 1299 1300 mid_value = (screen_size_y(&data->screen) - 1) / 2; 1301 return (window_copy_cmd_scroll_to(cs, mid_value)); 1302 } 1303 1304 /* Scroll line containing the cursor to the top. */ 1305 static enum window_copy_cmd_action 1306 window_copy_cmd_scroll_top(struct window_copy_cmd_state *cs) 1307 { 1308 return (window_copy_cmd_scroll_to(cs, 0)); 1309 } 1310 1311 static enum window_copy_cmd_action 1312 window_copy_cmd_cursor_up(struct window_copy_cmd_state *cs) 1313 { 1314 struct window_mode_entry *wme = cs->wme; 1315 u_int np = wme->prefix; 1316 1317 for (; np != 0; np--) 1318 window_copy_cursor_up(wme, 0); 1319 return (WINDOW_COPY_CMD_NOTHING); 1320 } 1321 1322 static enum window_copy_cmd_action 1323 window_copy_cmd_end_of_line(struct window_copy_cmd_state *cs) 1324 { 1325 struct window_mode_entry *wme = cs->wme; 1326 1327 window_copy_cursor_end_of_line(wme); 1328 return (WINDOW_COPY_CMD_NOTHING); 1329 } 1330 1331 static enum window_copy_cmd_action 1332 window_copy_cmd_halfpage_down(struct window_copy_cmd_state *cs) 1333 { 1334 struct window_mode_entry *wme = cs->wme; 1335 struct window_copy_mode_data *data = wme->data; 1336 u_int np = wme->prefix; 1337 1338 for (; np != 0; np--) { 1339 if (window_copy_pagedown(wme, 1, data->scroll_exit)) 1340 return (WINDOW_COPY_CMD_CANCEL); 1341 } 1342 return (WINDOW_COPY_CMD_NOTHING); 1343 } 1344 1345 static enum window_copy_cmd_action 1346 window_copy_cmd_halfpage_down_and_cancel(struct window_copy_cmd_state *cs) 1347 { 1348 1349 struct window_mode_entry *wme = cs->wme; 1350 u_int np = wme->prefix; 1351 1352 for (; np != 0; np--) { 1353 if (window_copy_pagedown(wme, 1, 1)) 1354 return (WINDOW_COPY_CMD_CANCEL); 1355 } 1356 return (WINDOW_COPY_CMD_NOTHING); 1357 } 1358 1359 static enum window_copy_cmd_action 1360 window_copy_cmd_halfpage_up(struct window_copy_cmd_state *cs) 1361 { 1362 struct window_mode_entry *wme = cs->wme; 1363 u_int np = wme->prefix; 1364 1365 for (; np != 0; np--) 1366 window_copy_pageup1(wme, 1); 1367 return (WINDOW_COPY_CMD_NOTHING); 1368 } 1369 1370 static enum window_copy_cmd_action 1371 window_copy_cmd_toggle_position(struct window_copy_cmd_state *cs) 1372 { 1373 struct window_mode_entry *wme = cs->wme; 1374 struct window_copy_mode_data *data = wme->data; 1375 1376 data->hide_position = !data->hide_position; 1377 return (WINDOW_COPY_CMD_REDRAW); 1378 } 1379 1380 static enum window_copy_cmd_action 1381 window_copy_cmd_history_bottom(struct window_copy_cmd_state *cs) 1382 { 1383 struct window_mode_entry *wme = cs->wme; 1384 struct window_copy_mode_data *data = wme->data; 1385 struct screen *s = data->backing; 1386 u_int oy; 1387 1388 oy = screen_hsize(s) + data->cy - data->oy; 1389 if (data->lineflag == LINE_SEL_RIGHT_LEFT && oy == data->endsely) 1390 window_copy_other_end(wme); 1391 1392 data->cy = screen_size_y(&data->screen) - 1; 1393 data->cx = window_copy_find_length(wme, screen_hsize(s) + data->cy); 1394 data->oy = 0; 1395 1396 if (data->searchmark != NULL && !data->timeout) 1397 window_copy_search_marks(wme, NULL, data->searchregex, 1); 1398 window_copy_update_selection(wme, 1, 0); 1399 return (WINDOW_COPY_CMD_REDRAW); 1400 } 1401 1402 static enum window_copy_cmd_action 1403 window_copy_cmd_history_top(struct window_copy_cmd_state *cs) 1404 { 1405 struct window_mode_entry *wme = cs->wme; 1406 struct window_copy_mode_data *data = wme->data; 1407 u_int oy; 1408 1409 oy = screen_hsize(data->backing) + data->cy - data->oy; 1410 if (data->lineflag == LINE_SEL_LEFT_RIGHT && oy == data->sely) 1411 window_copy_other_end(wme); 1412 1413 data->cy = 0; 1414 data->cx = 0; 1415 data->oy = screen_hsize(data->backing); 1416 1417 if (data->searchmark != NULL && !data->timeout) 1418 window_copy_search_marks(wme, NULL, data->searchregex, 1); 1419 window_copy_update_selection(wme, 1, 0); 1420 return (WINDOW_COPY_CMD_REDRAW); 1421 } 1422 1423 static enum window_copy_cmd_action 1424 window_copy_cmd_jump_again(struct window_copy_cmd_state *cs) 1425 { 1426 struct window_mode_entry *wme = cs->wme; 1427 struct window_copy_mode_data *data = wme->data; 1428 u_int np = wme->prefix; 1429 1430 switch (data->jumptype) { 1431 case WINDOW_COPY_JUMPFORWARD: 1432 for (; np != 0; np--) 1433 window_copy_cursor_jump(wme); 1434 break; 1435 case WINDOW_COPY_JUMPBACKWARD: 1436 for (; np != 0; np--) 1437 window_copy_cursor_jump_back(wme); 1438 break; 1439 case WINDOW_COPY_JUMPTOFORWARD: 1440 for (; np != 0; np--) 1441 window_copy_cursor_jump_to(wme); 1442 break; 1443 case WINDOW_COPY_JUMPTOBACKWARD: 1444 for (; np != 0; np--) 1445 window_copy_cursor_jump_to_back(wme); 1446 break; 1447 } 1448 return (WINDOW_COPY_CMD_NOTHING); 1449 } 1450 1451 static enum window_copy_cmd_action 1452 window_copy_cmd_jump_reverse(struct window_copy_cmd_state *cs) 1453 { 1454 struct window_mode_entry *wme = cs->wme; 1455 struct window_copy_mode_data *data = wme->data; 1456 u_int np = wme->prefix; 1457 1458 switch (data->jumptype) { 1459 case WINDOW_COPY_JUMPFORWARD: 1460 for (; np != 0; np--) 1461 window_copy_cursor_jump_back(wme); 1462 break; 1463 case WINDOW_COPY_JUMPBACKWARD: 1464 for (; np != 0; np--) 1465 window_copy_cursor_jump(wme); 1466 break; 1467 case WINDOW_COPY_JUMPTOFORWARD: 1468 for (; np != 0; np--) 1469 window_copy_cursor_jump_to_back(wme); 1470 break; 1471 case WINDOW_COPY_JUMPTOBACKWARD: 1472 for (; np != 0; np--) 1473 window_copy_cursor_jump_to(wme); 1474 break; 1475 } 1476 return (WINDOW_COPY_CMD_NOTHING); 1477 } 1478 1479 static enum window_copy_cmd_action 1480 window_copy_cmd_middle_line(struct window_copy_cmd_state *cs) 1481 { 1482 struct window_mode_entry *wme = cs->wme; 1483 struct window_copy_mode_data *data = wme->data; 1484 1485 data->cx = 0; 1486 data->cy = (screen_size_y(&data->screen) - 1) / 2; 1487 1488 window_copy_update_selection(wme, 1, 0); 1489 return (WINDOW_COPY_CMD_REDRAW); 1490 } 1491 1492 static enum window_copy_cmd_action 1493 window_copy_cmd_previous_matching_bracket(struct window_copy_cmd_state *cs) 1494 { 1495 struct window_mode_entry *wme = cs->wme; 1496 u_int np = wme->prefix; 1497 struct window_copy_mode_data *data = wme->data; 1498 struct screen *s = data->backing; 1499 char open[] = "{[(", close[] = "}])"; 1500 char tried, found, start, *cp; 1501 u_int px, py, xx, n; 1502 struct grid_cell gc; 1503 int failed; 1504 1505 for (; np != 0; np--) { 1506 /* Get cursor position and line length. */ 1507 px = data->cx; 1508 py = screen_hsize(s) + data->cy - data->oy; 1509 xx = window_copy_find_length(wme, py); 1510 if (xx == 0) 1511 break; 1512 1513 /* 1514 * Get the current character. If not on a bracket, try the 1515 * previous. If still not, then behave like previous-word. 1516 */ 1517 tried = 0; 1518 retry: 1519 grid_get_cell(s->grid, px, py, &gc); 1520 if (gc.data.size != 1 || (gc.flags & GRID_FLAG_PADDING)) 1521 cp = NULL; 1522 else { 1523 found = *gc.data.data; 1524 cp = strchr(close, found); 1525 } 1526 if (cp == NULL) { 1527 if (data->modekeys == MODEKEY_EMACS) { 1528 if (!tried && px > 0) { 1529 px--; 1530 tried = 1; 1531 goto retry; 1532 } 1533 window_copy_cursor_previous_word(wme, close, 1); 1534 } 1535 continue; 1536 } 1537 start = open[cp - close]; 1538 1539 /* Walk backward until the matching bracket is reached. */ 1540 n = 1; 1541 failed = 0; 1542 do { 1543 if (px == 0) { 1544 if (py == 0) { 1545 failed = 1; 1546 break; 1547 } 1548 do { 1549 py--; 1550 xx = window_copy_find_length(wme, py); 1551 } while (xx == 0 && py > 0); 1552 if (xx == 0 && py == 0) { 1553 failed = 1; 1554 break; 1555 } 1556 px = xx - 1; 1557 } else 1558 px--; 1559 1560 grid_get_cell(s->grid, px, py, &gc); 1561 if (gc.data.size == 1 && 1562 (~gc.flags & GRID_FLAG_PADDING)) { 1563 if (*gc.data.data == found) 1564 n++; 1565 else if (*gc.data.data == start) 1566 n--; 1567 } 1568 } while (n != 0); 1569 1570 /* Move the cursor to the found location if any. */ 1571 if (!failed) 1572 window_copy_scroll_to(wme, px, py, 0); 1573 } 1574 1575 return (WINDOW_COPY_CMD_NOTHING); 1576 } 1577 1578 static enum window_copy_cmd_action 1579 window_copy_cmd_next_matching_bracket(struct window_copy_cmd_state *cs) 1580 { 1581 struct window_mode_entry *wme = cs->wme; 1582 u_int np = wme->prefix; 1583 struct window_copy_mode_data *data = wme->data; 1584 struct screen *s = data->backing; 1585 char open[] = "{[(", close[] = "}])"; 1586 char tried, found, end, *cp; 1587 u_int px, py, xx, yy, sx, sy, n; 1588 struct grid_cell gc; 1589 int failed; 1590 struct grid_line *gl; 1591 1592 for (; np != 0; np--) { 1593 /* Get cursor position and line length. */ 1594 px = data->cx; 1595 py = screen_hsize(s) + data->cy - data->oy; 1596 xx = window_copy_find_length(wme, py); 1597 yy = screen_hsize(s) + screen_size_y(s) - 1; 1598 if (xx == 0) 1599 break; 1600 1601 /* 1602 * Get the current character. If not on a bracket, try the 1603 * next. If still not, then behave like next-word. 1604 */ 1605 tried = 0; 1606 retry: 1607 grid_get_cell(s->grid, px, py, &gc); 1608 if (gc.data.size != 1 || (gc.flags & GRID_FLAG_PADDING)) 1609 cp = NULL; 1610 else { 1611 found = *gc.data.data; 1612 1613 /* 1614 * In vi mode, attempt to move to previous bracket if a 1615 * closing bracket is found first. If this fails, 1616 * return to the original cursor position. 1617 */ 1618 cp = strchr(close, found); 1619 if (cp != NULL && data->modekeys == MODEKEY_VI) { 1620 sx = data->cx; 1621 sy = screen_hsize(s) + data->cy - data->oy; 1622 1623 window_copy_scroll_to(wme, px, py, 0); 1624 window_copy_cmd_previous_matching_bracket(cs); 1625 1626 px = data->cx; 1627 py = screen_hsize(s) + data->cy - data->oy; 1628 grid_get_cell(s->grid, px, py, &gc); 1629 if (gc.data.size == 1 && 1630 (~gc.flags & GRID_FLAG_PADDING) && 1631 strchr(close, *gc.data.data) != NULL) 1632 window_copy_scroll_to(wme, sx, sy, 0); 1633 break; 1634 } 1635 1636 cp = strchr(open, found); 1637 } 1638 if (cp == NULL) { 1639 if (data->modekeys == MODEKEY_EMACS) { 1640 if (!tried && px <= xx) { 1641 px++; 1642 tried = 1; 1643 goto retry; 1644 } 1645 window_copy_cursor_next_word_end(wme, open, 0); 1646 continue; 1647 } 1648 /* For vi, continue searching for bracket until EOL. */ 1649 if (px > xx) { 1650 if (py == yy) 1651 continue; 1652 gl = grid_get_line(s->grid, py); 1653 if (~gl->flags & GRID_LINE_WRAPPED) 1654 continue; 1655 if (gl->cellsize > s->grid->sx) 1656 continue; 1657 px = 0; 1658 py++; 1659 xx = window_copy_find_length(wme, py); 1660 } else 1661 px++; 1662 goto retry; 1663 } 1664 end = close[cp - open]; 1665 1666 /* Walk forward until the matching bracket is reached. */ 1667 n = 1; 1668 failed = 0; 1669 do { 1670 if (px > xx) { 1671 if (py == yy) { 1672 failed = 1; 1673 break; 1674 } 1675 px = 0; 1676 py++; 1677 xx = window_copy_find_length(wme, py); 1678 } else 1679 px++; 1680 1681 grid_get_cell(s->grid, px, py, &gc); 1682 if (gc.data.size == 1 && 1683 (~gc.flags & GRID_FLAG_PADDING)) { 1684 if (*gc.data.data == found) 1685 n++; 1686 else if (*gc.data.data == end) 1687 n--; 1688 } 1689 } while (n != 0); 1690 1691 /* Move the cursor to the found location if any. */ 1692 if (!failed) 1693 window_copy_scroll_to(wme, px, py, 0); 1694 } 1695 1696 return (WINDOW_COPY_CMD_NOTHING); 1697 } 1698 1699 static enum window_copy_cmd_action 1700 window_copy_cmd_next_paragraph(struct window_copy_cmd_state *cs) 1701 { 1702 struct window_mode_entry *wme = cs->wme; 1703 u_int np = wme->prefix; 1704 1705 for (; np != 0; np--) 1706 window_copy_next_paragraph(wme); 1707 return (WINDOW_COPY_CMD_NOTHING); 1708 } 1709 1710 static enum window_copy_cmd_action 1711 window_copy_cmd_next_space(struct window_copy_cmd_state *cs) 1712 { 1713 struct window_mode_entry *wme = cs->wme; 1714 u_int np = wme->prefix; 1715 1716 for (; np != 0; np--) 1717 window_copy_cursor_next_word(wme, ""); 1718 return (WINDOW_COPY_CMD_NOTHING); 1719 } 1720 1721 static enum window_copy_cmd_action 1722 window_copy_cmd_next_space_end(struct window_copy_cmd_state *cs) 1723 { 1724 struct window_mode_entry *wme = cs->wme; 1725 u_int np = wme->prefix; 1726 1727 for (; np != 0; np--) 1728 window_copy_cursor_next_word_end(wme, "", 0); 1729 return (WINDOW_COPY_CMD_NOTHING); 1730 } 1731 1732 static enum window_copy_cmd_action 1733 window_copy_cmd_next_word(struct window_copy_cmd_state *cs) 1734 { 1735 struct window_mode_entry *wme = cs->wme; 1736 u_int np = wme->prefix; 1737 const char *separators; 1738 1739 separators = options_get_string(cs->s->options, "word-separators"); 1740 1741 for (; np != 0; np--) 1742 window_copy_cursor_next_word(wme, separators); 1743 return (WINDOW_COPY_CMD_NOTHING); 1744 } 1745 1746 static enum window_copy_cmd_action 1747 window_copy_cmd_next_word_end(struct window_copy_cmd_state *cs) 1748 { 1749 struct window_mode_entry *wme = cs->wme; 1750 u_int np = wme->prefix; 1751 const char *separators; 1752 1753 separators = options_get_string(cs->s->options, "word-separators"); 1754 1755 for (; np != 0; np--) 1756 window_copy_cursor_next_word_end(wme, separators, 0); 1757 return (WINDOW_COPY_CMD_NOTHING); 1758 } 1759 1760 static enum window_copy_cmd_action 1761 window_copy_cmd_other_end(struct window_copy_cmd_state *cs) 1762 { 1763 struct window_mode_entry *wme = cs->wme; 1764 u_int np = wme->prefix; 1765 struct window_copy_mode_data *data = wme->data; 1766 1767 data->selflag = SEL_CHAR; 1768 if ((np % 2) != 0) 1769 window_copy_other_end(wme); 1770 return (WINDOW_COPY_CMD_NOTHING); 1771 } 1772 1773 static enum window_copy_cmd_action 1774 window_copy_cmd_page_down(struct window_copy_cmd_state *cs) 1775 { 1776 struct window_mode_entry *wme = cs->wme; 1777 struct window_copy_mode_data *data = wme->data; 1778 u_int np = wme->prefix; 1779 1780 for (; np != 0; np--) { 1781 if (window_copy_pagedown(wme, 0, data->scroll_exit)) 1782 return (WINDOW_COPY_CMD_CANCEL); 1783 } 1784 return (WINDOW_COPY_CMD_NOTHING); 1785 } 1786 1787 static enum window_copy_cmd_action 1788 window_copy_cmd_page_down_and_cancel(struct window_copy_cmd_state *cs) 1789 { 1790 struct window_mode_entry *wme = cs->wme; 1791 u_int np = wme->prefix; 1792 1793 for (; np != 0; np--) { 1794 if (window_copy_pagedown(wme, 0, 1)) 1795 return (WINDOW_COPY_CMD_CANCEL); 1796 } 1797 return (WINDOW_COPY_CMD_NOTHING); 1798 } 1799 1800 static enum window_copy_cmd_action 1801 window_copy_cmd_page_up(struct window_copy_cmd_state *cs) 1802 { 1803 struct window_mode_entry *wme = cs->wme; 1804 u_int np = wme->prefix; 1805 1806 for (; np != 0; np--) 1807 window_copy_pageup1(wme, 0); 1808 return (WINDOW_COPY_CMD_NOTHING); 1809 } 1810 1811 static enum window_copy_cmd_action 1812 window_copy_cmd_previous_paragraph(struct window_copy_cmd_state *cs) 1813 { 1814 struct window_mode_entry *wme = cs->wme; 1815 u_int np = wme->prefix; 1816 1817 for (; np != 0; np--) 1818 window_copy_previous_paragraph(wme); 1819 return (WINDOW_COPY_CMD_NOTHING); 1820 } 1821 1822 static enum window_copy_cmd_action 1823 window_copy_cmd_previous_space(struct window_copy_cmd_state *cs) 1824 { 1825 struct window_mode_entry *wme = cs->wme; 1826 u_int np = wme->prefix; 1827 1828 for (; np != 0; np--) 1829 window_copy_cursor_previous_word(wme, "", 1); 1830 return (WINDOW_COPY_CMD_NOTHING); 1831 } 1832 1833 static enum window_copy_cmd_action 1834 window_copy_cmd_previous_word(struct window_copy_cmd_state *cs) 1835 { 1836 struct window_mode_entry *wme = cs->wme; 1837 u_int np = wme->prefix; 1838 const char *separators; 1839 1840 separators = options_get_string(cs->s->options, "word-separators"); 1841 1842 for (; np != 0; np--) 1843 window_copy_cursor_previous_word(wme, separators, 1); 1844 return (WINDOW_COPY_CMD_NOTHING); 1845 } 1846 1847 static enum window_copy_cmd_action 1848 window_copy_cmd_rectangle_on(struct window_copy_cmd_state *cs) 1849 { 1850 struct window_mode_entry *wme = cs->wme; 1851 struct window_copy_mode_data *data = wme->data; 1852 1853 data->lineflag = LINE_SEL_NONE; 1854 window_copy_rectangle_set(wme, 1); 1855 1856 return (WINDOW_COPY_CMD_NOTHING); 1857 } 1858 1859 static enum window_copy_cmd_action 1860 window_copy_cmd_rectangle_off(struct window_copy_cmd_state *cs) 1861 { 1862 struct window_mode_entry *wme = cs->wme; 1863 struct window_copy_mode_data *data = wme->data; 1864 1865 data->lineflag = LINE_SEL_NONE; 1866 window_copy_rectangle_set(wme, 0); 1867 1868 return (WINDOW_COPY_CMD_NOTHING); 1869 } 1870 1871 static enum window_copy_cmd_action 1872 window_copy_cmd_rectangle_toggle(struct window_copy_cmd_state *cs) 1873 { 1874 struct window_mode_entry *wme = cs->wme; 1875 struct window_copy_mode_data *data = wme->data; 1876 1877 data->lineflag = LINE_SEL_NONE; 1878 window_copy_rectangle_set(wme, !data->rectflag); 1879 1880 return (WINDOW_COPY_CMD_NOTHING); 1881 } 1882 1883 static enum window_copy_cmd_action 1884 window_copy_cmd_scroll_down(struct window_copy_cmd_state *cs) 1885 { 1886 struct window_mode_entry *wme = cs->wme; 1887 struct window_copy_mode_data *data = wme->data; 1888 u_int np = wme->prefix; 1889 1890 for (; np != 0; np--) 1891 window_copy_cursor_down(wme, 1); 1892 if (data->scroll_exit && data->oy == 0) 1893 return (WINDOW_COPY_CMD_CANCEL); 1894 return (WINDOW_COPY_CMD_NOTHING); 1895 } 1896 1897 static enum window_copy_cmd_action 1898 window_copy_cmd_scroll_down_and_cancel(struct window_copy_cmd_state *cs) 1899 { 1900 struct window_mode_entry *wme = cs->wme; 1901 struct window_copy_mode_data *data = wme->data; 1902 u_int np = wme->prefix; 1903 1904 for (; np != 0; np--) 1905 window_copy_cursor_down(wme, 1); 1906 if (data->oy == 0) 1907 return (WINDOW_COPY_CMD_CANCEL); 1908 return (WINDOW_COPY_CMD_NOTHING); 1909 } 1910 1911 static enum window_copy_cmd_action 1912 window_copy_cmd_scroll_up(struct window_copy_cmd_state *cs) 1913 { 1914 struct window_mode_entry *wme = cs->wme; 1915 u_int np = wme->prefix; 1916 1917 for (; np != 0; np--) 1918 window_copy_cursor_up(wme, 1); 1919 return (WINDOW_COPY_CMD_NOTHING); 1920 } 1921 1922 static enum window_copy_cmd_action 1923 window_copy_cmd_search_again(struct window_copy_cmd_state *cs) 1924 { 1925 struct window_mode_entry *wme = cs->wme; 1926 struct window_copy_mode_data *data = wme->data; 1927 u_int np = wme->prefix; 1928 1929 if (data->searchtype == WINDOW_COPY_SEARCHUP) { 1930 for (; np != 0; np--) 1931 window_copy_search_up(wme, data->searchregex); 1932 } else if (data->searchtype == WINDOW_COPY_SEARCHDOWN) { 1933 for (; np != 0; np--) 1934 window_copy_search_down(wme, data->searchregex); 1935 } 1936 return (WINDOW_COPY_CMD_NOTHING); 1937 } 1938 1939 static enum window_copy_cmd_action 1940 window_copy_cmd_search_reverse(struct window_copy_cmd_state *cs) 1941 { 1942 struct window_mode_entry *wme = cs->wme; 1943 struct window_copy_mode_data *data = wme->data; 1944 u_int np = wme->prefix; 1945 1946 if (data->searchtype == WINDOW_COPY_SEARCHUP) { 1947 for (; np != 0; np--) 1948 window_copy_search_down(wme, data->searchregex); 1949 } else if (data->searchtype == WINDOW_COPY_SEARCHDOWN) { 1950 for (; np != 0; np--) 1951 window_copy_search_up(wme, data->searchregex); 1952 } 1953 return (WINDOW_COPY_CMD_NOTHING); 1954 } 1955 1956 static enum window_copy_cmd_action 1957 window_copy_cmd_select_line(struct window_copy_cmd_state *cs) 1958 { 1959 struct window_mode_entry *wme = cs->wme; 1960 struct window_copy_mode_data *data = wme->data; 1961 u_int np = wme->prefix; 1962 1963 data->lineflag = LINE_SEL_LEFT_RIGHT; 1964 data->rectflag = 0; 1965 data->selflag = SEL_LINE; 1966 data->dx = data->cx; 1967 data->dy = screen_hsize(data->backing) + data->cy - data->oy; 1968 1969 window_copy_cursor_start_of_line(wme); 1970 data->selrx = data->cx; 1971 data->selry = screen_hsize(data->backing) + data->cy - data->oy; 1972 data->endselry = data->selry; 1973 window_copy_start_selection(wme); 1974 window_copy_cursor_end_of_line(wme); 1975 data->endselry = screen_hsize(data->backing) + data->cy - data->oy; 1976 data->endselrx = window_copy_find_length(wme, data->endselry); 1977 for (; np > 1; np--) { 1978 window_copy_cursor_down(wme, 0); 1979 window_copy_cursor_end_of_line(wme); 1980 } 1981 1982 return (WINDOW_COPY_CMD_REDRAW); 1983 } 1984 1985 static enum window_copy_cmd_action 1986 window_copy_cmd_select_word(struct window_copy_cmd_state *cs) 1987 { 1988 struct window_mode_entry *wme = cs->wme; 1989 struct options *session_options = cs->s->options; 1990 struct window_copy_mode_data *data = wme->data; 1991 u_int px, py, nextx, nexty; 1992 1993 data->lineflag = LINE_SEL_LEFT_RIGHT; 1994 data->rectflag = 0; 1995 data->selflag = SEL_WORD; 1996 data->dx = data->cx; 1997 data->dy = screen_hsize(data->backing) + data->cy - data->oy; 1998 1999 data->separators = options_get_string(session_options, 2000 "word-separators"); 2001 window_copy_cursor_previous_word(wme, data->separators, 0); 2002 px = data->cx; 2003 py = screen_hsize(data->backing) + data->cy - data->oy; 2004 data->selrx = px; 2005 data->selry = py; 2006 window_copy_start_selection(wme); 2007 2008 /* Handle single character words. */ 2009 nextx = px + 1; 2010 nexty = py; 2011 if (grid_get_line(data->backing->grid, nexty)->flags & 2012 GRID_LINE_WRAPPED && nextx > screen_size_x(data->backing) - 1) { 2013 nextx = 0; 2014 nexty++; 2015 } 2016 if (px >= window_copy_find_length(wme, py) || 2017 !window_copy_in_set(wme, nextx, nexty, WHITESPACE)) 2018 window_copy_cursor_next_word_end(wme, data->separators, 1); 2019 else { 2020 window_copy_update_cursor(wme, px, data->cy); 2021 if (window_copy_update_selection(wme, 1, 1)) 2022 window_copy_redraw_lines(wme, data->cy, 1); 2023 } 2024 data->endselrx = data->cx; 2025 data->endselry = screen_hsize(data->backing) + data->cy - data->oy; 2026 if (data->dy > data->endselry) { 2027 data->dy = data->endselry; 2028 data->dx = data->endselrx; 2029 } else if (data->dx > data->endselrx) 2030 data->dx = data->endselrx; 2031 2032 return (WINDOW_COPY_CMD_REDRAW); 2033 } 2034 2035 static enum window_copy_cmd_action 2036 window_copy_cmd_set_mark(struct window_copy_cmd_state *cs) 2037 { 2038 struct window_copy_mode_data *data = cs->wme->data; 2039 2040 data->mx = data->cx; 2041 data->my = screen_hsize(data->backing) + data->cy - data->oy; 2042 data->showmark = 1; 2043 return (WINDOW_COPY_CMD_REDRAW); 2044 } 2045 2046 static enum window_copy_cmd_action 2047 window_copy_cmd_start_of_line(struct window_copy_cmd_state *cs) 2048 { 2049 struct window_mode_entry *wme = cs->wme; 2050 2051 window_copy_cursor_start_of_line(wme); 2052 return (WINDOW_COPY_CMD_NOTHING); 2053 } 2054 2055 static enum window_copy_cmd_action 2056 window_copy_cmd_top_line(struct window_copy_cmd_state *cs) 2057 { 2058 struct window_mode_entry *wme = cs->wme; 2059 struct window_copy_mode_data *data = wme->data; 2060 2061 data->cx = 0; 2062 data->cy = 0; 2063 2064 window_copy_update_selection(wme, 1, 0); 2065 return (WINDOW_COPY_CMD_REDRAW); 2066 } 2067 2068 static enum window_copy_cmd_action 2069 window_copy_cmd_copy_pipe_no_clear(struct window_copy_cmd_state *cs) 2070 { 2071 struct window_mode_entry *wme = cs->wme; 2072 struct client *c = cs->c; 2073 struct session *s = cs->s; 2074 struct winlink *wl = cs->wl; 2075 struct window_pane *wp = wme->wp; 2076 char *command = NULL, *prefix = NULL; 2077 const char *arg1 = args_string(cs->args, 1); 2078 const char *arg2 = args_string(cs->args, 2); 2079 2080 if (arg2 != NULL) 2081 prefix = format_single(NULL, arg2, c, s, wl, wp); 2082 2083 if (s != NULL && arg1 != NULL && *arg1 != '\0') 2084 command = format_single(NULL, arg1, c, s, wl, wp); 2085 window_copy_copy_pipe(wme, s, prefix, command); 2086 free(command); 2087 2088 free(prefix); 2089 return (WINDOW_COPY_CMD_NOTHING); 2090 } 2091 2092 static enum window_copy_cmd_action 2093 window_copy_cmd_copy_pipe(struct window_copy_cmd_state *cs) 2094 { 2095 struct window_mode_entry *wme = cs->wme; 2096 2097 window_copy_cmd_copy_pipe_no_clear(cs); 2098 window_copy_clear_selection(wme); 2099 return (WINDOW_COPY_CMD_REDRAW); 2100 } 2101 2102 static enum window_copy_cmd_action 2103 window_copy_cmd_copy_pipe_and_cancel(struct window_copy_cmd_state *cs) 2104 { 2105 struct window_mode_entry *wme = cs->wme; 2106 2107 window_copy_cmd_copy_pipe_no_clear(cs); 2108 window_copy_clear_selection(wme); 2109 return (WINDOW_COPY_CMD_CANCEL); 2110 } 2111 2112 static enum window_copy_cmd_action 2113 window_copy_cmd_pipe_no_clear(struct window_copy_cmd_state *cs) 2114 { 2115 struct window_mode_entry *wme = cs->wme; 2116 struct client *c = cs->c; 2117 struct session *s = cs->s; 2118 struct winlink *wl = cs->wl; 2119 struct window_pane *wp = wme->wp; 2120 char *command = NULL; 2121 const char *arg1 = args_string(cs->args, 1); 2122 2123 if (s != NULL && arg1 != NULL && *arg1 != '\0') 2124 command = format_single(NULL, arg1, c, s, wl, wp); 2125 window_copy_pipe(wme, s, command); 2126 free(command); 2127 2128 return (WINDOW_COPY_CMD_NOTHING); 2129 } 2130 2131 static enum window_copy_cmd_action 2132 window_copy_cmd_pipe(struct window_copy_cmd_state *cs) 2133 { 2134 struct window_mode_entry *wme = cs->wme; 2135 2136 window_copy_cmd_pipe_no_clear(cs); 2137 window_copy_clear_selection(wme); 2138 return (WINDOW_COPY_CMD_REDRAW); 2139 } 2140 2141 static enum window_copy_cmd_action 2142 window_copy_cmd_pipe_and_cancel(struct window_copy_cmd_state *cs) 2143 { 2144 struct window_mode_entry *wme = cs->wme; 2145 2146 window_copy_cmd_pipe_no_clear(cs); 2147 window_copy_clear_selection(wme); 2148 return (WINDOW_COPY_CMD_CANCEL); 2149 } 2150 2151 static enum window_copy_cmd_action 2152 window_copy_cmd_goto_line(struct window_copy_cmd_state *cs) 2153 { 2154 struct window_mode_entry *wme = cs->wme; 2155 const char *arg1 = args_string(cs->args, 1); 2156 2157 if (*arg1 != '\0') 2158 window_copy_goto_line(wme, arg1); 2159 return (WINDOW_COPY_CMD_NOTHING); 2160 } 2161 2162 static enum window_copy_cmd_action 2163 window_copy_cmd_jump_backward(struct window_copy_cmd_state *cs) 2164 { 2165 struct window_mode_entry *wme = cs->wme; 2166 struct window_copy_mode_data *data = wme->data; 2167 u_int np = wme->prefix; 2168 const char *arg1 = args_string(cs->args, 1); 2169 2170 if (*arg1 != '\0') { 2171 data->jumptype = WINDOW_COPY_JUMPBACKWARD; 2172 free(data->jumpchar); 2173 data->jumpchar = utf8_fromcstr(arg1); 2174 for (; np != 0; np--) 2175 window_copy_cursor_jump_back(wme); 2176 } 2177 return (WINDOW_COPY_CMD_NOTHING); 2178 } 2179 2180 static enum window_copy_cmd_action 2181 window_copy_cmd_jump_forward(struct window_copy_cmd_state *cs) 2182 { 2183 struct window_mode_entry *wme = cs->wme; 2184 struct window_copy_mode_data *data = wme->data; 2185 u_int np = wme->prefix; 2186 const char *arg1 = args_string(cs->args, 1); 2187 2188 if (*arg1 != '\0') { 2189 data->jumptype = WINDOW_COPY_JUMPFORWARD; 2190 free(data->jumpchar); 2191 data->jumpchar = utf8_fromcstr(arg1); 2192 for (; np != 0; np--) 2193 window_copy_cursor_jump(wme); 2194 } 2195 return (WINDOW_COPY_CMD_NOTHING); 2196 } 2197 2198 static enum window_copy_cmd_action 2199 window_copy_cmd_jump_to_backward(struct window_copy_cmd_state *cs) 2200 { 2201 struct window_mode_entry *wme = cs->wme; 2202 struct window_copy_mode_data *data = wme->data; 2203 u_int np = wme->prefix; 2204 const char *arg1 = args_string(cs->args, 1); 2205 2206 if (*arg1 != '\0') { 2207 data->jumptype = WINDOW_COPY_JUMPTOBACKWARD; 2208 free(data->jumpchar); 2209 data->jumpchar = utf8_fromcstr(arg1); 2210 for (; np != 0; np--) 2211 window_copy_cursor_jump_to_back(wme); 2212 } 2213 return (WINDOW_COPY_CMD_NOTHING); 2214 } 2215 2216 static enum window_copy_cmd_action 2217 window_copy_cmd_jump_to_forward(struct window_copy_cmd_state *cs) 2218 { 2219 struct window_mode_entry *wme = cs->wme; 2220 struct window_copy_mode_data *data = wme->data; 2221 u_int np = wme->prefix; 2222 const char *arg1 = args_string(cs->args, 1); 2223 2224 if (*arg1 != '\0') { 2225 data->jumptype = WINDOW_COPY_JUMPTOFORWARD; 2226 free(data->jumpchar); 2227 data->jumpchar = utf8_fromcstr(arg1); 2228 for (; np != 0; np--) 2229 window_copy_cursor_jump_to(wme); 2230 } 2231 return (WINDOW_COPY_CMD_NOTHING); 2232 } 2233 2234 static enum window_copy_cmd_action 2235 window_copy_cmd_jump_to_mark(struct window_copy_cmd_state *cs) 2236 { 2237 struct window_mode_entry *wme = cs->wme; 2238 2239 window_copy_jump_to_mark(wme); 2240 return (WINDOW_COPY_CMD_NOTHING); 2241 } 2242 2243 static enum window_copy_cmd_action 2244 window_copy_cmd_search_backward(struct window_copy_cmd_state *cs) 2245 { 2246 struct window_mode_entry *wme = cs->wme; 2247 struct window_copy_mode_data *data = wme->data; 2248 u_int np = wme->prefix; 2249 2250 if (!window_copy_expand_search_string(cs)) 2251 return (WINDOW_COPY_CMD_NOTHING); 2252 2253 if (data->searchstr != NULL) { 2254 data->searchtype = WINDOW_COPY_SEARCHUP; 2255 data->searchregex = 1; 2256 data->timeout = 0; 2257 for (; np != 0; np--) 2258 window_copy_search_up(wme, 1); 2259 } 2260 return (WINDOW_COPY_CMD_NOTHING); 2261 } 2262 2263 static enum window_copy_cmd_action 2264 window_copy_cmd_search_backward_text(struct window_copy_cmd_state *cs) 2265 { 2266 struct window_mode_entry *wme = cs->wme; 2267 struct window_copy_mode_data *data = wme->data; 2268 u_int np = wme->prefix; 2269 2270 if (!window_copy_expand_search_string(cs)) 2271 return (WINDOW_COPY_CMD_NOTHING); 2272 2273 if (data->searchstr != NULL) { 2274 data->searchtype = WINDOW_COPY_SEARCHUP; 2275 data->searchregex = 0; 2276 data->timeout = 0; 2277 for (; np != 0; np--) 2278 window_copy_search_up(wme, 0); 2279 } 2280 return (WINDOW_COPY_CMD_NOTHING); 2281 } 2282 2283 static enum window_copy_cmd_action 2284 window_copy_cmd_search_forward(struct window_copy_cmd_state *cs) 2285 { 2286 struct window_mode_entry *wme = cs->wme; 2287 struct window_copy_mode_data *data = wme->data; 2288 u_int np = wme->prefix; 2289 2290 if (!window_copy_expand_search_string(cs)) 2291 return (WINDOW_COPY_CMD_NOTHING); 2292 2293 if (data->searchstr != NULL) { 2294 data->searchtype = WINDOW_COPY_SEARCHDOWN; 2295 data->searchregex = 1; 2296 data->timeout = 0; 2297 for (; np != 0; np--) 2298 window_copy_search_down(wme, 1); 2299 } 2300 return (WINDOW_COPY_CMD_NOTHING); 2301 } 2302 2303 static enum window_copy_cmd_action 2304 window_copy_cmd_search_forward_text(struct window_copy_cmd_state *cs) 2305 { 2306 struct window_mode_entry *wme = cs->wme; 2307 struct window_copy_mode_data *data = wme->data; 2308 u_int np = wme->prefix; 2309 2310 if (!window_copy_expand_search_string(cs)) 2311 return (WINDOW_COPY_CMD_NOTHING); 2312 2313 if (data->searchstr != NULL) { 2314 data->searchtype = WINDOW_COPY_SEARCHDOWN; 2315 data->searchregex = 0; 2316 data->timeout = 0; 2317 for (; np != 0; np--) 2318 window_copy_search_down(wme, 0); 2319 } 2320 return (WINDOW_COPY_CMD_NOTHING); 2321 } 2322 2323 static enum window_copy_cmd_action 2324 window_copy_cmd_search_backward_incremental(struct window_copy_cmd_state *cs) 2325 { 2326 struct window_mode_entry *wme = cs->wme; 2327 struct window_copy_mode_data *data = wme->data; 2328 const char *arg1 = args_string(cs->args, 1); 2329 const char *ss = data->searchstr; 2330 char prefix; 2331 enum window_copy_cmd_action action = WINDOW_COPY_CMD_NOTHING; 2332 2333 data->timeout = 0; 2334 2335 log_debug("%s: %s", __func__, arg1); 2336 2337 prefix = *arg1++; 2338 if (data->searchx == -1 || data->searchy == -1) { 2339 data->searchx = data->cx; 2340 data->searchy = data->cy; 2341 data->searcho = data->oy; 2342 } else if (ss != NULL && strcmp(arg1, ss) != 0) { 2343 data->cx = data->searchx; 2344 data->cy = data->searchy; 2345 data->oy = data->searcho; 2346 action = WINDOW_COPY_CMD_REDRAW; 2347 } 2348 if (*arg1 == '\0') { 2349 window_copy_clear_marks(wme); 2350 return (WINDOW_COPY_CMD_REDRAW); 2351 } 2352 switch (prefix) { 2353 case '=': 2354 case '-': 2355 data->searchtype = WINDOW_COPY_SEARCHUP; 2356 data->searchregex = 0; 2357 free(data->searchstr); 2358 data->searchstr = xstrdup(arg1); 2359 if (!window_copy_search_up(wme, 0)) { 2360 window_copy_clear_marks(wme); 2361 return (WINDOW_COPY_CMD_REDRAW); 2362 } 2363 break; 2364 case '+': 2365 data->searchtype = WINDOW_COPY_SEARCHDOWN; 2366 data->searchregex = 0; 2367 free(data->searchstr); 2368 data->searchstr = xstrdup(arg1); 2369 if (!window_copy_search_down(wme, 0)) { 2370 window_copy_clear_marks(wme); 2371 return (WINDOW_COPY_CMD_REDRAW); 2372 } 2373 break; 2374 } 2375 return (action); 2376 } 2377 2378 static enum window_copy_cmd_action 2379 window_copy_cmd_search_forward_incremental(struct window_copy_cmd_state *cs) 2380 { 2381 struct window_mode_entry *wme = cs->wme; 2382 struct window_copy_mode_data *data = wme->data; 2383 const char *arg1 = args_string(cs->args, 1); 2384 const char *ss = data->searchstr; 2385 char prefix; 2386 enum window_copy_cmd_action action = WINDOW_COPY_CMD_NOTHING; 2387 2388 data->timeout = 0; 2389 2390 log_debug("%s: %s", __func__, arg1); 2391 2392 prefix = *arg1++; 2393 if (data->searchx == -1 || data->searchy == -1) { 2394 data->searchx = data->cx; 2395 data->searchy = data->cy; 2396 data->searcho = data->oy; 2397 } else if (ss != NULL && strcmp(arg1, ss) != 0) { 2398 data->cx = data->searchx; 2399 data->cy = data->searchy; 2400 data->oy = data->searcho; 2401 action = WINDOW_COPY_CMD_REDRAW; 2402 } 2403 if (*arg1 == '\0') { 2404 window_copy_clear_marks(wme); 2405 return (WINDOW_COPY_CMD_REDRAW); 2406 } 2407 switch (prefix) { 2408 case '=': 2409 case '+': 2410 data->searchtype = WINDOW_COPY_SEARCHDOWN; 2411 data->searchregex = 0; 2412 free(data->searchstr); 2413 data->searchstr = xstrdup(arg1); 2414 if (!window_copy_search_down(wme, 0)) { 2415 window_copy_clear_marks(wme); 2416 return (WINDOW_COPY_CMD_REDRAW); 2417 } 2418 break; 2419 case '-': 2420 data->searchtype = WINDOW_COPY_SEARCHUP; 2421 data->searchregex = 0; 2422 free(data->searchstr); 2423 data->searchstr = xstrdup(arg1); 2424 if (!window_copy_search_up(wme, 0)) { 2425 window_copy_clear_marks(wme); 2426 return (WINDOW_COPY_CMD_REDRAW); 2427 } 2428 } 2429 return (action); 2430 } 2431 2432 static enum window_copy_cmd_action 2433 window_copy_cmd_refresh_from_pane(struct window_copy_cmd_state *cs) 2434 { 2435 struct window_mode_entry *wme = cs->wme; 2436 struct window_pane *wp = wme->swp; 2437 struct window_copy_mode_data *data = wme->data; 2438 2439 if (data->viewmode) 2440 return (WINDOW_COPY_CMD_NOTHING); 2441 2442 screen_free(data->backing); 2443 free(data->backing); 2444 data->backing = window_copy_clone_screen(&wp->base, &data->screen, NULL, NULL, wme->swp != wme->wp); 2445 2446 window_copy_size_changed(wme); 2447 return (WINDOW_COPY_CMD_REDRAW); 2448 } 2449 2450 static const struct { 2451 const char *command; 2452 u_int minargs; 2453 u_int maxargs; 2454 enum window_copy_cmd_clear clear; 2455 enum window_copy_cmd_action (*f)(struct window_copy_cmd_state *); 2456 } window_copy_cmd_table[] = { 2457 { .command = "append-selection", 2458 .minargs = 0, 2459 .maxargs = 0, 2460 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2461 .f = window_copy_cmd_append_selection 2462 }, 2463 { .command = "append-selection-and-cancel", 2464 .minargs = 0, 2465 .maxargs = 0, 2466 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2467 .f = window_copy_cmd_append_selection_and_cancel 2468 }, 2469 { .command = "back-to-indentation", 2470 .minargs = 0, 2471 .maxargs = 0, 2472 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2473 .f = window_copy_cmd_back_to_indentation 2474 }, 2475 { .command = "begin-selection", 2476 .minargs = 0, 2477 .maxargs = 0, 2478 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2479 .f = window_copy_cmd_begin_selection 2480 }, 2481 { .command = "bottom-line", 2482 .minargs = 0, 2483 .maxargs = 0, 2484 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2485 .f = window_copy_cmd_bottom_line 2486 }, 2487 { .command = "cancel", 2488 .minargs = 0, 2489 .maxargs = 0, 2490 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2491 .f = window_copy_cmd_cancel 2492 }, 2493 { .command = "clear-selection", 2494 .minargs = 0, 2495 .maxargs = 0, 2496 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2497 .f = window_copy_cmd_clear_selection 2498 }, 2499 { .command = "copy-end-of-line", 2500 .minargs = 0, 2501 .maxargs = 1, 2502 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2503 .f = window_copy_cmd_copy_end_of_line 2504 }, 2505 { .command = "copy-end-of-line-and-cancel", 2506 .minargs = 0, 2507 .maxargs = 1, 2508 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2509 .f = window_copy_cmd_copy_end_of_line_and_cancel 2510 }, 2511 { .command = "copy-pipe-end-of-line", 2512 .minargs = 0, 2513 .maxargs = 2, 2514 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2515 .f = window_copy_cmd_copy_pipe_end_of_line 2516 }, 2517 { .command = "copy-pipe-end-of-line-and-cancel", 2518 .minargs = 0, 2519 .maxargs = 2, 2520 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2521 .f = window_copy_cmd_copy_pipe_end_of_line_and_cancel 2522 }, 2523 { .command = "copy-line", 2524 .minargs = 0, 2525 .maxargs = 1, 2526 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2527 .f = window_copy_cmd_copy_line 2528 }, 2529 { .command = "copy-line-and-cancel", 2530 .minargs = 0, 2531 .maxargs = 1, 2532 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2533 .f = window_copy_cmd_copy_line_and_cancel 2534 }, 2535 { .command = "copy-pipe-line", 2536 .minargs = 0, 2537 .maxargs = 2, 2538 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2539 .f = window_copy_cmd_copy_pipe_line 2540 }, 2541 { .command = "copy-pipe-line-and-cancel", 2542 .minargs = 0, 2543 .maxargs = 2, 2544 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2545 .f = window_copy_cmd_copy_pipe_line_and_cancel 2546 }, 2547 { .command = "copy-pipe-no-clear", 2548 .minargs = 0, 2549 .maxargs = 2, 2550 .clear = WINDOW_COPY_CMD_CLEAR_NEVER, 2551 .f = window_copy_cmd_copy_pipe_no_clear 2552 }, 2553 { .command = "copy-pipe", 2554 .minargs = 0, 2555 .maxargs = 2, 2556 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2557 .f = window_copy_cmd_copy_pipe 2558 }, 2559 { .command = "copy-pipe-and-cancel", 2560 .minargs = 0, 2561 .maxargs = 2, 2562 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2563 .f = window_copy_cmd_copy_pipe_and_cancel 2564 }, 2565 { .command = "copy-selection-no-clear", 2566 .minargs = 0, 2567 .maxargs = 1, 2568 .clear = WINDOW_COPY_CMD_CLEAR_NEVER, 2569 .f = window_copy_cmd_copy_selection_no_clear 2570 }, 2571 { .command = "copy-selection", 2572 .minargs = 0, 2573 .maxargs = 1, 2574 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2575 .f = window_copy_cmd_copy_selection 2576 }, 2577 { .command = "copy-selection-and-cancel", 2578 .minargs = 0, 2579 .maxargs = 1, 2580 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2581 .f = window_copy_cmd_copy_selection_and_cancel 2582 }, 2583 { .command = "cursor-down", 2584 .minargs = 0, 2585 .maxargs = 0, 2586 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2587 .f = window_copy_cmd_cursor_down 2588 }, 2589 { .command = "cursor-down-and-cancel", 2590 .minargs = 0, 2591 .maxargs = 0, 2592 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2593 .f = window_copy_cmd_cursor_down_and_cancel 2594 }, 2595 { .command = "cursor-left", 2596 .minargs = 0, 2597 .maxargs = 0, 2598 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2599 .f = window_copy_cmd_cursor_left 2600 }, 2601 { .command = "cursor-right", 2602 .minargs = 0, 2603 .maxargs = 0, 2604 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2605 .f = window_copy_cmd_cursor_right 2606 }, 2607 { .command = "cursor-up", 2608 .minargs = 0, 2609 .maxargs = 0, 2610 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2611 .f = window_copy_cmd_cursor_up 2612 }, 2613 { .command = "end-of-line", 2614 .minargs = 0, 2615 .maxargs = 0, 2616 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2617 .f = window_copy_cmd_end_of_line 2618 }, 2619 { .command = "goto-line", 2620 .minargs = 1, 2621 .maxargs = 1, 2622 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2623 .f = window_copy_cmd_goto_line 2624 }, 2625 { .command = "halfpage-down", 2626 .minargs = 0, 2627 .maxargs = 0, 2628 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2629 .f = window_copy_cmd_halfpage_down 2630 }, 2631 { .command = "halfpage-down-and-cancel", 2632 .minargs = 0, 2633 .maxargs = 0, 2634 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2635 .f = window_copy_cmd_halfpage_down_and_cancel 2636 }, 2637 { .command = "halfpage-up", 2638 .minargs = 0, 2639 .maxargs = 0, 2640 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2641 .f = window_copy_cmd_halfpage_up 2642 }, 2643 { .command = "history-bottom", 2644 .minargs = 0, 2645 .maxargs = 0, 2646 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2647 .f = window_copy_cmd_history_bottom 2648 }, 2649 { .command = "history-top", 2650 .minargs = 0, 2651 .maxargs = 0, 2652 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2653 .f = window_copy_cmd_history_top 2654 }, 2655 { .command = "jump-again", 2656 .minargs = 0, 2657 .maxargs = 0, 2658 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2659 .f = window_copy_cmd_jump_again 2660 }, 2661 { .command = "jump-backward", 2662 .minargs = 1, 2663 .maxargs = 1, 2664 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2665 .f = window_copy_cmd_jump_backward 2666 }, 2667 { .command = "jump-forward", 2668 .minargs = 1, 2669 .maxargs = 1, 2670 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2671 .f = window_copy_cmd_jump_forward 2672 }, 2673 { .command = "jump-reverse", 2674 .minargs = 0, 2675 .maxargs = 0, 2676 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2677 .f = window_copy_cmd_jump_reverse 2678 }, 2679 { .command = "jump-to-backward", 2680 .minargs = 1, 2681 .maxargs = 1, 2682 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2683 .f = window_copy_cmd_jump_to_backward 2684 }, 2685 { .command = "jump-to-forward", 2686 .minargs = 1, 2687 .maxargs = 1, 2688 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2689 .f = window_copy_cmd_jump_to_forward 2690 }, 2691 { .command = "jump-to-mark", 2692 .minargs = 0, 2693 .maxargs = 0, 2694 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2695 .f = window_copy_cmd_jump_to_mark 2696 }, 2697 { .command = "middle-line", 2698 .minargs = 0, 2699 .maxargs = 0, 2700 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2701 .f = window_copy_cmd_middle_line 2702 }, 2703 { .command = "next-matching-bracket", 2704 .minargs = 0, 2705 .maxargs = 0, 2706 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2707 .f = window_copy_cmd_next_matching_bracket 2708 }, 2709 { .command = "next-paragraph", 2710 .minargs = 0, 2711 .maxargs = 0, 2712 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2713 .f = window_copy_cmd_next_paragraph 2714 }, 2715 { .command = "next-space", 2716 .minargs = 0, 2717 .maxargs = 0, 2718 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2719 .f = window_copy_cmd_next_space 2720 }, 2721 { .command = "next-space-end", 2722 .minargs = 0, 2723 .maxargs = 0, 2724 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2725 .f = window_copy_cmd_next_space_end 2726 }, 2727 { .command = "next-word", 2728 .minargs = 0, 2729 .maxargs = 0, 2730 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2731 .f = window_copy_cmd_next_word 2732 }, 2733 { .command = "next-word-end", 2734 .minargs = 0, 2735 .maxargs = 0, 2736 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2737 .f = window_copy_cmd_next_word_end 2738 }, 2739 { .command = "other-end", 2740 .minargs = 0, 2741 .maxargs = 0, 2742 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2743 .f = window_copy_cmd_other_end 2744 }, 2745 { .command = "page-down", 2746 .minargs = 0, 2747 .maxargs = 0, 2748 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2749 .f = window_copy_cmd_page_down 2750 }, 2751 { .command = "page-down-and-cancel", 2752 .minargs = 0, 2753 .maxargs = 0, 2754 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2755 .f = window_copy_cmd_page_down_and_cancel 2756 }, 2757 { .command = "page-up", 2758 .minargs = 0, 2759 .maxargs = 0, 2760 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2761 .f = window_copy_cmd_page_up 2762 }, 2763 { .command = "pipe-no-clear", 2764 .minargs = 0, 2765 .maxargs = 1, 2766 .clear = WINDOW_COPY_CMD_CLEAR_NEVER, 2767 .f = window_copy_cmd_pipe_no_clear 2768 }, 2769 { .command = "pipe", 2770 .minargs = 0, 2771 .maxargs = 1, 2772 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2773 .f = window_copy_cmd_pipe 2774 }, 2775 { .command = "pipe-and-cancel", 2776 .minargs = 0, 2777 .maxargs = 1, 2778 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2779 .f = window_copy_cmd_pipe_and_cancel 2780 }, 2781 { .command = "previous-matching-bracket", 2782 .minargs = 0, 2783 .maxargs = 0, 2784 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2785 .f = window_copy_cmd_previous_matching_bracket 2786 }, 2787 { .command = "previous-paragraph", 2788 .minargs = 0, 2789 .maxargs = 0, 2790 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2791 .f = window_copy_cmd_previous_paragraph 2792 }, 2793 { .command = "previous-space", 2794 .minargs = 0, 2795 .maxargs = 0, 2796 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2797 .f = window_copy_cmd_previous_space 2798 }, 2799 { .command = "previous-word", 2800 .minargs = 0, 2801 .maxargs = 0, 2802 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2803 .f = window_copy_cmd_previous_word 2804 }, 2805 { .command = "rectangle-on", 2806 .minargs = 0, 2807 .maxargs = 0, 2808 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2809 .f = window_copy_cmd_rectangle_on 2810 }, 2811 { .command = "rectangle-off", 2812 .minargs = 0, 2813 .maxargs = 0, 2814 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2815 .f = window_copy_cmd_rectangle_off 2816 }, 2817 { .command = "rectangle-toggle", 2818 .minargs = 0, 2819 .maxargs = 0, 2820 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2821 .f = window_copy_cmd_rectangle_toggle 2822 }, 2823 { .command = "refresh-from-pane", 2824 .minargs = 0, 2825 .maxargs = 0, 2826 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2827 .f = window_copy_cmd_refresh_from_pane 2828 }, 2829 { .command = "scroll-bottom", 2830 .minargs = 0, 2831 .maxargs = 0, 2832 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2833 .f = window_copy_cmd_scroll_bottom 2834 }, 2835 { .command = "scroll-down", 2836 .minargs = 0, 2837 .maxargs = 0, 2838 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2839 .f = window_copy_cmd_scroll_down 2840 }, 2841 { .command = "scroll-down-and-cancel", 2842 .minargs = 0, 2843 .maxargs = 0, 2844 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2845 .f = window_copy_cmd_scroll_down_and_cancel 2846 }, 2847 { .command = "scroll-middle", 2848 .minargs = 0, 2849 .maxargs = 0, 2850 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2851 .f = window_copy_cmd_scroll_middle 2852 }, 2853 { .command = "scroll-top", 2854 .minargs = 0, 2855 .maxargs = 0, 2856 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2857 .f = window_copy_cmd_scroll_top 2858 }, 2859 { .command = "scroll-up", 2860 .minargs = 0, 2861 .maxargs = 0, 2862 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2863 .f = window_copy_cmd_scroll_up 2864 }, 2865 { .command = "search-again", 2866 .minargs = 0, 2867 .maxargs = 0, 2868 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2869 .f = window_copy_cmd_search_again 2870 }, 2871 { .command = "search-backward", 2872 .minargs = 0, 2873 .maxargs = 1, 2874 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2875 .f = window_copy_cmd_search_backward 2876 }, 2877 { .command = "search-backward-text", 2878 .minargs = 0, 2879 .maxargs = 1, 2880 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2881 .f = window_copy_cmd_search_backward_text 2882 }, 2883 { .command = "search-backward-incremental", 2884 .minargs = 1, 2885 .maxargs = 1, 2886 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2887 .f = window_copy_cmd_search_backward_incremental 2888 }, 2889 { .command = "search-forward", 2890 .minargs = 0, 2891 .maxargs = 1, 2892 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2893 .f = window_copy_cmd_search_forward 2894 }, 2895 { .command = "search-forward-text", 2896 .minargs = 0, 2897 .maxargs = 1, 2898 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2899 .f = window_copy_cmd_search_forward_text 2900 }, 2901 { .command = "search-forward-incremental", 2902 .minargs = 1, 2903 .maxargs = 1, 2904 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2905 .f = window_copy_cmd_search_forward_incremental 2906 }, 2907 { .command = "search-reverse", 2908 .minargs = 0, 2909 .maxargs = 0, 2910 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2911 .f = window_copy_cmd_search_reverse 2912 }, 2913 { .command = "select-line", 2914 .minargs = 0, 2915 .maxargs = 0, 2916 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2917 .f = window_copy_cmd_select_line 2918 }, 2919 { .command = "select-word", 2920 .minargs = 0, 2921 .maxargs = 0, 2922 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2923 .f = window_copy_cmd_select_word 2924 }, 2925 { .command = "set-mark", 2926 .minargs = 0, 2927 .maxargs = 0, 2928 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2929 .f = window_copy_cmd_set_mark 2930 }, 2931 { .command = "start-of-line", 2932 .minargs = 0, 2933 .maxargs = 0, 2934 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2935 .f = window_copy_cmd_start_of_line 2936 }, 2937 { .command = "stop-selection", 2938 .minargs = 0, 2939 .maxargs = 0, 2940 .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS, 2941 .f = window_copy_cmd_stop_selection 2942 }, 2943 { .command = "toggle-position", 2944 .minargs = 0, 2945 .maxargs = 0, 2946 .clear = WINDOW_COPY_CMD_CLEAR_NEVER, 2947 .f = window_copy_cmd_toggle_position 2948 }, 2949 { .command = "top-line", 2950 .minargs = 0, 2951 .maxargs = 0, 2952 .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY, 2953 .f = window_copy_cmd_top_line 2954 } 2955 }; 2956 2957 static void 2958 window_copy_command(struct window_mode_entry *wme, struct client *c, 2959 struct session *s, struct winlink *wl, struct args *args, 2960 struct mouse_event *m) 2961 { 2962 struct window_copy_mode_data *data = wme->data; 2963 struct window_copy_cmd_state cs; 2964 enum window_copy_cmd_action action; 2965 enum window_copy_cmd_clear clear = WINDOW_COPY_CMD_CLEAR_NEVER; 2966 const char *command; 2967 u_int i, count = args_count(args); 2968 int keys; 2969 2970 if (count == 0) 2971 return; 2972 command = args_string(args, 0); 2973 2974 if (m != NULL && m->valid && !MOUSE_WHEEL(m->b)) 2975 window_copy_move_mouse(m); 2976 2977 cs.wme = wme; 2978 cs.args = args; 2979 cs.m = m; 2980 2981 cs.c = c; 2982 cs.s = s; 2983 cs.wl = wl; 2984 2985 action = WINDOW_COPY_CMD_NOTHING; 2986 for (i = 0; i < nitems(window_copy_cmd_table); i++) { 2987 if (strcmp(window_copy_cmd_table[i].command, command) == 0) { 2988 if (count - 1 < window_copy_cmd_table[i].minargs || 2989 count - 1 > window_copy_cmd_table[i].maxargs) 2990 break; 2991 clear = window_copy_cmd_table[i].clear; 2992 action = window_copy_cmd_table[i].f(&cs); 2993 break; 2994 } 2995 } 2996 2997 if (strncmp(command, "search-", 7) != 0 && data->searchmark != NULL) { 2998 keys = options_get_number(wme->wp->window->options, "mode-keys"); 2999 if (clear == WINDOW_COPY_CMD_CLEAR_EMACS_ONLY && 3000 keys == MODEKEY_VI) 3001 clear = WINDOW_COPY_CMD_CLEAR_NEVER; 3002 if (clear != WINDOW_COPY_CMD_CLEAR_NEVER) { 3003 window_copy_clear_marks(wme); 3004 data->searchx = data->searchy = -1; 3005 } 3006 if (action == WINDOW_COPY_CMD_NOTHING) 3007 action = WINDOW_COPY_CMD_REDRAW; 3008 } 3009 wme->prefix = 1; 3010 3011 if (action == WINDOW_COPY_CMD_CANCEL) 3012 window_pane_reset_mode(wme->wp); 3013 else if (action == WINDOW_COPY_CMD_REDRAW) 3014 window_copy_redraw_screen(wme); 3015 } 3016 3017 static void 3018 window_copy_scroll_to(struct window_mode_entry *wme, u_int px, u_int py, 3019 int no_redraw) 3020 { 3021 struct window_copy_mode_data *data = wme->data; 3022 struct grid *gd = data->backing->grid; 3023 u_int offset, gap; 3024 3025 data->cx = px; 3026 3027 if (py >= gd->hsize - data->oy && py < gd->hsize - data->oy + gd->sy) 3028 data->cy = py - (gd->hsize - data->oy); 3029 else { 3030 gap = gd->sy / 4; 3031 if (py < gd->sy) { 3032 offset = 0; 3033 data->cy = py; 3034 } else if (py > gd->hsize + gd->sy - gap) { 3035 offset = gd->hsize; 3036 data->cy = py - gd->hsize; 3037 } else { 3038 offset = py + gap - gd->sy; 3039 data->cy = py - offset; 3040 } 3041 data->oy = gd->hsize - offset; 3042 } 3043 3044 if (!no_redraw && data->searchmark != NULL && !data->timeout) 3045 window_copy_search_marks(wme, NULL, data->searchregex, 1); 3046 window_copy_update_selection(wme, 1, 0); 3047 if (!no_redraw) 3048 window_copy_redraw_screen(wme); 3049 } 3050 3051 static int 3052 window_copy_search_compare(struct grid *gd, u_int px, u_int py, 3053 struct grid *sgd, u_int spx, int cis) 3054 { 3055 struct grid_cell gc, sgc; 3056 const struct utf8_data *ud, *sud; 3057 3058 grid_get_cell(gd, px, py, &gc); 3059 ud = &gc.data; 3060 grid_get_cell(sgd, spx, 0, &sgc); 3061 sud = &sgc.data; 3062 3063 if (ud->size != sud->size || ud->width != sud->width) 3064 return (0); 3065 3066 if (cis && ud->size == 1) 3067 return (tolower(ud->data[0]) == sud->data[0]); 3068 3069 return (memcmp(ud->data, sud->data, ud->size) == 0); 3070 } 3071 3072 static int 3073 window_copy_search_lr(struct grid *gd, struct grid *sgd, u_int *ppx, u_int py, 3074 u_int first, u_int last, int cis) 3075 { 3076 u_int ax, bx, px, pywrap, endline; 3077 int matched; 3078 struct grid_line *gl; 3079 3080 endline = gd->hsize + gd->sy - 1; 3081 for (ax = first; ax < last; ax++) { 3082 for (bx = 0; bx < sgd->sx; bx++) { 3083 px = ax + bx; 3084 pywrap = py; 3085 /* Wrap line. */ 3086 while (px >= gd->sx && pywrap < endline) { 3087 gl = grid_get_line(gd, pywrap); 3088 if (~gl->flags & GRID_LINE_WRAPPED) 3089 break; 3090 px -= gd->sx; 3091 pywrap++; 3092 } 3093 /* We have run off the end of the grid. */ 3094 if (px >= gd->sx) 3095 break; 3096 matched = window_copy_search_compare(gd, px, pywrap, 3097 sgd, bx, cis); 3098 if (!matched) 3099 break; 3100 } 3101 if (bx == sgd->sx) { 3102 *ppx = ax; 3103 return (1); 3104 } 3105 } 3106 return (0); 3107 } 3108 3109 static int 3110 window_copy_search_rl(struct grid *gd, 3111 struct grid *sgd, u_int *ppx, u_int py, u_int first, u_int last, int cis) 3112 { 3113 u_int ax, bx, px, pywrap, endline; 3114 int matched; 3115 struct grid_line *gl; 3116 3117 endline = gd->hsize + gd->sy - 1; 3118 for (ax = last; ax > first; ax--) { 3119 for (bx = 0; bx < sgd->sx; bx++) { 3120 px = ax - 1 + bx; 3121 pywrap = py; 3122 /* Wrap line. */ 3123 while (px >= gd->sx && pywrap < endline) { 3124 gl = grid_get_line(gd, pywrap); 3125 if (~gl->flags & GRID_LINE_WRAPPED) 3126 break; 3127 px -= gd->sx; 3128 pywrap++; 3129 } 3130 /* We have run off the end of the grid. */ 3131 if (px >= gd->sx) 3132 break; 3133 matched = window_copy_search_compare(gd, px, pywrap, 3134 sgd, bx, cis); 3135 if (!matched) 3136 break; 3137 } 3138 if (bx == sgd->sx) { 3139 *ppx = ax - 1; 3140 return (1); 3141 } 3142 } 3143 return (0); 3144 } 3145 3146 static int 3147 window_copy_search_lr_regex(struct grid *gd, u_int *ppx, u_int *psx, u_int py, 3148 u_int first, u_int last, regex_t *reg) 3149 { 3150 int eflags = 0; 3151 u_int endline, foundx, foundy, len, pywrap, size = 1; 3152 char *buf; 3153 regmatch_t regmatch; 3154 struct grid_line *gl; 3155 3156 /* 3157 * This can happen during search if the last match was the last 3158 * character on a line. 3159 */ 3160 if (first >= last) 3161 return (0); 3162 3163 /* Set flags for regex search. */ 3164 if (first != 0) 3165 eflags |= REG_NOTBOL; 3166 3167 /* Need to look at the entire string. */ 3168 buf = xmalloc(size); 3169 buf[0] = '\0'; 3170 buf = window_copy_stringify(gd, py, first, gd->sx, buf, &size); 3171 len = gd->sx - first; 3172 endline = gd->hsize + gd->sy - 1; 3173 pywrap = py; 3174 while (buf != NULL && pywrap <= endline) { 3175 gl = grid_get_line(gd, pywrap); 3176 if (~gl->flags & GRID_LINE_WRAPPED) 3177 break; 3178 pywrap++; 3179 buf = window_copy_stringify(gd, pywrap, 0, gd->sx, buf, &size); 3180 len += gd->sx; 3181 } 3182 3183 if (regexec(reg, buf, 1, ®match, eflags) == 0 && 3184 regmatch.rm_so != regmatch.rm_eo) { 3185 foundx = first; 3186 foundy = py; 3187 window_copy_cstrtocellpos(gd, len, &foundx, &foundy, 3188 buf + regmatch.rm_so); 3189 if (foundy == py && foundx < last) { 3190 *ppx = foundx; 3191 len -= foundx - first; 3192 window_copy_cstrtocellpos(gd, len, &foundx, &foundy, 3193 buf + regmatch.rm_eo); 3194 *psx = foundx; 3195 while (foundy > py) { 3196 *psx += gd->sx; 3197 foundy--; 3198 } 3199 *psx -= *ppx; 3200 free(buf); 3201 return (1); 3202 } 3203 } 3204 3205 free(buf); 3206 *ppx = 0; 3207 *psx = 0; 3208 return (0); 3209 } 3210 3211 static int 3212 window_copy_search_rl_regex(struct grid *gd, u_int *ppx, u_int *psx, u_int py, 3213 u_int first, u_int last, regex_t *reg) 3214 { 3215 int eflags = 0; 3216 u_int endline, len, pywrap, size = 1; 3217 char *buf; 3218 struct grid_line *gl; 3219 3220 /* Set flags for regex search. */ 3221 if (first != 0) 3222 eflags |= REG_NOTBOL; 3223 3224 /* Need to look at the entire string. */ 3225 buf = xmalloc(size); 3226 buf[0] = '\0'; 3227 buf = window_copy_stringify(gd, py, first, gd->sx, buf, &size); 3228 len = gd->sx - first; 3229 endline = gd->hsize + gd->sy - 1; 3230 pywrap = py; 3231 while (buf != NULL && (pywrap <= endline)) { 3232 gl = grid_get_line(gd, pywrap); 3233 if (~gl->flags & GRID_LINE_WRAPPED) 3234 break; 3235 pywrap++; 3236 buf = window_copy_stringify(gd, pywrap, 0, gd->sx, buf, &size); 3237 len += gd->sx; 3238 } 3239 3240 if (window_copy_last_regex(gd, py, first, last, len, ppx, psx, buf, 3241 reg, eflags)) 3242 { 3243 free(buf); 3244 return (1); 3245 } 3246 3247 free(buf); 3248 *ppx = 0; 3249 *psx = 0; 3250 return (0); 3251 } 3252 3253 static const char * 3254 window_copy_cellstring(const struct grid_line *gl, u_int px, size_t *size, 3255 int *allocated) 3256 { 3257 static struct utf8_data ud; 3258 struct grid_cell_entry *gce; 3259 char *copy; 3260 3261 if (px >= gl->cellsize) { 3262 *size = 1; 3263 *allocated = 0; 3264 return (" "); 3265 } 3266 3267 gce = &gl->celldata[px]; 3268 if (gce->flags & GRID_FLAG_PADDING) { 3269 *size = 0; 3270 *allocated = 0; 3271 return (NULL); 3272 } 3273 if (~gce->flags & GRID_FLAG_EXTENDED) { 3274 *size = 1; 3275 *allocated = 0; 3276 return (&gce->data.data); 3277 } 3278 3279 utf8_to_data(gl->extddata[gce->offset].data, &ud); 3280 if (ud.size == 0) { 3281 *size = 0; 3282 *allocated = 0; 3283 return (NULL); 3284 } 3285 *size = ud.size; 3286 *allocated = 1; 3287 3288 copy = xmalloc(ud.size); 3289 memcpy(copy, ud.data, ud.size); 3290 return (copy); 3291 } 3292 3293 /* Find last match in given range. */ 3294 static int 3295 window_copy_last_regex(struct grid *gd, u_int py, u_int first, u_int last, 3296 u_int len, u_int *ppx, u_int *psx, const char *buf, const regex_t *preg, 3297 int eflags) 3298 { 3299 u_int foundx, foundy, oldx, px = 0, savepx, savesx = 0; 3300 regmatch_t regmatch; 3301 3302 foundx = first; 3303 foundy = py; 3304 oldx = first; 3305 while (regexec(preg, buf + px, 1, ®match, eflags) == 0) { 3306 if (regmatch.rm_so == regmatch.rm_eo) 3307 break; 3308 window_copy_cstrtocellpos(gd, len, &foundx, &foundy, 3309 buf + px + regmatch.rm_so); 3310 if (foundy > py || foundx >= last) 3311 break; 3312 len -= foundx - oldx; 3313 savepx = foundx; 3314 window_copy_cstrtocellpos(gd, len, &foundx, &foundy, 3315 buf + px + regmatch.rm_eo); 3316 if (foundy > py || foundx >= last) { 3317 *ppx = savepx; 3318 *psx = foundx; 3319 while (foundy > py) { 3320 *psx += gd->sx; 3321 foundy--; 3322 } 3323 *psx -= *ppx; 3324 return (1); 3325 } else { 3326 savesx = foundx - savepx; 3327 len -= savesx; 3328 oldx = foundx; 3329 } 3330 px += regmatch.rm_eo; 3331 } 3332 3333 if (savesx > 0) { 3334 *ppx = savepx; 3335 *psx = savesx; 3336 return (1); 3337 } else { 3338 *ppx = 0; 3339 *psx = 0; 3340 return (0); 3341 } 3342 } 3343 3344 /* Stringify line and append to input buffer. Caller frees. */ 3345 static char * 3346 window_copy_stringify(struct grid *gd, u_int py, u_int first, u_int last, 3347 char *buf, u_int *size) 3348 { 3349 u_int ax, bx, newsize = *size; 3350 const struct grid_line *gl; 3351 const char *d; 3352 size_t bufsize = 1024, dlen; 3353 int allocated; 3354 3355 while (bufsize < newsize) 3356 bufsize *= 2; 3357 buf = xrealloc(buf, bufsize); 3358 3359 gl = grid_peek_line(gd, py); 3360 bx = *size - 1; 3361 for (ax = first; ax < last; ax++) { 3362 d = window_copy_cellstring(gl, ax, &dlen, &allocated); 3363 newsize += dlen; 3364 while (bufsize < newsize) { 3365 bufsize *= 2; 3366 buf = xrealloc(buf, bufsize); 3367 } 3368 if (dlen == 1) 3369 buf[bx++] = *d; 3370 else { 3371 memcpy(buf + bx, d, dlen); 3372 bx += dlen; 3373 } 3374 if (allocated) 3375 free((void *)d); 3376 } 3377 buf[newsize - 1] = '\0'; 3378 3379 *size = newsize; 3380 return (buf); 3381 } 3382 3383 /* Map start of C string containing UTF-8 data to grid cell position. */ 3384 static void 3385 window_copy_cstrtocellpos(struct grid *gd, u_int ncells, u_int *ppx, u_int *ppy, 3386 const char *str) 3387 { 3388 u_int cell, ccell, px, pywrap, pos, len; 3389 int match; 3390 const struct grid_line *gl; 3391 const char *d; 3392 size_t dlen; 3393 struct { 3394 const char *d; 3395 size_t dlen; 3396 int allocated; 3397 } *cells; 3398 3399 /* Populate the array of cell data. */ 3400 cells = xreallocarray(NULL, ncells, sizeof cells[0]); 3401 cell = 0; 3402 px = *ppx; 3403 pywrap = *ppy; 3404 gl = grid_peek_line(gd, pywrap); 3405 while (cell < ncells) { 3406 cells[cell].d = window_copy_cellstring(gl, px, 3407 &cells[cell].dlen, &cells[cell].allocated); 3408 cell++; 3409 px++; 3410 if (px == gd->sx) { 3411 px = 0; 3412 pywrap++; 3413 gl = grid_peek_line(gd, pywrap); 3414 } 3415 } 3416 3417 /* Locate starting cell. */ 3418 cell = 0; 3419 len = strlen(str); 3420 while (cell < ncells) { 3421 ccell = cell; 3422 pos = 0; 3423 match = 1; 3424 while (ccell < ncells) { 3425 if (str[pos] == '\0') { 3426 match = 0; 3427 break; 3428 } 3429 d = cells[ccell].d; 3430 dlen = cells[ccell].dlen; 3431 if (dlen == 1) { 3432 if (str[pos] != *d) { 3433 match = 0; 3434 break; 3435 } 3436 pos++; 3437 } else { 3438 if (dlen > len - pos) 3439 dlen = len - pos; 3440 if (memcmp(str + pos, d, dlen) != 0) { 3441 match = 0; 3442 break; 3443 } 3444 pos += dlen; 3445 } 3446 ccell++; 3447 } 3448 if (match) 3449 break; 3450 cell++; 3451 } 3452 3453 /* If not found this will be one past the end. */ 3454 px = *ppx + cell; 3455 pywrap = *ppy; 3456 while (px >= gd->sx) { 3457 px -= gd->sx; 3458 pywrap++; 3459 } 3460 3461 *ppx = px; 3462 *ppy = pywrap; 3463 3464 /* Free cell data. */ 3465 for (cell = 0; cell < ncells; cell++) { 3466 if (cells[cell].allocated) 3467 free((void *)cells[cell].d); 3468 } 3469 free(cells); 3470 } 3471 3472 static void 3473 window_copy_move_left(struct screen *s, u_int *fx, u_int *fy, int wrapflag) 3474 { 3475 if (*fx == 0) { /* left */ 3476 if (*fy == 0) { /* top */ 3477 if (wrapflag) { 3478 *fx = screen_size_x(s) - 1; 3479 *fy = screen_hsize(s) + screen_size_y(s) - 1; 3480 } 3481 return; 3482 } 3483 *fx = screen_size_x(s) - 1; 3484 *fy = *fy - 1; 3485 } else 3486 *fx = *fx - 1; 3487 } 3488 3489 static void 3490 window_copy_move_right(struct screen *s, u_int *fx, u_int *fy, int wrapflag) 3491 { 3492 if (*fx == screen_size_x(s) - 1) { /* right */ 3493 if (*fy == screen_hsize(s) + screen_size_y(s) - 1) { /* bottom */ 3494 if (wrapflag) { 3495 *fx = 0; 3496 *fy = 0; 3497 } 3498 return; 3499 } 3500 *fx = 0; 3501 *fy = *fy + 1; 3502 } else 3503 *fx = *fx + 1; 3504 } 3505 3506 static int 3507 window_copy_is_lowercase(const char *ptr) 3508 { 3509 while (*ptr != '\0') { 3510 if (*ptr != tolower((u_char)*ptr)) 3511 return (0); 3512 ++ptr; 3513 } 3514 return (1); 3515 } 3516 3517 /* 3518 * Handle backward wrapped regex searches with overlapping matches. In this case 3519 * find the longest overlapping match from previous wrapped lines. 3520 */ 3521 static void 3522 window_copy_search_back_overlap(struct grid *gd, regex_t *preg, u_int *ppx, 3523 u_int *psx, u_int *ppy, u_int endline) 3524 { 3525 u_int endx, endy, oldendx, oldendy, px, py, sx; 3526 int found = 1; 3527 3528 oldendx = *ppx + *psx; 3529 oldendy = *ppy - 1; 3530 while (oldendx > gd->sx - 1) { 3531 oldendx -= gd->sx; 3532 oldendy++; 3533 } 3534 endx = oldendx; 3535 endy = oldendy; 3536 px = *ppx; 3537 py = *ppy; 3538 while (found && px == 0 && py - 1 > endline && 3539 grid_get_line(gd, py - 2)->flags & GRID_LINE_WRAPPED && 3540 endx == oldendx && endy == oldendy) { 3541 py--; 3542 found = window_copy_search_rl_regex(gd, &px, &sx, py - 1, 0, 3543 gd->sx, preg); 3544 if (found) { 3545 endx = px + sx; 3546 endy = py - 1; 3547 while (endx > gd->sx - 1) { 3548 endx -= gd->sx; 3549 endy++; 3550 } 3551 if (endx == oldendx && endy == oldendy) { 3552 *ppx = px; 3553 *ppy = py; 3554 } 3555 } 3556 } 3557 } 3558 3559 /* 3560 * Search for text stored in sgd starting from position fx,fy up to endline. If 3561 * found, jump to it. If cis then ignore case. The direction is 0 for searching 3562 * up, down otherwise. If wrap then go to begin/end of grid and try again if 3563 * not found. 3564 */ 3565 static int 3566 window_copy_search_jump(struct window_mode_entry *wme, struct grid *gd, 3567 struct grid *sgd, u_int fx, u_int fy, u_int endline, int cis, int wrap, 3568 int direction, int regex) 3569 { 3570 u_int i, px, sx, ssize = 1; 3571 int found = 0, cflags = REG_EXTENDED; 3572 char *sbuf; 3573 regex_t reg; 3574 3575 if (regex) { 3576 sbuf = xmalloc(ssize); 3577 sbuf[0] = '\0'; 3578 sbuf = window_copy_stringify(sgd, 0, 0, sgd->sx, sbuf, &ssize); 3579 if (cis) 3580 cflags |= REG_ICASE; 3581 if (regcomp(®, sbuf, cflags) != 0) { 3582 free(sbuf); 3583 return (0); 3584 } 3585 free(sbuf); 3586 } 3587 3588 if (direction) { 3589 for (i = fy; i <= endline; i++) { 3590 if (regex) { 3591 found = window_copy_search_lr_regex(gd, 3592 &px, &sx, i, fx, gd->sx, ®); 3593 } else { 3594 found = window_copy_search_lr(gd, sgd, 3595 &px, i, fx, gd->sx, cis); 3596 } 3597 if (found) 3598 break; 3599 fx = 0; 3600 } 3601 } else { 3602 for (i = fy + 1; endline < i; i--) { 3603 if (regex) { 3604 found = window_copy_search_rl_regex(gd, 3605 &px, &sx, i - 1, 0, fx + 1, ®); 3606 if (found) { 3607 window_copy_search_back_overlap(gd, 3608 ®, &px, &sx, &i, endline); 3609 } 3610 } else { 3611 found = window_copy_search_rl(gd, sgd, 3612 &px, i - 1, 0, fx + 1, cis); 3613 } 3614 if (found) { 3615 i--; 3616 break; 3617 } 3618 fx = gd->sx - 1; 3619 } 3620 } 3621 if (regex) 3622 regfree(®); 3623 3624 if (found) { 3625 window_copy_scroll_to(wme, px, i, 1); 3626 return (1); 3627 } 3628 if (wrap) { 3629 return (window_copy_search_jump(wme, gd, sgd, 3630 direction ? 0 : gd->sx - 1, 3631 direction ? 0 : gd->hsize + gd->sy - 1, fy, cis, 0, 3632 direction, regex)); 3633 } 3634 return (0); 3635 } 3636 3637 static void 3638 window_copy_move_after_search_mark(struct window_copy_mode_data *data, 3639 u_int *fx, u_int *fy, int wrapflag) 3640 { 3641 struct screen *s = data->backing; 3642 u_int at, start; 3643 3644 if (window_copy_search_mark_at(data, *fx, *fy, &start) == 0 && 3645 data->searchmark[start] != 0) { 3646 while (window_copy_search_mark_at(data, *fx, *fy, &at) == 0) { 3647 if (data->searchmark[at] != data->searchmark[start]) 3648 break; 3649 /* Stop if not wrapping and at the end of the grid. */ 3650 if (!wrapflag && 3651 *fx == screen_size_x(s) - 1 && 3652 *fy == screen_hsize(s) + screen_size_y(s) - 1) 3653 break; 3654 3655 window_copy_move_right(s, fx, fy, wrapflag); 3656 } 3657 } 3658 } 3659 3660 /* 3661 * Search in for text searchstr. If direction is 0 then search up, otherwise 3662 * down. 3663 */ 3664 static int 3665 window_copy_search(struct window_mode_entry *wme, int direction, int regex) 3666 { 3667 struct window_pane *wp = wme->wp; 3668 struct window_copy_mode_data *data = wme->data; 3669 struct screen *s = data->backing, ss; 3670 struct screen_write_ctx ctx; 3671 struct grid *gd = s->grid; 3672 const char *str = data->searchstr; 3673 u_int at, endline, fx, fy, start; 3674 int cis, found, keys, visible_only; 3675 int wrapflag; 3676 3677 if (regex && str[strcspn(str, "^$*+()?[].\\")] == '\0') 3678 regex = 0; 3679 3680 data->searchdirection = direction; 3681 3682 if (data->timeout) 3683 return (0); 3684 3685 if (data->searchall || wp->searchstr == NULL || 3686 wp->searchregex != regex) { 3687 visible_only = 0; 3688 data->searchall = 0; 3689 } else 3690 visible_only = (strcmp(wp->searchstr, str) == 0); 3691 if (visible_only == 0 && data->searchmark != NULL) 3692 window_copy_clear_marks(wme); 3693 free(wp->searchstr); 3694 wp->searchstr = xstrdup(str); 3695 wp->searchregex = regex; 3696 3697 fx = data->cx; 3698 fy = screen_hsize(data->backing) - data->oy + data->cy; 3699 3700 screen_init(&ss, screen_write_strlen("%s", str), 1, 0); 3701 screen_write_start(&ctx, &ss); 3702 screen_write_nputs(&ctx, -1, &grid_default_cell, "%s", str); 3703 screen_write_stop(&ctx); 3704 3705 wrapflag = options_get_number(wp->window->options, "wrap-search"); 3706 cis = window_copy_is_lowercase(str); 3707 3708 keys = options_get_number(wp->window->options, "mode-keys"); 3709 3710 if (direction) { 3711 /* 3712 * Behave according to mode-keys. If it is emacs, search forward 3713 * leaves the cursor after the match. If it is vi, the cursor 3714 * remains at the beginning of the match, regardless of 3715 * direction, which means that we need to start the next search 3716 * after the term the cursor is currently on when searching 3717 * forward. 3718 */ 3719 if (keys == MODEKEY_VI) { 3720 if (data->searchmark != NULL) 3721 window_copy_move_after_search_mark(data, &fx, 3722 &fy, wrapflag); 3723 else { 3724 /* 3725 * When there are no search marks, start the 3726 * search after the current cursor position. 3727 */ 3728 window_copy_move_right(s, &fx, &fy, wrapflag); 3729 } 3730 } 3731 endline = gd->hsize + gd->sy - 1; 3732 } 3733 else { 3734 window_copy_move_left(s, &fx, &fy, wrapflag); 3735 endline = 0; 3736 } 3737 3738 found = window_copy_search_jump(wme, gd, ss.grid, fx, fy, endline, cis, 3739 wrapflag, direction, regex); 3740 if (found) { 3741 window_copy_search_marks(wme, &ss, regex, visible_only); 3742 fx = data->cx; 3743 fy = screen_hsize(data->backing) - data->oy + data->cy; 3744 3745 /* 3746 * When searching forward, if the cursor is not at the beginning 3747 * of the mark, search again. 3748 */ 3749 if (direction && 3750 window_copy_search_mark_at(data, fx, fy, &at) == 0 && 3751 at > 0 && 3752 data->searchmark != NULL && 3753 data->searchmark[at] == data->searchmark[at - 1]) { 3754 window_copy_move_after_search_mark(data, &fx, &fy, 3755 wrapflag); 3756 window_copy_search_jump(wme, gd, ss.grid, fx, 3757 fy, endline, cis, wrapflag, direction, 3758 regex); 3759 fx = data->cx; 3760 fy = screen_hsize(data->backing) - data->oy + data->cy; 3761 } 3762 3763 if (direction) { 3764 /* 3765 * When in Emacs mode, position the cursor just after 3766 * the mark. 3767 */ 3768 if (keys == MODEKEY_EMACS) { 3769 window_copy_move_after_search_mark(data, &fx, 3770 &fy, wrapflag); 3771 data->cx = fx; 3772 data->cy = fy - screen_hsize(data->backing) + 3773 data-> oy; 3774 } 3775 } 3776 else { 3777 /* 3778 * When searching backward, position the cursor at the 3779 * beginning of the mark. 3780 */ 3781 if (window_copy_search_mark_at(data, fx, fy, 3782 &start) == 0) { 3783 while (window_copy_search_mark_at(data, fx, fy, 3784 &at) == 0 && 3785 data->searchmark != NULL && 3786 data->searchmark[at] == 3787 data->searchmark[start]) { 3788 data->cx = fx; 3789 data->cy = fy - 3790 screen_hsize(data->backing) + 3791 data-> oy; 3792 if (at == 0) 3793 break; 3794 3795 window_copy_move_left(s, &fx, &fy, 0); 3796 } 3797 } 3798 } 3799 } 3800 window_copy_redraw_screen(wme); 3801 3802 screen_free(&ss); 3803 return (found); 3804 } 3805 3806 static void 3807 window_copy_visible_lines(struct window_copy_mode_data *data, u_int *start, 3808 u_int *end) 3809 { 3810 struct grid *gd = data->backing->grid; 3811 const struct grid_line *gl; 3812 3813 for (*start = gd->hsize - data->oy; *start > 0; (*start)--) { 3814 gl = grid_peek_line(gd, (*start) - 1); 3815 if (~gl->flags & GRID_LINE_WRAPPED) 3816 break; 3817 } 3818 *end = gd->hsize - data->oy + gd->sy; 3819 } 3820 3821 static int 3822 window_copy_search_mark_at(struct window_copy_mode_data *data, u_int px, 3823 u_int py, u_int *at) 3824 { 3825 struct screen *s = data->backing; 3826 struct grid *gd = s->grid; 3827 3828 if (py < gd->hsize - data->oy) 3829 return (-1); 3830 if (py > gd->hsize - data->oy + gd->sy - 1) 3831 return (-1); 3832 *at = ((py - (gd->hsize - data->oy)) * gd->sx) + px; 3833 return (0); 3834 } 3835 3836 static int 3837 window_copy_search_marks(struct window_mode_entry *wme, struct screen *ssp, 3838 int regex, int visible_only) 3839 { 3840 struct window_copy_mode_data *data = wme->data; 3841 struct screen *s = data->backing, ss; 3842 struct screen_write_ctx ctx; 3843 struct grid *gd = s->grid; 3844 int found, cis, stopped = 0; 3845 int cflags = REG_EXTENDED; 3846 u_int px, py, i, b, nfound = 0, width; 3847 u_int ssize = 1, start, end; 3848 char *sbuf; 3849 regex_t reg; 3850 uint64_t stop = 0, tstart, t; 3851 3852 if (ssp == NULL) { 3853 width = screen_write_strlen("%s", data->searchstr); 3854 screen_init(&ss, width, 1, 0); 3855 screen_write_start(&ctx, &ss); 3856 screen_write_nputs(&ctx, -1, &grid_default_cell, "%s", 3857 data->searchstr); 3858 screen_write_stop(&ctx); 3859 ssp = &ss; 3860 } else 3861 width = screen_size_x(ssp); 3862 3863 cis = window_copy_is_lowercase(data->searchstr); 3864 3865 if (regex) { 3866 sbuf = xmalloc(ssize); 3867 sbuf[0] = '\0'; 3868 sbuf = window_copy_stringify(ssp->grid, 0, 0, ssp->grid->sx, 3869 sbuf, &ssize); 3870 if (cis) 3871 cflags |= REG_ICASE; 3872 if (regcomp(®, sbuf, cflags) != 0) { 3873 free(sbuf); 3874 return (0); 3875 } 3876 free(sbuf); 3877 } 3878 tstart = get_timer(); 3879 3880 if (visible_only) 3881 window_copy_visible_lines(data, &start, &end); 3882 else { 3883 start = 0; 3884 end = gd->hsize + gd->sy; 3885 stop = get_timer() + WINDOW_COPY_SEARCH_ALL_TIMEOUT; 3886 } 3887 3888 again: 3889 free(data->searchmark); 3890 data->searchmark = xcalloc(gd->sx, gd->sy); 3891 data->searchgen = 1; 3892 3893 for (py = start; py < end; py++) { 3894 px = 0; 3895 for (;;) { 3896 if (regex) { 3897 found = window_copy_search_lr_regex(gd, 3898 &px, &width, py, px, gd->sx, ®); 3899 if (!found) 3900 break; 3901 } else { 3902 found = window_copy_search_lr(gd, ssp->grid, 3903 &px, py, px, gd->sx, cis); 3904 if (!found) 3905 break; 3906 } 3907 nfound++; 3908 3909 if (window_copy_search_mark_at(data, px, py, &b) == 0) { 3910 if (b + width > gd->sx * gd->sy) 3911 width = (gd->sx * gd->sy) - b; 3912 for (i = b; i < b + width; i++) { 3913 if (data->searchmark[i] != 0) 3914 continue; 3915 data->searchmark[i] = data->searchgen; 3916 } 3917 if (data->searchgen == UCHAR_MAX) 3918 data->searchgen = 1; 3919 else 3920 data->searchgen++; 3921 } 3922 px += width; 3923 } 3924 3925 t = get_timer(); 3926 if (t - tstart > WINDOW_COPY_SEARCH_TIMEOUT) { 3927 data->timeout = 1; 3928 break; 3929 } 3930 if (stop != 0 && t > stop) { 3931 stopped = 1; 3932 break; 3933 } 3934 } 3935 if (data->timeout) { 3936 window_copy_clear_marks(wme); 3937 goto out; 3938 } 3939 3940 if (stopped && stop != 0) { 3941 /* Try again but just the visible context. */ 3942 window_copy_visible_lines(data, &start, &end); 3943 stop = 0; 3944 goto again; 3945 } 3946 3947 if (!visible_only) { 3948 if (stopped) { 3949 if (nfound > 1000) 3950 data->searchcount = 1000; 3951 else if (nfound > 100) 3952 data->searchcount = 100; 3953 else if (nfound > 10) 3954 data->searchcount = 10; 3955 else 3956 data->searchcount = -1; 3957 data->searchmore = 1; 3958 } else { 3959 data->searchcount = nfound; 3960 data->searchmore = 0; 3961 } 3962 } 3963 3964 out: 3965 if (ssp == &ss) 3966 screen_free(&ss); 3967 if (regex) 3968 regfree(®); 3969 return (1); 3970 } 3971 3972 static void 3973 window_copy_clear_marks(struct window_mode_entry *wme) 3974 { 3975 struct window_copy_mode_data *data = wme->data; 3976 3977 free(data->searchmark); 3978 data->searchmark = NULL; 3979 } 3980 3981 static int 3982 window_copy_search_up(struct window_mode_entry *wme, int regex) 3983 { 3984 return (window_copy_search(wme, 0, regex)); 3985 } 3986 3987 static int 3988 window_copy_search_down(struct window_mode_entry *wme, int regex) 3989 { 3990 return (window_copy_search(wme, 1, regex)); 3991 } 3992 3993 static void 3994 window_copy_goto_line(struct window_mode_entry *wme, const char *linestr) 3995 { 3996 struct window_copy_mode_data *data = wme->data; 3997 const char *errstr; 3998 int lineno; 3999 4000 lineno = strtonum(linestr, -1, INT_MAX, &errstr); 4001 if (errstr != NULL) 4002 return; 4003 if (lineno < 0 || (u_int)lineno > screen_hsize(data->backing)) 4004 lineno = screen_hsize(data->backing); 4005 4006 data->oy = lineno; 4007 window_copy_update_selection(wme, 1, 0); 4008 window_copy_redraw_screen(wme); 4009 } 4010 4011 static void 4012 window_copy_match_start_end(struct window_copy_mode_data *data, u_int at, 4013 u_int *start, u_int *end) 4014 { 4015 struct grid *gd = data->backing->grid; 4016 u_int last = (gd->sy * gd->sx) - 1; 4017 u_char mark = data->searchmark[at]; 4018 4019 *start = *end = at; 4020 while (*start != 0 && data->searchmark[*start] == mark) 4021 (*start)--; 4022 if (data->searchmark[*start] != mark) 4023 (*start)++; 4024 while (*end != last && data->searchmark[*end] == mark) 4025 (*end)++; 4026 if (data->searchmark[*end] != mark) 4027 (*end)--; 4028 } 4029 4030 static char * 4031 window_copy_match_at_cursor(struct window_copy_mode_data *data) 4032 { 4033 struct grid *gd = data->backing->grid; 4034 struct grid_cell gc; 4035 u_int at, start, end, cy, px, py; 4036 u_int sx = screen_size_x(data->backing); 4037 char *buf = NULL; 4038 size_t len = 0; 4039 4040 if (data->searchmark == NULL) 4041 return (NULL); 4042 4043 cy = screen_hsize(data->backing) - data->oy + data->cy; 4044 if (window_copy_search_mark_at(data, data->cx, cy, &at) != 0) 4045 return (NULL); 4046 if (data->searchmark[at] == 0) { 4047 /* Allow one position after the match. */ 4048 if (at == 0 || data->searchmark[--at] == 0) 4049 return (NULL); 4050 } 4051 window_copy_match_start_end(data, at, &start, &end); 4052 4053 /* 4054 * Cells will not be set in the marked array unless they are valid text 4055 * and wrapping will be taken care of, so we can just copy. 4056 */ 4057 for (at = start; at <= end; at++) { 4058 py = at / sx; 4059 px = at - (py * sx); 4060 4061 grid_get_cell(gd, px, gd->hsize + py - data->oy, &gc); 4062 buf = xrealloc(buf, len + gc.data.size + 1); 4063 memcpy(buf + len, gc.data.data, gc.data.size); 4064 len += gc.data.size; 4065 } 4066 if (len != 0) 4067 buf[len] = '\0'; 4068 return (buf); 4069 } 4070 4071 static void 4072 window_copy_update_style(struct window_mode_entry *wme, u_int fx, u_int fy, 4073 struct grid_cell *gc, const struct grid_cell *mgc, 4074 const struct grid_cell *cgc, const struct grid_cell *mkgc) 4075 { 4076 struct window_pane *wp = wme->wp; 4077 struct window_copy_mode_data *data = wme->data; 4078 u_int mark, start, end, cy, cursor, current; 4079 int inv = 0, found = 0; 4080 int keys; 4081 4082 if (data->showmark && fy == data->my) { 4083 gc->attr = mkgc->attr; 4084 if (fx == data->mx) 4085 inv = 1; 4086 if (inv) { 4087 gc->fg = mkgc->bg; 4088 gc->bg = mkgc->fg; 4089 } 4090 else { 4091 gc->fg = mkgc->fg; 4092 gc->bg = mkgc->bg; 4093 } 4094 } 4095 4096 if (data->searchmark == NULL) 4097 return; 4098 4099 if (window_copy_search_mark_at(data, fx, fy, ¤t) != 0) 4100 return; 4101 mark = data->searchmark[current]; 4102 if (mark == 0) 4103 return; 4104 4105 cy = screen_hsize(data->backing) - data->oy + data->cy; 4106 if (window_copy_search_mark_at(data, data->cx, cy, &cursor) == 0) { 4107 keys = options_get_number(wp->window->options, "mode-keys"); 4108 if (cursor != 0 && 4109 keys == MODEKEY_EMACS && 4110 data->searchdirection) { 4111 if (data->searchmark[cursor - 1] == mark) { 4112 cursor--; 4113 found = 1; 4114 } 4115 } else if (data->searchmark[cursor] == mark) 4116 found = 1; 4117 if (found) { 4118 window_copy_match_start_end(data, cursor, &start, &end); 4119 if (current >= start && current <= end) { 4120 gc->attr = cgc->attr; 4121 if (inv) { 4122 gc->fg = cgc->bg; 4123 gc->bg = cgc->fg; 4124 } 4125 else { 4126 gc->fg = cgc->fg; 4127 gc->bg = cgc->bg; 4128 } 4129 return; 4130 } 4131 } 4132 } 4133 4134 gc->attr = mgc->attr; 4135 if (inv) { 4136 gc->fg = mgc->bg; 4137 gc->bg = mgc->fg; 4138 } 4139 else { 4140 gc->fg = mgc->fg; 4141 gc->bg = mgc->bg; 4142 } 4143 } 4144 4145 static void 4146 window_copy_write_one(struct window_mode_entry *wme, 4147 struct screen_write_ctx *ctx, u_int py, u_int fy, u_int nx, 4148 const struct grid_cell *mgc, const struct grid_cell *cgc, 4149 const struct grid_cell *mkgc) 4150 { 4151 struct window_copy_mode_data *data = wme->data; 4152 struct grid *gd = data->backing->grid; 4153 struct grid_cell gc; 4154 u_int fx; 4155 4156 screen_write_cursormove(ctx, 0, py, 0); 4157 for (fx = 0; fx < nx; fx++) { 4158 grid_get_cell(gd, fx, fy, &gc); 4159 if (fx + gc.data.width <= nx) { 4160 window_copy_update_style(wme, fx, fy, &gc, mgc, cgc, 4161 mkgc); 4162 screen_write_cell(ctx, &gc); 4163 } 4164 } 4165 } 4166 4167 static void 4168 window_copy_write_line(struct window_mode_entry *wme, 4169 struct screen_write_ctx *ctx, u_int py) 4170 { 4171 struct window_pane *wp = wme->wp; 4172 struct window_copy_mode_data *data = wme->data; 4173 struct screen *s = &data->screen; 4174 struct options *oo = wp->window->options; 4175 struct grid_line *gl; 4176 struct grid_cell gc, mgc, cgc, mkgc; 4177 char hdr[512], tmp[256], *t; 4178 size_t size = 0; 4179 u_int hsize = screen_hsize(data->backing); 4180 4181 style_apply(&gc, oo, "mode-style", NULL); 4182 gc.flags |= GRID_FLAG_NOPALETTE; 4183 style_apply(&mgc, oo, "copy-mode-match-style", NULL); 4184 mgc.flags |= GRID_FLAG_NOPALETTE; 4185 style_apply(&cgc, oo, "copy-mode-current-match-style", NULL); 4186 cgc.flags |= GRID_FLAG_NOPALETTE; 4187 style_apply(&mkgc, oo, "copy-mode-mark-style", NULL); 4188 mkgc.flags |= GRID_FLAG_NOPALETTE; 4189 4190 if (py == 0 && s->rupper < s->rlower && !data->hide_position) { 4191 gl = grid_get_line(data->backing->grid, hsize - data->oy); 4192 if (gl->time == 0) 4193 xsnprintf(tmp, sizeof tmp, "[%u/%u]", data->oy, hsize); 4194 else { 4195 t = format_pretty_time(gl->time, 1); 4196 xsnprintf(tmp, sizeof tmp, "%s [%u/%u]", t, data->oy, 4197 hsize); 4198 free(t); 4199 } 4200 4201 if (data->searchmark == NULL) { 4202 if (data->timeout) { 4203 size = xsnprintf(hdr, sizeof hdr, 4204 "(timed out) %s", tmp); 4205 } else 4206 size = xsnprintf(hdr, sizeof hdr, "%s", tmp); 4207 } else { 4208 if (data->searchcount == -1) 4209 size = xsnprintf(hdr, sizeof hdr, "%s", tmp); 4210 else { 4211 size = xsnprintf(hdr, sizeof hdr, 4212 "(%d%s results) %s", data->searchcount, 4213 data->searchmore ? "+" : "", tmp); 4214 } 4215 } 4216 if (size > screen_size_x(s)) 4217 size = screen_size_x(s); 4218 screen_write_cursormove(ctx, screen_size_x(s) - size, 0, 0); 4219 screen_write_puts(ctx, &gc, "%s", hdr); 4220 } else 4221 size = 0; 4222 4223 if (size < screen_size_x(s)) { 4224 window_copy_write_one(wme, ctx, py, hsize - data->oy + py, 4225 screen_size_x(s) - size, &mgc, &cgc, &mkgc); 4226 } 4227 4228 if (py == data->cy && data->cx == screen_size_x(s)) { 4229 screen_write_cursormove(ctx, screen_size_x(s) - 1, py, 0); 4230 screen_write_putc(ctx, &grid_default_cell, '$'); 4231 } 4232 } 4233 4234 static void 4235 window_copy_write_lines(struct window_mode_entry *wme, 4236 struct screen_write_ctx *ctx, u_int py, u_int ny) 4237 { 4238 u_int yy; 4239 4240 for (yy = py; yy < py + ny; yy++) 4241 window_copy_write_line(wme, ctx, py); 4242 } 4243 4244 static void 4245 window_copy_redraw_selection(struct window_mode_entry *wme, u_int old_y) 4246 { 4247 struct window_copy_mode_data *data = wme->data; 4248 struct grid *gd = data->backing->grid; 4249 u_int new_y, start, end; 4250 4251 new_y = data->cy; 4252 if (old_y <= new_y) { 4253 start = old_y; 4254 end = new_y; 4255 } else { 4256 start = new_y; 4257 end = old_y; 4258 } 4259 4260 /* 4261 * In word selection mode the first word on the line below the cursor 4262 * might be selected, so add this line to the redraw area. 4263 */ 4264 if (data->selflag == SEL_WORD) { 4265 /* Last grid line in data coordinates. */ 4266 if (end < gd->sy + data->oy - 1) 4267 end++; 4268 } 4269 window_copy_redraw_lines(wme, start, end - start + 1); 4270 } 4271 4272 static void 4273 window_copy_redraw_lines(struct window_mode_entry *wme, u_int py, u_int ny) 4274 { 4275 struct window_pane *wp = wme->wp; 4276 struct window_copy_mode_data *data = wme->data; 4277 struct screen_write_ctx ctx; 4278 u_int i; 4279 4280 screen_write_start_pane(&ctx, wp, NULL); 4281 for (i = py; i < py + ny; i++) 4282 window_copy_write_line(wme, &ctx, i); 4283 screen_write_cursormove(&ctx, data->cx, data->cy, 0); 4284 screen_write_stop(&ctx); 4285 } 4286 4287 static void 4288 window_copy_redraw_screen(struct window_mode_entry *wme) 4289 { 4290 struct window_copy_mode_data *data = wme->data; 4291 4292 window_copy_redraw_lines(wme, 0, screen_size_y(&data->screen)); 4293 } 4294 4295 static void 4296 window_copy_synchronize_cursor_end(struct window_mode_entry *wme, int begin, 4297 int no_reset) 4298 { 4299 struct window_copy_mode_data *data = wme->data; 4300 u_int xx, yy; 4301 4302 xx = data->cx; 4303 yy = screen_hsize(data->backing) + data->cy - data->oy; 4304 switch (data->selflag) { 4305 case SEL_WORD: 4306 if (no_reset) 4307 break; 4308 begin = 0; 4309 if (data->dy > yy || (data->dy == yy && data->dx > xx)) { 4310 /* Right to left selection. */ 4311 window_copy_cursor_previous_word_pos(wme, 4312 data->separators, &xx, &yy); 4313 begin = 1; 4314 4315 /* Reset the end. */ 4316 data->endselx = data->endselrx; 4317 data->endsely = data->endselry; 4318 } else { 4319 /* Left to right selection. */ 4320 if (xx >= window_copy_find_length(wme, yy) || 4321 !window_copy_in_set(wme, xx + 1, yy, WHITESPACE)) { 4322 window_copy_cursor_next_word_end_pos(wme, 4323 data->separators, &xx, &yy); 4324 } 4325 4326 /* Reset the start. */ 4327 data->selx = data->selrx; 4328 data->sely = data->selry; 4329 } 4330 break; 4331 case SEL_LINE: 4332 if (no_reset) 4333 break; 4334 begin = 0; 4335 if (data->dy > yy) { 4336 /* Right to left selection. */ 4337 xx = 0; 4338 begin = 1; 4339 4340 /* Reset the end. */ 4341 data->endselx = data->endselrx; 4342 data->endsely = data->endselry; 4343 } else { 4344 /* Left to right selection. */ 4345 if (yy < data->endselry) 4346 yy = data->endselry; 4347 xx = window_copy_find_length(wme, yy); 4348 4349 /* Reset the start. */ 4350 data->selx = data->selrx; 4351 data->sely = data->selry; 4352 } 4353 break; 4354 case SEL_CHAR: 4355 break; 4356 } 4357 if (begin) { 4358 data->selx = xx; 4359 data->sely = yy; 4360 } else { 4361 data->endselx = xx; 4362 data->endsely = yy; 4363 } 4364 } 4365 4366 static void 4367 window_copy_synchronize_cursor(struct window_mode_entry *wme, int no_reset) 4368 { 4369 struct window_copy_mode_data *data = wme->data; 4370 4371 switch (data->cursordrag) { 4372 case CURSORDRAG_ENDSEL: 4373 window_copy_synchronize_cursor_end(wme, 0, no_reset); 4374 break; 4375 case CURSORDRAG_SEL: 4376 window_copy_synchronize_cursor_end(wme, 1, no_reset); 4377 break; 4378 case CURSORDRAG_NONE: 4379 break; 4380 } 4381 } 4382 4383 static void 4384 window_copy_update_cursor(struct window_mode_entry *wme, u_int cx, u_int cy) 4385 { 4386 struct window_pane *wp = wme->wp; 4387 struct window_copy_mode_data *data = wme->data; 4388 struct screen *s = &data->screen; 4389 struct screen_write_ctx ctx; 4390 u_int old_cx, old_cy; 4391 4392 old_cx = data->cx; old_cy = data->cy; 4393 data->cx = cx; data->cy = cy; 4394 if (old_cx == screen_size_x(s)) 4395 window_copy_redraw_lines(wme, old_cy, 1); 4396 if (data->cx == screen_size_x(s)) 4397 window_copy_redraw_lines(wme, data->cy, 1); 4398 else { 4399 screen_write_start_pane(&ctx, wp, NULL); 4400 screen_write_cursormove(&ctx, data->cx, data->cy, 0); 4401 screen_write_stop(&ctx); 4402 } 4403 } 4404 4405 static void 4406 window_copy_start_selection(struct window_mode_entry *wme) 4407 { 4408 struct window_copy_mode_data *data = wme->data; 4409 4410 data->selx = data->cx; 4411 data->sely = screen_hsize(data->backing) + data->cy - data->oy; 4412 4413 data->endselx = data->selx; 4414 data->endsely = data->sely; 4415 4416 data->cursordrag = CURSORDRAG_ENDSEL; 4417 4418 window_copy_set_selection(wme, 1, 0); 4419 } 4420 4421 static int 4422 window_copy_adjust_selection(struct window_mode_entry *wme, u_int *selx, 4423 u_int *sely) 4424 { 4425 struct window_copy_mode_data *data = wme->data; 4426 struct screen *s = &data->screen; 4427 u_int sx, sy, ty; 4428 int relpos; 4429 4430 sx = *selx; 4431 sy = *sely; 4432 4433 ty = screen_hsize(data->backing) - data->oy; 4434 if (sy < ty) { 4435 relpos = WINDOW_COPY_REL_POS_ABOVE; 4436 if (!data->rectflag) 4437 sx = 0; 4438 sy = 0; 4439 } else if (sy > ty + screen_size_y(s) - 1) { 4440 relpos = WINDOW_COPY_REL_POS_BELOW; 4441 if (!data->rectflag) 4442 sx = screen_size_x(s) - 1; 4443 sy = screen_size_y(s) - 1; 4444 } else { 4445 relpos = WINDOW_COPY_REL_POS_ON_SCREEN; 4446 sy -= ty; 4447 } 4448 4449 *selx = sx; 4450 *sely = sy; 4451 return (relpos); 4452 } 4453 4454 static int 4455 window_copy_update_selection(struct window_mode_entry *wme, int may_redraw, 4456 int no_reset) 4457 { 4458 struct window_copy_mode_data *data = wme->data; 4459 struct screen *s = &data->screen; 4460 4461 if (s->sel == NULL && data->lineflag == LINE_SEL_NONE) 4462 return (0); 4463 return (window_copy_set_selection(wme, may_redraw, no_reset)); 4464 } 4465 4466 static int 4467 window_copy_set_selection(struct window_mode_entry *wme, int may_redraw, 4468 int no_reset) 4469 { 4470 struct window_pane *wp = wme->wp; 4471 struct window_copy_mode_data *data = wme->data; 4472 struct screen *s = &data->screen; 4473 struct options *oo = wp->window->options; 4474 struct grid_cell gc; 4475 u_int sx, sy, cy, endsx, endsy; 4476 int startrelpos, endrelpos; 4477 4478 window_copy_synchronize_cursor(wme, no_reset); 4479 4480 /* Adjust the selection. */ 4481 sx = data->selx; 4482 sy = data->sely; 4483 startrelpos = window_copy_adjust_selection(wme, &sx, &sy); 4484 4485 /* Adjust the end of selection. */ 4486 endsx = data->endselx; 4487 endsy = data->endsely; 4488 endrelpos = window_copy_adjust_selection(wme, &endsx, &endsy); 4489 4490 /* Selection is outside of the current screen */ 4491 if (startrelpos == endrelpos && 4492 startrelpos != WINDOW_COPY_REL_POS_ON_SCREEN) { 4493 screen_hide_selection(s); 4494 return (0); 4495 } 4496 4497 /* Set colours and selection. */ 4498 style_apply(&gc, oo, "mode-style", NULL); 4499 gc.flags |= GRID_FLAG_NOPALETTE; 4500 screen_set_selection(s, sx, sy, endsx, endsy, data->rectflag, 4501 data->modekeys, &gc); 4502 4503 if (data->rectflag && may_redraw) { 4504 /* 4505 * Can't rely on the caller to redraw the right lines for 4506 * rectangle selection - find the highest line and the number 4507 * of lines, and redraw just past that in both directions 4508 */ 4509 cy = data->cy; 4510 if (data->cursordrag == CURSORDRAG_ENDSEL) { 4511 if (sy < cy) 4512 window_copy_redraw_lines(wme, sy, cy - sy + 1); 4513 else 4514 window_copy_redraw_lines(wme, cy, sy - cy + 1); 4515 } else { 4516 if (endsy < cy) { 4517 window_copy_redraw_lines(wme, endsy, 4518 cy - endsy + 1); 4519 } else { 4520 window_copy_redraw_lines(wme, cy, 4521 endsy - cy + 1); 4522 } 4523 } 4524 } 4525 4526 return (1); 4527 } 4528 4529 static void * 4530 window_copy_get_selection(struct window_mode_entry *wme, size_t *len) 4531 { 4532 struct window_pane *wp = wme->wp; 4533 struct window_copy_mode_data *data = wme->data; 4534 struct screen *s = &data->screen; 4535 char *buf; 4536 size_t off; 4537 u_int i, xx, yy, sx, sy, ex, ey, ey_last; 4538 u_int firstsx, lastex, restex, restsx, selx; 4539 int keys; 4540 4541 if (data->screen.sel == NULL && data->lineflag == LINE_SEL_NONE) { 4542 buf = window_copy_match_at_cursor(data); 4543 if (buf != NULL) 4544 *len = strlen(buf); 4545 else 4546 *len = 0; 4547 return (buf); 4548 } 4549 4550 buf = xmalloc(1); 4551 off = 0; 4552 4553 *buf = '\0'; 4554 4555 /* 4556 * The selection extends from selx,sely to (adjusted) cx,cy on 4557 * the base screen. 4558 */ 4559 4560 /* Find start and end. */ 4561 xx = data->endselx; 4562 yy = data->endsely; 4563 if (yy < data->sely || (yy == data->sely && xx < data->selx)) { 4564 sx = xx; sy = yy; 4565 ex = data->selx; ey = data->sely; 4566 } else { 4567 sx = data->selx; sy = data->sely; 4568 ex = xx; ey = yy; 4569 } 4570 4571 /* Trim ex to end of line. */ 4572 ey_last = window_copy_find_length(wme, ey); 4573 if (ex > ey_last) 4574 ex = ey_last; 4575 4576 /* 4577 * Deal with rectangle-copy if necessary; four situations: start of 4578 * first line (firstsx), end of last line (lastex), start (restsx) and 4579 * end (restex) of all other lines. 4580 */ 4581 xx = screen_size_x(s); 4582 4583 /* 4584 * Behave according to mode-keys. If it is emacs, copy like emacs, 4585 * keeping the top-left-most character, and dropping the 4586 * bottom-right-most, regardless of copy direction. If it is vi, also 4587 * keep bottom-right-most character. 4588 */ 4589 keys = options_get_number(wp->window->options, "mode-keys"); 4590 if (data->rectflag) { 4591 /* 4592 * Need to ignore the column with the cursor in it, which for 4593 * rectangular copy means knowing which side the cursor is on. 4594 */ 4595 if (data->cursordrag == CURSORDRAG_ENDSEL) 4596 selx = data->selx; 4597 else 4598 selx = data->endselx; 4599 if (selx < data->cx) { 4600 /* Selection start is on the left. */ 4601 if (keys == MODEKEY_EMACS) { 4602 lastex = data->cx; 4603 restex = data->cx; 4604 } 4605 else { 4606 lastex = data->cx + 1; 4607 restex = data->cx + 1; 4608 } 4609 firstsx = selx; 4610 restsx = selx; 4611 } else { 4612 /* Cursor is on the left. */ 4613 lastex = selx + 1; 4614 restex = selx + 1; 4615 firstsx = data->cx; 4616 restsx = data->cx; 4617 } 4618 } else { 4619 if (keys == MODEKEY_EMACS) 4620 lastex = ex; 4621 else 4622 lastex = ex + 1; 4623 restex = xx; 4624 firstsx = sx; 4625 restsx = 0; 4626 } 4627 4628 /* Copy the lines. */ 4629 for (i = sy; i <= ey; i++) { 4630 window_copy_copy_line(wme, &buf, &off, i, 4631 (i == sy ? firstsx : restsx), 4632 (i == ey ? lastex : restex)); 4633 } 4634 4635 /* Don't bother if no data. */ 4636 if (off == 0) { 4637 free(buf); 4638 *len = 0; 4639 return (NULL); 4640 } 4641 /* Remove final \n (unless at end in vi mode). */ 4642 if (keys == MODEKEY_EMACS || lastex <= ey_last) { 4643 if (~grid_get_line(data->backing->grid, ey)->flags & 4644 GRID_LINE_WRAPPED || lastex != ey_last) 4645 off -= 1; 4646 } 4647 *len = off; 4648 return (buf); 4649 } 4650 4651 static void 4652 window_copy_copy_buffer(struct window_mode_entry *wme, const char *prefix, 4653 void *buf, size_t len) 4654 { 4655 struct window_pane *wp = wme->wp; 4656 struct screen_write_ctx ctx; 4657 4658 if (options_get_number(global_options, "set-clipboard") != 0) { 4659 screen_write_start_pane(&ctx, wp, NULL); 4660 screen_write_setselection(&ctx, "", buf, len); 4661 screen_write_stop(&ctx); 4662 notify_pane("pane-set-clipboard", wp); 4663 } 4664 4665 paste_add(prefix, buf, len); 4666 } 4667 4668 static void * 4669 window_copy_pipe_run(struct window_mode_entry *wme, struct session *s, 4670 const char *cmd, size_t *len) 4671 { 4672 void *buf; 4673 struct job *job; 4674 4675 buf = window_copy_get_selection(wme, len); 4676 if (cmd == NULL || *cmd == '\0') 4677 cmd = options_get_string(global_options, "copy-command"); 4678 if (cmd != NULL && *cmd != '\0') { 4679 job = job_run(cmd, 0, NULL, NULL, s, NULL, NULL, NULL, NULL, 4680 NULL, JOB_NOWAIT, -1, -1); 4681 bufferevent_write(job_get_event(job), buf, *len); 4682 } 4683 return (buf); 4684 } 4685 4686 static void 4687 window_copy_pipe(struct window_mode_entry *wme, struct session *s, 4688 const char *cmd) 4689 { 4690 size_t len; 4691 4692 window_copy_pipe_run(wme, s, cmd, &len); 4693 } 4694 4695 static void 4696 window_copy_copy_pipe(struct window_mode_entry *wme, struct session *s, 4697 const char *prefix, const char *cmd) 4698 { 4699 void *buf; 4700 size_t len; 4701 4702 buf = window_copy_pipe_run(wme, s, cmd, &len); 4703 if (buf != NULL) 4704 window_copy_copy_buffer(wme, prefix, buf, len); 4705 } 4706 4707 static void 4708 window_copy_copy_selection(struct window_mode_entry *wme, const char *prefix) 4709 { 4710 char *buf; 4711 size_t len; 4712 4713 buf = window_copy_get_selection(wme, &len); 4714 if (buf != NULL) 4715 window_copy_copy_buffer(wme, prefix, buf, len); 4716 } 4717 4718 static void 4719 window_copy_append_selection(struct window_mode_entry *wme) 4720 { 4721 struct window_pane *wp = wme->wp; 4722 char *buf; 4723 struct paste_buffer *pb; 4724 const char *bufdata, *bufname = NULL; 4725 size_t len, bufsize; 4726 struct screen_write_ctx ctx; 4727 4728 buf = window_copy_get_selection(wme, &len); 4729 if (buf == NULL) 4730 return; 4731 4732 if (options_get_number(global_options, "set-clipboard") != 0) { 4733 screen_write_start_pane(&ctx, wp, NULL); 4734 screen_write_setselection(&ctx, "", buf, len); 4735 screen_write_stop(&ctx); 4736 notify_pane("pane-set-clipboard", wp); 4737 } 4738 4739 pb = paste_get_top(&bufname); 4740 if (pb != NULL) { 4741 bufdata = paste_buffer_data(pb, &bufsize); 4742 buf = xrealloc(buf, len + bufsize); 4743 memmove(buf + bufsize, buf, len); 4744 memcpy(buf, bufdata, bufsize); 4745 len += bufsize; 4746 } 4747 if (paste_set(buf, len, bufname, NULL) != 0) 4748 free(buf); 4749 } 4750 4751 static void 4752 window_copy_copy_line(struct window_mode_entry *wme, char **buf, size_t *off, 4753 u_int sy, u_int sx, u_int ex) 4754 { 4755 struct window_copy_mode_data *data = wme->data; 4756 struct grid *gd = data->backing->grid; 4757 struct grid_cell gc; 4758 struct grid_line *gl; 4759 struct utf8_data ud; 4760 u_int i, xx, wrapped = 0; 4761 const char *s; 4762 4763 if (sx > ex) 4764 return; 4765 4766 /* 4767 * Work out if the line was wrapped at the screen edge and all of it is 4768 * on screen. 4769 */ 4770 gl = grid_get_line(gd, sy); 4771 if (gl->flags & GRID_LINE_WRAPPED && gl->cellsize <= gd->sx) 4772 wrapped = 1; 4773 4774 /* If the line was wrapped, don't strip spaces (use the full length). */ 4775 if (wrapped) 4776 xx = gl->cellsize; 4777 else 4778 xx = window_copy_find_length(wme, sy); 4779 if (ex > xx) 4780 ex = xx; 4781 if (sx > xx) 4782 sx = xx; 4783 4784 if (sx < ex) { 4785 for (i = sx; i < ex; i++) { 4786 grid_get_cell(gd, i, sy, &gc); 4787 if (gc.flags & GRID_FLAG_PADDING) 4788 continue; 4789 utf8_copy(&ud, &gc.data); 4790 if (ud.size == 1 && (gc.attr & GRID_ATTR_CHARSET)) { 4791 s = tty_acs_get(NULL, ud.data[0]); 4792 if (s != NULL && strlen(s) <= sizeof ud.data) { 4793 ud.size = strlen(s); 4794 memcpy(ud.data, s, ud.size); 4795 } 4796 } 4797 4798 *buf = xrealloc(*buf, (*off) + ud.size); 4799 memcpy(*buf + *off, ud.data, ud.size); 4800 *off += ud.size; 4801 } 4802 } 4803 4804 /* Only add a newline if the line wasn't wrapped. */ 4805 if (!wrapped || ex != xx) { 4806 *buf = xrealloc(*buf, (*off) + 1); 4807 (*buf)[(*off)++] = '\n'; 4808 } 4809 } 4810 4811 static void 4812 window_copy_clear_selection(struct window_mode_entry *wme) 4813 { 4814 struct window_copy_mode_data *data = wme->data; 4815 u_int px, py; 4816 4817 screen_clear_selection(&data->screen); 4818 4819 data->cursordrag = CURSORDRAG_NONE; 4820 data->lineflag = LINE_SEL_NONE; 4821 data->selflag = SEL_CHAR; 4822 4823 py = screen_hsize(data->backing) + data->cy - data->oy; 4824 px = window_copy_find_length(wme, py); 4825 if (data->cx > px) 4826 window_copy_update_cursor(wme, px, data->cy); 4827 } 4828 4829 static int 4830 window_copy_in_set(struct window_mode_entry *wme, u_int px, u_int py, 4831 const char *set) 4832 { 4833 struct window_copy_mode_data *data = wme->data; 4834 struct grid_cell gc; 4835 4836 grid_get_cell(data->backing->grid, px, py, &gc); 4837 if (gc.flags & GRID_FLAG_PADDING) 4838 return (0); 4839 return (utf8_cstrhas(set, &gc.data)); 4840 } 4841 4842 static u_int 4843 window_copy_find_length(struct window_mode_entry *wme, u_int py) 4844 { 4845 struct window_copy_mode_data *data = wme->data; 4846 4847 return (grid_line_length(data->backing->grid, py)); 4848 } 4849 4850 static void 4851 window_copy_cursor_start_of_line(struct window_mode_entry *wme) 4852 { 4853 struct window_copy_mode_data *data = wme->data; 4854 struct screen *back_s = data->backing; 4855 struct grid_reader gr; 4856 u_int px, py, oldy, hsize; 4857 4858 px = data->cx; 4859 hsize = screen_hsize(back_s); 4860 py = hsize + data->cy - data->oy; 4861 oldy = data->cy; 4862 4863 grid_reader_start(&gr, back_s->grid, px, py); 4864 grid_reader_cursor_start_of_line(&gr, 1); 4865 grid_reader_get_cursor(&gr, &px, &py); 4866 window_copy_acquire_cursor_up(wme, hsize, data->oy, oldy, px, py); 4867 } 4868 4869 static void 4870 window_copy_cursor_back_to_indentation(struct window_mode_entry *wme) 4871 { 4872 struct window_copy_mode_data *data = wme->data; 4873 struct screen *back_s = data->backing; 4874 struct grid_reader gr; 4875 u_int px, py, oldy, hsize; 4876 4877 px = data->cx; 4878 hsize = screen_hsize(back_s); 4879 py = hsize + data->cy - data->oy; 4880 oldy = data->cy; 4881 4882 grid_reader_start(&gr, back_s->grid, px, py); 4883 grid_reader_cursor_back_to_indentation(&gr); 4884 grid_reader_get_cursor(&gr, &px, &py); 4885 window_copy_acquire_cursor_up(wme, hsize, data->oy, oldy, px, py); 4886 } 4887 4888 static void 4889 window_copy_cursor_end_of_line(struct window_mode_entry *wme) 4890 { 4891 struct window_copy_mode_data *data = wme->data; 4892 struct screen *back_s = data->backing; 4893 struct grid_reader gr; 4894 u_int px, py, oldy, hsize; 4895 4896 px = data->cx; 4897 hsize = screen_hsize(back_s); 4898 py = hsize + data->cy - data->oy; 4899 oldy = data->cy; 4900 4901 grid_reader_start(&gr, back_s->grid, px, py); 4902 if (data->screen.sel != NULL && data->rectflag) 4903 grid_reader_cursor_end_of_line(&gr, 1, 1); 4904 else 4905 grid_reader_cursor_end_of_line(&gr, 1, 0); 4906 grid_reader_get_cursor(&gr, &px, &py); 4907 window_copy_acquire_cursor_down(wme, hsize, screen_size_y(back_s), 4908 data->oy, oldy, px, py, 0); 4909 } 4910 4911 static void 4912 window_copy_other_end(struct window_mode_entry *wme) 4913 { 4914 struct window_copy_mode_data *data = wme->data; 4915 struct screen *s = &data->screen; 4916 u_int selx, sely, cy, yy, hsize; 4917 4918 if (s->sel == NULL && data->lineflag == LINE_SEL_NONE) 4919 return; 4920 4921 if (data->lineflag == LINE_SEL_LEFT_RIGHT) 4922 data->lineflag = LINE_SEL_RIGHT_LEFT; 4923 else if (data->lineflag == LINE_SEL_RIGHT_LEFT) 4924 data->lineflag = LINE_SEL_LEFT_RIGHT; 4925 4926 switch (data->cursordrag) { 4927 case CURSORDRAG_NONE: 4928 case CURSORDRAG_SEL: 4929 data->cursordrag = CURSORDRAG_ENDSEL; 4930 break; 4931 case CURSORDRAG_ENDSEL: 4932 data->cursordrag = CURSORDRAG_SEL; 4933 break; 4934 } 4935 4936 selx = data->endselx; 4937 sely = data->endsely; 4938 if (data->cursordrag == CURSORDRAG_SEL) { 4939 selx = data->selx; 4940 sely = data->sely; 4941 } 4942 4943 cy = data->cy; 4944 yy = screen_hsize(data->backing) + data->cy - data->oy; 4945 4946 data->cx = selx; 4947 4948 hsize = screen_hsize(data->backing); 4949 if (sely < hsize - data->oy) { /* above */ 4950 data->oy = hsize - sely; 4951 data->cy = 0; 4952 } else if (sely > hsize - data->oy + screen_size_y(s)) { /* below */ 4953 data->oy = hsize - sely + screen_size_y(s) - 1; 4954 data->cy = screen_size_y(s) - 1; 4955 } else 4956 data->cy = cy + sely - yy; 4957 4958 window_copy_update_selection(wme, 1, 1); 4959 window_copy_redraw_screen(wme); 4960 } 4961 4962 static void 4963 window_copy_cursor_left(struct window_mode_entry *wme) 4964 { 4965 struct window_copy_mode_data *data = wme->data; 4966 struct screen *back_s = data->backing; 4967 struct grid_reader gr; 4968 u_int px, py, oldy, hsize; 4969 4970 px = data->cx; 4971 hsize = screen_hsize(back_s); 4972 py = hsize + data->cy - data->oy; 4973 oldy = data->cy; 4974 4975 grid_reader_start(&gr, back_s->grid, px, py); 4976 grid_reader_cursor_left(&gr, 1); 4977 grid_reader_get_cursor(&gr, &px, &py); 4978 window_copy_acquire_cursor_up(wme, hsize, data->oy, oldy, px, py); 4979 } 4980 4981 static void 4982 window_copy_cursor_right(struct window_mode_entry *wme, int all) 4983 { 4984 struct window_copy_mode_data *data = wme->data; 4985 struct screen *back_s = data->backing; 4986 struct grid_reader gr; 4987 u_int px, py, oldy, hsize; 4988 4989 px = data->cx; 4990 hsize = screen_hsize(back_s); 4991 py = hsize + data->cy - data->oy; 4992 oldy = data->cy; 4993 4994 grid_reader_start(&gr, back_s->grid, px, py); 4995 grid_reader_cursor_right(&gr, 1, all); 4996 grid_reader_get_cursor(&gr, &px, &py); 4997 window_copy_acquire_cursor_down(wme, hsize, screen_size_y(back_s), 4998 data->oy, oldy, px, py, 0); 4999 } 5000 5001 static void 5002 window_copy_cursor_up(struct window_mode_entry *wme, int scroll_only) 5003 { 5004 struct window_copy_mode_data *data = wme->data; 5005 struct screen *s = &data->screen; 5006 u_int ox, oy, px, py; 5007 int norectsel; 5008 5009 norectsel = data->screen.sel == NULL || !data->rectflag; 5010 oy = screen_hsize(data->backing) + data->cy - data->oy; 5011 ox = window_copy_find_length(wme, oy); 5012 if (norectsel && data->cx != ox) { 5013 data->lastcx = data->cx; 5014 data->lastsx = ox; 5015 } 5016 5017 if (data->lineflag == LINE_SEL_LEFT_RIGHT && oy == data->sely) 5018 window_copy_other_end(wme); 5019 5020 if (scroll_only || data->cy == 0) { 5021 if (norectsel) 5022 data->cx = data->lastcx; 5023 window_copy_scroll_down(wme, 1); 5024 if (scroll_only) { 5025 if (data->cy == screen_size_y(s) - 1) 5026 window_copy_redraw_lines(wme, data->cy, 1); 5027 else 5028 window_copy_redraw_lines(wme, data->cy, 2); 5029 } 5030 } else { 5031 if (norectsel) { 5032 window_copy_update_cursor(wme, data->lastcx, 5033 data->cy - 1); 5034 } else 5035 window_copy_update_cursor(wme, data->cx, data->cy - 1); 5036 if (window_copy_update_selection(wme, 1, 0)) { 5037 if (data->cy == screen_size_y(s) - 1) 5038 window_copy_redraw_lines(wme, data->cy, 1); 5039 else 5040 window_copy_redraw_lines(wme, data->cy, 2); 5041 } 5042 } 5043 5044 if (norectsel) { 5045 py = screen_hsize(data->backing) + data->cy - data->oy; 5046 px = window_copy_find_length(wme, py); 5047 if ((data->cx >= data->lastsx && data->cx != px) || 5048 data->cx > px) 5049 { 5050 window_copy_update_cursor(wme, px, data->cy); 5051 if (window_copy_update_selection(wme, 1, 0)) 5052 window_copy_redraw_lines(wme, data->cy, 1); 5053 } 5054 } 5055 5056 if (data->lineflag == LINE_SEL_LEFT_RIGHT) 5057 { 5058 py = screen_hsize(data->backing) + data->cy - data->oy; 5059 if (data->rectflag) 5060 px = screen_size_x(data->backing); 5061 else 5062 px = window_copy_find_length(wme, py); 5063 window_copy_update_cursor(wme, px, data->cy); 5064 if (window_copy_update_selection(wme, 1, 0)) 5065 window_copy_redraw_lines(wme, data->cy, 1); 5066 } 5067 else if (data->lineflag == LINE_SEL_RIGHT_LEFT) 5068 { 5069 window_copy_update_cursor(wme, 0, data->cy); 5070 if (window_copy_update_selection(wme, 1, 0)) 5071 window_copy_redraw_lines(wme, data->cy, 1); 5072 } 5073 } 5074 5075 static void 5076 window_copy_cursor_down(struct window_mode_entry *wme, int scroll_only) 5077 { 5078 struct window_copy_mode_data *data = wme->data; 5079 struct screen *s = &data->screen; 5080 u_int ox, oy, px, py; 5081 int norectsel; 5082 5083 norectsel = data->screen.sel == NULL || !data->rectflag; 5084 oy = screen_hsize(data->backing) + data->cy - data->oy; 5085 ox = window_copy_find_length(wme, oy); 5086 if (norectsel && data->cx != ox) { 5087 data->lastcx = data->cx; 5088 data->lastsx = ox; 5089 } 5090 5091 if (data->lineflag == LINE_SEL_RIGHT_LEFT && oy == data->endsely) 5092 window_copy_other_end(wme); 5093 5094 if (scroll_only || data->cy == screen_size_y(s) - 1) { 5095 if (norectsel) 5096 data->cx = data->lastcx; 5097 window_copy_scroll_up(wme, 1); 5098 if (scroll_only && data->cy > 0) 5099 window_copy_redraw_lines(wme, data->cy - 1, 2); 5100 } else { 5101 if (norectsel) { 5102 window_copy_update_cursor(wme, data->lastcx, 5103 data->cy + 1); 5104 } else 5105 window_copy_update_cursor(wme, data->cx, data->cy + 1); 5106 if (window_copy_update_selection(wme, 1, 0)) 5107 window_copy_redraw_lines(wme, data->cy - 1, 2); 5108 } 5109 5110 if (norectsel) { 5111 py = screen_hsize(data->backing) + data->cy - data->oy; 5112 px = window_copy_find_length(wme, py); 5113 if ((data->cx >= data->lastsx && data->cx != px) || 5114 data->cx > px) 5115 { 5116 window_copy_update_cursor(wme, px, data->cy); 5117 if (window_copy_update_selection(wme, 1, 0)) 5118 window_copy_redraw_lines(wme, data->cy, 1); 5119 } 5120 } 5121 5122 if (data->lineflag == LINE_SEL_LEFT_RIGHT) 5123 { 5124 py = screen_hsize(data->backing) + data->cy - data->oy; 5125 if (data->rectflag) 5126 px = screen_size_x(data->backing); 5127 else 5128 px = window_copy_find_length(wme, py); 5129 window_copy_update_cursor(wme, px, data->cy); 5130 if (window_copy_update_selection(wme, 1, 0)) 5131 window_copy_redraw_lines(wme, data->cy, 1); 5132 } 5133 else if (data->lineflag == LINE_SEL_RIGHT_LEFT) 5134 { 5135 window_copy_update_cursor(wme, 0, data->cy); 5136 if (window_copy_update_selection(wme, 1, 0)) 5137 window_copy_redraw_lines(wme, data->cy, 1); 5138 } 5139 } 5140 5141 static void 5142 window_copy_cursor_jump(struct window_mode_entry *wme) 5143 { 5144 struct window_copy_mode_data *data = wme->data; 5145 struct screen *back_s = data->backing; 5146 struct grid_reader gr; 5147 u_int px, py, oldy, hsize; 5148 5149 px = data->cx + 1; 5150 hsize = screen_hsize(back_s); 5151 py = hsize + data->cy - data->oy; 5152 oldy = data->cy; 5153 5154 grid_reader_start(&gr, back_s->grid, px, py); 5155 if (grid_reader_cursor_jump(&gr, data->jumpchar)) { 5156 grid_reader_get_cursor(&gr, &px, &py); 5157 window_copy_acquire_cursor_down(wme, hsize, 5158 screen_size_y(back_s), data->oy, oldy, px, py, 0); 5159 } 5160 } 5161 5162 static void 5163 window_copy_cursor_jump_back(struct window_mode_entry *wme) 5164 { 5165 struct window_copy_mode_data *data = wme->data; 5166 struct screen *back_s = data->backing; 5167 struct grid_reader gr; 5168 u_int px, py, oldy, hsize; 5169 5170 px = data->cx; 5171 hsize = screen_hsize(back_s); 5172 py = hsize + data->cy - data->oy; 5173 oldy = data->cy; 5174 5175 grid_reader_start(&gr, back_s->grid, px, py); 5176 grid_reader_cursor_left(&gr, 0); 5177 if (grid_reader_cursor_jump_back(&gr, data->jumpchar)) { 5178 grid_reader_get_cursor(&gr, &px, &py); 5179 window_copy_acquire_cursor_up(wme, hsize, data->oy, oldy, px, 5180 py); 5181 } 5182 } 5183 5184 static void 5185 window_copy_cursor_jump_to(struct window_mode_entry *wme) 5186 { 5187 struct window_copy_mode_data *data = wme->data; 5188 struct screen *back_s = data->backing; 5189 struct grid_reader gr; 5190 u_int px, py, oldy, hsize; 5191 5192 px = data->cx + 2; 5193 hsize = screen_hsize(back_s); 5194 py = hsize + data->cy - data->oy; 5195 oldy = data->cy; 5196 5197 grid_reader_start(&gr, back_s->grid, px, py); 5198 if (grid_reader_cursor_jump(&gr, data->jumpchar)) { 5199 grid_reader_cursor_left(&gr, 1); 5200 grid_reader_get_cursor(&gr, &px, &py); 5201 window_copy_acquire_cursor_down(wme, hsize, 5202 screen_size_y(back_s), data->oy, oldy, px, py, 0); 5203 } 5204 } 5205 5206 static void 5207 window_copy_cursor_jump_to_back(struct window_mode_entry *wme) 5208 { 5209 struct window_copy_mode_data *data = wme->data; 5210 struct screen *back_s = data->backing; 5211 struct grid_reader gr; 5212 u_int px, py, oldy, hsize; 5213 5214 px = data->cx; 5215 hsize = screen_hsize(back_s); 5216 py = hsize + data->cy - data->oy; 5217 oldy = data->cy; 5218 5219 grid_reader_start(&gr, back_s->grid, px, py); 5220 grid_reader_cursor_left(&gr, 0); 5221 grid_reader_cursor_left(&gr, 0); 5222 if (grid_reader_cursor_jump_back(&gr, data->jumpchar)) { 5223 grid_reader_cursor_right(&gr, 1, 0); 5224 grid_reader_get_cursor(&gr, &px, &py); 5225 window_copy_acquire_cursor_up(wme, hsize, data->oy, oldy, px, 5226 py); 5227 } 5228 } 5229 5230 static void 5231 window_copy_cursor_next_word(struct window_mode_entry *wme, 5232 const char *separators) 5233 { 5234 struct window_copy_mode_data *data = wme->data; 5235 struct screen *back_s = data->backing; 5236 struct grid_reader gr; 5237 u_int px, py, oldy, hsize; 5238 5239 px = data->cx; 5240 hsize = screen_hsize(back_s); 5241 py = hsize + data->cy - data->oy; 5242 oldy = data->cy; 5243 5244 grid_reader_start(&gr, back_s->grid, px, py); 5245 grid_reader_cursor_next_word(&gr, separators); 5246 grid_reader_get_cursor(&gr, &px, &py); 5247 window_copy_acquire_cursor_down(wme, hsize, screen_size_y(back_s), 5248 data->oy, oldy, px, py, 0); 5249 } 5250 5251 /* Compute the next place where a word ends. */ 5252 static void 5253 window_copy_cursor_next_word_end_pos(struct window_mode_entry *wme, 5254 const char *separators, u_int *ppx, u_int *ppy) 5255 { 5256 struct window_pane *wp = wme->wp; 5257 struct window_copy_mode_data *data = wme->data; 5258 struct options *oo = wp->window->options; 5259 struct screen *back_s = data->backing; 5260 struct grid_reader gr; 5261 u_int px, py, hsize; 5262 5263 px = data->cx; 5264 hsize = screen_hsize(back_s); 5265 py = hsize + data->cy - data->oy; 5266 5267 grid_reader_start(&gr, back_s->grid, px, py); 5268 if (options_get_number(oo, "mode-keys") == MODEKEY_VI) { 5269 if (!grid_reader_in_set(&gr, WHITESPACE)) 5270 grid_reader_cursor_right(&gr, 0, 0); 5271 grid_reader_cursor_next_word_end(&gr, separators); 5272 grid_reader_cursor_left(&gr, 1); 5273 } else 5274 grid_reader_cursor_next_word_end(&gr, separators); 5275 grid_reader_get_cursor(&gr, &px, &py); 5276 *ppx = px; 5277 *ppy = py; 5278 } 5279 5280 /* Move to the next place where a word ends. */ 5281 static void 5282 window_copy_cursor_next_word_end(struct window_mode_entry *wme, 5283 const char *separators, int no_reset) 5284 { 5285 struct window_pane *wp = wme->wp; 5286 struct window_copy_mode_data *data = wme->data; 5287 struct options *oo = wp->window->options; 5288 struct screen *back_s = data->backing; 5289 struct grid_reader gr; 5290 u_int px, py, oldy, hsize; 5291 5292 px = data->cx; 5293 hsize = screen_hsize(back_s); 5294 py = hsize + data->cy - data->oy; 5295 oldy = data->cy; 5296 5297 grid_reader_start(&gr, back_s->grid, px, py); 5298 if (options_get_number(oo, "mode-keys") == MODEKEY_VI) { 5299 if (!grid_reader_in_set(&gr, WHITESPACE)) 5300 grid_reader_cursor_right(&gr, 0, 0); 5301 grid_reader_cursor_next_word_end(&gr, separators); 5302 grid_reader_cursor_left(&gr, 1); 5303 } else 5304 grid_reader_cursor_next_word_end(&gr, separators); 5305 grid_reader_get_cursor(&gr, &px, &py); 5306 window_copy_acquire_cursor_down(wme, hsize, screen_size_y(back_s), 5307 data->oy, oldy, px, py, no_reset); 5308 } 5309 5310 /* Compute the previous place where a word begins. */ 5311 static void 5312 window_copy_cursor_previous_word_pos(struct window_mode_entry *wme, 5313 const char *separators, u_int *ppx, u_int *ppy) 5314 { 5315 struct window_copy_mode_data *data = wme->data; 5316 struct screen *back_s = data->backing; 5317 struct grid_reader gr; 5318 u_int px, py, hsize; 5319 5320 px = data->cx; 5321 hsize = screen_hsize(back_s); 5322 py = hsize + data->cy - data->oy; 5323 5324 grid_reader_start(&gr, back_s->grid, px, py); 5325 grid_reader_cursor_previous_word(&gr, separators, /* already= */ 0, 5326 /* stop_at_eol= */ 1); 5327 grid_reader_get_cursor(&gr, &px, &py); 5328 *ppx = px; 5329 *ppy = py; 5330 } 5331 5332 /* Move to the previous place where a word begins. */ 5333 static void 5334 window_copy_cursor_previous_word(struct window_mode_entry *wme, 5335 const char *separators, int already) 5336 { 5337 struct window_copy_mode_data *data = wme->data; 5338 struct window *w = wme->wp->window; 5339 struct screen *back_s = data->backing; 5340 struct grid_reader gr; 5341 u_int px, py, oldy, hsize; 5342 int stop_at_eol; 5343 5344 if (options_get_number(w->options, "mode-keys") == MODEKEY_EMACS) 5345 stop_at_eol = 1; 5346 else 5347 stop_at_eol = 0; 5348 5349 px = data->cx; 5350 hsize = screen_hsize(back_s); 5351 py = hsize + data->cy - data->oy; 5352 oldy = data->cy; 5353 5354 grid_reader_start(&gr, back_s->grid, px, py); 5355 grid_reader_cursor_previous_word(&gr, separators, already, stop_at_eol); 5356 grid_reader_get_cursor(&gr, &px, &py); 5357 window_copy_acquire_cursor_up(wme, hsize, data->oy, oldy, px, py); 5358 } 5359 5360 static void 5361 window_copy_scroll_up(struct window_mode_entry *wme, u_int ny) 5362 { 5363 struct window_pane *wp = wme->wp; 5364 struct window_copy_mode_data *data = wme->data; 5365 struct screen *s = &data->screen; 5366 struct screen_write_ctx ctx; 5367 5368 if (data->oy < ny) 5369 ny = data->oy; 5370 if (ny == 0) 5371 return; 5372 data->oy -= ny; 5373 5374 if (data->searchmark != NULL && !data->timeout) 5375 window_copy_search_marks(wme, NULL, data->searchregex, 1); 5376 window_copy_update_selection(wme, 0, 0); 5377 5378 screen_write_start_pane(&ctx, wp, NULL); 5379 screen_write_cursormove(&ctx, 0, 0, 0); 5380 screen_write_deleteline(&ctx, ny, 8); 5381 window_copy_write_lines(wme, &ctx, screen_size_y(s) - ny, ny); 5382 window_copy_write_line(wme, &ctx, 0); 5383 if (screen_size_y(s) > 1) 5384 window_copy_write_line(wme, &ctx, 1); 5385 if (screen_size_y(s) > 3) 5386 window_copy_write_line(wme, &ctx, screen_size_y(s) - 2); 5387 if (s->sel != NULL && screen_size_y(s) > ny) 5388 window_copy_write_line(wme, &ctx, screen_size_y(s) - ny - 1); 5389 screen_write_cursormove(&ctx, data->cx, data->cy, 0); 5390 screen_write_stop(&ctx); 5391 } 5392 5393 static void 5394 window_copy_scroll_down(struct window_mode_entry *wme, u_int ny) 5395 { 5396 struct window_pane *wp = wme->wp; 5397 struct window_copy_mode_data *data = wme->data; 5398 struct screen *s = &data->screen; 5399 struct screen_write_ctx ctx; 5400 5401 if (ny > screen_hsize(data->backing)) 5402 return; 5403 5404 if (data->oy > screen_hsize(data->backing) - ny) 5405 ny = screen_hsize(data->backing) - data->oy; 5406 if (ny == 0) 5407 return; 5408 data->oy += ny; 5409 5410 if (data->searchmark != NULL && !data->timeout) 5411 window_copy_search_marks(wme, NULL, data->searchregex, 1); 5412 window_copy_update_selection(wme, 0, 0); 5413 5414 screen_write_start_pane(&ctx, wp, NULL); 5415 screen_write_cursormove(&ctx, 0, 0, 0); 5416 screen_write_insertline(&ctx, ny, 8); 5417 window_copy_write_lines(wme, &ctx, 0, ny); 5418 if (s->sel != NULL && screen_size_y(s) > ny) 5419 window_copy_write_line(wme, &ctx, ny); 5420 else if (ny == 1) /* nuke position */ 5421 window_copy_write_line(wme, &ctx, 1); 5422 screen_write_cursormove(&ctx, data->cx, data->cy, 0); 5423 screen_write_stop(&ctx); 5424 } 5425 5426 static void 5427 window_copy_rectangle_set(struct window_mode_entry *wme, int rectflag) 5428 { 5429 struct window_copy_mode_data *data = wme->data; 5430 u_int px, py; 5431 5432 data->rectflag = rectflag; 5433 5434 py = screen_hsize(data->backing) + data->cy - data->oy; 5435 px = window_copy_find_length(wme, py); 5436 if (data->cx > px) 5437 window_copy_update_cursor(wme, px, data->cy); 5438 5439 window_copy_update_selection(wme, 1, 0); 5440 window_copy_redraw_screen(wme); 5441 } 5442 5443 static void 5444 window_copy_move_mouse(struct mouse_event *m) 5445 { 5446 struct window_pane *wp; 5447 struct window_mode_entry *wme; 5448 u_int x, y; 5449 5450 wp = cmd_mouse_pane(m, NULL, NULL); 5451 if (wp == NULL) 5452 return; 5453 wme = TAILQ_FIRST(&wp->modes); 5454 if (wme == NULL) 5455 return; 5456 if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode) 5457 return; 5458 5459 if (cmd_mouse_at(wp, m, &x, &y, 0) != 0) 5460 return; 5461 5462 window_copy_update_cursor(wme, x, y); 5463 } 5464 5465 void 5466 window_copy_start_drag(struct client *c, struct mouse_event *m) 5467 { 5468 struct window_pane *wp; 5469 struct window_mode_entry *wme; 5470 struct window_copy_mode_data *data; 5471 u_int x, y, yg; 5472 5473 if (c == NULL) 5474 return; 5475 5476 wp = cmd_mouse_pane(m, NULL, NULL); 5477 if (wp == NULL) 5478 return; 5479 wme = TAILQ_FIRST(&wp->modes); 5480 if (wme == NULL) 5481 return; 5482 if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode) 5483 return; 5484 5485 if (cmd_mouse_at(wp, m, &x, &y, 1) != 0) 5486 return; 5487 5488 c->tty.mouse_drag_update = window_copy_drag_update; 5489 c->tty.mouse_drag_release = window_copy_drag_release; 5490 5491 data = wme->data; 5492 yg = screen_hsize(data->backing) + y - data->oy; 5493 if (x < data->selrx || x > data->endselrx || yg != data->selry) 5494 data->selflag = SEL_CHAR; 5495 switch (data->selflag) { 5496 case SEL_WORD: 5497 if (data->separators != NULL) { 5498 window_copy_update_cursor(wme, x, y); 5499 window_copy_cursor_previous_word_pos(wme, 5500 data->separators, &x, &y); 5501 y -= screen_hsize(data->backing) - data->oy; 5502 } 5503 window_copy_update_cursor(wme, x, y); 5504 break; 5505 case SEL_LINE: 5506 window_copy_update_cursor(wme, 0, y); 5507 break; 5508 case SEL_CHAR: 5509 window_copy_update_cursor(wme, x, y); 5510 window_copy_start_selection(wme); 5511 break; 5512 } 5513 5514 window_copy_redraw_screen(wme); 5515 window_copy_drag_update(c, m); 5516 } 5517 5518 static void 5519 window_copy_drag_update(struct client *c, struct mouse_event *m) 5520 { 5521 struct window_pane *wp; 5522 struct window_mode_entry *wme; 5523 struct window_copy_mode_data *data; 5524 u_int x, y, old_cx, old_cy; 5525 struct timeval tv = { 5526 .tv_usec = WINDOW_COPY_DRAG_REPEAT_TIME 5527 }; 5528 5529 if (c == NULL) 5530 return; 5531 5532 wp = cmd_mouse_pane(m, NULL, NULL); 5533 if (wp == NULL) 5534 return; 5535 wme = TAILQ_FIRST(&wp->modes); 5536 if (wme == NULL) 5537 return; 5538 if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode) 5539 return; 5540 5541 data = wme->data; 5542 evtimer_del(&data->dragtimer); 5543 5544 if (cmd_mouse_at(wp, m, &x, &y, 0) != 0) 5545 return; 5546 old_cx = data->cx; 5547 old_cy = data->cy; 5548 5549 window_copy_update_cursor(wme, x, y); 5550 if (window_copy_update_selection(wme, 1, 0)) 5551 window_copy_redraw_selection(wme, old_cy); 5552 if (old_cy != data->cy || old_cx == data->cx) { 5553 if (y == 0) { 5554 evtimer_add(&data->dragtimer, &tv); 5555 window_copy_cursor_up(wme, 1); 5556 } else if (y == screen_size_y(&data->screen) - 1) { 5557 evtimer_add(&data->dragtimer, &tv); 5558 window_copy_cursor_down(wme, 1); 5559 } 5560 } 5561 } 5562 5563 static void 5564 window_copy_drag_release(struct client *c, struct mouse_event *m) 5565 { 5566 struct window_pane *wp; 5567 struct window_mode_entry *wme; 5568 struct window_copy_mode_data *data; 5569 5570 if (c == NULL) 5571 return; 5572 5573 wp = cmd_mouse_pane(m, NULL, NULL); 5574 if (wp == NULL) 5575 return; 5576 wme = TAILQ_FIRST(&wp->modes); 5577 if (wme == NULL) 5578 return; 5579 if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode) 5580 return; 5581 5582 data = wme->data; 5583 evtimer_del(&data->dragtimer); 5584 } 5585 5586 static void 5587 window_copy_jump_to_mark(struct window_mode_entry *wme) 5588 { 5589 struct window_copy_mode_data *data = wme->data; 5590 u_int tmx, tmy; 5591 5592 tmx = data->cx; 5593 tmy = screen_hsize(data->backing) + data->cy - data->oy; 5594 data->cx = data->mx; 5595 if (data->my < screen_hsize(data->backing)) { 5596 data->cy = 0; 5597 data->oy = screen_hsize(data->backing) - data->my; 5598 } else { 5599 data->cy = data->my - screen_hsize(data->backing); 5600 data->oy = 0; 5601 } 5602 data->mx = tmx; 5603 data->my = tmy; 5604 data->showmark = 1; 5605 window_copy_update_selection(wme, 0, 0); 5606 window_copy_redraw_screen(wme); 5607 } 5608 5609 /* Scroll up if the cursor went off the visible screen. */ 5610 static void 5611 window_copy_acquire_cursor_up(struct window_mode_entry *wme, u_int hsize, 5612 u_int oy, u_int oldy, u_int px, u_int py) 5613 { 5614 u_int cy, yy, ny, nd; 5615 5616 yy = hsize - oy; 5617 if (py < yy) { 5618 ny = yy - py; 5619 cy = 0; 5620 nd = 1; 5621 } else { 5622 ny = 0; 5623 cy = py - yy; 5624 nd = oldy - cy + 1; 5625 } 5626 while (ny > 0) { 5627 window_copy_cursor_up(wme, 1); 5628 ny--; 5629 } 5630 window_copy_update_cursor(wme, px, cy); 5631 if (window_copy_update_selection(wme, 1, 0)) 5632 window_copy_redraw_lines(wme, cy, nd); 5633 } 5634 5635 /* Scroll down if the cursor went off the visible screen. */ 5636 static void 5637 window_copy_acquire_cursor_down(struct window_mode_entry *wme, u_int hsize, 5638 u_int sy, u_int oy, u_int oldy, u_int px, u_int py, int no_reset) 5639 { 5640 u_int cy, yy, ny, nd; 5641 5642 cy = py - hsize + oy; 5643 yy = sy - 1; 5644 if (cy > yy) { 5645 ny = cy - yy; 5646 oldy = yy; 5647 nd = 1; 5648 } else { 5649 ny = 0; 5650 nd = cy - oldy + 1; 5651 } 5652 while (ny > 0) { 5653 window_copy_cursor_down(wme, 1); 5654 ny--; 5655 } 5656 if (cy > yy) 5657 window_copy_update_cursor(wme, px, yy); 5658 else 5659 window_copy_update_cursor(wme, px, cy); 5660 if (window_copy_update_selection(wme, 1, no_reset)) 5661 window_copy_redraw_lines(wme, oldy, nd); 5662 } 5663