xref: /openbsd-src/usr.bin/tmux/format-draw.c (revision 5355e038b0b07a74c9a228681497529956e8349e)
1*5355e038Snicm /* $OpenBSD: format-draw.c,v 1.28 2023/08/17 14:10:28 nicm Exp $ */
24ffcb1c8Snicm 
34ffcb1c8Snicm /*
44ffcb1c8Snicm  * Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.com>
54ffcb1c8Snicm  *
64ffcb1c8Snicm  * Permission to use, copy, modify, and distribute this software for any
74ffcb1c8Snicm  * purpose with or without fee is hereby granted, provided that the above
84ffcb1c8Snicm  * copyright notice and this permission notice appear in all copies.
94ffcb1c8Snicm  *
104ffcb1c8Snicm  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
114ffcb1c8Snicm  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
124ffcb1c8Snicm  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
134ffcb1c8Snicm  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
144ffcb1c8Snicm  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
154ffcb1c8Snicm  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
164ffcb1c8Snicm  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
174ffcb1c8Snicm  */
184ffcb1c8Snicm 
194ffcb1c8Snicm #include <sys/types.h>
204ffcb1c8Snicm 
214ffcb1c8Snicm #include <stdlib.h>
224ffcb1c8Snicm #include <string.h>
234ffcb1c8Snicm 
244ffcb1c8Snicm #include "tmux.h"
254ffcb1c8Snicm 
264ffcb1c8Snicm /* Format range. */
274ffcb1c8Snicm struct format_range {
284ffcb1c8Snicm 	u_int				 index;
294ffcb1c8Snicm 	struct screen			*s;
304ffcb1c8Snicm 
314ffcb1c8Snicm 	u_int				 start;
324ffcb1c8Snicm 	u_int				 end;
334ffcb1c8Snicm 
344ffcb1c8Snicm 	enum style_range_type		 type;
354ffcb1c8Snicm 	u_int				 argument;
36*5355e038Snicm 	char                             string[16];
374ffcb1c8Snicm 
384ffcb1c8Snicm 	TAILQ_ENTRY(format_range)	 entry;
394ffcb1c8Snicm };
404ffcb1c8Snicm TAILQ_HEAD(format_ranges, format_range);
414ffcb1c8Snicm 
424ffcb1c8Snicm /* Does this range match this style? */
434ffcb1c8Snicm static int
format_is_type(struct format_range * fr,struct style * sy)444ffcb1c8Snicm format_is_type(struct format_range *fr, struct style *sy)
454ffcb1c8Snicm {
464ffcb1c8Snicm 	if (fr->type != sy->range_type)
474ffcb1c8Snicm 		return (0);
48*5355e038Snicm 	switch (fr->type) {
49*5355e038Snicm 	case STYLE_RANGE_NONE:
50*5355e038Snicm 	case STYLE_RANGE_LEFT:
51*5355e038Snicm 	case STYLE_RANGE_RIGHT:
52*5355e038Snicm 		return (1);
53*5355e038Snicm 	case STYLE_RANGE_PANE:
54*5355e038Snicm 	case STYLE_RANGE_WINDOW:
55*5355e038Snicm 	case STYLE_RANGE_SESSION:
56*5355e038Snicm 		return (fr->argument == sy->range_argument);
57*5355e038Snicm 	case STYLE_RANGE_USER:
58*5355e038Snicm 		return (strcmp(fr->string, sy->range_string) == 0);
59*5355e038Snicm 	}
604ffcb1c8Snicm 	return (1);
614ffcb1c8Snicm }
624ffcb1c8Snicm 
634ffcb1c8Snicm /* Free a range. */
644ffcb1c8Snicm static void
format_free_range(struct format_ranges * frs,struct format_range * fr)654ffcb1c8Snicm format_free_range(struct format_ranges *frs, struct format_range *fr)
664ffcb1c8Snicm {
674ffcb1c8Snicm 	TAILQ_REMOVE(frs, fr, entry);
684ffcb1c8Snicm 	free(fr);
694ffcb1c8Snicm }
704ffcb1c8Snicm 
714ffcb1c8Snicm /* Fix range positions. */
724ffcb1c8Snicm static void
format_update_ranges(struct format_ranges * frs,struct screen * s,u_int offset,u_int start,u_int width)734ffcb1c8Snicm format_update_ranges(struct format_ranges *frs, struct screen *s, u_int offset,
744ffcb1c8Snicm     u_int start, u_int width)
754ffcb1c8Snicm {
764ffcb1c8Snicm 	struct format_range	*fr, *fr1;
774ffcb1c8Snicm 
784ffcb1c8Snicm 	if (frs == NULL)
794ffcb1c8Snicm 		return;
804ffcb1c8Snicm 
814ffcb1c8Snicm 	TAILQ_FOREACH_SAFE(fr, frs, entry, fr1) {
824ffcb1c8Snicm 		if (fr->s != s)
834ffcb1c8Snicm 			continue;
844ffcb1c8Snicm 
854ffcb1c8Snicm 		if (fr->end <= start || fr->start >= start + width) {
864ffcb1c8Snicm 			format_free_range(frs, fr);
874ffcb1c8Snicm 			continue;
884ffcb1c8Snicm 		}
894ffcb1c8Snicm 
904ffcb1c8Snicm 		if (fr->start < start)
914ffcb1c8Snicm 			fr->start = start;
924ffcb1c8Snicm 		if (fr->end > start + width)
934ffcb1c8Snicm 			fr->end = start + width;
944ffcb1c8Snicm 		if (fr->start == fr->end) {
954ffcb1c8Snicm 			format_free_range(frs, fr);
964ffcb1c8Snicm 			continue;
974ffcb1c8Snicm 		}
984ffcb1c8Snicm 
99cff1d32dSnicm 		fr->start -= start;
100cff1d32dSnicm 		fr->end -= start;
101cff1d32dSnicm 
1024ffcb1c8Snicm 		fr->start += offset;
1034ffcb1c8Snicm 		fr->end += offset;
1044ffcb1c8Snicm 	}
1054ffcb1c8Snicm }
1064ffcb1c8Snicm 
1074ffcb1c8Snicm /* Draw a part of the format. */
1084ffcb1c8Snicm 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)1094ffcb1c8Snicm format_draw_put(struct screen_write_ctx *octx, u_int ocx, u_int ocy,
1104ffcb1c8Snicm     struct screen *s, struct format_ranges *frs, u_int offset, u_int start,
1114ffcb1c8Snicm     u_int width)
1124ffcb1c8Snicm {
1134ffcb1c8Snicm 	/*
1144ffcb1c8Snicm 	 * The offset is how far from the cursor on the target screen; start
1154ffcb1c8Snicm 	 * and width how much to copy from the source screen.
1164ffcb1c8Snicm 	 */
1174ffcb1c8Snicm 	screen_write_cursormove(octx, ocx + offset, ocy, 0);
1184ffcb1c8Snicm 	screen_write_fast_copy(octx, s, start, 0, width, 1);
1194ffcb1c8Snicm 	format_update_ranges(frs, s, offset, start, width);
1204ffcb1c8Snicm }
1214ffcb1c8Snicm 
1224ffcb1c8Snicm /* Draw list part of format. */
1234ffcb1c8Snicm 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)1244ffcb1c8Snicm format_draw_put_list(struct screen_write_ctx *octx,
1254ffcb1c8Snicm     u_int ocx, u_int ocy, u_int offset, u_int width, struct screen *list,
1264ffcb1c8Snicm     struct screen *list_left, struct screen *list_right, int focus_start,
1274ffcb1c8Snicm     int focus_end, struct format_ranges *frs)
1284ffcb1c8Snicm {
1294ffcb1c8Snicm 	u_int	start, focus_centre;
1304ffcb1c8Snicm 
1314ffcb1c8Snicm 	/* If there is enough space for the list, draw it entirely. */
1324ffcb1c8Snicm 	if (width >= list->cx) {
1334ffcb1c8Snicm 		format_draw_put(octx, ocx, ocy, list, frs, offset, 0, width);
1344ffcb1c8Snicm 		return;
1354ffcb1c8Snicm 	}
1364ffcb1c8Snicm 
1374ffcb1c8Snicm 	/* The list needs to be trimmed. Try to keep the focus visible. */
1384ffcb1c8Snicm 	focus_centre = focus_start + (focus_end - focus_start) / 2;
1394ffcb1c8Snicm 	if (focus_centre < width / 2)
1404ffcb1c8Snicm 		start = 0;
1414ffcb1c8Snicm 	else
1424ffcb1c8Snicm 		start = focus_centre - width / 2;
1434ffcb1c8Snicm 	if (start + width > list->cx)
1444ffcb1c8Snicm 		start = list->cx - width;
1454ffcb1c8Snicm 
1464ffcb1c8Snicm 	/* Draw <> markers at either side if needed. */
1474ffcb1c8Snicm 	if (start != 0 && width > list_left->cx) {
1484ffcb1c8Snicm 		screen_write_cursormove(octx, ocx + offset, ocy, 0);
1494ffcb1c8Snicm 		screen_write_fast_copy(octx, list_left, 0, 0, list_left->cx, 1);
1504ffcb1c8Snicm 		offset += list_left->cx;
1514ffcb1c8Snicm 		start += list_left->cx;
1524ffcb1c8Snicm 		width -= list_left->cx;
1534ffcb1c8Snicm 	}
1544ffcb1c8Snicm 	if (start + width < list->cx && width > list_right->cx) {
155b19d25a0Snicm 		screen_write_cursormove(octx, ocx + offset + width -
156b19d25a0Snicm 		    list_right->cx, ocy, 0);
1574ffcb1c8Snicm 		screen_write_fast_copy(octx, list_right, 0, 0, list_right->cx,
1584ffcb1c8Snicm 		    1);
1594ffcb1c8Snicm 		width -= list_right->cx;
1604ffcb1c8Snicm 	}
1614ffcb1c8Snicm 
1624ffcb1c8Snicm 	/* Draw the list screen itself. */
1634ffcb1c8Snicm 	format_draw_put(octx, ocx, ocy, list, frs, offset, start, width);
1644ffcb1c8Snicm }
1654ffcb1c8Snicm 
1664ffcb1c8Snicm /* Draw format with no list. */
1674ffcb1c8Snicm static void
format_draw_none(struct screen_write_ctx * octx,u_int available,u_int ocx,u_int ocy,struct screen * left,struct screen * centre,struct screen * right,struct screen * abs_centre,struct format_ranges * frs)1684ffcb1c8Snicm format_draw_none(struct screen_write_ctx *octx, u_int available, u_int ocx,
1694ffcb1c8Snicm     u_int ocy, struct screen *left, struct screen *centre, struct screen *right,
17084b35168Snicm     struct screen *abs_centre, struct format_ranges *frs)
1714ffcb1c8Snicm {
17284b35168Snicm 	u_int	width_left, width_centre, width_right, width_abs_centre;
1734ffcb1c8Snicm 
1744ffcb1c8Snicm 	width_left = left->cx;
1754ffcb1c8Snicm 	width_centre = centre->cx;
1764ffcb1c8Snicm 	width_right = right->cx;
17784b35168Snicm 	width_abs_centre = abs_centre->cx;
1784ffcb1c8Snicm 
1794ffcb1c8Snicm 	/*
1804ffcb1c8Snicm 	 * Try to keep as much of the left and right as possible at the expense
1814ffcb1c8Snicm 	 * of the centre.
1824ffcb1c8Snicm 	 */
1834ffcb1c8Snicm 	while (width_left + width_centre + width_right > available) {
1844ffcb1c8Snicm 		if (width_centre > 0)
1854ffcb1c8Snicm 			width_centre--;
1864ffcb1c8Snicm 		else if (width_right > 0)
1874ffcb1c8Snicm 			width_right--;
1884ffcb1c8Snicm 		else
1894ffcb1c8Snicm 			width_left--;
1904ffcb1c8Snicm 	}
1914ffcb1c8Snicm 
1924ffcb1c8Snicm 	/* Write left. */
1934ffcb1c8Snicm 	format_draw_put(octx, ocx, ocy, left, frs, 0, 0, width_left);
1944ffcb1c8Snicm 
1954ffcb1c8Snicm 	/* Write right at available - width_right. */
1964ffcb1c8Snicm 	format_draw_put(octx, ocx, ocy, right, frs,
1974ffcb1c8Snicm 	    available - width_right,
1984ffcb1c8Snicm 	    right->cx - width_right,
1994ffcb1c8Snicm 	    width_right);
2004ffcb1c8Snicm 
2014ffcb1c8Snicm 	/*
2024ffcb1c8Snicm 	 * Write centre halfway between
2034ffcb1c8Snicm 	 *     width_left
2044ffcb1c8Snicm 	 * and
2054ffcb1c8Snicm 	 *     available - width_right.
2064ffcb1c8Snicm 	 */
2074ffcb1c8Snicm 	format_draw_put(octx, ocx, ocy, centre, frs,
2084ffcb1c8Snicm 	    width_left
2094ffcb1c8Snicm 	    + ((available - width_right) - width_left) / 2
2104ffcb1c8Snicm 	    - width_centre / 2,
2114ffcb1c8Snicm 	    centre->cx / 2 - width_centre / 2,
2124ffcb1c8Snicm 	    width_centre);
21384b35168Snicm 
21484b35168Snicm 	/*
21584b35168Snicm 	 * Write abs_centre in the perfect centre of all horizontal space.
21684b35168Snicm 	 */
21784b35168Snicm 	if (width_abs_centre > available)
21884b35168Snicm 		width_abs_centre = available;
21984b35168Snicm 	format_draw_put(octx, ocx, ocy, abs_centre, frs,
22084b35168Snicm 	    (available - width_abs_centre) / 2,
22184b35168Snicm 	    0,
22284b35168Snicm 	    width_abs_centre);
2234ffcb1c8Snicm }
2244ffcb1c8Snicm 
2254ffcb1c8Snicm /* Draw format with list on the left. */
2264ffcb1c8Snicm static void
format_draw_left(struct screen_write_ctx * octx,u_int available,u_int ocx,u_int ocy,struct screen * left,struct screen * centre,struct screen * right,struct screen * abs_centre,struct screen * list,struct screen * list_left,struct screen * list_right,struct screen * after,int focus_start,int focus_end,struct format_ranges * frs)2274ffcb1c8Snicm format_draw_left(struct screen_write_ctx *octx, u_int available, u_int ocx,
2284ffcb1c8Snicm     u_int ocy, struct screen *left, struct screen *centre, struct screen *right,
22984b35168Snicm     struct screen *abs_centre, struct screen *list, struct screen *list_left,
23084b35168Snicm     struct screen *list_right, struct screen *after, int focus_start,
23184b35168Snicm     int focus_end, struct format_ranges *frs)
2324ffcb1c8Snicm {
2334ffcb1c8Snicm 	u_int			width_left, width_centre, width_right;
23484b35168Snicm 	u_int			width_list, width_after, width_abs_centre;
2354ffcb1c8Snicm 	struct screen_write_ctx	ctx;
2364ffcb1c8Snicm 
2374ffcb1c8Snicm 	width_left = left->cx;
2384ffcb1c8Snicm 	width_centre = centre->cx;
2394ffcb1c8Snicm 	width_right = right->cx;
24084b35168Snicm 	width_abs_centre = abs_centre->cx;
2414ffcb1c8Snicm 	width_list = list->cx;
2424ffcb1c8Snicm 	width_after = after->cx;
2434ffcb1c8Snicm 
2444ffcb1c8Snicm 	/*
2454ffcb1c8Snicm 	 * Trim first the centre, then the list, then the right, then after the
2464ffcb1c8Snicm 	 * list, then the left.
2474ffcb1c8Snicm 	 */
2484ffcb1c8Snicm 	while (width_left +
2494ffcb1c8Snicm 	    width_centre +
2504ffcb1c8Snicm 	    width_right +
2514ffcb1c8Snicm 	    width_list +
2524ffcb1c8Snicm 	    width_after > available) {
2534ffcb1c8Snicm 		if (width_centre > 0)
2544ffcb1c8Snicm 			width_centre--;
2554ffcb1c8Snicm 		else if (width_list > 0)
2564ffcb1c8Snicm 			width_list--;
2574ffcb1c8Snicm 		else if (width_right > 0)
2584ffcb1c8Snicm 			width_right--;
2594ffcb1c8Snicm 		else if (width_after > 0)
2604ffcb1c8Snicm 			width_after--;
2614ffcb1c8Snicm 		else
2624ffcb1c8Snicm 			width_left--;
2634ffcb1c8Snicm 	}
2644ffcb1c8Snicm 
2654ffcb1c8Snicm 	/* If there is no list left, pass off to the no list function. */
2664ffcb1c8Snicm 	if (width_list == 0) {
26783e83a91Snicm 		screen_write_start(&ctx, left);
2684ffcb1c8Snicm 		screen_write_fast_copy(&ctx, after, 0, 0, width_after, 1);
2694ffcb1c8Snicm 		screen_write_stop(&ctx);
2704ffcb1c8Snicm 
2714ffcb1c8Snicm 		format_draw_none(octx, available, ocx, ocy, left, centre,
27284b35168Snicm 		    right, abs_centre, frs);
2734ffcb1c8Snicm 		return;
2744ffcb1c8Snicm 	}
2754ffcb1c8Snicm 
2764ffcb1c8Snicm 	/* Write left at 0. */
2774ffcb1c8Snicm 	format_draw_put(octx, ocx, ocy, left, frs, 0, 0, width_left);
2784ffcb1c8Snicm 
2794ffcb1c8Snicm 	/* Write right at available - width_right. */
2804ffcb1c8Snicm 	format_draw_put(octx, ocx, ocy, right, frs,
2814ffcb1c8Snicm 	    available - width_right,
2824ffcb1c8Snicm 	    right->cx - width_right,
2834ffcb1c8Snicm 	    width_right);
2844ffcb1c8Snicm 
2854ffcb1c8Snicm 	/* Write after at width_left + width_list. */
2864ffcb1c8Snicm 	format_draw_put(octx, ocx, ocy, after, frs,
2874ffcb1c8Snicm 	    width_left + width_list,
2884ffcb1c8Snicm 	    0,
2894ffcb1c8Snicm 	    width_after);
2904ffcb1c8Snicm 
2914ffcb1c8Snicm 	/*
2924ffcb1c8Snicm 	 * Write centre halfway between
2934ffcb1c8Snicm 	 *     width_left + width_list + width_after
2944ffcb1c8Snicm 	 * and
2954ffcb1c8Snicm 	 *     available - width_right.
2964ffcb1c8Snicm 	 */
2974ffcb1c8Snicm 	format_draw_put(octx, ocx, ocy, centre, frs,
2984ffcb1c8Snicm 	    (width_left + width_list + width_after)
2994ffcb1c8Snicm 	    + ((available - width_right)
3004ffcb1c8Snicm 		- (width_left + width_list + width_after)) / 2
3014ffcb1c8Snicm 	    - width_centre / 2,
3024ffcb1c8Snicm 	    centre->cx / 2 - width_centre / 2,
3034ffcb1c8Snicm 	    width_centre);
3044ffcb1c8Snicm 
3054ffcb1c8Snicm 	/*
3064ffcb1c8Snicm 	 * The list now goes from
3074ffcb1c8Snicm 	 *     width_left
3084ffcb1c8Snicm 	 * to
3094ffcb1c8Snicm 	 *     width_left + width_list.
3104ffcb1c8Snicm 	 * If there is no focus given, keep the left in focus.
3114ffcb1c8Snicm 	 */
3124ffcb1c8Snicm 	if (focus_start == -1 || focus_end == -1)
3134ffcb1c8Snicm 		focus_start = focus_end = 0;
3144ffcb1c8Snicm 	format_draw_put_list(octx, ocx, ocy, width_left, width_list, list,
3154ffcb1c8Snicm 	    list_left, list_right, focus_start, focus_end, frs);
31684b35168Snicm 
31784b35168Snicm 	/*
31884b35168Snicm 	 * Write abs_centre in the perfect centre of all horizontal space.
31984b35168Snicm 	 */
32084b35168Snicm 	if (width_abs_centre > available)
32184b35168Snicm 		width_abs_centre = available;
32284b35168Snicm 	format_draw_put(octx, ocx, ocy, abs_centre, frs,
32384b35168Snicm 	    (available - width_abs_centre) / 2,
32484b35168Snicm 	    0,
32584b35168Snicm 	    width_abs_centre);
3264ffcb1c8Snicm }
3274ffcb1c8Snicm 
3284ffcb1c8Snicm /* Draw format with list in the centre. */
3294ffcb1c8Snicm static void
format_draw_centre(struct screen_write_ctx * octx,u_int available,u_int ocx,u_int ocy,struct screen * left,struct screen * centre,struct screen * right,struct screen * abs_centre,struct screen * list,struct screen * list_left,struct screen * list_right,struct screen * after,int focus_start,int focus_end,struct format_ranges * frs)3304ffcb1c8Snicm format_draw_centre(struct screen_write_ctx *octx, u_int available, u_int ocx,
3314ffcb1c8Snicm     u_int ocy, struct screen *left, struct screen *centre, struct screen *right,
33284b35168Snicm     struct screen *abs_centre, struct screen *list, struct screen *list_left,
33384b35168Snicm     struct screen *list_right, struct screen *after, int focus_start,
33484b35168Snicm     int focus_end, struct format_ranges *frs)
3354ffcb1c8Snicm {
33684b35168Snicm 	u_int			width_left, width_centre, width_right, middle;
33784b35168Snicm 	u_int			width_list, width_after, width_abs_centre;
3384ffcb1c8Snicm 	struct screen_write_ctx	ctx;
3394ffcb1c8Snicm 
3404ffcb1c8Snicm 	width_left = left->cx;
3414ffcb1c8Snicm 	width_centre = centre->cx;
3424ffcb1c8Snicm 	width_right = right->cx;
34384b35168Snicm 	width_abs_centre = abs_centre->cx;
3444ffcb1c8Snicm 	width_list = list->cx;
3454ffcb1c8Snicm 	width_after = after->cx;
3464ffcb1c8Snicm 
3474ffcb1c8Snicm 	/*
3484ffcb1c8Snicm 	 * Trim first the list, then after the list, then the centre, then the
3494ffcb1c8Snicm 	 * right, then the left.
3504ffcb1c8Snicm 	 */
3514ffcb1c8Snicm 	while (width_left +
3524ffcb1c8Snicm 	    width_centre +
3534ffcb1c8Snicm 	    width_right +
3544ffcb1c8Snicm 	    width_list +
3554ffcb1c8Snicm 	    width_after > available) {
3564ffcb1c8Snicm 		if (width_list > 0)
3574ffcb1c8Snicm 			width_list--;
3584ffcb1c8Snicm 		else if (width_after > 0)
3594ffcb1c8Snicm 			width_after--;
3604ffcb1c8Snicm 		else if (width_centre > 0)
3614ffcb1c8Snicm 			width_centre--;
3624ffcb1c8Snicm 		else if (width_right > 0)
3634ffcb1c8Snicm 			width_right--;
3644ffcb1c8Snicm 		else
3654ffcb1c8Snicm 			width_left--;
3664ffcb1c8Snicm 	}
3674ffcb1c8Snicm 
3684ffcb1c8Snicm 	/* If there is no list left, pass off to the no list function. */
3694ffcb1c8Snicm 	if (width_list == 0) {
37083e83a91Snicm 		screen_write_start(&ctx, centre);
3714ffcb1c8Snicm 		screen_write_fast_copy(&ctx, after, 0, 0, width_after, 1);
3724ffcb1c8Snicm 		screen_write_stop(&ctx);
3734ffcb1c8Snicm 
3744ffcb1c8Snicm 		format_draw_none(octx, available, ocx, ocy, left, centre,
37584b35168Snicm 		    right, abs_centre, frs);
3764ffcb1c8Snicm 		return;
3774ffcb1c8Snicm 	}
3784ffcb1c8Snicm 
3794ffcb1c8Snicm 	/* Write left at 0. */
3804ffcb1c8Snicm 	format_draw_put(octx, ocx, ocy, left, frs, 0, 0, width_left);
3814ffcb1c8Snicm 
3824ffcb1c8Snicm 	/* Write right at available - width_right. */
3834ffcb1c8Snicm 	format_draw_put(octx, ocx, ocy, right, frs,
3844ffcb1c8Snicm 	    available - width_right,
3854ffcb1c8Snicm 	    right->cx - width_right,
3864ffcb1c8Snicm 	    width_right);
3874ffcb1c8Snicm 
3884ffcb1c8Snicm 	/*
3894ffcb1c8Snicm 	 * All three centre sections are offset from the middle of the
3904ffcb1c8Snicm 	 * available space.
3914ffcb1c8Snicm 	 */
3924ffcb1c8Snicm 	middle = (width_left + ((available - width_right) - width_left) / 2);
3934ffcb1c8Snicm 
3944ffcb1c8Snicm 	/*
3954ffcb1c8Snicm 	 * Write centre at
3964ffcb1c8Snicm 	 *     middle - width_list / 2 - width_centre.
3974ffcb1c8Snicm 	 */
3984ffcb1c8Snicm 	format_draw_put(octx, ocx, ocy, centre, frs,
3994ffcb1c8Snicm 	    middle - width_list / 2 - width_centre,
4004ffcb1c8Snicm 	    0,
4014ffcb1c8Snicm 	    width_centre);
4024ffcb1c8Snicm 
4034ffcb1c8Snicm 	/*
4044ffcb1c8Snicm 	 * Write after at
40501992925Snicm 	 *     middle - width_list / 2 + width_list
4064ffcb1c8Snicm 	 */
4074ffcb1c8Snicm 	format_draw_put(octx, ocx, ocy, after, frs,
40801992925Snicm 	    middle - width_list / 2 + width_list,
4094ffcb1c8Snicm 	    0,
4104ffcb1c8Snicm 	    width_after);
4114ffcb1c8Snicm 
4124ffcb1c8Snicm 	/*
4134ffcb1c8Snicm 	 * The list now goes from
4144ffcb1c8Snicm 	 *     middle - width_list / 2
4154ffcb1c8Snicm 	 * to
4164ffcb1c8Snicm 	 *     middle + width_list / 2
4174ffcb1c8Snicm 	 * If there is no focus given, keep the centre in focus.
4184ffcb1c8Snicm 	 */
4194ffcb1c8Snicm 	if (focus_start == -1 || focus_end == -1)
4204ffcb1c8Snicm 		focus_start = focus_end = list->cx / 2;
4214ffcb1c8Snicm 	format_draw_put_list(octx, ocx, ocy, middle - width_list / 2,
4224ffcb1c8Snicm 	    width_list, list, list_left, list_right, focus_start, focus_end,
4234ffcb1c8Snicm 	    frs);
42484b35168Snicm 
42584b35168Snicm 	/*
42684b35168Snicm 	 * Write abs_centre in the perfect centre of all horizontal space.
42784b35168Snicm 	 */
42884b35168Snicm 	if (width_abs_centre > available)
42984b35168Snicm 		width_abs_centre = available;
43084b35168Snicm 	format_draw_put(octx, ocx, ocy, abs_centre, frs,
43184b35168Snicm 	    (available - width_abs_centre) / 2,
43284b35168Snicm 	    0,
43384b35168Snicm 	    width_abs_centre);
4344ffcb1c8Snicm }
4354ffcb1c8Snicm 
4364ffcb1c8Snicm /* Draw format with list on the right. */
4374ffcb1c8Snicm static void
format_draw_right(struct screen_write_ctx * octx,u_int available,u_int ocx,u_int ocy,struct screen * left,struct screen * centre,struct screen * right,struct screen * abs_centre,struct screen * list,struct screen * list_left,struct screen * list_right,struct screen * after,int focus_start,int focus_end,struct format_ranges * frs)4384ffcb1c8Snicm format_draw_right(struct screen_write_ctx *octx, u_int available, u_int ocx,
4394ffcb1c8Snicm     u_int ocy, struct screen *left, struct screen *centre, struct screen *right,
44084b35168Snicm     struct screen *abs_centre,     struct screen *list,
44184b35168Snicm     struct screen *list_left, struct screen *list_right, struct screen *after,
44284b35168Snicm     int focus_start, int focus_end, struct format_ranges *frs)
4434ffcb1c8Snicm {
4444ffcb1c8Snicm 	u_int			width_left, width_centre, width_right;
44584b35168Snicm 	u_int			width_list, width_after, width_abs_centre;
4464ffcb1c8Snicm 	struct screen_write_ctx	ctx;
4474ffcb1c8Snicm 
4484ffcb1c8Snicm 	width_left = left->cx;
4494ffcb1c8Snicm 	width_centre = centre->cx;
4504ffcb1c8Snicm 	width_right = right->cx;
45184b35168Snicm 	width_abs_centre = abs_centre->cx;
4524ffcb1c8Snicm 	width_list = list->cx;
4534ffcb1c8Snicm 	width_after = after->cx;
4544ffcb1c8Snicm 
4554ffcb1c8Snicm 	/*
4564ffcb1c8Snicm 	 * Trim first the centre, then the list, then the right, then
4574ffcb1c8Snicm 	 * after the list, then the left.
4584ffcb1c8Snicm 	 */
4594ffcb1c8Snicm 	while (width_left +
4604ffcb1c8Snicm 	    width_centre +
4614ffcb1c8Snicm 	    width_right +
4624ffcb1c8Snicm 	    width_list +
4634ffcb1c8Snicm 	    width_after > available) {
4644ffcb1c8Snicm 		if (width_centre > 0)
4654ffcb1c8Snicm 			width_centre--;
4664ffcb1c8Snicm 		else if (width_list > 0)
4674ffcb1c8Snicm 			width_list--;
4684ffcb1c8Snicm 		else if (width_right > 0)
4694ffcb1c8Snicm 			width_right--;
4704ffcb1c8Snicm 		else if (width_after > 0)
4714ffcb1c8Snicm 			width_after--;
4724ffcb1c8Snicm 		else
4734ffcb1c8Snicm 			width_left--;
4744ffcb1c8Snicm 	}
4754ffcb1c8Snicm 
4764ffcb1c8Snicm 	/* If there is no list left, pass off to the no list function. */
4774ffcb1c8Snicm 	if (width_list == 0) {
47883e83a91Snicm 		screen_write_start(&ctx, right);
4794ffcb1c8Snicm 		screen_write_fast_copy(&ctx, after, 0, 0, width_after, 1);
4804ffcb1c8Snicm 		screen_write_stop(&ctx);
4814ffcb1c8Snicm 
4824ffcb1c8Snicm 		format_draw_none(octx, available, ocx, ocy, left, centre,
48384b35168Snicm 		    right, abs_centre, frs);
4844ffcb1c8Snicm 		return;
4854ffcb1c8Snicm 	}
4864ffcb1c8Snicm 
4874ffcb1c8Snicm 	/* Write left at 0. */
4884ffcb1c8Snicm 	format_draw_put(octx, ocx, ocy, left, frs, 0, 0, width_left);
4894ffcb1c8Snicm 
4904ffcb1c8Snicm 	/* Write after at available - width_after. */
4914ffcb1c8Snicm 	format_draw_put(octx, ocx, ocy, after, frs,
4924ffcb1c8Snicm 	    available - width_after,
4934ffcb1c8Snicm 	    after->cx - width_after,
4944ffcb1c8Snicm 	    width_after);
4954ffcb1c8Snicm 
4964ffcb1c8Snicm 	/*
4974ffcb1c8Snicm 	 * Write right at
4984ffcb1c8Snicm 	 *     available - width_right - width_list - width_after.
4994ffcb1c8Snicm 	 */
5004ffcb1c8Snicm 	format_draw_put(octx, ocx, ocy, right, frs,
5014ffcb1c8Snicm 	    available - width_right - width_list - width_after,
5024ffcb1c8Snicm 	    0,
5034ffcb1c8Snicm 	    width_right);
5044ffcb1c8Snicm 
5054ffcb1c8Snicm 	/*
5064ffcb1c8Snicm 	 * Write centre halfway between
5074ffcb1c8Snicm 	 *     width_left
5084ffcb1c8Snicm 	 * and
5094ffcb1c8Snicm 	 *     available - width_right - width_list - width_after.
5104ffcb1c8Snicm 	 */
5114ffcb1c8Snicm 	format_draw_put(octx, ocx, ocy, centre, frs,
5124ffcb1c8Snicm 	    width_left
5134ffcb1c8Snicm 	    + ((available - width_right - width_list - width_after)
5144ffcb1c8Snicm 		- width_left) / 2
5154ffcb1c8Snicm 	    - width_centre / 2,
5164ffcb1c8Snicm 	    centre->cx / 2 - width_centre / 2,
5174ffcb1c8Snicm 	    width_centre);
5184ffcb1c8Snicm 
5194ffcb1c8Snicm 	/*
5204ffcb1c8Snicm 	 * The list now goes from
5214ffcb1c8Snicm 	 *     available - width_list - width_after
5224ffcb1c8Snicm 	 * to
5234ffcb1c8Snicm 	 *     available - width_after
5244ffcb1c8Snicm 	 * If there is no focus given, keep the right in focus.
5254ffcb1c8Snicm 	 */
5264ffcb1c8Snicm 	if (focus_start == -1 || focus_end == -1)
5274ffcb1c8Snicm 		focus_start = focus_end = 0;
5284ffcb1c8Snicm 	format_draw_put_list(octx, ocx, ocy, available - width_list -
5294ffcb1c8Snicm 	    width_after, width_list, list, list_left, list_right, focus_start,
5304ffcb1c8Snicm 	    focus_end, frs);
53184b35168Snicm 
53284b35168Snicm 	/*
53384b35168Snicm 	 * Write abs_centre in the perfect centre of all horizontal space.
53484b35168Snicm 	 */
53584b35168Snicm 	if (width_abs_centre > available)
53684b35168Snicm 		width_abs_centre = available;
53784b35168Snicm 	format_draw_put(octx, ocx, ocy, abs_centre, frs,
53884b35168Snicm 	    (available - width_abs_centre) / 2,
53984b35168Snicm 	    0,
54084b35168Snicm 	    width_abs_centre);
54184b35168Snicm }
54284b35168Snicm 
54384b35168Snicm static void
format_draw_absolute_centre(struct screen_write_ctx * octx,u_int available,u_int ocx,u_int ocy,struct screen * left,struct screen * centre,struct screen * right,struct screen * abs_centre,struct screen * list,struct screen * list_left,struct screen * list_right,struct screen * after,int focus_start,int focus_end,struct format_ranges * frs)54484b35168Snicm format_draw_absolute_centre(struct screen_write_ctx *octx, u_int available,
54584b35168Snicm     u_int ocx, u_int ocy, struct screen *left, struct screen *centre,
54684b35168Snicm     struct screen *right, struct screen *abs_centre, struct screen *list,
54784b35168Snicm     struct screen *list_left, struct screen *list_right, struct screen *after,
54884b35168Snicm     int focus_start, int focus_end, struct format_ranges *frs)
54984b35168Snicm {
55084b35168Snicm 	u_int	width_left, width_centre, width_right, width_abs_centre;
55184b35168Snicm 	u_int	width_list, width_after, middle, abs_centre_offset;
55284b35168Snicm 
55384b35168Snicm 	width_left = left->cx;
55484b35168Snicm 	width_centre = centre->cx;
55584b35168Snicm 	width_right = right->cx;
55684b35168Snicm 	width_abs_centre = abs_centre->cx;
55784b35168Snicm 	width_list = list->cx;
55884b35168Snicm 	width_after = after->cx;
55984b35168Snicm 
56084b35168Snicm 	/*
56184b35168Snicm 	 * Trim first centre, then the right, then the left.
56284b35168Snicm 	 */
56384b35168Snicm 	while (width_left +
56484b35168Snicm 	    width_centre +
56584b35168Snicm 	    width_right > available) {
56684b35168Snicm 		if (width_centre > 0)
56784b35168Snicm 			width_centre--;
56884b35168Snicm 		else if (width_right > 0)
56984b35168Snicm 			width_right--;
57084b35168Snicm 		else
57184b35168Snicm 			width_left--;
57284b35168Snicm 	}
57384b35168Snicm 
57484b35168Snicm 	/*
57584b35168Snicm 	 * We trim list after and abs_centre independently, as we are drawing
57684b35168Snicm 	 * them over the rest. Trim first the list, then after the list, then
57784b35168Snicm 	 * abs_centre.
57884b35168Snicm 	 */
57984b35168Snicm 	while (width_list + width_after + width_abs_centre > available) {
58084b35168Snicm 		if (width_list > 0)
58184b35168Snicm 			width_list--;
58284b35168Snicm 		else if (width_after > 0)
58384b35168Snicm 			width_after--;
58484b35168Snicm 		else
58584b35168Snicm 			width_abs_centre--;
58684b35168Snicm 	}
58784b35168Snicm 
58884b35168Snicm 	/* Write left at 0. */
58984b35168Snicm 	format_draw_put(octx, ocx, ocy, left, frs, 0, 0, width_left);
59084b35168Snicm 
59184b35168Snicm 	/* Write right at available - width_right. */
59284b35168Snicm 	format_draw_put(octx, ocx, ocy, right, frs,
59384b35168Snicm 	    available - width_right,
59484b35168Snicm 	    right->cx - width_right,
59584b35168Snicm 	    width_right);
59684b35168Snicm 
59784b35168Snicm 	/*
59884b35168Snicm 	 * Keep writing centre at the relative centre. Only the list is written
59984b35168Snicm 	 * in the absolute centre of the horizontal space.
60084b35168Snicm 	 */
60184b35168Snicm 	middle = (width_left + ((available - width_right) - width_left) / 2);
60284b35168Snicm 
60384b35168Snicm 	/*
60484b35168Snicm 	 * Write centre at
60584b35168Snicm 	 *     middle - width_centre.
60684b35168Snicm 	 */
60784b35168Snicm 	format_draw_put(octx, ocx, ocy, centre, frs,
60884b35168Snicm 		middle - width_centre,
60984b35168Snicm 		0,
61084b35168Snicm 		width_centre);
61184b35168Snicm 
61284b35168Snicm 	/*
61384b35168Snicm 	 * If there is no focus given, keep the centre in focus.
61484b35168Snicm 	 */
61584b35168Snicm 	if (focus_start == -1 || focus_end == -1)
61684b35168Snicm 		focus_start = focus_end = list->cx / 2;
61784b35168Snicm 
61884b35168Snicm 	/*
61984b35168Snicm 	 * We centre abs_centre and the list together, so their shared centre is
62084b35168Snicm 	 * in the perfect centre of horizontal space.
62184b35168Snicm 	 */
62284b35168Snicm 	abs_centre_offset = (available - width_list - width_abs_centre) / 2;
62384b35168Snicm 
62484b35168Snicm 	/*
62584b35168Snicm 	 * Write abs_centre before the list.
62684b35168Snicm 	 */
62784b35168Snicm 	format_draw_put(octx, ocx, ocy, abs_centre, frs, abs_centre_offset,
62884b35168Snicm 	    0, width_abs_centre);
62984b35168Snicm 	abs_centre_offset += width_abs_centre;
63084b35168Snicm 
63184b35168Snicm 	/*
63284b35168Snicm 	 * Draw the list in the absolute centre
63384b35168Snicm 	 */
63484b35168Snicm 	format_draw_put_list(octx, ocx, ocy, abs_centre_offset, width_list,
63584b35168Snicm 	    list, list_left, list_right, focus_start, focus_end, frs);
63684b35168Snicm 	abs_centre_offset += width_list;
63784b35168Snicm 
63884b35168Snicm 	/*
63984b35168Snicm 	 * Write after at the end of the centre
64084b35168Snicm 	 */
64184b35168Snicm 	format_draw_put(octx, ocx, ocy, after, frs, abs_centre_offset, 0,
64284b35168Snicm 	    width_after);
6434ffcb1c8Snicm }
6444ffcb1c8Snicm 
64550e21e7bSnicm /* Get width and count of any leading #s. */
64650e21e7bSnicm static const char *
format_leading_hashes(const char * cp,u_int * n,u_int * width)64750e21e7bSnicm format_leading_hashes(const char *cp, u_int *n, u_int *width)
64850e21e7bSnicm {
64950e21e7bSnicm 	for (*n = 0; cp[*n] == '#'; (*n)++)
65050e21e7bSnicm 		/* nothing */;
65150e21e7bSnicm 	if (*n == 0) {
65250e21e7bSnicm 		*width = 0;
65350e21e7bSnicm 		return (cp);
65450e21e7bSnicm 	}
65550e21e7bSnicm 	if (cp[*n] != '[') {
65650e21e7bSnicm 		if ((*n % 2) == 0)
65750e21e7bSnicm 			*width = (*n / 2);
65850e21e7bSnicm 		else
65950e21e7bSnicm 			*width = (*n / 2) + 1;
66050e21e7bSnicm 		return (cp + *n);
66150e21e7bSnicm 	}
66250e21e7bSnicm 	*width = (*n / 2);
66350e21e7bSnicm 	if ((*n % 2) == 0) {
66450e21e7bSnicm 		/*
66550e21e7bSnicm 		 * An even number of #s means that all #s are escaped, so not a
66650e21e7bSnicm 		 * style. The caller should not skip this. Return pointing to
66750e21e7bSnicm 		 * the [.
66850e21e7bSnicm 		 */
66950e21e7bSnicm 		return (cp + *n);
67050e21e7bSnicm 	}
67150e21e7bSnicm 	/* This is a style, so return pointing to the #. */
67250e21e7bSnicm 	return (cp + *n - 1);
67350e21e7bSnicm }
67450e21e7bSnicm 
675987e1c89Snicm /* Draw multiple characters. */
676987e1c89Snicm static void
format_draw_many(struct screen_write_ctx * ctx,struct style * sy,char ch,u_int n)677987e1c89Snicm format_draw_many(struct screen_write_ctx *ctx, struct style *sy, char ch,
678987e1c89Snicm     u_int n)
679987e1c89Snicm {
680987e1c89Snicm 	u_int	i;
681987e1c89Snicm 
682987e1c89Snicm 	utf8_set(&sy->gc.data, ch);
683987e1c89Snicm 	for (i = 0; i < n; i++)
684987e1c89Snicm 		screen_write_cell(ctx, &sy->gc);
685987e1c89Snicm }
686987e1c89Snicm 
6874ffcb1c8Snicm /* Draw a format to a screen. */
6884ffcb1c8Snicm void
format_draw(struct screen_write_ctx * octx,const struct grid_cell * base,u_int available,const char * expanded,struct style_ranges * srs,int default_colours)6894ffcb1c8Snicm format_draw(struct screen_write_ctx *octx, const struct grid_cell *base,
690173e8225Snicm     u_int available, const char *expanded, struct style_ranges *srs,
691173e8225Snicm     int default_colours)
6924ffcb1c8Snicm {
6934ffcb1c8Snicm 	enum { LEFT,
6944ffcb1c8Snicm 	       CENTRE,
6954ffcb1c8Snicm 	       RIGHT,
69684b35168Snicm 	       ABSOLUTE_CENTRE,
6974ffcb1c8Snicm 	       LIST,
6984ffcb1c8Snicm 	       LIST_LEFT,
6994ffcb1c8Snicm 	       LIST_RIGHT,
7004ffcb1c8Snicm 	       AFTER,
7014ffcb1c8Snicm 	       TOTAL } current = LEFT, last = LEFT;
7024ffcb1c8Snicm 	const char	        *names[] = { "LEFT",
7034ffcb1c8Snicm 					     "CENTRE",
7044ffcb1c8Snicm 					     "RIGHT",
70584b35168Snicm 					     "ABSOLUTE_CENTRE",
7064ffcb1c8Snicm 					     "LIST",
7074ffcb1c8Snicm 					     "LIST_LEFT",
7084ffcb1c8Snicm 					     "LIST_RIGHT",
7094ffcb1c8Snicm 					     "AFTER" };
7104ffcb1c8Snicm 	size_t			 size = strlen(expanded);
7114ffcb1c8Snicm 	struct screen		*os = octx->s, s[TOTAL];
7124ffcb1c8Snicm 	struct screen_write_ctx	 ctx[TOTAL];
713987e1c89Snicm 	u_int			 ocx = os->cx, ocy = os->cy, n, i, width[TOTAL];
71484b35168Snicm 	u_int			 map[] = { LEFT,
71584b35168Snicm 					   LEFT,
71684b35168Snicm 					   CENTRE,
71784b35168Snicm 					   RIGHT,
71884b35168Snicm 					   ABSOLUTE_CENTRE };
7194ffcb1c8Snicm 	int			 focus_start = -1, focus_end = -1;
720987e1c89Snicm 	int			 list_state = -1, fill = -1, even;
7214ffcb1c8Snicm 	enum style_align	 list_align = STYLE_ALIGN_DEFAULT;
722c8f0e702Snicm 	struct grid_cell	 gc, current_default;
723c8f0e702Snicm 	struct style		 sy, saved_sy;
7244ffcb1c8Snicm 	struct utf8_data	*ud = &sy.gc.data;
7254ffcb1c8Snicm 	const char		*cp, *end;
7264ffcb1c8Snicm 	enum utf8_state		 more;
7274ffcb1c8Snicm 	char			*tmp;
7284ffcb1c8Snicm 	struct format_range	*fr = NULL, *fr1;
7294ffcb1c8Snicm 	struct format_ranges	 frs;
7304ffcb1c8Snicm 	struct style_range	*sr;
7314ffcb1c8Snicm 
732c8f0e702Snicm 	memcpy(&current_default, base, sizeof current_default);
733c8f0e702Snicm 	style_set(&sy, &current_default);
7344ffcb1c8Snicm 	TAILQ_INIT(&frs);
735180e3f4cSnicm 	log_debug("%s: %s", __func__, expanded);
7364ffcb1c8Snicm 
7374ffcb1c8Snicm 	/*
7384ffcb1c8Snicm 	 * We build three screens for left, right, centre alignment, one for
7394ffcb1c8Snicm 	 * the list, one for anything after the list and two for the list left
7404ffcb1c8Snicm 	 * and right markers.
7414ffcb1c8Snicm 	 */
7424ffcb1c8Snicm 	for (i = 0; i < TOTAL; i++) {
7434ffcb1c8Snicm 		screen_init(&s[i], size, 1, 0);
74483e83a91Snicm 		screen_write_start(&ctx[i], &s[i]);
745c8f0e702Snicm 		screen_write_clearendofline(&ctx[i], current_default.bg);
7464ffcb1c8Snicm 		width[i] = 0;
7474ffcb1c8Snicm 	}
7484ffcb1c8Snicm 
7494ffcb1c8Snicm 	/*
7504ffcb1c8Snicm 	 * Walk the string and add to the corresponding screens,
7514ffcb1c8Snicm 	 * parsing styles as we go.
7524ffcb1c8Snicm 	 */
7534ffcb1c8Snicm 	cp = expanded;
7544ffcb1c8Snicm 	while (*cp != '\0') {
755987e1c89Snicm 		/* Handle sequences of #. */
756987e1c89Snicm 		if (cp[0] == '#' && cp[1] != '[' && cp[1] != '\0') {
757987e1c89Snicm 			for (n = 1; cp[n] == '#'; n++)
758987e1c89Snicm 				 /* nothing */;
75920cd170dSnicm 			even = ((n % 2) == 0);
760987e1c89Snicm 			if (cp[n] != '[') {
761987e1c89Snicm 				cp += n;
76220cd170dSnicm 				if (even)
76320cd170dSnicm 					n = (n / 2);
76420cd170dSnicm 				else
76520cd170dSnicm 					n = (n / 2) + 1;
76620cd170dSnicm 				width[current] += n;
767987e1c89Snicm 				format_draw_many(&ctx[current], &sy, '#', n);
768987e1c89Snicm 				continue;
769987e1c89Snicm 			}
770987e1c89Snicm 			if (even)
771987e1c89Snicm 				cp += (n + 1);
772987e1c89Snicm 			else
773987e1c89Snicm 				cp += (n - 1);
774987e1c89Snicm 			if (sy.ignore)
775987e1c89Snicm 				continue;
776987e1c89Snicm 			format_draw_many(&ctx[current], &sy, '#', n / 2);
777987e1c89Snicm 			width[current] += (n / 2);
778987e1c89Snicm 			if (even) {
779987e1c89Snicm 				utf8_set(ud, '[');
780987e1c89Snicm 				screen_write_cell(&ctx[current], &sy.gc);
781987e1c89Snicm 				width[current]++;
782987e1c89Snicm 			}
783987e1c89Snicm 			continue;
784987e1c89Snicm 		}
785987e1c89Snicm 
786987e1c89Snicm 		/* Is this not a style? */
78767c16a7cSnicm 		if (cp[0] != '#' || cp[1] != '[' || sy.ignore) {
7884ffcb1c8Snicm 			/* See if this is a UTF-8 character. */
7894ffcb1c8Snicm 			if ((more = utf8_open(ud, *cp)) == UTF8_MORE) {
7904ffcb1c8Snicm 				while (*++cp != '\0' && more == UTF8_MORE)
7914ffcb1c8Snicm 					more = utf8_append(ud, *cp);
7924ffcb1c8Snicm 				if (more != UTF8_DONE)
7934ffcb1c8Snicm 					cp -= ud->have;
7944ffcb1c8Snicm 			}
7954ffcb1c8Snicm 
7964ffcb1c8Snicm 			/* Not a UTF-8 character - ASCII or not valid. */
7974ffcb1c8Snicm 			if (more != UTF8_DONE) {
7984ffcb1c8Snicm 				if (*cp < 0x20 || *cp > 0x7e) {
7994ffcb1c8Snicm 					/* Ignore nonprintable characters. */
8004ffcb1c8Snicm 					cp++;
8014ffcb1c8Snicm 					continue;
8024ffcb1c8Snicm 				}
8034ffcb1c8Snicm 				utf8_set(ud, *cp);
8044ffcb1c8Snicm 				cp++;
8054ffcb1c8Snicm 			}
8064ffcb1c8Snicm 
8071fabeb5aSnicm 			/* Draw the cell to the current screen. */
8084ffcb1c8Snicm 			screen_write_cell(&ctx[current], &sy.gc);
8094ffcb1c8Snicm 			width[current] += ud->width;
8104ffcb1c8Snicm 			continue;
8114ffcb1c8Snicm 		}
8124ffcb1c8Snicm 
8134ffcb1c8Snicm 		/* This is a style. Work out where the end is and parse it. */
8144ffcb1c8Snicm 		end = format_skip(cp + 2, "]");
815180e3f4cSnicm 		if (end == NULL) {
816967b71d8Snicm 			log_debug("%s: no terminating ] at '%s'", __func__,
817967b71d8Snicm 			    cp + 2);
818b6b6b1b7Snicm 			TAILQ_FOREACH_SAFE(fr, &frs, entry, fr1)
819b6b6b1b7Snicm 			    format_free_range(&frs, fr);
820b6b6b1b7Snicm 			goto out;
821180e3f4cSnicm 		}
8224ffcb1c8Snicm 		tmp = xstrndup(cp + 2, end - (cp + 2));
823c8f0e702Snicm 		style_copy(&saved_sy, &sy);
824c8f0e702Snicm 		if (style_parse(&sy, &current_default, tmp) != 0) {
825967b71d8Snicm 			log_debug("%s: invalid style '%s'", __func__, tmp);
8264ffcb1c8Snicm 			free(tmp);
82738c9d6a4Snicm 			cp = end + 1;
82838c9d6a4Snicm 			continue;
8294ffcb1c8Snicm 		}
830967b71d8Snicm 		log_debug("%s: style '%s' -> '%s'", __func__, tmp,
831967b71d8Snicm 		    style_tostring(&sy));
8324ffcb1c8Snicm 		free(tmp);
833173e8225Snicm 		if (default_colours) {
834173e8225Snicm 			sy.gc.bg = base->bg;
835173e8225Snicm 			sy.gc.fg = base->fg;
836173e8225Snicm 		}
8374ffcb1c8Snicm 
838db7cf71aSnicm 		/* If this style has a fill colour, store it for later. */
839db7cf71aSnicm 		if (sy.fill != 8)
840db7cf71aSnicm 			fill = sy.fill;
841db7cf71aSnicm 
842c8f0e702Snicm 		/* If this style pushed or popped the default, update it. */
843c8f0e702Snicm 		if (sy.default_type == STYLE_DEFAULT_PUSH) {
844523d1daeSnicm 			memcpy(&current_default, &saved_sy.gc,
845523d1daeSnicm 			    sizeof current_default);
846c8f0e702Snicm 			sy.default_type = STYLE_DEFAULT_BASE;
847c8f0e702Snicm 		} else if (sy.default_type == STYLE_DEFAULT_POP) {
848c8f0e702Snicm 			memcpy(&current_default, base, sizeof current_default);
849c8f0e702Snicm 			sy.default_type = STYLE_DEFAULT_BASE;
850c8f0e702Snicm 		}
851c8f0e702Snicm 
8524ffcb1c8Snicm 		/* Check the list state. */
8534ffcb1c8Snicm 		switch (sy.list) {
8544ffcb1c8Snicm 		case STYLE_LIST_ON:
8554ffcb1c8Snicm 			/*
8564ffcb1c8Snicm 			 * Entering the list, exiting a marker, or exiting the
8574ffcb1c8Snicm 			 * focus.
8584ffcb1c8Snicm 			 */
8594ffcb1c8Snicm 			if (list_state != 0) {
8604ffcb1c8Snicm 				if (fr != NULL) { /* abort any region */
8614ffcb1c8Snicm 					free(fr);
8624ffcb1c8Snicm 					fr = NULL;
8634ffcb1c8Snicm 				}
8644ffcb1c8Snicm 				list_state = 0;
8654ffcb1c8Snicm 				list_align = sy.align;
8664ffcb1c8Snicm 			}
8674ffcb1c8Snicm 
8684ffcb1c8Snicm 			/* End the focus if started. */
8694ffcb1c8Snicm 			if (focus_start != -1 && focus_end == -1)
8704ffcb1c8Snicm 				focus_end = s[LIST].cx;
8714ffcb1c8Snicm 
8724ffcb1c8Snicm 			current = LIST;
8734ffcb1c8Snicm 			break;
8744ffcb1c8Snicm 		case STYLE_LIST_FOCUS:
8754ffcb1c8Snicm 			/* Entering the focus. */
8764ffcb1c8Snicm 			if (list_state != 0) /* not inside the list */
8774ffcb1c8Snicm 				break;
8784ffcb1c8Snicm 			if (focus_start == -1) /* focus already started */
8794ffcb1c8Snicm 				focus_start = s[LIST].cx;
8804ffcb1c8Snicm 			break;
8814ffcb1c8Snicm 		case STYLE_LIST_OFF:
8824ffcb1c8Snicm 			/* Exiting or outside the list. */
8834ffcb1c8Snicm 			if (list_state == 0) {
8844ffcb1c8Snicm 				if (fr != NULL) { /* abort any region */
8854ffcb1c8Snicm 					free(fr);
8864ffcb1c8Snicm 					fr = NULL;
8874ffcb1c8Snicm 				}
8884ffcb1c8Snicm 				if (focus_start != -1 && focus_end == -1)
8894ffcb1c8Snicm 					focus_end = s[LIST].cx;
8904ffcb1c8Snicm 
8914ffcb1c8Snicm 				map[list_align] = AFTER;
8924ffcb1c8Snicm 				if (list_align == STYLE_ALIGN_LEFT)
8934ffcb1c8Snicm 					map[STYLE_ALIGN_DEFAULT] = AFTER;
8944ffcb1c8Snicm 				list_state = 1;
8954ffcb1c8Snicm 			}
8964ffcb1c8Snicm 			current = map[sy.align];
8974ffcb1c8Snicm 			break;
8984ffcb1c8Snicm 		case STYLE_LIST_LEFT_MARKER:
8994ffcb1c8Snicm 			/* Entering left marker. */
9004ffcb1c8Snicm 			if (list_state != 0) /* not inside the list */
9014ffcb1c8Snicm 				break;
9024ffcb1c8Snicm 			if (s[LIST_LEFT].cx != 0) /* already have marker */
9034ffcb1c8Snicm 				break;
9044ffcb1c8Snicm 			if (fr != NULL) { /* abort any region */
9054ffcb1c8Snicm 				free(fr);
9064ffcb1c8Snicm 				fr = NULL;
9074ffcb1c8Snicm 			}
9084ffcb1c8Snicm 			if (focus_start != -1 && focus_end == -1)
9094ffcb1c8Snicm 				focus_start = focus_end = -1;
9104ffcb1c8Snicm 			current = LIST_LEFT;
9114ffcb1c8Snicm 			break;
9124ffcb1c8Snicm 		case STYLE_LIST_RIGHT_MARKER:
9134ffcb1c8Snicm 			/* Entering right marker. */
9144ffcb1c8Snicm 			if (list_state != 0) /* not inside the list */
9154ffcb1c8Snicm 				break;
9164ffcb1c8Snicm 			if (s[LIST_RIGHT].cx != 0) /* already have marker */
9174ffcb1c8Snicm 				break;
9184ffcb1c8Snicm 			if (fr != NULL) { /* abort any region */
9194ffcb1c8Snicm 				free(fr);
9204ffcb1c8Snicm 				fr = NULL;
9214ffcb1c8Snicm 			}
9224ffcb1c8Snicm 			if (focus_start != -1 && focus_end == -1)
9234ffcb1c8Snicm 				focus_start = focus_end = -1;
9244ffcb1c8Snicm 			current = LIST_RIGHT;
9254ffcb1c8Snicm 			break;
9264ffcb1c8Snicm 		}
9274ffcb1c8Snicm 		if (current != last) {
9284ffcb1c8Snicm 			log_debug("%s: change %s -> %s", __func__,
9294ffcb1c8Snicm 			    names[last], names[current]);
9304ffcb1c8Snicm 			last = current;
9314ffcb1c8Snicm 		}
9324ffcb1c8Snicm 
9334ffcb1c8Snicm 		/*
9344ffcb1c8Snicm 		 * Check if the range style has changed and if so end the
9354ffcb1c8Snicm 		 * current range and start a new one if needed.
9364ffcb1c8Snicm 		 */
9374ffcb1c8Snicm 		if (srs != NULL) {
9384ffcb1c8Snicm 			if (fr != NULL && !format_is_type(fr, &sy)) {
9394ffcb1c8Snicm 				if (s[current].cx != fr->start) {
9404ffcb1c8Snicm 					fr->end = s[current].cx + 1;
9414ffcb1c8Snicm 					TAILQ_INSERT_TAIL(&frs, fr, entry);
9424ffcb1c8Snicm 				} else
9434ffcb1c8Snicm 					free(fr);
9444ffcb1c8Snicm 				fr = NULL;
9454ffcb1c8Snicm 			}
9464ffcb1c8Snicm 			if (fr == NULL && sy.range_type != STYLE_RANGE_NONE) {
9474ffcb1c8Snicm 				fr = xcalloc(1, sizeof *fr);
9484ffcb1c8Snicm 				fr->index = current;
9494ffcb1c8Snicm 
9504ffcb1c8Snicm 				fr->s = &s[current];
9514ffcb1c8Snicm 				fr->start = s[current].cx;
9524ffcb1c8Snicm 
9534ffcb1c8Snicm 				fr->type = sy.range_type;
9544ffcb1c8Snicm 				fr->argument = sy.range_argument;
955*5355e038Snicm 				strlcpy(fr->string, sy.range_string,
956*5355e038Snicm 				    sizeof fr->string);
9574ffcb1c8Snicm 			}
9584ffcb1c8Snicm 		}
9594ffcb1c8Snicm 
9604ffcb1c8Snicm 		cp = end + 1;
9614ffcb1c8Snicm 	}
9624ffcb1c8Snicm 	free(fr);
9634ffcb1c8Snicm 
964d680d99bSnicm 	for (i = 0; i < TOTAL; i++) {
965d680d99bSnicm 		screen_write_stop(&ctx[i]);
9664ffcb1c8Snicm 		log_debug("%s: width %s is %u", __func__, names[i], width[i]);
967d680d99bSnicm 	}
9684ffcb1c8Snicm 	if (focus_start != -1 && focus_end != -1)
969967b71d8Snicm 		log_debug("%s: focus %d-%d", __func__, focus_start, focus_end);
9704ffcb1c8Snicm 	TAILQ_FOREACH(fr, &frs, entry) {
9714ffcb1c8Snicm 		log_debug("%s: range %d|%u is %s %u-%u", __func__, fr->type,
9724ffcb1c8Snicm 		    fr->argument, names[fr->index], fr->start, fr->end);
9734ffcb1c8Snicm 	}
9744ffcb1c8Snicm 
975db7cf71aSnicm 	/* Clear the available area. */
976db7cf71aSnicm 	if (fill != -1) {
977db7cf71aSnicm 		memcpy(&gc, &grid_default_cell, sizeof gc);
978db7cf71aSnicm 		gc.bg = fill;
979db7cf71aSnicm 		for (i = 0; i < available; i++)
980db7cf71aSnicm 			screen_write_putc(octx, &gc, ' ');
981db7cf71aSnicm 	}
982db7cf71aSnicm 
9834ffcb1c8Snicm 	/*
9844ffcb1c8Snicm 	 * Draw the screens. How they are arranged depends on where the list
985a6c9106fSnicm 	 * appears.
9864ffcb1c8Snicm 	 */
9874ffcb1c8Snicm 	switch (list_align) {
9884ffcb1c8Snicm 	case STYLE_ALIGN_DEFAULT:
9894ffcb1c8Snicm 		/* No list. */
9904ffcb1c8Snicm 		format_draw_none(octx, available, ocx, ocy, &s[LEFT],
99184b35168Snicm 		    &s[CENTRE], &s[RIGHT], &s[ABSOLUTE_CENTRE], &frs);
9924ffcb1c8Snicm 		break;
9934ffcb1c8Snicm 	case STYLE_ALIGN_LEFT:
9944ffcb1c8Snicm 		/* List is part of the left. */
9954ffcb1c8Snicm 		format_draw_left(octx, available, ocx, ocy, &s[LEFT],
99684b35168Snicm 		    &s[CENTRE], &s[RIGHT], &s[ABSOLUTE_CENTRE], &s[LIST],
99784b35168Snicm 		    &s[LIST_LEFT], &s[LIST_RIGHT], &s[AFTER],
99884b35168Snicm 		    focus_start, focus_end, &frs);
9994ffcb1c8Snicm 		break;
10004ffcb1c8Snicm 	case STYLE_ALIGN_CENTRE:
10014ffcb1c8Snicm 		/* List is part of the centre. */
10024ffcb1c8Snicm 		format_draw_centre(octx, available, ocx, ocy, &s[LEFT],
100384b35168Snicm 		    &s[CENTRE], &s[RIGHT], &s[ABSOLUTE_CENTRE], &s[LIST],
100484b35168Snicm 		    &s[LIST_LEFT], &s[LIST_RIGHT], &s[AFTER],
100584b35168Snicm 		    focus_start, focus_end, &frs);
10064ffcb1c8Snicm 		break;
10074ffcb1c8Snicm 	case STYLE_ALIGN_RIGHT:
10084ffcb1c8Snicm 		/* List is part of the right. */
10094ffcb1c8Snicm 		format_draw_right(octx, available, ocx, ocy, &s[LEFT],
101084b35168Snicm 		    &s[CENTRE], &s[RIGHT], &s[ABSOLUTE_CENTRE], &s[LIST],
101184b35168Snicm 		    &s[LIST_LEFT], &s[LIST_RIGHT], &s[AFTER],
101284b35168Snicm 		    focus_start, focus_end, &frs);
101384b35168Snicm 		break;
101484b35168Snicm 	case STYLE_ALIGN_ABSOLUTE_CENTRE:
101584b35168Snicm 		/* List is in the centre of the entire horizontal space. */
101684b35168Snicm 		format_draw_absolute_centre(octx, available, ocx, ocy, &s[LEFT],
101784b35168Snicm 		    &s[CENTRE], &s[RIGHT], &s[ABSOLUTE_CENTRE], &s[LIST],
101884b35168Snicm 		    &s[LIST_LEFT], &s[LIST_RIGHT], &s[AFTER],
101984b35168Snicm 		    focus_start, focus_end, &frs);
10204ffcb1c8Snicm 		break;
10214ffcb1c8Snicm 	}
10224ffcb1c8Snicm 
10234ffcb1c8Snicm 	/* Create ranges to return. */
10244ffcb1c8Snicm 	TAILQ_FOREACH_SAFE(fr, &frs, entry, fr1) {
10254ffcb1c8Snicm 		sr = xcalloc(1, sizeof *sr);
10264ffcb1c8Snicm 		sr->type = fr->type;
10274ffcb1c8Snicm 		sr->argument = fr->argument;
1028*5355e038Snicm 		strlcpy(sr->string, fr->string, sizeof sr->string);
10294ffcb1c8Snicm 		sr->start = fr->start;
10304ffcb1c8Snicm 		sr->end = fr->end;
10314ffcb1c8Snicm 		TAILQ_INSERT_TAIL(srs, sr, entry);
10324ffcb1c8Snicm 
1033*5355e038Snicm 		switch (sr->type) {
1034*5355e038Snicm 		case STYLE_RANGE_NONE:
1035*5355e038Snicm 			break;
1036*5355e038Snicm 		case STYLE_RANGE_LEFT:
1037*5355e038Snicm 			log_debug("%s: range left at %u-%u", __func__,
1038*5355e038Snicm 			    sr->start, sr->end);
1039*5355e038Snicm 			break;
1040*5355e038Snicm 		case STYLE_RANGE_RIGHT:
1041*5355e038Snicm 			log_debug("%s: range right at %u-%u", __func__,
1042*5355e038Snicm 			    sr->start, sr->end);
1043*5355e038Snicm 			break;
1044*5355e038Snicm 		case STYLE_RANGE_PANE:
1045*5355e038Snicm 			log_debug("%s: range pane|%%%u at %u-%u", __func__,
10464ffcb1c8Snicm 			    sr->argument, sr->start, sr->end);
1047*5355e038Snicm 			break;
1048*5355e038Snicm 		case STYLE_RANGE_WINDOW:
1049*5355e038Snicm 			log_debug("%s: range window|%u at %u-%u", __func__,
1050*5355e038Snicm 			    sr->argument, sr->start, sr->end);
1051*5355e038Snicm 			break;
1052*5355e038Snicm 		case STYLE_RANGE_SESSION:
1053*5355e038Snicm 			log_debug("%s: range session|$%u at %u-%u", __func__,
1054*5355e038Snicm 			    sr->argument, sr->start, sr->end);
1055*5355e038Snicm 			break;
1056*5355e038Snicm 		case STYLE_RANGE_USER:
1057*5355e038Snicm 			log_debug("%s: range user|%u at %u-%u", __func__,
1058*5355e038Snicm 			    sr->argument, sr->start, sr->end);
1059*5355e038Snicm 			break;
1060*5355e038Snicm 		}
10614ffcb1c8Snicm 		format_free_range(&frs, fr);
10624ffcb1c8Snicm 	}
10634ffcb1c8Snicm 
1064b6b6b1b7Snicm out:
1065d680d99bSnicm 	/* Free the screens. */
1066d680d99bSnicm 	for (i = 0; i < TOTAL; i++)
1067d680d99bSnicm 		screen_free(&s[i]);
1068d680d99bSnicm 
10694ffcb1c8Snicm 	/* Restore the original cursor position. */
10704ffcb1c8Snicm 	screen_write_cursormove(octx, ocx, ocy, 0);
10714ffcb1c8Snicm }
10724ffcb1c8Snicm 
10734ffcb1c8Snicm /* Get width, taking #[] into account. */
10744ffcb1c8Snicm u_int
format_width(const char * expanded)10754ffcb1c8Snicm format_width(const char *expanded)
10764ffcb1c8Snicm {
10774ffcb1c8Snicm 	const char		*cp, *end;
107850e21e7bSnicm 	u_int			 n, leading_width, width = 0;
10794ffcb1c8Snicm 	struct utf8_data	 ud;
10804ffcb1c8Snicm 	enum utf8_state		 more;
10814ffcb1c8Snicm 
10824ffcb1c8Snicm 	cp = expanded;
10834ffcb1c8Snicm 	while (*cp != '\0') {
1084987e1c89Snicm 		if (*cp == '#') {
108550e21e7bSnicm 			end = format_leading_hashes(cp, &n, &leading_width);
108650e21e7bSnicm 			width += leading_width;
108750e21e7bSnicm 			cp = end;
108850e21e7bSnicm 			if (*cp == '#') {
10894ffcb1c8Snicm 				end = format_skip(cp + 2, "]");
10904ffcb1c8Snicm 				if (end == NULL)
1091acf6cf7cSnicm 					return (0);
10924ffcb1c8Snicm 				cp = end + 1;
109350e21e7bSnicm 			}
10944ffcb1c8Snicm 		} else if ((more = utf8_open(&ud, *cp)) == UTF8_MORE) {
10954ffcb1c8Snicm 			while (*++cp != '\0' && more == UTF8_MORE)
10964ffcb1c8Snicm 				more = utf8_append(&ud, *cp);
10974ffcb1c8Snicm 			if (more == UTF8_DONE)
10984ffcb1c8Snicm 				width += ud.width;
10994ffcb1c8Snicm 			else
11004ffcb1c8Snicm 				cp -= ud.have;
11014ffcb1c8Snicm 		} else if (*cp > 0x1f && *cp < 0x7f) {
11024ffcb1c8Snicm 			width++;
11034ffcb1c8Snicm 			cp++;
11048b65c88bSnicm 		} else
11058b65c88bSnicm 			cp++;
11064ffcb1c8Snicm 	}
11074ffcb1c8Snicm 	return (width);
11084ffcb1c8Snicm }
11094ffcb1c8Snicm 
1110987e1c89Snicm /*
1111987e1c89Snicm  * Trim on the left, taking #[] into account.  Note, we copy the whole set of
1112987e1c89Snicm  * unescaped #s, but only add their escaped size to width. This is because the
1113987e1c89Snicm  * format_draw function will actually do the escaping when it runs
1114987e1c89Snicm  */
11154ffcb1c8Snicm char *
format_trim_left(const char * expanded,u_int limit)11164ffcb1c8Snicm format_trim_left(const char *expanded, u_int limit)
11174ffcb1c8Snicm {
11184ffcb1c8Snicm 	char			*copy, *out;
11194ffcb1c8Snicm 	const char		*cp = expanded, *end;
112050e21e7bSnicm 	u_int			 n, width = 0, leading_width;
11214ffcb1c8Snicm 	struct utf8_data	 ud;
11224ffcb1c8Snicm 	enum utf8_state		 more;
11234ffcb1c8Snicm 
11240803d750Snicm 	out = copy = xcalloc(2, strlen(expanded) + 1);
11254ffcb1c8Snicm 	while (*cp != '\0') {
1126987e1c89Snicm 		if (width >= limit)
1127987e1c89Snicm 			break;
1128987e1c89Snicm 		if (*cp == '#') {
112950e21e7bSnicm 			end = format_leading_hashes(cp, &n, &leading_width);
113050e21e7bSnicm 			if (leading_width > limit - width)
113150e21e7bSnicm 				leading_width = limit - width;
113250e21e7bSnicm 			if (leading_width != 0) {
113350e21e7bSnicm 				if (n == 1)
113450e21e7bSnicm 					*out++ = '#';
113550e21e7bSnicm 				else {
113650e21e7bSnicm 					memset(out, '#', 2 * leading_width);
113750e21e7bSnicm 					out += 2 * leading_width;
113850e21e7bSnicm 				}
113950e21e7bSnicm 				width += leading_width;
114050e21e7bSnicm 			}
1141987e1c89Snicm 			cp = end;
114250e21e7bSnicm 			if (*cp == '#') {
11434ffcb1c8Snicm 				end = format_skip(cp + 2, "]");
11444ffcb1c8Snicm 				if (end == NULL)
11454ffcb1c8Snicm 					break;
11464ffcb1c8Snicm 				memcpy(out, cp, end + 1 - cp);
11474ffcb1c8Snicm 				out += (end + 1 - cp);
11484ffcb1c8Snicm 				cp = end + 1;
114950e21e7bSnicm 			}
11504ffcb1c8Snicm 		} else if ((more = utf8_open(&ud, *cp)) == UTF8_MORE) {
11514ffcb1c8Snicm 			while (*++cp != '\0' && more == UTF8_MORE)
11524ffcb1c8Snicm 				more = utf8_append(&ud, *cp);
11534ffcb1c8Snicm 			if (more == UTF8_DONE) {
11544ffcb1c8Snicm 				if (width + ud.width <= limit) {
11554ffcb1c8Snicm 					memcpy(out, ud.data, ud.size);
11564ffcb1c8Snicm 					out += ud.size;
11574ffcb1c8Snicm 				}
11584ffcb1c8Snicm 				width += ud.width;
11599446d752Snicm 			} else {
11604ffcb1c8Snicm 				cp -= ud.have;
11619446d752Snicm 				cp++;
11629446d752Snicm 			}
11634ffcb1c8Snicm 		} else if (*cp > 0x1f && *cp < 0x7f) {
11644ffcb1c8Snicm 			if (width + 1 <= limit)
11654ffcb1c8Snicm 				*out++ = *cp;
11664ffcb1c8Snicm 			width++;
11674ffcb1c8Snicm 			cp++;
1168770b648cSnicm 		} else
1169770b648cSnicm 			cp++;
11704ffcb1c8Snicm 	}
11714ffcb1c8Snicm 	*out = '\0';
11724ffcb1c8Snicm 	return (copy);
11734ffcb1c8Snicm }
11744ffcb1c8Snicm 
11754ffcb1c8Snicm /* Trim on the right, taking #[] into account. */
11764ffcb1c8Snicm char *
format_trim_right(const char * expanded,u_int limit)11774ffcb1c8Snicm format_trim_right(const char *expanded, u_int limit)
11784ffcb1c8Snicm {
11794ffcb1c8Snicm 	char			*copy, *out;
11804ffcb1c8Snicm 	const char		*cp = expanded, *end;
118150e21e7bSnicm 	u_int			 width = 0, total_width, skip, n;
118250e21e7bSnicm 	u_int			 leading_width, copy_width;
11834ffcb1c8Snicm 	struct utf8_data	 ud;
11844ffcb1c8Snicm 	enum utf8_state		 more;
11854ffcb1c8Snicm 
11864ffcb1c8Snicm 	total_width = format_width(expanded);
11874ffcb1c8Snicm 	if (total_width <= limit)
11884ffcb1c8Snicm 		return (xstrdup(expanded));
11894ffcb1c8Snicm 	skip = total_width - limit;
11904ffcb1c8Snicm 
11910803d750Snicm 	out = copy = xcalloc(2, strlen(expanded) + 1);
11924ffcb1c8Snicm 	while (*cp != '\0') {
1193987e1c89Snicm 		if (*cp == '#') {
119450e21e7bSnicm 			end = format_leading_hashes(cp, &n, &leading_width);
1195f45ad91fSnicm 			copy_width = leading_width;
1196987e1c89Snicm 			if (width <= skip) {
1197f45ad91fSnicm 				if (skip - width >= copy_width)
119850e21e7bSnicm 					copy_width = 0;
1199987e1c89Snicm 				else
120050e21e7bSnicm 					copy_width -= (skip - width);
1201f45ad91fSnicm 			}
120250e21e7bSnicm 			if (copy_width != 0) {
120350e21e7bSnicm 				if (n == 1)
120450e21e7bSnicm 					*out++ = '#';
120550e21e7bSnicm 				else {
120650e21e7bSnicm 					memset(out, '#', 2 * copy_width);
120750e21e7bSnicm 					out += 2 * copy_width;
1208987e1c89Snicm 				}
1209987e1c89Snicm 			}
121050e21e7bSnicm 			width += leading_width;
1211987e1c89Snicm 			cp = end;
121250e21e7bSnicm 			if (*cp == '#') {
12134ffcb1c8Snicm 				end = format_skip(cp + 2, "]");
121450e21e7bSnicm 				if (end == NULL)
12154ffcb1c8Snicm 					break;
12164ffcb1c8Snicm 				memcpy(out, cp, end + 1 - cp);
12174ffcb1c8Snicm 				out += (end + 1 - cp);
12184ffcb1c8Snicm 				cp = end + 1;
121950e21e7bSnicm 			}
12204ffcb1c8Snicm 		} else if ((more = utf8_open(&ud, *cp)) == UTF8_MORE) {
12214ffcb1c8Snicm 			while (*++cp != '\0' && more == UTF8_MORE)
12224ffcb1c8Snicm 				more = utf8_append(&ud, *cp);
12234ffcb1c8Snicm 			if (more == UTF8_DONE) {
12244ffcb1c8Snicm 				if (width >= skip) {
12254ffcb1c8Snicm 					memcpy(out, ud.data, ud.size);
12264ffcb1c8Snicm 					out += ud.size;
12274ffcb1c8Snicm 				}
12284ffcb1c8Snicm 				width += ud.width;
12299446d752Snicm 			} else {
12304ffcb1c8Snicm 				cp -= ud.have;
12319446d752Snicm 				cp++;
12329446d752Snicm 			}
12334ffcb1c8Snicm 		} else if (*cp > 0x1f && *cp < 0x7f) {
12344ffcb1c8Snicm 			if (width >= skip)
12354ffcb1c8Snicm 				*out++ = *cp;
12364ffcb1c8Snicm 			width++;
12374ffcb1c8Snicm 			cp++;
1238770b648cSnicm 		} else
1239770b648cSnicm 			cp++;
12404ffcb1c8Snicm 	}
12414ffcb1c8Snicm 	*out = '\0';
12424ffcb1c8Snicm 	return (copy);
12434ffcb1c8Snicm }
1244