Annotation of src/usr.bin/tmux/window-copy.c, Revision 1.261
1.261 ! nicm 1: /* $OpenBSD: window-copy.c,v 1.260 2020/04/01 07:35:10 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.244 nicm 22: #include <regex.h>
1.21 nicm 23: #include <stdlib.h>
1.1 nicm 24: #include <string.h>
25:
26: #include "tmux.h"
27:
1.208 nicm 28: static const char *window_copy_key_table(struct window_mode_entry *);
29: static void window_copy_command(struct window_mode_entry *, struct client *,
1.205 nicm 30: struct session *, struct winlink *, struct args *,
31: struct mouse_event *);
1.208 nicm 32: static struct screen *window_copy_init(struct window_mode_entry *,
1.177 nicm 33: struct cmd_find_state *, struct args *);
1.210 nicm 34: static struct screen *window_copy_view_init(struct window_mode_entry *,
35: struct cmd_find_state *, struct args *);
1.208 nicm 36: static void window_copy_free(struct window_mode_entry *);
37: static void window_copy_resize(struct window_mode_entry *, u_int, u_int);
38: static void window_copy_formats(struct window_mode_entry *,
39: struct format_tree *);
40: static void window_copy_pageup1(struct window_mode_entry *, int);
41: static int window_copy_pagedown(struct window_mode_entry *, int, int);
42: static void window_copy_next_paragraph(struct window_mode_entry *);
43: static void window_copy_previous_paragraph(struct window_mode_entry *);
44:
45: static void window_copy_redraw_selection(struct window_mode_entry *, u_int);
46: static void window_copy_redraw_lines(struct window_mode_entry *, u_int,
47: u_int);
48: static void window_copy_redraw_screen(struct window_mode_entry *);
49: static void window_copy_write_line(struct window_mode_entry *,
1.157 nicm 50: struct screen_write_ctx *, u_int);
1.208 nicm 51: static void window_copy_write_lines(struct window_mode_entry *,
1.157 nicm 52: struct screen_write_ctx *, u_int, u_int);
53:
1.208 nicm 54: static void window_copy_scroll_to(struct window_mode_entry *, u_int, u_int);
1.157 nicm 55: static int window_copy_search_compare(struct grid *, u_int, u_int,
56: struct grid *, u_int, int);
57: static int window_copy_search_lr(struct grid *, struct grid *, u_int *,
58: u_int, u_int, u_int, int);
59: static int window_copy_search_rl(struct grid *, struct grid *, u_int *,
60: u_int, u_int, u_int, int);
1.244 nicm 61: static int window_copy_last_regex(struct grid *gd, u_int py, u_int first,
62: u_int last, u_int len, u_int *ppx, u_int *psx,
63: const char *buf, const regex_t *preg, int eflags);
64: static char *window_copy_stringify(struct grid *, u_int, u_int, u_int,
65: char *, u_int *);
66: static void window_copy_cstrtocellpos(struct grid *, u_int, u_int *, u_int *,
67: const char *str);
1.208 nicm 68: static int window_copy_search_marks(struct window_mode_entry *,
1.244 nicm 69: struct screen *, int);
1.208 nicm 70: static void window_copy_clear_marks(struct window_mode_entry *);
1.230 nicm 71: static void window_copy_move_left(struct screen *, u_int *, u_int *, int);
72: static void window_copy_move_right(struct screen *, u_int *, u_int *, int);
1.157 nicm 73: static int window_copy_is_lowercase(const char *);
1.208 nicm 74: static int window_copy_search_jump(struct window_mode_entry *,
75: struct grid *, struct grid *, u_int, u_int, u_int, int, int,
1.244 nicm 76: int, int);
77: static int window_copy_search(struct window_mode_entry *, int, int);
78: static int window_copy_search_up(struct window_mode_entry *, int);
79: static int window_copy_search_down(struct window_mode_entry *, int);
1.208 nicm 80: static void window_copy_goto_line(struct window_mode_entry *, const char *);
81: static void window_copy_update_cursor(struct window_mode_entry *, u_int,
82: u_int);
83: static void window_copy_start_selection(struct window_mode_entry *);
84: static int window_copy_adjust_selection(struct window_mode_entry *,
85: u_int *, u_int *);
1.252 nicm 86: static int window_copy_set_selection(struct window_mode_entry *, int, int);
87: static int window_copy_update_selection(struct window_mode_entry *, int,
88: int);
89: static void window_copy_synchronize_cursor(struct window_mode_entry *, int);
1.208 nicm 90: static void *window_copy_get_selection(struct window_mode_entry *, size_t *);
1.215 nicm 91: static void window_copy_copy_buffer(struct window_mode_entry *,
92: const char *, void *, size_t);
1.208 nicm 93: static void window_copy_copy_pipe(struct window_mode_entry *,
1.215 nicm 94: struct session *, const char *, const char *);
95: static void window_copy_copy_selection(struct window_mode_entry *,
96: const char *);
1.208 nicm 97: static void window_copy_append_selection(struct window_mode_entry *);
98: static void window_copy_clear_selection(struct window_mode_entry *);
99: static void window_copy_copy_line(struct window_mode_entry *, char **,
100: size_t *, u_int, u_int, u_int);
101: static int window_copy_in_set(struct window_mode_entry *, u_int, u_int,
1.157 nicm 102: const char *);
1.208 nicm 103: static u_int window_copy_find_length(struct window_mode_entry *, u_int);
104: static void window_copy_cursor_start_of_line(struct window_mode_entry *);
105: static void window_copy_cursor_back_to_indentation(
106: struct window_mode_entry *);
107: static void window_copy_cursor_end_of_line(struct window_mode_entry *);
108: static void window_copy_other_end(struct window_mode_entry *);
109: static void window_copy_cursor_left(struct window_mode_entry *);
110: static void window_copy_cursor_right(struct window_mode_entry *);
111: static void window_copy_cursor_up(struct window_mode_entry *, int);
112: static void window_copy_cursor_down(struct window_mode_entry *, int);
113: static void window_copy_cursor_jump(struct window_mode_entry *);
114: static void window_copy_cursor_jump_back(struct window_mode_entry *);
115: static void window_copy_cursor_jump_to(struct window_mode_entry *);
116: static void window_copy_cursor_jump_to_back(struct window_mode_entry *);
117: static void window_copy_cursor_next_word(struct window_mode_entry *,
1.157 nicm 118: const char *);
1.247 nicm 119: static void window_copy_cursor_next_word_end_pos(struct window_mode_entry *,
120: const char *, u_int *, u_int *);
1.208 nicm 121: static void window_copy_cursor_next_word_end(struct window_mode_entry *,
1.255 nicm 122: const char *, int);
1.247 nicm 123: static void window_copy_cursor_previous_word_pos(struct window_mode_entry *,
124: const char *, int, u_int *, u_int *);
1.208 nicm 125: static void window_copy_cursor_previous_word(struct window_mode_entry *,
1.229 nicm 126: const char *, int);
1.208 nicm 127: static void window_copy_scroll_up(struct window_mode_entry *, u_int);
128: static void window_copy_scroll_down(struct window_mode_entry *, u_int);
129: static void window_copy_rectangle_toggle(struct window_mode_entry *);
1.157 nicm 130: static void window_copy_move_mouse(struct mouse_event *);
131: static void window_copy_drag_update(struct client *, struct mouse_event *);
1.217 nicm 132: static void window_copy_drag_release(struct client *, struct mouse_event *);
1.1 nicm 133:
134: const struct window_mode window_copy_mode = {
1.173 nicm 135: .name = "copy-mode",
136:
1.155 nicm 137: .init = window_copy_init,
138: .free = window_copy_free,
139: .resize = window_copy_resize,
140: .key_table = window_copy_key_table,
141: .command = window_copy_command,
1.206 nicm 142: .formats = window_copy_formats,
1.1 nicm 143: };
144:
1.210 nicm 145: const struct window_mode window_view_mode = {
146: .name = "view-mode",
147:
148: .init = window_copy_view_init,
149: .free = window_copy_free,
150: .resize = window_copy_resize,
151: .key_table = window_copy_key_table,
152: .command = window_copy_command,
153: .formats = window_copy_formats,
154: };
155:
1.155 nicm 156: enum {
1.21 nicm 157: WINDOW_COPY_OFF,
158: WINDOW_COPY_SEARCHUP,
159: WINDOW_COPY_SEARCHDOWN,
1.52 nicm 160: WINDOW_COPY_JUMPFORWARD,
1.155 nicm 161: WINDOW_COPY_JUMPBACKWARD,
1.76 nicm 162: WINDOW_COPY_JUMPTOFORWARD,
1.155 nicm 163: WINDOW_COPY_JUMPTOBACKWARD,
1.21 nicm 164: };
165:
1.161 nicm 166: enum {
167: WINDOW_COPY_REL_POS_ABOVE,
168: WINDOW_COPY_REL_POS_ON_SCREEN,
169: WINDOW_COPY_REL_POS_BELOW,
170: };
171:
1.213 nicm 172: enum window_copy_cmd_action {
173: WINDOW_COPY_CMD_NOTHING,
174: WINDOW_COPY_CMD_REDRAW,
175: WINDOW_COPY_CMD_CANCEL,
176: };
177:
178: struct window_copy_cmd_state {
179: struct window_mode_entry *wme;
180: struct args *args;
181: struct mouse_event *m;
1.215 nicm 182:
1.213 nicm 183: struct client *c;
184: struct session *s;
1.215 nicm 185: struct winlink *wl;
1.213 nicm 186: };
187:
1.54 nicm 188: /*
1.161 nicm 189: * Copy mode's visible screen (the "screen" field) is filled from one of two
190: * sources: the original contents of the pane (used when we actually enter via
191: * the "copy-mode" command, to copy the contents of the current pane), or else
192: * a series of lines containing the output from an output-writing tmux command
193: * (such as any of the "show-*" or "list-*" commands).
1.54 nicm 194: *
1.161 nicm 195: * In either case, the full content of the copy-mode grid is pointed at by the
196: * "backing" field, and is copied into "screen" as needed (that is, when
197: * scrolling occurs). When copy-mode is backed by a pane, backing points
198: * directly at that pane's screen structure (&wp->base); when backed by a list
199: * of output-lines from a command, it points at a newly-allocated screen
200: * structure (which is deallocated when the mode ends).
1.54 nicm 201: */
1.1 nicm 202: struct window_copy_mode_data {
1.155 nicm 203: struct screen screen;
1.1 nicm 204:
1.155 nicm 205: struct screen *backing;
206: int backing_written; /* backing display started */
1.54 nicm 207:
1.192 nicm 208: u_int oy; /* number of lines scrolled up */
1.21 nicm 209:
1.192 nicm 210: u_int selx; /* beginning of selection */
1.155 nicm 211: u_int sely;
1.21 nicm 212:
1.192 nicm 213: u_int endselx; /* end of selection */
1.161 nicm 214: u_int endsely;
215:
216: enum {
217: CURSORDRAG_NONE, /* selection is independent of cursor */
218: CURSORDRAG_ENDSEL, /* end is synchronized with cursor */
219: CURSORDRAG_SEL, /* start is synchronized with cursor */
220: } cursordrag;
221:
1.192 nicm 222: int modekeys;
223: enum {
224: LINE_SEL_NONE,
225: LINE_SEL_LEFT_RIGHT,
226: LINE_SEL_RIGHT_LEFT,
227: } lineflag; /* line selection mode */
1.155 nicm 228: int rectflag; /* in rectangle copy mode? */
229: int scroll_exit; /* exit on scroll to end? */
1.249 nicm 230: int hide_position; /* hide position marker */
1.1 nicm 231:
1.247 nicm 232: enum {
233: SEL_CHAR, /* select one char at a time */
234: SEL_WORD, /* select one word at a time */
235: SEL_LINE, /* select one line at a time */
236: } selflag;
237:
238: const char *ws; /* word separators */
239:
240: u_int dx; /* drag start position */
241: u_int dy;
242:
243: u_int selrx; /* selection reset positions */
244: u_int selry;
245: u_int endselrx;
246: u_int endselry;
247:
1.155 nicm 248: u_int cx;
249: u_int cy;
1.42 nicm 250:
1.192 nicm 251: u_int lastcx; /* position in last line w/ content */
252: u_int lastsx; /* size of last line w/ content */
1.1 nicm 253:
1.155 nicm 254: int searchtype;
1.259 nicm 255: int searchregex;
1.155 nicm 256: char *searchstr;
1.218 nicm 257: bitstr_t *searchmark;
1.170 nicm 258: u_int searchcount;
259: int searchthis;
1.163 nicm 260: int searchx;
261: int searchy;
262: int searcho;
1.25 nicm 263:
1.155 nicm 264: int jumptype;
265: char jumpchar;
1.217 nicm 266:
267: struct event dragtimer;
268: #define WINDOW_COPY_DRAG_REPEAT_TIME 50000
1.1 nicm 269: };
270:
1.217 nicm 271: static void
272: window_copy_scroll_timer(__unused int fd, __unused short events, void *arg)
273: {
274: struct window_mode_entry *wme = arg;
275: struct window_pane *wp = wme->wp;
276: struct window_copy_mode_data *data = wme->data;
277: struct timeval tv = {
278: .tv_usec = WINDOW_COPY_DRAG_REPEAT_TIME
279: };
280:
281: evtimer_del(&data->dragtimer);
282:
283: if (TAILQ_FIRST(&wp->modes) != wme)
284: return;
285:
286: if (data->cy == 0) {
287: evtimer_add(&data->dragtimer, &tv);
288: window_copy_cursor_up(wme, 1);
289: } else if (data->cy == screen_size_y(&data->screen) - 1) {
290: evtimer_add(&data->dragtimer, &tv);
291: window_copy_cursor_down(wme, 1);
292: }
293: }
294:
1.210 nicm 295: static struct window_copy_mode_data *
296: window_copy_common_init(struct window_mode_entry *wme)
1.1 nicm 297: {
1.208 nicm 298: struct window_pane *wp = wme->wp;
1.1 nicm 299: struct window_copy_mode_data *data;
1.210 nicm 300: struct screen *base = &wp->base;
1.1 nicm 301:
1.208 nicm 302: wme->data = data = xcalloc(1, sizeof *data);
1.1 nicm 303:
1.161 nicm 304: data->cursordrag = CURSORDRAG_NONE;
1.193 nicm 305: data->lineflag = LINE_SEL_NONE;
1.250 nicm 306: data->selflag = SEL_CHAR;
1.42 nicm 307:
1.174 nicm 308: if (wp->searchstr != NULL) {
309: data->searchtype = WINDOW_COPY_SEARCHUP;
1.259 nicm 310: data->searchregex = wp->searchregex;
1.174 nicm 311: data->searchstr = xstrdup(wp->searchstr);
312: } else {
313: data->searchtype = WINDOW_COPY_OFF;
1.259 nicm 314: data->searchregex = 0;
1.174 nicm 315: data->searchstr = NULL;
316: }
1.162 nicm 317: data->searchmark = NULL;
1.163 nicm 318: data->searchx = data->searchy = data->searcho = -1;
1.21 nicm 319:
1.52 nicm 320: data->jumptype = WINDOW_COPY_OFF;
321: data->jumpchar = '\0';
322:
1.210 nicm 323: screen_init(&data->screen, screen_size_x(base), screen_size_y(base), 0);
1.192 nicm 324: data->modekeys = options_get_number(wp->window->options, "mode-keys");
1.1 nicm 325:
1.217 nicm 326: evtimer_set(&data->dragtimer, window_copy_scroll_timer, wme);
327:
1.210 nicm 328: return (data);
1.54 nicm 329: }
330:
1.210 nicm 331: static struct screen *
332: window_copy_init(struct window_mode_entry *wme,
333: __unused struct cmd_find_state *fs, struct args *args)
1.54 nicm 334: {
1.210 nicm 335: struct window_pane *wp = wme->wp;
336: struct window_copy_mode_data *data;
337: struct screen_write_ctx ctx;
1.54 nicm 338: u_int i;
339:
1.210 nicm 340: data = window_copy_common_init(wme);
341:
1.211 nicm 342: if (wp->fd != -1 && wp->disabled++ == 0)
1.210 nicm 343: bufferevent_disable(wp->event, EV_READ|EV_WRITE);
1.54 nicm 344:
345: data->backing = &wp->base;
346: data->cx = data->backing->cx;
347: data->cy = data->backing->cy;
348:
1.210 nicm 349: data->scroll_exit = args_has(args, 'e');
1.249 nicm 350: data->hide_position = args_has(args, 'H');
1.1 nicm 351:
1.210 nicm 352: data->screen.cx = data->cx;
353: data->screen.cy = data->cy;
354:
355: screen_write_start(&ctx, NULL, &data->screen);
356: for (i = 0; i < screen_size_y(&data->screen); i++)
1.208 nicm 357: window_copy_write_line(wme, &ctx, i);
1.212 nicm 358: screen_write_cursormove(&ctx, data->cx, data->cy, 0);
1.1 nicm 359: screen_write_stop(&ctx);
1.210 nicm 360:
361: return (&data->screen);
1.54 nicm 362: }
1.1 nicm 363:
1.210 nicm 364: static struct screen *
365: window_copy_view_init(struct window_mode_entry *wme,
366: __unused struct cmd_find_state *fs, __unused struct args *args)
1.54 nicm 367: {
1.210 nicm 368: struct window_pane *wp = wme->wp;
1.208 nicm 369: struct window_copy_mode_data *data;
1.210 nicm 370: struct screen *base = &wp->base;
371: struct screen *s;
1.54 nicm 372:
1.210 nicm 373: data = window_copy_common_init(wme);
1.208 nicm 374:
1.210 nicm 375: data->backing = s = xmalloc(sizeof *data->backing);
376: screen_init(s, screen_size_x(base), screen_size_y(base), UINT_MAX);
1.207 nicm 377:
1.210 nicm 378: return (&data->screen);
1.1 nicm 379: }
380:
1.157 nicm 381: static void
1.208 nicm 382: window_copy_free(struct window_mode_entry *wme)
1.1 nicm 383: {
1.208 nicm 384: struct window_pane *wp = wme->wp;
385: struct window_copy_mode_data *data = wme->data;
1.46 nicm 386:
1.217 nicm 387: evtimer_del(&data->dragtimer);
388:
1.211 nicm 389: if (wp->fd != -1 && --wp->disabled == 0)
1.61 nicm 390: bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1.1 nicm 391:
1.162 nicm 392: free(data->searchmark);
1.81 nicm 393: free(data->searchstr);
1.21 nicm 394:
1.54 nicm 395: if (data->backing != &wp->base) {
396: screen_free(data->backing);
1.81 nicm 397: free(data->backing);
1.54 nicm 398: }
1.1 nicm 399: screen_free(&data->screen);
1.21 nicm 400:
1.81 nicm 401: free(data);
1.1 nicm 402: }
403:
404: void
1.54 nicm 405: window_copy_add(struct window_pane *wp, const char *fmt, ...)
406: {
407: va_list ap;
408:
409: va_start(ap, fmt);
410: window_copy_vadd(wp, fmt, ap);
411: va_end(ap);
412: }
413:
414: void
415: window_copy_vadd(struct window_pane *wp, const char *fmt, va_list ap)
416: {
1.211 nicm 417: struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
1.208 nicm 418: struct window_copy_mode_data *data = wme->data;
1.54 nicm 419: struct screen *backing = data->backing;
420: struct screen_write_ctx back_ctx, ctx;
421: struct grid_cell gc;
1.119 nicm 422: u_int old_hsize, old_cy;
1.54 nicm 423:
424: if (backing == &wp->base)
425: return;
426:
427: memcpy(&gc, &grid_default_cell, sizeof gc);
428:
429: old_hsize = screen_hsize(data->backing);
430: screen_write_start(&back_ctx, NULL, backing);
431: if (data->backing_written) {
432: /*
433: * On the second or later line, do a CRLF before writing
434: * (so it's on a new line).
435: */
436: screen_write_carriagereturn(&back_ctx);
1.175 nicm 437: screen_write_linefeed(&back_ctx, 0, 8);
1.54 nicm 438: } else
439: data->backing_written = 1;
1.119 nicm 440: old_cy = backing->cy;
1.139 nicm 441: screen_write_vnputs(&back_ctx, 0, &gc, fmt, ap);
1.54 nicm 442: screen_write_stop(&back_ctx);
443:
444: data->oy += screen_hsize(data->backing) - old_hsize;
445:
446: screen_write_start(&ctx, wp, &data->screen);
447:
448: /*
449: * If the history has changed, draw the top line.
450: * (If there's any history at all, it has changed.)
451: */
452: if (screen_hsize(data->backing))
1.208 nicm 453: window_copy_redraw_lines(wme, 0, 1);
1.54 nicm 454:
1.119 nicm 455: /* Write the new lines. */
1.208 nicm 456: window_copy_redraw_lines(wme, old_cy, backing->cy - old_cy + 1);
1.54 nicm 457:
458: screen_write_stop(&ctx);
459: }
460:
461: void
1.149 nicm 462: window_copy_pageup(struct window_pane *wp, int half_page)
1.1 nicm 463: {
1.211 nicm 464: window_copy_pageup1(TAILQ_FIRST(&wp->modes), half_page);
1.208 nicm 465: }
466:
467: static void
468: window_copy_pageup1(struct window_mode_entry *wme, int half_page)
469: {
470: struct window_copy_mode_data *data = wme->data;
1.1 nicm 471: struct screen *s = &data->screen;
1.162 nicm 472: u_int n, ox, oy, px, py;
1.147 nicm 473:
474: oy = screen_hsize(data->backing) + data->cy - data->oy;
1.208 nicm 475: ox = window_copy_find_length(wme, oy);
1.147 nicm 476:
477: if (data->cx != ox) {
478: data->lastcx = data->cx;
479: data->lastsx = ox;
480: }
481: data->cx = data->lastcx;
1.1 nicm 482:
1.19 nicm 483: n = 1;
1.149 nicm 484: if (screen_size_y(s) > 2) {
485: if (half_page)
486: n = screen_size_y(s) / 2;
487: else
488: n = screen_size_y(s) - 2;
489: }
1.147 nicm 490:
1.195 nicm 491: if (data->oy + n > screen_hsize(data->backing)) {
1.54 nicm 492: data->oy = screen_hsize(data->backing);
1.195 nicm 493: if (data->cy < n)
494: data->cy = 0;
495: else
496: data->cy -= n;
497: } else
1.19 nicm 498: data->oy += n;
1.147 nicm 499:
1.192 nicm 500: if (data->screen.sel == NULL || !data->rectflag) {
1.162 nicm 501: py = screen_hsize(data->backing) + data->cy - data->oy;
1.208 nicm 502: px = window_copy_find_length(wme, py);
1.162 nicm 503: if ((data->cx >= data->lastsx && data->cx != px) ||
504: data->cx > px)
1.208 nicm 505: window_copy_cursor_end_of_line(wme);
1.147 nicm 506: }
507:
1.252 nicm 508: window_copy_update_selection(wme, 1, 0);
1.208 nicm 509: window_copy_redraw_screen(wme);
1.147 nicm 510: }
511:
1.166 nicm 512: static int
1.208 nicm 513: window_copy_pagedown(struct window_mode_entry *wme, int half_page,
514: int scroll_exit)
1.147 nicm 515: {
1.208 nicm 516: struct window_copy_mode_data *data = wme->data;
1.147 nicm 517: struct screen *s = &data->screen;
1.161 nicm 518: u_int n, ox, oy, px, py;
1.147 nicm 519:
520: oy = screen_hsize(data->backing) + data->cy - data->oy;
1.208 nicm 521: ox = window_copy_find_length(wme, oy);
1.147 nicm 522:
523: if (data->cx != ox) {
524: data->lastcx = data->cx;
525: data->lastsx = ox;
526: }
527: data->cx = data->lastcx;
528:
529: n = 1;
1.149 nicm 530: if (screen_size_y(s) > 2) {
531: if (half_page)
532: n = screen_size_y(s) / 2;
533: else
534: n = screen_size_y(s) - 2;
535: }
1.147 nicm 536:
1.195 nicm 537: if (data->oy < n) {
1.147 nicm 538: data->oy = 0;
1.195 nicm 539: if (data->cy + (n - data->oy) >= screen_size_y(data->backing))
540: data->cy = screen_size_y(data->backing) - 1;
541: else
542: data->cy += n - data->oy;
543: } else
1.147 nicm 544: data->oy -= n;
545:
1.192 nicm 546: if (data->screen.sel == NULL || !data->rectflag) {
1.161 nicm 547: py = screen_hsize(data->backing) + data->cy - data->oy;
1.208 nicm 548: px = window_copy_find_length(wme, py);
1.161 nicm 549: if ((data->cx >= data->lastsx && data->cx != px) ||
550: data->cx > px)
1.208 nicm 551: window_copy_cursor_end_of_line(wme);
1.147 nicm 552: }
553:
1.186 nicm 554: if (scroll_exit && data->oy == 0)
1.166 nicm 555: return (1);
1.252 nicm 556: window_copy_update_selection(wme, 1, 0);
1.208 nicm 557: window_copy_redraw_screen(wme);
1.166 nicm 558: return (0);
1.1 nicm 559: }
560:
1.157 nicm 561: static void
1.208 nicm 562: window_copy_previous_paragraph(struct window_mode_entry *wme)
1.148 nicm 563: {
1.208 nicm 564: struct window_copy_mode_data *data = wme->data;
1.151 nicm 565: u_int oy;
1.148 nicm 566:
567: oy = screen_hsize(data->backing) + data->cy - data->oy;
568:
1.208 nicm 569: while (oy > 0 && window_copy_find_length(wme, oy) == 0)
1.148 nicm 570: oy--;
571:
1.208 nicm 572: while (oy > 0 && window_copy_find_length(wme, oy) > 0)
1.148 nicm 573: oy--;
574:
1.208 nicm 575: window_copy_scroll_to(wme, 0, oy);
1.148 nicm 576: }
577:
1.157 nicm 578: static void
1.208 nicm 579: window_copy_next_paragraph(struct window_mode_entry *wme)
1.148 nicm 580: {
1.208 nicm 581: struct window_copy_mode_data *data = wme->data;
1.148 nicm 582: struct screen *s = &data->screen;
583: u_int maxy, ox, oy;
584:
585: oy = screen_hsize(data->backing) + data->cy - data->oy;
586: maxy = screen_hsize(data->backing) + screen_size_y(s) - 1;
587:
1.208 nicm 588: while (oy < maxy && window_copy_find_length(wme, oy) == 0)
1.148 nicm 589: oy++;
590:
1.208 nicm 591: while (oy < maxy && window_copy_find_length(wme, oy) > 0)
1.148 nicm 592: oy++;
593:
1.208 nicm 594: ox = window_copy_find_length(wme, oy);
595: window_copy_scroll_to(wme, ox, oy);
1.148 nicm 596: }
597:
1.256 nicm 598: char *
599: window_copy_get_word(struct window_pane *wp, u_int x, u_int y)
600: {
601: struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
602: struct window_copy_mode_data *data = wme->data;
603: struct grid *gd = data->screen.grid;
604:
605: return (format_grid_word(gd, x, gd->hsize + y));
606: }
607:
608: char *
609: window_copy_get_line(struct window_pane *wp, u_int y)
610: {
611: struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
612: struct window_copy_mode_data *data = wme->data;
613: struct grid *gd = data->screen.grid;
614:
615: return (format_grid_line(gd, gd->hsize + y));
616: }
617:
1.157 nicm 618: static void
1.208 nicm 619: window_copy_formats(struct window_mode_entry *wme, struct format_tree *ft)
1.206 nicm 620: {
1.208 nicm 621: struct window_copy_mode_data *data = wme->data;
1.256 nicm 622: struct grid *gd = data->screen.grid;
1.237 nicm 623: char *s;
1.206 nicm 624:
625: format_add(ft, "scroll_position", "%d", data->oy);
626: format_add(ft, "rectangle_toggle", "%d", data->rectflag);
1.236 nicm 627:
628: format_add(ft, "copy_cursor_x", "%d", data->cx);
629: format_add(ft, "copy_cursor_y", "%d", data->cy);
630:
631: format_add(ft, "selection_present", "%d", data->screen.sel != NULL);
632: if (data->screen.sel != NULL) {
633: format_add(ft, "selection_start_x", "%d", data->selx);
634: format_add(ft, "selection_start_y", "%d", data->sely);
635: format_add(ft, "selection_end_x", "%d", data->endselx);
636: format_add(ft, "selection_end_y", "%d", data->endsely);
1.246 nicm 637: format_add(ft, "selection_active", "%d",
638: data->cursordrag != CURSORDRAG_NONE);
639: } else
640: format_add(ft, "selection_active", "%d", 0);
1.237 nicm 641:
1.256 nicm 642: s = format_grid_word(gd, data->cx, gd->hsize + data->cy);
1.237 nicm 643: if (s != NULL) {
644: format_add(ft, "copy_cursor_word", "%s", s);
645: free(s);
646: }
647:
1.256 nicm 648: s = format_grid_line(gd, gd->hsize + data->cy);
1.237 nicm 649: if (s != NULL) {
650: format_add(ft, "copy_cursor_line", "%s", s);
651: free(s);
1.236 nicm 652: }
1.206 nicm 653: }
654:
655: static void
1.208 nicm 656: window_copy_resize(struct window_mode_entry *wme, u_int sx, u_int sy)
1.1 nicm 657: {
1.208 nicm 658: struct window_pane *wp = wme->wp;
659: struct window_copy_mode_data *data = wme->data;
1.1 nicm 660: struct screen *s = &data->screen;
661: struct screen_write_ctx ctx;
1.228 nicm 662: int search;
1.1 nicm 663:
1.119 nicm 664: screen_resize(s, sx, sy, 1);
1.54 nicm 665: if (data->backing != &wp->base)
1.119 nicm 666: screen_resize(data->backing, sx, sy, 1);
1.35 nicm 667:
1.18 nicm 668: if (data->cy > sy - 1)
669: data->cy = sy - 1;
670: if (data->cx > sx)
671: data->cx = sx;
1.63 nicm 672: if (data->oy > screen_hsize(data->backing))
673: data->oy = screen_hsize(data->backing);
1.18 nicm 674:
1.228 nicm 675: search = (data->searchmark != NULL);
1.208 nicm 676: window_copy_clear_selection(wme);
1.228 nicm 677: window_copy_clear_marks(wme);
1.18 nicm 678:
1.1 nicm 679: screen_write_start(&ctx, NULL, s);
1.208 nicm 680: window_copy_write_lines(wme, &ctx, 0, screen_size_y(s) - 1);
1.1 nicm 681: screen_write_stop(&ctx);
1.18 nicm 682:
1.228 nicm 683: if (search)
1.244 nicm 684: window_copy_search_marks(wme, NULL, 1);
1.163 nicm 685: data->searchx = data->cx;
686: data->searchy = data->cy;
687: data->searcho = data->oy;
1.162 nicm 688:
1.208 nicm 689: window_copy_redraw_screen(wme);
1.1 nicm 690: }
691:
1.157 nicm 692: static const char *
1.208 nicm 693: window_copy_key_table(struct window_mode_entry *wme)
1.155 nicm 694: {
1.208 nicm 695: struct window_pane *wp = wme->wp;
696:
1.155 nicm 697: if (options_get_number(wp->window->options, "mode-keys") == MODEKEY_VI)
698: return ("copy-mode-vi");
699: return ("copy-mode");
700: }
701:
1.259 nicm 702: static int
703: window_copy_expand_search_string(struct window_copy_cmd_state *cs)
704: {
705: struct window_mode_entry *wme = cs->wme;
706: struct window_copy_mode_data *data = wme->data;
707: const char *argument;
708: char *expanded;
709:
710: if (cs->args->argc == 2) {
711: argument = cs->args->argv[1];
712: if (*argument != '\0') {
713: if (args_has(cs->args, 'F')) {
714: expanded = format_single(NULL, argument, NULL,
715: NULL, NULL, wme->wp);
716: if (*expanded == '\0') {
717: free(expanded);
718: return (0);
719: }
720: free(data->searchstr);
721: data->searchstr = expanded;
722: } else {
723: free(data->searchstr);
724: data->searchstr = xstrdup(argument);
725: }
726: }
727: }
728: return (1);
729: }
730:
1.213 nicm 731: static enum window_copy_cmd_action
732: window_copy_cmd_append_selection(struct window_copy_cmd_state *cs)
733: {
734: struct window_mode_entry *wme = cs->wme;
735: struct session *s = cs->s;
736:
737: if (s != NULL)
738: window_copy_append_selection(wme);
739: window_copy_clear_selection(wme);
740: return (WINDOW_COPY_CMD_REDRAW);
741: }
742:
743: static enum window_copy_cmd_action
744: window_copy_cmd_append_selection_and_cancel(struct window_copy_cmd_state *cs)
745: {
746: struct window_mode_entry *wme = cs->wme;
747: struct session *s = cs->s;
748:
749: if (s != NULL)
750: window_copy_append_selection(wme);
751: window_copy_clear_selection(wme);
752: return (WINDOW_COPY_CMD_CANCEL);
753: }
754:
755: static enum window_copy_cmd_action
756: window_copy_cmd_back_to_indentation(struct window_copy_cmd_state *cs)
757: {
758: struct window_mode_entry *wme = cs->wme;
759:
760: window_copy_cursor_back_to_indentation(wme);
761: return (WINDOW_COPY_CMD_NOTHING);
762: }
763:
764: static enum window_copy_cmd_action
765: window_copy_cmd_begin_selection(struct window_copy_cmd_state *cs)
766: {
767: struct window_mode_entry *wme = cs->wme;
768: struct client *c = cs->c;
769: struct mouse_event *m = cs->m;
770: struct window_copy_mode_data *data = wme->data;
771:
772: if (m != NULL) {
773: window_copy_start_drag(c, m);
774: return (WINDOW_COPY_CMD_NOTHING);
775: }
776:
777: data->lineflag = LINE_SEL_NONE;
1.252 nicm 778: data->selflag = SEL_CHAR;
1.213 nicm 779: window_copy_start_selection(wme);
780: return (WINDOW_COPY_CMD_REDRAW);
781: }
782:
783: static enum window_copy_cmd_action
784: window_copy_cmd_stop_selection(struct window_copy_cmd_state *cs)
785: {
786: struct window_mode_entry *wme = cs->wme;
787: struct window_copy_mode_data *data = wme->data;
788:
789: data->cursordrag = CURSORDRAG_NONE;
1.214 nicm 790: data->lineflag = LINE_SEL_NONE;
1.250 nicm 791: data->selflag = SEL_CHAR;
1.213 nicm 792: return (WINDOW_COPY_CMD_NOTHING);
793: }
794:
795: static enum window_copy_cmd_action
796: window_copy_cmd_bottom_line(struct window_copy_cmd_state *cs)
797: {
798: struct window_mode_entry *wme = cs->wme;
799: struct window_copy_mode_data *data = wme->data;
800:
801: data->cx = 0;
802: data->cy = screen_size_y(&data->screen) - 1;
803:
1.252 nicm 804: window_copy_update_selection(wme, 1, 0);
1.213 nicm 805: return (WINDOW_COPY_CMD_REDRAW);
806: }
807:
808: static enum window_copy_cmd_action
809: window_copy_cmd_cancel(__unused struct window_copy_cmd_state *cs)
810: {
811: return (WINDOW_COPY_CMD_CANCEL);
812: }
813:
814: static enum window_copy_cmd_action
815: window_copy_cmd_clear_selection(struct window_copy_cmd_state *cs)
816: {
817: struct window_mode_entry *wme = cs->wme;
818:
819: window_copy_clear_selection(wme);
820: return (WINDOW_COPY_CMD_REDRAW);
821: }
822:
823: static enum window_copy_cmd_action
824: window_copy_cmd_copy_end_of_line(struct window_copy_cmd_state *cs)
825: {
826: struct window_mode_entry *wme = cs->wme;
1.215 nicm 827: struct client *c = cs->c;
1.213 nicm 828: struct session *s = cs->s;
1.215 nicm 829: struct winlink *wl = cs->wl;
830: struct window_pane *wp = wme->wp;
1.213 nicm 831: u_int np = wme->prefix;
1.215 nicm 832: char *prefix = NULL;
833:
834: if (cs->args->argc == 2)
835: prefix = format_single(NULL, cs->args->argv[1], c, s, wl, wp);
1.213 nicm 836:
837: window_copy_start_selection(wme);
838: for (; np > 1; np--)
839: window_copy_cursor_down(wme, 0);
840: window_copy_cursor_end_of_line(wme);
841:
842: if (s != NULL) {
1.215 nicm 843: window_copy_copy_selection(wme, prefix);
844:
845: free(prefix);
1.213 nicm 846: return (WINDOW_COPY_CMD_CANCEL);
847: }
1.215 nicm 848:
849: free(prefix);
1.213 nicm 850: return (WINDOW_COPY_CMD_REDRAW);
851: }
852:
853: static enum window_copy_cmd_action
854: window_copy_cmd_copy_line(struct window_copy_cmd_state *cs)
855: {
856: struct window_mode_entry *wme = cs->wme;
1.215 nicm 857: struct client *c = cs->c;
1.213 nicm 858: struct session *s = cs->s;
1.215 nicm 859: struct winlink *wl = cs->wl;
860: struct window_pane *wp = wme->wp;
1.252 nicm 861: struct window_copy_mode_data *data = wme->data;
1.213 nicm 862: u_int np = wme->prefix;
1.215 nicm 863: char *prefix = NULL;
864:
865: if (cs->args->argc == 2)
866: prefix = format_single(NULL, cs->args->argv[1], c, s, wl, wp);
1.213 nicm 867:
1.252 nicm 868: data->selflag = SEL_CHAR;
1.213 nicm 869: window_copy_cursor_start_of_line(wme);
870: window_copy_start_selection(wme);
871: for (; np > 1; np--)
872: window_copy_cursor_down(wme, 0);
873: window_copy_cursor_end_of_line(wme);
874:
875: if (s != NULL) {
1.215 nicm 876: window_copy_copy_selection(wme, prefix);
877:
878: free(prefix);
1.213 nicm 879: return (WINDOW_COPY_CMD_CANCEL);
880: }
1.215 nicm 881:
882: free(prefix);
1.213 nicm 883: return (WINDOW_COPY_CMD_REDRAW);
884: }
885:
886: static enum window_copy_cmd_action
1.216 nicm 887: window_copy_cmd_copy_selection_no_clear(struct window_copy_cmd_state *cs)
1.213 nicm 888: {
889: struct window_mode_entry *wme = cs->wme;
1.215 nicm 890: struct client *c = cs->c;
1.213 nicm 891: struct session *s = cs->s;
1.215 nicm 892: struct winlink *wl = cs->wl;
893: struct window_pane *wp = wme->wp;
894: char *prefix = NULL;
895:
896: if (cs->args->argc == 2)
897: prefix = format_single(NULL, cs->args->argv[1], c, s, wl, wp);
1.213 nicm 898:
899: if (s != NULL)
1.215 nicm 900: window_copy_copy_selection(wme, prefix);
901:
902: free(prefix);
1.216 nicm 903: return (WINDOW_COPY_CMD_NOTHING);
904: }
905:
906: static enum window_copy_cmd_action
907: window_copy_cmd_copy_selection(struct window_copy_cmd_state *cs)
908: {
909: struct window_mode_entry *wme = cs->wme;
910:
911: window_copy_cmd_copy_selection_no_clear(cs);
912: window_copy_clear_selection(wme);
1.213 nicm 913: return (WINDOW_COPY_CMD_REDRAW);
914: }
915:
916: static enum window_copy_cmd_action
917: window_copy_cmd_copy_selection_and_cancel(struct window_copy_cmd_state *cs)
918: {
919: struct window_mode_entry *wme = cs->wme;
1.215 nicm 920:
1.216 nicm 921: window_copy_cmd_copy_selection_no_clear(cs);
1.213 nicm 922: window_copy_clear_selection(wme);
923: return (WINDOW_COPY_CMD_CANCEL);
924: }
925:
926: static enum window_copy_cmd_action
927: window_copy_cmd_cursor_down(struct window_copy_cmd_state *cs)
928: {
929: struct window_mode_entry *wme = cs->wme;
930: u_int np = wme->prefix;
931:
932: for (; np != 0; np--)
933: window_copy_cursor_down(wme, 0);
934: return (WINDOW_COPY_CMD_NOTHING);
935: }
936:
937: static enum window_copy_cmd_action
1.234 nicm 938: window_copy_cmd_cursor_down_and_cancel(struct window_copy_cmd_state *cs)
939: {
940: struct window_mode_entry *wme = cs->wme;
941: struct window_copy_mode_data *data = wme->data;
942: u_int np = wme->prefix, cy;
943:
944: cy = data->cy;
945: for (; np != 0; np--)
946: window_copy_cursor_down(wme, 0);
947: if (cy == data->cy && data->oy == 0)
948: return (WINDOW_COPY_CMD_CANCEL);
949: return (WINDOW_COPY_CMD_NOTHING);
950: }
951:
952: static enum window_copy_cmd_action
1.213 nicm 953: window_copy_cmd_cursor_left(struct window_copy_cmd_state *cs)
954: {
955: struct window_mode_entry *wme = cs->wme;
956: u_int np = wme->prefix;
957:
958: for (; np != 0; np--)
959: window_copy_cursor_left(wme);
960: return (WINDOW_COPY_CMD_NOTHING);
961: }
962:
963: static enum window_copy_cmd_action
964: window_copy_cmd_cursor_right(struct window_copy_cmd_state *cs)
965: {
966: struct window_mode_entry *wme = cs->wme;
967: u_int np = wme->prefix;
968:
969: for (; np != 0; np--)
970: window_copy_cursor_right(wme);
971: return (WINDOW_COPY_CMD_NOTHING);
972: }
973:
974: static enum window_copy_cmd_action
975: window_copy_cmd_cursor_up(struct window_copy_cmd_state *cs)
976: {
977: struct window_mode_entry *wme = cs->wme;
978: u_int np = wme->prefix;
979:
980: for (; np != 0; np--)
981: window_copy_cursor_up(wme, 0);
982: return (WINDOW_COPY_CMD_NOTHING);
983: }
984:
985: static enum window_copy_cmd_action
986: window_copy_cmd_end_of_line(struct window_copy_cmd_state *cs)
987: {
988: struct window_mode_entry *wme = cs->wme;
989:
990: window_copy_cursor_end_of_line(wme);
991: return (WINDOW_COPY_CMD_NOTHING);
992: }
993:
994: static enum window_copy_cmd_action
995: window_copy_cmd_halfpage_down(struct window_copy_cmd_state *cs)
996: {
997: struct window_mode_entry *wme = cs->wme;
998: struct window_copy_mode_data *data = wme->data;
999: u_int np = wme->prefix;
1000:
1001: for (; np != 0; np--) {
1002: if (window_copy_pagedown(wme, 1, data->scroll_exit))
1003: return (WINDOW_COPY_CMD_CANCEL);
1004: }
1005: return (WINDOW_COPY_CMD_NOTHING);
1006: }
1007:
1008: static enum window_copy_cmd_action
1009: window_copy_cmd_halfpage_down_and_cancel(struct window_copy_cmd_state *cs)
1010: {
1011:
1012: struct window_mode_entry *wme = cs->wme;
1013: u_int np = wme->prefix;
1014:
1015: for (; np != 0; np--) {
1016: if (window_copy_pagedown(wme, 1, 1))
1017: return (WINDOW_COPY_CMD_CANCEL);
1018: }
1019: return (WINDOW_COPY_CMD_NOTHING);
1020: }
1021:
1022: static enum window_copy_cmd_action
1023: window_copy_cmd_halfpage_up(struct window_copy_cmd_state *cs)
1024: {
1025: struct window_mode_entry *wme = cs->wme;
1026: u_int np = wme->prefix;
1027:
1028: for (; np != 0; np--)
1029: window_copy_pageup1(wme, 1);
1030: return (WINDOW_COPY_CMD_NOTHING);
1031: }
1032:
1033: static enum window_copy_cmd_action
1034: window_copy_cmd_history_bottom(struct window_copy_cmd_state *cs)
1035: {
1036: struct window_mode_entry *wme = cs->wme;
1037: struct window_copy_mode_data *data = wme->data;
1.221 nicm 1038: u_int oy;
1039:
1040: oy = screen_hsize(data->backing) + data->cy - data->oy;
1041: if (data->lineflag == LINE_SEL_RIGHT_LEFT && oy == data->endsely)
1042: window_copy_other_end(wme);
1.213 nicm 1043:
1044: data->cy = screen_size_y(&data->screen) - 1;
1.221 nicm 1045: data->cx = window_copy_find_length(wme, data->cy);
1.213 nicm 1046: data->oy = 0;
1047:
1.252 nicm 1048: window_copy_update_selection(wme, 1, 0);
1.213 nicm 1049: return (WINDOW_COPY_CMD_REDRAW);
1050: }
1051:
1052: static enum window_copy_cmd_action
1053: window_copy_cmd_history_top(struct window_copy_cmd_state *cs)
1054: {
1055: struct window_mode_entry *wme = cs->wme;
1056: struct window_copy_mode_data *data = wme->data;
1.221 nicm 1057: u_int oy;
1.213 nicm 1058:
1.221 nicm 1059: oy = screen_hsize(data->backing) + data->cy - data->oy;
1060: if (data->lineflag == LINE_SEL_LEFT_RIGHT && oy == data->sely)
1061: window_copy_other_end(wme);
1062:
1063: data->cy = 0;
1.213 nicm 1064: data->cx = 0;
1065: data->oy = screen_hsize(data->backing);
1066:
1.252 nicm 1067: window_copy_update_selection(wme, 1, 0);
1.213 nicm 1068: return (WINDOW_COPY_CMD_REDRAW);
1069: }
1070:
1071: static enum window_copy_cmd_action
1072: window_copy_cmd_jump_again(struct window_copy_cmd_state *cs)
1073: {
1074: struct window_mode_entry *wme = cs->wme;
1075: struct window_copy_mode_data *data = wme->data;
1076: u_int np = wme->prefix;
1077:
1078: switch (data->jumptype) {
1079: case WINDOW_COPY_JUMPFORWARD:
1080: for (; np != 0; np--)
1081: window_copy_cursor_jump(wme);
1082: break;
1083: case WINDOW_COPY_JUMPBACKWARD:
1084: for (; np != 0; np--)
1085: window_copy_cursor_jump_back(wme);
1086: break;
1087: case WINDOW_COPY_JUMPTOFORWARD:
1088: for (; np != 0; np--)
1089: window_copy_cursor_jump_to(wme);
1090: break;
1091: case WINDOW_COPY_JUMPTOBACKWARD:
1092: for (; np != 0; np--)
1093: window_copy_cursor_jump_to_back(wme);
1094: break;
1095: }
1096: return (WINDOW_COPY_CMD_NOTHING);
1097: }
1098:
1099: static enum window_copy_cmd_action
1100: window_copy_cmd_jump_reverse(struct window_copy_cmd_state *cs)
1101: {
1102: struct window_mode_entry *wme = cs->wme;
1103: struct window_copy_mode_data *data = wme->data;
1104: u_int np = wme->prefix;
1105:
1106: switch (data->jumptype) {
1107: case WINDOW_COPY_JUMPFORWARD:
1108: for (; np != 0; np--)
1109: window_copy_cursor_jump_back(wme);
1110: break;
1111: case WINDOW_COPY_JUMPBACKWARD:
1112: for (; np != 0; np--)
1113: window_copy_cursor_jump(wme);
1114: break;
1115: case WINDOW_COPY_JUMPTOFORWARD:
1116: for (; np != 0; np--)
1117: window_copy_cursor_jump_to_back(wme);
1118: break;
1119: case WINDOW_COPY_JUMPTOBACKWARD:
1120: for (; np != 0; np--)
1121: window_copy_cursor_jump_to(wme);
1122: break;
1123: }
1124: return (WINDOW_COPY_CMD_NOTHING);
1125: }
1126:
1127: static enum window_copy_cmd_action
1128: window_copy_cmd_middle_line(struct window_copy_cmd_state *cs)
1129: {
1130: struct window_mode_entry *wme = cs->wme;
1131: struct window_copy_mode_data *data = wme->data;
1132:
1133: data->cx = 0;
1134: data->cy = (screen_size_y(&data->screen) - 1) / 2;
1135:
1.252 nicm 1136: window_copy_update_selection(wme, 1, 0);
1.213 nicm 1137: return (WINDOW_COPY_CMD_REDRAW);
1138: }
1139:
1140: static enum window_copy_cmd_action
1.218 nicm 1141: window_copy_cmd_previous_matching_bracket(struct window_copy_cmd_state *cs)
1142: {
1143: struct window_mode_entry *wme = cs->wme;
1144: u_int np = wme->prefix;
1145: struct window_copy_mode_data *data = wme->data;
1146: struct screen *s = data->backing;
1147: char open[] = "{[(", close[] = "}])";
1148: char tried, found, start, *cp;
1.219 nicm 1149: u_int px, py, xx, n;
1.218 nicm 1150: struct grid_cell gc;
1151: int failed;
1152:
1153: for (; np != 0; np--) {
1154: /* Get cursor position and line length. */
1155: px = data->cx;
1156: py = screen_hsize(s) + data->cy - data->oy;
1157: xx = window_copy_find_length(wme, py);
1158: if (xx == 0)
1159: break;
1160:
1161: /*
1162: * Get the current character. If not on a bracket, try the
1163: * previous. If still not, then behave like previous-word.
1164: */
1165: tried = 0;
1166: retry:
1167: grid_get_cell(s->grid, px, py, &gc);
1168: if (gc.data.size != 1 || (gc.flags & GRID_FLAG_PADDING))
1169: cp = NULL;
1170: else {
1171: found = *gc.data.data;
1172: cp = strchr(close, found);
1173: }
1174: if (cp == NULL) {
1175: if (data->modekeys == MODEKEY_EMACS) {
1176: if (!tried && px > 0) {
1177: px--;
1178: tried = 1;
1179: goto retry;
1180: }
1.229 nicm 1181: window_copy_cursor_previous_word(wme, "}]) ", 1);
1.218 nicm 1182: }
1183: continue;
1184: }
1185: start = open[cp - close];
1186:
1187: /* Walk backward until the matching bracket is reached. */
1188: n = 1;
1189: failed = 0;
1190: do {
1191: if (px == 0) {
1192: if (py == 0) {
1193: failed = 1;
1194: break;
1195: }
1196: do {
1197: py--;
1198: xx = window_copy_find_length(wme, py);
1199: } while (xx == 0 && py > 0);
1200: if (xx == 0 && py == 0) {
1201: failed = 1;
1202: break;
1203: }
1204: px = xx - 1;
1205: } else
1206: px--;
1207:
1208: grid_get_cell(s->grid, px, py, &gc);
1209: if (gc.data.size == 1 &&
1210: (~gc.flags & GRID_FLAG_PADDING)) {
1211: if (*gc.data.data == found)
1212: n++;
1213: else if (*gc.data.data == start)
1214: n--;
1215: }
1216: } while (n != 0);
1217:
1218: /* Move the cursor to the found location if any. */
1219: if (!failed)
1220: window_copy_scroll_to(wme, px, py);
1221: }
1222:
1223: return (WINDOW_COPY_CMD_NOTHING);
1224: }
1225:
1226: static enum window_copy_cmd_action
1227: window_copy_cmd_next_matching_bracket(struct window_copy_cmd_state *cs)
1228: {
1229: struct window_mode_entry *wme = cs->wme;
1230: u_int np = wme->prefix;
1231: struct window_copy_mode_data *data = wme->data;
1232: struct screen *s = data->backing;
1233: char open[] = "{[(", close[] = "}])";
1234: char tried, found, end, *cp;
1235: u_int px, py, xx, yy, sx, sy, n;
1236: struct grid_cell gc;
1237: int failed;
1238: struct grid_line *gl;
1239:
1240: for (; np != 0; np--) {
1241: /* Get cursor position and line length. */
1242: px = data->cx;
1243: py = screen_hsize(s) + data->cy - data->oy;
1244: xx = window_copy_find_length(wme, py);
1245: yy = screen_hsize(s) + screen_size_y(s) - 1;
1246: if (xx == 0)
1247: break;
1248:
1249: /*
1250: * Get the current character. If not on a bracket, try the
1251: * next. If still not, then behave like next-word.
1252: */
1253: tried = 0;
1254: retry:
1255: grid_get_cell(s->grid, px, py, &gc);
1256: if (gc.data.size != 1 || (gc.flags & GRID_FLAG_PADDING))
1257: cp = NULL;
1258: else {
1259: found = *gc.data.data;
1260:
1261: /*
1262: * In vi mode, attempt to move to previous bracket if a
1263: * closing bracket is found first. If this fails,
1264: * return to the original cursor position.
1265: */
1266: cp = strchr(close, found);
1267: if (cp != NULL && data->modekeys == MODEKEY_VI) {
1268: sx = data->cx;
1269: sy = screen_hsize(s) + data->cy - data->oy;
1270:
1271: window_copy_scroll_to(wme, px, py);
1272: window_copy_cmd_previous_matching_bracket(cs);
1273:
1274: px = data->cx;
1275: py = screen_hsize(s) + data->cy - data->oy;
1276: grid_get_cell(s->grid, px, py, &gc);
1277: if (gc.data.size != 1 ||
1278: (gc.flags & GRID_FLAG_PADDING) ||
1279: strchr(close, *gc.data.data) == NULL)
1280: window_copy_scroll_to(wme, sx, sy);
1281: break;
1282: }
1283:
1284: cp = strchr(open, found);
1285: }
1286: if (cp == NULL) {
1287: if (data->modekeys == MODEKEY_EMACS) {
1288: if (!tried && px <= xx) {
1289: px++;
1290: tried = 1;
1291: goto retry;
1292: }
1.255 nicm 1293: window_copy_cursor_next_word_end(wme, "{[( ",
1294: 0);
1.218 nicm 1295: continue;
1296: }
1297: /* For vi, continue searching for bracket until EOL. */
1298: if (px > xx) {
1299: if (py == yy)
1300: continue;
1301: gl = grid_get_line(s->grid, py);
1302: if (~gl->flags & GRID_LINE_WRAPPED)
1303: continue;
1304: if (gl->cellsize > s->grid->sx)
1305: continue;
1306: px = 0;
1307: py++;
1308: xx = window_copy_find_length(wme, py);
1309: } else
1310: px++;
1311: goto retry;
1312: }
1313: end = close[cp - open];
1314:
1315: /* Walk forward until the matching bracket is reached. */
1316: n = 1;
1317: failed = 0;
1318: do {
1319: if (px > xx) {
1320: if (py == yy) {
1321: failed = 1;
1322: break;
1323: }
1324: px = 0;
1325: py++;
1326: xx = window_copy_find_length(wme, py);
1327: } else
1328: px++;
1329:
1330: grid_get_cell(s->grid, px, py, &gc);
1331: if (gc.data.size == 1 &&
1332: (~gc.flags & GRID_FLAG_PADDING)) {
1333: if (*gc.data.data == found)
1334: n++;
1335: else if (*gc.data.data == end)
1336: n--;
1337: }
1338: } while (n != 0);
1339:
1340: /* Move the cursor to the found location if any. */
1341: if (!failed)
1342: window_copy_scroll_to(wme, px, py);
1343: }
1344:
1345: return (WINDOW_COPY_CMD_NOTHING);
1346: }
1347:
1348: static enum window_copy_cmd_action
1.213 nicm 1349: window_copy_cmd_next_paragraph(struct window_copy_cmd_state *cs)
1350: {
1351: struct window_mode_entry *wme = cs->wme;
1352: u_int np = wme->prefix;
1353:
1354: for (; np != 0; np--)
1355: window_copy_next_paragraph(wme);
1356: return (WINDOW_COPY_CMD_NOTHING);
1357: }
1358:
1359: static enum window_copy_cmd_action
1360: window_copy_cmd_next_space(struct window_copy_cmd_state *cs)
1361: {
1362: struct window_mode_entry *wme = cs->wme;
1363: u_int np = wme->prefix;
1364:
1365: for (; np != 0; np--)
1366: window_copy_cursor_next_word(wme, " ");
1367: return (WINDOW_COPY_CMD_NOTHING);
1368: }
1369:
1370: static enum window_copy_cmd_action
1371: window_copy_cmd_next_space_end(struct window_copy_cmd_state *cs)
1372: {
1373: struct window_mode_entry *wme = cs->wme;
1374: u_int np = wme->prefix;
1375:
1376: for (; np != 0; np--)
1.255 nicm 1377: window_copy_cursor_next_word_end(wme, " ", 0);
1.213 nicm 1378: return (WINDOW_COPY_CMD_NOTHING);
1379: }
1380:
1381: static enum window_copy_cmd_action
1382: window_copy_cmd_next_word(struct window_copy_cmd_state *cs)
1383: {
1384: struct window_mode_entry *wme = cs->wme;
1385: struct session *s = cs->s;
1386: u_int np = wme->prefix;
1387: const char *ws;
1388:
1389: ws = options_get_string(s->options, "word-separators");
1390: for (; np != 0; np--)
1391: window_copy_cursor_next_word(wme, ws);
1392: return (WINDOW_COPY_CMD_NOTHING);
1393: }
1394:
1395: static enum window_copy_cmd_action
1396: window_copy_cmd_next_word_end(struct window_copy_cmd_state *cs)
1397: {
1398: struct window_mode_entry *wme = cs->wme;
1399: struct session *s = cs->s;
1400: u_int np = wme->prefix;
1401: const char *ws;
1402:
1403: ws = options_get_string(s->options, "word-separators");
1404: for (; np != 0; np--)
1.255 nicm 1405: window_copy_cursor_next_word_end(wme, ws, 0);
1.213 nicm 1406: return (WINDOW_COPY_CMD_NOTHING);
1407: }
1408:
1409: static enum window_copy_cmd_action
1410: window_copy_cmd_other_end(struct window_copy_cmd_state *cs)
1411: {
1412: struct window_mode_entry *wme = cs->wme;
1413: u_int np = wme->prefix;
1.254 nicm 1414: struct window_copy_mode_data *data = wme->data;
1.213 nicm 1415:
1.254 nicm 1416: data->selflag = SEL_CHAR;
1.213 nicm 1417: if ((np % 2) != 0)
1418: window_copy_other_end(wme);
1419: return (WINDOW_COPY_CMD_NOTHING);
1420: }
1421:
1422: static enum window_copy_cmd_action
1423: window_copy_cmd_page_down(struct window_copy_cmd_state *cs)
1424: {
1425: struct window_mode_entry *wme = cs->wme;
1426: struct window_copy_mode_data *data = wme->data;
1427: u_int np = wme->prefix;
1428:
1429: for (; np != 0; np--) {
1430: if (window_copy_pagedown(wme, 0, data->scroll_exit))
1431: return (WINDOW_COPY_CMD_CANCEL);
1432: }
1433: return (WINDOW_COPY_CMD_NOTHING);
1434: }
1435:
1436: static enum window_copy_cmd_action
1437: window_copy_cmd_page_down_and_cancel(struct window_copy_cmd_state *cs)
1438: {
1439: struct window_mode_entry *wme = cs->wme;
1440: u_int np = wme->prefix;
1441:
1442: for (; np != 0; np--) {
1443: if (window_copy_pagedown(wme, 0, 1))
1444: return (WINDOW_COPY_CMD_CANCEL);
1445: }
1446: return (WINDOW_COPY_CMD_NOTHING);
1447: }
1448:
1449: static enum window_copy_cmd_action
1450: window_copy_cmd_page_up(struct window_copy_cmd_state *cs)
1451: {
1452: struct window_mode_entry *wme = cs->wme;
1453: u_int np = wme->prefix;
1454:
1455: for (; np != 0; np--)
1456: window_copy_pageup1(wme, 0);
1457: return (WINDOW_COPY_CMD_NOTHING);
1458: }
1459:
1460: static enum window_copy_cmd_action
1461: window_copy_cmd_previous_paragraph(struct window_copy_cmd_state *cs)
1462: {
1463: struct window_mode_entry *wme = cs->wme;
1464: u_int np = wme->prefix;
1465:
1466: for (; np != 0; np--)
1467: window_copy_previous_paragraph(wme);
1468: return (WINDOW_COPY_CMD_NOTHING);
1469: }
1470:
1471: static enum window_copy_cmd_action
1472: window_copy_cmd_previous_space(struct window_copy_cmd_state *cs)
1473: {
1474: struct window_mode_entry *wme = cs->wme;
1475: u_int np = wme->prefix;
1476:
1477: for (; np != 0; np--)
1.229 nicm 1478: window_copy_cursor_previous_word(wme, " ", 1);
1.213 nicm 1479: return (WINDOW_COPY_CMD_NOTHING);
1480: }
1481:
1482: static enum window_copy_cmd_action
1483: window_copy_cmd_previous_word(struct window_copy_cmd_state *cs)
1484: {
1485: struct window_mode_entry *wme = cs->wme;
1486: struct session *s = cs->s;
1487: u_int np = wme->prefix;
1488: const char *ws;
1489:
1490: ws = options_get_string(s->options, "word-separators");
1491: for (; np != 0; np--)
1.229 nicm 1492: window_copy_cursor_previous_word(wme, ws, 1);
1.213 nicm 1493: return (WINDOW_COPY_CMD_NOTHING);
1494: }
1495:
1496: static enum window_copy_cmd_action
1497: window_copy_cmd_rectangle_toggle(struct window_copy_cmd_state *cs)
1498: {
1499: struct window_mode_entry *wme = cs->wme;
1500: struct window_copy_mode_data *data = wme->data;
1501:
1502: data->lineflag = LINE_SEL_NONE;
1503: window_copy_rectangle_toggle(wme);
1504:
1505: return (WINDOW_COPY_CMD_NOTHING);
1506: }
1507:
1508: static enum window_copy_cmd_action
1509: window_copy_cmd_scroll_down(struct window_copy_cmd_state *cs)
1510: {
1511: struct window_mode_entry *wme = cs->wme;
1512: struct window_copy_mode_data *data = wme->data;
1513: u_int np = wme->prefix;
1514:
1515: for (; np != 0; np--)
1516: window_copy_cursor_down(wme, 1);
1517: if (data->scroll_exit && data->oy == 0)
1518: return (WINDOW_COPY_CMD_CANCEL);
1519: return (WINDOW_COPY_CMD_NOTHING);
1520: }
1521:
1522: static enum window_copy_cmd_action
1523: window_copy_cmd_scroll_down_and_cancel(struct window_copy_cmd_state *cs)
1524: {
1525: struct window_mode_entry *wme = cs->wme;
1526: struct window_copy_mode_data *data = wme->data;
1527: u_int np = wme->prefix;
1528:
1529: for (; np != 0; np--)
1530: window_copy_cursor_down(wme, 1);
1531: if (data->oy == 0)
1532: return (WINDOW_COPY_CMD_CANCEL);
1533: return (WINDOW_COPY_CMD_NOTHING);
1534: }
1535:
1536: static enum window_copy_cmd_action
1537: window_copy_cmd_scroll_up(struct window_copy_cmd_state *cs)
1538: {
1539: struct window_mode_entry *wme = cs->wme;
1540: u_int np = wme->prefix;
1541:
1542: for (; np != 0; np--)
1543: window_copy_cursor_up(wme, 1);
1544: return (WINDOW_COPY_CMD_NOTHING);
1545: }
1546:
1547: static enum window_copy_cmd_action
1548: window_copy_cmd_search_again(struct window_copy_cmd_state *cs)
1549: {
1550: struct window_mode_entry *wme = cs->wme;
1551: struct window_copy_mode_data *data = wme->data;
1552: u_int np = wme->prefix;
1553:
1554: if (data->searchtype == WINDOW_COPY_SEARCHUP) {
1555: for (; np != 0; np--)
1.259 nicm 1556: window_copy_search_up(wme, data->searchregex);
1.213 nicm 1557: } else if (data->searchtype == WINDOW_COPY_SEARCHDOWN) {
1558: for (; np != 0; np--)
1.259 nicm 1559: window_copy_search_down(wme, data->searchregex);
1.213 nicm 1560: }
1561: return (WINDOW_COPY_CMD_NOTHING);
1562: }
1563:
1564: static enum window_copy_cmd_action
1565: window_copy_cmd_search_reverse(struct window_copy_cmd_state *cs)
1566: {
1567: struct window_mode_entry *wme = cs->wme;
1568: struct window_copy_mode_data *data = wme->data;
1569: u_int np = wme->prefix;
1570:
1571: if (data->searchtype == WINDOW_COPY_SEARCHUP) {
1572: for (; np != 0; np--)
1.259 nicm 1573: window_copy_search_down(wme, data->searchregex);
1.213 nicm 1574: } else if (data->searchtype == WINDOW_COPY_SEARCHDOWN) {
1575: for (; np != 0; np--)
1.259 nicm 1576: window_copy_search_up(wme, data->searchregex);
1.213 nicm 1577: }
1578: return (WINDOW_COPY_CMD_NOTHING);
1579: }
1580:
1581: static enum window_copy_cmd_action
1582: window_copy_cmd_select_line(struct window_copy_cmd_state *cs)
1583: {
1584: struct window_mode_entry *wme = cs->wme;
1585: struct window_copy_mode_data *data = wme->data;
1586: u_int np = wme->prefix;
1587:
1588: data->lineflag = LINE_SEL_LEFT_RIGHT;
1589: data->rectflag = 0;
1.247 nicm 1590: data->selflag = SEL_LINE;
1591: data->dx = data->cx;
1592: data->dy = screen_hsize(data->backing) + data->cy - data->oy;
1.213 nicm 1593:
1594: window_copy_cursor_start_of_line(wme);
1.247 nicm 1595: data->selrx = data->cx;
1596: data->selry = screen_hsize(data->backing) + data->cy - data->oy;
1.258 nicm 1597: data->endselrx = window_copy_find_length(wme, data->selry);
1598: data->endselry = data->selry;
1.213 nicm 1599: window_copy_start_selection(wme);
1600: for (; np > 1; np--)
1601: window_copy_cursor_down(wme, 0);
1602: window_copy_cursor_end_of_line(wme);
1603:
1604: return (WINDOW_COPY_CMD_REDRAW);
1605: }
1606:
1607: static enum window_copy_cmd_action
1608: window_copy_cmd_select_word(struct window_copy_cmd_state *cs)
1609: {
1610: struct window_mode_entry *wme = cs->wme;
1611: struct session *s = cs->s;
1612: struct window_copy_mode_data *data = wme->data;
1.247 nicm 1613: u_int px, py;
1.213 nicm 1614:
1615: data->lineflag = LINE_SEL_LEFT_RIGHT;
1616: data->rectflag = 0;
1.247 nicm 1617: data->selflag = SEL_WORD;
1618: data->dx = data->cx;
1619: data->dy = screen_hsize(data->backing) + data->cy - data->oy;
1.213 nicm 1620:
1.257 nicm 1621: data->ws = options_get_string(s->options, "word-separators");
1622: window_copy_cursor_previous_word(wme, data->ws, 0);
1.245 nicm 1623: px = data->cx;
1624: py = screen_hsize(data->backing) + data->cy - data->oy;
1.257 nicm 1625: data->selrx = px;
1626: data->selry = py;
1.213 nicm 1627: window_copy_start_selection(wme);
1.245 nicm 1628:
1.247 nicm 1629: if (px >= window_copy_find_length(wme, py) ||
1.252 nicm 1630: !window_copy_in_set(wme, px + 1, py, data->ws))
1.255 nicm 1631: window_copy_cursor_next_word_end(wme, data->ws, 1);
1.245 nicm 1632: else {
1633: window_copy_update_cursor(wme, px, data->cy);
1.255 nicm 1634: if (window_copy_update_selection(wme, 1, 1))
1.245 nicm 1635: window_copy_redraw_lines(wme, data->cy, 1);
1636: }
1.247 nicm 1637: data->endselrx = data->cx;
1638: data->endselry = screen_hsize(data->backing) + data->cy - data->oy;
1.257 nicm 1639: if (data->dx > data->endselrx)
1640: data->dx = data->endselrx;
1.213 nicm 1641:
1642: return (WINDOW_COPY_CMD_REDRAW);
1643: }
1644:
1645: static enum window_copy_cmd_action
1646: window_copy_cmd_start_of_line(struct window_copy_cmd_state *cs)
1647: {
1648: struct window_mode_entry *wme = cs->wme;
1649:
1650: window_copy_cursor_start_of_line(wme);
1651: return (WINDOW_COPY_CMD_NOTHING);
1652: }
1653:
1654: static enum window_copy_cmd_action
1655: window_copy_cmd_top_line(struct window_copy_cmd_state *cs)
1656: {
1657: struct window_mode_entry *wme = cs->wme;
1658: struct window_copy_mode_data *data = wme->data;
1659:
1660: data->cx = 0;
1661: data->cy = 0;
1662:
1.252 nicm 1663: window_copy_update_selection(wme, 1, 0);
1.213 nicm 1664: return (WINDOW_COPY_CMD_REDRAW);
1665: }
1666:
1667: static enum window_copy_cmd_action
1.216 nicm 1668: window_copy_cmd_copy_pipe_no_clear(struct window_copy_cmd_state *cs)
1.213 nicm 1669: {
1670: struct window_mode_entry *wme = cs->wme;
1.215 nicm 1671: struct client *c = cs->c;
1.213 nicm 1672: struct session *s = cs->s;
1.215 nicm 1673: struct winlink *wl = cs->wl;
1674: struct window_pane *wp = wme->wp;
1675: char *command = NULL;
1676: char *prefix = NULL;
1677:
1678: if (cs->args->argc == 3)
1679: prefix = format_single(NULL, cs->args->argv[2], c, s, wl, wp);
1680:
1681: if (s != NULL && *cs->args->argv[1] != '\0') {
1682: command = format_single(NULL, cs->args->argv[1], c, s, wl, wp);
1683: window_copy_copy_pipe(wme, s, prefix, command);
1684: free(command);
1685: }
1.213 nicm 1686:
1.215 nicm 1687: free(prefix);
1.213 nicm 1688: return (WINDOW_COPY_CMD_NOTHING);
1689: }
1690:
1691: static enum window_copy_cmd_action
1.216 nicm 1692: window_copy_cmd_copy_pipe(struct window_copy_cmd_state *cs)
1.213 nicm 1693: {
1694: struct window_mode_entry *wme = cs->wme;
1.215 nicm 1695:
1.216 nicm 1696: window_copy_cmd_copy_pipe_no_clear(cs);
1697: window_copy_clear_selection(wme);
1698: return (WINDOW_COPY_CMD_REDRAW);
1699: }
1.215 nicm 1700:
1.216 nicm 1701: static enum window_copy_cmd_action
1702: window_copy_cmd_copy_pipe_and_cancel(struct window_copy_cmd_state *cs)
1703: {
1704: struct window_mode_entry *wme = cs->wme;
1.213 nicm 1705:
1.216 nicm 1706: window_copy_cmd_copy_pipe_no_clear(cs);
1707: window_copy_clear_selection(wme);
1708: return (WINDOW_COPY_CMD_CANCEL);
1.213 nicm 1709: }
1710:
1711: static enum window_copy_cmd_action
1712: window_copy_cmd_goto_line(struct window_copy_cmd_state *cs)
1713: {
1714: struct window_mode_entry *wme = cs->wme;
1715: const char *argument = cs->args->argv[1];
1716:
1717: if (*argument != '\0')
1718: window_copy_goto_line(wme, argument);
1719: return (WINDOW_COPY_CMD_NOTHING);
1720: }
1721:
1722: static enum window_copy_cmd_action
1723: window_copy_cmd_jump_backward(struct window_copy_cmd_state *cs)
1724: {
1725: struct window_mode_entry *wme = cs->wme;
1726: struct window_copy_mode_data *data = wme->data;
1727: u_int np = wme->prefix;
1728: const char *argument = cs->args->argv[1];
1729:
1730: if (*argument != '\0') {
1731: data->jumptype = WINDOW_COPY_JUMPBACKWARD;
1732: data->jumpchar = *argument;
1733: for (; np != 0; np--)
1734: window_copy_cursor_jump_back(wme);
1735: }
1736: return (WINDOW_COPY_CMD_NOTHING);
1737: }
1738:
1739: static enum window_copy_cmd_action
1740: window_copy_cmd_jump_forward(struct window_copy_cmd_state *cs)
1741: {
1742: struct window_mode_entry *wme = cs->wme;
1743: struct window_copy_mode_data *data = wme->data;
1744: u_int np = wme->prefix;
1745: const char *argument = cs->args->argv[1];
1746:
1747: if (*argument != '\0') {
1748: data->jumptype = WINDOW_COPY_JUMPFORWARD;
1749: data->jumpchar = *argument;
1750: for (; np != 0; np--)
1.215 nicm 1751: window_copy_cursor_jump(wme);
1.213 nicm 1752: }
1753: return (WINDOW_COPY_CMD_NOTHING);
1754: }
1755:
1756: static enum window_copy_cmd_action
1757: window_copy_cmd_jump_to_backward(struct window_copy_cmd_state *cs)
1758: {
1759: struct window_mode_entry *wme = cs->wme;
1760: struct window_copy_mode_data *data = wme->data;
1761: u_int np = wme->prefix;
1762: const char *argument = cs->args->argv[1];
1763:
1764: if (*argument != '\0') {
1765: data->jumptype = WINDOW_COPY_JUMPTOBACKWARD;
1766: data->jumpchar = *argument;
1767: for (; np != 0; np--)
1768: window_copy_cursor_jump_to_back(wme);
1769: }
1770: return (WINDOW_COPY_CMD_NOTHING);
1771: }
1772:
1773: static enum window_copy_cmd_action
1774: window_copy_cmd_jump_to_forward(struct window_copy_cmd_state *cs)
1775: {
1776: struct window_mode_entry *wme = cs->wme;
1777: struct window_copy_mode_data *data = wme->data;
1778: u_int np = wme->prefix;
1779: const char *argument = cs->args->argv[1];
1780:
1781: if (*argument != '\0') {
1782: data->jumptype = WINDOW_COPY_JUMPTOFORWARD;
1783: data->jumpchar = *argument;
1784: for (; np != 0; np--)
1785: window_copy_cursor_jump_to(wme);
1786: }
1787: return (WINDOW_COPY_CMD_NOTHING);
1788: }
1789:
1790: static enum window_copy_cmd_action
1791: window_copy_cmd_search_backward(struct window_copy_cmd_state *cs)
1792: {
1793: struct window_mode_entry *wme = cs->wme;
1794: struct window_copy_mode_data *data = wme->data;
1795: u_int np = wme->prefix;
1796:
1.259 nicm 1797: if (!window_copy_expand_search_string(cs))
1798: return (WINDOW_COPY_CMD_NOTHING);
1799:
1800: if (data->searchstr != NULL) {
1801: data->searchtype = WINDOW_COPY_SEARCHUP;
1802: data->searchregex = 1;
1803: for (; np != 0; np--)
1804: window_copy_search_up(wme, 1);
1.233 nicm 1805: }
1.259 nicm 1806: return (WINDOW_COPY_CMD_NOTHING);
1807: }
1808:
1809: static enum window_copy_cmd_action
1810: window_copy_cmd_search_backward_text(struct window_copy_cmd_state *cs)
1811: {
1812: struct window_mode_entry *wme = cs->wme;
1813: struct window_copy_mode_data *data = wme->data;
1814: u_int np = wme->prefix;
1815:
1816: if (!window_copy_expand_search_string(cs))
1817: return (WINDOW_COPY_CMD_NOTHING);
1818:
1.233 nicm 1819: if (data->searchstr != NULL) {
1.232 nicm 1820: data->searchtype = WINDOW_COPY_SEARCHUP;
1.259 nicm 1821: data->searchregex = 0;
1.213 nicm 1822: for (; np != 0; np--)
1.259 nicm 1823: window_copy_search_up(wme, 0);
1.213 nicm 1824: }
1825: return (WINDOW_COPY_CMD_NOTHING);
1826: }
1827:
1828: static enum window_copy_cmd_action
1829: window_copy_cmd_search_forward(struct window_copy_cmd_state *cs)
1830: {
1831: struct window_mode_entry *wme = cs->wme;
1832: struct window_copy_mode_data *data = wme->data;
1833: u_int np = wme->prefix;
1834:
1.259 nicm 1835: if (!window_copy_expand_search_string(cs))
1836: return (WINDOW_COPY_CMD_NOTHING);
1837:
1838: if (data->searchstr != NULL) {
1839: data->searchtype = WINDOW_COPY_SEARCHDOWN;
1840: data->searchregex = 1;
1841: for (; np != 0; np--)
1842: window_copy_search_down(wme, 1);
1.233 nicm 1843: }
1.259 nicm 1844: return (WINDOW_COPY_CMD_NOTHING);
1845: }
1846:
1847: static enum window_copy_cmd_action
1848: window_copy_cmd_search_forward_text(struct window_copy_cmd_state *cs)
1849: {
1850: struct window_mode_entry *wme = cs->wme;
1851: struct window_copy_mode_data *data = wme->data;
1852: u_int np = wme->prefix;
1853:
1854: if (!window_copy_expand_search_string(cs))
1855: return (WINDOW_COPY_CMD_NOTHING);
1856:
1.233 nicm 1857: if (data->searchstr != NULL) {
1.232 nicm 1858: data->searchtype = WINDOW_COPY_SEARCHDOWN;
1.259 nicm 1859: data->searchregex = 0;
1.213 nicm 1860: for (; np != 0; np--)
1.259 nicm 1861: window_copy_search_down(wme, 0);
1.213 nicm 1862: }
1863: return (WINDOW_COPY_CMD_NOTHING);
1864: }
1865:
1866: static enum window_copy_cmd_action
1867: window_copy_cmd_search_backward_incremental(struct window_copy_cmd_state *cs)
1868: {
1869: struct window_mode_entry *wme = cs->wme;
1870: struct window_copy_mode_data *data = wme->data;
1871: const char *argument = cs->args->argv[1];
1.225 nicm 1872: const char *ss = data->searchstr;
1873: char prefix;
1.213 nicm 1874: enum window_copy_cmd_action action = WINDOW_COPY_CMD_NOTHING;
1875:
1.225 nicm 1876: prefix = *argument++;
1.213 nicm 1877: if (data->searchx == -1 || data->searchy == -1) {
1878: data->searchx = data->cx;
1879: data->searchy = data->cy;
1880: data->searcho = data->oy;
1881: } else if (ss != NULL && strcmp(argument, ss) != 0) {
1882: data->cx = data->searchx;
1883: data->cy = data->searchy;
1884: data->oy = data->searcho;
1885: action = WINDOW_COPY_CMD_REDRAW;
1886: }
1887: if (*argument == '\0') {
1888: window_copy_clear_marks(wme);
1889: return (WINDOW_COPY_CMD_REDRAW);
1890: }
1.225 nicm 1891: switch (prefix) {
1.213 nicm 1892: case '=':
1893: case '-':
1894: data->searchtype = WINDOW_COPY_SEARCHUP;
1.259 nicm 1895: data->searchregex = 0;
1.213 nicm 1896: free(data->searchstr);
1897: data->searchstr = xstrdup(argument);
1.244 nicm 1898: if (!window_copy_search_up(wme, 0)) {
1.213 nicm 1899: window_copy_clear_marks(wme);
1900: return (WINDOW_COPY_CMD_REDRAW);
1901: }
1902: break;
1903: case '+':
1904: data->searchtype = WINDOW_COPY_SEARCHDOWN;
1.259 nicm 1905: data->searchregex = 0;
1.213 nicm 1906: free(data->searchstr);
1907: data->searchstr = xstrdup(argument);
1.244 nicm 1908: if (!window_copy_search_down(wme, 0)) {
1.213 nicm 1909: window_copy_clear_marks(wme);
1910: return (WINDOW_COPY_CMD_REDRAW);
1911: }
1912: break;
1913: }
1914: return (action);
1915: }
1916:
1917: static enum window_copy_cmd_action
1918: window_copy_cmd_search_forward_incremental(struct window_copy_cmd_state *cs)
1919: {
1920: struct window_mode_entry *wme = cs->wme;
1921: struct window_copy_mode_data *data = wme->data;
1922: const char *argument = cs->args->argv[1];
1.225 nicm 1923: const char *ss = data->searchstr;
1924: char prefix;
1.213 nicm 1925: enum window_copy_cmd_action action = WINDOW_COPY_CMD_NOTHING;
1926:
1.225 nicm 1927: prefix = *argument++;
1.213 nicm 1928: if (data->searchx == -1 || data->searchy == -1) {
1929: data->searchx = data->cx;
1930: data->searchy = data->cy;
1931: data->searcho = data->oy;
1932: } else if (ss != NULL && strcmp(argument, ss) != 0) {
1933: data->cx = data->searchx;
1934: data->cy = data->searchy;
1935: data->oy = data->searcho;
1936: action = WINDOW_COPY_CMD_REDRAW;
1937: }
1938: if (*argument == '\0') {
1939: window_copy_clear_marks(wme);
1940: return (WINDOW_COPY_CMD_REDRAW);
1941: }
1.225 nicm 1942: switch (prefix) {
1.213 nicm 1943: case '=':
1944: case '+':
1945: data->searchtype = WINDOW_COPY_SEARCHDOWN;
1.259 nicm 1946: data->searchregex = 0;
1.213 nicm 1947: free(data->searchstr);
1948: data->searchstr = xstrdup(argument);
1.244 nicm 1949: if (!window_copy_search_down(wme, 0)) {
1.213 nicm 1950: window_copy_clear_marks(wme);
1951: return (WINDOW_COPY_CMD_REDRAW);
1952: }
1953: break;
1954: case '-':
1955: data->searchtype = WINDOW_COPY_SEARCHUP;
1.259 nicm 1956: data->searchregex = 0;
1.213 nicm 1957: free(data->searchstr);
1958: data->searchstr = xstrdup(argument);
1.244 nicm 1959: if (!window_copy_search_up(wme, 0)) {
1.213 nicm 1960: window_copy_clear_marks(wme);
1961: return (WINDOW_COPY_CMD_REDRAW);
1962: }
1963: }
1964: return (action);
1965: }
1966:
1967: static const struct {
1968: const char *command;
1969: int minargs;
1970: int maxargs;
1.239 nicm 1971: int ismotion;
1.213 nicm 1972: enum window_copy_cmd_action (*f)(struct window_copy_cmd_state *);
1973: } window_copy_cmd_table[] = {
1.239 nicm 1974: { "append-selection", 0, 0, 0,
1.213 nicm 1975: window_copy_cmd_append_selection },
1.239 nicm 1976: { "append-selection-and-cancel", 0, 0, 0,
1.213 nicm 1977: window_copy_cmd_append_selection_and_cancel },
1.239 nicm 1978: { "back-to-indentation", 0, 0, 0,
1.213 nicm 1979: window_copy_cmd_back_to_indentation },
1.239 nicm 1980: { "begin-selection", 0, 0, 0,
1.213 nicm 1981: window_copy_cmd_begin_selection },
1.239 nicm 1982: { "bottom-line", 0, 0, 1,
1.213 nicm 1983: window_copy_cmd_bottom_line },
1.239 nicm 1984: { "cancel", 0, 0, 0,
1.213 nicm 1985: window_copy_cmd_cancel },
1.239 nicm 1986: { "clear-selection", 0, 0, 0,
1.213 nicm 1987: window_copy_cmd_clear_selection },
1.239 nicm 1988: { "copy-end-of-line", 0, 1, 0,
1.213 nicm 1989: window_copy_cmd_copy_end_of_line },
1.239 nicm 1990: { "copy-line", 0, 1, 0,
1.213 nicm 1991: window_copy_cmd_copy_line },
1.239 nicm 1992: { "copy-pipe-no-clear", 1, 2, 0,
1.216 nicm 1993: window_copy_cmd_copy_pipe_no_clear },
1.239 nicm 1994: { "copy-pipe", 1, 2, 0,
1.213 nicm 1995: window_copy_cmd_copy_pipe },
1.239 nicm 1996: { "copy-pipe-and-cancel", 1, 2, 0,
1.213 nicm 1997: window_copy_cmd_copy_pipe_and_cancel },
1.239 nicm 1998: { "copy-selection-no-clear", 0, 1, 0,
1.216 nicm 1999: window_copy_cmd_copy_selection_no_clear },
1.239 nicm 2000: { "copy-selection", 0, 1, 0,
1.213 nicm 2001: window_copy_cmd_copy_selection },
1.239 nicm 2002: { "copy-selection-and-cancel", 0, 1, 0,
1.213 nicm 2003: window_copy_cmd_copy_selection_and_cancel },
1.239 nicm 2004: { "cursor-down", 0, 0, 1,
1.213 nicm 2005: window_copy_cmd_cursor_down },
1.239 nicm 2006: { "cursor-down-and-cancel", 0, 0, 0,
1.234 nicm 2007: window_copy_cmd_cursor_down_and_cancel },
1.239 nicm 2008: { "cursor-left", 0, 0, 1,
1.213 nicm 2009: window_copy_cmd_cursor_left },
1.239 nicm 2010: { "cursor-right", 0, 0, 1,
1.213 nicm 2011: window_copy_cmd_cursor_right },
1.239 nicm 2012: { "cursor-up", 0, 0, 1,
1.213 nicm 2013: window_copy_cmd_cursor_up },
1.239 nicm 2014: { "end-of-line", 0, 0, 1,
1.213 nicm 2015: window_copy_cmd_end_of_line },
1.239 nicm 2016: { "goto-line", 1, 1, 1,
1.213 nicm 2017: window_copy_cmd_goto_line },
1.239 nicm 2018: { "halfpage-down", 0, 0, 1,
1.213 nicm 2019: window_copy_cmd_halfpage_down },
1.239 nicm 2020: { "halfpage-down-and-cancel", 0, 0, 0,
1.213 nicm 2021: window_copy_cmd_halfpage_down_and_cancel },
1.239 nicm 2022: { "halfpage-up", 0, 0, 1,
1.213 nicm 2023: window_copy_cmd_halfpage_up },
1.239 nicm 2024: { "history-bottom", 0, 0, 1,
1.213 nicm 2025: window_copy_cmd_history_bottom },
1.239 nicm 2026: { "history-top", 0, 0, 1,
1.213 nicm 2027: window_copy_cmd_history_top },
1.239 nicm 2028: { "jump-again", 0, 0, 1,
1.213 nicm 2029: window_copy_cmd_jump_again },
1.239 nicm 2030: { "jump-backward", 1, 1, 1,
1.213 nicm 2031: window_copy_cmd_jump_backward },
1.239 nicm 2032: { "jump-forward", 1, 1, 1,
1.213 nicm 2033: window_copy_cmd_jump_forward },
1.239 nicm 2034: { "jump-reverse", 0, 0, 1,
1.213 nicm 2035: window_copy_cmd_jump_reverse },
1.239 nicm 2036: { "jump-to-backward", 1, 1, 1,
1.213 nicm 2037: window_copy_cmd_jump_to_backward },
1.239 nicm 2038: { "jump-to-forward", 1, 1, 1,
1.213 nicm 2039: window_copy_cmd_jump_to_forward },
1.239 nicm 2040: { "middle-line", 0, 0, 1,
1.213 nicm 2041: window_copy_cmd_middle_line },
1.239 nicm 2042: { "next-matching-bracket", 0, 0, 0,
1.218 nicm 2043: window_copy_cmd_next_matching_bracket },
1.239 nicm 2044: { "next-paragraph", 0, 0, 1,
1.213 nicm 2045: window_copy_cmd_next_paragraph },
1.239 nicm 2046: { "next-space", 0, 0, 1,
1.213 nicm 2047: window_copy_cmd_next_space },
1.239 nicm 2048: { "next-space-end", 0, 0, 1,
1.213 nicm 2049: window_copy_cmd_next_space_end },
1.239 nicm 2050: { "next-word", 0, 0, 1,
1.213 nicm 2051: window_copy_cmd_next_word },
1.239 nicm 2052: { "next-word-end", 0, 0, 1,
1.213 nicm 2053: window_copy_cmd_next_word_end },
1.239 nicm 2054: { "other-end", 0, 0, 1,
1.213 nicm 2055: window_copy_cmd_other_end },
1.239 nicm 2056: { "page-down", 0, 0, 1,
1.213 nicm 2057: window_copy_cmd_page_down },
1.239 nicm 2058: { "page-down-and-cancel", 0, 0, 0,
1.213 nicm 2059: window_copy_cmd_page_down_and_cancel },
1.239 nicm 2060: { "page-up", 0, 0, 1,
1.213 nicm 2061: window_copy_cmd_page_up },
1.239 nicm 2062: { "previous-matching-bracket", 0, 0, 0,
1.218 nicm 2063: window_copy_cmd_previous_matching_bracket },
1.239 nicm 2064: { "previous-paragraph", 0, 0, 1,
1.213 nicm 2065: window_copy_cmd_previous_paragraph },
1.239 nicm 2066: { "previous-space", 0, 0, 1,
1.213 nicm 2067: window_copy_cmd_previous_space },
1.239 nicm 2068: { "previous-word", 0, 0, 1,
1.213 nicm 2069: window_copy_cmd_previous_word },
1.239 nicm 2070: { "rectangle-toggle", 0, 0, 0,
1.213 nicm 2071: window_copy_cmd_rectangle_toggle },
1.239 nicm 2072: { "scroll-down", 0, 0, 1,
1.213 nicm 2073: window_copy_cmd_scroll_down },
1.239 nicm 2074: { "scroll-down-and-cancel", 0, 0, 0,
1.213 nicm 2075: window_copy_cmd_scroll_down_and_cancel },
1.239 nicm 2076: { "scroll-up", 0, 0, 1,
1.213 nicm 2077: window_copy_cmd_scroll_up },
1.239 nicm 2078: { "search-again", 0, 0, 0,
1.213 nicm 2079: window_copy_cmd_search_again },
1.239 nicm 2080: { "search-backward", 0, 1, 0,
1.213 nicm 2081: window_copy_cmd_search_backward },
1.259 nicm 2082: { "search-backward-text", 0, 1, 0,
2083: window_copy_cmd_search_backward_text },
1.239 nicm 2084: { "search-backward-incremental", 1, 1, 0,
1.213 nicm 2085: window_copy_cmd_search_backward_incremental },
1.239 nicm 2086: { "search-forward", 0, 1, 0,
1.213 nicm 2087: window_copy_cmd_search_forward },
1.259 nicm 2088: { "search-forward-text", 0, 1, 0,
2089: window_copy_cmd_search_forward_text },
1.239 nicm 2090: { "search-forward-incremental", 1, 1, 0,
1.213 nicm 2091: window_copy_cmd_search_forward_incremental },
1.239 nicm 2092: { "search-reverse", 0, 0, 0,
1.213 nicm 2093: window_copy_cmd_search_reverse },
1.239 nicm 2094: { "select-line", 0, 0, 0,
1.213 nicm 2095: window_copy_cmd_select_line },
1.239 nicm 2096: { "select-word", 0, 0, 0,
1.213 nicm 2097: window_copy_cmd_select_word },
1.239 nicm 2098: { "start-of-line", 0, 0, 1,
1.213 nicm 2099: window_copy_cmd_start_of_line },
1.239 nicm 2100: { "stop-selection", 0, 0, 0,
1.213 nicm 2101: window_copy_cmd_stop_selection },
1.240 nicm 2102: { "top-line", 0, 0, 1,
1.213 nicm 2103: window_copy_cmd_top_line },
2104: };
2105:
1.157 nicm 2106: static void
1.208 nicm 2107: window_copy_command(struct window_mode_entry *wme, struct client *c,
1.215 nicm 2108: struct session *s, struct winlink *wl, struct args *args,
1.208 nicm 2109: struct mouse_event *m)
1.1 nicm 2110: {
1.208 nicm 2111: struct window_copy_mode_data *data = wme->data;
1.213 nicm 2112: struct window_copy_cmd_state cs;
2113: enum window_copy_cmd_action action;
2114: const char *command;
2115: u_int i;
1.241 nicm 2116: int ismotion = 0, keys;
1.155 nicm 2117:
2118: if (args->argc == 0)
2119: return;
2120: command = args->argv[0];
1.21 nicm 2121:
1.202 nicm 2122: if (m != NULL && m->valid && !MOUSE_WHEEL(m->b))
1.156 nicm 2123: window_copy_move_mouse(m);
2124:
1.213 nicm 2125: cs.wme = wme;
2126: cs.args = args;
2127: cs.m = m;
1.215 nicm 2128:
1.213 nicm 2129: cs.c = c;
2130: cs.s = s;
1.215 nicm 2131: cs.wl = wl;
1.213 nicm 2132:
2133: action = WINDOW_COPY_CMD_NOTHING;
2134: for (i = 0; i < nitems(window_copy_cmd_table); i++) {
2135: if (strcmp(window_copy_cmd_table[i].command, command) == 0) {
2136: if (args->argc - 1 < window_copy_cmd_table[i].minargs ||
2137: args->argc - 1 > window_copy_cmd_table[i].maxargs)
1.155 nicm 2138: break;
1.239 nicm 2139: ismotion = window_copy_cmd_table[i].ismotion;
1.213 nicm 2140: action = window_copy_cmd_table[i].f (&cs);
2141: break;
1.163 nicm 2142: }
1.1 nicm 2143: }
1.21 nicm 2144:
1.162 nicm 2145: if (strncmp(command, "search-", 7) != 0 && data->searchmark != NULL) {
1.239 nicm 2146: keys = options_get_number(wme->wp->window->options, "mode-keys");
2147: if (keys != MODEKEY_VI || !ismotion) {
2148: window_copy_clear_marks(wme);
2149: data->searchx = data->searchy = -1;
2150: } else if (data->searchthis != -1) {
2151: data->searchthis = -1;
2152: action = WINDOW_COPY_CMD_REDRAW;
2153: }
1.213 nicm 2154: if (action == WINDOW_COPY_CMD_NOTHING)
2155: action = WINDOW_COPY_CMD_REDRAW;
1.162 nicm 2156: }
1.209 nicm 2157: wme->prefix = 1;
2158:
1.213 nicm 2159: if (action == WINDOW_COPY_CMD_CANCEL)
2160: window_pane_reset_mode(wme->wp);
2161: else if (action == WINDOW_COPY_CMD_REDRAW)
1.208 nicm 2162: window_copy_redraw_screen(wme);
1.50 nicm 2163: }
2164:
1.157 nicm 2165: static void
1.208 nicm 2166: window_copy_scroll_to(struct window_mode_entry *wme, u_int px, u_int py)
1.21 nicm 2167: {
1.208 nicm 2168: struct window_copy_mode_data *data = wme->data;
1.54 nicm 2169: struct grid *gd = data->backing->grid;
1.21 nicm 2170: u_int offset, gap;
2171:
2172: data->cx = px;
2173:
1.185 nicm 2174: if (py >= gd->hsize - data->oy && py < gd->hsize - data->oy + gd->sy)
2175: data->cy = py - (gd->hsize - data->oy);
2176: else {
2177: gap = gd->sy / 4;
2178: if (py < gd->sy) {
2179: offset = 0;
2180: data->cy = py;
2181: } else if (py > gd->hsize + gd->sy - gap) {
2182: offset = gd->hsize;
2183: data->cy = py - gd->hsize;
2184: } else {
2185: offset = py + gap - gd->sy;
2186: data->cy = py - offset;
2187: }
2188: data->oy = gd->hsize - offset;
1.21 nicm 2189: }
2190:
1.252 nicm 2191: window_copy_update_selection(wme, 1, 0);
1.208 nicm 2192: window_copy_redraw_screen(wme);
1.21 nicm 2193: }
2194:
1.157 nicm 2195: static int
1.118 nicm 2196: window_copy_search_compare(struct grid *gd, u_int px, u_int py,
2197: struct grid *sgd, u_int spx, int cis)
1.21 nicm 2198: {
1.140 nicm 2199: struct grid_cell gc, sgc;
2200: const struct utf8_data *ud, *sud;
1.21 nicm 2201:
1.140 nicm 2202: grid_get_cell(gd, px, py, &gc);
2203: ud = &gc.data;
2204: grid_get_cell(sgd, spx, 0, &sgc);
2205: sud = &sgc.data;
1.35 nicm 2206:
1.140 nicm 2207: if (ud->size != sud->size || ud->width != sud->width)
1.21 nicm 2208: return (0);
1.97 nicm 2209:
1.140 nicm 2210: if (cis && ud->size == 1)
2211: return (tolower(ud->data[0]) == sud->data[0]);
1.97 nicm 2212:
1.140 nicm 2213: return (memcmp(ud->data, sud->data, ud->size) == 0);
1.21 nicm 2214: }
2215:
1.157 nicm 2216: static int
1.21 nicm 2217: window_copy_search_lr(struct grid *gd,
1.97 nicm 2218: struct grid *sgd, u_int *ppx, u_int py, u_int first, u_int last, int cis)
1.21 nicm 2219: {
1.243 nicm 2220: u_int ax, bx, px, pywrap, endline;
2221: int matched;
2222: struct grid_line *gl;
1.21 nicm 2223:
1.242 nicm 2224: endline = gd->hsize + gd->sy - 1;
1.21 nicm 2225: for (ax = first; ax < last; ax++) {
2226: for (bx = 0; bx < sgd->sx; bx++) {
2227: px = ax + bx;
1.242 nicm 2228: pywrap = py;
2229: /* Wrap line. */
2230: while (px >= gd->sx && pywrap < endline) {
1.243 nicm 2231: gl = grid_get_line(gd, pywrap);
2232: if (~gl->flags & GRID_LINE_WRAPPED)
2233: break;
1.242 nicm 2234: px -= gd->sx;
2235: pywrap++;
2236: }
2237: /* We have run off the end of the grid. */
2238: if (px >= gd->sx)
2239: break;
2240: matched = window_copy_search_compare(gd, px, pywrap,
2241: sgd, bx, cis);
1.97 nicm 2242: if (!matched)
1.21 nicm 2243: break;
2244: }
2245: if (bx == sgd->sx) {
2246: *ppx = ax;
2247: return (1);
2248: }
2249: }
2250: return (0);
2251: }
2252:
1.157 nicm 2253: static int
1.21 nicm 2254: window_copy_search_rl(struct grid *gd,
1.97 nicm 2255: struct grid *sgd, u_int *ppx, u_int py, u_int first, u_int last, int cis)
1.21 nicm 2256: {
1.243 nicm 2257: u_int ax, bx, px, pywrap, endline;
2258: int matched;
2259: struct grid_line *gl;
1.21 nicm 2260:
1.242 nicm 2261: endline = gd->hsize + gd->sy - 1;
2262: for (ax = last; ax > first; ax--) {
1.21 nicm 2263: for (bx = 0; bx < sgd->sx; bx++) {
2264: px = ax - 1 + bx;
1.242 nicm 2265: pywrap = py;
2266: /* Wrap line. */
2267: while (px >= gd->sx && pywrap < endline) {
1.243 nicm 2268: gl = grid_get_line(gd, pywrap);
2269: if (~gl->flags & GRID_LINE_WRAPPED)
2270: break;
1.242 nicm 2271: px -= gd->sx;
2272: pywrap++;
2273: }
2274: /* We have run off the end of the grid. */
2275: if (px >= gd->sx)
2276: break;
2277: matched = window_copy_search_compare(gd, px, pywrap,
2278: sgd, bx, cis);
1.97 nicm 2279: if (!matched)
1.21 nicm 2280: break;
2281: }
2282: if (bx == sgd->sx) {
2283: *ppx = ax - 1;
2284: return (1);
2285: }
2286: }
2287: return (0);
2288: }
2289:
1.244 nicm 2290: static int
1.260 nicm 2291: window_copy_search_lr_regex(struct grid *gd, u_int *ppx, u_int *psx, u_int py,
2292: u_int first, u_int last, regex_t *reg)
1.244 nicm 2293: {
1.260 nicm 2294: int eflags = 0;
1.244 nicm 2295: u_int endline, foundx, foundy, len, pywrap, size = 1;
1.260 nicm 2296: char *buf;
1.244 nicm 2297: regmatch_t regmatch;
2298: struct grid_line *gl;
2299:
2300: /*
2301: * This can happen during search if the last match was the last
2302: * character on a line.
2303: */
2304: if (first >= last)
2305: return (0);
2306:
2307: /* Set flags for regex search. */
2308: if (first != 0)
2309: eflags |= REG_NOTBOL;
2310:
2311: /* Need to look at the entire string. */
2312: buf = xmalloc(size);
2313: buf[0] = '\0';
2314: buf = window_copy_stringify(gd, py, first, gd->sx, buf, &size);
2315: len = gd->sx - first;
2316: endline = gd->hsize + gd->sy - 1;
2317: pywrap = py;
2318: while (buf != NULL && pywrap <= endline) {
2319: gl = grid_get_line(gd, pywrap);
2320: if (~gl->flags & GRID_LINE_WRAPPED)
2321: break;
2322: pywrap++;
2323: buf = window_copy_stringify(gd, pywrap, 0, gd->sx, buf, &size);
2324: len += gd->sx;
2325: }
2326:
1.260 nicm 2327: if (regexec(reg, buf, 1, ®match, eflags) == 0) {
1.244 nicm 2328: foundx = first;
2329: foundy = py;
2330: window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
2331: buf + regmatch.rm_so);
2332: if (foundy == py && foundx < last) {
2333: *ppx = foundx;
2334: len -= foundx - first;
2335: window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
2336: buf + regmatch.rm_eo);
2337: *psx = foundx;
2338: while (foundy > py) {
2339: *psx += gd->sx;
2340: foundy--;
2341: }
2342: *psx -= *ppx;
2343: free(buf);
2344: return (1);
2345: }
2346: }
2347:
2348: free(buf);
2349: *ppx = 0;
2350: *psx = 0;
2351: return (0);
2352: }
2353:
2354: static int
1.260 nicm 2355: window_copy_search_rl_regex(struct grid *gd, u_int *ppx, u_int *psx, u_int py,
2356: u_int first, u_int last, regex_t *reg)
1.244 nicm 2357: {
1.260 nicm 2358: int eflags = 0;
2359: u_int endline, len, pywrap, size = 1;
2360: char *buf;
1.244 nicm 2361: struct grid_line *gl;
2362:
2363: /* Set flags for regex search. */
2364: if (first != 0)
2365: eflags |= REG_NOTBOL;
2366:
2367: /* Need to look at the entire string. */
2368: buf = xmalloc(size);
2369: buf[0] = '\0';
2370: buf = window_copy_stringify(gd, py, first, gd->sx, buf, &size);
2371: len = gd->sx - first;
2372: endline = gd->hsize + gd->sy - 1;
2373: pywrap = py;
2374: while (buf != NULL && (pywrap <= endline)) {
2375: gl = grid_get_line(gd, pywrap);
2376: if (~gl->flags & GRID_LINE_WRAPPED)
2377: break;
2378: pywrap++;
2379: buf = window_copy_stringify(gd, pywrap, 0, gd->sx, buf, &size);
2380: len += gd->sx;
2381: }
2382:
2383: if (window_copy_last_regex(gd, py, first, last, len, ppx, psx, buf,
1.260 nicm 2384: reg, eflags))
1.244 nicm 2385: {
2386: free(buf);
2387: return (1);
2388: }
2389:
2390: free(buf);
2391: *ppx = 0;
2392: *psx = 0;
2393: return (0);
2394: }
2395:
1.260 nicm 2396: static const char *
2397: window_copy_cellstring(const struct grid_line *gl, u_int px, size_t *size)
2398: {
2399: struct grid_cell_entry *gce;
2400:
2401: if (px >= gl->cellsize) {
2402: *size = 1;
2403: return (" ");
2404: }
2405:
2406: gce = &gl->celldata[px];
2407: if (~gce->flags & GRID_FLAG_EXTENDED) {
2408: *size = 1;
2409: return (&gce->data.data);
2410: }
2411:
2412: *size = gl->extddata[gce->offset].data.size;
2413: return (gl->extddata[gce->offset].data.data);
2414: }
2415:
1.244 nicm 2416: /* Find last match in given range. */
2417: static int
2418: window_copy_last_regex(struct grid *gd, u_int py, u_int first, u_int last,
2419: u_int len, u_int *ppx, u_int *psx, const char *buf, const regex_t *preg,
2420: int eflags)
2421: {
2422: u_int foundx, foundy, oldx, px = 0, savepx, savesx = 0;
2423: regmatch_t regmatch;
2424:
2425: foundx = first;
2426: foundy = py;
2427: oldx = first;
2428: while (regexec(preg, buf + px, 1, ®match, eflags) == 0) {
2429: window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
2430: buf + px + regmatch.rm_so);
2431: if (foundy > py || foundx >= last)
2432: break;
2433: len -= foundx - oldx;
2434: savepx = foundx;
2435: window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
2436: buf + px + regmatch.rm_eo);
2437: if (foundy > py || foundx >= last) {
2438: *ppx = savepx;
2439: *psx = foundx;
2440: while (foundy > py) {
2441: *psx += gd->sx;
2442: foundy--;
2443: }
2444: *psx -= *ppx;
2445: return (1);
2446: } else {
2447: savesx = foundx - savepx;
2448: len -= savesx;
2449: oldx = foundx;
2450: }
2451: px += regmatch.rm_eo;
2452: }
2453:
2454: if (savesx > 0) {
2455: *ppx = savepx;
2456: *psx = savesx;
2457: return (1);
2458: } else {
2459: *ppx = 0;
2460: *psx = 0;
2461: return (0);
2462: }
2463: }
2464:
2465: /* Stringify line and append to input buffer. Caller frees. */
2466: static char *
2467: window_copy_stringify(struct grid *gd, u_int py, u_int first, u_int last,
2468: char *buf, u_int *size)
2469: {
1.260 nicm 2470: u_int ax, bx, newsize = *size;
2471: const struct grid_line *gl;
2472: const char *d;
2473: size_t bufsize = 1024, dlen;
2474:
2475: while (bufsize < newsize)
2476: bufsize *= 2;
2477: buf = xrealloc(buf, bufsize);
1.244 nicm 2478:
1.260 nicm 2479: gl = grid_peek_line(gd, py);
1.244 nicm 2480: bx = *size - 1;
2481: for (ax = first; ax < last; ax++) {
1.260 nicm 2482: d = window_copy_cellstring(gl, ax, &dlen);
2483: newsize += dlen;
2484: while (bufsize < newsize) {
2485: bufsize *= 2;
2486: buf = xrealloc(buf, bufsize);
2487: }
2488: if (dlen == 1)
2489: buf[bx++] = *d;
2490: else {
2491: memcpy(buf + bx, d, dlen);
2492: bx += dlen;
2493: }
1.244 nicm 2494: }
1.260 nicm 2495: buf[newsize - 1] = '\0';
1.244 nicm 2496:
2497: *size = newsize;
2498: return (buf);
2499: }
2500:
2501: /* Map start of C string containing UTF-8 data to grid cell position. */
2502: static void
2503: window_copy_cstrtocellpos(struct grid *gd, u_int ncells, u_int *ppx, u_int *ppy,
2504: const char *str)
2505: {
1.260 nicm 2506: u_int cell, ccell, px, pywrap, pos, len;
2507: int match;
2508: const struct grid_line *gl;
2509: const char *d;
2510: size_t dlen;
2511: struct {
2512: const char *d;
2513: size_t dlen;
2514: } *cells;
1.244 nicm 2515:
1.260 nicm 2516: /* Populate the array of cell data. */
1.244 nicm 2517: cells = xreallocarray(NULL, ncells, sizeof cells[0]);
2518: cell = 0;
2519: px = *ppx;
2520: pywrap = *ppy;
1.260 nicm 2521: gl = grid_peek_line(gd, pywrap);
1.244 nicm 2522: while (cell < ncells) {
1.260 nicm 2523: cells[cell].d = window_copy_cellstring(gl, px,
2524: &cells[cell].dlen);
1.244 nicm 2525: cell++;
1.261 ! nicm 2526: px++;
! 2527: if (px == gd->sx) {
! 2528: px = 0;
1.244 nicm 2529: pywrap++;
1.260 nicm 2530: gl = grid_peek_line(gd, pywrap);
2531: }
1.244 nicm 2532: }
2533:
2534: /* Locate starting cell. */
2535: cell = 0;
1.260 nicm 2536: len = strlen(str);
1.244 nicm 2537: while (cell < ncells) {
2538: ccell = cell;
1.260 nicm 2539: pos = 0;
1.244 nicm 2540: match = 1;
2541: while (ccell < ncells) {
1.260 nicm 2542: if (str[pos] == '\0') {
1.244 nicm 2543: match = 0;
2544: break;
2545: }
1.260 nicm 2546: d = cells[ccell].d;
2547: dlen = cells[ccell].dlen;
2548: if (dlen == 1) {
2549: if (str[pos] != *d) {
2550: match = 0;
2551: break;
2552: }
2553: pos++;
2554: } else {
2555: if (dlen > len - pos)
2556: dlen = len - pos;
2557: if (memcmp(str + pos, d, dlen) != 0) {
1.244 nicm 2558: match = 0;
2559: break;
2560: }
1.260 nicm 2561: pos += dlen;
1.244 nicm 2562: }
2563: ccell++;
2564: }
2565: if (match)
2566: break;
2567: cell++;
2568: }
2569:
2570: /* If not found this will be one past the end. */
2571: px = *ppx + cell;
2572: pywrap = *ppy;
2573: while (px >= gd->sx) {
2574: px -= gd->sx;
2575: pywrap++;
2576: }
2577:
2578: *ppx = px;
2579: *ppy = pywrap;
2580:
2581: /* Free cell data. */
2582: free(cells);
2583: }
2584:
1.157 nicm 2585: static void
1.230 nicm 2586: window_copy_move_left(struct screen *s, u_int *fx, u_int *fy, int wrapflag)
1.21 nicm 2587: {
1.150 nicm 2588: if (*fx == 0) { /* left */
1.230 nicm 2589: if (*fy == 0) { /* top */
2590: if (wrapflag) {
2591: *fx = screen_size_x(s) - 1;
1.242 nicm 2592: *fy = screen_hsize(s) + screen_size_y(s) - 1;
1.230 nicm 2593: }
1.150 nicm 2594: return;
1.230 nicm 2595: }
1.150 nicm 2596: *fx = screen_size_x(s) - 1;
2597: *fy = *fy - 1;
2598: } else
2599: *fx = *fx - 1;
2600: }
1.21 nicm 2601:
1.157 nicm 2602: static void
1.230 nicm 2603: window_copy_move_right(struct screen *s, u_int *fx, u_int *fy, int wrapflag)
1.150 nicm 2604: {
2605: if (*fx == screen_size_x(s) - 1) { /* right */
1.242 nicm 2606: if (*fy == screen_hsize(s) + screen_size_y(s) - 1) { /* bottom */
1.230 nicm 2607: if (wrapflag) {
2608: *fx = 0;
2609: *fy = 0;
2610: }
1.21 nicm 2611: return;
1.230 nicm 2612: }
1.150 nicm 2613: *fx = 0;
2614: *fy = *fy + 1;
1.21 nicm 2615: } else
1.150 nicm 2616: *fx = *fx + 1;
2617: }
1.21 nicm 2618:
1.157 nicm 2619: static int
1.150 nicm 2620: window_copy_is_lowercase(const char *ptr)
2621: {
2622: while (*ptr != '\0') {
2623: if (*ptr != tolower((u_char)*ptr))
2624: return (0);
2625: ++ptr;
1.97 nicm 2626: }
1.150 nicm 2627: return (1);
2628: }
1.97 nicm 2629:
1.150 nicm 2630: /*
2631: * Search for text stored in sgd starting from position fx,fy up to endline. If
2632: * found, jump to it. If cis then ignore case. The direction is 0 for searching
2633: * up, down otherwise. If wrap then go to begin/end of grid and try again if
2634: * not found.
2635: */
1.163 nicm 2636: static int
1.208 nicm 2637: window_copy_search_jump(struct window_mode_entry *wme, struct grid *gd,
1.150 nicm 2638: struct grid *sgd, u_int fx, u_int fy, u_int endline, int cis, int wrap,
1.244 nicm 2639: int direction, int regex)
1.150 nicm 2640: {
1.260 nicm 2641: u_int i, px, sx, ssize = 1;
2642: int found = 0, cflags = REG_EXTENDED;
2643: char *sbuf;
2644: regex_t reg;
2645:
2646: if (regex) {
2647: sbuf = xmalloc(ssize);
2648: sbuf[0] = '\0';
2649: sbuf = window_copy_stringify(sgd, 0, 0, sgd->sx, sbuf, &ssize);
2650: if (cis)
2651: cflags |= REG_ICASE;
2652: if (regcomp(®, sbuf, cflags) != 0) {
2653: free(sbuf);
2654: return (0);
2655: }
2656: }
1.150 nicm 2657:
2658: if (direction) {
2659: for (i = fy; i <= endline; i++) {
1.260 nicm 2660: if (regex) {
2661: found = window_copy_search_lr_regex(gd,
2662: &px, &sx, i, fx, gd->sx, ®);
2663: } else {
1.244 nicm 2664: found = window_copy_search_lr(gd, sgd,
2665: &px, i, fx, gd->sx, cis);
1.260 nicm 2666: }
1.150 nicm 2667: if (found)
2668: break;
2669: fx = 0;
2670: }
2671: } else {
2672: for (i = fy + 1; endline < i; i--) {
1.260 nicm 2673: if (regex) {
2674: found = window_copy_search_rl_regex(gd,
2675: &px, &sx, i - 1, 0, fx + 1, ®);
2676: } else {
1.244 nicm 2677: found = window_copy_search_rl(gd, sgd,
2678: &px, i - 1, 0, fx + 1, cis);
1.260 nicm 2679: }
1.150 nicm 2680: if (found) {
2681: i--;
2682: break;
2683: }
1.242 nicm 2684: fx = gd->sx - 1;
1.21 nicm 2685: }
2686: }
1.260 nicm 2687: if (regex) {
2688: free(sbuf);
2689: regfree(®);
2690: }
1.150 nicm 2691:
1.163 nicm 2692: if (found) {
1.208 nicm 2693: window_copy_scroll_to(wme, px, i);
1.163 nicm 2694: return (1);
2695: }
2696: if (wrap) {
1.208 nicm 2697: return (window_copy_search_jump(wme, gd, sgd,
1.163 nicm 2698: direction ? 0 : gd->sx - 1,
1.150 nicm 2699: direction ? 0 : gd->hsize + gd->sy - 1, fy, cis, 0,
1.244 nicm 2700: direction, regex));
1.21 nicm 2701: }
1.163 nicm 2702: return (0);
1.21 nicm 2703: }
2704:
1.232 nicm 2705: /*
2706: * Search in for text searchstr. If direction is 0 then search up, otherwise
2707: * down.
2708: */
1.163 nicm 2709: static int
1.244 nicm 2710: window_copy_search(struct window_mode_entry *wme, int direction, int regex)
1.21 nicm 2711: {
1.208 nicm 2712: struct window_pane *wp = wme->wp;
2713: struct window_copy_mode_data *data = wme->data;
1.54 nicm 2714: struct screen *s = data->backing, ss;
1.21 nicm 2715: struct screen_write_ctx ctx;
1.150 nicm 2716: struct grid *gd = s->grid;
2717: u_int fx, fy, endline;
1.163 nicm 2718: int wrapflag, cis, found;
1.21 nicm 2719:
1.174 nicm 2720: free(wp->searchstr);
2721: wp->searchstr = xstrdup(data->searchstr);
1.259 nicm 2722: wp->searchregex = regex;
1.174 nicm 2723:
1.150 nicm 2724: fx = data->cx;
2725: fy = screen_hsize(data->backing) - data->oy + data->cy;
1.21 nicm 2726:
1.162 nicm 2727: screen_init(&ss, screen_write_strlen("%s", data->searchstr), 1, 0);
1.21 nicm 2728: screen_write_start(&ctx, NULL, &ss);
1.162 nicm 2729: screen_write_nputs(&ctx, -1, &grid_default_cell, "%s", data->searchstr);
1.21 nicm 2730: screen_write_stop(&ctx);
2731:
1.150 nicm 2732: wrapflag = options_get_number(wp->window->options, "wrap-search");
1.162 nicm 2733: cis = window_copy_is_lowercase(data->searchstr);
1.21 nicm 2734:
1.230 nicm 2735: if (direction) {
2736: window_copy_move_right(s, &fx, &fy, wrapflag);
1.150 nicm 2737: endline = gd->hsize + gd->sy - 1;
1.230 nicm 2738: } else {
2739: window_copy_move_left(s, &fx, &fy, wrapflag);
1.150 nicm 2740: endline = 0;
1.230 nicm 2741: }
1.244 nicm 2742:
1.208 nicm 2743: found = window_copy_search_jump(wme, gd, ss.grid, fx, fy, endline, cis,
1.244 nicm 2744: wrapflag, direction, regex);
1.162 nicm 2745:
1.244 nicm 2746: if (window_copy_search_marks(wme, &ss, regex))
1.208 nicm 2747: window_copy_redraw_screen(wme);
1.21 nicm 2748:
1.150 nicm 2749: screen_free(&ss);
1.163 nicm 2750: return (found);
1.150 nicm 2751: }
1.97 nicm 2752:
1.162 nicm 2753: static int
1.244 nicm 2754: window_copy_search_marks(struct window_mode_entry *wme, struct screen *ssp,
2755: int regex)
1.162 nicm 2756: {
1.208 nicm 2757: struct window_copy_mode_data *data = wme->data;
1.162 nicm 2758: struct screen *s = data->backing, ss;
2759: struct screen_write_ctx ctx;
2760: struct grid *gd = s->grid;
1.170 nicm 2761: int found, cis, which = -1;
1.260 nicm 2762: int cflags = REG_EXTENDED;
1.162 nicm 2763: u_int px, py, b, nfound = 0, width;
1.260 nicm 2764: u_int ssize = 1;
2765: char *sbuf;
2766: regex_t reg;
1.162 nicm 2767:
2768: if (ssp == NULL) {
2769: width = screen_write_strlen("%s", data->searchstr);
2770: screen_init(&ss, width, 1, 0);
2771: screen_write_start(&ctx, NULL, &ss);
2772: screen_write_nputs(&ctx, -1, &grid_default_cell, "%s",
2773: data->searchstr);
2774: screen_write_stop(&ctx);
2775: ssp = &ss;
2776: } else
2777: width = screen_size_x(ssp);
2778:
2779: cis = window_copy_is_lowercase(data->searchstr);
2780:
2781: free(data->searchmark);
2782: data->searchmark = bit_alloc((gd->hsize + gd->sy) * gd->sx);
2783:
1.260 nicm 2784: if (regex) {
2785: sbuf = xmalloc(ssize);
2786: sbuf[0] = '\0';
2787: sbuf = window_copy_stringify(ssp->grid, 0, 0, ssp->grid->sx,
2788: sbuf, &ssize);
2789: if (cis)
2790: cflags |= REG_ICASE;
2791: if (regcomp(®, sbuf, cflags) != 0) {
2792: free(sbuf);
2793: return (0);
2794: }
2795: }
1.162 nicm 2796: for (py = 0; py < gd->hsize + gd->sy; py++) {
2797: px = 0;
2798: for (;;) {
1.244 nicm 2799: if (regex) {
2800: found = window_copy_search_lr_regex(gd,
1.260 nicm 2801: &px, &width, py, px, gd->sx, ®);
1.244 nicm 2802: if (!found)
2803: break;
1.260 nicm 2804: } else {
1.244 nicm 2805: found = window_copy_search_lr(gd, ssp->grid,
1.260 nicm 2806: &px, py, px, gd->sx, cis);
1.244 nicm 2807: if (!found)
2808: break;
2809: }
1.170 nicm 2810:
1.162 nicm 2811: nfound++;
1.260 nicm 2812: if (px == data->cx &&
2813: py == gd->hsize + data->cy - data->oy)
1.170 nicm 2814: which = nfound;
1.162 nicm 2815:
2816: b = (py * gd->sx) + px;
2817: bit_nset(data->searchmark, b, b + width - 1);
2818:
2819: px++;
2820: }
1.260 nicm 2821: }
2822: if (regex) {
2823: free(sbuf);
2824: regfree(®);
1.162 nicm 2825: }
2826:
1.170 nicm 2827: if (which != -1)
2828: data->searchthis = 1 + nfound - which;
2829: else
2830: data->searchthis = -1;
2831: data->searchcount = nfound;
2832:
1.162 nicm 2833: if (ssp == &ss)
2834: screen_free(&ss);
2835: return (nfound);
2836: }
2837:
1.157 nicm 2838: static void
1.208 nicm 2839: window_copy_clear_marks(struct window_mode_entry *wme)
1.163 nicm 2840: {
1.208 nicm 2841: struct window_copy_mode_data *data = wme->data;
1.163 nicm 2842:
2843: free(data->searchmark);
2844: data->searchmark = NULL;
2845: }
2846:
2847: static int
1.244 nicm 2848: window_copy_search_up(struct window_mode_entry *wme, int regex)
1.150 nicm 2849: {
1.244 nicm 2850: return (window_copy_search(wme, 0, regex));
1.150 nicm 2851: }
1.21 nicm 2852:
1.163 nicm 2853: static int
1.244 nicm 2854: window_copy_search_down(struct window_mode_entry *wme, int regex)
1.150 nicm 2855: {
1.244 nicm 2856: return (window_copy_search(wme, 1, regex));
1.21 nicm 2857: }
2858:
1.157 nicm 2859: static void
1.208 nicm 2860: window_copy_goto_line(struct window_mode_entry *wme, const char *linestr)
1.21 nicm 2861: {
1.208 nicm 2862: struct window_copy_mode_data *data = wme->data;
1.21 nicm 2863: const char *errstr;
1.199 nicm 2864: int lineno;
1.21 nicm 2865:
1.199 nicm 2866: lineno = strtonum(linestr, -1, INT_MAX, &errstr);
1.21 nicm 2867: if (errstr != NULL)
2868: return;
1.199 nicm 2869: if (lineno < 0 || (u_int)lineno > screen_hsize(data->backing))
2870: lineno = screen_hsize(data->backing);
1.35 nicm 2871:
1.21 nicm 2872: data->oy = lineno;
1.252 nicm 2873: window_copy_update_selection(wme, 1, 0);
1.208 nicm 2874: window_copy_redraw_screen(wme);
1.21 nicm 2875: }
2876:
1.157 nicm 2877: static void
1.208 nicm 2878: window_copy_write_line(struct window_mode_entry *wme,
2879: struct screen_write_ctx *ctx, u_int py)
1.1 nicm 2880: {
1.208 nicm 2881: struct window_pane *wp = wme->wp;
2882: struct window_copy_mode_data *data = wme->data;
1.1 nicm 2883: struct screen *s = &data->screen;
1.136 nicm 2884: struct options *oo = wp->window->options;
1.1 nicm 2885: struct grid_cell gc;
1.100 nicm 2886: char hdr[512];
1.162 nicm 2887: size_t size = 0;
1.21 nicm 2888:
1.101 nicm 2889: style_apply(&gc, oo, "mode-style");
1.164 nicm 2890: gc.flags |= GRID_FLAG_NOPALETTE;
1.1 nicm 2891:
1.249 nicm 2892: if (py == 0 && s->rupper < s->rlower && !data->hide_position) {
1.170 nicm 2893: if (data->searchmark == NULL) {
2894: size = xsnprintf(hdr, sizeof hdr,
2895: "[%u/%u]", data->oy, screen_hsize(data->backing));
2896: } else {
2897: if (data->searchthis == -1) {
2898: size = xsnprintf(hdr, sizeof hdr,
2899: "(%u results) [%d/%u]", data->searchcount,
2900: data->oy, screen_hsize(data->backing));
2901: } else {
2902: size = xsnprintf(hdr, sizeof hdr,
2903: "(%u/%u results) [%d/%u]", data->searchthis,
2904: data->searchcount, data->oy,
2905: screen_hsize(data->backing));
2906: }
2907: }
1.62 nicm 2908: if (size > screen_size_x(s))
2909: size = screen_size_x(s);
1.212 nicm 2910: screen_write_cursormove(ctx, screen_size_x(s) - size, 0, 0);
1.1 nicm 2911: screen_write_puts(ctx, &gc, "%s", hdr);
2912: } else
2913: size = 0;
2914:
1.105 nicm 2915: if (size < screen_size_x(s)) {
1.212 nicm 2916: screen_write_cursormove(ctx, 0, py, 0);
1.162 nicm 2917: screen_write_copy(ctx, data->backing, 0,
1.105 nicm 2918: (screen_hsize(data->backing) - data->oy) + py,
1.162 nicm 2919: screen_size_x(s) - size, 1, data->searchmark, &gc);
1.105 nicm 2920: }
1.18 nicm 2921:
2922: if (py == data->cy && data->cx == screen_size_x(s)) {
2923: memcpy(&gc, &grid_default_cell, sizeof gc);
1.212 nicm 2924: screen_write_cursormove(ctx, screen_size_x(s) - 1, py, 0);
1.18 nicm 2925: screen_write_putc(ctx, &gc, '$');
2926: }
1.1 nicm 2927: }
2928:
1.157 nicm 2929: static void
1.208 nicm 2930: window_copy_write_lines(struct window_mode_entry *wme,
2931: struct screen_write_ctx *ctx, u_int py, u_int ny)
1.1 nicm 2932: {
2933: u_int yy;
2934:
2935: for (yy = py; yy < py + ny; yy++)
1.208 nicm 2936: window_copy_write_line(wme, ctx, py);
1.121 nicm 2937: }
2938:
1.157 nicm 2939: static void
1.208 nicm 2940: window_copy_redraw_selection(struct window_mode_entry *wme, u_int old_y)
1.121 nicm 2941: {
1.208 nicm 2942: struct window_copy_mode_data *data = wme->data;
1.247 nicm 2943: struct grid *gd = data->backing->grid;
1.121 nicm 2944: u_int new_y, start, end;
2945:
2946: new_y = data->cy;
2947: if (old_y <= new_y) {
2948: start = old_y;
2949: end = new_y;
2950: } else {
2951: start = new_y;
2952: end = old_y;
2953: }
1.247 nicm 2954:
2955: /*
2956: * In word selection mode the first word on the line below the cursor
2957: * might be selected, so add this line to the redraw area.
2958: */
2959: if (data->selflag == SEL_WORD) {
2960: /* Last grid line in data coordinates. */
2961: if (end < gd->sy + data->oy - 1)
2962: end++;
2963: }
1.208 nicm 2964: window_copy_redraw_lines(wme, start, end - start + 1);
1.1 nicm 2965: }
2966:
1.157 nicm 2967: static void
1.208 nicm 2968: window_copy_redraw_lines(struct window_mode_entry *wme, u_int py, u_int ny)
1.1 nicm 2969: {
1.208 nicm 2970: struct window_pane *wp = wme->wp;
2971: struct window_copy_mode_data *data = wme->data;
1.1 nicm 2972: struct screen_write_ctx ctx;
2973: u_int i;
2974:
2975: screen_write_start(&ctx, wp, NULL);
2976: for (i = py; i < py + ny; i++)
1.208 nicm 2977: window_copy_write_line(wme, &ctx, i);
1.212 nicm 2978: screen_write_cursormove(&ctx, data->cx, data->cy, 0);
1.1 nicm 2979: screen_write_stop(&ctx);
2980: }
2981:
1.157 nicm 2982: static void
1.208 nicm 2983: window_copy_redraw_screen(struct window_mode_entry *wme)
1.1 nicm 2984: {
1.208 nicm 2985: struct window_copy_mode_data *data = wme->data;
1.1 nicm 2986:
1.208 nicm 2987: window_copy_redraw_lines(wme, 0, screen_size_y(&data->screen));
1.1 nicm 2988: }
2989:
1.157 nicm 2990: static void
1.252 nicm 2991: window_copy_synchronize_cursor_end(struct window_mode_entry *wme, int begin,
2992: int no_reset)
1.161 nicm 2993: {
1.208 nicm 2994: struct window_copy_mode_data *data = wme->data;
1.161 nicm 2995: u_int xx, yy;
2996:
2997: yy = screen_hsize(data->backing) + data->cy - data->oy;
1.247 nicm 2998: switch (data->selflag) {
2999: case SEL_WORD:
3000: xx = data->cx;
1.252 nicm 3001: if (no_reset)
1.247 nicm 3002: break;
1.252 nicm 3003: begin = 0;
1.247 nicm 3004: if (data->dy > yy || (data->dy == yy && data->dx > xx)) {
3005: /* Right to left selection. */
3006: window_copy_cursor_previous_word_pos(wme, data->ws, 0,
3007: &xx, &yy);
3008: begin = 1;
3009:
3010: /* Reset the end. */
3011: data->endselx = data->endselrx;
3012: data->endsely = data->endselry;
3013: } else {
3014: /* Left to right selection. */
3015: if (xx >= window_copy_find_length(wme, yy) ||
3016: !window_copy_in_set(wme, xx + 1, yy, data->ws))
3017: window_copy_cursor_next_word_end_pos(wme,
3018: data->ws, &xx, &yy);
3019:
3020: /* Reset the start. */
3021: data->selx = data->selrx;
3022: data->sely = data->selry;
3023: }
3024: break;
3025: case SEL_LINE:
1.252 nicm 3026: if (no_reset) {
3027: xx = data->cx;
3028: break;
3029: }
1.251 nicm 3030: begin = 0;
1.247 nicm 3031: if (data->dy > yy) {
3032: /* Right to left selection. */
3033: xx = 0;
3034: begin = 1;
3035:
3036: /* Reset the end. */
3037: data->endselx = data->endselrx;
3038: data->endsely = data->endselry;
3039: } else {
3040: /* Left to right selection. */
3041: xx = window_copy_find_length(wme, yy);
3042:
3043: /* Reset the start. */
3044: data->selx = data->selrx;
3045: data->sely = data->selry;
3046: }
3047: break;
3048: case SEL_CHAR:
3049: xx = data->cx;
3050: break;
3051: }
3052: if (begin) {
3053: data->selx = xx;
3054: data->sely = yy;
3055: } else {
3056: data->endselx = xx;
3057: data->endsely = yy;
3058: }
3059: }
3060:
3061: static void
1.252 nicm 3062: window_copy_synchronize_cursor(struct window_mode_entry *wme, int no_reset)
1.247 nicm 3063: {
3064: struct window_copy_mode_data *data = wme->data;
1.161 nicm 3065:
3066: switch (data->cursordrag) {
3067: case CURSORDRAG_ENDSEL:
1.252 nicm 3068: window_copy_synchronize_cursor_end(wme, 0, no_reset);
1.161 nicm 3069: break;
3070: case CURSORDRAG_SEL:
1.252 nicm 3071: window_copy_synchronize_cursor_end(wme, 1, no_reset);
1.161 nicm 3072: break;
3073: case CURSORDRAG_NONE:
3074: break;
3075: }
3076: }
3077:
3078: static void
1.208 nicm 3079: window_copy_update_cursor(struct window_mode_entry *wme, u_int cx, u_int cy)
1.1 nicm 3080: {
1.208 nicm 3081: struct window_pane *wp = wme->wp;
3082: struct window_copy_mode_data *data = wme->data;
1.18 nicm 3083: struct screen *s = &data->screen;
1.1 nicm 3084: struct screen_write_ctx ctx;
1.18 nicm 3085: u_int old_cx, old_cy;
1.1 nicm 3086:
1.18 nicm 3087: old_cx = data->cx; old_cy = data->cy;
3088: data->cx = cx; data->cy = cy;
3089: if (old_cx == screen_size_x(s))
1.208 nicm 3090: window_copy_redraw_lines(wme, old_cy, 1);
1.18 nicm 3091: if (data->cx == screen_size_x(s))
1.208 nicm 3092: window_copy_redraw_lines(wme, data->cy, 1);
1.18 nicm 3093: else {
3094: screen_write_start(&ctx, wp, NULL);
1.212 nicm 3095: screen_write_cursormove(&ctx, data->cx, data->cy, 0);
1.18 nicm 3096: screen_write_stop(&ctx);
3097: }
1.1 nicm 3098: }
3099:
1.157 nicm 3100: static void
1.208 nicm 3101: window_copy_start_selection(struct window_mode_entry *wme)
1.1 nicm 3102: {
1.208 nicm 3103: struct window_copy_mode_data *data = wme->data;
1.1 nicm 3104:
1.18 nicm 3105: data->selx = data->cx;
1.54 nicm 3106: data->sely = screen_hsize(data->backing) + data->cy - data->oy;
1.1 nicm 3107:
1.161 nicm 3108: data->endselx = data->selx;
3109: data->endsely = data->sely;
3110:
3111: data->cursordrag = CURSORDRAG_ENDSEL;
3112:
1.252 nicm 3113: window_copy_set_selection(wme, 1, 0);
1.1 nicm 3114: }
3115:
1.157 nicm 3116: static int
1.208 nicm 3117: window_copy_adjust_selection(struct window_mode_entry *wme, u_int *selx,
3118: u_int *sely)
1.161 nicm 3119: {
1.208 nicm 3120: struct window_copy_mode_data *data = wme->data;
1.161 nicm 3121: struct screen *s = &data->screen;
3122: u_int sx, sy, ty;
3123: int relpos;
3124:
3125: sx = *selx;
3126: sy = *sely;
3127:
3128: ty = screen_hsize(data->backing) - data->oy;
3129: if (sy < ty) {
3130: relpos = WINDOW_COPY_REL_POS_ABOVE;
3131: if (!data->rectflag)
3132: sx = 0;
3133: sy = 0;
3134: } else if (sy > ty + screen_size_y(s) - 1) {
3135: relpos = WINDOW_COPY_REL_POS_BELOW;
3136: if (!data->rectflag)
3137: sx = screen_size_x(s) - 1;
3138: sy = screen_size_y(s) - 1;
3139: } else {
3140: relpos = WINDOW_COPY_REL_POS_ON_SCREEN;
3141: sy -= ty;
3142: }
3143:
3144: *selx = sx;
1.176 nicm 3145: *sely = sy;
1.161 nicm 3146: return (relpos);
3147: }
3148:
3149: static int
1.252 nicm 3150: window_copy_update_selection(struct window_mode_entry *wme, int may_redraw,
3151: int no_reset)
1.1 nicm 3152: {
1.208 nicm 3153: struct window_copy_mode_data *data = wme->data;
1.1 nicm 3154: struct screen *s = &data->screen;
1.192 nicm 3155:
3156: if (s->sel == NULL && data->lineflag == LINE_SEL_NONE)
3157: return (0);
1.252 nicm 3158: return (window_copy_set_selection(wme, may_redraw, no_reset));
1.192 nicm 3159: }
3160:
3161: static int
1.252 nicm 3162: window_copy_set_selection(struct window_mode_entry *wme, int may_redraw,
3163: int no_reset)
1.192 nicm 3164: {
1.208 nicm 3165: struct window_pane *wp = wme->wp;
3166: struct window_copy_mode_data *data = wme->data;
1.192 nicm 3167: struct screen *s = &data->screen;
1.136 nicm 3168: struct options *oo = wp->window->options;
1.1 nicm 3169: struct grid_cell gc;
1.161 nicm 3170: u_int sx, sy, cy, endsx, endsy;
3171: int startrelpos, endrelpos;
1.1 nicm 3172:
1.252 nicm 3173: window_copy_synchronize_cursor(wme, no_reset);
1.1 nicm 3174:
3175: /* Adjust the selection. */
3176: sx = data->selx;
3177: sy = data->sely;
1.208 nicm 3178: startrelpos = window_copy_adjust_selection(wme, &sx, &sy);
1.161 nicm 3179:
3180: /* Adjust the end of selection. */
3181: endsx = data->endselx;
3182: endsy = data->endsely;
1.208 nicm 3183: endrelpos = window_copy_adjust_selection(wme, &endsx, &endsy);
1.161 nicm 3184:
3185: /* Selection is outside of the current screen */
3186: if (startrelpos == endrelpos &&
3187: startrelpos != WINDOW_COPY_REL_POS_ON_SCREEN) {
3188: screen_hide_selection(s);
3189: return (0);
3190: }
1.1 nicm 3191:
1.161 nicm 3192: /* Set colours and selection. */
3193: style_apply(&gc, oo, "mode-style");
1.164 nicm 3194: gc.flags |= GRID_FLAG_NOPALETTE;
1.192 nicm 3195: screen_set_selection(s, sx, sy, endsx, endsy, data->rectflag,
3196: data->modekeys, &gc);
1.42 nicm 3197:
1.96 nicm 3198: if (data->rectflag && may_redraw) {
1.42 nicm 3199: /*
3200: * Can't rely on the caller to redraw the right lines for
3201: * rectangle selection - find the highest line and the number
3202: * of lines, and redraw just past that in both directions
3203: */
3204: cy = data->cy;
1.182 nicm 3205: if (data->cursordrag == CURSORDRAG_ENDSEL) {
3206: if (sy < cy)
1.208 nicm 3207: window_copy_redraw_lines(wme, sy, cy - sy + 1);
1.182 nicm 3208: else
1.208 nicm 3209: window_copy_redraw_lines(wme, cy, sy - cy + 1);
1.182 nicm 3210: } else {
1.208 nicm 3211: if (endsy < cy) {
3212: window_copy_redraw_lines(wme, endsy,
3213: cy - endsy + 1);
3214: } else {
3215: window_copy_redraw_lines(wme, cy,
3216: endsy - cy + 1);
3217: }
1.182 nicm 3218: }
1.42 nicm 3219: }
3220:
1.1 nicm 3221: return (1);
3222: }
3223:
1.157 nicm 3224: static void *
1.208 nicm 3225: window_copy_get_selection(struct window_mode_entry *wme, size_t *len)
1.1 nicm 3226: {
1.208 nicm 3227: struct window_pane *wp = wme->wp;
3228: struct window_copy_mode_data *data = wme->data;
1.1 nicm 3229: struct screen *s = &data->screen;
3230: char *buf;
1.42 nicm 3231: size_t off;
1.111 nicm 3232: u_int i, xx, yy, sx, sy, ex, ey, ey_last;
1.188 nicm 3233: u_int firstsx, lastex, restex, restsx, selx;
1.69 nicm 3234: int keys;
1.1 nicm 3235:
1.192 nicm 3236: if (data->screen.sel == NULL && data->lineflag == LINE_SEL_NONE)
1.89 nicm 3237: return (NULL);
1.1 nicm 3238:
3239: buf = xmalloc(1);
3240: off = 0;
3241:
3242: *buf = '\0';
3243:
3244: /*
3245: * The selection extends from selx,sely to (adjusted) cx,cy on
3246: * the base screen.
3247: */
3248:
3249: /* Find start and end. */
1.161 nicm 3250: xx = data->endselx;
3251: yy = data->endsely;
1.2 nicm 3252: if (yy < data->sely || (yy == data->sely && xx < data->selx)) {
1.1 nicm 3253: sx = xx; sy = yy;
3254: ex = data->selx; ey = data->sely;
3255: } else {
3256: sx = data->selx; sy = data->sely;
3257: ex = xx; ey = yy;
3258: }
3259:
3260: /* Trim ex to end of line. */
1.208 nicm 3261: ey_last = window_copy_find_length(wme, ey);
1.111 nicm 3262: if (ex > ey_last)
3263: ex = ey_last;
1.1 nicm 3264:
1.42 nicm 3265: /*
3266: * Deal with rectangle-copy if necessary; four situations: start of
3267: * first line (firstsx), end of last line (lastex), start (restsx) and
3268: * end (restex) of all other lines.
3269: */
3270: xx = screen_size_x(s);
1.69 nicm 3271:
3272: /*
3273: * Behave according to mode-keys. If it is emacs, copy like emacs,
3274: * keeping the top-left-most character, and dropping the
3275: * bottom-right-most, regardless of copy direction. If it is vi, also
3276: * keep bottom-right-most character.
3277: */
1.136 nicm 3278: keys = options_get_number(wp->window->options, "mode-keys");
1.42 nicm 3279: if (data->rectflag) {
3280: /*
3281: * Need to ignore the column with the cursor in it, which for
3282: * rectangular copy means knowing which side the cursor is on.
3283: */
1.188 nicm 3284: if (data->cursordrag == CURSORDRAG_ENDSEL)
3285: selx = data->selx;
3286: else
3287: selx = data->endselx;
3288: if (selx < data->cx) {
1.42 nicm 3289: /* Selection start is on the left. */
1.69 nicm 3290: if (keys == MODEKEY_EMACS) {
3291: lastex = data->cx;
3292: restex = data->cx;
3293: }
3294: else {
3295: lastex = data->cx + 1;
3296: restex = data->cx + 1;
3297: }
1.188 nicm 3298: firstsx = selx;
3299: restsx = selx;
1.42 nicm 3300: } else {
3301: /* Cursor is on the left. */
1.188 nicm 3302: lastex = selx + 1;
3303: restex = selx + 1;
1.64 nicm 3304: firstsx = data->cx;
3305: restsx = data->cx;
1.42 nicm 3306: }
3307: } else {
1.69 nicm 3308: if (keys == MODEKEY_EMACS)
3309: lastex = ex;
1.74 nicm 3310: else
1.69 nicm 3311: lastex = ex + 1;
1.42 nicm 3312: restex = xx;
3313: firstsx = sx;
3314: restsx = 0;
3315: }
3316:
1.1 nicm 3317: /* Copy the lines. */
1.110 nicm 3318: for (i = sy; i <= ey; i++) {
1.208 nicm 3319: window_copy_copy_line(wme, &buf, &off, i,
1.110 nicm 3320: (i == sy ? firstsx : restsx),
3321: (i == ey ? lastex : restex));
1.1 nicm 3322: }
3323:
1.26 nicm 3324: /* Don't bother if no data. */
3325: if (off == 0) {
1.81 nicm 3326: free(buf);
1.89 nicm 3327: return (NULL);
1.26 nicm 3328: }
1.111 nicm 3329: if (keys == MODEKEY_EMACS || lastex <= ey_last)
3330: off -= 1; /* remove final \n (unless at end in vi mode) */
3331: *len = off;
1.89 nicm 3332: return (buf);
3333: }
3334:
1.157 nicm 3335: static void
1.215 nicm 3336: window_copy_copy_buffer(struct window_mode_entry *wme, const char *prefix,
3337: void *buf, size_t len)
1.89 nicm 3338: {
1.208 nicm 3339: struct window_pane *wp = wme->wp;
3340: struct screen_write_ctx ctx;
1.72 nicm 3341:
1.178 nicm 3342: if (options_get_number(global_options, "set-clipboard") != 0) {
1.91 nicm 3343: screen_write_start(&ctx, wp, NULL);
3344: screen_write_setselection(&ctx, buf, len);
3345: screen_write_stop(&ctx);
1.179 nicm 3346: notify_pane("pane-set-clipboard", wp);
1.91 nicm 3347: }
1.1 nicm 3348:
1.215 nicm 3349: paste_add(prefix, buf, len);
1.89 nicm 3350: }
3351:
1.157 nicm 3352: static void
1.208 nicm 3353: window_copy_copy_pipe(struct window_mode_entry *wme, struct session *s,
1.253 nicm 3354: const char *prefix, const char *cmd)
1.89 nicm 3355: {
1.215 nicm 3356: void *buf;
3357: size_t len;
3358: struct job *job;
1.89 nicm 3359:
1.208 nicm 3360: buf = window_copy_get_selection(wme, &len);
1.89 nicm 3361: if (buf == NULL)
3362: return;
3363:
1.253 nicm 3364: job = job_run(cmd, s, NULL, NULL, NULL, NULL, NULL, JOB_NOWAIT, -1, -1);
1.196 nicm 3365: bufferevent_write(job_get_event(job), buf, len);
1.215 nicm 3366: window_copy_copy_buffer(wme, prefix, buf, len);
1.89 nicm 3367: }
3368:
1.157 nicm 3369: static void
1.215 nicm 3370: window_copy_copy_selection(struct window_mode_entry *wme, const char *prefix)
1.89 nicm 3371: {
1.203 nicm 3372: char *buf;
1.114 nicm 3373: size_t len;
1.89 nicm 3374:
1.208 nicm 3375: buf = window_copy_get_selection(wme, &len);
1.203 nicm 3376: if (buf != NULL)
1.215 nicm 3377: window_copy_copy_buffer(wme, prefix, buf, len);
1.103 nicm 3378: }
3379:
1.157 nicm 3380: static void
1.208 nicm 3381: window_copy_append_selection(struct window_mode_entry *wme)
1.103 nicm 3382: {
1.208 nicm 3383: struct window_pane *wp = wme->wp;
1.108 nicm 3384: char *buf;
3385: struct paste_buffer *pb;
1.227 nicm 3386: const char *bufdata, *bufname = NULL;
1.132 nicm 3387: size_t len, bufsize;
1.108 nicm 3388: struct screen_write_ctx ctx;
1.103 nicm 3389:
1.208 nicm 3390: buf = window_copy_get_selection(wme, &len);
1.103 nicm 3391: if (buf == NULL)
3392: return;
3393:
1.178 nicm 3394: if (options_get_number(global_options, "set-clipboard") != 0) {
1.103 nicm 3395: screen_write_start(&ctx, wp, NULL);
3396: screen_write_setselection(&ctx, buf, len);
3397: screen_write_stop(&ctx);
1.179 nicm 3398: notify_pane("pane-set-clipboard", wp);
1.103 nicm 3399: }
3400:
1.203 nicm 3401: pb = paste_get_top(&bufname);
1.103 nicm 3402: if (pb != NULL) {
1.132 nicm 3403: bufdata = paste_buffer_data(pb, &bufsize);
3404: buf = xrealloc(buf, len + bufsize);
3405: memmove(buf + bufsize, buf, len);
3406: memcpy(buf, bufdata, bufsize);
3407: len += bufsize;
1.103 nicm 3408: }
1.108 nicm 3409: if (paste_set(buf, len, bufname, NULL) != 0)
1.103 nicm 3410: free(buf);
1.1 nicm 3411: }
3412:
1.157 nicm 3413: static void
1.208 nicm 3414: window_copy_copy_line(struct window_mode_entry *wme, char **buf, size_t *off,
3415: u_int sy, u_int sx, u_int ex)
1.1 nicm 3416: {
1.208 nicm 3417: struct window_copy_mode_data *data = wme->data;
1.54 nicm 3418: struct grid *gd = data->backing->grid;
1.140 nicm 3419: struct grid_cell gc;
1.54 nicm 3420: struct grid_line *gl;
1.86 nicm 3421: struct utf8_data ud;
1.54 nicm 3422: u_int i, xx, wrapped = 0;
1.115 nicm 3423: const char *s;
1.1 nicm 3424:
3425: if (sx > ex)
3426: return;
3427:
1.16 nicm 3428: /*
3429: * Work out if the line was wrapped at the screen edge and all of it is
3430: * on screen.
3431: */
1.190 nicm 3432: gl = grid_get_line(gd, sy);
1.35 nicm 3433: if (gl->flags & GRID_LINE_WRAPPED && gl->cellsize <= gd->sx)
1.16 nicm 3434: wrapped = 1;
3435:
3436: /* If the line was wrapped, don't strip spaces (use the full length). */
3437: if (wrapped)
3438: xx = gl->cellsize;
3439: else
1.208 nicm 3440: xx = window_copy_find_length(wme, sy);
1.1 nicm 3441: if (ex > xx)
3442: ex = xx;
3443: if (sx > xx)
3444: sx = xx;
3445:
3446: if (sx < ex) {
3447: for (i = sx; i < ex; i++) {
1.140 nicm 3448: grid_get_cell(gd, i, sy, &gc);
3449: if (gc.flags & GRID_FLAG_PADDING)
1.1 nicm 3450: continue;
1.140 nicm 3451: utf8_copy(&ud, &gc.data);
3452: if (ud.size == 1 && (gc.attr & GRID_ATTR_CHARSET)) {
1.115 nicm 3453: s = tty_acs_get(NULL, ud.data[0]);
3454: if (s != NULL && strlen(s) <= sizeof ud.data) {
3455: ud.size = strlen(s);
1.117 nicm 3456: memcpy(ud.data, s, ud.size);
1.115 nicm 3457: }
3458: }
1.86 nicm 3459:
1.116 nicm 3460: *buf = xrealloc(*buf, (*off) + ud.size);
1.86 nicm 3461: memcpy(*buf + *off, ud.data, ud.size);
3462: *off += ud.size;
1.1 nicm 3463: }
3464: }
3465:
1.16 nicm 3466: /* Only add a newline if the line wasn't wrapped. */
1.44 nicm 3467: if (!wrapped || ex != xx) {
1.116 nicm 3468: *buf = xrealloc(*buf, (*off) + 1);
1.16 nicm 3469: (*buf)[(*off)++] = '\n';
3470: }
1.1 nicm 3471: }
3472:
1.157 nicm 3473: static void
1.208 nicm 3474: window_copy_clear_selection(struct window_mode_entry *wme)
1.47 nicm 3475: {
1.208 nicm 3476: struct window_copy_mode_data *data = wme->data;
1.47 nicm 3477: u_int px, py;
3478:
3479: screen_clear_selection(&data->screen);
3480:
1.161 nicm 3481: data->cursordrag = CURSORDRAG_NONE;
1.197 nicm 3482: data->lineflag = LINE_SEL_NONE;
1.250 nicm 3483: data->selflag = SEL_CHAR;
1.161 nicm 3484:
1.54 nicm 3485: py = screen_hsize(data->backing) + data->cy - data->oy;
1.208 nicm 3486: px = window_copy_find_length(wme, py);
1.47 nicm 3487: if (data->cx > px)
1.208 nicm 3488: window_copy_update_cursor(wme, px, data->cy);
1.47 nicm 3489: }
3490:
1.157 nicm 3491: static int
1.208 nicm 3492: window_copy_in_set(struct window_mode_entry *wme, u_int px, u_int py,
3493: const char *set)
1.1 nicm 3494: {
1.208 nicm 3495: struct window_copy_mode_data *data = wme->data;
1.140 nicm 3496: struct grid_cell gc;
3497:
3498: grid_get_cell(data->backing->grid, px, py, &gc);
1.204 nicm 3499: if (gc.flags & GRID_FLAG_PADDING)
3500: return (0);
1.224 nicm 3501: return (utf8_cstrhas(set, &gc.data));
1.1 nicm 3502: }
3503:
1.157 nicm 3504: static u_int
1.208 nicm 3505: window_copy_find_length(struct window_mode_entry *wme, u_int py)
1.1 nicm 3506: {
1.208 nicm 3507: struct window_copy_mode_data *data = wme->data;
1.1 nicm 3508:
1.224 nicm 3509: return (grid_line_length(data->backing->grid, py));
1.1 nicm 3510: }
3511:
1.157 nicm 3512: static void
1.208 nicm 3513: window_copy_cursor_start_of_line(struct window_mode_entry *wme)
1.5 nicm 3514: {
1.208 nicm 3515: struct window_copy_mode_data *data = wme->data;
1.58 nicm 3516: struct screen *back_s = data->backing;
3517: struct grid *gd = back_s->grid;
3518: u_int py;
3519:
1.192 nicm 3520: if (data->cx == 0 && data->lineflag == LINE_SEL_NONE) {
1.58 nicm 3521: py = screen_hsize(back_s) + data->cy - data->oy;
1.118 nicm 3522: while (py > 0 &&
1.190 nicm 3523: grid_get_line(gd, py - 1)->flags & GRID_LINE_WRAPPED) {
1.208 nicm 3524: window_copy_cursor_up(wme, 0);
1.58 nicm 3525: py = screen_hsize(back_s) + data->cy - data->oy;
3526: }
3527: }
1.208 nicm 3528: window_copy_update_cursor(wme, 0, data->cy);
1.252 nicm 3529: if (window_copy_update_selection(wme, 1, 0))
1.208 nicm 3530: window_copy_redraw_lines(wme, data->cy, 1);
1.6 nicm 3531: }
3532:
1.157 nicm 3533: static void
1.208 nicm 3534: window_copy_cursor_back_to_indentation(struct window_mode_entry *wme)
1.6 nicm 3535: {
1.208 nicm 3536: struct window_copy_mode_data *data = wme->data;
1.6 nicm 3537: u_int px, py, xx;
1.140 nicm 3538: struct grid_cell gc;
1.6 nicm 3539:
3540: px = 0;
1.54 nicm 3541: py = screen_hsize(data->backing) + data->cy - data->oy;
1.208 nicm 3542: xx = window_copy_find_length(wme, py);
1.6 nicm 3543:
3544: while (px < xx) {
1.140 nicm 3545: grid_get_cell(data->backing->grid, px, py, &gc);
3546: if (gc.data.size != 1 || *gc.data.data != ' ')
1.6 nicm 3547: break;
3548: px++;
3549: }
3550:
1.208 nicm 3551: window_copy_update_cursor(wme, px, data->cy);
1.252 nicm 3552: if (window_copy_update_selection(wme, 1, 0))
1.208 nicm 3553: window_copy_redraw_lines(wme, data->cy, 1);
1.5 nicm 3554: }
3555:
1.157 nicm 3556: static void
1.208 nicm 3557: window_copy_cursor_end_of_line(struct window_mode_entry *wme)
1.5 nicm 3558: {
1.208 nicm 3559: struct window_copy_mode_data *data = wme->data;
1.54 nicm 3560: struct screen *back_s = data->backing;
3561: struct grid *gd = back_s->grid;
1.190 nicm 3562: struct grid_line *gl;
1.5 nicm 3563: u_int px, py;
3564:
1.54 nicm 3565: py = screen_hsize(back_s) + data->cy - data->oy;
1.208 nicm 3566: px = window_copy_find_length(wme, py);
1.5 nicm 3567:
1.192 nicm 3568: if (data->cx == px && data->lineflag == LINE_SEL_NONE) {
3569: if (data->screen.sel != NULL && data->rectflag)
1.54 nicm 3570: px = screen_size_x(back_s);
1.190 nicm 3571: gl = grid_get_line(gd, py);
3572: if (gl->flags & GRID_LINE_WRAPPED) {
3573: while (py < gd->sy + gd->hsize) {
3574: gl = grid_get_line(gd, py);
3575: if (~gl->flags & GRID_LINE_WRAPPED)
3576: break;
1.208 nicm 3577: window_copy_cursor_down(wme, 0);
1.190 nicm 3578: py = screen_hsize(back_s) + data->cy - data->oy;
1.49 nicm 3579: }
1.208 nicm 3580: px = window_copy_find_length(wme, py);
1.49 nicm 3581: }
3582: }
1.208 nicm 3583: window_copy_update_cursor(wme, px, data->cy);
1.49 nicm 3584:
1.252 nicm 3585: if (window_copy_update_selection(wme, 1, 0))
1.208 nicm 3586: window_copy_redraw_lines(wme, data->cy, 1);
1.95 nicm 3587: }
3588:
1.157 nicm 3589: static void
1.208 nicm 3590: window_copy_other_end(struct window_mode_entry *wme)
1.95 nicm 3591: {
1.208 nicm 3592: struct window_copy_mode_data *data = wme->data;
1.95 nicm 3593: struct screen *s = &data->screen;
1.161 nicm 3594: u_int selx, sely, cy, yy, hsize;
1.95 nicm 3595:
1.192 nicm 3596: if (s->sel == NULL && data->lineflag == LINE_SEL_NONE)
1.95 nicm 3597: return;
3598:
1.192 nicm 3599: if (data->lineflag == LINE_SEL_LEFT_RIGHT)
3600: data->lineflag = LINE_SEL_RIGHT_LEFT;
3601: else if (data->lineflag == LINE_SEL_RIGHT_LEFT)
3602: data->lineflag = LINE_SEL_LEFT_RIGHT;
1.118 nicm 3603:
1.161 nicm 3604: switch (data->cursordrag) {
3605: case CURSORDRAG_NONE:
3606: case CURSORDRAG_SEL:
3607: data->cursordrag = CURSORDRAG_ENDSEL;
3608: break;
3609: case CURSORDRAG_ENDSEL:
3610: data->cursordrag = CURSORDRAG_SEL;
3611: break;
3612: }
3613:
3614: selx = data->endselx;
3615: sely = data->endsely;
3616: if (data->cursordrag == CURSORDRAG_SEL) {
3617: selx = data->selx;
3618: sely = data->sely;
3619: }
3620:
1.95 nicm 3621: cy = data->cy;
3622: yy = screen_hsize(data->backing) + data->cy - data->oy;
3623:
3624: data->cx = selx;
3625:
1.122 nicm 3626: hsize = screen_hsize(data->backing);
1.161 nicm 3627: if (sely < hsize - data->oy) { /* above */
1.122 nicm 3628: data->oy = hsize - sely;
1.95 nicm 3629: data->cy = 0;
1.161 nicm 3630: } else if (sely > hsize - data->oy + screen_size_y(s)) { /* below */
1.122 nicm 3631: data->oy = hsize - sely + screen_size_y(s) - 1;
1.95 nicm 3632: data->cy = screen_size_y(s) - 1;
3633: } else
3634: data->cy = cy + sely - yy;
3635:
1.252 nicm 3636: window_copy_update_selection(wme, 1, 1);
1.208 nicm 3637: window_copy_redraw_screen(wme);
1.5 nicm 3638: }
3639:
1.157 nicm 3640: static void
1.208 nicm 3641: window_copy_cursor_left(struct window_mode_entry *wme)
1.1 nicm 3642: {
1.208 nicm 3643: struct window_copy_mode_data *data = wme->data;
1.168 nicm 3644: u_int py, cx;
3645: struct grid_cell gc;
1.1 nicm 3646:
1.145 nicm 3647: py = screen_hsize(data->backing) + data->cy - data->oy;
1.168 nicm 3648: cx = data->cx;
3649: while (cx > 0) {
3650: grid_get_cell(data->backing->grid, cx, py, &gc);
3651: if (~gc.flags & GRID_FLAG_PADDING)
3652: break;
3653: cx--;
3654: }
3655: if (cx == 0 && py > 0) {
1.208 nicm 3656: window_copy_cursor_up(wme, 0);
3657: window_copy_cursor_end_of_line(wme);
1.168 nicm 3658: } else if (cx > 0) {
1.208 nicm 3659: window_copy_update_cursor(wme, cx - 1, data->cy);
1.252 nicm 3660: if (window_copy_update_selection(wme, 1, 0))
1.208 nicm 3661: window_copy_redraw_lines(wme, data->cy, 1);
1.1 nicm 3662: }
3663: }
3664:
1.157 nicm 3665: static void
1.208 nicm 3666: window_copy_cursor_right(struct window_mode_entry *wme)
1.1 nicm 3667: {
1.208 nicm 3668: struct window_copy_mode_data *data = wme->data;
1.168 nicm 3669: u_int px, py, yy, cx, cy;
3670: struct grid_cell gc;
1.1 nicm 3671:
1.145 nicm 3672: py = screen_hsize(data->backing) + data->cy - data->oy;
3673: yy = screen_hsize(data->backing) + screen_size_y(data->backing) - 1;
1.192 nicm 3674: if (data->screen.sel != NULL && data->rectflag)
1.47 nicm 3675: px = screen_size_x(&data->screen);
1.168 nicm 3676: else
1.208 nicm 3677: px = window_copy_find_length(wme, py);
1.1 nicm 3678:
1.145 nicm 3679: if (data->cx >= px && py < yy) {
1.208 nicm 3680: window_copy_cursor_start_of_line(wme);
3681: window_copy_cursor_down(wme, 0);
1.145 nicm 3682: } else if (data->cx < px) {
1.168 nicm 3683: cx = data->cx + 1;
3684: cy = screen_hsize(data->backing) + data->cy - data->oy;
3685: while (cx < px) {
3686: grid_get_cell(data->backing->grid, cx, cy, &gc);
3687: if (~gc.flags & GRID_FLAG_PADDING)
3688: break;
3689: cx++;
3690: }
1.208 nicm 3691: window_copy_update_cursor(wme, cx, data->cy);
1.252 nicm 3692: if (window_copy_update_selection(wme, 1, 0))
1.208 nicm 3693: window_copy_redraw_lines(wme, data->cy, 1);
1.1 nicm 3694: }
3695: }
3696:
1.157 nicm 3697: static void
1.208 nicm 3698: window_copy_cursor_up(struct window_mode_entry *wme, int scroll_only)
1.1 nicm 3699: {
1.208 nicm 3700: struct window_copy_mode_data *data = wme->data;
1.36 nicm 3701: struct screen *s = &data->screen;
1.1 nicm 3702: u_int ox, oy, px, py;
3703:
1.54 nicm 3704: oy = screen_hsize(data->backing) + data->cy - data->oy;
1.208 nicm 3705: ox = window_copy_find_length(wme, oy);
1.77 nicm 3706: if (data->cx != ox) {
1.25 nicm 3707: data->lastcx = data->cx;
3708: data->lastsx = ox;
3709: }
1.1 nicm 3710:
1.192 nicm 3711: if (data->lineflag == LINE_SEL_LEFT_RIGHT && oy == data->sely)
1.208 nicm 3712: window_copy_other_end(wme);
1.118 nicm 3713:
1.28 nicm 3714: if (scroll_only || data->cy == 0) {
1.226 nicm 3715: data->cx = data->lastcx;
1.208 nicm 3716: window_copy_scroll_down(wme, 1);
1.36 nicm 3717: if (scroll_only) {
3718: if (data->cy == screen_size_y(s) - 1)
1.208 nicm 3719: window_copy_redraw_lines(wme, data->cy, 1);
1.36 nicm 3720: else
1.208 nicm 3721: window_copy_redraw_lines(wme, data->cy, 2);
1.36 nicm 3722: }
1.28 nicm 3723: } else {
1.226 nicm 3724: window_copy_update_cursor(wme, data->lastcx, data->cy - 1);
1.252 nicm 3725: if (window_copy_update_selection(wme, 1, 0)) {
1.36 nicm 3726: if (data->cy == screen_size_y(s) - 1)
1.208 nicm 3727: window_copy_redraw_lines(wme, data->cy, 1);
1.36 nicm 3728: else
1.208 nicm 3729: window_copy_redraw_lines(wme, data->cy, 2);
1.36 nicm 3730: }
1.1 nicm 3731: }
3732:
1.198 nicm 3733: if (data->screen.sel == NULL || !data->rectflag) {
1.54 nicm 3734: py = screen_hsize(data->backing) + data->cy - data->oy;
1.208 nicm 3735: px = window_copy_find_length(wme, py);
1.49 nicm 3736: if ((data->cx >= data->lastsx && data->cx != px) ||
3737: data->cx > px)
1.208 nicm 3738: window_copy_cursor_end_of_line(wme);
1.47 nicm 3739: }
1.118 nicm 3740:
1.192 nicm 3741: if (data->lineflag == LINE_SEL_LEFT_RIGHT)
1.208 nicm 3742: window_copy_cursor_end_of_line(wme);
1.192 nicm 3743: else if (data->lineflag == LINE_SEL_RIGHT_LEFT)
1.208 nicm 3744: window_copy_cursor_start_of_line(wme);
1.1 nicm 3745: }
3746:
1.157 nicm 3747: static void
1.208 nicm 3748: window_copy_cursor_down(struct window_mode_entry *wme, int scroll_only)
1.1 nicm 3749: {
1.208 nicm 3750: struct window_copy_mode_data *data = wme->data;
1.1 nicm 3751: struct screen *s = &data->screen;
3752: u_int ox, oy, px, py;
3753:
1.54 nicm 3754: oy = screen_hsize(data->backing) + data->cy - data->oy;
1.208 nicm 3755: ox = window_copy_find_length(wme, oy);
1.77 nicm 3756: if (data->cx != ox) {
1.25 nicm 3757: data->lastcx = data->cx;
3758: data->lastsx = ox;
3759: }
1.1 nicm 3760:
1.192 nicm 3761: if (data->lineflag == LINE_SEL_RIGHT_LEFT && oy == data->endsely)
1.208 nicm 3762: window_copy_other_end(wme);
1.118 nicm 3763:
1.28 nicm 3764: if (scroll_only || data->cy == screen_size_y(s) - 1) {
1.226 nicm 3765: data->cx = data->lastcx;
1.208 nicm 3766: window_copy_scroll_up(wme, 1);
1.31 nicm 3767: if (scroll_only && data->cy > 0)
1.208 nicm 3768: window_copy_redraw_lines(wme, data->cy - 1, 2);
1.28 nicm 3769: } else {
1.226 nicm 3770: window_copy_update_cursor(wme, data->lastcx, data->cy + 1);
1.252 nicm 3771: if (window_copy_update_selection(wme, 1, 0))
1.208 nicm 3772: window_copy_redraw_lines(wme, data->cy - 1, 2);
1.1 nicm 3773: }
3774:
1.192 nicm 3775: if (data->screen.sel == NULL || !data->rectflag) {
1.54 nicm 3776: py = screen_hsize(data->backing) + data->cy - data->oy;
1.208 nicm 3777: px = window_copy_find_length(wme, py);
1.49 nicm 3778: if ((data->cx >= data->lastsx && data->cx != px) ||
3779: data->cx > px)
1.208 nicm 3780: window_copy_cursor_end_of_line(wme);
1.52 nicm 3781: }
1.118 nicm 3782:
1.192 nicm 3783: if (data->lineflag == LINE_SEL_LEFT_RIGHT)
1.208 nicm 3784: window_copy_cursor_end_of_line(wme);
1.192 nicm 3785: else if (data->lineflag == LINE_SEL_RIGHT_LEFT)
1.208 nicm 3786: window_copy_cursor_start_of_line(wme);
1.52 nicm 3787: }
3788:
1.157 nicm 3789: static void
1.208 nicm 3790: window_copy_cursor_jump(struct window_mode_entry *wme)
1.52 nicm 3791: {
1.208 nicm 3792: struct window_copy_mode_data *data = wme->data;
1.54 nicm 3793: struct screen *back_s = data->backing;
1.140 nicm 3794: struct grid_cell gc;
1.67 nicm 3795: u_int px, py, xx;
1.52 nicm 3796:
3797: px = data->cx + 1;
1.54 nicm 3798: py = screen_hsize(back_s) + data->cy - data->oy;
1.208 nicm 3799: xx = window_copy_find_length(wme, py);
1.52 nicm 3800:
3801: while (px < xx) {
1.140 nicm 3802: grid_get_cell(back_s->grid, px, py, &gc);
3803: if (!(gc.flags & GRID_FLAG_PADDING) &&
3804: gc.data.size == 1 && *gc.data.data == data->jumpchar) {
1.208 nicm 3805: window_copy_update_cursor(wme, px, data->cy);
1.252 nicm 3806: if (window_copy_update_selection(wme, 1, 0))
1.208 nicm 3807: window_copy_redraw_lines(wme, data->cy, 1);
1.52 nicm 3808: return;
3809: }
3810: px++;
3811: }
3812: }
3813:
1.157 nicm 3814: static void
1.208 nicm 3815: window_copy_cursor_jump_back(struct window_mode_entry *wme)
1.52 nicm 3816: {
1.208 nicm 3817: struct window_copy_mode_data *data = wme->data;
1.54 nicm 3818: struct screen *back_s = data->backing;
1.140 nicm 3819: struct grid_cell gc;
1.67 nicm 3820: u_int px, py;
1.52 nicm 3821:
3822: px = data->cx;
1.54 nicm 3823: py = screen_hsize(back_s) + data->cy - data->oy;
1.52 nicm 3824:
3825: if (px > 0)
3826: px--;
3827:
3828: for (;;) {
1.140 nicm 3829: grid_get_cell(back_s->grid, px, py, &gc);
3830: if (!(gc.flags & GRID_FLAG_PADDING) &&
3831: gc.data.size == 1 && *gc.data.data == data->jumpchar) {
1.208 nicm 3832: window_copy_update_cursor(wme, px, data->cy);
1.252 nicm 3833: if (window_copy_update_selection(wme, 1, 0))
1.208 nicm 3834: window_copy_redraw_lines(wme, data->cy, 1);
1.76 nicm 3835: return;
3836: }
3837: if (px == 0)
3838: break;
3839: px--;
3840: }
3841: }
3842:
1.157 nicm 3843: static void
1.208 nicm 3844: window_copy_cursor_jump_to(struct window_mode_entry *wme)
1.76 nicm 3845: {
1.208 nicm 3846: struct window_copy_mode_data *data = wme->data;
1.76 nicm 3847: struct screen *back_s = data->backing;
1.140 nicm 3848: struct grid_cell gc;
1.76 nicm 3849: u_int px, py, xx;
3850:
1.184 nicm 3851: px = data->cx + 2;
1.76 nicm 3852: py = screen_hsize(back_s) + data->cy - data->oy;
1.208 nicm 3853: xx = window_copy_find_length(wme, py);
1.76 nicm 3854:
3855: while (px < xx) {
1.140 nicm 3856: grid_get_cell(back_s->grid, px, py, &gc);
3857: if (!(gc.flags & GRID_FLAG_PADDING) &&
3858: gc.data.size == 1 && *gc.data.data == data->jumpchar) {
1.208 nicm 3859: window_copy_update_cursor(wme, px - 1, data->cy);
1.252 nicm 3860: if (window_copy_update_selection(wme, 1, 0))
1.208 nicm 3861: window_copy_redraw_lines(wme, data->cy, 1);
1.76 nicm 3862: return;
3863: }
3864: px++;
3865: }
3866: }
3867:
1.157 nicm 3868: static void
1.208 nicm 3869: window_copy_cursor_jump_to_back(struct window_mode_entry *wme)
1.76 nicm 3870: {
1.208 nicm 3871: struct window_copy_mode_data *data = wme->data;
1.76 nicm 3872: struct screen *back_s = data->backing;
1.140 nicm 3873: struct grid_cell gc;
1.76 nicm 3874: u_int px, py;
3875:
3876: px = data->cx;
3877: py = screen_hsize(back_s) + data->cy - data->oy;
3878:
3879: if (px > 0)
1.127 nicm 3880: px--;
3881:
1.184 nicm 3882: if (px > 0)
1.76 nicm 3883: px--;
3884:
3885: for (;;) {
1.140 nicm 3886: grid_get_cell(back_s->grid, px, py, &gc);
3887: if (!(gc.flags & GRID_FLAG_PADDING) &&
3888: gc.data.size == 1 && *gc.data.data == data->jumpchar) {
1.208 nicm 3889: window_copy_update_cursor(wme, px + 1, data->cy);
1.252 nicm 3890: if (window_copy_update_selection(wme, 1, 0))
1.208 nicm 3891: window_copy_redraw_lines(wme, data->cy, 1);
1.52 nicm 3892: return;
3893: }
3894: if (px == 0)
3895: break;
3896: px--;
1.47 nicm 3897: }
1.1 nicm 3898: }
3899:
1.157 nicm 3900: static void
1.208 nicm 3901: window_copy_cursor_next_word(struct window_mode_entry *wme,
3902: const char *separators)
1.40 nicm 3903: {
1.208 nicm 3904: struct window_copy_mode_data *data = wme->data;
1.54 nicm 3905: struct screen *back_s = data->backing;
1.40 nicm 3906: u_int px, py, xx, yy;
1.48 nicm 3907: int expected = 0;
1.40 nicm 3908:
3909: px = data->cx;
1.54 nicm 3910: py = screen_hsize(back_s) + data->cy - data->oy;
1.208 nicm 3911: xx = window_copy_find_length(wme, py);
1.54 nicm 3912: yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
1.40 nicm 3913:
1.48 nicm 3914: /*
3915: * First skip past any nonword characters and then any word characters.
3916: *
3917: * expected is initially set to 0 for the former and then 1 for the
3918: * latter.
3919: */
3920: do {
3921: while (px > xx ||
1.208 nicm 3922: window_copy_in_set(wme, px, py, separators) == expected) {
1.48 nicm 3923: /* Move down if we're past the end of the line. */
3924: if (px > xx) {
3925: if (py == yy)
3926: return;
1.208 nicm 3927: window_copy_cursor_down(wme, 0);
1.48 nicm 3928: px = 0;
3929:
1.54 nicm 3930: py = screen_hsize(back_s) + data->cy - data->oy;
1.208 nicm 3931: xx = window_copy_find_length(wme, py);
1.48 nicm 3932: } else
3933: px++;
3934: }
3935: expected = !expected;
3936: } while (expected == 1);
1.40 nicm 3937:
1.208 nicm 3938: window_copy_update_cursor(wme, px, data->cy);
1.252 nicm 3939: if (window_copy_update_selection(wme, 1, 0))
1.208 nicm 3940: window_copy_redraw_lines(wme, data->cy, 1);
1.40 nicm 3941: }
3942:
1.157 nicm 3943: static void
1.247 nicm 3944: window_copy_cursor_next_word_end_pos(struct window_mode_entry *wme,
3945: const char *separators, u_int *ppx, u_int *ppy)
3946: {
3947: struct window_pane *wp = wme->wp;
3948: struct window_copy_mode_data *data = wme->data;
3949: struct options *oo = wp->window->options;
3950: struct screen *back_s = data->backing;
3951: u_int px, py, xx, yy;
3952: int keys, expected = 1;
3953:
3954: px = data->cx;
3955: py = screen_hsize(back_s) + data->cy - data->oy;
3956: xx = window_copy_find_length(wme, py);
3957: yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
3958:
3959: keys = options_get_number(oo, "mode-keys");
3960: if (keys == MODEKEY_VI && !window_copy_in_set(wme, px, py, separators))
3961: px++;
3962:
3963: /*
3964: * First skip past any word characters, then any non-word characters.
3965: *
3966: * expected is initially set to 1 for the former and then 0 for the
3967: * latter.
3968: */
3969: do {
3970: while (px > xx ||
3971: window_copy_in_set(wme, px, py, separators) == expected) {
3972: /* Move down if we're past the end of the line. */
3973: if (px > xx) {
3974: if (py == yy)
3975: return;
3976: py++;
3977: px = 0;
3978: xx = window_copy_find_length(wme, py);
3979: } else
3980: px++;
3981: }
3982: expected = !expected;
3983: } while (expected == 0);
3984:
3985: if (keys == MODEKEY_VI && px != 0)
3986: px--;
3987:
3988: *ppx = px;
3989: *ppy = py;
3990: }
3991:
3992: static void
1.208 nicm 3993: window_copy_cursor_next_word_end(struct window_mode_entry *wme,
1.255 nicm 3994: const char *separators, int no_reset)
1.1 nicm 3995: {
1.208 nicm 3996: struct window_pane *wp = wme->wp;
3997: struct window_copy_mode_data *data = wme->data;
1.136 nicm 3998: struct options *oo = wp->window->options;
1.54 nicm 3999: struct screen *back_s = data->backing;
1.39 nicm 4000: u_int px, py, xx, yy;
1.94 nicm 4001: int keys, expected = 1;
1.1 nicm 4002:
1.18 nicm 4003: px = data->cx;
1.54 nicm 4004: py = screen_hsize(back_s) + data->cy - data->oy;
1.208 nicm 4005: xx = window_copy_find_length(wme, py);
1.54 nicm 4006: yy = screen_hsize(back_s) + screen_size_y(back_s) - 1;
1.1 nicm 4007:
1.94 nicm 4008: keys = options_get_number(oo, "mode-keys");
1.208 nicm 4009: if (keys == MODEKEY_VI && !window_copy_in_set(wme, px, py, separators))
1.94 nicm 4010: px++;
4011:
1.48 nicm 4012: /*
4013: * First skip past any word characters, then any nonword characters.
4014: *
4015: * expected is initially set to 1 for the former and then 0 for the
4016: * latter.
4017: */
4018: do {
4019: while (px > xx ||
1.208 nicm 4020: window_copy_in_set(wme, px, py, separators) == expected) {
1.48 nicm 4021: /* Move down if we're past the end of the line. */
4022: if (px > xx) {
4023: if (py == yy)
4024: return;
1.208 nicm 4025: window_copy_cursor_down(wme, 0);
1.48 nicm 4026: px = 0;
4027:
1.54 nicm 4028: py = screen_hsize(back_s) + data->cy - data->oy;
1.208 nicm 4029: xx = window_copy_find_length(wme, py);
1.48 nicm 4030: } else
4031: px++;
4032: }
4033: expected = !expected;
4034: } while (expected == 0);
1.92 nicm 4035:
1.94 nicm 4036: if (keys == MODEKEY_VI && px != 0)
1.92 nicm 4037: px--;
1.18 nicm 4038:
1.208 nicm 4039: window_copy_update_cursor(wme, px, data->cy);
1.255 nicm 4040: if (window_copy_update_selection(wme, 1, no_reset))
1.208 nicm 4041: window_copy_redraw_lines(wme, data->cy, 1);
1.1 nicm 4042: }
4043:
1.247 nicm 4044: /* Compute the previous place where a word begins. */
4045: static void
4046: window_copy_cursor_previous_word_pos(struct window_mode_entry *wme,
4047: const char *separators, int already, u_int *ppx, u_int *ppy)
4048: {
4049: struct window_copy_mode_data *data = wme->data;
4050: u_int px, py;
4051:
4052: px = data->cx;
4053: py = screen_hsize(data->backing) + data->cy - data->oy;
4054:
4055: /* Move back to the previous word character. */
4056: if (already || window_copy_in_set(wme, px, py, separators)) {
4057: for (;;) {
4058: if (px > 0) {
4059: px--;
4060: if (!window_copy_in_set(wme, px, py,
4061: separators))
4062: break;
4063: } else {
4064: if (data->cy == 0 &&
4065: (screen_hsize(data->backing) == 0 ||
4066: data->oy >=
4067: screen_hsize(data->backing) - 1))
4068: goto out;
4069: py--;
4070:
4071: py = screen_hsize(data->backing) + data->cy -
4072: data->oy;
4073: px = window_copy_find_length(wme, py);
4074:
4075: /* Stop if separator at EOL. */
4076: if (px > 0 && window_copy_in_set(wme, px - 1,
4077: py, separators))
4078: break;
4079: }
4080: }
4081: }
4082:
4083: /* Move back to the beginning of this word. */
4084: while (px > 0 && !window_copy_in_set(wme, px - 1, py, separators))
4085: px--;
4086:
4087: out:
4088: *ppx = px;
4089: *ppy = py;
4090: }
4091:
1.8 nicm 4092: /* Move to the previous place where a word begins. */
1.157 nicm 4093: static void
1.208 nicm 4094: window_copy_cursor_previous_word(struct window_mode_entry *wme,
1.229 nicm 4095: const char *separators, int already)
1.1 nicm 4096: {
1.208 nicm 4097: struct window_copy_mode_data *data = wme->data;
1.8 nicm 4098: u_int px, py;
1.1 nicm 4099:
1.18 nicm 4100: px = data->cx;
1.54 nicm 4101: py = screen_hsize(data->backing) + data->cy - data->oy;
1.1 nicm 4102:
1.8 nicm 4103: /* Move back to the previous word character. */
1.229 nicm 4104: if (already || window_copy_in_set(wme, px, py, separators)) {
4105: for (;;) {
4106: if (px > 0) {
4107: px--;
1.247 nicm 4108: if (!window_copy_in_set(wme, px, py,
4109: separators))
1.229 nicm 4110: break;
4111: } else {
4112: if (data->cy == 0 &&
4113: (screen_hsize(data->backing) == 0 ||
1.247 nicm 4114: data->oy >=
4115: screen_hsize(data->backing) - 1))
1.229 nicm 4116: goto out;
4117: window_copy_cursor_up(wme, 0);
4118:
1.247 nicm 4119: py = screen_hsize(data->backing) + data->cy -
4120: data->oy;
1.229 nicm 4121: px = window_copy_find_length(wme, py);
4122:
4123: /* Stop if separator at EOL. */
1.247 nicm 4124: if (px > 0 && window_copy_in_set(wme, px - 1,
4125: py, separators))
1.229 nicm 4126: break;
4127: }
1.1 nicm 4128: }
1.8 nicm 4129: }
1.1 nicm 4130:
1.8 nicm 4131: /* Move back to the beginning of this word. */
1.208 nicm 4132: while (px > 0 && !window_copy_in_set(wme, px - 1, py, separators))
1.1 nicm 4133: px--;
1.8 nicm 4134:
1.1 nicm 4135: out:
1.208 nicm 4136: window_copy_update_cursor(wme, px, data->cy);
1.252 nicm 4137: if (window_copy_update_selection(wme, 1, 0))
1.208 nicm 4138: window_copy_redraw_lines(wme, data->cy, 1);
1.1 nicm 4139: }
4140:
1.157 nicm 4141: static void
1.208 nicm 4142: window_copy_scroll_up(struct window_mode_entry *wme, u_int ny)
1.1 nicm 4143: {
1.208 nicm 4144: struct window_pane *wp = wme->wp;
4145: struct window_copy_mode_data *data = wme->data;
1.1 nicm 4146: struct screen *s = &data->screen;
4147: struct screen_write_ctx ctx;
4148:
4149: if (data->oy < ny)
4150: ny = data->oy;
4151: if (ny == 0)
4152: return;
4153: data->oy -= ny;
4154:
1.252 nicm 4155: window_copy_update_selection(wme, 0, 0);
1.96 nicm 4156:
1.1 nicm 4157: screen_write_start(&ctx, wp, NULL);
1.212 nicm 4158: screen_write_cursormove(&ctx, 0, 0, 0);
1.159 nicm 4159: screen_write_deleteline(&ctx, ny, 8);
1.208 nicm 4160: window_copy_write_lines(wme, &ctx, screen_size_y(s) - ny, ny);
4161: window_copy_write_line(wme, &ctx, 0);
1.31 nicm 4162: if (screen_size_y(s) > 1)
1.208 nicm 4163: window_copy_write_line(wme, &ctx, 1);
1.31 nicm 4164: if (screen_size_y(s) > 3)
1.208 nicm 4165: window_copy_write_line(wme, &ctx, screen_size_y(s) - 2);
1.192 nicm 4166: if (s->sel != NULL && screen_size_y(s) > ny)
1.208 nicm 4167: window_copy_write_line(wme, &ctx, screen_size_y(s) - ny - 1);
1.212 nicm 4168: screen_write_cursormove(&ctx, data->cx, data->cy, 0);
1.1 nicm 4169: screen_write_stop(&ctx);
4170: }
4171:
1.157 nicm 4172: static void
1.208 nicm 4173: window_copy_scroll_down(struct window_mode_entry *wme, u_int ny)
1.1 nicm 4174: {
1.208 nicm 4175: struct window_pane *wp = wme->wp;
4176: struct window_copy_mode_data *data = wme->data;
1.1 nicm 4177: struct screen *s = &data->screen;
4178: struct screen_write_ctx ctx;
4179:
1.54 nicm 4180: if (ny > screen_hsize(data->backing))
1.1 nicm 4181: return;
4182:
1.54 nicm 4183: if (data->oy > screen_hsize(data->backing) - ny)
4184: ny = screen_hsize(data->backing) - data->oy;
1.1 nicm 4185: if (ny == 0)
4186: return;
4187: data->oy += ny;
4188:
1.252 nicm 4189: window_copy_update_selection(wme, 0, 0);
1.96 nicm 4190:
1.1 nicm 4191: screen_write_start(&ctx, wp, NULL);
1.212 nicm 4192: screen_write_cursormove(&ctx, 0, 0, 0);
1.159 nicm 4193: screen_write_insertline(&ctx, ny, 8);
1.208 nicm 4194: window_copy_write_lines(wme, &ctx, 0, ny);
1.192 nicm 4195: if (s->sel != NULL && screen_size_y(s) > ny)
1.208 nicm 4196: window_copy_write_line(wme, &ctx, ny);
1.96 nicm 4197: else if (ny == 1) /* nuke position */
1.208 nicm 4198: window_copy_write_line(wme, &ctx, 1);
1.212 nicm 4199: screen_write_cursormove(&ctx, data->cx, data->cy, 0);
1.1 nicm 4200: screen_write_stop(&ctx);
4201: }
1.42 nicm 4202:
1.157 nicm 4203: static void
1.208 nicm 4204: window_copy_rectangle_toggle(struct window_mode_entry *wme)
1.42 nicm 4205: {
1.208 nicm 4206: struct window_copy_mode_data *data = wme->data;
1.47 nicm 4207: u_int px, py;
1.42 nicm 4208:
4209: data->rectflag = !data->rectflag;
1.47 nicm 4210:
1.54 nicm 4211: py = screen_hsize(data->backing) + data->cy - data->oy;
1.208 nicm 4212: px = window_copy_find_length(wme, py);
1.47 nicm 4213: if (data->cx > px)
1.208 nicm 4214: window_copy_update_cursor(wme, px, data->cy);
1.42 nicm 4215:
1.252 nicm 4216: window_copy_update_selection(wme, 1, 0);
1.208 nicm 4217: window_copy_redraw_screen(wme);
1.156 nicm 4218: }
4219:
1.157 nicm 4220: static void
1.156 nicm 4221: window_copy_move_mouse(struct mouse_event *m)
4222: {
1.211 nicm 4223: struct window_pane *wp;
4224: struct window_mode_entry *wme;
4225: u_int x, y;
1.156 nicm 4226:
4227: wp = cmd_mouse_pane(m, NULL, NULL);
1.211 nicm 4228: if (wp == NULL)
4229: return;
4230: wme = TAILQ_FIRST(&wp->modes);
1.223 nicm 4231: if (wme == NULL)
4232: return;
4233: if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
1.156 nicm 4234: return;
4235:
1.183 nicm 4236: if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
1.156 nicm 4237: return;
4238:
1.211 nicm 4239: window_copy_update_cursor(wme, x, y);
1.126 nicm 4240: }
4241:
4242: void
1.141 nicm 4243: window_copy_start_drag(struct client *c, struct mouse_event *m)
1.126 nicm 4244: {
1.211 nicm 4245: struct window_pane *wp;
4246: struct window_mode_entry *wme;
1.247 nicm 4247: struct window_copy_mode_data *data;
1.248 nicm 4248: u_int x, y, yg;
1.126 nicm 4249:
1.155 nicm 4250: if (c == NULL)
4251: return;
4252:
1.126 nicm 4253: wp = cmd_mouse_pane(m, NULL, NULL);
1.211 nicm 4254: if (wp == NULL)
4255: return;
4256: wme = TAILQ_FIRST(&wp->modes);
1.223 nicm 4257: if (wme == NULL)
4258: return;
4259: if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
1.126 nicm 4260: return;
4261:
4262: if (cmd_mouse_at(wp, m, &x, &y, 1) != 0)
4263: return;
4264:
4265: c->tty.mouse_drag_update = window_copy_drag_update;
1.217 nicm 4266: c->tty.mouse_drag_release = window_copy_drag_release;
1.126 nicm 4267:
1.247 nicm 4268: data = wme->data;
1.248 nicm 4269: yg = screen_hsize(data->backing) + y - data->oy;
4270: if (x < data->selrx || x > data->endselrx || yg != data->selry)
4271: data->selflag = SEL_CHAR;
1.247 nicm 4272: switch (data->selflag) {
4273: case SEL_WORD:
4274: if (data->ws) {
4275: window_copy_update_cursor(wme, x, y);
4276: window_copy_cursor_previous_word_pos(wme,
4277: data->ws, 0, &x, &y);
4278: y -= screen_hsize(data->backing) - data->oy;
4279: }
4280: window_copy_update_cursor(wme, x, y);
4281: break;
4282: case SEL_LINE:
4283: window_copy_update_cursor(wme, 0, y);
4284: break;
4285: case SEL_CHAR:
4286: window_copy_update_cursor(wme, x, y);
4287: window_copy_start_selection(wme);
4288: break;
4289: }
4290:
1.211 nicm 4291: window_copy_redraw_screen(wme);
1.220 nicm 4292: window_copy_drag_update(c, m);
1.126 nicm 4293: }
4294:
1.157 nicm 4295: static void
1.211 nicm 4296: window_copy_drag_update(struct client *c, struct mouse_event *m)
1.126 nicm 4297: {
4298: struct window_pane *wp;
1.211 nicm 4299: struct window_mode_entry *wme;
1.126 nicm 4300: struct window_copy_mode_data *data;
1.217 nicm 4301: u_int x, y, old_cx, old_cy;
4302: struct timeval tv = {
4303: .tv_usec = WINDOW_COPY_DRAG_REPEAT_TIME
4304: };
1.126 nicm 4305:
1.211 nicm 4306: if (c == NULL)
4307: return;
4308:
1.126 nicm 4309: wp = cmd_mouse_pane(m, NULL, NULL);
1.211 nicm 4310: if (wp == NULL)
1.126 nicm 4311: return;
1.211 nicm 4312: wme = TAILQ_FIRST(&wp->modes);
1.223 nicm 4313: if (wme == NULL)
4314: return;
4315: if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
1.211 nicm 4316: return;
1.217 nicm 4317:
1.211 nicm 4318: data = wme->data;
1.217 nicm 4319: evtimer_del(&data->dragtimer);
1.126 nicm 4320:
4321: if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
4322: return;
1.217 nicm 4323: old_cx = data->cx;
1.126 nicm 4324: old_cy = data->cy;
4325:
1.211 nicm 4326: window_copy_update_cursor(wme, x, y);
1.252 nicm 4327: if (window_copy_update_selection(wme, 1, 0))
1.211 nicm 4328: window_copy_redraw_selection(wme, old_cy);
1.217 nicm 4329: if (old_cy != data->cy || old_cx == data->cx) {
4330: if (y == 0) {
4331: evtimer_add(&data->dragtimer, &tv);
4332: window_copy_cursor_up(wme, 1);
4333: } else if (y == screen_size_y(&data->screen) - 1) {
4334: evtimer_add(&data->dragtimer, &tv);
4335: window_copy_cursor_down(wme, 1);
4336: }
4337: }
4338: }
4339:
4340: static void
4341: window_copy_drag_release(struct client *c, struct mouse_event *m)
4342: {
4343: struct window_pane *wp;
4344: struct window_mode_entry *wme;
4345: struct window_copy_mode_data *data;
4346:
4347: if (c == NULL)
4348: return;
4349:
4350: wp = cmd_mouse_pane(m, NULL, NULL);
4351: if (wp == NULL)
4352: return;
4353: wme = TAILQ_FIRST(&wp->modes);
1.223 nicm 4354: if (wme == NULL)
4355: return;
4356: if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
1.217 nicm 4357: return;
4358:
4359: data = wme->data;
4360: evtimer_del(&data->dragtimer);
1.42 nicm 4361: }