2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2009-06-01 22:58:49 +00:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2009-06-04 18:48:24 +00:00
|
|
|
#include <stdlib.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
#include <string.h>
|
2010-04-06 21:35:44 +00:00
|
|
|
#include <unistd.h>
|
2017-06-04 09:02:36 +00:00
|
|
|
#include <vis.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2018-07-31 11:49:26 +00:00
|
|
|
/* Selected area in screen. */
|
|
|
|
struct screen_sel {
|
|
|
|
int hidden;
|
|
|
|
int rectangle;
|
|
|
|
int modekeys;
|
|
|
|
|
|
|
|
u_int sx;
|
|
|
|
u_int sy;
|
|
|
|
|
|
|
|
u_int ex;
|
|
|
|
u_int ey;
|
|
|
|
|
|
|
|
struct grid_cell cell;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Entry on title stack. */
|
2017-10-05 13:29:18 +00:00
|
|
|
struct screen_title_entry {
|
|
|
|
char *text;
|
|
|
|
|
|
|
|
TAILQ_ENTRY(screen_title_entry) entry;
|
|
|
|
};
|
|
|
|
TAILQ_HEAD(screen_titles, screen_title_entry);
|
|
|
|
|
2020-04-22 08:48:44 +00:00
|
|
|
static void screen_resize_y(struct screen *, u_int, int, u_int *);
|
2020-05-16 15:49:20 +00:00
|
|
|
static void screen_reflow(struct screen *, u_int, u_int *, u_int *, int);
|
2016-10-11 13:21:59 +00:00
|
|
|
|
2017-10-05 13:29:18 +00:00
|
|
|
/* Free titles stack. */
|
|
|
|
static void
|
|
|
|
screen_free_titles(struct screen *s)
|
|
|
|
{
|
|
|
|
struct screen_title_entry *title_entry;
|
|
|
|
|
|
|
|
if (s->titles == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while ((title_entry = TAILQ_FIRST(s->titles)) != NULL) {
|
|
|
|
TAILQ_REMOVE(s->titles, title_entry, entry);
|
|
|
|
free(title_entry->text);
|
|
|
|
free(title_entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(s->titles);
|
|
|
|
s->titles = NULL;
|
|
|
|
}
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
/* Create a new screen. */
|
|
|
|
void
|
|
|
|
screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit)
|
|
|
|
{
|
|
|
|
s->grid = grid_create(sx, sy, hlimit);
|
2020-03-31 07:00:34 +00:00
|
|
|
s->saved_grid = NULL;
|
|
|
|
|
2015-08-28 17:11:12 +00:00
|
|
|
s->title = xstrdup("");
|
2017-10-05 13:29:18 +00:00
|
|
|
s->titles = NULL;
|
2020-10-30 11:33:41 +00:00
|
|
|
s->path = NULL;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2021-06-10 07:36:47 +00:00
|
|
|
s->cstyle = SCREEN_CURSOR_DEFAULT;
|
2021-11-03 13:37:17 +00:00
|
|
|
s->default_cstyle = SCREEN_CURSOR_DEFAULT;
|
|
|
|
s->default_mode = 0;
|
2021-11-01 09:34:49 +00:00
|
|
|
s->ccolour = -1;
|
|
|
|
s->default_ccolour = -1;
|
2009-06-04 18:48:24 +00:00
|
|
|
s->tabs = NULL;
|
2018-07-31 11:49:26 +00:00
|
|
|
s->sel = NULL;
|
2009-06-04 18:48:24 +00:00
|
|
|
|
2020-04-18 15:12:28 +00:00
|
|
|
s->write_list = NULL;
|
2022-06-30 09:55:53 +00:00
|
|
|
s->hyperlinks = NULL;
|
2020-04-18 15:12:28 +00:00
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
screen_reinit(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reinitialise screen. */
|
|
|
|
void
|
|
|
|
screen_reinit(struct screen *s)
|
|
|
|
{
|
|
|
|
s->cx = 0;
|
|
|
|
s->cy = 0;
|
|
|
|
|
|
|
|
s->rupper = 0;
|
|
|
|
s->rlower = screen_size_y(s) - 1;
|
|
|
|
|
2022-04-01 10:11:59 +00:00
|
|
|
s->mode = MODE_CURSOR|MODE_WRAP|(s->mode & MODE_CRLF);
|
2021-06-10 07:24:10 +00:00
|
|
|
if (options_get_number(global_options, "extended-keys") == 2)
|
|
|
|
s->mode |= MODE_KEXTENDED;
|
2009-12-03 22:50:09 +00:00
|
|
|
|
2020-03-31 07:00:34 +00:00
|
|
|
if (s->saved_grid != NULL)
|
|
|
|
screen_alternate_off(s, NULL, 0);
|
|
|
|
s->saved_cx = UINT_MAX;
|
|
|
|
s->saved_cy = UINT_MAX;
|
|
|
|
|
2009-06-04 18:48:24 +00:00
|
|
|
screen_reset_tabs(s);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2016-10-13 20:27:27 +00:00
|
|
|
grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy, 8);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
screen_clear_selection(s);
|
2017-10-05 13:29:18 +00:00
|
|
|
screen_free_titles(s);
|
2022-06-30 09:55:53 +00:00
|
|
|
screen_reset_hyperlinks(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset hyperlinks of a screen. */
|
|
|
|
void
|
|
|
|
screen_reset_hyperlinks(struct screen *s)
|
|
|
|
{
|
|
|
|
if (s->hyperlinks == NULL)
|
|
|
|
s->hyperlinks = hyperlinks_init();
|
|
|
|
else
|
|
|
|
hyperlinks_reset(s->hyperlinks);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Destroy a screen. */
|
|
|
|
void
|
|
|
|
screen_free(struct screen *s)
|
|
|
|
{
|
2018-07-31 11:49:26 +00:00
|
|
|
free(s->sel);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(s->tabs);
|
2020-10-30 11:33:41 +00:00
|
|
|
free(s->path);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(s->title);
|
2017-10-05 13:29:18 +00:00
|
|
|
|
2020-04-18 15:12:28 +00:00
|
|
|
if (s->write_list != NULL)
|
|
|
|
screen_write_free_list(s);
|
|
|
|
|
2020-03-31 07:00:34 +00:00
|
|
|
if (s->saved_grid != NULL)
|
|
|
|
grid_destroy(s->saved_grid);
|
2009-06-01 22:58:49 +00:00
|
|
|
grid_destroy(s->grid);
|
2017-10-05 13:29:18 +00:00
|
|
|
|
2022-06-30 09:55:53 +00:00
|
|
|
if (s->hyperlinks != NULL)
|
|
|
|
hyperlinks_free(s->hyperlinks);
|
2017-10-05 13:29:18 +00:00
|
|
|
screen_free_titles(s);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2009-06-04 18:48:24 +00:00
|
|
|
/* Reset tabs to default, eight spaces apart. */
|
|
|
|
void
|
|
|
|
screen_reset_tabs(struct screen *s)
|
|
|
|
{
|
|
|
|
u_int i;
|
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
free(s->tabs);
|
2009-06-04 18:48:24 +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);
|
|
|
|
}
|
|
|
|
|
2021-11-03 13:37:17 +00:00
|
|
|
/* Set screen cursor style and mode. */
|
2011-05-20 19:17:39 +00:00
|
|
|
void
|
2021-11-03 13:37:17 +00:00
|
|
|
screen_set_cursor_style(u_int style, enum screen_cursor_style *cstyle,
|
|
|
|
int *mode)
|
2011-05-20 19:17:39 +00:00
|
|
|
{
|
2021-06-10 07:43:44 +00:00
|
|
|
switch (style) {
|
2021-06-10 07:36:47 +00:00
|
|
|
case 0:
|
2021-11-03 13:37:17 +00:00
|
|
|
*cstyle = SCREEN_CURSOR_DEFAULT;
|
2021-06-10 07:36:47 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2021-11-03 13:37:17 +00:00
|
|
|
*cstyle = SCREEN_CURSOR_BLOCK;
|
|
|
|
*mode |= MODE_CURSOR_BLINKING;
|
2021-06-10 07:36:47 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2021-11-03 13:37:17 +00:00
|
|
|
*cstyle = SCREEN_CURSOR_BLOCK;
|
|
|
|
*mode &= ~MODE_CURSOR_BLINKING;
|
2021-06-10 07:36:47 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
2021-11-03 13:37:17 +00:00
|
|
|
*cstyle = SCREEN_CURSOR_UNDERLINE;
|
|
|
|
*mode |= MODE_CURSOR_BLINKING;
|
2021-06-10 07:36:47 +00:00
|
|
|
break;
|
|
|
|
case 4:
|
2021-11-03 13:37:17 +00:00
|
|
|
*cstyle = SCREEN_CURSOR_UNDERLINE;
|
|
|
|
*mode &= ~MODE_CURSOR_BLINKING;
|
2021-06-10 07:36:47 +00:00
|
|
|
break;
|
|
|
|
case 5:
|
2021-11-03 13:37:17 +00:00
|
|
|
*cstyle = SCREEN_CURSOR_BAR;
|
|
|
|
*mode |= MODE_CURSOR_BLINKING;
|
2021-06-10 07:36:47 +00:00
|
|
|
break;
|
|
|
|
case 6:
|
2021-11-03 13:37:17 +00:00
|
|
|
*cstyle = SCREEN_CURSOR_BAR;
|
|
|
|
*mode &= ~MODE_CURSOR_BLINKING;
|
2021-06-10 07:36:47 +00:00
|
|
|
break;
|
2021-03-12 08:39:17 +00:00
|
|
|
}
|
2011-05-20 19:17:39 +00:00
|
|
|
}
|
|
|
|
|
2011-05-20 19:03:58 +00:00
|
|
|
/* Set screen cursor colour. */
|
|
|
|
void
|
2021-11-01 09:34:49 +00:00
|
|
|
screen_set_cursor_colour(struct screen *s, int colour)
|
2011-05-20 19:03:58 +00:00
|
|
|
{
|
2021-11-01 09:34:49 +00:00
|
|
|
s->ccolour = colour;
|
2011-05-20 19:03:58 +00:00
|
|
|
}
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
/* Set screen title. */
|
2020-02-03 13:46:27 +00:00
|
|
|
int
|
2009-06-01 22:58:49 +00:00
|
|
|
screen_set_title(struct screen *s, const char *title)
|
|
|
|
{
|
2020-02-03 13:46:27 +00:00
|
|
|
if (!utf8_isvalid(title))
|
|
|
|
return (0);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(s->title);
|
2020-02-03 13:46:27 +00:00
|
|
|
s->title = xstrdup(title);
|
|
|
|
return (1);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 11:16:53 +00:00
|
|
|
/* Set screen path. */
|
|
|
|
void
|
|
|
|
screen_set_path(struct screen *s, const char *path)
|
|
|
|
{
|
|
|
|
free(s->path);
|
|
|
|
utf8_stravis(&s->path, path, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
|
|
|
|
}
|
|
|
|
|
2017-10-05 13:29:18 +00:00
|
|
|
/* Push the current title onto the stack. */
|
|
|
|
void
|
|
|
|
screen_push_title(struct screen *s)
|
|
|
|
{
|
|
|
|
struct screen_title_entry *title_entry;
|
|
|
|
|
|
|
|
if (s->titles == NULL) {
|
|
|
|
s->titles = xmalloc(sizeof *s->titles);
|
|
|
|
TAILQ_INIT(s->titles);
|
|
|
|
}
|
|
|
|
title_entry = xmalloc(sizeof *title_entry);
|
|
|
|
title_entry->text = xstrdup(s->title);
|
|
|
|
TAILQ_INSERT_HEAD(s->titles, title_entry, entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pop a title from the stack and set it as the screen title. If the stack is
|
|
|
|
* empty, do nothing.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
screen_pop_title(struct screen *s)
|
|
|
|
{
|
|
|
|
struct screen_title_entry *title_entry;
|
|
|
|
|
|
|
|
if (s->titles == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
title_entry = TAILQ_FIRST(s->titles);
|
|
|
|
if (title_entry != NULL) {
|
|
|
|
screen_set_title(s, title_entry->text);
|
|
|
|
|
|
|
|
TAILQ_REMOVE(s->titles, title_entry, entry);
|
|
|
|
free(title_entry->text);
|
|
|
|
free(title_entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-16 15:49:20 +00:00
|
|
|
/* Resize screen with options. */
|
2009-06-01 22:58:49 +00:00
|
|
|
void
|
2020-04-15 17:50:02 +00:00
|
|
|
screen_resize_cursor(struct screen *s, u_int sx, u_int sy, int reflow,
|
2020-05-16 15:49:20 +00:00
|
|
|
int eat_empty, int cursor)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2020-05-16 15:49:20 +00:00
|
|
|
u_int cx = s->cx, cy = s->grid->hsize + s->cy;
|
2020-04-15 17:50:02 +00:00
|
|
|
|
2020-04-18 17:20:25 +00:00
|
|
|
if (s->write_list != NULL)
|
2020-04-18 15:12:28 +00:00
|
|
|
screen_write_free_list(s);
|
|
|
|
|
2020-04-17 14:06:42 +00:00
|
|
|
log_debug("%s: new size %ux%u, now %ux%u (cursor %u,%u = %u,%u)",
|
|
|
|
__func__, sx, sy, screen_size_x(s), screen_size_y(s), s->cx, s->cy,
|
2020-05-16 15:49:20 +00:00
|
|
|
cx, cy);
|
2020-04-15 17:50:02 +00:00
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
if (sx < 1)
|
|
|
|
sx = 1;
|
|
|
|
if (sy < 1)
|
|
|
|
sy = 1;
|
|
|
|
|
2009-06-04 18:48:24 +00:00
|
|
|
if (sx != screen_size_x(s)) {
|
2019-03-20 19:19:11 +00:00
|
|
|
s->grid->sx = sx;
|
2009-06-04 18:48:24 +00:00
|
|
|
screen_reset_tabs(s);
|
2017-11-02 18:26:38 +00:00
|
|
|
} else
|
|
|
|
reflow = 0;
|
2009-06-04 18:48:24 +00:00
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
if (sy != screen_size_y(s))
|
2020-05-16 15:49:20 +00:00
|
|
|
screen_resize_y(s, sy, eat_empty, &cy);
|
2013-02-05 11:08:59 +00:00
|
|
|
|
|
|
|
if (reflow)
|
2020-05-16 15:49:20 +00:00
|
|
|
screen_reflow(s, sx, &cx, &cy, cursor);
|
2020-04-15 17:50:02 +00:00
|
|
|
|
2020-05-16 15:49:20 +00:00
|
|
|
if (cy >= s->grid->hsize) {
|
|
|
|
s->cx = cx;
|
|
|
|
s->cy = cy - s->grid->hsize;
|
2020-04-15 17:50:02 +00:00
|
|
|
} else {
|
|
|
|
s->cx = 0;
|
|
|
|
s->cy = 0;
|
|
|
|
}
|
2020-05-16 15:49:20 +00:00
|
|
|
|
2020-04-17 14:06:42 +00:00
|
|
|
log_debug("%s: cursor finished at %u,%u = %u,%u", __func__, s->cx,
|
2020-05-16 15:49:20 +00:00
|
|
|
s->cy, cx, cy);
|
2020-04-18 17:20:25 +00:00
|
|
|
|
|
|
|
if (s->write_list != NULL)
|
|
|
|
screen_write_make_list(s);
|
2020-04-15 17:50:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Resize screen. */
|
|
|
|
void
|
|
|
|
screen_resize(struct screen *s, u_int sx, u_int sy, int reflow)
|
|
|
|
{
|
2020-05-16 15:49:20 +00:00
|
|
|
screen_resize_cursor(s, sx, sy, reflow, 1, 1);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 21:29:23 +00:00
|
|
|
static void
|
2020-04-22 08:48:44 +00:00
|
|
|
screen_resize_y(struct screen *s, u_int sy, int eat_empty, u_int *cy)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
|
|
|
struct grid *gd = s->grid;
|
2009-06-24 19:12:44 +00:00
|
|
|
u_int needed, available, oldy, i;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
if (sy == 0)
|
|
|
|
fatalx("zero size");
|
2009-06-24 19:12:44 +00:00
|
|
|
oldy = screen_size_y(s);
|
|
|
|
|
2009-12-03 22:50:09 +00:00
|
|
|
/*
|
2009-06-24 19:12:44 +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-03 22:50:09 +00:00
|
|
|
*
|
2016-09-02 20:57:20 +00:00
|
|
|
* When increasing, pull as many lines as possible from scrolled
|
|
|
|
* history (not explicitly cleared from view) to the top, then fill the
|
|
|
|
* remaining with blanks at the bottom.
|
2009-06-24 19:12:44 +00:00
|
|
|
*/
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
/* Size decreasing. */
|
2009-06-24 19:12:44 +00:00
|
|
|
if (sy < oldy) {
|
|
|
|
needed = oldy - sy;
|
|
|
|
|
|
|
|
/* Delete as many lines as possible from the bottom. */
|
2020-04-15 17:50:02 +00:00
|
|
|
if (eat_empty) {
|
|
|
|
available = oldy - 1 - s->cy;
|
|
|
|
if (available > 0) {
|
|
|
|
if (available > needed)
|
|
|
|
available = needed;
|
|
|
|
grid_view_delete_lines(gd, oldy - available,
|
|
|
|
available, 8);
|
|
|
|
}
|
|
|
|
needed -= available;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2009-06-24 19:12:44 +00:00
|
|
|
/*
|
2009-07-13 10:43:52 +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.
|
2009-06-24 19:12:44 +00:00
|
|
|
*/
|
2009-07-13 10:43:52 +00:00
|
|
|
available = s->cy;
|
2016-09-02 20:57:20 +00:00
|
|
|
if (gd->flags & GRID_HISTORY) {
|
|
|
|
gd->hscrolled += needed;
|
2009-07-13 10:43:52 +00:00
|
|
|
gd->hsize += needed;
|
2016-09-02 20:57:20 +00:00
|
|
|
} else if (needed > 0 && available > 0) {
|
2009-07-13 10:43:52 +00:00
|
|
|
if (available > needed)
|
|
|
|
available = needed;
|
2016-10-13 20:27:27 +00:00
|
|
|
grid_view_delete_lines(gd, 0, available, 8);
|
2020-04-22 08:48:44 +00:00
|
|
|
(*cy) -= available;
|
2009-07-13 10:43:52 +00:00
|
|
|
}
|
2009-12-03 22:50:09 +00:00
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2018-07-04 09:44:07 +00:00
|
|
|
/* Resize line array. */
|
|
|
|
grid_adjust_lines(gd, gd->hsize + sy);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
/* Size increasing. */
|
2009-06-24 19:12:44 +00:00
|
|
|
if (sy > oldy) {
|
|
|
|
needed = sy - oldy;
|
|
|
|
|
2009-07-13 10:43:52 +00:00
|
|
|
/*
|
2016-10-10 21:29:23 +00:00
|
|
|
* Try to pull as much as possible out of scrolled history, if
|
|
|
|
* is is enabled.
|
2009-07-13 10:43:52 +00:00
|
|
|
*/
|
2016-09-02 20:57:20 +00:00
|
|
|
available = gd->hscrolled;
|
2009-07-13 10:43:52 +00:00
|
|
|
if (gd->flags & GRID_HISTORY && available > 0) {
|
2009-06-24 19:12:44 +00:00
|
|
|
if (available > needed)
|
|
|
|
available = needed;
|
2016-09-02 20:57:20 +00:00
|
|
|
gd->hscrolled -= available;
|
2009-06-24 19:12:44 +00:00
|
|
|
gd->hsize -= available;
|
2009-07-13 10:43:52 +00:00
|
|
|
} else
|
|
|
|
available = 0;
|
2009-06-24 19:12:44 +00:00
|
|
|
needed -= available;
|
|
|
|
|
|
|
|
/* Then fill the rest in with blanks. */
|
2009-08-08 13:29:27 +00:00
|
|
|
for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
|
2020-04-15 12:59:20 +00:00
|
|
|
grid_empty_line(gd, i, 8);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2009-06-24 19:12:44 +00:00
|
|
|
/* Set the new size, and reset the scroll region. */
|
2009-06-01 22:58:49 +00:00
|
|
|
gd->sy = sy;
|
|
|
|
s->rupper = 0;
|
|
|
|
s->rlower = screen_size_y(s) - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set selection. */
|
|
|
|
void
|
2010-02-06 17:35:01 +00:00
|
|
|
screen_set_selection(struct screen *s, u_int sx, u_int sy,
|
2018-07-31 11:49:26 +00:00
|
|
|
u_int ex, u_int ey, u_int rectangle, int modekeys, struct grid_cell *gc)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2018-07-31 11:49:26 +00:00
|
|
|
if (s->sel == NULL)
|
|
|
|
s->sel = xcalloc(1, sizeof *s->sel);
|
|
|
|
|
|
|
|
memcpy(&s->sel->cell, gc, sizeof s->sel->cell);
|
|
|
|
s->sel->hidden = 0;
|
|
|
|
s->sel->rectangle = rectangle;
|
|
|
|
s->sel->modekeys = modekeys;
|
|
|
|
|
|
|
|
s->sel->sx = sx;
|
|
|
|
s->sel->sy = sy;
|
|
|
|
s->sel->ex = ex;
|
|
|
|
s->sel->ey = ey;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear selection. */
|
|
|
|
void
|
|
|
|
screen_clear_selection(struct screen *s)
|
|
|
|
{
|
2018-07-31 11:49:26 +00:00
|
|
|
free(s->sel);
|
|
|
|
s->sel = NULL;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 13:38:44 +00:00
|
|
|
/* Hide selection. */
|
|
|
|
void
|
|
|
|
screen_hide_selection(struct screen *s)
|
|
|
|
{
|
2018-07-31 11:49:26 +00:00
|
|
|
if (s->sel != NULL)
|
|
|
|
s->sel->hidden = 1;
|
2016-11-24 13:38:44 +00:00
|
|
|
}
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
/* Check if cell in selection. */
|
|
|
|
int
|
|
|
|
screen_check_selection(struct screen *s, u_int px, u_int py)
|
|
|
|
{
|
2018-07-31 11:49:26 +00:00
|
|
|
struct screen_sel *sel = s->sel;
|
2014-08-11 22:18:16 +00:00
|
|
|
u_int xx;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2018-07-31 11:49:26 +00:00
|
|
|
if (sel == NULL || sel->hidden)
|
2009-06-01 22:58:49 +00:00
|
|
|
return (0);
|
|
|
|
|
2018-07-31 11:49:26 +00:00
|
|
|
if (sel->rectangle) {
|
2010-02-06 17:35:01 +00:00
|
|
|
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:56:01 +00:00
|
|
|
if (px < sel->ex)
|
2010-02-06 17:35:01 +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:56:01 +00:00
|
|
|
if (px > sel->ex)
|
2010-02-06 17:35:01 +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
|
|
|
|
2019-01-15 09:56:31 +00:00
|
|
|
if (sel->modekeys == MODEKEY_EMACS)
|
|
|
|
xx = (sel->ex == 0 ? 0 : sel->ex - 1);
|
|
|
|
else
|
|
|
|
xx = sel->ex;
|
|
|
|
if (py == sel->ey && px > xx)
|
2010-02-06 17:35:01 +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;
|
2017-02-08 16:45:18 +00:00
|
|
|
if (py == sel->sy && (sel->sx == 0 || px > xx))
|
2010-02-06 17:35:01 +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-06 17:35:01 +00:00
|
|
|
return (0);
|
|
|
|
} else {
|
|
|
|
/* selection start (sx) is on the left */
|
2019-01-15 09:56:31 +00:00
|
|
|
if (sel->modekeys == MODEKEY_EMACS)
|
|
|
|
xx = (sel->ex == 0 ? 0 : sel->ex - 1);
|
|
|
|
else
|
|
|
|
xx = sel->ex;
|
|
|
|
if (px < sel->sx || px > xx)
|
2010-02-06 17:35:01 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
2013-02-05 11:08:59 +00:00
|
|
|
|
2016-10-12 13:24:07 +00:00
|
|
|
/* Get selected grid cell. */
|
|
|
|
void
|
|
|
|
screen_select_cell(struct screen *s, struct grid_cell *dst,
|
|
|
|
const struct grid_cell *src)
|
|
|
|
{
|
2018-07-31 11:49:26 +00:00
|
|
|
if (s->sel == NULL || s->sel->hidden)
|
2016-10-12 13:24:07 +00:00
|
|
|
return;
|
|
|
|
|
2018-07-31 11:49:26 +00:00
|
|
|
memcpy(dst, &s->sel->cell, sizeof *dst);
|
2016-10-12 13:24:07 +00:00
|
|
|
|
|
|
|
utf8_copy(&dst->data, &src->data);
|
|
|
|
dst->attr = dst->attr & ~GRID_ATTR_CHARSET;
|
|
|
|
dst->attr |= src->attr & GRID_ATTR_CHARSET;
|
|
|
|
dst->flags = src->flags;
|
|
|
|
}
|
|
|
|
|
2013-02-05 11:08:59 +00:00
|
|
|
/* Reflow wrapped lines. */
|
2016-10-11 13:21:59 +00:00
|
|
|
static void
|
2020-05-16 15:49:20 +00:00
|
|
|
screen_reflow(struct screen *s, u_int new_x, u_int *cx, u_int *cy, int cursor)
|
2013-02-05 11:08:59 +00:00
|
|
|
{
|
2020-04-15 17:50:02 +00:00
|
|
|
u_int wx, wy;
|
2019-03-20 19:19:11 +00:00
|
|
|
|
2020-05-16 15:49:20 +00:00
|
|
|
if (cursor) {
|
|
|
|
grid_wrap_position(s->grid, *cx, *cy, &wx, &wy);
|
|
|
|
log_debug("%s: cursor %u,%u is %u,%u", __func__, *cx, *cy, wx,
|
|
|
|
wy);
|
|
|
|
}
|
2019-03-20 19:19:11 +00:00
|
|
|
|
|
|
|
grid_reflow(s->grid, new_x);
|
|
|
|
|
2020-05-16 15:49:20 +00:00
|
|
|
if (cursor) {
|
|
|
|
grid_unwrap_position(s->grid, cx, cy, wx, wy);
|
|
|
|
log_debug("%s: new cursor is %u,%u", __func__, *cx, *cy);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*cx = 0;
|
|
|
|
*cy = s->grid->hsize;
|
|
|
|
}
|
2013-02-05 11:08:59 +00:00
|
|
|
}
|
2020-03-31 07:00:34 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Enter alternative screen mode. A copy of the visible screen is saved and the
|
|
|
|
* history is not updated.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
screen_alternate_on(struct screen *s, struct grid_cell *gc, int cursor)
|
|
|
|
{
|
|
|
|
u_int sx, sy;
|
|
|
|
|
|
|
|
if (s->saved_grid != NULL)
|
|
|
|
return;
|
|
|
|
sx = screen_size_x(s);
|
|
|
|
sy = screen_size_y(s);
|
|
|
|
|
|
|
|
s->saved_grid = grid_create(sx, sy, 0);
|
|
|
|
grid_duplicate_lines(s->saved_grid, 0, s->grid, screen_hsize(s), sy);
|
|
|
|
if (cursor) {
|
|
|
|
s->saved_cx = s->cx;
|
|
|
|
s->saved_cy = s->cy;
|
|
|
|
}
|
|
|
|
memcpy(&s->saved_cell, gc, sizeof s->saved_cell);
|
|
|
|
|
|
|
|
grid_view_clear(s->grid, 0, 0, sx, sy, 8);
|
|
|
|
|
2020-04-07 13:55:24 +00:00
|
|
|
s->saved_flags = s->grid->flags;
|
2020-03-31 07:00:34 +00:00
|
|
|
s->grid->flags &= ~GRID_HISTORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Exit alternate screen mode and restore the copied grid. */
|
|
|
|
void
|
|
|
|
screen_alternate_off(struct screen *s, struct grid_cell *gc, int cursor)
|
|
|
|
{
|
2021-01-26 09:32:52 +00:00
|
|
|
u_int sx = screen_size_x(s), sy = screen_size_y(s);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the current size is different, temporarily resize to the old size
|
|
|
|
* before copying back.
|
|
|
|
*/
|
|
|
|
if (s->saved_grid != NULL)
|
|
|
|
screen_resize(s, s->saved_grid->sx, s->saved_grid->sy, 1);
|
2020-03-31 07:00:34 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Restore the cursor position and cell. This happens even if not
|
|
|
|
* currently in the alternate screen.
|
|
|
|
*/
|
|
|
|
if (cursor && s->saved_cx != UINT_MAX && s->saved_cy != UINT_MAX) {
|
|
|
|
s->cx = s->saved_cx;
|
|
|
|
s->cy = s->saved_cy;
|
|
|
|
if (gc != NULL)
|
|
|
|
memcpy(gc, &s->saved_cell, sizeof *gc);
|
|
|
|
}
|
|
|
|
|
2021-01-26 09:32:52 +00:00
|
|
|
/* If not in the alternate screen, do nothing more. */
|
|
|
|
if (s->saved_grid == NULL) {
|
|
|
|
if (s->cx > screen_size_x(s) - 1)
|
|
|
|
s->cx = screen_size_x(s) - 1;
|
|
|
|
if (s->cy > screen_size_y(s) - 1)
|
|
|
|
s->cy = screen_size_y(s) - 1;
|
2020-03-31 07:00:34 +00:00
|
|
|
return;
|
2021-01-26 09:32:52 +00:00
|
|
|
}
|
2020-03-31 07:00:34 +00:00
|
|
|
|
|
|
|
/* Restore the saved grid. */
|
2021-01-26 09:32:52 +00:00
|
|
|
grid_duplicate_lines(s->grid, screen_hsize(s), s->saved_grid, 0,
|
|
|
|
s->saved_grid->sy);
|
2020-03-31 07:00:34 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Turn history back on (so resize can use it) and then resize back to
|
|
|
|
* the current size.
|
|
|
|
*/
|
2020-04-07 13:55:24 +00:00
|
|
|
if (s->saved_flags & GRID_HISTORY)
|
|
|
|
s->grid->flags |= GRID_HISTORY;
|
2021-01-26 09:32:52 +00:00
|
|
|
screen_resize(s, sx, sy, 1);
|
2020-03-31 07:00:34 +00:00
|
|
|
|
|
|
|
grid_destroy(s->saved_grid);
|
|
|
|
s->saved_grid = NULL;
|
2021-01-26 09:32:52 +00:00
|
|
|
|
|
|
|
if (s->cx > screen_size_x(s) - 1)
|
|
|
|
s->cx = screen_size_x(s) - 1;
|
|
|
|
if (s->cy > screen_size_y(s) - 1)
|
|
|
|
s->cy = screen_size_y(s) - 1;
|
2020-03-31 07:00:34 +00:00
|
|
|
}
|
2021-06-10 07:43:44 +00:00
|
|
|
|
|
|
|
/* Get mode as a string. */
|
|
|
|
const char *
|
|
|
|
screen_mode_to_string(int mode)
|
|
|
|
{
|
|
|
|
static char tmp[1024];
|
|
|
|
|
|
|
|
if (mode == 0)
|
2022-05-30 12:55:25 +00:00
|
|
|
return ("NONE");
|
2021-06-10 07:43:44 +00:00
|
|
|
if (mode == ALL_MODES)
|
2022-05-30 12:55:25 +00:00
|
|
|
return ("ALL");
|
2021-06-10 07:43:44 +00:00
|
|
|
|
|
|
|
*tmp = '\0';
|
|
|
|
if (mode & MODE_CURSOR)
|
|
|
|
strlcat(tmp, "CURSOR,", sizeof tmp);
|
|
|
|
if (mode & MODE_INSERT)
|
|
|
|
strlcat(tmp, "INSERT,", sizeof tmp);
|
|
|
|
if (mode & MODE_KCURSOR)
|
|
|
|
strlcat(tmp, "KCURSOR,", sizeof tmp);
|
|
|
|
if (mode & MODE_KKEYPAD)
|
|
|
|
strlcat(tmp, "KKEYPAD,", sizeof tmp);
|
|
|
|
if (mode & MODE_WRAP)
|
|
|
|
strlcat(tmp, "WRAP,", sizeof tmp);
|
|
|
|
if (mode & MODE_MOUSE_STANDARD)
|
2021-09-09 06:57:48 +00:00
|
|
|
strlcat(tmp, "MOUSE_STANDARD,", sizeof tmp);
|
2021-06-10 07:43:44 +00:00
|
|
|
if (mode & MODE_MOUSE_BUTTON)
|
2021-09-09 06:57:48 +00:00
|
|
|
strlcat(tmp, "MOUSE_BUTTON,", sizeof tmp);
|
2021-10-05 12:46:02 +00:00
|
|
|
if (mode & MODE_CURSOR_BLINKING)
|
|
|
|
strlcat(tmp, "CURSOR_BLINKING,", sizeof tmp);
|
|
|
|
if (mode & MODE_CURSOR_VERY_VISIBLE)
|
|
|
|
strlcat(tmp, "CURSOR_VERY_VISIBLE,", sizeof tmp);
|
2021-06-10 07:43:44 +00:00
|
|
|
if (mode & MODE_MOUSE_UTF8)
|
|
|
|
strlcat(tmp, "UTF8,", sizeof tmp);
|
|
|
|
if (mode & MODE_MOUSE_SGR)
|
|
|
|
strlcat(tmp, "SGR,", sizeof tmp);
|
|
|
|
if (mode & MODE_BRACKETPASTE)
|
|
|
|
strlcat(tmp, "BRACKETPASTE,", sizeof tmp);
|
|
|
|
if (mode & MODE_FOCUSON)
|
|
|
|
strlcat(tmp, "FOCUSON,", sizeof tmp);
|
|
|
|
if (mode & MODE_MOUSE_ALL)
|
2021-09-09 06:57:48 +00:00
|
|
|
strlcat(tmp, "MOUSE_ALL,", sizeof tmp);
|
2021-06-10 07:43:44 +00:00
|
|
|
if (mode & MODE_ORIGIN)
|
|
|
|
strlcat(tmp, "ORIGIN,", sizeof tmp);
|
|
|
|
if (mode & MODE_CRLF)
|
|
|
|
strlcat(tmp, "CRLF,", sizeof tmp);
|
|
|
|
if (mode & MODE_KEXTENDED)
|
|
|
|
strlcat(tmp, "KEXTENDED,", sizeof tmp);
|
2021-08-20 17:50:42 +00:00
|
|
|
tmp[strlen(tmp) - 1] = '\0';
|
2021-06-10 07:43:44 +00:00
|
|
|
return (tmp);
|
|
|
|
}
|