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