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

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