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

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