Annotation of src/usr.bin/tmux/grid.c, Revision 1.106
1.106 ! nicm 1: /* $OpenBSD: grid.c,v 1.105 2020/04/09 13:53:50 nicm Exp $ */
1.1 nicm 2:
3: /*
1.50 nicm 4: * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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:
1.20 nicm 21: #include <stdlib.h>
1.1 nicm 22: #include <string.h>
23:
24: #include "tmux.h"
25:
26: /*
27: * Grid data. This is the basic data structure that represents what is shown on
28: * screen.
29: *
30: * A grid is a grid of cells (struct grid_cell). Lines are not allocated until
31: * cells in that line are written to. The grid is split into history and
32: * viewable data with the history starting at row (line) 0 and extending to
33: * (hsize - 1); from hsize to hsize + (sy - 1) is the viewable data. All
34: * functions in this file work on absolute coordinates, grid-view.c has
35: * functions which work on the screen data.
36: */
37:
38: /* Default grid cell data. */
1.48 nicm 39: const struct grid_cell grid_default_cell = {
1.97 nicm 40: { { ' ' }, 0, 1, 1 }, 0, 0, 8, 8, 0
1.48 nicm 41: };
1.89 nicm 42:
43: /* Cleared grid cell data. */
44: const struct grid_cell grid_cleared_cell = {
1.97 nicm 45: { { ' ' }, 0, 1, 1 }, 0, GRID_FLAG_CLEARED, 8, 8, 0
1.89 nicm 46: };
47: static const struct grid_cell_entry grid_cleared_entry = {
48: GRID_FLAG_CLEARED, { .data = { 0, 8, 8, ' ' } }
1.48 nicm 49: };
1.1 nicm 50:
1.63 nicm 51: /* Store cell in entry. */
52: static void
53: grid_store_cell(struct grid_cell_entry *gce, const struct grid_cell *gc,
54: u_char c)
55: {
1.89 nicm 56: gce->flags = (gc->flags & ~GRID_FLAG_CLEARED);
1.63 nicm 57:
58: gce->data.fg = gc->fg & 0xff;
59: if (gc->fg & COLOUR_FLAG_256)
60: gce->flags |= GRID_FLAG_FG256;
61:
62: gce->data.bg = gc->bg & 0xff;
63: if (gc->bg & COLOUR_FLAG_256)
64: gce->flags |= GRID_FLAG_BG256;
65:
66: gce->data.attr = gc->attr;
67: gce->data.data = c;
68: }
69:
1.89 nicm 70: /* Check if a cell should be an extended cell. */
1.65 nicm 71: static int
72: grid_need_extended_cell(const struct grid_cell_entry *gce,
73: const struct grid_cell *gc)
74: {
75: if (gce->flags & GRID_FLAG_EXTENDED)
76: return (1);
1.68 nicm 77: if (gc->attr > 0xff)
78: return (1);
1.65 nicm 79: if (gc->data.size != 1 || gc->data.width != 1)
80: return (1);
1.69 nicm 81: if ((gc->fg & COLOUR_FLAG_RGB) || (gc->bg & COLOUR_FLAG_RGB))
1.96 nicm 82: return (1);
83: if (gc->us != 0) /* only supports 256 or RGB */
1.65 nicm 84: return (1);
85: return (0);
86: }
87:
1.89 nicm 88: /* Get an extended cell. */
89: static void
90: grid_get_extended_cell(struct grid_line *gl, struct grid_cell_entry *gce,
91: int flags)
92: {
93: u_int at = gl->extdsize + 1;
94:
95: gl->extddata = xreallocarray(gl->extddata, at, sizeof *gl->extddata);
96: gl->extdsize = at;
97:
98: gce->offset = at - 1;
99: gce->flags = (flags | GRID_FLAG_EXTENDED);
100: }
101:
102: /* Set cell as extended. */
103: static struct grid_cell *
104: grid_extended_cell(struct grid_line *gl, struct grid_cell_entry *gce,
105: const struct grid_cell *gc)
106: {
107: struct grid_cell *gcp;
108: int flags = (gc->flags & ~GRID_FLAG_CLEARED);
109:
110: if (~gce->flags & GRID_FLAG_EXTENDED)
111: grid_get_extended_cell(gl, gce, flags);
112: else if (gce->offset >= gl->extdsize)
113: fatalx("offset too big");
114: gl->flags |= GRID_LINE_EXTENDED;
115:
116: gcp = &gl->extddata[gce->offset];
117: memcpy(gcp, gc, sizeof *gcp);
118: gcp->flags = flags;
119: return (gcp);
120: }
121:
1.76 nicm 122: /* Free up unused extended cells. */
123: static void
124: grid_compact_line(struct grid_line *gl)
125: {
126: int new_extdsize = 0;
127: struct grid_cell *new_extddata;
128: struct grid_cell_entry *gce;
129: struct grid_cell *gc;
130: u_int px, idx;
131:
132: if (gl->extdsize == 0)
133: return;
134:
135: for (px = 0; px < gl->cellsize; px++) {
136: gce = &gl->celldata[px];
137: if (gce->flags & GRID_FLAG_EXTENDED)
138: new_extdsize++;
139: }
140:
141: if (new_extdsize == 0) {
142: free(gl->extddata);
143: gl->extddata = NULL;
144: gl->extdsize = 0;
145: return;
146: }
147: new_extddata = xreallocarray(NULL, new_extdsize, sizeof *gl->extddata);
148:
149: idx = 0;
150: for (px = 0; px < gl->cellsize; px++) {
151: gce = &gl->celldata[px];
152: if (gce->flags & GRID_FLAG_EXTENDED) {
153: gc = &gl->extddata[gce->offset];
154: memcpy(&new_extddata[idx], gc, sizeof *gc);
155: gce->offset = idx++;
156: }
157: }
158:
159: free(gl->extddata);
160: gl->extddata = new_extddata;
161: gl->extdsize = new_extdsize;
162: }
163:
1.85 nicm 164: /* Get line data. */
1.84 nicm 165: struct grid_line *
166: grid_get_line(struct grid *gd, u_int line)
167: {
168: return (&gd->linedata[line]);
169: }
170:
1.85 nicm 171: /* Adjust number of lines. */
1.84 nicm 172: void
173: grid_adjust_lines(struct grid *gd, u_int lines)
174: {
175: gd->linedata = xreallocarray(gd->linedata, lines, sizeof *gd->linedata);
1.59 nicm 176: }
177:
1.48 nicm 178: /* Copy default into a cell. */
179: static void
1.58 nicm 180: grid_clear_cell(struct grid *gd, u_int px, u_int py, u_int bg)
1.48 nicm 181: {
1.59 nicm 182: struct grid_line *gl = &gd->linedata[py];
183: struct grid_cell_entry *gce = &gl->celldata[px];
184: struct grid_cell *gc;
185:
1.89 nicm 186: memcpy(gce, &grid_cleared_entry, sizeof *gce);
1.100 nicm 187: if (bg != 8) {
188: if (bg & COLOUR_FLAG_RGB) {
189: grid_get_extended_cell(gl, gce, gce->flags);
190: gl->flags |= GRID_LINE_EXTENDED;
191:
192: gc = &gl->extddata[gce->offset];
193: memcpy(gc, &grid_cleared_cell, sizeof *gc);
194: gc->bg = bg;
195: } else {
196: if (bg & COLOUR_FLAG_256)
197: gce->flags |= GRID_FLAG_BG256;
198: gce->data.bg = bg;
199: }
1.59 nicm 200: }
1.48 nicm 201: }
202:
1.43 nicm 203: /* Check grid y position. */
1.54 nicm 204: static int
1.101 nicm 205: grid_check_y(struct grid *gd, const char *from, u_int py)
1.1 nicm 206: {
1.78 nicm 207: if (py >= gd->hsize + gd->sy) {
1.81 nicm 208: log_debug("%s: y out of range: %u", from, py);
1.1 nicm 209: return (-1);
210: }
211: return (0);
212: }
1.23 nicm 213:
1.54 nicm 214: /* Compare grid cells. Return 1 if equal, 0 if not. */
215: int
216: grid_cells_equal(const struct grid_cell *gca, const struct grid_cell *gcb)
217: {
218: if (gca->fg != gcb->fg || gca->bg != gcb->bg)
219: return (0);
220: if (gca->attr != gcb->attr || gca->flags != gcb->flags)
221: return (0);
222: if (gca->data.width != gcb->data.width)
223: return (0);
224: if (gca->data.size != gcb->data.size)
225: return (0);
226: return (memcmp(gca->data.data, gcb->data.data, gca->data.size) == 0);
227: }
228:
1.75 nicm 229: /* Free one line. */
230: static void
231: grid_free_line(struct grid *gd, u_int py)
232: {
233: free(gd->linedata[py].celldata);
234: gd->linedata[py].celldata = NULL;
235: free(gd->linedata[py].extddata);
236: gd->linedata[py].extddata = NULL;
237: }
238:
239: /* Free several lines. */
240: static void
241: grid_free_lines(struct grid *gd, u_int py, u_int ny)
242: {
243: u_int yy;
244:
245: for (yy = py; yy < py + ny; yy++)
246: grid_free_line(gd, yy);
247: }
248:
1.1 nicm 249: /* Create a new grid. */
250: struct grid *
251: grid_create(u_int sx, u_int sy, u_int hlimit)
252: {
253: struct grid *gd;
254:
255: gd = xmalloc(sizeof *gd);
256: gd->sx = sx;
257: gd->sy = sy;
258:
1.102 nicm 259: if (hlimit != 0)
260: gd->flags = GRID_HISTORY;
261: else
262: gd->flags = 0;
1.7 nicm 263:
1.55 nicm 264: gd->hscrolled = 0;
1.1 nicm 265: gd->hsize = 0;
266: gd->hlimit = hlimit;
267:
1.80 nicm 268: if (gd->sy != 0)
269: gd->linedata = xcalloc(gd->sy, sizeof *gd->linedata);
270: else
271: gd->linedata = NULL;
1.1 nicm 272:
273: return (gd);
274: }
275:
276: /* Destroy grid. */
277: void
278: grid_destroy(struct grid *gd)
279: {
1.75 nicm 280: grid_free_lines(gd, 0, gd->hsize + gd->sy);
1.1 nicm 281:
1.20 nicm 282: free(gd->linedata);
1.1 nicm 283:
1.20 nicm 284: free(gd);
1.1 nicm 285: }
286:
287: /* Compare grids. */
288: int
289: grid_compare(struct grid *ga, struct grid *gb)
290: {
1.10 nicm 291: struct grid_line *gla, *glb;
1.48 nicm 292: struct grid_cell gca, gcb;
1.1 nicm 293: u_int xx, yy;
294:
1.33 nicm 295: if (ga->sx != gb->sx || ga->sy != gb->sy)
1.1 nicm 296: return (1);
297:
298: for (yy = 0; yy < ga->sy; yy++) {
1.10 nicm 299: gla = &ga->linedata[yy];
300: glb = &gb->linedata[yy];
301: if (gla->cellsize != glb->cellsize)
1.1 nicm 302: return (1);
1.48 nicm 303: for (xx = 0; xx < gla->cellsize; xx++) {
304: grid_get_cell(ga, xx, yy, &gca);
305: grid_get_cell(gb, xx, yy, &gcb);
1.54 nicm 306: if (!grid_cells_equal(&gca, &gcb))
1.1 nicm 307: return (1);
308: }
309: }
310:
311: return (0);
312: }
313:
1.86 nicm 314: /* Trim lines from the history. */
315: static void
316: grid_trim_history(struct grid *gd, u_int ny)
317: {
318: grid_free_lines(gd, 0, ny);
319: memmove(&gd->linedata[0], &gd->linedata[ny],
320: (gd->hsize + gd->sy - ny) * (sizeof *gd->linedata));
321: }
322:
1.15 nicm 323: /*
324: * Collect lines from the history if at the limit. Free the top (oldest) 10%
325: * and shift up.
326: */
1.1 nicm 327: void
1.75 nicm 328: grid_collect_history(struct grid *gd)
1.1 nicm 329: {
1.75 nicm 330: u_int ny;
1.1 nicm 331:
1.77 nicm 332: if (gd->hsize == 0 || gd->hsize < gd->hlimit)
1.15 nicm 333: return;
334:
1.75 nicm 335: ny = gd->hlimit / 10;
336: if (ny < 1)
337: ny = 1;
1.77 nicm 338: if (ny > gd->hsize)
339: ny = gd->hsize;
1.75 nicm 340:
341: /*
342: * Free the lines from 0 to ny then move the remaining lines over
343: * them.
344: */
1.86 nicm 345: grid_trim_history(gd, ny);
1.15 nicm 346:
1.75 nicm 347: gd->hsize -= ny;
1.55 nicm 348: if (gd->hscrolled > gd->hsize)
349: gd->hscrolled = gd->hsize;
1.104 nicm 350: }
351:
352: /* Remove lines from the bottom of the history. */
353: void
354: grid_remove_history(struct grid *gd, u_int ny)
355: {
356: u_int yy;
357:
358: if (ny > gd->hsize)
359: return;
360: for (yy = 0; yy < ny; yy++)
361: grid_free_line(gd, gd->hsize + gd->sy - 1 - yy);
362: gd->hsize -= ny;
1.15 nicm 363: }
364:
1.17 nicm 365: /*
1.15 nicm 366: * Scroll the entire visible screen, moving one line into the history. Just
367: * allocate a new line at the bottom and move the history size indicator.
368: */
369: void
1.58 nicm 370: grid_scroll_history(struct grid *gd, u_int bg)
1.15 nicm 371: {
372: u_int yy;
1.1 nicm 373:
374: yy = gd->hsize + gd->sy;
1.41 nicm 375: gd->linedata = xreallocarray(gd->linedata, yy + 1,
376: sizeof *gd->linedata);
1.58 nicm 377: grid_empty_line(gd, yy, bg);
1.17 nicm 378:
1.55 nicm 379: gd->hscrolled++;
1.76 nicm 380: grid_compact_line(&gd->linedata[gd->hsize]);
1.15 nicm 381: gd->hsize++;
382: }
1.1 nicm 383:
1.46 nicm 384: /* Clear the history. */
385: void
386: grid_clear_history(struct grid *gd)
387: {
1.86 nicm 388: grid_trim_history(gd, gd->hsize);
1.46 nicm 389:
1.55 nicm 390: gd->hscrolled = 0;
1.46 nicm 391: gd->hsize = 0;
1.55 nicm 392:
1.46 nicm 393: gd->linedata = xreallocarray(gd->linedata, gd->sy,
394: sizeof *gd->linedata);
395: }
396:
1.15 nicm 397: /* Scroll a region up, moving the top line into the history. */
398: void
1.71 nicm 399: grid_scroll_history_region(struct grid *gd, u_int upper, u_int lower, u_int bg)
1.15 nicm 400: {
1.73 nicm 401: struct grid_line *gl_history, *gl_upper;
1.15 nicm 402: u_int yy;
403:
404: /* Create a space for a new line. */
405: yy = gd->hsize + gd->sy;
1.41 nicm 406: gd->linedata = xreallocarray(gd->linedata, yy + 1,
407: sizeof *gd->linedata);
1.1 nicm 408:
1.15 nicm 409: /* Move the entire screen down to free a space for this line. */
410: gl_history = &gd->linedata[gd->hsize];
411: memmove(gl_history + 1, gl_history, gd->sy * sizeof *gl_history);
412:
413: /* Adjust the region and find its start and end. */
414: upper++;
415: gl_upper = &gd->linedata[upper];
416: lower++;
417:
418: /* Move the line into the history. */
419: memcpy(gl_history, gl_upper, sizeof *gl_history);
420:
421: /* Then move the region up and clear the bottom line. */
422: memmove(gl_upper, gl_upper + 1, (lower - upper) * sizeof *gl_upper);
1.71 nicm 423: grid_empty_line(gd, lower, bg);
1.15 nicm 424:
425: /* Move the history offset down over the line. */
1.55 nicm 426: gd->hscrolled++;
1.1 nicm 427: gd->hsize++;
428: }
429:
430: /* Expand line to fit to cell. */
1.57 nicm 431: static void
1.58 nicm 432: grid_expand_line(struct grid *gd, u_int py, u_int sx, u_int bg)
1.1 nicm 433: {
1.10 nicm 434: struct grid_line *gl;
1.14 nicm 435: u_int xx;
1.1 nicm 436:
1.10 nicm 437: gl = &gd->linedata[py];
1.14 nicm 438: if (sx <= gl->cellsize)
1.1 nicm 439: return;
1.61 nicm 440:
1.62 nicm 441: if (sx < gd->sx / 4)
442: sx = gd->sx / 4;
443: else if (sx < gd->sx / 2)
444: sx = gd->sx / 2;
445: else
446: sx = gd->sx;
1.1 nicm 447:
1.41 nicm 448: gl->celldata = xreallocarray(gl->celldata, sx, sizeof *gl->celldata);
1.10 nicm 449: for (xx = gl->cellsize; xx < sx; xx++)
1.58 nicm 450: grid_clear_cell(gd, xx, py, bg);
1.10 nicm 451: gl->cellsize = sx;
1.1 nicm 452: }
453:
1.58 nicm 454: /* Empty a line and set background colour if needed. */
1.106 ! nicm 455: void
1.58 nicm 456: grid_empty_line(struct grid *gd, u_int py, u_int bg)
457: {
458: memset(&gd->linedata[py], 0, sizeof gd->linedata[py]);
1.88 nicm 459: if (!COLOUR_DEFAULT(bg))
1.58 nicm 460: grid_expand_line(gd, py, gd->sx, bg);
461: }
462:
1.26 nicm 463: /* Peek at grid line. */
464: const struct grid_line *
465: grid_peek_line(struct grid *gd, u_int py)
466: {
1.81 nicm 467: if (grid_check_y(gd, __func__, py) != 0)
1.26 nicm 468: return (NULL);
469: return (&gd->linedata[py]);
470: }
471:
1.79 nicm 472: /* Get cell from line. */
473: static void
474: grid_get_cell1(struct grid_line *gl, u_int px, struct grid_cell *gc)
1.1 nicm 475: {
1.79 nicm 476: struct grid_cell_entry *gce = &gl->celldata[px];
1.1 nicm 477:
1.48 nicm 478: if (gce->flags & GRID_FLAG_EXTENDED) {
479: if (gce->offset >= gl->extdsize)
480: memcpy(gc, &grid_default_cell, sizeof *gc);
481: else
482: memcpy(gc, &gl->extddata[gce->offset], sizeof *gc);
483: return;
484: }
1.1 nicm 485:
1.53 nicm 486: gc->flags = gce->flags & ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
1.48 nicm 487: gc->attr = gce->data.attr;
488: gc->fg = gce->data.fg;
1.53 nicm 489: if (gce->flags & GRID_FLAG_FG256)
490: gc->fg |= COLOUR_FLAG_256;
1.48 nicm 491: gc->bg = gce->data.bg;
1.53 nicm 492: if (gce->flags & GRID_FLAG_BG256)
493: gc->bg |= COLOUR_FLAG_256;
1.97 nicm 494: gc->us = 0;
1.48 nicm 495: utf8_set(&gc->data, gce->data.data);
1.1 nicm 496: }
497:
1.79 nicm 498: /* Get cell for reading. */
499: void
500: grid_get_cell(struct grid *gd, u_int px, u_int py, struct grid_cell *gc)
501: {
1.81 nicm 502: if (grid_check_y(gd, __func__, py) != 0 ||
1.90 nicm 503: px >= gd->linedata[py].cellsize)
1.79 nicm 504: memcpy(gc, &grid_default_cell, sizeof *gc);
1.90 nicm 505: else
506: grid_get_cell1(&gd->linedata[py], px, gc);
1.79 nicm 507: }
508:
1.1 nicm 509: /* Set cell at relative position. */
510: void
1.31 nicm 511: grid_set_cell(struct grid *gd, u_int px, u_int py, const struct grid_cell *gc)
1.1 nicm 512: {
1.48 nicm 513: struct grid_line *gl;
514: struct grid_cell_entry *gce;
515:
1.81 nicm 516: if (grid_check_y(gd, __func__, py) != 0)
1.1 nicm 517: return;
518:
1.58 nicm 519: grid_expand_line(gd, py, px + 1, 8);
1.48 nicm 520:
521: gl = &gd->linedata[py];
1.58 nicm 522: if (px + 1 > gl->cellused)
523: gl->cellused = px + 1;
524:
1.63 nicm 525: gce = &gl->celldata[px];
1.65 nicm 526: if (grid_need_extended_cell(gce, gc))
1.59 nicm 527: grid_extended_cell(gl, gce, gc);
1.63 nicm 528: else
529: grid_store_cell(gce, gc, gc->data.data[0]);
1.64 nicm 530: }
531:
532: /* Set cells at relative position. */
533: void
534: grid_set_cells(struct grid *gd, u_int px, u_int py, const struct grid_cell *gc,
535: const char *s, size_t slen)
536: {
537: struct grid_line *gl;
538: struct grid_cell_entry *gce;
539: struct grid_cell *gcp;
540: u_int i;
541:
1.81 nicm 542: if (grid_check_y(gd, __func__, py) != 0)
1.64 nicm 543: return;
544:
545: grid_expand_line(gd, py, px + slen, 8);
546:
547: gl = &gd->linedata[py];
548: if (px + slen > gl->cellused)
549: gl->cellused = px + slen;
550:
551: for (i = 0; i < slen; i++) {
552: gce = &gl->celldata[px + i];
1.65 nicm 553: if (grid_need_extended_cell(gce, gc)) {
554: gcp = grid_extended_cell(gl, gce, gc);
1.64 nicm 555: utf8_set(&gcp->data, s[i]);
556: } else
557: grid_store_cell(gce, gc, s[i]);
558: }
1.1 nicm 559: }
560:
1.14 nicm 561: /* Clear area. */
1.1 nicm 562: void
1.58 nicm 563: grid_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny, u_int bg)
1.1 nicm 564: {
1.88 nicm 565: struct grid_line *gl;
1.98 nicm 566: u_int xx, yy, ox, sx;
1.1 nicm 567:
568: if (nx == 0 || ny == 0)
569: return;
570:
571: if (px == 0 && nx == gd->sx) {
1.58 nicm 572: grid_clear_lines(gd, py, ny, bg);
1.1 nicm 573: return;
574: }
575:
1.81 nicm 576: if (grid_check_y(gd, __func__, py) != 0)
1.1 nicm 577: return;
1.81 nicm 578: if (grid_check_y(gd, __func__, py + ny - 1) != 0)
1.1 nicm 579: return;
580:
581: for (yy = py; yy < py + ny; yy++) {
1.88 nicm 582: gl = &gd->linedata[yy];
1.98 nicm 583:
584: sx = gd->sx;
585: if (sx > gl->cellsize)
586: sx = gl->cellsize;
587: ox = nx;
588: if (COLOUR_DEFAULT(bg)) {
589: if (px > sx)
590: continue;
591: if (px + nx > sx)
592: ox = sx - px;
1.14 nicm 593: }
1.98 nicm 594:
595: grid_expand_line(gd, yy, px + ox, 8); /* default bg first */
596: for (xx = px; xx < px + ox; xx++)
1.58 nicm 597: grid_clear_cell(gd, xx, yy, bg);
1.1 nicm 598: }
599: }
600:
601: /* Clear lines. This just frees and truncates the lines. */
602: void
1.58 nicm 603: grid_clear_lines(struct grid *gd, u_int py, u_int ny, u_int bg)
1.1 nicm 604: {
1.75 nicm 605: u_int yy;
1.1 nicm 606:
607: if (ny == 0)
608: return;
609:
1.81 nicm 610: if (grid_check_y(gd, __func__, py) != 0)
1.1 nicm 611: return;
1.81 nicm 612: if (grid_check_y(gd, __func__, py + ny - 1) != 0)
1.1 nicm 613: return;
614:
615: for (yy = py; yy < py + ny; yy++) {
1.75 nicm 616: grid_free_line(gd, yy);
1.58 nicm 617: grid_empty_line(gd, yy, bg);
1.1 nicm 618: }
619: }
620:
621: /* Move a group of lines. */
622: void
1.58 nicm 623: grid_move_lines(struct grid *gd, u_int dy, u_int py, u_int ny, u_int bg)
1.1 nicm 624: {
625: u_int yy;
626:
627: if (ny == 0 || py == dy)
628: return;
629:
1.81 nicm 630: if (grid_check_y(gd, __func__, py) != 0)
1.1 nicm 631: return;
1.81 nicm 632: if (grid_check_y(gd, __func__, py + ny - 1) != 0)
1.1 nicm 633: return;
1.81 nicm 634: if (grid_check_y(gd, __func__, dy) != 0)
1.1 nicm 635: return;
1.81 nicm 636: if (grid_check_y(gd, __func__, dy + ny - 1) != 0)
1.1 nicm 637: return;
638:
639: /* Free any lines which are being replaced. */
640: for (yy = dy; yy < dy + ny; yy++) {
641: if (yy >= py && yy < py + ny)
642: continue;
1.75 nicm 643: grid_free_line(gd, yy);
1.1 nicm 644: }
645:
1.46 nicm 646: memmove(&gd->linedata[dy], &gd->linedata[py],
647: ny * (sizeof *gd->linedata));
1.1 nicm 648:
1.75 nicm 649: /*
650: * Wipe any lines that have been moved (without freeing them - they are
651: * still present).
652: */
1.1 nicm 653: for (yy = py; yy < py + ny; yy++) {
1.58 nicm 654: if (yy < dy || yy >= dy + ny)
655: grid_empty_line(gd, yy, bg);
1.1 nicm 656: }
657: }
658:
659: /* Move a group of cells. */
660: void
1.58 nicm 661: grid_move_cells(struct grid *gd, u_int dx, u_int px, u_int py, u_int nx,
662: u_int bg)
1.1 nicm 663: {
1.10 nicm 664: struct grid_line *gl;
665: u_int xx;
1.1 nicm 666:
667: if (nx == 0 || px == dx)
668: return;
669:
1.81 nicm 670: if (grid_check_y(gd, __func__, py) != 0)
1.1 nicm 671: return;
1.10 nicm 672: gl = &gd->linedata[py];
1.1 nicm 673:
1.58 nicm 674: grid_expand_line(gd, py, px + nx, 8);
675: grid_expand_line(gd, py, dx + nx, 8);
1.46 nicm 676: memmove(&gl->celldata[dx], &gl->celldata[px],
677: nx * sizeof *gl->celldata);
1.67 nicm 678: if (dx + nx > gl->cellused)
679: gl->cellused = dx + nx;
1.1 nicm 680:
681: /* Wipe any cells that have been moved. */
682: for (xx = px; xx < px + nx; xx++) {
683: if (xx >= dx && xx < dx + nx)
684: continue;
1.58 nicm 685: grid_clear_cell(gd, xx, py, bg);
1.1 nicm 686: }
1.3 nicm 687: }
688:
1.24 nicm 689: /* Get ANSI foreground sequence. */
1.56 nicm 690: static size_t
1.24 nicm 691: grid_string_cells_fg(const struct grid_cell *gc, int *values)
692: {
693: size_t n;
1.53 nicm 694: u_char r, g, b;
1.24 nicm 695:
696: n = 0;
1.53 nicm 697: if (gc->fg & COLOUR_FLAG_256) {
1.24 nicm 698: values[n++] = 38;
699: values[n++] = 5;
1.53 nicm 700: values[n++] = gc->fg & 0xff;
701: } else if (gc->fg & COLOUR_FLAG_RGB) {
1.52 nicm 702: values[n++] = 38;
703: values[n++] = 2;
1.53 nicm 704: colour_split_rgb(gc->fg, &r, &g, &b);
705: values[n++] = r;
706: values[n++] = g;
707: values[n++] = b;
1.24 nicm 708: } else {
709: switch (gc->fg) {
1.45 nicm 710: case 0:
711: case 1:
712: case 2:
713: case 3:
714: case 4:
715: case 5:
716: case 6:
717: case 7:
718: values[n++] = gc->fg + 30;
719: break;
720: case 8:
721: values[n++] = 39;
722: break;
723: case 90:
724: case 91:
725: case 92:
726: case 93:
727: case 94:
728: case 95:
729: case 96:
730: case 97:
731: values[n++] = gc->fg;
732: break;
1.24 nicm 733: }
734: }
735: return (n);
736: }
737:
738: /* Get ANSI background sequence. */
1.56 nicm 739: static size_t
1.24 nicm 740: grid_string_cells_bg(const struct grid_cell *gc, int *values)
741: {
742: size_t n;
1.53 nicm 743: u_char r, g, b;
1.24 nicm 744:
745: n = 0;
1.53 nicm 746: if (gc->bg & COLOUR_FLAG_256) {
1.24 nicm 747: values[n++] = 48;
748: values[n++] = 5;
1.53 nicm 749: values[n++] = gc->bg & 0xff;
750: } else if (gc->bg & COLOUR_FLAG_RGB) {
1.52 nicm 751: values[n++] = 48;
752: values[n++] = 2;
1.53 nicm 753: colour_split_rgb(gc->bg, &r, &g, &b);
754: values[n++] = r;
755: values[n++] = g;
756: values[n++] = b;
1.24 nicm 757: } else {
758: switch (gc->bg) {
759: case 0:
760: case 1:
761: case 2:
762: case 3:
763: case 4:
764: case 5:
765: case 6:
766: case 7:
767: values[n++] = gc->bg + 40;
768: break;
769: case 8:
770: values[n++] = 49;
771: break;
1.103 nicm 772: case 90:
773: case 91:
774: case 92:
775: case 93:
776: case 94:
777: case 95:
778: case 96:
779: case 97:
780: values[n++] = gc->bg + 10;
1.24 nicm 781: break;
782: }
783: }
784: return (n);
785: }
786:
787: /*
788: * Returns ANSI code to set particular attributes (colour, bold and so on)
1.74 nicm 789: * given a current state.
1.24 nicm 790: */
1.56 nicm 791: static void
1.24 nicm 792: grid_string_cells_code(const struct grid_cell *lastgc,
1.26 nicm 793: const struct grid_cell *gc, char *buf, size_t len, int escape_c0)
1.24 nicm 794: {
1.52 nicm 795: int oldc[64], newc[64], s[128];
1.24 nicm 796: size_t noldc, nnewc, n, i;
1.70 nicm 797: u_int attr = gc->attr, lastattr = lastgc->attr;
1.24 nicm 798: char tmp[64];
799:
800: struct {
801: u_int mask;
802: u_int code;
803: } attrs[] = {
804: { GRID_ATTR_BRIGHT, 1 },
805: { GRID_ATTR_DIM, 2 },
806: { GRID_ATTR_ITALICS, 3 },
807: { GRID_ATTR_UNDERSCORE, 4 },
808: { GRID_ATTR_BLINK, 5 },
809: { GRID_ATTR_REVERSE, 7 },
1.68 nicm 810: { GRID_ATTR_HIDDEN, 8 },
1.87 nicm 811: { GRID_ATTR_STRIKETHROUGH, 9 },
812: { GRID_ATTR_UNDERSCORE_2, 42 },
813: { GRID_ATTR_UNDERSCORE_3, 43 },
814: { GRID_ATTR_UNDERSCORE_4, 44 },
815: { GRID_ATTR_UNDERSCORE_5, 45 },
1.94 nicm 816: { GRID_ATTR_OVERLINE, 53 },
1.24 nicm 817: };
818: n = 0;
819:
820: /* If any attribute is removed, begin with 0. */
821: for (i = 0; i < nitems(attrs); i++) {
822: if (!(attr & attrs[i].mask) && (lastattr & attrs[i].mask)) {
823: s[n++] = 0;
1.25 nicm 824: lastattr &= GRID_ATTR_CHARSET;
1.24 nicm 825: break;
826: }
827: }
828: /* For each attribute that is newly set, add its code. */
829: for (i = 0; i < nitems(attrs); i++) {
830: if ((attr & attrs[i].mask) && !(lastattr & attrs[i].mask))
831: s[n++] = attrs[i].code;
832: }
833:
1.70 nicm 834: /* Write the attributes. */
835: *buf = '\0';
836: if (n > 0) {
837: if (escape_c0)
838: strlcat(buf, "\\033[", len);
839: else
840: strlcat(buf, "\033[", len);
841: for (i = 0; i < n; i++) {
1.87 nicm 842: if (s[i] < 10)
1.70 nicm 843: xsnprintf(tmp, sizeof tmp, "%d", s[i]);
1.87 nicm 844: else {
845: xsnprintf(tmp, sizeof tmp, "%d:%d", s[i] / 10,
846: s[i] % 10);
847: }
1.70 nicm 848: strlcat(buf, tmp, len);
1.87 nicm 849: if (i + 1 < n)
850: strlcat(buf, ";", len);
1.70 nicm 851: }
852: strlcat(buf, "m", len);
853: }
854:
855: /* If the foreground colour changed, write its parameters. */
1.24 nicm 856: nnewc = grid_string_cells_fg(gc, newc);
857: noldc = grid_string_cells_fg(lastgc, oldc);
1.70 nicm 858: if (nnewc != noldc ||
859: memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0 ||
860: (n != 0 && s[0] == 0)) {
861: if (escape_c0)
862: strlcat(buf, "\\033[", len);
863: else
864: strlcat(buf, "\033[", len);
865: for (i = 0; i < nnewc; i++) {
866: if (i + 1 < nnewc)
867: xsnprintf(tmp, sizeof tmp, "%d;", newc[i]);
868: else
869: xsnprintf(tmp, sizeof tmp, "%d", newc[i]);
870: strlcat(buf, tmp, len);
871: }
872: strlcat(buf, "m", len);
1.24 nicm 873: }
874:
1.39 nicm 875: /* If the background colour changed, append its parameters. */
1.24 nicm 876: nnewc = grid_string_cells_bg(gc, newc);
877: noldc = grid_string_cells_bg(lastgc, oldc);
1.70 nicm 878: if (nnewc != noldc ||
879: memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0 ||
880: (n != 0 && s[0] == 0)) {
1.26 nicm 881: if (escape_c0)
882: strlcat(buf, "\\033[", len);
883: else
884: strlcat(buf, "\033[", len);
1.70 nicm 885: for (i = 0; i < nnewc; i++) {
886: if (i + 1 < nnewc)
887: xsnprintf(tmp, sizeof tmp, "%d;", newc[i]);
1.24 nicm 888: else
1.70 nicm 889: xsnprintf(tmp, sizeof tmp, "%d", newc[i]);
1.24 nicm 890: strlcat(buf, tmp, len);
891: }
892: strlcat(buf, "m", len);
893: }
894:
895: /* Append shift in/shift out if needed. */
1.26 nicm 896: if ((attr & GRID_ATTR_CHARSET) && !(lastattr & GRID_ATTR_CHARSET)) {
897: if (escape_c0)
1.70 nicm 898: strlcat(buf, "\\016", len); /* SO */
1.26 nicm 899: else
900: strlcat(buf, "\016", len); /* SO */
901: }
902: if (!(attr & GRID_ATTR_CHARSET) && (lastattr & GRID_ATTR_CHARSET)) {
903: if (escape_c0)
1.70 nicm 904: strlcat(buf, "\\017", len); /* SI */
1.26 nicm 905: else
906: strlcat(buf, "\017", len); /* SI */
907: }
1.24 nicm 908: }
909:
1.3 nicm 910: /* Convert cells into a string. */
911: char *
1.24 nicm 912: grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx,
1.28 nicm 913: struct grid_cell **lastgc, int with_codes, int escape_c0, int trim)
1.3 nicm 914: {
1.48 nicm 915: struct grid_cell gc;
1.24 nicm 916: static struct grid_cell lastgc1;
1.38 nicm 917: const char *data;
1.24 nicm 918: char *buf, code[128];
1.26 nicm 919: size_t len, off, size, codelen;
1.16 nicm 920: u_int xx;
1.30 nicm 921: const struct grid_line *gl;
1.3 nicm 922:
1.29 nicm 923: if (lastgc != NULL && *lastgc == NULL) {
1.24 nicm 924: memcpy(&lastgc1, &grid_default_cell, sizeof lastgc1);
925: *lastgc = &lastgc1;
926: }
927:
1.3 nicm 928: len = 128;
929: buf = xmalloc(len);
930: off = 0;
931:
1.30 nicm 932: gl = grid_peek_line(gd, py);
1.3 nicm 933: for (xx = px; xx < px + nx; xx++) {
1.30 nicm 934: if (gl == NULL || xx >= gl->cellsize)
935: break;
1.48 nicm 936: grid_get_cell(gd, xx, py, &gc);
937: if (gc.flags & GRID_FLAG_PADDING)
1.3 nicm 938: continue;
939:
1.24 nicm 940: if (with_codes) {
1.48 nicm 941: grid_string_cells_code(*lastgc, &gc, code, sizeof code,
1.26 nicm 942: escape_c0);
1.24 nicm 943: codelen = strlen(code);
1.48 nicm 944: memcpy(*lastgc, &gc, sizeof **lastgc);
1.24 nicm 945: } else
946: codelen = 0;
947:
1.48 nicm 948: data = gc.data.data;
949: size = gc.data.size;
1.26 nicm 950: if (escape_c0 && size == 1 && *data == '\\') {
1.27 nicm 951: data = "\\\\";
1.26 nicm 952: size = 2;
953: }
954:
955: while (len < off + size + codelen + 1) {
1.41 nicm 956: buf = xreallocarray(buf, 2, len);
1.21 nicm 957: len *= 2;
958: }
1.3 nicm 959:
1.24 nicm 960: if (codelen != 0) {
961: memcpy(buf + off, code, codelen);
962: off += codelen;
963: }
1.26 nicm 964: memcpy(buf + off, data, size);
965: off += size;
1.3 nicm 966: }
1.17 nicm 967:
1.37 nicm 968: if (trim) {
1.28 nicm 969: while (off > 0 && buf[off - 1] == ' ')
970: off--;
1.32 nicm 971: }
1.3 nicm 972: buf[off] = '\0';
1.26 nicm 973:
1.3 nicm 974: return (buf);
1.7 nicm 975: }
976:
1.17 nicm 977: /*
1.75 nicm 978: * Duplicate a set of lines between two grids. Both source and destination
979: * should be big enough.
1.7 nicm 980: */
981: void
1.31 nicm 982: grid_duplicate_lines(struct grid *dst, u_int dy, struct grid *src, u_int sy,
983: u_int ny)
1.7 nicm 984: {
1.10 nicm 985: struct grid_line *dstl, *srcl;
986: u_int yy;
1.7 nicm 987:
988: if (dy + ny > dst->hsize + dst->sy)
989: ny = dst->hsize + dst->sy - dy;
990: if (sy + ny > src->hsize + src->sy)
991: ny = src->hsize + src->sy - sy;
1.75 nicm 992: grid_free_lines(dst, dy, ny);
1.7 nicm 993:
994: for (yy = 0; yy < ny; yy++) {
1.11 nicm 995: srcl = &src->linedata[sy];
996: dstl = &dst->linedata[dy];
1.10 nicm 997:
998: memcpy(dstl, srcl, sizeof *dstl);
999: if (srcl->cellsize != 0) {
1.42 deraadt 1000: dstl->celldata = xreallocarray(NULL,
1.10 nicm 1001: srcl->cellsize, sizeof *dstl->celldata);
1002: memcpy(dstl->celldata, srcl->celldata,
1003: srcl->cellsize * sizeof *dstl->celldata);
1.44 nicm 1004: } else
1005: dstl->celldata = NULL;
1.7 nicm 1006:
1.48 nicm 1007: if (srcl->extdsize != 0) {
1008: dstl->extdsize = srcl->extdsize;
1009: dstl->extddata = xreallocarray(NULL, dstl->extdsize,
1010: sizeof *dstl->extddata);
1011: memcpy(dstl->extddata, srcl->extddata, dstl->extdsize *
1012: sizeof *dstl->extddata);
1013: }
1014:
1.10 nicm 1015: sy++;
1016: dy++;
1.7 nicm 1017: }
1.22 nicm 1018: }
1019:
1.80 nicm 1020: /* Mark line as dead. */
1021: static void
1022: grid_reflow_dead(struct grid_line *gl)
1023: {
1024: memset(gl, 0, sizeof *gl);
1025: gl->flags = GRID_LINE_DEAD;
1026: }
1027:
1028: /* Add lines, return the first new one. */
1029: static struct grid_line *
1030: grid_reflow_add(struct grid *gd, u_int n)
1031: {
1032: struct grid_line *gl;
1033: u_int sy = gd->sy + n;
1034:
1035: gd->linedata = xreallocarray(gd->linedata, sy, sizeof *gd->linedata);
1036: gl = &gd->linedata[gd->sy];
1037: memset(gl, 0, n * (sizeof *gl));
1038: gd->sy = sy;
1039: return (gl);
1040: }
1041:
1042: /* Move a line across. */
1043: static struct grid_line *
1044: grid_reflow_move(struct grid *gd, struct grid_line *from)
1045: {
1046: struct grid_line *to;
1047:
1048: to = grid_reflow_add(gd, 1);
1049: memcpy(to, from, sizeof *to);
1050: grid_reflow_dead(from);
1051: return (to);
1052: }
1053:
1.79 nicm 1054: /* Join line below onto this one. */
1.56 nicm 1055: static void
1.80 nicm 1056: grid_reflow_join(struct grid *target, struct grid *gd, u_int sx, u_int yy,
1.91 nicm 1057: u_int width, int already)
1.48 nicm 1058: {
1.83 nicm 1059: struct grid_line *gl, *from = NULL;
1.79 nicm 1060: struct grid_cell gc;
1.83 nicm 1061: u_int lines, left, i, to, line, want = 0;
1.80 nicm 1062: u_int at;
1.79 nicm 1063: int wrapped = 1;
1.48 nicm 1064:
1.80 nicm 1065: /*
1066: * Add a new target line.
1067: */
1068: if (!already) {
1069: to = target->sy;
1070: gl = grid_reflow_move(target, &gd->linedata[yy]);
1071: } else {
1072: to = target->sy - 1;
1073: gl = &target->linedata[to];
1074: }
1075: at = gl->cellused;
1076:
1077: /*
1078: * Loop until no more to consume or the target line is full.
1079: */
1.79 nicm 1080: lines = 0;
1081: for (;;) {
1082: /*
1083: * If this is now the last line, there is nothing more to be
1084: * done.
1085: */
1.82 nicm 1086: if (yy + 1 + lines == gd->hsize + gd->sy)
1.79 nicm 1087: break;
1088: line = yy + 1 + lines;
1.48 nicm 1089:
1.79 nicm 1090: /* If the next line is empty, skip it. */
1091: if (~gd->linedata[line].flags & GRID_LINE_WRAPPED)
1092: wrapped = 0;
1093: if (gd->linedata[line].cellused == 0) {
1094: if (!wrapped)
1095: break;
1.82 nicm 1096: lines++;
1.48 nicm 1097: continue;
1.79 nicm 1098: }
1099:
1100: /*
1101: * Is the destination line now full? Copy the first character
1102: * separately because we need to leave "from" set to the last
1103: * line if this line is full.
1104: */
1105: grid_get_cell1(&gd->linedata[line], 0, &gc);
1106: if (width + gc.data.width > sx)
1107: break;
1108: width += gc.data.width;
1.80 nicm 1109: grid_set_cell(target, at, to, &gc);
1.79 nicm 1110: at++;
1111:
1112: /* Join as much more as possible onto the current line. */
1113: from = &gd->linedata[line];
1114: for (want = 1; want < from->cellused; want++) {
1115: grid_get_cell1(from, want, &gc);
1116: if (width + gc.data.width > sx)
1117: break;
1118: width += gc.data.width;
1119:
1.80 nicm 1120: grid_set_cell(target, at, to, &gc);
1.79 nicm 1121: at++;
1122: }
1123: lines++;
1124:
1125: /*
1126: * If this line wasn't wrapped or we didn't consume the entire
1127: * line, don't try to join any further lines.
1128: */
1129: if (!wrapped || want != from->cellused || width == sx)
1130: break;
1131: }
1132: if (lines == 0)
1133: return;
1.48 nicm 1134:
1.79 nicm 1135: /*
1136: * If we didn't consume the entire final line, then remove what we did
1137: * consume. If we consumed the entire line and it wasn't wrapped,
1138: * remove the wrap flag from this line.
1139: */
1140: left = from->cellused - want;
1141: if (left != 0) {
1142: grid_move_cells(gd, 0, want, yy + lines, left, 8);
1143: from->cellsize = from->cellused = left;
1144: lines--;
1145: } else if (!wrapped)
1146: gl->flags &= ~GRID_LINE_WRAPPED;
1147:
1148: /* Remove the lines that were completely consumed. */
1.80 nicm 1149: for (i = yy + 1; i < yy + 1 + lines; i++) {
1150: free(gd->linedata[i].celldata);
1151: free(gd->linedata[i].extddata);
1152: grid_reflow_dead(&gd->linedata[i]);
1.48 nicm 1153: }
1.79 nicm 1154:
1.91 nicm 1155: /* Adjust scroll position. */
1.80 nicm 1156: if (gd->hscrolled > to + lines)
1.79 nicm 1157: gd->hscrolled -= lines;
1.80 nicm 1158: else if (gd->hscrolled > to)
1159: gd->hscrolled = to;
1.48 nicm 1160: }
1161:
1.79 nicm 1162: /* Split this line into several new ones */
1.56 nicm 1163: static void
1.80 nicm 1164: grid_reflow_split(struct grid *target, struct grid *gd, u_int sx, u_int yy,
1.91 nicm 1165: u_int at)
1.23 nicm 1166: {
1.80 nicm 1167: struct grid_line *gl = &gd->linedata[yy], *first;
1.79 nicm 1168: struct grid_cell gc;
1.80 nicm 1169: u_int line, lines, width, i, xx;
1170: u_int used = gl->cellused;
1.79 nicm 1171: int flags = gl->flags;
1.23 nicm 1172:
1.80 nicm 1173: /* How many lines do we need to insert? We know we need at least two. */
1.79 nicm 1174: if (~gl->flags & GRID_LINE_EXTENDED)
1.80 nicm 1175: lines = 1 + (gl->cellused - 1) / sx;
1.79 nicm 1176: else {
1.80 nicm 1177: lines = 2;
1.79 nicm 1178: width = 0;
1179: for (i = at; i < used; i++) {
1180: grid_get_cell1(gl, i, &gc);
1181: if (width + gc.data.width > sx) {
1182: lines++;
1183: width = 0;
1184: }
1185: width += gc.data.width;
1186: }
1187: }
1.23 nicm 1188:
1.79 nicm 1189: /* Insert new lines. */
1.80 nicm 1190: line = target->sy + 1;
1191: first = grid_reflow_add(target, lines);
1.79 nicm 1192:
1193: /* Copy sections from the original line. */
1194: width = 0;
1195: xx = 0;
1196: for (i = at; i < used; i++) {
1197: grid_get_cell1(gl, i, &gc);
1198: if (width + gc.data.width > sx) {
1.80 nicm 1199: target->linedata[line].flags |= GRID_LINE_WRAPPED;
1.79 nicm 1200:
1201: line++;
1202: width = 0;
1203: xx = 0;
1204: }
1205: width += gc.data.width;
1.80 nicm 1206: grid_set_cell(target, xx, line, &gc);
1.79 nicm 1207: xx++;
1208: }
1209: if (flags & GRID_LINE_WRAPPED)
1.80 nicm 1210: target->linedata[line].flags |= GRID_LINE_WRAPPED;
1211:
1212: /* Move the remainder of the original line. */
1213: gl->cellsize = gl->cellused = at;
1214: gl->flags |= GRID_LINE_WRAPPED;
1215: memcpy(first, gl, sizeof *first);
1216: grid_reflow_dead(gl);
1.79 nicm 1217:
1.91 nicm 1218: /* Adjust the scroll position. */
1.79 nicm 1219: if (yy <= gd->hscrolled)
1.80 nicm 1220: gd->hscrolled += lines - 1;
1.23 nicm 1221:
1.79 nicm 1222: /*
1223: * If the original line had the wrapped flag and there is still space
1224: * in the last new line, try to join with the next lines.
1225: */
1226: if (width < sx && (flags & GRID_LINE_WRAPPED))
1.91 nicm 1227: grid_reflow_join(target, gd, sx, yy, width, 1);
1.23 nicm 1228: }
1229:
1.79 nicm 1230: /* Reflow lines on grid to new width. */
1231: void
1.91 nicm 1232: grid_reflow(struct grid *gd, u_int sx)
1.23 nicm 1233: {
1.80 nicm 1234: struct grid *target;
1.79 nicm 1235: struct grid_line *gl;
1236: struct grid_cell gc;
1.91 nicm 1237: u_int yy, width, i, at, first;
1.23 nicm 1238:
1.79 nicm 1239: /*
1.80 nicm 1240: * Create a destination grid. This is just used as a container for the
1241: * line data and may not be fully valid.
1242: */
1243: target = grid_create(gd->sx, 0, 0);
1244:
1245: /*
1246: * Loop over each source line.
1.79 nicm 1247: */
1248: for (yy = 0; yy < gd->hsize + gd->sy; yy++) {
1249: gl = &gd->linedata[yy];
1.80 nicm 1250: if (gl->flags & GRID_LINE_DEAD)
1251: continue;
1.23 nicm 1252:
1.80 nicm 1253: /*
1254: * Work out the width of this line. first is the width of the
1255: * first character, at is the point at which the available
1256: * width is hit, and width is the full line width.
1257: */
1.79 nicm 1258: first = at = width = 0;
1259: if (~gl->flags & GRID_LINE_EXTENDED) {
1260: first = 1;
1261: width = gl->cellused;
1262: if (width > sx)
1263: at = sx;
1264: else
1265: at = width;
1266: } else {
1267: for (i = 0; i < gl->cellused; i++) {
1268: grid_get_cell1(gl, i, &gc);
1269: if (i == 0)
1270: first = gc.data.width;
1271: if (at == 0 && width + gc.data.width > sx)
1272: at = i;
1273: width += gc.data.width;
1274: }
1275: }
1.23 nicm 1276:
1.79 nicm 1277: /*
1.80 nicm 1278: * If the line is exactly right or the first character is wider
1279: * than the targe width, just move it across unchanged.
1.79 nicm 1280: */
1.80 nicm 1281: if (width == sx || first > sx) {
1282: grid_reflow_move(target, gl);
1.79 nicm 1283: continue;
1.80 nicm 1284: }
1.23 nicm 1285:
1.79 nicm 1286: /*
1287: * If the line is too big, it needs to be split, whether or not
1288: * it was previously wrapped.
1289: */
1290: if (width > sx) {
1.91 nicm 1291: grid_reflow_split(target, gd, sx, yy, at);
1.79 nicm 1292: continue;
1293: }
1.23 nicm 1294:
1.79 nicm 1295: /*
1296: * If the line was previously wrapped, join as much as possible
1297: * of the next line.
1298: */
1299: if (gl->flags & GRID_LINE_WRAPPED)
1.91 nicm 1300: grid_reflow_join(target, gd, sx, yy, width, 0);
1.80 nicm 1301: else
1302: grid_reflow_move(target, gl);
1303: }
1.55 nicm 1304:
1.80 nicm 1305: /*
1306: * Replace the old grid with the new.
1307: */
1308: if (target->sy < gd->sy)
1309: grid_reflow_add(target, gd->sy - target->sy);
1310: gd->hsize = target->sy - gd->sy;
1.92 nicm 1311: if (gd->hscrolled > gd->hsize)
1312: gd->hscrolled = gd->hsize;
1.80 nicm 1313: free(gd->linedata);
1314: gd->linedata = target->linedata;
1315: free(target);
1.91 nicm 1316: }
1.22 nicm 1317:
1.93 nicm 1318: /* Convert to position based on wrapped lines. */
1319: void
1320: grid_wrap_position(struct grid *gd, u_int px, u_int py, u_int *wx, u_int *wy)
1.91 nicm 1321: {
1.93 nicm 1322: u_int ax = 0, ay = 0, yy;
1.91 nicm 1323:
1.93 nicm 1324: for (yy = 0; yy < py; yy++) {
1325: if (gd->linedata[yy].flags & GRID_LINE_WRAPPED)
1326: ax += gd->linedata[yy].cellused;
1327: else {
1328: ax = 0;
1329: ay++;
1330: }
1.91 nicm 1331: }
1.93 nicm 1332: if (px >= gd->linedata[yy].cellused)
1333: ax = UINT_MAX;
1334: else
1335: ax += px;
1336: *wx = ax;
1337: *wy = ay;
1.91 nicm 1338: }
1339:
1.93 nicm 1340: /* Convert position based on wrapped lines back. */
1.91 nicm 1341: void
1.93 nicm 1342: grid_unwrap_position(struct grid *gd, u_int *px, u_int *py, u_int wx, u_int wy)
1.91 nicm 1343: {
1.105 nicm 1344: u_int yy, ay = 0;
1.91 nicm 1345:
1346: for (yy = 0; yy < gd->hsize + gd->sy - 1; yy++) {
1.93 nicm 1347: if (ay == wy)
1.91 nicm 1348: break;
1.105 nicm 1349: if (~gd->linedata[yy].flags & GRID_LINE_WRAPPED)
1.93 nicm 1350: ay++;
1.91 nicm 1351: }
1.93 nicm 1352:
1353: /*
1354: * yy is now 0 on the unwrapped line which contains wx. Walk forwards
1355: * until we find the end or the line now containing wx.
1356: */
1357: if (wx == UINT_MAX) {
1358: while (gd->linedata[yy].flags & GRID_LINE_WRAPPED)
1359: yy++;
1360: wx = gd->linedata[yy].cellused;
1361: } else {
1362: while (gd->linedata[yy].flags & GRID_LINE_WRAPPED) {
1363: if (wx < gd->linedata[yy].cellused)
1364: break;
1365: wx -= gd->linedata[yy].cellused;
1366: yy++;
1367: }
1368: }
1369: *px = wx;
1.91 nicm 1370: *py = yy;
1.95 nicm 1371: }
1372:
1373: /* Get length of line. */
1374: u_int
1375: grid_line_length(struct grid *gd, u_int py)
1376: {
1377: struct grid_cell gc;
1378: u_int px;
1379:
1380: px = grid_get_line(gd, py)->cellsize;
1381: if (px > gd->sx)
1382: px = gd->sx;
1383: while (px > 0) {
1384: grid_get_cell(gd, px - 1, py, &gc);
1385: if (gc.data.size != 1 || *gc.data.data != ' ')
1386: break;
1387: px--;
1388: }
1389: return (px);
1.1 nicm 1390: }