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

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