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

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