2011-07-09 09:42:33 +00:00
|
|
|
/* $Id$ */
|
2007-07-09 19:04:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2010-07-19 18:31:42 +00:00
|
|
|
#include <netdb.h>
|
2009-06-25 15:54:38 +00:00
|
|
|
#include <stdlib.h>
|
2007-07-09 19:04:12 +00:00
|
|
|
#include <string.h>
|
2010-04-06 22:01:32 +00:00
|
|
|
#include <unistd.h>
|
2007-07-09 19:04:12 +00:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2008-09-25 20:08:57 +00:00
|
|
|
void screen_resize_x(struct screen *, u_int);
|
|
|
|
void screen_resize_y(struct screen *, u_int);
|
2007-07-09 19:04:12 +00:00
|
|
|
|
|
|
|
/* Create a new screen. */
|
|
|
|
void
|
2008-09-25 20:08:57 +00:00
|
|
|
screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit)
|
2007-07-09 19:04:12 +00:00
|
|
|
{
|
2010-04-06 22:01:32 +00:00
|
|
|
char hn[MAXHOSTNAMELEN];
|
|
|
|
|
2008-09-25 20:08:57 +00:00
|
|
|
s->grid = grid_create(sx, sy, hlimit);
|
2007-07-09 19:04:12 +00:00
|
|
|
|
2010-04-06 22:01:32 +00:00
|
|
|
if (gethostname(hn, MAXHOSTNAMELEN) == 0)
|
|
|
|
s->title = xstrdup(hn);
|
|
|
|
else
|
|
|
|
s->title = xstrdup("");
|
2007-07-09 19:04:12 +00:00
|
|
|
|
2011-05-22 16:26:09 +00:00
|
|
|
s->cstyle = 0;
|
2011-05-22 16:25:02 +00:00
|
|
|
s->ccolour = xstrdup("");
|
2009-06-25 15:54:38 +00:00
|
|
|
s->tabs = NULL;
|
|
|
|
|
2008-09-25 20:08:57 +00:00
|
|
|
screen_reinit(s);
|
2007-07-09 19:04:12 +00:00
|
|
|
}
|
|
|
|
|
2008-06-29 07:04:31 +00:00
|
|
|
/* Reinitialise screen. */
|
|
|
|
void
|
2008-09-10 19:15:06 +00:00
|
|
|
screen_reinit(struct screen *s)
|
2008-06-29 07:04:31 +00:00
|
|
|
{
|
|
|
|
s->cx = 0;
|
|
|
|
s->cy = 0;
|
|
|
|
|
|
|
|
s->rupper = 0;
|
2008-09-25 20:08:57 +00:00
|
|
|
s->rlower = screen_size_y(s) - 1;
|
2008-06-29 07:04:31 +00:00
|
|
|
|
2010-04-05 05:11:44 +00:00
|
|
|
s->mode = MODE_CURSOR | MODE_WRAP;
|
2009-12-04 22:14:47 +00:00
|
|
|
|
2009-06-25 15:54:38 +00:00
|
|
|
screen_reset_tabs(s);
|
2008-06-29 07:04:31 +00:00
|
|
|
|
2009-07-08 18:02:31 +00:00
|
|
|
grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy);
|
2008-09-26 06:45:28 +00:00
|
|
|
|
|
|
|
screen_clear_selection(s);
|
2008-06-29 07:04:31 +00:00
|
|
|
}
|
|
|
|
|
2008-09-10 19:15:06 +00:00
|
|
|
/* Destroy a screen. */
|
|
|
|
void
|
|
|
|
screen_free(struct screen *s)
|
|
|
|
{
|
2012-07-11 19:34:16 +00:00
|
|
|
free(s->tabs);
|
|
|
|
free(s->title);
|
|
|
|
free(s->ccolour);
|
2008-09-25 20:08:57 +00:00
|
|
|
grid_destroy(s->grid);
|
|
|
|
}
|
|
|
|
|
2009-06-25 15:54:38 +00:00
|
|
|
/* Reset tabs to default, eight spaces apart. */
|
|
|
|
void
|
|
|
|
screen_reset_tabs(struct screen *s)
|
|
|
|
{
|
|
|
|
u_int i;
|
|
|
|
|
2012-07-11 19:34:16 +00:00
|
|
|
free(s->tabs);
|
2009-06-25 15:54:38 +00:00
|
|
|
|
|
|
|
if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL)
|
|
|
|
fatal("bit_alloc failed");
|
|
|
|
for (i = 8; i < screen_size_x(s); i += 8)
|
|
|
|
bit_set(s->tabs, i);
|
|
|
|
}
|
|
|
|
|
2011-05-22 16:26:09 +00:00
|
|
|
/* Set screen cursor style. */
|
|
|
|
void
|
|
|
|
screen_set_cursor_style(struct screen *s, u_int style)
|
|
|
|
{
|
2012-03-18 02:11:27 +00:00
|
|
|
if (style <= 6)
|
2011-05-22 16:26:09 +00:00
|
|
|
s->cstyle = style;
|
|
|
|
}
|
|
|
|
|
2011-05-22 16:25:02 +00:00
|
|
|
/* Set screen cursor colour. */
|
|
|
|
void
|
|
|
|
screen_set_cursor_colour(struct screen *s, const char *colour_string)
|
|
|
|
{
|
2012-07-11 19:34:16 +00:00
|
|
|
free(s->ccolour);
|
2011-05-22 16:25:02 +00:00
|
|
|
s->ccolour = xstrdup(colour_string);
|
|
|
|
}
|
|
|
|
|
2008-09-25 20:08:57 +00:00
|
|
|
/* Set screen title. */
|
|
|
|
void
|
|
|
|
screen_set_title(struct screen *s, const char *title)
|
|
|
|
{
|
2012-07-11 19:34:16 +00:00
|
|
|
free(s->title);
|
2014-01-09 14:05:55 +00:00
|
|
|
s->title = xstrdup(title);
|
2008-09-10 19:15:06 +00:00
|
|
|
}
|
|
|
|
|
2007-07-09 19:04:12 +00:00
|
|
|
/* Resize screen. */
|
|
|
|
void
|
2013-02-05 11:08:59 +00:00
|
|
|
screen_resize(struct screen *s, u_int sx, u_int sy, int reflow)
|
2007-07-09 19:04:12 +00:00
|
|
|
{
|
|
|
|
if (sx < 1)
|
|
|
|
sx = 1;
|
|
|
|
if (sy < 1)
|
|
|
|
sy = 1;
|
|
|
|
|
2009-06-25 15:54:38 +00:00
|
|
|
if (sx != screen_size_x(s)) {
|
2008-09-25 20:08:57 +00:00
|
|
|
screen_resize_x(s, sx);
|
2009-06-25 15:54:38 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* It is unclear what should happen to tabs on resize. xterm
|
|
|
|
* seems to try and maintain them, rxvt resets them. Resetting
|
|
|
|
* is simpler and more reliable so let's do that.
|
|
|
|
*/
|
|
|
|
screen_reset_tabs(s);
|
|
|
|
}
|
|
|
|
|
2008-09-25 20:08:57 +00:00
|
|
|
if (sy != screen_size_y(s))
|
|
|
|
screen_resize_y(s, sy);
|
2013-02-05 11:08:59 +00:00
|
|
|
|
|
|
|
if (reflow)
|
|
|
|
screen_reflow(s, sx);
|
2008-09-25 20:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
screen_resize_x(struct screen *s, u_int sx)
|
|
|
|
{
|
2009-03-28 15:43:41 +00:00
|
|
|
struct grid *gd = s->grid;
|
2008-09-25 20:08:57 +00:00
|
|
|
|
2008-09-29 16:58:02 +00:00
|
|
|
if (sx == 0)
|
|
|
|
fatalx("zero size");
|
|
|
|
|
2009-07-12 17:03:11 +00:00
|
|
|
/*
|
|
|
|
* Treat resizing horizontally simply: just ensure the cursor is
|
|
|
|
* on-screen and change the size. Don't bother to truncate any lines -
|
|
|
|
* then the data should be accessible if the size is then incrased.
|
|
|
|
*
|
|
|
|
* The only potential wrinkle is if UTF-8 double-width characters are
|
|
|
|
* left in the last column, but UTF-8 terminals should deal with this
|
|
|
|
* sanely.
|
|
|
|
*/
|
2008-09-25 20:08:57 +00:00
|
|
|
if (s->cx >= sx)
|
|
|
|
s->cx = sx - 1;
|
|
|
|
gd->sx = sx;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
screen_resize_y(struct screen *s, u_int sy)
|
|
|
|
{
|
2009-03-28 15:43:41 +00:00
|
|
|
struct grid *gd = s->grid;
|
2009-06-25 16:02:10 +00:00
|
|
|
u_int needed, available, oldy, i;
|
2007-11-21 13:11:41 +00:00
|
|
|
|
2008-09-29 16:58:02 +00:00
|
|
|
if (sy == 0)
|
|
|
|
fatalx("zero size");
|
2009-06-25 16:02:10 +00:00
|
|
|
oldy = screen_size_y(s);
|
|
|
|
|
2009-12-04 22:14:47 +00:00
|
|
|
/*
|
2009-06-25 16:02:10 +00:00
|
|
|
* When resizing:
|
|
|
|
*
|
|
|
|
* If the height is decreasing, delete lines from the bottom until
|
|
|
|
* hitting the cursor, then push lines from the top into the history.
|
2009-12-04 22:14:47 +00:00
|
|
|
*
|
2009-06-25 16:02:10 +00:00
|
|
|
* When increasing, pull as many lines as possible from the history to
|
|
|
|
* the top, then fill the remaining with blanks at the bottom.
|
|
|
|
*/
|
2008-09-29 16:58:02 +00:00
|
|
|
|
2007-11-21 13:11:41 +00:00
|
|
|
/* Size decreasing. */
|
2009-06-25 16:02:10 +00:00
|
|
|
if (sy < oldy) {
|
|
|
|
needed = oldy - sy;
|
|
|
|
|
|
|
|
/* Delete as many lines as possible from the bottom. */
|
|
|
|
available = oldy - 1 - s->cy;
|
|
|
|
if (available > 0) {
|
|
|
|
if (available > needed)
|
|
|
|
available = needed;
|
|
|
|
grid_view_delete_lines(gd, oldy - available, available);
|
2007-12-06 09:46:23 +00:00
|
|
|
}
|
2009-06-25 16:02:10 +00:00
|
|
|
needed -= available;
|
2007-11-21 13:11:41 +00:00
|
|
|
|
2009-06-25 16:02:10 +00:00
|
|
|
/*
|
2009-07-14 06:40:33 +00:00
|
|
|
* Now just increase the history size, if possible, to take
|
|
|
|
* over the lines which are left. If history is off, delete
|
|
|
|
* lines from the top.
|
|
|
|
*
|
|
|
|
* XXX Should apply history limit?
|
2009-06-25 16:02:10 +00:00
|
|
|
*/
|
2009-07-14 06:40:33 +00:00
|
|
|
available = s->cy;
|
|
|
|
if (gd->flags & GRID_HISTORY)
|
|
|
|
gd->hsize += needed;
|
2009-07-17 18:35:55 +00:00
|
|
|
else if (needed > 0 && available > 0) {
|
2009-07-14 06:40:33 +00:00
|
|
|
if (available > needed)
|
|
|
|
available = needed;
|
|
|
|
grid_view_delete_lines(gd, 0, available);
|
|
|
|
}
|
2009-06-25 16:02:10 +00:00
|
|
|
s->cy -= needed;
|
2009-12-04 22:14:47 +00:00
|
|
|
}
|
2007-12-06 09:46:23 +00:00
|
|
|
|
2007-11-21 13:11:41 +00:00
|
|
|
/* Resize line arrays. */
|
2009-08-09 17:28:24 +00:00
|
|
|
gd->linedata = xrealloc(
|
|
|
|
gd->linedata, gd->hsize + sy, sizeof *gd->linedata);
|
2007-11-21 13:11:41 +00:00
|
|
|
|
|
|
|
/* Size increasing. */
|
2009-06-25 16:02:10 +00:00
|
|
|
if (sy > oldy) {
|
|
|
|
needed = sy - oldy;
|
|
|
|
|
2009-07-14 06:40:33 +00:00
|
|
|
/*
|
|
|
|
* Try to pull as much as possible out of the history, if is
|
|
|
|
* is enabled.
|
|
|
|
*/
|
2009-06-25 16:02:10 +00:00
|
|
|
available = gd->hsize;
|
2009-07-14 06:40:33 +00:00
|
|
|
if (gd->flags & GRID_HISTORY && available > 0) {
|
2009-06-25 16:02:10 +00:00
|
|
|
if (available > needed)
|
|
|
|
available = needed;
|
|
|
|
gd->hsize -= available;
|
|
|
|
s->cy += available;
|
2009-07-14 06:40:33 +00:00
|
|
|
} else
|
|
|
|
available = 0;
|
2009-06-25 16:02:10 +00:00
|
|
|
needed -= available;
|
|
|
|
|
|
|
|
/* Then fill the rest in with blanks. */
|
2009-08-09 17:28:24 +00:00
|
|
|
for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
|
|
|
|
memset(&gd->linedata[i], 0, sizeof gd->linedata[i]);
|
2007-07-09 19:04:12 +00:00
|
|
|
}
|
|
|
|
|
2009-06-25 16:02:10 +00:00
|
|
|
/* Set the new size, and reset the scroll region. */
|
2008-09-25 20:08:57 +00:00
|
|
|
gd->sy = sy;
|
|
|
|
s->rupper = 0;
|
|
|
|
s->rlower = screen_size_y(s) - 1;
|
2007-07-09 19:04:12 +00:00
|
|
|
}
|
|
|
|
|
2007-12-06 09:46:23 +00:00
|
|
|
/* Set selection. */
|
2007-09-29 18:48:04 +00:00
|
|
|
void
|
2010-02-08 18:13:17 +00:00
|
|
|
screen_set_selection(struct screen *s, u_int sx, u_int sy,
|
|
|
|
u_int ex, u_int ey, u_int rectflag, struct grid_cell *gc)
|
2007-09-29 18:48:04 +00:00
|
|
|
{
|
2007-12-06 09:46:23 +00:00
|
|
|
struct screen_sel *sel = &s->sel;
|
2007-11-21 22:20:44 +00:00
|
|
|
|
2008-12-08 16:19:51 +00:00
|
|
|
memcpy(&sel->cell, gc, sizeof sel->cell);
|
2007-12-06 09:46:23 +00:00
|
|
|
sel->flag = 1;
|
2010-02-08 18:13:17 +00:00
|
|
|
sel->rectflag = rectflag;
|
2010-01-05 23:54:53 +00:00
|
|
|
|
2010-02-08 18:13:17 +00:00
|
|
|
sel->sx = sx; sel->sy = sy;
|
|
|
|
sel->ex = ex; sel->ey = ey;
|
2007-12-06 09:46:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear selection. */
|
|
|
|
void
|
|
|
|
screen_clear_selection(struct screen *s)
|
|
|
|
{
|
|
|
|
struct screen_sel *sel = &s->sel;
|
|
|
|
|
|
|
|
sel->flag = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if cell in selection. */
|
|
|
|
int
|
|
|
|
screen_check_selection(struct screen *s, u_int px, u_int py)
|
|
|
|
{
|
|
|
|
struct screen_sel *sel = &s->sel;
|
2014-08-11 22:18:16 +00:00
|
|
|
u_int xx;
|
2007-12-06 09:46:23 +00:00
|
|
|
|
2010-02-08 18:13:17 +00:00
|
|
|
if (!sel->flag)
|
2007-12-06 09:46:23 +00:00
|
|
|
return (0);
|
|
|
|
|
2010-02-08 18:13:17 +00:00
|
|
|
if (sel->rectflag) {
|
|
|
|
if (sel->sy < sel->ey) {
|
|
|
|
/* start line < end line -- downward selection. */
|
|
|
|
if (py < sel->sy || py > sel->ey)
|
|
|
|
return (0);
|
|
|
|
} else if (sel->sy > sel->ey) {
|
|
|
|
/* start line > end line -- upward selection. */
|
|
|
|
if (py > sel->sy || py < sel->ey)
|
|
|
|
return (0);
|
|
|
|
} else {
|
|
|
|
/* starting line == ending line. */
|
|
|
|
if (py != sel->sy)
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Need to include the selection start row, but not the cursor
|
|
|
|
* row, which means the selection changes depending on which
|
|
|
|
* one is on the left.
|
|
|
|
*/
|
|
|
|
if (sel->ex < sel->sx) {
|
|
|
|
/* Cursor (ex) is on the left. */
|
2010-12-11 17:57:28 +00:00
|
|
|
if (px < sel->ex)
|
2010-02-08 18:13:17 +00:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
if (px > sel->sx)
|
|
|
|
return (0);
|
|
|
|
} else {
|
|
|
|
/* Selection start (sx) is on the left. */
|
|
|
|
if (px < sel->sx)
|
|
|
|
return (0);
|
|
|
|
|
2010-12-11 17:57:28 +00:00
|
|
|
if (px > sel->ex)
|
2010-02-08 18:13:17 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Like emacs, keep the top-left-most character, and drop the
|
|
|
|
* bottom-right-most, regardless of copy direction.
|
|
|
|
*/
|
|
|
|
if (sel->sy < sel->ey) {
|
|
|
|
/* starting line < ending line -- downward selection. */
|
|
|
|
if (py < sel->sy || py > sel->ey)
|
|
|
|
return (0);
|
|
|
|
|
2014-08-11 22:18:16 +00:00
|
|
|
if (py == sel->sy && px < sel->sx)
|
2014-09-01 21:50:18 +00:00
|
|
|
return (0);
|
2014-08-11 22:18:16 +00:00
|
|
|
|
|
|
|
if (py == sel->ey && px > sel->ex)
|
2010-02-08 18:13:17 +00:00
|
|
|
return (0);
|
|
|
|
} else if (sel->sy > sel->ey) {
|
|
|
|
/* starting line > ending line -- upward selection. */
|
|
|
|
if (py > sel->sy || py < sel->ey)
|
|
|
|
return (0);
|
|
|
|
|
2014-08-11 22:18:16 +00:00
|
|
|
if (py == sel->ey && px < sel->ex)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
if (sel->modekeys == MODEKEY_EMACS)
|
|
|
|
xx = sel->sx - 1;
|
|
|
|
else
|
|
|
|
xx = sel->sx;
|
|
|
|
if (py == sel->sy && px > xx)
|
2010-02-08 18:13:17 +00:00
|
|
|
return (0);
|
|
|
|
} else {
|
|
|
|
/* starting line == ending line. */
|
|
|
|
if (py != sel->sy)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
if (sel->ex < sel->sx) {
|
|
|
|
/* cursor (ex) is on the left */
|
2014-08-11 22:18:16 +00:00
|
|
|
if (sel->modekeys == MODEKEY_EMACS)
|
|
|
|
xx = sel->sx - 1;
|
|
|
|
else
|
|
|
|
xx = sel->sx;
|
|
|
|
if (px > xx || px < sel->ex)
|
2010-02-08 18:13:17 +00:00
|
|
|
return (0);
|
|
|
|
} else {
|
|
|
|
/* selection start (sx) is on the left */
|
|
|
|
if (px < sel->sx || px > sel->ex)
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
2007-12-06 09:46:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (1);
|
2007-07-09 19:04:12 +00:00
|
|
|
}
|
2013-02-05 11:08:59 +00:00
|
|
|
|
|
|
|
/* Reflow wrapped lines. */
|
|
|
|
void
|
2013-02-10 18:15:30 +00:00
|
|
|
screen_reflow(struct screen *s, u_int new_x)
|
2013-02-05 11:08:59 +00:00
|
|
|
{
|
2013-02-10 18:15:30 +00:00
|
|
|
struct grid *old = s->grid;
|
2013-05-15 15:39:51 +00:00
|
|
|
u_int change;
|
2013-02-05 11:08:59 +00:00
|
|
|
|
2013-02-10 18:15:30 +00:00
|
|
|
s->grid = grid_create(old->sx, old->sy, old->hlimit);
|
2013-05-15 15:39:51 +00:00
|
|
|
|
|
|
|
change = grid_reflow(s->grid, old, new_x);
|
|
|
|
if (change < s->cy)
|
|
|
|
s->cy -= change;
|
|
|
|
else
|
|
|
|
s->cy = 0;
|
2013-02-05 11:08:59 +00:00
|
|
|
}
|