/* $OpenBSD$ */
/*
* Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.com>
*
* 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>
#include <stdlib.h>
#include <string.h>
#include "tmux.h"
/* Format range. */
struct format_range {
u_int index;
struct screen *s;
u_int start;
u_int end;
enum style_range_type type;
u_int argument;
TAILQ_ENTRY(format_range) entry;
};
TAILQ_HEAD(format_ranges, format_range);
/* Does this range match this style? */
static int
format_is_type(struct format_range *fr, struct style *sy)
{
if (fr->type != sy->range_type)
return (0);
if (fr->type == STYLE_RANGE_WINDOW &&
fr->argument != sy->range_argument)
return (0);
return (1);
}
/* Free a range. */
static void
format_free_range(struct format_ranges *frs, struct format_range *fr)
{
TAILQ_REMOVE(frs, fr, entry);
free(fr);
}
/* Fix range positions. */
static void
format_update_ranges(struct format_ranges *frs, struct screen *s, u_int offset,
u_int start, u_int width)
{
struct format_range *fr, *fr1;
if (frs == NULL)
return;
TAILQ_FOREACH_SAFE(fr, frs, entry, fr1) {
if (fr->s != s)
continue;
if (fr->end <= start || fr->start >= start + width) {
format_free_range(frs, fr);
continue;
}
if (fr->start < start)
fr->start = start;
if (fr->end > start + width)
fr->end = start + width;
if (fr->start == fr->end) {
format_free_range(frs, fr);
continue;
}
fr->start -= start;
fr->end -= start;
fr->start += offset;
fr->end += offset;
}
}
/* Draw a part of the format. */
static void
format_draw_put(struct screen_write_ctx *octx, u_int ocx, u_int ocy,
struct screen *s, struct format_ranges *frs, u_int offset, u_int start,
u_int width)
{
/*
* The offset is how far from the cursor on the target screen; start
* and width how much to copy from the source screen.
*/
screen_write_cursormove(octx, ocx + offset, ocy, 0);
screen_write_fast_copy(octx, s, start, 0, width, 1);
format_update_ranges(frs, s, offset, start, width);
}
/* Draw list part of format. */
static void
format_draw_put_list(struct screen_write_ctx *octx,
u_int ocx, u_int ocy, u_int offset, u_int width, struct screen *list,
struct screen *list_left, struct screen *list_right, int focus_start,
int focus_end, struct format_ranges *frs)
{
u_int start, focus_centre;
/* If there is enough space for the list, draw it entirely. */
if (width >= list->cx) {
format_draw_put(octx, ocx, ocy, list, frs, offset, 0, width);
return;
}
/* The list needs to be trimmed. Try to keep the focus visible. */
focus_centre = focus_start + (focus_end - focus_start) / 2;
if (focus_centre < width / 2)
start = 0;
else
start = focus_centre - width / 2;
if (start + width > list->cx)
start = list->cx - width;
/* Draw <> markers at either side if needed. */
if (start != 0 && width > list_left->cx) {
screen_write_cursormove(octx, ocx + offset, ocy, 0);
screen_write_fast_copy(octx, list_left, 0, 0, list_left->cx, 1);
offset += list_left->cx;
start += list_left->cx;
width -= list_left->cx;
}
if (start + width < list->cx && width > list_right->cx) {
screen_write_cursormove(octx, ocx + offset + width - 1, ocy, 0);
screen_write_fast_copy(octx, list_right, 0, 0, list_right->cx,
1);
width -= list_right->cx;
}
/* Draw the list screen itself. */
format_draw_put(octx, ocx, ocy, <