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