[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.285

1.285   ! nicm        1: /* $OpenBSD: window-copy.c,v 1.284 2020/05/16 15:38:14 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);
                   1368:                                if (gc.data.size != 1 ||
                   1369:                                    (gc.flags & GRID_FLAG_PADDING) ||
                   1370:                                    strchr(close, *gc.data.data) == NULL)
                   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.260     nicm     2448:        if (regexec(reg, buf, 1, &regmatch, eflags) == 0) {
1.244     nicm     2449:                foundx = first;
                   2450:                foundy = py;
                   2451:                window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
                   2452:                    buf + regmatch.rm_so);
                   2453:                if (foundy == py && foundx < last) {
                   2454:                        *ppx = foundx;
                   2455:                        len -= foundx - first;
                   2456:                        window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
                   2457:                            buf + regmatch.rm_eo);
                   2458:                        *psx = foundx;
                   2459:                        while (foundy > py) {
                   2460:                                *psx += gd->sx;
                   2461:                                foundy--;
                   2462:                        }
                   2463:                        *psx -= *ppx;
                   2464:                        free(buf);
                   2465:                        return (1);
                   2466:                }
                   2467:        }
                   2468:
                   2469:        free(buf);
                   2470:        *ppx = 0;
                   2471:        *psx = 0;
                   2472:        return (0);
                   2473: }
                   2474:
                   2475: static int
1.260     nicm     2476: window_copy_search_rl_regex(struct grid *gd, u_int *ppx, u_int *psx, u_int py,
                   2477:     u_int first, u_int last, regex_t *reg)
1.244     nicm     2478: {
1.260     nicm     2479:        int                     eflags = 0;
                   2480:        u_int                   endline, len, pywrap, size = 1;
                   2481:        char                   *buf;
1.244     nicm     2482:        struct grid_line       *gl;
                   2483:
                   2484:        /* Set flags for regex search. */
                   2485:        if (first != 0)
                   2486:                eflags |= REG_NOTBOL;
                   2487:
                   2488:        /* Need to look at the entire string. */
                   2489:        buf = xmalloc(size);
                   2490:        buf[0] = '\0';
                   2491:        buf = window_copy_stringify(gd, py, first, gd->sx, buf, &size);
                   2492:        len = gd->sx - first;
                   2493:        endline = gd->hsize + gd->sy - 1;
                   2494:        pywrap = py;
                   2495:        while (buf != NULL && (pywrap <= endline)) {
                   2496:                gl = grid_get_line(gd, pywrap);
                   2497:                if (~gl->flags & GRID_LINE_WRAPPED)
                   2498:                        break;
                   2499:                pywrap++;
                   2500:                buf = window_copy_stringify(gd, pywrap, 0, gd->sx, buf, &size);
                   2501:                len += gd->sx;
                   2502:        }
                   2503:
                   2504:        if (window_copy_last_regex(gd, py, first, last, len, ppx, psx, buf,
1.260     nicm     2505:            reg, eflags))
1.244     nicm     2506:        {
                   2507:                free(buf);
                   2508:                return (1);
                   2509:        }
                   2510:
                   2511:        free(buf);
                   2512:        *ppx = 0;
                   2513:        *psx = 0;
                   2514:        return (0);
                   2515: }
                   2516:
1.260     nicm     2517: static const char *
                   2518: window_copy_cellstring(const struct grid_line *gl, u_int px, size_t *size)
                   2519: {
                   2520:        struct grid_cell_entry  *gce;
                   2521:
                   2522:        if (px >= gl->cellsize) {
                   2523:                *size = 1;
                   2524:                return (" ");
                   2525:        }
                   2526:
                   2527:        gce = &gl->celldata[px];
                   2528:        if (~gce->flags & GRID_FLAG_EXTENDED) {
                   2529:                *size = 1;
                   2530:                return (&gce->data.data);
                   2531:        }
                   2532:
                   2533:        *size = gl->extddata[gce->offset].data.size;
                   2534:        return (gl->extddata[gce->offset].data.data);
                   2535: }
                   2536:
1.244     nicm     2537: /* Find last match in given range. */
                   2538: static int
                   2539: window_copy_last_regex(struct grid *gd, u_int py, u_int first, u_int last,
                   2540:     u_int len, u_int *ppx, u_int *psx, const char *buf, const regex_t *preg,
                   2541:     int eflags)
                   2542: {
                   2543:        u_int           foundx, foundy, oldx, px = 0, savepx, savesx = 0;
                   2544:        regmatch_t      regmatch;
                   2545:
                   2546:        foundx = first;
                   2547:        foundy = py;
                   2548:        oldx = first;
                   2549:        while (regexec(preg, buf + px, 1, &regmatch, eflags) == 0) {
                   2550:                window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
                   2551:                    buf + px + regmatch.rm_so);
                   2552:                if (foundy > py || foundx >= last)
                   2553:                        break;
                   2554:                len -= foundx - oldx;
                   2555:                savepx = foundx;
                   2556:                window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
                   2557:                    buf + px + regmatch.rm_eo);
                   2558:                if (foundy > py || foundx >= last) {
                   2559:                        *ppx = savepx;
                   2560:                        *psx = foundx;
                   2561:                        while (foundy > py) {
                   2562:                                *psx += gd->sx;
                   2563:                                foundy--;
                   2564:                        }
                   2565:                        *psx -= *ppx;
                   2566:                        return (1);
                   2567:                } else {
                   2568:                        savesx = foundx - savepx;
                   2569:                        len -= savesx;
                   2570:                        oldx = foundx;
                   2571:                }
                   2572:                px += regmatch.rm_eo;
                   2573:        }
                   2574:
                   2575:        if (savesx > 0) {
                   2576:                *ppx = savepx;
                   2577:                *psx = savesx;
                   2578:                return (1);
                   2579:        } else {
                   2580:                *ppx = 0;
                   2581:                *psx = 0;
                   2582:                return (0);
                   2583:        }
                   2584: }
                   2585:
                   2586: /* Stringify line and append to input buffer. Caller frees. */
                   2587: static char *
                   2588: window_copy_stringify(struct grid *gd, u_int py, u_int first, u_int last,
                   2589:     char *buf, u_int *size)
                   2590: {
1.260     nicm     2591:        u_int                    ax, bx, newsize = *size;
                   2592:        const struct grid_line  *gl;
                   2593:        const char              *d;
                   2594:        size_t                   bufsize = 1024, dlen;
                   2595:
                   2596:        while (bufsize < newsize)
                   2597:                bufsize *= 2;
                   2598:        buf = xrealloc(buf, bufsize);
1.244     nicm     2599:
1.260     nicm     2600:        gl = grid_peek_line(gd, py);
1.244     nicm     2601:        bx = *size - 1;
                   2602:        for (ax = first; ax < last; ax++) {
1.260     nicm     2603:                d = window_copy_cellstring(gl, ax, &dlen);
                   2604:                newsize += dlen;
                   2605:                while (bufsize < newsize) {
                   2606:                        bufsize *= 2;
                   2607:                        buf = xrealloc(buf, bufsize);
                   2608:                }
                   2609:                if (dlen == 1)
                   2610:                        buf[bx++] = *d;
                   2611:                else {
                   2612:                        memcpy(buf + bx, d, dlen);
                   2613:                        bx += dlen;
                   2614:                }
1.244     nicm     2615:        }
1.260     nicm     2616:        buf[newsize - 1] = '\0';
1.244     nicm     2617:
                   2618:        *size = newsize;
                   2619:        return (buf);
                   2620: }
                   2621:
                   2622: /* Map start of C string containing UTF-8 data to grid cell position. */
                   2623: static void
                   2624: window_copy_cstrtocellpos(struct grid *gd, u_int ncells, u_int *ppx, u_int *ppy,
                   2625:     const char *str)
                   2626: {
1.260     nicm     2627:        u_int                    cell, ccell, px, pywrap, pos, len;
                   2628:        int                      match;
                   2629:        const struct grid_line  *gl;
                   2630:        const char              *d;
                   2631:        size_t                   dlen;
                   2632:        struct {
                   2633:                const char      *d;
                   2634:                size_t           dlen;
                   2635:        } *cells;
1.244     nicm     2636:
1.260     nicm     2637:        /* Populate the array of cell data. */
1.244     nicm     2638:        cells = xreallocarray(NULL, ncells, sizeof cells[0]);
                   2639:        cell = 0;
                   2640:        px = *ppx;
                   2641:        pywrap = *ppy;
1.260     nicm     2642:        gl = grid_peek_line(gd, pywrap);
1.244     nicm     2643:        while (cell < ncells) {
1.260     nicm     2644:                cells[cell].d = window_copy_cellstring(gl, px,
                   2645:                    &cells[cell].dlen);
1.244     nicm     2646:                cell++;
1.261     nicm     2647:                px++;
                   2648:                if (px == gd->sx) {
                   2649:                        px = 0;
1.244     nicm     2650:                        pywrap++;
1.260     nicm     2651:                        gl = grid_peek_line(gd, pywrap);
                   2652:                }
1.244     nicm     2653:        }
                   2654:
                   2655:        /* Locate starting cell. */
                   2656:        cell = 0;
1.260     nicm     2657:        len = strlen(str);
1.244     nicm     2658:        while (cell < ncells) {
                   2659:                ccell = cell;
1.260     nicm     2660:                pos = 0;
1.244     nicm     2661:                match = 1;
                   2662:                while (ccell < ncells) {
1.260     nicm     2663:                        if (str[pos] == '\0') {
1.244     nicm     2664:                                match = 0;
                   2665:                                break;
                   2666:                        }
1.260     nicm     2667:                        d = cells[ccell].d;
                   2668:                        dlen = cells[ccell].dlen;
                   2669:                        if (dlen == 1) {
                   2670:                                if (str[pos] != *d) {
                   2671:                                        match = 0;
                   2672:                                        break;
                   2673:                                }
                   2674:                                pos++;
                   2675:                        } else {
                   2676:                                if (dlen > len - pos)
                   2677:                                        dlen = len - pos;
                   2678:                                if (memcmp(str + pos, d, dlen) != 0) {
1.244     nicm     2679:                                        match = 0;
                   2680:                                        break;
                   2681:                                }
1.260     nicm     2682:                                pos += dlen;
1.244     nicm     2683:                        }
                   2684:                        ccell++;
                   2685:                }
                   2686:                if (match)
                   2687:                        break;
                   2688:                cell++;
                   2689:        }
                   2690:
                   2691:        /* If not found this will be one past the end. */
                   2692:        px = *ppx + cell;
                   2693:        pywrap = *ppy;
                   2694:        while (px >= gd->sx) {
                   2695:                px -= gd->sx;
                   2696:                pywrap++;
                   2697:        }
                   2698:
                   2699:        *ppx = px;
                   2700:        *ppy = pywrap;
                   2701:
                   2702:        /* Free cell data. */
                   2703:        free(cells);
                   2704: }
                   2705:
1.157     nicm     2706: static void
1.230     nicm     2707: window_copy_move_left(struct screen *s, u_int *fx, u_int *fy, int wrapflag)
1.21      nicm     2708: {
1.150     nicm     2709:        if (*fx == 0) { /* left */
1.230     nicm     2710:                if (*fy == 0) { /* top */
                   2711:                        if (wrapflag) {
                   2712:                                *fx = screen_size_x(s) - 1;
1.242     nicm     2713:                                *fy = screen_hsize(s) + screen_size_y(s) - 1;
1.230     nicm     2714:                        }
1.150     nicm     2715:                        return;
1.230     nicm     2716:                }
1.150     nicm     2717:                *fx = screen_size_x(s) - 1;
                   2718:                *fy = *fy - 1;
                   2719:        } else
                   2720:                *fx = *fx - 1;
                   2721: }
1.21      nicm     2722:
1.157     nicm     2723: static void
1.230     nicm     2724: window_copy_move_right(struct screen *s, u_int *fx, u_int *fy, int wrapflag)
1.150     nicm     2725: {
                   2726:        if (*fx == screen_size_x(s) - 1) { /* right */
1.242     nicm     2727:                if (*fy == screen_hsize(s) + screen_size_y(s) - 1) { /* bottom */
1.230     nicm     2728:                        if (wrapflag) {
                   2729:                                *fx = 0;
                   2730:                                *fy = 0;
                   2731:                        }
1.21      nicm     2732:                        return;
1.230     nicm     2733:                }
1.150     nicm     2734:                *fx = 0;
                   2735:                *fy = *fy + 1;
1.21      nicm     2736:        } else
1.150     nicm     2737:                *fx = *fx + 1;
                   2738: }
1.21      nicm     2739:
1.157     nicm     2740: static int
1.150     nicm     2741: window_copy_is_lowercase(const char *ptr)
                   2742: {
                   2743:        while (*ptr != '\0') {
                   2744:                if (*ptr != tolower((u_char)*ptr))
                   2745:                        return (0);
                   2746:                ++ptr;
1.97      nicm     2747:        }
1.150     nicm     2748:        return (1);
                   2749: }
1.97      nicm     2750:
1.150     nicm     2751: /*
                   2752:  * Search for text stored in sgd starting from position fx,fy up to endline. If
                   2753:  * found, jump to it. If cis then ignore case. The direction is 0 for searching
                   2754:  * up, down otherwise. If wrap then go to begin/end of grid and try again if
                   2755:  * not found.
                   2756:  */
1.163     nicm     2757: static int
1.208     nicm     2758: window_copy_search_jump(struct window_mode_entry *wme, struct grid *gd,
1.150     nicm     2759:     struct grid *sgd, u_int fx, u_int fy, u_int endline, int cis, int wrap,
1.244     nicm     2760:     int direction, int regex)
1.150     nicm     2761: {
1.260     nicm     2762:        u_int    i, px, sx, ssize = 1;
                   2763:        int      found = 0, cflags = REG_EXTENDED;
                   2764:        char    *sbuf;
                   2765:        regex_t  reg;
                   2766:
                   2767:        if (regex) {
                   2768:                sbuf = xmalloc(ssize);
                   2769:                sbuf[0] = '\0';
                   2770:                sbuf = window_copy_stringify(sgd, 0, 0, sgd->sx, sbuf, &ssize);
                   2771:                if (cis)
                   2772:                        cflags |= REG_ICASE;
                   2773:                if (regcomp(&reg, sbuf, cflags) != 0) {
                   2774:                        free(sbuf);
                   2775:                        return (0);
                   2776:                }
                   2777:        }
1.150     nicm     2778:
                   2779:        if (direction) {
                   2780:                for (i = fy; i <= endline; i++) {
1.260     nicm     2781:                        if (regex) {
                   2782:                                found = window_copy_search_lr_regex(gd,
                   2783:                                    &px, &sx, i, fx, gd->sx, &reg);
                   2784:                        } else {
1.244     nicm     2785:                                found = window_copy_search_lr(gd, sgd,
                   2786:                                    &px, i, fx, gd->sx, cis);
1.260     nicm     2787:                        }
1.150     nicm     2788:                        if (found)
                   2789:                                break;
                   2790:                        fx = 0;
                   2791:                }
                   2792:        } else {
                   2793:                for (i = fy + 1; endline < i; i--) {
1.260     nicm     2794:                        if (regex) {
                   2795:                                found = window_copy_search_rl_regex(gd,
                   2796:                                    &px, &sx, i - 1, 0, fx + 1, &reg);
                   2797:                        } else {
1.244     nicm     2798:                                found = window_copy_search_rl(gd, sgd,
                   2799:                                    &px, i - 1, 0, fx + 1, cis);
1.260     nicm     2800:                        }
1.150     nicm     2801:                        if (found) {
                   2802:                                i--;
                   2803:                                break;
                   2804:                        }
1.242     nicm     2805:                        fx = gd->sx - 1;
1.21      nicm     2806:                }
                   2807:        }
1.260     nicm     2808:        if (regex) {
                   2809:                free(sbuf);
                   2810:                regfree(&reg);
                   2811:        }
1.150     nicm     2812:
1.163     nicm     2813:        if (found) {
1.208     nicm     2814:                window_copy_scroll_to(wme, px, i);
1.163     nicm     2815:                return (1);
                   2816:        }
                   2817:        if (wrap) {
1.208     nicm     2818:                return (window_copy_search_jump(wme, gd, sgd,
1.163     nicm     2819:                    direction ? 0 : gd->sx - 1,
1.150     nicm     2820:                    direction ? 0 : gd->hsize + gd->sy - 1, fy, cis, 0,
1.244     nicm     2821:                    direction, regex));
1.21      nicm     2822:        }
1.163     nicm     2823:        return (0);
1.21      nicm     2824: }
                   2825:
1.232     nicm     2826: /*
                   2827:  * Search in for text searchstr. If direction is 0 then search up, otherwise
                   2828:  * down.
                   2829:  */
1.163     nicm     2830: static int
1.244     nicm     2831: window_copy_search(struct window_mode_entry *wme, int direction, int regex)
1.21      nicm     2832: {
1.208     nicm     2833:        struct window_pane              *wp = wme->wp;
                   2834:        struct window_copy_mode_data    *data = wme->data;
1.54      nicm     2835:        struct screen                   *s = data->backing, ss;
1.21      nicm     2836:        struct screen_write_ctx          ctx;
1.150     nicm     2837:        struct grid                     *gd = s->grid;
1.262     nicm     2838:        const char                      *str = data->searchstr;
1.150     nicm     2839:        u_int                            fx, fy, endline;
1.163     nicm     2840:        int                              wrapflag, cis, found;
1.21      nicm     2841:
1.262     nicm     2842:        if (regex && str[strcspn(str, "^$*+()?[].\\")] == '\0')
                   2843:                regex = 0;
                   2844:
1.263     nicm     2845:        if (data->timeout)
                   2846:                return (0);
                   2847:
1.174     nicm     2848:        free(wp->searchstr);
1.262     nicm     2849:        wp->searchstr = xstrdup(str);
1.259     nicm     2850:        wp->searchregex = regex;
1.174     nicm     2851:
1.150     nicm     2852:        fx = data->cx;
                   2853:        fy = screen_hsize(data->backing) - data->oy + data->cy;
1.21      nicm     2854:
1.262     nicm     2855:        screen_init(&ss, screen_write_strlen("%s", str), 1, 0);
1.283     nicm     2856:        screen_write_start(&ctx, &ss);
1.262     nicm     2857:        screen_write_nputs(&ctx, -1, &grid_default_cell, "%s", str);
1.21      nicm     2858:        screen_write_stop(&ctx);
                   2859:
1.150     nicm     2860:        wrapflag = options_get_number(wp->window->options, "wrap-search");
1.262     nicm     2861:        cis = window_copy_is_lowercase(str);
1.21      nicm     2862:
1.230     nicm     2863:        if (direction) {
                   2864:                window_copy_move_right(s, &fx, &fy, wrapflag);
1.150     nicm     2865:                endline = gd->hsize + gd->sy - 1;
1.230     nicm     2866:        } else {
                   2867:                window_copy_move_left(s, &fx, &fy, wrapflag);
1.150     nicm     2868:                endline = 0;
1.230     nicm     2869:        }
1.244     nicm     2870:
1.208     nicm     2871:        found = window_copy_search_jump(wme, gd, ss.grid, fx, fy, endline, cis,
1.244     nicm     2872:            wrapflag, direction, regex);
1.162     nicm     2873:
1.244     nicm     2874:        if (window_copy_search_marks(wme, &ss, regex))
1.208     nicm     2875:                window_copy_redraw_screen(wme);
1.21      nicm     2876:
1.150     nicm     2877:        screen_free(&ss);
1.163     nicm     2878:        return (found);
1.150     nicm     2879: }
1.97      nicm     2880:
1.284     nicm     2881: static uint64_t
                   2882: window_copy_get_time(void)
                   2883: {
                   2884:        struct timeval  tv;
                   2885:
                   2886:        gettimeofday(&tv, NULL);
                   2887:        return ((tv.tv_sec * 1000ULL) + (tv.tv_usec / 1000ULL));
                   2888: }
                   2889:
1.162     nicm     2890: static int
1.244     nicm     2891: window_copy_search_marks(struct window_mode_entry *wme, struct screen *ssp,
                   2892:     int regex)
1.162     nicm     2893: {
1.208     nicm     2894:        struct window_copy_mode_data    *data = wme->data;
1.162     nicm     2895:        struct screen                   *s = data->backing, ss;
                   2896:        struct screen_write_ctx          ctx;
                   2897:        struct grid                     *gd = s->grid;
1.264     nicm     2898:        const struct grid_line          *gl;
1.284     nicm     2899:        int                              found, cis, which = -1, stopped = 0;
1.260     nicm     2900:        int                              cflags = REG_EXTENDED;
1.282     nicm     2901:        u_int                            px, py, i, b, nfound = 0, width;
1.284     nicm     2902:        u_int                            ssize = 1, start, end;
1.260     nicm     2903:        char                            *sbuf;
                   2904:        regex_t                          reg;
1.284     nicm     2905:        uint64_t                         stop = 0, tstart, t;
1.162     nicm     2906:
                   2907:        if (ssp == NULL) {
                   2908:                width = screen_write_strlen("%s", data->searchstr);
                   2909:                screen_init(&ss, width, 1, 0);
1.283     nicm     2910:                screen_write_start(&ctx, &ss);
1.162     nicm     2911:                screen_write_nputs(&ctx, -1, &grid_default_cell, "%s",
                   2912:                    data->searchstr);
                   2913:                screen_write_stop(&ctx);
                   2914:                ssp = &ss;
                   2915:        } else
                   2916:                width = screen_size_x(ssp);
                   2917:
                   2918:        cis = window_copy_is_lowercase(data->searchstr);
                   2919:
1.260     nicm     2920:        if (regex) {
                   2921:                sbuf = xmalloc(ssize);
                   2922:                sbuf[0] = '\0';
                   2923:                sbuf = window_copy_stringify(ssp->grid, 0, 0, ssp->grid->sx,
                   2924:                    sbuf, &ssize);
                   2925:                if (cis)
                   2926:                        cflags |= REG_ICASE;
                   2927:                if (regcomp(&reg, sbuf, cflags) != 0) {
                   2928:                        free(sbuf);
                   2929:                        return (0);
                   2930:                }
                   2931:        }
1.284     nicm     2932:        tstart = window_copy_get_time();
                   2933:
                   2934:        start = 0;
                   2935:        end = gd->hsize + gd->sy;
                   2936:        stop = window_copy_get_time() + WINDOW_COPY_SEARCH_ALL_TIMEOUT;
                   2937:
                   2938: again:
                   2939:        free(data->searchmark);
                   2940:        data->searchmark = xcalloc(gd->hsize + gd->sy, gd->sx);
                   2941:        data->searchgen = 1;
                   2942:
                   2943:        for (py = start; py < end; py++) {
1.162     nicm     2944:                px = 0;
                   2945:                for (;;) {
1.244     nicm     2946:                        if (regex) {
                   2947:                                found = window_copy_search_lr_regex(gd,
1.260     nicm     2948:                                    &px, &width, py, px, gd->sx, &reg);
1.244     nicm     2949:                                if (!found)
                   2950:                                        break;
1.260     nicm     2951:                        } else {
1.244     nicm     2952:                                found = window_copy_search_lr(gd, ssp->grid,
1.260     nicm     2953:                                    &px, py, px, gd->sx, cis);
1.244     nicm     2954:                                if (!found)
                   2955:                                        break;
                   2956:                        }
1.170     nicm     2957:
1.162     nicm     2958:                        nfound++;
1.260     nicm     2959:                        if (px == data->cx &&
                   2960:                            py == gd->hsize + data->cy - data->oy)
1.170     nicm     2961:                                which = nfound;
1.162     nicm     2962:
                   2963:                        b = (py * gd->sx) + px;
1.282     nicm     2964:                        for (i = b; i < b + width; i++)
                   2965:                                data->searchmark[i] = data->searchgen;
                   2966:                        if (data->searchgen == UCHAR_MAX)
                   2967:                                data->searchgen = 1;
                   2968:                        else
                   2969:                                data->searchgen++;
1.162     nicm     2970:
                   2971:                        px++;
                   2972:                }
1.263     nicm     2973:
1.284     nicm     2974:                t = window_copy_get_time();
1.263     nicm     2975:                if (t - tstart > WINDOW_COPY_SEARCH_TIMEOUT) {
                   2976:                        data->timeout = 1;
                   2977:                        break;
                   2978:                }
1.284     nicm     2979:                if (stop != 0 && t > stop) {
                   2980:                        stopped = 1;
                   2981:                        break;
                   2982:                }
1.162     nicm     2983:        }
1.263     nicm     2984:        if (data->timeout) {
                   2985:                window_copy_clear_marks(wme);
1.284     nicm     2986:                goto out;
                   2987:        }
                   2988:
                   2989:        if (stopped && stop != 0) {
                   2990:                /* Try again but just the visible context. */
                   2991:                for (start = gd->hsize - data->oy; start > 0; start--) {
                   2992:                        gl = grid_peek_line(gd, start - 1);
                   2993:                        if (~gl->flags & GRID_LINE_WRAPPED)
                   2994:                                break;
                   2995:                }
                   2996:                end = gd->hsize - data->oy + gd->sy;
                   2997:                stop = 0;
                   2998:                goto again;
1.263     nicm     2999:        }
1.162     nicm     3000:
1.284     nicm     3001:        if (stopped) {
1.170     nicm     3002:                data->searchthis = -1;
1.284     nicm     3003:                if (nfound > 1000)
                   3004:                        data->searchcount = 1000;
                   3005:                else if (nfound > 100)
                   3006:                        data->searchcount = 100;
                   3007:                else if (nfound > 10)
                   3008:                        data->searchcount = 10;
                   3009:                else
                   3010:                        data->searchcount = -1;
                   3011:                data->searchmore = 1;
                   3012:        } else {
                   3013:                if (which != -1)
                   3014:                        data->searchthis = 1 + nfound - which;
                   3015:                else
                   3016:                        data->searchthis = -1;
                   3017:                data->searchcount = nfound;
                   3018:                data->searchmore = 0;
                   3019:        }
1.170     nicm     3020:
1.284     nicm     3021: out:
1.162     nicm     3022:        if (ssp == &ss)
                   3023:                screen_free(&ss);
1.284     nicm     3024:        if (regex) {
                   3025:                free(sbuf);
                   3026:                regfree(&reg);
                   3027:        }
1.263     nicm     3028:        return (1);
1.162     nicm     3029: }
                   3030:
1.157     nicm     3031: static void
1.208     nicm     3032: window_copy_clear_marks(struct window_mode_entry *wme)
1.163     nicm     3033: {
1.208     nicm     3034:        struct window_copy_mode_data    *data = wme->data;
1.163     nicm     3035:
                   3036:        free(data->searchmark);
                   3037:        data->searchmark = NULL;
                   3038: }
                   3039:
                   3040: static int
1.244     nicm     3041: window_copy_search_up(struct window_mode_entry *wme, int regex)
1.150     nicm     3042: {
1.244     nicm     3043:        return (window_copy_search(wme, 0, regex));
1.150     nicm     3044: }
1.21      nicm     3045:
1.163     nicm     3046: static int
1.244     nicm     3047: window_copy_search_down(struct window_mode_entry *wme, int regex)
1.150     nicm     3048: {
1.244     nicm     3049:        return (window_copy_search(wme, 1, regex));
1.21      nicm     3050: }
                   3051:
1.157     nicm     3052: static void
1.208     nicm     3053: window_copy_goto_line(struct window_mode_entry *wme, const char *linestr)
1.21      nicm     3054: {
1.208     nicm     3055:        struct window_copy_mode_data    *data = wme->data;
1.21      nicm     3056:        const char                      *errstr;
1.199     nicm     3057:        int                              lineno;
1.21      nicm     3058:
1.199     nicm     3059:        lineno = strtonum(linestr, -1, INT_MAX, &errstr);
1.21      nicm     3060:        if (errstr != NULL)
                   3061:                return;
1.199     nicm     3062:        if (lineno < 0 || (u_int)lineno > screen_hsize(data->backing))
                   3063:                lineno = screen_hsize(data->backing);
1.35      nicm     3064:
1.21      nicm     3065:        data->oy = lineno;
1.252     nicm     3066:        window_copy_update_selection(wme, 1, 0);
1.208     nicm     3067:        window_copy_redraw_screen(wme);
1.21      nicm     3068: }
                   3069:
1.157     nicm     3070: static void
1.282     nicm     3071: window_copy_match_start_end(struct window_copy_mode_data *data, u_int at,
                   3072:     u_int *start, u_int *end)
                   3073: {
                   3074:        struct grid     *gd = data->backing->grid;
                   3075:        u_int            last = (gd->hsize + gd->sy) * gd->sx - 1;
                   3076:        u_char           mark = data->searchmark[at];
                   3077:
                   3078:        *start = *end = at;
                   3079:        while (*start != 0 && data->searchmark[*start] == mark)
                   3080:                (*start)--;
                   3081:        if (data->searchmark[*start] != mark)
                   3082:                (*start)++;
                   3083:        while (*end != last && data->searchmark[*end] == mark)
                   3084:                (*end)++;
                   3085:        if (data->searchmark[*end] != mark)
                   3086:                (*end)--;
                   3087: }
                   3088:
                   3089: static char *
                   3090: window_copy_match_at_cursor(struct window_copy_mode_data *data)
                   3091: {
                   3092:        struct grid     *gd = data->backing->grid;
                   3093:        struct grid_cell gc;
                   3094:        u_int            at, start, end, cy, px, py;
                   3095:        u_int            sx = screen_size_x(data->backing);
                   3096:        char            *buf = NULL;
                   3097:        size_t           len = 0;
                   3098:
                   3099:        if (data->searchmark == NULL)
                   3100:                return (NULL);
                   3101:
                   3102:        cy = screen_hsize(data->backing) - data->oy + data->cy;
                   3103:        at = (cy * sx) + data->cx;
                   3104:        if (data->searchmark[at] == 0)
                   3105:                return (NULL);
                   3106:        window_copy_match_start_end(data, at, &start, &end);
                   3107:
                   3108:        /*
                   3109:         * Cells will not be set in the marked array unless they are valid text
                   3110:         * and wrapping will be taken care of, so we can just copy.
                   3111:         */
                   3112:        for (at = start; at <= end; at++) {
                   3113:                py = at / sx;
                   3114:                px = at % (py * sx);
                   3115:
                   3116:                grid_get_cell(gd, px, py, &gc);
                   3117:                buf = xrealloc(buf, len + gc.data.size + 1);
                   3118:                memcpy(buf + len, gc.data.data, gc.data.size);
                   3119:                len += gc.data.size;
                   3120:        }
                   3121:        if (len != 0)
                   3122:                buf[len] = '\0';
                   3123:        return (buf);
                   3124: }
                   3125:
                   3126: static void
                   3127: window_copy_update_style(struct window_mode_entry *wme, u_int fx, u_int fy,
                   3128:     struct grid_cell *gc, const struct grid_cell *mgc,
                   3129:     const struct grid_cell *cgc)
                   3130: {
                   3131:        struct window_copy_mode_data    *data = wme->data;
1.284     nicm     3132:        u_int                            mark, start, end, cy, cursor, current;
1.282     nicm     3133:        u_int                            sx = screen_size_x(data->backing);
                   3134:
                   3135:        if (data->searchmark == NULL)
                   3136:                return;
1.284     nicm     3137:
                   3138:        current = (fy * sx) + fx;
                   3139:
                   3140:        mark = data->searchmark[current];
1.282     nicm     3141:        if (mark == 0)
                   3142:                return;
                   3143:
                   3144:        cy = screen_hsize(data->backing) - data->oy + data->cy;
1.284     nicm     3145:        cursor = (cy * sx) + data->cx;
                   3146:        if (data->searchmark[cursor] == mark) {
                   3147:                window_copy_match_start_end(data, cursor, &start, &end);
                   3148:                if (current >= start && current <= end) {
1.282     nicm     3149:                        gc->attr = cgc->attr;
                   3150:                        gc->fg = cgc->fg;
                   3151:                        gc->bg = cgc->bg;
1.284     nicm     3152:                        return;
1.282     nicm     3153:                }
                   3154:        }
                   3155:
                   3156:        gc->attr = mgc->attr;
                   3157:        gc->fg = mgc->fg;
                   3158:        gc->bg = mgc->bg;
                   3159: }
                   3160:
                   3161: static void
                   3162: window_copy_write_one(struct window_mode_entry *wme,
                   3163:     struct screen_write_ctx *ctx, u_int py, u_int fy, u_int nx,
                   3164:     const struct grid_cell *mgc, const struct grid_cell *cgc)
                   3165: {
                   3166:        struct window_copy_mode_data    *data = wme->data;
                   3167:        struct grid                     *gd = data->backing->grid;
                   3168:        struct grid_cell                 gc;
                   3169:        u_int                            fx;
                   3170:
                   3171:        screen_write_cursormove(ctx, 0, py, 0);
                   3172:        for (fx = 0; fx < nx; fx++) {
                   3173:                grid_get_cell(gd, fx, fy, &gc);
                   3174:                if (fx + gc.data.width <= nx) {
                   3175:                        window_copy_update_style(wme, fx, fy, &gc, mgc, cgc);
                   3176:                        screen_write_cell(ctx, &gc);
                   3177:                }
                   3178:        }
                   3179: }
                   3180:
                   3181: static void
1.208     nicm     3182: window_copy_write_line(struct window_mode_entry *wme,
                   3183:     struct screen_write_ctx *ctx, u_int py)
1.1       nicm     3184: {
1.208     nicm     3185:        struct window_pane              *wp = wme->wp;
                   3186:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     3187:        struct screen                   *s = &data->screen;
1.136     nicm     3188:        struct options                  *oo = wp->window->options;
1.282     nicm     3189:        struct grid_cell                 gc, mgc, cgc;
1.100     nicm     3190:        char                             hdr[512];
1.162     nicm     3191:        size_t                           size = 0;
1.270     nicm     3192:        u_int                            hsize = screen_hsize(data->backing);
1.21      nicm     3193:
1.281     nicm     3194:        style_apply(&gc, oo, "mode-style", NULL);
1.164     nicm     3195:        gc.flags |= GRID_FLAG_NOPALETTE;
1.282     nicm     3196:        style_apply(&mgc, oo, "copy-mode-match-style", NULL);
                   3197:        mgc.flags |= GRID_FLAG_NOPALETTE;
                   3198:        style_apply(&cgc, oo, "copy-mode-current-match-style", NULL);
                   3199:        cgc.flags |= GRID_FLAG_NOPALETTE;
1.1       nicm     3200:
1.249     nicm     3201:        if (py == 0 && s->rupper < s->rlower && !data->hide_position) {
1.170     nicm     3202:                if (data->searchmark == NULL) {
1.263     nicm     3203:                        if (data->timeout) {
                   3204:                                size = xsnprintf(hdr, sizeof hdr,
1.270     nicm     3205:                                    "(timed out) [%u/%u]", data->oy, hsize);
1.263     nicm     3206:                        } else {
                   3207:                                size = xsnprintf(hdr, sizeof hdr,
1.270     nicm     3208:                                    "[%u/%u]", data->oy, hsize);
1.263     nicm     3209:                        }
1.170     nicm     3210:                } else {
1.284     nicm     3211:                        if (data->searchcount == -1) {
                   3212:                                size = xsnprintf(hdr, sizeof hdr,
                   3213:                                    "[%u/%u]", data->oy, hsize);
                   3214:                        } else if (data->searchthis == -1) {
1.170     nicm     3215:                                size = xsnprintf(hdr, sizeof hdr,
1.284     nicm     3216:                                    "(%d%s results) [%u/%u]", data->searchcount,
                   3217:                                    data->searchmore ? "+" : "", data->oy, hsize);
1.170     nicm     3218:                        } else {
                   3219:                                size = xsnprintf(hdr, sizeof hdr,
1.284     nicm     3220:                                    "(%d/%d results) [%u/%u]", data->searchthis,
1.270     nicm     3221:                                    data->searchcount, data->oy, hsize);
1.170     nicm     3222:                        }
                   3223:                }
1.62      nicm     3224:                if (size > screen_size_x(s))
                   3225:                        size = screen_size_x(s);
1.212     nicm     3226:                screen_write_cursormove(ctx, screen_size_x(s) - size, 0, 0);
1.1       nicm     3227:                screen_write_puts(ctx, &gc, "%s", hdr);
                   3228:        } else
                   3229:                size = 0;
                   3230:
1.105     nicm     3231:        if (size < screen_size_x(s)) {
1.282     nicm     3232:                window_copy_write_one(wme, ctx, py, hsize - data->oy + py,
                   3233:                    screen_size_x(s) - size, &mgc, &cgc);
1.105     nicm     3234:        }
1.18      nicm     3235:
                   3236:        if (py == data->cy && data->cx == screen_size_x(s)) {
1.212     nicm     3237:                screen_write_cursormove(ctx, screen_size_x(s) - 1, py, 0);
1.282     nicm     3238:                screen_write_putc(ctx, &grid_default_cell, '$');
1.18      nicm     3239:        }
1.1       nicm     3240: }
                   3241:
1.157     nicm     3242: static void
1.208     nicm     3243: window_copy_write_lines(struct window_mode_entry *wme,
                   3244:     struct screen_write_ctx *ctx, u_int py, u_int ny)
1.1       nicm     3245: {
                   3246:        u_int   yy;
                   3247:
                   3248:        for (yy = py; yy < py + ny; yy++)
1.208     nicm     3249:                window_copy_write_line(wme, ctx, py);
1.121     nicm     3250: }
                   3251:
1.157     nicm     3252: static void
1.208     nicm     3253: window_copy_redraw_selection(struct window_mode_entry *wme, u_int old_y)
1.121     nicm     3254: {
1.208     nicm     3255:        struct window_copy_mode_data    *data = wme->data;
1.247     nicm     3256:        struct grid                     *gd = data->backing->grid;
1.121     nicm     3257:        u_int                            new_y, start, end;
                   3258:
                   3259:        new_y = data->cy;
                   3260:        if (old_y <= new_y) {
                   3261:                start = old_y;
                   3262:                end = new_y;
                   3263:        } else {
                   3264:                start = new_y;
                   3265:                end = old_y;
                   3266:        }
1.247     nicm     3267:
                   3268:        /*
                   3269:         * In word selection mode the first word on the line below the cursor
                   3270:         * might be selected, so add this line to the redraw area.
                   3271:         */
                   3272:        if (data->selflag == SEL_WORD) {
                   3273:                /* Last grid line in data coordinates. */
                   3274:                if (end < gd->sy + data->oy - 1)
                   3275:                        end++;
                   3276:        }
1.208     nicm     3277:        window_copy_redraw_lines(wme, start, end - start + 1);
1.1       nicm     3278: }
                   3279:
1.157     nicm     3280: static void
1.208     nicm     3281: window_copy_redraw_lines(struct window_mode_entry *wme, u_int py, u_int ny)
1.1       nicm     3282: {
1.208     nicm     3283:        struct window_pane              *wp = wme->wp;
                   3284:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     3285:        struct screen_write_ctx          ctx;
                   3286:        u_int                            i;
                   3287:
1.283     nicm     3288:        screen_write_start_pane(&ctx, wp, NULL);
1.1       nicm     3289:        for (i = py; i < py + ny; i++)
1.208     nicm     3290:                window_copy_write_line(wme, &ctx, i);
1.212     nicm     3291:        screen_write_cursormove(&ctx, data->cx, data->cy, 0);
1.1       nicm     3292:        screen_write_stop(&ctx);
                   3293: }
                   3294:
1.157     nicm     3295: static void
1.208     nicm     3296: window_copy_redraw_screen(struct window_mode_entry *wme)
1.1       nicm     3297: {
1.208     nicm     3298:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     3299:
1.208     nicm     3300:        window_copy_redraw_lines(wme, 0, screen_size_y(&data->screen));
1.1       nicm     3301: }
                   3302:
1.157     nicm     3303: static void
1.252     nicm     3304: window_copy_synchronize_cursor_end(struct window_mode_entry *wme, int begin,
                   3305:     int no_reset)
1.161     nicm     3306: {
1.208     nicm     3307:        struct window_copy_mode_data    *data = wme->data;
1.161     nicm     3308:        u_int                            xx, yy;
                   3309:
                   3310:        yy = screen_hsize(data->backing) + data->cy - data->oy;
1.247     nicm     3311:        switch (data->selflag) {
                   3312:        case SEL_WORD:
                   3313:                xx = data->cx;
1.252     nicm     3314:                if (no_reset)
1.247     nicm     3315:                        break;
1.252     nicm     3316:                begin = 0;
1.247     nicm     3317:                if (data->dy > yy || (data->dy == yy && data->dx > xx)) {
                   3318:                        /* Right to left selection. */
                   3319:                        window_copy_cursor_previous_word_pos(wme, data->ws, 0,
                   3320:                            &xx, &yy);
                   3321:                        begin = 1;
                   3322:
                   3323:                        /* Reset the end. */
                   3324:                        data->endselx = data->endselrx;
                   3325:                        data->endsely = data->endselry;
                   3326:                } else {
                   3327:                        /* Left to right selection. */
                   3328:                        if (xx >= window_copy_find_length(wme, yy) ||
                   3329:                            !window_copy_in_set(wme, xx + 1, yy, data->ws))
                   3330:                                window_copy_cursor_next_word_end_pos(wme,
                   3331:                                    data->ws, &xx, &yy);
                   3332:
                   3333:                        /* Reset the start. */
                   3334:                        data->selx = data->selrx;
                   3335:                        data->sely = data->selry;
                   3336:                }
                   3337:                break;
                   3338:        case SEL_LINE:
1.252     nicm     3339:                if (no_reset) {
                   3340:                        xx = data->cx;
                   3341:                        break;
                   3342:                }
1.251     nicm     3343:                begin = 0;
1.247     nicm     3344:                if (data->dy > yy) {
                   3345:                        /* Right to left selection. */
                   3346:                        xx = 0;
                   3347:                        begin = 1;
                   3348:
                   3349:                        /* Reset the end. */
                   3350:                        data->endselx = data->endselrx;
                   3351:                        data->endsely = data->endselry;
                   3352:                } else {
                   3353:                        /* Left to right selection. */
                   3354:                        xx = window_copy_find_length(wme, yy);
                   3355:
                   3356:                        /* Reset the start. */
                   3357:                        data->selx = data->selrx;
                   3358:                        data->sely = data->selry;
                   3359:                }
                   3360:                break;
                   3361:        case SEL_CHAR:
                   3362:                xx = data->cx;
                   3363:                break;
                   3364:        }
                   3365:        if (begin) {
                   3366:                data->selx = xx;
                   3367:                data->sely = yy;
                   3368:        } else {
                   3369:                data->endselx = xx;
                   3370:                data->endsely = yy;
                   3371:        }
                   3372: }
                   3373:
                   3374: static void
1.252     nicm     3375: window_copy_synchronize_cursor(struct window_mode_entry *wme, int no_reset)
1.247     nicm     3376: {
                   3377:        struct window_copy_mode_data    *data = wme->data;
1.161     nicm     3378:
                   3379:        switch (data->cursordrag) {
                   3380:        case CURSORDRAG_ENDSEL:
1.252     nicm     3381:                window_copy_synchronize_cursor_end(wme, 0, no_reset);
1.161     nicm     3382:                break;
                   3383:        case CURSORDRAG_SEL:
1.252     nicm     3384:                window_copy_synchronize_cursor_end(wme, 1, no_reset);
1.161     nicm     3385:                break;
                   3386:        case CURSORDRAG_NONE:
                   3387:                break;
                   3388:        }
                   3389: }
                   3390:
                   3391: static void
1.208     nicm     3392: window_copy_update_cursor(struct window_mode_entry *wme, u_int cx, u_int cy)
1.1       nicm     3393: {
1.208     nicm     3394:        struct window_pane              *wp = wme->wp;
                   3395:        struct window_copy_mode_data    *data = wme->data;
1.18      nicm     3396:        struct screen                   *s = &data->screen;
1.1       nicm     3397:        struct screen_write_ctx          ctx;
1.18      nicm     3398:        u_int                            old_cx, old_cy;
1.1       nicm     3399:
1.18      nicm     3400:        old_cx = data->cx; old_cy = data->cy;
                   3401:        data->cx = cx; data->cy = cy;
                   3402:        if (old_cx == screen_size_x(s))
1.208     nicm     3403:                window_copy_redraw_lines(wme, old_cy, 1);
1.18      nicm     3404:        if (data->cx == screen_size_x(s))
1.208     nicm     3405:                window_copy_redraw_lines(wme, data->cy, 1);
1.18      nicm     3406:        else {
1.283     nicm     3407:                screen_write_start_pane(&ctx, wp, NULL);
1.212     nicm     3408:                screen_write_cursormove(&ctx, data->cx, data->cy, 0);
1.18      nicm     3409:                screen_write_stop(&ctx);
                   3410:        }
1.1       nicm     3411: }
                   3412:
1.157     nicm     3413: static void
1.208     nicm     3414: window_copy_start_selection(struct window_mode_entry *wme)
1.1       nicm     3415: {
1.208     nicm     3416:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     3417:
1.18      nicm     3418:        data->selx = data->cx;
1.54      nicm     3419:        data->sely = screen_hsize(data->backing) + data->cy - data->oy;
1.1       nicm     3420:
1.161     nicm     3421:        data->endselx = data->selx;
                   3422:        data->endsely = data->sely;
                   3423:
                   3424:        data->cursordrag = CURSORDRAG_ENDSEL;
                   3425:
1.252     nicm     3426:        window_copy_set_selection(wme, 1, 0);
1.1       nicm     3427: }
                   3428:
1.157     nicm     3429: static int
1.208     nicm     3430: window_copy_adjust_selection(struct window_mode_entry *wme, u_int *selx,
                   3431:     u_int *sely)
1.161     nicm     3432: {
1.208     nicm     3433:        struct window_copy_mode_data    *data = wme->data;
1.161     nicm     3434:        struct screen                   *s = &data->screen;
                   3435:        u_int                            sx, sy, ty;
                   3436:        int                              relpos;
                   3437:
                   3438:        sx = *selx;
                   3439:        sy = *sely;
                   3440:
                   3441:        ty = screen_hsize(data->backing) - data->oy;
                   3442:        if (sy < ty) {
                   3443:                relpos = WINDOW_COPY_REL_POS_ABOVE;
                   3444:                if (!data->rectflag)
                   3445:                        sx = 0;
                   3446:                sy = 0;
                   3447:        } else if (sy > ty + screen_size_y(s) - 1) {
                   3448:                relpos = WINDOW_COPY_REL_POS_BELOW;
                   3449:                if (!data->rectflag)
                   3450:                        sx = screen_size_x(s) - 1;
                   3451:                sy = screen_size_y(s) - 1;
                   3452:        } else {
                   3453:                relpos = WINDOW_COPY_REL_POS_ON_SCREEN;
                   3454:                sy -= ty;
                   3455:        }
                   3456:
                   3457:        *selx = sx;
1.176     nicm     3458:        *sely = sy;
1.161     nicm     3459:        return (relpos);
                   3460: }
                   3461:
                   3462: static int
1.252     nicm     3463: window_copy_update_selection(struct window_mode_entry *wme, int may_redraw,
                   3464:     int no_reset)
1.1       nicm     3465: {
1.208     nicm     3466:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     3467:        struct screen                   *s = &data->screen;
1.192     nicm     3468:
                   3469:        if (s->sel == NULL && data->lineflag == LINE_SEL_NONE)
                   3470:                return (0);
1.252     nicm     3471:        return (window_copy_set_selection(wme, may_redraw, no_reset));
1.192     nicm     3472: }
                   3473:
                   3474: static int
1.252     nicm     3475: window_copy_set_selection(struct window_mode_entry *wme, int may_redraw,
                   3476:     int no_reset)
1.192     nicm     3477: {
1.208     nicm     3478:        struct window_pane              *wp = wme->wp;
                   3479:        struct window_copy_mode_data    *data = wme->data;
1.192     nicm     3480:        struct screen                   *s = &data->screen;
1.136     nicm     3481:        struct options                  *oo = wp->window->options;
1.1       nicm     3482:        struct grid_cell                 gc;
1.161     nicm     3483:        u_int                            sx, sy, cy, endsx, endsy;
                   3484:        int                              startrelpos, endrelpos;
1.1       nicm     3485:
1.252     nicm     3486:        window_copy_synchronize_cursor(wme, no_reset);
1.1       nicm     3487:
                   3488:        /* Adjust the selection. */
                   3489:        sx = data->selx;
                   3490:        sy = data->sely;
1.208     nicm     3491:        startrelpos = window_copy_adjust_selection(wme, &sx, &sy);
1.161     nicm     3492:
                   3493:        /* Adjust the end of selection. */
                   3494:        endsx = data->endselx;
                   3495:        endsy = data->endsely;
1.208     nicm     3496:        endrelpos = window_copy_adjust_selection(wme, &endsx, &endsy);
1.161     nicm     3497:
                   3498:        /* Selection is outside of the current screen */
                   3499:        if (startrelpos == endrelpos &&
                   3500:            startrelpos != WINDOW_COPY_REL_POS_ON_SCREEN) {
                   3501:                screen_hide_selection(s);
                   3502:                return (0);
                   3503:        }
1.1       nicm     3504:
1.161     nicm     3505:        /* Set colours and selection. */
1.281     nicm     3506:        style_apply(&gc, oo, "mode-style", NULL);
1.164     nicm     3507:        gc.flags |= GRID_FLAG_NOPALETTE;
1.192     nicm     3508:        screen_set_selection(s, sx, sy, endsx, endsy, data->rectflag,
                   3509:            data->modekeys, &gc);
1.42      nicm     3510:
1.96      nicm     3511:        if (data->rectflag && may_redraw) {
1.42      nicm     3512:                /*
                   3513:                 * Can't rely on the caller to redraw the right lines for
                   3514:                 * rectangle selection - find the highest line and the number
                   3515:                 * of lines, and redraw just past that in both directions
                   3516:                 */
                   3517:                cy = data->cy;
1.182     nicm     3518:                if (data->cursordrag == CURSORDRAG_ENDSEL) {
                   3519:                        if (sy < cy)
1.208     nicm     3520:                                window_copy_redraw_lines(wme, sy, cy - sy + 1);
1.182     nicm     3521:                        else
1.208     nicm     3522:                                window_copy_redraw_lines(wme, cy, sy - cy + 1);
1.182     nicm     3523:                } else {
1.208     nicm     3524:                        if (endsy < cy) {
                   3525:                                window_copy_redraw_lines(wme, endsy,
                   3526:                                    cy - endsy + 1);
                   3527:                        } else {
                   3528:                                window_copy_redraw_lines(wme, cy,
                   3529:                                    endsy - cy + 1);
                   3530:                        }
1.182     nicm     3531:                }
1.42      nicm     3532:        }
                   3533:
1.1       nicm     3534:        return (1);
                   3535: }
                   3536:
1.157     nicm     3537: static void *
1.208     nicm     3538: window_copy_get_selection(struct window_mode_entry *wme, size_t *len)
1.1       nicm     3539: {
1.208     nicm     3540:        struct window_pane              *wp = wme->wp;
                   3541:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     3542:        struct screen                   *s = &data->screen;
                   3543:        char                            *buf;
1.42      nicm     3544:        size_t                           off;
1.111     nicm     3545:        u_int                            i, xx, yy, sx, sy, ex, ey, ey_last;
1.188     nicm     3546:        u_int                            firstsx, lastex, restex, restsx, selx;
1.69      nicm     3547:        int                              keys;
1.1       nicm     3548:
1.282     nicm     3549:        if (data->screen.sel == NULL && data->lineflag == LINE_SEL_NONE) {
                   3550:                buf = window_copy_match_at_cursor(data);
                   3551:                if (buf != NULL)
                   3552:                        *len = strlen(buf);
                   3553:                return (buf);
                   3554:        }
1.1       nicm     3555:
                   3556:        buf = xmalloc(1);
                   3557:        off = 0;
                   3558:
                   3559:        *buf = '\0';
                   3560:
                   3561:        /*
                   3562:         * The selection extends from selx,sely to (adjusted) cx,cy on
                   3563:         * the base screen.
                   3564:         */
                   3565:
                   3566:        /* Find start and end. */
1.161     nicm     3567:        xx = data->endselx;
                   3568:        yy = data->endsely;
1.2       nicm     3569:        if (yy < data->sely || (yy == data->sely && xx < data->selx)) {
1.1       nicm     3570:                sx = xx; sy = yy;
                   3571:                ex = data->selx; ey = data->sely;
                   3572:        } else {
                   3573:                sx = data->selx; sy = data->sely;
                   3574:                ex = xx; ey = yy;
                   3575:        }
                   3576:
                   3577:        /* Trim ex to end of line. */
1.208     nicm     3578:        ey_last = window_copy_find_length(wme, ey);
1.111     nicm     3579:        if (ex > ey_last)
                   3580:                ex = ey_last;
1.1       nicm     3581:
1.42      nicm     3582:        /*
                   3583:         * Deal with rectangle-copy if necessary; four situations: start of
                   3584:         * first line (firstsx), end of last line (lastex), start (restsx) and
                   3585:         * end (restex) of all other lines.
                   3586:         */
                   3587:        xx = screen_size_x(s);
1.69      nicm     3588:
                   3589:        /*
                   3590:         * Behave according to mode-keys. If it is emacs, copy like emacs,
                   3591:         * keeping the top-left-most character, and dropping the
                   3592:         * bottom-right-most, regardless of copy direction. If it is vi, also
                   3593:         * keep bottom-right-most character.
                   3594:         */
1.136     nicm     3595:        keys = options_get_number(wp->window->options, "mode-keys");
1.42      nicm     3596:        if (data->rectflag) {
                   3597:                /*
                   3598:                 * Need to ignore the column with the cursor in it, which for
                   3599:                 * rectangular copy means knowing which side the cursor is on.
                   3600:                 */
1.188     nicm     3601:                if (data->cursordrag == CURSORDRAG_ENDSEL)
                   3602:                        selx = data->selx;
                   3603:                else
                   3604:                        selx = data->endselx;
                   3605:                if (selx < data->cx) {
1.42      nicm     3606:                        /* Selection start is on the left. */
1.69      nicm     3607:                        if (keys == MODEKEY_EMACS) {
                   3608:                                lastex = data->cx;
                   3609:                                restex = data->cx;
                   3610:                        }
                   3611:                        else {
                   3612:                                lastex = data->cx + 1;
                   3613:                                restex = data->cx + 1;
                   3614:                        }
1.188     nicm     3615:                        firstsx = selx;
                   3616:                        restsx = selx;
1.42      nicm     3617:                } else {
                   3618:                        /* Cursor is on the left. */
1.188     nicm     3619:                        lastex = selx + 1;
                   3620:                        restex = selx + 1;
1.64      nicm     3621:                        firstsx = data->cx;
                   3622:                        restsx = data->cx;
1.42      nicm     3623:                }
                   3624:        } else {
1.69      nicm     3625:                if (keys == MODEKEY_EMACS)
                   3626:                        lastex = ex;
1.74      nicm     3627:                else
1.69      nicm     3628:                        lastex = ex + 1;
1.42      nicm     3629:                restex = xx;
                   3630:                firstsx = sx;
                   3631:                restsx = 0;
                   3632:        }
                   3633:
1.1       nicm     3634:        /* Copy the lines. */
1.110     nicm     3635:        for (i = sy; i <= ey; i++) {
1.208     nicm     3636:                window_copy_copy_line(wme, &buf, &off, i,
1.110     nicm     3637:                    (i == sy ? firstsx : restsx),
                   3638:                    (i == ey ? lastex : restex));
1.1       nicm     3639:        }
                   3640:
1.26      nicm     3641:        /* Don't bother if no data. */
                   3642:        if (off == 0) {
1.81      nicm     3643:                free(buf);
1.89      nicm     3644:                return (NULL);
1.26      nicm     3645:        }
1.111     nicm     3646:        if (keys == MODEKEY_EMACS || lastex <= ey_last)
                   3647:                off -= 1; /* remove final \n (unless at end in vi mode) */
                   3648:        *len = off;
1.89      nicm     3649:        return (buf);
                   3650: }
                   3651:
1.157     nicm     3652: static void
1.215     nicm     3653: window_copy_copy_buffer(struct window_mode_entry *wme, const char *prefix,
                   3654:     void *buf, size_t len)
1.89      nicm     3655: {
1.208     nicm     3656:        struct window_pane      *wp = wme->wp;
                   3657:        struct screen_write_ctx  ctx;
1.72      nicm     3658:
1.178     nicm     3659:        if (options_get_number(global_options, "set-clipboard") != 0) {
1.283     nicm     3660:                screen_write_start_pane(&ctx, wp, NULL);
1.91      nicm     3661:                screen_write_setselection(&ctx, buf, len);
                   3662:                screen_write_stop(&ctx);
1.179     nicm     3663:                notify_pane("pane-set-clipboard", wp);
1.91      nicm     3664:        }
1.1       nicm     3665:
1.215     nicm     3666:        paste_add(prefix, buf, len);
1.89      nicm     3667: }
                   3668:
1.157     nicm     3669: static void
1.208     nicm     3670: window_copy_copy_pipe(struct window_mode_entry *wme, struct session *s,
1.253     nicm     3671:     const char *prefix, const char *cmd)
1.89      nicm     3672: {
1.215     nicm     3673:        void            *buf;
                   3674:        size_t           len;
                   3675:        struct job      *job;
1.89      nicm     3676:
1.208     nicm     3677:        buf = window_copy_get_selection(wme, &len);
1.89      nicm     3678:        if (buf == NULL)
                   3679:                return;
                   3680:
1.277     nicm     3681:        if (cmd == NULL || *cmd == '\0')
                   3682:                cmd = options_get_string(global_options, "copy-command");
                   3683:        if (cmd != NULL && *cmd != '\0') {
                   3684:                job = job_run(cmd, s, NULL, NULL, NULL, NULL, NULL, JOB_NOWAIT,
                   3685:                    -1, -1);
                   3686:                bufferevent_write(job_get_event(job), buf, len);
                   3687:        }
1.215     nicm     3688:        window_copy_copy_buffer(wme, prefix, buf, len);
1.89      nicm     3689: }
                   3690:
1.157     nicm     3691: static void
1.215     nicm     3692: window_copy_copy_selection(struct window_mode_entry *wme, const char *prefix)
1.89      nicm     3693: {
1.203     nicm     3694:        char    *buf;
1.114     nicm     3695:        size_t   len;
1.89      nicm     3696:
1.208     nicm     3697:        buf = window_copy_get_selection(wme, &len);
1.203     nicm     3698:        if (buf != NULL)
1.215     nicm     3699:                window_copy_copy_buffer(wme, prefix, buf, len);
1.103     nicm     3700: }
                   3701:
1.157     nicm     3702: static void
1.208     nicm     3703: window_copy_append_selection(struct window_mode_entry *wme)
1.103     nicm     3704: {
1.208     nicm     3705:        struct window_pane              *wp = wme->wp;
1.108     nicm     3706:        char                            *buf;
                   3707:        struct paste_buffer             *pb;
1.227     nicm     3708:        const char                      *bufdata, *bufname = NULL;
1.132     nicm     3709:        size_t                           len, bufsize;
1.108     nicm     3710:        struct screen_write_ctx          ctx;
1.103     nicm     3711:
1.208     nicm     3712:        buf = window_copy_get_selection(wme, &len);
1.103     nicm     3713:        if (buf == NULL)
                   3714:                return;
                   3715:
1.178     nicm     3716:        if (options_get_number(global_options, "set-clipboard") != 0) {
1.283     nicm     3717:                screen_write_start_pane(&ctx, wp, NULL);
1.103     nicm     3718:                screen_write_setselection(&ctx, buf, len);
                   3719:                screen_write_stop(&ctx);
1.179     nicm     3720:                notify_pane("pane-set-clipboard", wp);
1.103     nicm     3721:        }
                   3722:
1.203     nicm     3723:        pb = paste_get_top(&bufname);
1.103     nicm     3724:        if (pb != NULL) {
1.132     nicm     3725:                bufdata = paste_buffer_data(pb, &bufsize);
                   3726:                buf = xrealloc(buf, len + bufsize);
                   3727:                memmove(buf + bufsize, buf, len);
                   3728:                memcpy(buf, bufdata, bufsize);
                   3729:                len += bufsize;
1.103     nicm     3730:        }
1.108     nicm     3731:        if (paste_set(buf, len, bufname, NULL) != 0)
1.103     nicm     3732:                free(buf);
1.1       nicm     3733: }
                   3734:
1.157     nicm     3735: static void
1.208     nicm     3736: window_copy_copy_line(struct window_mode_entry *wme, char **buf, size_t *off,
                   3737:     u_int sy, u_int sx, u_int ex)
1.1       nicm     3738: {
1.208     nicm     3739:        struct window_copy_mode_data    *data = wme->data;
1.54      nicm     3740:        struct grid                     *gd = data->backing->grid;
1.140     nicm     3741:        struct grid_cell                 gc;
1.54      nicm     3742:        struct grid_line                *gl;
1.86      nicm     3743:        struct utf8_data                 ud;
1.54      nicm     3744:        u_int                            i, xx, wrapped = 0;
1.115     nicm     3745:        const char                      *s;
1.1       nicm     3746:
                   3747:        if (sx > ex)
                   3748:                return;
                   3749:
1.16      nicm     3750:        /*
                   3751:         * Work out if the line was wrapped at the screen edge and all of it is
                   3752:         * on screen.
                   3753:         */
1.190     nicm     3754:        gl = grid_get_line(gd, sy);
1.35      nicm     3755:        if (gl->flags & GRID_LINE_WRAPPED && gl->cellsize <= gd->sx)
1.16      nicm     3756:                wrapped = 1;
                   3757:
                   3758:        /* If the line was wrapped, don't strip spaces (use the full length). */
                   3759:        if (wrapped)
                   3760:                xx = gl->cellsize;
                   3761:        else
1.208     nicm     3762:                xx = window_copy_find_length(wme, sy);
1.1       nicm     3763:        if (ex > xx)
                   3764:                ex = xx;
                   3765:        if (sx > xx)
                   3766:                sx = xx;
                   3767:
                   3768:        if (sx < ex) {
                   3769:                for (i = sx; i < ex; i++) {
1.140     nicm     3770:                        grid_get_cell(gd, i, sy, &gc);
                   3771:                        if (gc.flags & GRID_FLAG_PADDING)
1.1       nicm     3772:                                continue;
1.140     nicm     3773:                        utf8_copy(&ud, &gc.data);
                   3774:                        if (ud.size == 1 && (gc.attr & GRID_ATTR_CHARSET)) {
1.115     nicm     3775:                                s = tty_acs_get(NULL, ud.data[0]);
                   3776:                                if (s != NULL && strlen(s) <= sizeof ud.data) {
                   3777:                                        ud.size = strlen(s);
1.117     nicm     3778:                                        memcpy(ud.data, s, ud.size);
1.115     nicm     3779:                                }
                   3780:                        }
1.86      nicm     3781:
1.116     nicm     3782:                        *buf = xrealloc(*buf, (*off) + ud.size);
1.86      nicm     3783:                        memcpy(*buf + *off, ud.data, ud.size);
                   3784:                        *off += ud.size;
1.1       nicm     3785:                }
                   3786:        }
                   3787:
1.16      nicm     3788:        /* Only add a newline if the line wasn't wrapped. */
1.44      nicm     3789:        if (!wrapped || ex != xx) {
1.116     nicm     3790:                *buf = xrealloc(*buf, (*off) + 1);
1.16      nicm     3791:                (*buf)[(*off)++] = '\n';
                   3792:        }
1.1       nicm     3793: }
                   3794:
1.157     nicm     3795: static void
1.208     nicm     3796: window_copy_clear_selection(struct window_mode_entry *wme)
1.47      nicm     3797: {
1.208     nicm     3798:        struct window_copy_mode_data   *data = wme->data;
1.47      nicm     3799:        u_int                           px, py;
                   3800:
                   3801:        screen_clear_selection(&data->screen);
                   3802:
1.161     nicm     3803:        data->cursordrag = CURSORDRAG_NONE;
1.197     nicm     3804:        data->lineflag = LINE_SEL_NONE;
1.250     nicm     3805:        data->selflag = SEL_CHAR;
1.161     nicm     3806:
1.54      nicm     3807:        py = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm     3808:        px = window_copy_find_length(wme, py);
1.47      nicm     3809:        if (data->cx > px)
1.208     nicm     3810:                window_copy_update_cursor(wme, px, data->cy);
1.47      nicm     3811: }
                   3812:
1.157     nicm     3813: static int
1.208     nicm     3814: window_copy_in_set(struct window_mode_entry *wme, u_int px, u_int py,
                   3815:     const char *set)
1.1       nicm     3816: {
1.208     nicm     3817:        struct window_copy_mode_data    *data = wme->data;
1.140     nicm     3818:        struct grid_cell                 gc;
                   3819:
                   3820:        grid_get_cell(data->backing->grid, px, py, &gc);
1.204     nicm     3821:        if (gc.flags & GRID_FLAG_PADDING)
                   3822:                return (0);
1.224     nicm     3823:        return (utf8_cstrhas(set, &gc.data));
1.1       nicm     3824: }
                   3825:
1.157     nicm     3826: static u_int
1.208     nicm     3827: window_copy_find_length(struct window_mode_entry *wme, u_int py)
1.1       nicm     3828: {
1.208     nicm     3829:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     3830:
1.224     nicm     3831:        return (grid_line_length(data->backing->grid, py));
1.1       nicm     3832: }
                   3833:
1.157     nicm     3834: static void
1.208     nicm     3835: window_copy_cursor_start_of_line(struct window_mode_entry *wme)
1.5       nicm     3836: {
1.208     nicm     3837:        struct window_copy_mode_data    *data = wme->data;
1.58      nicm     3838:        struct screen                   *back_s = data->backing;
                   3839:        struct grid                     *gd = back_s->grid;
                   3840:        u_int                            py;
                   3841:
1.192     nicm     3842:        if (data->cx == 0 && data->lineflag == LINE_SEL_NONE) {
1.58      nicm     3843:                py = screen_hsize(back_s) + data->cy - data->oy;
1.118     nicm     3844:                while (py > 0 &&
1.190     nicm     3845:                    grid_get_line(gd, py - 1)->flags & GRID_LINE_WRAPPED) {
1.208     nicm     3846:                        window_copy_cursor_up(wme, 0);
1.58      nicm     3847:                        py = screen_hsize(back_s) + data->cy - data->oy;
                   3848:                }
                   3849:        }
1.208     nicm     3850:        window_copy_update_cursor(wme, 0, data->cy);
1.252     nicm     3851:        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     3852:                window_copy_redraw_lines(wme, data->cy, 1);
1.6       nicm     3853: }
                   3854:
1.157     nicm     3855: static void
1.208     nicm     3856: window_copy_cursor_back_to_indentation(struct window_mode_entry *wme)
1.6       nicm     3857: {
1.208     nicm     3858:        struct window_copy_mode_data    *data = wme->data;
1.6       nicm     3859:        u_int                            px, py, xx;
1.140     nicm     3860:        struct grid_cell                 gc;
1.6       nicm     3861:
                   3862:        px = 0;
1.54      nicm     3863:        py = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm     3864:        xx = window_copy_find_length(wme, py);
1.6       nicm     3865:
                   3866:        while (px < xx) {
1.140     nicm     3867:                grid_get_cell(data->backing->grid, px, py, &gc);
                   3868:                if (gc.data.size != 1 || *gc.data.data != ' ')
1.6       nicm     3869:                        break;
                   3870:                px++;
                   3871:        }
                   3872:
1.208     nicm     3873:        window_copy_update_cursor(wme, px, data->cy);
1.252     nicm     3874:        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     3875:                window_copy_redraw_lines(wme, data->cy, 1);
1.5       nicm     3876: }
                   3877:
1.157     nicm     3878: static void
1.208     nicm     3879: window_copy_cursor_end_of_line(struct window_mode_entry *wme)
1.5       nicm     3880: {
1.208     nicm     3881:        struct window_copy_mode_data    *data = wme->data;
1.54      nicm     3882:        struct screen                   *back_s = data->backing;
                   3883:        struct grid                     *gd = back_s->grid;
1.190     nicm     3884:        struct grid_line                *gl;
1.5       nicm     3885:        u_int                            px, py;
                   3886:
1.54      nicm     3887:        py = screen_hsize(back_s) + data->cy - data->oy;
1.208     nicm     3888:        px = window_copy_find_length(wme, py);
1.5       nicm     3889:
1.192     nicm     3890:        if (data->cx == px && data->lineflag == LINE_SEL_NONE) {
                   3891:                if (data->screen.sel != NULL && data->rectflag)
1.54      nicm     3892:                        px = screen_size_x(back_s);
1.190     nicm     3893:                gl = grid_get_line(gd, py);
                   3894:                if (gl->flags & GRID_LINE_WRAPPED) {
                   3895:                        while (py < gd->sy + gd->hsize) {
                   3896:                                gl = grid_get_line(gd, py);
                   3897:                                if (~gl->flags & GRID_LINE_WRAPPED)
                   3898:                                        break;
1.208     nicm     3899:                                window_copy_cursor_down(wme, 0);
1.190     nicm     3900:                                py = screen_hsize(back_s) + data->cy - data->oy;
1.49      nicm     3901:                        }
1.208     nicm     3902:                        px = window_copy_find_length(wme, py);
1.49      nicm     3903:                }
                   3904:        }
1.208     nicm     3905:        window_copy_update_cursor(wme, px, data->cy);
1.49      nicm     3906:
1.252     nicm     3907:        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     3908:                window_copy_redraw_lines(wme, data->cy, 1);
1.95      nicm     3909: }
                   3910:
1.157     nicm     3911: static void
1.208     nicm     3912: window_copy_other_end(struct window_mode_entry *wme)
1.95      nicm     3913: {
1.208     nicm     3914:        struct window_copy_mode_data    *data = wme->data;
1.95      nicm     3915:        struct screen                   *s = &data->screen;
1.161     nicm     3916:        u_int                            selx, sely, cy, yy, hsize;
1.95      nicm     3917:
1.192     nicm     3918:        if (s->sel == NULL && data->lineflag == LINE_SEL_NONE)
1.95      nicm     3919:                return;
                   3920:
1.192     nicm     3921:        if (data->lineflag == LINE_SEL_LEFT_RIGHT)
                   3922:                data->lineflag = LINE_SEL_RIGHT_LEFT;
                   3923:        else if (data->lineflag == LINE_SEL_RIGHT_LEFT)
                   3924:                data->lineflag = LINE_SEL_LEFT_RIGHT;
1.118     nicm     3925:
1.161     nicm     3926:        switch (data->cursordrag) {
                   3927:                case CURSORDRAG_NONE:
                   3928:                case CURSORDRAG_SEL:
                   3929:                        data->cursordrag = CURSORDRAG_ENDSEL;
                   3930:                        break;
                   3931:                case CURSORDRAG_ENDSEL:
                   3932:                        data->cursordrag = CURSORDRAG_SEL;
                   3933:                        break;
                   3934:        }
                   3935:
                   3936:        selx = data->endselx;
                   3937:        sely = data->endsely;
                   3938:        if (data->cursordrag == CURSORDRAG_SEL) {
                   3939:                selx = data->selx;
                   3940:                sely = data->sely;
                   3941:        }
                   3942:
1.95      nicm     3943:        cy = data->cy;
                   3944:        yy = screen_hsize(data->backing) + data->cy - data->oy;
                   3945:
                   3946:        data->cx = selx;
                   3947:
1.122     nicm     3948:        hsize = screen_hsize(data->backing);
1.161     nicm     3949:        if (sely < hsize - data->oy) { /* above */
1.122     nicm     3950:                data->oy = hsize - sely;
1.95      nicm     3951:                data->cy = 0;
1.161     nicm     3952:        } else if (sely > hsize - data->oy + screen_size_y(s)) { /* below */
1.122     nicm     3953:                data->oy = hsize - sely + screen_size_y(s) - 1;
1.95      nicm     3954:                data->cy = screen_size_y(s) - 1;
                   3955:        } else
                   3956:                data->cy = cy + sely - yy;
                   3957:
1.252     nicm     3958:        window_copy_update_selection(wme, 1, 1);
1.208     nicm     3959:        window_copy_redraw_screen(wme);
1.5       nicm     3960: }
                   3961:
1.157     nicm     3962: static void
1.208     nicm     3963: window_copy_cursor_left(struct window_mode_entry *wme)
1.1       nicm     3964: {
1.208     nicm     3965:        struct window_copy_mode_data    *data = wme->data;
1.168     nicm     3966:        u_int                            py, cx;
                   3967:        struct grid_cell                 gc;
1.1       nicm     3968:
1.145     nicm     3969:        py = screen_hsize(data->backing) + data->cy - data->oy;
1.168     nicm     3970:        cx = data->cx;
                   3971:        while (cx > 0) {
                   3972:                grid_get_cell(data->backing->grid, cx, py, &gc);
                   3973:                if (~gc.flags & GRID_FLAG_PADDING)
                   3974:                        break;
                   3975:                cx--;
                   3976:        }
                   3977:        if (cx == 0 && py > 0) {
1.208     nicm     3978:                window_copy_cursor_up(wme, 0);
                   3979:                window_copy_cursor_end_of_line(wme);
1.168     nicm     3980:        } else if (cx > 0) {
1.208     nicm     3981:                window_copy_update_cursor(wme, cx - 1, data->cy);
1.252     nicm     3982:                if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     3983:                        window_copy_redraw_lines(wme, data->cy, 1);
1.1       nicm     3984:        }
                   3985: }
                   3986:
1.157     nicm     3987: static void
1.208     nicm     3988: window_copy_cursor_right(struct window_mode_entry *wme)
1.1       nicm     3989: {
1.208     nicm     3990:        struct window_copy_mode_data    *data = wme->data;
1.168     nicm     3991:        u_int                            px, py, yy, cx, cy;
                   3992:        struct grid_cell                 gc;
1.1       nicm     3993:
1.145     nicm     3994:        py = screen_hsize(data->backing) + data->cy - data->oy;
                   3995:        yy = screen_hsize(data->backing) + screen_size_y(data->backing) - 1;
1.192     nicm     3996:        if (data->screen.sel != NULL && data->rectflag)
1.47      nicm     3997:                px = screen_size_x(&data->screen);
1.168     nicm     3998:        else
1.208     nicm     3999:                px = window_copy_find_length(wme, py);
1.1       nicm     4000:
1.145     nicm     4001:        if (data->cx >= px && py < yy) {
1.208     nicm     4002:                window_copy_cursor_start_of_line(wme);
                   4003:                window_copy_cursor_down(wme, 0);
1.145     nicm     4004:        } else if (data->cx < px) {
1.168     nicm     4005:                cx = data->cx + 1;
                   4006:                cy = screen_hsize(data->backing) + data->cy - data->oy;
                   4007:                while (cx < px) {
                   4008:                        grid_get_cell(data->backing->grid, cx, cy, &gc);
                   4009:                        if (~gc.flags & GRID_FLAG_PADDING)
                   4010:                                break;
                   4011:                        cx++;
                   4012:                }
1.208     nicm     4013:                window_copy_update_cursor(wme, cx, data->cy);
1.252     nicm     4014:                if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     4015:                        window_copy_redraw_lines(wme, data->cy, 1);
1.1       nicm     4016:        }
                   4017: }
                   4018:
1.157     nicm     4019: static void
1.208     nicm     4020: window_copy_cursor_up(struct window_mode_entry *wme, int scroll_only)
1.1       nicm     4021: {
1.208     nicm     4022:        struct window_copy_mode_data    *data = wme->data;
1.36      nicm     4023:        struct screen                   *s = &data->screen;
1.1       nicm     4024:        u_int                            ox, oy, px, py;
                   4025:
1.54      nicm     4026:        oy = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm     4027:        ox = window_copy_find_length(wme, oy);
1.77      nicm     4028:        if (data->cx != ox) {
1.25      nicm     4029:                data->lastcx = data->cx;
                   4030:                data->lastsx = ox;
                   4031:        }
1.1       nicm     4032:
1.192     nicm     4033:        if (data->lineflag == LINE_SEL_LEFT_RIGHT && oy == data->sely)
1.208     nicm     4034:                window_copy_other_end(wme);
1.118     nicm     4035:
1.28      nicm     4036:        if (scroll_only || data->cy == 0) {
1.226     nicm     4037:                data->cx = data->lastcx;
1.208     nicm     4038:                window_copy_scroll_down(wme, 1);
1.36      nicm     4039:                if (scroll_only) {
                   4040:                        if (data->cy == screen_size_y(s) - 1)
1.208     nicm     4041:                                window_copy_redraw_lines(wme, data->cy, 1);
1.36      nicm     4042:                        else
1.208     nicm     4043:                                window_copy_redraw_lines(wme, data->cy, 2);
1.36      nicm     4044:                }
1.28      nicm     4045:        } else {
1.226     nicm     4046:                window_copy_update_cursor(wme, data->lastcx, data->cy - 1);
1.252     nicm     4047:                if (window_copy_update_selection(wme, 1, 0)) {
1.36      nicm     4048:                        if (data->cy == screen_size_y(s) - 1)
1.208     nicm     4049:                                window_copy_redraw_lines(wme, data->cy, 1);
1.36      nicm     4050:                        else
1.208     nicm     4051:                                window_copy_redraw_lines(wme, data->cy, 2);
1.36      nicm     4052:                }
1.1       nicm     4053:        }
                   4054:
1.198     nicm     4055:        if (data->screen.sel == NULL || !data->rectflag) {
1.54      nicm     4056:                py = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm     4057:                px = window_copy_find_length(wme, py);
1.49      nicm     4058:                if ((data->cx >= data->lastsx && data->cx != px) ||
                   4059:                    data->cx > px)
1.208     nicm     4060:                        window_copy_cursor_end_of_line(wme);
1.47      nicm     4061:        }
1.118     nicm     4062:
1.192     nicm     4063:        if (data->lineflag == LINE_SEL_LEFT_RIGHT)
1.208     nicm     4064:                window_copy_cursor_end_of_line(wme);
1.192     nicm     4065:        else if (data->lineflag == LINE_SEL_RIGHT_LEFT)
1.208     nicm     4066:                window_copy_cursor_start_of_line(wme);
1.1       nicm     4067: }
                   4068:
1.157     nicm     4069: static void
1.208     nicm     4070: window_copy_cursor_down(struct window_mode_entry *wme, int scroll_only)
1.1       nicm     4071: {
1.208     nicm     4072:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     4073:        struct screen                   *s = &data->screen;
                   4074:        u_int                            ox, oy, px, py;
                   4075:
1.54      nicm     4076:        oy = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm     4077:        ox = window_copy_find_length(wme, oy);
1.77      nicm     4078:        if (data->cx != ox) {
1.25      nicm     4079:                data->lastcx = data->cx;
                   4080:                data->lastsx = ox;
                   4081:        }
1.1       nicm     4082:
1.192     nicm     4083:        if (data->lineflag == LINE_SEL_RIGHT_LEFT && oy == data->endsely)
1.208     nicm     4084:                window_copy_other_end(wme);
1.118     nicm     4085:
1.28      nicm     4086:        if (scroll_only || data->cy == screen_size_y(s) - 1) {
1.226     nicm     4087:                data->cx = data->lastcx;
1.208     nicm     4088:                window_copy_scroll_up(wme, 1);
1.31      nicm     4089:                if (scroll_only && data->cy > 0)
1.208     nicm     4090:                        window_copy_redraw_lines(wme, data->cy - 1, 2);
1.28      nicm     4091:        } else {
1.226     nicm     4092:                window_copy_update_cursor(wme, data->lastcx, data->cy + 1);
1.252     nicm     4093:                if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     4094:                        window_copy_redraw_lines(wme, data->cy - 1, 2);
1.1       nicm     4095:        }
                   4096:
1.192     nicm     4097:        if (data->screen.sel == NULL || !data->rectflag) {
1.54      nicm     4098:                py = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm     4099:                px = window_copy_find_length(wme, py);
1.49      nicm     4100:                if ((data->cx >= data->lastsx && data->cx != px) ||
                   4101:                    data->cx > px)
1.208     nicm     4102:                        window_copy_cursor_end_of_line(wme);
1.52      nicm     4103:        }
1.118     nicm     4104:
1.192     nicm     4105:        if (data->lineflag == LINE_SEL_LEFT_RIGHT)
1.208     nicm     4106:                window_copy_cursor_end_of_line(wme);
1.192     nicm     4107:        else if (data->lineflag == LINE_SEL_RIGHT_LEFT)
1.208     nicm     4108:                window_copy_cursor_start_of_line(wme);
1.52      nicm     4109: }
                   4110:
1.157     nicm     4111: static void
1.208     nicm     4112: window_copy_cursor_jump(struct window_mode_entry *wme)
1.52      nicm     4113: {
1.208     nicm     4114:        struct window_copy_mode_data    *data = wme->data;
1.54      nicm     4115:        struct screen                   *back_s = data->backing;
1.140     nicm     4116:        struct grid_cell                 gc;
1.67      nicm     4117:        u_int                            px, py, xx;
1.52      nicm     4118:
                   4119:        px = data->cx + 1;
1.54      nicm     4120:        py = screen_hsize(back_s) + data->cy - data->oy;
1.208     nicm     4121:        xx = window_copy_find_length(wme, py);
1.52      nicm     4122:
                   4123:        while (px < xx) {
1.140     nicm     4124:                grid_get_cell(back_s->grid, px, py, &gc);
                   4125:                if (!(gc.flags & GRID_FLAG_PADDING) &&
                   4126:                    gc.data.size == 1 && *gc.data.data == data->jumpchar) {
1.208     nicm     4127:                        window_copy_update_cursor(wme, px, data->cy);
1.252     nicm     4128:                        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     4129:                                window_copy_redraw_lines(wme, data->cy, 1);
1.52      nicm     4130:                        return;
                   4131:                }
                   4132:                px++;
                   4133:        }
                   4134: }
                   4135:
1.157     nicm     4136: static void
1.208     nicm     4137: window_copy_cursor_jump_back(struct window_mode_entry *wme)
1.52      nicm     4138: {
1.208     nicm     4139:        struct window_copy_mode_data    *data = wme->data;
1.54      nicm     4140:        struct screen                   *back_s = data->backing;
1.140     nicm     4141:        struct grid_cell                 gc;
1.67      nicm     4142:        u_int                            px, py;
1.52      nicm     4143:
                   4144:        px = data->cx;
1.54      nicm     4145:        py = screen_hsize(back_s) + data->cy - data->oy;
1.52      nicm     4146:
                   4147:        if (px > 0)
                   4148:                px--;
                   4149:
                   4150:        for (;;) {
1.140     nicm     4151:                grid_get_cell(back_s->grid, px, py, &gc);
                   4152:                if (!(gc.flags & GRID_FLAG_PADDING) &&
                   4153:                    gc.data.size == 1 && *gc.data.data == data->jumpchar) {
1.208     nicm     4154:                        window_copy_update_cursor(wme, px, data->cy);
1.252     nicm     4155:                        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     4156:                                window_copy_redraw_lines(wme, data->cy, 1);
1.76      nicm     4157:                        return;
                   4158:                }
                   4159:                if (px == 0)
                   4160:                        break;
                   4161:                px--;
                   4162:        }
                   4163: }
                   4164:
1.157     nicm     4165: static void
1.208     nicm     4166: window_copy_cursor_jump_to(struct window_mode_entry *wme)
1.76      nicm     4167: {
1.208     nicm     4168:        struct window_copy_mode_data    *data = wme->data;
1.76      nicm     4169:        struct screen                   *back_s = data->backing;
1.140     nicm     4170:        struct grid_cell                 gc;
1.76      nicm     4171:        u_int                            px, py, xx;
                   4172:
1.184     nicm     4173:        px = data->cx + 2;
1.76      nicm     4174:        py = screen_hsize(back_s) + data->cy - data->oy;
1.208     nicm     4175:        xx = window_copy_find_length(wme, py);
1.76      nicm     4176:
                   4177:        while (px < xx) {
1.140     nicm     4178:                grid_get_cell(back_s->grid, px, py, &gc);
                   4179:                if (!(gc.flags & GRID_FLAG_PADDING) &&
                   4180:                    gc.data.size == 1 && *gc.data.data == data->jumpchar) {
1.208     nicm     4181:                        window_copy_update_cursor(wme, px - 1, data->cy);
1.252     nicm     4182:                        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     4183:                                window_copy_redraw_lines(wme, data->cy, 1);
1.76      nicm     4184:                        return;
                   4185:                }
                   4186:                px++;
                   4187:        }
                   4188: }
                   4189:
1.157     nicm     4190: static void
1.208     nicm     4191: window_copy_cursor_jump_to_back(struct window_mode_entry *wme)
1.76      nicm     4192: {
1.208     nicm     4193:        struct window_copy_mode_data    *data = wme->data;
1.76      nicm     4194:        struct screen                   *back_s = data->backing;
1.140     nicm     4195:        struct grid_cell                 gc;
1.76      nicm     4196:        u_int                            px, py;
                   4197:
                   4198:        px = data->cx;
                   4199:        py = screen_hsize(back_s) + data->cy - data->oy;
                   4200:
                   4201:        if (px > 0)
1.127     nicm     4202:                px--;
                   4203:
1.184     nicm     4204:        if (px > 0)
1.76      nicm     4205:                px--;
                   4206:
                   4207:        for (;;) {
1.140     nicm     4208:                grid_get_cell(back_s->grid, px, py, &gc);
                   4209:                if (!(gc.flags & GRID_FLAG_PADDING) &&
                   4210:                    gc.data.size == 1 && *gc.data.data == data->jumpchar) {
1.208     nicm     4211:                        window_copy_update_cursor(wme, px + 1, data->cy);
1.252     nicm     4212:                        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     4213:                                window_copy_redraw_lines(wme, data->cy, 1);
1.52      nicm     4214:                        return;
                   4215:                }
                   4216:                if (px == 0)
                   4217:                        break;
                   4218:                px--;
1.47      nicm     4219:        }
1.1       nicm     4220: }
                   4221:
1.157     nicm     4222: static void
1.208     nicm     4223: window_copy_cursor_next_word(struct window_mode_entry *wme,
                   4224:     const char *separators)
1.40      nicm     4225: {
1.208     nicm     4226:        struct window_copy_mode_data    *data = wme->data;
1.54      nicm     4227:        struct screen                   *back_s = data->backing;
1.40      nicm     4228:        u_int                            px, py, xx, yy;
1.48      nicm     4229:        int                              expected = 0;
1.40      nicm     4230:
                   4231:        px = data->cx;
1.54      nicm     4232:        py = screen_hsize(back_s) + data->cy - data->oy;
1.208     nicm     4233:        xx = window_copy_find_length(wme, py);
1.54      nicm     4234:        yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
1.40      nicm     4235:
1.48      nicm     4236:        /*
                   4237:         * First skip past any nonword characters and then any word characters.
                   4238:         *
                   4239:         * expected is initially set to 0 for the former and then 1 for the
                   4240:         * latter.
                   4241:         */
                   4242:        do {
                   4243:                while (px > xx ||
1.208     nicm     4244:                    window_copy_in_set(wme, px, py, separators) == expected) {
1.48      nicm     4245:                        /* Move down if we're past the end of the line. */
                   4246:                        if (px > xx) {
                   4247:                                if (py == yy)
                   4248:                                        return;
1.208     nicm     4249:                                window_copy_cursor_down(wme, 0);
1.48      nicm     4250:                                px = 0;
                   4251:
1.54      nicm     4252:                                py = screen_hsize(back_s) + data->cy - data->oy;
1.208     nicm     4253:                                xx = window_copy_find_length(wme, py);
1.48      nicm     4254:                        } else
                   4255:                                px++;
                   4256:                }
                   4257:                expected = !expected;
                   4258:        } while (expected == 1);
1.40      nicm     4259:
1.208     nicm     4260:        window_copy_update_cursor(wme, px, data->cy);
1.252     nicm     4261:        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     4262:                window_copy_redraw_lines(wme, data->cy, 1);
1.40      nicm     4263: }
                   4264:
1.157     nicm     4265: static void
1.247     nicm     4266: window_copy_cursor_next_word_end_pos(struct window_mode_entry *wme,
                   4267:     const char *separators, u_int *ppx, u_int *ppy)
                   4268: {
                   4269:        struct window_pane              *wp = wme->wp;
                   4270:        struct window_copy_mode_data    *data = wme->data;
                   4271:        struct options                  *oo = wp->window->options;
                   4272:        struct screen                   *back_s = data->backing;
                   4273:        u_int                            px, py, xx, yy;
                   4274:        int                              keys, expected = 1;
                   4275:
                   4276:        px = data->cx;
                   4277:        py = screen_hsize(back_s) + data->cy - data->oy;
                   4278:        xx = window_copy_find_length(wme, py);
                   4279:        yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
                   4280:
                   4281:        keys = options_get_number(oo, "mode-keys");
                   4282:        if (keys == MODEKEY_VI && !window_copy_in_set(wme, px, py, separators))
                   4283:                px++;
                   4284:
                   4285:        /*
                   4286:         * First skip past any word characters, then any non-word characters.
                   4287:         *
                   4288:         * expected is initially set to 1 for the former and then 0 for the
                   4289:         * latter.
                   4290:         */
                   4291:        do {
                   4292:                while (px > xx ||
                   4293:                    window_copy_in_set(wme, px, py, separators) == expected) {
                   4294:                        /* Move down if we're past the end of the line. */
                   4295:                        if (px > xx) {
                   4296:                                if (py == yy)
                   4297:                                        return;
                   4298:                                py++;
                   4299:                                px = 0;
                   4300:                                xx = window_copy_find_length(wme, py);
                   4301:                        } else
                   4302:                                px++;
                   4303:                }
                   4304:                expected = !expected;
                   4305:        } while (expected == 0);
                   4306:
                   4307:        if (keys == MODEKEY_VI && px != 0)
                   4308:                px--;
                   4309:
                   4310:        *ppx = px;
                   4311:        *ppy = py;
                   4312: }
                   4313:
                   4314: static void
1.208     nicm     4315: window_copy_cursor_next_word_end(struct window_mode_entry *wme,
1.255     nicm     4316:     const char *separators, int no_reset)
1.1       nicm     4317: {
1.208     nicm     4318:        struct window_pane              *wp = wme->wp;
                   4319:        struct window_copy_mode_data    *data = wme->data;
1.136     nicm     4320:        struct options                  *oo = wp->window->options;
1.54      nicm     4321:        struct screen                   *back_s = data->backing;
1.39      nicm     4322:        u_int                            px, py, xx, yy;
1.94      nicm     4323:        int                              keys, expected = 1;
1.1       nicm     4324:
1.18      nicm     4325:        px = data->cx;
1.54      nicm     4326:        py = screen_hsize(back_s) + data->cy - data->oy;
1.208     nicm     4327:        xx = window_copy_find_length(wme, py);
1.54      nicm     4328:        yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
1.1       nicm     4329:
1.94      nicm     4330:        keys = options_get_number(oo, "mode-keys");
1.208     nicm     4331:        if (keys == MODEKEY_VI && !window_copy_in_set(wme, px, py, separators))
1.94      nicm     4332:                px++;
                   4333:
1.48      nicm     4334:        /*
                   4335:         * First skip past any word characters, then any nonword characters.
                   4336:         *
                   4337:         * expected is initially set to 1 for the former and then 0 for the
                   4338:         * latter.
                   4339:         */
                   4340:        do {
                   4341:                while (px > xx ||
1.208     nicm     4342:                    window_copy_in_set(wme, px, py, separators) == expected) {
1.48      nicm     4343:                        /* Move down if we're past the end of the line. */
                   4344:                        if (px > xx) {
                   4345:                                if (py == yy)
                   4346:                                        return;
1.208     nicm     4347:                                window_copy_cursor_down(wme, 0);
1.48      nicm     4348:                                px = 0;
                   4349:
1.54      nicm     4350:                                py = screen_hsize(back_s) + data->cy - data->oy;
1.208     nicm     4351:                                xx = window_copy_find_length(wme, py);
1.48      nicm     4352:                        } else
                   4353:                                px++;
                   4354:                }
                   4355:                expected = !expected;
                   4356:        } while (expected == 0);
1.92      nicm     4357:
1.94      nicm     4358:        if (keys == MODEKEY_VI && px != 0)
1.92      nicm     4359:                px--;
1.18      nicm     4360:
1.208     nicm     4361:        window_copy_update_cursor(wme, px, data->cy);
1.255     nicm     4362:        if (window_copy_update_selection(wme, 1, no_reset))
1.208     nicm     4363:                window_copy_redraw_lines(wme, data->cy, 1);
1.1       nicm     4364: }
                   4365:
1.247     nicm     4366: /* Compute the previous place where a word begins. */
                   4367: static void
                   4368: window_copy_cursor_previous_word_pos(struct window_mode_entry *wme,
                   4369:     const char *separators, int already, u_int *ppx, u_int *ppy)
                   4370: {
                   4371:        struct window_copy_mode_data    *data = wme->data;
                   4372:        u_int                            px, py;
                   4373:
                   4374:        px = data->cx;
                   4375:        py = screen_hsize(data->backing) + data->cy - data->oy;
                   4376:
                   4377:        /* Move back to the previous word character. */
                   4378:        if (already || window_copy_in_set(wme, px, py, separators)) {
                   4379:                for (;;) {
                   4380:                        if (px > 0) {
                   4381:                                px--;
                   4382:                                if (!window_copy_in_set(wme, px, py,
                   4383:                                    separators))
                   4384:                                        break;
                   4385:                        } else {
                   4386:                                if (data->cy == 0 &&
                   4387:                                    (screen_hsize(data->backing) == 0 ||
                   4388:                                    data->oy >=
                   4389:                                    screen_hsize(data->backing) - 1))
                   4390:                                        goto out;
                   4391:
                   4392:                                py = screen_hsize(data->backing) + data->cy -
                   4393:                                    data->oy;
                   4394:                                px = window_copy_find_length(wme, py);
                   4395:
                   4396:                                /* Stop if separator at EOL. */
                   4397:                                if (px > 0 && window_copy_in_set(wme, px - 1,
                   4398:                                    py, separators))
                   4399:                                        break;
                   4400:                        }
                   4401:                }
                   4402:        }
                   4403:
                   4404:        /* Move back to the beginning of this word. */
                   4405:        while (px > 0 && !window_copy_in_set(wme, px - 1, py, separators))
                   4406:                px--;
                   4407:
                   4408: out:
                   4409:        *ppx = px;
                   4410:        *ppy = py;
                   4411: }
                   4412:
1.8       nicm     4413: /* Move to the previous place where a word begins. */
1.157     nicm     4414: static void
1.208     nicm     4415: window_copy_cursor_previous_word(struct window_mode_entry *wme,
1.229     nicm     4416:     const char *separators, int already)
1.1       nicm     4417: {
1.208     nicm     4418:        struct window_copy_mode_data    *data = wme->data;
1.8       nicm     4419:        u_int                            px, py;
1.1       nicm     4420:
1.18      nicm     4421:        px = data->cx;
1.54      nicm     4422:        py = screen_hsize(data->backing) + data->cy - data->oy;
1.1       nicm     4423:
1.8       nicm     4424:        /* Move back to the previous word character. */
1.229     nicm     4425:        if (already || window_copy_in_set(wme, px, py, separators)) {
                   4426:                for (;;) {
                   4427:                        if (px > 0) {
                   4428:                                px--;
1.247     nicm     4429:                                if (!window_copy_in_set(wme, px, py,
                   4430:                                    separators))
1.229     nicm     4431:                                        break;
                   4432:                        } else {
                   4433:                                if (data->cy == 0 &&
                   4434:                                    (screen_hsize(data->backing) == 0 ||
1.247     nicm     4435:                                    data->oy >=
                   4436:                                    screen_hsize(data->backing) - 1))
1.229     nicm     4437:                                        goto out;
                   4438:                                window_copy_cursor_up(wme, 0);
                   4439:
1.247     nicm     4440:                                py = screen_hsize(data->backing) + data->cy -
                   4441:                                    data->oy;
1.229     nicm     4442:                                px = window_copy_find_length(wme, py);
                   4443:
                   4444:                                /* Stop if separator at EOL. */
1.247     nicm     4445:                                if (px > 0 && window_copy_in_set(wme, px - 1,
                   4446:                                    py, separators))
1.229     nicm     4447:                                        break;
                   4448:                        }
1.1       nicm     4449:                }
1.8       nicm     4450:        }
1.1       nicm     4451:
1.8       nicm     4452:        /* Move back to the beginning of this word. */
1.208     nicm     4453:        while (px > 0 && !window_copy_in_set(wme, px - 1, py, separators))
1.1       nicm     4454:                px--;
1.8       nicm     4455:
1.1       nicm     4456: out:
1.208     nicm     4457:        window_copy_update_cursor(wme, px, data->cy);
1.252     nicm     4458:        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     4459:                window_copy_redraw_lines(wme, data->cy, 1);
1.1       nicm     4460: }
                   4461:
1.157     nicm     4462: static void
1.208     nicm     4463: window_copy_scroll_up(struct window_mode_entry *wme, u_int ny)
1.1       nicm     4464: {
1.208     nicm     4465:        struct window_pane              *wp = wme->wp;
                   4466:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     4467:        struct screen                   *s = &data->screen;
                   4468:        struct screen_write_ctx          ctx;
                   4469:
                   4470:        if (data->oy < ny)
                   4471:                ny = data->oy;
                   4472:        if (ny == 0)
                   4473:                return;
                   4474:        data->oy -= ny;
                   4475:
1.264     nicm     4476:        if (data->searchmark != NULL && !data->timeout)
                   4477:                window_copy_search_marks(wme, NULL, data->searchregex);
1.252     nicm     4478:        window_copy_update_selection(wme, 0, 0);
1.96      nicm     4479:
1.283     nicm     4480:        screen_write_start_pane(&ctx, wp, NULL);
1.212     nicm     4481:        screen_write_cursormove(&ctx, 0, 0, 0);
1.159     nicm     4482:        screen_write_deleteline(&ctx, ny, 8);
1.208     nicm     4483:        window_copy_write_lines(wme, &ctx, screen_size_y(s) - ny, ny);
                   4484:        window_copy_write_line(wme, &ctx, 0);
1.31      nicm     4485:        if (screen_size_y(s) > 1)
1.208     nicm     4486:                window_copy_write_line(wme, &ctx, 1);
1.31      nicm     4487:        if (screen_size_y(s) > 3)
1.208     nicm     4488:                window_copy_write_line(wme, &ctx, screen_size_y(s) - 2);
1.192     nicm     4489:        if (s->sel != NULL && screen_size_y(s) > ny)
1.208     nicm     4490:                window_copy_write_line(wme, &ctx, screen_size_y(s) - ny - 1);
1.212     nicm     4491:        screen_write_cursormove(&ctx, data->cx, data->cy, 0);
1.1       nicm     4492:        screen_write_stop(&ctx);
                   4493: }
                   4494:
1.157     nicm     4495: static void
1.208     nicm     4496: window_copy_scroll_down(struct window_mode_entry *wme, u_int ny)
1.1       nicm     4497: {
1.208     nicm     4498:        struct window_pane              *wp = wme->wp;
                   4499:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     4500:        struct screen                   *s = &data->screen;
                   4501:        struct screen_write_ctx          ctx;
                   4502:
1.54      nicm     4503:        if (ny > screen_hsize(data->backing))
1.1       nicm     4504:                return;
                   4505:
1.54      nicm     4506:        if (data->oy > screen_hsize(data->backing) - ny)
                   4507:                ny = screen_hsize(data->backing) - data->oy;
1.1       nicm     4508:        if (ny == 0)
                   4509:                return;
                   4510:        data->oy += ny;
                   4511:
1.264     nicm     4512:        if (data->searchmark != NULL && !data->timeout)
                   4513:                window_copy_search_marks(wme, NULL, data->searchregex);
1.252     nicm     4514:        window_copy_update_selection(wme, 0, 0);
1.96      nicm     4515:
1.283     nicm     4516:        screen_write_start_pane(&ctx, wp, NULL);
1.212     nicm     4517:        screen_write_cursormove(&ctx, 0, 0, 0);
1.159     nicm     4518:        screen_write_insertline(&ctx, ny, 8);
1.208     nicm     4519:        window_copy_write_lines(wme, &ctx, 0, ny);
1.192     nicm     4520:        if (s->sel != NULL && screen_size_y(s) > ny)
1.208     nicm     4521:                window_copy_write_line(wme, &ctx, ny);
1.96      nicm     4522:        else if (ny == 1) /* nuke position */
1.208     nicm     4523:                window_copy_write_line(wme, &ctx, 1);
1.212     nicm     4524:        screen_write_cursormove(&ctx, data->cx, data->cy, 0);
1.1       nicm     4525:        screen_write_stop(&ctx);
                   4526: }
1.42      nicm     4527:
1.157     nicm     4528: static void
1.208     nicm     4529: window_copy_rectangle_toggle(struct window_mode_entry *wme)
1.42      nicm     4530: {
1.208     nicm     4531:        struct window_copy_mode_data    *data = wme->data;
1.47      nicm     4532:        u_int                            px, py;
1.42      nicm     4533:
                   4534:        data->rectflag = !data->rectflag;
1.47      nicm     4535:
1.54      nicm     4536:        py = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm     4537:        px = window_copy_find_length(wme, py);
1.47      nicm     4538:        if (data->cx > px)
1.208     nicm     4539:                window_copy_update_cursor(wme, px, data->cy);
1.42      nicm     4540:
1.252     nicm     4541:        window_copy_update_selection(wme, 1, 0);
1.208     nicm     4542:        window_copy_redraw_screen(wme);
1.156     nicm     4543: }
                   4544:
1.157     nicm     4545: static void
1.156     nicm     4546: window_copy_move_mouse(struct mouse_event *m)
                   4547: {
1.211     nicm     4548:        struct window_pane              *wp;
                   4549:        struct window_mode_entry        *wme;
                   4550:        u_int                            x, y;
1.156     nicm     4551:
                   4552:        wp = cmd_mouse_pane(m, NULL, NULL);
1.211     nicm     4553:        if (wp == NULL)
                   4554:                return;
                   4555:        wme = TAILQ_FIRST(&wp->modes);
1.223     nicm     4556:        if (wme == NULL)
                   4557:                return;
                   4558:        if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
1.156     nicm     4559:                return;
                   4560:
1.183     nicm     4561:        if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
1.156     nicm     4562:                return;
                   4563:
1.211     nicm     4564:        window_copy_update_cursor(wme, x, y);
1.126     nicm     4565: }
                   4566:
                   4567: void
1.141     nicm     4568: window_copy_start_drag(struct client *c, struct mouse_event *m)
1.126     nicm     4569: {
1.211     nicm     4570:        struct window_pane              *wp;
                   4571:        struct window_mode_entry        *wme;
1.247     nicm     4572:        struct window_copy_mode_data    *data;
1.248     nicm     4573:        u_int                            x, y, yg;
1.126     nicm     4574:
1.155     nicm     4575:        if (c == NULL)
                   4576:                return;
                   4577:
1.126     nicm     4578:        wp = cmd_mouse_pane(m, NULL, NULL);
1.211     nicm     4579:        if (wp == NULL)
                   4580:                return;
                   4581:        wme = TAILQ_FIRST(&wp->modes);
1.223     nicm     4582:        if (wme == NULL)
                   4583:                return;
                   4584:        if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
1.126     nicm     4585:                return;
                   4586:
                   4587:        if (cmd_mouse_at(wp, m, &x, &y, 1) != 0)
                   4588:                return;
                   4589:
                   4590:        c->tty.mouse_drag_update = window_copy_drag_update;
1.217     nicm     4591:        c->tty.mouse_drag_release = window_copy_drag_release;
1.126     nicm     4592:
1.247     nicm     4593:        data = wme->data;
1.248     nicm     4594:        yg = screen_hsize(data->backing) + y - data->oy;
                   4595:        if (x < data->selrx || x > data->endselrx || yg != data->selry)
                   4596:                data->selflag = SEL_CHAR;
1.247     nicm     4597:        switch (data->selflag) {
                   4598:                case SEL_WORD:
1.268     nicm     4599:                        if (data->ws != NULL) {
1.247     nicm     4600:                                window_copy_update_cursor(wme, x, y);
                   4601:                                window_copy_cursor_previous_word_pos(wme,
                   4602:                                    data->ws, 0, &x, &y);
                   4603:                                y -= screen_hsize(data->backing) - data->oy;
                   4604:                        }
                   4605:                        window_copy_update_cursor(wme, x, y);
                   4606:                        break;
                   4607:                case SEL_LINE:
                   4608:                        window_copy_update_cursor(wme, 0, y);
                   4609:                        break;
                   4610:                case SEL_CHAR:
                   4611:                        window_copy_update_cursor(wme, x, y);
                   4612:                        window_copy_start_selection(wme);
                   4613:                        break;
                   4614:        }
                   4615:
1.211     nicm     4616:        window_copy_redraw_screen(wme);
1.220     nicm     4617:        window_copy_drag_update(c, m);
1.126     nicm     4618: }
                   4619:
1.157     nicm     4620: static void
1.211     nicm     4621: window_copy_drag_update(struct client *c, struct mouse_event *m)
1.126     nicm     4622: {
                   4623:        struct window_pane              *wp;
1.211     nicm     4624:        struct window_mode_entry        *wme;
1.126     nicm     4625:        struct window_copy_mode_data    *data;
1.217     nicm     4626:        u_int                            x, y, old_cx, old_cy;
                   4627:        struct timeval                   tv = {
                   4628:                .tv_usec = WINDOW_COPY_DRAG_REPEAT_TIME
                   4629:        };
1.126     nicm     4630:
1.211     nicm     4631:        if (c == NULL)
                   4632:                return;
                   4633:
1.126     nicm     4634:        wp = cmd_mouse_pane(m, NULL, NULL);
1.211     nicm     4635:        if (wp == NULL)
1.126     nicm     4636:                return;
1.211     nicm     4637:        wme = TAILQ_FIRST(&wp->modes);
1.223     nicm     4638:        if (wme == NULL)
                   4639:                return;
                   4640:        if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
1.211     nicm     4641:                return;
1.217     nicm     4642:
1.211     nicm     4643:        data = wme->data;
1.217     nicm     4644:        evtimer_del(&data->dragtimer);
1.126     nicm     4645:
                   4646:        if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
                   4647:                return;
1.217     nicm     4648:        old_cx = data->cx;
1.126     nicm     4649:        old_cy = data->cy;
                   4650:
1.211     nicm     4651:        window_copy_update_cursor(wme, x, y);
1.252     nicm     4652:        if (window_copy_update_selection(wme, 1, 0))
1.211     nicm     4653:                window_copy_redraw_selection(wme, old_cy);
1.217     nicm     4654:        if (old_cy != data->cy || old_cx == data->cx) {
                   4655:                if (y == 0) {
                   4656:                        evtimer_add(&data->dragtimer, &tv);
                   4657:                        window_copy_cursor_up(wme, 1);
                   4658:                } else if (y == screen_size_y(&data->screen) - 1) {
                   4659:                        evtimer_add(&data->dragtimer, &tv);
                   4660:                        window_copy_cursor_down(wme, 1);
                   4661:                }
                   4662:        }
                   4663: }
                   4664:
                   4665: static void
                   4666: window_copy_drag_release(struct client *c, struct mouse_event *m)
                   4667: {
                   4668:        struct window_pane              *wp;
                   4669:        struct window_mode_entry        *wme;
                   4670:        struct window_copy_mode_data    *data;
                   4671:
                   4672:        if (c == NULL)
                   4673:                return;
                   4674:
                   4675:        wp = cmd_mouse_pane(m, NULL, NULL);
                   4676:        if (wp == NULL)
                   4677:                return;
                   4678:        wme = TAILQ_FIRST(&wp->modes);
1.223     nicm     4679:        if (wme == NULL)
                   4680:                return;
                   4681:        if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
1.217     nicm     4682:                return;
                   4683:
                   4684:        data = wme->data;
                   4685:        evtimer_del(&data->dragtimer);
1.42      nicm     4686: }