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

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