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

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