Annotation of src/usr.bin/tmux/window-copy.c, Revision 1.203
1.203 ! nicm 1: /* $OpenBSD: window-copy.c,v 1.202 2018/10/03 15:27:55 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.203 ! nicm 72: static void window_copy_copy_buffer(struct window_pane *, void *, size_t);
1.157 nicm 73: static void window_copy_copy_pipe(struct window_pane *, struct session *,
74: const char *);
1.203 ! nicm 75: static void window_copy_copy_selection(struct window_pane *);
! 76: static void window_copy_append_selection(struct window_pane *);
1.157 nicm 77: static void window_copy_clear_selection(struct window_pane *);
78: static void window_copy_copy_line(struct window_pane *, char **, size_t *,
79: u_int, u_int, u_int);
80: static int window_copy_in_set(struct window_pane *, u_int, u_int,
81: const char *);
82: static u_int window_copy_find_length(struct window_pane *, u_int);
83: static void window_copy_cursor_start_of_line(struct window_pane *);
84: static void window_copy_cursor_back_to_indentation(struct window_pane *);
85: static void window_copy_cursor_end_of_line(struct window_pane *);
86: static void window_copy_other_end(struct window_pane *);
87: static void window_copy_cursor_left(struct window_pane *);
88: static void window_copy_cursor_right(struct window_pane *);
89: static void window_copy_cursor_up(struct window_pane *, int);
90: static void window_copy_cursor_down(struct window_pane *, int);
91: static void window_copy_cursor_jump(struct window_pane *);
92: static void window_copy_cursor_jump_back(struct window_pane *);
1.184 nicm 93: static void window_copy_cursor_jump_to(struct window_pane *);
94: static void window_copy_cursor_jump_to_back(struct window_pane *);
1.157 nicm 95: static void window_copy_cursor_next_word(struct window_pane *,
96: const char *);
97: static void window_copy_cursor_next_word_end(struct window_pane *,
98: const char *);
99: static void window_copy_cursor_previous_word(struct window_pane *,
100: const char *);
101: static void window_copy_scroll_up(struct window_pane *, u_int);
102: static void window_copy_scroll_down(struct window_pane *, u_int);
103: static void window_copy_rectangle_toggle(struct window_pane *);
104: static void window_copy_move_mouse(struct mouse_event *);
105: static void window_copy_drag_update(struct client *, struct mouse_event *);
1.1 nicm 106:
107: const struct window_mode window_copy_mode = {
1.173 nicm 108: .name = "copy-mode",
109:
1.155 nicm 110: .init = window_copy_init,
111: .free = window_copy_free,
112: .resize = window_copy_resize,
113: .key_table = window_copy_key_table,
114: .command = window_copy_command,
1.1 nicm 115: };
116:
1.155 nicm 117: enum {
1.21 nicm 118: WINDOW_COPY_OFF,
119: WINDOW_COPY_SEARCHUP,
120: WINDOW_COPY_SEARCHDOWN,
1.52 nicm 121: WINDOW_COPY_JUMPFORWARD,
1.155 nicm 122: WINDOW_COPY_JUMPBACKWARD,
1.76 nicm 123: WINDOW_COPY_JUMPTOFORWARD,
1.155 nicm 124: WINDOW_COPY_JUMPTOBACKWARD,
1.21 nicm 125: };
126:
1.161 nicm 127: enum {
128: WINDOW_COPY_REL_POS_ABOVE,
129: WINDOW_COPY_REL_POS_ON_SCREEN,
130: WINDOW_COPY_REL_POS_BELOW,
131: };
132:
1.54 nicm 133: /*
1.161 nicm 134: * Copy mode's visible screen (the "screen" field) is filled from one of two
135: * sources: the original contents of the pane (used when we actually enter via
136: * the "copy-mode" command, to copy the contents of the current pane), or else
137: * a series of lines containing the output from an output-writing tmux command
138: * (such as any of the "show-*" or "list-*" commands).
1.54 nicm 139: *
1.161 nicm 140: * In either case, the full content of the copy-mode grid is pointed at by the
141: * "backing" field, and is copied into "screen" as needed (that is, when
142: * scrolling occurs). When copy-mode is backed by a pane, backing points
143: * directly at that pane's screen structure (&wp->base); when backed by a list
144: * of output-lines from a command, it points at a newly-allocated screen
145: * structure (which is deallocated when the mode ends).
1.54 nicm 146: */
1.1 nicm 147: struct window_copy_mode_data {
1.155 nicm 148: struct screen screen;
1.1 nicm 149:
1.155 nicm 150: struct screen *backing;
151: int backing_written; /* backing display started */
1.54 nicm 152:
1.192 nicm 153: u_int oy; /* number of lines scrolled up */
1.21 nicm 154:
1.192 nicm 155: u_int selx; /* beginning of selection */
1.155 nicm 156: u_int sely;
1.21 nicm 157:
1.192 nicm 158: u_int endselx; /* end of selection */
1.161 nicm 159: u_int endsely;
160:
161: enum {
162: CURSORDRAG_NONE, /* selection is independent of cursor */
163: CURSORDRAG_ENDSEL, /* end is synchronized with cursor */
164: CURSORDRAG_SEL, /* start is synchronized with cursor */
165: } cursordrag;
166:
1.192 nicm 167: int modekeys;
168: enum {
169: LINE_SEL_NONE,
170: LINE_SEL_LEFT_RIGHT,
171: LINE_SEL_RIGHT_LEFT,
172: } lineflag; /* line selection mode */
1.155 nicm 173: int rectflag; /* in rectangle copy mode? */
174: int scroll_exit; /* exit on scroll to end? */
1.1 nicm 175:
1.155 nicm 176: u_int cx;
177: u_int cy;
1.42 nicm 178:
1.192 nicm 179: u_int lastcx; /* position in last line w/ content */
180: u_int lastsx; /* size of last line w/ content */
1.1 nicm 181:
1.155 nicm 182: int searchtype;
183: char *searchstr;
1.162 nicm 184: bitstr_t *searchmark;
1.170 nicm 185: u_int searchcount;
186: int searchthis;
1.163 nicm 187: int searchx;
188: int searchy;
189: int searcho;
1.25 nicm 190:
1.155 nicm 191: int jumptype;
192: char jumpchar;
1.1 nicm 193: };
194:
1.157 nicm 195: static struct screen *
1.177 nicm 196: window_copy_init(struct window_pane *wp, __unused struct cmd_find_state *fs,
197: __unused struct args *args)
1.1 nicm 198: {
199: struct window_copy_mode_data *data;
200: struct screen *s;
201:
1.194 nicm 202: wp->modedata = data = xcalloc(1, sizeof *data);
1.1 nicm 203:
1.161 nicm 204: data->cursordrag = CURSORDRAG_NONE;
1.193 nicm 205: data->lineflag = LINE_SEL_NONE;
1.42 nicm 206:
1.174 nicm 207: if (wp->searchstr != NULL) {
208: data->searchtype = WINDOW_COPY_SEARCHUP;
209: data->searchstr = xstrdup(wp->searchstr);
210: } else {
211: data->searchtype = WINDOW_COPY_OFF;
212: data->searchstr = NULL;
213: }
1.162 nicm 214: data->searchmark = NULL;
1.163 nicm 215: data->searchx = data->searchy = data->searcho = -1;
1.21 nicm 216:
1.61 nicm 217: if (wp->fd != -1)
218: bufferevent_disable(wp->event, EV_READ|EV_WRITE);
1.46 nicm 219:
1.52 nicm 220: data->jumptype = WINDOW_COPY_OFF;
221: data->jumpchar = '\0';
222:
1.1 nicm 223: s = &data->screen;
224: screen_init(s, screen_size_x(&wp->base), screen_size_y(&wp->base), 0);
1.192 nicm 225: data->modekeys = options_get_number(wp->window->options, "mode-keys");
1.1 nicm 226:
1.54 nicm 227: data->backing = NULL;
228:
229: return (s);
230: }
231:
232: void
1.134 nicm 233: window_copy_init_from_pane(struct window_pane *wp, int scroll_exit)
1.54 nicm 234: {
235: struct window_copy_mode_data *data = wp->modedata;
236: struct screen *s = &data->screen;
237: struct screen_write_ctx ctx;
238: u_int i;
239:
240: if (wp->mode != &window_copy_mode)
241: fatalx("not in copy mode");
242:
243: data->backing = &wp->base;
244: data->cx = data->backing->cx;
245: data->cy = data->backing->cy;
1.133 nicm 246: data->scroll_exit = scroll_exit;
1.54 nicm 247:
1.1 nicm 248: s->cx = data->cx;
249: s->cy = data->cy;
250:
251: screen_write_start(&ctx, NULL, s);
252: for (i = 0; i < screen_size_y(s); i++)
253: window_copy_write_line(wp, &ctx, i);
254: screen_write_cursormove(&ctx, data->cx, data->cy);
255: screen_write_stop(&ctx);
1.54 nicm 256: }
1.1 nicm 257:
1.54 nicm 258: void
259: window_copy_init_for_output(struct window_pane *wp)
260: {
261: struct window_copy_mode_data *data = wp->modedata;
262:
263: data->backing = xmalloc(sizeof *data->backing);
264: screen_init(data->backing, screen_size_x(&wp->base),
265: screen_size_y(&wp->base), UINT_MAX);
1.1 nicm 266: }
267:
1.157 nicm 268: static void
1.1 nicm 269: window_copy_free(struct window_pane *wp)
270: {
271: struct window_copy_mode_data *data = wp->modedata;
1.46 nicm 272:
1.61 nicm 273: if (wp->fd != -1)
274: bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1.1 nicm 275:
1.162 nicm 276: free(data->searchmark);
1.81 nicm 277: free(data->searchstr);
1.21 nicm 278:
1.54 nicm 279: if (data->backing != &wp->base) {
280: screen_free(data->backing);
1.81 nicm 281: free(data->backing);
1.54 nicm 282: }
1.1 nicm 283: screen_free(&data->screen);
1.21 nicm 284:
1.81 nicm 285: free(data);
1.1 nicm 286: }
287:
288: void
1.54 nicm 289: window_copy_add(struct window_pane *wp, const char *fmt, ...)
290: {
291: va_list ap;
292:
293: va_start(ap, fmt);
294: window_copy_vadd(wp, fmt, ap);
295: va_end(ap);
296: }
297:
298: void
299: window_copy_vadd(struct window_pane *wp, const char *fmt, va_list ap)
300: {
301: struct window_copy_mode_data *data = wp->modedata;
302: struct screen *backing = data->backing;
303: struct screen_write_ctx back_ctx, ctx;
304: struct grid_cell gc;
1.119 nicm 305: u_int old_hsize, old_cy;
1.54 nicm 306:
307: if (backing == &wp->base)
308: return;
309:
310: memcpy(&gc, &grid_default_cell, sizeof gc);
311:
312: old_hsize = screen_hsize(data->backing);
313: screen_write_start(&back_ctx, NULL, backing);
314: if (data->backing_written) {
315: /*
316: * On the second or later line, do a CRLF before writing
317: * (so it's on a new line).
318: */
319: screen_write_carriagereturn(&back_ctx);
1.175 nicm 320: screen_write_linefeed(&back_ctx, 0, 8);
1.54 nicm 321: } else
322: data->backing_written = 1;
1.119 nicm 323: old_cy = backing->cy;
1.139 nicm 324: screen_write_vnputs(&back_ctx, 0, &gc, fmt, ap);
1.54 nicm 325: screen_write_stop(&back_ctx);
326:
327: data->oy += screen_hsize(data->backing) - old_hsize;
328:
329: screen_write_start(&ctx, wp, &data->screen);
330:
331: /*
332: * If the history has changed, draw the top line.
333: * (If there's any history at all, it has changed.)
334: */
335: if (screen_hsize(data->backing))
336: window_copy_redraw_lines(wp, 0, 1);
337:
1.119 nicm 338: /* Write the new lines. */
339: window_copy_redraw_lines(wp, old_cy, backing->cy - old_cy + 1);
1.54 nicm 340:
341: screen_write_stop(&ctx);
342: }
343:
344: void
1.149 nicm 345: window_copy_pageup(struct window_pane *wp, int half_page)
1.1 nicm 346: {
347: struct window_copy_mode_data *data = wp->modedata;
348: struct screen *s = &data->screen;
1.162 nicm 349: u_int n, ox, oy, px, py;
1.147 nicm 350:
351: oy = screen_hsize(data->backing) + data->cy - data->oy;
352: ox = window_copy_find_length(wp, oy);
353:
354: if (data->cx != ox) {
355: data->lastcx = data->cx;
356: data->lastsx = ox;
357: }
358: data->cx = data->lastcx;
1.1 nicm 359:
1.19 nicm 360: n = 1;
1.149 nicm 361: if (screen_size_y(s) > 2) {
362: if (half_page)
363: n = screen_size_y(s) / 2;
364: else
365: n = screen_size_y(s) - 2;
366: }
1.147 nicm 367:
1.195 nicm 368: if (data->oy + n > screen_hsize(data->backing)) {
1.54 nicm 369: data->oy = screen_hsize(data->backing);
1.195 nicm 370: if (data->cy < n)
371: data->cy = 0;
372: else
373: data->cy -= n;
374: } else
1.19 nicm 375: data->oy += n;
1.147 nicm 376:
1.192 nicm 377: if (data->screen.sel == NULL || !data->rectflag) {
1.162 nicm 378: py = screen_hsize(data->backing) + data->cy - data->oy;
379: px = window_copy_find_length(wp, py);
380: if ((data->cx >= data->lastsx && data->cx != px) ||
381: data->cx > px)
1.147 nicm 382: window_copy_cursor_end_of_line(wp);
383: }
384:
385: window_copy_update_selection(wp, 1);
386: window_copy_redraw_screen(wp);
387: }
388:
1.166 nicm 389: static int
1.186 nicm 390: window_copy_pagedown(struct window_pane *wp, int half_page, int scroll_exit)
1.147 nicm 391: {
392: struct window_copy_mode_data *data = wp->modedata;
393: struct screen *s = &data->screen;
1.161 nicm 394: u_int n, ox, oy, px, py;
1.147 nicm 395:
396: oy = screen_hsize(data->backing) + data->cy - data->oy;
397: ox = window_copy_find_length(wp, oy);
398:
399: if (data->cx != ox) {
400: data->lastcx = data->cx;
401: data->lastsx = ox;
402: }
403: data->cx = data->lastcx;
404:
405: n = 1;
1.149 nicm 406: if (screen_size_y(s) > 2) {
407: if (half_page)
408: n = screen_size_y(s) / 2;
409: else
410: n = screen_size_y(s) - 2;
411: }
1.147 nicm 412:
1.195 nicm 413: if (data->oy < n) {
1.147 nicm 414: data->oy = 0;
1.195 nicm 415: if (data->cy + (n - data->oy) >= screen_size_y(data->backing))
416: data->cy = screen_size_y(data->backing) - 1;
417: else
418: data->cy += n - data->oy;
419: } else
1.147 nicm 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.202 nicm 530: if (m != NULL && m->valid && !MOUSE_WHEEL(m->b))
1.156 nicm 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)
1.203 ! nicm 536: window_copy_append_selection(wp);
1.155 nicm 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)
1.203 ! nicm 542: window_copy_append_selection(wp);
1.155 nicm 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) {
1.203 ! nicm 580: window_copy_copy_selection(wp);
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) {
1.203 ! nicm 593: window_copy_copy_selection(wp);
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)
1.203 ! nicm 599: window_copy_copy_selection(wp);
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)
1.203 ! nicm 605: window_copy_copy_selection(wp);
1.155 nicm 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)
1.203 ! nicm 829: window_copy_copy_pipe(wp, s, argument);
1.160 nicm 830: }
831: if (strcmp(command, "copy-pipe-and-cancel") == 0) {
1.155 nicm 832: if (s != NULL) {
1.203 ! nicm 833: window_copy_copy_pipe(wp, s, 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:
1174: wrapflag = options_get_number(wp->window->options, "wrap-search");
1.162 nicm 1175: cis = window_copy_is_lowercase(data->searchstr);
1.21 nicm 1176:
1.150 nicm 1177: if (direction)
1178: endline = gd->hsize + gd->sy - 1;
1179: else
1180: endline = 0;
1.163 nicm 1181: found = window_copy_search_jump(wp, gd, ss.grid, fx, fy, endline, cis,
1.162 nicm 1182: wrapflag, direction);
1183:
1184: if (window_copy_search_marks(wp, &ss))
1185: window_copy_redraw_screen(wp);
1.21 nicm 1186:
1.150 nicm 1187: screen_free(&ss);
1.163 nicm 1188: return (found);
1.150 nicm 1189: }
1.97 nicm 1190:
1.162 nicm 1191: static int
1192: window_copy_search_marks(struct window_pane *wp, struct screen *ssp)
1193: {
1194: struct window_copy_mode_data *data = wp->modedata;
1195: struct screen *s = data->backing, ss;
1196: struct screen_write_ctx ctx;
1197: struct grid *gd = s->grid;
1.170 nicm 1198: int found, cis, which = -1;
1.162 nicm 1199: u_int px, py, b, nfound = 0, width;
1200:
1201: if (ssp == NULL) {
1202: width = screen_write_strlen("%s", data->searchstr);
1203: screen_init(&ss, width, 1, 0);
1204: screen_write_start(&ctx, NULL, &ss);
1205: screen_write_nputs(&ctx, -1, &grid_default_cell, "%s",
1206: data->searchstr);
1207: screen_write_stop(&ctx);
1208: ssp = &ss;
1209: } else
1210: width = screen_size_x(ssp);
1211:
1212: cis = window_copy_is_lowercase(data->searchstr);
1213:
1214: free(data->searchmark);
1215: data->searchmark = bit_alloc((gd->hsize + gd->sy) * gd->sx);
1216:
1217: for (py = 0; py < gd->hsize + gd->sy; py++) {
1218: px = 0;
1219: for (;;) {
1220: found = window_copy_search_lr(gd, ssp->grid, &px, py,
1221: px, gd->sx, cis);
1222: if (!found)
1223: break;
1.170 nicm 1224:
1.162 nicm 1225: nfound++;
1.170 nicm 1226: if (px == data->cx && py == gd->hsize + data->cy - data->oy)
1227: which = nfound;
1.162 nicm 1228:
1229: b = (py * gd->sx) + px;
1230: bit_nset(data->searchmark, b, b + width - 1);
1231:
1232: px++;
1233: }
1234: }
1235:
1.170 nicm 1236: if (which != -1)
1237: data->searchthis = 1 + nfound - which;
1238: else
1239: data->searchthis = -1;
1240: data->searchcount = nfound;
1241:
1.162 nicm 1242: if (ssp == &ss)
1243: screen_free(&ss);
1244: return (nfound);
1245: }
1246:
1.157 nicm 1247: static void
1.163 nicm 1248: window_copy_clear_marks(struct window_pane *wp)
1249: {
1250: struct window_copy_mode_data *data = wp->modedata;
1251:
1252: free(data->searchmark);
1253: data->searchmark = NULL;
1254: }
1255:
1256: static int
1.184 nicm 1257: window_copy_search_up(struct window_pane *wp)
1.150 nicm 1258: {
1.184 nicm 1259: return (window_copy_search(wp, 0));
1.150 nicm 1260: }
1.21 nicm 1261:
1.163 nicm 1262: static int
1.184 nicm 1263: window_copy_search_down(struct window_pane *wp)
1.150 nicm 1264: {
1.184 nicm 1265: return (window_copy_search(wp, 1));
1.21 nicm 1266: }
1267:
1.157 nicm 1268: static void
1.21 nicm 1269: window_copy_goto_line(struct window_pane *wp, const char *linestr)
1270: {
1271: struct window_copy_mode_data *data = wp->modedata;
1272: const char *errstr;
1.199 nicm 1273: int lineno;
1.21 nicm 1274:
1.199 nicm 1275: lineno = strtonum(linestr, -1, INT_MAX, &errstr);
1.21 nicm 1276: if (errstr != NULL)
1277: return;
1.199 nicm 1278: if (lineno < 0 || (u_int)lineno > screen_hsize(data->backing))
1279: lineno = screen_hsize(data->backing);
1.35 nicm 1280:
1.21 nicm 1281: data->oy = lineno;
1.96 nicm 1282: window_copy_update_selection(wp, 1);
1.21 nicm 1283: window_copy_redraw_screen(wp);
1284: }
1285:
1.157 nicm 1286: static void
1.118 nicm 1287: window_copy_write_line(struct window_pane *wp, struct screen_write_ctx *ctx,
1288: u_int py)
1.1 nicm 1289: {
1290: struct window_copy_mode_data *data = wp->modedata;
1291: struct screen *s = &data->screen;
1.136 nicm 1292: struct options *oo = wp->window->options;
1.1 nicm 1293: struct grid_cell gc;
1.100 nicm 1294: char hdr[512];
1.162 nicm 1295: size_t size = 0;
1.21 nicm 1296:
1.101 nicm 1297: style_apply(&gc, oo, "mode-style");
1.164 nicm 1298: gc.flags |= GRID_FLAG_NOPALETTE;
1.1 nicm 1299:
1.201 nicm 1300: if (py == 0 && s->rupper < s->rlower) {
1.170 nicm 1301: if (data->searchmark == NULL) {
1302: size = xsnprintf(hdr, sizeof hdr,
1303: "[%u/%u]", data->oy, screen_hsize(data->backing));
1304: } else {
1305: if (data->searchthis == -1) {
1306: size = xsnprintf(hdr, sizeof hdr,
1307: "(%u results) [%d/%u]", data->searchcount,
1308: data->oy, screen_hsize(data->backing));
1309: } else {
1310: size = xsnprintf(hdr, sizeof hdr,
1311: "(%u/%u results) [%d/%u]", data->searchthis,
1312: data->searchcount, data->oy,
1313: screen_hsize(data->backing));
1314: }
1315: }
1.62 nicm 1316: if (size > screen_size_x(s))
1317: size = screen_size_x(s);
1.1 nicm 1318: screen_write_cursormove(ctx, screen_size_x(s) - size, 0);
1319: screen_write_puts(ctx, &gc, "%s", hdr);
1320: } else
1321: size = 0;
1322:
1.105 nicm 1323: if (size < screen_size_x(s)) {
1.162 nicm 1324: screen_write_cursormove(ctx, 0, py);
1325: screen_write_copy(ctx, data->backing, 0,
1.105 nicm 1326: (screen_hsize(data->backing) - data->oy) + py,
1.162 nicm 1327: screen_size_x(s) - size, 1, data->searchmark, &gc);
1.105 nicm 1328: }
1.18 nicm 1329:
1330: if (py == data->cy && data->cx == screen_size_x(s)) {
1331: memcpy(&gc, &grid_default_cell, sizeof gc);
1332: screen_write_cursormove(ctx, screen_size_x(s) - 1, py);
1333: screen_write_putc(ctx, &gc, '$');
1334: }
1.1 nicm 1335: }
1336:
1.157 nicm 1337: static void
1.118 nicm 1338: window_copy_write_lines(struct window_pane *wp, struct screen_write_ctx *ctx,
1339: u_int py, u_int ny)
1.1 nicm 1340: {
1341: u_int yy;
1342:
1343: for (yy = py; yy < py + ny; yy++)
1344: window_copy_write_line(wp, ctx, py);
1.121 nicm 1345: }
1346:
1.157 nicm 1347: static void
1.121 nicm 1348: window_copy_redraw_selection(struct window_pane *wp, u_int old_y)
1349: {
1350: struct window_copy_mode_data *data = wp->modedata;
1351: u_int new_y, start, end;
1352:
1353: new_y = data->cy;
1354: if (old_y <= new_y) {
1355: start = old_y;
1356: end = new_y;
1357: } else {
1358: start = new_y;
1359: end = old_y;
1360: }
1361: window_copy_redraw_lines(wp, start, end - start + 1);
1.1 nicm 1362: }
1363:
1.157 nicm 1364: static void
1.1 nicm 1365: window_copy_redraw_lines(struct window_pane *wp, u_int py, u_int ny)
1366: {
1367: struct window_copy_mode_data *data = wp->modedata;
1368: struct screen_write_ctx ctx;
1369: u_int i;
1370:
1371: screen_write_start(&ctx, wp, NULL);
1372: for (i = py; i < py + ny; i++)
1373: window_copy_write_line(wp, &ctx, i);
1374: screen_write_cursormove(&ctx, data->cx, data->cy);
1375: screen_write_stop(&ctx);
1376: }
1377:
1.157 nicm 1378: static void
1.1 nicm 1379: window_copy_redraw_screen(struct window_pane *wp)
1380: {
1381: struct window_copy_mode_data *data = wp->modedata;
1382:
1383: window_copy_redraw_lines(wp, 0, screen_size_y(&data->screen));
1384: }
1385:
1.157 nicm 1386: static void
1.161 nicm 1387: window_copy_synchronize_cursor(struct window_pane *wp)
1388: {
1389: struct window_copy_mode_data *data = wp->modedata;
1390: u_int xx, yy;
1391:
1392: xx = data->cx;
1393: yy = screen_hsize(data->backing) + data->cy - data->oy;
1394:
1395: switch (data->cursordrag) {
1396: case CURSORDRAG_ENDSEL:
1397: data->endselx = xx;
1398: data->endsely = yy;
1399: break;
1400: case CURSORDRAG_SEL:
1401: data->selx = xx;
1402: data->sely = yy;
1403: break;
1404: case CURSORDRAG_NONE:
1405: break;
1406: }
1407: }
1408:
1409: static void
1.18 nicm 1410: window_copy_update_cursor(struct window_pane *wp, u_int cx, u_int cy)
1.1 nicm 1411: {
1412: struct window_copy_mode_data *data = wp->modedata;
1.18 nicm 1413: struct screen *s = &data->screen;
1.1 nicm 1414: struct screen_write_ctx ctx;
1.18 nicm 1415: u_int old_cx, old_cy;
1.1 nicm 1416:
1.18 nicm 1417: old_cx = data->cx; old_cy = data->cy;
1418: data->cx = cx; data->cy = cy;
1419: if (old_cx == screen_size_x(s))
1420: window_copy_redraw_lines(wp, old_cy, 1);
1421: if (data->cx == screen_size_x(s))
1422: window_copy_redraw_lines(wp, data->cy, 1);
1423: else {
1424: screen_write_start(&ctx, wp, NULL);
1425: screen_write_cursormove(&ctx, data->cx, data->cy);
1426: screen_write_stop(&ctx);
1427: }
1.1 nicm 1428: }
1429:
1.157 nicm 1430: static void
1.1 nicm 1431: window_copy_start_selection(struct window_pane *wp)
1432: {
1433: struct window_copy_mode_data *data = wp->modedata;
1434:
1.18 nicm 1435: data->selx = data->cx;
1.54 nicm 1436: data->sely = screen_hsize(data->backing) + data->cy - data->oy;
1.1 nicm 1437:
1.161 nicm 1438: data->endselx = data->selx;
1439: data->endsely = data->sely;
1440:
1441: data->cursordrag = CURSORDRAG_ENDSEL;
1442:
1.192 nicm 1443: window_copy_set_selection(wp, 1);
1.1 nicm 1444: }
1445:
1.157 nicm 1446: static int
1.161 nicm 1447: window_copy_adjust_selection(struct window_pane *wp, u_int *selx, u_int *sely)
1448: {
1449: struct window_copy_mode_data *data = wp->modedata;
1450: struct screen *s = &data->screen;
1451: u_int sx, sy, ty;
1452: int relpos;
1453:
1454: sx = *selx;
1455: sy = *sely;
1456:
1457: ty = screen_hsize(data->backing) - data->oy;
1458: if (sy < ty) {
1459: relpos = WINDOW_COPY_REL_POS_ABOVE;
1460: if (!data->rectflag)
1461: sx = 0;
1462: sy = 0;
1463: } else if (sy > ty + screen_size_y(s) - 1) {
1464: relpos = WINDOW_COPY_REL_POS_BELOW;
1465: if (!data->rectflag)
1466: sx = screen_size_x(s) - 1;
1467: sy = screen_size_y(s) - 1;
1468: } else {
1469: relpos = WINDOW_COPY_REL_POS_ON_SCREEN;
1470: sy -= ty;
1471: }
1472:
1473: *selx = sx;
1.176 nicm 1474: *sely = sy;
1.161 nicm 1475: return (relpos);
1476: }
1477:
1478: static int
1.96 nicm 1479: window_copy_update_selection(struct window_pane *wp, int may_redraw)
1.1 nicm 1480: {
1481: struct window_copy_mode_data *data = wp->modedata;
1482: struct screen *s = &data->screen;
1.192 nicm 1483:
1484: if (s->sel == NULL && data->lineflag == LINE_SEL_NONE)
1485: return (0);
1486: return (window_copy_set_selection(wp, may_redraw));
1487: }
1488:
1489: static int
1490: window_copy_set_selection(struct window_pane *wp, int may_redraw)
1491: {
1492: struct window_copy_mode_data *data = wp->modedata;
1493: struct screen *s = &data->screen;
1.136 nicm 1494: struct options *oo = wp->window->options;
1.1 nicm 1495: struct grid_cell gc;
1.161 nicm 1496: u_int sx, sy, cy, endsx, endsy;
1497: int startrelpos, endrelpos;
1.1 nicm 1498:
1.161 nicm 1499: window_copy_synchronize_cursor(wp);
1.1 nicm 1500:
1501: /* Adjust the selection. */
1502: sx = data->selx;
1503: sy = data->sely;
1.161 nicm 1504: startrelpos = window_copy_adjust_selection(wp, &sx, &sy);
1505:
1506: /* Adjust the end of selection. */
1507: endsx = data->endselx;
1508: endsy = data->endsely;
1509: endrelpos = window_copy_adjust_selection(wp, &endsx, &endsy);
1510:
1511: /* Selection is outside of the current screen */
1512: if (startrelpos == endrelpos &&
1513: startrelpos != WINDOW_COPY_REL_POS_ON_SCREEN) {
1514: screen_hide_selection(s);
1515: return (0);
1516: }
1.1 nicm 1517:
1.161 nicm 1518: /* Set colours and selection. */
1519: style_apply(&gc, oo, "mode-style");
1.164 nicm 1520: gc.flags |= GRID_FLAG_NOPALETTE;
1.192 nicm 1521: screen_set_selection(s, sx, sy, endsx, endsy, data->rectflag,
1522: data->modekeys, &gc);
1.42 nicm 1523:
1.96 nicm 1524: if (data->rectflag && may_redraw) {
1.42 nicm 1525: /*
1526: * Can't rely on the caller to redraw the right lines for
1527: * rectangle selection - find the highest line and the number
1528: * of lines, and redraw just past that in both directions
1529: */
1530: cy = data->cy;
1.182 nicm 1531: if (data->cursordrag == CURSORDRAG_ENDSEL) {
1532: if (sy < cy)
1533: window_copy_redraw_lines(wp, sy, cy - sy + 1);
1534: else
1535: window_copy_redraw_lines(wp, cy, sy - cy + 1);
1536: } else {
1537: if (endsy < cy)
1538: window_copy_redraw_lines(wp, endsy, cy - endsy + 1);
1539: else
1540: window_copy_redraw_lines(wp, cy, endsy - cy + 1);
1541: }
1.42 nicm 1542: }
1543:
1.1 nicm 1544: return (1);
1545: }
1546:
1.157 nicm 1547: static void *
1.89 nicm 1548: window_copy_get_selection(struct window_pane *wp, size_t *len)
1.1 nicm 1549: {
1550: struct window_copy_mode_data *data = wp->modedata;
1551: struct screen *s = &data->screen;
1552: char *buf;
1.42 nicm 1553: size_t off;
1.111 nicm 1554: u_int i, xx, yy, sx, sy, ex, ey, ey_last;
1.188 nicm 1555: u_int firstsx, lastex, restex, restsx, selx;
1.69 nicm 1556: int keys;
1.1 nicm 1557:
1.192 nicm 1558: if (data->screen.sel == NULL && data->lineflag == LINE_SEL_NONE)
1.89 nicm 1559: return (NULL);
1.1 nicm 1560:
1561: buf = xmalloc(1);
1562: off = 0;
1563:
1564: *buf = '\0';
1565:
1566: /*
1567: * The selection extends from selx,sely to (adjusted) cx,cy on
1568: * the base screen.
1569: */
1570:
1571: /* Find start and end. */
1.161 nicm 1572: xx = data->endselx;
1573: yy = data->endsely;
1.2 nicm 1574: if (yy < data->sely || (yy == data->sely && xx < data->selx)) {
1.1 nicm 1575: sx = xx; sy = yy;
1576: ex = data->selx; ey = data->sely;
1577: } else {
1578: sx = data->selx; sy = data->sely;
1579: ex = xx; ey = yy;
1580: }
1581:
1582: /* Trim ex to end of line. */
1.111 nicm 1583: ey_last = window_copy_find_length(wp, ey);
1584: if (ex > ey_last)
1585: ex = ey_last;
1.1 nicm 1586:
1.42 nicm 1587: /*
1588: * Deal with rectangle-copy if necessary; four situations: start of
1589: * first line (firstsx), end of last line (lastex), start (restsx) and
1590: * end (restex) of all other lines.
1591: */
1592: xx = screen_size_x(s);
1.69 nicm 1593:
1594: /*
1595: * Behave according to mode-keys. If it is emacs, copy like emacs,
1596: * keeping the top-left-most character, and dropping the
1597: * bottom-right-most, regardless of copy direction. If it is vi, also
1598: * keep bottom-right-most character.
1599: */
1.136 nicm 1600: keys = options_get_number(wp->window->options, "mode-keys");
1.42 nicm 1601: if (data->rectflag) {
1602: /*
1603: * Need to ignore the column with the cursor in it, which for
1604: * rectangular copy means knowing which side the cursor is on.
1605: */
1.188 nicm 1606: if (data->cursordrag == CURSORDRAG_ENDSEL)
1607: selx = data->selx;
1608: else
1609: selx = data->endselx;
1610: if (selx < data->cx) {
1.42 nicm 1611: /* Selection start is on the left. */
1.69 nicm 1612: if (keys == MODEKEY_EMACS) {
1613: lastex = data->cx;
1614: restex = data->cx;
1615: }
1616: else {
1617: lastex = data->cx + 1;
1618: restex = data->cx + 1;
1619: }
1.188 nicm 1620: firstsx = selx;
1621: restsx = selx;
1.42 nicm 1622: } else {
1623: /* Cursor is on the left. */
1.188 nicm 1624: lastex = selx + 1;
1625: restex = selx + 1;
1.64 nicm 1626: firstsx = data->cx;
1627: restsx = data->cx;
1.42 nicm 1628: }
1629: } else {
1.69 nicm 1630: if (keys == MODEKEY_EMACS)
1631: lastex = ex;
1.74 nicm 1632: else
1.69 nicm 1633: lastex = ex + 1;
1.42 nicm 1634: restex = xx;
1635: firstsx = sx;
1636: restsx = 0;
1637: }
1638:
1.1 nicm 1639: /* Copy the lines. */
1.110 nicm 1640: for (i = sy; i <= ey; i++) {
1641: window_copy_copy_line(wp, &buf, &off, i,
1642: (i == sy ? firstsx : restsx),
1643: (i == ey ? lastex : restex));
1.1 nicm 1644: }
1645:
1.26 nicm 1646: /* Don't bother if no data. */
1647: if (off == 0) {
1.81 nicm 1648: free(buf);
1.89 nicm 1649: return (NULL);
1.26 nicm 1650: }
1.111 nicm 1651: if (keys == MODEKEY_EMACS || lastex <= ey_last)
1652: off -= 1; /* remove final \n (unless at end in vi mode) */
1653: *len = off;
1.89 nicm 1654: return (buf);
1655: }
1656:
1.157 nicm 1657: static void
1.203 ! nicm 1658: window_copy_copy_buffer(struct window_pane *wp, void *buf, 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.203 ! nicm 1669: if (paste_set(buf, len, NULL, 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.203 ! nicm 1675: const char *fmt)
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.203 ! nicm 1685: expanded = format_single(NULL, fmt, 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.196 nicm 1688: bufferevent_write(job_get_event(job), buf, len);
1.120 nicm 1689:
1690: free(expanded);
1.203 ! nicm 1691: window_copy_copy_buffer(wp, buf, len);
1.89 nicm 1692: }
1693:
1.157 nicm 1694: static void
1.203 ! nicm 1695: window_copy_copy_selection(struct window_pane *wp)
1.89 nicm 1696: {
1.203 ! nicm 1697: char *buf;
1.114 nicm 1698: size_t len;
1.89 nicm 1699:
1700: buf = window_copy_get_selection(wp, &len);
1.203 ! nicm 1701: if (buf != NULL)
! 1702: window_copy_copy_buffer(wp, buf, len);
1.103 nicm 1703: }
1704:
1.157 nicm 1705: static void
1.203 ! nicm 1706: window_copy_append_selection(struct window_pane *wp)
1.103 nicm 1707: {
1.108 nicm 1708: char *buf;
1709: struct paste_buffer *pb;
1.203 ! nicm 1710: const char *bufdata, *bufname;
1.132 nicm 1711: size_t len, bufsize;
1.108 nicm 1712: struct screen_write_ctx ctx;
1.103 nicm 1713:
1714: buf = window_copy_get_selection(wp, &len);
1715: if (buf == NULL)
1716: return;
1717:
1.178 nicm 1718: if (options_get_number(global_options, "set-clipboard") != 0) {
1.103 nicm 1719: screen_write_start(&ctx, wp, NULL);
1720: screen_write_setselection(&ctx, buf, len);
1721: screen_write_stop(&ctx);
1.179 nicm 1722: notify_pane("pane-set-clipboard", wp);
1.103 nicm 1723: }
1724:
1.203 ! nicm 1725: pb = paste_get_top(&bufname);
1.103 nicm 1726: if (pb != NULL) {
1.132 nicm 1727: bufdata = paste_buffer_data(pb, &bufsize);
1728: buf = xrealloc(buf, len + bufsize);
1729: memmove(buf + bufsize, buf, len);
1730: memcpy(buf, bufdata, bufsize);
1731: len += bufsize;
1.103 nicm 1732: }
1.108 nicm 1733: if (paste_set(buf, len, bufname, NULL) != 0)
1.103 nicm 1734: free(buf);
1.1 nicm 1735: }
1736:
1.157 nicm 1737: static void
1.140 nicm 1738: window_copy_copy_line(struct window_pane *wp, char **buf, size_t *off, u_int sy,
1739: u_int sx, u_int ex)
1.1 nicm 1740: {
1.54 nicm 1741: struct window_copy_mode_data *data = wp->modedata;
1742: struct grid *gd = data->backing->grid;
1.140 nicm 1743: struct grid_cell gc;
1.54 nicm 1744: struct grid_line *gl;
1.86 nicm 1745: struct utf8_data ud;
1.54 nicm 1746: u_int i, xx, wrapped = 0;
1.115 nicm 1747: const char *s;
1.1 nicm 1748:
1749: if (sx > ex)
1750: return;
1751:
1.16 nicm 1752: /*
1753: * Work out if the line was wrapped at the screen edge and all of it is
1754: * on screen.
1755: */
1.190 nicm 1756: gl = grid_get_line(gd, sy);
1.35 nicm 1757: if (gl->flags & GRID_LINE_WRAPPED && gl->cellsize <= gd->sx)
1.16 nicm 1758: wrapped = 1;
1759:
1760: /* If the line was wrapped, don't strip spaces (use the full length). */
1761: if (wrapped)
1762: xx = gl->cellsize;
1763: else
1764: xx = window_copy_find_length(wp, sy);
1.1 nicm 1765: if (ex > xx)
1766: ex = xx;
1767: if (sx > xx)
1768: sx = xx;
1769:
1770: if (sx < ex) {
1771: for (i = sx; i < ex; i++) {
1.140 nicm 1772: grid_get_cell(gd, i, sy, &gc);
1773: if (gc.flags & GRID_FLAG_PADDING)
1.1 nicm 1774: continue;
1.140 nicm 1775: utf8_copy(&ud, &gc.data);
1776: if (ud.size == 1 && (gc.attr & GRID_ATTR_CHARSET)) {
1.115 nicm 1777: s = tty_acs_get(NULL, ud.data[0]);
1778: if (s != NULL && strlen(s) <= sizeof ud.data) {
1779: ud.size = strlen(s);
1.117 nicm 1780: memcpy(ud.data, s, ud.size);
1.115 nicm 1781: }
1782: }
1.86 nicm 1783:
1.116 nicm 1784: *buf = xrealloc(*buf, (*off) + ud.size);
1.86 nicm 1785: memcpy(*buf + *off, ud.data, ud.size);
1786: *off += ud.size;
1.1 nicm 1787: }
1788: }
1789:
1.16 nicm 1790: /* Only add a newline if the line wasn't wrapped. */
1.44 nicm 1791: if (!wrapped || ex != xx) {
1.116 nicm 1792: *buf = xrealloc(*buf, (*off) + 1);
1.16 nicm 1793: (*buf)[(*off)++] = '\n';
1794: }
1.1 nicm 1795: }
1796:
1.157 nicm 1797: static void
1.47 nicm 1798: window_copy_clear_selection(struct window_pane *wp)
1799: {
1800: struct window_copy_mode_data *data = wp->modedata;
1801: u_int px, py;
1802:
1803: screen_clear_selection(&data->screen);
1804:
1.161 nicm 1805: data->cursordrag = CURSORDRAG_NONE;
1.197 nicm 1806: data->lineflag = LINE_SEL_NONE;
1.161 nicm 1807:
1.54 nicm 1808: py = screen_hsize(data->backing) + data->cy - data->oy;
1.47 nicm 1809: px = window_copy_find_length(wp, py);
1810: if (data->cx > px)
1811: window_copy_update_cursor(wp, px, data->cy);
1812: }
1813:
1.157 nicm 1814: static int
1.41 nicm 1815: window_copy_in_set(struct window_pane *wp, u_int px, u_int py, const char *set)
1.1 nicm 1816: {
1.54 nicm 1817: struct window_copy_mode_data *data = wp->modedata;
1.140 nicm 1818: struct grid_cell gc;
1819: const struct utf8_data *ud;
1820:
1821: grid_get_cell(data->backing->grid, px, py, &gc);
1.1 nicm 1822:
1.140 nicm 1823: ud = &gc.data;
1824: if (ud->size != 1 || (gc.flags & GRID_FLAG_PADDING))
1.1 nicm 1825: return (0);
1.140 nicm 1826: if (*ud->data == 0x00 || *ud->data == 0x7f)
1.1 nicm 1827: return (0);
1.140 nicm 1828: return (strchr(set, *ud->data) != NULL);
1.1 nicm 1829: }
1830:
1.157 nicm 1831: static u_int
1.1 nicm 1832: window_copy_find_length(struct window_pane *wp, u_int py)
1833: {
1.54 nicm 1834: struct window_copy_mode_data *data = wp->modedata;
1835: struct screen *s = data->backing;
1.140 nicm 1836: struct grid_cell gc;
1.54 nicm 1837: u_int px;
1.1 nicm 1838:
1.4 nicm 1839: /*
1840: * If the pane has been resized, its grid can contain old overlong
1841: * lines. grid_peek_cell does not allow accessing cells beyond the
1842: * width of the grid, and screen_write_copy treats them as spaces, so
1843: * ignore them here too.
1844: */
1.190 nicm 1845: px = grid_get_line(s->grid, py)->cellsize;
1.54 nicm 1846: if (px > screen_size_x(s))
1847: px = screen_size_x(s);
1.1 nicm 1848: while (px > 0) {
1.140 nicm 1849: grid_get_cell(s->grid, px - 1, py, &gc);
1850: if (gc.data.size != 1 || *gc.data.data != ' ')
1.1 nicm 1851: break;
1852: px--;
1853: }
1854: return (px);
1855: }
1856:
1.157 nicm 1857: static void
1.5 nicm 1858: window_copy_cursor_start_of_line(struct window_pane *wp)
1859: {
1860: struct window_copy_mode_data *data = wp->modedata;
1.58 nicm 1861: struct screen *back_s = data->backing;
1862: struct grid *gd = back_s->grid;
1863: u_int py;
1864:
1.192 nicm 1865: if (data->cx == 0 && data->lineflag == LINE_SEL_NONE) {
1.58 nicm 1866: py = screen_hsize(back_s) + data->cy - data->oy;
1.118 nicm 1867: while (py > 0 &&
1.190 nicm 1868: grid_get_line(gd, py - 1)->flags & GRID_LINE_WRAPPED) {
1.58 nicm 1869: window_copy_cursor_up(wp, 0);
1870: py = screen_hsize(back_s) + data->cy - data->oy;
1871: }
1872: }
1.18 nicm 1873: window_copy_update_cursor(wp, 0, data->cy);
1.96 nicm 1874: if (window_copy_update_selection(wp, 1))
1.5 nicm 1875: window_copy_redraw_lines(wp, data->cy, 1);
1.6 nicm 1876: }
1877:
1.157 nicm 1878: static void
1.6 nicm 1879: window_copy_cursor_back_to_indentation(struct window_pane *wp)
1880: {
1881: struct window_copy_mode_data *data = wp->modedata;
1882: u_int px, py, xx;
1.140 nicm 1883: struct grid_cell gc;
1.6 nicm 1884:
1885: px = 0;
1.54 nicm 1886: py = screen_hsize(data->backing) + data->cy - data->oy;
1.6 nicm 1887: xx = window_copy_find_length(wp, py);
1888:
1889: while (px < xx) {
1.140 nicm 1890: grid_get_cell(data->backing->grid, px, py, &gc);
1891: if (gc.data.size != 1 || *gc.data.data != ' ')
1.6 nicm 1892: break;
1893: px++;
1894: }
1895:
1.18 nicm 1896: window_copy_update_cursor(wp, px, data->cy);
1.96 nicm 1897: if (window_copy_update_selection(wp, 1))
1.18 nicm 1898: window_copy_redraw_lines(wp, data->cy, 1);
1.5 nicm 1899: }
1900:
1.157 nicm 1901: static void
1.5 nicm 1902: window_copy_cursor_end_of_line(struct window_pane *wp)
1903: {
1904: struct window_copy_mode_data *data = wp->modedata;
1.54 nicm 1905: struct screen *back_s = data->backing;
1906: struct grid *gd = back_s->grid;
1.190 nicm 1907: struct grid_line *gl;
1.5 nicm 1908: u_int px, py;
1909:
1.54 nicm 1910: py = screen_hsize(back_s) + data->cy - data->oy;
1.5 nicm 1911: px = window_copy_find_length(wp, py);
1912:
1.192 nicm 1913: if (data->cx == px && data->lineflag == LINE_SEL_NONE) {
1914: if (data->screen.sel != NULL && data->rectflag)
1.54 nicm 1915: px = screen_size_x(back_s);
1.190 nicm 1916: gl = grid_get_line(gd, py);
1917: if (gl->flags & GRID_LINE_WRAPPED) {
1918: while (py < gd->sy + gd->hsize) {
1919: gl = grid_get_line(gd, py);
1920: if (~gl->flags & GRID_LINE_WRAPPED)
1921: break;
1.49 nicm 1922: window_copy_cursor_down(wp, 0);
1.190 nicm 1923: py = screen_hsize(back_s) + data->cy - data->oy;
1.49 nicm 1924: }
1925: px = window_copy_find_length(wp, py);
1926: }
1927: }
1.18 nicm 1928: window_copy_update_cursor(wp, px, data->cy);
1.49 nicm 1929:
1.96 nicm 1930: if (window_copy_update_selection(wp, 1))
1.18 nicm 1931: window_copy_redraw_lines(wp, data->cy, 1);
1.95 nicm 1932: }
1933:
1.157 nicm 1934: static void
1.95 nicm 1935: window_copy_other_end(struct window_pane *wp)
1936: {
1937: struct window_copy_mode_data *data = wp->modedata;
1938: struct screen *s = &data->screen;
1.161 nicm 1939: u_int selx, sely, cy, yy, hsize;
1.95 nicm 1940:
1.192 nicm 1941: if (s->sel == NULL && data->lineflag == LINE_SEL_NONE)
1.95 nicm 1942: return;
1943:
1.192 nicm 1944: if (data->lineflag == LINE_SEL_LEFT_RIGHT)
1945: data->lineflag = LINE_SEL_RIGHT_LEFT;
1946: else if (data->lineflag == LINE_SEL_RIGHT_LEFT)
1947: data->lineflag = LINE_SEL_LEFT_RIGHT;
1.118 nicm 1948:
1.161 nicm 1949: switch (data->cursordrag) {
1950: case CURSORDRAG_NONE:
1951: case CURSORDRAG_SEL:
1952: data->cursordrag = CURSORDRAG_ENDSEL;
1953: break;
1954: case CURSORDRAG_ENDSEL:
1955: data->cursordrag = CURSORDRAG_SEL;
1956: break;
1957: }
1958:
1959: selx = data->endselx;
1960: sely = data->endsely;
1961: if (data->cursordrag == CURSORDRAG_SEL) {
1962: selx = data->selx;
1963: sely = data->sely;
1964: }
1965:
1.95 nicm 1966: cy = data->cy;
1967: yy = screen_hsize(data->backing) + data->cy - data->oy;
1968:
1969: data->cx = selx;
1970:
1.122 nicm 1971: hsize = screen_hsize(data->backing);
1.161 nicm 1972: if (sely < hsize - data->oy) { /* above */
1.122 nicm 1973: data->oy = hsize - sely;
1.95 nicm 1974: data->cy = 0;
1.161 nicm 1975: } else if (sely > hsize - data->oy + screen_size_y(s)) { /* below */
1.122 nicm 1976: data->oy = hsize - sely + screen_size_y(s) - 1;
1.95 nicm 1977: data->cy = screen_size_y(s) - 1;
1978: } else
1979: data->cy = cy + sely - yy;
1980:
1.161 nicm 1981: window_copy_update_selection(wp, 1);
1.95 nicm 1982: window_copy_redraw_screen(wp);
1.5 nicm 1983: }
1984:
1.157 nicm 1985: static void
1.1 nicm 1986: window_copy_cursor_left(struct window_pane *wp)
1987: {
1988: struct window_copy_mode_data *data = wp->modedata;
1.168 nicm 1989: u_int py, cx;
1990: struct grid_cell gc;
1.1 nicm 1991:
1.145 nicm 1992: py = screen_hsize(data->backing) + data->cy - data->oy;
1.168 nicm 1993: cx = data->cx;
1994: while (cx > 0) {
1995: grid_get_cell(data->backing->grid, cx, py, &gc);
1996: if (~gc.flags & GRID_FLAG_PADDING)
1997: break;
1998: cx--;
1999: }
2000: if (cx == 0 && py > 0) {
1.28 nicm 2001: window_copy_cursor_up(wp, 0);
1.18 nicm 2002: window_copy_cursor_end_of_line(wp);
1.168 nicm 2003: } else if (cx > 0) {
2004: window_copy_update_cursor(wp, cx - 1, data->cy);
1.96 nicm 2005: if (window_copy_update_selection(wp, 1))
1.1 nicm 2006: window_copy_redraw_lines(wp, data->cy, 1);
2007: }
2008: }
2009:
1.157 nicm 2010: static void
1.1 nicm 2011: window_copy_cursor_right(struct window_pane *wp)
2012: {
2013: struct window_copy_mode_data *data = wp->modedata;
1.168 nicm 2014: u_int px, py, yy, cx, cy;
2015: struct grid_cell gc;
1.1 nicm 2016:
1.145 nicm 2017: py = screen_hsize(data->backing) + data->cy - data->oy;
2018: yy = screen_hsize(data->backing) + screen_size_y(data->backing) - 1;
1.192 nicm 2019: if (data->screen.sel != NULL && data->rectflag)
1.47 nicm 2020: px = screen_size_x(&data->screen);
1.168 nicm 2021: else
1.47 nicm 2022: px = window_copy_find_length(wp, py);
1.1 nicm 2023:
1.145 nicm 2024: if (data->cx >= px && py < yy) {
1.1 nicm 2025: window_copy_cursor_start_of_line(wp);
1.28 nicm 2026: window_copy_cursor_down(wp, 0);
1.145 nicm 2027: } else if (data->cx < px) {
1.168 nicm 2028: cx = data->cx + 1;
2029: cy = screen_hsize(data->backing) + data->cy - data->oy;
2030: while (cx < px) {
2031: grid_get_cell(data->backing->grid, cx, cy, &gc);
2032: if (~gc.flags & GRID_FLAG_PADDING)
2033: break;
2034: cx++;
2035: }
2036: window_copy_update_cursor(wp, cx, data->cy);
1.96 nicm 2037: if (window_copy_update_selection(wp, 1))
1.1 nicm 2038: window_copy_redraw_lines(wp, data->cy, 1);
2039: }
2040: }
2041:
1.157 nicm 2042: static void
1.28 nicm 2043: window_copy_cursor_up(struct window_pane *wp, int scroll_only)
1.1 nicm 2044: {
2045: struct window_copy_mode_data *data = wp->modedata;
1.36 nicm 2046: struct screen *s = &data->screen;
1.1 nicm 2047: u_int ox, oy, px, py;
2048:
1.54 nicm 2049: oy = screen_hsize(data->backing) + data->cy - data->oy;
1.1 nicm 2050: ox = window_copy_find_length(wp, oy);
1.77 nicm 2051: if (data->cx != ox) {
1.25 nicm 2052: data->lastcx = data->cx;
2053: data->lastsx = ox;
2054: }
1.1 nicm 2055:
1.192 nicm 2056: if (data->lineflag == LINE_SEL_LEFT_RIGHT && oy == data->sely)
1.118 nicm 2057: window_copy_other_end(wp);
2058:
1.25 nicm 2059: data->cx = data->lastcx;
1.28 nicm 2060: if (scroll_only || data->cy == 0) {
1.1 nicm 2061: window_copy_scroll_down(wp, 1);
1.36 nicm 2062: if (scroll_only) {
2063: if (data->cy == screen_size_y(s) - 1)
2064: window_copy_redraw_lines(wp, data->cy, 1);
2065: else
2066: window_copy_redraw_lines(wp, data->cy, 2);
2067: }
1.28 nicm 2068: } else {
1.18 nicm 2069: window_copy_update_cursor(wp, data->cx, data->cy - 1);
1.96 nicm 2070: if (window_copy_update_selection(wp, 1)) {
1.36 nicm 2071: if (data->cy == screen_size_y(s) - 1)
2072: window_copy_redraw_lines(wp, data->cy, 1);
2073: else
2074: window_copy_redraw_lines(wp, data->cy, 2);
2075: }
1.1 nicm 2076: }
2077:
1.198 nicm 2078: if (data->screen.sel == NULL || !data->rectflag) {
1.54 nicm 2079: py = screen_hsize(data->backing) + data->cy - data->oy;
1.47 nicm 2080: px = window_copy_find_length(wp, py);
1.49 nicm 2081: if ((data->cx >= data->lastsx && data->cx != px) ||
2082: data->cx > px)
1.47 nicm 2083: window_copy_cursor_end_of_line(wp);
2084: }
1.118 nicm 2085:
1.192 nicm 2086: if (data->lineflag == LINE_SEL_LEFT_RIGHT)
1.118 nicm 2087: window_copy_cursor_end_of_line(wp);
1.192 nicm 2088: else if (data->lineflag == LINE_SEL_RIGHT_LEFT)
1.118 nicm 2089: window_copy_cursor_start_of_line(wp);
1.1 nicm 2090: }
2091:
1.157 nicm 2092: static void
1.28 nicm 2093: window_copy_cursor_down(struct window_pane *wp, int scroll_only)
1.1 nicm 2094: {
2095: struct window_copy_mode_data *data = wp->modedata;
2096: struct screen *s = &data->screen;
2097: u_int ox, oy, px, py;
2098:
1.54 nicm 2099: oy = screen_hsize(data->backing) + data->cy - data->oy;
1.1 nicm 2100: ox = window_copy_find_length(wp, oy);
1.77 nicm 2101: if (data->cx != ox) {
1.25 nicm 2102: data->lastcx = data->cx;
2103: data->lastsx = ox;
2104: }
1.1 nicm 2105:
1.192 nicm 2106: if (data->lineflag == LINE_SEL_RIGHT_LEFT && oy == data->endsely)
1.118 nicm 2107: window_copy_other_end(wp);
2108:
1.25 nicm 2109: data->cx = data->lastcx;
1.28 nicm 2110: if (scroll_only || data->cy == screen_size_y(s) - 1) {
1.1 nicm 2111: window_copy_scroll_up(wp, 1);
1.31 nicm 2112: if (scroll_only && data->cy > 0)
1.28 nicm 2113: window_copy_redraw_lines(wp, data->cy - 1, 2);
2114: } else {
1.18 nicm 2115: window_copy_update_cursor(wp, data->cx, data->cy + 1);
1.96 nicm 2116: if (window_copy_update_selection(wp, 1))
1.1 nicm 2117: window_copy_redraw_lines(wp, data->cy - 1, 2);
2118: }
2119:
1.192 nicm 2120: if (data->screen.sel == NULL || !data->rectflag) {
1.54 nicm 2121: py = screen_hsize(data->backing) + data->cy - data->oy;
1.47 nicm 2122: px = window_copy_find_length(wp, py);
1.49 nicm 2123: if ((data->cx >= data->lastsx && data->cx != px) ||
2124: data->cx > px)
1.47 nicm 2125: window_copy_cursor_end_of_line(wp);
1.52 nicm 2126: }
1.118 nicm 2127:
1.192 nicm 2128: if (data->lineflag == LINE_SEL_LEFT_RIGHT)
1.118 nicm 2129: window_copy_cursor_end_of_line(wp);
1.192 nicm 2130: else if (data->lineflag == LINE_SEL_RIGHT_LEFT)
1.118 nicm 2131: window_copy_cursor_start_of_line(wp);
1.52 nicm 2132: }
2133:
1.157 nicm 2134: static void
1.52 nicm 2135: window_copy_cursor_jump(struct window_pane *wp)
2136: {
2137: struct window_copy_mode_data *data = wp->modedata;
1.54 nicm 2138: struct screen *back_s = data->backing;
1.140 nicm 2139: struct grid_cell gc;
1.67 nicm 2140: u_int px, py, xx;
1.52 nicm 2141:
2142: px = data->cx + 1;
1.54 nicm 2143: py = screen_hsize(back_s) + data->cy - data->oy;
1.52 nicm 2144: xx = window_copy_find_length(wp, py);
2145:
2146: while (px < xx) {
1.140 nicm 2147: grid_get_cell(back_s->grid, px, py, &gc);
2148: if (!(gc.flags & GRID_FLAG_PADDING) &&
2149: gc.data.size == 1 && *gc.data.data == data->jumpchar) {
1.52 nicm 2150: window_copy_update_cursor(wp, px, data->cy);
1.96 nicm 2151: if (window_copy_update_selection(wp, 1))
1.52 nicm 2152: window_copy_redraw_lines(wp, data->cy, 1);
2153: return;
2154: }
2155: px++;
2156: }
2157: }
2158:
1.157 nicm 2159: static void
1.52 nicm 2160: window_copy_cursor_jump_back(struct window_pane *wp)
2161: {
2162: struct window_copy_mode_data *data = wp->modedata;
1.54 nicm 2163: struct screen *back_s = data->backing;
1.140 nicm 2164: struct grid_cell gc;
1.67 nicm 2165: u_int px, py;
1.52 nicm 2166:
2167: px = data->cx;
1.54 nicm 2168: py = screen_hsize(back_s) + data->cy - data->oy;
1.52 nicm 2169:
2170: if (px > 0)
2171: px--;
2172:
2173: for (;;) {
1.140 nicm 2174: grid_get_cell(back_s->grid, px, py, &gc);
2175: if (!(gc.flags & GRID_FLAG_PADDING) &&
2176: gc.data.size == 1 && *gc.data.data == data->jumpchar) {
1.52 nicm 2177: window_copy_update_cursor(wp, px, data->cy);
1.96 nicm 2178: if (window_copy_update_selection(wp, 1))
1.76 nicm 2179: window_copy_redraw_lines(wp, data->cy, 1);
2180: return;
2181: }
2182: if (px == 0)
2183: break;
2184: px--;
2185: }
2186: }
2187:
1.157 nicm 2188: static void
1.184 nicm 2189: window_copy_cursor_jump_to(struct window_pane *wp)
1.76 nicm 2190: {
2191: struct window_copy_mode_data *data = wp->modedata;
2192: struct screen *back_s = data->backing;
1.140 nicm 2193: struct grid_cell gc;
1.76 nicm 2194: u_int px, py, xx;
2195:
1.184 nicm 2196: px = data->cx + 2;
1.76 nicm 2197: py = screen_hsize(back_s) + data->cy - data->oy;
2198: xx = window_copy_find_length(wp, py);
2199:
2200: while (px < xx) {
1.140 nicm 2201: grid_get_cell(back_s->grid, px, py, &gc);
2202: if (!(gc.flags & GRID_FLAG_PADDING) &&
2203: gc.data.size == 1 && *gc.data.data == data->jumpchar) {
1.76 nicm 2204: window_copy_update_cursor(wp, px - 1, data->cy);
1.96 nicm 2205: if (window_copy_update_selection(wp, 1))
1.76 nicm 2206: window_copy_redraw_lines(wp, data->cy, 1);
2207: return;
2208: }
2209: px++;
2210: }
2211: }
2212:
1.157 nicm 2213: static void
1.184 nicm 2214: window_copy_cursor_jump_to_back(struct window_pane *wp)
1.76 nicm 2215: {
2216: struct window_copy_mode_data *data = wp->modedata;
2217: struct screen *back_s = data->backing;
1.140 nicm 2218: struct grid_cell gc;
1.76 nicm 2219: u_int px, py;
2220:
2221: px = data->cx;
2222: py = screen_hsize(back_s) + data->cy - data->oy;
2223:
2224: if (px > 0)
1.127 nicm 2225: px--;
2226:
1.184 nicm 2227: if (px > 0)
1.76 nicm 2228: px--;
2229:
2230: for (;;) {
1.140 nicm 2231: grid_get_cell(back_s->grid, px, py, &gc);
2232: if (!(gc.flags & GRID_FLAG_PADDING) &&
2233: gc.data.size == 1 && *gc.data.data == data->jumpchar) {
1.76 nicm 2234: window_copy_update_cursor(wp, px + 1, data->cy);
1.96 nicm 2235: if (window_copy_update_selection(wp, 1))
1.52 nicm 2236: window_copy_redraw_lines(wp, data->cy, 1);
2237: return;
2238: }
2239: if (px == 0)
2240: break;
2241: px--;
1.47 nicm 2242: }
1.1 nicm 2243: }
2244:
1.157 nicm 2245: static void
1.41 nicm 2246: window_copy_cursor_next_word(struct window_pane *wp, const char *separators)
1.40 nicm 2247: {
2248: struct window_copy_mode_data *data = wp->modedata;
1.54 nicm 2249: struct screen *back_s = data->backing;
1.40 nicm 2250: u_int px, py, xx, yy;
1.48 nicm 2251: int expected = 0;
1.40 nicm 2252:
2253: px = data->cx;
1.54 nicm 2254: py = screen_hsize(back_s) + data->cy - data->oy;
1.40 nicm 2255: xx = window_copy_find_length(wp, py);
1.54 nicm 2256: yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
1.40 nicm 2257:
1.48 nicm 2258: /*
2259: * First skip past any nonword characters and then any word characters.
2260: *
2261: * expected is initially set to 0 for the former and then 1 for the
2262: * latter.
2263: */
2264: do {
2265: while (px > xx ||
2266: window_copy_in_set(wp, px, py, separators) == expected) {
2267: /* Move down if we're past the end of the line. */
2268: if (px > xx) {
2269: if (py == yy)
2270: return;
2271: window_copy_cursor_down(wp, 0);
2272: px = 0;
2273:
1.54 nicm 2274: py = screen_hsize(back_s) + data->cy - data->oy;
1.48 nicm 2275: xx = window_copy_find_length(wp, py);
2276: } else
2277: px++;
2278: }
2279: expected = !expected;
2280: } while (expected == 1);
1.40 nicm 2281:
2282: window_copy_update_cursor(wp, px, data->cy);
1.96 nicm 2283: if (window_copy_update_selection(wp, 1))
1.40 nicm 2284: window_copy_redraw_lines(wp, data->cy, 1);
2285: }
2286:
1.157 nicm 2287: static void
1.106 nicm 2288: window_copy_cursor_next_word_end(struct window_pane *wp,
2289: const char *separators)
1.1 nicm 2290: {
2291: struct window_copy_mode_data *data = wp->modedata;
1.136 nicm 2292: struct options *oo = wp->window->options;
1.54 nicm 2293: struct screen *back_s = data->backing;
1.39 nicm 2294: u_int px, py, xx, yy;
1.94 nicm 2295: int keys, expected = 1;
1.1 nicm 2296:
1.18 nicm 2297: px = data->cx;
1.54 nicm 2298: py = screen_hsize(back_s) + data->cy - data->oy;
1.1 nicm 2299: xx = window_copy_find_length(wp, py);
1.54 nicm 2300: yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
1.1 nicm 2301:
1.94 nicm 2302: keys = options_get_number(oo, "mode-keys");
2303: if (keys == MODEKEY_VI && !window_copy_in_set(wp, px, py, separators))
2304: px++;
2305:
1.48 nicm 2306: /*
2307: * First skip past any word characters, then any nonword characters.
2308: *
2309: * expected is initially set to 1 for the former and then 0 for the
2310: * latter.
2311: */
2312: do {
2313: while (px > xx ||
2314: window_copy_in_set(wp, px, py, separators) == expected) {
2315: /* Move down if we're past the end of the line. */
2316: if (px > xx) {
2317: if (py == yy)
2318: return;
2319: window_copy_cursor_down(wp, 0);
2320: px = 0;
2321:
1.54 nicm 2322: py = screen_hsize(back_s) + data->cy - data->oy;
1.48 nicm 2323: xx = window_copy_find_length(wp, py);
2324: } else
2325: px++;
2326: }
2327: expected = !expected;
2328: } while (expected == 0);
1.92 nicm 2329:
1.94 nicm 2330: if (keys == MODEKEY_VI && px != 0)
1.92 nicm 2331: px--;
1.18 nicm 2332:
2333: window_copy_update_cursor(wp, px, data->cy);
1.96 nicm 2334: if (window_copy_update_selection(wp, 1))
1.18 nicm 2335: window_copy_redraw_lines(wp, data->cy, 1);
1.1 nicm 2336: }
2337:
1.8 nicm 2338: /* Move to the previous place where a word begins. */
1.157 nicm 2339: static void
1.106 nicm 2340: window_copy_cursor_previous_word(struct window_pane *wp,
2341: const char *separators)
1.1 nicm 2342: {
2343: struct window_copy_mode_data *data = wp->modedata;
1.8 nicm 2344: u_int px, py;
1.1 nicm 2345:
1.18 nicm 2346: px = data->cx;
1.54 nicm 2347: py = screen_hsize(data->backing) + data->cy - data->oy;
1.1 nicm 2348:
1.8 nicm 2349: /* Move back to the previous word character. */
1.1 nicm 2350: for (;;) {
1.8 nicm 2351: if (px > 0) {
2352: px--;
1.41 nicm 2353: if (!window_copy_in_set(wp, px, py, separators))
1.1 nicm 2354: break;
1.8 nicm 2355: } else {
2356: if (data->cy == 0 &&
1.54 nicm 2357: (screen_hsize(data->backing) == 0 ||
2358: data->oy >= screen_hsize(data->backing) - 1))
1.8 nicm 2359: goto out;
1.28 nicm 2360: window_copy_cursor_up(wp, 0);
1.1 nicm 2361:
1.54 nicm 2362: py = screen_hsize(data->backing) + data->cy - data->oy;
1.8 nicm 2363: px = window_copy_find_length(wp, py);
1.1 nicm 2364: }
1.8 nicm 2365: }
1.1 nicm 2366:
1.8 nicm 2367: /* Move back to the beginning of this word. */
1.41 nicm 2368: while (px > 0 && !window_copy_in_set(wp, px - 1, py, separators))
1.1 nicm 2369: px--;
1.8 nicm 2370:
1.1 nicm 2371: out:
1.18 nicm 2372: window_copy_update_cursor(wp, px, data->cy);
1.96 nicm 2373: if (window_copy_update_selection(wp, 1))
1.18 nicm 2374: window_copy_redraw_lines(wp, data->cy, 1);
1.1 nicm 2375: }
2376:
1.157 nicm 2377: static void
1.1 nicm 2378: window_copy_scroll_up(struct window_pane *wp, u_int ny)
2379: {
2380: struct window_copy_mode_data *data = wp->modedata;
2381: struct screen *s = &data->screen;
2382: struct screen_write_ctx ctx;
2383:
2384: if (data->oy < ny)
2385: ny = data->oy;
2386: if (ny == 0)
2387: return;
2388: data->oy -= ny;
2389:
1.96 nicm 2390: window_copy_update_selection(wp, 0);
2391:
1.1 nicm 2392: screen_write_start(&ctx, wp, NULL);
2393: screen_write_cursormove(&ctx, 0, 0);
1.159 nicm 2394: screen_write_deleteline(&ctx, ny, 8);
1.1 nicm 2395: window_copy_write_lines(wp, &ctx, screen_size_y(s) - ny, ny);
2396: window_copy_write_line(wp, &ctx, 0);
1.31 nicm 2397: if (screen_size_y(s) > 1)
2398: window_copy_write_line(wp, &ctx, 1);
2399: if (screen_size_y(s) > 3)
2400: window_copy_write_line(wp, &ctx, screen_size_y(s) - 2);
1.192 nicm 2401: if (s->sel != NULL && screen_size_y(s) > ny)
1.1 nicm 2402: window_copy_write_line(wp, &ctx, screen_size_y(s) - ny - 1);
2403: screen_write_cursormove(&ctx, data->cx, data->cy);
2404: screen_write_stop(&ctx);
2405: }
2406:
1.157 nicm 2407: static void
1.1 nicm 2408: window_copy_scroll_down(struct window_pane *wp, u_int ny)
2409: {
2410: struct window_copy_mode_data *data = wp->modedata;
2411: struct screen *s = &data->screen;
2412: struct screen_write_ctx ctx;
2413:
1.54 nicm 2414: if (ny > screen_hsize(data->backing))
1.1 nicm 2415: return;
2416:
1.54 nicm 2417: if (data->oy > screen_hsize(data->backing) - ny)
2418: ny = screen_hsize(data->backing) - data->oy;
1.1 nicm 2419: if (ny == 0)
2420: return;
2421: data->oy += ny;
2422:
1.96 nicm 2423: window_copy_update_selection(wp, 0);
2424:
1.1 nicm 2425: screen_write_start(&ctx, wp, NULL);
2426: screen_write_cursormove(&ctx, 0, 0);
1.159 nicm 2427: screen_write_insertline(&ctx, ny, 8);
1.1 nicm 2428: window_copy_write_lines(wp, &ctx, 0, ny);
1.192 nicm 2429: if (s->sel != NULL && screen_size_y(s) > ny)
1.1 nicm 2430: window_copy_write_line(wp, &ctx, ny);
1.96 nicm 2431: else if (ny == 1) /* nuke position */
1.1 nicm 2432: window_copy_write_line(wp, &ctx, 1);
2433: screen_write_cursormove(&ctx, data->cx, data->cy);
2434: screen_write_stop(&ctx);
1.135 nicm 2435: }
2436:
1.180 nicm 2437: void
2438: window_copy_add_formats(struct window_pane *wp, struct format_tree *ft)
1.135 nicm 2439: {
2440: struct window_copy_mode_data *data = wp->modedata;
2441:
2442: if (wp->mode != &window_copy_mode)
1.180 nicm 2443: return;
2444:
1.192 nicm 2445: format_add(ft, "selection_present", "%d", data->screen.sel != NULL);
1.180 nicm 2446: format_add(ft, "scroll_position", "%d", data->oy);
1.191 nicm 2447: format_add(ft, "rectangle_toggle", "%d", data->rectflag);
1.1 nicm 2448: }
1.42 nicm 2449:
1.157 nicm 2450: static void
1.42 nicm 2451: window_copy_rectangle_toggle(struct window_pane *wp)
2452: {
2453: struct window_copy_mode_data *data = wp->modedata;
1.47 nicm 2454: u_int px, py;
1.42 nicm 2455:
2456: data->rectflag = !data->rectflag;
1.47 nicm 2457:
1.54 nicm 2458: py = screen_hsize(data->backing) + data->cy - data->oy;
1.47 nicm 2459: px = window_copy_find_length(wp, py);
2460: if (data->cx > px)
2461: window_copy_update_cursor(wp, px, data->cy);
1.42 nicm 2462:
1.96 nicm 2463: window_copy_update_selection(wp, 1);
1.42 nicm 2464: window_copy_redraw_screen(wp);
1.156 nicm 2465: }
2466:
1.157 nicm 2467: static void
1.156 nicm 2468: window_copy_move_mouse(struct mouse_event *m)
2469: {
2470: struct window_pane *wp;
2471: u_int x, y;
2472:
2473: wp = cmd_mouse_pane(m, NULL, NULL);
2474: if (wp == NULL || wp->mode != &window_copy_mode)
2475: return;
2476:
1.183 nicm 2477: if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
1.156 nicm 2478: return;
2479:
2480: window_copy_update_cursor(wp, x, y);
1.126 nicm 2481: }
2482:
2483: void
1.141 nicm 2484: window_copy_start_drag(struct client *c, struct mouse_event *m)
1.126 nicm 2485: {
1.128 nicm 2486: struct window_pane *wp;
2487: u_int x, y;
1.126 nicm 2488:
1.155 nicm 2489: if (c == NULL)
2490: return;
2491:
1.126 nicm 2492: wp = cmd_mouse_pane(m, NULL, NULL);
1.129 nicm 2493: if (wp == NULL || wp->mode != &window_copy_mode)
1.126 nicm 2494: return;
2495:
2496: if (cmd_mouse_at(wp, m, &x, &y, 1) != 0)
2497: return;
2498:
2499: c->tty.mouse_drag_update = window_copy_drag_update;
1.155 nicm 2500: c->tty.mouse_drag_release = NULL; /* will fire MouseDragEnd key */
1.126 nicm 2501:
2502: window_copy_update_cursor(wp, x, y);
2503: window_copy_start_selection(wp);
2504: window_copy_redraw_screen(wp);
2505: }
2506:
1.157 nicm 2507: static void
1.141 nicm 2508: window_copy_drag_update(__unused struct client *c, struct mouse_event *m)
1.126 nicm 2509: {
2510: struct window_pane *wp;
2511: struct window_copy_mode_data *data;
2512: u_int x, y, old_cy;
2513:
2514: wp = cmd_mouse_pane(m, NULL, NULL);
1.129 nicm 2515: if (wp == NULL || wp->mode != &window_copy_mode)
1.126 nicm 2516: return;
2517: data = wp->modedata;
2518:
2519: if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
2520: return;
2521: old_cy = data->cy;
2522:
2523: window_copy_update_cursor(wp, x, y);
2524: if (window_copy_update_selection(wp, 1))
2525: window_copy_redraw_selection(wp, old_cy);
1.42 nicm 2526: }