Annotation of src/usr.bin/tmux/window-copy.c, Revision 1.333
1.333 ! nicm 1: /* $OpenBSD: window-copy.c,v 1.332 2021/12/20 09:02:12 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>
1.280 nicm 25: #include <time.h>
1.1 nicm 26:
27: #include "tmux.h"
28:
1.299 nicm 29: struct window_copy_mode_data;
30:
1.208 nicm 31: static const char *window_copy_key_table(struct window_mode_entry *);
32: static void window_copy_command(struct window_mode_entry *, struct client *,
1.205 nicm 33: struct session *, struct winlink *, struct args *,
34: struct mouse_event *);
1.208 nicm 35: static struct screen *window_copy_init(struct window_mode_entry *,
1.177 nicm 36: struct cmd_find_state *, struct args *);
1.210 nicm 37: static struct screen *window_copy_view_init(struct window_mode_entry *,
38: struct cmd_find_state *, struct args *);
1.208 nicm 39: static void window_copy_free(struct window_mode_entry *);
40: static void window_copy_resize(struct window_mode_entry *, u_int, u_int);
41: static void window_copy_formats(struct window_mode_entry *,
42: struct format_tree *);
43: static void window_copy_pageup1(struct window_mode_entry *, int);
44: static int window_copy_pagedown(struct window_mode_entry *, int, int);
45: static void window_copy_next_paragraph(struct window_mode_entry *);
46: static void window_copy_previous_paragraph(struct window_mode_entry *);
47: static void window_copy_redraw_selection(struct window_mode_entry *, u_int);
48: static void window_copy_redraw_lines(struct window_mode_entry *, u_int,
49: u_int);
50: static void window_copy_redraw_screen(struct window_mode_entry *);
51: static void window_copy_write_line(struct window_mode_entry *,
1.157 nicm 52: struct screen_write_ctx *, u_int);
1.208 nicm 53: static void window_copy_write_lines(struct window_mode_entry *,
1.157 nicm 54: struct screen_write_ctx *, u_int, u_int);
1.299 nicm 55: static char *window_copy_match_at_cursor(struct window_copy_mode_data *);
1.296 nicm 56: static void window_copy_scroll_to(struct window_mode_entry *, u_int, u_int,
57: int);
1.157 nicm 58: static int window_copy_search_compare(struct grid *, u_int, u_int,
59: struct grid *, u_int, int);
60: static int window_copy_search_lr(struct grid *, struct grid *, u_int *,
61: u_int, u_int, u_int, int);
62: static int window_copy_search_rl(struct grid *, struct grid *, u_int *,
63: u_int, u_int, u_int, int);
1.299 nicm 64: static int window_copy_last_regex(struct grid *, u_int, u_int, u_int,
65: u_int, u_int *, u_int *, const char *, const regex_t *,
66: int);
1.321 nicm 67: static int window_copy_search_mark_at(struct window_copy_mode_data *,
68: u_int, u_int, u_int *);
1.244 nicm 69: static char *window_copy_stringify(struct grid *, u_int, u_int, u_int,
70: char *, u_int *);
1.299 nicm 71: static void window_copy_cstrtocellpos(struct grid *, u_int, u_int *,
72: u_int *, const char *);
1.208 nicm 73: static int window_copy_search_marks(struct window_mode_entry *,
1.296 nicm 74: struct screen *, int, int);
1.208 nicm 75: static void window_copy_clear_marks(struct window_mode_entry *);
1.157 nicm 76: static int window_copy_is_lowercase(const char *);
1.317 nicm 77: static void window_copy_search_back_overlap(struct grid *, regex_t *,
78: u_int *, u_int *, u_int *, u_int);
1.208 nicm 79: static int window_copy_search_jump(struct window_mode_entry *,
80: struct grid *, struct grid *, u_int, u_int, u_int, int, int,
1.321 nicm 81: int, int);
82: static int window_copy_search(struct window_mode_entry *, int, int);
83: static int window_copy_search_up(struct window_mode_entry *, int);
84: static int window_copy_search_down(struct window_mode_entry *, int);
1.208 nicm 85: static void window_copy_goto_line(struct window_mode_entry *, const char *);
86: static void window_copy_update_cursor(struct window_mode_entry *, u_int,
87: u_int);
88: static void window_copy_start_selection(struct window_mode_entry *);
89: static int window_copy_adjust_selection(struct window_mode_entry *,
90: u_int *, u_int *);
1.252 nicm 91: static int window_copy_set_selection(struct window_mode_entry *, int, int);
92: static int window_copy_update_selection(struct window_mode_entry *, int,
93: int);
94: static void window_copy_synchronize_cursor(struct window_mode_entry *, int);
1.208 nicm 95: static void *window_copy_get_selection(struct window_mode_entry *, size_t *);
1.215 nicm 96: static void window_copy_copy_buffer(struct window_mode_entry *,
97: const char *, void *, size_t);
1.314 nicm 98: static void window_copy_pipe(struct window_mode_entry *,
99: struct session *, const char *);
1.208 nicm 100: static void window_copy_copy_pipe(struct window_mode_entry *,
1.215 nicm 101: struct session *, const char *, const char *);
102: static void window_copy_copy_selection(struct window_mode_entry *,
103: const char *);
1.208 nicm 104: static void window_copy_append_selection(struct window_mode_entry *);
105: static void window_copy_clear_selection(struct window_mode_entry *);
106: static void window_copy_copy_line(struct window_mode_entry *, char **,
107: size_t *, u_int, u_int, u_int);
108: static int window_copy_in_set(struct window_mode_entry *, u_int, u_int,
1.157 nicm 109: const char *);
1.208 nicm 110: static u_int window_copy_find_length(struct window_mode_entry *, u_int);
111: static void window_copy_cursor_start_of_line(struct window_mode_entry *);
112: static void window_copy_cursor_back_to_indentation(
113: struct window_mode_entry *);
114: static void window_copy_cursor_end_of_line(struct window_mode_entry *);
115: static void window_copy_other_end(struct window_mode_entry *);
116: static void window_copy_cursor_left(struct window_mode_entry *);
1.305 nicm 117: static void window_copy_cursor_right(struct window_mode_entry *, int);
1.208 nicm 118: static void window_copy_cursor_up(struct window_mode_entry *, int);
119: static void window_copy_cursor_down(struct window_mode_entry *, int);
120: static void window_copy_cursor_jump(struct window_mode_entry *);
121: static void window_copy_cursor_jump_back(struct window_mode_entry *);
122: static void window_copy_cursor_jump_to(struct window_mode_entry *);
123: static void window_copy_cursor_jump_to_back(struct window_mode_entry *);
124: static void window_copy_cursor_next_word(struct window_mode_entry *,
1.157 nicm 125: const char *);
1.247 nicm 126: static void window_copy_cursor_next_word_end_pos(struct window_mode_entry *,
127: const char *, u_int *, u_int *);
1.208 nicm 128: static void window_copy_cursor_next_word_end(struct window_mode_entry *,
1.255 nicm 129: const char *, int);
1.247 nicm 130: static void window_copy_cursor_previous_word_pos(struct window_mode_entry *,
1.323 nicm 131: const char *, u_int *, u_int *);
1.208 nicm 132: static void window_copy_cursor_previous_word(struct window_mode_entry *,
1.229 nicm 133: const char *, int);
1.208 nicm 134: static void window_copy_scroll_up(struct window_mode_entry *, u_int);
135: static void window_copy_scroll_down(struct window_mode_entry *, u_int);
1.313 nicm 136: static void window_copy_rectangle_set(struct window_mode_entry *, int);
1.157 nicm 137: static void window_copy_move_mouse(struct mouse_event *);
138: static void window_copy_drag_update(struct client *, struct mouse_event *);
1.217 nicm 139: static void window_copy_drag_release(struct client *, struct mouse_event *);
1.288 nicm 140: static void window_copy_jump_to_mark(struct window_mode_entry *);
1.315 nicm 141: static void window_copy_acquire_cursor_up(struct window_mode_entry *,
142: u_int, u_int, u_int, u_int, u_int);
143: static void window_copy_acquire_cursor_down(struct window_mode_entry *,
144: u_int, u_int, u_int, u_int, u_int, u_int, int);
1.1 nicm 145:
146: const struct window_mode window_copy_mode = {
1.173 nicm 147: .name = "copy-mode",
148:
1.155 nicm 149: .init = window_copy_init,
150: .free = window_copy_free,
151: .resize = window_copy_resize,
152: .key_table = window_copy_key_table,
153: .command = window_copy_command,
1.206 nicm 154: .formats = window_copy_formats,
1.1 nicm 155: };
156:
1.210 nicm 157: const struct window_mode window_view_mode = {
158: .name = "view-mode",
159:
160: .init = window_copy_view_init,
161: .free = window_copy_free,
162: .resize = window_copy_resize,
163: .key_table = window_copy_key_table,
164: .command = window_copy_command,
165: .formats = window_copy_formats,
166: };
167:
1.155 nicm 168: enum {
1.21 nicm 169: WINDOW_COPY_OFF,
170: WINDOW_COPY_SEARCHUP,
171: WINDOW_COPY_SEARCHDOWN,
1.52 nicm 172: WINDOW_COPY_JUMPFORWARD,
1.155 nicm 173: WINDOW_COPY_JUMPBACKWARD,
1.76 nicm 174: WINDOW_COPY_JUMPTOFORWARD,
1.155 nicm 175: WINDOW_COPY_JUMPTOBACKWARD,
1.21 nicm 176: };
177:
1.161 nicm 178: enum {
179: WINDOW_COPY_REL_POS_ABOVE,
180: WINDOW_COPY_REL_POS_ON_SCREEN,
181: WINDOW_COPY_REL_POS_BELOW,
182: };
183:
1.213 nicm 184: enum window_copy_cmd_action {
185: WINDOW_COPY_CMD_NOTHING,
186: WINDOW_COPY_CMD_REDRAW,
187: WINDOW_COPY_CMD_CANCEL,
188: };
189:
1.295 nicm 190: enum window_copy_cmd_clear {
191: WINDOW_COPY_CMD_CLEAR_ALWAYS,
192: WINDOW_COPY_CMD_CLEAR_NEVER,
193: WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
194: };
195:
1.213 nicm 196: struct window_copy_cmd_state {
197: struct window_mode_entry *wme;
198: struct args *args;
199: struct mouse_event *m;
1.215 nicm 200:
1.213 nicm 201: struct client *c;
202: struct session *s;
1.215 nicm 203: struct winlink *wl;
1.213 nicm 204: };
205:
1.54 nicm 206: /*
1.161 nicm 207: * Copy mode's visible screen (the "screen" field) is filled from one of two
208: * sources: the original contents of the pane (used when we actually enter via
209: * the "copy-mode" command, to copy the contents of the current pane), or else
210: * a series of lines containing the output from an output-writing tmux command
211: * (such as any of the "show-*" or "list-*" commands).
1.54 nicm 212: *
1.161 nicm 213: * In either case, the full content of the copy-mode grid is pointed at by the
214: * "backing" field, and is copied into "screen" as needed (that is, when
215: * scrolling occurs). When copy-mode is backed by a pane, backing points
216: * directly at that pane's screen structure (&wp->base); when backed by a list
217: * of output-lines from a command, it points at a newly-allocated screen
218: * structure (which is deallocated when the mode ends).
1.54 nicm 219: */
1.1 nicm 220: struct window_copy_mode_data {
1.155 nicm 221: struct screen screen;
1.1 nicm 222:
1.155 nicm 223: struct screen *backing;
224: int backing_written; /* backing display started */
1.54 nicm 225:
1.266 nicm 226: int viewmode; /* view mode entered */
227:
1.192 nicm 228: u_int oy; /* number of lines scrolled up */
1.21 nicm 229:
1.192 nicm 230: u_int selx; /* beginning of selection */
1.155 nicm 231: u_int sely;
1.21 nicm 232:
1.192 nicm 233: u_int endselx; /* end of selection */
1.161 nicm 234: u_int endsely;
235:
236: enum {
237: CURSORDRAG_NONE, /* selection is independent of cursor */
238: CURSORDRAG_ENDSEL, /* end is synchronized with cursor */
239: CURSORDRAG_SEL, /* start is synchronized with cursor */
240: } cursordrag;
241:
1.192 nicm 242: int modekeys;
243: enum {
244: LINE_SEL_NONE,
245: LINE_SEL_LEFT_RIGHT,
246: LINE_SEL_RIGHT_LEFT,
247: } lineflag; /* line selection mode */
1.155 nicm 248: int rectflag; /* in rectangle copy mode? */
249: int scroll_exit; /* exit on scroll to end? */
1.249 nicm 250: int hide_position; /* hide position marker */
1.1 nicm 251:
1.247 nicm 252: enum {
253: SEL_CHAR, /* select one char at a time */
254: SEL_WORD, /* select one word at a time */
255: SEL_LINE, /* select one line at a time */
256: } selflag;
257:
1.323 nicm 258: const char *separators; /* word separators */
1.247 nicm 259:
260: u_int dx; /* drag start position */
261: u_int dy;
262:
263: u_int selrx; /* selection reset positions */
264: u_int selry;
265: u_int endselrx;
266: u_int endselry;
267:
1.155 nicm 268: u_int cx;
269: u_int cy;
1.42 nicm 270:
1.192 nicm 271: u_int lastcx; /* position in last line w/ content */
272: u_int lastsx; /* size of last line w/ content */
1.1 nicm 273:
1.288 nicm 274: u_int mx; /* mark position */
275: u_int my;
276: int showmark;
277:
1.155 nicm 278: int searchtype;
1.321 nicm 279: int searchdirection;
1.259 nicm 280: int searchregex;
1.155 nicm 281: char *searchstr;
1.282 nicm 282: u_char *searchmark;
1.284 nicm 283: int searchcount;
284: int searchmore;
1.308 nicm 285: int searchall;
1.163 nicm 286: int searchx;
287: int searchy;
288: int searcho;
1.282 nicm 289: u_char searchgen;
1.25 nicm 290:
1.263 nicm 291: int timeout; /* search has timed out */
1.284 nicm 292: #define WINDOW_COPY_SEARCH_TIMEOUT 10000
293: #define WINDOW_COPY_SEARCH_ALL_TIMEOUT 200
1.263 nicm 294:
1.315 nicm 295: int jumptype;
296: struct utf8_data *jumpchar;
1.217 nicm 297:
298: struct event dragtimer;
299: #define WINDOW_COPY_DRAG_REPEAT_TIME 50000
1.1 nicm 300: };
301:
1.217 nicm 302: static void
303: window_copy_scroll_timer(__unused int fd, __unused short events, void *arg)
304: {
305: struct window_mode_entry *wme = arg;
306: struct window_pane *wp = wme->wp;
307: struct window_copy_mode_data *data = wme->data;
308: struct timeval tv = {
309: .tv_usec = WINDOW_COPY_DRAG_REPEAT_TIME
310: };
311:
312: evtimer_del(&data->dragtimer);
313:
314: if (TAILQ_FIRST(&wp->modes) != wme)
315: return;
316:
317: if (data->cy == 0) {
318: evtimer_add(&data->dragtimer, &tv);
319: window_copy_cursor_up(wme, 1);
320: } else if (data->cy == screen_size_y(&data->screen) - 1) {
321: evtimer_add(&data->dragtimer, &tv);
322: window_copy_cursor_down(wme, 1);
323: }
324: }
325:
1.266 nicm 326: static struct screen *
1.274 nicm 327: window_copy_clone_screen(struct screen *src, struct screen *hint, u_int *cx,
1.279 nicm 328: u_int *cy, int trim)
1.266 nicm 329: {
1.274 nicm 330: struct screen *dst;
331: const struct grid_line *gl;
1.285 nicm 332: u_int sy, wx, wy;
333: int reflow;
1.266 nicm 334:
335: dst = xcalloc(1, sizeof *dst);
1.270 nicm 336:
337: sy = screen_hsize(src) + screen_size_y(src);
1.279 nicm 338: if (trim) {
339: while (sy > screen_hsize(src)) {
340: gl = grid_peek_line(src->grid, sy - 1);
341: if (gl->cellused != 0)
342: break;
343: sy--;
344: }
1.274 nicm 345: }
1.278 nicm 346: log_debug("%s: target screen is %ux%u, source %ux%u", __func__,
347: screen_size_x(src), sy, screen_size_x(hint),
348: screen_hsize(src) + screen_size_y(src));
1.274 nicm 349: screen_init(dst, screen_size_x(src), sy, screen_hlimit(src));
1.285 nicm 350:
351: /*
352: * Ensure history is on for the backing grid so lines are not deleted
353: * during resizing.
354: */
355: dst->grid->flags |= GRID_HISTORY;
1.274 nicm 356: grid_duplicate_lines(dst->grid, 0, src->grid, 0, sy);
1.270 nicm 357:
1.274 nicm 358: dst->grid->sy = sy - screen_hsize(src);
359: dst->grid->hsize = screen_hsize(src);
360: dst->grid->hscrolled = src->grid->hscrolled;
1.278 nicm 361: if (src->cy > dst->grid->sy - 1) {
362: dst->cx = 0;
363: dst->cy = dst->grid->sy - 1;
364: } else {
365: dst->cx = src->cx;
366: dst->cy = src->cy;
367: }
1.266 nicm 368:
1.285 nicm 369: if (cx != NULL && cy != NULL) {
370: *cx = dst->cx;
371: *cy = screen_hsize(dst) + dst->cy;
372: reflow = (screen_size_x(hint) != screen_size_x(dst));
373: }
374: else
375: reflow = 0;
376: if (reflow)
377: grid_wrap_position(dst->grid, *cx, *cy, &wx, &wy);
1.274 nicm 378: screen_resize_cursor(dst, screen_size_x(hint), screen_size_y(hint), 1,
1.285 nicm 379: 0, 0);
380: if (reflow)
381: grid_unwrap_position(dst->grid, cx, cy, wx, wy);
1.266 nicm 382:
383: return (dst);
384: }
385:
1.210 nicm 386: static struct window_copy_mode_data *
387: window_copy_common_init(struct window_mode_entry *wme)
1.1 nicm 388: {
1.208 nicm 389: struct window_pane *wp = wme->wp;
1.1 nicm 390: struct window_copy_mode_data *data;
1.210 nicm 391: struct screen *base = &wp->base;
1.1 nicm 392:
1.208 nicm 393: wme->data = data = xcalloc(1, sizeof *data);
1.1 nicm 394:
1.161 nicm 395: data->cursordrag = CURSORDRAG_NONE;
1.193 nicm 396: data->lineflag = LINE_SEL_NONE;
1.250 nicm 397: data->selflag = SEL_CHAR;
1.42 nicm 398:
1.174 nicm 399: if (wp->searchstr != NULL) {
400: data->searchtype = WINDOW_COPY_SEARCHUP;
1.259 nicm 401: data->searchregex = wp->searchregex;
1.174 nicm 402: data->searchstr = xstrdup(wp->searchstr);
403: } else {
404: data->searchtype = WINDOW_COPY_OFF;
1.259 nicm 405: data->searchregex = 0;
1.174 nicm 406: data->searchstr = NULL;
407: }
1.163 nicm 408: data->searchx = data->searchy = data->searcho = -1;
1.308 nicm 409: data->searchall = 1;
1.21 nicm 410:
1.52 nicm 411: data->jumptype = WINDOW_COPY_OFF;
1.315 nicm 412: data->jumpchar = NULL;
1.52 nicm 413:
1.210 nicm 414: screen_init(&data->screen, screen_size_x(base), screen_size_y(base), 0);
1.192 nicm 415: data->modekeys = options_get_number(wp->window->options, "mode-keys");
1.1 nicm 416:
1.217 nicm 417: evtimer_set(&data->dragtimer, window_copy_scroll_timer, wme);
418:
1.210 nicm 419: return (data);
1.54 nicm 420: }
421:
1.210 nicm 422: static struct screen *
423: window_copy_init(struct window_mode_entry *wme,
424: __unused struct cmd_find_state *fs, struct args *args)
1.54 nicm 425: {
1.270 nicm 426: struct window_pane *wp = wme->swp;
1.210 nicm 427: struct window_copy_mode_data *data;
1.274 nicm 428: struct screen *base = &wp->base;
1.210 nicm 429: struct screen_write_ctx ctx;
1.274 nicm 430: u_int i, cx, cy;
1.54 nicm 431:
1.210 nicm 432: data = window_copy_common_init(wme);
1.279 nicm 433: data->backing = window_copy_clone_screen(base, &data->screen, &cx, &cy,
434: wme->swp != wme->wp);
1.210 nicm 435:
1.285 nicm 436: data->cx = cx;
1.274 nicm 437: if (cy < screen_hsize(data->backing)) {
1.272 nicm 438: data->cy = 0;
1.274 nicm 439: data->oy = screen_hsize(data->backing) - cy;
440: } else {
1.285 nicm 441: data->cy = cy - screen_hsize(data->backing);
1.274 nicm 442: data->oy = 0;
443: }
1.54 nicm 444:
1.210 nicm 445: data->scroll_exit = args_has(args, 'e');
1.249 nicm 446: data->hide_position = args_has(args, 'H');
1.1 nicm 447:
1.210 nicm 448: data->screen.cx = data->cx;
449: data->screen.cy = data->cy;
1.288 nicm 450: data->mx = data->cx;
451: data->my = screen_hsize(data->backing) + data->cy - data->oy;
452: data->showmark = 0;
1.210 nicm 453:
1.283 nicm 454: screen_write_start(&ctx, &data->screen);
1.210 nicm 455: for (i = 0; i < screen_size_y(&data->screen); i++)
1.208 nicm 456: window_copy_write_line(wme, &ctx, i);
1.212 nicm 457: screen_write_cursormove(&ctx, data->cx, data->cy, 0);
1.1 nicm 458: screen_write_stop(&ctx);
1.210 nicm 459:
460: return (&data->screen);
1.54 nicm 461: }
1.1 nicm 462:
1.210 nicm 463: static struct screen *
464: window_copy_view_init(struct window_mode_entry *wme,
465: __unused struct cmd_find_state *fs, __unused struct args *args)
1.54 nicm 466: {
1.210 nicm 467: struct window_pane *wp = wme->wp;
1.208 nicm 468: struct window_copy_mode_data *data;
1.210 nicm 469: struct screen *base = &wp->base;
470: struct screen *s;
1.54 nicm 471:
1.210 nicm 472: data = window_copy_common_init(wme);
1.266 nicm 473: data->viewmode = 1;
1.208 nicm 474:
1.210 nicm 475: data->backing = s = xmalloc(sizeof *data->backing);
476: screen_init(s, screen_size_x(base), screen_size_y(base), UINT_MAX);
1.288 nicm 477: data->mx = data->cx;
478: data->my = screen_hsize(data->backing) + data->cy - data->oy;
479: data->showmark = 0;
1.207 nicm 480:
1.210 nicm 481: return (&data->screen);
1.1 nicm 482: }
483:
1.157 nicm 484: static void
1.208 nicm 485: window_copy_free(struct window_mode_entry *wme)
1.1 nicm 486: {
1.208 nicm 487: struct window_copy_mode_data *data = wme->data;
1.46 nicm 488:
1.217 nicm 489: evtimer_del(&data->dragtimer);
490:
1.162 nicm 491: free(data->searchmark);
1.81 nicm 492: free(data->searchstr);
1.315 nicm 493: free(data->jumpchar);
1.21 nicm 494:
1.266 nicm 495: screen_free(data->backing);
496: free(data->backing);
497:
1.1 nicm 498: screen_free(&data->screen);
1.81 nicm 499: free(data);
1.1 nicm 500: }
501:
502: void
1.54 nicm 503: window_copy_add(struct window_pane *wp, const char *fmt, ...)
504: {
505: va_list ap;
506:
507: va_start(ap, fmt);
508: window_copy_vadd(wp, fmt, ap);
509: va_end(ap);
510: }
511:
512: void
513: window_copy_vadd(struct window_pane *wp, const char *fmt, va_list ap)
514: {
1.211 nicm 515: struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
1.208 nicm 516: struct window_copy_mode_data *data = wme->data;
1.54 nicm 517: struct screen *backing = data->backing;
518: struct screen_write_ctx back_ctx, ctx;
519: struct grid_cell gc;
1.119 nicm 520: u_int old_hsize, old_cy;
1.54 nicm 521:
522: memcpy(&gc, &grid_default_cell, sizeof gc);
523:
524: old_hsize = screen_hsize(data->backing);
1.283 nicm 525: screen_write_start(&back_ctx, backing);
1.54 nicm 526: if (data->backing_written) {
527: /*
528: * On the second or later line, do a CRLF before writing
529: * (so it's on a new line).
530: */
531: screen_write_carriagereturn(&back_ctx);
1.175 nicm 532: screen_write_linefeed(&back_ctx, 0, 8);
1.54 nicm 533: } else
534: data->backing_written = 1;
1.119 nicm 535: old_cy = backing->cy;
1.139 nicm 536: screen_write_vnputs(&back_ctx, 0, &gc, fmt, ap);
1.54 nicm 537: screen_write_stop(&back_ctx);
538:
539: data->oy += screen_hsize(data->backing) - old_hsize;
540:
1.283 nicm 541: screen_write_start_pane(&ctx, wp, &data->screen);
1.54 nicm 542:
543: /*
544: * If the history has changed, draw the top line.
545: * (If there's any history at all, it has changed.)
546: */
547: if (screen_hsize(data->backing))
1.208 nicm 548: window_copy_redraw_lines(wme, 0, 1);
1.54 nicm 549:
1.119 nicm 550: /* Write the new lines. */
1.208 nicm 551: window_copy_redraw_lines(wme, old_cy, backing->cy - old_cy + 1);
1.54 nicm 552:
553: screen_write_stop(&ctx);
554: }
555:
556: void
1.149 nicm 557: window_copy_pageup(struct window_pane *wp, int half_page)
1.1 nicm 558: {
1.211 nicm 559: window_copy_pageup1(TAILQ_FIRST(&wp->modes), half_page);
1.208 nicm 560: }
561:
562: static void
563: window_copy_pageup1(struct window_mode_entry *wme, int half_page)
564: {
565: struct window_copy_mode_data *data = wme->data;
1.1 nicm 566: struct screen *s = &data->screen;
1.162 nicm 567: u_int n, ox, oy, px, py;
1.147 nicm 568:
569: oy = screen_hsize(data->backing) + data->cy - data->oy;
1.208 nicm 570: ox = window_copy_find_length(wme, oy);
1.147 nicm 571:
572: if (data->cx != ox) {
573: data->lastcx = data->cx;
574: data->lastsx = ox;
575: }
576: data->cx = data->lastcx;
1.1 nicm 577:
1.19 nicm 578: n = 1;
1.149 nicm 579: if (screen_size_y(s) > 2) {
580: if (half_page)
581: n = screen_size_y(s) / 2;
582: else
583: n = screen_size_y(s) - 2;
584: }
1.147 nicm 585:
1.195 nicm 586: if (data->oy + n > screen_hsize(data->backing)) {
1.54 nicm 587: data->oy = screen_hsize(data->backing);
1.195 nicm 588: if (data->cy < n)
589: data->cy = 0;
590: else
591: data->cy -= n;
592: } else
1.19 nicm 593: data->oy += n;
1.147 nicm 594:
1.192 nicm 595: if (data->screen.sel == NULL || !data->rectflag) {
1.162 nicm 596: py = screen_hsize(data->backing) + data->cy - data->oy;
1.208 nicm 597: px = window_copy_find_length(wme, py);
1.162 nicm 598: if ((data->cx >= data->lastsx && data->cx != px) ||
599: data->cx > px)
1.208 nicm 600: window_copy_cursor_end_of_line(wme);
1.147 nicm 601: }
602:
1.264 nicm 603: if (data->searchmark != NULL && !data->timeout)
1.296 nicm 604: window_copy_search_marks(wme, NULL, data->searchregex, 1);
1.252 nicm 605: window_copy_update_selection(wme, 1, 0);
1.208 nicm 606: window_copy_redraw_screen(wme);
1.147 nicm 607: }
608:
1.166 nicm 609: static int
1.208 nicm 610: window_copy_pagedown(struct window_mode_entry *wme, int half_page,
611: int scroll_exit)
1.147 nicm 612: {
1.208 nicm 613: struct window_copy_mode_data *data = wme->data;
1.147 nicm 614: struct screen *s = &data->screen;
1.161 nicm 615: u_int n, ox, oy, px, py;
1.147 nicm 616:
617: oy = screen_hsize(data->backing) + data->cy - data->oy;
1.208 nicm 618: ox = window_copy_find_length(wme, oy);
1.147 nicm 619:
620: if (data->cx != ox) {
621: data->lastcx = data->cx;
622: data->lastsx = ox;
623: }
624: data->cx = data->lastcx;
625:
626: n = 1;
1.149 nicm 627: if (screen_size_y(s) > 2) {
628: if (half_page)
629: n = screen_size_y(s) / 2;
630: else
631: n = screen_size_y(s) - 2;
632: }
1.147 nicm 633:
1.195 nicm 634: if (data->oy < n) {
1.147 nicm 635: data->oy = 0;
1.195 nicm 636: if (data->cy + (n - data->oy) >= screen_size_y(data->backing))
637: data->cy = screen_size_y(data->backing) - 1;
638: else
639: data->cy += n - data->oy;
640: } else
1.147 nicm 641: data->oy -= n;
642:
1.192 nicm 643: if (data->screen.sel == NULL || !data->rectflag) {
1.161 nicm 644: py = screen_hsize(data->backing) + data->cy - data->oy;
1.208 nicm 645: px = window_copy_find_length(wme, py);
1.161 nicm 646: if ((data->cx >= data->lastsx && data->cx != px) ||
647: data->cx > px)
1.208 nicm 648: window_copy_cursor_end_of_line(wme);
1.147 nicm 649: }
650:
1.186 nicm 651: if (scroll_exit && data->oy == 0)
1.166 nicm 652: return (1);
1.264 nicm 653: if (data->searchmark != NULL && !data->timeout)
1.296 nicm 654: window_copy_search_marks(wme, NULL, data->searchregex, 1);
1.252 nicm 655: window_copy_update_selection(wme, 1, 0);
1.208 nicm 656: window_copy_redraw_screen(wme);
1.166 nicm 657: return (0);
1.1 nicm 658: }
659:
1.157 nicm 660: static void
1.208 nicm 661: window_copy_previous_paragraph(struct window_mode_entry *wme)
1.148 nicm 662: {
1.208 nicm 663: struct window_copy_mode_data *data = wme->data;
1.151 nicm 664: u_int oy;
1.148 nicm 665:
666: oy = screen_hsize(data->backing) + data->cy - data->oy;
667:
1.208 nicm 668: while (oy > 0 && window_copy_find_length(wme, oy) == 0)
1.148 nicm 669: oy--;
670:
1.208 nicm 671: while (oy > 0 && window_copy_find_length(wme, oy) > 0)
1.148 nicm 672: oy--;
673:
1.296 nicm 674: window_copy_scroll_to(wme, 0, oy, 0);
1.148 nicm 675: }
676:
1.157 nicm 677: static void
1.208 nicm 678: window_copy_next_paragraph(struct window_mode_entry *wme)
1.148 nicm 679: {
1.208 nicm 680: struct window_copy_mode_data *data = wme->data;
1.148 nicm 681: struct screen *s = &data->screen;
682: u_int maxy, ox, oy;
683:
684: oy = screen_hsize(data->backing) + data->cy - data->oy;
685: maxy = screen_hsize(data->backing) + screen_size_y(s) - 1;
686:
1.208 nicm 687: while (oy < maxy && window_copy_find_length(wme, oy) == 0)
1.148 nicm 688: oy++;
689:
1.208 nicm 690: while (oy < maxy && window_copy_find_length(wme, oy) > 0)
1.148 nicm 691: oy++;
692:
1.208 nicm 693: ox = window_copy_find_length(wme, oy);
1.296 nicm 694: window_copy_scroll_to(wme, ox, oy, 0);
1.148 nicm 695: }
696:
1.256 nicm 697: char *
698: window_copy_get_word(struct window_pane *wp, u_int x, u_int y)
699: {
700: struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
701: struct window_copy_mode_data *data = wme->data;
702: struct grid *gd = data->screen.grid;
703:
704: return (format_grid_word(gd, x, gd->hsize + y));
705: }
706:
707: char *
708: window_copy_get_line(struct window_pane *wp, u_int y)
709: {
710: struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
711: struct window_copy_mode_data *data = wme->data;
712: struct grid *gd = data->screen.grid;
713:
714: return (format_grid_line(gd, gd->hsize + y));
715: }
716:
1.316 nicm 717: static void *
1.299 nicm 718: window_copy_cursor_word_cb(struct format_tree *ft)
719: {
720: struct window_pane *wp = format_get_pane(ft);
721: struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
722: struct window_copy_mode_data *data = wme->data;
723:
724: return (window_copy_get_word(wp, data->cx, data->cy));
725: }
726:
1.316 nicm 727: static void *
1.299 nicm 728: window_copy_cursor_line_cb(struct format_tree *ft)
729: {
730: struct window_pane *wp = format_get_pane(ft);
731: struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
732: struct window_copy_mode_data *data = wme->data;
733:
734: return (window_copy_get_line(wp, data->cy));
735: }
736:
1.316 nicm 737: static void *
1.299 nicm 738: window_copy_search_match_cb(struct format_tree *ft)
739: {
740: struct window_pane *wp = format_get_pane(ft);
741: struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
742: struct window_copy_mode_data *data = wme->data;
743:
744: return (window_copy_match_at_cursor(data));
745: }
746:
1.157 nicm 747: static void
1.208 nicm 748: window_copy_formats(struct window_mode_entry *wme, struct format_tree *ft)
1.206 nicm 749: {
1.208 nicm 750: struct window_copy_mode_data *data = wme->data;
1.206 nicm 751:
752: format_add(ft, "scroll_position", "%d", data->oy);
753: format_add(ft, "rectangle_toggle", "%d", data->rectflag);
1.236 nicm 754:
755: format_add(ft, "copy_cursor_x", "%d", data->cx);
756: format_add(ft, "copy_cursor_y", "%d", data->cy);
757:
758: format_add(ft, "selection_present", "%d", data->screen.sel != NULL);
759: if (data->screen.sel != NULL) {
760: format_add(ft, "selection_start_x", "%d", data->selx);
761: format_add(ft, "selection_start_y", "%d", data->sely);
762: format_add(ft, "selection_end_x", "%d", data->endselx);
763: format_add(ft, "selection_end_y", "%d", data->endsely);
1.246 nicm 764: format_add(ft, "selection_active", "%d",
765: data->cursordrag != CURSORDRAG_NONE);
766: } else
767: format_add(ft, "selection_active", "%d", 0);
1.237 nicm 768:
1.299 nicm 769: format_add(ft, "search_present", "%d", data->searchmark != NULL);
770: format_add_cb(ft, "search_match", window_copy_search_match_cb);
1.237 nicm 771:
1.299 nicm 772: format_add_cb(ft, "copy_cursor_word", window_copy_cursor_word_cb);
773: format_add_cb(ft, "copy_cursor_line", window_copy_cursor_line_cb);
1.206 nicm 774: }
775:
776: static void
1.275 nicm 777: window_copy_size_changed(struct window_mode_entry *wme)
1.1 nicm 778: {
1.208 nicm 779: struct window_copy_mode_data *data = wme->data;
1.1 nicm 780: struct screen *s = &data->screen;
1.275 nicm 781: struct screen_write_ctx ctx;
782: int search = (data->searchmark != NULL);
1.35 nicm 783:
1.208 nicm 784: window_copy_clear_selection(wme);
1.228 nicm 785: window_copy_clear_marks(wme);
1.18 nicm 786:
1.283 nicm 787: screen_write_start(&ctx, s);
1.276 nicm 788: window_copy_write_lines(wme, &ctx, 0, screen_size_y(s));
1.1 nicm 789: screen_write_stop(&ctx);
1.18 nicm 790:
1.263 nicm 791: if (search && !data->timeout)
1.296 nicm 792: window_copy_search_marks(wme, NULL, data->searchregex, 0);
1.163 nicm 793: data->searchx = data->cx;
794: data->searchy = data->cy;
795: data->searcho = data->oy;
1.275 nicm 796: }
797:
798: static void
799: window_copy_resize(struct window_mode_entry *wme, u_int sx, u_int sy)
800: {
801: struct window_copy_mode_data *data = wme->data;
802: struct screen *s = &data->screen;
1.285 nicm 803: struct grid *gd = data->backing->grid;
804: u_int cx, cy, wx, wy;
805: int reflow;
1.275 nicm 806:
807: screen_resize(s, sx, sy, 0);
1.285 nicm 808: cx = data->cx;
809: cy = gd->hsize + data->cy - data->oy;
810: reflow = (gd->sx != sx);
811: if (reflow)
812: grid_wrap_position(gd, cx, cy, &wx, &wy);
813: screen_resize_cursor(data->backing, sx, sy, 1, 0, 0);
814: if (reflow)
815: grid_unwrap_position(gd, &cx, &cy, wx, wy);
1.275 nicm 816:
1.285 nicm 817: data->cx = cx;
818: if (cy < gd->hsize) {
819: data->cy = 0;
820: data->oy = gd->hsize - cy;
821: } else {
822: data->cy = cy - gd->hsize;
823: data->oy = 0;
824: }
1.162 nicm 825:
1.275 nicm 826: window_copy_size_changed(wme);
1.208 nicm 827: window_copy_redraw_screen(wme);
1.1 nicm 828: }
829:
1.157 nicm 830: static const char *
1.208 nicm 831: window_copy_key_table(struct window_mode_entry *wme)
1.155 nicm 832: {
1.208 nicm 833: struct window_pane *wp = wme->wp;
834:
1.155 nicm 835: if (options_get_number(wp->window->options, "mode-keys") == MODEKEY_VI)
836: return ("copy-mode-vi");
837: return ("copy-mode");
838: }
839:
1.259 nicm 840: static int
841: window_copy_expand_search_string(struct window_copy_cmd_state *cs)
842: {
843: struct window_mode_entry *wme = cs->wme;
844: struct window_copy_mode_data *data = wme->data;
1.329 nicm 845: const char *ss = args_string(cs->args, 1);
1.259 nicm 846: char *expanded;
847:
1.329 nicm 848: if (ss == NULL || *ss == '\0')
849: return (0);
850:
851: if (args_has(cs->args, 'F')) {
852: expanded = format_single(NULL, ss, NULL, NULL, NULL, wme->wp);
853: if (*expanded == '\0') {
854: free(expanded);
855: return (0);
1.259 nicm 856: }
1.329 nicm 857: free(data->searchstr);
858: data->searchstr = expanded;
859: } else {
860: free(data->searchstr);
861: data->searchstr = xstrdup(ss);
1.259 nicm 862: }
863: return (1);
864: }
865:
1.213 nicm 866: static enum window_copy_cmd_action
867: window_copy_cmd_append_selection(struct window_copy_cmd_state *cs)
868: {
869: struct window_mode_entry *wme = cs->wme;
870: struct session *s = cs->s;
871:
872: if (s != NULL)
873: window_copy_append_selection(wme);
874: window_copy_clear_selection(wme);
875: return (WINDOW_COPY_CMD_REDRAW);
876: }
877:
878: static enum window_copy_cmd_action
879: window_copy_cmd_append_selection_and_cancel(struct window_copy_cmd_state *cs)
880: {
881: struct window_mode_entry *wme = cs->wme;
882: struct session *s = cs->s;
883:
884: if (s != NULL)
885: window_copy_append_selection(wme);
886: window_copy_clear_selection(wme);
887: return (WINDOW_COPY_CMD_CANCEL);
888: }
889:
890: static enum window_copy_cmd_action
891: window_copy_cmd_back_to_indentation(struct window_copy_cmd_state *cs)
892: {
893: struct window_mode_entry *wme = cs->wme;
894:
895: window_copy_cursor_back_to_indentation(wme);
896: return (WINDOW_COPY_CMD_NOTHING);
897: }
898:
899: static enum window_copy_cmd_action
900: window_copy_cmd_begin_selection(struct window_copy_cmd_state *cs)
901: {
902: struct window_mode_entry *wme = cs->wme;
903: struct client *c = cs->c;
904: struct mouse_event *m = cs->m;
905: struct window_copy_mode_data *data = wme->data;
906:
907: if (m != NULL) {
908: window_copy_start_drag(c, m);
909: return (WINDOW_COPY_CMD_NOTHING);
910: }
911:
912: data->lineflag = LINE_SEL_NONE;
1.252 nicm 913: data->selflag = SEL_CHAR;
1.213 nicm 914: window_copy_start_selection(wme);
915: return (WINDOW_COPY_CMD_REDRAW);
916: }
917:
918: static enum window_copy_cmd_action
919: window_copy_cmd_stop_selection(struct window_copy_cmd_state *cs)
920: {
921: struct window_mode_entry *wme = cs->wme;
922: struct window_copy_mode_data *data = wme->data;
923:
924: data->cursordrag = CURSORDRAG_NONE;
1.214 nicm 925: data->lineflag = LINE_SEL_NONE;
1.250 nicm 926: data->selflag = SEL_CHAR;
1.213 nicm 927: return (WINDOW_COPY_CMD_NOTHING);
928: }
929:
930: static enum window_copy_cmd_action
931: window_copy_cmd_bottom_line(struct window_copy_cmd_state *cs)
932: {
933: struct window_mode_entry *wme = cs->wme;
934: struct window_copy_mode_data *data = wme->data;
935:
936: data->cx = 0;
937: data->cy = screen_size_y(&data->screen) - 1;
938:
1.252 nicm 939: window_copy_update_selection(wme, 1, 0);
1.213 nicm 940: return (WINDOW_COPY_CMD_REDRAW);
941: }
942:
943: static enum window_copy_cmd_action
944: window_copy_cmd_cancel(__unused struct window_copy_cmd_state *cs)
945: {
946: return (WINDOW_COPY_CMD_CANCEL);
947: }
948:
949: static enum window_copy_cmd_action
950: window_copy_cmd_clear_selection(struct window_copy_cmd_state *cs)
951: {
952: struct window_mode_entry *wme = cs->wme;
953:
954: window_copy_clear_selection(wme);
955: return (WINDOW_COPY_CMD_REDRAW);
956: }
957:
958: static enum window_copy_cmd_action
1.326 nicm 959: window_copy_do_copy_end_of_line(struct window_copy_cmd_state *cs, int pipe,
960: int cancel)
1.213 nicm 961: {
1.326 nicm 962: struct window_mode_entry *wme = cs->wme;
963: struct client *c = cs->c;
964: struct session *s = cs->s;
965: struct winlink *wl = cs->wl;
966: struct window_pane *wp = wme->wp;
1.329 nicm 967: u_int count = args_count(cs->args);
1.326 nicm 968: u_int np = wme->prefix, ocx, ocy, ooy;
969: struct window_copy_mode_data *data = wme->data;
970: char *prefix = NULL, *command = NULL;
1.329 nicm 971: const char *arg1 = args_string(cs->args, 1);
972: const char *arg2 = args_string(cs->args, 2);
1.326 nicm 973:
974: if (pipe) {
1.329 nicm 975: if (count == 3)
976: prefix = format_single(NULL, arg2, c, s, wl, wp);
977: if (s != NULL && count > 1 && *arg1 != '\0')
978: command = format_single(NULL, arg1, c, s, wl, wp);
1.326 nicm 979: } else {
1.329 nicm 980: if (count == 2)
981: prefix = format_single(NULL, arg1, c, s, wl, wp);
1.326 nicm 982: }
1.325 nicm 983:
984: ocx = data->cx;
985: ocy = data->cy;
986: ooy = data->oy;
987:
988: window_copy_start_selection(wme);
989: for (; np > 1; np--)
990: window_copy_cursor_down(wme, 0);
991: window_copy_cursor_end_of_line(wme);
992:
1.326 nicm 993: if (s != NULL) {
994: if (pipe)
995: window_copy_copy_pipe(wme, s, prefix, command);
996: else
997: window_copy_copy_selection(wme, prefix);
998:
999: if (cancel) {
1000: free(prefix);
1001: free(command);
1002: return (WINDOW_COPY_CMD_CANCEL);
1003: }
1004: }
1.325 nicm 1005: window_copy_clear_selection(wme);
1006:
1007: data->cx = ocx;
1008: data->cy = ocy;
1009: data->oy = ooy;
1010:
1011: free(prefix);
1.326 nicm 1012: free(command);
1.325 nicm 1013: return (WINDOW_COPY_CMD_REDRAW);
1014: }
1015:
1016: static enum window_copy_cmd_action
1.326 nicm 1017: window_copy_cmd_copy_end_of_line(struct window_copy_cmd_state *cs)
1018: {
1019: return (window_copy_do_copy_end_of_line(cs, 0, 0));
1020: }
1021:
1022: static enum window_copy_cmd_action
1.325 nicm 1023: window_copy_cmd_copy_end_of_line_and_cancel(struct window_copy_cmd_state *cs)
1024: {
1.326 nicm 1025: return (window_copy_do_copy_end_of_line(cs, 0, 1));
1026: }
1.215 nicm 1027:
1.326 nicm 1028: static enum window_copy_cmd_action
1029: window_copy_cmd_copy_pipe_end_of_line(struct window_copy_cmd_state *cs)
1030: {
1031: return (window_copy_do_copy_end_of_line(cs, 1, 0));
1032: }
1.213 nicm 1033:
1.326 nicm 1034: static enum window_copy_cmd_action
1035: window_copy_cmd_copy_pipe_end_of_line_and_cancel(
1036: struct window_copy_cmd_state *cs)
1037: {
1038: return (window_copy_do_copy_end_of_line(cs, 1, 1));
1.213 nicm 1039: }
1040:
1041: static enum window_copy_cmd_action
1.326 nicm 1042: window_copy_do_copy_line(struct window_copy_cmd_state *cs, int pipe, int cancel)
1.213 nicm 1043: {
1.326 nicm 1044: struct window_mode_entry *wme = cs->wme;
1045: struct client *c = cs->c;
1046: struct session *s = cs->s;
1047: struct winlink *wl = cs->wl;
1048: struct window_pane *wp = wme->wp;
1049: struct window_copy_mode_data *data = wme->data;
1.329 nicm 1050: u_int count = args_count(cs->args);
1.326 nicm 1051: u_int np = wme->prefix, ocx, ocy, ooy;
1052: char *prefix = NULL, *command = NULL;
1.329 nicm 1053: const char *arg1 = args_string(cs->args, 1);
1054: const char *arg2 = args_string(cs->args, 2);
1.325 nicm 1055:
1.326 nicm 1056: if (pipe) {
1.329 nicm 1057: if (count == 3)
1058: prefix = format_single(NULL, arg2, c, s, wl, wp);
1059: if (s != NULL && count > 1 && *arg1 != '\0')
1060: command = format_single(NULL, arg1, c, s, wl, wp);
1.326 nicm 1061: } else {
1.329 nicm 1062: if (count == 2)
1063: prefix = format_single(NULL, arg1, c, s, wl, wp);
1.326 nicm 1064: }
1.325 nicm 1065:
1066: ocx = data->cx;
1067: ocy = data->cy;
1068: ooy = data->oy;
1069:
1070: data->selflag = SEL_CHAR;
1071: window_copy_cursor_start_of_line(wme);
1072: window_copy_start_selection(wme);
1073: for (; np > 1; np--)
1074: window_copy_cursor_down(wme, 0);
1075: window_copy_cursor_end_of_line(wme);
1076:
1.326 nicm 1077: if (s != NULL) {
1078: if (pipe)
1079: window_copy_copy_pipe(wme, s, prefix, command);
1080: else
1081: window_copy_copy_selection(wme, prefix);
1082:
1083: if (cancel) {
1084: free(prefix);
1085: free(command);
1086: return (WINDOW_COPY_CMD_CANCEL);
1087: }
1088: }
1.325 nicm 1089: window_copy_clear_selection(wme);
1090:
1091: data->cx = ocx;
1092: data->cy = ocy;
1093: data->oy = ooy;
1094:
1095: free(prefix);
1.326 nicm 1096: free(command);
1.325 nicm 1097: return (WINDOW_COPY_CMD_REDRAW);
1098: }
1099:
1100: static enum window_copy_cmd_action
1.326 nicm 1101: window_copy_cmd_copy_line(struct window_copy_cmd_state *cs)
1102: {
1103: return (window_copy_do_copy_line(cs, 0, 0));
1104: }
1105:
1106: static enum window_copy_cmd_action
1.325 nicm 1107: window_copy_cmd_copy_line_and_cancel(struct window_copy_cmd_state *cs)
1108: {
1.326 nicm 1109: return (window_copy_do_copy_line(cs, 0, 1));
1110: }
1.215 nicm 1111:
1.326 nicm 1112: static enum window_copy_cmd_action
1113: window_copy_cmd_copy_pipe_line(struct window_copy_cmd_state *cs)
1114: {
1115: return (window_copy_do_copy_line(cs, 1, 0));
1116: }
1.213 nicm 1117:
1.326 nicm 1118: static enum window_copy_cmd_action
1119: window_copy_cmd_copy_pipe_line_and_cancel(struct window_copy_cmd_state *cs)
1120: {
1121: return (window_copy_do_copy_line(cs, 1, 1));
1.213 nicm 1122: }
1123:
1124: static enum window_copy_cmd_action
1.216 nicm 1125: window_copy_cmd_copy_selection_no_clear(struct window_copy_cmd_state *cs)
1.213 nicm 1126: {
1127: struct window_mode_entry *wme = cs->wme;
1.215 nicm 1128: struct client *c = cs->c;
1.213 nicm 1129: struct session *s = cs->s;
1.215 nicm 1130: struct winlink *wl = cs->wl;
1131: struct window_pane *wp = wme->wp;
1132: char *prefix = NULL;
1.329 nicm 1133: const char *arg1 = args_string(cs->args, 1);
1.215 nicm 1134:
1.329 nicm 1135: if (arg1 != NULL)
1136: prefix = format_single(NULL, arg1, c, s, wl, wp);
1.213 nicm 1137:
1138: if (s != NULL)
1.215 nicm 1139: window_copy_copy_selection(wme, prefix);
1140:
1141: free(prefix);
1.216 nicm 1142: return (WINDOW_COPY_CMD_NOTHING);
1143: }
1144:
1145: static enum window_copy_cmd_action
1146: window_copy_cmd_copy_selection(struct window_copy_cmd_state *cs)
1147: {
1148: struct window_mode_entry *wme = cs->wme;
1149:
1150: window_copy_cmd_copy_selection_no_clear(cs);
1151: window_copy_clear_selection(wme);
1.213 nicm 1152: return (WINDOW_COPY_CMD_REDRAW);
1153: }
1154:
1155: static enum window_copy_cmd_action
1156: window_copy_cmd_copy_selection_and_cancel(struct window_copy_cmd_state *cs)
1157: {
1158: struct window_mode_entry *wme = cs->wme;
1.215 nicm 1159:
1.216 nicm 1160: window_copy_cmd_copy_selection_no_clear(cs);
1.213 nicm 1161: window_copy_clear_selection(wme);
1162: return (WINDOW_COPY_CMD_CANCEL);
1163: }
1164:
1165: static enum window_copy_cmd_action
1166: window_copy_cmd_cursor_down(struct window_copy_cmd_state *cs)
1167: {
1168: struct window_mode_entry *wme = cs->wme;
1169: u_int np = wme->prefix;
1170:
1171: for (; np != 0; np--)
1172: window_copy_cursor_down(wme, 0);
1173: return (WINDOW_COPY_CMD_NOTHING);
1174: }
1175:
1176: static enum window_copy_cmd_action
1.234 nicm 1177: window_copy_cmd_cursor_down_and_cancel(struct window_copy_cmd_state *cs)
1178: {
1179: struct window_mode_entry *wme = cs->wme;
1180: struct window_copy_mode_data *data = wme->data;
1181: u_int np = wme->prefix, cy;
1182:
1183: cy = data->cy;
1184: for (; np != 0; np--)
1185: window_copy_cursor_down(wme, 0);
1186: if (cy == data->cy && data->oy == 0)
1187: return (WINDOW_COPY_CMD_CANCEL);
1188: return (WINDOW_COPY_CMD_NOTHING);
1189: }
1190:
1191: static enum window_copy_cmd_action
1.213 nicm 1192: window_copy_cmd_cursor_left(struct window_copy_cmd_state *cs)
1193: {
1194: struct window_mode_entry *wme = cs->wme;
1195: u_int np = wme->prefix;
1196:
1197: for (; np != 0; np--)
1198: window_copy_cursor_left(wme);
1199: return (WINDOW_COPY_CMD_NOTHING);
1200: }
1201:
1202: static enum window_copy_cmd_action
1203: window_copy_cmd_cursor_right(struct window_copy_cmd_state *cs)
1204: {
1205: struct window_mode_entry *wme = cs->wme;
1.324 nicm 1206: struct window_copy_mode_data *data = wme->data;
1.213 nicm 1207: u_int np = wme->prefix;
1208:
1.324 nicm 1209: for (; np != 0; np--) {
1210: window_copy_cursor_right(wme, data->screen.sel != NULL &&
1211: data->rectflag);
1212: }
1.213 nicm 1213: return (WINDOW_COPY_CMD_NOTHING);
1214: }
1215:
1216: static enum window_copy_cmd_action
1217: window_copy_cmd_cursor_up(struct window_copy_cmd_state *cs)
1218: {
1219: struct window_mode_entry *wme = cs->wme;
1220: u_int np = wme->prefix;
1221:
1222: for (; np != 0; np--)
1223: window_copy_cursor_up(wme, 0);
1224: return (WINDOW_COPY_CMD_NOTHING);
1225: }
1226:
1227: static enum window_copy_cmd_action
1228: window_copy_cmd_end_of_line(struct window_copy_cmd_state *cs)
1229: {
1230: struct window_mode_entry *wme = cs->wme;
1231:
1232: window_copy_cursor_end_of_line(wme);
1233: return (WINDOW_COPY_CMD_NOTHING);
1234: }
1235:
1236: static enum window_copy_cmd_action
1237: window_copy_cmd_halfpage_down(struct window_copy_cmd_state *cs)
1238: {
1239: struct window_mode_entry *wme = cs->wme;
1240: struct window_copy_mode_data *data = wme->data;
1241: u_int np = wme->prefix;
1242:
1243: for (; np != 0; np--) {
1244: if (window_copy_pagedown(wme, 1, data->scroll_exit))
1245: return (WINDOW_COPY_CMD_CANCEL);
1246: }
1247: return (WINDOW_COPY_CMD_NOTHING);
1248: }
1249:
1250: static enum window_copy_cmd_action
1251: window_copy_cmd_halfpage_down_and_cancel(struct window_copy_cmd_state *cs)
1252: {
1253:
1254: struct window_mode_entry *wme = cs->wme;
1255: u_int np = wme->prefix;
1256:
1257: for (; np != 0; np--) {
1258: if (window_copy_pagedown(wme, 1, 1))
1259: return (WINDOW_COPY_CMD_CANCEL);
1260: }
1261: return (WINDOW_COPY_CMD_NOTHING);
1262: }
1263:
1264: static enum window_copy_cmd_action
1265: window_copy_cmd_halfpage_up(struct window_copy_cmd_state *cs)
1266: {
1267: struct window_mode_entry *wme = cs->wme;
1268: u_int np = wme->prefix;
1269:
1270: for (; np != 0; np--)
1271: window_copy_pageup1(wme, 1);
1272: return (WINDOW_COPY_CMD_NOTHING);
1273: }
1274:
1275: static enum window_copy_cmd_action
1.333 ! nicm 1276: window_copy_cmd_toggle_position(struct window_copy_cmd_state *cs)
! 1277: {
! 1278: struct window_mode_entry *wme = cs->wme;
! 1279: struct window_copy_mode_data *data = wme->data;
! 1280:
! 1281: data->hide_position = !data->hide_position;
! 1282: return (WINDOW_COPY_CMD_REDRAW);
! 1283: }
! 1284:
! 1285: static enum window_copy_cmd_action
1.213 nicm 1286: window_copy_cmd_history_bottom(struct window_copy_cmd_state *cs)
1287: {
1288: struct window_mode_entry *wme = cs->wme;
1289: struct window_copy_mode_data *data = wme->data;
1.269 nicm 1290: struct screen *s = data->backing;
1.221 nicm 1291: u_int oy;
1292:
1.269 nicm 1293: oy = screen_hsize(s) + data->cy - data->oy;
1.221 nicm 1294: if (data->lineflag == LINE_SEL_RIGHT_LEFT && oy == data->endsely)
1295: window_copy_other_end(wme);
1.213 nicm 1296:
1297: data->cy = screen_size_y(&data->screen) - 1;
1.269 nicm 1298: data->cx = window_copy_find_length(wme, screen_hsize(s) + data->cy);
1.213 nicm 1299: data->oy = 0;
1300:
1.264 nicm 1301: if (data->searchmark != NULL && !data->timeout)
1.296 nicm 1302: window_copy_search_marks(wme, NULL, data->searchregex, 1);
1.252 nicm 1303: window_copy_update_selection(wme, 1, 0);
1.213 nicm 1304: return (WINDOW_COPY_CMD_REDRAW);
1305: }
1306:
1307: static enum window_copy_cmd_action
1308: window_copy_cmd_history_top(struct window_copy_cmd_state *cs)
1309: {
1310: struct window_mode_entry *wme = cs->wme;
1311: struct window_copy_mode_data *data = wme->data;
1.221 nicm 1312: u_int oy;
1.213 nicm 1313:
1.221 nicm 1314: oy = screen_hsize(data->backing) + data->cy - data->oy;
1315: if (data->lineflag == LINE_SEL_LEFT_RIGHT && oy == data->sely)
1316: window_copy_other_end(wme);
1317:
1318: data->cy = 0;
1.213 nicm 1319: data->cx = 0;
1320: data->oy = screen_hsize(data->backing);
1321:
1.264 nicm 1322: if (data->searchmark != NULL && !data->timeout)
1.296 nicm 1323: window_copy_search_marks(wme, NULL, data->searchregex, 1);
1.252 nicm 1324: window_copy_update_selection(wme, 1, 0);
1.213 nicm 1325: return (WINDOW_COPY_CMD_REDRAW);
1326: }
1327:
1328: static enum window_copy_cmd_action
1329: window_copy_cmd_jump_again(struct window_copy_cmd_state *cs)
1330: {
1331: struct window_mode_entry *wme = cs->wme;
1332: struct window_copy_mode_data *data = wme->data;
1333: u_int np = wme->prefix;
1334:
1335: switch (data->jumptype) {
1336: case WINDOW_COPY_JUMPFORWARD:
1337: for (; np != 0; np--)
1338: window_copy_cursor_jump(wme);
1339: break;
1340: case WINDOW_COPY_JUMPBACKWARD:
1341: for (; np != 0; np--)
1342: window_copy_cursor_jump_back(wme);
1343: break;
1344: case WINDOW_COPY_JUMPTOFORWARD:
1345: for (; np != 0; np--)
1346: window_copy_cursor_jump_to(wme);
1347: break;
1348: case WINDOW_COPY_JUMPTOBACKWARD:
1349: for (; np != 0; np--)
1350: window_copy_cursor_jump_to_back(wme);
1351: break;
1352: }
1353: return (WINDOW_COPY_CMD_NOTHING);
1354: }
1355:
1356: static enum window_copy_cmd_action
1357: window_copy_cmd_jump_reverse(struct window_copy_cmd_state *cs)
1358: {
1359: struct window_mode_entry *wme = cs->wme;
1360: struct window_copy_mode_data *data = wme->data;
1361: u_int np = wme->prefix;
1362:
1363: switch (data->jumptype) {
1364: case WINDOW_COPY_JUMPFORWARD:
1365: for (; np != 0; np--)
1366: window_copy_cursor_jump_back(wme);
1367: break;
1368: case WINDOW_COPY_JUMPBACKWARD:
1369: for (; np != 0; np--)
1370: window_copy_cursor_jump(wme);
1371: break;
1372: case WINDOW_COPY_JUMPTOFORWARD:
1373: for (; np != 0; np--)
1374: window_copy_cursor_jump_to_back(wme);
1375: break;
1376: case WINDOW_COPY_JUMPTOBACKWARD:
1377: for (; np != 0; np--)
1378: window_copy_cursor_jump_to(wme);
1379: break;
1380: }
1381: return (WINDOW_COPY_CMD_NOTHING);
1382: }
1383:
1384: static enum window_copy_cmd_action
1385: window_copy_cmd_middle_line(struct window_copy_cmd_state *cs)
1386: {
1387: struct window_mode_entry *wme = cs->wme;
1388: struct window_copy_mode_data *data = wme->data;
1389:
1390: data->cx = 0;
1391: data->cy = (screen_size_y(&data->screen) - 1) / 2;
1392:
1.252 nicm 1393: window_copy_update_selection(wme, 1, 0);
1.213 nicm 1394: return (WINDOW_COPY_CMD_REDRAW);
1395: }
1396:
1397: static enum window_copy_cmd_action
1.218 nicm 1398: window_copy_cmd_previous_matching_bracket(struct window_copy_cmd_state *cs)
1399: {
1400: struct window_mode_entry *wme = cs->wme;
1401: u_int np = wme->prefix;
1402: struct window_copy_mode_data *data = wme->data;
1403: struct screen *s = data->backing;
1404: char open[] = "{[(", close[] = "}])";
1405: char tried, found, start, *cp;
1.219 nicm 1406: u_int px, py, xx, n;
1.218 nicm 1407: struct grid_cell gc;
1408: int failed;
1409:
1410: for (; np != 0; np--) {
1411: /* Get cursor position and line length. */
1412: px = data->cx;
1413: py = screen_hsize(s) + data->cy - data->oy;
1414: xx = window_copy_find_length(wme, py);
1415: if (xx == 0)
1416: break;
1417:
1418: /*
1419: * Get the current character. If not on a bracket, try the
1420: * previous. If still not, then behave like previous-word.
1421: */
1422: tried = 0;
1423: retry:
1424: grid_get_cell(s->grid, px, py, &gc);
1425: if (gc.data.size != 1 || (gc.flags & GRID_FLAG_PADDING))
1426: cp = NULL;
1427: else {
1428: found = *gc.data.data;
1429: cp = strchr(close, found);
1430: }
1431: if (cp == NULL) {
1432: if (data->modekeys == MODEKEY_EMACS) {
1433: if (!tried && px > 0) {
1434: px--;
1435: tried = 1;
1436: goto retry;
1437: }
1.323 nicm 1438: window_copy_cursor_previous_word(wme, close, 1);
1.218 nicm 1439: }
1440: continue;
1441: }
1442: start = open[cp - close];
1443:
1444: /* Walk backward until the matching bracket is reached. */
1445: n = 1;
1446: failed = 0;
1447: do {
1448: if (px == 0) {
1449: if (py == 0) {
1450: failed = 1;
1451: break;
1452: }
1453: do {
1454: py--;
1455: xx = window_copy_find_length(wme, py);
1456: } while (xx == 0 && py > 0);
1457: if (xx == 0 && py == 0) {
1458: failed = 1;
1459: break;
1460: }
1461: px = xx - 1;
1462: } else
1463: px--;
1464:
1465: grid_get_cell(s->grid, px, py, &gc);
1466: if (gc.data.size == 1 &&
1467: (~gc.flags & GRID_FLAG_PADDING)) {
1468: if (*gc.data.data == found)
1469: n++;
1470: else if (*gc.data.data == start)
1471: n--;
1472: }
1473: } while (n != 0);
1474:
1475: /* Move the cursor to the found location if any. */
1476: if (!failed)
1.296 nicm 1477: window_copy_scroll_to(wme, px, py, 0);
1.218 nicm 1478: }
1479:
1480: return (WINDOW_COPY_CMD_NOTHING);
1481: }
1482:
1483: static enum window_copy_cmd_action
1484: window_copy_cmd_next_matching_bracket(struct window_copy_cmd_state *cs)
1485: {
1486: struct window_mode_entry *wme = cs->wme;
1487: u_int np = wme->prefix;
1488: struct window_copy_mode_data *data = wme->data;
1489: struct screen *s = data->backing;
1490: char open[] = "{[(", close[] = "}])";
1491: char tried, found, end, *cp;
1492: u_int px, py, xx, yy, sx, sy, n;
1493: struct grid_cell gc;
1494: int failed;
1495: struct grid_line *gl;
1496:
1497: for (; np != 0; np--) {
1498: /* Get cursor position and line length. */
1499: px = data->cx;
1500: py = screen_hsize(s) + data->cy - data->oy;
1501: xx = window_copy_find_length(wme, py);
1502: yy = screen_hsize(s) + screen_size_y(s) - 1;
1503: if (xx == 0)
1504: break;
1505:
1506: /*
1507: * Get the current character. If not on a bracket, try the
1508: * next. If still not, then behave like next-word.
1509: */
1510: tried = 0;
1511: retry:
1512: grid_get_cell(s->grid, px, py, &gc);
1513: if (gc.data.size != 1 || (gc.flags & GRID_FLAG_PADDING))
1514: cp = NULL;
1515: else {
1516: found = *gc.data.data;
1517:
1518: /*
1519: * In vi mode, attempt to move to previous bracket if a
1520: * closing bracket is found first. If this fails,
1521: * return to the original cursor position.
1522: */
1523: cp = strchr(close, found);
1524: if (cp != NULL && data->modekeys == MODEKEY_VI) {
1525: sx = data->cx;
1526: sy = screen_hsize(s) + data->cy - data->oy;
1527:
1.296 nicm 1528: window_copy_scroll_to(wme, px, py, 0);
1.218 nicm 1529: window_copy_cmd_previous_matching_bracket(cs);
1530:
1531: px = data->cx;
1532: py = screen_hsize(s) + data->cy - data->oy;
1533: grid_get_cell(s->grid, px, py, &gc);
1.286 nicm 1534: if (gc.data.size == 1 &&
1535: (~gc.flags & GRID_FLAG_PADDING) &&
1536: strchr(close, *gc.data.data) != NULL)
1.296 nicm 1537: window_copy_scroll_to(wme, sx, sy, 0);
1.218 nicm 1538: break;
1539: }
1540:
1541: cp = strchr(open, found);
1542: }
1543: if (cp == NULL) {
1544: if (data->modekeys == MODEKEY_EMACS) {
1545: if (!tried && px <= xx) {
1546: px++;
1547: tried = 1;
1548: goto retry;
1549: }
1.323 nicm 1550: window_copy_cursor_next_word_end(wme, open, 0);
1.218 nicm 1551: continue;
1552: }
1553: /* For vi, continue searching for bracket until EOL. */
1554: if (px > xx) {
1555: if (py == yy)
1556: continue;
1557: gl = grid_get_line(s->grid, py);
1558: if (~gl->flags & GRID_LINE_WRAPPED)
1559: continue;
1560: if (gl->cellsize > s->grid->sx)
1561: continue;
1562: px = 0;
1563: py++;
1564: xx = window_copy_find_length(wme, py);
1565: } else
1566: px++;
1567: goto retry;
1568: }
1569: end = close[cp - open];
1570:
1571: /* Walk forward until the matching bracket is reached. */
1572: n = 1;
1573: failed = 0;
1574: do {
1575: if (px > xx) {
1576: if (py == yy) {
1577: failed = 1;
1578: break;
1579: }
1580: px = 0;
1581: py++;
1582: xx = window_copy_find_length(wme, py);
1583: } else
1584: px++;
1585:
1586: grid_get_cell(s->grid, px, py, &gc);
1587: if (gc.data.size == 1 &&
1588: (~gc.flags & GRID_FLAG_PADDING)) {
1589: if (*gc.data.data == found)
1590: n++;
1591: else if (*gc.data.data == end)
1592: n--;
1593: }
1594: } while (n != 0);
1595:
1596: /* Move the cursor to the found location if any. */
1597: if (!failed)
1.296 nicm 1598: window_copy_scroll_to(wme, px, py, 0);
1.218 nicm 1599: }
1600:
1601: return (WINDOW_COPY_CMD_NOTHING);
1602: }
1603:
1604: static enum window_copy_cmd_action
1.213 nicm 1605: window_copy_cmd_next_paragraph(struct window_copy_cmd_state *cs)
1606: {
1607: struct window_mode_entry *wme = cs->wme;
1608: u_int np = wme->prefix;
1609:
1610: for (; np != 0; np--)
1611: window_copy_next_paragraph(wme);
1612: return (WINDOW_COPY_CMD_NOTHING);
1613: }
1614:
1615: static enum window_copy_cmd_action
1616: window_copy_cmd_next_space(struct window_copy_cmd_state *cs)
1617: {
1618: struct window_mode_entry *wme = cs->wme;
1619: u_int np = wme->prefix;
1620:
1621: for (; np != 0; np--)
1.323 nicm 1622: window_copy_cursor_next_word(wme, "");
1.213 nicm 1623: return (WINDOW_COPY_CMD_NOTHING);
1624: }
1625:
1626: static enum window_copy_cmd_action
1627: window_copy_cmd_next_space_end(struct window_copy_cmd_state *cs)
1628: {
1629: struct window_mode_entry *wme = cs->wme;
1630: u_int np = wme->prefix;
1631:
1632: for (; np != 0; np--)
1.323 nicm 1633: window_copy_cursor_next_word_end(wme, "", 0);
1.213 nicm 1634: return (WINDOW_COPY_CMD_NOTHING);
1635: }
1636:
1637: static enum window_copy_cmd_action
1638: window_copy_cmd_next_word(struct window_copy_cmd_state *cs)
1639: {
1640: struct window_mode_entry *wme = cs->wme;
1641: u_int np = wme->prefix;
1.323 nicm 1642: const char *separators;
1643:
1644: separators = options_get_string(cs->s->options, "word-separators");
1.213 nicm 1645:
1646: for (; np != 0; np--)
1.323 nicm 1647: window_copy_cursor_next_word(wme, separators);
1.213 nicm 1648: return (WINDOW_COPY_CMD_NOTHING);
1649: }
1650:
1651: static enum window_copy_cmd_action
1652: window_copy_cmd_next_word_end(struct window_copy_cmd_state *cs)
1653: {
1654: struct window_mode_entry *wme = cs->wme;
1655: u_int np = wme->prefix;
1.323 nicm 1656: const char *separators;
1657:
1658: separators = options_get_string(cs->s->options, "word-separators");
1.213 nicm 1659:
1660: for (; np != 0; np--)
1.323 nicm 1661: window_copy_cursor_next_word_end(wme, separators, 0);
1.213 nicm 1662: return (WINDOW_COPY_CMD_NOTHING);
1663: }
1664:
1665: static enum window_copy_cmd_action
1666: window_copy_cmd_other_end(struct window_copy_cmd_state *cs)
1667: {
1668: struct window_mode_entry *wme = cs->wme;
1669: u_int np = wme->prefix;
1.254 nicm 1670: struct window_copy_mode_data *data = wme->data;
1.213 nicm 1671:
1.254 nicm 1672: data->selflag = SEL_CHAR;
1.213 nicm 1673: if ((np % 2) != 0)
1674: window_copy_other_end(wme);
1675: return (WINDOW_COPY_CMD_NOTHING);
1676: }
1677:
1678: static enum window_copy_cmd_action
1679: window_copy_cmd_page_down(struct window_copy_cmd_state *cs)
1680: {
1681: struct window_mode_entry *wme = cs->wme;
1682: struct window_copy_mode_data *data = wme->data;
1683: u_int np = wme->prefix;
1684:
1685: for (; np != 0; np--) {
1686: if (window_copy_pagedown(wme, 0, data->scroll_exit))
1687: return (WINDOW_COPY_CMD_CANCEL);
1688: }
1689: return (WINDOW_COPY_CMD_NOTHING);
1690: }
1691:
1692: static enum window_copy_cmd_action
1693: window_copy_cmd_page_down_and_cancel(struct window_copy_cmd_state *cs)
1694: {
1695: struct window_mode_entry *wme = cs->wme;
1696: u_int np = wme->prefix;
1697:
1698: for (; np != 0; np--) {
1699: if (window_copy_pagedown(wme, 0, 1))
1700: return (WINDOW_COPY_CMD_CANCEL);
1701: }
1702: return (WINDOW_COPY_CMD_NOTHING);
1703: }
1704:
1705: static enum window_copy_cmd_action
1706: window_copy_cmd_page_up(struct window_copy_cmd_state *cs)
1707: {
1708: struct window_mode_entry *wme = cs->wme;
1709: u_int np = wme->prefix;
1710:
1711: for (; np != 0; np--)
1712: window_copy_pageup1(wme, 0);
1713: return (WINDOW_COPY_CMD_NOTHING);
1714: }
1715:
1716: static enum window_copy_cmd_action
1717: window_copy_cmd_previous_paragraph(struct window_copy_cmd_state *cs)
1718: {
1719: struct window_mode_entry *wme = cs->wme;
1720: u_int np = wme->prefix;
1721:
1722: for (; np != 0; np--)
1723: window_copy_previous_paragraph(wme);
1724: return (WINDOW_COPY_CMD_NOTHING);
1725: }
1726:
1727: static enum window_copy_cmd_action
1728: window_copy_cmd_previous_space(struct window_copy_cmd_state *cs)
1729: {
1730: struct window_mode_entry *wme = cs->wme;
1731: u_int np = wme->prefix;
1732:
1733: for (; np != 0; np--)
1.323 nicm 1734: window_copy_cursor_previous_word(wme, "", 1);
1.213 nicm 1735: return (WINDOW_COPY_CMD_NOTHING);
1736: }
1737:
1738: static enum window_copy_cmd_action
1739: window_copy_cmd_previous_word(struct window_copy_cmd_state *cs)
1740: {
1741: struct window_mode_entry *wme = cs->wme;
1742: u_int np = wme->prefix;
1.323 nicm 1743: const char *separators;
1744:
1745: separators = options_get_string(cs->s->options, "word-separators");
1.213 nicm 1746:
1747: for (; np != 0; np--)
1.323 nicm 1748: window_copy_cursor_previous_word(wme, separators, 1);
1.213 nicm 1749: return (WINDOW_COPY_CMD_NOTHING);
1750: }
1751:
1752: static enum window_copy_cmd_action
1.313 nicm 1753: window_copy_cmd_rectangle_on(struct window_copy_cmd_state *cs)
1754: {
1755: struct window_mode_entry *wme = cs->wme;
1756: struct window_copy_mode_data *data = wme->data;
1757:
1758: data->lineflag = LINE_SEL_NONE;
1759: window_copy_rectangle_set(wme, 1);
1760:
1761: return (WINDOW_COPY_CMD_NOTHING);
1762: }
1763:
1764: static enum window_copy_cmd_action
1765: window_copy_cmd_rectangle_off(struct window_copy_cmd_state *cs)
1766: {
1767: struct window_mode_entry *wme = cs->wme;
1768: struct window_copy_mode_data *data = wme->data;
1769:
1770: data->lineflag = LINE_SEL_NONE;
1771: window_copy_rectangle_set(wme, 0);
1772:
1773: return (WINDOW_COPY_CMD_NOTHING);
1774: }
1775:
1776: static enum window_copy_cmd_action
1.213 nicm 1777: window_copy_cmd_rectangle_toggle(struct window_copy_cmd_state *cs)
1778: {
1779: struct window_mode_entry *wme = cs->wme;
1780: struct window_copy_mode_data *data = wme->data;
1781:
1782: data->lineflag = LINE_SEL_NONE;
1.313 nicm 1783: window_copy_rectangle_set(wme, !data->rectflag);
1.213 nicm 1784:
1785: return (WINDOW_COPY_CMD_NOTHING);
1786: }
1787:
1788: static enum window_copy_cmd_action
1789: window_copy_cmd_scroll_down(struct window_copy_cmd_state *cs)
1790: {
1791: struct window_mode_entry *wme = cs->wme;
1792: struct window_copy_mode_data *data = wme->data;
1793: u_int np = wme->prefix;
1794:
1795: for (; np != 0; np--)
1796: window_copy_cursor_down(wme, 1);
1797: if (data->scroll_exit && data->oy == 0)
1798: return (WINDOW_COPY_CMD_CANCEL);
1799: return (WINDOW_COPY_CMD_NOTHING);
1800: }
1801:
1802: static enum window_copy_cmd_action
1803: window_copy_cmd_scroll_down_and_cancel(struct window_copy_cmd_state *cs)
1804: {
1805: struct window_mode_entry *wme = cs->wme;
1806: struct window_copy_mode_data *data = wme->data;
1807: u_int np = wme->prefix;
1808:
1809: for (; np != 0; np--)
1810: window_copy_cursor_down(wme, 1);
1811: if (data->oy == 0)
1812: return (WINDOW_COPY_CMD_CANCEL);
1813: return (WINDOW_COPY_CMD_NOTHING);
1814: }
1815:
1816: static enum window_copy_cmd_action
1817: window_copy_cmd_scroll_up(struct window_copy_cmd_state *cs)
1818: {
1819: struct window_mode_entry *wme = cs->wme;
1820: u_int np = wme->prefix;
1821:
1822: for (; np != 0; np--)
1823: window_copy_cursor_up(wme, 1);
1824: return (WINDOW_COPY_CMD_NOTHING);
1825: }
1826:
1827: static enum window_copy_cmd_action
1828: window_copy_cmd_search_again(struct window_copy_cmd_state *cs)
1829: {
1830: struct window_mode_entry *wme = cs->wme;
1831: struct window_copy_mode_data *data = wme->data;
1832: u_int np = wme->prefix;
1833:
1834: if (data->searchtype == WINDOW_COPY_SEARCHUP) {
1835: for (; np != 0; np--)
1.321 nicm 1836: window_copy_search_up(wme, data->searchregex);
1.213 nicm 1837: } else if (data->searchtype == WINDOW_COPY_SEARCHDOWN) {
1838: for (; np != 0; np--)
1.321 nicm 1839: window_copy_search_down(wme, data->searchregex);
1.213 nicm 1840: }
1841: return (WINDOW_COPY_CMD_NOTHING);
1842: }
1843:
1844: static enum window_copy_cmd_action
1845: window_copy_cmd_search_reverse(struct window_copy_cmd_state *cs)
1846: {
1847: struct window_mode_entry *wme = cs->wme;
1848: struct window_copy_mode_data *data = wme->data;
1849: u_int np = wme->prefix;
1850:
1851: if (data->searchtype == WINDOW_COPY_SEARCHUP) {
1852: for (; np != 0; np--)
1.321 nicm 1853: window_copy_search_down(wme, data->searchregex);
1.213 nicm 1854: } else if (data->searchtype == WINDOW_COPY_SEARCHDOWN) {
1855: for (; np != 0; np--)
1.321 nicm 1856: window_copy_search_up(wme, data->searchregex);
1.213 nicm 1857: }
1858: return (WINDOW_COPY_CMD_NOTHING);
1859: }
1860:
1861: static enum window_copy_cmd_action
1862: window_copy_cmd_select_line(struct window_copy_cmd_state *cs)
1863: {
1864: struct window_mode_entry *wme = cs->wme;
1865: struct window_copy_mode_data *data = wme->data;
1866: u_int np = wme->prefix;
1867:
1868: data->lineflag = LINE_SEL_LEFT_RIGHT;
1869: data->rectflag = 0;
1.247 nicm 1870: data->selflag = SEL_LINE;
1871: data->dx = data->cx;
1872: data->dy = screen_hsize(data->backing) + data->cy - data->oy;
1.213 nicm 1873:
1874: window_copy_cursor_start_of_line(wme);
1.247 nicm 1875: data->selrx = data->cx;
1876: data->selry = screen_hsize(data->backing) + data->cy - data->oy;
1.258 nicm 1877: data->endselry = data->selry;
1.213 nicm 1878: window_copy_start_selection(wme);
1.319 nicm 1879: window_copy_cursor_end_of_line(wme);
1880: data->endselry = screen_hsize(data->backing) + data->cy - data->oy;
1881: data->endselrx = window_copy_find_length(wme, data->endselry);
1882: for (; np > 1; np--) {
1.213 nicm 1883: window_copy_cursor_down(wme, 0);
1.319 nicm 1884: window_copy_cursor_end_of_line(wme);
1885: }
1.213 nicm 1886:
1887: return (WINDOW_COPY_CMD_REDRAW);
1888: }
1889:
1890: static enum window_copy_cmd_action
1891: window_copy_cmd_select_word(struct window_copy_cmd_state *cs)
1892: {
1893: struct window_mode_entry *wme = cs->wme;
1.323 nicm 1894: struct options *session_options = cs->s->options;
1.213 nicm 1895: struct window_copy_mode_data *data = wme->data;
1.319 nicm 1896: u_int px, py, nextx, nexty;
1.323 nicm 1897:
1.213 nicm 1898: data->lineflag = LINE_SEL_LEFT_RIGHT;
1899: data->rectflag = 0;
1.247 nicm 1900: data->selflag = SEL_WORD;
1901: data->dx = data->cx;
1902: data->dy = screen_hsize(data->backing) + data->cy - data->oy;
1.213 nicm 1903:
1.323 nicm 1904: data->separators = options_get_string(session_options,
1905: "word-separators");
1906: window_copy_cursor_previous_word(wme, data->separators, 0);
1.245 nicm 1907: px = data->cx;
1908: py = screen_hsize(data->backing) + data->cy - data->oy;
1.257 nicm 1909: data->selrx = px;
1910: data->selry = py;
1.213 nicm 1911: window_copy_start_selection(wme);
1.245 nicm 1912:
1.319 nicm 1913: /* Handle single character words. */
1914: nextx = px + 1;
1915: nexty = py;
1916: if (grid_get_line(data->backing->grid, nexty)->flags &
1917: GRID_LINE_WRAPPED && nextx > screen_size_x(data->backing) - 1) {
1918: nextx = 0;
1919: nexty++;
1920: }
1.247 nicm 1921: if (px >= window_copy_find_length(wme, py) ||
1.323 nicm 1922: !window_copy_in_set(wme, nextx, nexty, WHITESPACE))
1923: window_copy_cursor_next_word_end(wme, data->separators, 1);
1.245 nicm 1924: else {
1925: window_copy_update_cursor(wme, px, data->cy);
1.255 nicm 1926: if (window_copy_update_selection(wme, 1, 1))
1.245 nicm 1927: window_copy_redraw_lines(wme, data->cy, 1);
1928: }
1.247 nicm 1929: data->endselrx = data->cx;
1930: data->endselry = screen_hsize(data->backing) + data->cy - data->oy;
1.319 nicm 1931: if (data->dy > data->endselry) {
1932: data->dy = data->endselry;
1933: data->dx = data->endselrx;
1934: } else if (data->dx > data->endselrx)
1.257 nicm 1935: data->dx = data->endselrx;
1.213 nicm 1936:
1937: return (WINDOW_COPY_CMD_REDRAW);
1938: }
1939:
1940: static enum window_copy_cmd_action
1.288 nicm 1941: window_copy_cmd_set_mark(struct window_copy_cmd_state *cs)
1942: {
1943: struct window_copy_mode_data *data = cs->wme->data;
1944:
1945: data->mx = data->cx;
1946: data->my = screen_hsize(data->backing) + data->cy - data->oy;
1947: data->showmark = 1;
1948: return (WINDOW_COPY_CMD_REDRAW);
1949: }
1950:
1951: static enum window_copy_cmd_action
1.213 nicm 1952: window_copy_cmd_start_of_line(struct window_copy_cmd_state *cs)
1953: {
1954: struct window_mode_entry *wme = cs->wme;
1955:
1956: window_copy_cursor_start_of_line(wme);
1957: return (WINDOW_COPY_CMD_NOTHING);
1958: }
1959:
1960: static enum window_copy_cmd_action
1961: window_copy_cmd_top_line(struct window_copy_cmd_state *cs)
1962: {
1963: struct window_mode_entry *wme = cs->wme;
1964: struct window_copy_mode_data *data = wme->data;
1965:
1966: data->cx = 0;
1967: data->cy = 0;
1968:
1.252 nicm 1969: window_copy_update_selection(wme, 1, 0);
1.213 nicm 1970: return (WINDOW_COPY_CMD_REDRAW);
1971: }
1972:
1973: static enum window_copy_cmd_action
1.216 nicm 1974: window_copy_cmd_copy_pipe_no_clear(struct window_copy_cmd_state *cs)
1.213 nicm 1975: {
1976: struct window_mode_entry *wme = cs->wme;
1.215 nicm 1977: struct client *c = cs->c;
1.213 nicm 1978: struct session *s = cs->s;
1.215 nicm 1979: struct winlink *wl = cs->wl;
1980: struct window_pane *wp = wme->wp;
1.329 nicm 1981: char *command = NULL, *prefix = NULL;
1982: const char *arg1 = args_string(cs->args, 1);
1983: const char *arg2 = args_string(cs->args, 2);
1.215 nicm 1984:
1.329 nicm 1985: if (arg2 != NULL)
1986: prefix = format_single(NULL, arg2, c, s, wl, wp);
1.215 nicm 1987:
1.329 nicm 1988: if (s != NULL && arg1 != NULL && *arg1 != '\0')
1989: command = format_single(NULL, arg1, c, s, wl, wp);
1.277 nicm 1990: window_copy_copy_pipe(wme, s, prefix, command);
1991: free(command);
1.213 nicm 1992:
1.215 nicm 1993: free(prefix);
1.213 nicm 1994: return (WINDOW_COPY_CMD_NOTHING);
1995: }
1996:
1997: static enum window_copy_cmd_action
1.216 nicm 1998: window_copy_cmd_copy_pipe(struct window_copy_cmd_state *cs)
1.213 nicm 1999: {
2000: struct window_mode_entry *wme = cs->wme;
1.215 nicm 2001:
1.216 nicm 2002: window_copy_cmd_copy_pipe_no_clear(cs);
2003: window_copy_clear_selection(wme);
2004: return (WINDOW_COPY_CMD_REDRAW);
2005: }
1.215 nicm 2006:
1.216 nicm 2007: static enum window_copy_cmd_action
2008: window_copy_cmd_copy_pipe_and_cancel(struct window_copy_cmd_state *cs)
2009: {
2010: struct window_mode_entry *wme = cs->wme;
1.213 nicm 2011:
1.216 nicm 2012: window_copy_cmd_copy_pipe_no_clear(cs);
2013: window_copy_clear_selection(wme);
2014: return (WINDOW_COPY_CMD_CANCEL);
1.213 nicm 2015: }
2016:
2017: static enum window_copy_cmd_action
1.314 nicm 2018: window_copy_cmd_pipe_no_clear(struct window_copy_cmd_state *cs)
2019: {
2020: struct window_mode_entry *wme = cs->wme;
2021: struct client *c = cs->c;
2022: struct session *s = cs->s;
2023: struct winlink *wl = cs->wl;
2024: struct window_pane *wp = wme->wp;
2025: char *command = NULL;
1.329 nicm 2026: const char *arg1 = args_string(cs->args, 1);
1.314 nicm 2027:
1.329 nicm 2028: if (s != NULL && arg1 != NULL && *arg1 != '\0')
2029: command = format_single(NULL, arg1, c, s, wl, wp);
1.314 nicm 2030: window_copy_pipe(wme, s, command);
2031: free(command);
2032:
2033: return (WINDOW_COPY_CMD_NOTHING);
2034: }
2035:
2036: static enum window_copy_cmd_action
2037: window_copy_cmd_pipe(struct window_copy_cmd_state *cs)
2038: {
2039: struct window_mode_entry *wme = cs->wme;
2040:
2041: window_copy_cmd_pipe_no_clear(cs);
2042: window_copy_clear_selection(wme);
2043: return (WINDOW_COPY_CMD_REDRAW);
2044: }
2045:
2046: static enum window_copy_cmd_action
2047: window_copy_cmd_pipe_and_cancel(struct window_copy_cmd_state *cs)
2048: {
2049: struct window_mode_entry *wme = cs->wme;
2050:
2051: window_copy_cmd_pipe_no_clear(cs);
2052: window_copy_clear_selection(wme);
2053: return (WINDOW_COPY_CMD_CANCEL);
2054: }
2055:
2056: static enum window_copy_cmd_action
1.213 nicm 2057: window_copy_cmd_goto_line(struct window_copy_cmd_state *cs)
2058: {
2059: struct window_mode_entry *wme = cs->wme;
1.329 nicm 2060: const char *arg1 = args_string(cs->args, 1);
1.213 nicm 2061:
1.329 nicm 2062: if (*arg1 != '\0')
2063: window_copy_goto_line(wme, arg1);
1.213 nicm 2064: return (WINDOW_COPY_CMD_NOTHING);
2065: }
2066:
2067: static enum window_copy_cmd_action
2068: window_copy_cmd_jump_backward(struct window_copy_cmd_state *cs)
2069: {
2070: struct window_mode_entry *wme = cs->wme;
2071: struct window_copy_mode_data *data = wme->data;
2072: u_int np = wme->prefix;
1.329 nicm 2073: const char *arg1 = args_string(cs->args, 1);
1.213 nicm 2074:
1.329 nicm 2075: if (*arg1 != '\0') {
1.213 nicm 2076: data->jumptype = WINDOW_COPY_JUMPBACKWARD;
1.315 nicm 2077: free(data->jumpchar);
1.329 nicm 2078: data->jumpchar = utf8_fromcstr(arg1);
1.213 nicm 2079: for (; np != 0; np--)
2080: window_copy_cursor_jump_back(wme);
2081: }
2082: return (WINDOW_COPY_CMD_NOTHING);
2083: }
2084:
2085: static enum window_copy_cmd_action
2086: window_copy_cmd_jump_forward(struct window_copy_cmd_state *cs)
2087: {
2088: struct window_mode_entry *wme = cs->wme;
2089: struct window_copy_mode_data *data = wme->data;
2090: u_int np = wme->prefix;
1.329 nicm 2091: const char *arg1 = args_string(cs->args, 1);
1.213 nicm 2092:
1.329 nicm 2093: if (*arg1 != '\0') {
1.213 nicm 2094: data->jumptype = WINDOW_COPY_JUMPFORWARD;
1.315 nicm 2095: free(data->jumpchar);
1.329 nicm 2096: data->jumpchar = utf8_fromcstr(arg1);
1.213 nicm 2097: for (; np != 0; np--)
1.215 nicm 2098: window_copy_cursor_jump(wme);
1.213 nicm 2099: }
2100: return (WINDOW_COPY_CMD_NOTHING);
2101: }
2102:
2103: static enum window_copy_cmd_action
2104: window_copy_cmd_jump_to_backward(struct window_copy_cmd_state *cs)
2105: {
2106: struct window_mode_entry *wme = cs->wme;
2107: struct window_copy_mode_data *data = wme->data;
2108: u_int np = wme->prefix;
1.329 nicm 2109: const char *arg1 = args_string(cs->args, 1);
1.213 nicm 2110:
1.329 nicm 2111: if (*arg1 != '\0') {
1.213 nicm 2112: data->jumptype = WINDOW_COPY_JUMPTOBACKWARD;
1.315 nicm 2113: free(data->jumpchar);
1.329 nicm 2114: data->jumpchar = utf8_fromcstr(arg1);
1.213 nicm 2115: for (; np != 0; np--)
2116: window_copy_cursor_jump_to_back(wme);
2117: }
2118: return (WINDOW_COPY_CMD_NOTHING);
2119: }
2120:
2121: static enum window_copy_cmd_action
2122: window_copy_cmd_jump_to_forward(struct window_copy_cmd_state *cs)
2123: {
2124: struct window_mode_entry *wme = cs->wme;
2125: struct window_copy_mode_data *data = wme->data;
2126: u_int np = wme->prefix;
1.329 nicm 2127: const char *arg1 = args_string(cs->args, 1);
1.213 nicm 2128:
1.329 nicm 2129: if (*arg1 != '\0') {
1.213 nicm 2130: data->jumptype = WINDOW_COPY_JUMPTOFORWARD;
1.315 nicm 2131: free(data->jumpchar);
1.329 nicm 2132: data->jumpchar = utf8_fromcstr(arg1);
1.213 nicm 2133: for (; np != 0; np--)
2134: window_copy_cursor_jump_to(wme);
2135: }
2136: return (WINDOW_COPY_CMD_NOTHING);
2137: }
2138:
2139: static enum window_copy_cmd_action
1.288 nicm 2140: window_copy_cmd_jump_to_mark(struct window_copy_cmd_state *cs)
2141: {
2142: struct window_mode_entry *wme = cs->wme;
2143:
2144: window_copy_jump_to_mark(wme);
2145: return (WINDOW_COPY_CMD_NOTHING);
2146: }
2147:
2148: static enum window_copy_cmd_action
1.213 nicm 2149: window_copy_cmd_search_backward(struct window_copy_cmd_state *cs)
2150: {
2151: struct window_mode_entry *wme = cs->wme;
2152: struct window_copy_mode_data *data = wme->data;
2153: u_int np = wme->prefix;
2154:
1.259 nicm 2155: if (!window_copy_expand_search_string(cs))
2156: return (WINDOW_COPY_CMD_NOTHING);
2157:
2158: if (data->searchstr != NULL) {
2159: data->searchtype = WINDOW_COPY_SEARCHUP;
2160: data->searchregex = 1;
1.263 nicm 2161: data->timeout = 0;
1.259 nicm 2162: for (; np != 0; np--)
1.321 nicm 2163: window_copy_search_up(wme, 1);
1.233 nicm 2164: }
1.259 nicm 2165: return (WINDOW_COPY_CMD_NOTHING);
2166: }
2167:
2168: static enum window_copy_cmd_action
2169: window_copy_cmd_search_backward_text(struct window_copy_cmd_state *cs)
2170: {
2171: struct window_mode_entry *wme = cs->wme;
2172: struct window_copy_mode_data *data = wme->data;
2173: u_int np = wme->prefix;
2174:
2175: if (!window_copy_expand_search_string(cs))
2176: return (WINDOW_COPY_CMD_NOTHING);
2177:
1.233 nicm 2178: if (data->searchstr != NULL) {
1.232 nicm 2179: data->searchtype = WINDOW_COPY_SEARCHUP;
1.259 nicm 2180: data->searchregex = 0;
1.263 nicm 2181: data->timeout = 0;
1.213 nicm 2182: for (; np != 0; np--)
1.321 nicm 2183: window_copy_search_up(wme, 0);
1.213 nicm 2184: }
2185: return (WINDOW_COPY_CMD_NOTHING);
2186: }
2187:
2188: static enum window_copy_cmd_action
2189: window_copy_cmd_search_forward(struct window_copy_cmd_state *cs)
2190: {
2191: struct window_mode_entry *wme = cs->wme;
2192: struct window_copy_mode_data *data = wme->data;
2193: u_int np = wme->prefix;
2194:
1.259 nicm 2195: if (!window_copy_expand_search_string(cs))
2196: return (WINDOW_COPY_CMD_NOTHING);
2197:
2198: if (data->searchstr != NULL) {
2199: data->searchtype = WINDOW_COPY_SEARCHDOWN;
2200: data->searchregex = 1;
1.263 nicm 2201: data->timeout = 0;
1.259 nicm 2202: for (; np != 0; np--)
1.321 nicm 2203: window_copy_search_down(wme, 1);
1.233 nicm 2204: }
1.259 nicm 2205: return (WINDOW_COPY_CMD_NOTHING);
2206: }
2207:
2208: static enum window_copy_cmd_action
2209: window_copy_cmd_search_forward_text(struct window_copy_cmd_state *cs)
2210: {
2211: struct window_mode_entry *wme = cs->wme;
2212: struct window_copy_mode_data *data = wme->data;
2213: u_int np = wme->prefix;
2214:
2215: if (!window_copy_expand_search_string(cs))
2216: return (WINDOW_COPY_CMD_NOTHING);
2217:
1.233 nicm 2218: if (data->searchstr != NULL) {
1.232 nicm 2219: data->searchtype = WINDOW_COPY_SEARCHDOWN;
1.259 nicm 2220: data->searchregex = 0;
1.263 nicm 2221: data->timeout = 0;
1.213 nicm 2222: for (; np != 0; np--)
1.321 nicm 2223: window_copy_search_down(wme, 0);
1.213 nicm 2224: }
2225: return (WINDOW_COPY_CMD_NOTHING);
2226: }
2227:
2228: static enum window_copy_cmd_action
2229: window_copy_cmd_search_backward_incremental(struct window_copy_cmd_state *cs)
2230: {
2231: struct window_mode_entry *wme = cs->wme;
2232: struct window_copy_mode_data *data = wme->data;
1.329 nicm 2233: const char *arg1 = args_string(cs->args, 1);
1.225 nicm 2234: const char *ss = data->searchstr;
2235: char prefix;
1.213 nicm 2236: enum window_copy_cmd_action action = WINDOW_COPY_CMD_NOTHING;
2237:
1.263 nicm 2238: data->timeout = 0;
2239:
1.329 nicm 2240: log_debug("%s: %s", __func__, arg1);
1.310 nicm 2241:
1.329 nicm 2242: prefix = *arg1++;
1.213 nicm 2243: if (data->searchx == -1 || data->searchy == -1) {
2244: data->searchx = data->cx;
2245: data->searchy = data->cy;
2246: data->searcho = data->oy;
1.329 nicm 2247: } else if (ss != NULL && strcmp(arg1, ss) != 0) {
1.213 nicm 2248: data->cx = data->searchx;
2249: data->cy = data->searchy;
2250: data->oy = data->searcho;
2251: action = WINDOW_COPY_CMD_REDRAW;
2252: }
1.329 nicm 2253: if (*arg1 == '\0') {
1.213 nicm 2254: window_copy_clear_marks(wme);
2255: return (WINDOW_COPY_CMD_REDRAW);
2256: }
1.225 nicm 2257: switch (prefix) {
1.213 nicm 2258: case '=':
2259: case '-':
2260: data->searchtype = WINDOW_COPY_SEARCHUP;
1.259 nicm 2261: data->searchregex = 0;
1.213 nicm 2262: free(data->searchstr);
1.329 nicm 2263: data->searchstr = xstrdup(arg1);
1.321 nicm 2264: if (!window_copy_search_up(wme, 0)) {
1.213 nicm 2265: window_copy_clear_marks(wme);
2266: return (WINDOW_COPY_CMD_REDRAW);
2267: }
2268: break;
2269: case '+':
2270: data->searchtype = WINDOW_COPY_SEARCHDOWN;
1.259 nicm 2271: data->searchregex = 0;
1.213 nicm 2272: free(data->searchstr);
1.329 nicm 2273: data->searchstr = xstrdup(arg1);
1.321 nicm 2274: if (!window_copy_search_down(wme, 0)) {
1.213 nicm 2275: window_copy_clear_marks(wme);
2276: return (WINDOW_COPY_CMD_REDRAW);
2277: }
2278: break;
2279: }
2280: return (action);
2281: }
2282:
2283: static enum window_copy_cmd_action
2284: window_copy_cmd_search_forward_incremental(struct window_copy_cmd_state *cs)
2285: {
2286: struct window_mode_entry *wme = cs->wme;
2287: struct window_copy_mode_data *data = wme->data;
1.329 nicm 2288: const char *arg1 = args_string(cs->args, 1);
1.225 nicm 2289: const char *ss = data->searchstr;
2290: char prefix;
1.213 nicm 2291: enum window_copy_cmd_action action = WINDOW_COPY_CMD_NOTHING;
2292:
1.263 nicm 2293: data->timeout = 0;
1.310 nicm 2294:
1.329 nicm 2295: log_debug("%s: %s", __func__, arg1);
1.263 nicm 2296:
1.329 nicm 2297: prefix = *arg1++;
1.213 nicm 2298: if (data->searchx == -1 || data->searchy == -1) {
2299: data->searchx = data->cx;
2300: data->searchy = data->cy;
2301: data->searcho = data->oy;
1.329 nicm 2302: } else if (ss != NULL && strcmp(arg1, ss) != 0) {
1.213 nicm 2303: data->cx = data->searchx;
2304: data->cy = data->searchy;
2305: data->oy = data->searcho;
2306: action = WINDOW_COPY_CMD_REDRAW;
2307: }
1.329 nicm 2308: if (*arg1 == '\0') {
1.213 nicm 2309: window_copy_clear_marks(wme);
2310: return (WINDOW_COPY_CMD_REDRAW);
2311: }
1.225 nicm 2312: switch (prefix) {
1.213 nicm 2313: case '=':
2314: case '+':
2315: data->searchtype = WINDOW_COPY_SEARCHDOWN;
1.259 nicm 2316: data->searchregex = 0;
1.213 nicm 2317: free(data->searchstr);
1.329 nicm 2318: data->searchstr = xstrdup(arg1);
1.321 nicm 2319: if (!window_copy_search_down(wme, 0)) {
1.213 nicm 2320: window_copy_clear_marks(wme);
2321: return (WINDOW_COPY_CMD_REDRAW);
2322: }
2323: break;
2324: case '-':
2325: data->searchtype = WINDOW_COPY_SEARCHUP;
1.259 nicm 2326: data->searchregex = 0;
1.213 nicm 2327: free(data->searchstr);
1.329 nicm 2328: data->searchstr = xstrdup(arg1);
1.321 nicm 2329: if (!window_copy_search_up(wme, 0)) {
1.213 nicm 2330: window_copy_clear_marks(wme);
2331: return (WINDOW_COPY_CMD_REDRAW);
2332: }
2333: }
2334: return (action);
2335: }
2336:
1.266 nicm 2337: static enum window_copy_cmd_action
2338: window_copy_cmd_refresh_from_pane(struct window_copy_cmd_state *cs)
2339: {
2340: struct window_mode_entry *wme = cs->wme;
1.270 nicm 2341: struct window_pane *wp = wme->swp;
1.266 nicm 2342: struct window_copy_mode_data *data = wme->data;
2343:
2344: if (data->viewmode)
2345: return (WINDOW_COPY_CMD_NOTHING);
2346:
2347: screen_free(data->backing);
2348: free(data->backing);
1.326 nicm 2349: data->backing = window_copy_clone_screen(&wp->base, &data->screen, NULL, NULL, wme->swp != wme->wp);
1.266 nicm 2350:
1.275 nicm 2351: window_copy_size_changed(wme);
1.266 nicm 2352: return (WINDOW_COPY_CMD_REDRAW);
2353: }
2354:
1.213 nicm 2355: static const struct {
2356: const char *command;
1.329 nicm 2357: u_int minargs;
2358: u_int maxargs;
1.295 nicm 2359: enum window_copy_cmd_clear clear;
1.213 nicm 2360: enum window_copy_cmd_action (*f)(struct window_copy_cmd_state *);
2361: } window_copy_cmd_table[] = {
1.326 nicm 2362: { .command = "append-selection",
2363: .minargs = 0,
2364: .maxargs = 0,
2365: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2366: .f = window_copy_cmd_append_selection
2367: },
2368: { .command = "append-selection-and-cancel",
2369: .minargs = 0,
2370: .maxargs = 0,
2371: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2372: .f = window_copy_cmd_append_selection_and_cancel
2373: },
2374: { .command = "back-to-indentation",
2375: .minargs = 0,
2376: .maxargs = 0,
2377: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2378: .f = window_copy_cmd_back_to_indentation
2379: },
2380: { .command = "begin-selection",
2381: .minargs = 0,
2382: .maxargs = 0,
2383: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2384: .f = window_copy_cmd_begin_selection
2385: },
2386: { .command = "bottom-line",
2387: .minargs = 0,
2388: .maxargs = 0,
2389: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2390: .f = window_copy_cmd_bottom_line
2391: },
2392: { .command = "cancel",
2393: .minargs = 0,
2394: .maxargs = 0,
2395: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2396: .f = window_copy_cmd_cancel
2397: },
2398: { .command = "clear-selection",
2399: .minargs = 0,
2400: .maxargs = 0,
2401: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2402: .f = window_copy_cmd_clear_selection
2403: },
2404: { .command = "copy-end-of-line",
2405: .minargs = 0,
2406: .maxargs = 1,
2407: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2408: .f = window_copy_cmd_copy_end_of_line
2409: },
2410: { .command = "copy-end-of-line-and-cancel",
2411: .minargs = 0,
2412: .maxargs = 1,
2413: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2414: .f = window_copy_cmd_copy_end_of_line_and_cancel
2415: },
2416: { .command = "copy-pipe-end-of-line",
2417: .minargs = 0,
2418: .maxargs = 2,
2419: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2420: .f = window_copy_cmd_copy_pipe_end_of_line
2421: },
2422: { .command = "copy-pipe-end-of-line-and-cancel",
2423: .minargs = 0,
2424: .maxargs = 2,
2425: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2426: .f = window_copy_cmd_copy_pipe_end_of_line_and_cancel
2427: },
2428: { .command = "copy-line",
2429: .minargs = 0,
2430: .maxargs = 1,
2431: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2432: .f = window_copy_cmd_copy_line
2433: },
2434: { .command = "copy-line-and-cancel",
2435: .minargs = 0,
2436: .maxargs = 1,
2437: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2438: .f = window_copy_cmd_copy_line_and_cancel
2439: },
2440: { .command = "copy-pipe-line",
2441: .minargs = 0,
2442: .maxargs = 2,
2443: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2444: .f = window_copy_cmd_copy_pipe_line
2445: },
2446: { .command = "copy-pipe-line-and-cancel",
2447: .minargs = 0,
2448: .maxargs = 2,
2449: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2450: .f = window_copy_cmd_copy_pipe_line_and_cancel
2451: },
2452: { .command = "copy-pipe-no-clear",
2453: .minargs = 0,
2454: .maxargs = 2,
2455: .clear = WINDOW_COPY_CMD_CLEAR_NEVER,
2456: .f = window_copy_cmd_copy_pipe_no_clear
2457: },
2458: { .command = "copy-pipe",
2459: .minargs = 0,
2460: .maxargs = 2,
2461: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2462: .f = window_copy_cmd_copy_pipe
2463: },
2464: { .command = "copy-pipe-and-cancel",
2465: .minargs = 0,
2466: .maxargs = 2,
2467: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2468: .f = window_copy_cmd_copy_pipe_and_cancel
2469: },
2470: { .command = "copy-selection-no-clear",
2471: .minargs = 0,
2472: .maxargs = 1,
2473: .clear = WINDOW_COPY_CMD_CLEAR_NEVER,
2474: .f = window_copy_cmd_copy_selection_no_clear
2475: },
2476: { .command = "copy-selection",
2477: .minargs = 0,
2478: .maxargs = 1,
2479: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2480: .f = window_copy_cmd_copy_selection
2481: },
2482: { .command = "copy-selection-and-cancel",
2483: .minargs = 0,
2484: .maxargs = 1,
2485: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2486: .f = window_copy_cmd_copy_selection_and_cancel
2487: },
2488: { .command = "cursor-down",
2489: .minargs = 0,
2490: .maxargs = 0,
2491: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2492: .f = window_copy_cmd_cursor_down
2493: },
2494: { .command = "cursor-down-and-cancel",
2495: .minargs = 0,
2496: .maxargs = 0,
2497: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2498: .f = window_copy_cmd_cursor_down_and_cancel
2499: },
2500: { .command = "cursor-left",
2501: .minargs = 0,
2502: .maxargs = 0,
2503: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2504: .f = window_copy_cmd_cursor_left
2505: },
2506: { .command = "cursor-right",
2507: .minargs = 0,
2508: .maxargs = 0,
2509: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2510: .f = window_copy_cmd_cursor_right
2511: },
2512: { .command = "cursor-up",
2513: .minargs = 0,
2514: .maxargs = 0,
2515: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2516: .f = window_copy_cmd_cursor_up
2517: },
2518: { .command = "end-of-line",
2519: .minargs = 0,
2520: .maxargs = 0,
2521: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2522: .f = window_copy_cmd_end_of_line
2523: },
2524: { .command = "goto-line",
2525: .minargs = 1,
2526: .maxargs = 1,
2527: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2528: .f = window_copy_cmd_goto_line
2529: },
2530: { .command = "halfpage-down",
2531: .minargs = 0,
2532: .maxargs = 0,
2533: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2534: .f = window_copy_cmd_halfpage_down
2535: },
2536: { .command = "halfpage-down-and-cancel",
2537: .minargs = 0,
2538: .maxargs = 0,
2539: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2540: .f = window_copy_cmd_halfpage_down_and_cancel
2541: },
2542: { .command = "halfpage-up",
2543: .minargs = 0,
2544: .maxargs = 0,
2545: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2546: .f = window_copy_cmd_halfpage_up
2547: },
2548: { .command = "history-bottom",
2549: .minargs = 0,
2550: .maxargs = 0,
2551: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2552: .f = window_copy_cmd_history_bottom
2553: },
2554: { .command = "history-top",
2555: .minargs = 0,
2556: .maxargs = 0,
2557: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2558: .f = window_copy_cmd_history_top
2559: },
2560: { .command = "jump-again",
2561: .minargs = 0,
2562: .maxargs = 0,
2563: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2564: .f = window_copy_cmd_jump_again
2565: },
2566: { .command = "jump-backward",
2567: .minargs = 1,
2568: .maxargs = 1,
2569: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2570: .f = window_copy_cmd_jump_backward
2571: },
2572: { .command = "jump-forward",
2573: .minargs = 1,
2574: .maxargs = 1,
2575: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2576: .f = window_copy_cmd_jump_forward
2577: },
2578: { .command = "jump-reverse",
2579: .minargs = 0,
2580: .maxargs = 0,
2581: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2582: .f = window_copy_cmd_jump_reverse
2583: },
2584: { .command = "jump-to-backward",
2585: .minargs = 1,
2586: .maxargs = 1,
2587: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2588: .f = window_copy_cmd_jump_to_backward
2589: },
2590: { .command = "jump-to-forward",
2591: .minargs = 1,
2592: .maxargs = 1,
2593: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2594: .f = window_copy_cmd_jump_to_forward
2595: },
2596: { .command = "jump-to-mark",
2597: .minargs = 0,
2598: .maxargs = 0,
2599: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2600: .f = window_copy_cmd_jump_to_mark
2601: },
2602: { .command = "middle-line",
2603: .minargs = 0,
2604: .maxargs = 0,
2605: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2606: .f = window_copy_cmd_middle_line
2607: },
2608: { .command = "next-matching-bracket",
2609: .minargs = 0,
2610: .maxargs = 0,
2611: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2612: .f = window_copy_cmd_next_matching_bracket
2613: },
2614: { .command = "next-paragraph",
2615: .minargs = 0,
2616: .maxargs = 0,
2617: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2618: .f = window_copy_cmd_next_paragraph
2619: },
2620: { .command = "next-space",
2621: .minargs = 0,
2622: .maxargs = 0,
2623: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2624: .f = window_copy_cmd_next_space
2625: },
2626: { .command = "next-space-end",
2627: .minargs = 0,
2628: .maxargs = 0,
2629: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2630: .f = window_copy_cmd_next_space_end
2631: },
2632: { .command = "next-word",
2633: .minargs = 0,
2634: .maxargs = 0,
2635: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2636: .f = window_copy_cmd_next_word
2637: },
2638: { .command = "next-word-end",
2639: .minargs = 0,
2640: .maxargs = 0,
2641: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2642: .f = window_copy_cmd_next_word_end
2643: },
2644: { .command = "other-end",
2645: .minargs = 0,
2646: .maxargs = 0,
2647: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2648: .f = window_copy_cmd_other_end
2649: },
2650: { .command = "page-down",
2651: .minargs = 0,
2652: .maxargs = 0,
2653: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2654: .f = window_copy_cmd_page_down
2655: },
2656: { .command = "page-down-and-cancel",
2657: .minargs = 0,
2658: .maxargs = 0,
2659: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2660: .f = window_copy_cmd_page_down_and_cancel
2661: },
2662: { .command = "page-up",
2663: .minargs = 0,
2664: .maxargs = 0,
2665: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2666: .f = window_copy_cmd_page_up
2667: },
2668: { .command = "pipe-no-clear",
2669: .minargs = 0,
2670: .maxargs = 1,
2671: .clear = WINDOW_COPY_CMD_CLEAR_NEVER,
2672: .f = window_copy_cmd_pipe_no_clear
2673: },
2674: { .command = "pipe",
2675: .minargs = 0,
2676: .maxargs = 1,
2677: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2678: .f = window_copy_cmd_pipe
2679: },
2680: { .command = "pipe-and-cancel",
2681: .minargs = 0,
2682: .maxargs = 1,
2683: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2684: .f = window_copy_cmd_pipe_and_cancel
2685: },
2686: { .command = "previous-matching-bracket",
2687: .minargs = 0,
2688: .maxargs = 0,
2689: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2690: .f = window_copy_cmd_previous_matching_bracket
2691: },
2692: { .command = "previous-paragraph",
2693: .minargs = 0,
2694: .maxargs = 0,
2695: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2696: .f = window_copy_cmd_previous_paragraph
2697: },
2698: { .command = "previous-space",
2699: .minargs = 0,
2700: .maxargs = 0,
2701: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2702: .f = window_copy_cmd_previous_space
2703: },
2704: { .command = "previous-word",
2705: .minargs = 0,
2706: .maxargs = 0,
2707: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2708: .f = window_copy_cmd_previous_word
2709: },
2710: { .command = "rectangle-on",
2711: .minargs = 0,
2712: .maxargs = 0,
2713: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2714: .f = window_copy_cmd_rectangle_on
2715: },
2716: { .command = "rectangle-off",
2717: .minargs = 0,
2718: .maxargs = 0,
2719: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2720: .f = window_copy_cmd_rectangle_off
2721: },
2722: { .command = "rectangle-toggle",
2723: .minargs = 0,
2724: .maxargs = 0,
2725: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2726: .f = window_copy_cmd_rectangle_toggle
2727: },
2728: { .command = "refresh-from-pane",
2729: .minargs = 0,
2730: .maxargs = 0,
2731: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2732: .f = window_copy_cmd_refresh_from_pane
2733: },
2734: { .command = "scroll-down",
2735: .minargs = 0,
2736: .maxargs = 0,
2737: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2738: .f = window_copy_cmd_scroll_down
2739: },
2740: { .command = "scroll-down-and-cancel",
2741: .minargs = 0,
2742: .maxargs = 0,
2743: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2744: .f = window_copy_cmd_scroll_down_and_cancel
2745: },
2746: { .command = "scroll-up",
2747: .minargs = 0,
2748: .maxargs = 0,
2749: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2750: .f = window_copy_cmd_scroll_up
2751: },
2752: { .command = "search-again",
2753: .minargs = 0,
2754: .maxargs = 0,
2755: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2756: .f = window_copy_cmd_search_again
2757: },
2758: { .command = "search-backward",
2759: .minargs = 0,
2760: .maxargs = 1,
2761: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2762: .f = window_copy_cmd_search_backward
2763: },
2764: { .command = "search-backward-text",
2765: .minargs = 0,
2766: .maxargs = 1,
2767: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2768: .f = window_copy_cmd_search_backward_text
2769: },
2770: { .command = "search-backward-incremental",
2771: .minargs = 1,
2772: .maxargs = 1,
2773: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2774: .f = window_copy_cmd_search_backward_incremental
2775: },
2776: { .command = "search-forward",
2777: .minargs = 0,
2778: .maxargs = 1,
2779: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2780: .f = window_copy_cmd_search_forward
2781: },
2782: { .command = "search-forward-text",
2783: .minargs = 0,
2784: .maxargs = 1,
2785: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2786: .f = window_copy_cmd_search_forward_text
2787: },
2788: { .command = "search-forward-incremental",
2789: .minargs = 1,
2790: .maxargs = 1,
2791: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2792: .f = window_copy_cmd_search_forward_incremental
2793: },
2794: { .command = "search-reverse",
2795: .minargs = 0,
2796: .maxargs = 0,
2797: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2798: .f = window_copy_cmd_search_reverse
2799: },
2800: { .command = "select-line",
2801: .minargs = 0,
2802: .maxargs = 0,
2803: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2804: .f = window_copy_cmd_select_line
2805: },
2806: { .command = "select-word",
2807: .minargs = 0,
2808: .maxargs = 0,
2809: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2810: .f = window_copy_cmd_select_word
2811: },
2812: { .command = "set-mark",
2813: .minargs = 0,
2814: .maxargs = 0,
2815: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2816: .f = window_copy_cmd_set_mark
2817: },
2818: { .command = "start-of-line",
2819: .minargs = 0,
2820: .maxargs = 0,
2821: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2822: .f = window_copy_cmd_start_of_line
2823: },
2824: { .command = "stop-selection",
2825: .minargs = 0,
2826: .maxargs = 0,
2827: .clear = WINDOW_COPY_CMD_CLEAR_ALWAYS,
2828: .f = window_copy_cmd_stop_selection
1.333 ! nicm 2829: },
! 2830: { .command = "toggle-position",
! 2831: .minargs = 0,
! 2832: .maxargs = 0,
! 2833: .clear = WINDOW_COPY_CMD_CLEAR_NEVER,
! 2834: .f = window_copy_cmd_toggle_position
1.326 nicm 2835: },
2836: { .command = "top-line",
2837: .minargs = 0,
2838: .maxargs = 0,
2839: .clear = WINDOW_COPY_CMD_CLEAR_EMACS_ONLY,
2840: .f = window_copy_cmd_top_line
2841: }
1.213 nicm 2842: };
2843:
1.157 nicm 2844: static void
1.208 nicm 2845: window_copy_command(struct window_mode_entry *wme, struct client *c,
1.215 nicm 2846: struct session *s, struct winlink *wl, struct args *args,
1.208 nicm 2847: struct mouse_event *m)
1.1 nicm 2848: {
1.208 nicm 2849: struct window_copy_mode_data *data = wme->data;
1.213 nicm 2850: struct window_copy_cmd_state cs;
2851: enum window_copy_cmd_action action;
1.295 nicm 2852: enum window_copy_cmd_clear clear = WINDOW_COPY_CMD_CLEAR_NEVER;
1.213 nicm 2853: const char *command;
1.329 nicm 2854: u_int i, count = args_count(args);
1.295 nicm 2855: int keys;
1.155 nicm 2856:
1.329 nicm 2857: if (count == 0)
1.155 nicm 2858: return;
1.329 nicm 2859: command = args_string(args, 0);
1.330 nicm 2860:
1.202 nicm 2861: if (m != NULL && m->valid && !MOUSE_WHEEL(m->b))
1.156 nicm 2862: window_copy_move_mouse(m);
2863:
1.213 nicm 2864: cs.wme = wme;
2865: cs.args = args;
2866: cs.m = m;
1.215 nicm 2867:
1.213 nicm 2868: cs.c = c;
2869: cs.s = s;
1.215 nicm 2870: cs.wl = wl;
1.213 nicm 2871:
2872: action = WINDOW_COPY_CMD_NOTHING;
2873: for (i = 0; i < nitems(window_copy_cmd_table); i++) {
2874: if (strcmp(window_copy_cmd_table[i].command, command) == 0) {
1.329 nicm 2875: if (count - 1 < window_copy_cmd_table[i].minargs ||
2876: count - 1 > window_copy_cmd_table[i].maxargs)
1.155 nicm 2877: break;
1.295 nicm 2878: clear = window_copy_cmd_table[i].clear;
1.327 nicm 2879: action = window_copy_cmd_table[i].f(&cs);
1.213 nicm 2880: break;
1.163 nicm 2881: }
1.1 nicm 2882: }
1.21 nicm 2883:
1.162 nicm 2884: if (strncmp(command, "search-", 7) != 0 && data->searchmark != NULL) {
1.239 nicm 2885: keys = options_get_number(wme->wp->window->options, "mode-keys");
1.295 nicm 2886: if (clear == WINDOW_COPY_CMD_CLEAR_EMACS_ONLY &&
2887: keys == MODEKEY_VI)
2888: clear = WINDOW_COPY_CMD_CLEAR_NEVER;
2889: if (clear != WINDOW_COPY_CMD_CLEAR_NEVER) {
1.239 nicm 2890: window_copy_clear_marks(wme);
2891: data->searchx = data->searchy = -1;
2892: }
1.213 nicm 2893: if (action == WINDOW_COPY_CMD_NOTHING)
2894: action = WINDOW_COPY_CMD_REDRAW;
1.162 nicm 2895: }
1.209 nicm 2896: wme->prefix = 1;
2897:
1.213 nicm 2898: if (action == WINDOW_COPY_CMD_CANCEL)
2899: window_pane_reset_mode(wme->wp);
2900: else if (action == WINDOW_COPY_CMD_REDRAW)
1.208 nicm 2901: window_copy_redraw_screen(wme);
1.50 nicm 2902: }
2903:
1.157 nicm 2904: static void
1.296 nicm 2905: window_copy_scroll_to(struct window_mode_entry *wme, u_int px, u_int py,
2906: int no_redraw)
1.21 nicm 2907: {
1.208 nicm 2908: struct window_copy_mode_data *data = wme->data;
1.54 nicm 2909: struct grid *gd = data->backing->grid;
1.21 nicm 2910: u_int offset, gap;
2911:
2912: data->cx = px;
2913:
1.185 nicm 2914: if (py >= gd->hsize - data->oy && py < gd->hsize - data->oy + gd->sy)
2915: data->cy = py - (gd->hsize - data->oy);
2916: else {
2917: gap = gd->sy / 4;
2918: if (py < gd->sy) {
2919: offset = 0;
2920: data->cy = py;
2921: } else if (py > gd->hsize + gd->sy - gap) {
2922: offset = gd->hsize;
2923: data->cy = py - gd->hsize;
2924: } else {
2925: offset = py + gap - gd->sy;
2926: data->cy = py - offset;
2927: }
2928: data->oy = gd->hsize - offset;
1.21 nicm 2929: }
2930:
1.296 nicm 2931: if (!no_redraw && data->searchmark != NULL && !data->timeout)
2932: window_copy_search_marks(wme, NULL, data->searchregex, 1);
1.252 nicm 2933: window_copy_update_selection(wme, 1, 0);
1.296 nicm 2934: if (!no_redraw)
2935: window_copy_redraw_screen(wme);
1.21 nicm 2936: }
2937:
1.157 nicm 2938: static int
1.118 nicm 2939: window_copy_search_compare(struct grid *gd, u_int px, u_int py,
2940: struct grid *sgd, u_int spx, int cis)
1.21 nicm 2941: {
1.140 nicm 2942: struct grid_cell gc, sgc;
2943: const struct utf8_data *ud, *sud;
1.21 nicm 2944:
1.140 nicm 2945: grid_get_cell(gd, px, py, &gc);
2946: ud = &gc.data;
2947: grid_get_cell(sgd, spx, 0, &sgc);
2948: sud = &sgc.data;
1.35 nicm 2949:
1.140 nicm 2950: if (ud->size != sud->size || ud->width != sud->width)
1.21 nicm 2951: return (0);
1.97 nicm 2952:
1.140 nicm 2953: if (cis && ud->size == 1)
2954: return (tolower(ud->data[0]) == sud->data[0]);
1.97 nicm 2955:
1.140 nicm 2956: return (memcmp(ud->data, sud->data, ud->size) == 0);
1.21 nicm 2957: }
2958:
1.157 nicm 2959: static int
1.302 nicm 2960: window_copy_search_lr(struct grid *gd, struct grid *sgd, u_int *ppx, u_int py,
2961: u_int first, u_int last, int cis)
1.21 nicm 2962: {
1.243 nicm 2963: u_int ax, bx, px, pywrap, endline;
2964: int matched;
2965: struct grid_line *gl;
1.21 nicm 2966:
1.242 nicm 2967: endline = gd->hsize + gd->sy - 1;
1.21 nicm 2968: for (ax = first; ax < last; ax++) {
2969: for (bx = 0; bx < sgd->sx; bx++) {
2970: px = ax + bx;
1.242 nicm 2971: pywrap = py;
2972: /* Wrap line. */
2973: while (px >= gd->sx && pywrap < endline) {
1.243 nicm 2974: gl = grid_get_line(gd, pywrap);
2975: if (~gl->flags & GRID_LINE_WRAPPED)
2976: break;
1.242 nicm 2977: px -= gd->sx;
2978: pywrap++;
2979: }
2980: /* We have run off the end of the grid. */
2981: if (px >= gd->sx)
2982: break;
2983: matched = window_copy_search_compare(gd, px, pywrap,
2984: sgd, bx, cis);
1.97 nicm 2985: if (!matched)
1.21 nicm 2986: break;
2987: }
2988: if (bx == sgd->sx) {
2989: *ppx = ax;
2990: return (1);
2991: }
2992: }
2993: return (0);
2994: }
2995:
1.157 nicm 2996: static int
1.21 nicm 2997: window_copy_search_rl(struct grid *gd,
1.97 nicm 2998: struct grid *sgd, u_int *ppx, u_int py, u_int first, u_int last, int cis)
1.21 nicm 2999: {
1.243 nicm 3000: u_int ax, bx, px, pywrap, endline;
3001: int matched;
3002: struct grid_line *gl;
1.21 nicm 3003:
1.242 nicm 3004: endline = gd->hsize + gd->sy - 1;
3005: for (ax = last; ax > first; ax--) {
1.21 nicm 3006: for (bx = 0; bx < sgd->sx; bx++) {
3007: px = ax - 1 + bx;
1.242 nicm 3008: pywrap = py;
3009: /* Wrap line. */
3010: while (px >= gd->sx && pywrap < endline) {
1.243 nicm 3011: gl = grid_get_line(gd, pywrap);
3012: if (~gl->flags & GRID_LINE_WRAPPED)
3013: break;
1.242 nicm 3014: px -= gd->sx;
3015: pywrap++;
3016: }
3017: /* We have run off the end of the grid. */
3018: if (px >= gd->sx)
3019: break;
3020: matched = window_copy_search_compare(gd, px, pywrap,
3021: sgd, bx, cis);
1.97 nicm 3022: if (!matched)
1.21 nicm 3023: break;
3024: }
3025: if (bx == sgd->sx) {
3026: *ppx = ax - 1;
3027: return (1);
3028: }
3029: }
3030: return (0);
3031: }
3032:
1.244 nicm 3033: static int
1.260 nicm 3034: window_copy_search_lr_regex(struct grid *gd, u_int *ppx, u_int *psx, u_int py,
3035: u_int first, u_int last, regex_t *reg)
1.244 nicm 3036: {
1.260 nicm 3037: int eflags = 0;
1.244 nicm 3038: u_int endline, foundx, foundy, len, pywrap, size = 1;
1.260 nicm 3039: char *buf;
1.244 nicm 3040: regmatch_t regmatch;
3041: struct grid_line *gl;
3042:
3043: /*
3044: * This can happen during search if the last match was the last
3045: * character on a line.
3046: */
3047: if (first >= last)
3048: return (0);
3049:
3050: /* Set flags for regex search. */
3051: if (first != 0)
3052: eflags |= REG_NOTBOL;
3053:
3054: /* Need to look at the entire string. */
3055: buf = xmalloc(size);
3056: buf[0] = '\0';
3057: buf = window_copy_stringify(gd, py, first, gd->sx, buf, &size);
3058: len = gd->sx - first;
3059: endline = gd->hsize + gd->sy - 1;
3060: pywrap = py;
3061: while (buf != NULL && pywrap <= endline) {
3062: gl = grid_get_line(gd, pywrap);
3063: if (~gl->flags & GRID_LINE_WRAPPED)
3064: break;
3065: pywrap++;
3066: buf = window_copy_stringify(gd, pywrap, 0, gd->sx, buf, &size);
3067: len += gd->sx;
3068: }
3069:
1.287 nicm 3070: if (regexec(reg, buf, 1, ®match, eflags) == 0 &&
3071: regmatch.rm_so != regmatch.rm_eo) {
1.244 nicm 3072: foundx = first;
3073: foundy = py;
3074: window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
3075: buf + regmatch.rm_so);
3076: if (foundy == py && foundx < last) {
3077: *ppx = foundx;
3078: len -= foundx - first;
3079: window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
3080: buf + regmatch.rm_eo);
3081: *psx = foundx;
3082: while (foundy > py) {
3083: *psx += gd->sx;
3084: foundy--;
3085: }
3086: *psx -= *ppx;
3087: free(buf);
3088: return (1);
3089: }
3090: }
3091:
3092: free(buf);
3093: *ppx = 0;
3094: *psx = 0;
3095: return (0);
3096: }
3097:
3098: static int
1.260 nicm 3099: window_copy_search_rl_regex(struct grid *gd, u_int *ppx, u_int *psx, u_int py,
3100: u_int first, u_int last, regex_t *reg)
1.244 nicm 3101: {
1.260 nicm 3102: int eflags = 0;
3103: u_int endline, len, pywrap, size = 1;
3104: char *buf;
1.244 nicm 3105: struct grid_line *gl;
3106:
3107: /* Set flags for regex search. */
3108: if (first != 0)
3109: eflags |= REG_NOTBOL;
3110:
3111: /* Need to look at the entire string. */
3112: buf = xmalloc(size);
3113: buf[0] = '\0';
3114: buf = window_copy_stringify(gd, py, first, gd->sx, buf, &size);
3115: len = gd->sx - first;
3116: endline = gd->hsize + gd->sy - 1;
3117: pywrap = py;
3118: while (buf != NULL && (pywrap <= endline)) {
3119: gl = grid_get_line(gd, pywrap);
3120: if (~gl->flags & GRID_LINE_WRAPPED)
3121: break;
3122: pywrap++;
3123: buf = window_copy_stringify(gd, pywrap, 0, gd->sx, buf, &size);
3124: len += gd->sx;
3125: }
3126:
3127: if (window_copy_last_regex(gd, py, first, last, len, ppx, psx, buf,
1.260 nicm 3128: reg, eflags))
1.244 nicm 3129: {
3130: free(buf);
3131: return (1);
3132: }
3133:
3134: free(buf);
3135: *ppx = 0;
3136: *psx = 0;
3137: return (0);
3138: }
3139:
1.260 nicm 3140: static const char *
1.289 nicm 3141: window_copy_cellstring(const struct grid_line *gl, u_int px, size_t *size,
3142: int *allocated)
1.260 nicm 3143: {
1.289 nicm 3144: static struct utf8_data ud;
1.260 nicm 3145: struct grid_cell_entry *gce;
1.289 nicm 3146: char *copy;
1.260 nicm 3147:
3148: if (px >= gl->cellsize) {
3149: *size = 1;
1.289 nicm 3150: *allocated = 0;
1.260 nicm 3151: return (" ");
3152: }
3153:
3154: gce = &gl->celldata[px];
1.301 nicm 3155: if (gce->flags & GRID_FLAG_PADDING) {
3156: *size = 0;
3157: *allocated = 0;
3158: return (NULL);
3159: }
1.260 nicm 3160: if (~gce->flags & GRID_FLAG_EXTENDED) {
3161: *size = 1;
1.289 nicm 3162: *allocated = 0;
1.260 nicm 3163: return (&gce->data.data);
3164: }
3165:
1.291 nicm 3166: utf8_to_data(gl->extddata[gce->offset].data, &ud);
1.332 nicm 3167: if (ud.size == 0) {
3168: *size = 0;
3169: *allocated = 0;
3170: return (NULL);
3171: }
1.289 nicm 3172: *size = ud.size;
3173: *allocated = 1;
3174:
3175: copy = xmalloc(ud.size);
3176: memcpy(copy, ud.data, ud.size);
3177: return (copy);
1.260 nicm 3178: }
3179:
1.244 nicm 3180: /* Find last match in given range. */
3181: static int
3182: window_copy_last_regex(struct grid *gd, u_int py, u_int first, u_int last,
3183: u_int len, u_int *ppx, u_int *psx, const char *buf, const regex_t *preg,
3184: int eflags)
3185: {
3186: u_int foundx, foundy, oldx, px = 0, savepx, savesx = 0;
3187: regmatch_t regmatch;
3188:
3189: foundx = first;
3190: foundy = py;
3191: oldx = first;
3192: while (regexec(preg, buf + px, 1, ®match, eflags) == 0) {
1.287 nicm 3193: if (regmatch.rm_so == regmatch.rm_eo)
3194: break;
1.244 nicm 3195: window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
3196: buf + px + regmatch.rm_so);
3197: if (foundy > py || foundx >= last)
3198: break;
3199: len -= foundx - oldx;
3200: savepx = foundx;
3201: window_copy_cstrtocellpos(gd, len, &foundx, &foundy,
3202: buf + px + regmatch.rm_eo);
3203: if (foundy > py || foundx >= last) {
3204: *ppx = savepx;
3205: *psx = foundx;
3206: while (foundy > py) {
3207: *psx += gd->sx;
3208: foundy--;
3209: }
3210: *psx -= *ppx;
3211: return (1);
3212: } else {
3213: savesx = foundx - savepx;
3214: len -= savesx;
3215: oldx = foundx;
3216: }
3217: px += regmatch.rm_eo;
3218: }
3219:
3220: if (savesx > 0) {
3221: *ppx = savepx;
3222: *psx = savesx;
3223: return (1);
3224: } else {
3225: *ppx = 0;
3226: *psx = 0;
3227: return (0);
3228: }
3229: }
3230:
3231: /* Stringify line and append to input buffer. Caller frees. */
3232: static char *
3233: window_copy_stringify(struct grid *gd, u_int py, u_int first, u_int last,
3234: char *buf, u_int *size)
3235: {
1.260 nicm 3236: u_int ax, bx, newsize = *size;
3237: const struct grid_line *gl;
3238: const char *d;
3239: size_t bufsize = 1024, dlen;
1.289 nicm 3240: int allocated;
1.260 nicm 3241:
3242: while (bufsize < newsize)
3243: bufsize *= 2;
3244: buf = xrealloc(buf, bufsize);
1.244 nicm 3245:
1.260 nicm 3246: gl = grid_peek_line(gd, py);
1.244 nicm 3247: bx = *size - 1;
3248: for (ax = first; ax < last; ax++) {
1.289 nicm 3249: d = window_copy_cellstring(gl, ax, &dlen, &allocated);
1.260 nicm 3250: newsize += dlen;
3251: while (bufsize < newsize) {
3252: bufsize *= 2;
3253: buf = xrealloc(buf, bufsize);
3254: }
3255: if (dlen == 1)
3256: buf[bx++] = *d;
3257: else {
3258: memcpy(buf + bx, d, dlen);
3259: bx += dlen;
3260: }
1.289 nicm 3261: if (allocated)
3262: free((void *)d);
1.244 nicm 3263: }
1.260 nicm 3264: buf[newsize - 1] = '\0';
1.244 nicm 3265:
3266: *size = newsize;
3267: return (buf);
3268: }
3269:
3270: /* Map start of C string containing UTF-8 data to grid cell position. */
3271: static void
3272: window_copy_cstrtocellpos(struct grid *gd, u_int ncells, u_int *ppx, u_int *ppy,
3273: const char *str)
3274: {
1.260 nicm 3275: u_int cell, ccell, px, pywrap, pos, len;
3276: int match;
3277: const struct grid_line *gl;
3278: const char *d;
3279: size_t dlen;
3280: struct {
3281: const char *d;
3282: size_t dlen;
1.289 nicm 3283: int allocated;
1.260 nicm 3284: } *cells;
1.244 nicm 3285:
1.260 nicm 3286: /* Populate the array of cell data. */
1.244 nicm 3287: cells = xreallocarray(NULL, ncells, sizeof cells[0]);
3288: cell = 0;
3289: px = *ppx;
3290: pywrap = *ppy;
1.260 nicm 3291: gl = grid_peek_line(gd, pywrap);
1.244 nicm 3292: while (cell < ncells) {
1.260 nicm 3293: cells[cell].d = window_copy_cellstring(gl, px,
1.289 nicm 3294: &cells[cell].dlen, &cells[cell].allocated);
1.244 nicm 3295: cell++;
1.261 nicm 3296: px++;
3297: if (px == gd->sx) {
3298: px = 0;
1.244 nicm 3299: pywrap++;
1.260 nicm 3300: gl = grid_peek_line(gd, pywrap);
3301: }
1.244 nicm 3302: }
3303:
3304: /* Locate starting cell. */
3305: cell = 0;
1.260 nicm 3306: len = strlen(str);
1.244 nicm 3307: while (cell < ncells) {
3308: ccell = cell;
1.260 nicm 3309: pos = 0;
1.244 nicm 3310: match = 1;
3311: while (ccell < ncells) {
1.260 nicm 3312: if (str[pos] == '\0') {
1.244 nicm 3313: match = 0;
3314: break;
3315: }
1.260 nicm 3316: d = cells[ccell].d;
3317: dlen = cells[ccell].dlen;
3318: if (dlen == 1) {
3319: if (str[pos] != *d) {
3320: match = 0;
3321: break;
3322: }
3323: pos++;
3324: } else {
3325: if (dlen > len - pos)
3326: dlen = len - pos;
3327: if (memcmp(str + pos, d, dlen) != 0) {
1.244 nicm 3328: match = 0;
3329: break;
3330: }
1.260 nicm 3331: pos += dlen;
1.244 nicm 3332: }
3333: ccell++;
3334: }
3335: if (match)
3336: break;
3337: cell++;
3338: }
3339:
3340: /* If not found this will be one past the end. */
3341: px = *ppx + cell;
3342: pywrap = *ppy;
3343: while (px >= gd->sx) {
3344: px -= gd->sx;
3345: pywrap++;
3346: }
3347:
3348: *ppx = px;
3349: *ppy = pywrap;
3350:
3351: /* Free cell data. */
1.289 nicm 3352: for (cell = 0; cell < ncells; cell++) {
3353: if (cells[cell].allocated)
3354: free((void *)cells[cell].d);
3355: }
1.244 nicm 3356: free(cells);
3357: }
3358:
1.157 nicm 3359: static void
1.230 nicm 3360: window_copy_move_left(struct screen *s, u_int *fx, u_int *fy, int wrapflag)
1.21 nicm 3361: {
1.150 nicm 3362: if (*fx == 0) { /* left */
1.230 nicm 3363: if (*fy == 0) { /* top */
3364: if (wrapflag) {
3365: *fx = screen_size_x(s) - 1;
1.242 nicm 3366: *fy = screen_hsize(s) + screen_size_y(s) - 1;
1.230 nicm 3367: }
1.150 nicm 3368: return;
1.230 nicm 3369: }
1.150 nicm 3370: *fx = screen_size_x(s) - 1;
3371: *fy = *fy - 1;
3372: } else
3373: *fx = *fx - 1;
3374: }
1.21 nicm 3375:
1.321 nicm 3376: static void
3377: window_copy_move_right(struct screen *s, u_int *fx, u_int *fy, int wrapflag)
3378: {
3379: if (*fx == screen_size_x(s) - 1) { /* right */
3380: if (*fy == screen_hsize(s) + screen_size_y(s) - 1) { /* bottom */
3381: if (wrapflag) {
3382: *fx = 0;
3383: *fy = 0;
3384: }
3385: return;
3386: }
3387: *fx = 0;
3388: *fy = *fy + 1;
3389: } else
3390: *fx = *fx + 1;
3391: }
3392:
1.157 nicm 3393: static int
1.150 nicm 3394: window_copy_is_lowercase(const char *ptr)
3395: {
3396: while (*ptr != '\0') {
3397: if (*ptr != tolower((u_char)*ptr))
3398: return (0);
3399: ++ptr;
1.97 nicm 3400: }
1.150 nicm 3401: return (1);
3402: }
1.97 nicm 3403:
1.150 nicm 3404: /*
1.317 nicm 3405: * Handle backward wrapped regex searches with overlapping matches. In this case
3406: * find the longest overlapping match from previous wrapped lines.
3407: */
3408: static void
3409: window_copy_search_back_overlap(struct grid *gd, regex_t *preg, u_int *ppx,
3410: u_int *psx, u_int *ppy, u_int endline)
3411: {
3412: u_int endx, endy, oldendx, oldendy, px, py, sx;
3413: int found = 1;
3414:
3415: oldendx = *ppx + *psx;
3416: oldendy = *ppy - 1;
3417: while (oldendx > gd->sx - 1) {
3418: oldendx -= gd->sx;
3419: oldendy++;
3420: }
3421: endx = oldendx;
3422: endy = oldendy;
3423: px = *ppx;
3424: py = *ppy;
3425: while (found && px == 0 && py - 1 > endline &&
3426: grid_get_line(gd, py - 2)->flags & GRID_LINE_WRAPPED &&
3427: endx == oldendx && endy == oldendy) {
3428: py--;
3429: found = window_copy_search_rl_regex(gd, &px, &sx, py - 1, 0,
3430: gd->sx, preg);
3431: if (found) {
3432: endx = px + sx;
3433: endy = py - 1;
3434: while (endx > gd->sx - 1) {
3435: endx -= gd->sx;
3436: endy++;
3437: }
3438: if (endx == oldendx && endy == oldendy) {
3439: *ppx = px;
3440: *ppy = py;
3441: }
3442: }
3443: }
3444: }
3445:
3446: /*
1.150 nicm 3447: * Search for text stored in sgd starting from position fx,fy up to endline. If
3448: * found, jump to it. If cis then ignore case. The direction is 0 for searching
3449: * up, down otherwise. If wrap then go to begin/end of grid and try again if
3450: * not found.
3451: */
1.163 nicm 3452: static int
1.208 nicm 3453: window_copy_search_jump(struct window_mode_entry *wme, struct grid *gd,
1.150 nicm 3454: struct grid *sgd, u_int fx, u_int fy, u_int endline, int cis, int wrap,
1.321 nicm 3455: int direction, int regex)
1.150 nicm 3456: {
1.260 nicm 3457: u_int i, px, sx, ssize = 1;
3458: int found = 0, cflags = REG_EXTENDED;
3459: char *sbuf;
3460: regex_t reg;
3461:
3462: if (regex) {
3463: sbuf = xmalloc(ssize);
3464: sbuf[0] = '\0';
3465: sbuf = window_copy_stringify(sgd, 0, 0, sgd->sx, sbuf, &ssize);
3466: if (cis)
3467: cflags |= REG_ICASE;
3468: if (regcomp(®, sbuf, cflags) != 0) {
3469: free(sbuf);
3470: return (0);
3471: }
1.304 nicm 3472: free(sbuf);
1.260 nicm 3473: }
1.150 nicm 3474:
3475: if (direction) {
3476: for (i = fy; i <= endline; i++) {
1.260 nicm 3477: if (regex) {
3478: found = window_copy_search_lr_regex(gd,
3479: &px, &sx, i, fx, gd->sx, ®);
3480: } else {
1.244 nicm 3481: found = window_copy_search_lr(gd, sgd,
3482: &px, i, fx, gd->sx, cis);
1.260 nicm 3483: }
1.150 nicm 3484: if (found)
3485: break;
3486: fx = 0;
3487: }
3488: } else {
3489: for (i = fy + 1; endline < i; i--) {
1.260 nicm 3490: if (regex) {
3491: found = window_copy_search_rl_regex(gd,
3492: &px, &sx, i - 1, 0, fx + 1, ®);
1.317 nicm 3493: if (found) {
3494: window_copy_search_back_overlap(gd,
3495: ®, &px, &sx, &i, endline);
3496: }
1.260 nicm 3497: } else {
1.244 nicm 3498: found = window_copy_search_rl(gd, sgd,
3499: &px, i - 1, 0, fx + 1, cis);
1.260 nicm 3500: }
1.150 nicm 3501: if (found) {
3502: i--;
3503: break;
3504: }
1.242 nicm 3505: fx = gd->sx - 1;
1.21 nicm 3506: }
3507: }
1.304 nicm 3508: if (regex)
1.260 nicm 3509: regfree(®);
1.150 nicm 3510:
1.163 nicm 3511: if (found) {
1.296 nicm 3512: window_copy_scroll_to(wme, px, i, 1);
1.163 nicm 3513: return (1);
3514: }
3515: if (wrap) {
1.208 nicm 3516: return (window_copy_search_jump(wme, gd, sgd,
1.163 nicm 3517: direction ? 0 : gd->sx - 1,
1.150 nicm 3518: direction ? 0 : gd->hsize + gd->sy - 1, fy, cis, 0,
1.321 nicm 3519: direction, regex));
1.21 nicm 3520: }
1.163 nicm 3521: return (0);
1.21 nicm 3522: }
3523:
1.321 nicm 3524: static void
3525: window_copy_move_after_search_mark(struct window_copy_mode_data *data,
3526: u_int *fx, u_int *fy, int wrapflag)
3527: {
3528: struct screen *s = data->backing;
3529: u_int at, start;
3530:
3531: if (window_copy_search_mark_at(data, *fx, *fy, &start) == 0 &&
3532: data->searchmark[start] != 0) {
3533: while (window_copy_search_mark_at(data, *fx, *fy, &at) == 0) {
3534: if (data->searchmark[at] != data->searchmark[start])
3535: break;
3536: /* Stop if not wrapping and at the end of the grid. */
3537: if (!wrapflag &&
3538: *fx == screen_size_x(s) - 1 &&
3539: *fy == screen_hsize(s) + screen_size_y(s) - 1)
3540: break;
3541:
3542: window_copy_move_right(s, fx, fy, wrapflag);
3543: }
3544: }
3545: }
3546:
1.232 nicm 3547: /*
3548: * Search in for text searchstr. If direction is 0 then search up, otherwise
3549: * down.
3550: */
1.163 nicm 3551: static int
1.321 nicm 3552: window_copy_search(struct window_mode_entry *wme, int direction, int regex)
1.21 nicm 3553: {
1.208 nicm 3554: struct window_pane *wp = wme->wp;
3555: struct window_copy_mode_data *data = wme->data;
1.54 nicm 3556: struct screen *s = data->backing, ss;
1.21 nicm 3557: struct screen_write_ctx ctx;
1.150 nicm 3558: struct grid *gd = s->grid;
1.262 nicm 3559: const char *str = data->searchstr;
1.321 nicm 3560: u_int at, endline, fx, fy, start;
3561: int cis, found, keys, visible_only;
3562: int wrapflag;
1.21 nicm 3563:
1.262 nicm 3564: if (regex && str[strcspn(str, "^$*+()?[].\\")] == '\0')
3565: regex = 0;
3566:
1.321 nicm 3567: data->searchdirection = direction;
3568:
1.263 nicm 3569: if (data->timeout)
3570: return (0);
3571:
1.308 nicm 3572: if (data->searchall || wp->searchstr == NULL ||
3573: wp->searchregex != regex) {
1.296 nicm 3574: visible_only = 0;
1.308 nicm 3575: data->searchall = 0;
3576: } else
1.296 nicm 3577: visible_only = (strcmp(wp->searchstr, str) == 0);
1.174 nicm 3578: free(wp->searchstr);
1.262 nicm 3579: wp->searchstr = xstrdup(str);
1.259 nicm 3580: wp->searchregex = regex;
1.174 nicm 3581:
1.150 nicm 3582: fx = data->cx;
3583: fy = screen_hsize(data->backing) - data->oy + data->cy;
1.21 nicm 3584:
1.262 nicm 3585: screen_init(&ss, screen_write_strlen("%s", str), 1, 0);
1.283 nicm 3586: screen_write_start(&ctx, &ss);
1.262 nicm 3587: screen_write_nputs(&ctx, -1, &grid_default_cell, "%s", str);
1.21 nicm 3588: screen_write_stop(&ctx);
3589:
1.150 nicm 3590: wrapflag = options_get_number(wp->window->options, "wrap-search");
1.262 nicm 3591: cis = window_copy_is_lowercase(str);
1.21 nicm 3592:
1.321 nicm 3593: keys = options_get_number(wp->window->options, "mode-keys");
3594:
3595: if (direction) {
3596: /*
3597: * Behave according to mode-keys. If it is emacs, search forward
3598: * leaves the cursor after the match. If it is vi, the cursor
3599: * remains at the beginning of the match, regardless of
3600: * direction, which means that we need to start the next search
3601: * after the term the cursor is currently on when searching
3602: * forward.
3603: */
3604: if (keys == MODEKEY_VI) {
3605: if (data->searchmark != NULL)
3606: window_copy_move_after_search_mark(data, &fx,
3607: &fy, wrapflag);
3608: else {
3609: /*
3610: * When there are no search marks, start the
3611: * search after the current cursor position.
3612: */
3613: window_copy_move_right(s, &fx, &fy, wrapflag);
3614: }
3615: }
1.150 nicm 3616: endline = gd->hsize + gd->sy - 1;
1.321 nicm 3617: }
1.302 nicm 3618: else {
1.321 nicm 3619: window_copy_move_left(s, &fx, &fy, wrapflag);
1.150 nicm 3620: endline = 0;
1.230 nicm 3621: }
1.244 nicm 3622:
1.208 nicm 3623: found = window_copy_search_jump(wme, gd, ss.grid, fx, fy, endline, cis,
1.321 nicm 3624: wrapflag, direction, regex);
1.302 nicm 3625: if (found) {
1.296 nicm 3626: window_copy_search_marks(wme, &ss, regex, visible_only);
1.321 nicm 3627: fx = data->cx;
3628: fy = screen_hsize(data->backing) - data->oy + data->cy;
3629:
3630: /*
3631: * When searching forward, if the cursor is not at the beginning
3632: * of the mark, search again.
3633: */
3634: if (direction &&
3635: window_copy_search_mark_at(data, fx, fy, &at) == 0 &&
3636: at > 0 &&
3637: data->searchmark[at] == data->searchmark[at - 1]) {
3638: window_copy_move_after_search_mark(data, &fx, &fy,
3639: wrapflag);
3640: window_copy_search_jump(wme, gd, ss.grid, fx,
3641: fy, endline, cis, wrapflag, direction,
3642: regex);
3643: fx = data->cx;
3644: fy = screen_hsize(data->backing) - data->oy + data->cy;
3645: }
3646:
3647: if (direction) {
3648: /*
3649: * When in Emacs mode, position the cursor just after
3650: * the mark.
3651: */
3652: if (keys == MODEKEY_EMACS) {
3653: window_copy_move_after_search_mark(data, &fx,
3654: &fy, wrapflag);
3655: data->cx = fx;
3656: data->cy = fy - screen_hsize(data->backing) +
3657: data-> oy;
3658: }
3659: }
3660: else {
3661: /*
3662: * When searching backward, position the cursor at the
3663: * beginning of the mark.
3664: */
3665: if (window_copy_search_mark_at(data, fx, fy,
3666: &start) == 0) {
3667: while (window_copy_search_mark_at(data, fx, fy,
3668: &at) == 0 &&
3669: data->searchmark[at] ==
3670: data->searchmark[start]) {
3671: data->cx = fx;
3672: data->cy = fy -
3673: screen_hsize(data->backing) +
3674: data-> oy;
3675: if (at == 0)
3676: break;
3677:
3678: window_copy_move_left(s, &fx, &fy, 0);
3679: }
1.317 nicm 3680: }
1.302 nicm 3681: }
3682: }
1.296 nicm 3683: window_copy_redraw_screen(wme);
1.21 nicm 3684:
1.150 nicm 3685: screen_free(&ss);
1.163 nicm 3686: return (found);
1.150 nicm 3687: }
1.97 nicm 3688:
1.296 nicm 3689: static void
3690: window_copy_visible_lines(struct window_copy_mode_data *data, u_int *start,
3691: u_int *end)
3692: {
3693: struct grid *gd = data->backing->grid;
3694: const struct grid_line *gl;
3695:
3696: for (*start = gd->hsize - data->oy; *start > 0; (*start)--) {
3697: gl = grid_peek_line(gd, (*start) - 1);
3698: if (~gl->flags & GRID_LINE_WRAPPED)
3699: break;
3700: }
3701: *end = gd->hsize - data->oy + gd->sy;
3702: }
3703:
1.162 nicm 3704: static int
1.302 nicm 3705: window_copy_search_mark_at(struct window_copy_mode_data *data, u_int px,
3706: u_int py, u_int *at)
1.297 nicm 3707: {
3708: struct screen *s = data->backing;
3709: struct grid *gd = s->grid;
3710:
3711: if (py < gd->hsize - data->oy)
3712: return (-1);
3713: if (py > gd->hsize - data->oy + gd->sy - 1)
3714: return (-1);
3715: *at = ((py - (gd->hsize - data->oy)) * gd->sx) + px;
3716: return (0);
3717: }
3718:
3719: static int
1.244 nicm 3720: window_copy_search_marks(struct window_mode_entry *wme, struct screen *ssp,
1.296 nicm 3721: int regex, int visible_only)
1.162 nicm 3722: {
1.208 nicm 3723: struct window_copy_mode_data *data = wme->data;
1.162 nicm 3724: struct screen *s = data->backing, ss;
3725: struct screen_write_ctx ctx;
3726: struct grid *gd = s->grid;
1.309 nicm 3727: int found, cis, stopped = 0;
1.260 nicm 3728: int cflags = REG_EXTENDED;
1.282 nicm 3729: u_int px, py, i, b, nfound = 0, width;
1.284 nicm 3730: u_int ssize = 1, start, end;
1.260 nicm 3731: char *sbuf;
3732: regex_t reg;
1.284 nicm 3733: uint64_t stop = 0, tstart, t;
1.162 nicm 3734:
3735: if (ssp == NULL) {
3736: width = screen_write_strlen("%s", data->searchstr);
3737: screen_init(&ss, width, 1, 0);
1.283 nicm 3738: screen_write_start(&ctx, &ss);
1.162 nicm 3739: screen_write_nputs(&ctx, -1, &grid_default_cell, "%s",
3740: data->searchstr);
3741: screen_write_stop(&ctx);
3742: ssp = &ss;
3743: } else
3744: width = screen_size_x(ssp);
3745:
3746: cis = window_copy_is_lowercase(data->searchstr);
3747:
1.260 nicm 3748: if (regex) {
3749: sbuf = xmalloc(ssize);
3750: sbuf[0] = '\0';
3751: sbuf = window_copy_stringify(ssp->grid, 0, 0, ssp->grid->sx,
3752: sbuf, &ssize);
3753: if (cis)
3754: cflags |= REG_ICASE;
3755: if (regcomp(®, sbuf, cflags) != 0) {
3756: free(sbuf);
3757: return (0);
3758: }
1.304 nicm 3759: free(sbuf);
1.260 nicm 3760: }
1.292 nicm 3761: tstart = get_timer();
1.284 nicm 3762:
1.296 nicm 3763: if (visible_only)
3764: window_copy_visible_lines(data, &start, &end);
3765: else {
3766: start = 0;
3767: end = gd->hsize + gd->sy;
3768: stop = get_timer() + WINDOW_COPY_SEARCH_ALL_TIMEOUT;
3769: }
1.284 nicm 3770:
3771: again:
3772: free(data->searchmark);
1.297 nicm 3773: data->searchmark = xcalloc(gd->sx, gd->sy);
1.284 nicm 3774: data->searchgen = 1;
3775:
3776: for (py = start; py < end; py++) {
1.162 nicm 3777: px = 0;
3778: for (;;) {
1.244 nicm 3779: if (regex) {
3780: found = window_copy_search_lr_regex(gd,
1.260 nicm 3781: &px, &width, py, px, gd->sx, ®);
1.244 nicm 3782: if (!found)
3783: break;
1.260 nicm 3784: } else {
1.244 nicm 3785: found = window_copy_search_lr(gd, ssp->grid,
1.260 nicm 3786: &px, py, px, gd->sx, cis);
1.244 nicm 3787: if (!found)
3788: break;
3789: }
1.162 nicm 3790: nfound++;
3791:
1.297 nicm 3792: if (window_copy_search_mark_at(data, px, py, &b) == 0) {
3793: if (b + width > gd->sx * gd->sy)
3794: width = (gd->sx * gd->sy) - b;
1.317 nicm 3795: for (i = b; i < b + width; i++) {
3796: if (data->searchmark[i] != 0)
3797: continue;
1.297 nicm 3798: data->searchmark[i] = data->searchgen;
1.317 nicm 3799: }
1.297 nicm 3800: if (data->searchgen == UCHAR_MAX)
3801: data->searchgen = 1;
3802: else
3803: data->searchgen++;
3804: }
1.302 nicm 3805: px += width;
1.162 nicm 3806: }
1.263 nicm 3807:
1.292 nicm 3808: t = get_timer();
1.263 nicm 3809: if (t - tstart > WINDOW_COPY_SEARCH_TIMEOUT) {
3810: data->timeout = 1;
3811: break;
3812: }
1.284 nicm 3813: if (stop != 0 && t > stop) {
3814: stopped = 1;
3815: break;
3816: }
1.162 nicm 3817: }
1.263 nicm 3818: if (data->timeout) {
3819: window_copy_clear_marks(wme);
1.284 nicm 3820: goto out;
3821: }
3822:
3823: if (stopped && stop != 0) {
3824: /* Try again but just the visible context. */
1.296 nicm 3825: window_copy_visible_lines(data, &start, &end);
1.284 nicm 3826: stop = 0;
3827: goto again;
1.263 nicm 3828: }
1.162 nicm 3829:
1.296 nicm 3830: if (!visible_only) {
3831: if (stopped) {
3832: if (nfound > 1000)
3833: data->searchcount = 1000;
3834: else if (nfound > 100)
3835: data->searchcount = 100;
3836: else if (nfound > 10)
3837: data->searchcount = 10;
3838: else
3839: data->searchcount = -1;
3840: data->searchmore = 1;
3841: } else {
3842: data->searchcount = nfound;
3843: data->searchmore = 0;
3844: }
1.284 nicm 3845: }
1.170 nicm 3846:
1.284 nicm 3847: out:
1.162 nicm 3848: if (ssp == &ss)
3849: screen_free(&ss);
1.304 nicm 3850: if (regex)
1.284 nicm 3851: regfree(®);
1.263 nicm 3852: return (1);
1.162 nicm 3853: }
3854:
1.157 nicm 3855: static void
1.208 nicm 3856: window_copy_clear_marks(struct window_mode_entry *wme)
1.163 nicm 3857: {
1.208 nicm 3858: struct window_copy_mode_data *data = wme->data;
1.163 nicm 3859:
3860: free(data->searchmark);
3861: data->searchmark = NULL;
3862: }
3863:
3864: static int
1.321 nicm 3865: window_copy_search_up(struct window_mode_entry *wme, int regex)
1.150 nicm 3866: {
1.321 nicm 3867: return (window_copy_search(wme, 0, regex));
1.150 nicm 3868: }
1.21 nicm 3869:
1.163 nicm 3870: static int
1.321 nicm 3871: window_copy_search_down(struct window_mode_entry *wme, int regex)
1.150 nicm 3872: {
1.321 nicm 3873: return (window_copy_search(wme, 1, regex));
1.21 nicm 3874: }
3875:
1.157 nicm 3876: static void
1.208 nicm 3877: window_copy_goto_line(struct window_mode_entry *wme, const char *linestr)
1.21 nicm 3878: {
1.208 nicm 3879: struct window_copy_mode_data *data = wme->data;
1.21 nicm 3880: const char *errstr;
1.199 nicm 3881: int lineno;
1.21 nicm 3882:
1.199 nicm 3883: lineno = strtonum(linestr, -1, INT_MAX, &errstr);
1.21 nicm 3884: if (errstr != NULL)
3885: return;
1.199 nicm 3886: if (lineno < 0 || (u_int)lineno > screen_hsize(data->backing))
3887: lineno = screen_hsize(data->backing);
1.35 nicm 3888:
1.21 nicm 3889: data->oy = lineno;
1.252 nicm 3890: window_copy_update_selection(wme, 1, 0);
1.208 nicm 3891: window_copy_redraw_screen(wme);
1.21 nicm 3892: }
3893:
1.157 nicm 3894: static void
1.282 nicm 3895: window_copy_match_start_end(struct window_copy_mode_data *data, u_int at,
3896: u_int *start, u_int *end)
3897: {
3898: struct grid *gd = data->backing->grid;
1.297 nicm 3899: u_int last = (gd->sy * gd->sx) - 1;
1.282 nicm 3900: u_char mark = data->searchmark[at];
3901:
3902: *start = *end = at;
3903: while (*start != 0 && data->searchmark[*start] == mark)
3904: (*start)--;
3905: if (data->searchmark[*start] != mark)
3906: (*start)++;
3907: while (*end != last && data->searchmark[*end] == mark)
3908: (*end)++;
3909: if (data->searchmark[*end] != mark)
3910: (*end)--;
3911: }
3912:
3913: static char *
3914: window_copy_match_at_cursor(struct window_copy_mode_data *data)
3915: {
3916: struct grid *gd = data->backing->grid;
3917: struct grid_cell gc;
3918: u_int at, start, end, cy, px, py;
3919: u_int sx = screen_size_x(data->backing);
3920: char *buf = NULL;
3921: size_t len = 0;
3922:
3923: if (data->searchmark == NULL)
3924: return (NULL);
3925:
3926: cy = screen_hsize(data->backing) - data->oy + data->cy;
1.297 nicm 3927: if (window_copy_search_mark_at(data, data->cx, cy, &at) != 0)
3928: return (NULL);
1.320 nicm 3929: if (data->searchmark[at] == 0) {
3930: /* Allow one position after the match. */
3931: if (at == 0 || data->searchmark[--at] == 0)
3932: return (NULL);
3933: }
1.282 nicm 3934: window_copy_match_start_end(data, at, &start, &end);
3935:
3936: /*
3937: * Cells will not be set in the marked array unless they are valid text
3938: * and wrapping will be taken care of, so we can just copy.
3939: */
3940: for (at = start; at <= end; at++) {
3941: py = at / sx;
1.290 nicm 3942: px = at - (py * sx);
1.282 nicm 3943:
1.297 nicm 3944: grid_get_cell(gd, px, gd->hsize + py - data->oy, &gc);
1.282 nicm 3945: buf = xrealloc(buf, len + gc.data.size + 1);
3946: memcpy(buf + len, gc.data.data, gc.data.size);
3947: len += gc.data.size;
3948: }
3949: if (len != 0)
3950: buf[len] = '\0';
3951: return (buf);
3952: }
3953:
3954: static void
3955: window_copy_update_style(struct window_mode_entry *wme, u_int fx, u_int fy,
3956: struct grid_cell *gc, const struct grid_cell *mgc,
1.288 nicm 3957: const struct grid_cell *cgc, const struct grid_cell *mkgc)
1.282 nicm 3958: {
1.321 nicm 3959: struct window_pane *wp = wme->wp;
1.282 nicm 3960: struct window_copy_mode_data *data = wme->data;
1.284 nicm 3961: u_int mark, start, end, cy, cursor, current;
1.302 nicm 3962: int inv = 0, found = 0;
1.321 nicm 3963: int keys;
1.288 nicm 3964:
3965: if (data->showmark && fy == data->my) {
3966: gc->attr = mkgc->attr;
3967: if (fx == data->mx)
3968: inv = 1;
3969: if (inv) {
3970: gc->fg = mkgc->bg;
3971: gc->bg = mkgc->fg;
3972: }
3973: else {
3974: gc->fg = mkgc->fg;
3975: gc->bg = mkgc->bg;
3976: }
3977: }
1.282 nicm 3978:
3979: if (data->searchmark == NULL)
3980: return;
1.284 nicm 3981:
1.297 nicm 3982: if (window_copy_search_mark_at(data, fx, fy, ¤t) != 0)
3983: return;
1.284 nicm 3984: mark = data->searchmark[current];
1.282 nicm 3985: if (mark == 0)
3986: return;
3987:
3988: cy = screen_hsize(data->backing) - data->oy + data->cy;
1.302 nicm 3989: if (window_copy_search_mark_at(data, data->cx, cy, &cursor) == 0) {
1.321 nicm 3990: keys = options_get_number(wp->window->options, "mode-keys");
3991: if (cursor != 0 &&
3992: keys == MODEKEY_EMACS &&
3993: data->searchdirection) {
3994: if (data->searchmark[cursor - 1] == mark) {
3995: cursor--;
3996: found = 1;
3997: }
3998: } else if (data->searchmark[cursor] == mark)
1.302 nicm 3999: found = 1;
4000: if (found) {
4001: window_copy_match_start_end(data, cursor, &start, &end);
4002: if (current >= start && current <= end) {
4003: gc->attr = cgc->attr;
4004: if (inv) {
4005: gc->fg = cgc->bg;
4006: gc->bg = cgc->fg;
4007: }
4008: else {
4009: gc->fg = cgc->fg;
4010: gc->bg = cgc->bg;
4011: }
4012: return;
1.288 nicm 4013: }
1.282 nicm 4014: }
4015: }
4016:
4017: gc->attr = mgc->attr;
1.288 nicm 4018: if (inv) {
4019: gc->fg = mgc->bg;
4020: gc->bg = mgc->fg;
4021: }
4022: else {
4023: gc->fg = mgc->fg;
4024: gc->bg = mgc->bg;
4025: }
1.282 nicm 4026: }
4027:
4028: static void
4029: window_copy_write_one(struct window_mode_entry *wme,
4030: struct screen_write_ctx *ctx, u_int py, u_int fy, u_int nx,
1.288 nicm 4031: const struct grid_cell *mgc, const struct grid_cell *cgc,
4032: const struct grid_cell *mkgc)
1.282 nicm 4033: {
4034: struct window_copy_mode_data *data = wme->data;
4035: struct grid *gd = data->backing->grid;
4036: struct grid_cell gc;
4037: u_int fx;
4038:
4039: screen_write_cursormove(ctx, 0, py, 0);
4040: for (fx = 0; fx < nx; fx++) {
4041: grid_get_cell(gd, fx, fy, &gc);
4042: if (fx + gc.data.width <= nx) {
1.288 nicm 4043: window_copy_update_style(wme, fx, fy, &gc, mgc, cgc,
4044: mkgc);
1.282 nicm 4045: screen_write_cell(ctx, &gc);
4046: }
4047: }
4048: }
4049:
4050: static void
1.208 nicm 4051: window_copy_write_line(struct window_mode_entry *wme,
4052: struct screen_write_ctx *ctx, u_int py)
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;
1.136 nicm 4057: struct options *oo = wp->window->options;
1.288 nicm 4058: struct grid_cell gc, mgc, cgc, mkgc;
1.100 nicm 4059: char hdr[512];
1.162 nicm 4060: size_t size = 0;
1.270 nicm 4061: u_int hsize = screen_hsize(data->backing);
1.21 nicm 4062:
1.281 nicm 4063: style_apply(&gc, oo, "mode-style", NULL);
1.164 nicm 4064: gc.flags |= GRID_FLAG_NOPALETTE;
1.282 nicm 4065: style_apply(&mgc, oo, "copy-mode-match-style", NULL);
4066: mgc.flags |= GRID_FLAG_NOPALETTE;
4067: style_apply(&cgc, oo, "copy-mode-current-match-style", NULL);
4068: cgc.flags |= GRID_FLAG_NOPALETTE;
1.288 nicm 4069: style_apply(&mkgc, oo, "copy-mode-mark-style", NULL);
4070: mkgc.flags |= GRID_FLAG_NOPALETTE;
1.1 nicm 4071:
1.249 nicm 4072: if (py == 0 && s->rupper < s->rlower && !data->hide_position) {
1.170 nicm 4073: if (data->searchmark == NULL) {
1.263 nicm 4074: if (data->timeout) {
4075: size = xsnprintf(hdr, sizeof hdr,
1.270 nicm 4076: "(timed out) [%u/%u]", data->oy, hsize);
1.263 nicm 4077: } else {
4078: size = xsnprintf(hdr, sizeof hdr,
1.270 nicm 4079: "[%u/%u]", data->oy, hsize);
1.263 nicm 4080: }
1.170 nicm 4081: } else {
1.284 nicm 4082: if (data->searchcount == -1) {
4083: size = xsnprintf(hdr, sizeof hdr,
4084: "[%u/%u]", data->oy, hsize);
1.308 nicm 4085: } else {
1.170 nicm 4086: size = xsnprintf(hdr, sizeof hdr,
1.284 nicm 4087: "(%d%s results) [%u/%u]", data->searchcount,
1.302 nicm 4088: data->searchmore ? "+" : "", data->oy,
4089: hsize);
1.170 nicm 4090: }
4091: }
1.62 nicm 4092: if (size > screen_size_x(s))
4093: size = screen_size_x(s);
1.212 nicm 4094: screen_write_cursormove(ctx, screen_size_x(s) - size, 0, 0);
1.1 nicm 4095: screen_write_puts(ctx, &gc, "%s", hdr);
4096: } else
4097: size = 0;
4098:
1.105 nicm 4099: if (size < screen_size_x(s)) {
1.282 nicm 4100: window_copy_write_one(wme, ctx, py, hsize - data->oy + py,
1.288 nicm 4101: screen_size_x(s) - size, &mgc, &cgc, &mkgc);
1.105 nicm 4102: }
1.18 nicm 4103:
4104: if (py == data->cy && data->cx == screen_size_x(s)) {
1.212 nicm 4105: screen_write_cursormove(ctx, screen_size_x(s) - 1, py, 0);
1.282 nicm 4106: screen_write_putc(ctx, &grid_default_cell, '$');
1.18 nicm 4107: }
1.1 nicm 4108: }
4109:
1.157 nicm 4110: static void
1.208 nicm 4111: window_copy_write_lines(struct window_mode_entry *wme,
4112: struct screen_write_ctx *ctx, u_int py, u_int ny)
1.1 nicm 4113: {
4114: u_int yy;
4115:
4116: for (yy = py; yy < py + ny; yy++)
1.208 nicm 4117: window_copy_write_line(wme, ctx, py);
1.121 nicm 4118: }
4119:
1.157 nicm 4120: static void
1.208 nicm 4121: window_copy_redraw_selection(struct window_mode_entry *wme, u_int old_y)
1.121 nicm 4122: {
1.208 nicm 4123: struct window_copy_mode_data *data = wme->data;
1.247 nicm 4124: struct grid *gd = data->backing->grid;
1.121 nicm 4125: u_int new_y, start, end;
4126:
4127: new_y = data->cy;
4128: if (old_y <= new_y) {
4129: start = old_y;
4130: end = new_y;
4131: } else {
4132: start = new_y;
4133: end = old_y;
4134: }
1.247 nicm 4135:
4136: /*
4137: * In word selection mode the first word on the line below the cursor
4138: * might be selected, so add this line to the redraw area.
4139: */
4140: if (data->selflag == SEL_WORD) {
4141: /* Last grid line in data coordinates. */
4142: if (end < gd->sy + data->oy - 1)
4143: end++;
4144: }
1.208 nicm 4145: window_copy_redraw_lines(wme, start, end - start + 1);
1.1 nicm 4146: }
4147:
1.157 nicm 4148: static void
1.208 nicm 4149: window_copy_redraw_lines(struct window_mode_entry *wme, u_int py, u_int ny)
1.1 nicm 4150: {
1.208 nicm 4151: struct window_pane *wp = wme->wp;
4152: struct window_copy_mode_data *data = wme->data;
1.1 nicm 4153: struct screen_write_ctx ctx;
4154: u_int i;
4155:
1.283 nicm 4156: screen_write_start_pane(&ctx, wp, NULL);
1.1 nicm 4157: for (i = py; i < py + ny; i++)
1.208 nicm 4158: window_copy_write_line(wme, &ctx, i);
1.212 nicm 4159: screen_write_cursormove(&ctx, data->cx, data->cy, 0);
1.1 nicm 4160: screen_write_stop(&ctx);
4161: }
4162:
1.157 nicm 4163: static void
1.208 nicm 4164: window_copy_redraw_screen(struct window_mode_entry *wme)
1.1 nicm 4165: {
1.208 nicm 4166: struct window_copy_mode_data *data = wme->data;
1.1 nicm 4167:
1.208 nicm 4168: window_copy_redraw_lines(wme, 0, screen_size_y(&data->screen));
1.1 nicm 4169: }
4170:
1.157 nicm 4171: static void
1.252 nicm 4172: window_copy_synchronize_cursor_end(struct window_mode_entry *wme, int begin,
4173: int no_reset)
1.161 nicm 4174: {
1.208 nicm 4175: struct window_copy_mode_data *data = wme->data;
1.161 nicm 4176: u_int xx, yy;
4177:
1.311 nicm 4178: xx = data->cx;
1.161 nicm 4179: yy = screen_hsize(data->backing) + data->cy - data->oy;
1.247 nicm 4180: switch (data->selflag) {
4181: case SEL_WORD:
1.252 nicm 4182: if (no_reset)
1.247 nicm 4183: break;
1.252 nicm 4184: begin = 0;
1.247 nicm 4185: if (data->dy > yy || (data->dy == yy && data->dx > xx)) {
4186: /* Right to left selection. */
1.323 nicm 4187: window_copy_cursor_previous_word_pos(wme,
4188: data->separators, &xx, &yy);
1.247 nicm 4189: begin = 1;
4190:
4191: /* Reset the end. */
4192: data->endselx = data->endselrx;
4193: data->endsely = data->endselry;
4194: } else {
4195: /* Left to right selection. */
4196: if (xx >= window_copy_find_length(wme, yy) ||
1.323 nicm 4197: !window_copy_in_set(wme, xx + 1, yy, WHITESPACE)) {
1.247 nicm 4198: window_copy_cursor_next_word_end_pos(wme,
1.323 nicm 4199: data->separators, &xx, &yy);
4200: }
1.247 nicm 4201:
4202: /* Reset the start. */
4203: data->selx = data->selrx;
4204: data->sely = data->selry;
4205: }
4206: break;
4207: case SEL_LINE:
1.311 nicm 4208: if (no_reset)
1.252 nicm 4209: break;
1.251 nicm 4210: begin = 0;
1.247 nicm 4211: if (data->dy > yy) {
4212: /* Right to left selection. */
4213: xx = 0;
4214: begin = 1;
4215:
4216: /* Reset the end. */
4217: data->endselx = data->endselrx;
4218: data->endsely = data->endselry;
4219: } else {
4220: /* Left to right selection. */
1.319 nicm 4221: if (yy < data->endselry)
4222: yy = data->endselry;
1.247 nicm 4223: xx = window_copy_find_length(wme, yy);
4224:
4225: /* Reset the start. */
4226: data->selx = data->selrx;
4227: data->sely = data->selry;
4228: }
4229: break;
4230: case SEL_CHAR:
4231: break;
4232: }
4233: if (begin) {
4234: data->selx = xx;
4235: data->sely = yy;
4236: } else {
4237: data->endselx = xx;
4238: data->endsely = yy;
4239: }
4240: }
4241:
4242: static void
1.252 nicm 4243: window_copy_synchronize_cursor(struct window_mode_entry *wme, int no_reset)
1.247 nicm 4244: {
4245: struct window_copy_mode_data *data = wme->data;
1.161 nicm 4246:
4247: switch (data->cursordrag) {
4248: case CURSORDRAG_ENDSEL:
1.252 nicm 4249: window_copy_synchronize_cursor_end(wme, 0, no_reset);
1.161 nicm 4250: break;
4251: case CURSORDRAG_SEL:
1.252 nicm 4252: window_copy_synchronize_cursor_end(wme, 1, no_reset);
1.161 nicm 4253: break;
4254: case CURSORDRAG_NONE:
4255: break;
4256: }
4257: }
4258:
4259: static void
1.208 nicm 4260: window_copy_update_cursor(struct window_mode_entry *wme, u_int cx, u_int cy)
1.1 nicm 4261: {
1.208 nicm 4262: struct window_pane *wp = wme->wp;
4263: struct window_copy_mode_data *data = wme->data;
1.18 nicm 4264: struct screen *s = &data->screen;
1.1 nicm 4265: struct screen_write_ctx ctx;
1.18 nicm 4266: u_int old_cx, old_cy;
1.1 nicm 4267:
1.18 nicm 4268: old_cx = data->cx; old_cy = data->cy;
4269: data->cx = cx; data->cy = cy;
4270: if (old_cx == screen_size_x(s))
1.208 nicm 4271: window_copy_redraw_lines(wme, old_cy, 1);
1.18 nicm 4272: if (data->cx == screen_size_x(s))
1.208 nicm 4273: window_copy_redraw_lines(wme, data->cy, 1);
1.18 nicm 4274: else {
1.283 nicm 4275: screen_write_start_pane(&ctx, wp, NULL);
1.212 nicm 4276: screen_write_cursormove(&ctx, data->cx, data->cy, 0);
1.18 nicm 4277: screen_write_stop(&ctx);
4278: }
1.1 nicm 4279: }
4280:
1.157 nicm 4281: static void
1.208 nicm 4282: window_copy_start_selection(struct window_mode_entry *wme)
1.1 nicm 4283: {
1.208 nicm 4284: struct window_copy_mode_data *data = wme->data;
1.1 nicm 4285:
1.18 nicm 4286: data->selx = data->cx;
1.54 nicm 4287: data->sely = screen_hsize(data->backing) + data->cy - data->oy;
1.1 nicm 4288:
1.161 nicm 4289: data->endselx = data->selx;
4290: data->endsely = data->sely;
4291:
4292: data->cursordrag = CURSORDRAG_ENDSEL;
4293:
1.252 nicm 4294: window_copy_set_selection(wme, 1, 0);
1.1 nicm 4295: }
4296:
1.157 nicm 4297: static int
1.208 nicm 4298: window_copy_adjust_selection(struct window_mode_entry *wme, u_int *selx,
4299: u_int *sely)
1.161 nicm 4300: {
1.208 nicm 4301: struct window_copy_mode_data *data = wme->data;
1.161 nicm 4302: struct screen *s = &data->screen;
4303: u_int sx, sy, ty;
4304: int relpos;
4305:
4306: sx = *selx;
4307: sy = *sely;
4308:
4309: ty = screen_hsize(data->backing) - data->oy;
4310: if (sy < ty) {
4311: relpos = WINDOW_COPY_REL_POS_ABOVE;
4312: if (!data->rectflag)
4313: sx = 0;
4314: sy = 0;
4315: } else if (sy > ty + screen_size_y(s) - 1) {
4316: relpos = WINDOW_COPY_REL_POS_BELOW;
4317: if (!data->rectflag)
4318: sx = screen_size_x(s) - 1;
4319: sy = screen_size_y(s) - 1;
4320: } else {
4321: relpos = WINDOW_COPY_REL_POS_ON_SCREEN;
4322: sy -= ty;
4323: }
4324:
4325: *selx = sx;
1.176 nicm 4326: *sely = sy;
1.161 nicm 4327: return (relpos);
4328: }
4329:
4330: static int
1.252 nicm 4331: window_copy_update_selection(struct window_mode_entry *wme, int may_redraw,
4332: int no_reset)
1.1 nicm 4333: {
1.208 nicm 4334: struct window_copy_mode_data *data = wme->data;
1.1 nicm 4335: struct screen *s = &data->screen;
1.192 nicm 4336:
4337: if (s->sel == NULL && data->lineflag == LINE_SEL_NONE)
4338: return (0);
1.252 nicm 4339: return (window_copy_set_selection(wme, may_redraw, no_reset));
1.192 nicm 4340: }
4341:
4342: static int
1.252 nicm 4343: window_copy_set_selection(struct window_mode_entry *wme, int may_redraw,
4344: int no_reset)
1.192 nicm 4345: {
1.208 nicm 4346: struct window_pane *wp = wme->wp;
4347: struct window_copy_mode_data *data = wme->data;
1.192 nicm 4348: struct screen *s = &data->screen;
1.136 nicm 4349: struct options *oo = wp->window->options;
1.1 nicm 4350: struct grid_cell gc;
1.161 nicm 4351: u_int sx, sy, cy, endsx, endsy;
4352: int startrelpos, endrelpos;
1.1 nicm 4353:
1.252 nicm 4354: window_copy_synchronize_cursor(wme, no_reset);
1.1 nicm 4355:
4356: /* Adjust the selection. */
4357: sx = data->selx;
4358: sy = data->sely;
1.208 nicm 4359: startrelpos = window_copy_adjust_selection(wme, &sx, &sy);
1.161 nicm 4360:
4361: /* Adjust the end of selection. */
4362: endsx = data->endselx;
4363: endsy = data->endsely;
1.208 nicm 4364: endrelpos = window_copy_adjust_selection(wme, &endsx, &endsy);
1.161 nicm 4365:
4366: /* Selection is outside of the current screen */
4367: if (startrelpos == endrelpos &&
4368: startrelpos != WINDOW_COPY_REL_POS_ON_SCREEN) {
4369: screen_hide_selection(s);
4370: return (0);
4371: }
1.1 nicm 4372:
1.161 nicm 4373: /* Set colours and selection. */
1.281 nicm 4374: style_apply(&gc, oo, "mode-style", NULL);
1.164 nicm 4375: gc.flags |= GRID_FLAG_NOPALETTE;
1.192 nicm 4376: screen_set_selection(s, sx, sy, endsx, endsy, data->rectflag,
4377: data->modekeys, &gc);
1.42 nicm 4378:
1.96 nicm 4379: if (data->rectflag && may_redraw) {
1.42 nicm 4380: /*
4381: * Can't rely on the caller to redraw the right lines for
4382: * rectangle selection - find the highest line and the number
4383: * of lines, and redraw just past that in both directions
4384: */
4385: cy = data->cy;
1.182 nicm 4386: if (data->cursordrag == CURSORDRAG_ENDSEL) {
4387: if (sy < cy)
1.208 nicm 4388: window_copy_redraw_lines(wme, sy, cy - sy + 1);
1.182 nicm 4389: else
1.208 nicm 4390: window_copy_redraw_lines(wme, cy, sy - cy + 1);
1.182 nicm 4391: } else {
1.208 nicm 4392: if (endsy < cy) {
4393: window_copy_redraw_lines(wme, endsy,
4394: cy - endsy + 1);
4395: } else {
4396: window_copy_redraw_lines(wme, cy,
4397: endsy - cy + 1);
4398: }
1.182 nicm 4399: }
1.42 nicm 4400: }
4401:
1.1 nicm 4402: return (1);
4403: }
4404:
1.157 nicm 4405: static void *
1.208 nicm 4406: window_copy_get_selection(struct window_mode_entry *wme, size_t *len)
1.1 nicm 4407: {
1.208 nicm 4408: struct window_pane *wp = wme->wp;
4409: struct window_copy_mode_data *data = wme->data;
1.1 nicm 4410: struct screen *s = &data->screen;
4411: char *buf;
1.42 nicm 4412: size_t off;
1.111 nicm 4413: u_int i, xx, yy, sx, sy, ex, ey, ey_last;
1.188 nicm 4414: u_int firstsx, lastex, restex, restsx, selx;
1.69 nicm 4415: int keys;
1.1 nicm 4416:
1.282 nicm 4417: if (data->screen.sel == NULL && data->lineflag == LINE_SEL_NONE) {
4418: buf = window_copy_match_at_cursor(data);
4419: if (buf != NULL)
4420: *len = strlen(buf);
1.293 nicm 4421: else
4422: *len = 0;
1.282 nicm 4423: return (buf);
4424: }
1.1 nicm 4425:
4426: buf = xmalloc(1);
4427: off = 0;
4428:
4429: *buf = '\0';
4430:
4431: /*
4432: * The selection extends from selx,sely to (adjusted) cx,cy on
4433: * the base screen.
4434: */
4435:
4436: /* Find start and end. */
1.161 nicm 4437: xx = data->endselx;
4438: yy = data->endsely;
1.2 nicm 4439: if (yy < data->sely || (yy == data->sely && xx < data->selx)) {
1.1 nicm 4440: sx = xx; sy = yy;
4441: ex = data->selx; ey = data->sely;
4442: } else {
4443: sx = data->selx; sy = data->sely;
4444: ex = xx; ey = yy;
4445: }
4446:
4447: /* Trim ex to end of line. */
1.208 nicm 4448: ey_last = window_copy_find_length(wme, ey);
1.111 nicm 4449: if (ex > ey_last)
4450: ex = ey_last;
1.1 nicm 4451:
1.42 nicm 4452: /*
4453: * Deal with rectangle-copy if necessary; four situations: start of
4454: * first line (firstsx), end of last line (lastex), start (restsx) and
4455: * end (restex) of all other lines.
4456: */
4457: xx = screen_size_x(s);
1.69 nicm 4458:
4459: /*
4460: * Behave according to mode-keys. If it is emacs, copy like emacs,
4461: * keeping the top-left-most character, and dropping the
4462: * bottom-right-most, regardless of copy direction. If it is vi, also
4463: * keep bottom-right-most character.
4464: */
1.136 nicm 4465: keys = options_get_number(wp->window->options, "mode-keys");
1.42 nicm 4466: if (data->rectflag) {
4467: /*
4468: * Need to ignore the column with the cursor in it, which for
4469: * rectangular copy means knowing which side the cursor is on.
4470: */
1.188 nicm 4471: if (data->cursordrag == CURSORDRAG_ENDSEL)
4472: selx = data->selx;
4473: else
4474: selx = data->endselx;
4475: if (selx < data->cx) {
1.42 nicm 4476: /* Selection start is on the left. */
1.69 nicm 4477: if (keys == MODEKEY_EMACS) {
4478: lastex = data->cx;
4479: restex = data->cx;
4480: }
4481: else {
4482: lastex = data->cx + 1;
4483: restex = data->cx + 1;
4484: }
1.188 nicm 4485: firstsx = selx;
4486: restsx = selx;
1.42 nicm 4487: } else {
4488: /* Cursor is on the left. */
1.188 nicm 4489: lastex = selx + 1;
4490: restex = selx + 1;
1.64 nicm 4491: firstsx = data->cx;
4492: restsx = data->cx;
1.42 nicm 4493: }
4494: } else {
1.69 nicm 4495: if (keys == MODEKEY_EMACS)
4496: lastex = ex;
1.74 nicm 4497: else
1.69 nicm 4498: lastex = ex + 1;
1.42 nicm 4499: restex = xx;
4500: firstsx = sx;
4501: restsx = 0;
4502: }
4503:
1.1 nicm 4504: /* Copy the lines. */
1.110 nicm 4505: for (i = sy; i <= ey; i++) {
1.208 nicm 4506: window_copy_copy_line(wme, &buf, &off, i,
1.110 nicm 4507: (i == sy ? firstsx : restsx),
4508: (i == ey ? lastex : restex));
1.1 nicm 4509: }
4510:
1.26 nicm 4511: /* Don't bother if no data. */
4512: if (off == 0) {
1.81 nicm 4513: free(buf);
1.294 nicm 4514: *len = 0;
1.89 nicm 4515: return (NULL);
1.26 nicm 4516: }
1.319 nicm 4517: /* Remove final \n (unless at end in vi mode). */
4518: if (keys == MODEKEY_EMACS || lastex <= ey_last) {
4519: if (~grid_get_line(data->backing->grid, ey)->flags &
4520: GRID_LINE_WRAPPED || lastex != ey_last)
4521: off -= 1;
4522: }
1.111 nicm 4523: *len = off;
1.89 nicm 4524: return (buf);
4525: }
4526:
1.157 nicm 4527: static void
1.215 nicm 4528: window_copy_copy_buffer(struct window_mode_entry *wme, const char *prefix,
4529: void *buf, size_t len)
1.89 nicm 4530: {
1.208 nicm 4531: struct window_pane *wp = wme->wp;
4532: struct screen_write_ctx ctx;
1.72 nicm 4533:
1.178 nicm 4534: if (options_get_number(global_options, "set-clipboard") != 0) {
1.283 nicm 4535: screen_write_start_pane(&ctx, wp, NULL);
1.91 nicm 4536: screen_write_setselection(&ctx, buf, len);
4537: screen_write_stop(&ctx);
1.179 nicm 4538: notify_pane("pane-set-clipboard", wp);
1.91 nicm 4539: }
1.1 nicm 4540:
1.215 nicm 4541: paste_add(prefix, buf, len);
1.89 nicm 4542: }
4543:
1.314 nicm 4544: static void *
4545: window_copy_pipe_run(struct window_mode_entry *wme, struct session *s,
4546: const char *cmd, size_t *len)
1.89 nicm 4547: {
1.215 nicm 4548: void *buf;
4549: struct job *job;
1.89 nicm 4550:
1.314 nicm 4551: buf = window_copy_get_selection(wme, len);
1.277 nicm 4552: if (cmd == NULL || *cmd == '\0')
4553: cmd = options_get_string(global_options, "copy-command");
4554: if (cmd != NULL && *cmd != '\0') {
1.331 nicm 4555: job = job_run(cmd, 0, NULL, NULL, s, NULL, NULL, NULL, NULL,
4556: NULL, JOB_NOWAIT, -1, -1);
1.314 nicm 4557: bufferevent_write(job_get_event(job), buf, *len);
1.277 nicm 4558: }
1.314 nicm 4559: return (buf);
4560: }
4561:
4562: static void
4563: window_copy_pipe(struct window_mode_entry *wme, struct session *s,
4564: const char *cmd)
4565: {
4566: size_t len;
4567:
4568: window_copy_pipe_run(wme, s, cmd, &len);
4569: }
4570:
4571: static void
4572: window_copy_copy_pipe(struct window_mode_entry *wme, struct session *s,
4573: const char *prefix, const char *cmd)
4574: {
4575: void *buf;
4576: size_t len;
4577:
4578: buf = window_copy_pipe_run(wme, s, cmd, &len);
1.293 nicm 4579: if (buf != NULL)
4580: window_copy_copy_buffer(wme, prefix, buf, len);
1.89 nicm 4581: }
4582:
1.157 nicm 4583: static void
1.215 nicm 4584: window_copy_copy_selection(struct window_mode_entry *wme, const char *prefix)
1.89 nicm 4585: {
1.203 nicm 4586: char *buf;
1.114 nicm 4587: size_t len;
1.89 nicm 4588:
1.208 nicm 4589: buf = window_copy_get_selection(wme, &len);
1.203 nicm 4590: if (buf != NULL)
1.215 nicm 4591: window_copy_copy_buffer(wme, prefix, buf, len);
1.103 nicm 4592: }
4593:
1.157 nicm 4594: static void
1.208 nicm 4595: window_copy_append_selection(struct window_mode_entry *wme)
1.103 nicm 4596: {
1.208 nicm 4597: struct window_pane *wp = wme->wp;
1.108 nicm 4598: char *buf;
4599: struct paste_buffer *pb;
1.227 nicm 4600: const char *bufdata, *bufname = NULL;
1.132 nicm 4601: size_t len, bufsize;
1.108 nicm 4602: struct screen_write_ctx ctx;
1.103 nicm 4603:
1.208 nicm 4604: buf = window_copy_get_selection(wme, &len);
1.103 nicm 4605: if (buf == NULL)
4606: return;
4607:
1.178 nicm 4608: if (options_get_number(global_options, "set-clipboard") != 0) {
1.283 nicm 4609: screen_write_start_pane(&ctx, wp, NULL);
1.103 nicm 4610: screen_write_setselection(&ctx, buf, len);
4611: screen_write_stop(&ctx);
1.179 nicm 4612: notify_pane("pane-set-clipboard", wp);
1.103 nicm 4613: }
4614:
1.203 nicm 4615: pb = paste_get_top(&bufname);
1.103 nicm 4616: if (pb != NULL) {
1.132 nicm 4617: bufdata = paste_buffer_data(pb, &bufsize);
4618: buf = xrealloc(buf, len + bufsize);
4619: memmove(buf + bufsize, buf, len);
4620: memcpy(buf, bufdata, bufsize);
4621: len += bufsize;
1.103 nicm 4622: }
1.108 nicm 4623: if (paste_set(buf, len, bufname, NULL) != 0)
1.103 nicm 4624: free(buf);
1.1 nicm 4625: }
4626:
1.157 nicm 4627: static void
1.208 nicm 4628: window_copy_copy_line(struct window_mode_entry *wme, char **buf, size_t *off,
4629: u_int sy, u_int sx, u_int ex)
1.1 nicm 4630: {
1.208 nicm 4631: struct window_copy_mode_data *data = wme->data;
1.54 nicm 4632: struct grid *gd = data->backing->grid;
1.140 nicm 4633: struct grid_cell gc;
1.54 nicm 4634: struct grid_line *gl;
1.86 nicm 4635: struct utf8_data ud;
1.54 nicm 4636: u_int i, xx, wrapped = 0;
1.115 nicm 4637: const char *s;
1.1 nicm 4638:
4639: if (sx > ex)
4640: return;
4641:
1.16 nicm 4642: /*
4643: * Work out if the line was wrapped at the screen edge and all of it is
4644: * on screen.
4645: */
1.190 nicm 4646: gl = grid_get_line(gd, sy);
1.35 nicm 4647: if (gl->flags & GRID_LINE_WRAPPED && gl->cellsize <= gd->sx)
1.16 nicm 4648: wrapped = 1;
4649:
4650: /* If the line was wrapped, don't strip spaces (use the full length). */
4651: if (wrapped)
4652: xx = gl->cellsize;
4653: else
1.208 nicm 4654: xx = window_copy_find_length(wme, sy);
1.1 nicm 4655: if (ex > xx)
4656: ex = xx;
4657: if (sx > xx)
4658: sx = xx;
4659:
4660: if (sx < ex) {
4661: for (i = sx; i < ex; i++) {
1.140 nicm 4662: grid_get_cell(gd, i, sy, &gc);
4663: if (gc.flags & GRID_FLAG_PADDING)
1.1 nicm 4664: continue;
1.140 nicm 4665: utf8_copy(&ud, &gc.data);
4666: if (ud.size == 1 && (gc.attr & GRID_ATTR_CHARSET)) {
1.115 nicm 4667: s = tty_acs_get(NULL, ud.data[0]);
4668: if (s != NULL && strlen(s) <= sizeof ud.data) {
4669: ud.size = strlen(s);
1.117 nicm 4670: memcpy(ud.data, s, ud.size);
1.115 nicm 4671: }
4672: }
1.86 nicm 4673:
1.116 nicm 4674: *buf = xrealloc(*buf, (*off) + ud.size);
1.86 nicm 4675: memcpy(*buf + *off, ud.data, ud.size);
4676: *off += ud.size;
1.1 nicm 4677: }
4678: }
4679:
1.16 nicm 4680: /* Only add a newline if the line wasn't wrapped. */
1.44 nicm 4681: if (!wrapped || ex != xx) {
1.116 nicm 4682: *buf = xrealloc(*buf, (*off) + 1);
1.16 nicm 4683: (*buf)[(*off)++] = '\n';
4684: }
1.1 nicm 4685: }
4686:
1.157 nicm 4687: static void
1.208 nicm 4688: window_copy_clear_selection(struct window_mode_entry *wme)
1.47 nicm 4689: {
1.208 nicm 4690: struct window_copy_mode_data *data = wme->data;
1.47 nicm 4691: u_int px, py;
4692:
4693: screen_clear_selection(&data->screen);
4694:
1.161 nicm 4695: data->cursordrag = CURSORDRAG_NONE;
1.197 nicm 4696: data->lineflag = LINE_SEL_NONE;
1.250 nicm 4697: data->selflag = SEL_CHAR;
1.161 nicm 4698:
1.54 nicm 4699: py = screen_hsize(data->backing) + data->cy - data->oy;
1.208 nicm 4700: px = window_copy_find_length(wme, py);
1.47 nicm 4701: if (data->cx > px)
1.208 nicm 4702: window_copy_update_cursor(wme, px, data->cy);
1.47 nicm 4703: }
4704:
1.157 nicm 4705: static int
1.208 nicm 4706: window_copy_in_set(struct window_mode_entry *wme, u_int px, u_int py,
4707: const char *set)
1.1 nicm 4708: {
1.208 nicm 4709: struct window_copy_mode_data *data = wme->data;
1.140 nicm 4710: struct grid_cell gc;
4711:
4712: grid_get_cell(data->backing->grid, px, py, &gc);
1.204 nicm 4713: if (gc.flags & GRID_FLAG_PADDING)
4714: return (0);
1.224 nicm 4715: return (utf8_cstrhas(set, &gc.data));
1.1 nicm 4716: }
4717:
1.157 nicm 4718: static u_int
1.208 nicm 4719: window_copy_find_length(struct window_mode_entry *wme, u_int py)
1.1 nicm 4720: {
1.208 nicm 4721: struct window_copy_mode_data *data = wme->data;
1.1 nicm 4722:
1.224 nicm 4723: return (grid_line_length(data->backing->grid, py));
1.1 nicm 4724: }
4725:
1.157 nicm 4726: static void
1.208 nicm 4727: window_copy_cursor_start_of_line(struct window_mode_entry *wme)
1.5 nicm 4728: {
1.208 nicm 4729: struct window_copy_mode_data *data = wme->data;
1.58 nicm 4730: struct screen *back_s = data->backing;
1.307 nicm 4731: struct grid_reader gr;
1.315 nicm 4732: u_int px, py, oldy, hsize;
1.58 nicm 4733:
1.307 nicm 4734: px = data->cx;
4735: hsize = screen_hsize(back_s);
4736: py = hsize + data->cy - data->oy;
1.312 nicm 4737: oldy = data->cy;
1.307 nicm 4738:
4739: grid_reader_start(&gr, back_s->grid, px, py);
4740: grid_reader_cursor_start_of_line(&gr, 1);
4741: grid_reader_get_cursor(&gr, &px, &py);
1.315 nicm 4742: window_copy_acquire_cursor_up(wme, hsize, data->oy, oldy, px, py);
1.6 nicm 4743: }
4744:
1.157 nicm 4745: static void
1.208 nicm 4746: window_copy_cursor_back_to_indentation(struct window_mode_entry *wme)
1.6 nicm 4747: {
1.208 nicm 4748: struct window_copy_mode_data *data = wme->data;
1.322 nicm 4749: struct screen *back_s = data->backing;
4750: struct grid_reader gr;
4751: u_int px, py, oldy, hsize;
1.6 nicm 4752:
1.322 nicm 4753: px = data->cx;
4754: hsize = screen_hsize(back_s);
4755: py = hsize + data->cy - data->oy;
4756: oldy = data->cy;
1.6 nicm 4757:
1.322 nicm 4758: grid_reader_start(&gr, back_s->grid, px, py);
4759: grid_reader_cursor_back_to_indentation(&gr);
4760: grid_reader_get_cursor(&gr, &px, &py);
4761: window_copy_acquire_cursor_up(wme, hsize, data->oy, oldy, px, py);
1.5 nicm 4762: }
4763:
1.157 nicm 4764: static void
1.208 nicm 4765: window_copy_cursor_end_of_line(struct window_mode_entry *wme)
1.5 nicm 4766: {
1.208 nicm 4767: struct window_copy_mode_data *data = wme->data;
1.54 nicm 4768: struct screen *back_s = data->backing;
1.307 nicm 4769: struct grid_reader gr;
1.315 nicm 4770: u_int px, py, oldy, hsize;
1.307 nicm 4771:
4772: px = data->cx;
4773: hsize = screen_hsize(back_s);
4774: py = hsize + data->cy - data->oy;
1.312 nicm 4775: oldy = data->cy;
1.5 nicm 4776:
1.307 nicm 4777: grid_reader_start(&gr, back_s->grid, px, py);
4778: if (data->screen.sel != NULL && data->rectflag)
4779: grid_reader_cursor_end_of_line(&gr, 1, 1);
4780: else
4781: grid_reader_cursor_end_of_line(&gr, 1, 0);
4782: grid_reader_get_cursor(&gr, &px, &py);
1.315 nicm 4783: window_copy_acquire_cursor_down(wme, hsize, screen_size_y(back_s),
4784: data->oy, oldy, px, py, 0);
1.95 nicm 4785: }
4786:
1.157 nicm 4787: static void
1.208 nicm 4788: window_copy_other_end(struct window_mode_entry *wme)
1.95 nicm 4789: {
1.208 nicm 4790: struct window_copy_mode_data *data = wme->data;
1.95 nicm 4791: struct screen *s = &data->screen;
1.161 nicm 4792: u_int selx, sely, cy, yy, hsize;
1.95 nicm 4793:
1.192 nicm 4794: if (s->sel == NULL && data->lineflag == LINE_SEL_NONE)
1.95 nicm 4795: return;
4796:
1.192 nicm 4797: if (data->lineflag == LINE_SEL_LEFT_RIGHT)
4798: data->lineflag = LINE_SEL_RIGHT_LEFT;
4799: else if (data->lineflag == LINE_SEL_RIGHT_LEFT)
4800: data->lineflag = LINE_SEL_LEFT_RIGHT;
1.118 nicm 4801:
1.161 nicm 4802: switch (data->cursordrag) {
4803: case CURSORDRAG_NONE:
4804: case CURSORDRAG_SEL:
4805: data->cursordrag = CURSORDRAG_ENDSEL;
4806: break;
4807: case CURSORDRAG_ENDSEL:
4808: data->cursordrag = CURSORDRAG_SEL;
4809: break;
4810: }
4811:
4812: selx = data->endselx;
4813: sely = data->endsely;
4814: if (data->cursordrag == CURSORDRAG_SEL) {
4815: selx = data->selx;
4816: sely = data->sely;
4817: }
4818:
1.95 nicm 4819: cy = data->cy;
4820: yy = screen_hsize(data->backing) + data->cy - data->oy;
4821:
4822: data->cx = selx;
4823:
1.122 nicm 4824: hsize = screen_hsize(data->backing);
1.161 nicm 4825: if (sely < hsize - data->oy) { /* above */
1.122 nicm 4826: data->oy = hsize - sely;
1.95 nicm 4827: data->cy = 0;
1.161 nicm 4828: } else if (sely > hsize - data->oy + screen_size_y(s)) { /* below */
1.122 nicm 4829: data->oy = hsize - sely + screen_size_y(s) - 1;
1.95 nicm 4830: data->cy = screen_size_y(s) - 1;
4831: } else
4832: data->cy = cy + sely - yy;
4833:
1.252 nicm 4834: window_copy_update_selection(wme, 1, 1);
1.208 nicm 4835: window_copy_redraw_screen(wme);
1.5 nicm 4836: }
4837:
1.157 nicm 4838: static void
1.208 nicm 4839: window_copy_cursor_left(struct window_mode_entry *wme)
1.1 nicm 4840: {
1.208 nicm 4841: struct window_copy_mode_data *data = wme->data;
1.307 nicm 4842: struct screen *back_s = data->backing;
4843: struct grid_reader gr;
1.315 nicm 4844: u_int px, py, oldy, hsize;
1.307 nicm 4845:
4846: px = data->cx;
4847: hsize = screen_hsize(back_s);
4848: py = hsize + data->cy - data->oy;
1.315 nicm 4849: oldy = data->cy;
1.1 nicm 4850:
1.307 nicm 4851: grid_reader_start(&gr, back_s->grid, px, py);
1.322 nicm 4852: grid_reader_cursor_left(&gr, 1);
1.307 nicm 4853: grid_reader_get_cursor(&gr, &px, &py);
1.315 nicm 4854: window_copy_acquire_cursor_up(wme, hsize, data->oy, oldy, px, py);
1.1 nicm 4855: }
4856:
1.157 nicm 4857: static void
1.305 nicm 4858: window_copy_cursor_right(struct window_mode_entry *wme, int all)
1.1 nicm 4859: {
1.208 nicm 4860: struct window_copy_mode_data *data = wme->data;
1.307 nicm 4861: struct screen *back_s = data->backing;
4862: struct grid_reader gr;
1.315 nicm 4863: u_int px, py, oldy, hsize;
1.307 nicm 4864:
4865: px = data->cx;
4866: hsize = screen_hsize(back_s);
4867: py = hsize + data->cy - data->oy;
1.315 nicm 4868: oldy = data->cy;
1.1 nicm 4869:
1.307 nicm 4870: grid_reader_start(&gr, back_s->grid, px, py);
4871: grid_reader_cursor_right(&gr, 1, all);
4872: grid_reader_get_cursor(&gr, &px, &py);
1.315 nicm 4873: window_copy_acquire_cursor_down(wme, hsize, screen_size_y(back_s),
4874: data->oy, oldy, px, py, 0);
1.1 nicm 4875: }
4876:
1.157 nicm 4877: static void
1.208 nicm 4878: window_copy_cursor_up(struct window_mode_entry *wme, int scroll_only)
1.1 nicm 4879: {
1.208 nicm 4880: struct window_copy_mode_data *data = wme->data;
1.36 nicm 4881: struct screen *s = &data->screen;
1.1 nicm 4882: u_int ox, oy, px, py;
1.324 nicm 4883: int norectsel;
1.1 nicm 4884:
1.324 nicm 4885: norectsel = data->screen.sel == NULL || !data->rectflag;
1.54 nicm 4886: oy = screen_hsize(data->backing) + data->cy - data->oy;
1.208 nicm 4887: ox = window_copy_find_length(wme, oy);
1.324 nicm 4888: if (norectsel && data->cx != ox) {
1.25 nicm 4889: data->lastcx = data->cx;
4890: data->lastsx = ox;
4891: }
1.1 nicm 4892:
1.192 nicm 4893: if (data->lineflag == LINE_SEL_LEFT_RIGHT && oy == data->sely)
1.208 nicm 4894: window_copy_other_end(wme);
1.118 nicm 4895:
1.28 nicm 4896: if (scroll_only || data->cy == 0) {
1.324 nicm 4897: if (norectsel)
4898: data->cx = data->lastcx;
1.208 nicm 4899: window_copy_scroll_down(wme, 1);
1.36 nicm 4900: if (scroll_only) {
4901: if (data->cy == screen_size_y(s) - 1)
1.208 nicm 4902: window_copy_redraw_lines(wme, data->cy, 1);
1.36 nicm 4903: else
1.208 nicm 4904: window_copy_redraw_lines(wme, data->cy, 2);
1.36 nicm 4905: }
1.28 nicm 4906: } else {
1.324 nicm 4907: if (norectsel) {
4908: window_copy_update_cursor(wme, data->lastcx,
4909: data->cy - 1);
4910: } else
4911: window_copy_update_cursor(wme, data->cx, data->cy - 1);
1.252 nicm 4912: if (window_copy_update_selection(wme, 1, 0)) {
1.36 nicm 4913: if (data->cy == screen_size_y(s) - 1)
1.208 nicm 4914: window_copy_redraw_lines(wme, data->cy, 1);
1.36 nicm 4915: else
1.208 nicm 4916: window_copy_redraw_lines(wme, data->cy, 2);
1.36 nicm 4917: }
1.1 nicm 4918: }
4919:
1.324 nicm 4920: if (norectsel) {
1.54 nicm 4921: py = screen_hsize(data->backing) + data->cy - data->oy;
1.208 nicm 4922: px = window_copy_find_length(wme, py);
1.49 nicm 4923: if ((data->cx >= data->lastsx && data->cx != px) ||
4924: data->cx > px)
1.307 nicm 4925: {
4926: window_copy_update_cursor(wme, px, data->cy);
4927: if (window_copy_update_selection(wme, 1, 0))
4928: window_copy_redraw_lines(wme, data->cy, 1);
4929: }
1.47 nicm 4930: }
1.118 nicm 4931:
1.192 nicm 4932: if (data->lineflag == LINE_SEL_LEFT_RIGHT)
1.307 nicm 4933: {
4934: py = screen_hsize(data->backing) + data->cy - data->oy;
4935: if (data->rectflag)
4936: px = screen_size_x(data->backing);
4937: else
4938: px = window_copy_find_length(wme, py);
4939: window_copy_update_cursor(wme, px, data->cy);
4940: if (window_copy_update_selection(wme, 1, 0))
4941: window_copy_redraw_lines(wme, data->cy, 1);
4942: }
1.192 nicm 4943: else if (data->lineflag == LINE_SEL_RIGHT_LEFT)
1.307 nicm 4944: {
4945: window_copy_update_cursor(wme, 0, data->cy);
4946: if (window_copy_update_selection(wme, 1, 0))
4947: window_copy_redraw_lines(wme, data->cy, 1);
4948: }
1.1 nicm 4949: }
4950:
1.157 nicm 4951: static void
1.208 nicm 4952: window_copy_cursor_down(struct window_mode_entry *wme, int scroll_only)
1.1 nicm 4953: {
1.208 nicm 4954: struct window_copy_mode_data *data = wme->data;
1.1 nicm 4955: struct screen *s = &data->screen;
4956: u_int ox, oy, px, py;
1.324 nicm 4957: int norectsel;
1.1 nicm 4958:
1.324 nicm 4959: norectsel = data->screen.sel == NULL || !data->rectflag;
1.54 nicm 4960: oy = screen_hsize(data->backing) + data->cy - data->oy;
1.208 nicm 4961: ox = window_copy_find_length(wme, oy);
1.324 nicm 4962: if (norectsel && data->cx != ox) {
1.25 nicm 4963: data->lastcx = data->cx;
4964: data->lastsx = ox;
4965: }
1.1 nicm 4966:
1.192 nicm 4967: if (data->lineflag == LINE_SEL_RIGHT_LEFT && oy == data->endsely)
1.208 nicm 4968: window_copy_other_end(wme);
1.118 nicm 4969:
1.28 nicm 4970: if (scroll_only || data->cy == screen_size_y(s) - 1) {
1.324 nicm 4971: if (norectsel)
4972: data->cx = data->lastcx;
1.208 nicm 4973: window_copy_scroll_up(wme, 1);
1.31 nicm 4974: if (scroll_only && data->cy > 0)
1.208 nicm 4975: window_copy_redraw_lines(wme, data->cy - 1, 2);
1.28 nicm 4976: } else {
1.324 nicm 4977: if (norectsel) {
4978: window_copy_update_cursor(wme, data->lastcx,
4979: data->cy + 1);
4980: } else
4981: window_copy_update_cursor(wme, data->cx, data->cy + 1);
1.252 nicm 4982: if (window_copy_update_selection(wme, 1, 0))
1.208 nicm 4983: window_copy_redraw_lines(wme, data->cy - 1, 2);
1.1 nicm 4984: }
4985:
1.324 nicm 4986: if (norectsel) {
1.54 nicm 4987: py = screen_hsize(data->backing) + data->cy - data->oy;
1.208 nicm 4988: px = window_copy_find_length(wme, py);
1.49 nicm 4989: if ((data->cx >= data->lastsx && data->cx != px) ||
4990: data->cx > px)
1.307 nicm 4991: {
4992: window_copy_update_cursor(wme, px, data->cy);
4993: if (window_copy_update_selection(wme, 1, 0))
4994: window_copy_redraw_lines(wme, data->cy, 1);
4995: }
1.52 nicm 4996: }
1.118 nicm 4997:
1.192 nicm 4998: if (data->lineflag == LINE_SEL_LEFT_RIGHT)
1.307 nicm 4999: {
5000: py = screen_hsize(data->backing) + data->cy - data->oy;
5001: if (data->rectflag)
5002: px = screen_size_x(data->backing);
5003: else
5004: px = window_copy_find_length(wme, py);
5005: window_copy_update_cursor(wme, px, data->cy);
5006: if (window_copy_update_selection(wme, 1, 0))
5007: window_copy_redraw_lines(wme, data->cy, 1);
5008: }
1.192 nicm 5009: else if (data->lineflag == LINE_SEL_RIGHT_LEFT)
1.307 nicm 5010: {
5011: window_copy_update_cursor(wme, 0, data->cy);
5012: if (window_copy_update_selection(wme, 1, 0))
5013: window_copy_redraw_lines(wme, data->cy, 1);
5014: }
1.52 nicm 5015: }
5016:
1.157 nicm 5017: static void
1.208 nicm 5018: window_copy_cursor_jump(struct window_mode_entry *wme)
1.52 nicm 5019: {
1.208 nicm 5020: struct window_copy_mode_data *data = wme->data;
1.54 nicm 5021: struct screen *back_s = data->backing;
1.315 nicm 5022: struct grid_reader gr;
5023: u_int px, py, oldy, hsize;
1.52 nicm 5024:
5025: px = data->cx + 1;
1.315 nicm 5026: hsize = screen_hsize(back_s);
5027: py = hsize + data->cy - data->oy;
5028: oldy = data->cy;
1.52 nicm 5029:
1.315 nicm 5030: grid_reader_start(&gr, back_s->grid, px, py);
5031: if (grid_reader_cursor_jump(&gr, data->jumpchar)) {
5032: grid_reader_get_cursor(&gr, &px, &py);
5033: window_copy_acquire_cursor_down(wme, hsize,
5034: screen_size_y(back_s), data->oy, oldy, px, py, 0);
1.52 nicm 5035: }
5036: }
5037:
1.157 nicm 5038: static void
1.208 nicm 5039: window_copy_cursor_jump_back(struct window_mode_entry *wme)
1.52 nicm 5040: {
1.208 nicm 5041: struct window_copy_mode_data *data = wme->data;
1.54 nicm 5042: struct screen *back_s = data->backing;
1.315 nicm 5043: struct grid_reader gr;
5044: u_int px, py, oldy, hsize;
1.52 nicm 5045:
5046: px = data->cx;
1.315 nicm 5047: hsize = screen_hsize(back_s);
5048: py = hsize + data->cy - data->oy;
5049: oldy = data->cy;
1.52 nicm 5050:
1.315 nicm 5051: grid_reader_start(&gr, back_s->grid, px, py);
1.322 nicm 5052: grid_reader_cursor_left(&gr, 0);
1.315 nicm 5053: if (grid_reader_cursor_jump_back(&gr, data->jumpchar)) {
5054: grid_reader_get_cursor(&gr, &px, &py);
5055: window_copy_acquire_cursor_up(wme, hsize, data->oy, oldy, px,
5056: py);
1.76 nicm 5057: }
5058: }
5059:
1.157 nicm 5060: static void
1.208 nicm 5061: window_copy_cursor_jump_to(struct window_mode_entry *wme)
1.76 nicm 5062: {
1.208 nicm 5063: struct window_copy_mode_data *data = wme->data;
1.76 nicm 5064: struct screen *back_s = data->backing;
1.315 nicm 5065: struct grid_reader gr;
5066: u_int px, py, oldy, hsize;
1.76 nicm 5067:
1.184 nicm 5068: px = data->cx + 2;
1.315 nicm 5069: hsize = screen_hsize(back_s);
5070: py = hsize + data->cy - data->oy;
5071: oldy = data->cy;
1.76 nicm 5072:
1.315 nicm 5073: grid_reader_start(&gr, back_s->grid, px, py);
5074: if (grid_reader_cursor_jump(&gr, data->jumpchar)) {
1.322 nicm 5075: grid_reader_cursor_left(&gr, 1);
1.315 nicm 5076: grid_reader_get_cursor(&gr, &px, &py);
5077: window_copy_acquire_cursor_down(wme, hsize,
5078: screen_size_y(back_s), data->oy, oldy, px, py, 0);
1.76 nicm 5079: }
5080: }
5081:
1.157 nicm 5082: static void
1.208 nicm 5083: window_copy_cursor_jump_to_back(struct window_mode_entry *wme)
1.76 nicm 5084: {
1.208 nicm 5085: struct window_copy_mode_data *data = wme->data;
1.76 nicm 5086: struct screen *back_s = data->backing;
1.315 nicm 5087: struct grid_reader gr;
5088: u_int px, py, oldy, hsize;
1.76 nicm 5089:
5090: px = data->cx;
1.315 nicm 5091: hsize = screen_hsize(back_s);
5092: py = hsize + data->cy - data->oy;
5093: oldy = data->cy;
1.76 nicm 5094:
1.315 nicm 5095: grid_reader_start(&gr, back_s->grid, px, py);
1.322 nicm 5096: grid_reader_cursor_left(&gr, 0);
5097: grid_reader_cursor_left(&gr, 0);
1.315 nicm 5098: if (grid_reader_cursor_jump_back(&gr, data->jumpchar)) {
5099: grid_reader_cursor_right(&gr, 1, 0);
5100: grid_reader_get_cursor(&gr, &px, &py);
5101: window_copy_acquire_cursor_up(wme, hsize, data->oy, oldy, px,
5102: py);
1.47 nicm 5103: }
1.1 nicm 5104: }
5105:
1.157 nicm 5106: static void
1.208 nicm 5107: window_copy_cursor_next_word(struct window_mode_entry *wme,
5108: const char *separators)
1.40 nicm 5109: {
1.208 nicm 5110: struct window_copy_mode_data *data = wme->data;
1.54 nicm 5111: struct screen *back_s = data->backing;
1.312 nicm 5112: struct grid_reader gr;
1.315 nicm 5113: u_int px, py, oldy, hsize;
1.40 nicm 5114:
5115: px = data->cx;
1.312 nicm 5116: hsize = screen_hsize(back_s);
5117: py = hsize + data->cy - data->oy;
5118: oldy = data->cy;
1.40 nicm 5119:
1.312 nicm 5120: grid_reader_start(&gr, back_s->grid, px, py);
5121: grid_reader_cursor_next_word(&gr, separators);
5122: grid_reader_get_cursor(&gr, &px, &py);
1.315 nicm 5123: window_copy_acquire_cursor_down(wme, hsize, screen_size_y(back_s),
5124: data->oy, oldy, px, py, 0);
1.40 nicm 5125: }
5126:
1.319 nicm 5127: /* Compute the next place where a word ends. */
1.157 nicm 5128: static void
1.247 nicm 5129: window_copy_cursor_next_word_end_pos(struct window_mode_entry *wme,
5130: const char *separators, u_int *ppx, u_int *ppy)
5131: {
5132: struct window_pane *wp = wme->wp;
5133: struct window_copy_mode_data *data = wme->data;
5134: struct options *oo = wp->window->options;
5135: struct screen *back_s = data->backing;
1.319 nicm 5136: struct grid_reader gr;
5137: u_int px, py, hsize;
1.247 nicm 5138:
5139: px = data->cx;
1.319 nicm 5140: hsize = screen_hsize(back_s);
5141: py = hsize + data->cy - data->oy;
1.247 nicm 5142:
1.319 nicm 5143: grid_reader_start(&gr, back_s->grid, px, py);
1.323 nicm 5144: if (options_get_number(oo, "mode-keys") == MODEKEY_VI) {
5145: if (!grid_reader_in_set(&gr, WHITESPACE))
5146: grid_reader_cursor_right(&gr, 0, 0);
5147: grid_reader_cursor_next_word_end(&gr, separators);
1.322 nicm 5148: grid_reader_cursor_left(&gr, 1);
1.323 nicm 5149: } else
5150: grid_reader_cursor_next_word_end(&gr, separators);
1.319 nicm 5151: grid_reader_get_cursor(&gr, &px, &py);
1.247 nicm 5152: *ppx = px;
5153: *ppy = py;
5154: }
5155:
1.319 nicm 5156: /* Move to the next place where a word ends. */
1.247 nicm 5157: static void
1.208 nicm 5158: window_copy_cursor_next_word_end(struct window_mode_entry *wme,
1.255 nicm 5159: const char *separators, int no_reset)
1.1 nicm 5160: {
1.208 nicm 5161: struct window_pane *wp = wme->wp;
5162: struct window_copy_mode_data *data = wme->data;
1.136 nicm 5163: struct options *oo = wp->window->options;
1.54 nicm 5164: struct screen *back_s = data->backing;
1.307 nicm 5165: struct grid_reader gr;
1.315 nicm 5166: u_int px, py, oldy, hsize;
1.1 nicm 5167:
1.18 nicm 5168: px = data->cx;
1.307 nicm 5169: hsize = screen_hsize(back_s);
5170: py = hsize + data->cy - data->oy;
1.312 nicm 5171: oldy = data->cy;
1.1 nicm 5172:
1.307 nicm 5173: grid_reader_start(&gr, back_s->grid, px, py);
1.323 nicm 5174: if (options_get_number(oo, "mode-keys") == MODEKEY_VI) {
5175: if (!grid_reader_in_set(&gr, WHITESPACE))
5176: grid_reader_cursor_right(&gr, 0, 0);
5177: grid_reader_cursor_next_word_end(&gr, separators);
1.322 nicm 5178: grid_reader_cursor_left(&gr, 1);
1.323 nicm 5179: } else
5180: grid_reader_cursor_next_word_end(&gr, separators);
1.307 nicm 5181: grid_reader_get_cursor(&gr, &px, &py);
1.315 nicm 5182: window_copy_acquire_cursor_down(wme, hsize, screen_size_y(back_s),
5183: data->oy, oldy, px, py, no_reset);
1.1 nicm 5184: }
5185:
1.247 nicm 5186: /* Compute the previous place where a word begins. */
5187: static void
5188: window_copy_cursor_previous_word_pos(struct window_mode_entry *wme,
1.323 nicm 5189: const char *separators, u_int *ppx, u_int *ppy)
1.247 nicm 5190: {
5191: struct window_copy_mode_data *data = wme->data;
1.319 nicm 5192: struct screen *back_s = data->backing;
5193: struct grid_reader gr;
1.306 nicm 5194: u_int px, py, hsize;
1.247 nicm 5195:
5196: px = data->cx;
1.319 nicm 5197: hsize = screen_hsize(back_s);
1.306 nicm 5198: py = hsize + data->cy - data->oy;
1.247 nicm 5199:
1.319 nicm 5200: grid_reader_start(&gr, back_s->grid, px, py);
1.323 nicm 5201: grid_reader_cursor_previous_word(&gr, separators, /* already= */ 0,
5202: /* stop_at_eol= */ 1);
1.319 nicm 5203: grid_reader_get_cursor(&gr, &px, &py);
1.247 nicm 5204: *ppx = px;
5205: *ppy = py;
5206: }
5207:
1.8 nicm 5208: /* Move to the previous place where a word begins. */
1.157 nicm 5209: static void
1.208 nicm 5210: window_copy_cursor_previous_word(struct window_mode_entry *wme,
1.229 nicm 5211: const char *separators, int already)
1.1 nicm 5212: {
1.208 nicm 5213: struct window_copy_mode_data *data = wme->data;
1.329 nicm 5214: struct window *w = wme->wp->window;
1.307 nicm 5215: struct screen *back_s = data->backing;
5216: struct grid_reader gr;
1.315 nicm 5217: u_int px, py, oldy, hsize;
1.323 nicm 5218: int stop_at_eol;
5219:
1.329 nicm 5220: if (options_get_number(w->options, "mode-keys") == MODEKEY_EMACS)
5221: stop_at_eol = 1;
5222: else
5223: stop_at_eol = 0;
1.1 nicm 5224:
1.18 nicm 5225: px = data->cx;
1.307 nicm 5226: hsize = screen_hsize(back_s);
1.306 nicm 5227: py = hsize + data->cy - data->oy;
1.312 nicm 5228: oldy = data->cy;
1.1 nicm 5229:
1.307 nicm 5230: grid_reader_start(&gr, back_s->grid, px, py);
1.323 nicm 5231: grid_reader_cursor_previous_word(&gr, separators, already, stop_at_eol);
1.307 nicm 5232: grid_reader_get_cursor(&gr, &px, &py);
1.315 nicm 5233: window_copy_acquire_cursor_up(wme, hsize, data->oy, oldy, px, py);
1.1 nicm 5234: }
5235:
1.157 nicm 5236: static void
1.208 nicm 5237: window_copy_scroll_up(struct window_mode_entry *wme, u_int ny)
1.1 nicm 5238: {
1.208 nicm 5239: struct window_pane *wp = wme->wp;
5240: struct window_copy_mode_data *data = wme->data;
1.1 nicm 5241: struct screen *s = &data->screen;
5242: struct screen_write_ctx ctx;
5243:
5244: if (data->oy < ny)
5245: ny = data->oy;
5246: if (ny == 0)
5247: return;
5248: data->oy -= ny;
5249:
1.264 nicm 5250: if (data->searchmark != NULL && !data->timeout)
1.296 nicm 5251: window_copy_search_marks(wme, NULL, data->searchregex, 1);
1.252 nicm 5252: window_copy_update_selection(wme, 0, 0);
1.96 nicm 5253:
1.283 nicm 5254: screen_write_start_pane(&ctx, wp, NULL);
1.212 nicm 5255: screen_write_cursormove(&ctx, 0, 0, 0);
1.159 nicm 5256: screen_write_deleteline(&ctx, ny, 8);
1.208 nicm 5257: window_copy_write_lines(wme, &ctx, screen_size_y(s) - ny, ny);
5258: window_copy_write_line(wme, &ctx, 0);
1.31 nicm 5259: if (screen_size_y(s) > 1)
1.208 nicm 5260: window_copy_write_line(wme, &ctx, 1);
1.31 nicm 5261: if (screen_size_y(s) > 3)
1.208 nicm 5262: window_copy_write_line(wme, &ctx, screen_size_y(s) - 2);
1.192 nicm 5263: if (s->sel != NULL && screen_size_y(s) > ny)
1.208 nicm 5264: window_copy_write_line(wme, &ctx, screen_size_y(s) - ny - 1);
1.212 nicm 5265: screen_write_cursormove(&ctx, data->cx, data->cy, 0);
1.1 nicm 5266: screen_write_stop(&ctx);
5267: }
5268:
1.157 nicm 5269: static void
1.208 nicm 5270: window_copy_scroll_down(struct window_mode_entry *wme, u_int ny)
1.1 nicm 5271: {
1.208 nicm 5272: struct window_pane *wp = wme->wp;
5273: struct window_copy_mode_data *data = wme->data;
1.1 nicm 5274: struct screen *s = &data->screen;
5275: struct screen_write_ctx ctx;
5276:
1.54 nicm 5277: if (ny > screen_hsize(data->backing))
1.1 nicm 5278: return;
5279:
1.54 nicm 5280: if (data->oy > screen_hsize(data->backing) - ny)
5281: ny = screen_hsize(data->backing) - data->oy;
1.1 nicm 5282: if (ny == 0)
5283: return;
5284: data->oy += ny;
5285:
1.264 nicm 5286: if (data->searchmark != NULL && !data->timeout)
1.296 nicm 5287: window_copy_search_marks(wme, NULL, data->searchregex, 1);
1.252 nicm 5288: window_copy_update_selection(wme, 0, 0);
1.96 nicm 5289:
1.283 nicm 5290: screen_write_start_pane(&ctx, wp, NULL);
1.212 nicm 5291: screen_write_cursormove(&ctx, 0, 0, 0);
1.159 nicm 5292: screen_write_insertline(&ctx, ny, 8);
1.208 nicm 5293: window_copy_write_lines(wme, &ctx, 0, ny);
1.192 nicm 5294: if (s->sel != NULL && screen_size_y(s) > ny)
1.208 nicm 5295: window_copy_write_line(wme, &ctx, ny);
1.96 nicm 5296: else if (ny == 1) /* nuke position */
1.208 nicm 5297: window_copy_write_line(wme, &ctx, 1);
1.212 nicm 5298: screen_write_cursormove(&ctx, data->cx, data->cy, 0);
1.1 nicm 5299: screen_write_stop(&ctx);
5300: }
1.42 nicm 5301:
1.157 nicm 5302: static void
1.313 nicm 5303: window_copy_rectangle_set(struct window_mode_entry *wme, int rectflag)
1.42 nicm 5304: {
1.208 nicm 5305: struct window_copy_mode_data *data = wme->data;
1.47 nicm 5306: u_int px, py;
1.42 nicm 5307:
1.313 nicm 5308: data->rectflag = rectflag;
1.47 nicm 5309:
1.54 nicm 5310: py = screen_hsize(data->backing) + data->cy - data->oy;
1.208 nicm 5311: px = window_copy_find_length(wme, py);
1.47 nicm 5312: if (data->cx > px)
1.208 nicm 5313: window_copy_update_cursor(wme, px, data->cy);
1.42 nicm 5314:
1.252 nicm 5315: window_copy_update_selection(wme, 1, 0);
1.208 nicm 5316: window_copy_redraw_screen(wme);
1.156 nicm 5317: }
5318:
1.157 nicm 5319: static void
1.156 nicm 5320: window_copy_move_mouse(struct mouse_event *m)
5321: {
1.211 nicm 5322: struct window_pane *wp;
5323: struct window_mode_entry *wme;
5324: u_int x, y;
1.156 nicm 5325:
5326: wp = cmd_mouse_pane(m, NULL, NULL);
1.211 nicm 5327: if (wp == NULL)
5328: return;
5329: wme = TAILQ_FIRST(&wp->modes);
1.223 nicm 5330: if (wme == NULL)
5331: return;
5332: if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
1.156 nicm 5333: return;
5334:
1.183 nicm 5335: if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
1.156 nicm 5336: return;
5337:
1.211 nicm 5338: window_copy_update_cursor(wme, x, y);
1.126 nicm 5339: }
5340:
5341: void
1.141 nicm 5342: window_copy_start_drag(struct client *c, struct mouse_event *m)
1.126 nicm 5343: {
1.211 nicm 5344: struct window_pane *wp;
5345: struct window_mode_entry *wme;
1.247 nicm 5346: struct window_copy_mode_data *data;
1.248 nicm 5347: u_int x, y, yg;
1.126 nicm 5348:
1.155 nicm 5349: if (c == NULL)
5350: return;
5351:
1.126 nicm 5352: wp = cmd_mouse_pane(m, NULL, NULL);
1.211 nicm 5353: if (wp == NULL)
5354: return;
5355: wme = TAILQ_FIRST(&wp->modes);
1.223 nicm 5356: if (wme == NULL)
5357: return;
5358: if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
1.126 nicm 5359: return;
5360:
5361: if (cmd_mouse_at(wp, m, &x, &y, 1) != 0)
5362: return;
5363:
5364: c->tty.mouse_drag_update = window_copy_drag_update;
1.217 nicm 5365: c->tty.mouse_drag_release = window_copy_drag_release;
1.126 nicm 5366:
1.247 nicm 5367: data = wme->data;
1.248 nicm 5368: yg = screen_hsize(data->backing) + y - data->oy;
5369: if (x < data->selrx || x > data->endselrx || yg != data->selry)
5370: data->selflag = SEL_CHAR;
1.247 nicm 5371: switch (data->selflag) {
1.311 nicm 5372: case SEL_WORD:
1.323 nicm 5373: if (data->separators != NULL) {
1.247 nicm 5374: window_copy_update_cursor(wme, x, y);
1.323 nicm 5375: window_copy_cursor_previous_word_pos(wme,
5376: data->separators, &x, &y);
1.311 nicm 5377: y -= screen_hsize(data->backing) - data->oy;
5378: }
5379: window_copy_update_cursor(wme, x, y);
5380: break;
5381: case SEL_LINE:
5382: window_copy_update_cursor(wme, 0, y);
5383: break;
5384: case SEL_CHAR:
5385: window_copy_update_cursor(wme, x, y);
5386: window_copy_start_selection(wme);
5387: break;
1.247 nicm 5388: }
5389:
1.211 nicm 5390: window_copy_redraw_screen(wme);
1.220 nicm 5391: window_copy_drag_update(c, m);
1.126 nicm 5392: }
5393:
1.157 nicm 5394: static void
1.211 nicm 5395: window_copy_drag_update(struct client *c, struct mouse_event *m)
1.126 nicm 5396: {
5397: struct window_pane *wp;
1.211 nicm 5398: struct window_mode_entry *wme;
1.126 nicm 5399: struct window_copy_mode_data *data;
1.217 nicm 5400: u_int x, y, old_cx, old_cy;
5401: struct timeval tv = {
5402: .tv_usec = WINDOW_COPY_DRAG_REPEAT_TIME
5403: };
1.126 nicm 5404:
1.211 nicm 5405: if (c == NULL)
5406: return;
5407:
1.126 nicm 5408: wp = cmd_mouse_pane(m, NULL, NULL);
1.211 nicm 5409: if (wp == NULL)
1.126 nicm 5410: return;
1.211 nicm 5411: wme = TAILQ_FIRST(&wp->modes);
1.223 nicm 5412: if (wme == NULL)
5413: return;
5414: if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
1.211 nicm 5415: return;
1.217 nicm 5416:
1.211 nicm 5417: data = wme->data;
1.217 nicm 5418: evtimer_del(&data->dragtimer);
1.126 nicm 5419:
5420: if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
5421: return;
1.217 nicm 5422: old_cx = data->cx;
1.126 nicm 5423: old_cy = data->cy;
5424:
1.211 nicm 5425: window_copy_update_cursor(wme, x, y);
1.252 nicm 5426: if (window_copy_update_selection(wme, 1, 0))
1.211 nicm 5427: window_copy_redraw_selection(wme, old_cy);
1.217 nicm 5428: if (old_cy != data->cy || old_cx == data->cx) {
5429: if (y == 0) {
5430: evtimer_add(&data->dragtimer, &tv);
5431: window_copy_cursor_up(wme, 1);
5432: } else if (y == screen_size_y(&data->screen) - 1) {
5433: evtimer_add(&data->dragtimer, &tv);
5434: window_copy_cursor_down(wme, 1);
5435: }
5436: }
5437: }
5438:
5439: static void
5440: window_copy_drag_release(struct client *c, struct mouse_event *m)
5441: {
5442: struct window_pane *wp;
5443: struct window_mode_entry *wme;
5444: struct window_copy_mode_data *data;
5445:
5446: if (c == NULL)
5447: return;
5448:
5449: wp = cmd_mouse_pane(m, NULL, NULL);
5450: if (wp == NULL)
5451: return;
5452: wme = TAILQ_FIRST(&wp->modes);
1.223 nicm 5453: if (wme == NULL)
5454: return;
5455: if (wme->mode != &window_copy_mode && wme->mode != &window_view_mode)
1.217 nicm 5456: return;
5457:
5458: data = wme->data;
5459: evtimer_del(&data->dragtimer);
1.288 nicm 5460: }
5461:
5462: static void
5463: window_copy_jump_to_mark(struct window_mode_entry *wme)
5464: {
5465: struct window_copy_mode_data *data = wme->data;
5466: u_int tmx, tmy;
5467:
5468: tmx = data->cx;
5469: tmy = screen_hsize(data->backing) + data->cy - data->oy;
5470: data->cx = data->mx;
5471: if (data->my < screen_hsize(data->backing)) {
5472: data->cy = 0;
5473: data->oy = screen_hsize(data->backing) - data->my;
5474: } else {
5475: data->cy = data->my - screen_hsize(data->backing);
5476: data->oy = 0;
5477: }
5478: data->mx = tmx;
5479: data->my = tmy;
5480: data->showmark = 1;
5481: window_copy_update_selection(wme, 0, 0);
5482: window_copy_redraw_screen(wme);
1.315 nicm 5483: }
5484:
5485: /* Scroll up if the cursor went off the visible screen. */
5486: static void
5487: window_copy_acquire_cursor_up(struct window_mode_entry *wme, u_int hsize,
5488: u_int oy, u_int oldy, u_int px, u_int py)
5489: {
5490: u_int cy, yy, ny, nd;
5491:
5492: yy = hsize - oy;
5493: if (py < yy) {
5494: ny = yy - py;
5495: cy = 0;
5496: nd = 1;
5497: } else {
5498: ny = 0;
5499: cy = py - yy;
5500: nd = oldy - cy + 1;
5501: }
5502: while (ny > 0) {
5503: window_copy_cursor_up(wme, 1);
5504: ny--;
5505: }
5506: window_copy_update_cursor(wme, px, cy);
5507: if (window_copy_update_selection(wme, 1, 0))
5508: window_copy_redraw_lines(wme, cy, nd);
5509: }
5510:
5511: /* Scroll down if the cursor went off the visible screen. */
5512: static void
5513: window_copy_acquire_cursor_down(struct window_mode_entry *wme, u_int hsize,
5514: u_int sy, u_int oy, u_int oldy, u_int px, u_int py, int no_reset)
5515: {
5516: u_int cy, yy, ny, nd;
5517:
5518: cy = py - hsize + oy;
5519: yy = sy - 1;
5520: if (cy > yy) {
5521: ny = cy - yy;
5522: oldy = yy;
5523: nd = 1;
5524: } else {
5525: ny = 0;
5526: nd = cy - oldy + 1;
5527: }
5528: while (ny > 0) {
5529: window_copy_cursor_down(wme, 1);
5530: ny--;
5531: }
5532: if (cy > yy)
5533: window_copy_update_cursor(wme, px, yy);
5534: else
5535: window_copy_update_cursor(wme, px, cy);
5536: if (window_copy_update_selection(wme, 1, no_reset))
5537: window_copy_redraw_lines(wme, oldy, nd);
1.42 nicm 5538: }