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