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

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