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