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

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