[BACK]Return to window-copy.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/window-copy.c, Revision 1.287

1.287   ! nicm        1: /* $OpenBSD: window-copy.c,v 1.286 2020/05/16 16:03:30 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.144     nicm        4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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:
1.97      nicm       21: #include <ctype.h>
1.244     nicm       22: #include <regex.h>
1.21      nicm       23: #include <stdlib.h>
1.1       nicm       24: #include <string.h>
1.280     nicm       25: #include <time.h>
1.1       nicm       26:
                     27: #include "tmux.h"
                     28:
1.208     nicm       29: static const char *window_copy_key_table(struct window_mode_entry *);
                     30: static void    window_copy_command(struct window_mode_entry *, struct client *,
1.205     nicm       31:                    struct session *, struct winlink *, struct args *,
                     32:                    struct mouse_event *);
1.208     nicm       33: static struct screen *window_copy_init(struct window_mode_entry *,
1.177     nicm       34:                    struct cmd_find_state *, struct args *);
1.210     nicm       35: static struct screen *window_copy_view_init(struct window_mode_entry *,
                     36:                    struct cmd_find_state *, struct args *);
1.208     nicm       37: static void    window_copy_free(struct window_mode_entry *);
                     38: static void    window_copy_resize(struct window_mode_entry *, u_int, u_int);
                     39: static void    window_copy_formats(struct window_mode_entry *,
                     40:                    struct format_tree *);
                     41: static void    window_copy_pageup1(struct window_mode_entry *, int);
                     42: static int     window_copy_pagedown(struct window_mode_entry *, int, int);
                     43: static void    window_copy_next_paragraph(struct window_mode_entry *);
                     44: static void    window_copy_previous_paragraph(struct window_mode_entry *);
                     45:
                     46: static void    window_copy_redraw_selection(struct window_mode_entry *, u_int);
                     47: static void    window_copy_redraw_lines(struct window_mode_entry *, u_int,
                     48:                    u_int);
                     49: static void    window_copy_redraw_screen(struct window_mode_entry *);
                     50: static void    window_copy_write_line(struct window_mode_entry *,
1.157     nicm       51:                    struct screen_write_ctx *, u_int);
1.208     nicm       52: static void    window_copy_write_lines(struct window_mode_entry *,
1.157     nicm       53:                    struct screen_write_ctx *, u_int, u_int);
                     54:
1.208     nicm       55: static void    window_copy_scroll_to(struct window_mode_entry *, u_int, u_int);
1.157     nicm       56: static int     window_copy_search_compare(struct grid *, u_int, u_int,
                     57:                    struct grid *, u_int, int);
                     58: static int     window_copy_search_lr(struct grid *, struct grid *, u_int *,
                     59:                    u_int, u_int, u_int, int);
                     60: static int     window_copy_search_rl(struct grid *, struct grid *, u_int *,
                     61:                    u_int, u_int, u_int, int);
1.244     nicm       62: static int     window_copy_last_regex(struct grid *gd, u_int py, u_int first,
                     63:                    u_int last, u_int len, u_int *ppx, u_int *psx,
                     64:                    const char *buf, const regex_t *preg, int eflags);
                     65: static char    *window_copy_stringify(struct grid *, u_int, u_int, u_int,
                     66:                    char *, u_int *);
                     67: static void    window_copy_cstrtocellpos(struct grid *, u_int, u_int *, u_int *,
                     68:                    const char *str);
1.208     nicm       69: static int     window_copy_search_marks(struct window_mode_entry *,
1.244     nicm       70:                    struct screen *, int);
1.208     nicm       71: static void    window_copy_clear_marks(struct window_mode_entry *);
1.230     nicm       72: static void    window_copy_move_left(struct screen *, u_int *, u_int *, int);
                     73: static void    window_copy_move_right(struct screen *, u_int *, u_int *, int);
1.157     nicm       74: static int     window_copy_is_lowercase(const char *);
1.208     nicm       75: static int     window_copy_search_jump(struct window_mode_entry *,
                     76:                    struct grid *, struct grid *, u_int, u_int, u_int, int, int,
1.244     nicm       77:                    int, int);
                     78: static int     window_copy_search(struct window_mode_entry *, int, int);
                     79: static int     window_copy_search_up(struct window_mode_entry *, int);
                     80: static int     window_copy_search_down(struct window_mode_entry *, int);
1.208     nicm       81: static void    window_copy_goto_line(struct window_mode_entry *, const char *);
                     82: static void    window_copy_update_cursor(struct window_mode_entry *, u_int,
                     83:                    u_int);
                     84: static void    window_copy_start_selection(struct window_mode_entry *);
                     85: static int     window_copy_adjust_selection(struct window_mode_entry *,
                     86:                    u_int *, u_int *);
1.252     nicm       87: static int     window_copy_set_selection(struct window_mode_entry *, int, int);
                     88: static int     window_copy_update_selection(struct window_mode_entry *, int,
                     89:                    int);
                     90: static void    window_copy_synchronize_cursor(struct window_mode_entry *, int);
1.208     nicm       91: static void    *window_copy_get_selection(struct window_mode_entry *, size_t *);
1.215     nicm       92: static void    window_copy_copy_buffer(struct window_mode_entry *,
                     93:                    const char *, void *, size_t);
1.208     nicm       94: static void    window_copy_copy_pipe(struct window_mode_entry *,
1.215     nicm       95:                    struct session *, const char *, const char *);
                     96: static void    window_copy_copy_selection(struct window_mode_entry *,
                     97:                    const char *);
1.208     nicm       98: static void    window_copy_append_selection(struct window_mode_entry *);
                     99: static void    window_copy_clear_selection(struct window_mode_entry *);
                    100: static void    window_copy_copy_line(struct window_mode_entry *, char **,
                    101:                    size_t *, u_int, u_int, u_int);
                    102: static int     window_copy_in_set(struct window_mode_entry *, u_int, u_int,
1.157     nicm      103:                    const char *);
1.208     nicm      104: static u_int   window_copy_find_length(struct window_mode_entry *, u_int);
                    105: static void    window_copy_cursor_start_of_line(struct window_mode_entry *);
                    106: static void    window_copy_cursor_back_to_indentation(
                    107:                    struct window_mode_entry *);
                    108: static void    window_copy_cursor_end_of_line(struct window_mode_entry *);
                    109: static void    window_copy_other_end(struct window_mode_entry *);
                    110: static void    window_copy_cursor_left(struct window_mode_entry *);
                    111: static void    window_copy_cursor_right(struct window_mode_entry *);
                    112: static void    window_copy_cursor_up(struct window_mode_entry *, int);
                    113: static void    window_copy_cursor_down(struct window_mode_entry *, int);
                    114: static void    window_copy_cursor_jump(struct window_mode_entry *);
                    115: static void    window_copy_cursor_jump_back(struct window_mode_entry *);
                    116: static void    window_copy_cursor_jump_to(struct window_mode_entry *);
                    117: static void    window_copy_cursor_jump_to_back(struct window_mode_entry *);
                    118: static void    window_copy_cursor_next_word(struct window_mode_entry *,
1.157     nicm      119:                    const char *);
1.247     nicm      120: static void    window_copy_cursor_next_word_end_pos(struct window_mode_entry *,
                    121:                    const char *, u_int *, u_int *);
1.208     nicm      122: static void    window_copy_cursor_next_word_end(struct window_mode_entry *,
1.255     nicm      123:                    const char *, int);
1.247     nicm      124: static void    window_copy_cursor_previous_word_pos(struct window_mode_entry *,
                    125:                    const char *, int, u_int *, u_int *);
1.208     nicm      126: static void    window_copy_cursor_previous_word(struct window_mode_entry *,
1.229     nicm      127:                    const char *, int);
1.208     nicm      128: static void    window_copy_scroll_up(struct window_mode_entry *, u_int);
                    129: static void    window_copy_scroll_down(struct window_mode_entry *, u_int);
                    130: static void    window_copy_rectangle_toggle(struct window_mode_entry *);
1.157     nicm      131: static void    window_copy_move_mouse(struct mouse_event *);
                    132: static void    window_copy_drag_update(struct client *, struct mouse_event *);
1.217     nicm      133: static void    window_copy_drag_release(struct client *, struct mouse_event *);
1.1       nicm      134:
                    135: const struct window_mode window_copy_mode = {
1.173     nicm      136:        .name = "copy-mode",
                    137:
1.155     nicm      138:        .init = window_copy_init,
                    139:        .free = window_copy_free,
                    140:        .resize = window_copy_resize,
                    141:        .key_table = window_copy_key_table,
                    142:        .command = window_copy_command,
1.206     nicm      143:        .formats = window_copy_formats,
1.1       nicm      144: };
                    145:
1.210     nicm      146: const struct window_mode window_view_mode = {
                    147:        .name = "view-mode",
                    148:
                    149:        .init = window_copy_view_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:
1.155     nicm      157: enum {
1.21      nicm      158:        WINDOW_COPY_OFF,
                    159:        WINDOW_COPY_SEARCHUP,
                    160:        WINDOW_COPY_SEARCHDOWN,
1.52      nicm      161:        WINDOW_COPY_JUMPFORWARD,
1.155     nicm      162:        WINDOW_COPY_JUMPBACKWARD,
1.76      nicm      163:        WINDOW_COPY_JUMPTOFORWARD,
1.155     nicm      164:        WINDOW_COPY_JUMPTOBACKWARD,
1.21      nicm      165: };
                    166:
1.161     nicm      167: enum {
                    168:        WINDOW_COPY_REL_POS_ABOVE,
                    169:        WINDOW_COPY_REL_POS_ON_SCREEN,
                    170:        WINDOW_COPY_REL_POS_BELOW,
                    171: };
                    172:
1.213     nicm      173: enum window_copy_cmd_action {
                    174:        WINDOW_COPY_CMD_NOTHING,
                    175:        WINDOW_COPY_CMD_REDRAW,
                    176:        WINDOW_COPY_CMD_CANCEL,
                    177: };
                    178:
                    179: struct window_copy_cmd_state {
                    180:        struct window_mode_entry        *wme;
                    181:        struct args                     *args;
                    182:        struct mouse_event              *m;
1.215     nicm      183:
1.213     nicm      184:        struct client                   *c;
                    185:        struct session                  *s;
1.215     nicm      186:        struct winlink                  *wl;
1.213     nicm      187: };
                    188:
1.54      nicm      189: /*
1.161     nicm      190:  * Copy mode's visible screen (the "screen" field) is filled from one of two
                    191:  * sources: the original contents of the pane (used when we actually enter via
                    192:  * the "copy-mode" command, to copy the contents of the current pane), or else
                    193:  * a series of lines containing the output from an output-writing tmux command
                    194:  * (such as any of the "show-*" or "list-*" commands).
1.54      nicm      195:  *
1.161     nicm      196:  * In either case, the full content of the copy-mode grid is pointed at by the
                    197:  * "backing" field, and is copied into "screen" as needed (that is, when
                    198:  * scrolling occurs). When copy-mode is backed by a pane, backing points
                    199:  * directly at that pane's screen structure (&wp->base); when backed by a list
                    200:  * of output-lines from a command, it points at a newly-allocated screen
                    201:  * structure (which is deallocated when the mode ends).
1.54      nicm      202:  */
1.1       nicm      203: struct window_copy_mode_data {
1.155     nicm      204:        struct screen    screen;
1.1       nicm      205:
1.155     nicm      206:        struct screen   *backing;
                    207:        int              backing_written; /* backing display started */
1.54      nicm      208:
1.266     nicm      209:        int              viewmode;      /* view mode entered */
                    210:
1.192     nicm      211:        u_int            oy;            /* number of lines scrolled up */
1.21      nicm      212:
1.192     nicm      213:        u_int            selx;          /* beginning of selection */
1.155     nicm      214:        u_int            sely;
1.21      nicm      215:
1.192     nicm      216:        u_int            endselx;       /* end of selection */
1.161     nicm      217:        u_int            endsely;
                    218:
                    219:        enum {
                    220:                CURSORDRAG_NONE,        /* selection is independent of cursor */
                    221:                CURSORDRAG_ENDSEL,      /* end is synchronized with cursor */
                    222:                CURSORDRAG_SEL,         /* start is synchronized with cursor */
                    223:        } cursordrag;
                    224:
1.192     nicm      225:        int              modekeys;
                    226:        enum {
                    227:                LINE_SEL_NONE,
                    228:                LINE_SEL_LEFT_RIGHT,
                    229:                LINE_SEL_RIGHT_LEFT,
                    230:        } lineflag;                     /* line selection mode */
1.155     nicm      231:        int              rectflag;      /* in rectangle copy mode? */
                    232:        int              scroll_exit;   /* exit on scroll to end? */
1.249     nicm      233:        int              hide_position; /* hide position marker */
1.1       nicm      234:
1.247     nicm      235:        enum {
                    236:                SEL_CHAR,               /* select one char at a time */
                    237:                SEL_WORD,               /* select one word at a time */
                    238:                SEL_LINE,               /* select one line at a time */
                    239:        } selflag;
                    240:
                    241:        const char      *ws;            /* word separators */
                    242:
                    243:        u_int            dx;            /* drag start position */
                    244:        u_int            dy;
                    245:
                    246:        u_int            selrx;         /* selection reset positions */
                    247:        u_int            selry;
                    248:        u_int            endselrx;
                    249:        u_int            endselry;
                    250:
1.155     nicm      251:        u_int            cx;
                    252:        u_int            cy;
1.42      nicm      253:
1.192     nicm      254:        u_int            lastcx;        /* position in last line w/ content */
                    255:        u_int            lastsx;        /* size of last line w/ content */
1.1       nicm      256:
1.155     nicm      257:        int              searchtype;
1.259     nicm      258:        int              searchregex;
1.155     nicm      259:        char            *searchstr;
1.282     nicm      260:        u_char          *searchmark;
1.284     nicm      261:        int              searchcount;
                    262:        int              searchmore;
1.170     nicm      263:        int              searchthis;
1.163     nicm      264:        int              searchx;
                    265:        int              searchy;
                    266:        int              searcho;
1.282     nicm      267:        u_char           searchgen;
1.25      nicm      268:
1.263     nicm      269:        int              timeout;       /* search has timed out */
1.284     nicm      270: #define WINDOW_COPY_SEARCH_TIMEOUT 10000
                    271: #define WINDOW_COPY_SEARCH_ALL_TIMEOUT 200
1.263     nicm      272:
1.155     nicm      273:        int              jumptype;
                    274:        char             jumpchar;
1.217     nicm      275:
                    276:        struct event     dragtimer;
                    277: #define WINDOW_COPY_DRAG_REPEAT_TIME 50000
1.1       nicm      278: };
                    279:
1.217     nicm      280: static void
                    281: window_copy_scroll_timer(__unused int fd, __unused short events, void *arg)
                    282: {
                    283:        struct window_mode_entry        *wme = arg;
                    284:        struct window_pane              *wp = wme->wp;
                    285:        struct window_copy_mode_data    *data = wme->data;
                    286:        struct timeval                   tv = {
                    287:                .tv_usec = WINDOW_COPY_DRAG_REPEAT_TIME
                    288:        };
                    289:
                    290:        evtimer_del(&data->dragtimer);
                    291:
                    292:        if (TAILQ_FIRST(&wp->modes) != wme)
                    293:                return;
                    294:
                    295:        if (data->cy == 0) {
                    296:                evtimer_add(&data->dragtimer, &tv);
                    297:                window_copy_cursor_up(wme, 1);
                    298:        } else if (data->cy == screen_size_y(&data->screen) - 1) {
                    299:                evtimer_add(&data->dragtimer, &tv);
                    300:                window_copy_cursor_down(wme, 1);
                    301:        }
                    302: }
                    303:
1.266     nicm      304: static struct screen *
1.274     nicm      305: window_copy_clone_screen(struct screen *src, struct screen *hint, u_int *cx,
1.279     nicm      306:     u_int *cy, int trim)
1.266     nicm      307: {
1.274     nicm      308:        struct screen           *dst;
                    309:        const struct grid_line  *gl;
1.285     nicm      310:        u_int                    sy, wx, wy;
                    311:        int                      reflow;
1.266     nicm      312:
                    313:        dst = xcalloc(1, sizeof *dst);
1.270     nicm      314:
                    315:        sy = screen_hsize(src) + screen_size_y(src);
1.279     nicm      316:        if (trim) {
                    317:                while (sy > screen_hsize(src)) {
                    318:                        gl = grid_peek_line(src->grid, sy - 1);
                    319:                        if (gl->cellused != 0)
                    320:                                break;
                    321:                        sy--;
                    322:                }
1.274     nicm      323:        }
1.278     nicm      324:        log_debug("%s: target screen is %ux%u, source %ux%u", __func__,
                    325:            screen_size_x(src), sy, screen_size_x(hint),
                    326:            screen_hsize(src) + screen_size_y(src));
1.274     nicm      327:        screen_init(dst, screen_size_x(src), sy, screen_hlimit(src));
1.285     nicm      328:
                    329:        /*
                    330:         * Ensure history is on for the backing grid so lines are not deleted
                    331:         * during resizing.
                    332:         */
                    333:        dst->grid->flags |= GRID_HISTORY;
1.274     nicm      334:        grid_duplicate_lines(dst->grid, 0, src->grid, 0, sy);
1.270     nicm      335:
1.274     nicm      336:        dst->grid->sy = sy - screen_hsize(src);
                    337:        dst->grid->hsize = screen_hsize(src);
                    338:        dst->grid->hscrolled = src->grid->hscrolled;
1.278     nicm      339:        if (src->cy > dst->grid->sy - 1) {
                    340:                dst->cx = 0;
                    341:                dst->cy = dst->grid->sy - 1;
                    342:        } else {
                    343:                dst->cx = src->cx;
                    344:                dst->cy = src->cy;
                    345:        }
1.266     nicm      346:
1.285     nicm      347:        if (cx != NULL && cy != NULL) {
                    348:                *cx = dst->cx;
                    349:                *cy = screen_hsize(dst) + dst->cy;
                    350:                reflow = (screen_size_x(hint) != screen_size_x(dst));
                    351:        }
                    352:        else
                    353:                reflow = 0;
                    354:        if (reflow)
                    355:                grid_wrap_position(dst->grid, *cx, *cy, &wx, &wy);
1.274     nicm      356:        screen_resize_cursor(dst, screen_size_x(hint), screen_size_y(hint), 1,
1.285     nicm      357:            0, 0);
                    358:        if (reflow)
                    359:                grid_unwrap_position(dst->grid, cx, cy, wx, wy);
1.266     nicm      360:
                    361:        return (dst);
                    362: }
                    363:
1.210     nicm      364: static struct window_copy_mode_data *
                    365: window_copy_common_init(struct window_mode_entry *wme)
1.1       nicm      366: {
1.208     nicm      367:        struct window_pane              *wp = wme->wp;
1.1       nicm      368:        struct window_copy_mode_data    *data;
1.210     nicm      369:        struct screen                   *base = &wp->base;
1.1       nicm      370:
1.208     nicm      371:        wme->data = data = xcalloc(1, sizeof *data);
1.1       nicm      372:
1.161     nicm      373:        data->cursordrag = CURSORDRAG_NONE;
1.193     nicm      374:        data->lineflag = LINE_SEL_NONE;
1.250     nicm      375:        data->selflag = SEL_CHAR;
1.42      nicm      376:
1.174     nicm      377:        if (wp->searchstr != NULL) {
                    378:                data->searchtype = WINDOW_COPY_SEARCHUP;
1.259     nicm      379:                data->searchregex = wp->searchregex;
1.174     nicm      380:                data->searchstr = xstrdup(wp->searchstr);
                    381:        } else {
                    382:                data->searchtype = WINDOW_COPY_OFF;
1.259     nicm      383:                data->searchregex = 0;
1.174     nicm      384:                data->searchstr = NULL;
                    385:        }
1.163     nicm      386:        data->searchx = data->searchy = data->searcho = -1;
1.21      nicm      387:
1.52      nicm      388:        data->jumptype = WINDOW_COPY_OFF;
                    389:        data->jumpchar = '\0';
                    390:
1.210     nicm      391:        screen_init(&data->screen, screen_size_x(base), screen_size_y(base), 0);
1.192     nicm      392:        data->modekeys = options_get_number(wp->window->options, "mode-keys");
1.1       nicm      393:
1.217     nicm      394:        evtimer_set(&data->dragtimer, window_copy_scroll_timer, wme);
                    395:
1.210     nicm      396:        return (data);
1.54      nicm      397: }
                    398:
1.210     nicm      399: static struct screen *
                    400: window_copy_init(struct window_mode_entry *wme,
                    401:     __unused struct cmd_find_state *fs, struct args *args)
1.54      nicm      402: {
1.270     nicm      403:        struct window_pane              *wp = wme->swp;
1.210     nicm      404:        struct window_copy_mode_data    *data;
1.274     nicm      405:        struct screen                   *base = &wp->base;
1.210     nicm      406:        struct screen_write_ctx          ctx;
1.274     nicm      407:        u_int                            i, cx, cy;
1.54      nicm      408:
1.210     nicm      409:        data = window_copy_common_init(wme);
1.279     nicm      410:        data->backing = window_copy_clone_screen(base, &data->screen, &cx, &cy,
                    411:            wme->swp != wme->wp);
1.210     nicm      412:
1.285     nicm      413:        data->cx = cx;
1.274     nicm      414:        if (cy < screen_hsize(data->backing)) {
1.272     nicm      415:                data->cy = 0;
1.274     nicm      416:                data->oy = screen_hsize(data->backing) - cy;
                    417:        } else {
1.285     nicm      418:                data->cy = cy - screen_hsize(data->backing);
1.274     nicm      419:                data->oy = 0;
                    420:        }
1.54      nicm      421:
1.210     nicm      422:        data->scroll_exit = args_has(args, 'e');
1.249     nicm      423:        data->hide_position = args_has(args, 'H');
1.1       nicm      424:
1.210     nicm      425:        data->screen.cx = data->cx;
                    426:        data->screen.cy = data->cy;
                    427:
1.283     nicm      428:        screen_write_start(&ctx, &data->screen);
1.210     nicm      429:        for (i = 0; i < screen_size_y(&data->screen); i++)
1.208     nicm      430:                window_copy_write_line(wme, &ctx, i);
1.212     nicm      431:        screen_write_cursormove(&ctx, data->cx, data->cy, 0);
1.1       nicm      432:        screen_write_stop(&ctx);
1.210     nicm      433:
                    434:        return (&data->screen);
1.54      nicm      435: }
1.1       nicm      436:
1.210     nicm      437: static struct screen *
                    438: window_copy_view_init(struct window_mode_entry *wme,
                    439:     __unused struct cmd_find_state *fs, __unused struct args *args)
1.54      nicm      440: {
1.210     nicm      441:        struct window_pane              *wp = wme->wp;
1.208     nicm      442:        struct window_copy_mode_data    *data;
1.210     nicm      443:        struct screen                   *base = &wp->base;
                    444:        struct screen                   *s;
1.54      nicm      445:
1.210     nicm      446:        data = window_copy_common_init(wme);
1.266     nicm      447:        data->viewmode = 1;
1.208     nicm      448:
1.210     nicm      449:        data->backing = s = xmalloc(sizeof *data->backing);
                    450:        screen_init(s, screen_size_x(base), screen_size_y(base), UINT_MAX);
1.207     nicm      451:
1.210     nicm      452:        return (&data->screen);
1.1       nicm      453: }
                    454:
1.157     nicm      455: static void
1.208     nicm      456: window_copy_free(struct window_mode_entry *wme)
1.1       nicm      457: {
1.208     nicm      458:        struct window_copy_mode_data    *data = wme->data;
1.46      nicm      459:
1.217     nicm      460:        evtimer_del(&data->dragtimer);
                    461:
1.162     nicm      462:        free(data->searchmark);
1.81      nicm      463:        free(data->searchstr);
1.21      nicm      464:
1.266     nicm      465:        screen_free(data->backing);
                    466:        free(data->backing);
                    467:
1.1       nicm      468:        screen_free(&data->screen);
1.81      nicm      469:        free(data);
1.1       nicm      470: }
                    471:
                    472: void
1.54      nicm      473: window_copy_add(struct window_pane *wp, const char *fmt, ...)
                    474: {
                    475:        va_list ap;
                    476:
                    477:        va_start(ap, fmt);
                    478:        window_copy_vadd(wp, fmt, ap);
                    479:        va_end(ap);
                    480: }
                    481:
                    482: void
                    483: window_copy_vadd(struct window_pane *wp, const char *fmt, va_list ap)
                    484: {
1.211     nicm      485:        struct window_mode_entry        *wme = TAILQ_FIRST(&wp->modes);
1.208     nicm      486:        struct window_copy_mode_data    *data = wme->data;
1.54      nicm      487:        struct screen                   *backing = data->backing;
                    488:        struct screen_write_ctx          back_ctx, ctx;
                    489:        struct grid_cell                 gc;
1.119     nicm      490:        u_int                            old_hsize, old_cy;
1.54      nicm      491:
                    492:        memcpy(&gc, &grid_default_cell, sizeof gc);
                    493:
                    494:        old_hsize = screen_hsize(data->backing);
1.283     nicm      495:        screen_write_start(&back_ctx, backing);
1.54      nicm      496:        if (data->backing_written) {
                    497:                /*
                    498:                 * On the second or later line, do a CRLF before writing
                    499:                 * (so it's on a new line).
                    500:                 */
                    501:                screen_write_carriagereturn(&back_ctx);
1.175     nicm      502:                screen_write_linefeed(&back_ctx, 0, 8);
1.54      nicm      503:        } else
                    504:                data->backing_written = 1;
1.119     nicm      505:        old_cy = backing->cy;
1.139     nicm      506:        screen_write_vnputs(&back_ctx, 0, &gc, fmt, ap);
1.54      nicm      507:        screen_write_stop(&back_ctx);
                    508:
                    509:        data->oy += screen_hsize(data->backing) - old_hsize;
                    510:
1.283     nicm      511:        screen_write_start_pane(&ctx, wp, &data->screen);
1.54      nicm      512:
                    513:        /*
                    514:         * If the history has changed, draw the top line.
                    515:         * (If there's any history at all, it has changed.)
                    516:         */
                    517:        if (screen_hsize(data->backing))
1.208     nicm      518:                window_copy_redraw_lines(wme, 0, 1);
1.54      nicm      519:
1.119     nicm      520:        /* Write the new lines. */
1.208     nicm      521:        window_copy_redraw_lines(wme, old_cy, backing->cy - old_cy + 1);
1.54      nicm      522:
                    523:        screen_write_stop(&ctx);
                    524: }
                    525:
                    526: void
1.149     nicm      527: window_copy_pageup(struct window_pane *wp, int half_page)
1.1       nicm      528: {
1.211     nicm      529:        window_copy_pageup1(TAILQ_FIRST(&wp->modes), half_page);
1.208     nicm      530: }
                    531:
                    532: static void
                    533: window_copy_pageup1(struct window_mode_entry *wme, int half_page)
                    534: {
                    535:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm      536:        struct screen                   *s = &data->screen;
1.162     nicm      537:        u_int                            n, ox, oy, px, py;
1.147     nicm      538:
                    539:        oy = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm      540:        ox = window_copy_find_length(wme, oy);
1.147     nicm      541:
                    542:        if (data->cx != ox) {
                    543:                data->lastcx = data->cx;
                    544:                data->lastsx = ox;
                    545:        }
                    546:        data->cx = data->lastcx;
1.1       nicm      547:
1.19      nicm      548:        n = 1;
1.149     nicm      549:        if (screen_size_y(s) > 2) {
                    550:                if (half_page)
                    551:                        n = screen_size_y(s) / 2;
                    552:                else
                    553:                        n = screen_size_y(s) - 2;
                    554:        }
1.147     nicm      555:
1.195     nicm      556:        if (data->oy + n > screen_hsize(data->backing)) {
1.54      nicm      557:                data->oy = screen_hsize(data->backing);
1.195     nicm      558:                if (data->cy < n)
                    559:                        data->cy = 0;
                    560:                else
                    561:                        data->cy -= n;
                    562:        } else
1.19      nicm      563:                data->oy += n;
1.147     nicm      564:
1.192     nicm      565:        if (data->screen.sel == NULL || !data->rectflag) {
1.162     nicm      566:                py = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm      567:                px = window_copy_find_length(wme, py);
1.162     nicm      568:                if ((data->cx >= data->lastsx && data->cx != px) ||
                    569:                    data->cx > px)
1.208     nicm      570:                        window_copy_cursor_end_of_line(wme);
1.147     nicm      571:        }
                    572:
1.264     nicm      573:        if (data->searchmark != NULL && !data->timeout)
                    574:                window_copy_search_marks(wme, NULL, data->searchregex);
1.252     nicm      575:        window_copy_update_selection(wme, 1, 0);
1.208     nicm      576:        window_copy_redraw_screen(wme);
1.147     nicm      577: }
                    578:
1.166     nicm      579: static int
1.208     nicm      580: window_copy_pagedown(struct window_mode_entry *wme, int half_page,
                    581:     int scroll_exit)
1.147     nicm      582: {
1.208     nicm      583:        struct window_copy_mode_data    *data = wme->data;
1.147     nicm      584:        struct screen                   *s = &data->screen;
1.161     nicm      585:        u_int                            n, ox, oy, px, py;
1.147     nicm      586:
                    587:        oy = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm      588:        ox = window_copy_find_length(wme, oy);
1.147     nicm      589:
                    590:        if (data->cx != ox) {
                    591:                data->lastcx = data->cx;
                    592:                data->lastsx = ox;
                    593:        }
                    594:        data->cx = data->lastcx;
                    595:
                    596:        n = 1;
1.149     nicm      597:        if (screen_size_y(s) > 2) {
                    598:                if (half_page)
                    599:                        n = screen_size_y(s) / 2;
                    600:                else
                    601:                        n = screen_size_y(s) - 2;
                    602:        }
1.147     nicm      603:
1.195     nicm      604:        if (data->oy < n) {
1.147     nicm      605:                data->oy = 0;
1.195     nicm      606:                if (data->cy + (n - data->oy) >= screen_size_y(data->backing))
                    607:                        data->cy = screen_size_y(data->backing) - 1;
                    608:                else
                    609:                        data->cy += n - data->oy;
                    610:        } else
1.147     nicm      611:                data->oy -= n;
                    612:
1.192     nicm      613:        if (data->screen.sel == NULL || !data->rectflag) {
1.161     nicm      614:                py = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm      615:                px = window_copy_find_length(wme, py);
1.161     nicm      616:                if ((data->cx >= data->lastsx && data->cx != px) ||
                    617:                    data->cx > px)
1.208     nicm      618:                        window_copy_cursor_end_of_line(wme);
1.147     nicm      619:        }
                    620:
1.186     nicm      621:        if (scroll_exit && data->oy == 0)
1.166     nicm      622:                return (1);
1.264     nicm      623:        if (data->searchmark != NULL && !data->timeout)
                    624:                window_copy_search_marks(wme, NULL, data->searchregex);
1.252     nicm      625:        window_copy_update_selection(wme, 1, 0);
1.208     nicm      626:        window_copy_redraw_screen(wme);
1.166     nicm      627:        return (0);
1.1       nicm      628: }
                    629:
1.157     nicm      630: static void
1.208     nicm      631: window_copy_previous_paragraph(struct window_mode_entry *wme)
1.148     nicm      632: {
1.208     nicm      633:        struct window_copy_mode_data    *data = wme->data;
1.151     nicm      634:        u_int                            oy;
1.148     nicm      635:
                    636:        oy = screen_hsize(data->backing) + data->cy - data->oy;
                    637:
1.208     nicm      638:        while (oy > 0 && window_copy_find_length(wme, oy) == 0)
1.148     nicm      639:                oy--;
                    640:
1.208     nicm      641:        while (oy > 0 && window_copy_find_length(wme, oy) > 0)
1.148     nicm      642:                oy--;
                    643:
1.208     nicm      644:        window_copy_scroll_to(wme, 0, oy);
1.148     nicm      645: }
                    646:
1.157     nicm      647: static void
1.208     nicm      648: window_copy_next_paragraph(struct window_mode_entry *wme)
1.148     nicm      649: {
1.208     nicm      650:        struct window_copy_mode_data    *data = wme->data;
1.148     nicm      651:        struct screen                   *s = &data->screen;
                    652:        u_int                            maxy, ox, oy;
                    653:
                    654:        oy = screen_hsize(data->backing) + data->cy - data->oy;
                    655:        maxy = screen_hsize(data->backing) + screen_size_y(s) - 1;
                    656:
1.208     nicm      657:        while (oy < maxy && window_copy_find_length(wme, oy) == 0)
1.148     nicm      658:                oy++;
                    659:
1.208     nicm      660:        while (oy < maxy && window_copy_find_length(wme, oy) > 0)
1.148     nicm      661:                oy++;
                    662:
1.208     nicm      663:        ox = window_copy_find_length(wme, oy);
                    664:        window_copy_scroll_to(wme, ox, oy);
1.148     nicm      665: }
                    666:
1.256     nicm      667: char *
                    668: window_copy_get_word(struct window_pane *wp, u_int x, u_int y)
                    669: {
                    670:        struct window_mode_entry        *wme = TAILQ_FIRST(&wp->modes);
                    671:        struct window_copy_mode_data    *data = wme->data;
                    672:        struct grid                     *gd = data->screen.grid;
                    673:
                    674:        return (format_grid_word(gd, x, gd->hsize + y));
                    675: }
                    676:
                    677: char *
                    678: window_copy_get_line(struct window_pane *wp, u_int y)
                    679: {
                    680:        struct window_mode_entry        *wme = TAILQ_FIRST(&wp->modes);
                    681:        struct window_copy_mode_data    *data = wme->data;
                    682:        struct grid                     *gd = data->screen.grid;
                    683:
                    684:        return (format_grid_line(gd, gd->hsize + y));
                    685: }
                    686:
1.157     nicm      687: static void
1.208     nicm      688: window_copy_formats(struct window_mode_entry *wme, struct format_tree *ft)
1.206     nicm      689: {
1.208     nicm      690:        struct window_copy_mode_data    *data = wme->data;
1.256     nicm      691:        struct grid                     *gd = data->screen.grid;
1.237     nicm      692:        char                            *s;
1.206     nicm      693:
                    694:        format_add(ft, "scroll_position", "%d", data->oy);
                    695:        format_add(ft, "rectangle_toggle", "%d", data->rectflag);
1.236     nicm      696:
                    697:        format_add(ft, "copy_cursor_x", "%d", data->cx);
                    698:        format_add(ft, "copy_cursor_y", "%d", data->cy);
                    699:
                    700:        format_add(ft, "selection_present", "%d", data->screen.sel != NULL);
                    701:        if (data->screen.sel != NULL) {
                    702:                format_add(ft, "selection_start_x", "%d", data->selx);
                    703:                format_add(ft, "selection_start_y", "%d", data->sely);
                    704:                format_add(ft, "selection_end_x", "%d", data->endselx);
                    705:                format_add(ft, "selection_end_y", "%d", data->endsely);
1.246     nicm      706:                format_add(ft, "selection_active", "%d",
                    707:                    data->cursordrag != CURSORDRAG_NONE);
                    708:        } else
                    709:                format_add(ft, "selection_active", "%d", 0);
1.237     nicm      710:
1.256     nicm      711:        s = format_grid_word(gd, data->cx, gd->hsize + data->cy);
1.237     nicm      712:        if (s != NULL) {
                    713:                format_add(ft, "copy_cursor_word", "%s", s);
                    714:                free(s);
                    715:        }
                    716:
1.256     nicm      717:        s = format_grid_line(gd, gd->hsize + data->cy);
1.237     nicm      718:        if (s != NULL) {
                    719:                format_add(ft, "copy_cursor_line", "%s", s);
                    720:                free(s);
1.236     nicm      721:        }
1.206     nicm      722: }
                    723:
                    724: static void
1.275     nicm      725: window_copy_size_changed(struct window_mode_entry *wme)
1.1       nicm      726: {
1.208     nicm      727:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm      728:        struct screen                   *s = &data->screen;
1.275     nicm      729:        struct screen_write_ctx          ctx;
                    730:        int                              search = (data->searchmark != NULL);
1.35      nicm      731:
1.208     nicm      732:        window_copy_clear_selection(wme);
1.228     nicm      733:        window_copy_clear_marks(wme);
1.18      nicm      734:
1.283     nicm      735:        screen_write_start(&ctx, s);
1.276     nicm      736:        window_copy_write_lines(wme, &ctx, 0, screen_size_y(s));
1.1       nicm      737:        screen_write_stop(&ctx);
1.18      nicm      738:
1.263     nicm      739:        if (search && !data->timeout)
                    740:                window_copy_search_marks(wme, NULL, data->searchregex);
1.163     nicm      741:        data->searchx = data->cx;
                    742:        data->searchy = data->cy;
                    743:        data->searcho = data->oy;
1.275     nicm      744: }
                    745:
                    746: static void
                    747: window_copy_resize(struct window_mode_entry *wme, u_int sx, u_int sy)
                    748: {
                    749:        struct window_copy_mode_data    *data = wme->data;
                    750:        struct screen                   *s = &data->screen;
1.285     nicm      751:        struct grid                     *gd = data->backing->grid;
                    752:        u_int                            cx, cy, wx, wy;
                    753:        int                              reflow;
1.275     nicm      754:
                    755:        screen_resize(s, sx, sy, 0);
1.285     nicm      756:        cx = data->cx;
                    757:        cy = gd->hsize + data->cy - data->oy;
                    758:        reflow = (gd->sx != sx);
                    759:        if (reflow)
                    760:                grid_wrap_position(gd, cx, cy, &wx, &wy);
                    761:        screen_resize_cursor(data->backing, sx, sy, 1, 0, 0);
                    762:        if (reflow)
                    763:                grid_unwrap_position(gd, &cx, &cy, wx, wy);
1.275     nicm      764:
1.285     nicm      765:        data->cx = cx;
                    766:        if (cy < gd->hsize) {
                    767:                data->cy = 0;
                    768:                data->oy = gd->hsize - cy;
                    769:        } else {
                    770:                data->cy = cy - gd->hsize;
                    771:                data->oy = 0;
                    772:        }
1.162     nicm      773:
1.275     nicm      774:        window_copy_size_changed(wme);
1.208     nicm      775:        window_copy_redraw_screen(wme);
1.1       nicm      776: }
                    777:
1.157     nicm      778: static const char *
1.208     nicm      779: window_copy_key_table(struct window_mode_entry *wme)
1.155     nicm      780: {
1.208     nicm      781:        struct window_pane      *wp = wme->wp;
                    782:
1.155     nicm      783:        if (options_get_number(wp->window->options, "mode-keys") == MODEKEY_VI)
                    784:                return ("copy-mode-vi");
                    785:        return ("copy-mode");
                    786: }
                    787:
1.259     nicm      788: static int
                    789: window_copy_expand_search_string(struct window_copy_cmd_state *cs)
                    790: {
                    791:        struct window_mode_entry        *wme = cs->wme;
                    792:        struct window_copy_mode_data    *data = wme->data;
                    793:        const char                      *argument;
                    794:        char                            *expanded;
                    795:
                    796:        if (cs->args->argc == 2) {
                    797:                argument = cs->args->argv[1];
                    798:                if (*argument != '\0') {
                    799:                        if (args_has(cs->args, 'F')) {
                    800:                                expanded = format_single(NULL, argument, NULL,
                    801:                                    NULL, NULL, wme->wp);
                    802:                                if (*expanded == '\0') {
                    803:                                        free(expanded);
                    804:                                        return (0);
                    805:                                }
                    806:                                free(data->searchstr);
                    807:                                data->searchstr = expanded;
                    808:                        } else {
                    809:                                free(data->searchstr);
                    810:                                data->searchstr = xstrdup(argument);
                    811:                        }
                    812:                }
                    813:        }
                    814:        return (1);
                    815: }
                    816:
1.213     nicm      817: static enum window_copy_cmd_action
                    818: window_copy_cmd_append_selection(struct window_copy_cmd_state *cs)
                    819: {
                    820:        struct window_mode_entry        *wme = cs->wme;
                    821:        struct session                  *s = cs->s;
                    822:
                    823:        if (s != NULL)
                    824:                window_copy_append_selection(wme);
                    825:        window_copy_clear_selection(wme);
                    826:        return (WINDOW_COPY_CMD_REDRAW);
                    827: }
                    828:
                    829: static enum window_copy_cmd_action
                    830: window_copy_cmd_append_selection_and_cancel(struct window_copy_cmd_state *cs)
                    831: {
                    832:        struct window_mode_entry        *wme = cs->wme;
                    833:        struct session                  *s = cs->s;
                    834:
                    835:        if (s != NULL)
                    836:                window_copy_append_selection(wme);
                    837:        window_copy_clear_selection(wme);
                    838:        return (WINDOW_COPY_CMD_CANCEL);
                    839: }
                    840:
                    841: static enum window_copy_cmd_action
                    842: window_copy_cmd_back_to_indentation(struct window_copy_cmd_state *cs)
                    843: {
                    844:        struct window_mode_entry        *wme = cs->wme;
                    845:
                    846:        window_copy_cursor_back_to_indentation(wme);
                    847:        return (WINDOW_COPY_CMD_NOTHING);
                    848: }
                    849:
                    850: static enum window_copy_cmd_action
                    851: window_copy_cmd_begin_selection(struct window_copy_cmd_state *cs)
                    852: {
                    853:        struct window_mode_entry        *wme = cs->wme;
                    854:        struct client                   *c = cs->c;
                    855:        struct mouse_event              *m = cs->m;
                    856:        struct window_copy_mode_data    *data = wme->data;
                    857:
                    858:        if (m != NULL) {
                    859:                window_copy_start_drag(c, m);
                    860:                return (WINDOW_COPY_CMD_NOTHING);
                    861:        }
                    862:
                    863:        data->lineflag = LINE_SEL_NONE;
1.252     nicm      864:        data->selflag = SEL_CHAR;
1.213     nicm      865:        window_copy_start_selection(wme);
                    866:        return (WINDOW_COPY_CMD_REDRAW);
                    867: }
                    868:
                    869: static enum window_copy_cmd_action
                    870: window_copy_cmd_stop_selection(struct window_copy_cmd_state *cs)
                    871: {
                    872:        struct window_mode_entry        *wme = cs->wme;
                    873:        struct window_copy_mode_data    *data = wme->data;
                    874:
                    875:        data->cursordrag = CURSORDRAG_NONE;
1.214     nicm      876:        data->lineflag = LINE_SEL_NONE;
1.250     nicm      877:        data->selflag = SEL_CHAR;
1.213     nicm      878:        return (WINDOW_COPY_CMD_NOTHING);
                    879: }
                    880:
                    881: static enum window_copy_cmd_action
                    882: window_copy_cmd_bottom_line(struct window_copy_cmd_state *cs)
                    883: {
                    884:        struct window_mode_entry        *wme = cs->wme;
                    885:        struct window_copy_mode_data    *data = wme->data;
                    886:
                    887:        data->cx = 0;
                    888:        data->cy = screen_size_y(&data->screen) - 1;
                    889:
1.252     nicm      890:        window_copy_update_selection(wme, 1, 0);
1.213     nicm      891:        return (WINDOW_COPY_CMD_REDRAW);
                    892: }
                    893:
                    894: static enum window_copy_cmd_action
                    895: window_copy_cmd_cancel(__unused struct window_copy_cmd_state *cs)
                    896: {
                    897:        return (WINDOW_COPY_CMD_CANCEL);
                    898: }
                    899:
                    900: static enum window_copy_cmd_action
                    901: window_copy_cmd_clear_selection(struct window_copy_cmd_state *cs)
                    902: {
                    903:        struct window_mode_entry        *wme = cs->wme;
                    904:
                    905:        window_copy_clear_selection(wme);
                    906:        return (WINDOW_COPY_CMD_REDRAW);
                    907: }
                    908:
                    909: static enum window_copy_cmd_action
                    910: window_copy_cmd_copy_end_of_line(struct window_copy_cmd_state *cs)
                    911: {
                    912:        struct window_mode_entry        *wme = cs->wme;
1.215     nicm      913:        struct client                   *c = cs->c;
1.213     nicm      914:        struct session                  *s = cs->s;
1.215     nicm      915:        struct winlink                  *wl = cs->wl;
                    916:        struct window_pane              *wp = wme->wp;
1.213     nicm      917:        u_int                            np = wme->prefix;
1.215     nicm      918:        char                            *prefix = NULL;
                    919:
                    920:        if (cs->args->argc == 2)
                    921:                prefix = format_single(NULL, cs->args->argv[1], c, s, wl, wp);
1.213     nicm      922:
                    923:        window_copy_start_selection(wme);
                    924:        for (; np > 1; np--)
                    925:                window_copy_cursor_down(wme, 0);
                    926:        window_copy_cursor_end_of_line(wme);
                    927:
                    928:        if (s != NULL) {
1.215     nicm      929:                window_copy_copy_selection(wme, prefix);
                    930:
                    931:                free(prefix);
1.213     nicm      932:                return (WINDOW_COPY_CMD_CANCEL);
                    933:        }
1.215     nicm      934:
                    935:        free(prefix);
1.213     nicm      936:        return (WINDOW_COPY_CMD_REDRAW);
                    937: }
                    938:
                    939: static enum window_copy_cmd_action
                    940: window_copy_cmd_copy_line(struct window_copy_cmd_state *cs)
                    941: {
                    942:        struct window_mode_entry        *wme = cs->wme;
1.215     nicm      943:        struct client                   *c = cs->c;
1.213     nicm      944:        struct session                  *s = cs->s;
1.215     nicm      945:        struct winlink                  *wl = cs->wl;
                    946:        struct window_pane              *wp = wme->wp;
1.252     nicm      947:        struct window_copy_mode_data    *data = wme->data;
1.213     nicm      948:        u_int                            np = wme->prefix;
1.215     nicm      949:        char                            *prefix = NULL;
                    950:
                    951:        if (cs->args->argc == 2)
                    952:                prefix = format_single(NULL, cs->args->argv[1], c, s, wl, wp);
1.213     nicm      953:
1.252     nicm      954:        data->selflag = SEL_CHAR;
1.213     nicm      955:        window_copy_cursor_start_of_line(wme);
                    956:        window_copy_start_selection(wme);
                    957:        for (; np > 1; np--)
                    958:                window_copy_cursor_down(wme, 0);
                    959:        window_copy_cursor_end_of_line(wme);
                    960:
                    961:        if (s != NULL) {
1.215     nicm      962:                window_copy_copy_selection(wme, prefix);
                    963:
                    964:                free(prefix);
1.213     nicm      965:                return (WINDOW_COPY_CMD_CANCEL);
                    966:        }
1.215     nicm      967:
                    968:        free(prefix);
1.213     nicm      969:        return (WINDOW_COPY_CMD_REDRAW);
                    970: }
                    971:
                    972: static enum window_copy_cmd_action
1.216     nicm      973: window_copy_cmd_copy_selection_no_clear(struct window_copy_cmd_state *cs)
1.213     nicm      974: {
                    975:        struct window_mode_entry        *wme = cs->wme;
1.215     nicm      976:        struct client                   *c = cs->c;
1.213     nicm      977:        struct session                  *s = cs->s;
1.215     nicm      978:        struct winlink                  *wl = cs->wl;
                    979:        struct window_pane              *wp = wme->wp;
                    980:        char                            *prefix = NULL;
                    981:
                    982:        if (cs->args->argc == 2)
                    983:                prefix = format_single(NULL, cs->args->argv[1], c, s, wl, wp);
1.213     nicm      984:
                    985:        if (s != NULL)
1.215     nicm      986:                window_copy_copy_selection(wme, prefix);
                    987:
                    988:        free(prefix);
1.216     nicm      989:        return (WINDOW_COPY_CMD_NOTHING);
                    990: }
                    991:
                    992: static enum window_copy_cmd_action
                    993: window_copy_cmd_copy_selection(struct window_copy_cmd_state *cs)
                    994: {
                    995:        struct window_mode_entry        *wme = cs->wme;
                    996:
                    997:        window_copy_cmd_copy_selection_no_clear(cs);
                    998:        window_copy_clear_selection(wme);
1.213     nicm      999:        return (WINDOW_COPY_CMD_REDRAW);
                   1000: }
                   1001:
                   1002: static enum window_copy_cmd_action
                   1003: window_copy_cmd_copy_selection_and_cancel(struct window_copy_cmd_state *cs)
                   1004: {
                   1005:        struct window_mode_entry        *wme = cs->wme;
1.215     nicm     1006:
1.216     nicm     1007:        window_copy_cmd_copy_selection_no_clear(cs);
1.213     nicm     1008:        window_copy_clear_selection(wme);
                   1009:        return (WINDOW_COPY_CMD_CANCEL);
                   1010: }
                   1011:
                   1012: static enum window_copy_cmd_action
                   1013: window_copy_cmd_cursor_down(struct window_copy_cmd_state *cs)
                   1014: {
                   1015:        struct window_mode_entry        *wme = cs->wme;
                   1016:        u_int                            np = wme->prefix;
                   1017:
                   1018:        for (; np != 0; np--)
                   1019:                window_copy_cursor_down(wme, 0);
                   1020:        return (WINDOW_COPY_CMD_NOTHING);
                   1021: }
                   1022:
                   1023: static enum window_copy_cmd_action
1.234     nicm     1024: window_copy_cmd_cursor_down_and_cancel(struct window_copy_cmd_state *cs)
                   1025: {
                   1026:        struct window_mode_entry        *wme = cs->wme;
                   1027:        struct window_copy_mode_data    *data = wme->data;
                   1028:        u_int                            np = wme->prefix, cy;
                   1029:
                   1030:        cy = data->cy;
                   1031:        for (; np != 0; np--)
                   1032:                window_copy_cursor_down(wme, 0);
                   1033:        if (cy == data->cy && data->oy == 0)
                   1034:                return (WINDOW_COPY_CMD_CANCEL);
                   1035:        return (WINDOW_COPY_CMD_NOTHING);
                   1036: }
                   1037:
                   1038: static enum window_copy_cmd_action
1.213     nicm     1039: window_copy_cmd_cursor_left(struct window_copy_cmd_state *cs)
                   1040: {
                   1041:        struct window_mode_entry        *wme = cs->wme;
                   1042:        u_int                            np = wme->prefix;
                   1043:
                   1044:        for (; np != 0; np--)
                   1045:                window_copy_cursor_left(wme);
                   1046:        return (WINDOW_COPY_CMD_NOTHING);
                   1047: }
                   1048:
                   1049: static enum window_copy_cmd_action
                   1050: window_copy_cmd_cursor_right(struct window_copy_cmd_state *cs)
                   1051: {
                   1052:        struct window_mode_entry        *wme = cs->wme;
                   1053:        u_int                            np = wme->prefix;
                   1054:
                   1055:        for (; np != 0; np--)
                   1056:                window_copy_cursor_right(wme);
                   1057:        return (WINDOW_COPY_CMD_NOTHING);
                   1058: }
                   1059:
                   1060: static enum window_copy_cmd_action
                   1061: window_copy_cmd_cursor_up(struct window_copy_cmd_state *cs)
                   1062: {
                   1063:        struct window_mode_entry        *wme = cs->wme;
                   1064:        u_int                            np = wme->prefix;
                   1065:
                   1066:        for (; np != 0; np--)
                   1067:                window_copy_cursor_up(wme, 0);
                   1068:        return (WINDOW_COPY_CMD_NOTHING);
                   1069: }
                   1070:
                   1071: static enum window_copy_cmd_action
                   1072: window_copy_cmd_end_of_line(struct window_copy_cmd_state *cs)
                   1073: {
                   1074:        struct window_mode_entry        *wme = cs->wme;
                   1075:
                   1076:        window_copy_cursor_end_of_line(wme);
                   1077:        return (WINDOW_COPY_CMD_NOTHING);
                   1078: }
                   1079:
                   1080: static enum window_copy_cmd_action
                   1081: window_copy_cmd_halfpage_down(struct window_copy_cmd_state *cs)
                   1082: {
                   1083:        struct window_mode_entry        *wme = cs->wme;
                   1084:        struct window_copy_mode_data    *data = wme->data;
                   1085:        u_int                            np = wme->prefix;
                   1086:
                   1087:        for (; np != 0; np--) {
                   1088:                if (window_copy_pagedown(wme, 1, data->scroll_exit))
                   1089:                        return (WINDOW_COPY_CMD_CANCEL);
                   1090:        }
                   1091:        return (WINDOW_COPY_CMD_NOTHING);
                   1092: }
                   1093:
                   1094: static enum window_copy_cmd_action
                   1095: window_copy_cmd_halfpage_down_and_cancel(struct window_copy_cmd_state *cs)
                   1096: {
                   1097:
                   1098:        struct window_mode_entry        *wme = cs->wme;
                   1099:        u_int                            np = wme->prefix;
                   1100:
                   1101:        for (; np != 0; np--) {
                   1102:                if (window_copy_pagedown(wme, 1, 1))
                   1103:                        return (WINDOW_COPY_CMD_CANCEL);
                   1104:        }
                   1105:        return (WINDOW_COPY_CMD_NOTHING);
                   1106: }
                   1107:
                   1108: static enum window_copy_cmd_action
                   1109: window_copy_cmd_halfpage_up(struct window_copy_cmd_state *cs)
                   1110: {
                   1111:        struct window_mode_entry        *wme = cs->wme;
                   1112:        u_int                            np = wme->prefix;
                   1113:
                   1114:        for (; np != 0; np--)
                   1115:                window_copy_pageup1(wme, 1);
                   1116:        return (WINDOW_COPY_CMD_NOTHING);
                   1117: }
                   1118:
                   1119: static enum window_copy_cmd_action
                   1120: window_copy_cmd_history_bottom(struct window_copy_cmd_state *cs)
                   1121: {
                   1122:        struct window_mode_entry        *wme = cs->wme;
                   1123:        struct window_copy_mode_data    *data = wme->data;
1.269     nicm     1124:        struct screen                   *s = data->backing;
1.221     nicm     1125:        u_int                            oy;
                   1126:
1.269     nicm     1127:        oy = screen_hsize(s) + data->cy - data->oy;
1.221     nicm     1128:        if (data->lineflag == LINE_SEL_RIGHT_LEFT && oy == data->endsely)
                   1129:                window_copy_other_end(wme);
1.213     nicm     1130:
                   1131:        data->cy = screen_size_y(&data->screen) - 1;
1.269     nicm     1132:        data->cx = window_copy_find_length(wme, screen_hsize(s) + data->cy);
1.213     nicm     1133:        data->oy = 0;
                   1134:
1.264     nicm     1135:        if (data->searchmark != NULL && !data->timeout)
                   1136:                window_copy_search_marks(wme, NULL, data->searchregex);
1.252     nicm     1137:        window_copy_update_selection(wme, 1, 0);
1.213     nicm     1138:        return (WINDOW_COPY_CMD_REDRAW);
                   1139: }
                   1140:
                   1141: static enum window_copy_cmd_action
                   1142: window_copy_cmd_history_top(struct window_copy_cmd_state *cs)
                   1143: {
                   1144:        struct window_mode_entry        *wme = cs->wme;
                   1145:        struct window_copy_mode_data    *data = wme->data;
1.221     nicm     1146:        u_int                            oy;
1.213     nicm     1147:
1.221     nicm     1148:        oy = screen_hsize(data->backing) + data->cy - data->oy;
                   1149:        if (data->lineflag == LINE_SEL_LEFT_RIGHT && oy == data->sely)
                   1150:                window_copy_other_end(wme);
                   1151:
                   1152:        data->cy = 0;
1.213     nicm     1153:        data->cx = 0;
                   1154:        data->oy = screen_hsize(data->backing);
                   1155:
1.264     nicm     1156:        if (data->searchmark != NULL && !data->timeout)
                   1157:                window_copy_search_marks(wme, NULL, data->searchregex);
1.252     nicm     1158:        window_copy_update_selection(wme, 1, 0);
1.213     nicm     1159:        return (WINDOW_COPY_CMD_REDRAW);
                   1160: }
                   1161:
                   1162: static enum window_copy_cmd_action
                   1163: window_copy_cmd_jump_again(struct window_copy_cmd_state *cs)
                   1164: {
                   1165:        struct window_mode_entry        *wme = cs->wme;
                   1166:        struct window_copy_mode_data    *data = wme->data;
                   1167:        u_int                            np = wme->prefix;
                   1168:
                   1169:        switch (data->jumptype) {
                   1170:        case WINDOW_COPY_JUMPFORWARD:
                   1171:                for (; np != 0; np--)
                   1172:                        window_copy_cursor_jump(wme);
                   1173:                break;
                   1174:        case WINDOW_COPY_JUMPBACKWARD:
                   1175:                for (; np != 0; np--)
                   1176:                        window_copy_cursor_jump_back(wme);
                   1177:                break;
                   1178:        case WINDOW_COPY_JUMPTOFORWARD:
                   1179:                for (; np != 0; np--)
                   1180:                        window_copy_cursor_jump_to(wme);
                   1181:                break;
                   1182:        case WINDOW_COPY_JUMPTOBACKWARD:
                   1183:                for (; np != 0; np--)
                   1184:                        window_copy_cursor_jump_to_back(wme);
                   1185:                break;
                   1186:        }
                   1187:        return (WINDOW_COPY_CMD_NOTHING);
                   1188: }
                   1189:
                   1190: static enum window_copy_cmd_action
                   1191: window_copy_cmd_jump_reverse(struct window_copy_cmd_state *cs)
                   1192: {
                   1193:        struct window_mode_entry        *wme = cs->wme;
                   1194:        struct window_copy_mode_data    *data = wme->data;
                   1195:        u_int                            np = wme->prefix;
                   1196:
                   1197:        switch (data->jumptype) {
                   1198:        case WINDOW_COPY_JUMPFORWARD:
                   1199:                for (; np != 0; np--)
                   1200:                        window_copy_cursor_jump_back(wme);
                   1201:                break;
                   1202:        case WINDOW_COPY_JUMPBACKWARD:
                   1203:                for (; np != 0; np--)
                   1204:                        window_copy_cursor_jump(wme);
                   1205:                break;
                   1206:        case WINDOW_COPY_JUMPTOFORWARD:
                   1207:                for (; np != 0; np--)
                   1208:                        window_copy_cursor_jump_to_back(wme);
                   1209:                break;
                   1210:        case WINDOW_COPY_JUMPTOBACKWARD:
                   1211:                for (; np != 0; np--)
                   1212:                        window_copy_cursor_jump_to(wme);
                   1213:                break;
                   1214:        }
                   1215:        return (WINDOW_COPY_CMD_NOTHING);
                   1216: }
                   1217:
                   1218: static enum window_copy_cmd_action
                   1219: window_copy_cmd_middle_line(struct window_copy_cmd_state *cs)
                   1220: {
                   1221:        struct window_mode_entry        *wme = cs->wme;
                   1222:        struct window_copy_mode_data    *data = wme->data;
                   1223:
                   1224:        data->cx = 0;
                   1225:        data->cy = (screen_size_y(&data->screen) - 1) / 2;
                   1226:
1.252     nicm     1227:        window_copy_update_selection(wme, 1, 0);
1.213     nicm     1228:        return (WINDOW_COPY_CMD_REDRAW);
                   1229: }
                   1230:
                   1231: static enum window_copy_cmd_action
1.218     nicm     1232: window_copy_cmd_previous_matching_bracket(struct window_copy_cmd_state *cs)
                   1233: {
                   1234:        struct window_mode_entry        *wme = cs->wme;
                   1235:        u_int                            np = wme->prefix;
                   1236:        struct window_copy_mode_data    *data = wme->data;
                   1237:        struct screen                   *s = data->backing;
                   1238:        char                             open[] = "{[(", close[] = "}])";
                   1239:        char                             tried, found, start, *cp;
1.219     nicm     1240:        u_int                            px, py, xx, n;
1.218     nicm     1241:        struct grid_cell                 gc;
                   1242:        int                              failed;
                   1243:
                   1244:        for (; np != 0; np--) {
                   1245:                /* Get cursor position and line length. */
                   1246:                px = data->cx;
                   1247:                py = screen_hsize(s) + data->cy - data->oy;
                   1248:                xx = window_copy_find_length(wme, py);
                   1249:                if (xx == 0)
                   1250:                        break;
                   1251:
                   1252:                /*
                   1253:                 * Get the current character. If not on a bracket, try the
                   1254:                 * previous. If still not, then behave like previous-word.
                   1255:                 */
                   1256:                tried = 0;
                   1257:        retry:
                   1258:                grid_get_cell(s->grid, px, py, &gc);
                   1259:                if (gc.data.size != 1 || (gc.flags & GRID_FLAG_PADDING))
                   1260:                        cp = NULL;
                   1261:                else {
                   1262:                        found = *gc.data.data;
                   1263:                        cp = strchr(close, found);
                   1264:                }
                   1265:                if (cp == NULL) {
                   1266:                        if (data->modekeys == MODEKEY_EMACS) {
                   1267:                                if (!tried && px > 0) {
                   1268:                                        px--;
                   1269:                                        tried = 1;
                   1270:                                        goto retry;
                   1271:                                }
1.229     nicm     1272:                                window_copy_cursor_previous_word(wme, "}]) ", 1);
1.218     nicm     1273:                        }
                   1274:                        continue;
                   1275:                }
                   1276:                start = open[cp - close];
                   1277:
                   1278:                /* Walk backward until the matching bracket is reached. */
                   1279:                n = 1;
                   1280:                failed = 0;
                   1281:                do {
                   1282:                        if (px == 0) {
                   1283:                                if (py == 0) {
                   1284:                                        failed = 1;
                   1285:                                        break;
                   1286:                                }
                   1287:                                do {
                   1288:                                        py--;
                   1289:                                        xx = window_copy_find_length(wme, py);
                   1290:                                } while (xx == 0 && py > 0);
                   1291:                                if (xx == 0 && py == 0) {
                   1292:                                        failed = 1;
                   1293:                                        break;
                   1294:                                }
                   1295:                                px = xx - 1;
                   1296:                        } else
                   1297:                                px--;
                   1298:
                   1299:                        grid_get_cell(s->grid, px, py, &gc);
                   1300:                        if (gc.data.size == 1 &&
                   1301:                            (~gc.flags & GRID_FLAG_PADDING)) {
                   1302:                                if (*gc.data.data == found)
                   1303:                                        n++;
                   1304:                                else if (*gc.data.data == start)
                   1305:                                        n--;
                   1306:                        }
                   1307:                } while (n != 0);
                   1308:
                   1309:                /* Move the cursor to the found location if any. */
                   1310:                if (!failed)
                   1311:                        window_copy_scroll_to(wme, px, py);
                   1312:        }
                   1313:
                   1314:        return (WINDOW_COPY_CMD_NOTHING);
                   1315: }
                   1316:
                   1317: static enum window_copy_cmd_action
                   1318: window_copy_cmd_next_matching_bracket(struct window_copy_cmd_state *cs)
                   1319: {
                   1320:        struct window_mode_entry        *wme = cs->wme;
                   1321:        u_int                            np = wme->prefix;
                   1322:        struct window_copy_mode_data    *data = wme->data;
                   1323:        struct screen                   *s = data->backing;
                   1324:        char                             open[] = "{[(", close[] = "}])";
                   1325:        char                             tried, found, end, *cp;
                   1326:        u_int                            px, py, xx, yy, sx, sy, n;
                   1327:        struct grid_cell                 gc;
                   1328:        int                              failed;
                   1329:        struct grid_line                *gl;
                   1330:
                   1331:        for (; np != 0; np--) {
                   1332:                /* Get cursor position and line length. */
                   1333:                px = data->cx;
                   1334:                py = screen_hsize(s) + data->cy - data->oy;
                   1335:                xx = window_copy_find_length(wme, py);
                   1336:                yy = screen_hsize(s) + screen_size_y(s) - 1;
                   1337:                if (xx == 0)
                   1338:                        break;
                   1339:
                   1340:                /*
                   1341:                 * Get the current character. If not on a bracket, try the
                   1342:                 * next. If still not, then behave like next-word.
                   1343:                 */
                   1344:                tried = 0;
                   1345:        retry:
                   1346:                grid_get_cell(s->grid, px, py, &gc);
                   1347:                if (gc.data.size != 1 || (gc.flags & GRID_FLAG_PADDING))
                   1348:                        cp = NULL;
                   1349:                else {
                   1350:                        found = *gc.data.data;
                   1351:
                   1352:                        /*
                   1353:                         * In vi mode, attempt to move to previous bracket if a
                   1354:                         * closing bracket is found first. If this fails,
                   1355:                         * return to the original cursor position.
                   1356:                         */
                   1357:                        cp = strchr(close, found);
                   1358:                        if (cp != NULL && data->modekeys == MODEKEY_VI) {
                   1359:                                sx = data->cx;
                   1360:                                sy = screen_hsize(s) + data->cy - data->oy;
                   1361:
                   1362:                                window_copy_scroll_to(wme, px, py);
                   1363:                                window_copy_cmd_previous_matching_bracket(cs);
                   1364:
                   1365:                                px = data->cx;
                   1366:                                py = screen_hsize(s) + data->cy - data->oy;
                   1367:                                grid_get_cell(s->grid, px, py, &gc);
1.286     nicm     1368:                                if (gc.data.size == 1 &&
                   1369:                                    (~gc.flags & GRID_FLAG_PADDING) &&
                   1370:                                    strchr(close, *gc.data.data) != NULL)
1.218     nicm     1371:                                        window_copy_scroll_to(wme, sx, sy);
                   1372:                                break;
                   1373:                        }
                   1374:
                   1375:                        cp = strchr(open, found);
                   1376:                }
                   1377:                if (cp == NULL) {
                   1378:                        if (data->modekeys == MODEKEY_EMACS) {
                   1379:                                if (!tried && px <= xx) {
                   1380:                                        px++;
                   1381:                                        tried = 1;
                   1382:                                        goto retry;
                   1383:                                }
1.255     nicm     1384:                                window_copy_cursor_next_word_end(wme, "{[( ",
                   1385:                                    0);
1.218     nicm     1386:                                continue;
                   1387:                        }
                   1388:                        /* For vi, continue searching for bracket until EOL. */
                   1389:                        if (px > xx) {
                   1390:                                if (py == yy)
                   1391:                                        continue;
                   1392:                                gl = grid_get_line(s->grid, py);
                   1393:                                if (~gl->flags & GRID_LINE_WRAPPED)
                   1394:                                        continue;
                   1395:                                if (gl->cellsize > s->grid->sx)
                   1396:                                        continue;
                   1397:                                px = 0;
                   1398:                                py++;
                   1399:                                xx = window_copy_find_length(wme, py);
                   1400:                        } else
                   1401:                                px++;
                   1402:                        goto retry;
                   1403:                }
                   1404:                end = close[cp - open];
                   1405:
                   1406:                /* Walk forward until the matching bracket is reached. */
                   1407:                n = 1;
                   1408:                failed = 0;
                   1409:                do {
                   1410:                        if (px > xx) {
                   1411:                                if (py == yy) {
                   1412:                                        failed = 1;
                   1413:                                        break;
                   1414:                                }
                   1415:                                px = 0;
                   1416:                                py++;
                   1417:                                xx = window_copy_find_length(wme, py);
                   1418:                        } else
                   1419:                                px++;
                   1420:
                   1421:                        grid_get_cell(s->grid, px, py, &gc);
                   1422:                        if (gc.data.size == 1 &&
                   1423:                            (~gc.flags & GRID_FLAG_PADDING)) {
                   1424:                                if (*gc.data.data == found)
                   1425:                                        n++;
                   1426:                                else if (*gc.data.data == end)
                   1427:                                        n--;
                   1428:                        }
                   1429:                } while (n != 0);
                   1430:
                   1431:                /* Move the cursor to the found location if any. */
                   1432:                if (!failed)
                   1433:                        window_copy_scroll_to(wme, px, py);
                   1434:        }
                   1435:
                   1436:        return (WINDOW_COPY_CMD_NOTHING);
                   1437: }
                   1438:
                   1439: static enum window_copy_cmd_action
1.213     nicm     1440: window_copy_cmd_next_paragraph(struct window_copy_cmd_state *cs)
                   1441: {
                   1442:        struct window_mode_entry        *wme = cs->wme;
                   1443:        u_int                            np = wme->prefix;
                   1444:
                   1445:        for (; np != 0; np--)
                   1446:                window_copy_next_paragraph(wme);
                   1447:        return (WINDOW_COPY_CMD_NOTHING);
                   1448: }
                   1449:
                   1450: static enum window_copy_cmd_action
                   1451: window_copy_cmd_next_space(struct window_copy_cmd_state *cs)
                   1452: {
                   1453:        struct window_mode_entry        *wme = cs->wme;
                   1454:        u_int                            np = wme->prefix;
                   1455:
                   1456:        for (; np != 0; np--)
                   1457:                window_copy_cursor_next_word(wme, " ");
                   1458:        return (WINDOW_COPY_CMD_NOTHING);
                   1459: }
                   1460:
                   1461: static enum window_copy_cmd_action
                   1462: window_copy_cmd_next_space_end(struct window_copy_cmd_state *cs)
                   1463: {
                   1464:        struct window_mode_entry        *wme = cs->wme;
                   1465:        u_int                            np = wme->prefix;
                   1466:
                   1467:        for (; np != 0; np--)
1.255     nicm     1468:                window_copy_cursor_next_word_end(wme, " ", 0);
1.213     nicm     1469:        return (WINDOW_COPY_CMD_NOTHING);
                   1470: }
                   1471:
                   1472: static enum window_copy_cmd_action
                   1473: window_copy_cmd_next_word(struct window_copy_cmd_state *cs)
                   1474: {
                   1475:        struct window_mode_entry        *wme = cs->wme;
                   1476:        struct session                  *s = cs->s;
                   1477:        u_int                            np = wme->prefix;
                   1478:        const char                      *ws;
                   1479:
                   1480:        ws = options_get_string(s->options, "word-separators");
                   1481:        for (; np != 0; np--)
                   1482:                window_copy_cursor_next_word(wme, ws);
                   1483:        return (WINDOW_COPY_CMD_NOTHING);
                   1484: }
                   1485:
                   1486: static enum window_copy_cmd_action
                   1487: window_copy_cmd_next_word_end(struct window_copy_cmd_state *cs)
                   1488: {
                   1489:        struct window_mode_entry        *wme = cs->wme;
                   1490:        struct session                  *s = cs->s;
                   1491:        u_int                            np = wme->prefix;
                   1492:        const char                      *ws;
                   1493:
                   1494:        ws = options_get_string(s->options, "word-separators");
                   1495:        for (; np != 0; np--)
1.255     nicm     1496:                window_copy_cursor_next_word_end(wme, ws, 0);
1.213     nicm     1497:        return (WINDOW_COPY_CMD_NOTHING);
                   1498: }
                   1499:
                   1500: static enum window_copy_cmd_action
                   1501: window_copy_cmd_other_end(struct window_copy_cmd_state *cs)
                   1502: {
                   1503:        struct window_mode_entry        *wme = cs->wme;
                   1504:        u_int                            np = wme->prefix;
1.254     nicm     1505:        struct window_copy_mode_data    *data = wme->data;
1.213     nicm     1506:
1.254     nicm     1507:        data->selflag = SEL_CHAR;
1.213     nicm     1508:        if ((np % 2) != 0)
                   1509:                window_copy_other_end(wme);
                   1510:        return (WINDOW_COPY_CMD_NOTHING);
                   1511: }
                   1512:
                   1513: static enum window_copy_cmd_action
                   1514: window_copy_cmd_page_down(struct window_copy_cmd_state *cs)
                   1515: {
                   1516:        struct window_mode_entry        *wme = cs->wme;
                   1517:        struct window_copy_mode_data    *data = wme->data;
                   1518:        u_int                            np = wme->prefix;
                   1519:
                   1520:        for (; np != 0; np--) {
                   1521:                if (window_copy_pagedown(wme, 0, data->scroll_exit))
                   1522:                        return (WINDOW_COPY_CMD_CANCEL);
                   1523:        }
                   1524:        return (WINDOW_COPY_CMD_NOTHING);
                   1525: }
                   1526:
                   1527: static enum window_copy_cmd_action
                   1528: window_copy_cmd_page_down_and_cancel(struct window_copy_cmd_state *cs)
                   1529: {
                   1530:        struct window_mode_entry        *wme = cs->wme;
                   1531:        u_int                            np = wme->prefix;
                   1532:
                   1533:        for (; np != 0; np--) {
                   1534:                if (window_copy_pagedown(wme, 0, 1))
                   1535:                        return (WINDOW_COPY_CMD_CANCEL);
                   1536:        }
                   1537:        return (WINDOW_COPY_CMD_NOTHING);
                   1538: }
                   1539:
                   1540: static enum window_copy_cmd_action
                   1541: window_copy_cmd_page_up(struct window_copy_cmd_state *cs)
                   1542: {
                   1543:        struct window_mode_entry        *wme = cs->wme;
                   1544:        u_int                            np = wme->prefix;
                   1545:
                   1546:        for (; np != 0; np--)
                   1547:                window_copy_pageup1(wme, 0);
                   1548:        return (WINDOW_COPY_CMD_NOTHING);
                   1549: }
                   1550:
                   1551: static enum window_copy_cmd_action
                   1552: window_copy_cmd_previous_paragraph(struct window_copy_cmd_state *cs)
                   1553: {
                   1554:        struct window_mode_entry        *wme = cs->wme;
                   1555:        u_int                            np = wme->prefix;
                   1556:
                   1557:        for (; np != 0; np--)
                   1558:                window_copy_previous_paragraph(wme);
                   1559:        return (WINDOW_COPY_CMD_NOTHING);
                   1560: }
                   1561:
                   1562: static enum window_copy_cmd_action
                   1563: window_copy_cmd_previous_space(struct window_copy_cmd_state *cs)
                   1564: {
                   1565:        struct window_mode_entry        *wme = cs->wme;
                   1566:        u_int                            np = wme->prefix;
                   1567:
                   1568:        for (; np != 0; np--)
1.229     nicm     1569:                window_copy_cursor_previous_word(wme, " ", 1);
1.213     nicm     1570:        return (WINDOW_COPY_CMD_NOTHING);
                   1571: }
                   1572:
                   1573: static enum window_copy_cmd_action
                   1574: window_copy_cmd_previous_word(struct window_copy_cmd_state *cs)
                   1575: {
                   1576:        struct window_mode_entry        *wme = cs->wme;
                   1577:        struct session                  *s = cs->s;
                   1578:        u_int                            np = wme->prefix;
                   1579:        const char                      *ws;
                   1580:
                   1581:        ws = options_get_string(s->options, "word-separators");
                   1582:        for (; np != 0; np--)
1.229     nicm     1583:                window_copy_cursor_previous_word(wme, ws, 1);
1.213     nicm     1584:        return (WINDOW_COPY_CMD_NOTHING);
                   1585: }
                   1586:
                   1587: static enum window_copy_cmd_action
                   1588: window_copy_cmd_rectangle_toggle(struct window_copy_cmd_state *cs)
                   1589: {
                   1590:        struct window_mode_entry        *wme = cs->wme;
                   1591:        struct window_copy_mode_data    *data = wme->data;
                   1592:
                   1593:        data->lineflag = LINE_SEL_NONE;
                   1594:        window_copy_rectangle_toggle(wme);
                   1595:
                   1596:        return (WINDOW_COPY_CMD_NOTHING);
                   1597: }
                   1598:
                   1599: static enum window_copy_cmd_action
                   1600: window_copy_cmd_scroll_down(struct window_copy_cmd_state *cs)
                   1601: {
                   1602:        struct window_mode_entry        *wme = cs->wme;
                   1603:        struct window_copy_mode_data    *data = wme->data;
                   1604:        u_int                            np = wme->prefix;
                   1605:
                   1606:        for (; np != 0; np--)
                   1607:                window_copy_cursor_down(wme, 1);
                   1608:        if (data->scroll_exit && data->oy == 0)
                   1609:                return (WINDOW_COPY_CMD_CANCEL);
                   1610:        return (WINDOW_COPY_CMD_NOTHING);
                   1611: }
                   1612:
                   1613: static enum window_copy_cmd_action
                   1614: window_copy_cmd_scroll_down_and_cancel(struct window_copy_cmd_state *cs)
                   1615: {
                   1616:        struct window_mode_entry        *wme = cs->wme;
                   1617:        struct window_copy_mode_data    *data = wme->data;
                   1618:        u_int                            np = wme->prefix;
                   1619:
                   1620:        for (; np != 0; np--)
                   1621:                window_copy_cursor_down(wme, 1);
                   1622:        if (data->oy == 0)
                   1623:                return (WINDOW_COPY_CMD_CANCEL);
                   1624:        return (WINDOW_COPY_CMD_NOTHING);
                   1625: }
                   1626:
                   1627: static enum window_copy_cmd_action
                   1628: window_copy_cmd_scroll_up(struct window_copy_cmd_state *cs)
                   1629: {
                   1630:        struct window_mode_entry        *wme = cs->wme;
                   1631:        u_int                            np = wme->prefix;
                   1632:
                   1633:        for (; np != 0; np--)
                   1634:                window_copy_cursor_up(wme, 1);
                   1635:        return (WINDOW_COPY_CMD_NOTHING);
                   1636: }
                   1637:
                   1638: static enum window_copy_cmd_action
                   1639: window_copy_cmd_search_again(struct window_copy_cmd_state *cs)
                   1640: {
                   1641:        struct window_mode_entry        *wme = cs->wme;
                   1642:        struct window_copy_mode_data    *data = wme->data;
                   1643:        u_int                            np = wme->prefix;
                   1644:
                   1645:        if (data->searchtype == WINDOW_COPY_SEARCHUP) {
                   1646:                for (; np != 0; np--)
1.259     nicm     1647:                        window_copy_search_up(wme, data->searchregex);
1.213     nicm     1648:        } else if (data->searchtype == WINDOW_COPY_SEARCHDOWN) {
                   1649:                for (; np != 0; np--)
1.259     nicm     1650:                        window_copy_search_down(wme, data->searchregex);
1.213     nicm     1651:        }
                   1652:        return (WINDOW_COPY_CMD_NOTHING);
                   1653: }
                   1654:
                   1655: static enum window_copy_cmd_action
                   1656: window_copy_cmd_search_reverse(struct window_copy_cmd_state *cs)
                   1657: {
                   1658:        struct window_mode_entry        *wme = cs->wme;
                   1659:        struct window_copy_mode_data    *data = wme->data;
                   1660:        u_int                            np = wme->prefix;
                   1661:
                   1662:        if (data->searchtype == WINDOW_COPY_SEARCHUP) {
                   1663:                for (; np != 0; np--)
1.259     nicm     1664:                        window_copy_search_down(wme, data->searchregex);
1.213     nicm     1665:        } else if (data->searchtype == WINDOW_COPY_SEARCHDOWN) {
                   1666:                for (; np != 0; np--)
1.259     nicm     1667:                        window_copy_search_up(wme, data->searchregex);
1.213     nicm     1668:        }
                   1669:        return (WINDOW_COPY_CMD_NOTHING);
                   1670: }
                   1671:
                   1672: static enum window_copy_cmd_action
                   1673: window_copy_cmd_select_line(struct window_copy_cmd_state *cs)
                   1674: {
                   1675:        struct window_mode_entry        *wme = cs->wme;
                   1676:        struct window_copy_mode_data    *data = wme->data;
                   1677:        u_int                            np = wme->prefix;
                   1678:
                   1679:        data->lineflag = LINE_SEL_LEFT_RIGHT;
                   1680:        data->rectflag = 0;
1.247     nicm     1681:        data->selflag = SEL_LINE;
                   1682:        data->dx = data->cx;
                   1683:        data->dy = screen_hsize(data->backing) + data->cy - data->oy;
1.213     nicm     1684:
                   1685:        window_copy_cursor_start_of_line(wme);
1.247     nicm     1686:        data->selrx = data->cx;
                   1687:        data->selry = screen_hsize(data->backing) + data->cy - data->oy;
1.258     nicm     1688:        data->endselrx = window_copy_find_length(wme, data->selry);
                   1689:        data->endselry = data->selry;
1.213     nicm     1690:        window_copy_start_selection(wme);
                   1691:        for (; np > 1; np--)
                   1692:                window_copy_cursor_down(wme, 0);
                   1693:        window_copy_cursor_end_of_line(wme);
                   1694:
                   1695:        return (WINDOW_COPY_CMD_REDRAW);
                   1696: }
                   1697:
                   1698: static enum window_copy_cmd_action
                   1699: window_copy_cmd_select_word(struct window_copy_cmd_state *cs)
                   1700: {
                   1701:        struct window_mode_entry        *wme = cs->wme;
                   1702:        struct session                  *s = cs->s;
                   1703:        struct window_copy_mode_data    *data = wme->data;
1.247     nicm     1704:        u_int                            px, py;
1.213     nicm     1705:
                   1706:        data->lineflag = LINE_SEL_LEFT_RIGHT;
                   1707:        data->rectflag = 0;
1.247     nicm     1708:        data->selflag = SEL_WORD;
                   1709:        data->dx = data->cx;
                   1710:        data->dy = screen_hsize(data->backing) + data->cy - data->oy;
1.213     nicm     1711:
1.257     nicm     1712:        data->ws = options_get_string(s->options, "word-separators");
                   1713:        window_copy_cursor_previous_word(wme, data->ws, 0);
1.245     nicm     1714:        px = data->cx;
                   1715:        py = screen_hsize(data->backing) + data->cy - data->oy;
1.257     nicm     1716:        data->selrx = px;
                   1717:        data->selry = py;
1.213     nicm     1718:        window_copy_start_selection(wme);
1.245     nicm     1719:
1.247     nicm     1720:        if (px >= window_copy_find_length(wme, py) ||
1.252     nicm     1721:            !window_copy_in_set(wme, px + 1, py, data->ws))
1.255     nicm     1722:                window_copy_cursor_next_word_end(wme, data->ws, 1);
1.245     nicm     1723:        else {
                   1724:                window_copy_update_cursor(wme, px, data->cy);
1.255     nicm     1725:                if (window_copy_update_selection(wme, 1, 1))
1.245     nicm     1726:                        window_copy_redraw_lines(wme, data->cy, 1);
                   1727:        }
1.247     nicm     1728:        data->endselrx = data->cx;
                   1729:        data->endselry = screen_hsize(data->backing) + data->cy - data->oy;
1.257     nicm     1730:        if (data->dx > data->endselrx)
                   1731:                data->dx = data->endselrx;
1.213     nicm     1732:
                   1733:        return (WINDOW_COPY_CMD_REDRAW);
                   1734: }
                   1735:
                   1736: static enum window_copy_cmd_action
                   1737: window_copy_cmd_start_of_line(struct window_copy_cmd_state *cs)
                   1738: {
                   1739:        struct window_mode_entry        *wme = cs->wme;
                   1740:
                   1741:        window_copy_cursor_start_of_line(wme);
                   1742:        return (WINDOW_COPY_CMD_NOTHING);
                   1743: }
                   1744:
                   1745: static enum window_copy_cmd_action
                   1746: window_copy_cmd_top_line(struct window_copy_cmd_state *cs)
                   1747: {
                   1748:        struct window_mode_entry        *wme = cs->wme;
                   1749:        struct window_copy_mode_data    *data = wme->data;
                   1750:
                   1751:        data->cx = 0;
                   1752:        data->cy = 0;
                   1753:
1.252     nicm     1754:        window_copy_update_selection(wme, 1, 0);
1.213     nicm     1755:        return (WINDOW_COPY_CMD_REDRAW);
                   1756: }
                   1757:
                   1758: static enum window_copy_cmd_action
1.216     nicm     1759: window_copy_cmd_copy_pipe_no_clear(struct window_copy_cmd_state *cs)
1.213     nicm     1760: {
                   1761:        struct window_mode_entry        *wme = cs->wme;
1.215     nicm     1762:        struct client                   *c = cs->c;
1.213     nicm     1763:        struct session                  *s = cs->s;
1.215     nicm     1764:        struct winlink                  *wl = cs->wl;
                   1765:        struct window_pane              *wp = wme->wp;
                   1766:        char                            *command = NULL;
                   1767:        char                            *prefix = NULL;
                   1768:
                   1769:        if (cs->args->argc == 3)
                   1770:                prefix = format_single(NULL, cs->args->argv[2], c, s, wl, wp);
                   1771:
1.277     nicm     1772:        if (s != NULL && cs->args->argc > 1 && *cs->args->argv[1] != '\0')
1.215     nicm     1773:                command = format_single(NULL, cs->args->argv[1], c, s, wl, wp);
1.277     nicm     1774:        window_copy_copy_pipe(wme, s, prefix, command);
                   1775:        free(command);
1.213     nicm     1776:
1.215     nicm     1777:        free(prefix);
1.213     nicm     1778:        return (WINDOW_COPY_CMD_NOTHING);
                   1779: }
                   1780:
                   1781: static enum window_copy_cmd_action
1.216     nicm     1782: window_copy_cmd_copy_pipe(struct window_copy_cmd_state *cs)
1.213     nicm     1783: {
                   1784:        struct window_mode_entry        *wme = cs->wme;
1.215     nicm     1785:
1.216     nicm     1786:        window_copy_cmd_copy_pipe_no_clear(cs);
                   1787:        window_copy_clear_selection(wme);
                   1788:        return (WINDOW_COPY_CMD_REDRAW);
                   1789: }
1.215     nicm     1790:
1.216     nicm     1791: static enum window_copy_cmd_action
                   1792: window_copy_cmd_copy_pipe_and_cancel(struct window_copy_cmd_state *cs)
                   1793: {
                   1794:        struct window_mode_entry        *wme = cs->wme;
1.213     nicm     1795:
1.216     nicm     1796:        window_copy_cmd_copy_pipe_no_clear(cs);
                   1797:        window_copy_clear_selection(wme);
                   1798:        return (WINDOW_COPY_CMD_CANCEL);
1.213     nicm     1799: }
                   1800:
                   1801: static enum window_copy_cmd_action
                   1802: window_copy_cmd_goto_line(struct window_copy_cmd_state *cs)
                   1803: {
                   1804:        struct window_mode_entry        *wme = cs->wme;
                   1805:        const char                      *argument = cs->args->argv[1];
                   1806:
                   1807:        if (*argument != '\0')
                   1808:                window_copy_goto_line(wme, argument);
                   1809:        return (WINDOW_COPY_CMD_NOTHING);
                   1810: }
                   1811:
                   1812: static enum window_copy_cmd_action
                   1813: window_copy_cmd_jump_backward(struct window_copy_cmd_state *cs)
                   1814: {
                   1815:        struct window_mode_entry        *wme = cs->wme;
                   1816:        struct window_copy_mode_data    *data = wme->data;
                   1817:        u_int                            np = wme->prefix;
                   1818:        const char                      *argument = cs->args->argv[1];
                   1819:
                   1820:        if (*argument != '\0') {
                   1821:                data->jumptype = WINDOW_COPY_JUMPBACKWARD;
                   1822:                data->jumpchar = *argument;
                   1823:                for (; np != 0; np--)
                   1824:                        window_copy_cursor_jump_back(wme);
                   1825:        }
                   1826:        return (WINDOW_COPY_CMD_NOTHING);
                   1827: }
                   1828:
                   1829: static enum window_copy_cmd_action
                   1830: window_copy_cmd_jump_forward(struct window_copy_cmd_state *cs)
                   1831: {
                   1832:        struct window_mode_entry        *wme = cs->wme;
                   1833:        struct window_copy_mode_data    *data = wme->data;
                   1834:        u_int                            np = wme->prefix;
                   1835:        const char                      *argument = cs->args->argv[1];
                   1836:
                   1837:        if (*argument != '\0') {
                   1838:                data->jumptype = WINDOW_COPY_JUMPFORWARD;
                   1839:                data->jumpchar = *argument;
                   1840:                for (; np != 0; np--)
1.215     nicm     1841:                        window_copy_cursor_jump(wme);
1.213     nicm     1842:        }
                   1843:        return (WINDOW_COPY_CMD_NOTHING);
                   1844: }
                   1845:
                   1846: static enum window_copy_cmd_action
                   1847: window_copy_cmd_jump_to_backward(struct window_copy_cmd_state *cs)
                   1848: {
                   1849:        struct window_mode_entry        *wme = cs->wme;
                   1850:        struct window_copy_mode_data    *data = wme->data;
                   1851:        u_int                            np = wme->prefix;
                   1852:        const char                      *argument = cs->args->argv[1];
                   1853:
                   1854:        if (*argument != '\0') {
                   1855:                data->jumptype = WINDOW_COPY_JUMPTOBACKWARD;
                   1856:                data->jumpchar = *argument;
                   1857:                for (; np != 0; np--)
                   1858:                        window_copy_cursor_jump_to_back(wme);
                   1859:        }
                   1860:        return (WINDOW_COPY_CMD_NOTHING);
                   1861: }
                   1862:
                   1863: static enum window_copy_cmd_action
                   1864: window_copy_cmd_jump_to_forward(struct window_copy_cmd_state *cs)
                   1865: {
                   1866:        struct window_mode_entry        *wme = cs->wme;
                   1867:        struct window_copy_mode_data    *data = wme->data;
                   1868:        u_int                            np = wme->prefix;
                   1869:        const char                      *argument = cs->args->argv[1];
                   1870:
                   1871:        if (*argument != '\0') {
                   1872:                data->jumptype = WINDOW_COPY_JUMPTOFORWARD;
                   1873:                data->jumpchar = *argument;
                   1874:                for (; np != 0; np--)
                   1875:                        window_copy_cursor_jump_to(wme);
                   1876:        }
                   1877:        return (WINDOW_COPY_CMD_NOTHING);
                   1878: }
                   1879:
                   1880: static enum window_copy_cmd_action
                   1881: window_copy_cmd_search_backward(struct window_copy_cmd_state *cs)
                   1882: {
                   1883:        struct window_mode_entry        *wme = cs->wme;
                   1884:        struct window_copy_mode_data    *data = wme->data;
                   1885:        u_int                            np = wme->prefix;
                   1886:
1.259     nicm     1887:        if (!window_copy_expand_search_string(cs))
                   1888:                return (WINDOW_COPY_CMD_NOTHING);
                   1889:
                   1890:        if (data->searchstr != NULL) {
                   1891:                data->searchtype = WINDOW_COPY_SEARCHUP;
                   1892:                data->searchregex = 1;
1.263     nicm     1893:                data->timeout = 0;
1.259     nicm     1894:                for (; np != 0; np--)
                   1895:                        window_copy_search_up(wme, 1);
1.233     nicm     1896:        }
1.259     nicm     1897:        return (WINDOW_COPY_CMD_NOTHING);
                   1898: }
                   1899:
                   1900: static enum window_copy_cmd_action
                   1901: window_copy_cmd_search_backward_text(struct window_copy_cmd_state *cs)
                   1902: {
                   1903:        struct window_mode_entry        *wme = cs->wme;
                   1904:        struct window_copy_mode_data    *data = wme->data;
                   1905:        u_int                            np = wme->prefix;
                   1906:
                   1907:        if (!window_copy_expand_search_string(cs))
                   1908:                return (WINDOW_COPY_CMD_NOTHING);
                   1909:
1.233     nicm     1910:        if (data->searchstr != NULL) {
1.232     nicm     1911:                data->searchtype = WINDOW_COPY_SEARCHUP;
1.259     nicm     1912:                data->searchregex = 0;
1.263     nicm     1913:                data->timeout = 0;
1.213     nicm     1914:                for (; np != 0; np--)
1.259     nicm     1915:                        window_copy_search_up(wme, 0);
1.213     nicm     1916:        }
                   1917:        return (WINDOW_COPY_CMD_NOTHING);
                   1918: }
                   1919:
                   1920: static enum window_copy_cmd_action
                   1921: window_copy_cmd_search_forward(struct window_copy_cmd_state *cs)
                   1922: {
                   1923:        struct window_mode_entry        *wme = cs->wme;
                   1924:        struct window_copy_mode_data    *data = wme->data;
                   1925:        u_int                            np = wme->prefix;
                   1926:
1.259     nicm     1927:        if (!window_copy_expand_search_string(cs))
                   1928:                return (WINDOW_COPY_CMD_NOTHING);
                   1929:
                   1930:        if (data->searchstr != NULL) {
                   1931:                data->searchtype = WINDOW_COPY_SEARCHDOWN;
                   1932:                data->searchregex = 1;
1.263     nicm     1933:                data->timeout = 0;
1.259     nicm     1934:                for (; np != 0; np--)
                   1935:                        window_copy_search_down(wme, 1);
1.233     nicm     1936:        }
1.259     nicm     1937:        return (WINDOW_COPY_CMD_NOTHING);
                   1938: }
                   1939:
                   1940: static enum window_copy_cmd_action
                   1941: window_copy_cmd_search_forward_text(struct window_copy_cmd_state *cs)
                   1942: {
                   1943:        struct window_mode_entry        *wme = cs->wme;
                   1944:        struct window_copy_mode_data    *data = wme->data;
                   1945:        u_int                            np = wme->prefix;
                   1946:
                   1947:        if (!window_copy_expand_search_string(cs))
                   1948:                return (WINDOW_COPY_CMD_NOTHING);
                   1949:
1.233     nicm     1950:        if (data->searchstr != NULL) {
1.232     nicm     1951:                data->searchtype = WINDOW_COPY_SEARCHDOWN;
1.259     nicm     1952:                data->searchregex = 0;
1.263     nicm     1953:                data->timeout = 0;
1.213     nicm     1954:                for (; np != 0; np--)
1.259     nicm     1955:                        window_copy_search_down(wme, 0);
1.213     nicm     1956:        }
                   1957:        return (WINDOW_COPY_CMD_NOTHING);
                   1958: }
                   1959:
                   1960: static enum window_copy_cmd_action
                   1961: window_copy_cmd_search_backward_incremental(struct window_copy_cmd_state *cs)
                   1962: {
                   1963:        struct window_mode_entry        *wme = cs->wme;
                   1964:        struct window_copy_mode_data    *data = wme->data;
                   1965:        const char                      *argument = cs->args->argv[1];
1.225     nicm     1966:        const char                      *ss = data->searchstr;
                   1967:        char                             prefix;
1.213     nicm     1968:        enum window_copy_cmd_action      action = WINDOW_COPY_CMD_NOTHING;
                   1969:
1.263     nicm     1970:        data->timeout = 0;
                   1971:
1.225     nicm     1972:        prefix = *argument++;
1.213     nicm     1973:        if (data->searchx == -1 || data->searchy == -1) {
                   1974:                data->searchx = data->cx;
                   1975:                data->searchy = data->cy;
                   1976:                data->searcho = data->oy;
                   1977:        } else if (ss != NULL && strcmp(argument, ss) != 0) {
                   1978:                data->cx = data->searchx;
                   1979:                data->cy = data->searchy;
                   1980:                data->oy = data->searcho;
                   1981:                action = WINDOW_COPY_CMD_REDRAW;
                   1982:        }
                   1983:        if (*argument == '\0') {
                   1984:                window_copy_clear_marks(wme);
                   1985:                return (WINDOW_COPY_CMD_REDRAW);
                   1986:        }
1.225     nicm     1987:        switch (prefix) {
1.213     nicm     1988:        case '=':
                   1989:        case '-':
                   1990:                data->searchtype = WINDOW_COPY_SEARCHUP;
1.259     nicm     1991:                data->searchregex = 0;
1.213     nicm     1992:                free(data->searchstr);
                   1993:                data->searchstr = xstrdup(argument);
1.244     nicm     1994:                if (!window_copy_search_up(wme, 0)) {
1.213     nicm     1995:                        window_copy_clear_marks(wme);
                   1996:                        return (WINDOW_COPY_CMD_REDRAW);
                   1997:                }
                   1998:                break;
                   1999:        case '+':
                   2000:                data->searchtype = WINDOW_COPY_SEARCHDOWN;
1.259     nicm     2001:                data->searchregex = 0;
1.213     nicm     2002:                free(data->searchstr);
                   2003:                data->searchstr = xstrdup(argument);
1.244     nicm     2004:                if (!window_copy_search_down(wme, 0)) {
1.213     nicm     2005:                        window_copy_clear_marks(wme);
                   2006:                        return (WINDOW_COPY_CMD_REDRAW);
                   2007:                }
                   2008:                break;
                   2009:        }
                   2010:        return (action);
                   2011: }
                   2012:
                   2013: static enum window_copy_cmd_action
                   2014: window_copy_cmd_search_forward_incremental(struct window_copy_cmd_state *cs)
                   2015: {
                   2016:        struct window_mode_entry        *wme = cs->wme;
                   2017:        struct window_copy_mode_data    *data = wme->data;
                   2018:        const char                      *argument = cs->args->argv[1];
1.225     nicm     2019:        const char                      *ss = data->searchstr;
                   2020:        char                             prefix;
1.213     nicm     2021:        enum window_copy_cmd_action      action = WINDOW_COPY_CMD_NOTHING;
                   2022:
1.263     nicm     2023:        data->timeout = 0;
                   2024:
1.225     nicm     2025:        prefix = *argument++;
1.213     nicm     2026:        if (data->searchx == -1 || data->searchy == -1) {
                   2027:                data->searchx = data->cx;
                   2028:                data->searchy = data->cy;
                   2029:                data->searcho = data->oy;
                   2030:        } else if (ss != NULL && strcmp(argument, ss) != 0) {
                   2031:                data->cx = data->searchx;
                   2032:                data->cy = data->searchy;
                   2033:                data->oy = data->searcho;
                   2034:                action = WINDOW_COPY_CMD_REDRAW;
                   2035:        }
                   2036:        if (*argument == '\0') {
                   2037:                window_copy_clear_marks(wme);
                   2038:                return (WINDOW_COPY_CMD_REDRAW);
                   2039:        }
1.225     nicm     2040:        switch (prefix) {
1.213     nicm     2041:        case '=':
                   2042:        case '+':
                   2043:                data->searchtype = WINDOW_COPY_SEARCHDOWN;
1.259     nicm     2044:                data->searchregex = 0;
1.213     nicm     2045:                free(data->searchstr);
                   2046:                data->searchstr = xstrdup(argument);
1.244     nicm     2047:                if (!window_copy_search_down(wme, 0)) {
1.213     nicm     2048:                        window_copy_clear_marks(wme);
                   2049:                        return (WINDOW_COPY_CMD_REDRAW);
                   2050:                }
                   2051:                break;
                   2052:        case '-':
                   2053:                data->searchtype = WINDOW_COPY_SEARCHUP;
1.259     nicm     2054:                data->searchregex = 0;
1.213     nicm     2055:                free(data->searchstr);
                   2056:                data->searchstr = xstrdup(argument);
1.244     nicm     2057:                if (!window_copy_search_up(wme, 0)) {
1.213     nicm     2058:                        window_copy_clear_marks(wme);
                   2059:                        return (WINDOW_COPY_CMD_REDRAW);
                   2060:                }
                   2061:        }
                   2062:        return (action);
                   2063: }
                   2064:
1.266     nicm     2065: static enum window_copy_cmd_action
                   2066: window_copy_cmd_refresh_from_pane(struct window_copy_cmd_state *cs)
                   2067: {
                   2068:        struct window_mode_entry        *wme = cs->wme;
1.270     nicm     2069:        struct window_pane              *wp = wme->swp;
1.266     nicm     2070:        struct window_copy_mode_data    *data = wme->data;
                   2071:
                   2072:        if (data->viewmode)
                   2073:                return (WINDOW_COPY_CMD_NOTHING);
                   2074:
                   2075:        screen_free(data->backing);
                   2076:        free(data->backing);
1.274     nicm     2077:        data->backing = window_copy_clone_screen(&wp->base, &data->screen, NULL,
1.279     nicm     2078:            NULL, wme->swp != wme->wp);
1.266     nicm     2079:
1.275     nicm     2080:        window_copy_size_changed(wme);
1.266     nicm     2081:        return (WINDOW_COPY_CMD_REDRAW);
                   2082: }
                   2083:
1.213     nicm     2084: static const struct {
                   2085:        const char                       *command;
                   2086:        int                               minargs;
                   2087:        int                               maxargs;
1.239     nicm     2088:        int                               ismotion;
1.213     nicm     2089:        enum window_copy_cmd_action     (*f)(struct window_copy_cmd_state *);
                   2090: } window_copy_cmd_table[] = {
1.239     nicm     2091:        { "append-selection", 0, 0, 0,
1.213     nicm     2092:          window_copy_cmd_append_selection },
1.239     nicm     2093:        { "append-selection-and-cancel", 0, 0, 0,
1.213     nicm     2094:          window_copy_cmd_append_selection_and_cancel },
1.239     nicm     2095:        { "back-to-indentation", 0, 0, 0,
1.213     nicm     2096:          window_copy_cmd_back_to_indentation },
1.239     nicm     2097:        { "begin-selection", 0, 0, 0,
1.213     nicm     2098:          window_copy_cmd_begin_selection },
1.239     nicm     2099:        { "bottom-line", 0, 0, 1,
1.213     nicm     2100:          window_copy_cmd_bottom_line },
1.239     nicm     2101:        { "cancel", 0, 0, 0,
1.213     nicm     2102:          window_copy_cmd_cancel },
1.239     nicm     2103:        { "clear-selection", 0, 0, 0,
1.213     nicm     2104:          window_copy_cmd_clear_selection },
1.239     nicm     2105:        { "copy-end-of-line", 0, 1, 0,
1.213     nicm     2106:          window_copy_cmd_copy_end_of_line },
1.239     nicm     2107:        { "copy-line", 0, 1, 0,
1.213     nicm     2108:          window_copy_cmd_copy_line },
1.277     nicm     2109:        { "copy-pipe-no-clear", 0, 2, 0,
1.216     nicm     2110:          window_copy_cmd_copy_pipe_no_clear },
1.277     nicm     2111:        { "copy-pipe", 0, 2, 0,
1.213     nicm     2112:          window_copy_cmd_copy_pipe },
1.277     nicm     2113:        { "copy-pipe-and-cancel", 0, 2, 0,
1.213     nicm     2114:          window_copy_cmd_copy_pipe_and_cancel },
1.239     nicm     2115:        { "copy-selection-no-clear", 0, 1, 0,
1.216     nicm     2116:          window_copy_cmd_copy_selection_no_clear },
1.239     nicm     2117:        { "copy-selection", 0, 1, 0,
1.213     nicm     2118:          window_copy_cmd_copy_selection },
1.239     nicm     2119:        { "copy-selection-and-cancel", 0, 1, 0,
1.213     nicm     2120:          window_copy_cmd_copy_selection_and_cancel },
1.239     nicm     2121:        { "cursor-down", 0, 0, 1,
1.213     nicm     2122:          window_copy_cmd_cursor_down },
1.239     nicm     2123:        { "cursor-down-and-cancel", 0, 0, 0,
1.234     nicm     2124:          window_copy_cmd_cursor_down_and_cancel },
1.239     nicm     2125:        { "cursor-left", 0, 0, 1,
1.213     nicm     2126:          window_copy_cmd_cursor_left },
1.239     nicm     2127:        { "cursor-right", 0, 0, 1,
1.213     nicm     2128:          window_copy_cmd_cursor_right },
1.239     nicm     2129:        { "cursor-up", 0, 0, 1,
1.213     nicm     2130:          window_copy_cmd_cursor_up },
1.239     nicm     2131:        { "end-of-line", 0, 0, 1,
1.213     nicm     2132:          window_copy_cmd_end_of_line },
1.239     nicm     2133:        { "goto-line", 1, 1, 1,
1.213     nicm     2134:          window_copy_cmd_goto_line },
1.239     nicm     2135:        { "halfpage-down", 0, 0, 1,
1.213     nicm     2136:          window_copy_cmd_halfpage_down },
1.239     nicm     2137:        { "halfpage-down-and-cancel", 0, 0, 0,
1.213     nicm     2138:          window_copy_cmd_halfpage_down_and_cancel },
1.239     nicm     2139:        { "halfpage-up", 0, 0, 1,
1.213     nicm     2140:          window_copy_cmd_halfpage_up },
1.239     nicm     2141:        { "history-bottom", 0, 0, 1,
1.213     nicm     2142:          window_copy_cmd_history_bottom },
1.239     nicm     2143:        { "history-top", 0, 0, 1,
1.213     nicm     2144:          window_copy_cmd_history_top },
1.239     nicm     2145:        { "jump-again", 0, 0, 1,
1.213     nicm     2146:          window_copy_cmd_jump_again },
1.239     nicm     2147:        { "jump-backward", 1, 1, 1,
1.213     nicm     2148:          window_copy_cmd_jump_backward },
1.239     nicm     2149:        { "jump-forward", 1, 1, 1,
1.213     nicm     2150:          window_copy_cmd_jump_forward },
1.239     nicm     2151:        { "jump-reverse", 0, 0, 1,
1.213     nicm     2152:          window_copy_cmd_jump_reverse },
1.239     nicm     2153:        { "jump-to-backward", 1, 1, 1,
1.213     nicm     2154:          window_copy_cmd_jump_to_backward },
1.239     nicm     2155:        { "jump-to-forward", 1, 1, 1,
1.213     nicm     2156:          window_copy_cmd_jump_to_forward },
1.239     nicm     2157:        { "middle-line", 0, 0, 1,
1.213     nicm     2158:          window_copy_cmd_middle_line },
1.239     nicm     2159:        { "next-matching-bracket", 0, 0, 0,
1.218     nicm     2160:          window_copy_cmd_next_matching_bracket },
1.239     nicm     2161:        { "next-paragraph", 0, 0, 1,
1.213     nicm     2162:          window_copy_cmd_next_paragraph },
1.239     nicm     2163:        { "next-space", 0, 0, 1,
1.213     nicm     2164:          window_copy_cmd_next_space },
1.239     nicm     2165:        { "next-space-end", 0, 0, 1,
1.213     nicm     2166:          window_copy_cmd_next_space_end },
1.239     nicm     2167:        { "next-word", 0, 0, 1,
1.213     nicm     2168:          window_copy_cmd_next_word },
1.239     nicm     2169:        { "next-word-end", 0, 0, 1,
1.213     nicm     2170:          window_copy_cmd_next_word_end },
1.239     nicm     2171:        { "other-end", 0, 0, 1,
1.213     nicm     2172:          window_copy_cmd_other_end },
1.239     nicm     2173:        { "page-down", 0, 0, 1,
1.213     nicm     2174:          window_copy_cmd_page_down },
1.239     nicm     2175:        { "page-down-and-cancel", 0, 0, 0,
1.213     nicm     2176:          window_copy_cmd_page_down_and_cancel },
1.239     nicm     2177:        { "page-up", 0, 0, 1,
1.213     nicm     2178:          window_copy_cmd_page_up },
1.239     nicm     2179:        { "previous-matching-bracket", 0, 0, 0,
1.218     nicm     2180:          window_copy_cmd_previous_matching_bracket },
1.239     nicm     2181:        { "previous-paragraph", 0, 0, 1,
1.213     nicm     2182:          window_copy_cmd_previous_paragraph },
1.239     nicm     2183:        { "previous-space", 0, 0, 1,
1.213     nicm     2184:          window_copy_cmd_previous_space },
1.239     nicm     2185:        { "previous-word", 0, 0, 1,
1.213     nicm     2186:          window_copy_cmd_previous_word },
1.239     nicm     2187:        { "rectangle-toggle", 0, 0, 0,
1.213     nicm     2188:          window_copy_cmd_rectangle_toggle },
1.266     nicm     2189:        { "refresh-from-pane", 0, 0, 0,
                   2190:          window_copy_cmd_refresh_from_pane },
1.239     nicm     2191:        { "scroll-down", 0, 0, 1,
1.213     nicm     2192:          window_copy_cmd_scroll_down },
1.239     nicm     2193:        { "scroll-down-and-cancel", 0, 0, 0,
1.213     nicm     2194:          window_copy_cmd_scroll_down_and_cancel },
1.239     nicm     2195:        { "scroll-up", 0, 0, 1,
1.213     nicm     2196:          window_copy_cmd_scroll_up },
1.239     nicm     2197:        { "search-again", 0, 0, 0,
1.213     nicm     2198:          window_copy_cmd_search_again },
1.239     nicm     2199:        { "search-backward", 0, 1, 0,
1.213     nicm     2200:          window_copy_cmd_search_backward },
1.259     nicm     2201:        { "search-backward-text", 0, 1, 0,
                   2202:          window_copy_cmd_search_backward_text },
1.239     nicm     2203:        { "search-backward-incremental", 1, 1, 0,
1.213     nicm     2204:          window_copy_cmd_search_backward_incremental },
1.239     nicm     2205:        { "search-forward", 0, 1, 0,
1.213     nicm     2206:          window_copy_cmd_search_forward },
1.259     nicm     2207:        { "search-forward-text", 0, 1, 0,
                   2208:          window_copy_cmd_search_forward_text },
1.239     nicm     2209:        { "search-forward-incremental", 1, 1, 0,
1.213     nicm     2210:          window_copy_cmd_search_forward_incremental },
1.239     nicm     2211:        { "search-reverse", 0, 0, 0,
1.213     nicm     2212:          window_copy_cmd_search_reverse },
1.239     nicm     2213:        { "select-line", 0, 0, 0,
1.213     nicm     2214:          window_copy_cmd_select_line },
1.239     nicm     2215:        { "select-word", 0, 0, 0,
1.213     nicm     2216:          window_copy_cmd_select_word },
1.239     nicm     2217:        { "start-of-line", 0, 0, 1,
1.213     nicm     2218:          window_copy_cmd_start_of_line },
1.239     nicm     2219:        { "stop-selection", 0, 0, 0,
1.213     nicm     2220:          window_copy_cmd_stop_selection },
1.240     nicm     2221:        { "top-line", 0, 0, 1,
1.213     nicm     2222:          window_copy_cmd_top_line },
                   2223: };
                   2224:
1.157     nicm     2225: static void
1.208     nicm     2226: window_copy_command(struct window_mode_entry *wme, struct client *c,
1.215     nicm     2227:     struct session *s, struct winlink *wl, struct args *args,
1.208     nicm     2228:     struct mouse_event *m)
1.1       nicm     2229: {
1.208     nicm     2230:        struct window_copy_mode_data    *data = wme->data;
1.213     nicm     2231:        struct window_copy_cmd_state     cs;
                   2232:        enum window_copy_cmd_action      action;
                   2233:        const char                      *command;
                   2234:        u_int                            i;
1.241     nicm     2235:        int                              ismotion = 0, keys;
1.155     nicm     2236:
                   2237:        if (args->argc == 0)
                   2238:                return;
                   2239:        command = args->argv[0];
1.21      nicm     2240:
1.202     nicm     2241:        if (m != NULL && m->valid && !MOUSE_WHEEL(m->b))
1.156     nicm     2242:                window_copy_move_mouse(m);
                   2243:
1.213     nicm     2244:        cs.wme = wme;
                   2245:        cs.args = args;
                   2246:        cs.m = m;
1.215     nicm     2247:
1.213     nicm     2248:        cs.c = c;
                   2249:        cs.s = s;
1.215     nicm     2250:        cs.wl = wl;
1.213     nicm     2251:
                   2252:        action = WINDOW_COPY_CMD_NOTHING;
                   2253:        for (i = 0; i < nitems(window_copy_cmd_table); i++) {
                   2254:                if (strcmp(window_copy_cmd_table[i].command, command) == 0) {
                   2255:                        if (args->argc - 1 < window_copy_cmd_table[i].minargs ||
                   2256:                            args->argc - 1 > window_copy_cmd_table[i].maxargs)
1.155     nicm     2257:                                break;
1.239     nicm     2258:                        ismotion = window_copy_cmd_table[i].ismotion;
1.213     nicm     2259:                        action = window_copy_cmd_table[i].f (&cs);
                   2260:                        break;
1.163     nicm     2261:                }
1.1       nicm     2262:        }
1.21      nicm     2263:
1.162     nicm     2264:        if (strncmp(command, "search-", 7) != 0 && data->searchmark != NULL) {
1.239     nicm     2265:                keys = options_get_number(wme->wp->window->options, "mode-keys");
                   2266:                if (keys != MODEKEY_VI || !ismotion) {
                   2267:                        window_copy_clear_marks(wme);
                   2268:                        data->searchx = data->searchy = -1;
                   2269:                } else if (data->searchthis != -1) {
                   2270:                        data->searchthis = -1;
                   2271:                        action = WINDOW_COPY_CMD_REDRAW;
                   2272:                }
1.213     nicm     2273:                if (action == WINDOW_COPY_CMD_NOTHING)
                   2274:                        action = WINDOW_COPY_CMD_REDRAW;
1.162     nicm     2275:        }
1.209     nicm     2276:        wme->prefix = 1;
                   2277:
1.213     nicm     2278:        if (action == WINDOW_COPY_CMD_CANCEL)
                   2279:                window_pane_reset_mode(wme->wp);
                   2280:        else if (action == WINDOW_COPY_CMD_REDRAW)
1.208     nicm     2281:                window_copy_redraw_screen(wme);
1.50      nicm     2282: }
                   2283:
1.157     nicm     2284: static void
1.208     nicm     2285: window_copy_scroll_to(struct window_mode_entry *wme, u_int px, u_int py)
1.21      nicm     2286: {
1.208     nicm     2287:        struct window_copy_mode_data    *data = wme->data;
1.54      nicm     2288:        struct grid                     *gd = data->backing->grid;
1.21      nicm     2289:        u_int                            offset, gap;
                   2290:
                   2291:        data->cx = px;
                   2292:
1.185     nicm     2293:        if (py >= gd->hsize - data->oy && py < gd->hsize - data->oy + gd->sy)
                   2294:                data->cy = py - (gd->hsize - data->oy);
                   2295:        else {
                   2296:                gap = gd->sy / 4;
                   2297:                if (py < gd->sy) {
                   2298:                        offset = 0;
                   2299:                        data->cy = py;
                   2300:                } else if (py > gd->hsize + gd->sy - gap) {
                   2301:                        offset = gd->hsize;
                   2302:                        data->cy = py - gd->hsize;
                   2303:                } else {
                   2304:                        offset = py + gap - gd->sy;
                   2305:                        data->cy = py - offset;
                   2306:                }
                   2307:                data->oy = gd->hsize - offset;
1.21      nicm     2308:        }
                   2309:
1.264     nicm     2310:        if (data->searchmark != NULL && !data->timeout)
                   2311:                window_copy_search_marks(wme, NULL, data->searchregex);
1.252     nicm     2312:        window_copy_update_selection(wme, 1, 0);
1.208     nicm     2313:        window_copy_redraw_screen(wme);
1.21      nicm     2314: }
                   2315:
1.157     nicm     2316: static int
1.118     nicm     2317: window_copy_search_compare(struct grid *gd, u_int px, u_int py,
                   2318:     struct grid *sgd, u_int spx, int cis)
1.21      nicm     2319: {
1.140     nicm     2320:        struct grid_cell         gc, sgc;
                   2321:        const struct utf8_data  *ud, *sud;
1.21      nicm     2322:
1.140     nicm     2323:        grid_get_cell(gd, px, py, &gc);
                   2324:        ud = &gc.data;
                   2325:        grid_get_cell(sgd, spx, 0, &sgc);
                   2326:        sud = &sgc.data;
1.35      nicm     2327:
1.140     nicm     2328:        if (ud->size != sud->size || ud->width != sud->width)
1.21      nicm     2329:                return (0);
1.97      nicm     2330:
1.140     nicm     2331:        if (cis && ud->size == 1)
                   2332:                return (tolower(ud->data[0]) == sud->data[0]);
1.97      nicm     2333:
1.140     nicm     2334:        return (memcmp(ud->data, sud->data, ud->size) == 0);
1.21      nicm     2335: }
                   2336:
1.157     nicm     2337: static int
1.21      nicm     2338: window_copy_search_lr(struct grid *gd,
1.97      nicm     2339:     struct grid *sgd, u_int *ppx, u_int py, u_int first, u_int last, int cis)
1.21      nicm     2340: {
1.243     nicm     2341:        u_int                    ax, bx, px, pywrap, endline;
                   2342:        int                      matched;
                   2343:        struct grid_line        *gl;
1.21      nicm     2344:
1.242     nicm     2345:        endline = gd->hsize + gd->sy - 1;
1.21      nicm     2346:        for (ax = first; ax < last; ax++) {
                   2347:                for (bx = 0; bx < sgd->sx; bx++) {
                   2348:                        px = ax + bx;
1.242     nicm     2349:                        pywrap = py;
                   2350:                        /* Wrap line. */
                   2351:                        while (px >= gd->sx && pywrap < endline) {
1.243     nicm     2352:                                gl = grid_get_line(gd, pywrap);
                   2353:                                if (~gl->flags & GRID_LINE_WRAPPED)
                   2354:                                        break;
1.242     nicm     2355:                                px -= gd->sx;
                   2356:                                pywrap++;
                   2357:                        }
                   2358:                        /* We have run off the end of the grid. */
                   2359:                        if (px >= gd->sx)
                   2360:                                break;
                   2361:                        matched = window_copy_search_compare(gd, px, pywrap,
                   2362:                            sgd, bx, cis);
1.97      nicm     2363:                        if (!matched)
1.21      nicm     2364:                                break;
                   2365:                }
                   2366:                if (bx == sgd->sx) {
                   2367:                        *ppx = ax;
                   2368:                        return (1);
                   2369:                }
                   2370:        }
                   2371:        return (0);
                   2372: }
                   2373:
1.157     nicm     2374: static int
1.21      nicm     2375: window_copy_search_rl(struct grid *gd,
1.97      nicm     2376:     struct grid *sgd, u_int *ppx, u_int py, u_int first, u_int last, int cis)
1.21      nicm     2377: {
1.243     nicm     2378:        u_int                    ax, bx, px, pywrap, endline;
                   2379:        int                      matched;
                   2380:        struct grid_line        *gl;
1.21      nicm     2381:
1.242     nicm     2382:        endline = gd->hsize + gd->sy - 1;
                   2383:        for (ax = last; ax > first; ax--) {
1.21      nicm     2384:                for (bx = 0; bx < sgd->sx; bx++) {
                   2385:                        px = ax - 1 + bx;
1.242     nicm     2386:                        pywrap = py;
                   2387:                        /* Wrap line. */
                   2388:                        while (px >= gd->sx && pywrap < endline) {
1.243     nicm     2389:                                gl = grid_get_line(gd, pywrap);
                   2390:                                if (~gl->flags & GRID_LINE_WRAPPED)
                   2391:                                        break;
1.242     nicm     2392:                                px -= gd->sx;
                   2393:                                pywrap++;
                   2394:                        }
                   2395:                        /* We have run off the end of the grid. */
                   2396:                        if (px >= gd->sx)
                   2397:                                break;
                   2398:                        matched = window_copy_search_compare(gd, px, pywrap,
                   2399:                            sgd, bx, cis);
1.97      nicm     2400:                        if (!matched)
1.21      nicm     2401:                                break;
                   2402:                }
                   2403:                if (bx == sgd->sx) {
                   2404:                        *ppx = ax - 1;
                   2405:                        return (1);
                   2406:                }
                   2407:        }
                   2408:        return (0);
                   2409: }
                   2410:
1.244     nicm     2411: static int
1.260     nicm     2412: window_copy_search_lr_regex(struct grid *gd, u_int *ppx, u_int *psx, u_int py,
                   2413:     u_int first, u_int last, regex_t *reg)
1.244     nicm     2414: {
1.260     nicm     2415:        int                     eflags = 0;
1.244     nicm     2416:        u_int                   endline, foundx, foundy, len, pywrap, size = 1;
1.260     nicm     2417:        char                   *buf;
1.244     nicm     2418:        regmatch_t              regmatch;
                   2419:        struct grid_line       *gl;
                   2420:
                   2421:        /*
                   2422:         * This can happen during search if the last match was the last
                   2423:         * character on a line.
                   2424:         */
                   2425:        if (first >= last)
                   2426:                return (0);
                   2427:
                   2428:        /* Set flags for regex search. */
                   2429:        if (first != 0)
                   2430:                eflags |= REG_NOTBOL;
                   2431:
                   2432:        /* Need to look at the entire string. */
                   2433:        buf = xmalloc(size);
                   2434:        buf[0] = '\0';
                   2435:        buf = window_copy_stringify(gd, py, first, gd->sx, buf, &size);
                   2436:        len = gd->sx - first;
                   2437:        endline = gd->hsize + gd->sy - 1;
                   2438:        pywrap = py;
                   2439:        while (buf != NULL && pywrap <= endline) {
                   2440:                gl = grid_get_line(gd, pywrap);
                   2441:                if (~gl->flags & GRID_LINE_WRAPPED)
                   2442:                        break;
                   2443:                pywrap++;
                   2444:                buf = window_copy_stringify(gd, pywrap, 0, gd->sx, buf, &size);
                   2445:                len += gd->sx;
                   2446:        }
                   2447:
1.287   ! nicm     2448:        if (regexec(reg, buf, 1, &regmatch, eflags) == 0 &&
        !          2449:            regmatch.rm_so != regmatch.rm_eo) {
1.244     nicm     2450:                foundx = first;
                   2451:                foundy = py;
                   2452:                window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
                   2453:                    buf + regmatch.rm_so);
                   2454:                if (foundy == py && foundx < last) {
                   2455:                        *ppx = foundx;
                   2456:                        len -= foundx - first;
                   2457:                        window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
                   2458:                            buf + regmatch.rm_eo);
                   2459:                        *psx = foundx;
                   2460:                        while (foundy > py) {
                   2461:                                *psx += gd->sx;
                   2462:                                foundy--;
                   2463:                        }
                   2464:                        *psx -= *ppx;
                   2465:                        free(buf);
                   2466:                        return (1);
                   2467:                }
                   2468:        }
                   2469:
                   2470:        free(buf);
                   2471:        *ppx = 0;
                   2472:        *psx = 0;
                   2473:        return (0);
                   2474: }
                   2475:
                   2476: static int
1.260     nicm     2477: window_copy_search_rl_regex(struct grid *gd, u_int *ppx, u_int *psx, u_int py,
                   2478:     u_int first, u_int last, regex_t *reg)
1.244     nicm     2479: {
1.260     nicm     2480:        int                     eflags = 0;
                   2481:        u_int                   endline, len, pywrap, size = 1;
                   2482:        char                   *buf;
1.244     nicm     2483:        struct grid_line       *gl;
                   2484:
                   2485:        /* Set flags for regex search. */
                   2486:        if (first != 0)
                   2487:                eflags |= REG_NOTBOL;
                   2488:
                   2489:        /* Need to look at the entire string. */
                   2490:        buf = xmalloc(size);
                   2491:        buf[0] = '\0';
                   2492:        buf = window_copy_stringify(gd, py, first, gd->sx, buf, &size);
                   2493:        len = gd->sx - first;
                   2494:        endline = gd->hsize + gd->sy - 1;
                   2495:        pywrap = py;
                   2496:        while (buf != NULL && (pywrap <= endline)) {
                   2497:                gl = grid_get_line(gd, pywrap);
                   2498:                if (~gl->flags & GRID_LINE_WRAPPED)
                   2499:                        break;
                   2500:                pywrap++;
                   2501:                buf = window_copy_stringify(gd, pywrap, 0, gd->sx, buf, &size);
                   2502:                len += gd->sx;
                   2503:        }
                   2504:
                   2505:        if (window_copy_last_regex(gd, py, first, last, len, ppx, psx, buf,
1.260     nicm     2506:            reg, eflags))
1.244     nicm     2507:        {
                   2508:                free(buf);
                   2509:                return (1);
                   2510:        }
                   2511:
                   2512:        free(buf);
                   2513:        *ppx = 0;
                   2514:        *psx = 0;
                   2515:        return (0);
                   2516: }
                   2517:
1.260     nicm     2518: static const char *
                   2519: window_copy_cellstring(const struct grid_line *gl, u_int px, size_t *size)
                   2520: {
                   2521:        struct grid_cell_entry  *gce;
                   2522:
                   2523:        if (px >= gl->cellsize) {
                   2524:                *size = 1;
                   2525:                return (" ");
                   2526:        }
                   2527:
                   2528:        gce = &gl->celldata[px];
                   2529:        if (~gce->flags & GRID_FLAG_EXTENDED) {
                   2530:                *size = 1;
                   2531:                return (&gce->data.data);
                   2532:        }
                   2533:
                   2534:        *size = gl->extddata[gce->offset].data.size;
                   2535:        return (gl->extddata[gce->offset].data.data);
                   2536: }
                   2537:
1.244     nicm     2538: /* Find last match in given range. */
                   2539: static int
                   2540: window_copy_last_regex(struct grid *gd, u_int py, u_int first, u_int last,
                   2541:     u_int len, u_int *ppx, u_int *psx, const char *buf, const regex_t *preg,
                   2542:     int eflags)
                   2543: {
                   2544:        u_int           foundx, foundy, oldx, px = 0, savepx, savesx = 0;
                   2545:        regmatch_t      regmatch;
                   2546:
                   2547:        foundx = first;
                   2548:        foundy = py;
                   2549:        oldx = first;
                   2550:        while (regexec(preg, buf + px, 1, &regmatch, eflags) == 0) {
1.287   ! nicm     2551:                if (regmatch.rm_so == regmatch.rm_eo)
        !          2552:                        break;
1.244     nicm     2553:                window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
                   2554:                    buf + px + regmatch.rm_so);
                   2555:                if (foundy > py || foundx >= last)
                   2556:                        break;
                   2557:                len -= foundx - oldx;
                   2558:                savepx = foundx;
                   2559:                window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
                   2560:                    buf + px + regmatch.rm_eo);
                   2561:                if (foundy > py || foundx >= last) {
                   2562:                        *ppx = savepx;
                   2563:                        *psx = foundx;
                   2564:                        while (foundy > py) {
                   2565:                                *psx += gd->sx;
                   2566:                                foundy--;
                   2567:                        }
                   2568:                        *psx -= *ppx;
                   2569:                        return (1);
                   2570:                } else {
                   2571:                        savesx = foundx - savepx;
                   2572:                        len -= savesx;
                   2573:                        oldx = foundx;
                   2574:                }
                   2575:                px += regmatch.rm_eo;
                   2576:        }
                   2577:
                   2578:        if (savesx > 0) {
                   2579:                *ppx = savepx;
                   2580:                *psx = savesx;
                   2581:                return (1);
                   2582:        } else {
                   2583:                *ppx = 0;
                   2584:                *psx = 0;
                   2585:                return (0);
                   2586:        }
                   2587: }
                   2588:
                   2589: /* Stringify line and append to input buffer. Caller frees. */
                   2590: static char *
                   2591: window_copy_stringify(struct grid *gd, u_int py, u_int first, u_int last,
                   2592:     char *buf, u_int *size)
                   2593: {
1.260     nicm     2594:        u_int                    ax, bx, newsize = *size;
                   2595:        const struct grid_line  *gl;
                   2596:        const char              *d;
                   2597:        size_t                   bufsize = 1024, dlen;
                   2598:
                   2599:        while (bufsize < newsize)
                   2600:                bufsize *= 2;
                   2601:        buf = xrealloc(buf, bufsize);
1.244     nicm     2602:
1.260     nicm     2603:        gl = grid_peek_line(gd, py);
1.244     nicm     2604:        bx = *size - 1;
                   2605:        for (ax = first; ax < last; ax++) {
1.260     nicm     2606:                d = window_copy_cellstring(gl, ax, &dlen);
                   2607:                newsize += dlen;
                   2608:                while (bufsize < newsize) {
                   2609:                        bufsize *= 2;
                   2610:                        buf = xrealloc(buf, bufsize);
                   2611:                }
                   2612:                if (dlen == 1)
                   2613:                        buf[bx++] = *d;
                   2614:                else {
                   2615:                        memcpy(buf + bx, d, dlen);
                   2616:                        bx += dlen;
                   2617:                }
1.244     nicm     2618:        }
1.260     nicm     2619:        buf[newsize - 1] = '\0';
1.244     nicm     2620:
                   2621:        *size = newsize;
                   2622:        return (buf);
                   2623: }
                   2624:
                   2625: /* Map start of C string containing UTF-8 data to grid cell position. */
                   2626: static void
                   2627: window_copy_cstrtocellpos(struct grid *gd, u_int ncells, u_int *ppx, u_int *ppy,
                   2628:     const char *str)
                   2629: {
1.260     nicm     2630:        u_int                    cell, ccell, px, pywrap, pos, len;
                   2631:        int                      match;
                   2632:        const struct grid_line  *gl;
                   2633:        const char              *d;
                   2634:        size_t                   dlen;
                   2635:        struct {
                   2636:                const char      *d;
                   2637:                size_t           dlen;
                   2638:        } *cells;
1.244     nicm     2639:
1.260     nicm     2640:        /* Populate the array of cell data. */
1.244     nicm     2641:        cells = xreallocarray(NULL, ncells, sizeof cells[0]);
                   2642:        cell = 0;
                   2643:        px = *ppx;
                   2644:        pywrap = *ppy;
1.260     nicm     2645:        gl = grid_peek_line(gd, pywrap);
1.244     nicm     2646:        while (cell < ncells) {
1.260     nicm     2647:                cells[cell].d = window_copy_cellstring(gl, px,
                   2648:                    &cells[cell].dlen);
1.244     nicm     2649:                cell++;
1.261     nicm     2650:                px++;
                   2651:                if (px == gd->sx) {
                   2652:                        px = 0;
1.244     nicm     2653:                        pywrap++;
1.260     nicm     2654:                        gl = grid_peek_line(gd, pywrap);
                   2655:                }
1.244     nicm     2656:        }
                   2657:
                   2658:        /* Locate starting cell. */
                   2659:        cell = 0;
1.260     nicm     2660:        len = strlen(str);
1.244     nicm     2661:        while (cell < ncells) {
                   2662:                ccell = cell;
1.260     nicm     2663:                pos = 0;
1.244     nicm     2664:                match = 1;
                   2665:                while (ccell < ncells) {
1.260     nicm     2666:                        if (str[pos] == '\0') {
1.244     nicm     2667:                                match = 0;
                   2668:                                break;
                   2669:                        }
1.260     nicm     2670:                        d = cells[ccell].d;
                   2671:                        dlen = cells[ccell].dlen;
                   2672:                        if (dlen == 1) {
                   2673:                                if (str[pos] != *d) {
                   2674:                                        match = 0;
                   2675:                                        break;
                   2676:                                }
                   2677:                                pos++;
                   2678:                        } else {
                   2679:                                if (dlen > len - pos)
                   2680:                                        dlen = len - pos;
                   2681:                                if (memcmp(str + pos, d, dlen) != 0) {
1.244     nicm     2682:                                        match = 0;
                   2683:                                        break;
                   2684:                                }
1.260     nicm     2685:                                pos += dlen;
1.244     nicm     2686:                        }
                   2687:                        ccell++;
                   2688:                }
                   2689:                if (match)
                   2690:                        break;
                   2691:                cell++;
                   2692:        }
                   2693:
                   2694:        /* If not found this will be one past the end. */
                   2695:        px = *ppx + cell;
                   2696:        pywrap = *ppy;
                   2697:        while (px >= gd->sx) {
                   2698:                px -= gd->sx;
                   2699:                pywrap++;
                   2700:        }
                   2701:
                   2702:        *ppx = px;
                   2703:        *ppy = pywrap;
                   2704:
                   2705:        /* Free cell data. */
                   2706:        free(cells);
                   2707: }
                   2708:
1.157     nicm     2709: static void
1.230     nicm     2710: window_copy_move_left(struct screen *s, u_int *fx, u_int *fy, int wrapflag)
1.21      nicm     2711: {
1.150     nicm     2712:        if (*fx == 0) { /* left */
1.230     nicm     2713:                if (*fy == 0) { /* top */
                   2714:                        if (wrapflag) {
                   2715:                                *fx = screen_size_x(s) - 1;
1.242     nicm     2716:                                *fy = screen_hsize(s) + screen_size_y(s) - 1;
1.230     nicm     2717:                        }
1.150     nicm     2718:                        return;
1.230     nicm     2719:                }
1.150     nicm     2720:                *fx = screen_size_x(s) - 1;
                   2721:                *fy = *fy - 1;
                   2722:        } else
                   2723:                *fx = *fx - 1;
                   2724: }
1.21      nicm     2725:
1.157     nicm     2726: static void
1.230     nicm     2727: window_copy_move_right(struct screen *s, u_int *fx, u_int *fy, int wrapflag)
1.150     nicm     2728: {
                   2729:        if (*fx == screen_size_x(s) - 1) { /* right */
1.242     nicm     2730:                if (*fy == screen_hsize(s) + screen_size_y(s) - 1) { /* bottom */
1.230     nicm     2731:                        if (wrapflag) {
                   2732:                                *fx = 0;
                   2733:                                *fy = 0;
                   2734:                        }
1.21      nicm     2735:                        return;
1.230     nicm     2736:                }
1.150     nicm     2737:                *fx = 0;
                   2738:                *fy = *fy + 1;
1.21      nicm     2739:        } else
1.150     nicm     2740:                *fx = *fx + 1;
                   2741: }
1.21      nicm     2742:
1.157     nicm     2743: static int
1.150     nicm     2744: window_copy_is_lowercase(const char *ptr)
                   2745: {
                   2746:        while (*ptr != '\0') {
                   2747:                if (*ptr != tolower((u_char)*ptr))
                   2748:                        return (0);
                   2749:                ++ptr;
1.97      nicm     2750:        }
1.150     nicm     2751:        return (1);
                   2752: }
1.97      nicm     2753:
1.150     nicm     2754: /*
                   2755:  * Search for text stored in sgd starting from position fx,fy up to endline. If
                   2756:  * found, jump to it. If cis then ignore case. The direction is 0 for searching
                   2757:  * up, down otherwise. If wrap then go to begin/end of grid and try again if
                   2758:  * not found.
                   2759:  */
1.163     nicm     2760: static int
1.208     nicm     2761: window_copy_search_jump(struct window_mode_entry *wme, struct grid *gd,
1.150     nicm     2762:     struct grid *sgd, u_int fx, u_int fy, u_int endline, int cis, int wrap,
1.244     nicm     2763:     int direction, int regex)
1.150     nicm     2764: {
1.260     nicm     2765:        u_int    i, px, sx, ssize = 1;
                   2766:        int      found = 0, cflags = REG_EXTENDED;
                   2767:        char    *sbuf;
                   2768:        regex_t  reg;
                   2769:
                   2770:        if (regex) {
                   2771:                sbuf = xmalloc(ssize);
                   2772:                sbuf[0] = '\0';
                   2773:                sbuf = window_copy_stringify(sgd, 0, 0, sgd->sx, sbuf, &ssize);
                   2774:                if (cis)
                   2775:                        cflags |= REG_ICASE;
                   2776:                if (regcomp(&reg, sbuf, cflags) != 0) {
                   2777:                        free(sbuf);
                   2778:                        return (0);
                   2779:                }
                   2780:        }
1.150     nicm     2781:
                   2782:        if (direction) {
                   2783:                for (i = fy; i <= endline; i++) {
1.260     nicm     2784:                        if (regex) {
                   2785:                                found = window_copy_search_lr_regex(gd,
                   2786:                                    &px, &sx, i, fx, gd->sx, &reg);
                   2787:                        } else {
1.244     nicm     2788:                                found = window_copy_search_lr(gd, sgd,
                   2789:                                    &px, i, fx, gd->sx, cis);
1.260     nicm     2790:                        }
1.150     nicm     2791:                        if (found)
                   2792:                                break;
                   2793:                        fx = 0;
                   2794:                }
                   2795:        } else {
                   2796:                for (i = fy + 1; endline < i; i--) {
1.260     nicm     2797:                        if (regex) {
                   2798:                                found = window_copy_search_rl_regex(gd,
                   2799:                                    &px, &sx, i - 1, 0, fx + 1, &reg);
                   2800:                        } else {
1.244     nicm     2801:                                found = window_copy_search_rl(gd, sgd,
                   2802:                                    &px, i - 1, 0, fx + 1, cis);
1.260     nicm     2803:                        }
1.150     nicm     2804:                        if (found) {
                   2805:                                i--;
                   2806:                                break;
                   2807:                        }
1.242     nicm     2808:                        fx = gd->sx - 1;
1.21      nicm     2809:                }
                   2810:        }
1.260     nicm     2811:        if (regex) {
                   2812:                free(sbuf);
                   2813:                regfree(&reg);
                   2814:        }
1.150     nicm     2815:
1.163     nicm     2816:        if (found) {
1.208     nicm     2817:                window_copy_scroll_to(wme, px, i);
1.163     nicm     2818:                return (1);
                   2819:        }
                   2820:        if (wrap) {
1.208     nicm     2821:                return (window_copy_search_jump(wme, gd, sgd,
1.163     nicm     2822:                    direction ? 0 : gd->sx - 1,
1.150     nicm     2823:                    direction ? 0 : gd->hsize + gd->sy - 1, fy, cis, 0,
1.244     nicm     2824:                    direction, regex));
1.21      nicm     2825:        }
1.163     nicm     2826:        return (0);
1.21      nicm     2827: }
                   2828:
1.232     nicm     2829: /*
                   2830:  * Search in for text searchstr. If direction is 0 then search up, otherwise
                   2831:  * down.
                   2832:  */
1.163     nicm     2833: static int
1.244     nicm     2834: window_copy_search(struct window_mode_entry *wme, int direction, int regex)
1.21      nicm     2835: {
1.208     nicm     2836:        struct window_pane              *wp = wme->wp;
                   2837:        struct window_copy_mode_data    *data = wme->data;
1.54      nicm     2838:        struct screen                   *s = data->backing, ss;
1.21      nicm     2839:        struct screen_write_ctx          ctx;
1.150     nicm     2840:        struct grid                     *gd = s->grid;
1.262     nicm     2841:        const char                      *str = data->searchstr;
1.150     nicm     2842:        u_int                            fx, fy, endline;
1.163     nicm     2843:        int                              wrapflag, cis, found;
1.21      nicm     2844:
1.262     nicm     2845:        if (regex && str[strcspn(str, "^$*+()?[].\\")] == '\0')
                   2846:                regex = 0;
                   2847:
1.263     nicm     2848:        if (data->timeout)
                   2849:                return (0);
                   2850:
1.174     nicm     2851:        free(wp->searchstr);
1.262     nicm     2852:        wp->searchstr = xstrdup(str);
1.259     nicm     2853:        wp->searchregex = regex;
1.174     nicm     2854:
1.150     nicm     2855:        fx = data->cx;
                   2856:        fy = screen_hsize(data->backing) - data->oy + data->cy;
1.21      nicm     2857:
1.262     nicm     2858:        screen_init(&ss, screen_write_strlen("%s", str), 1, 0);
1.283     nicm     2859:        screen_write_start(&ctx, &ss);
1.262     nicm     2860:        screen_write_nputs(&ctx, -1, &grid_default_cell, "%s", str);
1.21      nicm     2861:        screen_write_stop(&ctx);
                   2862:
1.150     nicm     2863:        wrapflag = options_get_number(wp->window->options, "wrap-search");
1.262     nicm     2864:        cis = window_copy_is_lowercase(str);
1.21      nicm     2865:
1.230     nicm     2866:        if (direction) {
                   2867:                window_copy_move_right(s, &fx, &fy, wrapflag);
1.150     nicm     2868:                endline = gd->hsize + gd->sy - 1;
1.230     nicm     2869:        } else {
                   2870:                window_copy_move_left(s, &fx, &fy, wrapflag);
1.150     nicm     2871:                endline = 0;
1.230     nicm     2872:        }
1.244     nicm     2873:
1.208     nicm     2874:        found = window_copy_search_jump(wme, gd, ss.grid, fx, fy, endline, cis,
1.244     nicm     2875:            wrapflag, direction, regex);
1.162     nicm     2876:
1.244     nicm     2877:        if (window_copy_search_marks(wme, &ss, regex))
1.208     nicm     2878:                window_copy_redraw_screen(wme);
1.21      nicm     2879:
1.150     nicm     2880:        screen_free(&ss);
1.163     nicm     2881:        return (found);
1.150     nicm     2882: }
1.97      nicm     2883:
1.284     nicm     2884: static uint64_t
                   2885: window_copy_get_time(void)
                   2886: {
                   2887:        struct timeval  tv;
                   2888:
                   2889:        gettimeofday(&tv, NULL);
                   2890:        return ((tv.tv_sec * 1000ULL) + (tv.tv_usec / 1000ULL));
                   2891: }
                   2892:
1.162     nicm     2893: static int
1.244     nicm     2894: window_copy_search_marks(struct window_mode_entry *wme, struct screen *ssp,
                   2895:     int regex)
1.162     nicm     2896: {
1.208     nicm     2897:        struct window_copy_mode_data    *data = wme->data;
1.162     nicm     2898:        struct screen                   *s = data->backing, ss;
                   2899:        struct screen_write_ctx          ctx;
                   2900:        struct grid                     *gd = s->grid;
1.264     nicm     2901:        const struct grid_line          *gl;
1.284     nicm     2902:        int                              found, cis, which = -1, stopped = 0;
1.260     nicm     2903:        int                              cflags = REG_EXTENDED;
1.282     nicm     2904:        u_int                            px, py, i, b, nfound = 0, width;
1.284     nicm     2905:        u_int                            ssize = 1, start, end;
1.260     nicm     2906:        char                            *sbuf;
                   2907:        regex_t                          reg;
1.284     nicm     2908:        uint64_t                         stop = 0, tstart, t;
1.162     nicm     2909:
                   2910:        if (ssp == NULL) {
                   2911:                width = screen_write_strlen("%s", data->searchstr);
                   2912:                screen_init(&ss, width, 1, 0);
1.283     nicm     2913:                screen_write_start(&ctx, &ss);
1.162     nicm     2914:                screen_write_nputs(&ctx, -1, &grid_default_cell, "%s",
                   2915:                    data->searchstr);
                   2916:                screen_write_stop(&ctx);
                   2917:                ssp = &ss;
                   2918:        } else
                   2919:                width = screen_size_x(ssp);
                   2920:
                   2921:        cis = window_copy_is_lowercase(data->searchstr);
                   2922:
1.260     nicm     2923:        if (regex) {
                   2924:                sbuf = xmalloc(ssize);
                   2925:                sbuf[0] = '\0';
                   2926:                sbuf = window_copy_stringify(ssp->grid, 0, 0, ssp->grid->sx,
                   2927:                    sbuf, &ssize);
                   2928:                if (cis)
                   2929:                        cflags |= REG_ICASE;
                   2930:                if (regcomp(&reg, sbuf, cflags) != 0) {
                   2931:                        free(sbuf);
                   2932:                        return (0);
                   2933:                }
                   2934:        }
1.284     nicm     2935:        tstart = window_copy_get_time();
                   2936:
                   2937:        start = 0;
                   2938:        end = gd->hsize + gd->sy;
                   2939:        stop = window_copy_get_time() + WINDOW_COPY_SEARCH_ALL_TIMEOUT;
                   2940:
                   2941: again:
                   2942:        free(data->searchmark);
                   2943:        data->searchmark = xcalloc(gd->hsize + gd->sy, gd->sx);
                   2944:        data->searchgen = 1;
                   2945:
                   2946:        for (py = start; py < end; py++) {
1.162     nicm     2947:                px = 0;
                   2948:                for (;;) {
1.244     nicm     2949:                        if (regex) {
                   2950:                                found = window_copy_search_lr_regex(gd,
1.260     nicm     2951:                                    &px, &width, py, px, gd->sx, &reg);
1.244     nicm     2952:                                if (!found)
                   2953:                                        break;
1.260     nicm     2954:                        } else {
1.244     nicm     2955:                                found = window_copy_search_lr(gd, ssp->grid,
1.260     nicm     2956:                                    &px, py, px, gd->sx, cis);
1.244     nicm     2957:                                if (!found)
                   2958:                                        break;
                   2959:                        }
1.170     nicm     2960:
1.162     nicm     2961:                        nfound++;
1.260     nicm     2962:                        if (px == data->cx &&
                   2963:                            py == gd->hsize + data->cy - data->oy)
1.170     nicm     2964:                                which = nfound;
1.162     nicm     2965:
                   2966:                        b = (py * gd->sx) + px;
1.282     nicm     2967:                        for (i = b; i < b + width; i++)
                   2968:                                data->searchmark[i] = data->searchgen;
                   2969:                        if (data->searchgen == UCHAR_MAX)
                   2970:                                data->searchgen = 1;
                   2971:                        else
                   2972:                                data->searchgen++;
1.162     nicm     2973:
                   2974:                        px++;
                   2975:                }
1.263     nicm     2976:
1.284     nicm     2977:                t = window_copy_get_time();
1.263     nicm     2978:                if (t - tstart > WINDOW_COPY_SEARCH_TIMEOUT) {
                   2979:                        data->timeout = 1;
                   2980:                        break;
                   2981:                }
1.284     nicm     2982:                if (stop != 0 && t > stop) {
                   2983:                        stopped = 1;
                   2984:                        break;
                   2985:                }
1.162     nicm     2986:        }
1.263     nicm     2987:        if (data->timeout) {
                   2988:                window_copy_clear_marks(wme);
1.284     nicm     2989:                goto out;
                   2990:        }
                   2991:
                   2992:        if (stopped && stop != 0) {
                   2993:                /* Try again but just the visible context. */
                   2994:                for (start = gd->hsize - data->oy; start > 0; start--) {
                   2995:                        gl = grid_peek_line(gd, start - 1);
                   2996:                        if (~gl->flags & GRID_LINE_WRAPPED)
                   2997:                                break;
                   2998:                }
                   2999:                end = gd->hsize - data->oy + gd->sy;
                   3000:                stop = 0;
                   3001:                goto again;
1.263     nicm     3002:        }
1.162     nicm     3003:
1.284     nicm     3004:        if (stopped) {
1.170     nicm     3005:                data->searchthis = -1;
1.284     nicm     3006:                if (nfound > 1000)
                   3007:                        data->searchcount = 1000;
                   3008:                else if (nfound > 100)
                   3009:                        data->searchcount = 100;
                   3010:                else if (nfound > 10)
                   3011:                        data->searchcount = 10;
                   3012:                else
                   3013:                        data->searchcount = -1;
                   3014:                data->searchmore = 1;
                   3015:        } else {
                   3016:                if (which != -1)
                   3017:                        data->searchthis = 1 + nfound - which;
                   3018:                else
                   3019:                        data->searchthis = -1;
                   3020:                data->searchcount = nfound;
                   3021:                data->searchmore = 0;
                   3022:        }
1.170     nicm     3023:
1.284     nicm     3024: out:
1.162     nicm     3025:        if (ssp == &ss)
                   3026:                screen_free(&ss);
1.284     nicm     3027:        if (regex) {
                   3028:                free(sbuf);
                   3029:                regfree(&reg);
                   3030:        }
1.263     nicm     3031:        return (1);
1.162     nicm     3032: }
                   3033:
1.157     nicm     3034: static void
1.208     nicm     3035: window_copy_clear_marks(struct window_mode_entry *wme)
1.163     nicm     3036: {
1.208     nicm     3037:        struct window_copy_mode_data    *data = wme->data;
1.163     nicm     3038:
                   3039:        free(data->searchmark);
                   3040:        data->searchmark = NULL;
                   3041: }
                   3042:
                   3043: static int
1.244     nicm     3044: window_copy_search_up(struct window_mode_entry *wme, int regex)
1.150     nicm     3045: {
1.244     nicm     3046:        return (window_copy_search(wme, 0, regex));
1.150     nicm     3047: }
1.21      nicm     3048:
1.163     nicm     3049: static int
1.244     nicm     3050: window_copy_search_down(struct window_mode_entry *wme, int regex)
1.150     nicm     3051: {
1.244     nicm     3052:        return (window_copy_search(wme, 1, regex));
1.21      nicm     3053: }
                   3054:
1.157     nicm     3055: static void
1.208     nicm     3056: window_copy_goto_line(struct window_mode_entry *wme, const char *linestr)
1.21      nicm     3057: {
1.208     nicm     3058:        struct window_copy_mode_data    *data = wme->data;
1.21      nicm     3059:        const char                      *errstr;
1.199     nicm     3060:        int                              lineno;
1.21      nicm     3061:
1.199     nicm     3062:        lineno = strtonum(linestr, -1, INT_MAX, &errstr);
1.21      nicm     3063:        if (errstr != NULL)
                   3064:                return;
1.199     nicm     3065:        if (lineno < 0 || (u_int)lineno > screen_hsize(data->backing))
                   3066:                lineno = screen_hsize(data->backing);
1.35      nicm     3067:
1.21      nicm     3068:        data->oy = lineno;
1.252     nicm     3069:        window_copy_update_selection(wme, 1, 0);
1.208     nicm     3070:        window_copy_redraw_screen(wme);
1.21      nicm     3071: }
                   3072:
1.157     nicm     3073: static void
1.282     nicm     3074: window_copy_match_start_end(struct window_copy_mode_data *data, u_int at,
                   3075:     u_int *start, u_int *end)
                   3076: {
                   3077:        struct grid     *gd = data->backing->grid;
                   3078:        u_int            last = (gd->hsize + gd->sy) * gd->sx - 1;
                   3079:        u_char           mark = data->searchmark[at];
                   3080:
                   3081:        *start = *end = at;
                   3082:        while (*start != 0 && data->searchmark[*start] == mark)
                   3083:                (*start)--;
                   3084:        if (data->searchmark[*start] != mark)
                   3085:                (*start)++;
                   3086:        while (*end != last && data->searchmark[*end] == mark)
                   3087:                (*end)++;
                   3088:        if (data->searchmark[*end] != mark)
                   3089:                (*end)--;
                   3090: }
                   3091:
                   3092: static char *
                   3093: window_copy_match_at_cursor(struct window_copy_mode_data *data)
                   3094: {
                   3095:        struct grid     *gd = data->backing->grid;
                   3096:        struct grid_cell gc;
                   3097:        u_int            at, start, end, cy, px, py;
                   3098:        u_int            sx = screen_size_x(data->backing);
                   3099:        char            *buf = NULL;
                   3100:        size_t           len = 0;
                   3101:
                   3102:        if (data->searchmark == NULL)
                   3103:                return (NULL);
                   3104:
                   3105:        cy = screen_hsize(data->backing) - data->oy + data->cy;
                   3106:        at = (cy * sx) + data->cx;
                   3107:        if (data->searchmark[at] == 0)
                   3108:                return (NULL);
                   3109:        window_copy_match_start_end(data, at, &start, &end);
                   3110:
                   3111:        /*
                   3112:         * Cells will not be set in the marked array unless they are valid text
                   3113:         * and wrapping will be taken care of, so we can just copy.
                   3114:         */
                   3115:        for (at = start; at <= end; at++) {
                   3116:                py = at / sx;
                   3117:                px = at % (py * sx);
                   3118:
                   3119:                grid_get_cell(gd, px, py, &gc);
                   3120:                buf = xrealloc(buf, len + gc.data.size + 1);
                   3121:                memcpy(buf + len, gc.data.data, gc.data.size);
                   3122:                len += gc.data.size;
                   3123:        }
                   3124:        if (len != 0)
                   3125:                buf[len] = '\0';
                   3126:        return (buf);
                   3127: }
                   3128:
                   3129: static void
                   3130: window_copy_update_style(struct window_mode_entry *wme, u_int fx, u_int fy,
                   3131:     struct grid_cell *gc, const struct grid_cell *mgc,
                   3132:     const struct grid_cell *cgc)
                   3133: {
                   3134:        struct window_copy_mode_data    *data = wme->data;
1.284     nicm     3135:        u_int                            mark, start, end, cy, cursor, current;
1.282     nicm     3136:        u_int                            sx = screen_size_x(data->backing);
                   3137:
                   3138:        if (data->searchmark == NULL)
                   3139:                return;
1.284     nicm     3140:
                   3141:        current = (fy * sx) + fx;
                   3142:
                   3143:        mark = data->searchmark[current];
1.282     nicm     3144:        if (mark == 0)
                   3145:                return;
                   3146:
                   3147:        cy = screen_hsize(data->backing) - data->oy + data->cy;
1.284     nicm     3148:        cursor = (cy * sx) + data->cx;
                   3149:        if (data->searchmark[cursor] == mark) {
                   3150:                window_copy_match_start_end(data, cursor, &start, &end);
                   3151:                if (current >= start && current <= end) {
1.282     nicm     3152:                        gc->attr = cgc->attr;
                   3153:                        gc->fg = cgc->fg;
                   3154:                        gc->bg = cgc->bg;
1.284     nicm     3155:                        return;
1.282     nicm     3156:                }
                   3157:        }
                   3158:
                   3159:        gc->attr = mgc->attr;
                   3160:        gc->fg = mgc->fg;
                   3161:        gc->bg = mgc->bg;
                   3162: }
                   3163:
                   3164: static void
                   3165: window_copy_write_one(struct window_mode_entry *wme,
                   3166:     struct screen_write_ctx *ctx, u_int py, u_int fy, u_int nx,
                   3167:     const struct grid_cell *mgc, const struct grid_cell *cgc)
                   3168: {
                   3169:        struct window_copy_mode_data    *data = wme->data;
                   3170:        struct grid                     *gd = data->backing->grid;
                   3171:        struct grid_cell                 gc;
                   3172:        u_int                            fx;
                   3173:
                   3174:        screen_write_cursormove(ctx, 0, py, 0);
                   3175:        for (fx = 0; fx < nx; fx++) {
                   3176:                grid_get_cell(gd, fx, fy, &gc);
                   3177:                if (fx + gc.data.width <= nx) {
                   3178:                        window_copy_update_style(wme, fx, fy, &gc, mgc, cgc);
                   3179:                        screen_write_cell(ctx, &gc);
                   3180:                }
                   3181:        }
                   3182: }
                   3183:
                   3184: static void
1.208     nicm     3185: window_copy_write_line(struct window_mode_entry *wme,
                   3186:     struct screen_write_ctx *ctx, u_int py)
1.1       nicm     3187: {
1.208     nicm     3188:        struct window_pane              *wp = wme->wp;
                   3189:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     3190:        struct screen                   *s = &data->screen;
1.136     nicm     3191:        struct options                  *oo = wp->window->options;
1.282     nicm     3192:        struct grid_cell                 gc, mgc, cgc;
1.100     nicm     3193:        char                             hdr[512];
1.162     nicm     3194:        size_t                           size = 0;
1.270     nicm     3195:        u_int                            hsize = screen_hsize(data->backing);
1.21      nicm     3196:
1.281     nicm     3197:        style_apply(&gc, oo, "mode-style", NULL);
1.164     nicm     3198:        gc.flags |= GRID_FLAG_NOPALETTE;
1.282     nicm     3199:        style_apply(&mgc, oo, "copy-mode-match-style", NULL);
                   3200:        mgc.flags |= GRID_FLAG_NOPALETTE;
                   3201:        style_apply(&cgc, oo, "copy-mode-current-match-style", NULL);
                   3202:        cgc.flags |= GRID_FLAG_NOPALETTE;
1.1       nicm     3203:
1.249     nicm     3204:        if (py == 0 && s->rupper < s->rlower && !data->hide_position) {
1.170     nicm     3205:                if (data->searchmark == NULL) {
1.263     nicm     3206:                        if (data->timeout) {
                   3207:                                size = xsnprintf(hdr, sizeof hdr,
1.270     nicm     3208:                                    "(timed out) [%u/%u]", data->oy, hsize);
1.263     nicm     3209:                        } else {
                   3210:                                size = xsnprintf(hdr, sizeof hdr,
1.270     nicm     3211:                                    "[%u/%u]", data->oy, hsize);
1.263     nicm     3212:                        }
1.170     nicm     3213:                } else {
1.284     nicm     3214:                        if (data->searchcount == -1) {
                   3215:                                size = xsnprintf(hdr, sizeof hdr,
                   3216:                                    "[%u/%u]", data->oy, hsize);
                   3217:                        } else if (data->searchthis == -1) {
1.170     nicm     3218:                                size = xsnprintf(hdr, sizeof hdr,
1.284     nicm     3219:                                    "(%d%s results) [%u/%u]", data->searchcount,
                   3220:                                    data->searchmore ? "+" : "", data->oy, hsize);
1.170     nicm     3221:                        } else {
                   3222:                                size = xsnprintf(hdr, sizeof hdr,
1.284     nicm     3223:                                    "(%d/%d results) [%u/%u]", data->searchthis,
1.270     nicm     3224:                                    data->searchcount, data->oy, hsize);
1.170     nicm     3225:                        }
                   3226:                }
1.62      nicm     3227:                if (size > screen_size_x(s))
                   3228:                        size = screen_size_x(s);
1.212     nicm     3229:                screen_write_cursormove(ctx, screen_size_x(s) - size, 0, 0);
1.1       nicm     3230:                screen_write_puts(ctx, &gc, "%s", hdr);
                   3231:        } else
                   3232:                size = 0;
                   3233:
1.105     nicm     3234:        if (size < screen_size_x(s)) {
1.282     nicm     3235:                window_copy_write_one(wme, ctx, py, hsize - data->oy + py,
                   3236:                    screen_size_x(s) - size, &mgc, &cgc);
1.105     nicm     3237:        }
1.18      nicm     3238:
                   3239:        if (py == data->cy && data->cx == screen_size_x(s)) {
1.212     nicm     3240:                screen_write_cursormove(ctx, screen_size_x(s) - 1, py, 0);
1.282     nicm     3241:                screen_write_putc(ctx, &grid_default_cell, '$');
1.18      nicm     3242:        }
1.1       nicm     3243: }
                   3244:
1.157     nicm     3245: static void
1.208     nicm     3246: window_copy_write_lines(struct window_mode_entry *wme,
                   3247:     struct screen_write_ctx *ctx, u_int py, u_int ny)
1.1       nicm     3248: {
                   3249:        u_int   yy;
                   3250:
                   3251:        for (yy = py; yy < py + ny; yy++)
1.208     nicm     3252:                window_copy_write_line(wme, ctx, py);
1.121     nicm     3253: }
                   3254:
1.157     nicm     3255: static void
1.208     nicm     3256: window_copy_redraw_selection(struct window_mode_entry *wme, u_int old_y)
1.121     nicm     3257: {
1.208     nicm     3258:        struct window_copy_mode_data    *data = wme->data;
1.247     nicm     3259:        struct grid                     *gd = data->backing->grid;
1.121     nicm     3260:        u_int                            new_y, start, end;
                   3261:
                   3262:        new_y = data->cy;
                   3263:        if (old_y <= new_y) {
                   3264:                start = old_y;
                   3265:                end = new_y;
                   3266:        } else {
                   3267:                start = new_y;
                   3268:                end = old_y;
                   3269:        }
1.247     nicm     3270:
                   3271:        /*
                   3272:         * In word selection mode the first word on the line below the cursor
                   3273:         * might be selected, so add this line to the redraw area.
                   3274:         */
                   3275:        if (data->selflag == SEL_WORD) {
                   3276:                /* Last grid line in data coordinates. */
                   3277:                if (end < gd->sy + data->oy - 1)
                   3278:                        end++;
                   3279:        }
1.208     nicm     3280:        window_copy_redraw_lines(wme, start, end - start + 1);
1.1       nicm     3281: }
                   3282:
1.157     nicm     3283: static void
1.208     nicm     3284: window_copy_redraw_lines(struct window_mode_entry *wme, u_int py, u_int ny)
1.1       nicm     3285: {
1.208     nicm     3286:        struct window_pane              *wp = wme->wp;
                   3287:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     3288:        struct screen_write_ctx          ctx;
                   3289:        u_int                            i;
                   3290:
1.283     nicm     3291:        screen_write_start_pane(&ctx, wp, NULL);
1.1       nicm     3292:        for (i = py; i < py + ny; i++)
1.208     nicm     3293:                window_copy_write_line(wme, &ctx, i);
1.212     nicm     3294:        screen_write_cursormove(&ctx, data->cx, data->cy, 0);
1.1       nicm     3295:        screen_write_stop(&ctx);
                   3296: }
                   3297:
1.157     nicm     3298: static void
1.208     nicm     3299: window_copy_redraw_screen(struct window_mode_entry *wme)
1.1       nicm     3300: {
1.208     nicm     3301:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     3302:
1.208     nicm     3303:        window_copy_redraw_lines(wme, 0, screen_size_y(&data->screen));
1.1       nicm     3304: }
                   3305:
1.157     nicm     3306: static void
1.252     nicm     3307: window_copy_synchronize_cursor_end(struct window_mode_entry *wme, int begin,
                   3308:     int no_reset)
1.161     nicm     3309: {
1.208     nicm     3310:        struct window_copy_mode_data    *data = wme->data;
1.161     nicm     3311:        u_int                            xx, yy;
                   3312:
                   3313:        yy = screen_hsize(data->backing) + data->cy - data->oy;
1.247     nicm     3314:        switch (data->selflag) {
                   3315:        case SEL_WORD:
                   3316:                xx = data->cx;
1.252     nicm     3317:                if (no_reset)
1.247     nicm     3318:                        break;
1.252     nicm     3319:                begin = 0;
1.247     nicm     3320:                if (data->dy > yy || (data->dy == yy && data->dx > xx)) {
                   3321:                        /* Right to left selection. */
                   3322:                        window_copy_cursor_previous_word_pos(wme, data->ws, 0,
                   3323:                            &xx, &yy);
                   3324:                        begin = 1;
                   3325:
                   3326:                        /* Reset the end. */
                   3327:                        data->endselx = data->endselrx;
                   3328:                        data->endsely = data->endselry;
                   3329:                } else {
                   3330:                        /* Left to right selection. */
                   3331:                        if (xx >= window_copy_find_length(wme, yy) ||
                   3332:                            !window_copy_in_set(wme, xx + 1, yy, data->ws))
                   3333:                                window_copy_cursor_next_word_end_pos(wme,
                   3334:                                    data->ws, &xx, &yy);
                   3335:
                   3336:                        /* Reset the start. */
                   3337:                        data->selx = data->selrx;
                   3338:                        data->sely = data->selry;
                   3339:                }
                   3340:                break;
                   3341:        case SEL_LINE:
1.252     nicm     3342:                if (no_reset) {
                   3343:                        xx = data->cx;
                   3344:                        break;
                   3345:                }
1.251     nicm     3346:                begin = 0;
1.247     nicm     3347:                if (data->dy > yy) {
                   3348:                        /* Right to left selection. */
                   3349:                        xx = 0;
                   3350:                        begin = 1;
                   3351:
                   3352:                        /* Reset the end. */
                   3353:                        data->endselx = data->endselrx;
                   3354:                        data->endsely = data->endselry;
                   3355:                } else {
                   3356:                        /* Left to right selection. */
                   3357:                        xx = window_copy_find_length(wme, yy);
                   3358:
                   3359:                        /* Reset the start. */
                   3360:                        data->selx = data->selrx;
                   3361:                        data->sely = data->selry;
                   3362:                }
                   3363:                break;
                   3364:        case SEL_CHAR:
                   3365:                xx = data->cx;
                   3366:                break;
                   3367:        }
                   3368:        if (begin) {
                   3369:                data->selx = xx;
                   3370:                data->sely = yy;
                   3371:        } else {
                   3372:                data->endselx = xx;
                   3373:                data->endsely = yy;
                   3374:        }
                   3375: }
                   3376:
                   3377: static void
1.252     nicm     3378: window_copy_synchronize_cursor(struct window_mode_entry *wme, int no_reset)
1.247     nicm     3379: {
                   3380:        struct window_copy_mode_data    *data = wme->data;
1.161     nicm     3381:
                   3382:        switch (data->cursordrag) {
                   3383:        case CURSORDRAG_ENDSEL:
1.252     nicm     3384:                window_copy_synchronize_cursor_end(wme, 0, no_reset);
1.161     nicm     3385:                break;
                   3386:        case CURSORDRAG_SEL:
1.252     nicm     3387:                window_copy_synchronize_cursor_end(wme, 1, no_reset);
1.161     nicm     3388:                break;
                   3389:        case CURSORDRAG_NONE:
                   3390:                break;
                   3391:        }
                   3392: }
                   3393:
                   3394: static void
1.208     nicm     3395: window_copy_update_cursor(struct window_mode_entry *wme, u_int cx, u_int cy)
1.1       nicm     3396: {
1.208     nicm     3397:        struct window_pane              *wp = wme->wp;
                   3398:        struct window_copy_mode_data    *data = wme->data;
1.18      nicm     3399:        struct screen                   *s = &data->screen;
1.1       nicm     3400:        struct screen_write_ctx          ctx;
1.18      nicm     3401:        u_int                            old_cx, old_cy;
1.1       nicm     3402:
1.18      nicm     3403:        old_cx = data->cx; old_cy = data->cy;
                   3404:        data->cx = cx; data->cy = cy;
                   3405:        if (old_cx == screen_size_x(s))
1.208     nicm     3406:                window_copy_redraw_lines(wme, old_cy, 1);
1.18      nicm     3407:        if (data->cx == screen_size_x(s))
1.208     nicm     3408:                window_copy_redraw_lines(wme, data->cy, 1);
1.18      nicm     3409:        else {
1.283     nicm     3410:                screen_write_start_pane(&ctx, wp, NULL);
1.212     nicm     3411:                screen_write_cursormove(&ctx, data->cx, data->cy, 0);
1.18      nicm     3412:                screen_write_stop(&ctx);
                   3413:        }
1.1       nicm     3414: }
                   3415:
1.157     nicm     3416: static void
1.208     nicm     3417: window_copy_start_selection(struct window_mode_entry *wme)
1.1       nicm     3418: {
1.208     nicm     3419:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     3420:
1.18      nicm     3421:        data->selx = data->cx;
1.54      nicm     3422:        data->sely = screen_hsize(data->backing) + data->cy - data->oy;
1.1       nicm     3423:
1.161     nicm     3424:        data->endselx = data->selx;
                   3425:        data->endsely = data->sely;
                   3426:
                   3427:        data->cursordrag = CURSORDRAG_ENDSEL;
                   3428:
1.252     nicm     3429:        window_copy_set_selection(wme, 1, 0);
1.1       nicm     3430: }
                   3431:
1.157     nicm     3432: static int
1.208     nicm     3433: window_copy_adjust_selection(struct window_mode_entry *wme, u_int *selx,
                   3434:     u_int *sely)
1.161     nicm     3435: {
1.208     nicm     3436:        struct window_copy_mode_data    *data = wme->data;
1.161     nicm     3437:        struct screen                   *s = &data->screen;
                   3438:        u_int                            sx, sy, ty;
                   3439:        int                              relpos;
                   3440:
                   3441:        sx = *selx;
                   3442:        sy = *sely;
                   3443:
                   3444:        ty = screen_hsize(data->backing) - data->oy;
                   3445:        if (sy < ty) {
                   3446:                relpos = WINDOW_COPY_REL_POS_ABOVE;
                   3447:                if (!data->rectflag)
                   3448:                        sx = 0;
                   3449:                sy = 0;
                   3450:        } else if (sy > ty + screen_size_y(s) - 1) {
                   3451:                relpos = WINDOW_COPY_REL_POS_BELOW;
                   3452:                if (!data->rectflag)
                   3453:                        sx = screen_size_x(s) - 1;
                   3454:                sy = screen_size_y(s) - 1;
                   3455:        } else {
                   3456:                relpos = WINDOW_COPY_REL_POS_ON_SCREEN;
                   3457:                sy -= ty;
                   3458:        }
                   3459:
                   3460:        *selx = sx;
1.176     nicm     3461:        *sely = sy;
1.161     nicm     3462:        return (relpos);
                   3463: }
                   3464:
                   3465: static int
1.252     nicm     3466: window_copy_update_selection(struct window_mode_entry *wme, int may_redraw,
                   3467:     int no_reset)
1.1       nicm     3468: {
1.208     nicm     3469:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     3470:        struct screen                   *s = &data->screen;
1.192     nicm     3471:
                   3472:        if (s->sel == NULL && data->lineflag == LINE_SEL_NONE)
                   3473:                return (0);
1.252     nicm     3474:        return (window_copy_set_selection(wme, may_redraw, no_reset));
1.192     nicm     3475: }
                   3476:
                   3477: static int
1.252     nicm     3478: window_copy_set_selection(struct window_mode_entry *wme, int may_redraw,
                   3479:     int no_reset)
1.192     nicm     3480: {
1.208     nicm     3481:        struct window_pane              *wp = wme->wp;
                   3482:        struct window_copy_mode_data    *data = wme->data;
1.192     nicm     3483:        struct screen                   *s = &data->screen;
1.136     nicm     3484:        struct options                  *oo = wp->window->options;
1.1       nicm     3485:        struct grid_cell                 gc;
1.161     nicm     3486:        u_int                            sx, sy, cy, endsx, endsy;
                   3487:        int                              startrelpos, endrelpos;
1.1       nicm     3488:
1.252     nicm     3489:        window_copy_synchronize_cursor(wme, no_reset);
1.1       nicm     3490:
                   3491:        /* Adjust the selection. */
                   3492:        sx = data->selx;
                   3493:        sy = data->sely;
1.208     nicm     3494:        startrelpos = window_copy_adjust_selection(wme, &sx, &sy);
1.161     nicm     3495:
                   3496:        /* Adjust the end of selection. */
                   3497:        endsx = data->endselx;
                   3498:        endsy = data->endsely;
1.208     nicm     3499:        endrelpos = window_copy_adjust_selection(wme, &endsx, &endsy);
1.161     nicm     3500:
                   3501:        /* Selection is outside of the current screen */
                   3502:        if (startrelpos == endrelpos &&
                   3503:            startrelpos != WINDOW_COPY_REL_POS_ON_SCREEN) {
                   3504:                screen_hide_selection(s);
                   3505:                return (0);
                   3506:        }
1.1       nicm     3507:
1.161     nicm     3508:        /* Set colours and selection. */
1.281     nicm     3509:        style_apply(&gc, oo, "mode-style", NULL);
1.164     nicm     3510:        gc.flags |= GRID_FLAG_NOPALETTE;
1.192     nicm     3511:        screen_set_selection(s, sx, sy, endsx, endsy, data->rectflag,
                   3512:            data->modekeys, &gc);
1.42      nicm     3513:
1.96      nicm     3514:        if (data->rectflag && may_redraw) {
1.42      nicm     3515:                /*
                   3516:                 * Can't rely on the caller to redraw the right lines for
                   3517:                 * rectangle selection - find the highest line and the number
                   3518:                 * of lines, and redraw just past that in both directions
                   3519:                 */
                   3520:                cy = data->cy;
1.182     nicm     3521:                if (data->cursordrag == CURSORDRAG_ENDSEL) {
                   3522:                        if (sy < cy)
1.208     nicm     3523:                                window_copy_redraw_lines(wme, sy, cy - sy + 1);
1.182     nicm     3524:                        else
1.208     nicm     3525:                                window_copy_redraw_lines(wme, cy, sy - cy + 1);
1.182     nicm     3526:                } else {
1.208     nicm     3527:                        if (endsy < cy) {
                   3528:                                window_copy_redraw_lines(wme, endsy,
                   3529:                                    cy - endsy + 1);
                   3530:                        } else {
                   3531:                                window_copy_redraw_lines(wme, cy,
                   3532:                                    endsy - cy + 1);
                   3533:                        }
1.182     nicm     3534:                }
1.42      nicm     3535:        }
                   3536:
1.1       nicm     3537:        return (1);
                   3538: }
                   3539:
1.157     nicm     3540: static void *
1.208     nicm     3541: window_copy_get_selection(struct window_mode_entry *wme, size_t *len)
1.1       nicm     3542: {
1.208     nicm     3543:        struct window_pane              *wp = wme->wp;
                   3544:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     3545:        struct screen                   *s = &data->screen;
                   3546:        char                            *buf;
1.42      nicm     3547:        size_t                           off;
1.111     nicm     3548:        u_int                            i, xx, yy, sx, sy, ex, ey, ey_last;
1.188     nicm     3549:        u_int                            firstsx, lastex, restex, restsx, selx;
1.69      nicm     3550:        int                              keys;
1.1       nicm     3551:
1.282     nicm     3552:        if (data->screen.sel == NULL && data->lineflag == LINE_SEL_NONE) {
                   3553:                buf = window_copy_match_at_cursor(data);
                   3554:                if (buf != NULL)
                   3555:                        *len = strlen(buf);
                   3556:                return (buf);
                   3557:        }
1.1       nicm     3558:
                   3559:        buf = xmalloc(1);
                   3560:        off = 0;
                   3561:
                   3562:        *buf = '\0';
                   3563:
                   3564:        /*
                   3565:         * The selection extends from selx,sely to (adjusted) cx,cy on
                   3566:         * the base screen.
                   3567:         */
                   3568:
                   3569:        /* Find start and end. */
1.161     nicm     3570:        xx = data->endselx;
                   3571:        yy = data->endsely;
1.2       nicm     3572:        if (yy < data->sely || (yy == data->sely && xx < data->selx)) {
1.1       nicm     3573:                sx = xx; sy = yy;
                   3574:                ex = data->selx; ey = data->sely;
                   3575:        } else {
                   3576:                sx = data->selx; sy = data->sely;
                   3577:                ex = xx; ey = yy;
                   3578:        }
                   3579:
                   3580:        /* Trim ex to end of line. */
1.208     nicm     3581:        ey_last = window_copy_find_length(wme, ey);
1.111     nicm     3582:        if (ex > ey_last)
                   3583:                ex = ey_last;
1.1       nicm     3584:
1.42      nicm     3585:        /*
                   3586:         * Deal with rectangle-copy if necessary; four situations: start of
                   3587:         * first line (firstsx), end of last line (lastex), start (restsx) and
                   3588:         * end (restex) of all other lines.
                   3589:         */
                   3590:        xx = screen_size_x(s);
1.69      nicm     3591:
                   3592:        /*
                   3593:         * Behave according to mode-keys. If it is emacs, copy like emacs,
                   3594:         * keeping the top-left-most character, and dropping the
                   3595:         * bottom-right-most, regardless of copy direction. If it is vi, also
                   3596:         * keep bottom-right-most character.
                   3597:         */
1.136     nicm     3598:        keys = options_get_number(wp->window->options, "mode-keys");
1.42      nicm     3599:        if (data->rectflag) {
                   3600:                /*
                   3601:                 * Need to ignore the column with the cursor in it, which for
                   3602:                 * rectangular copy means knowing which side the cursor is on.
                   3603:                 */
1.188     nicm     3604:                if (data->cursordrag == CURSORDRAG_ENDSEL)
                   3605:                        selx = data->selx;
                   3606:                else
                   3607:                        selx = data->endselx;
                   3608:                if (selx < data->cx) {
1.42      nicm     3609:                        /* Selection start is on the left. */
1.69      nicm     3610:                        if (keys == MODEKEY_EMACS) {
                   3611:                                lastex = data->cx;
                   3612:                                restex = data->cx;
                   3613:                        }
                   3614:                        else {
                   3615:                                lastex = data->cx + 1;
                   3616:                                restex = data->cx + 1;
                   3617:                        }
1.188     nicm     3618:                        firstsx = selx;
                   3619:                        restsx = selx;
1.42      nicm     3620:                } else {
                   3621:                        /* Cursor is on the left. */
1.188     nicm     3622:                        lastex = selx + 1;
                   3623:                        restex = selx + 1;
1.64      nicm     3624:                        firstsx = data->cx;
                   3625:                        restsx = data->cx;
1.42      nicm     3626:                }
                   3627:        } else {
1.69      nicm     3628:                if (keys == MODEKEY_EMACS)
                   3629:                        lastex = ex;
1.74      nicm     3630:                else
1.69      nicm     3631:                        lastex = ex + 1;
1.42      nicm     3632:                restex = xx;
                   3633:                firstsx = sx;
                   3634:                restsx = 0;
                   3635:        }
                   3636:
1.1       nicm     3637:        /* Copy the lines. */
1.110     nicm     3638:        for (i = sy; i <= ey; i++) {
1.208     nicm     3639:                window_copy_copy_line(wme, &buf, &off, i,
1.110     nicm     3640:                    (i == sy ? firstsx : restsx),
                   3641:                    (i == ey ? lastex : restex));
1.1       nicm     3642:        }
                   3643:
1.26      nicm     3644:        /* Don't bother if no data. */
                   3645:        if (off == 0) {
1.81      nicm     3646:                free(buf);
1.89      nicm     3647:                return (NULL);
1.26      nicm     3648:        }
1.111     nicm     3649:        if (keys == MODEKEY_EMACS || lastex <= ey_last)
                   3650:                off -= 1; /* remove final \n (unless at end in vi mode) */
                   3651:        *len = off;
1.89      nicm     3652:        return (buf);
                   3653: }
                   3654:
1.157     nicm     3655: static void
1.215     nicm     3656: window_copy_copy_buffer(struct window_mode_entry *wme, const char *prefix,
                   3657:     void *buf, size_t len)
1.89      nicm     3658: {
1.208     nicm     3659:        struct window_pane      *wp = wme->wp;
                   3660:        struct screen_write_ctx  ctx;
1.72      nicm     3661:
1.178     nicm     3662:        if (options_get_number(global_options, "set-clipboard") != 0) {
1.283     nicm     3663:                screen_write_start_pane(&ctx, wp, NULL);
1.91      nicm     3664:                screen_write_setselection(&ctx, buf, len);
                   3665:                screen_write_stop(&ctx);
1.179     nicm     3666:                notify_pane("pane-set-clipboard", wp);
1.91      nicm     3667:        }
1.1       nicm     3668:
1.215     nicm     3669:        paste_add(prefix, buf, len);
1.89      nicm     3670: }
                   3671:
1.157     nicm     3672: static void
1.208     nicm     3673: window_copy_copy_pipe(struct window_mode_entry *wme, struct session *s,
1.253     nicm     3674:     const char *prefix, const char *cmd)
1.89      nicm     3675: {
1.215     nicm     3676:        void            *buf;
                   3677:        size_t           len;
                   3678:        struct job      *job;
1.89      nicm     3679:
1.208     nicm     3680:        buf = window_copy_get_selection(wme, &len);
1.89      nicm     3681:        if (buf == NULL)
                   3682:                return;
                   3683:
1.277     nicm     3684:        if (cmd == NULL || *cmd == '\0')
                   3685:                cmd = options_get_string(global_options, "copy-command");
                   3686:        if (cmd != NULL && *cmd != '\0') {
                   3687:                job = job_run(cmd, s, NULL, NULL, NULL, NULL, NULL, JOB_NOWAIT,
                   3688:                    -1, -1);
                   3689:                bufferevent_write(job_get_event(job), buf, len);
                   3690:        }
1.215     nicm     3691:        window_copy_copy_buffer(wme, prefix, buf, len);
1.89      nicm     3692: }
                   3693:
1.157     nicm     3694: static void
1.215     nicm     3695: window_copy_copy_selection(struct window_mode_entry *wme, const char *prefix)
1.89      nicm     3696: {
1.203     nicm     3697:        char    *buf;
1.114     nicm     3698:        size_t   len;
1.89      nicm     3699:
1.208     nicm     3700:        buf = window_copy_get_selection(wme, &len);
1.203     nicm     3701:        if (buf != NULL)
1.215     nicm     3702:                window_copy_copy_buffer(wme, prefix, buf, len);
1.103     nicm     3703: }
                   3704:
1.157     nicm     3705: static void
1.208     nicm     3706: window_copy_append_selection(struct window_mode_entry *wme)
1.103     nicm     3707: {
1.208     nicm     3708:        struct window_pane              *wp = wme->wp;
1.108     nicm     3709:        char                            *buf;
                   3710:        struct paste_buffer             *pb;
1.227     nicm     3711:        const char                      *bufdata, *bufname = NULL;
1.132     nicm     3712:        size_t                           len, bufsize;
1.108     nicm     3713:        struct screen_write_ctx          ctx;
1.103     nicm     3714:
1.208     nicm     3715:        buf = window_copy_get_selection(wme, &len);
1.103     nicm     3716:        if (buf == NULL)
                   3717:                return;
                   3718:
1.178     nicm     3719:        if (options_get_number(global_options, "set-clipboard") != 0) {
1.283     nicm     3720:                screen_write_start_pane(&ctx, wp, NULL);
1.103     nicm     3721:                screen_write_setselection(&ctx, buf, len);
                   3722:                screen_write_stop(&ctx);
1.179     nicm     3723:                notify_pane("pane-set-clipboard", wp);
1.103     nicm     3724:        }
                   3725:
1.203     nicm     3726:        pb = paste_get_top(&bufname);
1.103     nicm     3727:        if (pb != NULL) {
1.132     nicm     3728:                bufdata = paste_buffer_data(pb, &bufsize);
                   3729:                buf = xrealloc(buf, len + bufsize);
                   3730:                memmove(buf + bufsize, buf, len);
                   3731:                memcpy(buf, bufdata, bufsize);
                   3732:                len += bufsize;
1.103     nicm     3733:        }
1.108     nicm     3734:        if (paste_set(buf, len, bufname, NULL) != 0)
1.103     nicm     3735:                free(buf);
1.1       nicm     3736: }
                   3737:
1.157     nicm     3738: static void
1.208     nicm     3739: window_copy_copy_line(struct window_mode_entry *wme, char **buf, size_t *off,
                   3740:     u_int sy, u_int sx, u_int ex)
1.1       nicm     3741: {
1.208     nicm     3742:        struct window_copy_mode_data    *data = wme->data;
1.54      nicm     3743:        struct grid                     *gd = data->backing->grid;
1.140     nicm     3744:        struct grid_cell                 gc;
1.54      nicm     3745:        struct grid_line                *gl;
1.86      nicm     3746:        struct utf8_data                 ud;
1.54      nicm     3747:        u_int                            i, xx, wrapped = 0;
1.115     nicm     3748:        const char                      *s;
1.1       nicm     3749:
                   3750:        if (sx > ex)
                   3751:                return;
                   3752:
1.16      nicm     3753:        /*
                   3754:         * Work out if the line was wrapped at the screen edge and all of it is
                   3755:         * on screen.
                   3756:         */
1.190     nicm     3757:        gl = grid_get_line(gd, sy);
1.35      nicm     3758:        if (gl->flags & GRID_LINE_WRAPPED && gl->cellsize <= gd->sx)
1.16      nicm     3759:                wrapped = 1;
                   3760:
                   3761:        /* If the line was wrapped, don't strip spaces (use the full length). */
                   3762:        if (wrapped)
                   3763:                xx = gl->cellsize;
                   3764:        else
1.208     nicm     3765:                xx = window_copy_find_length(wme, sy);
1.1       nicm     3766:        if (ex > xx)
                   3767:                ex = xx;
                   3768:        if (sx > xx)
                   3769:                sx = xx;
                   3770:
                   3771:        if (sx < ex) {
                   3772:                for (i = sx; i < ex; i++) {
1.140     nicm     3773:                        grid_get_cell(gd, i, sy, &gc);
                   3774:                        if (gc.flags & GRID_FLAG_PADDING)
1.1       nicm     3775:                                continue;
1.140     nicm     3776:                        utf8_copy(&ud, &gc.data);
                   3777:                        if (ud.size == 1 && (gc.attr & GRID_ATTR_CHARSET)) {
1.115     nicm     3778:                                s = tty_acs_get(NULL, ud.data[0]);
                   3779:                                if (s != NULL && strlen(s) <= sizeof ud.data) {
                   3780:                                        ud.size = strlen(s);
1.117     nicm     3781:                                        memcpy(ud.data, s, ud.size);
1.115     nicm     3782:                                }
                   3783:                        }
1.86      nicm     3784:
1.116     nicm     3785:                        *buf = xrealloc(*buf, (*off) + ud.size);
1.86      nicm     3786:                        memcpy(*buf + *off, ud.data, ud.size);
                   3787:                        *off += ud.size;
1.1       nicm     3788:                }
                   3789:        }
                   3790:
1.16      nicm     3791:        /* Only add a newline if the line wasn't wrapped. */
1.44      nicm     3792:        if (!wrapped || ex != xx) {
1.116     nicm     3793:                *buf = xrealloc(*buf, (*off) + 1);
1.16      nicm     3794:                (*buf)[(*off)++] = '\n';
                   3795:        }
1.1       nicm     3796: }
                   3797:
1.157     nicm     3798: static void
1.208     nicm     3799: window_copy_clear_selection(struct window_mode_entry *wme)
1.47      nicm     3800: {
1.208     nicm     3801:        struct window_copy_mode_data   *data = wme->data;
1.47      nicm     3802:        u_int                           px, py;
                   3803:
                   3804:        screen_clear_selection(&data->screen);
                   3805:
1.161     nicm     3806:        data->cursordrag = CURSORDRAG_NONE;
1.197     nicm     3807:        data->lineflag = LINE_SEL_NONE;
1.250     nicm     3808:        data->selflag = SEL_CHAR;
1.161     nicm     3809:
1.54      nicm     3810:        py = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm     3811:        px = window_copy_find_length(wme, py);
1.47      nicm     3812:        if (data->cx > px)
1.208     nicm     3813:                window_copy_update_cursor(wme, px, data->cy);
1.47      nicm     3814: }
                   3815:
1.157     nicm     3816: static int
1.208     nicm     3817: window_copy_in_set(struct window_mode_entry *wme, u_int px, u_int py,
                   3818:     const char *set)
1.1       nicm     3819: {
1.208     nicm     3820:        struct window_copy_mode_data    *data = wme->data;
1.140     nicm     3821:        struct grid_cell                 gc;
                   3822:
                   3823:        grid_get_cell(data->backing->grid, px, py, &gc);
1.204     nicm     3824:        if (gc.flags & GRID_FLAG_PADDING)
                   3825:                return (0);
1.224     nicm     3826:        return (utf8_cstrhas(set, &gc.data));
1.1       nicm     3827: }
                   3828:
1.157     nicm     3829: static u_int
1.208     nicm     3830: window_copy_find_length(struct window_mode_entry *wme, u_int py)
1.1       nicm     3831: {
1.208     nicm     3832:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     3833:
1.224     nicm     3834:        return (grid_line_length(data->backing->grid, py));
1.1       nicm     3835: }
                   3836:
1.157     nicm     3837: static void
1.208     nicm     3838: window_copy_cursor_start_of_line(struct window_mode_entry *wme)
1.5       nicm     3839: {
1.208     nicm     3840:        struct window_copy_mode_data    *data = wme->data;
1.58      nicm     3841:        struct screen                   *back_s = data->backing;
                   3842:        struct grid                     *gd = back_s->grid;
                   3843:        u_int                            py;
                   3844:
1.192     nicm     3845:        if (data->cx == 0 && data->lineflag == LINE_SEL_NONE) {
1.58      nicm     3846:                py = screen_hsize(back_s) + data->cy - data->oy;
1.118     nicm     3847:                while (py > 0 &&
1.190     nicm     3848:                    grid_get_line(gd, py - 1)->flags & GRID_LINE_WRAPPED) {
1.208     nicm     3849:                        window_copy_cursor_up(wme, 0);
1.58      nicm     3850:                        py = screen_hsize(back_s) + data->cy - data->oy;
                   3851:                }
                   3852:        }
1.208     nicm     3853:        window_copy_update_cursor(wme, 0, data->cy);
1.252     nicm     3854:        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     3855:                window_copy_redraw_lines(wme, data->cy, 1);
1.6       nicm     3856: }
                   3857:
1.157     nicm     3858: static void
1.208     nicm     3859: window_copy_cursor_back_to_indentation(struct window_mode_entry *wme)
1.6       nicm     3860: {
1.208     nicm     3861:        struct window_copy_mode_data    *data = wme->data;
1.6       nicm     3862:        u_int                            px, py, xx;
1.140     nicm     3863:        struct grid_cell                 gc;
1.6       nicm     3864:
                   3865:        px = 0;
1.54      nicm     3866:        py = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm     3867:        xx = window_copy_find_length(wme, py);
1.6       nicm     3868:
                   3869:        while (px < xx) {
1.140     nicm     3870:                grid_get_cell(data->backing->grid, px, py, &gc);
                   3871:                if (gc.data.size != 1 || *gc.data.data != ' ')
1.6       nicm     3872:                        break;
                   3873:                px++;
                   3874:        }
                   3875:
1.208     nicm     3876:        window_copy_update_cursor(wme, px, data->cy);
1.252     nicm     3877:        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     3878:                window_copy_redraw_lines(wme, data->cy, 1);
1.5       nicm     3879: }
                   3880:
1.157     nicm     3881: static void
1.208     nicm     3882: window_copy_cursor_end_of_line(struct window_mode_entry *wme)
1.5       nicm     3883: {
1.208     nicm     3884:        struct window_copy_mode_data    *data = wme->data;
1.54      nicm     3885:        struct screen                   *back_s = data->backing;
                   3886:        struct grid                     *gd = back_s->grid;
1.190     nicm     3887:        struct grid_line                *gl;
1.5       nicm     3888:        u_int                            px, py;
                   3889:
1.54      nicm     3890:        py = screen_hsize(back_s) + data->cy - data->oy;
1.208     nicm     3891:        px = window_copy_find_length(wme, py);
1.5       nicm     3892:
1.192     nicm     3893:        if (data->cx == px && data->lineflag == LINE_SEL_NONE) {
                   3894:                if (data->screen.sel != NULL && data->rectflag)
1.54      nicm     3895:                        px = screen_size_x(back_s);
1.190     nicm     3896:                gl = grid_get_line(gd, py);
                   3897:                if (gl->flags & GRID_LINE_WRAPPED) {
                   3898:                        while (py < gd->sy + gd->hsize) {
                   3899:                                gl = grid_get_line(gd, py);
                   3900:                                if (~gl->flags & GRID_LINE_WRAPPED)
                   3901:                                        break;
1.208     nicm     3902:                                window_copy_cursor_down(wme, 0);
1.190     nicm     3903:                                py = screen_hsize(back_s) + data->cy - data->oy;
1.49      nicm     3904:                        }
1.208     nicm     3905:                        px = window_copy_find_length(wme, py);
1.49      nicm     3906:                }
                   3907:        }
1.208     nicm     3908:        window_copy_update_cursor(wme, px, data->cy);
1.49      nicm     3909:
1.252     nicm     3910:        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     3911:                window_copy_redraw_lines(wme, data->cy, 1);
1.95      nicm     3912: }
                   3913:
1.157     nicm     3914: static void
1.208     nicm     3915: window_copy_other_end(struct window_mode_entry *wme)
1.95      nicm     3916: {
1.208     nicm     3917:        struct window_copy_mode_data    *data = wme->data;
1.95      nicm     3918:        struct screen                   *s = &data->screen;
1.161     nicm     3919:        u_int                            selx, sely, cy, yy, hsize;
1.95      nicm     3920:
1.192     nicm     3921:        if (s->sel == NULL && data->lineflag == LINE_SEL_NONE)
1.95      nicm     3922:                return;
                   3923:
1.192     nicm     3924:        if (data->lineflag == LINE_SEL_LEFT_RIGHT)
                   3925:                data->lineflag = LINE_SEL_RIGHT_LEFT;
                   3926:        else if (data->lineflag == LINE_SEL_RIGHT_LEFT)
                   3927:                data->lineflag = LINE_SEL_LEFT_RIGHT;
1.118     nicm     3928:
1.161     nicm     3929:        switch (data->cursordrag) {
                   3930:                case CURSORDRAG_NONE:
                   3931:                case CURSORDRAG_SEL:
                   3932:                        data->cursordrag = CURSORDRAG_ENDSEL;
                   3933:                        break;
                   3934:                case CURSORDRAG_ENDSEL:
                   3935:                        data->cursordrag = CURSORDRAG_SEL;
                   3936:                        break;
                   3937:        }
                   3938:
                   3939:        selx = data->endselx;
                   3940:        sely = data->endsely;
                   3941:        if (data->cursordrag == CURSORDRAG_SEL) {
                   3942:                selx = data->selx;
                   3943:                sely = data->sely;
                   3944:        }
                   3945:
1.95      nicm     3946:        cy = data->cy;
                   3947:        yy = screen_hsize(data->backing) + data->cy - data->oy;
                   3948:
                   3949:        data->cx = selx;
                   3950:
1.122     nicm     3951:        hsize = screen_hsize(data->backing);
1.161     nicm     3952:        if (sely < hsize - data->oy) { /* above */
1.122     nicm     3953:                data->oy = hsize - sely;
1.95      nicm     3954:                data->cy = 0;
1.161     nicm     3955:        } else if (sely > hsize - data->oy + screen_size_y(s)) { /* below */
1.122     nicm     3956:                data->oy = hsize - sely + screen_size_y(s) - 1;
1.95      nicm     3957:                data->cy = screen_size_y(s) - 1;
                   3958:        } else
                   3959:                data->cy = cy + sely - yy;
                   3960:
1.252     nicm     3961:        window_copy_update_selection(wme, 1, 1);
1.208     nicm     3962:        window_copy_redraw_screen(wme);
1.5       nicm     3963: }
                   3964:
1.157     nicm     3965: static void
1.208     nicm     3966: window_copy_cursor_left(struct window_mode_entry *wme)
1.1       nicm     3967: {
1.208     nicm     3968:        struct window_copy_mode_data    *data = wme->data;
1.168     nicm     3969:        u_int                            py, cx;
                   3970:        struct grid_cell                 gc;
1.1       nicm     3971:
1.145     nicm     3972:        py = screen_hsize(data->backing) + data->cy - data->oy;
1.168     nicm     3973:        cx = data->cx;
                   3974:        while (cx > 0) {
                   3975:                grid_get_cell(data->backing->grid, cx, py, &gc);
                   3976:                if (~gc.flags & GRID_FLAG_PADDING)
                   3977:                        break;
                   3978:                cx--;
                   3979:        }
                   3980:        if (cx == 0 && py > 0) {
1.208     nicm     3981:                window_copy_cursor_up(wme, 0);
                   3982:                window_copy_cursor_end_of_line(wme);
1.168     nicm     3983:        } else if (cx > 0) {
1.208     nicm     3984:                window_copy_update_cursor(wme, cx - 1, data->cy);
1.252     nicm     3985:                if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     3986:                        window_copy_redraw_lines(wme, data->cy, 1);
1.1       nicm     3987:        }
                   3988: }
                   3989:
1.157     nicm     3990: static void
1.208     nicm     3991: window_copy_cursor_right(struct window_mode_entry *wme)
1.1       nicm     3992: {
1.208     nicm     3993:        struct window_copy_mode_data    *data = wme->data;
1.168     nicm     3994:        u_int                            px, py, yy, cx, cy;
                   3995:        struct grid_cell                 gc;
1.1       nicm     3996:
1.145     nicm     3997:        py = screen_hsize(data->backing) + data->cy - data->oy;
                   3998:        yy = screen_hsize(data->backing) + screen_size_y(data->backing) - 1;
1.192     nicm     3999:        if (data->screen.sel != NULL && data->rectflag)
1.47      nicm     4000:                px = screen_size_x(&data->screen);
1.168     nicm     4001:        else
1.208     nicm     4002:                px = window_copy_find_length(wme, py);
1.1       nicm     4003:
1.145     nicm     4004:        if (data->cx >= px && py < yy) {
1.208     nicm     4005:                window_copy_cursor_start_of_line(wme);
                   4006:                window_copy_cursor_down(wme, 0);
1.145     nicm     4007:        } else if (data->cx < px) {
1.168     nicm     4008:                cx = data->cx + 1;
                   4009:                cy = screen_hsize(data->backing) + data->cy - data->oy;
                   4010:                while (cx < px) {
                   4011:                        grid_get_cell(data->backing->grid, cx, cy, &gc);
                   4012:                        if (~gc.flags & GRID_FLAG_PADDING)
                   4013:                                break;
                   4014:                        cx++;
                   4015:                }
1.208     nicm     4016:                window_copy_update_cursor(wme, cx, data->cy);
1.252     nicm     4017:                if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     4018:                        window_copy_redraw_lines(wme, data->cy, 1);
1.1       nicm     4019:        }
                   4020: }
                   4021:
1.157     nicm     4022: static void
1.208     nicm     4023: window_copy_cursor_up(struct window_mode_entry *wme, int scroll_only)
1.1       nicm     4024: {
1.208     nicm     4025:        struct window_copy_mode_data    *data = wme->data;
1.36      nicm     4026:        struct screen                   *s = &data->screen;
1.1       nicm     4027:        u_int                            ox, oy, px, py;
                   4028:
1.54      nicm     4029:        oy = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm     4030:        ox = window_copy_find_length(wme, oy);
1.77      nicm     4031:        if (data->cx != ox) {
1.25      nicm     4032:                data->lastcx = data->cx;
                   4033:                data->lastsx = ox;
                   4034:        }
1.1       nicm     4035:
1.192     nicm     4036:        if (data->lineflag == LINE_SEL_LEFT_RIGHT && oy == data->sely)
1.208     nicm     4037:                window_copy_other_end(wme);
1.118     nicm     4038:
1.28      nicm     4039:        if (scroll_only || data->cy == 0) {
1.226     nicm     4040:                data->cx = data->lastcx;
1.208     nicm     4041:                window_copy_scroll_down(wme, 1);
1.36      nicm     4042:                if (scroll_only) {
                   4043:                        if (data->cy == screen_size_y(s) - 1)
1.208     nicm     4044:                                window_copy_redraw_lines(wme, data->cy, 1);
1.36      nicm     4045:                        else
1.208     nicm     4046:                                window_copy_redraw_lines(wme, data->cy, 2);
1.36      nicm     4047:                }
1.28      nicm     4048:        } else {
1.226     nicm     4049:                window_copy_update_cursor(wme, data->lastcx, data->cy - 1);
1.252     nicm     4050:                if (window_copy_update_selection(wme, 1, 0)) {
1.36      nicm     4051:                        if (data->cy == screen_size_y(s) - 1)
1.208     nicm     4052:                                window_copy_redraw_lines(wme, data->cy, 1);
1.36      nicm     4053:                        else
1.208     nicm     4054:                                window_copy_redraw_lines(wme, data->cy, 2);
1.36      nicm     4055:                }
1.1       nicm     4056:        }
                   4057:
1.198     nicm     4058:        if (data->screen.sel == NULL || !data->rectflag) {
1.54      nicm     4059:                py = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm     4060:                px = window_copy_find_length(wme, py);
1.49      nicm     4061:                if ((data->cx >= data->lastsx && data->cx != px) ||
                   4062:                    data->cx > px)
1.208     nicm     4063:                        window_copy_cursor_end_of_line(wme);
1.47      nicm     4064:        }
1.118     nicm     4065:
1.192     nicm     4066:        if (data->lineflag == LINE_SEL_LEFT_RIGHT)
1.208     nicm     4067:                window_copy_cursor_end_of_line(wme);
1.192     nicm     4068:        else if (data->lineflag == LINE_SEL_RIGHT_LEFT)
1.208     nicm     4069:                window_copy_cursor_start_of_line(wme);
1.1       nicm     4070: }
                   4071:
1.157     nicm     4072: static void
1.208     nicm     4073: window_copy_cursor_down(struct window_mode_entry *wme, int scroll_only)
1.1       nicm     4074: {
1.208     nicm     4075:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     4076:        struct screen                   *s = &data->screen;
                   4077:        u_int                            ox, oy, px, py;
                   4078:
1.54      nicm     4079:        oy = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm     4080:        ox = window_copy_find_length(wme, oy);
1.77      nicm     4081:        if (data->cx != ox) {
1.25      nicm     4082:                data->lastcx = data->cx;
                   4083:                data->lastsx = ox;
                   4084:        }
1.1       nicm     4085:
1.192     nicm     4086:        if (data->lineflag == LINE_SEL_RIGHT_LEFT && oy == data->endsely)
1.208     nicm     4087:                window_copy_other_end(wme);
1.118     nicm     4088:
1.28      nicm     4089:        if (scroll_only || data->cy == screen_size_y(s) - 1) {
1.226     nicm     4090:                data->cx = data->lastcx;
1.208     nicm     4091:                window_copy_scroll_up(wme, 1);
1.31      nicm     4092:                if (scroll_only && data->cy > 0)
1.208     nicm     4093:                        window_copy_redraw_lines(wme, data->cy - 1, 2);
1.28      nicm     4094:        } else {
1.226     nicm     4095:                window_copy_update_cursor(wme, data->lastcx, data->cy + 1);
1.252     nicm     4096:                if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     4097:                        window_copy_redraw_lines(wme, data->cy - 1, 2);
1.1       nicm     4098:        }
                   4099:
1.192     nicm     4100:        if (data->screen.sel == NULL || !data->rectflag) {
1.54      nicm     4101:                py = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm     4102:                px = window_copy_find_length(wme, py);
1.49      nicm     4103:                if ((data->cx >= data->lastsx && data->cx != px) ||
                   4104:                    data->cx > px)
1.208     nicm     4105:                        window_copy_cursor_end_of_line(wme);
1.52      nicm     4106:        }
1.118     nicm     4107:
1.192     nicm     4108:        if (data->lineflag == LINE_SEL_LEFT_RIGHT)
1.208     nicm     4109:                window_copy_cursor_end_of_line(wme);
1.192     nicm     4110:        else if (data->lineflag == LINE_SEL_RIGHT_LEFT)
1.208     nicm     4111:                window_copy_cursor_start_of_line(wme);
1.52      nicm     4112: }
                   4113:
1.157     nicm     4114: static void
1.208     nicm     4115: window_copy_cursor_jump(struct window_mode_entry *wme)
1.52      nicm     4116: {
1.208     nicm     4117:        struct window_copy_mode_data    *data = wme->data;
1.54      nicm     4118:        struct screen                   *back_s = data->backing;
1.140     nicm     4119:        struct grid_cell                 gc;
1.67      nicm     4120:        u_int                            px, py, xx;
1.52      nicm     4121:
                   4122:        px = data->cx + 1;
1.54      nicm     4123:        py = screen_hsize(back_s) + data->cy - data->oy;
1.208     nicm     4124:        xx = window_copy_find_length(wme, py);
1.52      nicm     4125:
                   4126:        while (px < xx) {
1.140     nicm     4127:                grid_get_cell(back_s->grid, px, py, &gc);
                   4128:                if (!(gc.flags & GRID_FLAG_PADDING) &&
                   4129:                    gc.data.size == 1 && *gc.data.data == data->jumpchar) {
1.208     nicm     4130:                        window_copy_update_cursor(wme, px, data->cy);
1.252     nicm     4131:                        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     4132:                                window_copy_redraw_lines(wme, data->cy, 1);
1.52      nicm     4133:                        return;
                   4134:                }
                   4135:                px++;
                   4136:        }
                   4137: }
                   4138:
1.157     nicm     4139: static void
1.208     nicm     4140: window_copy_cursor_jump_back(struct window_mode_entry *wme)
1.52      nicm     4141: {
1.208     nicm     4142:        struct window_copy_mode_data    *data = wme->data;
1.54      nicm     4143:        struct screen                   *back_s = data->backing;
1.140     nicm     4144:        struct grid_cell                 gc;
1.67      nicm     4145:        u_int                            px, py;
1.52      nicm     4146:
                   4147:        px = data->cx;
1.54      nicm     4148:        py = screen_hsize(back_s) + data->cy - data->oy;
1.52      nicm     4149:
                   4150:        if (px > 0)
                   4151:                px--;
                   4152:
                   4153:        for (;;) {
1.140     nicm     4154:                grid_get_cell(back_s->grid, px, py, &gc);
                   4155:                if (!(gc.flags & GRID_FLAG_PADDING) &&
                   4156:                    gc.data.size == 1 && *gc.data.data == data->jumpchar) {
1.208     nicm     4157:                        window_copy_update_cursor(wme, px, data->cy);
1.252     nicm     4158:                        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     4159:                                window_copy_redraw_lines(wme, data->cy, 1);
1.76      nicm     4160:                        return;
                   4161:                }
                   4162:                if (px == 0)
                   4163:                        break;
                   4164:                px--;
                   4165:        }
                   4166: }
                   4167:
1.157     nicm     4168: static void
1.208     nicm     4169: window_copy_cursor_jump_to(struct window_mode_entry *wme)
1.76      nicm     4170: {
1.208     nicm     4171:        struct window_copy_mode_data    *data = wme->data;
1.76      nicm     4172:        struct screen                   *back_s = data->backing;
1.140     nicm     4173:        struct grid_cell                 gc;
1.76      nicm     4174:        u_int                            px, py, xx;
                   4175:
1.184     nicm     4176:        px = data->cx + 2;
1.76      nicm     4177:        py = screen_hsize(back_s) + data->cy - data->oy;
1.208     nicm     4178:        xx = window_copy_find_length(wme, py);
1.76      nicm     4179:
                   4180:        while (px < xx) {
1.140     nicm     4181:                grid_get_cell(back_s->grid, px, py, &gc);
                   4182:                if (!(gc.flags & GRID_FLAG_PADDING) &&
                   4183:                    gc.data.size == 1 && *gc.data.data == data->jumpchar) {
1.208     nicm     4184:                        window_copy_update_cursor(wme, px - 1, data->cy);
1.252     nicm     4185:                        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     4186:                                window_copy_redraw_lines(wme, data->cy, 1);
1.76      nicm     4187:                        return;
                   4188:                }
                   4189:                px++;
                   4190:        }
                   4191: }
                   4192:
1.157     nicm     4193: static void
1.208     nicm     4194: window_copy_cursor_jump_to_back(struct window_mode_entry *wme)
1.76      nicm     4195: {
1.208     nicm     4196:        struct window_copy_mode_data    *data = wme->data;
1.76      nicm     4197:        struct screen                   *back_s = data->backing;
1.140     nicm     4198:        struct grid_cell                 gc;
1.76      nicm     4199:        u_int                            px, py;
                   4200:
                   4201:        px = data->cx;
                   4202:        py = screen_hsize(back_s) + data->cy - data->oy;
                   4203:
                   4204:        if (px > 0)
1.127     nicm     4205:                px--;
                   4206:
1.184     nicm     4207:        if (px > 0)
1.76      nicm     4208:                px--;
                   4209:
                   4210:        for (;;) {
1.140     nicm     4211:                grid_get_cell(back_s->grid, px, py, &gc);
                   4212:                if (!(gc.flags & GRID_FLAG_PADDING) &&
                   4213:                    gc.data.size == 1 && *gc.data.data == data->jumpchar) {
1.208     nicm     4214:                        window_copy_update_cursor(wme, px + 1, data->cy);
1.252     nicm     4215:                        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     4216:                                window_copy_redraw_lines(wme, data->cy, 1);
1.52      nicm     4217:                        return;
                   4218:                }
                   4219:                if (px == 0)
                   4220:                        break;
                   4221:                px--;
1.47      nicm     4222:        }
1.1       nicm     4223: }
                   4224:
1.157     nicm     4225: static void
1.208     nicm     4226: window_copy_cursor_next_word(struct window_mode_entry *wme,
                   4227:     const char *separators)
1.40      nicm     4228: {
1.208     nicm     4229:        struct window_copy_mode_data    *data = wme->data;
1.54      nicm     4230:        struct screen                   *back_s = data->backing;
1.40      nicm     4231:        u_int                            px, py, xx, yy;
1.48      nicm     4232:        int                              expected = 0;
1.40      nicm     4233:
                   4234:        px = data->cx;
1.54      nicm     4235:        py = screen_hsize(back_s) + data->cy - data->oy;
1.208     nicm     4236:        xx = window_copy_find_length(wme, py);
1.54      nicm     4237:        yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
1.40      nicm     4238:
1.48      nicm     4239:        /*
                   4240:         * First skip past any nonword characters and then any word characters.
                   4241:         *
                   4242:         * expected is initially set to 0 for the former and then 1 for the
                   4243:         * latter.
                   4244:         */
                   4245:        do {
                   4246:                while (px > xx ||
1.208     nicm     4247:                    window_copy_in_set(wme, px, py, separators) == expected) {
1.48      nicm     4248:                        /* Move down if we're past the end of the line. */
                   4249:                        if (px > xx) {
                   4250:                                if (py == yy)
                   4251:                                        return;
1.208     nicm     4252:                                window_copy_cursor_down(wme, 0);
1.48      nicm     4253:                                px = 0;
                   4254:
1.54      nicm     4255:                                py = screen_hsize(back_s) + data->cy - data->oy;
1.208     nicm     4256:                                xx = window_copy_find_length(wme, py);
1.48      nicm     4257:                        } else
                   4258:                                px++;
                   4259:                }
                   4260:                expected = !expected;
                   4261:        } while (expected == 1);
1.40      nicm     4262:
1.208     nicm     4263:        window_copy_update_cursor(wme, px, data->cy);
1.252     nicm     4264:        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     4265:                window_copy_redraw_lines(wme, data->cy, 1);
1.40      nicm     4266: }
                   4267:
1.157     nicm     4268: static void
1.247     nicm     4269: window_copy_cursor_next_word_end_pos(struct window_mode_entry *wme,
                   4270:     const char *separators, u_int *ppx, u_int *ppy)
                   4271: {
                   4272:        struct window_pane              *wp = wme->wp;
                   4273:        struct window_copy_mode_data    *data = wme->data;
                   4274:        struct options                  *oo = wp->window->options;
                   4275:        struct screen                   *back_s = data->backing;
                   4276:        u_int                            px, py, xx, yy;
                   4277:        int                              keys, expected = 1;
                   4278:
                   4279:        px = data->cx;
                   4280:        py = screen_hsize(back_s) + data->cy - data->oy;
                   4281:        xx = window_copy_find_length(wme, py);
                   4282:        yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
                   4283:
                   4284:        keys = options_get_number(oo, "mode-keys");
                   4285:        if (keys == MODEKEY_VI && !window_copy_in_set(wme, px, py, separators))
                   4286:                px++;
                   4287:
                   4288:        /*
                   4289:         * First skip past any word characters, then any non-word characters.
                   4290:         *
                   4291:         * expected is initially set to 1 for the former and then 0 for the
                   4292:         * latter.
                   4293:         */
                   4294:        do {
                   4295:                while (px > xx ||
                   4296:                    window_copy_in_set(wme, px, py, separators) == expected) {
                   4297:                        /* Move down if we're past the end of the line. */
                   4298:                        if (px > xx) {
                   4299:                                if (py == yy)
                   4300:                                        return;
                   4301:                                py++;
                   4302:                                px = 0;
                   4303:                                xx = window_copy_find_length(wme, py);
                   4304:                        } else
                   4305:                                px++;
                   4306:                }
                   4307:                expected = !expected;
                   4308:        } while (expected == 0);
                   4309:
                   4310:        if (keys == MODEKEY_VI && px != 0)
                   4311:                px--;
                   4312:
                   4313:        *ppx = px;
                   4314:        *ppy = py;
                   4315: }
                   4316:
                   4317: static void
1.208     nicm     4318: window_copy_cursor_next_word_end(struct window_mode_entry *wme,
1.255     nicm     4319:     const char *separators, int no_reset)
1.1       nicm     4320: {
1.208     nicm     4321:        struct window_pane              *wp = wme->wp;
                   4322:        struct window_copy_mode_data    *data = wme->data;
1.136     nicm     4323:        struct options                  *oo = wp->window->options;
1.54      nicm     4324:        struct screen                   *back_s = data->backing;
1.39      nicm     4325:        u_int                            px, py, xx, yy;
1.94      nicm     4326:        int                              keys, expected = 1;
1.1       nicm     4327:
1.18      nicm     4328:        px = data->cx;
1.54      nicm     4329:        py = screen_hsize(back_s) + data->cy - data->oy;
1.208     nicm     4330:        xx = window_copy_find_length(wme, py);
1.54      nicm     4331:        yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
1.1       nicm     4332:
1.94      nicm     4333:        keys = options_get_number(oo, "mode-keys");
1.208     nicm     4334:        if (keys == MODEKEY_VI && !window_copy_in_set(wme, px, py, separators))
1.94      nicm     4335:                px++;
                   4336:
1.48      nicm     4337:        /*
                   4338:         * First skip past any word characters, then any nonword characters.
                   4339:         *
                   4340:         * expected is initially set to 1 for the former and then 0 for the
                   4341:         * latter.
                   4342:         */
                   4343:        do {
                   4344:                while (px > xx ||
1.208     nicm     4345:                    window_copy_in_set(wme, px, py, separators) == expected) {
1.48      nicm     4346:                        /* Move down if we're past the end of the line. */
                   4347:                        if (px > xx) {
                   4348:                                if (py == yy)
                   4349:                                        return;
1.208     nicm     4350:                                window_copy_cursor_down(wme, 0);
1.48      nicm     4351:                                px = 0;
                   4352:
1.54      nicm     4353:                                py = screen_hsize(back_s) + data->cy - data->oy;
1.208     nicm     4354:                                xx = window_copy_find_length(wme, py);
1.48      nicm     4355:                        } else
                   4356:                                px++;
                   4357:                }
                   4358:                expected = !expected;
                   4359:        } while (expected == 0);
1.92      nicm     4360:
1.94      nicm     4361:        if (keys == MODEKEY_VI && px != 0)
1.92      nicm     4362:                px--;
1.18      nicm     4363:
1.208     nicm     4364:        window_copy_update_cursor(wme, px, data->cy);
1.255     nicm     4365:        if (window_copy_update_selection(wme, 1, no_reset))
1.208     nicm     4366:                window_copy_redraw_lines(wme, data->cy, 1);
1.1       nicm     4367: }
                   4368:
1.247     nicm     4369: /* Compute the previous place where a word begins. */
                   4370: static void
                   4371: window_copy_cursor_previous_word_pos(struct window_mode_entry *wme,
                   4372:     const char *separators, int already, u_int *ppx, u_int *ppy)
                   4373: {
                   4374:        struct window_copy_mode_data    *data = wme->data;
                   4375:        u_int                            px, py;
                   4376:
                   4377:        px = data->cx;
                   4378:        py = screen_hsize(data->backing) + data->cy - data->oy;
                   4379:
                   4380:        /* Move back to the previous word character. */
                   4381:        if (already || window_copy_in_set(wme, px, py, separators)) {
                   4382:                for (;;) {
                   4383:                        if (px > 0) {
                   4384:                                px--;
                   4385:                                if (!window_copy_in_set(wme, px, py,
                   4386:                                    separators))
                   4387:                                        break;
                   4388:                        } else {
                   4389:                                if (data->cy == 0 &&
                   4390:                                    (screen_hsize(data->backing) == 0 ||
                   4391:                                    data->oy >=
                   4392:                                    screen_hsize(data->backing) - 1))
                   4393:                                        goto out;
                   4394:
                   4395:                                py = screen_hsize(data->backing) + data->cy -
                   4396:                                    data->oy;
                   4397:                                px = window_copy_find_length(wme, py);
                   4398:
                   4399:                                /* Stop if separator at EOL. */
                   4400:                                if (px > 0 && window_copy_in_set(wme, px - 1,
                   4401:                                    py, separators))
                   4402:                                        break;
                   4403:                        }
                   4404:                }
                   4405:        }
                   4406:
                   4407:        /* Move back to the beginning of this word. */
                   4408:        while (px > 0 && !window_copy_in_set(wme, px - 1, py, separators))
                   4409:                px--;
                   4410:
                   4411: out:
                   4412:        *ppx = px;
                   4413:        *ppy = py;
                   4414: }
                   4415:
1.8       nicm     4416: /* Move to the previous place where a word begins. */
1.157     nicm     4417: static void
1.208     nicm     4418: window_copy_cursor_previous_word(struct window_mode_entry *wme,
1.229     nicm     4419:     const char *separators, int already)
1.1       nicm     4420: {
1.208     nicm     4421:        struct window_copy_mode_data    *data = wme->data;
1.8       nicm     4422:        u_int                            px, py;
1.1       nicm     4423:
1.18      nicm     4424:        px = data->cx;
1.54      nicm     4425:        py = screen_hsize(data->backing) + data->cy - data->oy;
1.1       nicm     4426:
1.8       nicm     4427:        /* Move back to the previous word character. */
1.229     nicm     4428:        if (already || window_copy_in_set(wme, px, py, separators)) {
                   4429:                for (;;) {
                   4430:                        if (px > 0) {
                   4431:                                px--;
1.247     nicm     4432:                                if (!window_copy_in_set(wme, px, py,
                   4433:                                    separators))
1.229     nicm     4434:                                        break;
                   4435:                        } else {
                   4436:                                if (data->cy == 0 &&
                   4437:                                    (screen_hsize(data->backing) == 0 ||
1.247     nicm     4438:                                    data->oy >=
                   4439:                                    screen_hsize(data->backing) - 1))
1.229     nicm     4440:                                        goto out;
                   4441:                                window_copy_cursor_up(wme, 0);
                   4442:
1.247     nicm     4443:                                py = screen_hsize(data->backing) + data->cy -
                   4444:                                    data->oy;
1.229     nicm     4445:                                px = window_copy_find_length(wme, py);
                   4446:
                   4447:                                /* Stop if separator at EOL. */
1.247     nicm     4448:                                if (px > 0 && window_copy_in_set(wme, px - 1,
                   4449:                                    py, separators))
1.229     nicm     4450:                                        break;
                   4451:                        }
1.1       nicm     4452:                }
1.8       nicm     4453:        }
1.1       nicm     4454:
1.8       nicm     4455:        /* Move back to the beginning of this word. */
1.208     nicm     4456:        while (px > 0 && !window_copy_in_set(wme, px - 1, py, separators))
1.1       nicm     4457:                px--;
1.8       nicm     4458:
1.1       nicm     4459: out:
1.208     nicm     4460:        window_copy_update_cursor(wme, px, data->cy);
1.252     nicm     4461:        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     4462:                window_copy_redraw_lines(wme, data->cy, 1);
1.1       nicm     4463: }
                   4464:
1.157     nicm     4465: static void
1.208     nicm     4466: window_copy_scroll_up(struct window_mode_entry *wme, u_int ny)
1.1       nicm     4467: {
1.208     nicm     4468:        struct window_pane              *wp = wme->wp;
                   4469:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     4470:        struct screen                   *s = &data->screen;
                   4471:        struct screen_write_ctx          ctx;
                   4472:
                   4473:        if (data->oy < ny)
                   4474:                ny = data->oy;
                   4475:        if (ny == 0)
                   4476:                return;
                   4477:        data->oy -= ny;
                   4478:
1.264     nicm     4479:        if (data->searchmark != NULL && !data->timeout)
                   4480:                window_copy_search_marks(wme, NULL, data->searchregex);
1.252     nicm     4481:        window_copy_update_selection(wme, 0, 0);
1.96      nicm     4482:
1.283     nicm     4483:        screen_write_start_pane(&ctx, wp, NULL);
1.212     nicm     4484:        screen_write_cursormove(&ctx, 0, 0, 0);
1.159     nicm     4485:        screen_write_deleteline(&ctx, ny, 8);
1.208     nicm     4486:        window_copy_write_lines(wme, &ctx, screen_size_y(s) - ny, ny);
                   4487:        window_copy_write_line(wme, &ctx, 0);
1.31      nicm     4488:        if (screen_size_y(s) > 1)
1.208     nicm     4489:                window_copy_write_line(wme, &ctx, 1);
1.31      nicm     4490:        if (screen_size_y(s) > 3)
1.208     nicm     4491:                window_copy_write_line(wme, &ctx, screen_size_y(s) - 2);
1.192     nicm     4492:        if (s->sel != NULL && screen_size_y(s) > ny)
1.208     nicm     4493:                window_copy_write_line(wme, &ctx, screen_size_y(s) - ny - 1);
1.212     nicm     4494:        screen_write_cursormove(&ctx, data->cx, data->cy, 0);
1.1       nicm     4495:        screen_write_stop(&ctx);
                   4496: }
                   4497:
1.157     nicm     4498: static void
1.208     nicm     4499: window_copy_scroll_down(struct window_mode_entry *wme, u_int ny)
1.1       nicm     4500: {
1.208     nicm     4501:        struct window_pane              *wp = wme->wp;
                   4502:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     4503:        struct screen                   *s = &data->screen;
                   4504:        struct screen_write_ctx          ctx;
                   4505:
1.54      nicm     4506:        if (ny > screen_hsize(data->backing))
1.1       nicm     4507:                return;
                   4508:
1.54      nicm     4509:        if (data->oy > screen_hsize(data->backing) - ny)
                   4510:                ny = screen_hsize(data->backing) - data->oy;
1.1       nicm     4511:        if (ny == 0)
                   4512:                return;
                   4513:        data->oy += ny;
                   4514:
1.264     nicm     4515:        if (data->searchmark != NULL && !data->timeout)
                   4516:                window_copy_search_marks(wme, NULL, data->searchregex);
1.252     nicm     4517:        window_copy_update_selection(wme, 0, 0);
1.96      nicm     4518:
1.283     nicm     4519:        screen_write_start_pane(&ctx, wp, NULL);
1.212     nicm     4520:        screen_write_cursormove(&ctx, 0, 0, 0);
1.159     nicm     4521:        screen_write_insertline(&ctx, ny, 8);
1.208     nicm     4522:        window_copy_write_lines(wme, &ctx, 0, ny);
1.192     nicm     4523:        if (s->sel != NULL && screen_size_y(s) > ny)
1.208     nicm     4524:                window_copy_write_line(wme, &ctx, ny);
1.96      nicm     4525:        else if (ny == 1) /* nuke position */
1.208     nicm     4526:                window_copy_write_line(wme, &ctx, 1);
1.212     nicm     4527:        screen_write_cursormove(&ctx, data->cx, data->cy, 0);
1.1       nicm     4528:        screen_write_stop(&ctx);
                   4529: }
1.42      nicm     4530:
1.157     nicm     4531: static void
1.208     nicm     4532: window_copy_rectangle_toggle(struct window_mode_entry *wme)
1.42      nicm     4533: {
1.208     nicm     4534:        struct window_copy_mode_data    *data = wme->data;
1.47      nicm     4535:        u_int                            px, py;
1.42      nicm     4536:
                   4537:        data->rectflag = !data->rectflag;
1.47      nicm     4538:
1.54      nicm     4539:        py = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm     4540:        px = window_copy_find_length(wme, py);
1.47      nicm     4541:        if (data->cx > px)
1.208     nicm     4542:                window_copy_update_cursor(wme, px, data->cy);
1.42      nicm     4543:
1.252     nicm     4544:        window_copy_update_selection(wme, 1, 0);
1.208     nicm     4545:        window_copy_redraw_screen(wme);
1.156     nicm     4546: }
                   4547:
1.157     nicm     4548: static void
1.156     nicm     4549: window_copy_move_mouse(struct mouse_event *m)
                   4550: {
1.211     nicm     4551:        struct window_pane              *wp;
                   4552:        struct window_mode_entry        *wme;
                   4553:        u_int                            x, y;
1.156     nicm     4554:
                   4555:        wp = cmd_mouse_pane(m, NULL, NULL);
1.211     nicm     4556:        if (wp == NULL)
                   4557:                return;
                   4558:        wme = TAILQ_FIRST(&wp->modes);
1.223     nicm     4559:        if (wme == NULL)
                   4560:                return;
                   4561:        if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
1.156     nicm     4562:                return;
                   4563:
1.183     nicm     4564:        if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
1.156     nicm     4565:                return;
                   4566:
1.211     nicm     4567:        window_copy_update_cursor(wme, x, y);
1.126     nicm     4568: }
                   4569:
                   4570: void
1.141     nicm     4571: window_copy_start_drag(struct client *c, struct mouse_event *m)
1.126     nicm     4572: {
1.211     nicm     4573:        struct window_pane              *wp;
                   4574:        struct window_mode_entry        *wme;
1.247     nicm     4575:        struct window_copy_mode_data    *data;
1.248     nicm     4576:        u_int                            x, y, yg;
1.126     nicm     4577:
1.155     nicm     4578:        if (c == NULL)
                   4579:                return;
                   4580:
1.126     nicm     4581:        wp = cmd_mouse_pane(m, NULL, NULL);
1.211     nicm     4582:        if (wp == NULL)
                   4583:                return;
                   4584:        wme = TAILQ_FIRST(&wp->modes);
1.223     nicm     4585:        if (wme == NULL)
                   4586:                return;
                   4587:        if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
1.126     nicm     4588:                return;
                   4589:
                   4590:        if (cmd_mouse_at(wp, m, &x, &y, 1) != 0)
                   4591:                return;
                   4592:
                   4593:        c->tty.mouse_drag_update = window_copy_drag_update;
1.217     nicm     4594:        c->tty.mouse_drag_release = window_copy_drag_release;
1.126     nicm     4595:
1.247     nicm     4596:        data = wme->data;
1.248     nicm     4597:        yg = screen_hsize(data->backing) + y - data->oy;
                   4598:        if (x < data->selrx || x > data->endselrx || yg != data->selry)
                   4599:                data->selflag = SEL_CHAR;
1.247     nicm     4600:        switch (data->selflag) {
                   4601:                case SEL_WORD:
1.268     nicm     4602:                        if (data->ws != NULL) {
1.247     nicm     4603:                                window_copy_update_cursor(wme, x, y);
                   4604:                                window_copy_cursor_previous_word_pos(wme,
                   4605:                                    data->ws, 0, &x, &y);
                   4606:                                y -= screen_hsize(data->backing) - data->oy;
                   4607:                        }
                   4608:                        window_copy_update_cursor(wme, x, y);
                   4609:                        break;
                   4610:                case SEL_LINE:
                   4611:                        window_copy_update_cursor(wme, 0, y);
                   4612:                        break;
                   4613:                case SEL_CHAR:
                   4614:                        window_copy_update_cursor(wme, x, y);
                   4615:                        window_copy_start_selection(wme);
                   4616:                        break;
                   4617:        }
                   4618:
1.211     nicm     4619:        window_copy_redraw_screen(wme);
1.220     nicm     4620:        window_copy_drag_update(c, m);
1.126     nicm     4621: }
                   4622:
1.157     nicm     4623: static void
1.211     nicm     4624: window_copy_drag_update(struct client *c, struct mouse_event *m)
1.126     nicm     4625: {
                   4626:        struct window_pane              *wp;
1.211     nicm     4627:        struct window_mode_entry        *wme;
1.126     nicm     4628:        struct window_copy_mode_data    *data;
1.217     nicm     4629:        u_int                            x, y, old_cx, old_cy;
                   4630:        struct timeval                   tv = {
                   4631:                .tv_usec = WINDOW_COPY_DRAG_REPEAT_TIME
                   4632:        };
1.126     nicm     4633:
1.211     nicm     4634:        if (c == NULL)
                   4635:                return;
                   4636:
1.126     nicm     4637:        wp = cmd_mouse_pane(m, NULL, NULL);
1.211     nicm     4638:        if (wp == NULL)
1.126     nicm     4639:                return;
1.211     nicm     4640:        wme = TAILQ_FIRST(&wp->modes);
1.223     nicm     4641:        if (wme == NULL)
                   4642:                return;
                   4643:        if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
1.211     nicm     4644:                return;
1.217     nicm     4645:
1.211     nicm     4646:        data = wme->data;
1.217     nicm     4647:        evtimer_del(&data->dragtimer);
1.126     nicm     4648:
                   4649:        if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
                   4650:                return;
1.217     nicm     4651:        old_cx = data->cx;
1.126     nicm     4652:        old_cy = data->cy;
                   4653:
1.211     nicm     4654:        window_copy_update_cursor(wme, x, y);
1.252     nicm     4655:        if (window_copy_update_selection(wme, 1, 0))
1.211     nicm     4656:                window_copy_redraw_selection(wme, old_cy);
1.217     nicm     4657:        if (old_cy != data->cy || old_cx == data->cx) {
                   4658:                if (y == 0) {
                   4659:                        evtimer_add(&data->dragtimer, &tv);
                   4660:                        window_copy_cursor_up(wme, 1);
                   4661:                } else if (y == screen_size_y(&data->screen) - 1) {
                   4662:                        evtimer_add(&data->dragtimer, &tv);
                   4663:                        window_copy_cursor_down(wme, 1);
                   4664:                }
                   4665:        }
                   4666: }
                   4667:
                   4668: static void
                   4669: window_copy_drag_release(struct client *c, struct mouse_event *m)
                   4670: {
                   4671:        struct window_pane              *wp;
                   4672:        struct window_mode_entry        *wme;
                   4673:        struct window_copy_mode_data    *data;
                   4674:
                   4675:        if (c == NULL)
                   4676:                return;
                   4677:
                   4678:        wp = cmd_mouse_pane(m, NULL, NULL);
                   4679:        if (wp == NULL)
                   4680:                return;
                   4681:        wme = TAILQ_FIRST(&wp->modes);
1.223     nicm     4682:        if (wme == NULL)
                   4683:                return;
                   4684:        if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
1.217     nicm     4685:                return;
                   4686:
                   4687:        data = wme->data;
                   4688:        evtimer_del(&data->dragtimer);
1.42      nicm     4689: }