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

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