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