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

1.183   ! nicm        1: /* $OpenBSD: window-copy.c,v 1.182 2017/08/29 20:26:25 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.21      nicm       22: #include <stdlib.h>
1.1       nicm       23: #include <string.h>
                     24:
                     25: #include "tmux.h"
                     26:
1.157     nicm       27: static const char *window_copy_key_table(struct window_pane *);
                     28: static void    window_copy_command(struct window_pane *, struct client *,
                     29:                    struct session *, struct args *, struct mouse_event *);
1.177     nicm       30: static struct screen *window_copy_init(struct window_pane *,
                     31:                    struct cmd_find_state *, struct args *);
1.157     nicm       32: static void    window_copy_free(struct window_pane *);
1.166     nicm       33: static int     window_copy_pagedown(struct window_pane *, int);
1.157     nicm       34: static void    window_copy_next_paragraph(struct window_pane *);
                     35: static void    window_copy_previous_paragraph(struct window_pane *);
                     36: static void    window_copy_resize(struct window_pane *, u_int, u_int);
                     37:
                     38: static void    window_copy_redraw_selection(struct window_pane *, u_int);
                     39: static void    window_copy_redraw_lines(struct window_pane *, u_int, u_int);
                     40: static void    window_copy_redraw_screen(struct window_pane *);
                     41: static void    window_copy_write_line(struct window_pane *,
                     42:                    struct screen_write_ctx *, u_int);
                     43: static void    window_copy_write_lines(struct window_pane *,
                     44:                    struct screen_write_ctx *, u_int, u_int);
                     45:
                     46: static void    window_copy_scroll_to(struct window_pane *, u_int, u_int);
                     47: static int     window_copy_search_compare(struct grid *, u_int, u_int,
                     48:                    struct grid *, u_int, int);
                     49: static int     window_copy_search_lr(struct grid *, struct grid *, u_int *,
                     50:                    u_int, u_int, u_int, int);
                     51: static int     window_copy_search_rl(struct grid *, struct grid *, u_int *,
                     52:                    u_int, u_int, u_int, int);
1.162     nicm       53: static int     window_copy_search_marks(struct window_pane *, struct screen *);
1.163     nicm       54: static void    window_copy_clear_marks(struct window_pane *);
1.157     nicm       55: static void    window_copy_move_left(struct screen *, u_int *, u_int *);
                     56: static void    window_copy_move_right(struct screen *, u_int *, u_int *);
                     57: static int     window_copy_is_lowercase(const char *);
1.163     nicm       58: static int     window_copy_search_jump(struct window_pane *, struct grid *,
1.157     nicm       59:                    struct grid *, u_int, u_int, u_int, int, int, int);
1.163     nicm       60: static int     window_copy_search(struct window_pane *, int, int);
                     61: static int     window_copy_search_up(struct window_pane *, int);
                     62: static int     window_copy_search_down(struct window_pane *, int);
1.157     nicm       63: static void    window_copy_goto_line(struct window_pane *, const char *);
                     64: static void    window_copy_update_cursor(struct window_pane *, u_int, u_int);
                     65: static void    window_copy_start_selection(struct window_pane *);
1.161     nicm       66: static int     window_copy_adjust_selection(struct window_pane *, u_int *,
                     67:                    u_int *);
1.157     nicm       68: static int     window_copy_update_selection(struct window_pane *, int);
1.161     nicm       69: static void    window_copy_synchronize_cursor(struct window_pane *wp);
1.160     nicm       70: static void    *window_copy_get_selection(struct window_pane *, size_t *);
1.157     nicm       71: static void    window_copy_copy_buffer(struct window_pane *, const char *,
                     72:                    void *, size_t);
                     73: static void    window_copy_copy_pipe(struct window_pane *, struct session *,
                     74:                    const char *, const char *);
                     75: static void    window_copy_copy_selection(struct window_pane *, const char *);
                     76: static void    window_copy_append_selection(struct window_pane *,
                     77:                    const char *);
                     78: static void    window_copy_clear_selection(struct window_pane *);
                     79: static void    window_copy_copy_line(struct window_pane *, char **, size_t *,
                     80:                    u_int, u_int, u_int);
                     81: static int     window_copy_in_set(struct window_pane *, u_int, u_int,
                     82:                    const char *);
                     83: static u_int   window_copy_find_length(struct window_pane *, u_int);
                     84: static void    window_copy_cursor_start_of_line(struct window_pane *);
                     85: static void    window_copy_cursor_back_to_indentation(struct window_pane *);
                     86: static void    window_copy_cursor_end_of_line(struct window_pane *);
                     87: static void    window_copy_other_end(struct window_pane *);
                     88: static void    window_copy_cursor_left(struct window_pane *);
                     89: static void    window_copy_cursor_right(struct window_pane *);
                     90: static void    window_copy_cursor_up(struct window_pane *, int);
                     91: static void    window_copy_cursor_down(struct window_pane *, int);
                     92: static void    window_copy_cursor_jump(struct window_pane *);
                     93: static void    window_copy_cursor_jump_back(struct window_pane *);
                     94: static void    window_copy_cursor_jump_to(struct window_pane *, int);
                     95: static void    window_copy_cursor_jump_to_back(struct window_pane *, int);
                     96: static void    window_copy_cursor_next_word(struct window_pane *,
                     97:                    const char *);
                     98: static void    window_copy_cursor_next_word_end(struct window_pane *,
                     99:                    const char *);
                    100: static void    window_copy_cursor_previous_word(struct window_pane *,
                    101:                    const char *);
                    102: static void    window_copy_scroll_up(struct window_pane *, u_int);
                    103: static void    window_copy_scroll_down(struct window_pane *, u_int);
                    104: static void    window_copy_rectangle_toggle(struct window_pane *);
                    105: static void    window_copy_move_mouse(struct mouse_event *);
                    106: static void    window_copy_drag_update(struct client *, struct mouse_event *);
1.1       nicm      107:
                    108: const struct window_mode window_copy_mode = {
1.173     nicm      109:        .name = "copy-mode",
                    110:
1.155     nicm      111:        .init = window_copy_init,
                    112:        .free = window_copy_free,
                    113:        .resize = window_copy_resize,
                    114:        .key_table = window_copy_key_table,
                    115:        .command = window_copy_command,
1.1       nicm      116: };
                    117:
1.155     nicm      118: enum {
1.21      nicm      119:        WINDOW_COPY_OFF,
                    120:        WINDOW_COPY_SEARCHUP,
                    121:        WINDOW_COPY_SEARCHDOWN,
1.52      nicm      122:        WINDOW_COPY_JUMPFORWARD,
1.155     nicm      123:        WINDOW_COPY_JUMPBACKWARD,
1.76      nicm      124:        WINDOW_COPY_JUMPTOFORWARD,
1.155     nicm      125:        WINDOW_COPY_JUMPTOBACKWARD,
1.21      nicm      126: };
                    127:
1.161     nicm      128: enum {
                    129:        WINDOW_COPY_REL_POS_ABOVE,
                    130:        WINDOW_COPY_REL_POS_ON_SCREEN,
                    131:        WINDOW_COPY_REL_POS_BELOW,
                    132: };
                    133:
1.54      nicm      134: /*
1.161     nicm      135:  * Copy mode's visible screen (the "screen" field) is filled from one of two
                    136:  * sources: the original contents of the pane (used when we actually enter via
                    137:  * the "copy-mode" command, to copy the contents of the current pane), or else
                    138:  * a series of lines containing the output from an output-writing tmux command
                    139:  * (such as any of the "show-*" or "list-*" commands).
1.54      nicm      140:  *
1.161     nicm      141:  * In either case, the full content of the copy-mode grid is pointed at by the
                    142:  * "backing" field, and is copied into "screen" as needed (that is, when
                    143:  * scrolling occurs). When copy-mode is backed by a pane, backing points
                    144:  * directly at that pane's screen structure (&wp->base); when backed by a list
                    145:  * of output-lines from a command, it points at a newly-allocated screen
                    146:  * structure (which is deallocated when the mode ends).
1.54      nicm      147:  */
1.1       nicm      148: struct window_copy_mode_data {
1.155     nicm      149:        struct screen    screen;
1.1       nicm      150:
1.155     nicm      151:        struct screen   *backing;
                    152:        int              backing_written; /* backing display started */
1.54      nicm      153:
1.161     nicm      154:        u_int            oy; /* number of lines scrolled up */
1.21      nicm      155:
1.161     nicm      156:        u_int            selx; /* beginning of selection */
1.155     nicm      157:        u_int            sely;
1.21      nicm      158:
1.161     nicm      159:        u_int            endselx; /* end of selection */
                    160:        u_int            endsely;
                    161:
                    162:        enum {
                    163:                CURSORDRAG_NONE,        /* selection is independent of cursor */
                    164:                CURSORDRAG_ENDSEL,      /* end is synchronized with cursor */
                    165:                CURSORDRAG_SEL,         /* start is synchronized with cursor */
                    166:        } cursordrag;
                    167:
1.155     nicm      168:        int              rectflag;      /* in rectangle copy mode? */
                    169:        int              scroll_exit;   /* exit on scroll to end? */
1.1       nicm      170:
1.155     nicm      171:        u_int            cx;
                    172:        u_int            cy;
1.42      nicm      173:
1.155     nicm      174:        u_int            lastcx; /* position in last line w/ content */
                    175:        u_int            lastsx; /* size of last line w/ content */
1.1       nicm      176:
1.155     nicm      177:        int              searchtype;
                    178:        char            *searchstr;
1.162     nicm      179:        bitstr_t        *searchmark;
1.170     nicm      180:        u_int            searchcount;
                    181:        int              searchthis;
1.163     nicm      182:        int              searchx;
                    183:        int              searchy;
                    184:        int              searcho;
1.25      nicm      185:
1.155     nicm      186:        int              jumptype;
                    187:        char             jumpchar;
1.1       nicm      188: };
                    189:
1.157     nicm      190: static struct screen *
1.177     nicm      191: window_copy_init(struct window_pane *wp, __unused struct cmd_find_state *fs,
                    192:     __unused struct args *args)
1.1       nicm      193: {
                    194:        struct window_copy_mode_data    *data;
                    195:        struct screen                   *s;
                    196:
                    197:        wp->modedata = data = xmalloc(sizeof *data);
1.162     nicm      198:
1.1       nicm      199:        data->oy = 0;
1.54      nicm      200:        data->cx = 0;
                    201:        data->cy = 0;
1.1       nicm      202:
1.161     nicm      203:        data->cursordrag = CURSORDRAG_NONE;
                    204:
1.25      nicm      205:        data->lastcx = 0;
                    206:        data->lastsx = 0;
                    207:
1.54      nicm      208:        data->backing_written = 0;
                    209:
1.42      nicm      210:        data->rectflag = 0;
1.133     nicm      211:        data->scroll_exit = 0;
1.42      nicm      212:
1.174     nicm      213:        if (wp->searchstr != NULL) {
                    214:                data->searchtype = WINDOW_COPY_SEARCHUP;
                    215:                data->searchstr = xstrdup(wp->searchstr);
                    216:        } else {
                    217:                data->searchtype = WINDOW_COPY_OFF;
                    218:                data->searchstr = NULL;
                    219:        }
1.162     nicm      220:        data->searchmark = NULL;
1.163     nicm      221:        data->searchx = data->searchy = data->searcho = -1;
1.21      nicm      222:
1.61      nicm      223:        if (wp->fd != -1)
                    224:                bufferevent_disable(wp->event, EV_READ|EV_WRITE);
1.46      nicm      225:
1.52      nicm      226:        data->jumptype = WINDOW_COPY_OFF;
                    227:        data->jumpchar = '\0';
                    228:
1.1       nicm      229:        s = &data->screen;
                    230:        screen_init(s, screen_size_x(&wp->base), screen_size_y(&wp->base), 0);
1.155     nicm      231:        s->sel.modekeys = options_get_number(wp->window->options, "mode-keys");
1.1       nicm      232:
1.54      nicm      233:        data->backing = NULL;
                    234:
                    235:        return (s);
                    236: }
                    237:
                    238: void
1.134     nicm      239: window_copy_init_from_pane(struct window_pane *wp, int scroll_exit)
1.54      nicm      240: {
                    241:        struct window_copy_mode_data    *data = wp->modedata;
                    242:        struct screen                   *s = &data->screen;
                    243:        struct screen_write_ctx          ctx;
                    244:        u_int                            i;
                    245:
                    246:        if (wp->mode != &window_copy_mode)
                    247:                fatalx("not in copy mode");
                    248:
                    249:        data->backing = &wp->base;
                    250:        data->cx = data->backing->cx;
                    251:        data->cy = data->backing->cy;
1.133     nicm      252:        data->scroll_exit = scroll_exit;
1.54      nicm      253:
1.1       nicm      254:        s->cx = data->cx;
                    255:        s->cy = data->cy;
                    256:
                    257:        screen_write_start(&ctx, NULL, s);
                    258:        for (i = 0; i < screen_size_y(s); i++)
                    259:                window_copy_write_line(wp, &ctx, i);
                    260:        screen_write_cursormove(&ctx, data->cx, data->cy);
                    261:        screen_write_stop(&ctx);
1.54      nicm      262: }
1.1       nicm      263:
1.54      nicm      264: void
                    265: window_copy_init_for_output(struct window_pane *wp)
                    266: {
                    267:        struct window_copy_mode_data    *data = wp->modedata;
                    268:
                    269:        data->backing = xmalloc(sizeof *data->backing);
                    270:        screen_init(data->backing, screen_size_x(&wp->base),
                    271:            screen_size_y(&wp->base), UINT_MAX);
1.1       nicm      272: }
                    273:
1.157     nicm      274: static void
1.1       nicm      275: window_copy_free(struct window_pane *wp)
                    276: {
                    277:        struct window_copy_mode_data    *data = wp->modedata;
1.46      nicm      278:
1.61      nicm      279:        if (wp->fd != -1)
                    280:                bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1.1       nicm      281:
1.162     nicm      282:        free(data->searchmark);
1.81      nicm      283:        free(data->searchstr);
1.21      nicm      284:
1.54      nicm      285:        if (data->backing != &wp->base) {
                    286:                screen_free(data->backing);
1.81      nicm      287:                free(data->backing);
1.54      nicm      288:        }
1.1       nicm      289:        screen_free(&data->screen);
1.21      nicm      290:
1.81      nicm      291:        free(data);
1.1       nicm      292: }
                    293:
                    294: void
1.54      nicm      295: window_copy_add(struct window_pane *wp, const char *fmt, ...)
                    296: {
                    297:        va_list ap;
                    298:
                    299:        va_start(ap, fmt);
                    300:        window_copy_vadd(wp, fmt, ap);
                    301:        va_end(ap);
                    302: }
                    303:
                    304: void
                    305: window_copy_vadd(struct window_pane *wp, const char *fmt, va_list ap)
                    306: {
                    307:        struct window_copy_mode_data    *data = wp->modedata;
                    308:        struct screen                   *backing = data->backing;
                    309:        struct screen_write_ctx          back_ctx, ctx;
                    310:        struct grid_cell                 gc;
1.119     nicm      311:        u_int                            old_hsize, old_cy;
1.54      nicm      312:
                    313:        if (backing == &wp->base)
                    314:                return;
                    315:
                    316:        memcpy(&gc, &grid_default_cell, sizeof gc);
                    317:
                    318:        old_hsize = screen_hsize(data->backing);
                    319:        screen_write_start(&back_ctx, NULL, backing);
                    320:        if (data->backing_written) {
                    321:                /*
                    322:                 * On the second or later line, do a CRLF before writing
                    323:                 * (so it's on a new line).
                    324:                 */
                    325:                screen_write_carriagereturn(&back_ctx);
1.175     nicm      326:                screen_write_linefeed(&back_ctx, 0, 8);
1.54      nicm      327:        } else
                    328:                data->backing_written = 1;
1.119     nicm      329:        old_cy = backing->cy;
1.139     nicm      330:        screen_write_vnputs(&back_ctx, 0, &gc, fmt, ap);
1.54      nicm      331:        screen_write_stop(&back_ctx);
                    332:
                    333:        data->oy += screen_hsize(data->backing) - old_hsize;
                    334:
                    335:        screen_write_start(&ctx, wp, &data->screen);
                    336:
                    337:        /*
                    338:         * If the history has changed, draw the top line.
                    339:         * (If there's any history at all, it has changed.)
                    340:         */
                    341:        if (screen_hsize(data->backing))
                    342:                window_copy_redraw_lines(wp, 0, 1);
                    343:
1.119     nicm      344:        /* Write the new lines. */
                    345:        window_copy_redraw_lines(wp, old_cy, backing->cy - old_cy + 1);
1.54      nicm      346:
                    347:        screen_write_stop(&ctx);
                    348: }
                    349:
                    350: void
1.149     nicm      351: window_copy_pageup(struct window_pane *wp, int half_page)
1.1       nicm      352: {
                    353:        struct window_copy_mode_data    *data = wp->modedata;
                    354:        struct screen                   *s = &data->screen;
1.162     nicm      355:        u_int                            n, ox, oy, px, py;
1.147     nicm      356:
                    357:        oy = screen_hsize(data->backing) + data->cy - data->oy;
                    358:        ox = window_copy_find_length(wp, oy);
                    359:
                    360:        if (s->sel.lineflag == LINE_SEL_LEFT_RIGHT && oy == data->sely)
                    361:                window_copy_other_end(wp);
                    362:
                    363:        if (data->cx != ox) {
                    364:                data->lastcx = data->cx;
                    365:                data->lastsx = ox;
                    366:        }
                    367:        data->cx = data->lastcx;
1.1       nicm      368:
1.19      nicm      369:        n = 1;
1.149     nicm      370:        if (screen_size_y(s) > 2) {
                    371:                if (half_page)
                    372:                        n = screen_size_y(s) / 2;
                    373:                else
                    374:                        n = screen_size_y(s) - 2;
                    375:        }
1.147     nicm      376:
1.54      nicm      377:        if (data->oy + n > screen_hsize(data->backing))
                    378:                data->oy = screen_hsize(data->backing);
1.1       nicm      379:        else
1.19      nicm      380:                data->oy += n;
1.147     nicm      381:
                    382:        if (!data->screen.sel.flag || !data->rectflag) {
1.162     nicm      383:                py = screen_hsize(data->backing) + data->cy - data->oy;
                    384:                px = window_copy_find_length(wp, py);
                    385:                if ((data->cx >= data->lastsx && data->cx != px) ||
                    386:                    data->cx > px)
1.147     nicm      387:                        window_copy_cursor_end_of_line(wp);
                    388:        }
                    389:
                    390:        window_copy_update_selection(wp, 1);
                    391:        window_copy_redraw_screen(wp);
                    392: }
                    393:
1.166     nicm      394: static int
1.149     nicm      395: window_copy_pagedown(struct window_pane *wp, int half_page)
1.147     nicm      396: {
                    397:        struct window_copy_mode_data    *data = wp->modedata;
                    398:        struct screen                   *s = &data->screen;
1.161     nicm      399:        u_int                            n, ox, oy, px, py;
1.147     nicm      400:
                    401:        oy = screen_hsize(data->backing) + data->cy - data->oy;
                    402:        ox = window_copy_find_length(wp, oy);
                    403:
                    404:        if (s->sel.lineflag == LINE_SEL_RIGHT_LEFT && oy == data->sely)
                    405:                window_copy_other_end(wp);
                    406:
                    407:        if (data->cx != ox) {
                    408:                data->lastcx = data->cx;
                    409:                data->lastsx = ox;
                    410:        }
                    411:        data->cx = data->lastcx;
                    412:
                    413:        n = 1;
1.149     nicm      414:        if (screen_size_y(s) > 2) {
                    415:                if (half_page)
                    416:                        n = screen_size_y(s) / 2;
                    417:                else
                    418:                        n = screen_size_y(s) - 2;
                    419:        }
1.147     nicm      420:
                    421:        if (data->oy < n)
                    422:                data->oy = 0;
                    423:        else
                    424:                data->oy -= n;
                    425:
                    426:        if (!data->screen.sel.flag || !data->rectflag) {
1.161     nicm      427:                py = screen_hsize(data->backing) + data->cy - data->oy;
                    428:                px = window_copy_find_length(wp, py);
                    429:                if ((data->cx >= data->lastsx && data->cx != px) ||
                    430:                    data->cx > px)
1.147     nicm      431:                        window_copy_cursor_end_of_line(wp);
                    432:        }
                    433:
1.166     nicm      434:        if (data->scroll_exit && data->oy == 0)
                    435:                return (1);
1.96      nicm      436:        window_copy_update_selection(wp, 1);
1.1       nicm      437:        window_copy_redraw_screen(wp);
1.166     nicm      438:        return (0);
1.1       nicm      439: }
                    440:
1.157     nicm      441: static void
1.148     nicm      442: window_copy_previous_paragraph(struct window_pane *wp)
                    443: {
                    444:        struct window_copy_mode_data    *data = wp->modedata;
1.151     nicm      445:        u_int                            oy;
1.148     nicm      446:
                    447:        oy = screen_hsize(data->backing) + data->cy - data->oy;
                    448:
                    449:        while (oy > 0 && window_copy_find_length(wp, oy) == 0)
                    450:                oy--;
                    451:
                    452:        while (oy > 0 && window_copy_find_length(wp, oy) > 0)
                    453:                oy--;
                    454:
                    455:        window_copy_scroll_to(wp, 0, oy);
                    456: }
                    457:
1.157     nicm      458: static void
1.148     nicm      459: window_copy_next_paragraph(struct window_pane *wp)
                    460: {
                    461:        struct window_copy_mode_data    *data = wp->modedata;
                    462:        struct screen                   *s = &data->screen;
                    463:        u_int                            maxy, ox, oy;
                    464:
                    465:        oy = screen_hsize(data->backing) + data->cy - data->oy;
                    466:        maxy = screen_hsize(data->backing) + screen_size_y(s) - 1;
                    467:
                    468:        while (oy < maxy && window_copy_find_length(wp, oy) == 0)
                    469:                oy++;
                    470:
                    471:        while (oy < maxy && window_copy_find_length(wp, oy) > 0)
                    472:                oy++;
                    473:
                    474:        ox = window_copy_find_length(wp, oy);
                    475:        window_copy_scroll_to(wp, ox, oy);
                    476: }
                    477:
1.157     nicm      478: static void
1.1       nicm      479: window_copy_resize(struct window_pane *wp, u_int sx, u_int sy)
                    480: {
                    481:        struct window_copy_mode_data    *data = wp->modedata;
                    482:        struct screen                   *s = &data->screen;
                    483:        struct screen_write_ctx          ctx;
                    484:
1.119     nicm      485:        screen_resize(s, sx, sy, 1);
1.54      nicm      486:        if (data->backing != &wp->base)
1.119     nicm      487:                screen_resize(data->backing, sx, sy, 1);
1.35      nicm      488:
1.18      nicm      489:        if (data->cy > sy - 1)
                    490:                data->cy = sy - 1;
                    491:        if (data->cx > sx)
                    492:                data->cx = sx;
1.63      nicm      493:        if (data->oy > screen_hsize(data->backing))
                    494:                data->oy = screen_hsize(data->backing);
1.18      nicm      495:
1.47      nicm      496:        window_copy_clear_selection(wp);
1.18      nicm      497:
1.1       nicm      498:        screen_write_start(&ctx, NULL, s);
                    499:        window_copy_write_lines(wp, &ctx, 0, screen_size_y(s) - 1);
                    500:        screen_write_stop(&ctx);
1.18      nicm      501:
1.162     nicm      502:        if (data->searchmark != NULL)
                    503:                window_copy_search_marks(wp, NULL);
1.163     nicm      504:        data->searchx = data->cx;
                    505:        data->searchy = data->cy;
                    506:        data->searcho = data->oy;
1.162     nicm      507:
1.17      nicm      508:        window_copy_redraw_screen(wp);
1.1       nicm      509: }
                    510:
1.157     nicm      511: static const char *
1.155     nicm      512: window_copy_key_table(struct window_pane *wp)
                    513: {
                    514:        if (options_get_number(wp->window->options, "mode-keys") == MODEKEY_VI)
                    515:                return ("copy-mode-vi");
                    516:        return ("copy-mode");
                    517: }
                    518:
1.157     nicm      519: static void
1.155     nicm      520: window_copy_command(struct window_pane *wp, struct client *c, struct session *s,
                    521:     struct args *args, struct mouse_event *m)
1.1       nicm      522: {
                    523:        struct window_copy_mode_data    *data = wp->modedata;
1.155     nicm      524:        struct screen                   *sn = &data->screen;
1.162     nicm      525:        const char                      *command, *argument, *ws;
1.155     nicm      526:        u_int                            np = wp->modeprefix;
1.163     nicm      527:        int                              cancel = 0, redraw = 0;
                    528:        char                             prefix;
1.155     nicm      529:
                    530:        if (args->argc == 0)
                    531:                return;
                    532:        command = args->argv[0];
1.21      nicm      533:
1.156     nicm      534:        if (m != NULL && m->valid)
                    535:                window_copy_move_mouse(m);
                    536:
1.155     nicm      537:        if (args->argc == 1) {
                    538:                if (strcmp(command, "append-selection") == 0) {
                    539:                        if (s != NULL)
                    540:                                window_copy_append_selection(wp, NULL);
                    541:                        window_copy_clear_selection(wp);
1.163     nicm      542:                        redraw = 1;
1.155     nicm      543:                }
                    544:                if (strcmp(command, "append-selection-and-cancel") == 0) {
                    545:                        if (s != NULL)
                    546:                                window_copy_append_selection(wp, NULL);
                    547:                        window_copy_clear_selection(wp);
1.163     nicm      548:                        redraw = 1;
1.162     nicm      549:                        cancel = 1;
1.155     nicm      550:                }
                    551:                if (strcmp(command, "back-to-indentation") == 0)
                    552:                        window_copy_cursor_back_to_indentation(wp);
                    553:                if (strcmp(command, "begin-selection") == 0) {
                    554:                        if (m != NULL)
                    555:                                window_copy_start_drag(c, m);
                    556:                        else {
                    557:                                sn->sel.lineflag = LINE_SEL_NONE;
                    558:                                window_copy_start_selection(wp);
1.163     nicm      559:                                redraw = 1;
1.122     nicm      560:                        }
1.155     nicm      561:                }
1.161     nicm      562:                if (strcmp(command, "stop-selection") == 0)
                    563:                        data->cursordrag = CURSORDRAG_NONE;
1.155     nicm      564:                if (strcmp(command, "bottom-line") == 0) {
                    565:                        data->cx = 0;
                    566:                        data->cy = screen_size_y(sn) - 1;
                    567:                        window_copy_update_selection(wp, 1);
1.163     nicm      568:                        redraw = 1;
1.155     nicm      569:                }
                    570:                if (strcmp(command, "cancel") == 0)
1.162     nicm      571:                        cancel = 1;
1.155     nicm      572:                if (strcmp(command, "clear-selection") == 0) {
                    573:                        window_copy_clear_selection(wp);
1.163     nicm      574:                        redraw = 1;
1.155     nicm      575:                }
                    576:                if (strcmp(command, "copy-end-of-line") == 0) {
                    577:                        window_copy_start_selection(wp);
                    578:                        for (; np > 1; np--)
                    579:                                window_copy_cursor_down(wp, 0);
                    580:                        window_copy_cursor_end_of_line(wp);
1.163     nicm      581:                        redraw = 1;
1.155     nicm      582:
                    583:                        if (s != NULL) {
                    584:                                window_copy_copy_selection(wp, NULL);
1.162     nicm      585:                                cancel = 1;
1.52      nicm      586:                        }
                    587:                }
1.155     nicm      588:                if (strcmp(command, "copy-line") == 0) {
                    589:                        window_copy_cursor_start_of_line(wp);
                    590:                        window_copy_start_selection(wp);
                    591:                        for (; np > 1; np--)
                    592:                                window_copy_cursor_down(wp, 0);
                    593:                        window_copy_cursor_end_of_line(wp);
1.163     nicm      594:                        redraw = 1;
1.1       nicm      595:
1.155     nicm      596:                        if (s != NULL) {
                    597:                                window_copy_copy_selection(wp, NULL);
1.162     nicm      598:                                cancel = 1;
1.125     nicm      599:                        }
1.155     nicm      600:                }
                    601:                if (strcmp(command, "copy-selection") == 0) {
                    602:                        if (s != NULL)
                    603:                                window_copy_copy_selection(wp, NULL);
1.125     nicm      604:                        window_copy_clear_selection(wp);
1.163     nicm      605:                        redraw = 1;
1.103     nicm      606:                }
1.155     nicm      607:                if (strcmp(command, "copy-selection-and-cancel") == 0) {
                    608:                        if (s != NULL)
                    609:                                window_copy_copy_selection(wp, NULL);
                    610:                        window_copy_clear_selection(wp);
1.163     nicm      611:                        redraw = 1;
1.162     nicm      612:                        cancel = 1;
1.133     nicm      613:                }
1.155     nicm      614:                if (strcmp(command, "cursor-down") == 0) {
                    615:                        for (; np != 0; np--)
                    616:                                window_copy_cursor_down(wp, 0);
                    617:                }
                    618:                if (strcmp(command, "cursor-left") == 0) {
                    619:                        for (; np != 0; np--)
                    620:                                window_copy_cursor_left(wp);
                    621:                }
                    622:                if (strcmp(command, "cursor-right") == 0) {
                    623:                        for (; np != 0; np--)
                    624:                                window_copy_cursor_right(wp);
                    625:                }
                    626:                if (strcmp(command, "cursor-up") == 0) {
                    627:                        for (; np != 0; np--)
                    628:                                window_copy_cursor_up(wp, 0);
                    629:                }
                    630:                if (strcmp(command, "end-of-line") == 0)
                    631:                        window_copy_cursor_end_of_line(wp);
                    632:                if (strcmp(command, "halfpage-down") == 0) {
1.166     nicm      633:                        for (; np != 0; np--) {
                    634:                                if (window_copy_pagedown(wp, 1)) {
                    635:                                        cancel = 1;
                    636:                                        break;
                    637:                                }
                    638:                        }
1.155     nicm      639:                }
                    640:                if (strcmp(command, "halfpage-up") == 0) {
                    641:                        for (; np != 0; np--)
                    642:                                window_copy_pageup(wp, 1);
                    643:                }
                    644:                if (strcmp(command, "history-bottom") == 0) {
                    645:                        data->cx = 0;
                    646:                        data->cy = screen_size_y(sn) - 1;
                    647:                        data->oy = 0;
                    648:                        window_copy_update_selection(wp, 1);
1.163     nicm      649:                        redraw = 1;
1.126     nicm      650:                }
1.155     nicm      651:                if (strcmp(command, "history-top") == 0) {
                    652:                        data->cx = 0;
                    653:                        data->cy = 0;
                    654:                        data->oy = screen_hsize(data->backing);
                    655:                        window_copy_update_selection(wp, 1);
1.163     nicm      656:                        redraw = 1;
1.71      nicm      657:                }
1.155     nicm      658:                if (strcmp(command, "jump-again") == 0) {
                    659:                        switch (data->jumptype) {
                    660:                        case WINDOW_COPY_JUMPFORWARD:
                    661:                                for (; np != 0; np--)
                    662:                                        window_copy_cursor_jump(wp);
                    663:                                break;
                    664:                        case WINDOW_COPY_JUMPBACKWARD:
                    665:                                for (; np != 0; np--)
                    666:                                        window_copy_cursor_jump_back(wp);
                    667:                                break;
                    668:                        case WINDOW_COPY_JUMPTOFORWARD:
                    669:                                for (; np != 0; np--)
                    670:                                        window_copy_cursor_jump_to(wp, 1);
                    671:                                break;
                    672:                        case WINDOW_COPY_JUMPTOBACKWARD:
                    673:                                for (; np != 0; np--)
                    674:                                        window_copy_cursor_jump_to_back(wp, 1);
                    675:                                break;
                    676:                        }
1.89      nicm      677:                }
1.155     nicm      678:                if (strcmp(command, "jump-reverse") == 0) {
                    679:                        switch (data->jumptype) {
                    680:                        case WINDOW_COPY_JUMPFORWARD:
                    681:                                for (; np != 0; np--)
                    682:                                        window_copy_cursor_jump_back(wp);
                    683:                                break;
                    684:                        case WINDOW_COPY_JUMPBACKWARD:
                    685:                                for (; np != 0; np--)
                    686:                                        window_copy_cursor_jump(wp);
                    687:                                break;
                    688:                        case WINDOW_COPY_JUMPTOFORWARD:
                    689:                                for (; np != 0; np--)
                    690:                                        window_copy_cursor_jump_to_back(wp, 1);
                    691:                                break;
                    692:                        case WINDOW_COPY_JUMPTOBACKWARD:
                    693:                                for (; np != 0; np--)
                    694:                                        window_copy_cursor_jump_to(wp, 1);
                    695:                                break;
1.125     nicm      696:                        }
1.155     nicm      697:                }
                    698:                if (strcmp(command, "middle-line") == 0) {
                    699:                        data->cx = 0;
                    700:                        data->cy = (screen_size_y(sn) - 1) / 2;
                    701:                        window_copy_update_selection(wp, 1);
1.163     nicm      702:                        redraw = 1;
1.1       nicm      703:                }
1.155     nicm      704:                if (strcmp(command, "next-paragraph") == 0) {
                    705:                        for (; np != 0; np--)
                    706:                                window_copy_next_paragraph(wp);
                    707:                }
                    708:                if (strcmp(command, "next-space") == 0) {
                    709:                        for (; np != 0; np--)
                    710:                                window_copy_cursor_next_word(wp, " ");
                    711:                }
                    712:                if (strcmp(command, "next-space-end") == 0) {
                    713:                        for (; np != 0; np--)
                    714:                                window_copy_cursor_next_word_end(wp, " ");
                    715:                }
                    716:                if (strcmp(command, "next-word") == 0) {
                    717:                        ws = options_get_string(s->options, "word-separators");
                    718:                        for (; np != 0; np--)
                    719:                                window_copy_cursor_next_word(wp, ws);
                    720:                }
                    721:                if (strcmp(command, "next-word-end") == 0) {
                    722:                        ws = options_get_string(s->options, "word-separators");
1.52      nicm      723:                        for (; np != 0; np--)
1.155     nicm      724:                                window_copy_cursor_next_word_end(wp, ws);
                    725:                }
                    726:                if (strcmp(command, "other-end") == 0) {
                    727:                        if ((np % 2) != 0)
                    728:                                window_copy_other_end(wp);
                    729:                }
                    730:                if (strcmp(command, "page-down") == 0) {
1.166     nicm      731:                        for (; np != 0; np--) {
                    732:                                if (window_copy_pagedown(wp, 0)) {
                    733:                                        cancel = 1;
                    734:                                        break;
                    735:                                }
                    736:                        }
1.155     nicm      737:                }
                    738:                if (strcmp(command, "page-up") == 0) {
1.76      nicm      739:                        for (; np != 0; np--)
1.155     nicm      740:                                window_copy_pageup(wp, 0);
                    741:                }
                    742:                if (strcmp(command, "previous-paragraph") == 0) {
1.76      nicm      743:                        for (; np != 0; np--)
1.155     nicm      744:                                window_copy_previous_paragraph(wp);
1.52      nicm      745:                }
1.155     nicm      746:                if (strcmp(command, "previous-space") == 0) {
1.52      nicm      747:                        for (; np != 0; np--)
1.155     nicm      748:                                window_copy_cursor_previous_word(wp, " ");
                    749:                }
                    750:                if (strcmp(command, "previous-word") == 0) {
                    751:                        ws = options_get_string(s->options, "word-separators");
1.52      nicm      752:                        for (; np != 0; np--)
1.155     nicm      753:                                window_copy_cursor_previous_word(wp, ws);
                    754:                }
                    755:                if (strcmp(command, "rectangle-toggle") == 0) {
                    756:                        sn->sel.lineflag = LINE_SEL_NONE;
                    757:                        window_copy_rectangle_toggle(wp);
                    758:                }
                    759:                if (strcmp(command, "scroll-down") == 0) {
1.76      nicm      760:                        for (; np != 0; np--)
1.155     nicm      761:                                window_copy_cursor_down(wp, 1);
                    762:                        if (data->scroll_exit && data->oy == 0)
1.162     nicm      763:                                cancel = 1;
1.155     nicm      764:                }
                    765:                if (strcmp(command, "scroll-up") == 0) {
1.76      nicm      766:                        for (; np != 0; np--)
1.155     nicm      767:                                window_copy_cursor_up(wp, 1);
1.52      nicm      768:                }
1.155     nicm      769:                if (strcmp(command, "search-again") == 0) {
                    770:                        if (data->searchtype == WINDOW_COPY_SEARCHUP) {
1.118     nicm      771:                                for (; np != 0; np--)
1.162     nicm      772:                                        window_copy_search_up(wp, 1);
1.155     nicm      773:                        } else if (data->searchtype == WINDOW_COPY_SEARCHDOWN) {
1.118     nicm      774:                                for (; np != 0; np--)
1.162     nicm      775:                                        window_copy_search_down(wp, 1);
1.50      nicm      776:                        }
1.155     nicm      777:                }
                    778:                if (strcmp(command, "search-reverse") == 0) {
                    779:                        if (data->searchtype == WINDOW_COPY_SEARCHUP) {
1.118     nicm      780:                                for (; np != 0; np--)
1.162     nicm      781:                                        window_copy_search_down(wp, 1);
1.155     nicm      782:                        } else if (data->searchtype == WINDOW_COPY_SEARCHDOWN) {
1.118     nicm      783:                                for (; np != 0; np--)
1.162     nicm      784:                                        window_copy_search_up(wp, 1);
1.50      nicm      785:                        }
1.21      nicm      786:                }
1.155     nicm      787:                if (strcmp(command, "select-line") == 0) {
                    788:                        sn->sel.lineflag = LINE_SEL_LEFT_RIGHT;
                    789:                        data->rectflag = 0;
                    790:                        window_copy_cursor_start_of_line(wp);
                    791:                        window_copy_start_selection(wp);
                    792:                        for (; np > 1; np--)
                    793:                                window_copy_cursor_down(wp, 0);
                    794:                        window_copy_cursor_end_of_line(wp);
1.163     nicm      795:                        redraw = 1;
1.155     nicm      796:                }
1.156     nicm      797:                if (strcmp(command, "select-word") == 0) {
                    798:                        sn->sel.lineflag = LINE_SEL_LEFT_RIGHT;
                    799:                        data->rectflag = 0;
                    800:                        ws = options_get_string(s->options, "word-separators");
                    801:                        window_copy_cursor_previous_word(wp, ws);
                    802:                        window_copy_start_selection(wp);
                    803:                        window_copy_cursor_next_word_end(wp, ws);
1.163     nicm      804:                        redraw = 1;
1.156     nicm      805:                }
                    806:                if (strcmp(command, "start-of-line") == 0)
1.155     nicm      807:                        window_copy_cursor_start_of_line(wp);
                    808:                if (strcmp(command, "top-line") == 0) {
                    809:                        data->cx = 0;
                    810:                        data->cy = 0;
                    811:                        window_copy_update_selection(wp, 1);
1.163     nicm      812:                        redraw = 1;
1.155     nicm      813:                }
                    814:        } else if (args->argc == 2 && *args->argv[1] != '\0') {
                    815:                argument = args->argv[1];
                    816:                if (strcmp(command, "copy-pipe") == 0) {
1.160     nicm      817:                        if (s != NULL)
                    818:                                window_copy_copy_pipe(wp, s, NULL, argument);
                    819:                }
                    820:                if (strcmp(command, "copy-pipe-and-cancel") == 0) {
1.155     nicm      821:                        if (s != NULL) {
                    822:                                window_copy_copy_pipe(wp, s, NULL, argument);
1.162     nicm      823:                                cancel = 1;
1.155     nicm      824:                        }
                    825:                }
                    826:                if (strcmp(command, "goto-line") == 0)
                    827:                        window_copy_goto_line(wp, argument);
                    828:                if (strcmp(command, "jump-backward") == 0) {
                    829:                        data->jumptype = WINDOW_COPY_JUMPBACKWARD;
                    830:                        data->jumpchar = *argument;
                    831:                        for (; np != 0; np--)
                    832:                                window_copy_cursor_jump_back(wp);
                    833:                }
                    834:                if (strcmp(command, "jump-forward") == 0) {
                    835:                        data->jumptype = WINDOW_COPY_JUMPFORWARD;
                    836:                        data->jumpchar = *argument;
                    837:                        for (; np != 0; np--)
                    838:                                window_copy_cursor_jump(wp);
                    839:                }
                    840:                if (strcmp(command, "jump-to-backward") == 0) {
                    841:                        data->jumptype = WINDOW_COPY_JUMPTOBACKWARD;
                    842:                        data->jumpchar = *argument;
                    843:                        for (; np != 0; np--)
                    844:                                window_copy_cursor_jump_to_back(wp, 1);
1.50      nicm      845:                }
1.155     nicm      846:                if (strcmp(command, "jump-to-forward") == 0) {
                    847:                        data->jumptype = WINDOW_COPY_JUMPTOFORWARD;
                    848:                        data->jumpchar = *argument;
                    849:                        for (; np != 0; np--)
                    850:                                window_copy_cursor_jump_to(wp, 1);
1.100     nicm      851:                }
1.155     nicm      852:                if (strcmp(command, "search-backward") == 0) {
                    853:                        data->searchtype = WINDOW_COPY_SEARCHUP;
1.163     nicm      854:                        free(data->searchstr);
1.155     nicm      855:                        data->searchstr = xstrdup(argument);
1.150     nicm      856:                        for (; np != 0; np--)
1.162     nicm      857:                                window_copy_search_up(wp, 1);
1.155     nicm      858:                }
                    859:                if (strcmp(command, "search-forward") == 0) {
                    860:                        data->searchtype = WINDOW_COPY_SEARCHDOWN;
1.163     nicm      861:                        free(data->searchstr);
1.155     nicm      862:                        data->searchstr = xstrdup(argument);
1.150     nicm      863:                        for (; np != 0; np--)
1.162     nicm      864:                                window_copy_search_down(wp, 1);
1.21      nicm      865:                }
1.163     nicm      866:                if (strcmp(command, "search-backward-incremental") == 0) {
                    867:                        prefix = *argument++;
                    868:                        if (data->searchx == -1 || data->searchy == -1) {
                    869:                                data->searchx = data->cx;
                    870:                                data->searchy = data->cy;
                    871:                                data->searcho = data->oy;
                    872:                        } else if (data->searchstr != NULL &&
                    873:                            strcmp(argument, data->searchstr) != 0) {
                    874:                                data->cx = data->searchx;
                    875:                                data->cy = data->searchy;
                    876:                                data->oy = data->searcho;
                    877:                                redraw = 1;
                    878:                        }
                    879:                        if (*argument == '\0') {
                    880:                                window_copy_clear_marks(wp);
                    881:                                redraw = 1;
                    882:                        } else if (prefix == '=' || prefix == '-') {
                    883:                                data->searchtype = WINDOW_COPY_SEARCHUP;
                    884:                                free(data->searchstr);
                    885:                                data->searchstr = xstrdup(argument);
                    886:                                if (!window_copy_search_up(wp, 1)) {
                    887:                                        window_copy_clear_marks(wp);
                    888:                                        redraw = 1;
                    889:                                }
                    890:                        } else if (prefix == '+') {
                    891:                                data->searchtype = WINDOW_COPY_SEARCHDOWN;
                    892:                                free(data->searchstr);
                    893:                                data->searchstr = xstrdup(argument);
                    894:                                if (!window_copy_search_down(wp, 1)) {
                    895:                                        window_copy_clear_marks(wp);
                    896:                                        redraw = 1;
                    897:                                }
                    898:                        }
                    899:                }
                    900:                if (strcmp(command, "search-forward-incremental") == 0) {
                    901:                        prefix = *argument++;
                    902:                        if (data->searchx == -1 || data->searchy == -1) {
                    903:                                data->searchx = data->cx;
                    904:                                data->searchy = data->cy;
                    905:                                data->searcho = data->oy;
                    906:                        } else if (data->searchstr != NULL &&
                    907:                            strcmp(argument, data->searchstr) != 0) {
                    908:                                data->cx = data->searchx;
                    909:                                data->cy = data->searchy;
                    910:                                data->oy = data->searcho;
                    911:                                redraw = 1;
                    912:                        }
                    913:                        if (*argument == '\0') {
                    914:                                window_copy_clear_marks(wp);
                    915:                                redraw = 1;
                    916:                        } else if (prefix == '=' || prefix == '+') {
                    917:                                data->searchtype = WINDOW_COPY_SEARCHDOWN;
                    918:                                free(data->searchstr);
                    919:                                data->searchstr = xstrdup(argument);
                    920:                                if (!window_copy_search_down(wp, 1)) {
                    921:                                        window_copy_clear_marks(wp);
                    922:                                        redraw = 1;
                    923:                                }
                    924:                        } else if (prefix == '-') {
                    925:                                data->searchtype = WINDOW_COPY_SEARCHUP;
                    926:                                free(data->searchstr);
                    927:                                data->searchstr = xstrdup(argument);
                    928:                                if (!window_copy_search_up(wp, 1)) {
                    929:                                        window_copy_clear_marks(wp);
                    930:                                        redraw = 1;
                    931:                                }
                    932:                        }
                    933:                }
1.1       nicm      934:        }
1.21      nicm      935:
1.162     nicm      936:        if (strncmp(command, "search-", 7) != 0 && data->searchmark != NULL) {
1.163     nicm      937:                window_copy_clear_marks(wp);
                    938:                redraw = 1;
                    939:                data->searchx = data->searchy = -1;
1.162     nicm      940:        }
                    941:
                    942:        if (cancel)
                    943:                window_pane_reset_mode(wp);
1.163     nicm      944:        else if (redraw)
                    945:                window_copy_redraw_screen(wp);
1.155     nicm      946:        wp->modeprefix = 1;
1.50      nicm      947: }
                    948:
1.157     nicm      949: static void
1.21      nicm      950: window_copy_scroll_to(struct window_pane *wp, u_int px, u_int py)
                    951: {
                    952:        struct window_copy_mode_data    *data = wp->modedata;
1.54      nicm      953:        struct grid                     *gd = data->backing->grid;
1.21      nicm      954:        u_int                            offset, gap;
                    955:
                    956:        data->cx = px;
                    957:
                    958:        gap = gd->sy / 4;
                    959:        if (py < gd->sy) {
                    960:                offset = 0;
                    961:                data->cy = py;
                    962:        } else if (py > gd->hsize + gd->sy - gap) {
                    963:                offset = gd->hsize;
                    964:                data->cy = py - gd->hsize;
                    965:        } else {
                    966:                offset = py + gap - gd->sy;
                    967:                data->cy = py - offset;
                    968:        }
1.35      nicm      969:        data->oy = gd->hsize - offset;
1.21      nicm      970:
1.96      nicm      971:        window_copy_update_selection(wp, 1);
1.21      nicm      972:        window_copy_redraw_screen(wp);
                    973: }
                    974:
1.157     nicm      975: static int
1.118     nicm      976: window_copy_search_compare(struct grid *gd, u_int px, u_int py,
                    977:     struct grid *sgd, u_int spx, int cis)
1.21      nicm      978: {
1.140     nicm      979:        struct grid_cell         gc, sgc;
                    980:        const struct utf8_data  *ud, *sud;
1.21      nicm      981:
1.140     nicm      982:        grid_get_cell(gd, px, py, &gc);
                    983:        ud = &gc.data;
                    984:        grid_get_cell(sgd, spx, 0, &sgc);
                    985:        sud = &sgc.data;
1.35      nicm      986:
1.140     nicm      987:        if (ud->size != sud->size || ud->width != sud->width)
1.21      nicm      988:                return (0);
1.97      nicm      989:
1.140     nicm      990:        if (cis && ud->size == 1)
                    991:                return (tolower(ud->data[0]) == sud->data[0]);
1.97      nicm      992:
1.140     nicm      993:        return (memcmp(ud->data, sud->data, ud->size) == 0);
1.21      nicm      994: }
                    995:
1.157     nicm      996: static int
1.21      nicm      997: window_copy_search_lr(struct grid *gd,
1.97      nicm      998:     struct grid *sgd, u_int *ppx, u_int py, u_int first, u_int last, int cis)
1.21      nicm      999: {
                   1000:        u_int   ax, bx, px;
1.97      nicm     1001:        int     matched;
1.21      nicm     1002:
                   1003:        for (ax = first; ax < last; ax++) {
1.181     nicm     1004:                if (ax + sgd->sx > gd->sx)
1.21      nicm     1005:                        break;
                   1006:                for (bx = 0; bx < sgd->sx; bx++) {
                   1007:                        px = ax + bx;
1.97      nicm     1008:                        matched = window_copy_search_compare(gd, px, py, sgd,
                   1009:                            bx, cis);
                   1010:                        if (!matched)
1.21      nicm     1011:                                break;
                   1012:                }
                   1013:                if (bx == sgd->sx) {
                   1014:                        *ppx = ax;
                   1015:                        return (1);
                   1016:                }
                   1017:        }
                   1018:        return (0);
                   1019: }
                   1020:
1.157     nicm     1021: static int
1.21      nicm     1022: window_copy_search_rl(struct grid *gd,
1.97      nicm     1023:     struct grid *sgd, u_int *ppx, u_int py, u_int first, u_int last, int cis)
1.21      nicm     1024: {
                   1025:        u_int   ax, bx, px;
1.97      nicm     1026:        int     matched;
1.21      nicm     1027:
                   1028:        for (ax = last + 1; ax > first; ax--) {
1.24      nicm     1029:                if (gd->sx - (ax - 1) < sgd->sx)
                   1030:                        continue;
1.21      nicm     1031:                for (bx = 0; bx < sgd->sx; bx++) {
                   1032:                        px = ax - 1 + bx;
1.97      nicm     1033:                        matched = window_copy_search_compare(gd, px, py, sgd,
                   1034:                            bx, cis);
                   1035:                        if (!matched)
1.21      nicm     1036:                                break;
                   1037:                }
                   1038:                if (bx == sgd->sx) {
                   1039:                        *ppx = ax - 1;
                   1040:                        return (1);
                   1041:                }
                   1042:        }
                   1043:        return (0);
                   1044: }
                   1045:
1.157     nicm     1046: static void
1.150     nicm     1047: window_copy_move_left(struct screen *s, u_int *fx, u_int *fy)
1.21      nicm     1048: {
1.150     nicm     1049:        if (*fx == 0) { /* left */
                   1050:                if (*fy == 0) /* top */
                   1051:                        return;
                   1052:                *fx = screen_size_x(s) - 1;
                   1053:                *fy = *fy - 1;
                   1054:        } else
                   1055:                *fx = *fx - 1;
                   1056: }
1.21      nicm     1057:
1.157     nicm     1058: static void
1.150     nicm     1059: window_copy_move_right(struct screen *s, u_int *fx, u_int *fy)
                   1060: {
                   1061:        if (*fx == screen_size_x(s) - 1) { /* right */
                   1062:                if (*fy == screen_hsize(s) + screen_size_y(s)) /* bottom */
1.21      nicm     1063:                        return;
1.150     nicm     1064:                *fx = 0;
                   1065:                *fy = *fy + 1;
1.21      nicm     1066:        } else
1.150     nicm     1067:                *fx = *fx + 1;
                   1068: }
1.21      nicm     1069:
1.157     nicm     1070: static int
1.150     nicm     1071: window_copy_is_lowercase(const char *ptr)
                   1072: {
                   1073:        while (*ptr != '\0') {
                   1074:                if (*ptr != tolower((u_char)*ptr))
                   1075:                        return (0);
                   1076:                ++ptr;
1.97      nicm     1077:        }
1.150     nicm     1078:        return (1);
                   1079: }
1.97      nicm     1080:
1.150     nicm     1081: /*
                   1082:  * Search for text stored in sgd starting from position fx,fy up to endline. If
                   1083:  * found, jump to it. If cis then ignore case. The direction is 0 for searching
                   1084:  * up, down otherwise. If wrap then go to begin/end of grid and try again if
                   1085:  * not found.
                   1086:  */
1.163     nicm     1087: static int
1.150     nicm     1088: window_copy_search_jump(struct window_pane *wp, struct grid *gd,
                   1089:     struct grid *sgd, u_int fx, u_int fy, u_int endline, int cis, int wrap,
                   1090:     int direction)
                   1091: {
                   1092:        u_int   i, px;
                   1093:        int     found;
                   1094:
                   1095:        found = 0;
                   1096:        if (direction) {
                   1097:                for (i = fy; i <= endline; i++) {
                   1098:                        found = window_copy_search_lr(gd, sgd, &px, i, fx,
                   1099:                            gd->sx, cis);
                   1100:                        if (found)
                   1101:                                break;
                   1102:                        fx = 0;
                   1103:                }
                   1104:        } else {
                   1105:                for (i = fy + 1; endline < i; i--) {
                   1106:                        found = window_copy_search_rl(gd, sgd, &px, i - 1, 0,
                   1107:                            fx, cis);
                   1108:                        if (found) {
                   1109:                                i--;
                   1110:                                break;
                   1111:                        }
                   1112:                        fx = gd->sx;
1.21      nicm     1113:                }
                   1114:        }
1.150     nicm     1115:
1.163     nicm     1116:        if (found) {
1.150     nicm     1117:                window_copy_scroll_to(wp, px, i);
1.163     nicm     1118:                return (1);
                   1119:        }
                   1120:        if (wrap) {
                   1121:                return (window_copy_search_jump(wp, gd, sgd,
                   1122:                    direction ? 0 : gd->sx - 1,
1.150     nicm     1123:                    direction ? 0 : gd->hsize + gd->sy - 1, fy, cis, 0,
1.163     nicm     1124:                    direction));
1.21      nicm     1125:        }
1.163     nicm     1126:        return (0);
1.21      nicm     1127: }
                   1128:
1.150     nicm     1129: /*
                   1130:  * Search in for text searchstr. If direction is 0 then search up, otherwise
                   1131:  * down. If moveflag is 0 then look for string at the current cursor position
                   1132:  * as well.
                   1133:  */
1.163     nicm     1134: static int
1.162     nicm     1135: window_copy_search(struct window_pane *wp, int direction, int moveflag)
1.21      nicm     1136: {
                   1137:        struct window_copy_mode_data    *data = wp->modedata;
1.54      nicm     1138:        struct screen                   *s = data->backing, ss;
1.21      nicm     1139:        struct screen_write_ctx          ctx;
1.150     nicm     1140:        struct grid                     *gd = s->grid;
                   1141:        u_int                            fx, fy, endline;
1.163     nicm     1142:        int                              wrapflag, cis, found;
1.21      nicm     1143:
1.174     nicm     1144:        free(wp->searchstr);
                   1145:        wp->searchstr = xstrdup(data->searchstr);
                   1146:
1.150     nicm     1147:        fx = data->cx;
                   1148:        fy = screen_hsize(data->backing) - data->oy + data->cy;
1.21      nicm     1149:
1.162     nicm     1150:        screen_init(&ss, screen_write_strlen("%s", data->searchstr), 1, 0);
1.21      nicm     1151:        screen_write_start(&ctx, NULL, &ss);
1.162     nicm     1152:        screen_write_nputs(&ctx, -1, &grid_default_cell, "%s", data->searchstr);
1.21      nicm     1153:        screen_write_stop(&ctx);
                   1154:
1.150     nicm     1155:        if (moveflag) {
                   1156:                if (direction)
                   1157:                        window_copy_move_right(s, &fx, &fy);
                   1158:                else
                   1159:                        window_copy_move_left(s, &fx, &fy);
                   1160:        }
                   1161:        window_copy_clear_selection(wp);
                   1162:
                   1163:        wrapflag = options_get_number(wp->window->options, "wrap-search");
1.162     nicm     1164:        cis = window_copy_is_lowercase(data->searchstr);
1.21      nicm     1165:
1.150     nicm     1166:        if (direction)
                   1167:                endline = gd->hsize + gd->sy - 1;
                   1168:        else
                   1169:                endline = 0;
1.163     nicm     1170:        found = window_copy_search_jump(wp, gd, ss.grid, fx, fy, endline, cis,
1.162     nicm     1171:            wrapflag, direction);
                   1172:
                   1173:        if (window_copy_search_marks(wp, &ss))
                   1174:                window_copy_redraw_screen(wp);
1.21      nicm     1175:
1.150     nicm     1176:        screen_free(&ss);
1.163     nicm     1177:        return (found);
1.150     nicm     1178: }
1.97      nicm     1179:
1.162     nicm     1180: static int
                   1181: window_copy_search_marks(struct window_pane *wp, struct screen *ssp)
                   1182: {
                   1183:        struct window_copy_mode_data    *data = wp->modedata;
                   1184:        struct screen                   *s = data->backing, ss;
                   1185:        struct screen_write_ctx          ctx;
                   1186:        struct grid                     *gd = s->grid;
1.170     nicm     1187:        int                              found, cis, which = -1;
1.162     nicm     1188:        u_int                            px, py, b, nfound = 0, width;
                   1189:
                   1190:        if (ssp == NULL) {
                   1191:                width = screen_write_strlen("%s", data->searchstr);
                   1192:                screen_init(&ss, width, 1, 0);
                   1193:                screen_write_start(&ctx, NULL, &ss);
                   1194:                screen_write_nputs(&ctx, -1, &grid_default_cell, "%s",
                   1195:                    data->searchstr);
                   1196:                screen_write_stop(&ctx);
                   1197:                ssp = &ss;
                   1198:        } else
                   1199:                width = screen_size_x(ssp);
                   1200:
                   1201:        cis = window_copy_is_lowercase(data->searchstr);
                   1202:
                   1203:        free(data->searchmark);
                   1204:        data->searchmark = bit_alloc((gd->hsize + gd->sy) * gd->sx);
                   1205:
                   1206:        for (py = 0; py < gd->hsize + gd->sy; py++) {
                   1207:                px = 0;
                   1208:                for (;;) {
                   1209:                        found = window_copy_search_lr(gd, ssp->grid, &px, py,
                   1210:                            px, gd->sx, cis);
                   1211:                        if (!found)
                   1212:                                break;
1.170     nicm     1213:
1.162     nicm     1214:                        nfound++;
1.170     nicm     1215:                        if (px == data->cx && py == gd->hsize + data->cy - data->oy)
                   1216:                                which = nfound;
1.162     nicm     1217:
                   1218:                        b = (py * gd->sx) + px;
                   1219:                        bit_nset(data->searchmark, b, b + width - 1);
                   1220:
                   1221:                        px++;
                   1222:                }
                   1223:        }
                   1224:
1.170     nicm     1225:        if (which != -1)
                   1226:                data->searchthis = 1 + nfound - which;
                   1227:        else
                   1228:                data->searchthis = -1;
                   1229:        data->searchcount = nfound;
                   1230:
1.162     nicm     1231:        if (ssp == &ss)
                   1232:                screen_free(&ss);
                   1233:        return (nfound);
                   1234: }
                   1235:
1.157     nicm     1236: static void
1.163     nicm     1237: window_copy_clear_marks(struct window_pane *wp)
                   1238: {
                   1239:        struct window_copy_mode_data    *data = wp->modedata;
                   1240:
                   1241:        free(data->searchmark);
                   1242:        data->searchmark = NULL;
                   1243: }
                   1244:
                   1245: static int
1.162     nicm     1246: window_copy_search_up(struct window_pane *wp, int moveflag)
1.150     nicm     1247: {
1.163     nicm     1248:        return (window_copy_search(wp, 0, moveflag));
1.150     nicm     1249: }
1.21      nicm     1250:
1.163     nicm     1251: static int
1.162     nicm     1252: window_copy_search_down(struct window_pane *wp, int moveflag)
1.150     nicm     1253: {
1.163     nicm     1254:        return (window_copy_search(wp, 1, moveflag));
1.21      nicm     1255: }
                   1256:
1.157     nicm     1257: static void
1.21      nicm     1258: window_copy_goto_line(struct window_pane *wp, const char *linestr)
                   1259: {
                   1260:        struct window_copy_mode_data    *data = wp->modedata;
                   1261:        const char                      *errstr;
                   1262:        u_int                            lineno;
                   1263:
1.54      nicm     1264:        lineno = strtonum(linestr, 0, screen_hsize(data->backing), &errstr);
1.21      nicm     1265:        if (errstr != NULL)
                   1266:                return;
1.35      nicm     1267:
1.21      nicm     1268:        data->oy = lineno;
1.96      nicm     1269:        window_copy_update_selection(wp, 1);
1.21      nicm     1270:        window_copy_redraw_screen(wp);
                   1271: }
                   1272:
1.157     nicm     1273: static void
1.118     nicm     1274: window_copy_write_line(struct window_pane *wp, struct screen_write_ctx *ctx,
                   1275:     u_int py)
1.1       nicm     1276: {
                   1277:        struct window_copy_mode_data    *data = wp->modedata;
                   1278:        struct screen                   *s = &data->screen;
1.136     nicm     1279:        struct options                  *oo = wp->window->options;
1.1       nicm     1280:        struct grid_cell                 gc;
1.100     nicm     1281:        char                             hdr[512];
1.162     nicm     1282:        size_t                           size = 0;
1.21      nicm     1283:
1.101     nicm     1284:        style_apply(&gc, oo, "mode-style");
1.164     nicm     1285:        gc.flags |= GRID_FLAG_NOPALETTE;
1.1       nicm     1286:
                   1287:        if (py == 0) {
1.170     nicm     1288:                if (data->searchmark == NULL) {
                   1289:                        size = xsnprintf(hdr, sizeof hdr,
                   1290:                            "[%u/%u]", data->oy, screen_hsize(data->backing));
                   1291:                } else {
                   1292:                        if (data->searchthis == -1) {
                   1293:                                size = xsnprintf(hdr, sizeof hdr,
                   1294:                                    "(%u results) [%d/%u]", data->searchcount,
                   1295:                                    data->oy, screen_hsize(data->backing));
                   1296:                        } else {
                   1297:                                size = xsnprintf(hdr, sizeof hdr,
                   1298:                                    "(%u/%u results) [%d/%u]", data->searchthis,
                   1299:                                    data->searchcount, data->oy,
                   1300:                                    screen_hsize(data->backing));
                   1301:                        }
                   1302:                }
1.62      nicm     1303:                if (size > screen_size_x(s))
                   1304:                        size = screen_size_x(s);
1.1       nicm     1305:                screen_write_cursormove(ctx, screen_size_x(s) - size, 0);
                   1306:                screen_write_puts(ctx, &gc, "%s", hdr);
                   1307:        } else
                   1308:                size = 0;
                   1309:
1.105     nicm     1310:        if (size < screen_size_x(s)) {
1.162     nicm     1311:                screen_write_cursormove(ctx, 0, py);
                   1312:                screen_write_copy(ctx, data->backing, 0,
1.105     nicm     1313:                    (screen_hsize(data->backing) - data->oy) + py,
1.162     nicm     1314:                    screen_size_x(s) - size, 1, data->searchmark, &gc);
1.105     nicm     1315:        }
1.18      nicm     1316:
                   1317:        if (py == data->cy && data->cx == screen_size_x(s)) {
                   1318:                memcpy(&gc, &grid_default_cell, sizeof gc);
                   1319:                screen_write_cursormove(ctx, screen_size_x(s) - 1, py);
                   1320:                screen_write_putc(ctx, &gc, '$');
                   1321:        }
1.1       nicm     1322: }
                   1323:
1.157     nicm     1324: static void
1.118     nicm     1325: window_copy_write_lines(struct window_pane *wp, struct screen_write_ctx *ctx,
                   1326:     u_int py, u_int ny)
1.1       nicm     1327: {
                   1328:        u_int   yy;
                   1329:
                   1330:        for (yy = py; yy < py + ny; yy++)
                   1331:                window_copy_write_line(wp, ctx, py);
1.121     nicm     1332: }
                   1333:
1.157     nicm     1334: static void
1.121     nicm     1335: window_copy_redraw_selection(struct window_pane *wp, u_int old_y)
                   1336: {
                   1337:        struct window_copy_mode_data    *data = wp->modedata;
                   1338:        u_int                            new_y, start, end;
                   1339:
                   1340:        new_y = data->cy;
                   1341:        if (old_y <= new_y) {
                   1342:                start = old_y;
                   1343:                end = new_y;
                   1344:        } else {
                   1345:                start = new_y;
                   1346:                end = old_y;
                   1347:        }
                   1348:        window_copy_redraw_lines(wp, start, end - start + 1);
1.1       nicm     1349: }
                   1350:
1.157     nicm     1351: static void
1.1       nicm     1352: window_copy_redraw_lines(struct window_pane *wp, u_int py, u_int ny)
                   1353: {
                   1354:        struct window_copy_mode_data    *data = wp->modedata;
                   1355:        struct screen_write_ctx          ctx;
                   1356:        u_int                            i;
                   1357:
                   1358:        screen_write_start(&ctx, wp, NULL);
                   1359:        for (i = py; i < py + ny; i++)
                   1360:                window_copy_write_line(wp, &ctx, i);
                   1361:        screen_write_cursormove(&ctx, data->cx, data->cy);
                   1362:        screen_write_stop(&ctx);
                   1363: }
                   1364:
1.157     nicm     1365: static void
1.1       nicm     1366: window_copy_redraw_screen(struct window_pane *wp)
                   1367: {
                   1368:        struct window_copy_mode_data    *data = wp->modedata;
                   1369:
                   1370:        window_copy_redraw_lines(wp, 0, screen_size_y(&data->screen));
                   1371: }
                   1372:
1.157     nicm     1373: static void
1.161     nicm     1374: window_copy_synchronize_cursor(struct window_pane *wp)
                   1375: {
                   1376:        struct window_copy_mode_data    *data = wp->modedata;
                   1377:        u_int                            xx, yy;
                   1378:
                   1379:        xx = data->cx;
                   1380:        yy = screen_hsize(data->backing) + data->cy - data->oy;
                   1381:
                   1382:        switch (data->cursordrag) {
                   1383:        case CURSORDRAG_ENDSEL:
                   1384:                data->endselx = xx;
                   1385:                data->endsely = yy;
                   1386:                break;
                   1387:        case CURSORDRAG_SEL:
                   1388:                data->selx = xx;
                   1389:                data->sely = yy;
                   1390:                break;
                   1391:        case CURSORDRAG_NONE:
                   1392:                break;
                   1393:        }
                   1394: }
                   1395:
                   1396: static void
1.18      nicm     1397: window_copy_update_cursor(struct window_pane *wp, u_int cx, u_int cy)
1.1       nicm     1398: {
                   1399:        struct window_copy_mode_data    *data = wp->modedata;
1.18      nicm     1400:        struct screen                   *s = &data->screen;
1.1       nicm     1401:        struct screen_write_ctx          ctx;
1.18      nicm     1402:        u_int                            old_cx, old_cy;
1.1       nicm     1403:
1.18      nicm     1404:        old_cx = data->cx; old_cy = data->cy;
                   1405:        data->cx = cx; data->cy = cy;
                   1406:        if (old_cx == screen_size_x(s))
                   1407:                window_copy_redraw_lines(wp, old_cy, 1);
                   1408:        if (data->cx == screen_size_x(s))
                   1409:                window_copy_redraw_lines(wp, data->cy, 1);
                   1410:        else {
                   1411:                screen_write_start(&ctx, wp, NULL);
                   1412:                screen_write_cursormove(&ctx, data->cx, data->cy);
                   1413:                screen_write_stop(&ctx);
                   1414:        }
1.1       nicm     1415: }
                   1416:
1.157     nicm     1417: static void
1.1       nicm     1418: window_copy_start_selection(struct window_pane *wp)
                   1419: {
                   1420:        struct window_copy_mode_data    *data = wp->modedata;
                   1421:        struct screen                   *s = &data->screen;
                   1422:
1.18      nicm     1423:        data->selx = data->cx;
1.54      nicm     1424:        data->sely = screen_hsize(data->backing) + data->cy - data->oy;
1.1       nicm     1425:
1.161     nicm     1426:        data->endselx = data->selx;
                   1427:        data->endsely = data->sely;
                   1428:
                   1429:        data->cursordrag = CURSORDRAG_ENDSEL;
                   1430:
1.1       nicm     1431:        s->sel.flag = 1;
1.96      nicm     1432:        window_copy_update_selection(wp, 1);
1.1       nicm     1433: }
                   1434:
1.157     nicm     1435: static int
1.161     nicm     1436: window_copy_adjust_selection(struct window_pane *wp, u_int *selx, u_int *sely)
                   1437: {
                   1438:        struct window_copy_mode_data    *data = wp->modedata;
                   1439:        struct screen                   *s = &data->screen;
                   1440:        u_int                            sx, sy, ty;
                   1441:        int                              relpos;
                   1442:
                   1443:        sx = *selx;
                   1444:        sy = *sely;
                   1445:
                   1446:        ty = screen_hsize(data->backing) - data->oy;
                   1447:        if (sy < ty) {
                   1448:                relpos = WINDOW_COPY_REL_POS_ABOVE;
                   1449:                if (!data->rectflag)
                   1450:                        sx = 0;
                   1451:                sy = 0;
                   1452:        } else if (sy > ty + screen_size_y(s) - 1) {
                   1453:                relpos = WINDOW_COPY_REL_POS_BELOW;
                   1454:                if (!data->rectflag)
                   1455:                        sx = screen_size_x(s) - 1;
                   1456:                sy = screen_size_y(s) - 1;
                   1457:        } else {
                   1458:                relpos = WINDOW_COPY_REL_POS_ON_SCREEN;
                   1459:                sy -= ty;
                   1460:        }
                   1461:
                   1462:        *selx = sx;
1.176     nicm     1463:        *sely = sy;
1.161     nicm     1464:        return (relpos);
                   1465: }
                   1466:
                   1467: static int
1.96      nicm     1468: window_copy_update_selection(struct window_pane *wp, int may_redraw)
1.1       nicm     1469: {
                   1470:        struct window_copy_mode_data    *data = wp->modedata;
                   1471:        struct screen                   *s = &data->screen;
1.136     nicm     1472:        struct options                  *oo = wp->window->options;
1.1       nicm     1473:        struct grid_cell                 gc;
1.161     nicm     1474:        u_int                            sx, sy, cy, endsx, endsy;
                   1475:        int                              startrelpos, endrelpos;
1.1       nicm     1476:
1.118     nicm     1477:        if (!s->sel.flag && s->sel.lineflag == LINE_SEL_NONE)
1.1       nicm     1478:                return (0);
                   1479:
1.161     nicm     1480:        window_copy_synchronize_cursor(wp);
1.1       nicm     1481:
                   1482:        /* Adjust the selection. */
                   1483:        sx = data->selx;
                   1484:        sy = data->sely;
1.161     nicm     1485:        startrelpos = window_copy_adjust_selection(wp, &sx, &sy);
                   1486:
                   1487:        /* Adjust the end of selection. */
                   1488:        endsx = data->endselx;
                   1489:        endsy = data->endsely;
                   1490:        endrelpos = window_copy_adjust_selection(wp, &endsx, &endsy);
                   1491:
                   1492:        /* Selection is outside of the current screen */
                   1493:        if (startrelpos == endrelpos &&
                   1494:            startrelpos != WINDOW_COPY_REL_POS_ON_SCREEN) {
                   1495:                screen_hide_selection(s);
                   1496:                return (0);
                   1497:        }
1.1       nicm     1498:
1.161     nicm     1499:        /* Set colours and selection. */
                   1500:        style_apply(&gc, oo, "mode-style");
1.164     nicm     1501:        gc.flags |= GRID_FLAG_NOPALETTE;
1.161     nicm     1502:        screen_set_selection(s, sx, sy, endsx, endsy, data->rectflag, &gc);
1.42      nicm     1503:
1.96      nicm     1504:        if (data->rectflag && may_redraw) {
1.42      nicm     1505:                /*
                   1506:                 * Can't rely on the caller to redraw the right lines for
                   1507:                 * rectangle selection - find the highest line and the number
                   1508:                 * of lines, and redraw just past that in both directions
                   1509:                 */
                   1510:                cy = data->cy;
1.182     nicm     1511:                if (data->cursordrag == CURSORDRAG_ENDSEL) {
                   1512:                        if (sy < cy)
                   1513:                                window_copy_redraw_lines(wp, sy, cy - sy + 1);
                   1514:                        else
                   1515:                                window_copy_redraw_lines(wp, cy, sy - cy + 1);
                   1516:                } else {
                   1517:                        if (endsy < cy)
                   1518:                                window_copy_redraw_lines(wp, endsy, cy - endsy + 1);
                   1519:                        else
                   1520:                                window_copy_redraw_lines(wp, cy, endsy - cy + 1);
                   1521:                }
1.42      nicm     1522:        }
                   1523:
1.1       nicm     1524:        return (1);
                   1525: }
                   1526:
1.157     nicm     1527: static void *
1.89      nicm     1528: window_copy_get_selection(struct window_pane *wp, size_t *len)
1.1       nicm     1529: {
                   1530:        struct window_copy_mode_data    *data = wp->modedata;
                   1531:        struct screen                   *s = &data->screen;
                   1532:        char                            *buf;
1.42      nicm     1533:        size_t                           off;
1.111     nicm     1534:        u_int                            i, xx, yy, sx, sy, ex, ey, ey_last;
1.42      nicm     1535:        u_int                            firstsx, lastex, restex, restsx;
1.69      nicm     1536:        int                              keys;
1.1       nicm     1537:
1.118     nicm     1538:        if (!s->sel.flag && s->sel.lineflag == LINE_SEL_NONE)
1.89      nicm     1539:                return (NULL);
1.1       nicm     1540:
                   1541:        buf = xmalloc(1);
                   1542:        off = 0;
                   1543:
                   1544:        *buf = '\0';
                   1545:
                   1546:        /*
                   1547:         * The selection extends from selx,sely to (adjusted) cx,cy on
                   1548:         * the base screen.
                   1549:         */
                   1550:
                   1551:        /* Find start and end. */
1.161     nicm     1552:        xx = data->endselx;
                   1553:        yy = data->endsely;
1.2       nicm     1554:        if (yy < data->sely || (yy == data->sely && xx < data->selx)) {
1.1       nicm     1555:                sx = xx; sy = yy;
                   1556:                ex = data->selx; ey = data->sely;
                   1557:        } else {
                   1558:                sx = data->selx; sy = data->sely;
                   1559:                ex = xx; ey = yy;
                   1560:        }
                   1561:
                   1562:        /* Trim ex to end of line. */
1.111     nicm     1563:        ey_last = window_copy_find_length(wp, ey);
                   1564:        if (ex > ey_last)
                   1565:                ex = ey_last;
1.1       nicm     1566:
1.42      nicm     1567:        /*
                   1568:         * Deal with rectangle-copy if necessary; four situations: start of
                   1569:         * first line (firstsx), end of last line (lastex), start (restsx) and
                   1570:         * end (restex) of all other lines.
                   1571:         */
                   1572:        xx = screen_size_x(s);
1.69      nicm     1573:
                   1574:        /*
                   1575:         * Behave according to mode-keys. If it is emacs, copy like emacs,
                   1576:         * keeping the top-left-most character, and dropping the
                   1577:         * bottom-right-most, regardless of copy direction. If it is vi, also
                   1578:         * keep bottom-right-most character.
                   1579:         */
1.136     nicm     1580:        keys = options_get_number(wp->window->options, "mode-keys");
1.42      nicm     1581:        if (data->rectflag) {
                   1582:                /*
                   1583:                 * Need to ignore the column with the cursor in it, which for
                   1584:                 * rectangular copy means knowing which side the cursor is on.
                   1585:                 */
                   1586:                if (data->selx < data->cx) {
                   1587:                        /* Selection start is on the left. */
1.69      nicm     1588:                        if (keys == MODEKEY_EMACS) {
                   1589:                                lastex = data->cx;
                   1590:                                restex = data->cx;
                   1591:                        }
                   1592:                        else {
                   1593:                                lastex = data->cx + 1;
                   1594:                                restex = data->cx + 1;
                   1595:                        }
1.42      nicm     1596:                        firstsx = data->selx;
                   1597:                        restsx = data->selx;
                   1598:                } else {
                   1599:                        /* Cursor is on the left. */
                   1600:                        lastex = data->selx + 1;
                   1601:                        restex = data->selx + 1;
1.64      nicm     1602:                        firstsx = data->cx;
                   1603:                        restsx = data->cx;
1.42      nicm     1604:                }
                   1605:        } else {
1.69      nicm     1606:                if (keys == MODEKEY_EMACS)
                   1607:                        lastex = ex;
1.74      nicm     1608:                else
1.69      nicm     1609:                        lastex = ex + 1;
1.42      nicm     1610:                restex = xx;
                   1611:                firstsx = sx;
                   1612:                restsx = 0;
                   1613:        }
                   1614:
1.1       nicm     1615:        /* Copy the lines. */
1.110     nicm     1616:        for (i = sy; i <= ey; i++) {
                   1617:                window_copy_copy_line(wp, &buf, &off, i,
                   1618:                    (i == sy ? firstsx : restsx),
                   1619:                    (i == ey ? lastex : restex));
1.1       nicm     1620:        }
                   1621:
1.26      nicm     1622:        /* Don't bother if no data. */
                   1623:        if (off == 0) {
1.81      nicm     1624:                free(buf);
1.89      nicm     1625:                return (NULL);
1.26      nicm     1626:        }
1.111     nicm     1627:        if (keys == MODEKEY_EMACS || lastex <= ey_last)
                   1628:                off -= 1; /* remove final \n (unless at end in vi mode) */
                   1629:        *len = off;
1.89      nicm     1630:        return (buf);
                   1631: }
                   1632:
1.157     nicm     1633: static void
1.108     nicm     1634: window_copy_copy_buffer(struct window_pane *wp, const char *bufname, void *buf,
                   1635:     size_t len)
1.89      nicm     1636: {
1.91      nicm     1637:        struct screen_write_ctx ctx;
1.72      nicm     1638:
1.178     nicm     1639:        if (options_get_number(global_options, "set-clipboard") != 0) {
1.91      nicm     1640:                screen_write_start(&ctx, wp, NULL);
                   1641:                screen_write_setselection(&ctx, buf, len);
                   1642:                screen_write_stop(&ctx);
1.179     nicm     1643:                notify_pane("pane-set-clipboard", wp);
1.91      nicm     1644:        }
1.1       nicm     1645:
1.108     nicm     1646:        if (paste_set(buf, len, bufname, NULL) != 0)
1.102     nicm     1647:                free(buf);
1.89      nicm     1648: }
                   1649:
1.157     nicm     1650: static void
1.167     nicm     1651: window_copy_copy_pipe(struct window_pane *wp, struct session *s,
1.108     nicm     1652:     const char *bufname, const char *arg)
1.89      nicm     1653: {
1.167     nicm     1654:        void            *buf;
                   1655:        size_t           len;
                   1656:        struct job      *job;
                   1657:        char            *expanded;
1.89      nicm     1658:
                   1659:        buf = window_copy_get_selection(wp, &len);
                   1660:        if (buf == NULL)
                   1661:                return;
1.167     nicm     1662:        expanded = format_single(NULL, arg, NULL, s, NULL, wp);
1.89      nicm     1663:
1.171     nicm     1664:        job = job_run(expanded, s, NULL, NULL, NULL, NULL, NULL);
1.90      nicm     1665:        bufferevent_write(job->event, buf, len);
1.120     nicm     1666:
                   1667:        free(expanded);
1.108     nicm     1668:        window_copy_copy_buffer(wp, bufname, buf, len);
1.89      nicm     1669: }
                   1670:
1.157     nicm     1671: static void
1.108     nicm     1672: window_copy_copy_selection(struct window_pane *wp, const char *bufname)
1.89      nicm     1673: {
1.114     nicm     1674:        void    *buf;
                   1675:        size_t   len;
1.89      nicm     1676:
                   1677:        buf = window_copy_get_selection(wp, &len);
                   1678:        if (buf == NULL)
                   1679:                return;
                   1680:
1.108     nicm     1681:        window_copy_copy_buffer(wp, bufname, buf, len);
1.103     nicm     1682: }
                   1683:
1.157     nicm     1684: static void
1.108     nicm     1685: window_copy_append_selection(struct window_pane *wp, const char *bufname)
1.103     nicm     1686: {
1.108     nicm     1687:        char                            *buf;
                   1688:        struct paste_buffer             *pb;
1.132     nicm     1689:        const char                      *bufdata;
                   1690:        size_t                           len, bufsize;
1.108     nicm     1691:        struct screen_write_ctx          ctx;
1.103     nicm     1692:
                   1693:        buf = window_copy_get_selection(wp, &len);
                   1694:        if (buf == NULL)
                   1695:                return;
                   1696:
1.178     nicm     1697:        if (options_get_number(global_options, "set-clipboard") != 0) {
1.103     nicm     1698:                screen_write_start(&ctx, wp, NULL);
                   1699:                screen_write_setselection(&ctx, buf, len);
                   1700:                screen_write_stop(&ctx);
1.179     nicm     1701:                notify_pane("pane-set-clipboard", wp);
1.103     nicm     1702:        }
                   1703:
1.132     nicm     1704:        if (bufname == NULL || *bufname == '\0')
                   1705:                pb = paste_get_top(&bufname);
                   1706:        else
1.108     nicm     1707:                pb = paste_get_name(bufname);
1.103     nicm     1708:        if (pb != NULL) {
1.132     nicm     1709:                bufdata = paste_buffer_data(pb, &bufsize);
                   1710:                buf = xrealloc(buf, len + bufsize);
                   1711:                memmove(buf + bufsize, buf, len);
                   1712:                memcpy(buf, bufdata, bufsize);
                   1713:                len += bufsize;
1.103     nicm     1714:        }
1.108     nicm     1715:        if (paste_set(buf, len, bufname, NULL) != 0)
1.103     nicm     1716:                free(buf);
1.1       nicm     1717: }
                   1718:
1.157     nicm     1719: static void
1.140     nicm     1720: window_copy_copy_line(struct window_pane *wp, char **buf, size_t *off, u_int sy,
                   1721:     u_int sx, u_int ex)
1.1       nicm     1722: {
1.54      nicm     1723:        struct window_copy_mode_data    *data = wp->modedata;
                   1724:        struct grid                     *gd = data->backing->grid;
1.140     nicm     1725:        struct grid_cell                 gc;
1.54      nicm     1726:        struct grid_line                *gl;
1.86      nicm     1727:        struct utf8_data                 ud;
1.54      nicm     1728:        u_int                            i, xx, wrapped = 0;
1.115     nicm     1729:        const char                      *s;
1.1       nicm     1730:
                   1731:        if (sx > ex)
                   1732:                return;
                   1733:
1.16      nicm     1734:        /*
                   1735:         * Work out if the line was wrapped at the screen edge and all of it is
                   1736:         * on screen.
                   1737:         */
                   1738:        gl = &gd->linedata[sy];
1.35      nicm     1739:        if (gl->flags & GRID_LINE_WRAPPED && gl->cellsize <= gd->sx)
1.16      nicm     1740:                wrapped = 1;
                   1741:
                   1742:        /* If the line was wrapped, don't strip spaces (use the full length). */
                   1743:        if (wrapped)
                   1744:                xx = gl->cellsize;
                   1745:        else
                   1746:                xx = window_copy_find_length(wp, sy);
1.1       nicm     1747:        if (ex > xx)
                   1748:                ex = xx;
                   1749:        if (sx > xx)
                   1750:                sx = xx;
                   1751:
                   1752:        if (sx < ex) {
                   1753:                for (i = sx; i < ex; i++) {
1.140     nicm     1754:                        grid_get_cell(gd, i, sy, &gc);
                   1755:                        if (gc.flags & GRID_FLAG_PADDING)
1.1       nicm     1756:                                continue;
1.140     nicm     1757:                        utf8_copy(&ud, &gc.data);
                   1758:                        if (ud.size == 1 && (gc.attr & GRID_ATTR_CHARSET)) {
1.115     nicm     1759:                                s = tty_acs_get(NULL, ud.data[0]);
                   1760:                                if (s != NULL && strlen(s) <= sizeof ud.data) {
                   1761:                                        ud.size = strlen(s);
1.117     nicm     1762:                                        memcpy(ud.data, s, ud.size);
1.115     nicm     1763:                                }
                   1764:                        }
1.86      nicm     1765:
1.116     nicm     1766:                        *buf = xrealloc(*buf, (*off) + ud.size);
1.86      nicm     1767:                        memcpy(*buf + *off, ud.data, ud.size);
                   1768:                        *off += ud.size;
1.1       nicm     1769:                }
                   1770:        }
                   1771:
1.16      nicm     1772:        /* Only add a newline if the line wasn't wrapped. */
1.44      nicm     1773:        if (!wrapped || ex != xx) {
1.116     nicm     1774:                *buf = xrealloc(*buf, (*off) + 1);
1.16      nicm     1775:                (*buf)[(*off)++] = '\n';
                   1776:        }
1.1       nicm     1777: }
                   1778:
1.157     nicm     1779: static void
1.47      nicm     1780: window_copy_clear_selection(struct window_pane *wp)
                   1781: {
                   1782:        struct window_copy_mode_data   *data = wp->modedata;
                   1783:        u_int                           px, py;
                   1784:
                   1785:        screen_clear_selection(&data->screen);
                   1786:
1.161     nicm     1787:        data->cursordrag = CURSORDRAG_NONE;
                   1788:
1.54      nicm     1789:        py = screen_hsize(data->backing) + data->cy - data->oy;
1.47      nicm     1790:        px = window_copy_find_length(wp, py);
                   1791:        if (data->cx > px)
                   1792:                window_copy_update_cursor(wp, px, data->cy);
                   1793: }
                   1794:
1.157     nicm     1795: static int
1.41      nicm     1796: window_copy_in_set(struct window_pane *wp, u_int px, u_int py, const char *set)
1.1       nicm     1797: {
1.54      nicm     1798:        struct window_copy_mode_data    *data = wp->modedata;
1.140     nicm     1799:        struct grid_cell                 gc;
                   1800:        const struct utf8_data          *ud;
                   1801:
                   1802:        grid_get_cell(data->backing->grid, px, py, &gc);
1.1       nicm     1803:
1.140     nicm     1804:        ud = &gc.data;
                   1805:        if (ud->size != 1 || (gc.flags & GRID_FLAG_PADDING))
1.1       nicm     1806:                return (0);
1.140     nicm     1807:        if (*ud->data == 0x00 || *ud->data == 0x7f)
1.1       nicm     1808:                return (0);
1.140     nicm     1809:        return (strchr(set, *ud->data) != NULL);
1.1       nicm     1810: }
                   1811:
1.157     nicm     1812: static u_int
1.1       nicm     1813: window_copy_find_length(struct window_pane *wp, u_int py)
                   1814: {
1.54      nicm     1815:        struct window_copy_mode_data    *data = wp->modedata;
                   1816:        struct screen                   *s = data->backing;
1.140     nicm     1817:        struct grid_cell                 gc;
1.54      nicm     1818:        u_int                            px;
1.1       nicm     1819:
1.4       nicm     1820:        /*
                   1821:         * If the pane has been resized, its grid can contain old overlong
                   1822:         * lines. grid_peek_cell does not allow accessing cells beyond the
                   1823:         * width of the grid, and screen_write_copy treats them as spaces, so
                   1824:         * ignore them here too.
                   1825:         */
1.54      nicm     1826:        px = s->grid->linedata[py].cellsize;
                   1827:        if (px > screen_size_x(s))
                   1828:                px = screen_size_x(s);
1.1       nicm     1829:        while (px > 0) {
1.140     nicm     1830:                grid_get_cell(s->grid, px - 1, py, &gc);
                   1831:                if (gc.data.size != 1 || *gc.data.data != ' ')
1.1       nicm     1832:                        break;
                   1833:                px--;
                   1834:        }
                   1835:        return (px);
                   1836: }
                   1837:
1.157     nicm     1838: static void
1.5       nicm     1839: window_copy_cursor_start_of_line(struct window_pane *wp)
                   1840: {
                   1841:        struct window_copy_mode_data    *data = wp->modedata;
1.58      nicm     1842:        struct screen                   *back_s = data->backing;
1.118     nicm     1843:        struct screen                   *s = &data->screen;
1.58      nicm     1844:        struct grid                     *gd = back_s->grid;
                   1845:        u_int                            py;
                   1846:
1.118     nicm     1847:        if (data->cx == 0 && s->sel.lineflag == LINE_SEL_NONE) {
1.58      nicm     1848:                py = screen_hsize(back_s) + data->cy - data->oy;
1.118     nicm     1849:                while (py > 0 &&
                   1850:                    gd->linedata[py-1].flags & GRID_LINE_WRAPPED) {
1.58      nicm     1851:                        window_copy_cursor_up(wp, 0);
                   1852:                        py = screen_hsize(back_s) + data->cy - data->oy;
                   1853:                }
                   1854:        }
1.18      nicm     1855:        window_copy_update_cursor(wp, 0, data->cy);
1.96      nicm     1856:        if (window_copy_update_selection(wp, 1))
1.5       nicm     1857:                window_copy_redraw_lines(wp, data->cy, 1);
1.6       nicm     1858: }
                   1859:
1.157     nicm     1860: static void
1.6       nicm     1861: window_copy_cursor_back_to_indentation(struct window_pane *wp)
                   1862: {
                   1863:        struct window_copy_mode_data    *data = wp->modedata;
                   1864:        u_int                            px, py, xx;
1.140     nicm     1865:        struct grid_cell                 gc;
1.6       nicm     1866:
                   1867:        px = 0;
1.54      nicm     1868:        py = screen_hsize(data->backing) + data->cy - data->oy;
1.6       nicm     1869:        xx = window_copy_find_length(wp, py);
                   1870:
                   1871:        while (px < xx) {
1.140     nicm     1872:                grid_get_cell(data->backing->grid, px, py, &gc);
                   1873:                if (gc.data.size != 1 || *gc.data.data != ' ')
1.6       nicm     1874:                        break;
                   1875:                px++;
                   1876:        }
                   1877:
1.18      nicm     1878:        window_copy_update_cursor(wp, px, data->cy);
1.96      nicm     1879:        if (window_copy_update_selection(wp, 1))
1.18      nicm     1880:                window_copy_redraw_lines(wp, data->cy, 1);
1.5       nicm     1881: }
                   1882:
1.157     nicm     1883: static void
1.5       nicm     1884: window_copy_cursor_end_of_line(struct window_pane *wp)
                   1885: {
                   1886:        struct window_copy_mode_data    *data = wp->modedata;
1.54      nicm     1887:        struct screen                   *back_s = data->backing;
1.118     nicm     1888:        struct screen                   *s = &data->screen;
1.54      nicm     1889:        struct grid                     *gd = back_s->grid;
1.5       nicm     1890:        u_int                            px, py;
                   1891:
1.54      nicm     1892:        py = screen_hsize(back_s) + data->cy - data->oy;
1.5       nicm     1893:        px = window_copy_find_length(wp, py);
                   1894:
1.118     nicm     1895:        if (data->cx == px && s->sel.lineflag == LINE_SEL_NONE) {
1.49      nicm     1896:                if (data->screen.sel.flag && data->rectflag)
1.54      nicm     1897:                        px = screen_size_x(back_s);
1.49      nicm     1898:                if (gd->linedata[py].flags & GRID_LINE_WRAPPED) {
                   1899:                        while (py < gd->sy + gd->hsize &&
                   1900:                            gd->linedata[py].flags & GRID_LINE_WRAPPED) {
                   1901:                                window_copy_cursor_down(wp, 0);
1.54      nicm     1902:                                py = screen_hsize(back_s)
                   1903:                                     + data->cy - data->oy;
1.49      nicm     1904:                        }
                   1905:                        px = window_copy_find_length(wp, py);
                   1906:                }
                   1907:        }
1.18      nicm     1908:        window_copy_update_cursor(wp, px, data->cy);
1.49      nicm     1909:
1.96      nicm     1910:        if (window_copy_update_selection(wp, 1))
1.18      nicm     1911:                window_copy_redraw_lines(wp, data->cy, 1);
1.95      nicm     1912: }
                   1913:
1.157     nicm     1914: static void
1.95      nicm     1915: window_copy_other_end(struct window_pane *wp)
                   1916: {
                   1917:        struct window_copy_mode_data    *data = wp->modedata;
                   1918:        struct screen                   *s = &data->screen;
1.161     nicm     1919:        u_int                            selx, sely, cy, yy, hsize;
1.95      nicm     1920:
1.118     nicm     1921:        if (!s->sel.flag && s->sel.lineflag == LINE_SEL_NONE)
1.95      nicm     1922:                return;
                   1923:
1.118     nicm     1924:        if (s->sel.lineflag == LINE_SEL_LEFT_RIGHT)
                   1925:                s->sel.lineflag = LINE_SEL_RIGHT_LEFT;
                   1926:        else if (s->sel.lineflag == LINE_SEL_RIGHT_LEFT)
                   1927:                s->sel.lineflag = LINE_SEL_LEFT_RIGHT;
                   1928:
1.161     nicm     1929:        switch (data->cursordrag) {
                   1930:                case CURSORDRAG_NONE:
                   1931:                case CURSORDRAG_SEL:
                   1932:                        data->cursordrag = CURSORDRAG_ENDSEL;
                   1933:                        break;
                   1934:                case CURSORDRAG_ENDSEL:
                   1935:                        data->cursordrag = CURSORDRAG_SEL;
                   1936:                        break;
                   1937:        }
                   1938:
                   1939:        selx = data->endselx;
                   1940:        sely = data->endsely;
                   1941:        if (data->cursordrag == CURSORDRAG_SEL) {
                   1942:                selx = data->selx;
                   1943:                sely = data->sely;
                   1944:        }
                   1945:
1.95      nicm     1946:        cy = data->cy;
                   1947:        yy = screen_hsize(data->backing) + data->cy - data->oy;
                   1948:
                   1949:        data->cx = selx;
                   1950:
1.122     nicm     1951:        hsize = screen_hsize(data->backing);
1.161     nicm     1952:        if (sely < hsize - data->oy) { /* above */
1.122     nicm     1953:                data->oy = hsize - sely;
1.95      nicm     1954:                data->cy = 0;
1.161     nicm     1955:        } else if (sely > hsize - data->oy + screen_size_y(s)) { /* below */
1.122     nicm     1956:                data->oy = hsize - sely + screen_size_y(s) - 1;
1.95      nicm     1957:                data->cy = screen_size_y(s) - 1;
                   1958:        } else
                   1959:                data->cy = cy + sely - yy;
                   1960:
1.161     nicm     1961:        window_copy_update_selection(wp, 1);
1.95      nicm     1962:        window_copy_redraw_screen(wp);
1.5       nicm     1963: }
                   1964:
1.157     nicm     1965: static void
1.1       nicm     1966: window_copy_cursor_left(struct window_pane *wp)
                   1967: {
                   1968:        struct window_copy_mode_data    *data = wp->modedata;
1.168     nicm     1969:        u_int                            py, cx;
                   1970:        struct grid_cell                 gc;
1.1       nicm     1971:
1.145     nicm     1972:        py = screen_hsize(data->backing) + data->cy - data->oy;
1.168     nicm     1973:        cx = data->cx;
                   1974:        while (cx > 0) {
                   1975:                grid_get_cell(data->backing->grid, cx, py, &gc);
                   1976:                if (~gc.flags & GRID_FLAG_PADDING)
                   1977:                        break;
                   1978:                cx--;
                   1979:        }
                   1980:        if (cx == 0 && py > 0) {
1.28      nicm     1981:                window_copy_cursor_up(wp, 0);
1.18      nicm     1982:                window_copy_cursor_end_of_line(wp);
1.168     nicm     1983:        } else if (cx > 0) {
                   1984:                window_copy_update_cursor(wp, cx - 1, data->cy);
1.96      nicm     1985:                if (window_copy_update_selection(wp, 1))
1.1       nicm     1986:                        window_copy_redraw_lines(wp, data->cy, 1);
                   1987:        }
                   1988: }
                   1989:
1.157     nicm     1990: static void
1.1       nicm     1991: window_copy_cursor_right(struct window_pane *wp)
                   1992: {
                   1993:        struct window_copy_mode_data    *data = wp->modedata;
1.168     nicm     1994:        u_int                            px, py, yy, cx, cy;
                   1995:        struct grid_cell                 gc;
1.1       nicm     1996:
1.145     nicm     1997:        py = screen_hsize(data->backing) + data->cy - data->oy;
                   1998:        yy = screen_hsize(data->backing) + screen_size_y(data->backing) - 1;
1.47      nicm     1999:        if (data->screen.sel.flag && data->rectflag)
                   2000:                px = screen_size_x(&data->screen);
1.168     nicm     2001:        else
1.47      nicm     2002:                px = window_copy_find_length(wp, py);
1.1       nicm     2003:
1.145     nicm     2004:        if (data->cx >= px && py < yy) {
1.1       nicm     2005:                window_copy_cursor_start_of_line(wp);
1.28      nicm     2006:                window_copy_cursor_down(wp, 0);
1.145     nicm     2007:        } else if (data->cx < px) {
1.168     nicm     2008:                cx = data->cx + 1;
                   2009:                cy = screen_hsize(data->backing) + data->cy - data->oy;
                   2010:                while (cx < px) {
                   2011:                        grid_get_cell(data->backing->grid, cx, cy, &gc);
                   2012:                        if (~gc.flags & GRID_FLAG_PADDING)
                   2013:                                break;
                   2014:                        cx++;
                   2015:                }
                   2016:                window_copy_update_cursor(wp, cx, data->cy);
1.96      nicm     2017:                if (window_copy_update_selection(wp, 1))
1.1       nicm     2018:                        window_copy_redraw_lines(wp, data->cy, 1);
                   2019:        }
                   2020: }
                   2021:
1.157     nicm     2022: static void
1.28      nicm     2023: window_copy_cursor_up(struct window_pane *wp, int scroll_only)
1.1       nicm     2024: {
                   2025:        struct window_copy_mode_data    *data = wp->modedata;
1.36      nicm     2026:        struct screen                   *s = &data->screen;
1.1       nicm     2027:        u_int                            ox, oy, px, py;
                   2028:
1.54      nicm     2029:        oy = screen_hsize(data->backing) + data->cy - data->oy;
1.1       nicm     2030:        ox = window_copy_find_length(wp, oy);
1.77      nicm     2031:        if (data->cx != ox) {
1.25      nicm     2032:                data->lastcx = data->cx;
                   2033:                data->lastsx = ox;
                   2034:        }
1.1       nicm     2035:
1.118     nicm     2036:        if (s->sel.lineflag == LINE_SEL_LEFT_RIGHT && oy == data->sely)
                   2037:                window_copy_other_end(wp);
                   2038:
1.25      nicm     2039:        data->cx = data->lastcx;
1.28      nicm     2040:        if (scroll_only || data->cy == 0) {
1.1       nicm     2041:                window_copy_scroll_down(wp, 1);
1.36      nicm     2042:                if (scroll_only) {
                   2043:                        if (data->cy == screen_size_y(s) - 1)
                   2044:                                window_copy_redraw_lines(wp, data->cy, 1);
                   2045:                        else
                   2046:                                window_copy_redraw_lines(wp, data->cy, 2);
                   2047:                }
1.28      nicm     2048:        } else {
1.18      nicm     2049:                window_copy_update_cursor(wp, data->cx, data->cy - 1);
1.96      nicm     2050:                if (window_copy_update_selection(wp, 1)) {
1.36      nicm     2051:                        if (data->cy == screen_size_y(s) - 1)
                   2052:                                window_copy_redraw_lines(wp, data->cy, 1);
                   2053:                        else
                   2054:                                window_copy_redraw_lines(wp, data->cy, 2);
                   2055:                }
1.1       nicm     2056:        }
                   2057:
1.47      nicm     2058:        if (!data->screen.sel.flag || !data->rectflag) {
1.54      nicm     2059:                py = screen_hsize(data->backing) + data->cy - data->oy;
1.47      nicm     2060:                px = window_copy_find_length(wp, py);
1.49      nicm     2061:                if ((data->cx >= data->lastsx && data->cx != px) ||
                   2062:                    data->cx > px)
1.47      nicm     2063:                        window_copy_cursor_end_of_line(wp);
                   2064:        }
1.118     nicm     2065:
                   2066:        if (s->sel.lineflag == LINE_SEL_LEFT_RIGHT)
                   2067:                window_copy_cursor_end_of_line(wp);
                   2068:        else if (s->sel.lineflag == LINE_SEL_RIGHT_LEFT)
                   2069:                window_copy_cursor_start_of_line(wp);
1.1       nicm     2070: }
                   2071:
1.157     nicm     2072: static void
1.28      nicm     2073: window_copy_cursor_down(struct window_pane *wp, int scroll_only)
1.1       nicm     2074: {
                   2075:        struct window_copy_mode_data    *data = wp->modedata;
                   2076:        struct screen                   *s = &data->screen;
                   2077:        u_int                            ox, oy, px, py;
                   2078:
1.54      nicm     2079:        oy = screen_hsize(data->backing) + data->cy - data->oy;
1.1       nicm     2080:        ox = window_copy_find_length(wp, oy);
1.77      nicm     2081:        if (data->cx != ox) {
1.25      nicm     2082:                data->lastcx = data->cx;
                   2083:                data->lastsx = ox;
                   2084:        }
1.1       nicm     2085:
1.169     nicm     2086:        if (s->sel.lineflag == LINE_SEL_RIGHT_LEFT && oy == data->endsely)
1.118     nicm     2087:                window_copy_other_end(wp);
                   2088:
1.25      nicm     2089:        data->cx = data->lastcx;
1.28      nicm     2090:        if (scroll_only || data->cy == screen_size_y(s) - 1) {
1.1       nicm     2091:                window_copy_scroll_up(wp, 1);
1.31      nicm     2092:                if (scroll_only && data->cy > 0)
1.28      nicm     2093:                        window_copy_redraw_lines(wp, data->cy - 1, 2);
                   2094:        } else {
1.18      nicm     2095:                window_copy_update_cursor(wp, data->cx, data->cy + 1);
1.96      nicm     2096:                if (window_copy_update_selection(wp, 1))
1.1       nicm     2097:                        window_copy_redraw_lines(wp, data->cy - 1, 2);
                   2098:        }
                   2099:
1.47      nicm     2100:        if (!data->screen.sel.flag || !data->rectflag) {
1.54      nicm     2101:                py = screen_hsize(data->backing) + data->cy - data->oy;
1.47      nicm     2102:                px = window_copy_find_length(wp, py);
1.49      nicm     2103:                if ((data->cx >= data->lastsx && data->cx != px) ||
                   2104:                    data->cx > px)
1.47      nicm     2105:                        window_copy_cursor_end_of_line(wp);
1.52      nicm     2106:        }
1.118     nicm     2107:
                   2108:        if (s->sel.lineflag == LINE_SEL_LEFT_RIGHT)
                   2109:                window_copy_cursor_end_of_line(wp);
                   2110:        else if (s->sel.lineflag == LINE_SEL_RIGHT_LEFT)
                   2111:                window_copy_cursor_start_of_line(wp);
1.52      nicm     2112: }
                   2113:
1.157     nicm     2114: static void
1.52      nicm     2115: window_copy_cursor_jump(struct window_pane *wp)
                   2116: {
                   2117:        struct window_copy_mode_data    *data = wp->modedata;
1.54      nicm     2118:        struct screen                   *back_s = data->backing;
1.140     nicm     2119:        struct grid_cell                 gc;
1.67      nicm     2120:        u_int                            px, py, xx;
1.52      nicm     2121:
                   2122:        px = data->cx + 1;
1.54      nicm     2123:        py = screen_hsize(back_s) + data->cy - data->oy;
1.52      nicm     2124:        xx = window_copy_find_length(wp, py);
                   2125:
                   2126:        while (px < xx) {
1.140     nicm     2127:                grid_get_cell(back_s->grid, px, py, &gc);
                   2128:                if (!(gc.flags & GRID_FLAG_PADDING) &&
                   2129:                    gc.data.size == 1 && *gc.data.data == data->jumpchar) {
1.52      nicm     2130:                        window_copy_update_cursor(wp, px, data->cy);
1.96      nicm     2131:                        if (window_copy_update_selection(wp, 1))
1.52      nicm     2132:                                window_copy_redraw_lines(wp, data->cy, 1);
                   2133:                        return;
                   2134:                }
                   2135:                px++;
                   2136:        }
                   2137: }
                   2138:
1.157     nicm     2139: static void
1.52      nicm     2140: window_copy_cursor_jump_back(struct window_pane *wp)
                   2141: {
                   2142:        struct window_copy_mode_data    *data = wp->modedata;
1.54      nicm     2143:        struct screen                   *back_s = data->backing;
1.140     nicm     2144:        struct grid_cell                 gc;
1.67      nicm     2145:        u_int                            px, py;
1.52      nicm     2146:
                   2147:        px = data->cx;
1.54      nicm     2148:        py = screen_hsize(back_s) + data->cy - data->oy;
1.52      nicm     2149:
                   2150:        if (px > 0)
                   2151:                px--;
                   2152:
                   2153:        for (;;) {
1.140     nicm     2154:                grid_get_cell(back_s->grid, px, py, &gc);
                   2155:                if (!(gc.flags & GRID_FLAG_PADDING) &&
                   2156:                    gc.data.size == 1 && *gc.data.data == data->jumpchar) {
1.52      nicm     2157:                        window_copy_update_cursor(wp, px, data->cy);
1.96      nicm     2158:                        if (window_copy_update_selection(wp, 1))
1.76      nicm     2159:                                window_copy_redraw_lines(wp, data->cy, 1);
                   2160:                        return;
                   2161:                }
                   2162:                if (px == 0)
                   2163:                        break;
                   2164:                px--;
                   2165:        }
                   2166: }
                   2167:
1.157     nicm     2168: static void
1.127     nicm     2169: window_copy_cursor_jump_to(struct window_pane *wp, int jump_again)
1.76      nicm     2170: {
                   2171:        struct window_copy_mode_data    *data = wp->modedata;
                   2172:        struct screen                   *back_s = data->backing;
1.140     nicm     2173:        struct grid_cell                 gc;
1.76      nicm     2174:        u_int                            px, py, xx;
                   2175:
1.127     nicm     2176:        px = data->cx + 1 + jump_again;
1.76      nicm     2177:        py = screen_hsize(back_s) + data->cy - data->oy;
                   2178:        xx = window_copy_find_length(wp, py);
                   2179:
                   2180:        while (px < xx) {
1.140     nicm     2181:                grid_get_cell(back_s->grid, px, py, &gc);
                   2182:                if (!(gc.flags & GRID_FLAG_PADDING) &&
                   2183:                    gc.data.size == 1 && *gc.data.data == data->jumpchar) {
1.76      nicm     2184:                        window_copy_update_cursor(wp, px - 1, data->cy);
1.96      nicm     2185:                        if (window_copy_update_selection(wp, 1))
1.76      nicm     2186:                                window_copy_redraw_lines(wp, data->cy, 1);
                   2187:                        return;
                   2188:                }
                   2189:                px++;
                   2190:        }
                   2191: }
                   2192:
1.157     nicm     2193: static void
1.127     nicm     2194: window_copy_cursor_jump_to_back(struct window_pane *wp, int jump_again)
1.76      nicm     2195: {
                   2196:        struct window_copy_mode_data    *data = wp->modedata;
                   2197:        struct screen                   *back_s = data->backing;
1.140     nicm     2198:        struct grid_cell                 gc;
1.76      nicm     2199:        u_int                            px, py;
                   2200:
                   2201:        px = data->cx;
                   2202:        py = screen_hsize(back_s) + data->cy - data->oy;
                   2203:
                   2204:        if (px > 0)
1.127     nicm     2205:                px--;
                   2206:
                   2207:        if (jump_again && px > 0)
1.76      nicm     2208:                px--;
                   2209:
                   2210:        for (;;) {
1.140     nicm     2211:                grid_get_cell(back_s->grid, px, py, &gc);
                   2212:                if (!(gc.flags & GRID_FLAG_PADDING) &&
                   2213:                    gc.data.size == 1 && *gc.data.data == data->jumpchar) {
1.76      nicm     2214:                        window_copy_update_cursor(wp, px + 1, data->cy);
1.96      nicm     2215:                        if (window_copy_update_selection(wp, 1))
1.52      nicm     2216:                                window_copy_redraw_lines(wp, data->cy, 1);
                   2217:                        return;
                   2218:                }
                   2219:                if (px == 0)
                   2220:                        break;
                   2221:                px--;
1.47      nicm     2222:        }
1.1       nicm     2223: }
                   2224:
1.157     nicm     2225: static void
1.41      nicm     2226: window_copy_cursor_next_word(struct window_pane *wp, const char *separators)
1.40      nicm     2227: {
                   2228:        struct window_copy_mode_data    *data = wp->modedata;
1.54      nicm     2229:        struct screen                   *back_s = data->backing;
1.40      nicm     2230:        u_int                            px, py, xx, yy;
1.48      nicm     2231:        int                              expected = 0;
1.40      nicm     2232:
                   2233:        px = data->cx;
1.54      nicm     2234:        py = screen_hsize(back_s) + data->cy - data->oy;
1.40      nicm     2235:        xx = window_copy_find_length(wp, py);
1.54      nicm     2236:        yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
1.40      nicm     2237:
1.48      nicm     2238:        /*
                   2239:         * First skip past any nonword characters and then any word characters.
                   2240:         *
                   2241:         * expected is initially set to 0 for the former and then 1 for the
                   2242:         * latter.
                   2243:         */
                   2244:        do {
                   2245:                while (px > xx ||
                   2246:                    window_copy_in_set(wp, px, py, separators) == expected) {
                   2247:                        /* Move down if we're past the end of the line. */
                   2248:                        if (px > xx) {
                   2249:                                if (py == yy)
                   2250:                                        return;
                   2251:                                window_copy_cursor_down(wp, 0);
                   2252:                                px = 0;
                   2253:
1.54      nicm     2254:                                py = screen_hsize(back_s) + data->cy - data->oy;
1.48      nicm     2255:                                xx = window_copy_find_length(wp, py);
                   2256:                        } else
                   2257:                                px++;
                   2258:                }
                   2259:                expected = !expected;
                   2260:        } while (expected == 1);
1.40      nicm     2261:
                   2262:        window_copy_update_cursor(wp, px, data->cy);
1.96      nicm     2263:        if (window_copy_update_selection(wp, 1))
1.40      nicm     2264:                window_copy_redraw_lines(wp, data->cy, 1);
                   2265: }
                   2266:
1.157     nicm     2267: static void
1.106     nicm     2268: window_copy_cursor_next_word_end(struct window_pane *wp,
                   2269:     const char *separators)
1.1       nicm     2270: {
                   2271:        struct window_copy_mode_data    *data = wp->modedata;
1.136     nicm     2272:        struct options                  *oo = wp->window->options;
1.54      nicm     2273:        struct screen                   *back_s = data->backing;
1.39      nicm     2274:        u_int                            px, py, xx, yy;
1.94      nicm     2275:        int                              keys, expected = 1;
1.1       nicm     2276:
1.18      nicm     2277:        px = data->cx;
1.54      nicm     2278:        py = screen_hsize(back_s) + data->cy - data->oy;
1.1       nicm     2279:        xx = window_copy_find_length(wp, py);
1.54      nicm     2280:        yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
1.1       nicm     2281:
1.94      nicm     2282:        keys = options_get_number(oo, "mode-keys");
                   2283:        if (keys == MODEKEY_VI && !window_copy_in_set(wp, px, py, separators))
                   2284:                px++;
                   2285:
1.48      nicm     2286:        /*
                   2287:         * First skip past any word characters, then any nonword characters.
                   2288:         *
                   2289:         * expected is initially set to 1 for the former and then 0 for the
                   2290:         * latter.
                   2291:         */
                   2292:        do {
                   2293:                while (px > xx ||
                   2294:                    window_copy_in_set(wp, px, py, separators) == expected) {
                   2295:                        /* Move down if we're past the end of the line. */
                   2296:                        if (px > xx) {
                   2297:                                if (py == yy)
                   2298:                                        return;
                   2299:                                window_copy_cursor_down(wp, 0);
                   2300:                                px = 0;
                   2301:
1.54      nicm     2302:                                py = screen_hsize(back_s) + data->cy - data->oy;
1.48      nicm     2303:                                xx = window_copy_find_length(wp, py);
                   2304:                        } else
                   2305:                                px++;
                   2306:                }
                   2307:                expected = !expected;
                   2308:        } while (expected == 0);
1.92      nicm     2309:
1.94      nicm     2310:        if (keys == MODEKEY_VI && px != 0)
1.92      nicm     2311:                px--;
1.18      nicm     2312:
                   2313:        window_copy_update_cursor(wp, px, data->cy);
1.96      nicm     2314:        if (window_copy_update_selection(wp, 1))
1.18      nicm     2315:                window_copy_redraw_lines(wp, data->cy, 1);
1.1       nicm     2316: }
                   2317:
1.8       nicm     2318: /* Move to the previous place where a word begins. */
1.157     nicm     2319: static void
1.106     nicm     2320: window_copy_cursor_previous_word(struct window_pane *wp,
                   2321:     const char *separators)
1.1       nicm     2322: {
                   2323:        struct window_copy_mode_data    *data = wp->modedata;
1.8       nicm     2324:        u_int                            px, py;
1.1       nicm     2325:
1.18      nicm     2326:        px = data->cx;
1.54      nicm     2327:        py = screen_hsize(data->backing) + data->cy - data->oy;
1.1       nicm     2328:
1.8       nicm     2329:        /* Move back to the previous word character. */
1.1       nicm     2330:        for (;;) {
1.8       nicm     2331:                if (px > 0) {
                   2332:                        px--;
1.41      nicm     2333:                        if (!window_copy_in_set(wp, px, py, separators))
1.1       nicm     2334:                                break;
1.8       nicm     2335:                } else {
                   2336:                        if (data->cy == 0 &&
1.54      nicm     2337:                            (screen_hsize(data->backing) == 0 ||
                   2338:                            data->oy >= screen_hsize(data->backing) - 1))
1.8       nicm     2339:                                goto out;
1.28      nicm     2340:                        window_copy_cursor_up(wp, 0);
1.1       nicm     2341:
1.54      nicm     2342:                        py = screen_hsize(data->backing) + data->cy - data->oy;
1.8       nicm     2343:                        px = window_copy_find_length(wp, py);
1.1       nicm     2344:                }
1.8       nicm     2345:        }
1.1       nicm     2346:
1.8       nicm     2347:        /* Move back to the beginning of this word. */
1.41      nicm     2348:        while (px > 0 && !window_copy_in_set(wp, px - 1, py, separators))
1.1       nicm     2349:                px--;
1.8       nicm     2350:
1.1       nicm     2351: out:
1.18      nicm     2352:        window_copy_update_cursor(wp, px, data->cy);
1.96      nicm     2353:        if (window_copy_update_selection(wp, 1))
1.18      nicm     2354:                window_copy_redraw_lines(wp, data->cy, 1);
1.1       nicm     2355: }
                   2356:
1.157     nicm     2357: static void
1.1       nicm     2358: window_copy_scroll_up(struct window_pane *wp, u_int ny)
                   2359: {
                   2360:        struct window_copy_mode_data    *data = wp->modedata;
                   2361:        struct screen                   *s = &data->screen;
                   2362:        struct screen_write_ctx          ctx;
                   2363:
                   2364:        if (data->oy < ny)
                   2365:                ny = data->oy;
                   2366:        if (ny == 0)
                   2367:                return;
                   2368:        data->oy -= ny;
                   2369:
1.96      nicm     2370:        window_copy_update_selection(wp, 0);
                   2371:
1.1       nicm     2372:        screen_write_start(&ctx, wp, NULL);
                   2373:        screen_write_cursormove(&ctx, 0, 0);
1.159     nicm     2374:        screen_write_deleteline(&ctx, ny, 8);
1.1       nicm     2375:        window_copy_write_lines(wp, &ctx, screen_size_y(s) - ny, ny);
                   2376:        window_copy_write_line(wp, &ctx, 0);
1.31      nicm     2377:        if (screen_size_y(s) > 1)
                   2378:                window_copy_write_line(wp, &ctx, 1);
                   2379:        if (screen_size_y(s) > 3)
                   2380:                window_copy_write_line(wp, &ctx, screen_size_y(s) - 2);
1.96      nicm     2381:        if (s->sel.flag && screen_size_y(s) > ny)
1.1       nicm     2382:                window_copy_write_line(wp, &ctx, screen_size_y(s) - ny - 1);
                   2383:        screen_write_cursormove(&ctx, data->cx, data->cy);
                   2384:        screen_write_stop(&ctx);
                   2385: }
                   2386:
1.157     nicm     2387: static void
1.1       nicm     2388: window_copy_scroll_down(struct window_pane *wp, u_int ny)
                   2389: {
                   2390:        struct window_copy_mode_data    *data = wp->modedata;
                   2391:        struct screen                   *s = &data->screen;
                   2392:        struct screen_write_ctx          ctx;
                   2393:
1.54      nicm     2394:        if (ny > screen_hsize(data->backing))
1.1       nicm     2395:                return;
                   2396:
1.54      nicm     2397:        if (data->oy > screen_hsize(data->backing) - ny)
                   2398:                ny = screen_hsize(data->backing) - data->oy;
1.1       nicm     2399:        if (ny == 0)
                   2400:                return;
                   2401:        data->oy += ny;
                   2402:
1.96      nicm     2403:        window_copy_update_selection(wp, 0);
                   2404:
1.1       nicm     2405:        screen_write_start(&ctx, wp, NULL);
                   2406:        screen_write_cursormove(&ctx, 0, 0);
1.159     nicm     2407:        screen_write_insertline(&ctx, ny, 8);
1.1       nicm     2408:        window_copy_write_lines(wp, &ctx, 0, ny);
1.96      nicm     2409:        if (s->sel.flag && screen_size_y(s) > ny)
1.1       nicm     2410:                window_copy_write_line(wp, &ctx, ny);
1.96      nicm     2411:        else if (ny == 1) /* nuke position */
1.1       nicm     2412:                window_copy_write_line(wp, &ctx, 1);
                   2413:        screen_write_cursormove(&ctx, data->cx, data->cy);
                   2414:        screen_write_stop(&ctx);
1.135     nicm     2415: }
                   2416:
1.180     nicm     2417: void
                   2418: window_copy_add_formats(struct window_pane *wp, struct format_tree *ft)
1.135     nicm     2419: {
                   2420:        struct window_copy_mode_data    *data = wp->modedata;
1.180     nicm     2421:        struct screen                   *s = &data->screen;
1.135     nicm     2422:
                   2423:        if (wp->mode != &window_copy_mode)
1.180     nicm     2424:                return;
                   2425:
                   2426:        format_add(ft, "selection_present", "%d", s->sel.flag);
                   2427:        format_add(ft, "scroll_position", "%d", data->oy);
1.1       nicm     2428: }
1.42      nicm     2429:
1.157     nicm     2430: static void
1.42      nicm     2431: window_copy_rectangle_toggle(struct window_pane *wp)
                   2432: {
                   2433:        struct window_copy_mode_data    *data = wp->modedata;
1.47      nicm     2434:        u_int                            px, py;
1.42      nicm     2435:
                   2436:        data->rectflag = !data->rectflag;
1.47      nicm     2437:
1.54      nicm     2438:        py = screen_hsize(data->backing) + data->cy - data->oy;
1.47      nicm     2439:        px = window_copy_find_length(wp, py);
                   2440:        if (data->cx > px)
                   2441:                window_copy_update_cursor(wp, px, data->cy);
1.42      nicm     2442:
1.96      nicm     2443:        window_copy_update_selection(wp, 1);
1.42      nicm     2444:        window_copy_redraw_screen(wp);
1.156     nicm     2445: }
                   2446:
1.157     nicm     2447: static void
1.156     nicm     2448: window_copy_move_mouse(struct mouse_event *m)
                   2449: {
                   2450:        struct window_pane      *wp;
                   2451:        u_int                    x, y;
                   2452:
                   2453:        wp = cmd_mouse_pane(m, NULL, NULL);
                   2454:        if (wp == NULL || wp->mode != &window_copy_mode)
                   2455:                return;
                   2456:
1.183   ! nicm     2457:        if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
1.156     nicm     2458:                return;
                   2459:
                   2460:        window_copy_update_cursor(wp, x, y);
1.126     nicm     2461: }
                   2462:
                   2463: void
1.141     nicm     2464: window_copy_start_drag(struct client *c, struct mouse_event *m)
1.126     nicm     2465: {
1.128     nicm     2466:        struct window_pane      *wp;
                   2467:        u_int                    x, y;
1.126     nicm     2468:
1.155     nicm     2469:        if (c == NULL)
                   2470:                return;
                   2471:
1.126     nicm     2472:        wp = cmd_mouse_pane(m, NULL, NULL);
1.129     nicm     2473:        if (wp == NULL || wp->mode != &window_copy_mode)
1.126     nicm     2474:                return;
                   2475:
                   2476:        if (cmd_mouse_at(wp, m, &x, &y, 1) != 0)
                   2477:                return;
                   2478:
                   2479:        c->tty.mouse_drag_update = window_copy_drag_update;
1.155     nicm     2480:        c->tty.mouse_drag_release = NULL; /* will fire MouseDragEnd key */
1.126     nicm     2481:
                   2482:        window_copy_update_cursor(wp, x, y);
                   2483:        window_copy_start_selection(wp);
                   2484:        window_copy_redraw_screen(wp);
                   2485: }
                   2486:
1.157     nicm     2487: static void
1.141     nicm     2488: window_copy_drag_update(__unused struct client *c, struct mouse_event *m)
1.126     nicm     2489: {
                   2490:        struct window_pane              *wp;
                   2491:        struct window_copy_mode_data    *data;
                   2492:        u_int                            x, y, old_cy;
                   2493:
                   2494:        wp = cmd_mouse_pane(m, NULL, NULL);
1.129     nicm     2495:        if (wp == NULL || wp->mode != &window_copy_mode)
1.126     nicm     2496:                return;
                   2497:        data = wp->modedata;
                   2498:
                   2499:        if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
                   2500:                return;
                   2501:        old_cy = data->cy;
                   2502:
                   2503:        window_copy_update_cursor(wp, x, y);
                   2504:        if (window_copy_update_selection(wp, 1))
                   2505:                window_copy_redraw_selection(wp, old_cy);
1.42      nicm     2506: }