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