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