From 7ab0b466fe38835cc92f28fed490d7f6ca1a3b5c Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Thu, 22 Nov 2007 19:17:01 +0000 Subject: [PATCH] Text selection with C-space. --- screen.c | 33 ++++++++++++++++++++- tmux.h | 16 +++++++++- window-copy.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 123 insertions(+), 8 deletions(-) diff --git a/screen.c b/screen.c index 2e1f4a86..c829d062 100644 --- a/screen.c +++ b/screen.c @@ -1,4 +1,4 @@ -/* $Id: screen.c,v 1.37 2007-11-22 18:09:43 nicm Exp $ */ +/* $Id: screen.c,v 1.38 2007-11-22 19:17:01 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -278,6 +278,8 @@ screen_draw_start(struct screen_draw_ctx *ctx, ctx->cx = s->cx; ctx->cy = s->cy; + memset(&ctx->sel, 0, sizeof ctx->sel); + ctx->attr = s->attr; ctx->colr = s->colr; @@ -285,6 +287,32 @@ screen_draw_start(struct screen_draw_ctx *ctx, input_store_zero(b, CODE_CURSOROFF); } +/* Check if cell in selection. */ +int +screen_check_selection(struct screen_draw_ctx *ctx, u_int px, u_int py) +{ + struct screen_draw_sel *sel = &ctx->sel; + u_int xx, yy; + + if (!sel->flag) + return (0); + + if (sel->ey < sel->sy) { + xx = sel->sx; + yy = sel->sy; + sel->sx = sel->ex; + sel->sy = sel->ey; + sel->ex = xx; + sel->ey = yy; + } + + if (py < sel->sy || py > sel->ey) + return (0); + if ((py == sel->sy && px < sel->sx) || (py == sel->ey && px > sel->ex)) + return (0); + return (1); +} + /* Get cell data during drawing. */ void screen_draw_get_cell(struct screen_draw_ctx *ctx, @@ -297,6 +325,9 @@ screen_draw_get_cell(struct screen_draw_ctx *ctx, cy = screen_y(s, py) - ctx->oy; screen_get_cell(s, cx, cy, data, attr, colr); + + if (screen_check_selection(ctx, cx, cy)) + *attr |= ATTR_REVERSE; } /* Finalise drawing. */ diff --git a/tmux.h b/tmux.h index c55d7e08..0107858e 100644 --- a/tmux.h +++ b/tmux.h @@ -1,4 +1,4 @@ -/* $Id: tmux.h,v 1.94 2007-11-22 18:09:43 nicm Exp $ */ +/* $Id: tmux.h,v 1.95 2007-11-22 19:17:01 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -380,6 +380,17 @@ struct screen { int mode; }; +/* Screen redraw selection. */ +struct screen_draw_sel { + int flag; + + u_int sx; + u_int sy; + + u_int ex; + u_int ey; +}; + /* Screen redraw context. */ struct screen_draw_ctx { struct screen *s; @@ -393,6 +404,8 @@ struct screen_draw_ctx { u_int attr; u_int colr; + + struct screen_draw_sel sel; }; /* Screen display access macros. */ @@ -774,6 +787,7 @@ void screen_set_cell(struct screen *, u_int, u_int, u_char, u_char, u_char); void screen_draw_start(struct screen_draw_ctx *, struct screen *, struct buffer *, u_int, u_int); void screen_draw_stop(struct screen_draw_ctx *); +int screen_check_selection(struct screen_draw_ctx *, u_int, u_int); void screen_draw_get_cell(struct screen_draw_ctx *, u_int, u_int, u_char *, u_char *, u_char *); void screen_draw_move(struct screen_draw_ctx *, u_int, u_int); diff --git a/window-copy.c b/window-copy.c index 7fb25be5..983bbac2 100644 --- a/window-copy.c +++ b/window-copy.c @@ -1,4 +1,4 @@ -/* $Id: window-copy.c,v 1.1 2007-11-22 18:09:43 nicm Exp $ */ +/* $Id: window-copy.c,v 1.2 2007-11-22 19:17:01 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -35,6 +35,7 @@ void window_copy_cursor_left(struct window *); void window_copy_cursor_right(struct window *); void window_copy_cursor_up(struct window *); void window_copy_cursor_down(struct window *); +void window_copy_draw_lines(struct window *, u_int, u_int); void window_copy_scroll_left(struct window *, u_int); void window_copy_scroll_right(struct window *, u_int); void window_copy_scroll_up(struct window *, u_int); @@ -53,6 +54,8 @@ struct window_copy_mode_data { u_int cx; u_int cy; u_int size; + + struct screen_draw_sel sel; }; void @@ -66,6 +69,8 @@ window_copy_init(struct window *w) data->cy = w->screen.cy; data->size = w->screen.hsize; + memset(&data->sel, 0, sizeof data->sel); + w->screen.mode |= (MODE_BACKGROUND|MODE_BGCURSOR); } @@ -110,6 +115,9 @@ window_copy_draw(struct window *w, struct buffer *b, u_int py, u_int ny) } screen_draw_start(&ctx, s, b, data->ox, data->oy); + memcpy(&ctx.sel, &data->sel, sizeof ctx.sel); + ctx.sel.ex = data->cx + data->ox; + ctx.sel.ey = data->size + data->cy - data->oy; if (py != 0) screen_draw_lines(&ctx, py, ny); else if (ny > 1) @@ -175,6 +183,12 @@ window_copy_key(struct window *w, int key) else data->oy -= sy; break; + case '\000': /* C-space */ + data->sel.flag = 1; + data->sel.sx = data->cx + data->ox; + data->sel.sy = data->size + data->cy - data->oy; + oy = -1; /* XXX */ + break; /* XXX start/end of line, next word, prev word */ } if (data->oy != oy) { @@ -222,9 +236,12 @@ window_copy_cursor_left(struct window *w) return; if (data->cx == 0) - window_copy_scroll_right(w, 1); - else + window_copy_scroll_right(w, 1); + else { data->cx--; + if (data->sel.flag) + window_copy_draw_lines(w, data->cy, 1); + } window_copy_move_cursor(w); } @@ -239,8 +256,11 @@ window_copy_cursor_right(struct window *w) if (data->cx == screen_last_x(s)) window_copy_scroll_left(w, 1); - else + else { data->cx++; + if (data->sel.flag) + window_copy_draw_lines(w, data->cy, 1); + } window_copy_move_cursor(w); } @@ -254,8 +274,11 @@ window_copy_cursor_up(struct window *w) if (data->cy == 0) window_copy_scroll_down(w, 1); - else + else { data->cy--; + if (data->sel.flag) + window_copy_draw_lines(w, data->cy, 2); + } window_copy_move_cursor(w); } @@ -270,11 +293,44 @@ window_copy_cursor_down(struct window *w) if (data->cy == screen_last_y(s)) window_copy_scroll_up(w, 1); - else + else { data->cy++; + if (data->sel.flag) + window_copy_draw_lines(w, data->cy - 1, 2); + } window_copy_move_cursor(w); } +void +window_copy_draw_lines(struct window *w, u_int py, u_int ny) +{ + struct client *c; + struct buffer *b; + u_int i; + struct hdr hdr; + size_t size; + + for (i = 0; i < ARRAY_LENGTH(&clients); i++) { + c = ARRAY_ITEM(&clients, i); + if (c == NULL || c->session == NULL) + continue; + if (!session_has(c->session, w)) + continue; + b = c->out; + + buffer_ensure(b, sizeof hdr); + buffer_add(b, sizeof hdr); + size = BUFFER_USED(b); + + window_copy_draw(w, b, py, ny); + + size = BUFFER_USED(b) - size; + hdr.type = MSG_DATA; + hdr.size = size; + memcpy(BUFFER_IN(b) - size - sizeof hdr, &hdr, sizeof hdr); + } +} + void window_copy_scroll_left(struct window *w, u_int nx) { @@ -306,6 +362,9 @@ window_copy_scroll_left(struct window *w, u_int nx) size = BUFFER_USED(b); screen_draw_start(&ctx, s, b, data->ox, data->oy); + memcpy(&ctx.sel, &data->sel, sizeof ctx.sel); + ctx.sel.ex = data->cx + data->ox; + ctx.sel.ey = data->size + data->cy - data->oy; for (j = 1; j < screen_size_y(s); j++) { screen_draw_move(&ctx, 0, j); input_store_one(b, CODE_DELETECHARACTER, nx); @@ -353,6 +412,9 @@ window_copy_scroll_right(struct window *w, u_int nx) size = BUFFER_USED(b); screen_draw_start(&ctx, s, b, data->ox, data->oy); + memcpy(&ctx.sel, &data->sel, sizeof ctx.sel); + ctx.sel.ex = data->cx + data->ox; + ctx.sel.ey = data->size + data->cy - data->oy; for (j = 1; j < screen_size_y(s); j++) { screen_draw_move(&ctx, 0, j); input_store_one(b, CODE_INSERTCHARACTER, nx); @@ -398,10 +460,15 @@ window_copy_scroll_up(struct window *w, u_int ny) size = BUFFER_USED(c->out); screen_draw_start(&ctx, s, c->out, data->ox, data->oy); + memcpy(&ctx.sel, &data->sel, sizeof ctx.sel); + ctx.sel.ex = data->cx + data->ox; + ctx.sel.ey = data->size + data->cy - data->oy; screen_draw_move(&ctx, 0, 0); input_store_one(c->out, CODE_DELETELINE, ny); for (i = 0; i < ny; i++) screen_draw_line(&ctx, screen_last_y(s) - i); + if (data->sel.flag) + screen_draw_line(&ctx, screen_last_y(s) - ny); window_copy_draw_position(w, &ctx); screen_draw_stop(&ctx); @@ -441,6 +508,9 @@ window_copy_scroll_down(struct window *w, u_int ny) size = BUFFER_USED(c->out); screen_draw_start(&ctx, s, c->out, data->ox, data->oy); + memcpy(&ctx.sel, &data->sel, sizeof ctx.sel); + ctx.sel.ex = data->cx + data->ox; + ctx.sel.ey = data->size + data->cy - data->oy; screen_draw_move(&ctx, 0, 0); input_store_one(c->out, CODE_INSERTLINE, ny); for (i = 1; i < ny + 1; i++)