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

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