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