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

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