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