Text selection with C-space.

This commit is contained in:
Nicholas Marriott 2007-11-22 19:17:01 +00:00
parent 18d72e6928
commit 7ab0b466fe
3 changed files with 123 additions and 8 deletions

View File

@ -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 <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -278,6 +278,8 @@ screen_draw_start(struct screen_draw_ctx *ctx,
ctx->cx = s->cx; ctx->cx = s->cx;
ctx->cy = s->cy; ctx->cy = s->cy;
memset(&ctx->sel, 0, sizeof ctx->sel);
ctx->attr = s->attr; ctx->attr = s->attr;
ctx->colr = s->colr; ctx->colr = s->colr;
@ -285,6 +287,32 @@ screen_draw_start(struct screen_draw_ctx *ctx,
input_store_zero(b, CODE_CURSOROFF); 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. */ /* Get cell data during drawing. */
void void
screen_draw_get_cell(struct screen_draw_ctx *ctx, 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; cy = screen_y(s, py) - ctx->oy;
screen_get_cell(s, cx, cy, data, attr, colr); screen_get_cell(s, cx, cy, data, attr, colr);
if (screen_check_selection(ctx, cx, cy))
*attr |= ATTR_REVERSE;
} }
/* Finalise drawing. */ /* Finalise drawing. */

16
tmux.h
View File

@ -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 <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -380,6 +380,17 @@ struct screen {
int mode; 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. */ /* Screen redraw context. */
struct screen_draw_ctx { struct screen_draw_ctx {
struct screen *s; struct screen *s;
@ -393,6 +404,8 @@ struct screen_draw_ctx {
u_int attr; u_int attr;
u_int colr; u_int colr;
struct screen_draw_sel sel;
}; };
/* Screen display access macros. */ /* 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 *, void screen_draw_start(struct screen_draw_ctx *,
struct screen *, struct buffer *, u_int, u_int); struct screen *, struct buffer *, u_int, u_int);
void screen_draw_stop(struct screen_draw_ctx *); 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 *, void screen_draw_get_cell(struct screen_draw_ctx *,
u_int, u_int, u_char *, u_char *, u_char *); u_int, u_int, u_char *, u_char *, u_char *);
void screen_draw_move(struct screen_draw_ctx *, u_int, u_int); void screen_draw_move(struct screen_draw_ctx *, u_int, u_int);

View File

@ -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 <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -35,6 +35,7 @@ void window_copy_cursor_left(struct window *);
void window_copy_cursor_right(struct window *); void window_copy_cursor_right(struct window *);
void window_copy_cursor_up(struct window *); void window_copy_cursor_up(struct window *);
void window_copy_cursor_down(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_left(struct window *, u_int);
void window_copy_scroll_right(struct window *, u_int); void window_copy_scroll_right(struct window *, u_int);
void window_copy_scroll_up(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 cx;
u_int cy; u_int cy;
u_int size; u_int size;
struct screen_draw_sel sel;
}; };
void void
@ -66,6 +69,8 @@ window_copy_init(struct window *w)
data->cy = w->screen.cy; data->cy = w->screen.cy;
data->size = w->screen.hsize; data->size = w->screen.hsize;
memset(&data->sel, 0, sizeof data->sel);
w->screen.mode |= (MODE_BACKGROUND|MODE_BGCURSOR); 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); 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) if (py != 0)
screen_draw_lines(&ctx, py, ny); screen_draw_lines(&ctx, py, ny);
else if (ny > 1) else if (ny > 1)
@ -175,6 +183,12 @@ window_copy_key(struct window *w, int key)
else else
data->oy -= sy; data->oy -= sy;
break; 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 */ /* XXX start/end of line, next word, prev word */
} }
if (data->oy != oy) { if (data->oy != oy) {
@ -222,9 +236,12 @@ window_copy_cursor_left(struct window *w)
return; return;
if (data->cx == 0) if (data->cx == 0)
window_copy_scroll_right(w, 1); window_copy_scroll_right(w, 1);
else else {
data->cx--; data->cx--;
if (data->sel.flag)
window_copy_draw_lines(w, data->cy, 1);
}
window_copy_move_cursor(w); window_copy_move_cursor(w);
} }
@ -239,8 +256,11 @@ window_copy_cursor_right(struct window *w)
if (data->cx == screen_last_x(s)) if (data->cx == screen_last_x(s))
window_copy_scroll_left(w, 1); window_copy_scroll_left(w, 1);
else else {
data->cx++; data->cx++;
if (data->sel.flag)
window_copy_draw_lines(w, data->cy, 1);
}
window_copy_move_cursor(w); window_copy_move_cursor(w);
} }
@ -254,8 +274,11 @@ window_copy_cursor_up(struct window *w)
if (data->cy == 0) if (data->cy == 0)
window_copy_scroll_down(w, 1); window_copy_scroll_down(w, 1);
else else {
data->cy--; data->cy--;
if (data->sel.flag)
window_copy_draw_lines(w, data->cy, 2);
}
window_copy_move_cursor(w); window_copy_move_cursor(w);
} }
@ -270,11 +293,44 @@ window_copy_cursor_down(struct window *w)
if (data->cy == screen_last_y(s)) if (data->cy == screen_last_y(s))
window_copy_scroll_up(w, 1); window_copy_scroll_up(w, 1);
else else {
data->cy++; data->cy++;
if (data->sel.flag)
window_copy_draw_lines(w, data->cy - 1, 2);
}
window_copy_move_cursor(w); 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 void
window_copy_scroll_left(struct window *w, u_int nx) 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); size = BUFFER_USED(b);
screen_draw_start(&ctx, s, b, data->ox, data->oy); 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++) { for (j = 1; j < screen_size_y(s); j++) {
screen_draw_move(&ctx, 0, j); screen_draw_move(&ctx, 0, j);
input_store_one(b, CODE_DELETECHARACTER, nx); 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); size = BUFFER_USED(b);
screen_draw_start(&ctx, s, b, data->ox, data->oy); 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++) { for (j = 1; j < screen_size_y(s); j++) {
screen_draw_move(&ctx, 0, j); screen_draw_move(&ctx, 0, j);
input_store_one(b, CODE_INSERTCHARACTER, nx); 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); size = BUFFER_USED(c->out);
screen_draw_start(&ctx, s, c->out, data->ox, data->oy); 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); screen_draw_move(&ctx, 0, 0);
input_store_one(c->out, CODE_DELETELINE, ny); input_store_one(c->out, CODE_DELETELINE, ny);
for (i = 0; i < ny; i++) for (i = 0; i < ny; i++)
screen_draw_line(&ctx, screen_last_y(s) - 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); window_copy_draw_position(w, &ctx);
screen_draw_stop(&ctx); screen_draw_stop(&ctx);
@ -441,6 +508,9 @@ window_copy_scroll_down(struct window *w, u_int ny)
size = BUFFER_USED(c->out); size = BUFFER_USED(c->out);
screen_draw_start(&ctx, s, c->out, data->ox, data->oy); 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); screen_draw_move(&ctx, 0, 0);
input_store_one(c->out, CODE_INSERTLINE, ny); input_store_one(c->out, CODE_INSERTLINE, ny);
for (i = 1; i < ny + 1; i++) for (i = 1; i < ny + 1; i++)