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