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

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