void window_copy_cursor_down(struct window_pane *wp, int scroll_only) { struct window_copy_mode_data *data = wp->modedata; struct screen *s = &data->screen; u_int ox, oy, px, py; oy = screen_hsize(&wp->base) + data->cy - data->oy; ox = window_copy_find_length(wp, oy); if (ox != 0) { data->lastcx = data->cx; data->lastsx = ox; } data->cx = data->lastcx; if (scroll_only || data->cy == screen_size_y(s) - 1) { window_copy_scroll_up(wp, 1); if (scroll_only && data->cy > 0) window_copy_redraw_lines(wp, data->cy - 1, 2); } else { window_copy_update_cursor(wp, data->cx, data->cy + 1); if (window_copy_update_selection(wp)) window_copy_redraw_lines(wp, data->cy - 1, 2); } if (!data->screen.sel.flag || !data->rectflag) { py = screen_hsize(&wp->base) + data->cy - data->oy; px = window_copy_find_length(wp, py); if ((data->cx >= data->lastsx && data->cx != px) || data->cx > px) window_copy_cursor_end_of_line(wp); } }
int window_copy_update_selection(struct window_pane *wp) { struct window_copy_mode_data *data = wp->modedata; struct screen *s = &data->screen; struct options *oo = &wp->window->options; struct grid_cell gc; u_int sx, sy, ty, cy; if (!s->sel.flag) return (0); /* Set colours. */ memcpy(&gc, &grid_default_cell, sizeof gc); colour_set_fg(&gc, options_get_number(oo, "mode-fg")); colour_set_bg(&gc, options_get_number(oo, "mode-bg")); gc.attr |= options_get_number(oo, "mode-attr"); /* Find top of screen. */ ty = screen_hsize(&wp->base) - data->oy; /* Adjust the selection. */ sx = data->selx; sy = data->sely; if (sy < ty) { /* above screen */ if (!data->rectflag) sx = 0; sy = 0; } else if (sy > ty + screen_size_y(s) - 1) { /* below screen */ if (!data->rectflag) sx = screen_size_x(s) - 1; sy = screen_size_y(s) - 1; } else sy -= ty; sy = screen_hsize(s) + sy; screen_set_selection(s, sx, sy, data->cx, screen_hsize(s) + data->cy, data->rectflag, &gc); if (data->rectflag) { /* * Can't rely on the caller to redraw the right lines for * rectangle selection - find the highest line and the number * of lines, and redraw just past that in both directions */ cy = data->cy; if (sy < cy) window_copy_redraw_lines(wp, sy, cy - sy + 1); else window_copy_redraw_lines(wp, cy, sy - cy + 1); } return (1); }
void window_copy_redraw_screen(struct window_pane *wp) { struct window_copy_mode_data *data = wp->modedata; window_copy_redraw_lines(wp, 0, screen_size_y(&data->screen)); }
void window_copy_cursor_jump(struct window_pane *wp) { struct window_copy_mode_data *data = wp->modedata; struct screen *back_s = data->backing; const struct grid_cell *gc; uint px, py, xx; px = data->cx + 1; py = screen_hsize(back_s) + data->cy - data->oy; xx = window_copy_find_length(wp, py); while (px < xx) { gc = grid_peek_cell(back_s->grid, px, py); if ((gc->flags & (GRID_FLAG_PADDING|GRID_FLAG_UTF8)) == 0 && gc->data == data->jumpchar) { window_copy_update_cursor(wp, px, data->cy); if (window_copy_update_selection(wp)) window_copy_redraw_lines(wp, data->cy, 1); return; } px++; } }
void window_copy_cursor_end_of_line(struct window_pane *wp) { struct window_copy_mode_data *data = wp->modedata; struct screen *base_s = &wp->base; struct grid *gd = base_s->grid; u_int px, py; py = screen_hsize(base_s) + data->cy - data->oy; px = window_copy_find_length(wp, py); if (data->cx == px) { if (data->screen.sel.flag && data->rectflag) px = screen_size_x(&wp->base); if (gd->linedata[py].flags & GRID_LINE_WRAPPED) { while (py < gd->sy + gd->hsize && gd->linedata[py].flags & GRID_LINE_WRAPPED) { window_copy_cursor_down(wp, 0); py = screen_hsize(base_s) + data->cy - data->oy; } px = window_copy_find_length(wp, py); } } window_copy_update_cursor(wp, px, data->cy); if (window_copy_update_selection(wp)) window_copy_redraw_lines(wp, data->cy, 1); }
void window_copy_cursor_jump_back(struct window_pane *wp) { struct window_copy_mode_data *data = wp->modedata; struct screen *back_s = data->backing; const struct grid_cell *gc; uint px, py; px = data->cx; py = screen_hsize(back_s) + data->cy - data->oy; if (px > 0) px--; for (;;) { gc = grid_peek_cell(back_s->grid, px, py); if ((gc->flags & (GRID_FLAG_PADDING|GRID_FLAG_UTF8)) == 0 && gc->data == data->jumpchar) { window_copy_update_cursor(wp, px, data->cy); if (window_copy_update_selection(wp)) window_copy_redraw_lines(wp, data->cy, 1); return; } if (px == 0) break; px--; } }
void window_copy_vadd(struct window_pane *wp, const char *fmt, va_list ap) { struct window_copy_mode_data *data = wp->modedata; struct screen *backing = data->backing; struct screen_write_ctx back_ctx, ctx; struct grid_cell gc; int utf8flag; u_int old_hsize; if (backing == &wp->base) return; utf8flag = options_get_number(&wp->window->options, "utf8"); memcpy(&gc, &grid_default_cell, sizeof gc); old_hsize = screen_hsize(data->backing); screen_write_start(&back_ctx, NULL, backing); if (data->backing_written) { /* * On the second or later line, do a CRLF before writing * (so it's on a new line). */ screen_write_carriagereturn(&back_ctx); screen_write_linefeed(&back_ctx, 0); } else data->backing_written = 1; screen_write_vnputs(&back_ctx, 0, &gc, utf8flag, fmt, ap); screen_write_stop(&back_ctx); data->oy += screen_hsize(data->backing) - old_hsize; screen_write_start(&ctx, wp, &data->screen); /* * If the history has changed, draw the top line. * (If there's any history at all, it has changed.) */ if (screen_hsize(data->backing)) window_copy_redraw_lines(wp, 0, 1); /* Write the line, if it's visible. */ if (backing->cy + data->oy < screen_size_y(backing)) window_copy_redraw_lines(wp, backing->cy, 1); screen_write_stop(&ctx); }
void window_copy_cursor_start_of_line(struct window_pane *wp) { struct window_copy_mode_data *data = wp->modedata; window_copy_update_cursor(wp, 0, data->cy); if (window_copy_update_selection(wp)) window_copy_redraw_lines(wp, data->cy, 1); }
void window_copy_update_cursor(struct window_pane *wp, u_int cx, u_int cy) { struct window_copy_mode_data *data = wp->modedata; struct screen *s = &data->screen; struct screen_write_ctx ctx; u_int old_cx, old_cy; old_cx = data->cx; old_cy = data->cy; data->cx = cx; data->cy = cy; if (old_cx == screen_size_x(s)) window_copy_redraw_lines(wp, old_cy, 1); if (data->cx == screen_size_x(s)) window_copy_redraw_lines(wp, data->cy, 1); else { screen_write_start(&ctx, wp, NULL); screen_write_cursormove(&ctx, data->cx, data->cy); screen_write_stop(&ctx); } }
void window_copy_cursor_left(struct window_pane *wp) { struct window_copy_mode_data *data = wp->modedata; if (data->cx == 0) { window_copy_cursor_up(wp, 0); window_copy_cursor_end_of_line(wp); } else { window_copy_update_cursor(wp, data->cx - 1, data->cy); if (window_copy_update_selection(wp)) window_copy_redraw_lines(wp, data->cy, 1); } }
int window_copy_key_numeric_prefix(struct window_pane *wp, int key) { struct window_copy_mode_data *data = wp->modedata; struct screen *s = &data->screen; key &= 0xff; if (key < '0' || key > '9') return 1; if (data->numprefix >= 100) /* no more than three digits */ return 0; data->numprefix = data->numprefix * 10 + key - '0'; window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1); return 0; }
void window_copy_cursor_start_of_line(struct window_pane *wp) { struct window_copy_mode_data *data = wp->modedata; struct screen *back_s = data->backing; struct grid *gd = back_s->grid; u_int py; if (data->cx == 0) { py = screen_hsize(back_s) + data->cy - data->oy; while (py > 0 && gd->linedata[py-1].flags & GRID_LINE_WRAPPED) { window_copy_cursor_up(wp, 0); py = screen_hsize(back_s) + data->cy - data->oy; } } window_copy_update_cursor(wp, 0, data->cy); if (window_copy_update_selection(wp)) window_copy_redraw_lines(wp, data->cy, 1); }
void window_copy_cursor_next_word_end(struct window_pane *wp, const char *separators) { struct window_copy_mode_data *data = wp->modedata; struct screen *base_s = &wp->base; u_int px, py, xx, yy; int expected = 1; px = data->cx; py = screen_hsize(base_s) + data->cy - data->oy; xx = window_copy_find_length(wp, py); yy = screen_hsize(base_s) + screen_size_y(base_s) - 1; /* * First skip past any word characters, then any nonword characters. * * expected is initially set to 1 for the former and then 0 for the * latter. */ do { while (px > xx || window_copy_in_set(wp, px, py, separators) == expected) { /* Move down if we're past the end of the line. */ if (px > xx) { if (py == yy) return; window_copy_cursor_down(wp, 0); px = 0; py = screen_hsize(base_s) + data->cy - data->oy; xx = window_copy_find_length(wp, py); } else px++; } expected = !expected; } while (expected == 0); window_copy_update_cursor(wp, px, data->cy); if (window_copy_update_selection(wp)) window_copy_redraw_lines(wp, data->cy, 1); }
void window_copy_cursor_right(struct window_pane *wp) { struct window_copy_mode_data *data = wp->modedata; u_int px, py; if (data->screen.sel.flag && data->rectflag) px = screen_size_x(&data->screen); else { py = screen_hsize(&wp->base) + data->cy - data->oy; px = window_copy_find_length(wp, py); } if (data->cx >= px) { window_copy_cursor_start_of_line(wp); window_copy_cursor_down(wp, 0); } else { window_copy_update_cursor(wp, data->cx + 1, data->cy); if (window_copy_update_selection(wp)) window_copy_redraw_lines(wp, data->cy, 1); } }
/* Move to the previous place where a word begins. */ void window_copy_cursor_previous_word(struct window_pane *wp, const char *separators) { struct window_copy_mode_data *data = wp->modedata; u_int px, py; px = data->cx; py = screen_hsize(&wp->base) + data->cy - data->oy; /* Move back to the previous word character. */ for (;;) { if (px > 0) { px--; if (!window_copy_in_set(wp, px, py, separators)) break; } else { if (data->cy == 0 && (screen_hsize(&wp->base) == 0 || data->oy >= screen_hsize(&wp->base) - 1)) goto out; window_copy_cursor_up(wp, 0); py = screen_hsize(&wp->base) + data->cy - data->oy; px = window_copy_find_length(wp, py); } } /* Move back to the beginning of this word. */ while (px > 0 && !window_copy_in_set(wp, px - 1, py, separators)) px--; out: window_copy_update_cursor(wp, px, data->cy); if (window_copy_update_selection(wp)) window_copy_redraw_lines(wp, data->cy, 1); }
void window_copy_cursor_back_to_indentation(struct window_pane *wp) { struct window_copy_mode_data *data = wp->modedata; u_int px, py, xx; const struct grid_cell *gc; px = 0; py = screen_hsize(&wp->base) + data->cy - data->oy; xx = window_copy_find_length(wp, py); while (px < xx) { gc = grid_peek_cell(wp->base.grid, px, py); if (gc->flags & GRID_FLAG_UTF8) break; if (gc->data != ' ') break; px++; } window_copy_update_cursor(wp, px, data->cy); if (window_copy_update_selection(wp)) window_copy_redraw_lines(wp, data->cy, 1); }
int window_copy_key_input(struct window_pane *wp, int key) { struct window_copy_mode_data *data = wp->modedata; struct screen *s = &data->screen; size_t inputlen; u_int np; switch (mode_key_lookup(&data->mdata, key)) { case MODEKEYEDIT_CANCEL: data->numprefix = 0; return (-1); case MODEKEYEDIT_BACKSPACE: inputlen = strlen(data->inputstr); if (inputlen > 0) data->inputstr[inputlen - 1] = '\0'; break; case MODEKEYEDIT_DELETELINE: *data->inputstr = '\0'; break; case MODEKEYEDIT_ENTER: np = data->numprefix; if (np == 0) np = 1; switch (data->inputtype) { case WINDOW_COPY_OFF: case WINDOW_COPY_NUMERICPREFIX: break; case WINDOW_COPY_SEARCHUP: for (; np != 0; np--) window_copy_search_up(wp, data->inputstr); data->searchtype = data->inputtype; data->searchstr = xstrdup(data->inputstr); break; case WINDOW_COPY_SEARCHDOWN: for (; np != 0; np--) window_copy_search_down(wp, data->inputstr); data->searchtype = data->inputtype; data->searchstr = xstrdup(data->inputstr); break; case WINDOW_COPY_GOTOLINE: window_copy_goto_line(wp, data->inputstr); *data->inputstr = '\0'; break; } data->numprefix = 0; return (1); case MODEKEY_OTHER: if (key < 32 || key > 126) break; inputlen = strlen(data->inputstr) + 2; data->inputstr = xrealloc(data->inputstr, 1, inputlen); data->inputstr[inputlen - 2] = key; data->inputstr[inputlen - 1] = '\0'; break; default: break; } window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1); return (0); }
void window_copy_key(struct window_pane *wp, struct client *c, int key) { const char *word_separators; struct window_copy_mode_data *data = wp->modedata; struct screen *s = &data->screen; u_int n, np; int keys; enum mode_key_cmd cmd; np = data->numprefix; if (np == 0) np = 1; if (data->inputtype == WINDOW_COPY_NUMERICPREFIX) { if (window_copy_key_numeric_prefix(wp, key) == 0) return; data->inputtype = WINDOW_COPY_OFF; window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1); } else if (data->inputtype != WINDOW_COPY_OFF) { if (window_copy_key_input(wp, key) != 0) goto input_off; return; } cmd = mode_key_lookup(&data->mdata, key); switch (cmd) { case MODEKEYCOPY_CANCEL: for (; np != 0; np--) window_pane_reset_mode(wp); break; case MODEKEYCOPY_LEFT: for (; np != 0; np--) window_copy_cursor_left(wp); break; case MODEKEYCOPY_RIGHT: for (; np != 0; np--) window_copy_cursor_right(wp); break; case MODEKEYCOPY_UP: for (; np != 0; np--) window_copy_cursor_up(wp, 0); break; case MODEKEYCOPY_DOWN: for (; np != 0; np--) window_copy_cursor_down(wp, 0); break; case MODEKEYCOPY_SCROLLUP: for (; np != 0; np--) window_copy_cursor_up(wp, 1); break; case MODEKEYCOPY_SCROLLDOWN: for (; np != 0; np--) window_copy_cursor_down(wp, 1); break; case MODEKEYCOPY_PREVIOUSPAGE: for (; np != 0; np--) window_copy_pageup(wp); break; case MODEKEYCOPY_NEXTPAGE: n = 1; if (screen_size_y(s) > 2) n = screen_size_y(s) - 2; for (; np != 0; np--) { if (data->oy < n) data->oy = 0; else data->oy -= n; } window_copy_update_selection(wp); window_copy_redraw_screen(wp); break; case MODEKEYCOPY_HALFPAGEUP: n = screen_size_y(s) / 2; for (; np != 0; np--) { if (data->oy + n > screen_hsize(&wp->base)) data->oy = screen_hsize(&wp->base); else data->oy += n; } window_copy_update_selection(wp); window_copy_redraw_screen(wp); break; case MODEKEYCOPY_HALFPAGEDOWN: n = screen_size_y(s) / 2; for (; np != 0; np--) { if (data->oy < n) data->oy = 0; else data->oy -= n; } window_copy_update_selection(wp); window_copy_redraw_screen(wp); break; case MODEKEYCOPY_TOPLINE: data->cx = 0; data->cy = 0; window_copy_update_selection(wp); window_copy_redraw_screen(wp); break; case MODEKEYCOPY_MIDDLELINE: data->cx = 0; data->cy = (screen_size_y(s) - 1) / 2; window_copy_update_selection(wp); window_copy_redraw_screen(wp); break; case MODEKEYCOPY_BOTTOMLINE: data->cx = 0; data->cy = screen_size_y(s) - 1; window_copy_update_selection(wp); window_copy_redraw_screen(wp); break; case MODEKEYCOPY_HISTORYTOP: data->cx = 0; data->cy = 0; data->oy = screen_hsize(&wp->base); window_copy_update_selection(wp); window_copy_redraw_screen(wp); break; case MODEKEYCOPY_HISTORYBOTTOM: data->cx = 0; data->cy = screen_size_y(s) - 1; data->oy = 0; window_copy_update_selection(wp); window_copy_redraw_screen(wp); break; case MODEKEYCOPY_STARTSELECTION: window_copy_start_selection(wp); window_copy_redraw_screen(wp); break; case MODEKEYCOPY_CLEARSELECTION: window_copy_clear_selection(wp); window_copy_redraw_screen(wp); break; case MODEKEYCOPY_COPYSELECTION: if (c != NULL && c->session != NULL) { window_copy_copy_selection(wp, c); window_pane_reset_mode(wp); } break; case MODEKEYCOPY_STARTOFLINE: window_copy_cursor_start_of_line(wp); break; case MODEKEYCOPY_BACKTOINDENTATION: window_copy_cursor_back_to_indentation(wp); break; case MODEKEYCOPY_ENDOFLINE: window_copy_cursor_end_of_line(wp); break; case MODEKEYCOPY_NEXTSPACE: for (; np != 0; np--) window_copy_cursor_next_word(wp, " "); break; case MODEKEYCOPY_NEXTSPACEEND: for (; np != 0; np--) window_copy_cursor_next_word_end(wp, " "); break; case MODEKEYCOPY_NEXTWORD: word_separators = options_get_string(&wp->window->options, "word-separators"); for (; np != 0; np--) window_copy_cursor_next_word(wp, word_separators); break; case MODEKEYCOPY_NEXTWORDEND: word_separators = options_get_string(&wp->window->options, "word-separators"); for (; np != 0; np--) window_copy_cursor_next_word_end(wp, word_separators); break; case MODEKEYCOPY_PREVIOUSSPACE: for (; np != 0; np--) window_copy_cursor_previous_word(wp, " "); break; case MODEKEYCOPY_PREVIOUSWORD: word_separators = options_get_string(&wp->window->options, "word-separators"); for (; np != 0; np--) window_copy_cursor_previous_word(wp, word_separators); break; case MODEKEYCOPY_SEARCHUP: data->inputtype = WINDOW_COPY_SEARCHUP; data->inputprompt = "Search Up"; goto input_on; case MODEKEYCOPY_SEARCHDOWN: data->inputtype = WINDOW_COPY_SEARCHDOWN; data->inputprompt = "Search Down"; goto input_on; case MODEKEYCOPY_SEARCHAGAIN: case MODEKEYCOPY_SEARCHREVERSE: switch (data->searchtype) { case WINDOW_COPY_OFF: case WINDOW_COPY_GOTOLINE: case WINDOW_COPY_NUMERICPREFIX: break; case WINDOW_COPY_SEARCHUP: if (cmd == MODEKEYCOPY_SEARCHAGAIN) { for (; np != 0; np--) { window_copy_search_up( wp, data->searchstr); } } else { for (; np != 0; np--) { window_copy_search_down( wp, data->searchstr); } } break; case WINDOW_COPY_SEARCHDOWN: if (cmd == MODEKEYCOPY_SEARCHAGAIN) { for (; np != 0; np--) { window_copy_search_down( wp, data->searchstr); } } else { for (; np != 0; np--) { window_copy_search_up( wp, data->searchstr); } } break; } break; case MODEKEYCOPY_GOTOLINE: data->inputtype = WINDOW_COPY_GOTOLINE; data->inputprompt = "Goto Line"; *data->inputstr = '\0'; goto input_on; case MODEKEYCOPY_STARTNUMBERPREFIX: key &= 0xff; if (key >= '0' && key <= '9') { data->inputtype = WINDOW_COPY_NUMERICPREFIX; data->numprefix = 0; window_copy_key_numeric_prefix(wp, key); return; } break; case MODEKEYCOPY_RECTANGLETOGGLE: window_copy_rectangle_toggle(wp); break; default: break; } data->numprefix = 0; return; input_on: keys = options_get_number(&wp->window->options, "mode-keys"); if (keys == MODEKEY_EMACS) mode_key_init(&data->mdata, &mode_key_tree_emacs_edit); else mode_key_init(&data->mdata, &mode_key_tree_vi_edit); window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1); return; input_off: keys = options_get_number(&wp->window->options, "mode-keys"); if (keys == MODEKEY_EMACS) mode_key_init(&data->mdata, &mode_key_tree_emacs_copy); else mode_key_init(&data->mdata, &mode_key_tree_vi_copy); data->inputtype = WINDOW_COPY_OFF; data->inputprompt = NULL; window_copy_redraw_lines(wp, screen_size_y(s) - 1, 1); }