tty_clamp_area(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py,
    u_int nx, u_int ny, u_int *i, u_int *j, u_int *x, u_int *y, u_int *rx,
    u_int *ry)
{
	struct window_pane	*wp = ctx->wp;
	u_int			 xoff = wp->xoff + px, yoff = wp->yoff + py;

	if (!tty_is_visible(tty, ctx, px, py, nx, ny))
		return (0);

	if (xoff >= ctx->ox && xoff + nx <= ctx->ox + ctx->sx) {
		/* All visible. */
		*i = 0;
		*x = ctx->xoff + px - ctx->ox;
		*rx = nx;
	} else if (xoff < ctx->ox && xoff + nx > ctx->ox + ctx->sx) {
		/* Both left and right not visible. */
		*i = ctx->ox;
		*x = 0;
		*rx = ctx->sx;
	} else if (xoff < ctx->ox) {
		/* Left not visible. */
		*i = ctx->ox - (ctx->xoff + px);
		*x = 0;
		*rx = nx - *i;
	} else {
		/* Right not visible. */
		*i = 0;
		*x = (ctx->xoff + px) - ctx->ox;
		*rx = ctx->sx - *x;
	}
	if (*rx > nx)
		fatalx("%s: x too big, %u > %u", __func__, *rx, nx);

	if (yoff >= ctx->oy && yoff + ny <= ctx->oy + ctx->sy) {
		/* All visible. */
		*j = 0;
		*y = ctx->yoff + py - ctx->oy;
		*ry = ny;
	} else if (yoff < ctx->oy && yoff + ny > ctx->oy + ctx->sy) {
		/* Both left and right not visible. */
		*j = ctx->oy;
		*y = 0;
		*ry = ctx->sy;
	} else if (yoff < ctx->oy) {
		/* Left not visible. */
		*j = ctx->oy - (ctx->yoff + py);
		*y = 0;
		*ry = ny - *j;
	} else {
		/* Right not visible. */
		*j = 0;
		*y = (ctx->yoff + py) - ctx->oy;
		*ry = ctx->sy - *y;
	}
	if (*ry > ny)
		fatalx("%s: y too big, %u > %u", __func__, *ry, ny);

	return (1);
}
tty_clamp_area(struct tty *tty, const struct tty_ctx *ctx, u_int px, u_int py,
    u_int nx, u_int ny, u_int *i, u_int *j, u_int *x, u_int *y, u_int *rx,
    u_int *ry)
{
	struct window_pane	*wp = ctx->wp;
	u_int			 xoff = wp->xoff + px, yoff = wp->yoff + py;

	if (!tty_is_visible(tty, ctx, px, py, nx, ny))
		return (0);

	if (xoff >= ctx->ox && xoff + nx <= ctx->ox + ctx->sx) {
		 
		*i = 0;
		*x = ctx->xoff + px - ctx->ox;
		*rx = nx;
	} else if (xoff < ctx->ox && xoff + nx > ctx->ox + ctx->sx) {
		 
		*i = ctx->ox;
		*x = 0;
		*rx = ctx->sx;
	} else if (xoff < ctx->ox) {
		 
		*i = ctx->ox - (ctx->xoff + px);
		*x = 0;
		*rx = nx - *i;
	} else {
		 
		*i = 0;
		*x = (ctx->xoff + px) - ctx->ox;
		*rx = ctx->sx - *x;
	}
	if (*rx > nx)
		fatalx("%s: x too big, %u > %u", __func__, *rx, nx);

	if (yoff >= ctx->oy && yoff + ny <= ctx->oy + ctx->sy) {
		 
		*j = 0;
		*y = ctx->yoff + py - ctx->oy;
		*ry = ny;
	} else if (yoff < ctx->oy && yoff + ny > ctx->oy + ctx->sy) {
		 
		*j = ctx->oy;
		*y = 0;
		*ry = ctx->sy;
	} else if (yoff < ctx->oy) {
		 
		*j = ctx->oy - (ctx->yoff + py);
		*y = 0;
		*ry = ny - *j;
	} else {
		 
		*j = 0;
		*y = (ctx->yoff + py) - ctx->oy;
		*ry = ctx->sy - *y;
	}
	if (*ry > ny)
		fatalx("%s: y too big, %u > %u", __func__, *ry, ny);

	return (1);
}