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

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