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