xref: /openbsd-src/usr.bin/tmux/format-draw.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /* $OpenBSD: format-draw.c,v 1.14 2020/01/08 14:40:52 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "tmux.h"
25 
26 /* Format range. */
27 struct format_range {
28 	u_int				 index;
29 	struct screen			*s;
30 
31 	u_int				 start;
32 	u_int				 end;
33 
34 	enum style_range_type		 type;
35 	u_int				 argument;
36 
37 	TAILQ_ENTRY(format_range)	 entry;
38 };
39 TAILQ_HEAD(format_ranges, format_range);
40 
41 /* Does this range match this style? */
42 static int
43 format_is_type(struct format_range *fr, struct style *sy)
44 {
45 	if (fr->type != sy->range_type)
46 		return (0);
47 	if (fr->type == STYLE_RANGE_WINDOW &&
48 	    fr->argument != sy->range_argument)
49 		return (0);
50 	return (1);
51 }
52 
53 /* Free a range. */
54 static void
55 format_free_range(struct format_ranges *frs, struct format_range *fr)
56 {
57 	TAILQ_REMOVE(frs, fr, entry);
58 	free(fr);
59 }
60 
61 /* Fix range positions. */
62 static void
63 format_update_ranges(struct format_ranges *frs, struct screen *s, u_int offset,
64     u_int start, u_int width)
65 {
66 	struct format_range	*fr, *fr1;
67 
68 	if (frs == NULL)
69 		return;
70 
71 	TAILQ_FOREACH_SAFE(fr, frs, entry, fr1) {
72 		if (fr->s != s)
73 			continue;
74 
75 		if (fr->end <= start || fr->start >= start + width) {
76 			format_free_range(frs, fr);
77 			continue;
78 		}
79 
80 		if (fr->start < start)
81 			fr->start = start;
82 		if (fr->end > start + width)
83 			fr->end = start + width;
84 		if (fr->start == fr->end) {
85 			format_free_range(frs, fr);
86 			continue;
87 		}
88 
89 		fr->start -= start;
90 		fr->end -= start;
91 
92 		fr->start += offset;
93 		fr->end += offset;
94 	}
95 }
96 
97 /* Draw a part of the format. */
98 static void
99 format_draw_put(struct screen_write_ctx *octx, u_int ocx, u_int ocy,
100     struct screen *s, struct format_ranges *frs, u_int offset, u_int start,
101     u_int width)
102 {
103 	/*
104 	 * The offset is how far from the cursor on the target screen; start
105 	 * and width how much to copy from the source screen.
106 	 */
107 	screen_write_cursormove(octx, ocx + offset, ocy, 0);
108 	screen_write_fast_copy(octx, s, start, 0, width, 1);
109 	format_update_ranges(frs, s, offset, start, width);
110 }
111 
112 /* Draw list part of format. */
113 static void
114 format_draw_put_list(struct screen_write_ctx *octx,
115     u_int ocx, u_int ocy, u_int offset, u_int width, struct screen *list,
116     struct screen *list_left, struct screen *list_right, int focus_start,
117     int focus_end, struct format_ranges *frs)
118 {
119 	u_int	start, focus_centre;
120 
121 	/* If there is enough space for the list, draw it entirely. */
122 	if (width >= list->cx) {
123 		format_draw_put(octx, ocx, ocy, list, frs, offset, 0, width);
124 		return;
125 	}
126 
127 	/* The list needs to be trimmed. Try to keep the focus visible. */
128 	focus_centre = focus_start + (focus_end - focus_start) / 2;
129 	if (focus_centre < width / 2)
130 		start = 0;
131 	else
132 		start = focus_centre - width / 2;
133 	if (start + width > list->cx)
134 		start = list->cx - width;
135 
136 	/* Draw <> markers at either side if needed. */
137 	if (start != 0 && width > list_left->cx) {
138 		screen_write_cursormove(octx, ocx + offset, ocy, 0);
139 		screen_write_fast_copy(octx, list_left, 0, 0, list_left->cx, 1);
140 		offset += list_left->cx;
141 		start += list_left->cx;
142 		width -= list_left->cx;
143 	}
144 	if (start + width < list->cx && width > list_right->cx) {
145 		screen_write_cursormove(octx, ocx + offset + width - 1, ocy, 0);
146 		screen_write_fast_copy(octx, list_right, 0, 0, list_right->cx,
147 		    1);
148 		width -= list_right->cx;
149 	}
150 
151 	/* Draw the list screen itself. */
152 	format_draw_put(octx, ocx, ocy, list, frs, offset, start, width);
153 }
154 
155 /* Draw format with no list. */
156 static void
157 format_draw_none(struct screen_write_ctx *octx, u_int available, u_int ocx,
158     u_int ocy, struct screen *left, struct screen *centre, struct screen *right,
159     struct format_ranges *frs)
160 {
161 	u_int	width_left, width_centre, width_right;
162 
163 	width_left = left->cx;
164 	width_centre = centre->cx;
165 	width_right = right->cx;
166 
167 	/*
168 	 * Try to keep as much of the left and right as possible at the expense
169 	 * of the centre.
170 	 */
171 	while (width_left + width_centre + width_right > available) {
172 		if (width_centre > 0)
173 			width_centre--;
174 		else if (width_right > 0)
175 			width_right--;
176 		else
177 			width_left--;
178 	}
179 
180 	/* Write left. */
181 	format_draw_put(octx, ocx, ocy, left, frs, 0, 0, width_left);
182 
183 	/* Write right at available - width_right. */
184 	format_draw_put(octx, ocx, ocy, right, frs,
185 	    available - width_right,
186 	    right->cx - width_right,
187 	    width_right);
188 
189 	/*
190 	 * Write centre halfway between
191 	 *     width_left
192 	 * and
193 	 *     available - width_right.
194 	 */
195 	format_draw_put(octx, ocx, ocy, centre, frs,
196 	    width_left
197 	    + ((available - width_right) - width_left) / 2
198 	    - width_centre / 2,
199 	    centre->cx / 2 - width_centre / 2,
200 	    width_centre);
201 }
202 
203 /* Draw format with list on the left. */
204 static void
205 format_draw_left(struct screen_write_ctx *octx, u_int available, u_int ocx,
206     u_int ocy, struct screen *left, struct screen *centre, struct screen *right,
207     struct screen *list, struct screen *list_left, struct screen *list_right,
208     struct screen *after, int focus_start, int focus_end,
209     struct format_ranges *frs)
210 {
211 	u_int			width_left, width_centre, width_right;
212 	u_int			width_list, width_after;
213 	struct screen_write_ctx	ctx;
214 
215 	width_left = left->cx;
216 	width_centre = centre->cx;
217 	width_right = right->cx;
218 	width_list = list->cx;
219 	width_after = after->cx;
220 
221 	/*
222 	 * Trim first the centre, then the list, then the right, then after the
223 	 * list, then the left.
224 	 */
225 	while (width_left +
226 	    width_centre +
227 	    width_right +
228 	    width_list +
229 	    width_after > available) {
230 		if (width_centre > 0)
231 			width_centre--;
232 		else if (width_list > 0)
233 			width_list--;
234 		else if (width_right > 0)
235 			width_right--;
236 		else if (width_after > 0)
237 			width_after--;
238 		else
239 			width_left--;
240 	}
241 
242 	/* If there is no list left, pass off to the no list function. */
243 	if (width_list == 0) {
244 		screen_write_start(&ctx, NULL, left);
245 		screen_write_fast_copy(&ctx, after, 0, 0, width_after, 1);
246 		screen_write_stop(&ctx);
247 
248 		format_draw_none(octx, available, ocx, ocy, left, centre,
249 		    right, frs);
250 		return;
251 	}
252 
253 	/* Write left at 0. */
254 	format_draw_put(octx, ocx, ocy, left, frs, 0, 0, width_left);
255 
256 	/* Write right at available - width_right. */
257 	format_draw_put(octx, ocx, ocy, right, frs,
258 	    available - width_right,
259 	    right->cx - width_right,
260 	    width_right);
261 
262 	/* Write after at width_left + width_list. */
263 	format_draw_put(octx, ocx, ocy, after, frs,
264 	    width_left + width_list,
265 	    0,
266 	    width_after);
267 
268 	/*
269 	 * Write centre halfway between
270 	 *     width_left + width_list + width_after
271 	 * and
272 	 *     available - width_right.
273 	 */
274 	format_draw_put(octx, ocx, ocy, centre, frs,
275 	    (width_left + width_list + width_after)
276 	    + ((available - width_right)
277 		- (width_left + width_list + width_after)) / 2
278 	    - width_centre / 2,
279 	    centre->cx / 2 - width_centre / 2,
280 	    width_centre);
281 
282 	/*
283 	 * The list now goes from
284 	 *     width_left
285 	 * to
286 	 *     width_left + width_list.
287 	 * If there is no focus given, keep the left in focus.
288 	 */
289 	if (focus_start == -1 || focus_end == -1)
290 		focus_start = focus_end = 0;
291 	format_draw_put_list(octx, ocx, ocy, width_left, width_list, list,
292 	    list_left, list_right, focus_start, focus_end, frs);
293 }
294 
295 /* Draw format with list in the centre. */
296 static void
297 format_draw_centre(struct screen_write_ctx *octx, u_int available, u_int ocx,
298     u_int ocy, struct screen *left, struct screen *centre, struct screen *right,
299     struct screen *list, struct screen *list_left, struct screen *list_right,
300     struct screen *after, int focus_start, int focus_end,
301     struct format_ranges *frs)
302 {
303 	u_int			width_left, width_centre, width_right;
304 	u_int			width_list, width_after, middle;
305 	struct screen_write_ctx	ctx;
306 
307 	width_left = left->cx;
308 	width_centre = centre->cx;
309 	width_right = right->cx;
310 	width_list = list->cx;
311 	width_after = after->cx;
312 
313 	/*
314 	 * Trim first the list, then after the list, then the centre, then the
315 	 * right, then the left.
316 	 */
317 	while (width_left +
318 	    width_centre +
319 	    width_right +
320 	    width_list +
321 	    width_after > available) {
322 		if (width_list > 0)
323 			width_list--;
324 		else if (width_after > 0)
325 			width_after--;
326 		else if (width_centre > 0)
327 			width_centre--;
328 		else if (width_right > 0)
329 			width_right--;
330 		else
331 			width_left--;
332 	}
333 
334 	/* If there is no list left, pass off to the no list function. */
335 	if (width_list == 0) {
336 		screen_write_start(&ctx, NULL, centre);
337 		screen_write_fast_copy(&ctx, after, 0, 0, width_after, 1);
338 		screen_write_stop(&ctx);
339 
340 		format_draw_none(octx, available, ocx, ocy, left, centre,
341 		    right, frs);
342 		return;
343 	}
344 
345 	/* Write left at 0. */
346 	format_draw_put(octx, ocx, ocy, left, frs, 0, 0, width_left);
347 
348 	/* Write right at available - width_right. */
349 	format_draw_put(octx, ocx, ocy, right, frs,
350 	    available - width_right,
351 	    right->cx - width_right,
352 	    width_right);
353 
354 	/*
355 	 * All three centre sections are offset from the middle of the
356 	 * available space.
357 	 */
358 	middle = (width_left + ((available - width_right) - width_left) / 2);
359 
360 	/*
361 	 * Write centre at
362 	 *     middle - width_list / 2 - width_centre.
363 	 */
364 	format_draw_put(octx, ocx, ocy, centre, frs,
365 	    middle - width_list / 2 - width_centre,
366 	    0,
367 	    width_centre);
368 
369 	/*
370 	 * Write after at
371 	 *     middle - width_list / 2 + width_list
372 	 */
373 	format_draw_put(octx, ocx, ocy, after, frs,
374 	    middle - width_list / 2 + width_list,
375 	    0,
376 	    width_after);
377 
378 	/*
379 	 * The list now goes from
380 	 *     middle - width_list / 2
381 	 * to
382 	 *     middle + width_list / 2
383 	 * If there is no focus given, keep the centre in focus.
384 	 */
385 	if (focus_start == -1 || focus_end == -1)
386 		focus_start = focus_end = list->cx / 2;
387 	format_draw_put_list(octx, ocx, ocy, middle - width_list / 2,
388 	    width_list, list, list_left, list_right, focus_start, focus_end,
389 	    frs);
390 }
391 
392 /* Draw format with list on the right. */
393 static void
394 format_draw_right(struct screen_write_ctx *octx, u_int available, u_int ocx,
395     u_int ocy, struct screen *left, struct screen *centre, struct screen *right,
396     struct screen *list, struct screen *list_left, struct screen *list_right,
397     struct screen *after, int focus_start, int focus_end,
398     struct format_ranges *frs)
399 {
400 	u_int			width_left, width_centre, width_right;
401 	u_int			width_list, width_after;
402 	struct screen_write_ctx	ctx;
403 
404 	width_left = left->cx;
405 	width_centre = centre->cx;
406 	width_right = right->cx;
407 	width_list = list->cx;
408 	width_after = after->cx;
409 
410 	/*
411 	 * Trim first the centre, then the list, then the right, then
412 	 * after the list, then the left.
413 	 */
414 	while (width_left +
415 	    width_centre +
416 	    width_right +
417 	    width_list +
418 	    width_after > available) {
419 		if (width_centre > 0)
420 			width_centre--;
421 		else if (width_list > 0)
422 			width_list--;
423 		else if (width_right > 0)
424 			width_right--;
425 		else if (width_after > 0)
426 			width_after--;
427 		else
428 			width_left--;
429 	}
430 
431 	/* If there is no list left, pass off to the no list function. */
432 	if (width_list == 0) {
433 		screen_write_start(&ctx, NULL, right);
434 		screen_write_fast_copy(&ctx, after, 0, 0, width_after, 1);
435 		screen_write_stop(&ctx);
436 
437 		format_draw_none(octx, available, ocx, ocy, left, centre,
438 		    right, frs);
439 		return;
440 	}
441 
442 	/* Write left at 0. */
443 	format_draw_put(octx, ocx, ocy, left, frs, 0, 0, width_left);
444 
445 	/* Write after at available - width_after. */
446 	format_draw_put(octx, ocx, ocy, after, frs,
447 	    available - width_after,
448 	    after->cx - width_after,
449 	    width_after);
450 
451 	/*
452 	 * Write right at
453 	 *     available - width_right - width_list - width_after.
454 	 */
455 	format_draw_put(octx, ocx, ocy, right, frs,
456 	    available - width_right - width_list - width_after,
457 	    0,
458 	    width_right);
459 
460 	/*
461 	 * Write centre halfway between
462 	 *     width_left
463 	 * and
464 	 *     available - width_right - width_list - width_after.
465 	 */
466 	format_draw_put(octx, ocx, ocy, centre, frs,
467 	    width_left
468 	    + ((available - width_right - width_list - width_after)
469 		- width_left) / 2
470 	    - width_centre / 2,
471 	    centre->cx / 2 - width_centre / 2,
472 	    width_centre);
473 
474 	/*
475 	 * The list now goes from
476 	 *     available - width_list - width_after
477 	 * to
478 	 *     available - width_after
479 	 * If there is no focus given, keep the right in focus.
480 	 */
481 	if (focus_start == -1 || focus_end == -1)
482 		focus_start = focus_end = 0;
483 	format_draw_put_list(octx, ocx, ocy, available - width_list -
484 	    width_after, width_list, list, list_left, list_right, focus_start,
485 	    focus_end, frs);
486 }
487 
488 /* Draw a format to a screen. */
489 void
490 format_draw(struct screen_write_ctx *octx, const struct grid_cell *base,
491     u_int available, const char *expanded, struct style_ranges *srs)
492 {
493 	enum { LEFT,
494 	       CENTRE,
495 	       RIGHT,
496 	       LIST,
497 	       LIST_LEFT,
498 	       LIST_RIGHT,
499 	       AFTER,
500 	       TOTAL } current = LEFT, last = LEFT;
501 	const char	        *names[] = { "LEFT",
502 					     "CENTRE",
503 					     "RIGHT",
504 					     "LIST",
505 					     "LIST_LEFT",
506 					     "LIST_RIGHT",
507 					     "AFTER" };
508 	size_t			 size = strlen(expanded);
509 	struct screen		*os = octx->s, s[TOTAL];
510 	struct screen_write_ctx	 ctx[TOTAL];
511 	u_int			 ocx = os->cx, ocy = os->cy, i, width[TOTAL];
512 	u_int			 map[] = { LEFT, LEFT, CENTRE, RIGHT };
513 	int			 focus_start = -1, focus_end = -1;
514 	int			 list_state = -1, fill = -1;
515 	enum style_align	 list_align = STYLE_ALIGN_DEFAULT;
516 	struct grid_cell	 gc, current_default;
517 	struct style		 sy, saved_sy;
518 	struct utf8_data	*ud = &sy.gc.data;
519 	const char		*cp, *end;
520 	enum utf8_state		 more;
521 	char			*tmp;
522 	struct format_range	*fr = NULL, *fr1;
523 	struct format_ranges	 frs;
524 	struct style_range	*sr;
525 
526 	memcpy(&current_default, base, sizeof current_default);
527 	style_set(&sy, &current_default);
528 	TAILQ_INIT(&frs);
529 	log_debug("%s: %s", __func__, expanded);
530 
531 	/*
532 	 * We build three screens for left, right, centre alignment, one for
533 	 * the list, one for anything after the list and two for the list left
534 	 * and right markers.
535 	 */
536 	for (i = 0; i < TOTAL; i++) {
537 		screen_init(&s[i], size, 1, 0);
538 		screen_write_start(&ctx[i], NULL, &s[i]);
539 		screen_write_clearendofline(&ctx[i], current_default.bg);
540 		width[i] = 0;
541 	}
542 
543 	/*
544 	 * Walk the string and add to the corresponding screens,
545 	 * parsing styles as we go.
546 	 */
547 	cp = expanded;
548 	while (*cp != '\0') {
549 		if (cp[0] != '#' || cp[1] != '[') {
550 			/* See if this is a UTF-8 character. */
551 			if ((more = utf8_open(ud, *cp)) == UTF8_MORE) {
552 				while (*++cp != '\0' && more == UTF8_MORE)
553 					more = utf8_append(ud, *cp);
554 				if (more != UTF8_DONE)
555 					cp -= ud->have;
556 			}
557 
558 			/* Not a UTF-8 character - ASCII or not valid. */
559 			if (more != UTF8_DONE) {
560 				if (*cp < 0x20 || *cp > 0x7e) {
561 					/* Ignore nonprintable characters. */
562 					cp++;
563 					continue;
564 				}
565 				utf8_set(ud, *cp);
566 				cp++;
567 			}
568 
569 			/* Draw the cell to the current screen. */
570 			screen_write_cell(&ctx[current], &sy.gc);
571 			width[current] += ud->width;
572 			continue;
573 		}
574 
575 		/* This is a style. Work out where the end is and parse it. */
576 		end = format_skip(cp + 2, "]");
577 		if (end == NULL) {
578 			log_debug("%s: no terminating ] at '%s'", __func__,
579 			    cp + 2);
580 			TAILQ_FOREACH_SAFE(fr, &frs, entry, fr1)
581 			    format_free_range(&frs, fr);
582 			goto out;
583 		}
584 		tmp = xstrndup(cp + 2, end - (cp + 2));
585 		style_copy(&saved_sy, &sy);
586 		if (style_parse(&sy, &current_default, tmp) != 0) {
587 			log_debug("%s: invalid style '%s'", __func__, tmp);
588 			free(tmp);
589 			cp = end + 1;
590 			continue;
591 		}
592 		log_debug("%s: style '%s' -> '%s'", __func__, tmp,
593 		    style_tostring(&sy));
594 		free(tmp);
595 
596 		/* If this style has a fill colour, store it for later. */
597 		if (sy.fill != 8)
598 			fill = sy.fill;
599 
600 		/* If this style pushed or popped the default, update it. */
601 		if (sy.default_type == STYLE_DEFAULT_PUSH) {
602 			memcpy(&current_default, &saved_sy.gc, sizeof current_default);
603 			sy.default_type = STYLE_DEFAULT_BASE;
604 		} else if (sy.default_type == STYLE_DEFAULT_POP) {
605 			memcpy(&current_default, base, sizeof current_default);
606 			sy.default_type = STYLE_DEFAULT_BASE;
607 		}
608 
609 		/* Check the list state. */
610 		switch (sy.list) {
611 		case STYLE_LIST_ON:
612 			/*
613 			 * Entering the list, exiting a marker, or exiting the
614 			 * focus.
615 			 */
616 			if (list_state != 0) {
617 				if (fr != NULL) { /* abort any region */
618 					free(fr);
619 					fr = NULL;
620 				}
621 				list_state = 0;
622 				list_align = sy.align;
623 			}
624 
625 			/* End the focus if started. */
626 			if (focus_start != -1 && focus_end == -1)
627 				focus_end = s[LIST].cx;
628 
629 			current = LIST;
630 			break;
631 		case STYLE_LIST_FOCUS:
632 			/* Entering the focus. */
633 			if (list_state != 0) /* not inside the list */
634 				break;
635 			if (focus_start == -1) /* focus already started */
636 				focus_start = s[LIST].cx;
637 			break;
638 		case STYLE_LIST_OFF:
639 			/* Exiting or outside the list. */
640 			if (list_state == 0) {
641 				if (fr != NULL) { /* abort any region */
642 					free(fr);
643 					fr = NULL;
644 				}
645 				if (focus_start != -1 && focus_end == -1)
646 					focus_end = s[LIST].cx;
647 
648 				map[list_align] = AFTER;
649 				if (list_align == STYLE_ALIGN_LEFT)
650 					map[STYLE_ALIGN_DEFAULT] = AFTER;
651 				list_state = 1;
652 			}
653 			current = map[sy.align];
654 			break;
655 		case STYLE_LIST_LEFT_MARKER:
656 			/* Entering left marker. */
657 			if (list_state != 0) /* not inside the list */
658 				break;
659 			if (s[LIST_LEFT].cx != 0) /* already have marker */
660 				break;
661 			if (fr != NULL) { /* abort any region */
662 				free(fr);
663 				fr = NULL;
664 			}
665 			if (focus_start != -1 && focus_end == -1)
666 				focus_start = focus_end = -1;
667 			current = LIST_LEFT;
668 			break;
669 		case STYLE_LIST_RIGHT_MARKER:
670 			/* Entering right marker. */
671 			if (list_state != 0) /* not inside the list */
672 				break;
673 			if (s[LIST_RIGHT].cx != 0) /* already have marker */
674 				break;
675 			if (fr != NULL) { /* abort any region */
676 				free(fr);
677 				fr = NULL;
678 			}
679 			if (focus_start != -1 && focus_end == -1)
680 				focus_start = focus_end = -1;
681 			current = LIST_RIGHT;
682 			break;
683 		}
684 		if (current != last) {
685 			log_debug("%s: change %s -> %s", __func__,
686 			    names[last], names[current]);
687 			last = current;
688 		}
689 
690 		/*
691 		 * Check if the range style has changed and if so end the
692 		 * current range and start a new one if needed.
693 		 */
694 		if (srs != NULL) {
695 			if (fr != NULL && !format_is_type(fr, &sy)) {
696 				if (s[current].cx != fr->start) {
697 					fr->end = s[current].cx + 1;
698 					TAILQ_INSERT_TAIL(&frs, fr, entry);
699 				} else
700 					free(fr);
701 				fr = NULL;
702 			}
703 			if (fr == NULL && sy.range_type != STYLE_RANGE_NONE) {
704 				fr = xcalloc(1, sizeof *fr);
705 				fr->index = current;
706 
707 				fr->s = &s[current];
708 				fr->start = s[current].cx;
709 
710 				fr->type = sy.range_type;
711 				fr->argument = sy.range_argument;
712 			}
713 		}
714 
715 		cp = end + 1;
716 	}
717 	free(fr);
718 
719 	for (i = 0; i < TOTAL; i++) {
720 		screen_write_stop(&ctx[i]);
721 		log_debug("%s: width %s is %u", __func__, names[i], width[i]);
722 	}
723 	if (focus_start != -1 && focus_end != -1)
724 		log_debug("%s: focus %d-%d", __func__, focus_start, focus_end);
725 	TAILQ_FOREACH(fr, &frs, entry) {
726 		log_debug("%s: range %d|%u is %s %u-%u", __func__, fr->type,
727 		    fr->argument, names[fr->index], fr->start, fr->end);
728 	}
729 
730 	/* Clear the available area. */
731 	if (fill != -1) {
732 		memcpy(&gc, &grid_default_cell, sizeof gc);
733 		gc.bg = fill;
734 		for (i = 0; i < available; i++)
735 			screen_write_putc(octx, &gc, ' ');
736 	}
737 
738 	/*
739 	 * Draw the screens. How they are arranged depends on where the list
740 	 * appearsq.
741 	 */
742 	switch (list_align) {
743 	case STYLE_ALIGN_DEFAULT:
744 		/* No list. */
745 		format_draw_none(octx, available, ocx, ocy, &s[LEFT],
746 		    &s[CENTRE], &s[RIGHT], &frs);
747 		break;
748 	case STYLE_ALIGN_LEFT:
749 		/* List is part of the left. */
750 		format_draw_left(octx, available, ocx, ocy, &s[LEFT],
751 		    &s[CENTRE], &s[RIGHT], &s[LIST], &s[LIST_LEFT],
752 		    &s[LIST_RIGHT], &s[AFTER], focus_start, focus_end, &frs);
753 		break;
754 	case STYLE_ALIGN_CENTRE:
755 		/* List is part of the centre. */
756 		format_draw_centre(octx, available, ocx, ocy, &s[LEFT],
757 		    &s[CENTRE], &s[RIGHT], &s[LIST], &s[LIST_LEFT],
758 		    &s[LIST_RIGHT], &s[AFTER], focus_start, focus_end, &frs);
759 		break;
760 	case STYLE_ALIGN_RIGHT:
761 		/* List is part of the right. */
762 		format_draw_right(octx, available, ocx, ocy, &s[LEFT],
763 		    &s[CENTRE], &s[RIGHT], &s[LIST], &s[LIST_LEFT],
764 		    &s[LIST_RIGHT], &s[AFTER], focus_start, focus_end, &frs);
765 		break;
766 	}
767 
768 	/* Create ranges to return. */
769 	TAILQ_FOREACH_SAFE(fr, &frs, entry, fr1) {
770 		sr = xcalloc(1, sizeof *sr);
771 		sr->type = fr->type;
772 		sr->argument = fr->argument;
773 		sr->start = fr->start;
774 		sr->end = fr->end;
775 		TAILQ_INSERT_TAIL(srs, sr, entry);
776 
777 		log_debug("%s: range %d|%u at %u-%u", __func__, sr->type,
778 		    sr->argument, sr->start, sr->end);
779 
780 		format_free_range(&frs, fr);
781 	}
782 
783 out:
784 	/* Free the screens. */
785 	for (i = 0; i < TOTAL; i++)
786 		screen_free(&s[i]);
787 
788 	/* Restore the original cursor position. */
789 	screen_write_cursormove(octx, ocx, ocy, 0);
790 }
791 
792 /* Get width, taking #[] into account. */
793 u_int
794 format_width(const char *expanded)
795 {
796 	const char		*cp, *end;
797 	u_int			 width = 0;
798 	struct utf8_data	 ud;
799 	enum utf8_state		 more;
800 
801 	cp = expanded;
802 	while (*cp != '\0') {
803 		if (cp[0] == '#' && cp[1] == '[') {
804 			end = format_skip(cp + 2, "]");
805 			if (end == NULL)
806 				return 0;
807 			cp = end + 1;
808 		} else if ((more = utf8_open(&ud, *cp)) == UTF8_MORE) {
809 			while (*++cp != '\0' && more == UTF8_MORE)
810 				more = utf8_append(&ud, *cp);
811 			if (more == UTF8_DONE)
812 				width += ud.width;
813 			else
814 				cp -= ud.have;
815 		} else if (*cp > 0x1f && *cp < 0x7f) {
816 			width++;
817 			cp++;
818 		} else
819 			cp++;
820 	}
821 	return (width);
822 }
823 
824 /* Trim on the left, taking #[] into account. */
825 char *
826 format_trim_left(const char *expanded, u_int limit)
827 {
828 	char			*copy, *out;
829 	const char		*cp = expanded, *end;
830 	u_int			 width = 0;
831 	struct utf8_data	 ud;
832 	enum utf8_state		 more;
833 
834 	out = copy = xmalloc(strlen(expanded) + 1);
835 	while (*cp != '\0') {
836 		if (cp[0] == '#' && cp[1] == '[') {
837 			end = format_skip(cp + 2, "]");
838 			if (end == NULL)
839 				break;
840 			memcpy(out, cp, end + 1 - cp);
841 			out += (end + 1 - cp);
842 			cp = end + 1;
843 		} else if ((more = utf8_open(&ud, *cp)) == UTF8_MORE) {
844 			while (*++cp != '\0' && more == UTF8_MORE)
845 				more = utf8_append(&ud, *cp);
846 			if (more == UTF8_DONE) {
847 				if (width + ud.width <= limit) {
848 					memcpy(out, ud.data, ud.size);
849 					out += ud.size;
850 				}
851 				width += ud.width;
852 			} else {
853 				cp -= ud.have;
854 				cp++;
855 			}
856 		} else if (*cp > 0x1f && *cp < 0x7f) {
857 			if (width + 1 <= limit)
858 				*out++ = *cp;
859 			width++;
860 			cp++;
861 		} else
862 			cp++;
863 	}
864 	*out = '\0';
865 	return (copy);
866 }
867 
868 /* Trim on the right, taking #[] into account. */
869 char *
870 format_trim_right(const char *expanded, u_int limit)
871 {
872 	char			*copy, *out;
873 	const char		*cp = expanded, *end;
874 	u_int			 width = 0, total_width, skip;
875 	struct utf8_data	 ud;
876 	enum utf8_state		 more;
877 
878 	total_width = format_width(expanded);
879 	if (total_width <= limit)
880 		return (xstrdup(expanded));
881 	skip = total_width - limit;
882 
883 	out = copy = xmalloc(strlen(expanded) + 1);
884 	while (*cp != '\0') {
885 		if (cp[0] == '#' && cp[1] == '[') {
886 			end = format_skip(cp + 2, "]");
887 			if (end == NULL)
888 				break;
889 			memcpy(out, cp, end + 1 - cp);
890 			out += (end + 1 - cp);
891 			cp = end + 1;
892 		} else if ((more = utf8_open(&ud, *cp)) == UTF8_MORE) {
893 			while (*++cp != '\0' && more == UTF8_MORE)
894 				more = utf8_append(&ud, *cp);
895 			if (more == UTF8_DONE) {
896 				if (width >= skip) {
897 					memcpy(out, ud.data, ud.size);
898 					out += ud.size;
899 				}
900 				width += ud.width;
901 			} else {
902 				cp -= ud.have;
903 				cp++;
904 			}
905 		} else if (*cp > 0x1f && *cp < 0x7f) {
906 			if (width >= skip)
907 				*out++ = *cp;
908 			width++;
909 			cp++;
910 		} else
911 			cp++;
912 	}
913 	*out = '\0';
914 	return (copy);
915 }
916