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

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