xref: /netbsd-src/external/bsd/tmux/dist/format-draw.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /* $OpenBSD$ */
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;
517 	struct style		 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 	style_set(&sy, base);
527 	TAILQ_INIT(&frs);
528 	log_debug("%s: %s", __func__, expanded);
529 
530 	/*
531 	 * We build three screens for left, right, centre alignment, one for
532 	 * the list, one for anything after the list and two for the list left
533 	 * and right markers.
534 	 */
535 	for (i = 0; i < TOTAL; i++) {
536 		screen_init(&s[i], size, 1, 0);
537 		screen_write_start(&ctx[i], NULL, &s[i]);
538 		screen_write_clearendofline(&ctx[i], base->bg);
539 		width[i] = 0;
540 	}
541 
542 	/*
543 	 * Walk the string and add to the corresponding screens,
544 	 * parsing styles as we go.
545 	 */
546 	cp = expanded;
547 	while (*cp != '\0') {
548 		if (cp[0] != '#' || cp[1] != '[') {
549 			/* See if this is a UTF-8 character. */
550 			if ((more = utf8_open(ud, *cp)) == UTF8_MORE) {
551 				while (*++cp != '\0' && more == UTF8_MORE)
552 					more = utf8_append(ud, *cp);
553 				if (more != UTF8_DONE)
554 					cp -= ud->have;
555 			}
556 
557 			/* Not a UTF-8 character - ASCII or not valid. */
558 			if (more != UTF8_DONE) {
559 				if (*cp < 0x20 || *cp > 0x7e) {
560 					/* Ignore nonprintable characters. */
561 					cp++;
562 					continue;
563 				}
564 				utf8_set(ud, *cp);
565 				cp++;
566 			}
567 
568 			/* Draw the cell to the current screen. */
569 			screen_write_cell(&ctx[current], &sy.gc);
570 			width[current] += ud->width;
571 			continue;
572 		}
573 
574 		/* This is a style. Work out where the end is and parse it. */
575 		end = format_skip(cp + 2, "]");
576 		if (end == NULL) {
577 			log_debug("%s: no terminating ] at '%s'", __func__,
578 			    cp + 2);
579 			TAILQ_FOREACH_SAFE(fr, &frs, entry, fr1)
580 			    format_free_range(&frs, fr);
581 			goto out;
582 		}
583 		tmp = xstrndup(cp + 2, end - (cp + 2));
584 		if (style_parse(&sy, base, tmp) != 0) {
585 			log_debug("%s: invalid style '%s'", __func__, tmp);
586 			free(tmp);
587 			cp = end + 1;
588 			continue;
589 		}
590 		log_debug("%s: style '%s' -> '%s'", __func__, tmp,
591 		    style_tostring(&sy));
592 		free(tmp);
593 
594 		/* If this style has a fill colour, store it for later. */
595 		if (sy.fill != 8)
596 			fill = sy.fill;
597 
598 		/* Check the list state. */
599 		switch (sy.list) {
600 		case STYLE_LIST_ON:
601 			/*
602 			 * Entering the list, exiting a marker, or exiting the
603 			 * focus.
604 			 */
605 			if (list_state != 0) {
606 				if (fr != NULL) { /* abort any region */
607 					free(fr);
608 					fr = NULL;
609 				}
610 				list_state = 0;
611 				list_align = sy.align;
612 			}
613 
614 			/* End the focus if started. */
615 			if (focus_start != -1 && focus_end == -1)
616 				focus_end = s[LIST].cx;
617 
618 			current = LIST;
619 			break;
620 		case STYLE_LIST_FOCUS:
621 			/* Entering the focus. */
622 			if (list_state != 0) /* not inside the list */
623 				break;
624 			if (focus_start == -1) /* focus already started */
625 				focus_start = s[LIST].cx;
626 			break;
627 		case STYLE_LIST_OFF:
628 			/* Exiting or outside the list. */
629 			if (list_state == 0) {
630 				if (fr != NULL) { /* abort any region */
631 					free(fr);
632 					fr = NULL;
633 				}
634 				if (focus_start != -1 && focus_end == -1)
635 					focus_end = s[LIST].cx;
636 
637 				map[list_align] = AFTER;
638 				if (list_align == STYLE_ALIGN_LEFT)
639 					map[STYLE_ALIGN_DEFAULT] = AFTER;
640 				list_state = 1;
641 			}
642 			current = map[sy.align];
643 			break;
644 		case STYLE_LIST_LEFT_MARKER:
645 			/* Entering left marker. */
646 			if (list_state != 0) /* not inside the list */
647 				break;
648 			if (s[LIST_LEFT].cx != 0) /* already have marker */
649 				break;
650 			if (fr != NULL) { /* abort any region */
651 				free(fr);
652 				fr = NULL;
653 			}
654 			if (focus_start != -1 && focus_end == -1)
655 				focus_start = focus_end = -1;
656 			current = LIST_LEFT;
657 			break;
658 		case STYLE_LIST_RIGHT_MARKER:
659 			/* Entering right marker. */
660 			if (list_state != 0) /* not inside the list */
661 				break;
662 			if (s[LIST_RIGHT].cx != 0) /* already have marker */
663 				break;
664 			if (fr != NULL) { /* abort any region */
665 				free(fr);
666 				fr = NULL;
667 			}
668 			if (focus_start != -1 && focus_end == -1)
669 				focus_start = focus_end = -1;
670 			current = LIST_RIGHT;
671 			break;
672 		}
673 		if (current != last) {
674 			log_debug("%s: change %s -> %s", __func__,
675 			    names[last], names[current]);
676 			last = current;
677 		}
678 
679 		/*
680 		 * Check if the range style has changed and if so end the
681 		 * current range and start a new one if needed.
682 		 */
683 		if (srs != NULL) {
684 			if (fr != NULL && !format_is_type(fr, &sy)) {
685 				if (s[current].cx != fr->start) {
686 					fr->end = s[current].cx + 1;
687 					TAILQ_INSERT_TAIL(&frs, fr, entry);
688 				} else
689 					free(fr);
690 				fr = NULL;
691 			}
692 			if (fr == NULL && sy.range_type != STYLE_RANGE_NONE) {
693 				fr = xcalloc(1, sizeof *fr);
694 				fr->index = current;
695 
696 				fr->s = &s[current];
697 				fr->start = s[current].cx;
698 
699 				fr->type = sy.range_type;
700 				fr->argument = sy.range_argument;
701 			}
702 		}
703 
704 		cp = end + 1;
705 	}
706 	free(fr);
707 
708 	for (i = 0; i < TOTAL; i++) {
709 		screen_write_stop(&ctx[i]);
710 		log_debug("%s: width %s is %u", __func__, names[i], width[i]);
711 	}
712 	if (focus_start != -1 && focus_end != -1)
713 		log_debug("%s: focus %d-%d", __func__, focus_start, focus_end);
714 	TAILQ_FOREACH(fr, &frs, entry) {
715 		log_debug("%s: range %d|%u is %s %u-%u", __func__, fr->type,
716 		    fr->argument, names[fr->index], fr->start, fr->end);
717 	}
718 
719 	/* Clear the available area. */
720 	if (fill != -1) {
721 		memcpy(&gc, &grid_default_cell, sizeof gc);
722 		gc.bg = fill;
723 		for (i = 0; i < available; i++)
724 			screen_write_putc(octx, &gc, ' ');
725 	}
726 
727 	/*
728 	 * Draw the screens. How they are arranged depends on where the list
729 	 * appearsq.
730 	 */
731 	switch (list_align) {
732 	case STYLE_ALIGN_DEFAULT:
733 		/* No list. */
734 		format_draw_none(octx, available, ocx, ocy, &s[LEFT],
735 		    &s[CENTRE], &s[RIGHT], &frs);
736 		break;
737 	case STYLE_ALIGN_LEFT:
738 		/* List is part of the left. */
739 		format_draw_left(octx, available, ocx, ocy, &s[LEFT],
740 		    &s[CENTRE], &s[RIGHT], &s[LIST], &s[LIST_LEFT],
741 		    &s[LIST_RIGHT], &s[AFTER], focus_start, focus_end, &frs);
742 		break;
743 	case STYLE_ALIGN_CENTRE:
744 		/* List is part of the centre. */
745 		format_draw_centre(octx, available, ocx, ocy, &s[LEFT],
746 		    &s[CENTRE], &s[RIGHT], &s[LIST], &s[LIST_LEFT],
747 		    &s[LIST_RIGHT], &s[AFTER], focus_start, focus_end, &frs);
748 		break;
749 	case STYLE_ALIGN_RIGHT:
750 		/* List is part of the right. */
751 		format_draw_right(octx, available, ocx, ocy, &s[LEFT],
752 		    &s[CENTRE], &s[RIGHT], &s[LIST], &s[LIST_LEFT],
753 		    &s[LIST_RIGHT], &s[AFTER], focus_start, focus_end, &frs);
754 		break;
755 	}
756 
757 	/* Create ranges to return. */
758 	TAILQ_FOREACH_SAFE(fr, &frs, entry, fr1) {
759 		sr = xcalloc(1, sizeof *sr);
760 		sr->type = fr->type;
761 		sr->argument = fr->argument;
762 		sr->start = fr->start;
763 		sr->end = fr->end;
764 		TAILQ_INSERT_TAIL(srs, sr, entry);
765 
766 		log_debug("%s: range %d|%u at %u-%u", __func__, sr->type,
767 		    sr->argument, sr->start, sr->end);
768 
769 		format_free_range(&frs, fr);
770 	}
771 
772 out:
773 	/* Free the screens. */
774 	for (i = 0; i < TOTAL; i++)
775 		screen_free(&s[i]);
776 
777 	/* Restore the original cursor position. */
778 	screen_write_cursormove(octx, ocx, ocy, 0);
779 }
780 
781 /* Get width, taking #[] into account. */
782 u_int
783 format_width(const char *expanded)
784 {
785 	const char		*cp, *end;
786 	u_int			 width = 0;
787 	struct utf8_data	 ud;
788 	enum utf8_state		 more;
789 
790 	cp = expanded;
791 	while (*cp != '\0') {
792 		if (cp[0] == '#' && cp[1] == '[') {
793 			end = format_skip(cp + 2, "]");
794 			if (end == NULL)
795 				return 0;
796 			cp = end + 1;
797 		} else if ((more = utf8_open(&ud, *cp)) == UTF8_MORE) {
798 			while (*++cp != '\0' && more == UTF8_MORE)
799 				more = utf8_append(&ud, *cp);
800 			if (more == UTF8_DONE)
801 				width += ud.width;
802 			else
803 				cp -= ud.have;
804 		} else if (*cp > 0x1f && *cp < 0x7f) {
805 			width++;
806 			cp++;
807 		} else
808 			cp++;
809 	}
810 	return (width);
811 }
812 
813 /* Trim on the left, taking #[] into account. */
814 char *
815 format_trim_left(const char *expanded, u_int limit)
816 {
817 	char			*copy, *out;
818 	const char		*cp = expanded, *end;
819 	u_int			 width = 0;
820 	struct utf8_data	 ud;
821 	enum utf8_state		 more;
822 
823 	out = copy = xmalloc(strlen(expanded) + 1);
824 	while (*cp != '\0') {
825 		if (cp[0] == '#' && cp[1] == '[') {
826 			end = format_skip(cp + 2, "]");
827 			if (end == NULL)
828 				break;
829 			memcpy(out, cp, end + 1 - cp);
830 			out += (end + 1 - cp);
831 			cp = end + 1;
832 		} else if ((more = utf8_open(&ud, *cp)) == UTF8_MORE) {
833 			while (*++cp != '\0' && more == UTF8_MORE)
834 				more = utf8_append(&ud, *cp);
835 			if (more == UTF8_DONE) {
836 				if (width + ud.width <= limit) {
837 					memcpy(out, ud.data, ud.size);
838 					out += ud.size;
839 				}
840 				width += ud.width;
841 			} else
842 				cp -= ud.have;
843 		} else if (*cp > 0x1f && *cp < 0x7f) {
844 			if (width + 1 <= limit)
845 				*out++ = *cp;
846 			width++;
847 			cp++;
848 		} else
849 			cp++;
850 	}
851 	*out = '\0';
852 	return (copy);
853 }
854 
855 /* Trim on the right, taking #[] into account. */
856 char *
857 format_trim_right(const char *expanded, u_int limit)
858 {
859 	char			*copy, *out;
860 	const char		*cp = expanded, *end;
861 	u_int			 width = 0, total_width, skip;
862 	struct utf8_data	 ud;
863 	enum utf8_state		 more;
864 
865 	total_width = format_width(expanded);
866 	if (total_width <= limit)
867 		return (xstrdup(expanded));
868 	skip = total_width - limit;
869 
870 	out = copy = xmalloc(strlen(expanded) + 1);
871 	while (*cp != '\0') {
872 		if (cp[0] == '#' && cp[1] == '[') {
873 			end = format_skip(cp + 2, "]");
874 			if (end == NULL)
875 				break;
876 			memcpy(out, cp, end + 1 - cp);
877 			out += (end + 1 - cp);
878 			cp = end + 1;
879 		} else if ((more = utf8_open(&ud, *cp)) == UTF8_MORE) {
880 			while (*++cp != '\0' && more == UTF8_MORE)
881 				more = utf8_append(&ud, *cp);
882 			if (more == UTF8_DONE) {
883 				if (width >= skip) {
884 					memcpy(out, ud.data, ud.size);
885 					out += ud.size;
886 				}
887 				width += ud.width;
888 			} else
889 				cp -= ud.have;
890 		} else if (*cp > 0x1f && *cp < 0x7f) {
891 			if (width >= skip)
892 				*out++ = *cp;
893 			width++;
894 			cp++;
895 		} else
896 			cp++;
897 	}
898 	*out = '\0';
899 	return (copy);
900 }
901