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

1.254   ! nicm        1: /* $OpenBSD: window-copy.c,v 1.253 2020/03/19 13:43:18 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;
1.254   ! nicm     1364:        struct window_copy_mode_data    *data = wme->data;
1.213     nicm     1365:
1.254   ! nicm     1366:        data->selflag = SEL_CHAR;
1.213     nicm     1367:        if ((np % 2) != 0)
                   1368:                window_copy_other_end(wme);
                   1369:        return (WINDOW_COPY_CMD_NOTHING);
                   1370: }
                   1371:
                   1372: static enum window_copy_cmd_action
                   1373: window_copy_cmd_page_down(struct window_copy_cmd_state *cs)
                   1374: {
                   1375:        struct window_mode_entry        *wme = cs->wme;
                   1376:        struct window_copy_mode_data    *data = wme->data;
                   1377:        u_int                            np = wme->prefix;
                   1378:
                   1379:        for (; np != 0; np--) {
                   1380:                if (window_copy_pagedown(wme, 0, data->scroll_exit))
                   1381:                        return (WINDOW_COPY_CMD_CANCEL);
                   1382:        }
                   1383:        return (WINDOW_COPY_CMD_NOTHING);
                   1384: }
                   1385:
                   1386: static enum window_copy_cmd_action
                   1387: window_copy_cmd_page_down_and_cancel(struct window_copy_cmd_state *cs)
                   1388: {
                   1389:        struct window_mode_entry        *wme = cs->wme;
                   1390:        u_int                            np = wme->prefix;
                   1391:
                   1392:        for (; np != 0; np--) {
                   1393:                if (window_copy_pagedown(wme, 0, 1))
                   1394:                        return (WINDOW_COPY_CMD_CANCEL);
                   1395:        }
                   1396:        return (WINDOW_COPY_CMD_NOTHING);
                   1397: }
                   1398:
                   1399: static enum window_copy_cmd_action
                   1400: window_copy_cmd_page_up(struct window_copy_cmd_state *cs)
                   1401: {
                   1402:        struct window_mode_entry        *wme = cs->wme;
                   1403:        u_int                            np = wme->prefix;
                   1404:
                   1405:        for (; np != 0; np--)
                   1406:                window_copy_pageup1(wme, 0);
                   1407:        return (WINDOW_COPY_CMD_NOTHING);
                   1408: }
                   1409:
                   1410: static enum window_copy_cmd_action
                   1411: window_copy_cmd_previous_paragraph(struct window_copy_cmd_state *cs)
                   1412: {
                   1413:        struct window_mode_entry        *wme = cs->wme;
                   1414:        u_int                            np = wme->prefix;
                   1415:
                   1416:        for (; np != 0; np--)
                   1417:                window_copy_previous_paragraph(wme);
                   1418:        return (WINDOW_COPY_CMD_NOTHING);
                   1419: }
                   1420:
                   1421: static enum window_copy_cmd_action
                   1422: window_copy_cmd_previous_space(struct window_copy_cmd_state *cs)
                   1423: {
                   1424:        struct window_mode_entry        *wme = cs->wme;
                   1425:        u_int                            np = wme->prefix;
                   1426:
                   1427:        for (; np != 0; np--)
1.229     nicm     1428:                window_copy_cursor_previous_word(wme, " ", 1);
1.213     nicm     1429:        return (WINDOW_COPY_CMD_NOTHING);
                   1430: }
                   1431:
                   1432: static enum window_copy_cmd_action
                   1433: window_copy_cmd_previous_word(struct window_copy_cmd_state *cs)
                   1434: {
                   1435:        struct window_mode_entry        *wme = cs->wme;
                   1436:        struct session                  *s = cs->s;
                   1437:        u_int                            np = wme->prefix;
                   1438:        const char                      *ws;
                   1439:
                   1440:        ws = options_get_string(s->options, "word-separators");
                   1441:        for (; np != 0; np--)
1.229     nicm     1442:                window_copy_cursor_previous_word(wme, ws, 1);
1.213     nicm     1443:        return (WINDOW_COPY_CMD_NOTHING);
                   1444: }
                   1445:
                   1446: static enum window_copy_cmd_action
                   1447: window_copy_cmd_rectangle_toggle(struct window_copy_cmd_state *cs)
                   1448: {
                   1449:        struct window_mode_entry        *wme = cs->wme;
                   1450:        struct window_copy_mode_data    *data = wme->data;
                   1451:
                   1452:        data->lineflag = LINE_SEL_NONE;
                   1453:        window_copy_rectangle_toggle(wme);
                   1454:
                   1455:        return (WINDOW_COPY_CMD_NOTHING);
                   1456: }
                   1457:
                   1458: static enum window_copy_cmd_action
                   1459: window_copy_cmd_scroll_down(struct window_copy_cmd_state *cs)
                   1460: {
                   1461:        struct window_mode_entry        *wme = cs->wme;
                   1462:        struct window_copy_mode_data    *data = wme->data;
                   1463:        u_int                            np = wme->prefix;
                   1464:
                   1465:        for (; np != 0; np--)
                   1466:                window_copy_cursor_down(wme, 1);
                   1467:        if (data->scroll_exit && data->oy == 0)
                   1468:                return (WINDOW_COPY_CMD_CANCEL);
                   1469:        return (WINDOW_COPY_CMD_NOTHING);
                   1470: }
                   1471:
                   1472: static enum window_copy_cmd_action
                   1473: window_copy_cmd_scroll_down_and_cancel(struct window_copy_cmd_state *cs)
                   1474: {
                   1475:        struct window_mode_entry        *wme = cs->wme;
                   1476:        struct window_copy_mode_data    *data = wme->data;
                   1477:        u_int                            np = wme->prefix;
                   1478:
                   1479:        for (; np != 0; np--)
                   1480:                window_copy_cursor_down(wme, 1);
                   1481:        if (data->oy == 0)
                   1482:                return (WINDOW_COPY_CMD_CANCEL);
                   1483:        return (WINDOW_COPY_CMD_NOTHING);
                   1484: }
                   1485:
                   1486: static enum window_copy_cmd_action
                   1487: window_copy_cmd_scroll_up(struct window_copy_cmd_state *cs)
                   1488: {
                   1489:        struct window_mode_entry        *wme = cs->wme;
                   1490:        u_int                            np = wme->prefix;
                   1491:
                   1492:        for (; np != 0; np--)
                   1493:                window_copy_cursor_up(wme, 1);
                   1494:        return (WINDOW_COPY_CMD_NOTHING);
                   1495: }
                   1496:
                   1497: static enum window_copy_cmd_action
                   1498: window_copy_cmd_search_again(struct window_copy_cmd_state *cs)
                   1499: {
                   1500:        struct window_mode_entry        *wme = cs->wme;
                   1501:        struct window_copy_mode_data    *data = wme->data;
                   1502:        u_int                            np = wme->prefix;
                   1503:
                   1504:        if (data->searchtype == WINDOW_COPY_SEARCHUP) {
                   1505:                for (; np != 0; np--)
1.244     nicm     1506:                        window_copy_search_up(wme, 1);
1.213     nicm     1507:        } else if (data->searchtype == WINDOW_COPY_SEARCHDOWN) {
                   1508:                for (; np != 0; np--)
1.244     nicm     1509:                        window_copy_search_down(wme, 1);
1.213     nicm     1510:        }
                   1511:        return (WINDOW_COPY_CMD_NOTHING);
                   1512: }
                   1513:
                   1514: static enum window_copy_cmd_action
                   1515: window_copy_cmd_search_reverse(struct window_copy_cmd_state *cs)
                   1516: {
                   1517:        struct window_mode_entry        *wme = cs->wme;
                   1518:        struct window_copy_mode_data    *data = wme->data;
                   1519:        u_int                            np = wme->prefix;
                   1520:
                   1521:        if (data->searchtype == WINDOW_COPY_SEARCHUP) {
                   1522:                for (; np != 0; np--)
1.244     nicm     1523:                        window_copy_search_down(wme, 1);
1.213     nicm     1524:        } else if (data->searchtype == WINDOW_COPY_SEARCHDOWN) {
                   1525:                for (; np != 0; np--)
1.244     nicm     1526:                        window_copy_search_up(wme, 1);
1.213     nicm     1527:        }
                   1528:        return (WINDOW_COPY_CMD_NOTHING);
                   1529: }
                   1530:
                   1531: static enum window_copy_cmd_action
                   1532: window_copy_cmd_select_line(struct window_copy_cmd_state *cs)
                   1533: {
                   1534:        struct window_mode_entry        *wme = cs->wme;
                   1535:        struct window_copy_mode_data    *data = wme->data;
                   1536:        u_int                            np = wme->prefix;
                   1537:
                   1538:        data->lineflag = LINE_SEL_LEFT_RIGHT;
                   1539:        data->rectflag = 0;
1.247     nicm     1540:        data->selflag = SEL_LINE;
                   1541:        data->dx = data->cx;
                   1542:        data->dy = screen_hsize(data->backing) + data->cy - data->oy;
1.213     nicm     1543:
                   1544:        window_copy_cursor_start_of_line(wme);
1.247     nicm     1545:        data->selrx = data->cx;
                   1546:        data->selry = screen_hsize(data->backing) + data->cy - data->oy;
1.213     nicm     1547:        window_copy_start_selection(wme);
                   1548:        for (; np > 1; np--)
                   1549:                window_copy_cursor_down(wme, 0);
                   1550:        window_copy_cursor_end_of_line(wme);
1.247     nicm     1551:        data->endselrx = data->cx;
                   1552:        data->endselry = screen_hsize(data->backing) + data->cy - data->oy;
1.213     nicm     1553:
                   1554:        return (WINDOW_COPY_CMD_REDRAW);
                   1555: }
                   1556:
                   1557: static enum window_copy_cmd_action
                   1558: window_copy_cmd_select_word(struct window_copy_cmd_state *cs)
                   1559: {
                   1560:        struct window_mode_entry        *wme = cs->wme;
                   1561:        struct session                  *s = cs->s;
                   1562:        struct window_copy_mode_data    *data = wme->data;
1.247     nicm     1563:        u_int                            px, py;
1.213     nicm     1564:
                   1565:        data->lineflag = LINE_SEL_LEFT_RIGHT;
                   1566:        data->rectflag = 0;
1.247     nicm     1567:        data->selflag = SEL_WORD;
                   1568:        data->dx = data->cx;
                   1569:        data->dy = screen_hsize(data->backing) + data->cy - data->oy;
1.213     nicm     1570:
1.245     nicm     1571:        px = data->cx;
                   1572:        py = screen_hsize(data->backing) + data->cy - data->oy;
                   1573:
1.252     nicm     1574:        data->ws = options_get_string(s->options, "word-separators");
                   1575:        window_copy_cursor_previous_word(wme, data->ws, 0);
1.247     nicm     1576:        data->selrx = data->cx;
                   1577:        data->selry = screen_hsize(data->backing) + data->cy - data->oy;
1.213     nicm     1578:        window_copy_start_selection(wme);
1.245     nicm     1579:
1.247     nicm     1580:        if (px >= window_copy_find_length(wme, py) ||
1.252     nicm     1581:            !window_copy_in_set(wme, px + 1, py, data->ws))
                   1582:                window_copy_cursor_next_word_end(wme, data->ws);
1.245     nicm     1583:        else {
                   1584:                window_copy_update_cursor(wme, px, data->cy);
1.252     nicm     1585:                if (window_copy_update_selection(wme, 1, 0))
1.245     nicm     1586:                        window_copy_redraw_lines(wme, data->cy, 1);
                   1587:        }
1.247     nicm     1588:        data->endselrx = data->cx;
                   1589:        data->endselry = screen_hsize(data->backing) + data->cy - data->oy;
1.213     nicm     1590:
                   1591:        return (WINDOW_COPY_CMD_REDRAW);
                   1592: }
                   1593:
                   1594: static enum window_copy_cmd_action
                   1595: window_copy_cmd_start_of_line(struct window_copy_cmd_state *cs)
                   1596: {
                   1597:        struct window_mode_entry        *wme = cs->wme;
                   1598:
                   1599:        window_copy_cursor_start_of_line(wme);
                   1600:        return (WINDOW_COPY_CMD_NOTHING);
                   1601: }
                   1602:
                   1603: static enum window_copy_cmd_action
                   1604: window_copy_cmd_top_line(struct window_copy_cmd_state *cs)
                   1605: {
                   1606:        struct window_mode_entry        *wme = cs->wme;
                   1607:        struct window_copy_mode_data    *data = wme->data;
                   1608:
                   1609:        data->cx = 0;
                   1610:        data->cy = 0;
                   1611:
1.252     nicm     1612:        window_copy_update_selection(wme, 1, 0);
1.213     nicm     1613:        return (WINDOW_COPY_CMD_REDRAW);
                   1614: }
                   1615:
                   1616: static enum window_copy_cmd_action
1.216     nicm     1617: window_copy_cmd_copy_pipe_no_clear(struct window_copy_cmd_state *cs)
1.213     nicm     1618: {
                   1619:        struct window_mode_entry        *wme = cs->wme;
1.215     nicm     1620:        struct client                   *c = cs->c;
1.213     nicm     1621:        struct session                  *s = cs->s;
1.215     nicm     1622:        struct winlink                  *wl = cs->wl;
                   1623:        struct window_pane              *wp = wme->wp;
                   1624:        char                            *command = NULL;
                   1625:        char                            *prefix = NULL;
                   1626:
                   1627:        if (cs->args->argc == 3)
                   1628:                prefix = format_single(NULL, cs->args->argv[2], c, s, wl, wp);
                   1629:
                   1630:        if (s != NULL && *cs->args->argv[1] != '\0') {
                   1631:                command = format_single(NULL, cs->args->argv[1], c, s, wl, wp);
                   1632:                window_copy_copy_pipe(wme, s, prefix, command);
                   1633:                free(command);
                   1634:        }
1.213     nicm     1635:
1.215     nicm     1636:        free(prefix);
1.213     nicm     1637:        return (WINDOW_COPY_CMD_NOTHING);
                   1638: }
                   1639:
                   1640: static enum window_copy_cmd_action
1.216     nicm     1641: window_copy_cmd_copy_pipe(struct window_copy_cmd_state *cs)
1.213     nicm     1642: {
                   1643:        struct window_mode_entry        *wme = cs->wme;
1.215     nicm     1644:
1.216     nicm     1645:        window_copy_cmd_copy_pipe_no_clear(cs);
                   1646:        window_copy_clear_selection(wme);
                   1647:        return (WINDOW_COPY_CMD_REDRAW);
                   1648: }
1.215     nicm     1649:
1.216     nicm     1650: static enum window_copy_cmd_action
                   1651: window_copy_cmd_copy_pipe_and_cancel(struct window_copy_cmd_state *cs)
                   1652: {
                   1653:        struct window_mode_entry        *wme = cs->wme;
1.213     nicm     1654:
1.216     nicm     1655:        window_copy_cmd_copy_pipe_no_clear(cs);
                   1656:        window_copy_clear_selection(wme);
                   1657:        return (WINDOW_COPY_CMD_CANCEL);
1.213     nicm     1658: }
                   1659:
                   1660: static enum window_copy_cmd_action
                   1661: window_copy_cmd_goto_line(struct window_copy_cmd_state *cs)
                   1662: {
                   1663:        struct window_mode_entry        *wme = cs->wme;
                   1664:        const char                      *argument = cs->args->argv[1];
                   1665:
                   1666:        if (*argument != '\0')
                   1667:                window_copy_goto_line(wme, argument);
                   1668:        return (WINDOW_COPY_CMD_NOTHING);
                   1669: }
                   1670:
                   1671: static enum window_copy_cmd_action
                   1672: window_copy_cmd_jump_backward(struct window_copy_cmd_state *cs)
                   1673: {
                   1674:        struct window_mode_entry        *wme = cs->wme;
                   1675:        struct window_copy_mode_data    *data = wme->data;
                   1676:        u_int                            np = wme->prefix;
                   1677:        const char                      *argument = cs->args->argv[1];
                   1678:
                   1679:        if (*argument != '\0') {
                   1680:                data->jumptype = WINDOW_COPY_JUMPBACKWARD;
                   1681:                data->jumpchar = *argument;
                   1682:                for (; np != 0; np--)
                   1683:                        window_copy_cursor_jump_back(wme);
                   1684:        }
                   1685:        return (WINDOW_COPY_CMD_NOTHING);
                   1686: }
                   1687:
                   1688: static enum window_copy_cmd_action
                   1689: window_copy_cmd_jump_forward(struct window_copy_cmd_state *cs)
                   1690: {
                   1691:        struct window_mode_entry        *wme = cs->wme;
                   1692:        struct window_copy_mode_data    *data = wme->data;
                   1693:        u_int                            np = wme->prefix;
                   1694:        const char                      *argument = cs->args->argv[1];
                   1695:
                   1696:        if (*argument != '\0') {
                   1697:                data->jumptype = WINDOW_COPY_JUMPFORWARD;
                   1698:                data->jumpchar = *argument;
                   1699:                for (; np != 0; np--)
1.215     nicm     1700:                        window_copy_cursor_jump(wme);
1.213     nicm     1701:        }
                   1702:        return (WINDOW_COPY_CMD_NOTHING);
                   1703: }
                   1704:
                   1705: static enum window_copy_cmd_action
                   1706: window_copy_cmd_jump_to_backward(struct window_copy_cmd_state *cs)
                   1707: {
                   1708:        struct window_mode_entry        *wme = cs->wme;
                   1709:        struct window_copy_mode_data    *data = wme->data;
                   1710:        u_int                            np = wme->prefix;
                   1711:        const char                      *argument = cs->args->argv[1];
                   1712:
                   1713:        if (*argument != '\0') {
                   1714:                data->jumptype = WINDOW_COPY_JUMPTOBACKWARD;
                   1715:                data->jumpchar = *argument;
                   1716:                for (; np != 0; np--)
                   1717:                        window_copy_cursor_jump_to_back(wme);
                   1718:        }
                   1719:        return (WINDOW_COPY_CMD_NOTHING);
                   1720: }
                   1721:
                   1722: static enum window_copy_cmd_action
                   1723: window_copy_cmd_jump_to_forward(struct window_copy_cmd_state *cs)
                   1724: {
                   1725:        struct window_mode_entry        *wme = cs->wme;
                   1726:        struct window_copy_mode_data    *data = wme->data;
                   1727:        u_int                            np = wme->prefix;
                   1728:        const char                      *argument = cs->args->argv[1];
                   1729:
                   1730:        if (*argument != '\0') {
                   1731:                data->jumptype = WINDOW_COPY_JUMPTOFORWARD;
                   1732:                data->jumpchar = *argument;
                   1733:                for (; np != 0; np--)
                   1734:                        window_copy_cursor_jump_to(wme);
                   1735:        }
                   1736:        return (WINDOW_COPY_CMD_NOTHING);
                   1737: }
                   1738:
                   1739: static enum window_copy_cmd_action
                   1740: window_copy_cmd_search_backward(struct window_copy_cmd_state *cs)
                   1741: {
                   1742:        struct window_mode_entry        *wme = cs->wme;
                   1743:        struct window_copy_mode_data    *data = wme->data;
                   1744:        u_int                            np = wme->prefix;
1.233     nicm     1745:        const char                      *argument;
1.238     nicm     1746:        char                            *expanded;
1.213     nicm     1747:
1.233     nicm     1748:        if (cs->args->argc == 2) {
                   1749:                argument = cs->args->argv[1];
                   1750:                if (*argument != '\0') {
1.238     nicm     1751:                        if (args_has(cs->args, 'F')) {
                   1752:                                expanded = format_single(NULL, argument, NULL,
                   1753:                                    NULL, NULL, wme->wp);
                   1754:                                if (*expanded == '\0') {
                   1755:                                        free(expanded);
                   1756:                                        return (WINDOW_COPY_CMD_NOTHING);
                   1757:                                }
                   1758:                                free(data->searchstr);
                   1759:                                data->searchstr = expanded;
                   1760:                        } else {
                   1761:                                free(data->searchstr);
                   1762:                                data->searchstr = xstrdup(argument);
                   1763:                        }
1.233     nicm     1764:                }
                   1765:        }
                   1766:        if (data->searchstr != NULL) {
1.232     nicm     1767:                data->searchtype = WINDOW_COPY_SEARCHUP;
1.213     nicm     1768:                for (; np != 0; np--)
1.244     nicm     1769:                        window_copy_search_up(wme, 1);
1.213     nicm     1770:        }
                   1771:        return (WINDOW_COPY_CMD_NOTHING);
                   1772: }
                   1773:
                   1774: static enum window_copy_cmd_action
                   1775: window_copy_cmd_search_forward(struct window_copy_cmd_state *cs)
                   1776: {
                   1777:        struct window_mode_entry        *wme = cs->wme;
                   1778:        struct window_copy_mode_data    *data = wme->data;
                   1779:        u_int                            np = wme->prefix;
1.233     nicm     1780:        const char                      *argument;
1.238     nicm     1781:        char                            *expanded;
1.213     nicm     1782:
1.233     nicm     1783:        if (cs->args->argc == 2) {
                   1784:                argument = cs->args->argv[1];
                   1785:                if (*argument != '\0') {
1.238     nicm     1786:                        if (args_has(cs->args, 'F')) {
                   1787:                                expanded = format_single(NULL, argument, NULL,
                   1788:                                    NULL, NULL, wme->wp);
                   1789:                                if (*expanded == '\0') {
                   1790:                                        free(expanded);
                   1791:                                        return (WINDOW_COPY_CMD_NOTHING);
                   1792:                                }
                   1793:                                free(data->searchstr);
                   1794:                                data->searchstr = expanded;
                   1795:                        } else {
                   1796:                                free(data->searchstr);
                   1797:                                data->searchstr = xstrdup(argument);
                   1798:                        }
1.233     nicm     1799:                }
                   1800:        }
                   1801:        if (data->searchstr != NULL) {
1.232     nicm     1802:                data->searchtype = WINDOW_COPY_SEARCHDOWN;
1.213     nicm     1803:                for (; np != 0; np--)
1.244     nicm     1804:                        window_copy_search_down(wme, 1);
1.213     nicm     1805:        }
                   1806:        return (WINDOW_COPY_CMD_NOTHING);
                   1807: }
                   1808:
                   1809: static enum window_copy_cmd_action
                   1810: window_copy_cmd_search_backward_incremental(struct window_copy_cmd_state *cs)
                   1811: {
                   1812:        struct window_mode_entry        *wme = cs->wme;
                   1813:        struct window_copy_mode_data    *data = wme->data;
                   1814:        const char                      *argument = cs->args->argv[1];
1.225     nicm     1815:        const char                      *ss = data->searchstr;
                   1816:        char                             prefix;
1.213     nicm     1817:        enum window_copy_cmd_action      action = WINDOW_COPY_CMD_NOTHING;
                   1818:
1.225     nicm     1819:        prefix = *argument++;
1.213     nicm     1820:        if (data->searchx == -1 || data->searchy == -1) {
                   1821:                data->searchx = data->cx;
                   1822:                data->searchy = data->cy;
                   1823:                data->searcho = data->oy;
                   1824:        } else if (ss != NULL && strcmp(argument, ss) != 0) {
                   1825:                data->cx = data->searchx;
                   1826:                data->cy = data->searchy;
                   1827:                data->oy = data->searcho;
                   1828:                action = WINDOW_COPY_CMD_REDRAW;
                   1829:        }
                   1830:        if (*argument == '\0') {
                   1831:                window_copy_clear_marks(wme);
                   1832:                return (WINDOW_COPY_CMD_REDRAW);
                   1833:        }
1.225     nicm     1834:        switch (prefix) {
1.213     nicm     1835:        case '=':
                   1836:        case '-':
                   1837:                data->searchtype = WINDOW_COPY_SEARCHUP;
                   1838:                free(data->searchstr);
                   1839:                data->searchstr = xstrdup(argument);
1.244     nicm     1840:                if (!window_copy_search_up(wme, 0)) {
1.213     nicm     1841:                        window_copy_clear_marks(wme);
                   1842:                        return (WINDOW_COPY_CMD_REDRAW);
                   1843:                }
                   1844:                break;
                   1845:        case '+':
                   1846:                data->searchtype = WINDOW_COPY_SEARCHDOWN;
                   1847:                free(data->searchstr);
                   1848:                data->searchstr = xstrdup(argument);
1.244     nicm     1849:                if (!window_copy_search_down(wme, 0)) {
1.213     nicm     1850:                        window_copy_clear_marks(wme);
                   1851:                        return (WINDOW_COPY_CMD_REDRAW);
                   1852:                }
                   1853:                break;
                   1854:        }
                   1855:        return (action);
                   1856: }
                   1857:
                   1858: static enum window_copy_cmd_action
                   1859: window_copy_cmd_search_forward_incremental(struct window_copy_cmd_state *cs)
                   1860: {
                   1861:        struct window_mode_entry        *wme = cs->wme;
                   1862:        struct window_copy_mode_data    *data = wme->data;
                   1863:        const char                      *argument = cs->args->argv[1];
1.225     nicm     1864:        const char                      *ss = data->searchstr;
                   1865:        char                             prefix;
1.213     nicm     1866:        enum window_copy_cmd_action      action = WINDOW_COPY_CMD_NOTHING;
                   1867:
1.225     nicm     1868:        prefix = *argument++;
1.213     nicm     1869:        if (data->searchx == -1 || data->searchy == -1) {
                   1870:                data->searchx = data->cx;
                   1871:                data->searchy = data->cy;
                   1872:                data->searcho = data->oy;
                   1873:        } else if (ss != NULL && strcmp(argument, ss) != 0) {
                   1874:                data->cx = data->searchx;
                   1875:                data->cy = data->searchy;
                   1876:                data->oy = data->searcho;
                   1877:                action = WINDOW_COPY_CMD_REDRAW;
                   1878:        }
                   1879:        if (*argument == '\0') {
                   1880:                window_copy_clear_marks(wme);
                   1881:                return (WINDOW_COPY_CMD_REDRAW);
                   1882:        }
1.225     nicm     1883:        switch (prefix) {
1.213     nicm     1884:        case '=':
                   1885:        case '+':
                   1886:                data->searchtype = WINDOW_COPY_SEARCHDOWN;
                   1887:                free(data->searchstr);
                   1888:                data->searchstr = xstrdup(argument);
1.244     nicm     1889:                if (!window_copy_search_down(wme, 0)) {
1.213     nicm     1890:                        window_copy_clear_marks(wme);
                   1891:                        return (WINDOW_COPY_CMD_REDRAW);
                   1892:                }
                   1893:                break;
                   1894:        case '-':
                   1895:                data->searchtype = WINDOW_COPY_SEARCHUP;
                   1896:                free(data->searchstr);
                   1897:                data->searchstr = xstrdup(argument);
1.244     nicm     1898:                if (!window_copy_search_up(wme, 0)) {
1.213     nicm     1899:                        window_copy_clear_marks(wme);
                   1900:                        return (WINDOW_COPY_CMD_REDRAW);
                   1901:                }
                   1902:        }
                   1903:        return (action);
                   1904: }
                   1905:
                   1906: static const struct {
                   1907:        const char                       *command;
                   1908:        int                               minargs;
                   1909:        int                               maxargs;
1.239     nicm     1910:        int                               ismotion;
1.213     nicm     1911:        enum window_copy_cmd_action     (*f)(struct window_copy_cmd_state *);
                   1912: } window_copy_cmd_table[] = {
1.239     nicm     1913:        { "append-selection", 0, 0, 0,
1.213     nicm     1914:          window_copy_cmd_append_selection },
1.239     nicm     1915:        { "append-selection-and-cancel", 0, 0, 0,
1.213     nicm     1916:          window_copy_cmd_append_selection_and_cancel },
1.239     nicm     1917:        { "back-to-indentation", 0, 0, 0,
1.213     nicm     1918:          window_copy_cmd_back_to_indentation },
1.239     nicm     1919:        { "begin-selection", 0, 0, 0,
1.213     nicm     1920:          window_copy_cmd_begin_selection },
1.239     nicm     1921:        { "bottom-line", 0, 0, 1,
1.213     nicm     1922:          window_copy_cmd_bottom_line },
1.239     nicm     1923:        { "cancel", 0, 0, 0,
1.213     nicm     1924:          window_copy_cmd_cancel },
1.239     nicm     1925:        { "clear-selection", 0, 0, 0,
1.213     nicm     1926:          window_copy_cmd_clear_selection },
1.239     nicm     1927:        { "copy-end-of-line", 0, 1, 0,
1.213     nicm     1928:          window_copy_cmd_copy_end_of_line },
1.239     nicm     1929:        { "copy-line", 0, 1, 0,
1.213     nicm     1930:          window_copy_cmd_copy_line },
1.239     nicm     1931:        { "copy-pipe-no-clear", 1, 2, 0,
1.216     nicm     1932:          window_copy_cmd_copy_pipe_no_clear },
1.239     nicm     1933:        { "copy-pipe", 1, 2, 0,
1.213     nicm     1934:          window_copy_cmd_copy_pipe },
1.239     nicm     1935:        { "copy-pipe-and-cancel", 1, 2, 0,
1.213     nicm     1936:          window_copy_cmd_copy_pipe_and_cancel },
1.239     nicm     1937:        { "copy-selection-no-clear", 0, 1, 0,
1.216     nicm     1938:          window_copy_cmd_copy_selection_no_clear },
1.239     nicm     1939:        { "copy-selection", 0, 1, 0,
1.213     nicm     1940:          window_copy_cmd_copy_selection },
1.239     nicm     1941:        { "copy-selection-and-cancel", 0, 1, 0,
1.213     nicm     1942:          window_copy_cmd_copy_selection_and_cancel },
1.239     nicm     1943:        { "cursor-down", 0, 0, 1,
1.213     nicm     1944:          window_copy_cmd_cursor_down },
1.239     nicm     1945:        { "cursor-down-and-cancel", 0, 0, 0,
1.234     nicm     1946:          window_copy_cmd_cursor_down_and_cancel },
1.239     nicm     1947:        { "cursor-left", 0, 0, 1,
1.213     nicm     1948:          window_copy_cmd_cursor_left },
1.239     nicm     1949:        { "cursor-right", 0, 0, 1,
1.213     nicm     1950:          window_copy_cmd_cursor_right },
1.239     nicm     1951:        { "cursor-up", 0, 0, 1,
1.213     nicm     1952:          window_copy_cmd_cursor_up },
1.239     nicm     1953:        { "end-of-line", 0, 0, 1,
1.213     nicm     1954:          window_copy_cmd_end_of_line },
1.239     nicm     1955:        { "goto-line", 1, 1, 1,
1.213     nicm     1956:          window_copy_cmd_goto_line },
1.239     nicm     1957:        { "halfpage-down", 0, 0, 1,
1.213     nicm     1958:          window_copy_cmd_halfpage_down },
1.239     nicm     1959:        { "halfpage-down-and-cancel", 0, 0, 0,
1.213     nicm     1960:          window_copy_cmd_halfpage_down_and_cancel },
1.239     nicm     1961:        { "halfpage-up", 0, 0, 1,
1.213     nicm     1962:          window_copy_cmd_halfpage_up },
1.239     nicm     1963:        { "history-bottom", 0, 0, 1,
1.213     nicm     1964:          window_copy_cmd_history_bottom },
1.239     nicm     1965:        { "history-top", 0, 0, 1,
1.213     nicm     1966:          window_copy_cmd_history_top },
1.239     nicm     1967:        { "jump-again", 0, 0, 1,
1.213     nicm     1968:          window_copy_cmd_jump_again },
1.239     nicm     1969:        { "jump-backward", 1, 1, 1,
1.213     nicm     1970:          window_copy_cmd_jump_backward },
1.239     nicm     1971:        { "jump-forward", 1, 1, 1,
1.213     nicm     1972:          window_copy_cmd_jump_forward },
1.239     nicm     1973:        { "jump-reverse", 0, 0, 1,
1.213     nicm     1974:          window_copy_cmd_jump_reverse },
1.239     nicm     1975:        { "jump-to-backward", 1, 1, 1,
1.213     nicm     1976:          window_copy_cmd_jump_to_backward },
1.239     nicm     1977:        { "jump-to-forward", 1, 1, 1,
1.213     nicm     1978:          window_copy_cmd_jump_to_forward },
1.239     nicm     1979:        { "middle-line", 0, 0, 1,
1.213     nicm     1980:          window_copy_cmd_middle_line },
1.239     nicm     1981:        { "next-matching-bracket", 0, 0, 0,
1.218     nicm     1982:          window_copy_cmd_next_matching_bracket },
1.239     nicm     1983:        { "next-paragraph", 0, 0, 1,
1.213     nicm     1984:          window_copy_cmd_next_paragraph },
1.239     nicm     1985:        { "next-space", 0, 0, 1,
1.213     nicm     1986:          window_copy_cmd_next_space },
1.239     nicm     1987:        { "next-space-end", 0, 0, 1,
1.213     nicm     1988:          window_copy_cmd_next_space_end },
1.239     nicm     1989:        { "next-word", 0, 0, 1,
1.213     nicm     1990:          window_copy_cmd_next_word },
1.239     nicm     1991:        { "next-word-end", 0, 0, 1,
1.213     nicm     1992:          window_copy_cmd_next_word_end },
1.239     nicm     1993:        { "other-end", 0, 0, 1,
1.213     nicm     1994:          window_copy_cmd_other_end },
1.239     nicm     1995:        { "page-down", 0, 0, 1,
1.213     nicm     1996:          window_copy_cmd_page_down },
1.239     nicm     1997:        { "page-down-and-cancel", 0, 0, 0,
1.213     nicm     1998:          window_copy_cmd_page_down_and_cancel },
1.239     nicm     1999:        { "page-up", 0, 0, 1,
1.213     nicm     2000:          window_copy_cmd_page_up },
1.239     nicm     2001:        { "previous-matching-bracket", 0, 0, 0,
1.218     nicm     2002:          window_copy_cmd_previous_matching_bracket },
1.239     nicm     2003:        { "previous-paragraph", 0, 0, 1,
1.213     nicm     2004:          window_copy_cmd_previous_paragraph },
1.239     nicm     2005:        { "previous-space", 0, 0, 1,
1.213     nicm     2006:          window_copy_cmd_previous_space },
1.239     nicm     2007:        { "previous-word", 0, 0, 1,
1.213     nicm     2008:          window_copy_cmd_previous_word },
1.239     nicm     2009:        { "rectangle-toggle", 0, 0, 0,
1.213     nicm     2010:          window_copy_cmd_rectangle_toggle },
1.239     nicm     2011:        { "scroll-down", 0, 0, 1,
1.213     nicm     2012:          window_copy_cmd_scroll_down },
1.239     nicm     2013:        { "scroll-down-and-cancel", 0, 0, 0,
1.213     nicm     2014:          window_copy_cmd_scroll_down_and_cancel },
1.239     nicm     2015:        { "scroll-up", 0, 0, 1,
1.213     nicm     2016:          window_copy_cmd_scroll_up },
1.239     nicm     2017:        { "search-again", 0, 0, 0,
1.213     nicm     2018:          window_copy_cmd_search_again },
1.239     nicm     2019:        { "search-backward", 0, 1, 0,
1.213     nicm     2020:          window_copy_cmd_search_backward },
1.239     nicm     2021:        { "search-backward-incremental", 1, 1, 0,
1.213     nicm     2022:          window_copy_cmd_search_backward_incremental },
1.239     nicm     2023:        { "search-forward", 0, 1, 0,
1.213     nicm     2024:          window_copy_cmd_search_forward },
1.239     nicm     2025:        { "search-forward-incremental", 1, 1, 0,
1.213     nicm     2026:          window_copy_cmd_search_forward_incremental },
1.239     nicm     2027:        { "search-reverse", 0, 0, 0,
1.213     nicm     2028:          window_copy_cmd_search_reverse },
1.239     nicm     2029:        { "select-line", 0, 0, 0,
1.213     nicm     2030:          window_copy_cmd_select_line },
1.239     nicm     2031:        { "select-word", 0, 0, 0,
1.213     nicm     2032:          window_copy_cmd_select_word },
1.239     nicm     2033:        { "start-of-line", 0, 0, 1,
1.213     nicm     2034:          window_copy_cmd_start_of_line },
1.239     nicm     2035:        { "stop-selection", 0, 0, 0,
1.213     nicm     2036:          window_copy_cmd_stop_selection },
1.240     nicm     2037:        { "top-line", 0, 0, 1,
1.213     nicm     2038:          window_copy_cmd_top_line },
                   2039: };
                   2040:
1.157     nicm     2041: static void
1.208     nicm     2042: window_copy_command(struct window_mode_entry *wme, struct client *c,
1.215     nicm     2043:     struct session *s, struct winlink *wl, struct args *args,
1.208     nicm     2044:     struct mouse_event *m)
1.1       nicm     2045: {
1.208     nicm     2046:        struct window_copy_mode_data    *data = wme->data;
1.213     nicm     2047:        struct window_copy_cmd_state     cs;
                   2048:        enum window_copy_cmd_action      action;
                   2049:        const char                      *command;
                   2050:        u_int                            i;
1.241     nicm     2051:        int                              ismotion = 0, keys;
1.155     nicm     2052:
                   2053:        if (args->argc == 0)
                   2054:                return;
                   2055:        command = args->argv[0];
1.21      nicm     2056:
1.202     nicm     2057:        if (m != NULL && m->valid && !MOUSE_WHEEL(m->b))
1.156     nicm     2058:                window_copy_move_mouse(m);
                   2059:
1.213     nicm     2060:        cs.wme = wme;
                   2061:        cs.args = args;
                   2062:        cs.m = m;
1.215     nicm     2063:
1.213     nicm     2064:        cs.c = c;
                   2065:        cs.s = s;
1.215     nicm     2066:        cs.wl = wl;
1.213     nicm     2067:
                   2068:        action = WINDOW_COPY_CMD_NOTHING;
                   2069:        for (i = 0; i < nitems(window_copy_cmd_table); i++) {
                   2070:                if (strcmp(window_copy_cmd_table[i].command, command) == 0) {
                   2071:                        if (args->argc - 1 < window_copy_cmd_table[i].minargs ||
                   2072:                            args->argc - 1 > window_copy_cmd_table[i].maxargs)
1.155     nicm     2073:                                break;
1.239     nicm     2074:                        ismotion = window_copy_cmd_table[i].ismotion;
1.213     nicm     2075:                        action = window_copy_cmd_table[i].f (&cs);
                   2076:                        break;
1.163     nicm     2077:                }
1.1       nicm     2078:        }
1.21      nicm     2079:
1.162     nicm     2080:        if (strncmp(command, "search-", 7) != 0 && data->searchmark != NULL) {
1.239     nicm     2081:                keys = options_get_number(wme->wp->window->options, "mode-keys");
                   2082:                if (keys != MODEKEY_VI || !ismotion) {
                   2083:                        window_copy_clear_marks(wme);
                   2084:                        data->searchx = data->searchy = -1;
                   2085:                } else if (data->searchthis != -1) {
                   2086:                        data->searchthis = -1;
                   2087:                        action = WINDOW_COPY_CMD_REDRAW;
                   2088:                }
1.213     nicm     2089:                if (action == WINDOW_COPY_CMD_NOTHING)
                   2090:                        action = WINDOW_COPY_CMD_REDRAW;
1.162     nicm     2091:        }
1.209     nicm     2092:        wme->prefix = 1;
                   2093:
1.213     nicm     2094:        if (action == WINDOW_COPY_CMD_CANCEL)
                   2095:                window_pane_reset_mode(wme->wp);
                   2096:        else if (action == WINDOW_COPY_CMD_REDRAW)
1.208     nicm     2097:                window_copy_redraw_screen(wme);
1.50      nicm     2098: }
                   2099:
1.157     nicm     2100: static void
1.208     nicm     2101: window_copy_scroll_to(struct window_mode_entry *wme, u_int px, u_int py)
1.21      nicm     2102: {
1.208     nicm     2103:        struct window_copy_mode_data    *data = wme->data;
1.54      nicm     2104:        struct grid                     *gd = data->backing->grid;
1.21      nicm     2105:        u_int                            offset, gap;
                   2106:
                   2107:        data->cx = px;
                   2108:
1.185     nicm     2109:        if (py >= gd->hsize - data->oy && py < gd->hsize - data->oy + gd->sy)
                   2110:                data->cy = py - (gd->hsize - data->oy);
                   2111:        else {
                   2112:                gap = gd->sy / 4;
                   2113:                if (py < gd->sy) {
                   2114:                        offset = 0;
                   2115:                        data->cy = py;
                   2116:                } else if (py > gd->hsize + gd->sy - gap) {
                   2117:                        offset = gd->hsize;
                   2118:                        data->cy = py - gd->hsize;
                   2119:                } else {
                   2120:                        offset = py + gap - gd->sy;
                   2121:                        data->cy = py - offset;
                   2122:                }
                   2123:                data->oy = gd->hsize - offset;
1.21      nicm     2124:        }
                   2125:
1.252     nicm     2126:        window_copy_update_selection(wme, 1, 0);
1.208     nicm     2127:        window_copy_redraw_screen(wme);
1.21      nicm     2128: }
                   2129:
1.157     nicm     2130: static int
1.118     nicm     2131: window_copy_search_compare(struct grid *gd, u_int px, u_int py,
                   2132:     struct grid *sgd, u_int spx, int cis)
1.21      nicm     2133: {
1.140     nicm     2134:        struct grid_cell         gc, sgc;
                   2135:        const struct utf8_data  *ud, *sud;
1.21      nicm     2136:
1.140     nicm     2137:        grid_get_cell(gd, px, py, &gc);
                   2138:        ud = &gc.data;
                   2139:        grid_get_cell(sgd, spx, 0, &sgc);
                   2140:        sud = &sgc.data;
1.35      nicm     2141:
1.140     nicm     2142:        if (ud->size != sud->size || ud->width != sud->width)
1.21      nicm     2143:                return (0);
1.97      nicm     2144:
1.140     nicm     2145:        if (cis && ud->size == 1)
                   2146:                return (tolower(ud->data[0]) == sud->data[0]);
1.97      nicm     2147:
1.140     nicm     2148:        return (memcmp(ud->data, sud->data, ud->size) == 0);
1.21      nicm     2149: }
                   2150:
1.157     nicm     2151: static int
1.21      nicm     2152: window_copy_search_lr(struct grid *gd,
1.97      nicm     2153:     struct grid *sgd, u_int *ppx, u_int py, u_int first, u_int last, int cis)
1.21      nicm     2154: {
1.243     nicm     2155:        u_int                    ax, bx, px, pywrap, endline;
                   2156:        int                      matched;
                   2157:        struct grid_line        *gl;
1.21      nicm     2158:
1.242     nicm     2159:        endline = gd->hsize + gd->sy - 1;
1.21      nicm     2160:        for (ax = first; ax < last; ax++) {
                   2161:                for (bx = 0; bx < sgd->sx; bx++) {
                   2162:                        px = ax + bx;
1.242     nicm     2163:                        pywrap = py;
                   2164:                        /* Wrap line. */
                   2165:                        while (px >= gd->sx && pywrap < endline) {
1.243     nicm     2166:                                gl = grid_get_line(gd, pywrap);
                   2167:                                if (~gl->flags & GRID_LINE_WRAPPED)
                   2168:                                        break;
1.242     nicm     2169:                                px -= gd->sx;
                   2170:                                pywrap++;
                   2171:                        }
                   2172:                        /* We have run off the end of the grid. */
                   2173:                        if (px >= gd->sx)
                   2174:                                break;
                   2175:                        matched = window_copy_search_compare(gd, px, pywrap,
                   2176:                            sgd, bx, cis);
1.97      nicm     2177:                        if (!matched)
1.21      nicm     2178:                                break;
                   2179:                }
                   2180:                if (bx == sgd->sx) {
                   2181:                        *ppx = ax;
                   2182:                        return (1);
                   2183:                }
                   2184:        }
                   2185:        return (0);
                   2186: }
                   2187:
1.157     nicm     2188: static int
1.21      nicm     2189: window_copy_search_rl(struct grid *gd,
1.97      nicm     2190:     struct grid *sgd, u_int *ppx, u_int py, u_int first, u_int last, int cis)
1.21      nicm     2191: {
1.243     nicm     2192:        u_int                    ax, bx, px, pywrap, endline;
                   2193:        int                      matched;
                   2194:        struct grid_line        *gl;
1.21      nicm     2195:
1.242     nicm     2196:        endline = gd->hsize + gd->sy - 1;
                   2197:        for (ax = last; ax > first; ax--) {
1.21      nicm     2198:                for (bx = 0; bx < sgd->sx; bx++) {
                   2199:                        px = ax - 1 + bx;
1.242     nicm     2200:                        pywrap = py;
                   2201:                        /* Wrap line. */
                   2202:                        while (px >= gd->sx && pywrap < endline) {
1.243     nicm     2203:                                gl = grid_get_line(gd, pywrap);
                   2204:                                if (~gl->flags & GRID_LINE_WRAPPED)
                   2205:                                        break;
1.242     nicm     2206:                                px -= gd->sx;
                   2207:                                pywrap++;
                   2208:                        }
                   2209:                        /* We have run off the end of the grid. */
                   2210:                        if (px >= gd->sx)
                   2211:                                break;
                   2212:                        matched = window_copy_search_compare(gd, px, pywrap,
                   2213:                            sgd, bx, cis);
1.97      nicm     2214:                        if (!matched)
1.21      nicm     2215:                                break;
                   2216:                }
                   2217:                if (bx == sgd->sx) {
                   2218:                        *ppx = ax - 1;
                   2219:                        return (1);
                   2220:                }
                   2221:        }
                   2222:        return (0);
                   2223: }
                   2224:
1.244     nicm     2225: static int
                   2226: window_copy_search_lr_regex(struct grid *gd, struct grid *sgd,
                   2227:     u_int *ppx, u_int *psx, u_int py, u_int first, u_int last, int cis)
                   2228: {
                   2229:        int                     cflags = REG_EXTENDED, eflags = 0;
                   2230:        u_int                   endline, foundx, foundy, len, pywrap, size = 1;
                   2231:        u_int                   ssize = 1;
                   2232:        char                   *buf, *sbuf;
                   2233:        regex_t                 reg;
                   2234:        regmatch_t              regmatch;
                   2235:        struct grid_line       *gl;
                   2236:
                   2237:        /*
                   2238:         * This can happen during search if the last match was the last
                   2239:         * character on a line.
                   2240:         */
                   2241:        if (first >= last)
                   2242:                return (0);
                   2243:
                   2244:        sbuf = xmalloc(ssize);
                   2245:        sbuf[0] = '\0';
                   2246:        sbuf = window_copy_stringify(sgd, 0, 0, sgd->sx, sbuf, &ssize);
                   2247:        if (sbuf == NULL)
                   2248:                return (0);
                   2249:
                   2250:        /* Set flags for regex search. */
                   2251:        if (cis)
                   2252:                cflags |= REG_ICASE;
                   2253:        if (regcomp(&reg, sbuf, cflags) != 0) {
                   2254:                free(sbuf);
                   2255:                return (0);
                   2256:        }
                   2257:        if (first != 0)
                   2258:                eflags |= REG_NOTBOL;
                   2259:
                   2260:        /* Need to look at the entire string. */
                   2261:        buf = xmalloc(size);
                   2262:        buf[0] = '\0';
                   2263:        buf = window_copy_stringify(gd, py, first, gd->sx, buf, &size);
                   2264:        len = gd->sx - first;
                   2265:        endline = gd->hsize + gd->sy - 1;
                   2266:        pywrap = py;
                   2267:        while (buf != NULL && pywrap <= endline) {
                   2268:                gl = grid_get_line(gd, pywrap);
                   2269:                if (~gl->flags & GRID_LINE_WRAPPED)
                   2270:                        break;
                   2271:                pywrap++;
                   2272:                buf = window_copy_stringify(gd, pywrap, 0, gd->sx, buf, &size);
                   2273:                len += gd->sx;
                   2274:        }
                   2275:
                   2276:        if (regexec(&reg, buf, 1, &regmatch, eflags) == 0) {
                   2277:                foundx = first;
                   2278:                foundy = py;
                   2279:                window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
                   2280:                    buf + regmatch.rm_so);
                   2281:                if (foundy == py && foundx < last) {
                   2282:                        *ppx = foundx;
                   2283:                        len -= foundx - first;
                   2284:                        window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
                   2285:                            buf + regmatch.rm_eo);
                   2286:                        *psx = foundx;
                   2287:                        while (foundy > py) {
                   2288:                                *psx += gd->sx;
                   2289:                                foundy--;
                   2290:                        }
                   2291:                        *psx -= *ppx;
                   2292:                        regfree(&reg);
                   2293:                        free(sbuf);
                   2294:                        free(buf);
                   2295:                        return (1);
                   2296:                }
                   2297:        }
                   2298:
                   2299:        regfree(&reg);
                   2300:        free(sbuf);
                   2301:        free(buf);
                   2302:        *ppx = 0;
                   2303:        *psx = 0;
                   2304:        return (0);
                   2305: }
                   2306:
                   2307: static int
                   2308: window_copy_search_rl_regex(struct grid *gd, struct grid *sgd,
                   2309:     u_int *ppx, u_int *psx, u_int py, u_int first, u_int last, int cis)
                   2310: {
                   2311:        int                     cflags = REG_EXTENDED, eflags = 0;
                   2312:        u_int                   endline, len, pywrap, size = 1, ssize = 1;
                   2313:        char                   *buf, *sbuf;
                   2314:        regex_t                 reg;
                   2315:        struct grid_line       *gl;
                   2316:
                   2317:        sbuf = xmalloc(ssize);
                   2318:        sbuf[0] = '\0';
                   2319:        sbuf = window_copy_stringify(sgd, 0, 0, sgd->sx, sbuf, &ssize);
                   2320:        if (sbuf == NULL)
                   2321:                return (0);
                   2322:
                   2323:        /* Set flags for regex search. */
                   2324:        if (cis)
                   2325:                cflags |= REG_ICASE;
                   2326:        if (regcomp(&reg, sbuf, cflags) != 0) {
                   2327:                free(sbuf);
                   2328:                return (0);
                   2329:        }
                   2330:        if (first != 0)
                   2331:                eflags |= REG_NOTBOL;
                   2332:
                   2333:        /* Need to look at the entire string. */
                   2334:        buf = xmalloc(size);
                   2335:        buf[0] = '\0';
                   2336:        buf = window_copy_stringify(gd, py, first, gd->sx, buf, &size);
                   2337:        len = gd->sx - first;
                   2338:        endline = gd->hsize + gd->sy - 1;
                   2339:        pywrap = py;
                   2340:        while (buf != NULL && (pywrap <= endline)) {
                   2341:                gl = grid_get_line(gd, pywrap);
                   2342:                if (~gl->flags & GRID_LINE_WRAPPED)
                   2343:                        break;
                   2344:                pywrap++;
                   2345:                buf = window_copy_stringify(gd, pywrap, 0, gd->sx, buf, &size);
                   2346:                len += gd->sx;
                   2347:        }
                   2348:
                   2349:        if (window_copy_last_regex(gd, py, first, last, len, ppx, psx, buf,
                   2350:            &reg, eflags))
                   2351:        {
                   2352:                regfree(&reg);
                   2353:                free(sbuf);
                   2354:                free(buf);
                   2355:                return (1);
                   2356:        }
                   2357:
                   2358:        regfree(&reg);
                   2359:        free(sbuf);
                   2360:        free(buf);
                   2361:        *ppx = 0;
                   2362:        *psx = 0;
                   2363:        return (0);
                   2364: }
                   2365:
                   2366: /* Find last match in given range. */
                   2367: static int
                   2368: window_copy_last_regex(struct grid *gd, u_int py, u_int first, u_int last,
                   2369:     u_int len, u_int *ppx, u_int *psx, const char *buf, const regex_t *preg,
                   2370:     int eflags)
                   2371: {
                   2372:        u_int           foundx, foundy, oldx, px = 0, savepx, savesx = 0;
                   2373:        regmatch_t      regmatch;
                   2374:
                   2375:        foundx = first;
                   2376:        foundy = py;
                   2377:        oldx = first;
                   2378:        while (regexec(preg, buf + px, 1, &regmatch, eflags) == 0) {
                   2379:                window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
                   2380:                    buf + px + regmatch.rm_so);
                   2381:                if (foundy > py || foundx >= last)
                   2382:                        break;
                   2383:                len -= foundx - oldx;
                   2384:                savepx = foundx;
                   2385:                window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
                   2386:                    buf + px + regmatch.rm_eo);
                   2387:                if (foundy > py || foundx >= last) {
                   2388:                        *ppx = savepx;
                   2389:                        *psx = foundx;
                   2390:                        while (foundy > py) {
                   2391:                                *psx += gd->sx;
                   2392:                                foundy--;
                   2393:                        }
                   2394:                        *psx -= *ppx;
                   2395:                        return (1);
                   2396:                } else {
                   2397:                        savesx = foundx - savepx;
                   2398:                        len -= savesx;
                   2399:                        oldx = foundx;
                   2400:                }
                   2401:                px += regmatch.rm_eo;
                   2402:        }
                   2403:
                   2404:        if (savesx > 0) {
                   2405:                *ppx = savepx;
                   2406:                *psx = savesx;
                   2407:                return (1);
                   2408:        } else {
                   2409:                *ppx = 0;
                   2410:                *psx = 0;
                   2411:                return (0);
                   2412:        }
                   2413: }
                   2414:
                   2415: /* Stringify line and append to input buffer. Caller frees. */
                   2416: static char *
                   2417: window_copy_stringify(struct grid *gd, u_int py, u_int first, u_int last,
                   2418:     char *buf, u_int *size)
                   2419: {
                   2420:        u_int                   ax, bx, newsize;
                   2421:        struct grid_cell        gc;
                   2422:
                   2423:        bx = *size - 1;
                   2424:        newsize = *size;
                   2425:        for (ax = first; ax < last; ax++) {
                   2426:                grid_get_cell(gd, ax, py, &gc);
                   2427:                newsize += gc.data.size;
                   2428:                buf = xrealloc(buf, newsize);
                   2429:                memcpy(buf + bx, gc.data.data, gc.data.size);
                   2430:                bx += gc.data.size;
                   2431:        }
                   2432:
                   2433:        buf[newsize - 1] = '\0';
                   2434:        *size = newsize;
                   2435:        return (buf);
                   2436: }
                   2437:
                   2438: /* Map start of C string containing UTF-8 data to grid cell position. */
                   2439: static void
                   2440: window_copy_cstrtocellpos(struct grid *gd, u_int ncells, u_int *ppx, u_int *ppy,
                   2441:     const char *str)
                   2442: {
                   2443:        u_int                   cell, ccell, px, pywrap;
                   2444:        int                     match;
                   2445:        const char             *cstr;
                   2446:        char                   *celldata, **cells;
                   2447:        struct grid_cell        gc;
                   2448:
                   2449:        /* Set up staggered array of cell contents. This speeds up search. */
                   2450:        cells = xreallocarray(NULL, ncells, sizeof cells[0]);
                   2451:
                   2452:        /* Populate the array of cell data. */
                   2453:        cell = 0;
                   2454:        px = *ppx;
                   2455:        pywrap = *ppy;
                   2456:        while (cell < ncells) {
                   2457:                grid_get_cell(gd, px, pywrap, &gc);
                   2458:                celldata = xmalloc(gc.data.size + 1);
                   2459:                memcpy(celldata, gc.data.data, gc.data.size);
                   2460:                celldata[gc.data.size] = '\0';
                   2461:                cells[cell] = celldata;
                   2462:                cell++;
                   2463:                px = (px + 1) % gd->sx;
                   2464:                if (px == 0)
                   2465:                        pywrap++;
                   2466:        }
                   2467:
                   2468:        /* Locate starting cell. */
                   2469:        cell = 0;
                   2470:        while (cell < ncells) {
                   2471:                ccell = cell;
                   2472:                cstr = str;
                   2473:                match = 1;
                   2474:                while (ccell < ncells) {
                   2475:                        /* Anchor found to the end. */
                   2476:                        if (*cstr == '\0') {
                   2477:                                match = 0;
                   2478:                                break;
                   2479:                        }
                   2480:
                   2481:                        celldata = cells[ccell];
                   2482:                        while (*celldata != '\0' && *cstr != '\0') {
                   2483:                                if (*celldata++ != *cstr++) {
                   2484:                                        match = 0;
                   2485:                                        break;
                   2486:                                }
                   2487:                        }
                   2488:
                   2489:                        if (!match)
                   2490:                                break;
                   2491:                        ccell++;
                   2492:                }
                   2493:
                   2494:                if (match)
                   2495:                        break;
                   2496:                cell++;
                   2497:        }
                   2498:
                   2499:        /* If not found this will be one past the end. */
                   2500:        px = *ppx + cell;
                   2501:        pywrap = *ppy;
                   2502:        while (px >= gd->sx) {
                   2503:                px -= gd->sx;
                   2504:                pywrap++;
                   2505:        }
                   2506:
                   2507:        *ppx = px;
                   2508:        *ppy = pywrap;
                   2509:
                   2510:        /* Free cell data. */
                   2511:        for (cell = 0; cell < ncells; cell++)
                   2512:                free(cells[cell]);
                   2513:        free(cells);
                   2514: }
                   2515:
1.157     nicm     2516: static void
1.230     nicm     2517: window_copy_move_left(struct screen *s, u_int *fx, u_int *fy, int wrapflag)
1.21      nicm     2518: {
1.150     nicm     2519:        if (*fx == 0) { /* left */
1.230     nicm     2520:                if (*fy == 0) { /* top */
                   2521:                        if (wrapflag) {
                   2522:                                *fx = screen_size_x(s) - 1;
1.242     nicm     2523:                                *fy = screen_hsize(s) + screen_size_y(s) - 1;
1.230     nicm     2524:                        }
1.150     nicm     2525:                        return;
1.230     nicm     2526:                }
1.150     nicm     2527:                *fx = screen_size_x(s) - 1;
                   2528:                *fy = *fy - 1;
                   2529:        } else
                   2530:                *fx = *fx - 1;
                   2531: }
1.21      nicm     2532:
1.157     nicm     2533: static void
1.230     nicm     2534: window_copy_move_right(struct screen *s, u_int *fx, u_int *fy, int wrapflag)
1.150     nicm     2535: {
                   2536:        if (*fx == screen_size_x(s) - 1) { /* right */
1.242     nicm     2537:                if (*fy == screen_hsize(s) + screen_size_y(s) - 1) { /* bottom */
1.230     nicm     2538:                        if (wrapflag) {
                   2539:                                *fx = 0;
                   2540:                                *fy = 0;
                   2541:                        }
1.21      nicm     2542:                        return;
1.230     nicm     2543:                }
1.150     nicm     2544:                *fx = 0;
                   2545:                *fy = *fy + 1;
1.21      nicm     2546:        } else
1.150     nicm     2547:                *fx = *fx + 1;
                   2548: }
1.21      nicm     2549:
1.157     nicm     2550: static int
1.150     nicm     2551: window_copy_is_lowercase(const char *ptr)
                   2552: {
                   2553:        while (*ptr != '\0') {
                   2554:                if (*ptr != tolower((u_char)*ptr))
                   2555:                        return (0);
                   2556:                ++ptr;
1.97      nicm     2557:        }
1.150     nicm     2558:        return (1);
                   2559: }
1.97      nicm     2560:
1.150     nicm     2561: /*
                   2562:  * Search for text stored in sgd starting from position fx,fy up to endline. If
                   2563:  * found, jump to it. If cis then ignore case. The direction is 0 for searching
                   2564:  * up, down otherwise. If wrap then go to begin/end of grid and try again if
                   2565:  * not found.
                   2566:  */
1.163     nicm     2567: static int
1.208     nicm     2568: window_copy_search_jump(struct window_mode_entry *wme, struct grid *gd,
1.150     nicm     2569:     struct grid *sgd, u_int fx, u_int fy, u_int endline, int cis, int wrap,
1.244     nicm     2570:     int direction, int regex)
1.150     nicm     2571: {
1.244     nicm     2572:        u_int   i, px, sx;
                   2573:        int     found = 0;
1.150     nicm     2574:
                   2575:        if (direction) {
                   2576:                for (i = fy; i <= endline; i++) {
1.244     nicm     2577:                        if (regex)
                   2578:                                found = window_copy_search_lr_regex(gd, sgd,
                   2579:                                    &px, &sx, i, fx, gd->sx, cis);
                   2580:                        else
                   2581:                                found = window_copy_search_lr(gd, sgd,
                   2582:                                    &px, i, fx, gd->sx, cis);
1.150     nicm     2583:                        if (found)
                   2584:                                break;
                   2585:                        fx = 0;
                   2586:                }
                   2587:        } else {
                   2588:                for (i = fy + 1; endline < i; i--) {
1.244     nicm     2589:                        if (regex)
                   2590:                                found = window_copy_search_rl_regex(gd, sgd,
                   2591:                                    &px, &sx, i - 1, 0, fx + 1, cis);
                   2592:                        else
                   2593:                                found = window_copy_search_rl(gd, sgd,
                   2594:                                    &px, i - 1, 0, fx + 1, cis);
1.150     nicm     2595:                        if (found) {
                   2596:                                i--;
                   2597:                                break;
                   2598:                        }
1.242     nicm     2599:                        fx = gd->sx - 1;
1.21      nicm     2600:                }
                   2601:        }
1.150     nicm     2602:
1.163     nicm     2603:        if (found) {
1.208     nicm     2604:                window_copy_scroll_to(wme, px, i);
1.163     nicm     2605:                return (1);
                   2606:        }
                   2607:        if (wrap) {
1.208     nicm     2608:                return (window_copy_search_jump(wme, gd, sgd,
1.163     nicm     2609:                    direction ? 0 : gd->sx - 1,
1.150     nicm     2610:                    direction ? 0 : gd->hsize + gd->sy - 1, fy, cis, 0,
1.244     nicm     2611:                    direction, regex));
1.21      nicm     2612:        }
1.163     nicm     2613:        return (0);
1.21      nicm     2614: }
                   2615:
1.232     nicm     2616: /*
                   2617:  * Search in for text searchstr. If direction is 0 then search up, otherwise
                   2618:  * down.
                   2619:  */
1.163     nicm     2620: static int
1.244     nicm     2621: window_copy_search(struct window_mode_entry *wme, int direction, int regex)
1.21      nicm     2622: {
1.208     nicm     2623:        struct window_pane              *wp = wme->wp;
                   2624:        struct window_copy_mode_data    *data = wme->data;
1.54      nicm     2625:        struct screen                   *s = data->backing, ss;
1.21      nicm     2626:        struct screen_write_ctx          ctx;
1.150     nicm     2627:        struct grid                     *gd = s->grid;
                   2628:        u_int                            fx, fy, endline;
1.163     nicm     2629:        int                              wrapflag, cis, found;
1.21      nicm     2630:
1.174     nicm     2631:        free(wp->searchstr);
                   2632:        wp->searchstr = xstrdup(data->searchstr);
                   2633:
1.150     nicm     2634:        fx = data->cx;
                   2635:        fy = screen_hsize(data->backing) - data->oy + data->cy;
1.21      nicm     2636:
1.162     nicm     2637:        screen_init(&ss, screen_write_strlen("%s", data->searchstr), 1, 0);
1.21      nicm     2638:        screen_write_start(&ctx, NULL, &ss);
1.162     nicm     2639:        screen_write_nputs(&ctx, -1, &grid_default_cell, "%s", data->searchstr);
1.21      nicm     2640:        screen_write_stop(&ctx);
                   2641:
1.150     nicm     2642:        wrapflag = options_get_number(wp->window->options, "wrap-search");
1.162     nicm     2643:        cis = window_copy_is_lowercase(data->searchstr);
1.21      nicm     2644:
1.230     nicm     2645:        if (direction) {
                   2646:                window_copy_move_right(s, &fx, &fy, wrapflag);
1.150     nicm     2647:                endline = gd->hsize + gd->sy - 1;
1.230     nicm     2648:        } else {
                   2649:                window_copy_move_left(s, &fx, &fy, wrapflag);
1.150     nicm     2650:                endline = 0;
1.230     nicm     2651:        }
1.244     nicm     2652:
1.208     nicm     2653:        found = window_copy_search_jump(wme, gd, ss.grid, fx, fy, endline, cis,
1.244     nicm     2654:            wrapflag, direction, regex);
1.162     nicm     2655:
1.244     nicm     2656:        if (window_copy_search_marks(wme, &ss, regex))
1.208     nicm     2657:                window_copy_redraw_screen(wme);
1.21      nicm     2658:
1.150     nicm     2659:        screen_free(&ss);
1.163     nicm     2660:        return (found);
1.150     nicm     2661: }
1.97      nicm     2662:
1.162     nicm     2663: static int
1.244     nicm     2664: window_copy_search_marks(struct window_mode_entry *wme, struct screen *ssp,
                   2665:     int regex)
1.162     nicm     2666: {
1.208     nicm     2667:        struct window_copy_mode_data    *data = wme->data;
1.162     nicm     2668:        struct screen                   *s = data->backing, ss;
                   2669:        struct screen_write_ctx          ctx;
                   2670:        struct grid                     *gd = s->grid;
1.170     nicm     2671:        int                              found, cis, which = -1;
1.162     nicm     2672:        u_int                            px, py, b, nfound = 0, width;
                   2673:
                   2674:        if (ssp == NULL) {
                   2675:                width = screen_write_strlen("%s", data->searchstr);
                   2676:                screen_init(&ss, width, 1, 0);
                   2677:                screen_write_start(&ctx, NULL, &ss);
                   2678:                screen_write_nputs(&ctx, -1, &grid_default_cell, "%s",
                   2679:                    data->searchstr);
                   2680:                screen_write_stop(&ctx);
                   2681:                ssp = &ss;
                   2682:        } else
                   2683:                width = screen_size_x(ssp);
                   2684:
                   2685:        cis = window_copy_is_lowercase(data->searchstr);
                   2686:
                   2687:        free(data->searchmark);
                   2688:        data->searchmark = bit_alloc((gd->hsize + gd->sy) * gd->sx);
                   2689:
                   2690:        for (py = 0; py < gd->hsize + gd->sy; py++) {
                   2691:                px = 0;
                   2692:                for (;;) {
1.244     nicm     2693:                        if (regex) {
                   2694:                                found = window_copy_search_lr_regex(gd,
                   2695:                                            ssp->grid, &px, &width, py, px,
                   2696:                                            gd->sx, cis);
                   2697:                                if (!found)
                   2698:                                        break;
                   2699:                        }
                   2700:                        else {
                   2701:                                found = window_copy_search_lr(gd, ssp->grid,
                   2702:                                                &px, py, px, gd->sx, cis);
                   2703:                                if (!found)
                   2704:                                        break;
                   2705:                        }
1.170     nicm     2706:
1.162     nicm     2707:                        nfound++;
1.170     nicm     2708:                        if (px == data->cx && py == gd->hsize + data->cy - data->oy)
                   2709:                                which = nfound;
1.162     nicm     2710:
                   2711:                        b = (py * gd->sx) + px;
                   2712:                        bit_nset(data->searchmark, b, b + width - 1);
                   2713:
                   2714:                        px++;
                   2715:                }
                   2716:        }
                   2717:
1.170     nicm     2718:        if (which != -1)
                   2719:                data->searchthis = 1 + nfound - which;
                   2720:        else
                   2721:                data->searchthis = -1;
                   2722:        data->searchcount = nfound;
                   2723:
1.162     nicm     2724:        if (ssp == &ss)
                   2725:                screen_free(&ss);
                   2726:        return (nfound);
                   2727: }
                   2728:
1.157     nicm     2729: static void
1.208     nicm     2730: window_copy_clear_marks(struct window_mode_entry *wme)
1.163     nicm     2731: {
1.208     nicm     2732:        struct window_copy_mode_data    *data = wme->data;
1.163     nicm     2733:
                   2734:        free(data->searchmark);
                   2735:        data->searchmark = NULL;
                   2736: }
                   2737:
                   2738: static int
1.244     nicm     2739: window_copy_search_up(struct window_mode_entry *wme, int regex)
1.150     nicm     2740: {
1.244     nicm     2741:        return (window_copy_search(wme, 0, regex));
1.150     nicm     2742: }
1.21      nicm     2743:
1.163     nicm     2744: static int
1.244     nicm     2745: window_copy_search_down(struct window_mode_entry *wme, int regex)
1.150     nicm     2746: {
1.244     nicm     2747:        return (window_copy_search(wme, 1, regex));
1.21      nicm     2748: }
                   2749:
1.157     nicm     2750: static void
1.208     nicm     2751: window_copy_goto_line(struct window_mode_entry *wme, const char *linestr)
1.21      nicm     2752: {
1.208     nicm     2753:        struct window_copy_mode_data    *data = wme->data;
1.21      nicm     2754:        const char                      *errstr;
1.199     nicm     2755:        int                              lineno;
1.21      nicm     2756:
1.199     nicm     2757:        lineno = strtonum(linestr, -1, INT_MAX, &errstr);
1.21      nicm     2758:        if (errstr != NULL)
                   2759:                return;
1.199     nicm     2760:        if (lineno < 0 || (u_int)lineno > screen_hsize(data->backing))
                   2761:                lineno = screen_hsize(data->backing);
1.35      nicm     2762:
1.21      nicm     2763:        data->oy = lineno;
1.252     nicm     2764:        window_copy_update_selection(wme, 1, 0);
1.208     nicm     2765:        window_copy_redraw_screen(wme);
1.21      nicm     2766: }
                   2767:
1.157     nicm     2768: static void
1.208     nicm     2769: window_copy_write_line(struct window_mode_entry *wme,
                   2770:     struct screen_write_ctx *ctx, u_int py)
1.1       nicm     2771: {
1.208     nicm     2772:        struct window_pane              *wp = wme->wp;
                   2773:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     2774:        struct screen                   *s = &data->screen;
1.136     nicm     2775:        struct options                  *oo = wp->window->options;
1.1       nicm     2776:        struct grid_cell                 gc;
1.100     nicm     2777:        char                             hdr[512];
1.162     nicm     2778:        size_t                           size = 0;
1.21      nicm     2779:
1.101     nicm     2780:        style_apply(&gc, oo, "mode-style");
1.164     nicm     2781:        gc.flags |= GRID_FLAG_NOPALETTE;
1.1       nicm     2782:
1.249     nicm     2783:        if (py == 0 && s->rupper < s->rlower && !data->hide_position) {
1.170     nicm     2784:                if (data->searchmark == NULL) {
                   2785:                        size = xsnprintf(hdr, sizeof hdr,
                   2786:                            "[%u/%u]", data->oy, screen_hsize(data->backing));
                   2787:                } else {
                   2788:                        if (data->searchthis == -1) {
                   2789:                                size = xsnprintf(hdr, sizeof hdr,
                   2790:                                    "(%u results) [%d/%u]", data->searchcount,
                   2791:                                    data->oy, screen_hsize(data->backing));
                   2792:                        } else {
                   2793:                                size = xsnprintf(hdr, sizeof hdr,
                   2794:                                    "(%u/%u results) [%d/%u]", data->searchthis,
                   2795:                                    data->searchcount, data->oy,
                   2796:                                    screen_hsize(data->backing));
                   2797:                        }
                   2798:                }
1.62      nicm     2799:                if (size > screen_size_x(s))
                   2800:                        size = screen_size_x(s);
1.212     nicm     2801:                screen_write_cursormove(ctx, screen_size_x(s) - size, 0, 0);
1.1       nicm     2802:                screen_write_puts(ctx, &gc, "%s", hdr);
                   2803:        } else
                   2804:                size = 0;
                   2805:
1.105     nicm     2806:        if (size < screen_size_x(s)) {
1.212     nicm     2807:                screen_write_cursormove(ctx, 0, py, 0);
1.162     nicm     2808:                screen_write_copy(ctx, data->backing, 0,
1.105     nicm     2809:                    (screen_hsize(data->backing) - data->oy) + py,
1.162     nicm     2810:                    screen_size_x(s) - size, 1, data->searchmark, &gc);
1.105     nicm     2811:        }
1.18      nicm     2812:
                   2813:        if (py == data->cy && data->cx == screen_size_x(s)) {
                   2814:                memcpy(&gc, &grid_default_cell, sizeof gc);
1.212     nicm     2815:                screen_write_cursormove(ctx, screen_size_x(s) - 1, py, 0);
1.18      nicm     2816:                screen_write_putc(ctx, &gc, '$');
                   2817:        }
1.1       nicm     2818: }
                   2819:
1.157     nicm     2820: static void
1.208     nicm     2821: window_copy_write_lines(struct window_mode_entry *wme,
                   2822:     struct screen_write_ctx *ctx, u_int py, u_int ny)
1.1       nicm     2823: {
                   2824:        u_int   yy;
                   2825:
                   2826:        for (yy = py; yy < py + ny; yy++)
1.208     nicm     2827:                window_copy_write_line(wme, ctx, py);
1.121     nicm     2828: }
                   2829:
1.157     nicm     2830: static void
1.208     nicm     2831: window_copy_redraw_selection(struct window_mode_entry *wme, u_int old_y)
1.121     nicm     2832: {
1.208     nicm     2833:        struct window_copy_mode_data    *data = wme->data;
1.247     nicm     2834:        struct grid                     *gd = data->backing->grid;
1.121     nicm     2835:        u_int                            new_y, start, end;
                   2836:
                   2837:        new_y = data->cy;
                   2838:        if (old_y <= new_y) {
                   2839:                start = old_y;
                   2840:                end = new_y;
                   2841:        } else {
                   2842:                start = new_y;
                   2843:                end = old_y;
                   2844:        }
1.247     nicm     2845:
                   2846:        /*
                   2847:         * In word selection mode the first word on the line below the cursor
                   2848:         * might be selected, so add this line to the redraw area.
                   2849:         */
                   2850:        if (data->selflag == SEL_WORD) {
                   2851:                /* Last grid line in data coordinates. */
                   2852:                if (end < gd->sy + data->oy - 1)
                   2853:                        end++;
                   2854:        }
1.208     nicm     2855:        window_copy_redraw_lines(wme, start, end - start + 1);
1.1       nicm     2856: }
                   2857:
1.157     nicm     2858: static void
1.208     nicm     2859: window_copy_redraw_lines(struct window_mode_entry *wme, u_int py, u_int ny)
1.1       nicm     2860: {
1.208     nicm     2861:        struct window_pane              *wp = wme->wp;
                   2862:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     2863:        struct screen_write_ctx          ctx;
                   2864:        u_int                            i;
                   2865:
                   2866:        screen_write_start(&ctx, wp, NULL);
                   2867:        for (i = py; i < py + ny; i++)
1.208     nicm     2868:                window_copy_write_line(wme, &ctx, i);
1.212     nicm     2869:        screen_write_cursormove(&ctx, data->cx, data->cy, 0);
1.1       nicm     2870:        screen_write_stop(&ctx);
                   2871: }
                   2872:
1.157     nicm     2873: static void
1.208     nicm     2874: window_copy_redraw_screen(struct window_mode_entry *wme)
1.1       nicm     2875: {
1.208     nicm     2876:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     2877:
1.208     nicm     2878:        window_copy_redraw_lines(wme, 0, screen_size_y(&data->screen));
1.1       nicm     2879: }
                   2880:
1.157     nicm     2881: static void
1.252     nicm     2882: window_copy_synchronize_cursor_end(struct window_mode_entry *wme, int begin,
                   2883:     int no_reset)
1.161     nicm     2884: {
1.208     nicm     2885:        struct window_copy_mode_data    *data = wme->data;
1.161     nicm     2886:        u_int                            xx, yy;
                   2887:
                   2888:        yy = screen_hsize(data->backing) + data->cy - data->oy;
1.247     nicm     2889:        switch (data->selflag) {
                   2890:        case SEL_WORD:
                   2891:                xx = data->cx;
1.252     nicm     2892:                if (no_reset)
1.247     nicm     2893:                        break;
1.252     nicm     2894:                begin = 0;
1.247     nicm     2895:                if (data->dy > yy || (data->dy == yy && data->dx > xx)) {
                   2896:                        /* Right to left selection. */
                   2897:                        window_copy_cursor_previous_word_pos(wme, data->ws, 0,
                   2898:                            &xx, &yy);
                   2899:                        begin = 1;
                   2900:
                   2901:                        /* Reset the end. */
                   2902:                        data->endselx = data->endselrx;
                   2903:                        data->endsely = data->endselry;
                   2904:                } else {
                   2905:                        /* Left to right selection. */
                   2906:                        if (xx >= window_copy_find_length(wme, yy) ||
                   2907:                            !window_copy_in_set(wme, xx + 1, yy, data->ws))
                   2908:                                window_copy_cursor_next_word_end_pos(wme,
                   2909:                                    data->ws, &xx, &yy);
                   2910:
                   2911:                        /* Reset the start. */
                   2912:                        data->selx = data->selrx;
                   2913:                        data->sely = data->selry;
                   2914:                }
                   2915:                break;
                   2916:        case SEL_LINE:
1.252     nicm     2917:                if (no_reset) {
                   2918:                        xx = data->cx;
                   2919:                        break;
                   2920:                }
1.251     nicm     2921:                begin = 0;
1.247     nicm     2922:                if (data->dy > yy) {
                   2923:                        /* Right to left selection. */
                   2924:                        xx = 0;
                   2925:                        begin = 1;
                   2926:
                   2927:                        /* Reset the end. */
                   2928:                        data->endselx = data->endselrx;
                   2929:                        data->endsely = data->endselry;
                   2930:                } else {
                   2931:                        /* Left to right selection. */
                   2932:                        xx = window_copy_find_length(wme, yy);
                   2933:
                   2934:                        /* Reset the start. */
                   2935:                        data->selx = data->selrx;
                   2936:                        data->sely = data->selry;
                   2937:                }
                   2938:                break;
                   2939:        case SEL_CHAR:
                   2940:                xx = data->cx;
                   2941:                break;
                   2942:        }
                   2943:        if (begin) {
                   2944:                data->selx = xx;
                   2945:                data->sely = yy;
                   2946:        } else {
                   2947:                data->endselx = xx;
                   2948:                data->endsely = yy;
                   2949:        }
                   2950: }
                   2951:
                   2952: static void
1.252     nicm     2953: window_copy_synchronize_cursor(struct window_mode_entry *wme, int no_reset)
1.247     nicm     2954: {
                   2955:        struct window_copy_mode_data    *data = wme->data;
1.161     nicm     2956:
                   2957:        switch (data->cursordrag) {
                   2958:        case CURSORDRAG_ENDSEL:
1.252     nicm     2959:                window_copy_synchronize_cursor_end(wme, 0, no_reset);
1.161     nicm     2960:                break;
                   2961:        case CURSORDRAG_SEL:
1.252     nicm     2962:                window_copy_synchronize_cursor_end(wme, 1, no_reset);
1.161     nicm     2963:                break;
                   2964:        case CURSORDRAG_NONE:
                   2965:                break;
                   2966:        }
                   2967: }
                   2968:
                   2969: static void
1.208     nicm     2970: window_copy_update_cursor(struct window_mode_entry *wme, u_int cx, u_int cy)
1.1       nicm     2971: {
1.208     nicm     2972:        struct window_pane              *wp = wme->wp;
                   2973:        struct window_copy_mode_data    *data = wme->data;
1.18      nicm     2974:        struct screen                   *s = &data->screen;
1.1       nicm     2975:        struct screen_write_ctx          ctx;
1.18      nicm     2976:        u_int                            old_cx, old_cy;
1.1       nicm     2977:
1.18      nicm     2978:        old_cx = data->cx; old_cy = data->cy;
                   2979:        data->cx = cx; data->cy = cy;
                   2980:        if (old_cx == screen_size_x(s))
1.208     nicm     2981:                window_copy_redraw_lines(wme, old_cy, 1);
1.18      nicm     2982:        if (data->cx == screen_size_x(s))
1.208     nicm     2983:                window_copy_redraw_lines(wme, data->cy, 1);
1.18      nicm     2984:        else {
                   2985:                screen_write_start(&ctx, wp, NULL);
1.212     nicm     2986:                screen_write_cursormove(&ctx, data->cx, data->cy, 0);
1.18      nicm     2987:                screen_write_stop(&ctx);
                   2988:        }
1.1       nicm     2989: }
                   2990:
1.157     nicm     2991: static void
1.208     nicm     2992: window_copy_start_selection(struct window_mode_entry *wme)
1.1       nicm     2993: {
1.208     nicm     2994:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     2995:
1.18      nicm     2996:        data->selx = data->cx;
1.54      nicm     2997:        data->sely = screen_hsize(data->backing) + data->cy - data->oy;
1.1       nicm     2998:
1.161     nicm     2999:        data->endselx = data->selx;
                   3000:        data->endsely = data->sely;
                   3001:
                   3002:        data->cursordrag = CURSORDRAG_ENDSEL;
                   3003:
1.252     nicm     3004:        window_copy_set_selection(wme, 1, 0);
1.1       nicm     3005: }
                   3006:
1.157     nicm     3007: static int
1.208     nicm     3008: window_copy_adjust_selection(struct window_mode_entry *wme, u_int *selx,
                   3009:     u_int *sely)
1.161     nicm     3010: {
1.208     nicm     3011:        struct window_copy_mode_data    *data = wme->data;
1.161     nicm     3012:        struct screen                   *s = &data->screen;
                   3013:        u_int                            sx, sy, ty;
                   3014:        int                              relpos;
                   3015:
                   3016:        sx = *selx;
                   3017:        sy = *sely;
                   3018:
                   3019:        ty = screen_hsize(data->backing) - data->oy;
                   3020:        if (sy < ty) {
                   3021:                relpos = WINDOW_COPY_REL_POS_ABOVE;
                   3022:                if (!data->rectflag)
                   3023:                        sx = 0;
                   3024:                sy = 0;
                   3025:        } else if (sy > ty + screen_size_y(s) - 1) {
                   3026:                relpos = WINDOW_COPY_REL_POS_BELOW;
                   3027:                if (!data->rectflag)
                   3028:                        sx = screen_size_x(s) - 1;
                   3029:                sy = screen_size_y(s) - 1;
                   3030:        } else {
                   3031:                relpos = WINDOW_COPY_REL_POS_ON_SCREEN;
                   3032:                sy -= ty;
                   3033:        }
                   3034:
                   3035:        *selx = sx;
1.176     nicm     3036:        *sely = sy;
1.161     nicm     3037:        return (relpos);
                   3038: }
                   3039:
                   3040: static int
1.252     nicm     3041: window_copy_update_selection(struct window_mode_entry *wme, int may_redraw,
                   3042:     int no_reset)
1.1       nicm     3043: {
1.208     nicm     3044:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     3045:        struct screen                   *s = &data->screen;
1.192     nicm     3046:
                   3047:        if (s->sel == NULL && data->lineflag == LINE_SEL_NONE)
                   3048:                return (0);
1.252     nicm     3049:        return (window_copy_set_selection(wme, may_redraw, no_reset));
1.192     nicm     3050: }
                   3051:
                   3052: static int
1.252     nicm     3053: window_copy_set_selection(struct window_mode_entry *wme, int may_redraw,
                   3054:     int no_reset)
1.192     nicm     3055: {
1.208     nicm     3056:        struct window_pane              *wp = wme->wp;
                   3057:        struct window_copy_mode_data    *data = wme->data;
1.192     nicm     3058:        struct screen                   *s = &data->screen;
1.136     nicm     3059:        struct options                  *oo = wp->window->options;
1.1       nicm     3060:        struct grid_cell                 gc;
1.161     nicm     3061:        u_int                            sx, sy, cy, endsx, endsy;
                   3062:        int                              startrelpos, endrelpos;
1.1       nicm     3063:
1.252     nicm     3064:        window_copy_synchronize_cursor(wme, no_reset);
1.1       nicm     3065:
                   3066:        /* Adjust the selection. */
                   3067:        sx = data->selx;
                   3068:        sy = data->sely;
1.208     nicm     3069:        startrelpos = window_copy_adjust_selection(wme, &sx, &sy);
1.161     nicm     3070:
                   3071:        /* Adjust the end of selection. */
                   3072:        endsx = data->endselx;
                   3073:        endsy = data->endsely;
1.208     nicm     3074:        endrelpos = window_copy_adjust_selection(wme, &endsx, &endsy);
1.161     nicm     3075:
                   3076:        /* Selection is outside of the current screen */
                   3077:        if (startrelpos == endrelpos &&
                   3078:            startrelpos != WINDOW_COPY_REL_POS_ON_SCREEN) {
                   3079:                screen_hide_selection(s);
                   3080:                return (0);
                   3081:        }
1.1       nicm     3082:
1.161     nicm     3083:        /* Set colours and selection. */
                   3084:        style_apply(&gc, oo, "mode-style");
1.164     nicm     3085:        gc.flags |= GRID_FLAG_NOPALETTE;
1.192     nicm     3086:        screen_set_selection(s, sx, sy, endsx, endsy, data->rectflag,
                   3087:            data->modekeys, &gc);
1.42      nicm     3088:
1.96      nicm     3089:        if (data->rectflag && may_redraw) {
1.42      nicm     3090:                /*
                   3091:                 * Can't rely on the caller to redraw the right lines for
                   3092:                 * rectangle selection - find the highest line and the number
                   3093:                 * of lines, and redraw just past that in both directions
                   3094:                 */
                   3095:                cy = data->cy;
1.182     nicm     3096:                if (data->cursordrag == CURSORDRAG_ENDSEL) {
                   3097:                        if (sy < cy)
1.208     nicm     3098:                                window_copy_redraw_lines(wme, sy, cy - sy + 1);
1.182     nicm     3099:                        else
1.208     nicm     3100:                                window_copy_redraw_lines(wme, cy, sy - cy + 1);
1.182     nicm     3101:                } else {
1.208     nicm     3102:                        if (endsy < cy) {
                   3103:                                window_copy_redraw_lines(wme, endsy,
                   3104:                                    cy - endsy + 1);
                   3105:                        } else {
                   3106:                                window_copy_redraw_lines(wme, cy,
                   3107:                                    endsy - cy + 1);
                   3108:                        }
1.182     nicm     3109:                }
1.42      nicm     3110:        }
                   3111:
1.1       nicm     3112:        return (1);
                   3113: }
                   3114:
1.157     nicm     3115: static void *
1.208     nicm     3116: window_copy_get_selection(struct window_mode_entry *wme, size_t *len)
1.1       nicm     3117: {
1.208     nicm     3118:        struct window_pane              *wp = wme->wp;
                   3119:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     3120:        struct screen                   *s = &data->screen;
                   3121:        char                            *buf;
1.42      nicm     3122:        size_t                           off;
1.111     nicm     3123:        u_int                            i, xx, yy, sx, sy, ex, ey, ey_last;
1.188     nicm     3124:        u_int                            firstsx, lastex, restex, restsx, selx;
1.69      nicm     3125:        int                              keys;
1.1       nicm     3126:
1.192     nicm     3127:        if (data->screen.sel == NULL && data->lineflag == LINE_SEL_NONE)
1.89      nicm     3128:                return (NULL);
1.1       nicm     3129:
                   3130:        buf = xmalloc(1);
                   3131:        off = 0;
                   3132:
                   3133:        *buf = '\0';
                   3134:
                   3135:        /*
                   3136:         * The selection extends from selx,sely to (adjusted) cx,cy on
                   3137:         * the base screen.
                   3138:         */
                   3139:
                   3140:        /* Find start and end. */
1.161     nicm     3141:        xx = data->endselx;
                   3142:        yy = data->endsely;
1.2       nicm     3143:        if (yy < data->sely || (yy == data->sely && xx < data->selx)) {
1.1       nicm     3144:                sx = xx; sy = yy;
                   3145:                ex = data->selx; ey = data->sely;
                   3146:        } else {
                   3147:                sx = data->selx; sy = data->sely;
                   3148:                ex = xx; ey = yy;
                   3149:        }
                   3150:
                   3151:        /* Trim ex to end of line. */
1.208     nicm     3152:        ey_last = window_copy_find_length(wme, ey);
1.111     nicm     3153:        if (ex > ey_last)
                   3154:                ex = ey_last;
1.1       nicm     3155:
1.42      nicm     3156:        /*
                   3157:         * Deal with rectangle-copy if necessary; four situations: start of
                   3158:         * first line (firstsx), end of last line (lastex), start (restsx) and
                   3159:         * end (restex) of all other lines.
                   3160:         */
                   3161:        xx = screen_size_x(s);
1.69      nicm     3162:
                   3163:        /*
                   3164:         * Behave according to mode-keys. If it is emacs, copy like emacs,
                   3165:         * keeping the top-left-most character, and dropping the
                   3166:         * bottom-right-most, regardless of copy direction. If it is vi, also
                   3167:         * keep bottom-right-most character.
                   3168:         */
1.136     nicm     3169:        keys = options_get_number(wp->window->options, "mode-keys");
1.42      nicm     3170:        if (data->rectflag) {
                   3171:                /*
                   3172:                 * Need to ignore the column with the cursor in it, which for
                   3173:                 * rectangular copy means knowing which side the cursor is on.
                   3174:                 */
1.188     nicm     3175:                if (data->cursordrag == CURSORDRAG_ENDSEL)
                   3176:                        selx = data->selx;
                   3177:                else
                   3178:                        selx = data->endselx;
                   3179:                if (selx < data->cx) {
1.42      nicm     3180:                        /* Selection start is on the left. */
1.69      nicm     3181:                        if (keys == MODEKEY_EMACS) {
                   3182:                                lastex = data->cx;
                   3183:                                restex = data->cx;
                   3184:                        }
                   3185:                        else {
                   3186:                                lastex = data->cx + 1;
                   3187:                                restex = data->cx + 1;
                   3188:                        }
1.188     nicm     3189:                        firstsx = selx;
                   3190:                        restsx = selx;
1.42      nicm     3191:                } else {
                   3192:                        /* Cursor is on the left. */
1.188     nicm     3193:                        lastex = selx + 1;
                   3194:                        restex = selx + 1;
1.64      nicm     3195:                        firstsx = data->cx;
                   3196:                        restsx = data->cx;
1.42      nicm     3197:                }
                   3198:        } else {
1.69      nicm     3199:                if (keys == MODEKEY_EMACS)
                   3200:                        lastex = ex;
1.74      nicm     3201:                else
1.69      nicm     3202:                        lastex = ex + 1;
1.42      nicm     3203:                restex = xx;
                   3204:                firstsx = sx;
                   3205:                restsx = 0;
                   3206:        }
                   3207:
1.1       nicm     3208:        /* Copy the lines. */
1.110     nicm     3209:        for (i = sy; i <= ey; i++) {
1.208     nicm     3210:                window_copy_copy_line(wme, &buf, &off, i,
1.110     nicm     3211:                    (i == sy ? firstsx : restsx),
                   3212:                    (i == ey ? lastex : restex));
1.1       nicm     3213:        }
                   3214:
1.26      nicm     3215:        /* Don't bother if no data. */
                   3216:        if (off == 0) {
1.81      nicm     3217:                free(buf);
1.89      nicm     3218:                return (NULL);
1.26      nicm     3219:        }
1.111     nicm     3220:        if (keys == MODEKEY_EMACS || lastex <= ey_last)
                   3221:                off -= 1; /* remove final \n (unless at end in vi mode) */
                   3222:        *len = off;
1.89      nicm     3223:        return (buf);
                   3224: }
                   3225:
1.157     nicm     3226: static void
1.215     nicm     3227: window_copy_copy_buffer(struct window_mode_entry *wme, const char *prefix,
                   3228:     void *buf, size_t len)
1.89      nicm     3229: {
1.208     nicm     3230:        struct window_pane      *wp = wme->wp;
                   3231:        struct screen_write_ctx  ctx;
1.72      nicm     3232:
1.178     nicm     3233:        if (options_get_number(global_options, "set-clipboard") != 0) {
1.91      nicm     3234:                screen_write_start(&ctx, wp, NULL);
                   3235:                screen_write_setselection(&ctx, buf, len);
                   3236:                screen_write_stop(&ctx);
1.179     nicm     3237:                notify_pane("pane-set-clipboard", wp);
1.91      nicm     3238:        }
1.1       nicm     3239:
1.215     nicm     3240:        paste_add(prefix, buf, len);
1.89      nicm     3241: }
                   3242:
1.157     nicm     3243: static void
1.208     nicm     3244: window_copy_copy_pipe(struct window_mode_entry *wme, struct session *s,
1.253     nicm     3245:     const char *prefix, const char *cmd)
1.89      nicm     3246: {
1.215     nicm     3247:        void            *buf;
                   3248:        size_t           len;
                   3249:        struct job      *job;
1.89      nicm     3250:
1.208     nicm     3251:        buf = window_copy_get_selection(wme, &len);
1.89      nicm     3252:        if (buf == NULL)
                   3253:                return;
                   3254:
1.253     nicm     3255:        job = job_run(cmd, s, NULL, NULL, NULL, NULL, NULL, JOB_NOWAIT, -1, -1);
1.196     nicm     3256:        bufferevent_write(job_get_event(job), buf, len);
1.215     nicm     3257:        window_copy_copy_buffer(wme, prefix, buf, len);
1.89      nicm     3258: }
                   3259:
1.157     nicm     3260: static void
1.215     nicm     3261: window_copy_copy_selection(struct window_mode_entry *wme, const char *prefix)
1.89      nicm     3262: {
1.203     nicm     3263:        char    *buf;
1.114     nicm     3264:        size_t   len;
1.89      nicm     3265:
1.208     nicm     3266:        buf = window_copy_get_selection(wme, &len);
1.203     nicm     3267:        if (buf != NULL)
1.215     nicm     3268:                window_copy_copy_buffer(wme, prefix, buf, len);
1.103     nicm     3269: }
                   3270:
1.157     nicm     3271: static void
1.208     nicm     3272: window_copy_append_selection(struct window_mode_entry *wme)
1.103     nicm     3273: {
1.208     nicm     3274:        struct window_pane              *wp = wme->wp;
1.108     nicm     3275:        char                            *buf;
                   3276:        struct paste_buffer             *pb;
1.227     nicm     3277:        const char                      *bufdata, *bufname = NULL;
1.132     nicm     3278:        size_t                           len, bufsize;
1.108     nicm     3279:        struct screen_write_ctx          ctx;
1.103     nicm     3280:
1.208     nicm     3281:        buf = window_copy_get_selection(wme, &len);
1.103     nicm     3282:        if (buf == NULL)
                   3283:                return;
                   3284:
1.178     nicm     3285:        if (options_get_number(global_options, "set-clipboard") != 0) {
1.103     nicm     3286:                screen_write_start(&ctx, wp, NULL);
                   3287:                screen_write_setselection(&ctx, buf, len);
                   3288:                screen_write_stop(&ctx);
1.179     nicm     3289:                notify_pane("pane-set-clipboard", wp);
1.103     nicm     3290:        }
                   3291:
1.203     nicm     3292:        pb = paste_get_top(&bufname);
1.103     nicm     3293:        if (pb != NULL) {
1.132     nicm     3294:                bufdata = paste_buffer_data(pb, &bufsize);
                   3295:                buf = xrealloc(buf, len + bufsize);
                   3296:                memmove(buf + bufsize, buf, len);
                   3297:                memcpy(buf, bufdata, bufsize);
                   3298:                len += bufsize;
1.103     nicm     3299:        }
1.108     nicm     3300:        if (paste_set(buf, len, bufname, NULL) != 0)
1.103     nicm     3301:                free(buf);
1.1       nicm     3302: }
                   3303:
1.157     nicm     3304: static void
1.208     nicm     3305: window_copy_copy_line(struct window_mode_entry *wme, char **buf, size_t *off,
                   3306:     u_int sy, u_int sx, u_int ex)
1.1       nicm     3307: {
1.208     nicm     3308:        struct window_copy_mode_data    *data = wme->data;
1.54      nicm     3309:        struct grid                     *gd = data->backing->grid;
1.140     nicm     3310:        struct grid_cell                 gc;
1.54      nicm     3311:        struct grid_line                *gl;
1.86      nicm     3312:        struct utf8_data                 ud;
1.54      nicm     3313:        u_int                            i, xx, wrapped = 0;
1.115     nicm     3314:        const char                      *s;
1.1       nicm     3315:
                   3316:        if (sx > ex)
                   3317:                return;
                   3318:
1.16      nicm     3319:        /*
                   3320:         * Work out if the line was wrapped at the screen edge and all of it is
                   3321:         * on screen.
                   3322:         */
1.190     nicm     3323:        gl = grid_get_line(gd, sy);
1.35      nicm     3324:        if (gl->flags & GRID_LINE_WRAPPED && gl->cellsize <= gd->sx)
1.16      nicm     3325:                wrapped = 1;
                   3326:
                   3327:        /* If the line was wrapped, don't strip spaces (use the full length). */
                   3328:        if (wrapped)
                   3329:                xx = gl->cellsize;
                   3330:        else
1.208     nicm     3331:                xx = window_copy_find_length(wme, sy);
1.1       nicm     3332:        if (ex > xx)
                   3333:                ex = xx;
                   3334:        if (sx > xx)
                   3335:                sx = xx;
                   3336:
                   3337:        if (sx < ex) {
                   3338:                for (i = sx; i < ex; i++) {
1.140     nicm     3339:                        grid_get_cell(gd, i, sy, &gc);
                   3340:                        if (gc.flags & GRID_FLAG_PADDING)
1.1       nicm     3341:                                continue;
1.140     nicm     3342:                        utf8_copy(&ud, &gc.data);
                   3343:                        if (ud.size == 1 && (gc.attr & GRID_ATTR_CHARSET)) {
1.115     nicm     3344:                                s = tty_acs_get(NULL, ud.data[0]);
                   3345:                                if (s != NULL && strlen(s) <= sizeof ud.data) {
                   3346:                                        ud.size = strlen(s);
1.117     nicm     3347:                                        memcpy(ud.data, s, ud.size);
1.115     nicm     3348:                                }
                   3349:                        }
1.86      nicm     3350:
1.116     nicm     3351:                        *buf = xrealloc(*buf, (*off) + ud.size);
1.86      nicm     3352:                        memcpy(*buf + *off, ud.data, ud.size);
                   3353:                        *off += ud.size;
1.1       nicm     3354:                }
                   3355:        }
                   3356:
1.16      nicm     3357:        /* Only add a newline if the line wasn't wrapped. */
1.44      nicm     3358:        if (!wrapped || ex != xx) {
1.116     nicm     3359:                *buf = xrealloc(*buf, (*off) + 1);
1.16      nicm     3360:                (*buf)[(*off)++] = '\n';
                   3361:        }
1.1       nicm     3362: }
                   3363:
1.157     nicm     3364: static void
1.208     nicm     3365: window_copy_clear_selection(struct window_mode_entry *wme)
1.47      nicm     3366: {
1.208     nicm     3367:        struct window_copy_mode_data   *data = wme->data;
1.47      nicm     3368:        u_int                           px, py;
                   3369:
                   3370:        screen_clear_selection(&data->screen);
                   3371:
1.161     nicm     3372:        data->cursordrag = CURSORDRAG_NONE;
1.197     nicm     3373:        data->lineflag = LINE_SEL_NONE;
1.250     nicm     3374:        data->selflag = SEL_CHAR;
1.161     nicm     3375:
1.54      nicm     3376:        py = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm     3377:        px = window_copy_find_length(wme, py);
1.47      nicm     3378:        if (data->cx > px)
1.208     nicm     3379:                window_copy_update_cursor(wme, px, data->cy);
1.47      nicm     3380: }
                   3381:
1.157     nicm     3382: static int
1.208     nicm     3383: window_copy_in_set(struct window_mode_entry *wme, u_int px, u_int py,
                   3384:     const char *set)
1.1       nicm     3385: {
1.208     nicm     3386:        struct window_copy_mode_data    *data = wme->data;
1.140     nicm     3387:        struct grid_cell                 gc;
                   3388:
                   3389:        grid_get_cell(data->backing->grid, px, py, &gc);
1.204     nicm     3390:        if (gc.flags & GRID_FLAG_PADDING)
                   3391:                return (0);
1.224     nicm     3392:        return (utf8_cstrhas(set, &gc.data));
1.1       nicm     3393: }
                   3394:
1.157     nicm     3395: static u_int
1.208     nicm     3396: window_copy_find_length(struct window_mode_entry *wme, u_int py)
1.1       nicm     3397: {
1.208     nicm     3398:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     3399:
1.224     nicm     3400:        return (grid_line_length(data->backing->grid, py));
1.1       nicm     3401: }
                   3402:
1.157     nicm     3403: static void
1.208     nicm     3404: window_copy_cursor_start_of_line(struct window_mode_entry *wme)
1.5       nicm     3405: {
1.208     nicm     3406:        struct window_copy_mode_data    *data = wme->data;
1.58      nicm     3407:        struct screen                   *back_s = data->backing;
                   3408:        struct grid                     *gd = back_s->grid;
                   3409:        u_int                            py;
                   3410:
1.192     nicm     3411:        if (data->cx == 0 && data->lineflag == LINE_SEL_NONE) {
1.58      nicm     3412:                py = screen_hsize(back_s) + data->cy - data->oy;
1.118     nicm     3413:                while (py > 0 &&
1.190     nicm     3414:                    grid_get_line(gd, py - 1)->flags & GRID_LINE_WRAPPED) {
1.208     nicm     3415:                        window_copy_cursor_up(wme, 0);
1.58      nicm     3416:                        py = screen_hsize(back_s) + data->cy - data->oy;
                   3417:                }
                   3418:        }
1.208     nicm     3419:        window_copy_update_cursor(wme, 0, data->cy);
1.252     nicm     3420:        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     3421:                window_copy_redraw_lines(wme, data->cy, 1);
1.6       nicm     3422: }
                   3423:
1.157     nicm     3424: static void
1.208     nicm     3425: window_copy_cursor_back_to_indentation(struct window_mode_entry *wme)
1.6       nicm     3426: {
1.208     nicm     3427:        struct window_copy_mode_data    *data = wme->data;
1.6       nicm     3428:        u_int                            px, py, xx;
1.140     nicm     3429:        struct grid_cell                 gc;
1.6       nicm     3430:
                   3431:        px = 0;
1.54      nicm     3432:        py = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm     3433:        xx = window_copy_find_length(wme, py);
1.6       nicm     3434:
                   3435:        while (px < xx) {
1.140     nicm     3436:                grid_get_cell(data->backing->grid, px, py, &gc);
                   3437:                if (gc.data.size != 1 || *gc.data.data != ' ')
1.6       nicm     3438:                        break;
                   3439:                px++;
                   3440:        }
                   3441:
1.208     nicm     3442:        window_copy_update_cursor(wme, px, data->cy);
1.252     nicm     3443:        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     3444:                window_copy_redraw_lines(wme, data->cy, 1);
1.5       nicm     3445: }
                   3446:
1.157     nicm     3447: static void
1.208     nicm     3448: window_copy_cursor_end_of_line(struct window_mode_entry *wme)
1.5       nicm     3449: {
1.208     nicm     3450:        struct window_copy_mode_data    *data = wme->data;
1.54      nicm     3451:        struct screen                   *back_s = data->backing;
                   3452:        struct grid                     *gd = back_s->grid;
1.190     nicm     3453:        struct grid_line                *gl;
1.5       nicm     3454:        u_int                            px, py;
                   3455:
1.54      nicm     3456:        py = screen_hsize(back_s) + data->cy - data->oy;
1.208     nicm     3457:        px = window_copy_find_length(wme, py);
1.5       nicm     3458:
1.192     nicm     3459:        if (data->cx == px && data->lineflag == LINE_SEL_NONE) {
                   3460:                if (data->screen.sel != NULL && data->rectflag)
1.54      nicm     3461:                        px = screen_size_x(back_s);
1.190     nicm     3462:                gl = grid_get_line(gd, py);
                   3463:                if (gl->flags & GRID_LINE_WRAPPED) {
                   3464:                        while (py < gd->sy + gd->hsize) {
                   3465:                                gl = grid_get_line(gd, py);
                   3466:                                if (~gl->flags & GRID_LINE_WRAPPED)
                   3467:                                        break;
1.208     nicm     3468:                                window_copy_cursor_down(wme, 0);
1.190     nicm     3469:                                py = screen_hsize(back_s) + data->cy - data->oy;
1.49      nicm     3470:                        }
1.208     nicm     3471:                        px = window_copy_find_length(wme, py);
1.49      nicm     3472:                }
                   3473:        }
1.208     nicm     3474:        window_copy_update_cursor(wme, px, data->cy);
1.49      nicm     3475:
1.252     nicm     3476:        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     3477:                window_copy_redraw_lines(wme, data->cy, 1);
1.95      nicm     3478: }
                   3479:
1.157     nicm     3480: static void
1.208     nicm     3481: window_copy_other_end(struct window_mode_entry *wme)
1.95      nicm     3482: {
1.208     nicm     3483:        struct window_copy_mode_data    *data = wme->data;
1.95      nicm     3484:        struct screen                   *s = &data->screen;
1.161     nicm     3485:        u_int                            selx, sely, cy, yy, hsize;
1.95      nicm     3486:
1.192     nicm     3487:        if (s->sel == NULL && data->lineflag == LINE_SEL_NONE)
1.95      nicm     3488:                return;
                   3489:
1.192     nicm     3490:        if (data->lineflag == LINE_SEL_LEFT_RIGHT)
                   3491:                data->lineflag = LINE_SEL_RIGHT_LEFT;
                   3492:        else if (data->lineflag == LINE_SEL_RIGHT_LEFT)
                   3493:                data->lineflag = LINE_SEL_LEFT_RIGHT;
1.118     nicm     3494:
1.161     nicm     3495:        switch (data->cursordrag) {
                   3496:                case CURSORDRAG_NONE:
                   3497:                case CURSORDRAG_SEL:
                   3498:                        data->cursordrag = CURSORDRAG_ENDSEL;
                   3499:                        break;
                   3500:                case CURSORDRAG_ENDSEL:
                   3501:                        data->cursordrag = CURSORDRAG_SEL;
                   3502:                        break;
                   3503:        }
                   3504:
                   3505:        selx = data->endselx;
                   3506:        sely = data->endsely;
                   3507:        if (data->cursordrag == CURSORDRAG_SEL) {
                   3508:                selx = data->selx;
                   3509:                sely = data->sely;
                   3510:        }
                   3511:
1.95      nicm     3512:        cy = data->cy;
                   3513:        yy = screen_hsize(data->backing) + data->cy - data->oy;
                   3514:
                   3515:        data->cx = selx;
                   3516:
1.122     nicm     3517:        hsize = screen_hsize(data->backing);
1.161     nicm     3518:        if (sely < hsize - data->oy) { /* above */
1.122     nicm     3519:                data->oy = hsize - sely;
1.95      nicm     3520:                data->cy = 0;
1.161     nicm     3521:        } else if (sely > hsize - data->oy + screen_size_y(s)) { /* below */
1.122     nicm     3522:                data->oy = hsize - sely + screen_size_y(s) - 1;
1.95      nicm     3523:                data->cy = screen_size_y(s) - 1;
                   3524:        } else
                   3525:                data->cy = cy + sely - yy;
                   3526:
1.252     nicm     3527:        window_copy_update_selection(wme, 1, 1);
1.208     nicm     3528:        window_copy_redraw_screen(wme);
1.5       nicm     3529: }
                   3530:
1.157     nicm     3531: static void
1.208     nicm     3532: window_copy_cursor_left(struct window_mode_entry *wme)
1.1       nicm     3533: {
1.208     nicm     3534:        struct window_copy_mode_data    *data = wme->data;
1.168     nicm     3535:        u_int                            py, cx;
                   3536:        struct grid_cell                 gc;
1.1       nicm     3537:
1.145     nicm     3538:        py = screen_hsize(data->backing) + data->cy - data->oy;
1.168     nicm     3539:        cx = data->cx;
                   3540:        while (cx > 0) {
                   3541:                grid_get_cell(data->backing->grid, cx, py, &gc);
                   3542:                if (~gc.flags & GRID_FLAG_PADDING)
                   3543:                        break;
                   3544:                cx--;
                   3545:        }
                   3546:        if (cx == 0 && py > 0) {
1.208     nicm     3547:                window_copy_cursor_up(wme, 0);
                   3548:                window_copy_cursor_end_of_line(wme);
1.168     nicm     3549:        } else if (cx > 0) {
1.208     nicm     3550:                window_copy_update_cursor(wme, cx - 1, data->cy);
1.252     nicm     3551:                if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     3552:                        window_copy_redraw_lines(wme, data->cy, 1);
1.1       nicm     3553:        }
                   3554: }
                   3555:
1.157     nicm     3556: static void
1.208     nicm     3557: window_copy_cursor_right(struct window_mode_entry *wme)
1.1       nicm     3558: {
1.208     nicm     3559:        struct window_copy_mode_data    *data = wme->data;
1.168     nicm     3560:        u_int                            px, py, yy, cx, cy;
                   3561:        struct grid_cell                 gc;
1.1       nicm     3562:
1.145     nicm     3563:        py = screen_hsize(data->backing) + data->cy - data->oy;
                   3564:        yy = screen_hsize(data->backing) + screen_size_y(data->backing) - 1;
1.192     nicm     3565:        if (data->screen.sel != NULL && data->rectflag)
1.47      nicm     3566:                px = screen_size_x(&data->screen);
1.168     nicm     3567:        else
1.208     nicm     3568:                px = window_copy_find_length(wme, py);
1.1       nicm     3569:
1.145     nicm     3570:        if (data->cx >= px && py < yy) {
1.208     nicm     3571:                window_copy_cursor_start_of_line(wme);
                   3572:                window_copy_cursor_down(wme, 0);
1.145     nicm     3573:        } else if (data->cx < px) {
1.168     nicm     3574:                cx = data->cx + 1;
                   3575:                cy = screen_hsize(data->backing) + data->cy - data->oy;
                   3576:                while (cx < px) {
                   3577:                        grid_get_cell(data->backing->grid, cx, cy, &gc);
                   3578:                        if (~gc.flags & GRID_FLAG_PADDING)
                   3579:                                break;
                   3580:                        cx++;
                   3581:                }
1.208     nicm     3582:                window_copy_update_cursor(wme, cx, data->cy);
1.252     nicm     3583:                if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     3584:                        window_copy_redraw_lines(wme, data->cy, 1);
1.1       nicm     3585:        }
                   3586: }
                   3587:
1.157     nicm     3588: static void
1.208     nicm     3589: window_copy_cursor_up(struct window_mode_entry *wme, int scroll_only)
1.1       nicm     3590: {
1.208     nicm     3591:        struct window_copy_mode_data    *data = wme->data;
1.36      nicm     3592:        struct screen                   *s = &data->screen;
1.1       nicm     3593:        u_int                            ox, oy, px, py;
                   3594:
1.54      nicm     3595:        oy = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm     3596:        ox = window_copy_find_length(wme, oy);
1.77      nicm     3597:        if (data->cx != ox) {
1.25      nicm     3598:                data->lastcx = data->cx;
                   3599:                data->lastsx = ox;
                   3600:        }
1.1       nicm     3601:
1.192     nicm     3602:        if (data->lineflag == LINE_SEL_LEFT_RIGHT && oy == data->sely)
1.208     nicm     3603:                window_copy_other_end(wme);
1.118     nicm     3604:
1.28      nicm     3605:        if (scroll_only || data->cy == 0) {
1.226     nicm     3606:                data->cx = data->lastcx;
1.208     nicm     3607:                window_copy_scroll_down(wme, 1);
1.36      nicm     3608:                if (scroll_only) {
                   3609:                        if (data->cy == screen_size_y(s) - 1)
1.208     nicm     3610:                                window_copy_redraw_lines(wme, data->cy, 1);
1.36      nicm     3611:                        else
1.208     nicm     3612:                                window_copy_redraw_lines(wme, data->cy, 2);
1.36      nicm     3613:                }
1.28      nicm     3614:        } else {
1.226     nicm     3615:                window_copy_update_cursor(wme, data->lastcx, data->cy - 1);
1.252     nicm     3616:                if (window_copy_update_selection(wme, 1, 0)) {
1.36      nicm     3617:                        if (data->cy == screen_size_y(s) - 1)
1.208     nicm     3618:                                window_copy_redraw_lines(wme, data->cy, 1);
1.36      nicm     3619:                        else
1.208     nicm     3620:                                window_copy_redraw_lines(wme, data->cy, 2);
1.36      nicm     3621:                }
1.1       nicm     3622:        }
                   3623:
1.198     nicm     3624:        if (data->screen.sel == NULL || !data->rectflag) {
1.54      nicm     3625:                py = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm     3626:                px = window_copy_find_length(wme, py);
1.49      nicm     3627:                if ((data->cx >= data->lastsx && data->cx != px) ||
                   3628:                    data->cx > px)
1.208     nicm     3629:                        window_copy_cursor_end_of_line(wme);
1.47      nicm     3630:        }
1.118     nicm     3631:
1.192     nicm     3632:        if (data->lineflag == LINE_SEL_LEFT_RIGHT)
1.208     nicm     3633:                window_copy_cursor_end_of_line(wme);
1.192     nicm     3634:        else if (data->lineflag == LINE_SEL_RIGHT_LEFT)
1.208     nicm     3635:                window_copy_cursor_start_of_line(wme);
1.1       nicm     3636: }
                   3637:
1.157     nicm     3638: static void
1.208     nicm     3639: window_copy_cursor_down(struct window_mode_entry *wme, int scroll_only)
1.1       nicm     3640: {
1.208     nicm     3641:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     3642:        struct screen                   *s = &data->screen;
                   3643:        u_int                            ox, oy, px, py;
                   3644:
1.54      nicm     3645:        oy = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm     3646:        ox = window_copy_find_length(wme, oy);
1.77      nicm     3647:        if (data->cx != ox) {
1.25      nicm     3648:                data->lastcx = data->cx;
                   3649:                data->lastsx = ox;
                   3650:        }
1.1       nicm     3651:
1.192     nicm     3652:        if (data->lineflag == LINE_SEL_RIGHT_LEFT && oy == data->endsely)
1.208     nicm     3653:                window_copy_other_end(wme);
1.118     nicm     3654:
1.28      nicm     3655:        if (scroll_only || data->cy == screen_size_y(s) - 1) {
1.226     nicm     3656:                data->cx = data->lastcx;
1.208     nicm     3657:                window_copy_scroll_up(wme, 1);
1.31      nicm     3658:                if (scroll_only && data->cy > 0)
1.208     nicm     3659:                        window_copy_redraw_lines(wme, data->cy - 1, 2);
1.28      nicm     3660:        } else {
1.226     nicm     3661:                window_copy_update_cursor(wme, data->lastcx, data->cy + 1);
1.252     nicm     3662:                if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     3663:                        window_copy_redraw_lines(wme, data->cy - 1, 2);
1.1       nicm     3664:        }
                   3665:
1.192     nicm     3666:        if (data->screen.sel == NULL || !data->rectflag) {
1.54      nicm     3667:                py = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm     3668:                px = window_copy_find_length(wme, py);
1.49      nicm     3669:                if ((data->cx >= data->lastsx && data->cx != px) ||
                   3670:                    data->cx > px)
1.208     nicm     3671:                        window_copy_cursor_end_of_line(wme);
1.52      nicm     3672:        }
1.118     nicm     3673:
1.192     nicm     3674:        if (data->lineflag == LINE_SEL_LEFT_RIGHT)
1.208     nicm     3675:                window_copy_cursor_end_of_line(wme);
1.192     nicm     3676:        else if (data->lineflag == LINE_SEL_RIGHT_LEFT)
1.208     nicm     3677:                window_copy_cursor_start_of_line(wme);
1.52      nicm     3678: }
                   3679:
1.157     nicm     3680: static void
1.208     nicm     3681: window_copy_cursor_jump(struct window_mode_entry *wme)
1.52      nicm     3682: {
1.208     nicm     3683:        struct window_copy_mode_data    *data = wme->data;
1.54      nicm     3684:        struct screen                   *back_s = data->backing;
1.140     nicm     3685:        struct grid_cell                 gc;
1.67      nicm     3686:        u_int                            px, py, xx;
1.52      nicm     3687:
                   3688:        px = data->cx + 1;
1.54      nicm     3689:        py = screen_hsize(back_s) + data->cy - data->oy;
1.208     nicm     3690:        xx = window_copy_find_length(wme, py);
1.52      nicm     3691:
                   3692:        while (px < xx) {
1.140     nicm     3693:                grid_get_cell(back_s->grid, px, py, &gc);
                   3694:                if (!(gc.flags & GRID_FLAG_PADDING) &&
                   3695:                    gc.data.size == 1 && *gc.data.data == data->jumpchar) {
1.208     nicm     3696:                        window_copy_update_cursor(wme, px, data->cy);
1.252     nicm     3697:                        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     3698:                                window_copy_redraw_lines(wme, data->cy, 1);
1.52      nicm     3699:                        return;
                   3700:                }
                   3701:                px++;
                   3702:        }
                   3703: }
                   3704:
1.157     nicm     3705: static void
1.208     nicm     3706: window_copy_cursor_jump_back(struct window_mode_entry *wme)
1.52      nicm     3707: {
1.208     nicm     3708:        struct window_copy_mode_data    *data = wme->data;
1.54      nicm     3709:        struct screen                   *back_s = data->backing;
1.140     nicm     3710:        struct grid_cell                 gc;
1.67      nicm     3711:        u_int                            px, py;
1.52      nicm     3712:
                   3713:        px = data->cx;
1.54      nicm     3714:        py = screen_hsize(back_s) + data->cy - data->oy;
1.52      nicm     3715:
                   3716:        if (px > 0)
                   3717:                px--;
                   3718:
                   3719:        for (;;) {
1.140     nicm     3720:                grid_get_cell(back_s->grid, px, py, &gc);
                   3721:                if (!(gc.flags & GRID_FLAG_PADDING) &&
                   3722:                    gc.data.size == 1 && *gc.data.data == data->jumpchar) {
1.208     nicm     3723:                        window_copy_update_cursor(wme, px, data->cy);
1.252     nicm     3724:                        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     3725:                                window_copy_redraw_lines(wme, data->cy, 1);
1.76      nicm     3726:                        return;
                   3727:                }
                   3728:                if (px == 0)
                   3729:                        break;
                   3730:                px--;
                   3731:        }
                   3732: }
                   3733:
1.157     nicm     3734: static void
1.208     nicm     3735: window_copy_cursor_jump_to(struct window_mode_entry *wme)
1.76      nicm     3736: {
1.208     nicm     3737:        struct window_copy_mode_data    *data = wme->data;
1.76      nicm     3738:        struct screen                   *back_s = data->backing;
1.140     nicm     3739:        struct grid_cell                 gc;
1.76      nicm     3740:        u_int                            px, py, xx;
                   3741:
1.184     nicm     3742:        px = data->cx + 2;
1.76      nicm     3743:        py = screen_hsize(back_s) + data->cy - data->oy;
1.208     nicm     3744:        xx = window_copy_find_length(wme, py);
1.76      nicm     3745:
                   3746:        while (px < xx) {
1.140     nicm     3747:                grid_get_cell(back_s->grid, px, py, &gc);
                   3748:                if (!(gc.flags & GRID_FLAG_PADDING) &&
                   3749:                    gc.data.size == 1 && *gc.data.data == data->jumpchar) {
1.208     nicm     3750:                        window_copy_update_cursor(wme, px - 1, data->cy);
1.252     nicm     3751:                        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     3752:                                window_copy_redraw_lines(wme, data->cy, 1);
1.76      nicm     3753:                        return;
                   3754:                }
                   3755:                px++;
                   3756:        }
                   3757: }
                   3758:
1.157     nicm     3759: static void
1.208     nicm     3760: window_copy_cursor_jump_to_back(struct window_mode_entry *wme)
1.76      nicm     3761: {
1.208     nicm     3762:        struct window_copy_mode_data    *data = wme->data;
1.76      nicm     3763:        struct screen                   *back_s = data->backing;
1.140     nicm     3764:        struct grid_cell                 gc;
1.76      nicm     3765:        u_int                            px, py;
                   3766:
                   3767:        px = data->cx;
                   3768:        py = screen_hsize(back_s) + data->cy - data->oy;
                   3769:
                   3770:        if (px > 0)
1.127     nicm     3771:                px--;
                   3772:
1.184     nicm     3773:        if (px > 0)
1.76      nicm     3774:                px--;
                   3775:
                   3776:        for (;;) {
1.140     nicm     3777:                grid_get_cell(back_s->grid, px, py, &gc);
                   3778:                if (!(gc.flags & GRID_FLAG_PADDING) &&
                   3779:                    gc.data.size == 1 && *gc.data.data == data->jumpchar) {
1.208     nicm     3780:                        window_copy_update_cursor(wme, px + 1, data->cy);
1.252     nicm     3781:                        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     3782:                                window_copy_redraw_lines(wme, data->cy, 1);
1.52      nicm     3783:                        return;
                   3784:                }
                   3785:                if (px == 0)
                   3786:                        break;
                   3787:                px--;
1.47      nicm     3788:        }
1.1       nicm     3789: }
                   3790:
1.157     nicm     3791: static void
1.208     nicm     3792: window_copy_cursor_next_word(struct window_mode_entry *wme,
                   3793:     const char *separators)
1.40      nicm     3794: {
1.208     nicm     3795:        struct window_copy_mode_data    *data = wme->data;
1.54      nicm     3796:        struct screen                   *back_s = data->backing;
1.40      nicm     3797:        u_int                            px, py, xx, yy;
1.48      nicm     3798:        int                              expected = 0;
1.40      nicm     3799:
                   3800:        px = data->cx;
1.54      nicm     3801:        py = screen_hsize(back_s) + data->cy - data->oy;
1.208     nicm     3802:        xx = window_copy_find_length(wme, py);
1.54      nicm     3803:        yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
1.40      nicm     3804:
1.48      nicm     3805:        /*
                   3806:         * First skip past any nonword characters and then any word characters.
                   3807:         *
                   3808:         * expected is initially set to 0 for the former and then 1 for the
                   3809:         * latter.
                   3810:         */
                   3811:        do {
                   3812:                while (px > xx ||
1.208     nicm     3813:                    window_copy_in_set(wme, px, py, separators) == expected) {
1.48      nicm     3814:                        /* Move down if we're past the end of the line. */
                   3815:                        if (px > xx) {
                   3816:                                if (py == yy)
                   3817:                                        return;
1.208     nicm     3818:                                window_copy_cursor_down(wme, 0);
1.48      nicm     3819:                                px = 0;
                   3820:
1.54      nicm     3821:                                py = screen_hsize(back_s) + data->cy - data->oy;
1.208     nicm     3822:                                xx = window_copy_find_length(wme, py);
1.48      nicm     3823:                        } else
                   3824:                                px++;
                   3825:                }
                   3826:                expected = !expected;
                   3827:        } while (expected == 1);
1.40      nicm     3828:
1.208     nicm     3829:        window_copy_update_cursor(wme, px, data->cy);
1.252     nicm     3830:        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     3831:                window_copy_redraw_lines(wme, data->cy, 1);
1.40      nicm     3832: }
                   3833:
1.157     nicm     3834: static void
1.247     nicm     3835: window_copy_cursor_next_word_end_pos(struct window_mode_entry *wme,
                   3836:     const char *separators, u_int *ppx, u_int *ppy)
                   3837: {
                   3838:        struct window_pane              *wp = wme->wp;
                   3839:        struct window_copy_mode_data    *data = wme->data;
                   3840:        struct options                  *oo = wp->window->options;
                   3841:        struct screen                   *back_s = data->backing;
                   3842:        u_int                            px, py, xx, yy;
                   3843:        int                              keys, expected = 1;
                   3844:
                   3845:        px = data->cx;
                   3846:        py = screen_hsize(back_s) + data->cy - data->oy;
                   3847:        xx = window_copy_find_length(wme, py);
                   3848:        yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
                   3849:
                   3850:        keys = options_get_number(oo, "mode-keys");
                   3851:        if (keys == MODEKEY_VI && !window_copy_in_set(wme, px, py, separators))
                   3852:                px++;
                   3853:
                   3854:        /*
                   3855:         * First skip past any word characters, then any non-word characters.
                   3856:         *
                   3857:         * expected is initially set to 1 for the former and then 0 for the
                   3858:         * latter.
                   3859:         */
                   3860:        do {
                   3861:                while (px > xx ||
                   3862:                    window_copy_in_set(wme, px, py, separators) == expected) {
                   3863:                        /* Move down if we're past the end of the line. */
                   3864:                        if (px > xx) {
                   3865:                                if (py == yy)
                   3866:                                        return;
                   3867:                                py++;
                   3868:                                px = 0;
                   3869:                                xx = window_copy_find_length(wme, py);
                   3870:                        } else
                   3871:                                px++;
                   3872:                }
                   3873:                expected = !expected;
                   3874:        } while (expected == 0);
                   3875:
                   3876:        if (keys == MODEKEY_VI && px != 0)
                   3877:                px--;
                   3878:
                   3879:        *ppx = px;
                   3880:        *ppy = py;
                   3881: }
                   3882:
                   3883: static void
1.208     nicm     3884: window_copy_cursor_next_word_end(struct window_mode_entry *wme,
1.106     nicm     3885:     const char *separators)
1.1       nicm     3886: {
1.208     nicm     3887:        struct window_pane              *wp = wme->wp;
                   3888:        struct window_copy_mode_data    *data = wme->data;
1.136     nicm     3889:        struct options                  *oo = wp->window->options;
1.54      nicm     3890:        struct screen                   *back_s = data->backing;
1.39      nicm     3891:        u_int                            px, py, xx, yy;
1.94      nicm     3892:        int                              keys, expected = 1;
1.1       nicm     3893:
1.18      nicm     3894:        px = data->cx;
1.54      nicm     3895:        py = screen_hsize(back_s) + data->cy - data->oy;
1.208     nicm     3896:        xx = window_copy_find_length(wme, py);
1.54      nicm     3897:        yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
1.1       nicm     3898:
1.94      nicm     3899:        keys = options_get_number(oo, "mode-keys");
1.208     nicm     3900:        if (keys == MODEKEY_VI && !window_copy_in_set(wme, px, py, separators))
1.94      nicm     3901:                px++;
                   3902:
1.48      nicm     3903:        /*
                   3904:         * First skip past any word characters, then any nonword characters.
                   3905:         *
                   3906:         * expected is initially set to 1 for the former and then 0 for the
                   3907:         * latter.
                   3908:         */
                   3909:        do {
                   3910:                while (px > xx ||
1.208     nicm     3911:                    window_copy_in_set(wme, px, py, separators) == expected) {
1.48      nicm     3912:                        /* Move down if we're past the end of the line. */
                   3913:                        if (px > xx) {
                   3914:                                if (py == yy)
                   3915:                                        return;
1.208     nicm     3916:                                window_copy_cursor_down(wme, 0);
1.48      nicm     3917:                                px = 0;
                   3918:
1.54      nicm     3919:                                py = screen_hsize(back_s) + data->cy - data->oy;
1.208     nicm     3920:                                xx = window_copy_find_length(wme, py);
1.48      nicm     3921:                        } else
                   3922:                                px++;
                   3923:                }
                   3924:                expected = !expected;
                   3925:        } while (expected == 0);
1.92      nicm     3926:
1.94      nicm     3927:        if (keys == MODEKEY_VI && px != 0)
1.92      nicm     3928:                px--;
1.18      nicm     3929:
1.208     nicm     3930:        window_copy_update_cursor(wme, px, data->cy);
1.252     nicm     3931:        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     3932:                window_copy_redraw_lines(wme, data->cy, 1);
1.1       nicm     3933: }
                   3934:
1.247     nicm     3935: /* Compute the previous place where a word begins. */
                   3936: static void
                   3937: window_copy_cursor_previous_word_pos(struct window_mode_entry *wme,
                   3938:     const char *separators, int already, u_int *ppx, u_int *ppy)
                   3939: {
                   3940:        struct window_copy_mode_data    *data = wme->data;
                   3941:        u_int                            px, py;
                   3942:
                   3943:        px = data->cx;
                   3944:        py = screen_hsize(data->backing) + data->cy - data->oy;
                   3945:
                   3946:        /* Move back to the previous word character. */
                   3947:        if (already || window_copy_in_set(wme, px, py, separators)) {
                   3948:                for (;;) {
                   3949:                        if (px > 0) {
                   3950:                                px--;
                   3951:                                if (!window_copy_in_set(wme, px, py,
                   3952:                                    separators))
                   3953:                                        break;
                   3954:                        } else {
                   3955:                                if (data->cy == 0 &&
                   3956:                                    (screen_hsize(data->backing) == 0 ||
                   3957:                                    data->oy >=
                   3958:                                    screen_hsize(data->backing) - 1))
                   3959:                                        goto out;
                   3960:                                py--;
                   3961:
                   3962:                                py = screen_hsize(data->backing) + data->cy -
                   3963:                                    data->oy;
                   3964:                                px = window_copy_find_length(wme, py);
                   3965:
                   3966:                                /* Stop if separator at EOL. */
                   3967:                                if (px > 0 && window_copy_in_set(wme, px - 1,
                   3968:                                    py, separators))
                   3969:                                        break;
                   3970:                        }
                   3971:                }
                   3972:        }
                   3973:
                   3974:        /* Move back to the beginning of this word. */
                   3975:        while (px > 0 && !window_copy_in_set(wme, px - 1, py, separators))
                   3976:                px--;
                   3977:
                   3978: out:
                   3979:        *ppx = px;
                   3980:        *ppy = py;
                   3981: }
                   3982:
1.8       nicm     3983: /* Move to the previous place where a word begins. */
1.157     nicm     3984: static void
1.208     nicm     3985: window_copy_cursor_previous_word(struct window_mode_entry *wme,
1.229     nicm     3986:     const char *separators, int already)
1.1       nicm     3987: {
1.208     nicm     3988:        struct window_copy_mode_data    *data = wme->data;
1.8       nicm     3989:        u_int                            px, py;
1.1       nicm     3990:
1.18      nicm     3991:        px = data->cx;
1.54      nicm     3992:        py = screen_hsize(data->backing) + data->cy - data->oy;
1.1       nicm     3993:
1.8       nicm     3994:        /* Move back to the previous word character. */
1.229     nicm     3995:        if (already || window_copy_in_set(wme, px, py, separators)) {
                   3996:                for (;;) {
                   3997:                        if (px > 0) {
                   3998:                                px--;
1.247     nicm     3999:                                if (!window_copy_in_set(wme, px, py,
                   4000:                                    separators))
1.229     nicm     4001:                                        break;
                   4002:                        } else {
                   4003:                                if (data->cy == 0 &&
                   4004:                                    (screen_hsize(data->backing) == 0 ||
1.247     nicm     4005:                                    data->oy >=
                   4006:                                    screen_hsize(data->backing) - 1))
1.229     nicm     4007:                                        goto out;
                   4008:                                window_copy_cursor_up(wme, 0);
                   4009:
1.247     nicm     4010:                                py = screen_hsize(data->backing) + data->cy -
                   4011:                                    data->oy;
1.229     nicm     4012:                                px = window_copy_find_length(wme, py);
                   4013:
                   4014:                                /* Stop if separator at EOL. */
1.247     nicm     4015:                                if (px > 0 && window_copy_in_set(wme, px - 1,
                   4016:                                    py, separators))
1.229     nicm     4017:                                        break;
                   4018:                        }
1.1       nicm     4019:                }
1.8       nicm     4020:        }
1.1       nicm     4021:
1.8       nicm     4022:        /* Move back to the beginning of this word. */
1.208     nicm     4023:        while (px > 0 && !window_copy_in_set(wme, px - 1, py, separators))
1.1       nicm     4024:                px--;
1.8       nicm     4025:
1.1       nicm     4026: out:
1.208     nicm     4027:        window_copy_update_cursor(wme, px, data->cy);
1.252     nicm     4028:        if (window_copy_update_selection(wme, 1, 0))
1.208     nicm     4029:                window_copy_redraw_lines(wme, data->cy, 1);
1.1       nicm     4030: }
                   4031:
1.157     nicm     4032: static void
1.208     nicm     4033: window_copy_scroll_up(struct window_mode_entry *wme, u_int ny)
1.1       nicm     4034: {
1.208     nicm     4035:        struct window_pane              *wp = wme->wp;
                   4036:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     4037:        struct screen                   *s = &data->screen;
                   4038:        struct screen_write_ctx          ctx;
                   4039:
                   4040:        if (data->oy < ny)
                   4041:                ny = data->oy;
                   4042:        if (ny == 0)
                   4043:                return;
                   4044:        data->oy -= ny;
                   4045:
1.252     nicm     4046:        window_copy_update_selection(wme, 0, 0);
1.96      nicm     4047:
1.1       nicm     4048:        screen_write_start(&ctx, wp, NULL);
1.212     nicm     4049:        screen_write_cursormove(&ctx, 0, 0, 0);
1.159     nicm     4050:        screen_write_deleteline(&ctx, ny, 8);
1.208     nicm     4051:        window_copy_write_lines(wme, &ctx, screen_size_y(s) - ny, ny);
                   4052:        window_copy_write_line(wme, &ctx, 0);
1.31      nicm     4053:        if (screen_size_y(s) > 1)
1.208     nicm     4054:                window_copy_write_line(wme, &ctx, 1);
1.31      nicm     4055:        if (screen_size_y(s) > 3)
1.208     nicm     4056:                window_copy_write_line(wme, &ctx, screen_size_y(s) - 2);
1.192     nicm     4057:        if (s->sel != NULL && screen_size_y(s) > ny)
1.208     nicm     4058:                window_copy_write_line(wme, &ctx, screen_size_y(s) - ny - 1);
1.212     nicm     4059:        screen_write_cursormove(&ctx, data->cx, data->cy, 0);
1.1       nicm     4060:        screen_write_stop(&ctx);
                   4061: }
                   4062:
1.157     nicm     4063: static void
1.208     nicm     4064: window_copy_scroll_down(struct window_mode_entry *wme, u_int ny)
1.1       nicm     4065: {
1.208     nicm     4066:        struct window_pane              *wp = wme->wp;
                   4067:        struct window_copy_mode_data    *data = wme->data;
1.1       nicm     4068:        struct screen                   *s = &data->screen;
                   4069:        struct screen_write_ctx          ctx;
                   4070:
1.54      nicm     4071:        if (ny > screen_hsize(data->backing))
1.1       nicm     4072:                return;
                   4073:
1.54      nicm     4074:        if (data->oy > screen_hsize(data->backing) - ny)
                   4075:                ny = screen_hsize(data->backing) - data->oy;
1.1       nicm     4076:        if (ny == 0)
                   4077:                return;
                   4078:        data->oy += ny;
                   4079:
1.252     nicm     4080:        window_copy_update_selection(wme, 0, 0);
1.96      nicm     4081:
1.1       nicm     4082:        screen_write_start(&ctx, wp, NULL);
1.212     nicm     4083:        screen_write_cursormove(&ctx, 0, 0, 0);
1.159     nicm     4084:        screen_write_insertline(&ctx, ny, 8);
1.208     nicm     4085:        window_copy_write_lines(wme, &ctx, 0, ny);
1.192     nicm     4086:        if (s->sel != NULL && screen_size_y(s) > ny)
1.208     nicm     4087:                window_copy_write_line(wme, &ctx, ny);
1.96      nicm     4088:        else if (ny == 1) /* nuke position */
1.208     nicm     4089:                window_copy_write_line(wme, &ctx, 1);
1.212     nicm     4090:        screen_write_cursormove(&ctx, data->cx, data->cy, 0);
1.1       nicm     4091:        screen_write_stop(&ctx);
                   4092: }
1.42      nicm     4093:
1.157     nicm     4094: static void
1.208     nicm     4095: window_copy_rectangle_toggle(struct window_mode_entry *wme)
1.42      nicm     4096: {
1.208     nicm     4097:        struct window_copy_mode_data    *data = wme->data;
1.47      nicm     4098:        u_int                            px, py;
1.42      nicm     4099:
                   4100:        data->rectflag = !data->rectflag;
1.47      nicm     4101:
1.54      nicm     4102:        py = screen_hsize(data->backing) + data->cy - data->oy;
1.208     nicm     4103:        px = window_copy_find_length(wme, py);
1.47      nicm     4104:        if (data->cx > px)
1.208     nicm     4105:                window_copy_update_cursor(wme, px, data->cy);
1.42      nicm     4106:
1.252     nicm     4107:        window_copy_update_selection(wme, 1, 0);
1.208     nicm     4108:        window_copy_redraw_screen(wme);
1.156     nicm     4109: }
                   4110:
1.157     nicm     4111: static void
1.156     nicm     4112: window_copy_move_mouse(struct mouse_event *m)
                   4113: {
1.211     nicm     4114:        struct window_pane              *wp;
                   4115:        struct window_mode_entry        *wme;
                   4116:        u_int                            x, y;
1.156     nicm     4117:
                   4118:        wp = cmd_mouse_pane(m, NULL, NULL);
1.211     nicm     4119:        if (wp == NULL)
                   4120:                return;
                   4121:        wme = TAILQ_FIRST(&wp->modes);
1.223     nicm     4122:        if (wme == NULL)
                   4123:                return;
                   4124:        if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
1.156     nicm     4125:                return;
                   4126:
1.183     nicm     4127:        if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
1.156     nicm     4128:                return;
                   4129:
1.211     nicm     4130:        window_copy_update_cursor(wme, x, y);
1.126     nicm     4131: }
                   4132:
                   4133: void
1.141     nicm     4134: window_copy_start_drag(struct client *c, struct mouse_event *m)
1.126     nicm     4135: {
1.211     nicm     4136:        struct window_pane              *wp;
                   4137:        struct window_mode_entry        *wme;
1.247     nicm     4138:        struct window_copy_mode_data    *data;
1.248     nicm     4139:        u_int                            x, y, yg;
1.126     nicm     4140:
1.155     nicm     4141:        if (c == NULL)
                   4142:                return;
                   4143:
1.126     nicm     4144:        wp = cmd_mouse_pane(m, NULL, NULL);
1.211     nicm     4145:        if (wp == NULL)
                   4146:                return;
                   4147:        wme = TAILQ_FIRST(&wp->modes);
1.223     nicm     4148:        if (wme == NULL)
                   4149:                return;
                   4150:        if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
1.126     nicm     4151:                return;
                   4152:
                   4153:        if (cmd_mouse_at(wp, m, &x, &y, 1) != 0)
                   4154:                return;
                   4155:
                   4156:        c->tty.mouse_drag_update = window_copy_drag_update;
1.217     nicm     4157:        c->tty.mouse_drag_release = window_copy_drag_release;
1.126     nicm     4158:
1.247     nicm     4159:        data = wme->data;
1.248     nicm     4160:        yg = screen_hsize(data->backing) + y - data->oy;
                   4161:        if (x < data->selrx || x > data->endselrx || yg != data->selry)
                   4162:                data->selflag = SEL_CHAR;
1.247     nicm     4163:        switch (data->selflag) {
                   4164:                case SEL_WORD:
                   4165:                        if (data->ws) {
                   4166:                                window_copy_update_cursor(wme, x, y);
                   4167:                                window_copy_cursor_previous_word_pos(wme,
                   4168:                                    data->ws, 0, &x, &y);
                   4169:                                y -= screen_hsize(data->backing) - data->oy;
                   4170:                        }
                   4171:                        window_copy_update_cursor(wme, x, y);
                   4172:                        break;
                   4173:                case SEL_LINE:
                   4174:                        window_copy_update_cursor(wme, 0, y);
                   4175:                        break;
                   4176:                case SEL_CHAR:
                   4177:                        window_copy_update_cursor(wme, x, y);
                   4178:                        window_copy_start_selection(wme);
                   4179:                        break;
                   4180:        }
                   4181:
1.211     nicm     4182:        window_copy_redraw_screen(wme);
1.220     nicm     4183:        window_copy_drag_update(c, m);
1.126     nicm     4184: }
                   4185:
1.157     nicm     4186: static void
1.211     nicm     4187: window_copy_drag_update(struct client *c, struct mouse_event *m)
1.126     nicm     4188: {
                   4189:        struct window_pane              *wp;
1.211     nicm     4190:        struct window_mode_entry        *wme;
1.126     nicm     4191:        struct window_copy_mode_data    *data;
1.217     nicm     4192:        u_int                            x, y, old_cx, old_cy;
                   4193:        struct timeval                   tv = {
                   4194:                .tv_usec = WINDOW_COPY_DRAG_REPEAT_TIME
                   4195:        };
1.126     nicm     4196:
1.211     nicm     4197:        if (c == NULL)
                   4198:                return;
                   4199:
1.126     nicm     4200:        wp = cmd_mouse_pane(m, NULL, NULL);
1.211     nicm     4201:        if (wp == NULL)
1.126     nicm     4202:                return;
1.211     nicm     4203:        wme = TAILQ_FIRST(&wp->modes);
1.223     nicm     4204:        if (wme == NULL)
                   4205:                return;
                   4206:        if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
1.211     nicm     4207:                return;
1.217     nicm     4208:
1.211     nicm     4209:        data = wme->data;
1.217     nicm     4210:        evtimer_del(&data->dragtimer);
1.126     nicm     4211:
                   4212:        if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
                   4213:                return;
1.217     nicm     4214:        old_cx = data->cx;
1.126     nicm     4215:        old_cy = data->cy;
                   4216:
1.211     nicm     4217:        window_copy_update_cursor(wme, x, y);
1.252     nicm     4218:        if (window_copy_update_selection(wme, 1, 0))
1.211     nicm     4219:                window_copy_redraw_selection(wme, old_cy);
1.217     nicm     4220:        if (old_cy != data->cy || old_cx == data->cx) {
                   4221:                if (y == 0) {
                   4222:                        evtimer_add(&data->dragtimer, &tv);
                   4223:                        window_copy_cursor_up(wme, 1);
                   4224:                } else if (y == screen_size_y(&data->screen) - 1) {
                   4225:                        evtimer_add(&data->dragtimer, &tv);
                   4226:                        window_copy_cursor_down(wme, 1);
                   4227:                }
                   4228:        }
                   4229: }
                   4230:
                   4231: static void
                   4232: window_copy_drag_release(struct client *c, struct mouse_event *m)
                   4233: {
                   4234:        struct window_pane              *wp;
                   4235:        struct window_mode_entry        *wme;
                   4236:        struct window_copy_mode_data    *data;
                   4237:
                   4238:        if (c == NULL)
                   4239:                return;
                   4240:
                   4241:        wp = cmd_mouse_pane(m, NULL, NULL);
                   4242:        if (wp == NULL)
                   4243:                return;
                   4244:        wme = TAILQ_FIRST(&wp->modes);
1.223     nicm     4245:        if (wme == NULL)
                   4246:                return;
                   4247:        if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
1.217     nicm     4248:                return;
                   4249:
                   4250:        data = wme->data;
                   4251:        evtimer_del(&data->dragtimer);
1.42      nicm     4252: }