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

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