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