Tidier code by moving mess into functions.

pull/1/head
Nicholas Marriott 2007-11-22 19:40:17 +00:00
parent 7b4556946d
commit 26c8303733
3 changed files with 60 additions and 56 deletions

View File

@ -1,4 +1,4 @@
/* $Id: screen.c,v 1.39 2007-11-22 19:26:20 nicm Exp $ */
/* $Id: screen.c,v 1.40 2007-11-22 19:40:16 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -290,41 +290,43 @@ 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)
/* Set selection. */
void
screen_draw_set_selection(struct screen_draw_ctx *ctx,
int flag, u_int sx, u_int sy, u_int ex, u_int ey)
{
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 (sel->sy == sel->ey && sel->ex < sel->sx) {
xx = sel->sx;
sel->sx = sel->ex;
sel->ex = xx;
sel->flag = flag;
if (!flag)
return;
if (ey < sy || (sy == ey && ex < sx)) {
sel->sx = ex; sel->sy = ey;
sel->ex = sx; sel->ey = sy;
} else {
sel->sx = sx; sel->sy = sy;
sel->ex = ex; sel->ey = ey;
}
}
/* Check if cell in selection. */
int
screen_draw_check_selection(struct screen_draw_ctx *ctx, u_int px, u_int py)
{
struct screen_draw_sel *sel = &ctx->sel;
if (py < sel->sy || py > sel->ey)
return (0);
if (py == sel->sy && py == sel->ey) {
if (px < sel->sx || px > sel->ex)
return (0);
} else {
if (py == sel->sy && px < sel->sx)
return (0);
if (py == sel->ey && px > sel->ex)
return (0);
return (1);
}
if ((py == sel->sy && px < sel->sx) || (py == sel->ey && px > sel->ex))
return (0);
return (1);
}
@ -341,7 +343,7 @@ screen_draw_get_cell(struct screen_draw_ctx *ctx,
screen_get_cell(s, cx, cy, data, attr, colr);
if (screen_check_selection(ctx, cx, cy))
if (screen_draw_check_selection(ctx, cx, cy))
*attr |= ATTR_REVERSE;
}

6
tmux.h
View File

@ -1,4 +1,4 @@
/* $Id: tmux.h,v 1.95 2007-11-22 19:17:01 nicm Exp $ */
/* $Id: tmux.h,v 1.96 2007-11-22 19:40:16 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -787,7 +787,9 @@ 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_set_selection(
struct screen_draw_ctx *, int, u_int, u_int, u_int, u_int);
int screen_draw_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);

View File

@ -1,4 +1,4 @@
/* $Id: window-copy.c,v 1.3 2007-11-22 19:26:20 nicm Exp $ */
/* $Id: window-copy.c,v 1.4 2007-11-22 19:40:17 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -55,7 +55,9 @@ struct window_copy_mode_data {
u_int cy;
u_int size;
struct screen_draw_sel sel;
int selflag;
u_int selx;
u_int sely;
};
void
@ -68,9 +70,8 @@ window_copy_init(struct window *w)
data->cx = w->screen.cx;
data->cy = w->screen.cy;
data->size = w->screen.hsize;
memset(&data->sel, 0, sizeof data->sel);
data->selflag = 0;
w->screen.mode |= (MODE_BACKGROUND|MODE_BGCURSOR);
}
@ -115,9 +116,8 @@ 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;
screen_draw_set_selection(&ctx, data->selflag, data->selx, data->sely,
data->cx + data->ox, data->size + data->cy - data->oy);
if (py != 0)
screen_draw_lines(&ctx, py, ny);
else if (ny > 1)
@ -184,9 +184,9 @@ window_copy_key(struct window *w, int key)
data->oy -= sy;
break;
case '\000': /* C-space */
data->sel.flag = !data->sel.flag;
data->sel.sx = data->cx + data->ox;
data->sel.sy = data->size + data->cy - data->oy;
data->selflag = !data->selflag;
data->selx = data->cx + data->ox;
data->sely = data->size + data->cy - data->oy;
oy = -1; /* XXX */
break;
/* XXX start/end of line, next word, prev word */
@ -239,7 +239,7 @@ window_copy_cursor_left(struct window *w)
window_copy_scroll_right(w, 1);
else {
data->cx--;
if (data->sel.flag)
if (data->selflag)
window_copy_draw_lines(w, data->cy, 1);
}
window_copy_move_cursor(w);
@ -258,7 +258,7 @@ window_copy_cursor_right(struct window *w)
window_copy_scroll_left(w, 1);
else {
data->cx++;
if (data->sel.flag)
if (data->selflag)
window_copy_draw_lines(w, data->cy, 1);
}
window_copy_move_cursor(w);
@ -276,7 +276,7 @@ window_copy_cursor_up(struct window *w)
window_copy_scroll_down(w, 1);
else {
data->cy--;
if (data->sel.flag)
if (data->selflag)
window_copy_draw_lines(w, data->cy, 2);
}
window_copy_move_cursor(w);
@ -295,7 +295,7 @@ window_copy_cursor_down(struct window *w)
window_copy_scroll_up(w, 1);
else {
data->cy++;
if (data->sel.flag)
if (data->selflag)
window_copy_draw_lines(w, data->cy - 1, 2);
}
window_copy_move_cursor(w);
@ -362,9 +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;
screen_draw_set_selection(&ctx,
data->selflag, data->selx, data->sely,
data->cx + data->ox, 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);
@ -412,9 +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;
screen_draw_set_selection(&ctx,
data->selflag, data->selx, data->sely,
data->cx + data->ox, 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);
@ -460,14 +460,14 @@ 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_set_selection(&ctx,
data->selflag, data->selx, data->sely,
data->cx + data->ox, 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)
if (data->selflag)
screen_draw_line(&ctx, screen_last_y(s) - ny);
window_copy_draw_position(w, &ctx);
screen_draw_stop(&ctx);
@ -508,9 +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_set_selection(&ctx,
data->selflag, data->selx, data->sely,
data->cx + data->ox, 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++)